io-pgtable.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #ifndef __IO_PGTABLE_H
  2. #define __IO_PGTABLE_H
  3. /*
  4. * Public API for use by IOMMU drivers
  5. */
  6. enum io_pgtable_fmt {
  7. ARM_32_LPAE_S1,
  8. ARM_32_LPAE_S2,
  9. ARM_64_LPAE_S1,
  10. ARM_64_LPAE_S2,
  11. IO_PGTABLE_NUM_FMTS,
  12. };
  13. /**
  14. * struct iommu_gather_ops - IOMMU callbacks for TLB and page table management.
  15. *
  16. * @tlb_flush_all: Synchronously invalidate the entire TLB context.
  17. * @tlb_add_flush: Queue up a TLB invalidation for a virtual address range.
  18. * @tlb_sync: Ensure any queued TLB invalidation has taken effect, and
  19. * any corresponding page table updates are visible to the
  20. * IOMMU.
  21. *
  22. * Note that these can all be called in atomic context and must therefore
  23. * not block.
  24. */
  25. struct iommu_gather_ops {
  26. void (*tlb_flush_all)(void *cookie);
  27. void (*tlb_add_flush)(unsigned long iova, size_t size, bool leaf,
  28. void *cookie);
  29. void (*tlb_sync)(void *cookie);
  30. };
  31. /**
  32. * struct io_pgtable_cfg - Configuration data for a set of page tables.
  33. *
  34. * @quirks: A bitmap of hardware quirks that require some special
  35. * action by the low-level page table allocator.
  36. * @pgsize_bitmap: A bitmap of page sizes supported by this set of page
  37. * tables.
  38. * @ias: Input address (iova) size, in bits.
  39. * @oas: Output address (paddr) size, in bits.
  40. * @tlb: TLB management callbacks for this set of tables.
  41. * @iommu_dev: The device representing the DMA configuration for the
  42. * page table walker.
  43. */
  44. struct io_pgtable_cfg {
  45. #define IO_PGTABLE_QUIRK_ARM_NS (1 << 0) /* Set NS bit in PTEs */
  46. int quirks;
  47. unsigned long pgsize_bitmap;
  48. unsigned int ias;
  49. unsigned int oas;
  50. const struct iommu_gather_ops *tlb;
  51. struct device *iommu_dev;
  52. /* Low-level data specific to the table format */
  53. union {
  54. struct {
  55. u64 ttbr[2];
  56. u64 tcr;
  57. u64 mair[2];
  58. } arm_lpae_s1_cfg;
  59. struct {
  60. u64 vttbr;
  61. u64 vtcr;
  62. } arm_lpae_s2_cfg;
  63. };
  64. };
  65. /**
  66. * struct io_pgtable_ops - Page table manipulation API for IOMMU drivers.
  67. *
  68. * @map: Map a physically contiguous memory region.
  69. * @unmap: Unmap a physically contiguous memory region.
  70. * @iova_to_phys: Translate iova to physical address.
  71. *
  72. * These functions map directly onto the iommu_ops member functions with
  73. * the same names.
  74. */
  75. struct io_pgtable_ops {
  76. int (*map)(struct io_pgtable_ops *ops, unsigned long iova,
  77. phys_addr_t paddr, size_t size, int prot);
  78. int (*unmap)(struct io_pgtable_ops *ops, unsigned long iova,
  79. size_t size);
  80. phys_addr_t (*iova_to_phys)(struct io_pgtable_ops *ops,
  81. unsigned long iova);
  82. };
  83. /**
  84. * alloc_io_pgtable_ops() - Allocate a page table allocator for use by an IOMMU.
  85. *
  86. * @fmt: The page table format.
  87. * @cfg: The page table configuration. This will be modified to represent
  88. * the configuration actually provided by the allocator (e.g. the
  89. * pgsize_bitmap may be restricted).
  90. * @cookie: An opaque token provided by the IOMMU driver and passed back to
  91. * the callback routines in cfg->tlb.
  92. */
  93. struct io_pgtable_ops *alloc_io_pgtable_ops(enum io_pgtable_fmt fmt,
  94. struct io_pgtable_cfg *cfg,
  95. void *cookie);
  96. /**
  97. * free_io_pgtable_ops() - Free an io_pgtable_ops structure. The caller
  98. * *must* ensure that the page table is no longer
  99. * live, but the TLB can be dirty.
  100. *
  101. * @ops: The ops returned from alloc_io_pgtable_ops.
  102. */
  103. void free_io_pgtable_ops(struct io_pgtable_ops *ops);
  104. /*
  105. * Internal structures for page table allocator implementations.
  106. */
  107. /**
  108. * struct io_pgtable - Internal structure describing a set of page tables.
  109. *
  110. * @fmt: The page table format.
  111. * @cookie: An opaque token provided by the IOMMU driver and passed back to
  112. * any callback routines.
  113. * @cfg: A copy of the page table configuration.
  114. * @ops: The page table operations in use for this set of page tables.
  115. */
  116. struct io_pgtable {
  117. enum io_pgtable_fmt fmt;
  118. void *cookie;
  119. struct io_pgtable_cfg cfg;
  120. struct io_pgtable_ops ops;
  121. };
  122. /**
  123. * struct io_pgtable_init_fns - Alloc/free a set of page tables for a
  124. * particular format.
  125. *
  126. * @alloc: Allocate a set of page tables described by cfg.
  127. * @free: Free the page tables associated with iop.
  128. */
  129. struct io_pgtable_init_fns {
  130. struct io_pgtable *(*alloc)(struct io_pgtable_cfg *cfg, void *cookie);
  131. void (*free)(struct io_pgtable *iop);
  132. };
  133. extern struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s1_init_fns;
  134. extern struct io_pgtable_init_fns io_pgtable_arm_32_lpae_s2_init_fns;
  135. extern struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s1_init_fns;
  136. extern struct io_pgtable_init_fns io_pgtable_arm_64_lpae_s2_init_fns;
  137. #endif /* __IO_PGTABLE_H */