io-pgtable.h 6.9 KB

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