Will Deacon | fdb1d7b | 2014-11-14 17:16:49 +0000 | [diff] [blame] | 1 | #ifndef __IO_PGTABLE_H |
| 2 | #define __IO_PGTABLE_H |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 3 | #include <linux/bitops.h> |
Will Deacon | fdb1d7b | 2014-11-14 17:16:49 +0000 | [diff] [blame] | 4 | |
| 5 | /* |
| 6 | * Public API for use by IOMMU drivers |
| 7 | */ |
| 8 | enum io_pgtable_fmt { |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 9 | ARM_32_LPAE_S1, |
| 10 | ARM_32_LPAE_S2, |
| 11 | ARM_64_LPAE_S1, |
| 12 | ARM_64_LPAE_S2, |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 13 | ARM_V7S, |
Will Deacon | fdb1d7b | 2014-11-14 17:16:49 +0000 | [diff] [blame] | 14 | IO_PGTABLE_NUM_FMTS, |
| 15 | }; |
| 16 | |
| 17 | /** |
| 18 | * struct iommu_gather_ops - IOMMU callbacks for TLB and page table management. |
| 19 | * |
| 20 | * @tlb_flush_all: Synchronously invalidate the entire TLB context. |
| 21 | * @tlb_add_flush: Queue up a TLB invalidation for a virtual address range. |
Robin Murphy | 87a91b1 | 2015-07-29 19:46:09 +0100 | [diff] [blame] | 22 | * @tlb_sync: Ensure any queued TLB invalidation has taken effect, and |
| 23 | * any corresponding page table updates are visible to the |
| 24 | * IOMMU. |
Will Deacon | fdb1d7b | 2014-11-14 17:16:49 +0000 | [diff] [blame] | 25 | * |
| 26 | * Note that these can all be called in atomic context and must therefore |
| 27 | * not block. |
| 28 | */ |
| 29 | struct iommu_gather_ops { |
| 30 | void (*tlb_flush_all)(void *cookie); |
Robin Murphy | 06c610e | 2015-12-07 18:18:53 +0000 | [diff] [blame] | 31 | void (*tlb_add_flush)(unsigned long iova, size_t size, size_t granule, |
| 32 | bool leaf, void *cookie); |
Will Deacon | fdb1d7b | 2014-11-14 17:16:49 +0000 | [diff] [blame] | 33 | void (*tlb_sync)(void *cookie); |
Will Deacon | fdb1d7b | 2014-11-14 17:16:49 +0000 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | /** |
| 37 | * struct io_pgtable_cfg - Configuration data for a set of page tables. |
| 38 | * |
| 39 | * @quirks: A bitmap of hardware quirks that require some special |
| 40 | * action by the low-level page table allocator. |
| 41 | * @pgsize_bitmap: A bitmap of page sizes supported by this set of page |
| 42 | * tables. |
| 43 | * @ias: Input address (iova) size, in bits. |
| 44 | * @oas: Output address (paddr) size, in bits. |
| 45 | * @tlb: TLB management callbacks for this set of tables. |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 46 | * @iommu_dev: The device representing the DMA configuration for the |
| 47 | * page table walker. |
Will Deacon | fdb1d7b | 2014-11-14 17:16:49 +0000 | [diff] [blame] | 48 | */ |
| 49 | struct io_pgtable_cfg { |
Robin Murphy | 3850db4 | 2016-02-12 17:09:46 +0000 | [diff] [blame] | 50 | /* |
| 51 | * IO_PGTABLE_QUIRK_ARM_NS: (ARM formats) Set NS and NSTABLE bits in |
| 52 | * stage 1 PTEs, for hardware which insists on validating them |
| 53 | * even in non-secure state where they should normally be ignored. |
| 54 | * |
| 55 | * IO_PGTABLE_QUIRK_NO_PERMS: Ignore the IOMMU_READ, IOMMU_WRITE and |
| 56 | * IOMMU_NOEXEC flags and map everything with full access, for |
| 57 | * hardware which does not implement the permissions of a given |
| 58 | * format, and/or requires some format-specific default value. |
| 59 | * |
| 60 | * IO_PGTABLE_QUIRK_TLBI_ON_MAP: If the format forbids caching invalid |
| 61 | * (unmapped) entries but the hardware might do so anyway, perform |
| 62 | * TLB maintenance when mapping as well as when unmapping. |
| 63 | */ |
| 64 | #define IO_PGTABLE_QUIRK_ARM_NS BIT(0) |
| 65 | #define IO_PGTABLE_QUIRK_NO_PERMS BIT(1) |
| 66 | #define IO_PGTABLE_QUIRK_TLBI_ON_MAP BIT(2) |
| 67 | unsigned long quirks; |
Will Deacon | fdb1d7b | 2014-11-14 17:16:49 +0000 | [diff] [blame] | 68 | unsigned long pgsize_bitmap; |
| 69 | unsigned int ias; |
| 70 | unsigned int oas; |
| 71 | const struct iommu_gather_ops *tlb; |
Robin Murphy | f8d5496 | 2015-07-29 19:46:04 +0100 | [diff] [blame] | 72 | struct device *iommu_dev; |
Will Deacon | fdb1d7b | 2014-11-14 17:16:49 +0000 | [diff] [blame] | 73 | |
| 74 | /* Low-level data specific to the table format */ |
| 75 | union { |
Will Deacon | e1d3c0f | 2014-11-14 17:18:23 +0000 | [diff] [blame] | 76 | struct { |
| 77 | u64 ttbr[2]; |
| 78 | u64 tcr; |
| 79 | u64 mair[2]; |
| 80 | } arm_lpae_s1_cfg; |
| 81 | |
| 82 | struct { |
| 83 | u64 vttbr; |
| 84 | u64 vtcr; |
| 85 | } arm_lpae_s2_cfg; |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 86 | |
| 87 | struct { |
| 88 | u32 ttbr[2]; |
| 89 | u32 tcr; |
| 90 | u32 nmrr; |
| 91 | u32 prrr; |
| 92 | } arm_v7s_cfg; |
Will Deacon | fdb1d7b | 2014-11-14 17:16:49 +0000 | [diff] [blame] | 93 | }; |
| 94 | }; |
| 95 | |
| 96 | /** |
| 97 | * struct io_pgtable_ops - Page table manipulation API for IOMMU drivers. |
| 98 | * |
| 99 | * @map: Map a physically contiguous memory region. |
| 100 | * @unmap: Unmap a physically contiguous memory region. |
| 101 | * @iova_to_phys: Translate iova to physical address. |
| 102 | * |
| 103 | * These functions map directly onto the iommu_ops member functions with |
| 104 | * the same names. |
| 105 | */ |
| 106 | struct io_pgtable_ops { |
| 107 | int (*map)(struct io_pgtable_ops *ops, unsigned long iova, |
| 108 | phys_addr_t paddr, size_t size, int prot); |
| 109 | int (*unmap)(struct io_pgtable_ops *ops, unsigned long iova, |
| 110 | size_t size); |
| 111 | phys_addr_t (*iova_to_phys)(struct io_pgtable_ops *ops, |
| 112 | unsigned long iova); |
| 113 | }; |
| 114 | |
| 115 | /** |
| 116 | * alloc_io_pgtable_ops() - Allocate a page table allocator for use by an IOMMU. |
| 117 | * |
| 118 | * @fmt: The page table format. |
| 119 | * @cfg: The page table configuration. This will be modified to represent |
| 120 | * the configuration actually provided by the allocator (e.g. the |
| 121 | * pgsize_bitmap may be restricted). |
| 122 | * @cookie: An opaque token provided by the IOMMU driver and passed back to |
| 123 | * the callback routines in cfg->tlb. |
| 124 | */ |
| 125 | struct io_pgtable_ops *alloc_io_pgtable_ops(enum io_pgtable_fmt fmt, |
| 126 | struct io_pgtable_cfg *cfg, |
| 127 | void *cookie); |
| 128 | |
| 129 | /** |
| 130 | * free_io_pgtable_ops() - Free an io_pgtable_ops structure. The caller |
| 131 | * *must* ensure that the page table is no longer |
| 132 | * live, but the TLB can be dirty. |
| 133 | * |
| 134 | * @ops: The ops returned from alloc_io_pgtable_ops. |
| 135 | */ |
| 136 | void free_io_pgtable_ops(struct io_pgtable_ops *ops); |
| 137 | |
| 138 | |
| 139 | /* |
| 140 | * Internal structures for page table allocator implementations. |
| 141 | */ |
| 142 | |
| 143 | /** |
| 144 | * struct io_pgtable - Internal structure describing a set of page tables. |
| 145 | * |
| 146 | * @fmt: The page table format. |
| 147 | * @cookie: An opaque token provided by the IOMMU driver and passed back to |
| 148 | * any callback routines. |
Robin Murphy | 88492a4 | 2016-01-26 17:13:15 +0000 | [diff] [blame] | 149 | * @tlb_sync_pending: Private flag for optimising out redundant syncs. |
Will Deacon | fdb1d7b | 2014-11-14 17:16:49 +0000 | [diff] [blame] | 150 | * @cfg: A copy of the page table configuration. |
| 151 | * @ops: The page table operations in use for this set of page tables. |
| 152 | */ |
| 153 | struct io_pgtable { |
| 154 | enum io_pgtable_fmt fmt; |
| 155 | void *cookie; |
Robin Murphy | 88492a4 | 2016-01-26 17:13:15 +0000 | [diff] [blame] | 156 | bool tlb_sync_pending; |
Will Deacon | fdb1d7b | 2014-11-14 17:16:49 +0000 | [diff] [blame] | 157 | struct io_pgtable_cfg cfg; |
| 158 | struct io_pgtable_ops ops; |
| 159 | }; |
| 160 | |
Robin Murphy | fdc3896 | 2015-12-04 17:53:01 +0000 | [diff] [blame] | 161 | #define io_pgtable_ops_to_pgtable(x) container_of((x), struct io_pgtable, ops) |
| 162 | |
Robin Murphy | 507e4c9 | 2016-01-26 17:13:14 +0000 | [diff] [blame] | 163 | static inline void io_pgtable_tlb_flush_all(struct io_pgtable *iop) |
| 164 | { |
| 165 | iop->cfg.tlb->tlb_flush_all(iop->cookie); |
Robin Murphy | 88492a4 | 2016-01-26 17:13:15 +0000 | [diff] [blame] | 166 | iop->tlb_sync_pending = true; |
Robin Murphy | 507e4c9 | 2016-01-26 17:13:14 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static inline void io_pgtable_tlb_add_flush(struct io_pgtable *iop, |
| 170 | unsigned long iova, size_t size, size_t granule, bool leaf) |
| 171 | { |
| 172 | iop->cfg.tlb->tlb_add_flush(iova, size, granule, leaf, iop->cookie); |
Robin Murphy | 88492a4 | 2016-01-26 17:13:15 +0000 | [diff] [blame] | 173 | iop->tlb_sync_pending = true; |
Robin Murphy | 507e4c9 | 2016-01-26 17:13:14 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | static inline void io_pgtable_tlb_sync(struct io_pgtable *iop) |
| 177 | { |
Robin Murphy | 88492a4 | 2016-01-26 17:13:15 +0000 | [diff] [blame] | 178 | if (iop->tlb_sync_pending) { |
| 179 | iop->cfg.tlb->tlb_sync(iop->cookie); |
| 180 | iop->tlb_sync_pending = false; |
| 181 | } |
Robin Murphy | 507e4c9 | 2016-01-26 17:13:14 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Will Deacon | fdb1d7b | 2014-11-14 17:16:49 +0000 | [diff] [blame] | 184 | /** |
| 185 | * struct io_pgtable_init_fns - Alloc/free a set of page tables for a |
| 186 | * particular format. |
| 187 | * |
| 188 | * @alloc: Allocate a set of page tables described by cfg. |
| 189 | * @free: Free the page tables associated with iop. |
| 190 | */ |
| 191 | struct io_pgtable_init_fns { |
| 192 | struct io_pgtable *(*alloc)(struct io_pgtable_cfg *cfg, void *cookie); |
| 193 | void (*free)(struct io_pgtable *iop); |
| 194 | }; |
| 195 | |
Joerg Roedel | 2e169bb | 2015-08-13 12:01:10 +0200 | [diff] [blame] | 196 | extern struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns; |
| 197 | extern struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns; |
| 198 | extern struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns; |
| 199 | extern struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns; |
Robin Murphy | e5fc975 | 2016-01-26 17:13:13 +0000 | [diff] [blame] | 200 | extern struct io_pgtable_init_fns io_pgtable_arm_v7s_init_fns; |
Joerg Roedel | 2e169bb | 2015-08-13 12:01:10 +0200 | [diff] [blame] | 201 | |
Will Deacon | fdb1d7b | 2014-11-14 17:16:49 +0000 | [diff] [blame] | 202 | #endif /* __IO_PGTABLE_H */ |