dma_remapping.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #ifndef _DMA_REMAPPING_H
  2. #define _DMA_REMAPPING_H
  3. /*
  4. * We need a fixed PAGE_SIZE of 4K irrespective of
  5. * arch PAGE_SIZE for IOMMU page tables.
  6. */
  7. #define PAGE_SHIFT_4K (12)
  8. #define PAGE_SIZE_4K (1UL << PAGE_SHIFT_4K)
  9. #define PAGE_MASK_4K (((u64)-1) << PAGE_SHIFT_4K)
  10. #define PAGE_ALIGN_4K(addr) (((addr) + PAGE_SIZE_4K - 1) & PAGE_MASK_4K)
  11. #define IOVA_PFN(addr) ((addr) >> PAGE_SHIFT_4K)
  12. #define DMA_32BIT_PFN IOVA_PFN(DMA_32BIT_MASK)
  13. #define DMA_64BIT_PFN IOVA_PFN(DMA_64BIT_MASK)
  14. /*
  15. * 0: Present
  16. * 1-11: Reserved
  17. * 12-63: Context Ptr (12 - (haw-1))
  18. * 64-127: Reserved
  19. */
  20. struct root_entry {
  21. u64 val;
  22. u64 rsvd1;
  23. };
  24. #define ROOT_ENTRY_NR (PAGE_SIZE_4K/sizeof(struct root_entry))
  25. static inline bool root_present(struct root_entry *root)
  26. {
  27. return (root->val & 1);
  28. }
  29. static inline void set_root_present(struct root_entry *root)
  30. {
  31. root->val |= 1;
  32. }
  33. static inline void set_root_value(struct root_entry *root, unsigned long value)
  34. {
  35. root->val |= value & PAGE_MASK_4K;
  36. }
  37. struct context_entry;
  38. static inline struct context_entry *
  39. get_context_addr_from_root(struct root_entry *root)
  40. {
  41. return (struct context_entry *)
  42. (root_present(root)?phys_to_virt(
  43. root->val & PAGE_MASK_4K):
  44. NULL);
  45. }
  46. /*
  47. * low 64 bits:
  48. * 0: present
  49. * 1: fault processing disable
  50. * 2-3: translation type
  51. * 12-63: address space root
  52. * high 64 bits:
  53. * 0-2: address width
  54. * 3-6: aval
  55. * 8-23: domain id
  56. */
  57. struct context_entry {
  58. u64 lo;
  59. u64 hi;
  60. };
  61. #define context_present(c) ((c).lo & 1)
  62. #define context_fault_disable(c) (((c).lo >> 1) & 1)
  63. #define context_translation_type(c) (((c).lo >> 2) & 3)
  64. #define context_address_root(c) ((c).lo & PAGE_MASK_4K)
  65. #define context_address_width(c) ((c).hi & 7)
  66. #define context_domain_id(c) (((c).hi >> 8) & ((1 << 16) - 1))
  67. #define context_set_present(c) do {(c).lo |= 1;} while (0)
  68. #define context_set_fault_enable(c) \
  69. do {(c).lo &= (((u64)-1) << 2) | 1;} while (0)
  70. #define context_set_translation_type(c, val) \
  71. do { \
  72. (c).lo &= (((u64)-1) << 4) | 3; \
  73. (c).lo |= ((val) & 3) << 2; \
  74. } while (0)
  75. #define CONTEXT_TT_MULTI_LEVEL 0
  76. #define context_set_address_root(c, val) \
  77. do {(c).lo |= (val) & PAGE_MASK_4K;} while (0)
  78. #define context_set_address_width(c, val) do {(c).hi |= (val) & 7;} while (0)
  79. #define context_set_domain_id(c, val) \
  80. do {(c).hi |= ((val) & ((1 << 16) - 1)) << 8;} while (0)
  81. #define context_clear_entry(c) do {(c).lo = 0; (c).hi = 0;} while (0)
  82. /*
  83. * 0: readable
  84. * 1: writable
  85. * 2-6: reserved
  86. * 7: super page
  87. * 8-11: available
  88. * 12-63: Host physcial address
  89. */
  90. struct dma_pte {
  91. u64 val;
  92. };
  93. #define dma_clear_pte(p) do {(p).val = 0;} while (0)
  94. #define DMA_PTE_READ (1)
  95. #define DMA_PTE_WRITE (2)
  96. #define dma_set_pte_readable(p) do {(p).val |= DMA_PTE_READ;} while (0)
  97. #define dma_set_pte_writable(p) do {(p).val |= DMA_PTE_WRITE;} while (0)
  98. #define dma_set_pte_prot(p, prot) \
  99. do {(p).val = ((p).val & ~3) | ((prot) & 3); } while (0)
  100. #define dma_pte_addr(p) ((p).val & PAGE_MASK_4K)
  101. #define dma_set_pte_addr(p, addr) do {\
  102. (p).val |= ((addr) & PAGE_MASK_4K); } while (0)
  103. #define dma_pte_present(p) (((p).val & 3) != 0)
  104. struct intel_iommu;
  105. struct dmar_domain {
  106. int id; /* domain id */
  107. struct intel_iommu *iommu; /* back pointer to owning iommu */
  108. struct list_head devices; /* all devices' list */
  109. struct iova_domain iovad; /* iova's that belong to this domain */
  110. struct dma_pte *pgd; /* virtual address */
  111. spinlock_t mapping_lock; /* page table lock */
  112. int gaw; /* max guest address width */
  113. /* adjusted guest address width, 0 is level 2 30-bit */
  114. int agaw;
  115. #define DOMAIN_FLAG_MULTIPLE_DEVICES 1
  116. int flags;
  117. };
  118. /* PCI domain-device relationship */
  119. struct device_domain_info {
  120. struct list_head link; /* link to domain siblings */
  121. struct list_head global; /* link to global list */
  122. u8 bus; /* PCI bus numer */
  123. u8 devfn; /* PCI devfn number */
  124. struct pci_dev *dev; /* it's NULL for PCIE-to-PCI bridge */
  125. struct dmar_domain *domain; /* pointer to domain */
  126. };
  127. extern int init_dmars(void);
  128. extern void free_dmar_iommu(struct intel_iommu *iommu);
  129. extern int dmar_disabled;
  130. #ifndef CONFIG_DMAR_GFX_WA
  131. static inline void iommu_prepare_gfx_mapping(void)
  132. {
  133. return;
  134. }
  135. #endif /* !CONFIG_DMAR_GFX_WA */
  136. #endif