io-pgtable.h 6.7 KB

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