2
0

amd_iommu.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * QEMU emulation of an AMD IOMMU (AMD-Vi)
  3. *
  4. * Copyright (C) 2011 Eduard - Gabriel Munteanu
  5. * Copyright (C) 2015, 2016 David Kiarie Kahurani
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef AMD_IOMMU_H
  19. #define AMD_IOMMU_H
  20. #include "hw/pci/pci.h"
  21. #include "hw/i386/x86-iommu.h"
  22. #include "qom/object.h"
  23. /* Capability registers */
  24. #define AMDVI_CAPAB_BAR_LOW 0x04
  25. #define AMDVI_CAPAB_BAR_HIGH 0x08
  26. #define AMDVI_CAPAB_RANGE 0x0C
  27. #define AMDVI_CAPAB_MISC 0x10
  28. #define AMDVI_CAPAB_SIZE 0x18
  29. #define AMDVI_CAPAB_REG_SIZE 0x04
  30. /* Capability header data */
  31. #define AMDVI_CAPAB_ID_SEC 0xf
  32. #define AMDVI_CAPAB_FLAT_EXT (1 << 28)
  33. #define AMDVI_CAPAB_EFR_SUP (1 << 27)
  34. #define AMDVI_CAPAB_FLAG_NPCACHE (1 << 26)
  35. #define AMDVI_CAPAB_FLAG_HTTUNNEL (1 << 25)
  36. #define AMDVI_CAPAB_FLAG_IOTLBSUP (1 << 24)
  37. #define AMDVI_CAPAB_INIT_TYPE (3 << 16)
  38. /* No. of used MMIO registers */
  39. #define AMDVI_MMIO_REGS_HIGH 7
  40. #define AMDVI_MMIO_REGS_LOW 8
  41. /* MMIO registers */
  42. #define AMDVI_MMIO_DEVICE_TABLE 0x0000
  43. #define AMDVI_MMIO_COMMAND_BASE 0x0008
  44. #define AMDVI_MMIO_EVENT_BASE 0x0010
  45. #define AMDVI_MMIO_CONTROL 0x0018
  46. #define AMDVI_MMIO_EXCL_BASE 0x0020
  47. #define AMDVI_MMIO_EXCL_LIMIT 0x0028
  48. #define AMDVI_MMIO_EXT_FEATURES 0x0030
  49. #define AMDVI_MMIO_COMMAND_HEAD 0x2000
  50. #define AMDVI_MMIO_COMMAND_TAIL 0x2008
  51. #define AMDVI_MMIO_EVENT_HEAD 0x2010
  52. #define AMDVI_MMIO_EVENT_TAIL 0x2018
  53. #define AMDVI_MMIO_STATUS 0x2020
  54. #define AMDVI_MMIO_PPR_BASE 0x0038
  55. #define AMDVI_MMIO_PPR_HEAD 0x2030
  56. #define AMDVI_MMIO_PPR_TAIL 0x2038
  57. #define AMDVI_MMIO_SIZE 0x4000
  58. #define AMDVI_MMIO_DEVTAB_SIZE_MASK ((1ULL << 12) - 1)
  59. #define AMDVI_MMIO_DEVTAB_BASE_MASK (((1ULL << 52) - 1) & ~ \
  60. AMDVI_MMIO_DEVTAB_SIZE_MASK)
  61. #define AMDVI_MMIO_DEVTAB_ENTRY_SIZE 32
  62. #define AMDVI_MMIO_DEVTAB_SIZE_UNIT 4096
  63. /* some of this are similar but just for readability */
  64. #define AMDVI_MMIO_CMDBUF_SIZE_BYTE (AMDVI_MMIO_COMMAND_BASE + 7)
  65. #define AMDVI_MMIO_CMDBUF_SIZE_MASK 0x0f
  66. #define AMDVI_MMIO_CMDBUF_BASE_MASK AMDVI_MMIO_DEVTAB_BASE_MASK
  67. #define AMDVI_MMIO_CMDBUF_HEAD_MASK (((1ULL << 19) - 1) & ~0x0f)
  68. #define AMDVI_MMIO_CMDBUF_TAIL_MASK AMDVI_MMIO_EVTLOG_HEAD_MASK
  69. #define AMDVI_MMIO_EVTLOG_SIZE_BYTE (AMDVI_MMIO_EVENT_BASE + 7)
  70. #define AMDVI_MMIO_EVTLOG_SIZE_MASK AMDVI_MMIO_CMDBUF_SIZE_MASK
  71. #define AMDVI_MMIO_EVTLOG_BASE_MASK AMDVI_MMIO_CMDBUF_BASE_MASK
  72. #define AMDVI_MMIO_EVTLOG_HEAD_MASK (((1ULL << 19) - 1) & ~0x0f)
  73. #define AMDVI_MMIO_EVTLOG_TAIL_MASK AMDVI_MMIO_EVTLOG_HEAD_MASK
  74. #define AMDVI_MMIO_PPRLOG_SIZE_BYTE (AMDVI_MMIO_EVENT_BASE + 7)
  75. #define AMDVI_MMIO_PPRLOG_HEAD_MASK AMDVI_MMIO_EVTLOG_HEAD_MASK
  76. #define AMDVI_MMIO_PPRLOG_TAIL_MASK AMDVI_MMIO_EVTLOG_HEAD_MASK
  77. #define AMDVI_MMIO_PPRLOG_BASE_MASK AMDVI_MMIO_EVTLOG_BASE_MASK
  78. #define AMDVI_MMIO_PPRLOG_SIZE_MASK AMDVI_MMIO_EVTLOG_SIZE_MASK
  79. #define AMDVI_MMIO_EXCL_ENABLED_MASK (1ULL << 0)
  80. #define AMDVI_MMIO_EXCL_ALLOW_MASK (1ULL << 1)
  81. #define AMDVI_MMIO_EXCL_LIMIT_MASK AMDVI_MMIO_DEVTAB_BASE_MASK
  82. #define AMDVI_MMIO_EXCL_LIMIT_LOW 0xfff
  83. /* mmio control register flags */
  84. #define AMDVI_MMIO_CONTROL_AMDVIEN (1ULL << 0)
  85. #define AMDVI_MMIO_CONTROL_HTTUNEN (1ULL << 1)
  86. #define AMDVI_MMIO_CONTROL_EVENTLOGEN (1ULL << 2)
  87. #define AMDVI_MMIO_CONTROL_EVENTINTEN (1ULL << 3)
  88. #define AMDVI_MMIO_CONTROL_COMWAITINTEN (1ULL << 4)
  89. #define AMDVI_MMIO_CONTROL_CMDBUFLEN (1ULL << 12)
  90. #define AMDVI_MMIO_CONTROL_GAEN (1ULL << 17)
  91. /* MMIO status register bits */
  92. #define AMDVI_MMIO_STATUS_CMDBUF_RUN (1 << 4)
  93. #define AMDVI_MMIO_STATUS_EVT_RUN (1 << 3)
  94. #define AMDVI_MMIO_STATUS_COMP_INT (1 << 2)
  95. #define AMDVI_MMIO_STATUS_EVT_OVF (1 << 0)
  96. #define AMDVI_CMDBUF_ID_BYTE 0x07
  97. #define AMDVI_CMDBUF_ID_RSHIFT 4
  98. #define AMDVI_CMD_COMPLETION_WAIT 0x01
  99. #define AMDVI_CMD_INVAL_DEVTAB_ENTRY 0x02
  100. #define AMDVI_CMD_INVAL_AMDVI_PAGES 0x03
  101. #define AMDVI_CMD_INVAL_IOTLB_PAGES 0x04
  102. #define AMDVI_CMD_INVAL_INTR_TABLE 0x05
  103. #define AMDVI_CMD_PREFETCH_AMDVI_PAGES 0x06
  104. #define AMDVI_CMD_COMPLETE_PPR_REQUEST 0x07
  105. #define AMDVI_CMD_INVAL_AMDVI_ALL 0x08
  106. #define AMDVI_DEVTAB_ENTRY_SIZE 32
  107. /* Device table entry bits 0:63 */
  108. #define AMDVI_DEV_VALID (1ULL << 0)
  109. #define AMDVI_DEV_TRANSLATION_VALID (1ULL << 1)
  110. #define AMDVI_DEV_MODE_MASK 0x7
  111. #define AMDVI_DEV_MODE_RSHIFT 9
  112. #define AMDVI_DEV_PT_ROOT_MASK 0xffffffffff000
  113. #define AMDVI_DEV_PT_ROOT_RSHIFT 12
  114. #define AMDVI_DEV_PERM_SHIFT 61
  115. #define AMDVI_DEV_PERM_READ (1ULL << 61)
  116. #define AMDVI_DEV_PERM_WRITE (1ULL << 62)
  117. /* Device table entry bits 64:127 */
  118. #define AMDVI_DEV_DOMID_ID_MASK ((1ULL << 16) - 1)
  119. /* Event codes and flags, as stored in the info field */
  120. #define AMDVI_EVENT_ILLEGAL_DEVTAB_ENTRY (0x1U << 12)
  121. #define AMDVI_EVENT_IOPF (0x2U << 12)
  122. #define AMDVI_EVENT_IOPF_I (1U << 3)
  123. #define AMDVI_EVENT_DEV_TAB_HW_ERROR (0x3U << 12)
  124. #define AMDVI_EVENT_PAGE_TAB_HW_ERROR (0x4U << 12)
  125. #define AMDVI_EVENT_ILLEGAL_COMMAND_ERROR (0x5U << 12)
  126. #define AMDVI_EVENT_COMMAND_HW_ERROR (0x6U << 12)
  127. #define AMDVI_EVENT_LEN 16
  128. #define AMDVI_PERM_READ (1 << 0)
  129. #define AMDVI_PERM_WRITE (1 << 1)
  130. #define AMDVI_FEATURE_PREFETCH (1ULL << 0) /* page prefetch */
  131. #define AMDVI_FEATURE_PPR (1ULL << 1) /* PPR Support */
  132. #define AMDVI_FEATURE_XT (1ULL << 2) /* x2APIC Support */
  133. #define AMDVI_FEATURE_GT (1ULL << 4) /* Guest Translation */
  134. #define AMDVI_FEATURE_IA (1ULL << 6) /* inval all support */
  135. #define AMDVI_FEATURE_GA (1ULL << 7) /* guest VAPIC support */
  136. #define AMDVI_FEATURE_HE (1ULL << 8) /* hardware error regs */
  137. #define AMDVI_FEATURE_PC (1ULL << 9) /* Perf counters */
  138. /* reserved DTE bits */
  139. #define AMDVI_DTE_LOWER_QUAD_RESERVED 0x80300000000000fc
  140. #define AMDVI_DTE_MIDDLE_QUAD_RESERVED 0x0000000000000100
  141. #define AMDVI_DTE_UPPER_QUAD_RESERVED 0x08f0000000000000
  142. /* AMDVI paging mode */
  143. #define AMDVI_GATS_MODE (2ULL << 12)
  144. #define AMDVI_HATS_MODE (2ULL << 10)
  145. /* IOTLB */
  146. #define AMDVI_IOTLB_MAX_SIZE 1024
  147. #define AMDVI_DEVID_SHIFT 36
  148. /* default extended feature */
  149. #define AMDVI_DEFAULT_EXT_FEATURES \
  150. (AMDVI_FEATURE_PREFETCH | AMDVI_FEATURE_PPR | \
  151. AMDVI_FEATURE_IA | AMDVI_FEATURE_GT | AMDVI_FEATURE_HE | \
  152. AMDVI_GATS_MODE | AMDVI_HATS_MODE | AMDVI_FEATURE_GA)
  153. /* capabilities header */
  154. #define AMDVI_CAPAB_FEATURES (AMDVI_CAPAB_FLAT_EXT | \
  155. AMDVI_CAPAB_FLAG_NPCACHE | AMDVI_CAPAB_FLAG_IOTLBSUP \
  156. | AMDVI_CAPAB_ID_SEC | AMDVI_CAPAB_INIT_TYPE | \
  157. AMDVI_CAPAB_FLAG_HTTUNNEL | AMDVI_CAPAB_EFR_SUP)
  158. /* AMDVI default address */
  159. #define AMDVI_BASE_ADDR 0xfed80000ULL
  160. /* page management constants */
  161. #define AMDVI_PAGE_SHIFT 12
  162. #define AMDVI_PAGE_SIZE (1ULL << AMDVI_PAGE_SHIFT)
  163. #define AMDVI_PAGE_SHIFT_4K 12
  164. #define AMDVI_PAGE_MASK_4K (~((1ULL << AMDVI_PAGE_SHIFT_4K) - 1))
  165. #define AMDVI_MAX_VA_ADDR (48UL << 5)
  166. #define AMDVI_MAX_PH_ADDR (40UL << 8)
  167. #define AMDVI_MAX_GVA_ADDR (48UL << 15)
  168. /* Completion Wait data size */
  169. #define AMDVI_COMPLETION_DATA_SIZE 8
  170. #define AMDVI_COMMAND_SIZE 16
  171. /* Completion Wait data size */
  172. #define AMDVI_COMPLETION_DATA_SIZE 8
  173. #define AMDVI_COMMAND_SIZE 16
  174. #define AMDVI_INT_ADDR_FIRST 0xfee00000
  175. #define AMDVI_INT_ADDR_LAST 0xfeefffff
  176. #define AMDVI_INT_ADDR_SIZE (AMDVI_INT_ADDR_LAST - AMDVI_INT_ADDR_FIRST + 1)
  177. /* SB IOAPIC is always on this device in AMD systems */
  178. #define AMDVI_IOAPIC_SB_DEVID PCI_BUILD_BDF(0, PCI_DEVFN(0x14, 0))
  179. /* Interrupt remapping errors */
  180. #define AMDVI_IR_ERR 0x1
  181. #define AMDVI_IR_GET_IRTE 0x2
  182. #define AMDVI_IR_TARGET_ABORT 0x3
  183. /* Interrupt remapping */
  184. #define AMDVI_IR_REMAP_ENABLE 1ULL
  185. #define AMDVI_IR_INTCTL_SHIFT 60
  186. #define AMDVI_IR_INTCTL_ABORT 0
  187. #define AMDVI_IR_INTCTL_PASS 1
  188. #define AMDVI_IR_INTCTL_REMAP 2
  189. #define AMDVI_IR_PHYS_ADDR_MASK (((1ULL << 45) - 1) << 6)
  190. /* MSI data 10:0 bits (section 2.2.5.1 Fig 14) */
  191. #define AMDVI_IRTE_OFFSET 0x7ff
  192. /* Delivery mode of MSI data (same as IOAPIC deilver mode encoding) */
  193. #define AMDVI_IOAPIC_INT_TYPE_FIXED 0x0
  194. #define AMDVI_IOAPIC_INT_TYPE_ARBITRATED 0x1
  195. #define AMDVI_IOAPIC_INT_TYPE_SMI 0x2
  196. #define AMDVI_IOAPIC_INT_TYPE_NMI 0x4
  197. #define AMDVI_IOAPIC_INT_TYPE_INIT 0x5
  198. #define AMDVI_IOAPIC_INT_TYPE_EINT 0x7
  199. /* Pass through interrupt */
  200. #define AMDVI_DEV_INT_PASS_MASK (1ULL << 56)
  201. #define AMDVI_DEV_EINT_PASS_MASK (1ULL << 57)
  202. #define AMDVI_DEV_NMI_PASS_MASK (1ULL << 58)
  203. #define AMDVI_DEV_LINT0_PASS_MASK (1ULL << 62)
  204. #define AMDVI_DEV_LINT1_PASS_MASK (1ULL << 63)
  205. /* Interrupt remapping table fields (Guest VAPIC not enabled) */
  206. union irte {
  207. uint32_t val;
  208. struct {
  209. uint32_t valid:1,
  210. no_fault:1,
  211. int_type:3,
  212. rq_eoi:1,
  213. dm:1,
  214. guest_mode:1,
  215. destination:8,
  216. vector:8,
  217. rsvd:8;
  218. } fields;
  219. };
  220. /* Interrupt remapping table fields (Guest VAPIC is enabled) */
  221. union irte_ga_lo {
  222. uint64_t val;
  223. /* For int remapping */
  224. struct {
  225. uint64_t valid:1,
  226. no_fault:1,
  227. /* ------ */
  228. int_type:3,
  229. rq_eoi:1,
  230. dm:1,
  231. /* ------ */
  232. guest_mode:1,
  233. destination:24,
  234. rsvd_1:32;
  235. } fields_remap;
  236. };
  237. union irte_ga_hi {
  238. uint64_t val;
  239. struct {
  240. uint64_t vector:8,
  241. rsvd_2:48,
  242. destination_hi:8;
  243. } fields;
  244. };
  245. struct irte_ga {
  246. union irte_ga_lo lo;
  247. union irte_ga_hi hi;
  248. };
  249. #define TYPE_AMD_IOMMU_DEVICE "amd-iommu"
  250. OBJECT_DECLARE_SIMPLE_TYPE(AMDVIState, AMD_IOMMU_DEVICE)
  251. #define TYPE_AMD_IOMMU_PCI "AMDVI-PCI"
  252. OBJECT_DECLARE_SIMPLE_TYPE(AMDVIPCIState, AMD_IOMMU_PCI)
  253. #define TYPE_AMD_IOMMU_MEMORY_REGION "amd-iommu-iommu-memory-region"
  254. typedef struct AMDVIAddressSpace AMDVIAddressSpace;
  255. /* functions to steal PCI config space */
  256. struct AMDVIPCIState {
  257. PCIDevice dev; /* The PCI device itself */
  258. uint32_t capab_offset; /* capability offset pointer */
  259. };
  260. struct AMDVIState {
  261. X86IOMMUState iommu; /* IOMMU bus device */
  262. AMDVIPCIState pci; /* IOMMU PCI device */
  263. uint32_t version;
  264. uint64_t mmio_addr;
  265. bool enabled; /* IOMMU enabled */
  266. bool ats_enabled; /* address translation enabled */
  267. bool cmdbuf_enabled; /* command buffer enabled */
  268. bool evtlog_enabled; /* event log enabled */
  269. bool excl_enabled;
  270. hwaddr devtab; /* base address device table */
  271. size_t devtab_len; /* device table length */
  272. hwaddr cmdbuf; /* command buffer base address */
  273. uint64_t cmdbuf_len; /* command buffer length */
  274. uint32_t cmdbuf_head; /* current IOMMU read position */
  275. uint32_t cmdbuf_tail; /* next Software write position */
  276. bool completion_wait_intr;
  277. hwaddr evtlog; /* base address event log */
  278. bool evtlog_intr;
  279. uint32_t evtlog_len; /* event log length */
  280. uint32_t evtlog_head; /* current IOMMU write position */
  281. uint32_t evtlog_tail; /* current Software read position */
  282. /* unused for now */
  283. hwaddr excl_base; /* base DVA - IOMMU exclusion range */
  284. hwaddr excl_limit; /* limit of IOMMU exclusion range */
  285. bool excl_allow; /* translate accesses to the exclusion range */
  286. bool excl_enable; /* exclusion range enabled */
  287. hwaddr ppr_log; /* base address ppr log */
  288. uint32_t pprlog_len; /* ppr log len */
  289. uint32_t pprlog_head; /* ppr log head */
  290. uint32_t pprlog_tail; /* ppr log tail */
  291. MemoryRegion mr_mmio; /* MMIO region */
  292. MemoryRegion mr_sys;
  293. MemoryRegion mr_nodma;
  294. MemoryRegion mr_ir;
  295. uint8_t mmior[AMDVI_MMIO_SIZE]; /* read/write MMIO */
  296. uint8_t w1cmask[AMDVI_MMIO_SIZE]; /* read/write 1 clear mask */
  297. uint8_t romask[AMDVI_MMIO_SIZE]; /* MMIO read/only mask */
  298. bool mmio_enabled;
  299. /* for each served device */
  300. AMDVIAddressSpace **address_spaces[PCI_BUS_MAX];
  301. /* IOTLB */
  302. GHashTable *iotlb;
  303. /* Interrupt remapping */
  304. bool ga_enabled;
  305. bool xtsup;
  306. };
  307. uint64_t amdvi_extended_feature_register(AMDVIState *s);
  308. #endif