blob: ccd50c2ae75c5f9a829317eeff2be907aed7bc33 [file] [log] [blame]
Lu Baolu56283172018-07-14 15:46:54 +08001/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * intel-pasid.h - PASID idr, table and entry header
4 *
5 * Copyright (C) 2018 Intel Corporation
6 *
7 * Author: Lu Baolu <baolu.lu@linux.intel.com>
8 */
9
10#ifndef __INTEL_PASID_H
11#define __INTEL_PASID_H
12
Lu Baoluef848b72018-12-10 09:59:01 +080013#define PASID_RID2PASID 0x0
Lu Baolu56283172018-07-14 15:46:54 +080014#define PASID_MIN 0x1
Lu Baolu0bbeb012018-12-10 09:58:56 +080015#define PASID_MAX 0x100000
16#define PASID_PTE_MASK 0x3F
17#define PASID_PTE_PRESENT 1
18#define PDE_PFN_MASK PAGE_MASK
19#define PASID_PDE_SHIFT 6
Lu Baolu7373a8c2018-12-10 09:59:03 +080020#define MAX_NR_PASID_BITS 20
Sai Praneeth Prakhyacdd3a242019-05-24 16:40:16 -070021#define PASID_TBL_ENTRIES BIT(PASID_PDE_SHIFT)
22
23#define is_pasid_enabled(entry) (((entry)->lo >> 3) & 0x1)
24#define get_pasid_dir_size(entry) (1 << ((((entry)->lo >> 9) & 0x7) + 7))
Lu Baolu0bbeb012018-12-10 09:58:56 +080025
Lu Baolu3b33d4a2018-12-10 09:58:59 +080026/*
27 * Domain ID reserved for pasid entries programmed for first-level
28 * only and pass-through transfer modes.
29 */
30#define FLPT_DEFAULT_DID 1
31
Lu Baolu437f35e2018-12-10 09:59:04 +080032/*
33 * The SUPERVISOR_MODE flag indicates a first level translation which
34 * can be used for access to kernel addresses. It is valid only for
35 * access to the kernel's static 1:1 mapping of physical memory — not
36 * to vmalloc or even module mappings.
37 */
38#define PASID_FLAG_SUPERVISOR_MODE BIT(0)
Jacob Panb0d1f872020-05-16 14:20:46 +080039#define PASID_FLAG_NESTED BIT(1)
Lu Baolu437f35e2018-12-10 09:59:04 +080040
Lu Baolu87208f22020-01-02 08:18:16 +080041/*
42 * The PASID_FLAG_FL5LP flag Indicates using 5-level paging for first-
43 * level translation, otherwise, 4-level paging will be used.
44 */
45#define PASID_FLAG_FL5LP BIT(1)
46
Lu Baolu0bbeb012018-12-10 09:58:56 +080047struct pasid_dir_entry {
48 u64 val;
49};
Lu Baolu56283172018-07-14 15:46:54 +080050
Lu Baolucc580e42018-07-14 15:46:59 +080051struct pasid_entry {
Lu Baolu0bbeb012018-12-10 09:58:56 +080052 u64 val[8];
Lu Baolucc580e42018-07-14 15:46:59 +080053};
54
Jacob Panb0d1f872020-05-16 14:20:46 +080055#define PASID_ENTRY_PGTT_FL_ONLY (1)
56#define PASID_ENTRY_PGTT_SL_ONLY (2)
57#define PASID_ENTRY_PGTT_NESTED (3)
58#define PASID_ENTRY_PGTT_PT (4)
59
Lu Baolucc580e42018-07-14 15:46:59 +080060/* The representative of a PASID table */
61struct pasid_table {
62 void *table; /* pasid table pointer */
63 int order; /* page order of pasid table */
64 int max_pasid; /* max pasid */
65 struct list_head dev; /* device list */
66};
67
Sai Praneeth Prakhyacdd3a242019-05-24 16:40:16 -070068/* Get PRESENT bit of a PASID directory entry. */
69static inline bool pasid_pde_is_present(struct pasid_dir_entry *pde)
70{
71 return READ_ONCE(pde->val) & PASID_PTE_PRESENT;
72}
73
74/* Get PASID table from a PASID directory entry. */
75static inline struct pasid_entry *
76get_pasid_table_from_pde(struct pasid_dir_entry *pde)
77{
78 if (!pasid_pde_is_present(pde))
79 return NULL;
80
81 return phys_to_virt(READ_ONCE(pde->val) & PDE_PFN_MASK);
82}
83
84/* Get PRESENT bit of a PASID table entry. */
85static inline bool pasid_pte_is_present(struct pasid_entry *pte)
86{
87 return READ_ONCE(pte->val[0]) & PASID_PTE_PRESENT;
88}
89
Lu Baolu56283172018-07-14 15:46:54 +080090extern u32 intel_pasid_max_id;
91int intel_pasid_alloc_id(void *ptr, int start, int end, gfp_t gfp);
92void intel_pasid_free_id(int pasid);
93void *intel_pasid_lookup_id(int pasid);
Lu Baolucc580e42018-07-14 15:46:59 +080094int intel_pasid_alloc_table(struct device *dev);
95void intel_pasid_free_table(struct device *dev);
96struct pasid_table *intel_pasid_get_table(struct device *dev);
97int intel_pasid_get_dev_max_id(struct device *dev);
98struct pasid_entry *intel_pasid_get_entry(struct device *dev, int pasid);
Lu Baolu437f35e2018-12-10 09:59:04 +080099int intel_pasid_setup_first_level(struct intel_iommu *iommu,
100 struct device *dev, pgd_t *pgd,
101 int pasid, u16 did, int flags);
Lu Baolu6f7db752018-12-10 09:59:00 +0800102int intel_pasid_setup_second_level(struct intel_iommu *iommu,
103 struct dmar_domain *domain,
104 struct device *dev, int pasid);
105int intel_pasid_setup_pass_through(struct intel_iommu *iommu,
106 struct dmar_domain *domain,
107 struct device *dev, int pasid);
Jacob Panb0d1f872020-05-16 14:20:46 +0800108int intel_pasid_setup_nested(struct intel_iommu *iommu,
109 struct device *dev, pgd_t *pgd, int pasid,
110 struct iommu_gpasid_bind_data_vtd *pasid_data,
111 struct dmar_domain *domain, int addr_width);
Lu Baolu6f7db752018-12-10 09:59:00 +0800112void intel_pasid_tear_down_entry(struct intel_iommu *iommu,
113 struct device *dev, int pasid);
Lu Baolu56283172018-07-14 15:46:54 +0800114
115#endif /* __INTEL_PASID_H */