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