amd_iommu.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664
  1. /*
  2. * QEMU emulation of 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. * Cache implementation inspired by hw/i386/intel_iommu.c
  19. */
  20. #include "qemu/osdep.h"
  21. #include "hw/i386/pc.h"
  22. #include "hw/pci/msi.h"
  23. #include "hw/pci/pci_bus.h"
  24. #include "migration/vmstate.h"
  25. #include "amd_iommu.h"
  26. #include "qapi/error.h"
  27. #include "qemu/error-report.h"
  28. #include "hw/i386/apic_internal.h"
  29. #include "trace.h"
  30. #include "hw/i386/apic-msidef.h"
  31. /* used AMD-Vi MMIO registers */
  32. const char *amdvi_mmio_low[] = {
  33. "AMDVI_MMIO_DEVTAB_BASE",
  34. "AMDVI_MMIO_CMDBUF_BASE",
  35. "AMDVI_MMIO_EVTLOG_BASE",
  36. "AMDVI_MMIO_CONTROL",
  37. "AMDVI_MMIO_EXCL_BASE",
  38. "AMDVI_MMIO_EXCL_LIMIT",
  39. "AMDVI_MMIO_EXT_FEATURES",
  40. "AMDVI_MMIO_PPR_BASE",
  41. "UNHANDLED"
  42. };
  43. const char *amdvi_mmio_high[] = {
  44. "AMDVI_MMIO_COMMAND_HEAD",
  45. "AMDVI_MMIO_COMMAND_TAIL",
  46. "AMDVI_MMIO_EVTLOG_HEAD",
  47. "AMDVI_MMIO_EVTLOG_TAIL",
  48. "AMDVI_MMIO_STATUS",
  49. "AMDVI_MMIO_PPR_HEAD",
  50. "AMDVI_MMIO_PPR_TAIL",
  51. "UNHANDLED"
  52. };
  53. struct AMDVIAddressSpace {
  54. uint8_t bus_num; /* bus number */
  55. uint8_t devfn; /* device function */
  56. AMDVIState *iommu_state; /* AMDVI - one per machine */
  57. MemoryRegion root; /* AMDVI Root memory map region */
  58. IOMMUMemoryRegion iommu; /* Device's address translation region */
  59. MemoryRegion iommu_ir; /* Device's interrupt remapping region */
  60. AddressSpace as; /* device's corresponding address space */
  61. };
  62. /* AMDVI cache entry */
  63. typedef struct AMDVIIOTLBEntry {
  64. uint16_t domid; /* assigned domain id */
  65. uint16_t devid; /* device owning entry */
  66. uint64_t perms; /* access permissions */
  67. uint64_t translated_addr; /* translated address */
  68. uint64_t page_mask; /* physical page size */
  69. } AMDVIIOTLBEntry;
  70. /* configure MMIO registers at startup/reset */
  71. static void amdvi_set_quad(AMDVIState *s, hwaddr addr, uint64_t val,
  72. uint64_t romask, uint64_t w1cmask)
  73. {
  74. stq_le_p(&s->mmior[addr], val);
  75. stq_le_p(&s->romask[addr], romask);
  76. stq_le_p(&s->w1cmask[addr], w1cmask);
  77. }
  78. static uint16_t amdvi_readw(AMDVIState *s, hwaddr addr)
  79. {
  80. return lduw_le_p(&s->mmior[addr]);
  81. }
  82. static uint32_t amdvi_readl(AMDVIState *s, hwaddr addr)
  83. {
  84. return ldl_le_p(&s->mmior[addr]);
  85. }
  86. static uint64_t amdvi_readq(AMDVIState *s, hwaddr addr)
  87. {
  88. return ldq_le_p(&s->mmior[addr]);
  89. }
  90. /* internal write */
  91. static void amdvi_writeq_raw(AMDVIState *s, hwaddr addr, uint64_t val)
  92. {
  93. stq_le_p(&s->mmior[addr], val);
  94. }
  95. /* external write */
  96. static void amdvi_writew(AMDVIState *s, hwaddr addr, uint16_t val)
  97. {
  98. uint16_t romask = lduw_le_p(&s->romask[addr]);
  99. uint16_t w1cmask = lduw_le_p(&s->w1cmask[addr]);
  100. uint16_t oldval = lduw_le_p(&s->mmior[addr]);
  101. stw_le_p(&s->mmior[addr],
  102. ((oldval & romask) | (val & ~romask)) & ~(val & w1cmask));
  103. }
  104. static void amdvi_writel(AMDVIState *s, hwaddr addr, uint32_t val)
  105. {
  106. uint32_t romask = ldl_le_p(&s->romask[addr]);
  107. uint32_t w1cmask = ldl_le_p(&s->w1cmask[addr]);
  108. uint32_t oldval = ldl_le_p(&s->mmior[addr]);
  109. stl_le_p(&s->mmior[addr],
  110. ((oldval & romask) | (val & ~romask)) & ~(val & w1cmask));
  111. }
  112. static void amdvi_writeq(AMDVIState *s, hwaddr addr, uint64_t val)
  113. {
  114. uint64_t romask = ldq_le_p(&s->romask[addr]);
  115. uint64_t w1cmask = ldq_le_p(&s->w1cmask[addr]);
  116. uint32_t oldval = ldq_le_p(&s->mmior[addr]);
  117. stq_le_p(&s->mmior[addr],
  118. ((oldval & romask) | (val & ~romask)) & ~(val & w1cmask));
  119. }
  120. /* OR a 64-bit register with a 64-bit value */
  121. static bool amdvi_test_mask(AMDVIState *s, hwaddr addr, uint64_t val)
  122. {
  123. return amdvi_readq(s, addr) | val;
  124. }
  125. /* OR a 64-bit register with a 64-bit value storing result in the register */
  126. static void amdvi_assign_orq(AMDVIState *s, hwaddr addr, uint64_t val)
  127. {
  128. amdvi_writeq_raw(s, addr, amdvi_readq(s, addr) | val);
  129. }
  130. /* AND a 64-bit register with a 64-bit value storing result in the register */
  131. static void amdvi_assign_andq(AMDVIState *s, hwaddr addr, uint64_t val)
  132. {
  133. amdvi_writeq_raw(s, addr, amdvi_readq(s, addr) & val);
  134. }
  135. static void amdvi_generate_msi_interrupt(AMDVIState *s)
  136. {
  137. MSIMessage msg = {};
  138. MemTxAttrs attrs = {
  139. .requester_id = pci_requester_id(&s->pci.dev)
  140. };
  141. if (msi_enabled(&s->pci.dev)) {
  142. msg = msi_get_message(&s->pci.dev, 0);
  143. address_space_stl_le(&address_space_memory, msg.address, msg.data,
  144. attrs, NULL);
  145. }
  146. }
  147. static void amdvi_log_event(AMDVIState *s, uint64_t *evt)
  148. {
  149. /* event logging not enabled */
  150. if (!s->evtlog_enabled || amdvi_test_mask(s, AMDVI_MMIO_STATUS,
  151. AMDVI_MMIO_STATUS_EVT_OVF)) {
  152. return;
  153. }
  154. /* event log buffer full */
  155. if (s->evtlog_tail >= s->evtlog_len) {
  156. amdvi_assign_orq(s, AMDVI_MMIO_STATUS, AMDVI_MMIO_STATUS_EVT_OVF);
  157. /* generate interrupt */
  158. amdvi_generate_msi_interrupt(s);
  159. return;
  160. }
  161. if (dma_memory_write(&address_space_memory, s->evtlog + s->evtlog_tail,
  162. evt, AMDVI_EVENT_LEN, MEMTXATTRS_UNSPECIFIED)) {
  163. trace_amdvi_evntlog_fail(s->evtlog, s->evtlog_tail);
  164. }
  165. s->evtlog_tail += AMDVI_EVENT_LEN;
  166. amdvi_assign_orq(s, AMDVI_MMIO_STATUS, AMDVI_MMIO_STATUS_COMP_INT);
  167. amdvi_generate_msi_interrupt(s);
  168. }
  169. static void amdvi_setevent_bits(uint64_t *buffer, uint64_t value, int start,
  170. int length)
  171. {
  172. int index = start / 64, bitpos = start % 64;
  173. uint64_t mask = MAKE_64BIT_MASK(start, length);
  174. buffer[index] &= ~mask;
  175. buffer[index] |= (value << bitpos) & mask;
  176. }
  177. /*
  178. * AMDVi event structure
  179. * 0:15 -> DeviceID
  180. * 55:63 -> event type + miscellaneous info
  181. * 63:127 -> related address
  182. */
  183. static void amdvi_encode_event(uint64_t *evt, uint16_t devid, uint64_t addr,
  184. uint16_t info)
  185. {
  186. amdvi_setevent_bits(evt, devid, 0, 16);
  187. amdvi_setevent_bits(evt, info, 55, 8);
  188. amdvi_setevent_bits(evt, addr, 63, 64);
  189. }
  190. /* log an error encountered during a page walk
  191. *
  192. * @addr: virtual address in translation request
  193. */
  194. static void amdvi_page_fault(AMDVIState *s, uint16_t devid,
  195. hwaddr addr, uint16_t info)
  196. {
  197. uint64_t evt[4];
  198. info |= AMDVI_EVENT_IOPF_I | AMDVI_EVENT_IOPF;
  199. amdvi_encode_event(evt, devid, addr, info);
  200. amdvi_log_event(s, evt);
  201. pci_word_test_and_set_mask(s->pci.dev.config + PCI_STATUS,
  202. PCI_STATUS_SIG_TARGET_ABORT);
  203. }
  204. /*
  205. * log a master abort accessing device table
  206. * @devtab : address of device table entry
  207. * @info : error flags
  208. */
  209. static void amdvi_log_devtab_error(AMDVIState *s, uint16_t devid,
  210. hwaddr devtab, uint16_t info)
  211. {
  212. uint64_t evt[4];
  213. info |= AMDVI_EVENT_DEV_TAB_HW_ERROR;
  214. amdvi_encode_event(evt, devid, devtab, info);
  215. amdvi_log_event(s, evt);
  216. pci_word_test_and_set_mask(s->pci.dev.config + PCI_STATUS,
  217. PCI_STATUS_SIG_TARGET_ABORT);
  218. }
  219. /* log an event trying to access command buffer
  220. * @addr : address that couldn't be accessed
  221. */
  222. static void amdvi_log_command_error(AMDVIState *s, hwaddr addr)
  223. {
  224. uint64_t evt[4], info = AMDVI_EVENT_COMMAND_HW_ERROR;
  225. amdvi_encode_event(evt, 0, addr, info);
  226. amdvi_log_event(s, evt);
  227. pci_word_test_and_set_mask(s->pci.dev.config + PCI_STATUS,
  228. PCI_STATUS_SIG_TARGET_ABORT);
  229. }
  230. /* log an illegal comand event
  231. * @addr : address of illegal command
  232. */
  233. static void amdvi_log_illegalcom_error(AMDVIState *s, uint16_t info,
  234. hwaddr addr)
  235. {
  236. uint64_t evt[4];
  237. info |= AMDVI_EVENT_ILLEGAL_COMMAND_ERROR;
  238. amdvi_encode_event(evt, 0, addr, info);
  239. amdvi_log_event(s, evt);
  240. }
  241. /* log an error accessing device table
  242. *
  243. * @devid : device owning the table entry
  244. * @devtab : address of device table entry
  245. * @info : error flags
  246. */
  247. static void amdvi_log_illegaldevtab_error(AMDVIState *s, uint16_t devid,
  248. hwaddr addr, uint16_t info)
  249. {
  250. uint64_t evt[4];
  251. info |= AMDVI_EVENT_ILLEGAL_DEVTAB_ENTRY;
  252. amdvi_encode_event(evt, devid, addr, info);
  253. amdvi_log_event(s, evt);
  254. }
  255. /* log an error accessing a PTE entry
  256. * @addr : address that couldn't be accessed
  257. */
  258. static void amdvi_log_pagetab_error(AMDVIState *s, uint16_t devid,
  259. hwaddr addr, uint16_t info)
  260. {
  261. uint64_t evt[4];
  262. info |= AMDVI_EVENT_PAGE_TAB_HW_ERROR;
  263. amdvi_encode_event(evt, devid, addr, info);
  264. amdvi_log_event(s, evt);
  265. pci_word_test_and_set_mask(s->pci.dev.config + PCI_STATUS,
  266. PCI_STATUS_SIG_TARGET_ABORT);
  267. }
  268. static gboolean amdvi_uint64_equal(gconstpointer v1, gconstpointer v2)
  269. {
  270. return *((const uint64_t *)v1) == *((const uint64_t *)v2);
  271. }
  272. static guint amdvi_uint64_hash(gconstpointer v)
  273. {
  274. return (guint)*(const uint64_t *)v;
  275. }
  276. static AMDVIIOTLBEntry *amdvi_iotlb_lookup(AMDVIState *s, hwaddr addr,
  277. uint64_t devid)
  278. {
  279. uint64_t key = (addr >> AMDVI_PAGE_SHIFT_4K) |
  280. ((uint64_t)(devid) << AMDVI_DEVID_SHIFT);
  281. return g_hash_table_lookup(s->iotlb, &key);
  282. }
  283. static void amdvi_iotlb_reset(AMDVIState *s)
  284. {
  285. assert(s->iotlb);
  286. trace_amdvi_iotlb_reset();
  287. g_hash_table_remove_all(s->iotlb);
  288. }
  289. static gboolean amdvi_iotlb_remove_by_devid(gpointer key, gpointer value,
  290. gpointer user_data)
  291. {
  292. AMDVIIOTLBEntry *entry = (AMDVIIOTLBEntry *)value;
  293. uint16_t devid = *(uint16_t *)user_data;
  294. return entry->devid == devid;
  295. }
  296. static void amdvi_iotlb_remove_page(AMDVIState *s, hwaddr addr,
  297. uint64_t devid)
  298. {
  299. uint64_t key = (addr >> AMDVI_PAGE_SHIFT_4K) |
  300. ((uint64_t)(devid) << AMDVI_DEVID_SHIFT);
  301. g_hash_table_remove(s->iotlb, &key);
  302. }
  303. static void amdvi_update_iotlb(AMDVIState *s, uint16_t devid,
  304. uint64_t gpa, IOMMUTLBEntry to_cache,
  305. uint16_t domid)
  306. {
  307. AMDVIIOTLBEntry *entry = g_new(AMDVIIOTLBEntry, 1);
  308. uint64_t *key = g_new(uint64_t, 1);
  309. uint64_t gfn = gpa >> AMDVI_PAGE_SHIFT_4K;
  310. /* don't cache erroneous translations */
  311. if (to_cache.perm != IOMMU_NONE) {
  312. trace_amdvi_cache_update(domid, PCI_BUS_NUM(devid), PCI_SLOT(devid),
  313. PCI_FUNC(devid), gpa, to_cache.translated_addr);
  314. if (g_hash_table_size(s->iotlb) >= AMDVI_IOTLB_MAX_SIZE) {
  315. amdvi_iotlb_reset(s);
  316. }
  317. entry->domid = domid;
  318. entry->perms = to_cache.perm;
  319. entry->translated_addr = to_cache.translated_addr;
  320. entry->page_mask = to_cache.addr_mask;
  321. *key = gfn | ((uint64_t)(devid) << AMDVI_DEVID_SHIFT);
  322. g_hash_table_replace(s->iotlb, key, entry);
  323. }
  324. }
  325. static void amdvi_completion_wait(AMDVIState *s, uint64_t *cmd)
  326. {
  327. /* pad the last 3 bits */
  328. hwaddr addr = cpu_to_le64(extract64(cmd[0], 3, 49)) << 3;
  329. uint64_t data = cpu_to_le64(cmd[1]);
  330. if (extract64(cmd[0], 52, 8)) {
  331. amdvi_log_illegalcom_error(s, extract64(cmd[0], 60, 4),
  332. s->cmdbuf + s->cmdbuf_head);
  333. }
  334. if (extract64(cmd[0], 0, 1)) {
  335. if (dma_memory_write(&address_space_memory, addr, &data,
  336. AMDVI_COMPLETION_DATA_SIZE,
  337. MEMTXATTRS_UNSPECIFIED)) {
  338. trace_amdvi_completion_wait_fail(addr);
  339. }
  340. }
  341. /* set completion interrupt */
  342. if (extract64(cmd[0], 1, 1)) {
  343. amdvi_assign_orq(s, AMDVI_MMIO_STATUS, AMDVI_MMIO_STATUS_COMP_INT);
  344. /* generate interrupt */
  345. amdvi_generate_msi_interrupt(s);
  346. }
  347. trace_amdvi_completion_wait(addr, data);
  348. }
  349. /* log error without aborting since linux seems to be using reserved bits */
  350. static void amdvi_inval_devtab_entry(AMDVIState *s, uint64_t *cmd)
  351. {
  352. uint16_t devid = cpu_to_le16((uint16_t)extract64(cmd[0], 0, 16));
  353. /* This command should invalidate internal caches of which there isn't */
  354. if (extract64(cmd[0], 16, 44) || cmd[1]) {
  355. amdvi_log_illegalcom_error(s, extract64(cmd[0], 60, 4),
  356. s->cmdbuf + s->cmdbuf_head);
  357. }
  358. trace_amdvi_devtab_inval(PCI_BUS_NUM(devid), PCI_SLOT(devid),
  359. PCI_FUNC(devid));
  360. }
  361. static void amdvi_complete_ppr(AMDVIState *s, uint64_t *cmd)
  362. {
  363. if (extract64(cmd[0], 16, 16) || extract64(cmd[0], 52, 8) ||
  364. extract64(cmd[1], 0, 2) || extract64(cmd[1], 3, 29)
  365. || extract64(cmd[1], 48, 16)) {
  366. amdvi_log_illegalcom_error(s, extract64(cmd[0], 60, 4),
  367. s->cmdbuf + s->cmdbuf_head);
  368. }
  369. trace_amdvi_ppr_exec();
  370. }
  371. static void amdvi_inval_all(AMDVIState *s, uint64_t *cmd)
  372. {
  373. if (extract64(cmd[0], 0, 60) || cmd[1]) {
  374. amdvi_log_illegalcom_error(s, extract64(cmd[0], 60, 4),
  375. s->cmdbuf + s->cmdbuf_head);
  376. }
  377. amdvi_iotlb_reset(s);
  378. trace_amdvi_all_inval();
  379. }
  380. static gboolean amdvi_iotlb_remove_by_domid(gpointer key, gpointer value,
  381. gpointer user_data)
  382. {
  383. AMDVIIOTLBEntry *entry = (AMDVIIOTLBEntry *)value;
  384. uint16_t domid = *(uint16_t *)user_data;
  385. return entry->domid == domid;
  386. }
  387. /* we don't have devid - we can't remove pages by address */
  388. static void amdvi_inval_pages(AMDVIState *s, uint64_t *cmd)
  389. {
  390. uint16_t domid = cpu_to_le16((uint16_t)extract64(cmd[0], 32, 16));
  391. if (extract64(cmd[0], 20, 12) || extract64(cmd[0], 48, 12) ||
  392. extract64(cmd[1], 3, 9)) {
  393. amdvi_log_illegalcom_error(s, extract64(cmd[0], 60, 4),
  394. s->cmdbuf + s->cmdbuf_head);
  395. }
  396. g_hash_table_foreach_remove(s->iotlb, amdvi_iotlb_remove_by_domid,
  397. &domid);
  398. trace_amdvi_pages_inval(domid);
  399. }
  400. static void amdvi_prefetch_pages(AMDVIState *s, uint64_t *cmd)
  401. {
  402. if (extract64(cmd[0], 16, 8) || extract64(cmd[0], 52, 8) ||
  403. extract64(cmd[1], 1, 1) || extract64(cmd[1], 3, 1) ||
  404. extract64(cmd[1], 5, 7)) {
  405. amdvi_log_illegalcom_error(s, extract64(cmd[0], 60, 4),
  406. s->cmdbuf + s->cmdbuf_head);
  407. }
  408. trace_amdvi_prefetch_pages();
  409. }
  410. static void amdvi_inval_inttable(AMDVIState *s, uint64_t *cmd)
  411. {
  412. if (extract64(cmd[0], 16, 44) || cmd[1]) {
  413. amdvi_log_illegalcom_error(s, extract64(cmd[0], 60, 4),
  414. s->cmdbuf + s->cmdbuf_head);
  415. return;
  416. }
  417. trace_amdvi_intr_inval();
  418. }
  419. /* FIXME: Try to work with the specified size instead of all the pages
  420. * when the S bit is on
  421. */
  422. static void iommu_inval_iotlb(AMDVIState *s, uint64_t *cmd)
  423. {
  424. uint16_t devid = extract64(cmd[0], 0, 16);
  425. if (extract64(cmd[1], 1, 1) || extract64(cmd[1], 3, 1) ||
  426. extract64(cmd[1], 6, 6)) {
  427. amdvi_log_illegalcom_error(s, extract64(cmd[0], 60, 4),
  428. s->cmdbuf + s->cmdbuf_head);
  429. return;
  430. }
  431. if (extract64(cmd[1], 0, 1)) {
  432. g_hash_table_foreach_remove(s->iotlb, amdvi_iotlb_remove_by_devid,
  433. &devid);
  434. } else {
  435. amdvi_iotlb_remove_page(s, cpu_to_le64(extract64(cmd[1], 12, 52)) << 12,
  436. cpu_to_le16(extract64(cmd[1], 0, 16)));
  437. }
  438. trace_amdvi_iotlb_inval();
  439. }
  440. /* not honouring reserved bits is regarded as an illegal command */
  441. static void amdvi_cmdbuf_exec(AMDVIState *s)
  442. {
  443. uint64_t cmd[2];
  444. if (dma_memory_read(&address_space_memory, s->cmdbuf + s->cmdbuf_head,
  445. cmd, AMDVI_COMMAND_SIZE, MEMTXATTRS_UNSPECIFIED)) {
  446. trace_amdvi_command_read_fail(s->cmdbuf, s->cmdbuf_head);
  447. amdvi_log_command_error(s, s->cmdbuf + s->cmdbuf_head);
  448. return;
  449. }
  450. switch (extract64(cmd[0], 60, 4)) {
  451. case AMDVI_CMD_COMPLETION_WAIT:
  452. amdvi_completion_wait(s, cmd);
  453. break;
  454. case AMDVI_CMD_INVAL_DEVTAB_ENTRY:
  455. amdvi_inval_devtab_entry(s, cmd);
  456. break;
  457. case AMDVI_CMD_INVAL_AMDVI_PAGES:
  458. amdvi_inval_pages(s, cmd);
  459. break;
  460. case AMDVI_CMD_INVAL_IOTLB_PAGES:
  461. iommu_inval_iotlb(s, cmd);
  462. break;
  463. case AMDVI_CMD_INVAL_INTR_TABLE:
  464. amdvi_inval_inttable(s, cmd);
  465. break;
  466. case AMDVI_CMD_PREFETCH_AMDVI_PAGES:
  467. amdvi_prefetch_pages(s, cmd);
  468. break;
  469. case AMDVI_CMD_COMPLETE_PPR_REQUEST:
  470. amdvi_complete_ppr(s, cmd);
  471. break;
  472. case AMDVI_CMD_INVAL_AMDVI_ALL:
  473. amdvi_inval_all(s, cmd);
  474. break;
  475. default:
  476. trace_amdvi_unhandled_command(extract64(cmd[1], 60, 4));
  477. /* log illegal command */
  478. amdvi_log_illegalcom_error(s, extract64(cmd[1], 60, 4),
  479. s->cmdbuf + s->cmdbuf_head);
  480. }
  481. }
  482. static void amdvi_cmdbuf_run(AMDVIState *s)
  483. {
  484. if (!s->cmdbuf_enabled) {
  485. trace_amdvi_command_error(amdvi_readq(s, AMDVI_MMIO_CONTROL));
  486. return;
  487. }
  488. /* check if there is work to do. */
  489. while (s->cmdbuf_head != s->cmdbuf_tail) {
  490. trace_amdvi_command_exec(s->cmdbuf_head, s->cmdbuf_tail, s->cmdbuf);
  491. amdvi_cmdbuf_exec(s);
  492. s->cmdbuf_head += AMDVI_COMMAND_SIZE;
  493. amdvi_writeq_raw(s, AMDVI_MMIO_COMMAND_HEAD, s->cmdbuf_head);
  494. /* wrap head pointer */
  495. if (s->cmdbuf_head >= s->cmdbuf_len * AMDVI_COMMAND_SIZE) {
  496. s->cmdbuf_head = 0;
  497. }
  498. }
  499. }
  500. static void amdvi_mmio_trace(hwaddr addr, unsigned size)
  501. {
  502. uint8_t index = (addr & ~0x2000) / 8;
  503. if ((addr & 0x2000)) {
  504. /* high table */
  505. index = index >= AMDVI_MMIO_REGS_HIGH ? AMDVI_MMIO_REGS_HIGH : index;
  506. trace_amdvi_mmio_read(amdvi_mmio_high[index], addr, size, addr & ~0x07);
  507. } else {
  508. index = index >= AMDVI_MMIO_REGS_LOW ? AMDVI_MMIO_REGS_LOW : index;
  509. trace_amdvi_mmio_read(amdvi_mmio_low[index], addr, size, addr & ~0x07);
  510. }
  511. }
  512. static uint64_t amdvi_mmio_read(void *opaque, hwaddr addr, unsigned size)
  513. {
  514. AMDVIState *s = opaque;
  515. uint64_t val = -1;
  516. if (addr + size > AMDVI_MMIO_SIZE) {
  517. trace_amdvi_mmio_read_invalid(AMDVI_MMIO_SIZE, addr, size);
  518. return (uint64_t)-1;
  519. }
  520. if (size == 2) {
  521. val = amdvi_readw(s, addr);
  522. } else if (size == 4) {
  523. val = amdvi_readl(s, addr);
  524. } else if (size == 8) {
  525. val = amdvi_readq(s, addr);
  526. }
  527. amdvi_mmio_trace(addr, size);
  528. return val;
  529. }
  530. static void amdvi_handle_control_write(AMDVIState *s)
  531. {
  532. unsigned long control = amdvi_readq(s, AMDVI_MMIO_CONTROL);
  533. s->enabled = !!(control & AMDVI_MMIO_CONTROL_AMDVIEN);
  534. s->ats_enabled = !!(control & AMDVI_MMIO_CONTROL_HTTUNEN);
  535. s->evtlog_enabled = s->enabled && !!(control &
  536. AMDVI_MMIO_CONTROL_EVENTLOGEN);
  537. s->evtlog_intr = !!(control & AMDVI_MMIO_CONTROL_EVENTINTEN);
  538. s->completion_wait_intr = !!(control & AMDVI_MMIO_CONTROL_COMWAITINTEN);
  539. s->cmdbuf_enabled = s->enabled && !!(control &
  540. AMDVI_MMIO_CONTROL_CMDBUFLEN);
  541. s->ga_enabled = !!(control & AMDVI_MMIO_CONTROL_GAEN);
  542. /* update the flags depending on the control register */
  543. if (s->cmdbuf_enabled) {
  544. amdvi_assign_orq(s, AMDVI_MMIO_STATUS, AMDVI_MMIO_STATUS_CMDBUF_RUN);
  545. } else {
  546. amdvi_assign_andq(s, AMDVI_MMIO_STATUS, ~AMDVI_MMIO_STATUS_CMDBUF_RUN);
  547. }
  548. if (s->evtlog_enabled) {
  549. amdvi_assign_orq(s, AMDVI_MMIO_STATUS, AMDVI_MMIO_STATUS_EVT_RUN);
  550. } else {
  551. amdvi_assign_andq(s, AMDVI_MMIO_STATUS, ~AMDVI_MMIO_STATUS_EVT_RUN);
  552. }
  553. trace_amdvi_control_status(control);
  554. amdvi_cmdbuf_run(s);
  555. }
  556. static inline void amdvi_handle_devtab_write(AMDVIState *s)
  557. {
  558. uint64_t val = amdvi_readq(s, AMDVI_MMIO_DEVICE_TABLE);
  559. s->devtab = (val & AMDVI_MMIO_DEVTAB_BASE_MASK);
  560. /* set device table length */
  561. s->devtab_len = ((val & AMDVI_MMIO_DEVTAB_SIZE_MASK) + 1 *
  562. (AMDVI_MMIO_DEVTAB_SIZE_UNIT /
  563. AMDVI_MMIO_DEVTAB_ENTRY_SIZE));
  564. }
  565. static inline void amdvi_handle_cmdhead_write(AMDVIState *s)
  566. {
  567. s->cmdbuf_head = amdvi_readq(s, AMDVI_MMIO_COMMAND_HEAD)
  568. & AMDVI_MMIO_CMDBUF_HEAD_MASK;
  569. amdvi_cmdbuf_run(s);
  570. }
  571. static inline void amdvi_handle_cmdbase_write(AMDVIState *s)
  572. {
  573. s->cmdbuf = amdvi_readq(s, AMDVI_MMIO_COMMAND_BASE)
  574. & AMDVI_MMIO_CMDBUF_BASE_MASK;
  575. s->cmdbuf_len = 1UL << (amdvi_readq(s, AMDVI_MMIO_CMDBUF_SIZE_BYTE)
  576. & AMDVI_MMIO_CMDBUF_SIZE_MASK);
  577. s->cmdbuf_head = s->cmdbuf_tail = 0;
  578. }
  579. static inline void amdvi_handle_cmdtail_write(AMDVIState *s)
  580. {
  581. s->cmdbuf_tail = amdvi_readq(s, AMDVI_MMIO_COMMAND_TAIL)
  582. & AMDVI_MMIO_CMDBUF_TAIL_MASK;
  583. amdvi_cmdbuf_run(s);
  584. }
  585. static inline void amdvi_handle_excllim_write(AMDVIState *s)
  586. {
  587. uint64_t val = amdvi_readq(s, AMDVI_MMIO_EXCL_LIMIT);
  588. s->excl_limit = (val & AMDVI_MMIO_EXCL_LIMIT_MASK) |
  589. AMDVI_MMIO_EXCL_LIMIT_LOW;
  590. }
  591. static inline void amdvi_handle_evtbase_write(AMDVIState *s)
  592. {
  593. uint64_t val = amdvi_readq(s, AMDVI_MMIO_EVENT_BASE);
  594. s->evtlog = val & AMDVI_MMIO_EVTLOG_BASE_MASK;
  595. s->evtlog_len = 1UL << (amdvi_readq(s, AMDVI_MMIO_EVTLOG_SIZE_BYTE)
  596. & AMDVI_MMIO_EVTLOG_SIZE_MASK);
  597. }
  598. static inline void amdvi_handle_evttail_write(AMDVIState *s)
  599. {
  600. uint64_t val = amdvi_readq(s, AMDVI_MMIO_EVENT_TAIL);
  601. s->evtlog_tail = val & AMDVI_MMIO_EVTLOG_TAIL_MASK;
  602. }
  603. static inline void amdvi_handle_evthead_write(AMDVIState *s)
  604. {
  605. uint64_t val = amdvi_readq(s, AMDVI_MMIO_EVENT_HEAD);
  606. s->evtlog_head = val & AMDVI_MMIO_EVTLOG_HEAD_MASK;
  607. }
  608. static inline void amdvi_handle_pprbase_write(AMDVIState *s)
  609. {
  610. uint64_t val = amdvi_readq(s, AMDVI_MMIO_PPR_BASE);
  611. s->ppr_log = val & AMDVI_MMIO_PPRLOG_BASE_MASK;
  612. s->pprlog_len = 1UL << (amdvi_readq(s, AMDVI_MMIO_PPRLOG_SIZE_BYTE)
  613. & AMDVI_MMIO_PPRLOG_SIZE_MASK);
  614. }
  615. static inline void amdvi_handle_pprhead_write(AMDVIState *s)
  616. {
  617. uint64_t val = amdvi_readq(s, AMDVI_MMIO_PPR_HEAD);
  618. s->pprlog_head = val & AMDVI_MMIO_PPRLOG_HEAD_MASK;
  619. }
  620. static inline void amdvi_handle_pprtail_write(AMDVIState *s)
  621. {
  622. uint64_t val = amdvi_readq(s, AMDVI_MMIO_PPR_TAIL);
  623. s->pprlog_tail = val & AMDVI_MMIO_PPRLOG_TAIL_MASK;
  624. }
  625. /* FIXME: something might go wrong if System Software writes in chunks
  626. * of one byte but linux writes in chunks of 4 bytes so currently it
  627. * works correctly with linux but will definitely be busted if software
  628. * reads/writes 8 bytes
  629. */
  630. static void amdvi_mmio_reg_write(AMDVIState *s, unsigned size, uint64_t val,
  631. hwaddr addr)
  632. {
  633. if (size == 2) {
  634. amdvi_writew(s, addr, val);
  635. } else if (size == 4) {
  636. amdvi_writel(s, addr, val);
  637. } else if (size == 8) {
  638. amdvi_writeq(s, addr, val);
  639. }
  640. }
  641. static void amdvi_mmio_write(void *opaque, hwaddr addr, uint64_t val,
  642. unsigned size)
  643. {
  644. AMDVIState *s = opaque;
  645. unsigned long offset = addr & 0x07;
  646. if (addr + size > AMDVI_MMIO_SIZE) {
  647. trace_amdvi_mmio_write("error: addr outside region: max ",
  648. (uint64_t)AMDVI_MMIO_SIZE, size, val, offset);
  649. return;
  650. }
  651. amdvi_mmio_trace(addr, size);
  652. switch (addr & ~0x07) {
  653. case AMDVI_MMIO_CONTROL:
  654. amdvi_mmio_reg_write(s, size, val, addr);
  655. amdvi_handle_control_write(s);
  656. break;
  657. case AMDVI_MMIO_DEVICE_TABLE:
  658. amdvi_mmio_reg_write(s, size, val, addr);
  659. /* set device table address
  660. * This also suffers from inability to tell whether software
  661. * is done writing
  662. */
  663. if (offset || (size == 8)) {
  664. amdvi_handle_devtab_write(s);
  665. }
  666. break;
  667. case AMDVI_MMIO_COMMAND_HEAD:
  668. amdvi_mmio_reg_write(s, size, val, addr);
  669. amdvi_handle_cmdhead_write(s);
  670. break;
  671. case AMDVI_MMIO_COMMAND_BASE:
  672. amdvi_mmio_reg_write(s, size, val, addr);
  673. /* FIXME - make sure System Software has finished writing incase
  674. * it writes in chucks less than 8 bytes in a robust way.As for
  675. * now, this hacks works for the linux driver
  676. */
  677. if (offset || (size == 8)) {
  678. amdvi_handle_cmdbase_write(s);
  679. }
  680. break;
  681. case AMDVI_MMIO_COMMAND_TAIL:
  682. amdvi_mmio_reg_write(s, size, val, addr);
  683. amdvi_handle_cmdtail_write(s);
  684. break;
  685. case AMDVI_MMIO_EVENT_BASE:
  686. amdvi_mmio_reg_write(s, size, val, addr);
  687. amdvi_handle_evtbase_write(s);
  688. break;
  689. case AMDVI_MMIO_EVENT_HEAD:
  690. amdvi_mmio_reg_write(s, size, val, addr);
  691. amdvi_handle_evthead_write(s);
  692. break;
  693. case AMDVI_MMIO_EVENT_TAIL:
  694. amdvi_mmio_reg_write(s, size, val, addr);
  695. amdvi_handle_evttail_write(s);
  696. break;
  697. case AMDVI_MMIO_EXCL_LIMIT:
  698. amdvi_mmio_reg_write(s, size, val, addr);
  699. amdvi_handle_excllim_write(s);
  700. break;
  701. /* PPR log base - unused for now */
  702. case AMDVI_MMIO_PPR_BASE:
  703. amdvi_mmio_reg_write(s, size, val, addr);
  704. amdvi_handle_pprbase_write(s);
  705. break;
  706. /* PPR log head - also unused for now */
  707. case AMDVI_MMIO_PPR_HEAD:
  708. amdvi_mmio_reg_write(s, size, val, addr);
  709. amdvi_handle_pprhead_write(s);
  710. break;
  711. /* PPR log tail - unused for now */
  712. case AMDVI_MMIO_PPR_TAIL:
  713. amdvi_mmio_reg_write(s, size, val, addr);
  714. amdvi_handle_pprtail_write(s);
  715. break;
  716. }
  717. }
  718. static inline uint64_t amdvi_get_perms(uint64_t entry)
  719. {
  720. return (entry & (AMDVI_DEV_PERM_READ | AMDVI_DEV_PERM_WRITE)) >>
  721. AMDVI_DEV_PERM_SHIFT;
  722. }
  723. /* validate that reserved bits are honoured */
  724. static bool amdvi_validate_dte(AMDVIState *s, uint16_t devid,
  725. uint64_t *dte)
  726. {
  727. if ((dte[0] & AMDVI_DTE_LOWER_QUAD_RESERVED)
  728. || (dte[1] & AMDVI_DTE_MIDDLE_QUAD_RESERVED)
  729. || (dte[2] & AMDVI_DTE_UPPER_QUAD_RESERVED) || dte[3]) {
  730. amdvi_log_illegaldevtab_error(s, devid,
  731. s->devtab +
  732. devid * AMDVI_DEVTAB_ENTRY_SIZE, 0);
  733. return false;
  734. }
  735. return true;
  736. }
  737. /* get a device table entry given the devid */
  738. static bool amdvi_get_dte(AMDVIState *s, int devid, uint64_t *entry)
  739. {
  740. uint32_t offset = devid * AMDVI_DEVTAB_ENTRY_SIZE;
  741. if (dma_memory_read(&address_space_memory, s->devtab + offset, entry,
  742. AMDVI_DEVTAB_ENTRY_SIZE, MEMTXATTRS_UNSPECIFIED)) {
  743. trace_amdvi_dte_get_fail(s->devtab, offset);
  744. /* log error accessing dte */
  745. amdvi_log_devtab_error(s, devid, s->devtab + offset, 0);
  746. return false;
  747. }
  748. *entry = le64_to_cpu(*entry);
  749. if (!amdvi_validate_dte(s, devid, entry)) {
  750. trace_amdvi_invalid_dte(entry[0]);
  751. return false;
  752. }
  753. return true;
  754. }
  755. /* get pte translation mode */
  756. static inline uint8_t get_pte_translation_mode(uint64_t pte)
  757. {
  758. return (pte >> AMDVI_DEV_MODE_RSHIFT) & AMDVI_DEV_MODE_MASK;
  759. }
  760. static inline uint64_t pte_override_page_mask(uint64_t pte)
  761. {
  762. uint8_t page_mask = 13;
  763. uint64_t addr = (pte & AMDVI_DEV_PT_ROOT_MASK) >> 12;
  764. /* find the first zero bit */
  765. while (addr & 1) {
  766. page_mask++;
  767. addr = addr >> 1;
  768. }
  769. return ~((1ULL << page_mask) - 1);
  770. }
  771. static inline uint64_t pte_get_page_mask(uint64_t oldlevel)
  772. {
  773. return ~((1UL << ((oldlevel * 9) + 3)) - 1);
  774. }
  775. static inline uint64_t amdvi_get_pte_entry(AMDVIState *s, uint64_t pte_addr,
  776. uint16_t devid)
  777. {
  778. uint64_t pte;
  779. if (dma_memory_read(&address_space_memory, pte_addr,
  780. &pte, sizeof(pte), MEMTXATTRS_UNSPECIFIED)) {
  781. trace_amdvi_get_pte_hwerror(pte_addr);
  782. amdvi_log_pagetab_error(s, devid, pte_addr, 0);
  783. pte = 0;
  784. return pte;
  785. }
  786. pte = le64_to_cpu(pte);
  787. return pte;
  788. }
  789. static void amdvi_page_walk(AMDVIAddressSpace *as, uint64_t *dte,
  790. IOMMUTLBEntry *ret, unsigned perms,
  791. hwaddr addr)
  792. {
  793. unsigned level, present, pte_perms, oldlevel;
  794. uint64_t pte = dte[0], pte_addr, page_mask;
  795. /* make sure the DTE has TV = 1 */
  796. if (pte & AMDVI_DEV_TRANSLATION_VALID) {
  797. level = get_pte_translation_mode(pte);
  798. if (level >= 7) {
  799. trace_amdvi_mode_invalid(level, addr);
  800. return;
  801. }
  802. if (level == 0) {
  803. goto no_remap;
  804. }
  805. /* we are at the leaf page table or page table encodes a huge page */
  806. while (level > 0) {
  807. pte_perms = amdvi_get_perms(pte);
  808. present = pte & 1;
  809. if (!present || perms != (perms & pte_perms)) {
  810. amdvi_page_fault(as->iommu_state, as->devfn, addr, perms);
  811. trace_amdvi_page_fault(addr);
  812. return;
  813. }
  814. /* go to the next lower level */
  815. pte_addr = pte & AMDVI_DEV_PT_ROOT_MASK;
  816. /* add offset and load pte */
  817. pte_addr += ((addr >> (3 + 9 * level)) & 0x1FF) << 3;
  818. pte = amdvi_get_pte_entry(as->iommu_state, pte_addr, as->devfn);
  819. if (!pte) {
  820. return;
  821. }
  822. oldlevel = level;
  823. level = get_pte_translation_mode(pte);
  824. if (level == 0x7) {
  825. break;
  826. }
  827. }
  828. if (level == 0x7) {
  829. page_mask = pte_override_page_mask(pte);
  830. } else {
  831. page_mask = pte_get_page_mask(oldlevel);
  832. }
  833. /* get access permissions from pte */
  834. ret->iova = addr & page_mask;
  835. ret->translated_addr = (pte & AMDVI_DEV_PT_ROOT_MASK) & page_mask;
  836. ret->addr_mask = ~page_mask;
  837. ret->perm = amdvi_get_perms(pte);
  838. return;
  839. }
  840. no_remap:
  841. ret->iova = addr & AMDVI_PAGE_MASK_4K;
  842. ret->translated_addr = addr & AMDVI_PAGE_MASK_4K;
  843. ret->addr_mask = ~AMDVI_PAGE_MASK_4K;
  844. ret->perm = amdvi_get_perms(pte);
  845. }
  846. static void amdvi_do_translate(AMDVIAddressSpace *as, hwaddr addr,
  847. bool is_write, IOMMUTLBEntry *ret)
  848. {
  849. AMDVIState *s = as->iommu_state;
  850. uint16_t devid = PCI_BUILD_BDF(as->bus_num, as->devfn);
  851. AMDVIIOTLBEntry *iotlb_entry = amdvi_iotlb_lookup(s, addr, devid);
  852. uint64_t entry[4];
  853. if (iotlb_entry) {
  854. trace_amdvi_iotlb_hit(PCI_BUS_NUM(devid), PCI_SLOT(devid),
  855. PCI_FUNC(devid), addr, iotlb_entry->translated_addr);
  856. ret->iova = addr & ~iotlb_entry->page_mask;
  857. ret->translated_addr = iotlb_entry->translated_addr;
  858. ret->addr_mask = iotlb_entry->page_mask;
  859. ret->perm = iotlb_entry->perms;
  860. return;
  861. }
  862. if (!amdvi_get_dte(s, devid, entry)) {
  863. return;
  864. }
  865. /* devices with V = 0 are not translated */
  866. if (!(entry[0] & AMDVI_DEV_VALID)) {
  867. goto out;
  868. }
  869. amdvi_page_walk(as, entry, ret,
  870. is_write ? AMDVI_PERM_WRITE : AMDVI_PERM_READ, addr);
  871. amdvi_update_iotlb(s, devid, addr, *ret,
  872. entry[1] & AMDVI_DEV_DOMID_ID_MASK);
  873. return;
  874. out:
  875. ret->iova = addr & AMDVI_PAGE_MASK_4K;
  876. ret->translated_addr = addr & AMDVI_PAGE_MASK_4K;
  877. ret->addr_mask = ~AMDVI_PAGE_MASK_4K;
  878. ret->perm = IOMMU_RW;
  879. }
  880. static inline bool amdvi_is_interrupt_addr(hwaddr addr)
  881. {
  882. return addr >= AMDVI_INT_ADDR_FIRST && addr <= AMDVI_INT_ADDR_LAST;
  883. }
  884. static IOMMUTLBEntry amdvi_translate(IOMMUMemoryRegion *iommu, hwaddr addr,
  885. IOMMUAccessFlags flag, int iommu_idx)
  886. {
  887. AMDVIAddressSpace *as = container_of(iommu, AMDVIAddressSpace, iommu);
  888. AMDVIState *s = as->iommu_state;
  889. IOMMUTLBEntry ret = {
  890. .target_as = &address_space_memory,
  891. .iova = addr,
  892. .translated_addr = 0,
  893. .addr_mask = ~(hwaddr)0,
  894. .perm = IOMMU_NONE
  895. };
  896. if (!s->enabled) {
  897. /* AMDVI disabled - corresponds to iommu=off not
  898. * failure to provide any parameter
  899. */
  900. ret.iova = addr & AMDVI_PAGE_MASK_4K;
  901. ret.translated_addr = addr & AMDVI_PAGE_MASK_4K;
  902. ret.addr_mask = ~AMDVI_PAGE_MASK_4K;
  903. ret.perm = IOMMU_RW;
  904. return ret;
  905. } else if (amdvi_is_interrupt_addr(addr)) {
  906. ret.iova = addr & AMDVI_PAGE_MASK_4K;
  907. ret.translated_addr = addr & AMDVI_PAGE_MASK_4K;
  908. ret.addr_mask = ~AMDVI_PAGE_MASK_4K;
  909. ret.perm = IOMMU_WO;
  910. return ret;
  911. }
  912. amdvi_do_translate(as, addr, flag & IOMMU_WO, &ret);
  913. trace_amdvi_translation_result(as->bus_num, PCI_SLOT(as->devfn),
  914. PCI_FUNC(as->devfn), addr, ret.translated_addr);
  915. return ret;
  916. }
  917. static int amdvi_get_irte(AMDVIState *s, MSIMessage *origin, uint64_t *dte,
  918. union irte *irte, uint16_t devid)
  919. {
  920. uint64_t irte_root, offset;
  921. irte_root = dte[2] & AMDVI_IR_PHYS_ADDR_MASK;
  922. offset = (origin->data & AMDVI_IRTE_OFFSET) << 2;
  923. trace_amdvi_ir_irte(irte_root, offset);
  924. if (dma_memory_read(&address_space_memory, irte_root + offset,
  925. irte, sizeof(*irte), MEMTXATTRS_UNSPECIFIED)) {
  926. trace_amdvi_ir_err("failed to get irte");
  927. return -AMDVI_IR_GET_IRTE;
  928. }
  929. trace_amdvi_ir_irte_val(irte->val);
  930. return 0;
  931. }
  932. static int amdvi_int_remap_legacy(AMDVIState *iommu,
  933. MSIMessage *origin,
  934. MSIMessage *translated,
  935. uint64_t *dte,
  936. X86IOMMUIrq *irq,
  937. uint16_t sid)
  938. {
  939. int ret;
  940. union irte irte;
  941. /* get interrupt remapping table */
  942. ret = amdvi_get_irte(iommu, origin, dte, &irte, sid);
  943. if (ret < 0) {
  944. return ret;
  945. }
  946. if (!irte.fields.valid) {
  947. trace_amdvi_ir_target_abort("RemapEn is disabled");
  948. return -AMDVI_IR_TARGET_ABORT;
  949. }
  950. if (irte.fields.guest_mode) {
  951. error_report_once("guest mode is not zero");
  952. return -AMDVI_IR_ERR;
  953. }
  954. if (irte.fields.int_type > AMDVI_IOAPIC_INT_TYPE_ARBITRATED) {
  955. error_report_once("reserved int_type");
  956. return -AMDVI_IR_ERR;
  957. }
  958. irq->delivery_mode = irte.fields.int_type;
  959. irq->vector = irte.fields.vector;
  960. irq->dest_mode = irte.fields.dm;
  961. irq->redir_hint = irte.fields.rq_eoi;
  962. irq->dest = irte.fields.destination;
  963. return 0;
  964. }
  965. static int amdvi_get_irte_ga(AMDVIState *s, MSIMessage *origin, uint64_t *dte,
  966. struct irte_ga *irte, uint16_t devid)
  967. {
  968. uint64_t irte_root, offset;
  969. irte_root = dte[2] & AMDVI_IR_PHYS_ADDR_MASK;
  970. offset = (origin->data & AMDVI_IRTE_OFFSET) << 4;
  971. trace_amdvi_ir_irte(irte_root, offset);
  972. if (dma_memory_read(&address_space_memory, irte_root + offset,
  973. irte, sizeof(*irte), MEMTXATTRS_UNSPECIFIED)) {
  974. trace_amdvi_ir_err("failed to get irte_ga");
  975. return -AMDVI_IR_GET_IRTE;
  976. }
  977. trace_amdvi_ir_irte_ga_val(irte->hi.val, irte->lo.val);
  978. return 0;
  979. }
  980. static int amdvi_int_remap_ga(AMDVIState *iommu,
  981. MSIMessage *origin,
  982. MSIMessage *translated,
  983. uint64_t *dte,
  984. X86IOMMUIrq *irq,
  985. uint16_t sid)
  986. {
  987. int ret;
  988. struct irte_ga irte;
  989. /* get interrupt remapping table */
  990. ret = amdvi_get_irte_ga(iommu, origin, dte, &irte, sid);
  991. if (ret < 0) {
  992. return ret;
  993. }
  994. if (!irte.lo.fields_remap.valid) {
  995. trace_amdvi_ir_target_abort("RemapEn is disabled");
  996. return -AMDVI_IR_TARGET_ABORT;
  997. }
  998. if (irte.lo.fields_remap.guest_mode) {
  999. error_report_once("guest mode is not zero");
  1000. return -AMDVI_IR_ERR;
  1001. }
  1002. if (irte.lo.fields_remap.int_type > AMDVI_IOAPIC_INT_TYPE_ARBITRATED) {
  1003. error_report_once("reserved int_type is set");
  1004. return -AMDVI_IR_ERR;
  1005. }
  1006. irq->delivery_mode = irte.lo.fields_remap.int_type;
  1007. irq->vector = irte.hi.fields.vector;
  1008. irq->dest_mode = irte.lo.fields_remap.dm;
  1009. irq->redir_hint = irte.lo.fields_remap.rq_eoi;
  1010. irq->dest = irte.lo.fields_remap.destination;
  1011. return 0;
  1012. }
  1013. static int __amdvi_int_remap_msi(AMDVIState *iommu,
  1014. MSIMessage *origin,
  1015. MSIMessage *translated,
  1016. uint64_t *dte,
  1017. X86IOMMUIrq *irq,
  1018. uint16_t sid)
  1019. {
  1020. int ret;
  1021. uint8_t int_ctl;
  1022. int_ctl = (dte[2] >> AMDVI_IR_INTCTL_SHIFT) & 3;
  1023. trace_amdvi_ir_intctl(int_ctl);
  1024. switch (int_ctl) {
  1025. case AMDVI_IR_INTCTL_PASS:
  1026. memcpy(translated, origin, sizeof(*origin));
  1027. return 0;
  1028. case AMDVI_IR_INTCTL_REMAP:
  1029. break;
  1030. case AMDVI_IR_INTCTL_ABORT:
  1031. trace_amdvi_ir_target_abort("int_ctl abort");
  1032. return -AMDVI_IR_TARGET_ABORT;
  1033. default:
  1034. trace_amdvi_ir_err("int_ctl reserved");
  1035. return -AMDVI_IR_ERR;
  1036. }
  1037. if (iommu->ga_enabled) {
  1038. ret = amdvi_int_remap_ga(iommu, origin, translated, dte, irq, sid);
  1039. } else {
  1040. ret = amdvi_int_remap_legacy(iommu, origin, translated, dte, irq, sid);
  1041. }
  1042. return ret;
  1043. }
  1044. /* Interrupt remapping for MSI/MSI-X entry */
  1045. static int amdvi_int_remap_msi(AMDVIState *iommu,
  1046. MSIMessage *origin,
  1047. MSIMessage *translated,
  1048. uint16_t sid)
  1049. {
  1050. int ret = 0;
  1051. uint64_t pass = 0;
  1052. uint64_t dte[4] = { 0 };
  1053. X86IOMMUIrq irq = { 0 };
  1054. uint8_t dest_mode, delivery_mode;
  1055. assert(origin && translated);
  1056. /*
  1057. * When IOMMU is enabled, interrupt remap request will come either from
  1058. * IO-APIC or PCI device. If interrupt is from PCI device then it will
  1059. * have a valid requester id but if the interrupt is from IO-APIC
  1060. * then requester id will be invalid.
  1061. */
  1062. if (sid == X86_IOMMU_SID_INVALID) {
  1063. sid = AMDVI_IOAPIC_SB_DEVID;
  1064. }
  1065. trace_amdvi_ir_remap_msi_req(origin->address, origin->data, sid);
  1066. /* check if device table entry is set before we go further. */
  1067. if (!iommu || !iommu->devtab_len) {
  1068. memcpy(translated, origin, sizeof(*origin));
  1069. goto out;
  1070. }
  1071. if (!amdvi_get_dte(iommu, sid, dte)) {
  1072. return -AMDVI_IR_ERR;
  1073. }
  1074. /* Check if IR is enabled in DTE */
  1075. if (!(dte[2] & AMDVI_IR_REMAP_ENABLE)) {
  1076. memcpy(translated, origin, sizeof(*origin));
  1077. goto out;
  1078. }
  1079. /* validate that we are configure with intremap=on */
  1080. if (!x86_iommu_ir_supported(X86_IOMMU_DEVICE(iommu))) {
  1081. trace_amdvi_err("Interrupt remapping is enabled in the guest but "
  1082. "not in the host. Use intremap=on to enable interrupt "
  1083. "remapping in amd-iommu.");
  1084. return -AMDVI_IR_ERR;
  1085. }
  1086. if (origin->address & AMDVI_MSI_ADDR_HI_MASK) {
  1087. trace_amdvi_err("MSI address high 32 bits non-zero when "
  1088. "Interrupt Remapping enabled.");
  1089. return -AMDVI_IR_ERR;
  1090. }
  1091. if ((origin->address & AMDVI_MSI_ADDR_LO_MASK) != APIC_DEFAULT_ADDRESS) {
  1092. trace_amdvi_err("MSI is not from IOAPIC.");
  1093. return -AMDVI_IR_ERR;
  1094. }
  1095. /*
  1096. * The MSI data register [10:8] are used to get the upstream interrupt type.
  1097. *
  1098. * See MSI/MSI-X format:
  1099. * https://pdfs.semanticscholar.org/presentation/9420/c279e942eca568157711ef5c92b800c40a79.pdf
  1100. * (page 5)
  1101. */
  1102. delivery_mode = (origin->data >> MSI_DATA_DELIVERY_MODE_SHIFT) & 7;
  1103. switch (delivery_mode) {
  1104. case AMDVI_IOAPIC_INT_TYPE_FIXED:
  1105. case AMDVI_IOAPIC_INT_TYPE_ARBITRATED:
  1106. trace_amdvi_ir_delivery_mode("fixed/arbitrated");
  1107. ret = __amdvi_int_remap_msi(iommu, origin, translated, dte, &irq, sid);
  1108. if (ret < 0) {
  1109. goto remap_fail;
  1110. } else {
  1111. /* Translate IRQ to MSI messages */
  1112. x86_iommu_irq_to_msi_message(&irq, translated);
  1113. goto out;
  1114. }
  1115. break;
  1116. case AMDVI_IOAPIC_INT_TYPE_SMI:
  1117. error_report("SMI is not supported!");
  1118. ret = -AMDVI_IR_ERR;
  1119. break;
  1120. case AMDVI_IOAPIC_INT_TYPE_NMI:
  1121. pass = dte[3] & AMDVI_DEV_NMI_PASS_MASK;
  1122. trace_amdvi_ir_delivery_mode("nmi");
  1123. break;
  1124. case AMDVI_IOAPIC_INT_TYPE_INIT:
  1125. pass = dte[3] & AMDVI_DEV_INT_PASS_MASK;
  1126. trace_amdvi_ir_delivery_mode("init");
  1127. break;
  1128. case AMDVI_IOAPIC_INT_TYPE_EINT:
  1129. pass = dte[3] & AMDVI_DEV_EINT_PASS_MASK;
  1130. trace_amdvi_ir_delivery_mode("eint");
  1131. break;
  1132. default:
  1133. trace_amdvi_ir_delivery_mode("unsupported delivery_mode");
  1134. ret = -AMDVI_IR_ERR;
  1135. break;
  1136. }
  1137. if (ret < 0) {
  1138. goto remap_fail;
  1139. }
  1140. /*
  1141. * The MSI address register bit[2] is used to get the destination
  1142. * mode. The dest_mode 1 is valid for fixed and arbitrated interrupts
  1143. * only.
  1144. */
  1145. dest_mode = (origin->address >> MSI_ADDR_DEST_MODE_SHIFT) & 1;
  1146. if (dest_mode) {
  1147. trace_amdvi_ir_err("invalid dest_mode");
  1148. ret = -AMDVI_IR_ERR;
  1149. goto remap_fail;
  1150. }
  1151. if (pass) {
  1152. memcpy(translated, origin, sizeof(*origin));
  1153. } else {
  1154. trace_amdvi_ir_err("passthrough is not enabled");
  1155. ret = -AMDVI_IR_ERR;
  1156. goto remap_fail;
  1157. }
  1158. out:
  1159. trace_amdvi_ir_remap_msi(origin->address, origin->data,
  1160. translated->address, translated->data);
  1161. return 0;
  1162. remap_fail:
  1163. return ret;
  1164. }
  1165. static int amdvi_int_remap(X86IOMMUState *iommu,
  1166. MSIMessage *origin,
  1167. MSIMessage *translated,
  1168. uint16_t sid)
  1169. {
  1170. return amdvi_int_remap_msi(AMD_IOMMU_DEVICE(iommu), origin,
  1171. translated, sid);
  1172. }
  1173. static MemTxResult amdvi_mem_ir_write(void *opaque, hwaddr addr,
  1174. uint64_t value, unsigned size,
  1175. MemTxAttrs attrs)
  1176. {
  1177. int ret;
  1178. MSIMessage from = { 0, 0 }, to = { 0, 0 };
  1179. uint16_t sid = AMDVI_IOAPIC_SB_DEVID;
  1180. from.address = (uint64_t) addr + AMDVI_INT_ADDR_FIRST;
  1181. from.data = (uint32_t) value;
  1182. trace_amdvi_mem_ir_write_req(addr, value, size);
  1183. if (!attrs.unspecified) {
  1184. /* We have explicit Source ID */
  1185. sid = attrs.requester_id;
  1186. }
  1187. ret = amdvi_int_remap_msi(opaque, &from, &to, sid);
  1188. if (ret < 0) {
  1189. /* TODO: log the event using IOMMU log event interface */
  1190. error_report_once("failed to remap interrupt from devid 0x%x", sid);
  1191. return MEMTX_ERROR;
  1192. }
  1193. apic_get_class()->send_msi(&to);
  1194. trace_amdvi_mem_ir_write(to.address, to.data);
  1195. return MEMTX_OK;
  1196. }
  1197. static MemTxResult amdvi_mem_ir_read(void *opaque, hwaddr addr,
  1198. uint64_t *data, unsigned size,
  1199. MemTxAttrs attrs)
  1200. {
  1201. return MEMTX_OK;
  1202. }
  1203. static const MemoryRegionOps amdvi_ir_ops = {
  1204. .read_with_attrs = amdvi_mem_ir_read,
  1205. .write_with_attrs = amdvi_mem_ir_write,
  1206. .endianness = DEVICE_LITTLE_ENDIAN,
  1207. .impl = {
  1208. .min_access_size = 4,
  1209. .max_access_size = 4,
  1210. },
  1211. .valid = {
  1212. .min_access_size = 4,
  1213. .max_access_size = 4,
  1214. }
  1215. };
  1216. static AddressSpace *amdvi_host_dma_iommu(PCIBus *bus, void *opaque, int devfn)
  1217. {
  1218. char name[128];
  1219. AMDVIState *s = opaque;
  1220. AMDVIAddressSpace **iommu_as, *amdvi_dev_as;
  1221. int bus_num = pci_bus_num(bus);
  1222. iommu_as = s->address_spaces[bus_num];
  1223. /* allocate memory during the first run */
  1224. if (!iommu_as) {
  1225. iommu_as = g_new0(AMDVIAddressSpace *, PCI_DEVFN_MAX);
  1226. s->address_spaces[bus_num] = iommu_as;
  1227. }
  1228. /* set up AMD-Vi region */
  1229. if (!iommu_as[devfn]) {
  1230. snprintf(name, sizeof(name), "amd_iommu_devfn_%d", devfn);
  1231. iommu_as[devfn] = g_new0(AMDVIAddressSpace, 1);
  1232. iommu_as[devfn]->bus_num = (uint8_t)bus_num;
  1233. iommu_as[devfn]->devfn = (uint8_t)devfn;
  1234. iommu_as[devfn]->iommu_state = s;
  1235. amdvi_dev_as = iommu_as[devfn];
  1236. /*
  1237. * Memory region relationships looks like (Address range shows
  1238. * only lower 32 bits to make it short in length...):
  1239. *
  1240. * |-----------------+-------------------+----------|
  1241. * | Name | Address range | Priority |
  1242. * |-----------------+-------------------+----------+
  1243. * | amdvi_root | 00000000-ffffffff | 0 |
  1244. * | amdvi_iommu | 00000000-ffffffff | 1 |
  1245. * | amdvi_iommu_ir | fee00000-feefffff | 64 |
  1246. * |-----------------+-------------------+----------|
  1247. */
  1248. memory_region_init_iommu(&amdvi_dev_as->iommu,
  1249. sizeof(amdvi_dev_as->iommu),
  1250. TYPE_AMD_IOMMU_MEMORY_REGION,
  1251. OBJECT(s),
  1252. "amd_iommu", UINT64_MAX);
  1253. memory_region_init(&amdvi_dev_as->root, OBJECT(s),
  1254. "amdvi_root", UINT64_MAX);
  1255. address_space_init(&amdvi_dev_as->as, &amdvi_dev_as->root, name);
  1256. memory_region_init_io(&amdvi_dev_as->iommu_ir, OBJECT(s),
  1257. &amdvi_ir_ops, s, "amd_iommu_ir",
  1258. AMDVI_INT_ADDR_SIZE);
  1259. memory_region_add_subregion_overlap(&amdvi_dev_as->root,
  1260. AMDVI_INT_ADDR_FIRST,
  1261. &amdvi_dev_as->iommu_ir,
  1262. 64);
  1263. memory_region_add_subregion_overlap(&amdvi_dev_as->root, 0,
  1264. MEMORY_REGION(&amdvi_dev_as->iommu),
  1265. 1);
  1266. }
  1267. return &iommu_as[devfn]->as;
  1268. }
  1269. static const MemoryRegionOps mmio_mem_ops = {
  1270. .read = amdvi_mmio_read,
  1271. .write = amdvi_mmio_write,
  1272. .endianness = DEVICE_LITTLE_ENDIAN,
  1273. .impl = {
  1274. .min_access_size = 1,
  1275. .max_access_size = 8,
  1276. .unaligned = false,
  1277. },
  1278. .valid = {
  1279. .min_access_size = 1,
  1280. .max_access_size = 8,
  1281. }
  1282. };
  1283. static int amdvi_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu,
  1284. IOMMUNotifierFlag old,
  1285. IOMMUNotifierFlag new,
  1286. Error **errp)
  1287. {
  1288. AMDVIAddressSpace *as = container_of(iommu, AMDVIAddressSpace, iommu);
  1289. if (new & IOMMU_NOTIFIER_MAP) {
  1290. error_setg(errp,
  1291. "device %02x.%02x.%x requires iommu notifier which is not "
  1292. "currently supported", as->bus_num, PCI_SLOT(as->devfn),
  1293. PCI_FUNC(as->devfn));
  1294. return -EINVAL;
  1295. }
  1296. return 0;
  1297. }
  1298. static void amdvi_init(AMDVIState *s)
  1299. {
  1300. amdvi_iotlb_reset(s);
  1301. s->devtab_len = 0;
  1302. s->cmdbuf_len = 0;
  1303. s->cmdbuf_head = 0;
  1304. s->cmdbuf_tail = 0;
  1305. s->evtlog_head = 0;
  1306. s->evtlog_tail = 0;
  1307. s->excl_enabled = false;
  1308. s->excl_allow = false;
  1309. s->mmio_enabled = false;
  1310. s->enabled = false;
  1311. s->ats_enabled = false;
  1312. s->cmdbuf_enabled = false;
  1313. /* reset MMIO */
  1314. memset(s->mmior, 0, AMDVI_MMIO_SIZE);
  1315. amdvi_set_quad(s, AMDVI_MMIO_EXT_FEATURES, AMDVI_EXT_FEATURES,
  1316. 0xffffffffffffffef, 0);
  1317. amdvi_set_quad(s, AMDVI_MMIO_STATUS, 0, 0x98, 0x67);
  1318. /* reset device ident */
  1319. pci_config_set_vendor_id(s->pci.dev.config, PCI_VENDOR_ID_AMD);
  1320. pci_config_set_prog_interface(s->pci.dev.config, 00);
  1321. pci_config_set_device_id(s->pci.dev.config, s->devid);
  1322. pci_config_set_class(s->pci.dev.config, 0x0806);
  1323. /* reset AMDVI specific capabilities, all r/o */
  1324. pci_set_long(s->pci.dev.config + s->capab_offset, AMDVI_CAPAB_FEATURES);
  1325. pci_set_long(s->pci.dev.config + s->capab_offset + AMDVI_CAPAB_BAR_LOW,
  1326. s->mmio.addr & ~(0xffff0000));
  1327. pci_set_long(s->pci.dev.config + s->capab_offset + AMDVI_CAPAB_BAR_HIGH,
  1328. (s->mmio.addr & ~(0xffff)) >> 16);
  1329. pci_set_long(s->pci.dev.config + s->capab_offset + AMDVI_CAPAB_RANGE,
  1330. 0xff000000);
  1331. pci_set_long(s->pci.dev.config + s->capab_offset + AMDVI_CAPAB_MISC, 0);
  1332. pci_set_long(s->pci.dev.config + s->capab_offset + AMDVI_CAPAB_MISC,
  1333. AMDVI_MAX_PH_ADDR | AMDVI_MAX_GVA_ADDR | AMDVI_MAX_VA_ADDR);
  1334. }
  1335. static void amdvi_sysbus_reset(DeviceState *dev)
  1336. {
  1337. AMDVIState *s = AMD_IOMMU_DEVICE(dev);
  1338. msi_reset(&s->pci.dev);
  1339. amdvi_init(s);
  1340. }
  1341. static void amdvi_sysbus_realize(DeviceState *dev, Error **errp)
  1342. {
  1343. int ret = 0;
  1344. AMDVIState *s = AMD_IOMMU_DEVICE(dev);
  1345. MachineState *ms = MACHINE(qdev_get_machine());
  1346. PCMachineState *pcms = PC_MACHINE(ms);
  1347. X86MachineState *x86ms = X86_MACHINE(ms);
  1348. PCIBus *bus = pcms->bus;
  1349. s->iotlb = g_hash_table_new_full(amdvi_uint64_hash,
  1350. amdvi_uint64_equal, g_free, g_free);
  1351. /* This device should take care of IOMMU PCI properties */
  1352. if (!qdev_realize(DEVICE(&s->pci), &bus->qbus, errp)) {
  1353. return;
  1354. }
  1355. ret = pci_add_capability(&s->pci.dev, AMDVI_CAPAB_ID_SEC, 0,
  1356. AMDVI_CAPAB_SIZE, errp);
  1357. if (ret < 0) {
  1358. return;
  1359. }
  1360. s->capab_offset = ret;
  1361. ret = pci_add_capability(&s->pci.dev, PCI_CAP_ID_MSI, 0,
  1362. AMDVI_CAPAB_REG_SIZE, errp);
  1363. if (ret < 0) {
  1364. return;
  1365. }
  1366. ret = pci_add_capability(&s->pci.dev, PCI_CAP_ID_HT, 0,
  1367. AMDVI_CAPAB_REG_SIZE, errp);
  1368. if (ret < 0) {
  1369. return;
  1370. }
  1371. /* Pseudo address space under root PCI bus. */
  1372. x86ms->ioapic_as = amdvi_host_dma_iommu(bus, s, AMDVI_IOAPIC_SB_DEVID);
  1373. /* set up MMIO */
  1374. memory_region_init_io(&s->mmio, OBJECT(s), &mmio_mem_ops, s, "amdvi-mmio",
  1375. AMDVI_MMIO_SIZE);
  1376. sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->mmio);
  1377. sysbus_mmio_map(SYS_BUS_DEVICE(s), 0, AMDVI_BASE_ADDR);
  1378. pci_setup_iommu(bus, amdvi_host_dma_iommu, s);
  1379. s->devid = object_property_get_int(OBJECT(&s->pci), "addr", &error_abort);
  1380. msi_init(&s->pci.dev, 0, 1, true, false, errp);
  1381. amdvi_init(s);
  1382. }
  1383. static const VMStateDescription vmstate_amdvi_sysbus = {
  1384. .name = "amd-iommu",
  1385. .unmigratable = 1
  1386. };
  1387. static void amdvi_sysbus_instance_init(Object *klass)
  1388. {
  1389. AMDVIState *s = AMD_IOMMU_DEVICE(klass);
  1390. object_initialize(&s->pci, sizeof(s->pci), TYPE_AMD_IOMMU_PCI);
  1391. }
  1392. static void amdvi_sysbus_class_init(ObjectClass *klass, void *data)
  1393. {
  1394. DeviceClass *dc = DEVICE_CLASS(klass);
  1395. X86IOMMUClass *dc_class = X86_IOMMU_DEVICE_CLASS(klass);
  1396. dc->reset = amdvi_sysbus_reset;
  1397. dc->vmsd = &vmstate_amdvi_sysbus;
  1398. dc->hotpluggable = false;
  1399. dc_class->realize = amdvi_sysbus_realize;
  1400. dc_class->int_remap = amdvi_int_remap;
  1401. /* Supported by the pc-q35-* machine types */
  1402. dc->user_creatable = true;
  1403. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  1404. dc->desc = "AMD IOMMU (AMD-Vi) DMA Remapping device";
  1405. }
  1406. static const TypeInfo amdvi_sysbus = {
  1407. .name = TYPE_AMD_IOMMU_DEVICE,
  1408. .parent = TYPE_X86_IOMMU_DEVICE,
  1409. .instance_size = sizeof(AMDVIState),
  1410. .instance_init = amdvi_sysbus_instance_init,
  1411. .class_init = amdvi_sysbus_class_init
  1412. };
  1413. static void amdvi_pci_class_init(ObjectClass *klass, void *data)
  1414. {
  1415. DeviceClass *dc = DEVICE_CLASS(klass);
  1416. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  1417. dc->desc = "AMD IOMMU (AMD-Vi) DMA Remapping device";
  1418. }
  1419. static const TypeInfo amdvi_pci = {
  1420. .name = TYPE_AMD_IOMMU_PCI,
  1421. .parent = TYPE_PCI_DEVICE,
  1422. .instance_size = sizeof(AMDVIPCIState),
  1423. .class_init = amdvi_pci_class_init,
  1424. .interfaces = (InterfaceInfo[]) {
  1425. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  1426. { },
  1427. },
  1428. };
  1429. static void amdvi_iommu_memory_region_class_init(ObjectClass *klass, void *data)
  1430. {
  1431. IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_CLASS(klass);
  1432. imrc->translate = amdvi_translate;
  1433. imrc->notify_flag_changed = amdvi_iommu_notify_flag_changed;
  1434. }
  1435. static const TypeInfo amdvi_iommu_memory_region_info = {
  1436. .parent = TYPE_IOMMU_MEMORY_REGION,
  1437. .name = TYPE_AMD_IOMMU_MEMORY_REGION,
  1438. .class_init = amdvi_iommu_memory_region_class_init,
  1439. };
  1440. static void amdvi_register_types(void)
  1441. {
  1442. type_register_static(&amdvi_pci);
  1443. type_register_static(&amdvi_sysbus);
  1444. type_register_static(&amdvi_iommu_memory_region_info);
  1445. }
  1446. type_init(amdvi_register_types);