Lu Baolu | 5628317 | 2018-07-14 15:46:54 +0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | /* |
Lu Baolu | 02f3eff | 2020-07-24 09:49:25 +0800 | [diff] [blame] | 3 | * pasid.h - PASID idr, table and entry header |
Lu Baolu | 5628317 | 2018-07-14 15:46:54 +0800 | [diff] [blame] | 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 Baolu | ef848b7 | 2018-12-10 09:59:01 +0800 | [diff] [blame] | 13 | #define PASID_RID2PASID 0x0 |
Lu Baolu | 5628317 | 2018-07-14 15:46:54 +0800 | [diff] [blame] | 14 | #define PASID_MIN 0x1 |
Lu Baolu | 0bbeb01 | 2018-12-10 09:58:56 +0800 | [diff] [blame] | 15 | #define PASID_MAX 0x100000 |
| 16 | #define PASID_PTE_MASK 0x3F |
| 17 | #define PASID_PTE_PRESENT 1 |
Lu Baolu | 37e91bd | 2020-05-16 14:20:57 +0800 | [diff] [blame] | 18 | #define PASID_PTE_FPD 2 |
Lu Baolu | 0bbeb01 | 2018-12-10 09:58:56 +0800 | [diff] [blame] | 19 | #define PDE_PFN_MASK PAGE_MASK |
| 20 | #define PASID_PDE_SHIFT 6 |
Lu Baolu | 7373a8c | 2018-12-10 09:59:03 +0800 | [diff] [blame] | 21 | #define MAX_NR_PASID_BITS 20 |
Sai Praneeth Prakhya | cdd3a24 | 2019-05-24 16:40:16 -0700 | [diff] [blame] | 22 | #define PASID_TBL_ENTRIES BIT(PASID_PDE_SHIFT) |
| 23 | |
| 24 | #define is_pasid_enabled(entry) (((entry)->lo >> 3) & 0x1) |
| 25 | #define get_pasid_dir_size(entry) (1 << ((((entry)->lo >> 9) & 0x7) + 7)) |
Lu Baolu | 0bbeb01 | 2018-12-10 09:58:56 +0800 | [diff] [blame] | 26 | |
Lu Baolu | 24f27d3 | 2020-05-16 14:20:50 +0800 | [diff] [blame] | 27 | /* Virtual command interface for enlightened pasid management. */ |
| 28 | #define VCMD_CMD_ALLOC 0x1 |
| 29 | #define VCMD_CMD_FREE 0x2 |
| 30 | #define VCMD_VRSP_IP 0x1 |
| 31 | #define VCMD_VRSP_SC(e) (((e) >> 1) & 0x3) |
| 32 | #define VCMD_VRSP_SC_SUCCESS 0 |
| 33 | #define VCMD_VRSP_SC_NO_PASID_AVAIL 1 |
| 34 | #define VCMD_VRSP_SC_INVALID_PASID 1 |
| 35 | #define VCMD_VRSP_RESULT_PASID(e) (((e) >> 8) & 0xfffff) |
| 36 | #define VCMD_CMD_OPERAND(e) ((e) << 8) |
Lu Baolu | 3b33d4a | 2018-12-10 09:58:59 +0800 | [diff] [blame] | 37 | /* |
| 38 | * Domain ID reserved for pasid entries programmed for first-level |
| 39 | * only and pass-through transfer modes. |
| 40 | */ |
| 41 | #define FLPT_DEFAULT_DID 1 |
| 42 | |
Lu Baolu | 437f35e | 2018-12-10 09:59:04 +0800 | [diff] [blame] | 43 | /* |
| 44 | * The SUPERVISOR_MODE flag indicates a first level translation which |
| 45 | * can be used for access to kernel addresses. It is valid only for |
| 46 | * access to the kernel's static 1:1 mapping of physical memory — not |
| 47 | * to vmalloc or even module mappings. |
| 48 | */ |
| 49 | #define PASID_FLAG_SUPERVISOR_MODE BIT(0) |
Jacob Pan | b0d1f87 | 2020-05-16 14:20:46 +0800 | [diff] [blame] | 50 | #define PASID_FLAG_NESTED BIT(1) |
Lu Baolu | 437f35e | 2018-12-10 09:59:04 +0800 | [diff] [blame] | 51 | |
Lu Baolu | 87208f2 | 2020-01-02 08:18:16 +0800 | [diff] [blame] | 52 | /* |
| 53 | * The PASID_FLAG_FL5LP flag Indicates using 5-level paging for first- |
| 54 | * level translation, otherwise, 4-level paging will be used. |
| 55 | */ |
| 56 | #define PASID_FLAG_FL5LP BIT(1) |
| 57 | |
Lu Baolu | 0bbeb01 | 2018-12-10 09:58:56 +0800 | [diff] [blame] | 58 | struct pasid_dir_entry { |
| 59 | u64 val; |
| 60 | }; |
Lu Baolu | 5628317 | 2018-07-14 15:46:54 +0800 | [diff] [blame] | 61 | |
Lu Baolu | cc580e4 | 2018-07-14 15:46:59 +0800 | [diff] [blame] | 62 | struct pasid_entry { |
Lu Baolu | 0bbeb01 | 2018-12-10 09:58:56 +0800 | [diff] [blame] | 63 | u64 val[8]; |
Lu Baolu | cc580e4 | 2018-07-14 15:46:59 +0800 | [diff] [blame] | 64 | }; |
| 65 | |
Jacob Pan | b0d1f87 | 2020-05-16 14:20:46 +0800 | [diff] [blame] | 66 | #define PASID_ENTRY_PGTT_FL_ONLY (1) |
| 67 | #define PASID_ENTRY_PGTT_SL_ONLY (2) |
| 68 | #define PASID_ENTRY_PGTT_NESTED (3) |
| 69 | #define PASID_ENTRY_PGTT_PT (4) |
| 70 | |
Lu Baolu | cc580e4 | 2018-07-14 15:46:59 +0800 | [diff] [blame] | 71 | /* The representative of a PASID table */ |
| 72 | struct pasid_table { |
| 73 | void *table; /* pasid table pointer */ |
| 74 | int order; /* page order of pasid table */ |
| 75 | int max_pasid; /* max pasid */ |
| 76 | struct list_head dev; /* device list */ |
| 77 | }; |
| 78 | |
Sai Praneeth Prakhya | cdd3a24 | 2019-05-24 16:40:16 -0700 | [diff] [blame] | 79 | /* Get PRESENT bit of a PASID directory entry. */ |
| 80 | static inline bool pasid_pde_is_present(struct pasid_dir_entry *pde) |
| 81 | { |
| 82 | return READ_ONCE(pde->val) & PASID_PTE_PRESENT; |
| 83 | } |
| 84 | |
| 85 | /* Get PASID table from a PASID directory entry. */ |
| 86 | static inline struct pasid_entry * |
| 87 | get_pasid_table_from_pde(struct pasid_dir_entry *pde) |
| 88 | { |
| 89 | if (!pasid_pde_is_present(pde)) |
| 90 | return NULL; |
| 91 | |
| 92 | return phys_to_virt(READ_ONCE(pde->val) & PDE_PFN_MASK); |
| 93 | } |
| 94 | |
| 95 | /* Get PRESENT bit of a PASID table entry. */ |
| 96 | static inline bool pasid_pte_is_present(struct pasid_entry *pte) |
| 97 | { |
| 98 | return READ_ONCE(pte->val[0]) & PASID_PTE_PRESENT; |
| 99 | } |
| 100 | |
Lu Baolu | 5628317 | 2018-07-14 15:46:54 +0800 | [diff] [blame] | 101 | extern u32 intel_pasid_max_id; |
| 102 | int intel_pasid_alloc_id(void *ptr, int start, int end, gfp_t gfp); |
| 103 | void intel_pasid_free_id(int pasid); |
| 104 | void *intel_pasid_lookup_id(int pasid); |
Lu Baolu | cc580e4 | 2018-07-14 15:46:59 +0800 | [diff] [blame] | 105 | int intel_pasid_alloc_table(struct device *dev); |
| 106 | void intel_pasid_free_table(struct device *dev); |
| 107 | struct pasid_table *intel_pasid_get_table(struct device *dev); |
| 108 | int intel_pasid_get_dev_max_id(struct device *dev); |
| 109 | struct pasid_entry *intel_pasid_get_entry(struct device *dev, int pasid); |
Lu Baolu | 437f35e | 2018-12-10 09:59:04 +0800 | [diff] [blame] | 110 | int intel_pasid_setup_first_level(struct intel_iommu *iommu, |
| 111 | struct device *dev, pgd_t *pgd, |
| 112 | int pasid, u16 did, int flags); |
Lu Baolu | 6f7db75 | 2018-12-10 09:59:00 +0800 | [diff] [blame] | 113 | int intel_pasid_setup_second_level(struct intel_iommu *iommu, |
| 114 | struct dmar_domain *domain, |
| 115 | struct device *dev, int pasid); |
| 116 | int intel_pasid_setup_pass_through(struct intel_iommu *iommu, |
| 117 | struct dmar_domain *domain, |
| 118 | struct device *dev, int pasid); |
Jacob Pan | b0d1f87 | 2020-05-16 14:20:46 +0800 | [diff] [blame] | 119 | int intel_pasid_setup_nested(struct intel_iommu *iommu, |
| 120 | struct device *dev, pgd_t *pgd, int pasid, |
| 121 | struct iommu_gpasid_bind_data_vtd *pasid_data, |
| 122 | struct dmar_domain *domain, int addr_width); |
Lu Baolu | 6f7db75 | 2018-12-10 09:59:00 +0800 | [diff] [blame] | 123 | void intel_pasid_tear_down_entry(struct intel_iommu *iommu, |
Lu Baolu | 37e91bd | 2020-05-16 14:20:57 +0800 | [diff] [blame] | 124 | struct device *dev, int pasid, |
| 125 | bool fault_ignore); |
Lu Baolu | 24f27d3 | 2020-05-16 14:20:50 +0800 | [diff] [blame] | 126 | int vcmd_alloc_pasid(struct intel_iommu *iommu, unsigned int *pasid); |
| 127 | void vcmd_free_pasid(struct intel_iommu *iommu, unsigned int pasid); |
Lu Baolu | 5628317 | 2018-07-14 15:46:54 +0800 | [diff] [blame] | 128 | #endif /* __INTEL_PASID_H */ |