pci.c 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331
  1. /*
  2. * vfio based device assignment support
  3. *
  4. * Copyright Red Hat, Inc. 2012
  5. *
  6. * Authors:
  7. * Alex Williamson <alex.williamson@redhat.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2. See
  10. * the COPYING file in the top-level directory.
  11. *
  12. * Based on qemu-kvm device-assignment:
  13. * Adapted for KVM by Qumranet.
  14. * Copyright (c) 2007, Neocleus, Alex Novik (alex@neocleus.com)
  15. * Copyright (c) 2007, Neocleus, Guy Zana (guy@neocleus.com)
  16. * Copyright (C) 2008, Qumranet, Amit Shah (amit.shah@qumranet.com)
  17. * Copyright (C) 2008, Red Hat, Amit Shah (amit.shah@redhat.com)
  18. * Copyright (C) 2008, IBM, Muli Ben-Yehuda (muli@il.ibm.com)
  19. */
  20. #include "qemu/osdep.h"
  21. #include <linux/vfio.h>
  22. #include <sys/ioctl.h>
  23. #include "hw/hw.h"
  24. #include "hw/pci/msi.h"
  25. #include "hw/pci/msix.h"
  26. #include "hw/pci/pci_bridge.h"
  27. #include "hw/qdev-properties.h"
  28. #include "hw/qdev-properties-system.h"
  29. #include "migration/vmstate.h"
  30. #include "qapi/qmp/qdict.h"
  31. #include "qemu/error-report.h"
  32. #include "qemu/main-loop.h"
  33. #include "qemu/module.h"
  34. #include "qemu/range.h"
  35. #include "qemu/units.h"
  36. #include "sysemu/kvm.h"
  37. #include "sysemu/runstate.h"
  38. #include "pci.h"
  39. #include "trace.h"
  40. #include "qapi/error.h"
  41. #include "migration/blocker.h"
  42. #include "migration/qemu-file.h"
  43. #define TYPE_VFIO_PCI_NOHOTPLUG "vfio-pci-nohotplug"
  44. static void vfio_disable_interrupts(VFIOPCIDevice *vdev);
  45. static void vfio_mmap_set_enabled(VFIOPCIDevice *vdev, bool enabled);
  46. /*
  47. * Disabling BAR mmaping can be slow, but toggling it around INTx can
  48. * also be a huge overhead. We try to get the best of both worlds by
  49. * waiting until an interrupt to disable mmaps (subsequent transitions
  50. * to the same state are effectively no overhead). If the interrupt has
  51. * been serviced and the time gap is long enough, we re-enable mmaps for
  52. * performance. This works well for things like graphics cards, which
  53. * may not use their interrupt at all and are penalized to an unusable
  54. * level by read/write BAR traps. Other devices, like NICs, have more
  55. * regular interrupts and see much better latency by staying in non-mmap
  56. * mode. We therefore set the default mmap_timeout such that a ping
  57. * is just enough to keep the mmap disabled. Users can experiment with
  58. * other options with the x-intx-mmap-timeout-ms parameter (a value of
  59. * zero disables the timer).
  60. */
  61. static void vfio_intx_mmap_enable(void *opaque)
  62. {
  63. VFIOPCIDevice *vdev = opaque;
  64. if (vdev->intx.pending) {
  65. timer_mod(vdev->intx.mmap_timer,
  66. qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + vdev->intx.mmap_timeout);
  67. return;
  68. }
  69. vfio_mmap_set_enabled(vdev, true);
  70. }
  71. static void vfio_intx_interrupt(void *opaque)
  72. {
  73. VFIOPCIDevice *vdev = opaque;
  74. if (!event_notifier_test_and_clear(&vdev->intx.interrupt)) {
  75. return;
  76. }
  77. trace_vfio_intx_interrupt(vdev->vbasedev.name, 'A' + vdev->intx.pin);
  78. vdev->intx.pending = true;
  79. pci_irq_assert(&vdev->pdev);
  80. vfio_mmap_set_enabled(vdev, false);
  81. if (vdev->intx.mmap_timeout) {
  82. timer_mod(vdev->intx.mmap_timer,
  83. qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + vdev->intx.mmap_timeout);
  84. }
  85. }
  86. static void vfio_intx_eoi(VFIODevice *vbasedev)
  87. {
  88. VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
  89. if (!vdev->intx.pending) {
  90. return;
  91. }
  92. trace_vfio_intx_eoi(vbasedev->name);
  93. vdev->intx.pending = false;
  94. pci_irq_deassert(&vdev->pdev);
  95. vfio_unmask_single_irqindex(vbasedev, VFIO_PCI_INTX_IRQ_INDEX);
  96. }
  97. static void vfio_intx_enable_kvm(VFIOPCIDevice *vdev, Error **errp)
  98. {
  99. #ifdef CONFIG_KVM
  100. int irq_fd = event_notifier_get_fd(&vdev->intx.interrupt);
  101. if (vdev->no_kvm_intx || !kvm_irqfds_enabled() ||
  102. vdev->intx.route.mode != PCI_INTX_ENABLED ||
  103. !kvm_resamplefds_enabled()) {
  104. return;
  105. }
  106. /* Get to a known interrupt state */
  107. qemu_set_fd_handler(irq_fd, NULL, NULL, vdev);
  108. vfio_mask_single_irqindex(&vdev->vbasedev, VFIO_PCI_INTX_IRQ_INDEX);
  109. vdev->intx.pending = false;
  110. pci_irq_deassert(&vdev->pdev);
  111. /* Get an eventfd for resample/unmask */
  112. if (event_notifier_init(&vdev->intx.unmask, 0)) {
  113. error_setg(errp, "event_notifier_init failed eoi");
  114. goto fail;
  115. }
  116. if (kvm_irqchip_add_irqfd_notifier_gsi(kvm_state,
  117. &vdev->intx.interrupt,
  118. &vdev->intx.unmask,
  119. vdev->intx.route.irq)) {
  120. error_setg_errno(errp, errno, "failed to setup resample irqfd");
  121. goto fail_irqfd;
  122. }
  123. if (vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_INTX_IRQ_INDEX, 0,
  124. VFIO_IRQ_SET_ACTION_UNMASK,
  125. event_notifier_get_fd(&vdev->intx.unmask),
  126. errp)) {
  127. goto fail_vfio;
  128. }
  129. /* Let'em rip */
  130. vfio_unmask_single_irqindex(&vdev->vbasedev, VFIO_PCI_INTX_IRQ_INDEX);
  131. vdev->intx.kvm_accel = true;
  132. trace_vfio_intx_enable_kvm(vdev->vbasedev.name);
  133. return;
  134. fail_vfio:
  135. kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, &vdev->intx.interrupt,
  136. vdev->intx.route.irq);
  137. fail_irqfd:
  138. event_notifier_cleanup(&vdev->intx.unmask);
  139. fail:
  140. qemu_set_fd_handler(irq_fd, vfio_intx_interrupt, NULL, vdev);
  141. vfio_unmask_single_irqindex(&vdev->vbasedev, VFIO_PCI_INTX_IRQ_INDEX);
  142. #endif
  143. }
  144. static void vfio_intx_disable_kvm(VFIOPCIDevice *vdev)
  145. {
  146. #ifdef CONFIG_KVM
  147. if (!vdev->intx.kvm_accel) {
  148. return;
  149. }
  150. /*
  151. * Get to a known state, hardware masked, QEMU ready to accept new
  152. * interrupts, QEMU IRQ de-asserted.
  153. */
  154. vfio_mask_single_irqindex(&vdev->vbasedev, VFIO_PCI_INTX_IRQ_INDEX);
  155. vdev->intx.pending = false;
  156. pci_irq_deassert(&vdev->pdev);
  157. /* Tell KVM to stop listening for an INTx irqfd */
  158. if (kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, &vdev->intx.interrupt,
  159. vdev->intx.route.irq)) {
  160. error_report("vfio: Error: Failed to disable INTx irqfd: %m");
  161. }
  162. /* We only need to close the eventfd for VFIO to cleanup the kernel side */
  163. event_notifier_cleanup(&vdev->intx.unmask);
  164. /* QEMU starts listening for interrupt events. */
  165. qemu_set_fd_handler(event_notifier_get_fd(&vdev->intx.interrupt),
  166. vfio_intx_interrupt, NULL, vdev);
  167. vdev->intx.kvm_accel = false;
  168. /* If we've missed an event, let it re-fire through QEMU */
  169. vfio_unmask_single_irqindex(&vdev->vbasedev, VFIO_PCI_INTX_IRQ_INDEX);
  170. trace_vfio_intx_disable_kvm(vdev->vbasedev.name);
  171. #endif
  172. }
  173. static void vfio_intx_update(VFIOPCIDevice *vdev, PCIINTxRoute *route)
  174. {
  175. Error *err = NULL;
  176. trace_vfio_intx_update(vdev->vbasedev.name,
  177. vdev->intx.route.irq, route->irq);
  178. vfio_intx_disable_kvm(vdev);
  179. vdev->intx.route = *route;
  180. if (route->mode != PCI_INTX_ENABLED) {
  181. return;
  182. }
  183. vfio_intx_enable_kvm(vdev, &err);
  184. if (err) {
  185. warn_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);
  186. }
  187. /* Re-enable the interrupt in cased we missed an EOI */
  188. vfio_intx_eoi(&vdev->vbasedev);
  189. }
  190. static void vfio_intx_routing_notifier(PCIDevice *pdev)
  191. {
  192. VFIOPCIDevice *vdev = VFIO_PCI(pdev);
  193. PCIINTxRoute route;
  194. if (vdev->interrupt != VFIO_INT_INTx) {
  195. return;
  196. }
  197. route = pci_device_route_intx_to_irq(&vdev->pdev, vdev->intx.pin);
  198. if (pci_intx_route_changed(&vdev->intx.route, &route)) {
  199. vfio_intx_update(vdev, &route);
  200. }
  201. }
  202. static void vfio_irqchip_change(Notifier *notify, void *data)
  203. {
  204. VFIOPCIDevice *vdev = container_of(notify, VFIOPCIDevice,
  205. irqchip_change_notifier);
  206. vfio_intx_update(vdev, &vdev->intx.route);
  207. }
  208. static int vfio_intx_enable(VFIOPCIDevice *vdev, Error **errp)
  209. {
  210. uint8_t pin = vfio_pci_read_config(&vdev->pdev, PCI_INTERRUPT_PIN, 1);
  211. Error *err = NULL;
  212. int32_t fd;
  213. int ret;
  214. if (!pin) {
  215. return 0;
  216. }
  217. vfio_disable_interrupts(vdev);
  218. vdev->intx.pin = pin - 1; /* Pin A (1) -> irq[0] */
  219. pci_config_set_interrupt_pin(vdev->pdev.config, pin);
  220. #ifdef CONFIG_KVM
  221. /*
  222. * Only conditional to avoid generating error messages on platforms
  223. * where we won't actually use the result anyway.
  224. */
  225. if (kvm_irqfds_enabled() && kvm_resamplefds_enabled()) {
  226. vdev->intx.route = pci_device_route_intx_to_irq(&vdev->pdev,
  227. vdev->intx.pin);
  228. }
  229. #endif
  230. ret = event_notifier_init(&vdev->intx.interrupt, 0);
  231. if (ret) {
  232. error_setg_errno(errp, -ret, "event_notifier_init failed");
  233. return ret;
  234. }
  235. fd = event_notifier_get_fd(&vdev->intx.interrupt);
  236. qemu_set_fd_handler(fd, vfio_intx_interrupt, NULL, vdev);
  237. if (vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_INTX_IRQ_INDEX, 0,
  238. VFIO_IRQ_SET_ACTION_TRIGGER, fd, errp)) {
  239. qemu_set_fd_handler(fd, NULL, NULL, vdev);
  240. event_notifier_cleanup(&vdev->intx.interrupt);
  241. return -errno;
  242. }
  243. vfio_intx_enable_kvm(vdev, &err);
  244. if (err) {
  245. warn_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);
  246. }
  247. vdev->interrupt = VFIO_INT_INTx;
  248. trace_vfio_intx_enable(vdev->vbasedev.name);
  249. return 0;
  250. }
  251. static void vfio_intx_disable(VFIOPCIDevice *vdev)
  252. {
  253. int fd;
  254. timer_del(vdev->intx.mmap_timer);
  255. vfio_intx_disable_kvm(vdev);
  256. vfio_disable_irqindex(&vdev->vbasedev, VFIO_PCI_INTX_IRQ_INDEX);
  257. vdev->intx.pending = false;
  258. pci_irq_deassert(&vdev->pdev);
  259. vfio_mmap_set_enabled(vdev, true);
  260. fd = event_notifier_get_fd(&vdev->intx.interrupt);
  261. qemu_set_fd_handler(fd, NULL, NULL, vdev);
  262. event_notifier_cleanup(&vdev->intx.interrupt);
  263. vdev->interrupt = VFIO_INT_NONE;
  264. trace_vfio_intx_disable(vdev->vbasedev.name);
  265. }
  266. /*
  267. * MSI/X
  268. */
  269. static void vfio_msi_interrupt(void *opaque)
  270. {
  271. VFIOMSIVector *vector = opaque;
  272. VFIOPCIDevice *vdev = vector->vdev;
  273. MSIMessage (*get_msg)(PCIDevice *dev, unsigned vector);
  274. void (*notify)(PCIDevice *dev, unsigned vector);
  275. MSIMessage msg;
  276. int nr = vector - vdev->msi_vectors;
  277. if (!event_notifier_test_and_clear(&vector->interrupt)) {
  278. return;
  279. }
  280. if (vdev->interrupt == VFIO_INT_MSIX) {
  281. get_msg = msix_get_message;
  282. notify = msix_notify;
  283. /* A masked vector firing needs to use the PBA, enable it */
  284. if (msix_is_masked(&vdev->pdev, nr)) {
  285. set_bit(nr, vdev->msix->pending);
  286. memory_region_set_enabled(&vdev->pdev.msix_pba_mmio, true);
  287. trace_vfio_msix_pba_enable(vdev->vbasedev.name);
  288. }
  289. } else if (vdev->interrupt == VFIO_INT_MSI) {
  290. get_msg = msi_get_message;
  291. notify = msi_notify;
  292. } else {
  293. abort();
  294. }
  295. msg = get_msg(&vdev->pdev, nr);
  296. trace_vfio_msi_interrupt(vdev->vbasedev.name, nr, msg.address, msg.data);
  297. notify(&vdev->pdev, nr);
  298. }
  299. static int vfio_enable_vectors(VFIOPCIDevice *vdev, bool msix)
  300. {
  301. struct vfio_irq_set *irq_set;
  302. int ret = 0, i, argsz;
  303. int32_t *fds;
  304. argsz = sizeof(*irq_set) + (vdev->nr_vectors * sizeof(*fds));
  305. irq_set = g_malloc0(argsz);
  306. irq_set->argsz = argsz;
  307. irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
  308. irq_set->index = msix ? VFIO_PCI_MSIX_IRQ_INDEX : VFIO_PCI_MSI_IRQ_INDEX;
  309. irq_set->start = 0;
  310. irq_set->count = vdev->nr_vectors;
  311. fds = (int32_t *)&irq_set->data;
  312. for (i = 0; i < vdev->nr_vectors; i++) {
  313. int fd = -1;
  314. /*
  315. * MSI vs MSI-X - The guest has direct access to MSI mask and pending
  316. * bits, therefore we always use the KVM signaling path when setup.
  317. * MSI-X mask and pending bits are emulated, so we want to use the
  318. * KVM signaling path only when configured and unmasked.
  319. */
  320. if (vdev->msi_vectors[i].use) {
  321. if (vdev->msi_vectors[i].virq < 0 ||
  322. (msix && msix_is_masked(&vdev->pdev, i))) {
  323. fd = event_notifier_get_fd(&vdev->msi_vectors[i].interrupt);
  324. } else {
  325. fd = event_notifier_get_fd(&vdev->msi_vectors[i].kvm_interrupt);
  326. }
  327. }
  328. fds[i] = fd;
  329. }
  330. ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_SET_IRQS, irq_set);
  331. g_free(irq_set);
  332. return ret;
  333. }
  334. static void vfio_add_kvm_msi_virq(VFIOPCIDevice *vdev, VFIOMSIVector *vector,
  335. int vector_n, bool msix)
  336. {
  337. KVMRouteChange c;
  338. int virq;
  339. if ((msix && vdev->no_kvm_msix) || (!msix && vdev->no_kvm_msi)) {
  340. return;
  341. }
  342. if (event_notifier_init(&vector->kvm_interrupt, 0)) {
  343. return;
  344. }
  345. c = kvm_irqchip_begin_route_changes(kvm_state);
  346. virq = kvm_irqchip_add_msi_route(&c, vector_n, &vdev->pdev);
  347. if (virq < 0) {
  348. event_notifier_cleanup(&vector->kvm_interrupt);
  349. return;
  350. }
  351. kvm_irqchip_commit_route_changes(&c);
  352. if (kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, &vector->kvm_interrupt,
  353. NULL, virq) < 0) {
  354. kvm_irqchip_release_virq(kvm_state, virq);
  355. event_notifier_cleanup(&vector->kvm_interrupt);
  356. return;
  357. }
  358. vector->virq = virq;
  359. }
  360. static void vfio_remove_kvm_msi_virq(VFIOMSIVector *vector)
  361. {
  362. kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, &vector->kvm_interrupt,
  363. vector->virq);
  364. kvm_irqchip_release_virq(kvm_state, vector->virq);
  365. vector->virq = -1;
  366. event_notifier_cleanup(&vector->kvm_interrupt);
  367. }
  368. static void vfio_update_kvm_msi_virq(VFIOMSIVector *vector, MSIMessage msg,
  369. PCIDevice *pdev)
  370. {
  371. kvm_irqchip_update_msi_route(kvm_state, vector->virq, msg, pdev);
  372. kvm_irqchip_commit_routes(kvm_state);
  373. }
  374. static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr,
  375. MSIMessage *msg, IOHandler *handler)
  376. {
  377. VFIOPCIDevice *vdev = VFIO_PCI(pdev);
  378. VFIOMSIVector *vector;
  379. int ret;
  380. trace_vfio_msix_vector_do_use(vdev->vbasedev.name, nr);
  381. vector = &vdev->msi_vectors[nr];
  382. if (!vector->use) {
  383. vector->vdev = vdev;
  384. vector->virq = -1;
  385. if (event_notifier_init(&vector->interrupt, 0)) {
  386. error_report("vfio: Error: event_notifier_init failed");
  387. }
  388. vector->use = true;
  389. msix_vector_use(pdev, nr);
  390. }
  391. qemu_set_fd_handler(event_notifier_get_fd(&vector->interrupt),
  392. handler, NULL, vector);
  393. /*
  394. * Attempt to enable route through KVM irqchip,
  395. * default to userspace handling if unavailable.
  396. */
  397. if (vector->virq >= 0) {
  398. if (!msg) {
  399. vfio_remove_kvm_msi_virq(vector);
  400. } else {
  401. vfio_update_kvm_msi_virq(vector, *msg, pdev);
  402. }
  403. } else {
  404. if (msg) {
  405. vfio_add_kvm_msi_virq(vdev, vector, nr, true);
  406. }
  407. }
  408. /*
  409. * We don't want to have the host allocate all possible MSI vectors
  410. * for a device if they're not in use, so we shutdown and incrementally
  411. * increase them as needed.
  412. */
  413. if (vdev->nr_vectors < nr + 1) {
  414. vfio_disable_irqindex(&vdev->vbasedev, VFIO_PCI_MSIX_IRQ_INDEX);
  415. vdev->nr_vectors = nr + 1;
  416. ret = vfio_enable_vectors(vdev, true);
  417. if (ret) {
  418. error_report("vfio: failed to enable vectors, %d", ret);
  419. }
  420. } else {
  421. Error *err = NULL;
  422. int32_t fd;
  423. if (vector->virq >= 0) {
  424. fd = event_notifier_get_fd(&vector->kvm_interrupt);
  425. } else {
  426. fd = event_notifier_get_fd(&vector->interrupt);
  427. }
  428. if (vfio_set_irq_signaling(&vdev->vbasedev,
  429. VFIO_PCI_MSIX_IRQ_INDEX, nr,
  430. VFIO_IRQ_SET_ACTION_TRIGGER, fd, &err)) {
  431. error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);
  432. }
  433. }
  434. /* Disable PBA emulation when nothing more is pending. */
  435. clear_bit(nr, vdev->msix->pending);
  436. if (find_first_bit(vdev->msix->pending,
  437. vdev->nr_vectors) == vdev->nr_vectors) {
  438. memory_region_set_enabled(&vdev->pdev.msix_pba_mmio, false);
  439. trace_vfio_msix_pba_disable(vdev->vbasedev.name);
  440. }
  441. return 0;
  442. }
  443. static int vfio_msix_vector_use(PCIDevice *pdev,
  444. unsigned int nr, MSIMessage msg)
  445. {
  446. return vfio_msix_vector_do_use(pdev, nr, &msg, vfio_msi_interrupt);
  447. }
  448. static void vfio_msix_vector_release(PCIDevice *pdev, unsigned int nr)
  449. {
  450. VFIOPCIDevice *vdev = VFIO_PCI(pdev);
  451. VFIOMSIVector *vector = &vdev->msi_vectors[nr];
  452. trace_vfio_msix_vector_release(vdev->vbasedev.name, nr);
  453. /*
  454. * There are still old guests that mask and unmask vectors on every
  455. * interrupt. If we're using QEMU bypass with a KVM irqfd, leave all of
  456. * the KVM setup in place, simply switch VFIO to use the non-bypass
  457. * eventfd. We'll then fire the interrupt through QEMU and the MSI-X
  458. * core will mask the interrupt and set pending bits, allowing it to
  459. * be re-asserted on unmask. Nothing to do if already using QEMU mode.
  460. */
  461. if (vector->virq >= 0) {
  462. int32_t fd = event_notifier_get_fd(&vector->interrupt);
  463. Error *err = NULL;
  464. if (vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_MSIX_IRQ_INDEX, nr,
  465. VFIO_IRQ_SET_ACTION_TRIGGER, fd, &err)) {
  466. error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);
  467. }
  468. }
  469. }
  470. static void vfio_msix_enable(VFIOPCIDevice *vdev)
  471. {
  472. PCIDevice *pdev = &vdev->pdev;
  473. unsigned int nr, max_vec = 0;
  474. vfio_disable_interrupts(vdev);
  475. vdev->msi_vectors = g_new0(VFIOMSIVector, vdev->msix->entries);
  476. vdev->interrupt = VFIO_INT_MSIX;
  477. /*
  478. * Some communication channels between VF & PF or PF & fw rely on the
  479. * physical state of the device and expect that enabling MSI-X from the
  480. * guest enables the same on the host. When our guest is Linux, the
  481. * guest driver call to pci_enable_msix() sets the enabling bit in the
  482. * MSI-X capability, but leaves the vector table masked. We therefore
  483. * can't rely on a vector_use callback (from request_irq() in the guest)
  484. * to switch the physical device into MSI-X mode because that may come a
  485. * long time after pci_enable_msix(). This code enables vector 0 with
  486. * triggering to userspace, then immediately release the vector, leaving
  487. * the physical device with no vectors enabled, but MSI-X enabled, just
  488. * like the guest view.
  489. * If there are already unmasked vectors (in migration resume phase and
  490. * some guest startups) which will be enabled soon, we can allocate all
  491. * of them here to avoid inefficiently disabling and enabling vectors
  492. * repeatedly later.
  493. */
  494. if (!pdev->msix_function_masked) {
  495. for (nr = 0; nr < msix_nr_vectors_allocated(pdev); nr++) {
  496. if (!msix_is_masked(pdev, nr)) {
  497. max_vec = nr;
  498. }
  499. }
  500. }
  501. vfio_msix_vector_do_use(pdev, max_vec, NULL, NULL);
  502. vfio_msix_vector_release(pdev, max_vec);
  503. if (msix_set_vector_notifiers(pdev, vfio_msix_vector_use,
  504. vfio_msix_vector_release, NULL)) {
  505. error_report("vfio: msix_set_vector_notifiers failed");
  506. }
  507. trace_vfio_msix_enable(vdev->vbasedev.name);
  508. }
  509. static void vfio_msi_enable(VFIOPCIDevice *vdev)
  510. {
  511. int ret, i;
  512. vfio_disable_interrupts(vdev);
  513. vdev->nr_vectors = msi_nr_vectors_allocated(&vdev->pdev);
  514. retry:
  515. vdev->msi_vectors = g_new0(VFIOMSIVector, vdev->nr_vectors);
  516. for (i = 0; i < vdev->nr_vectors; i++) {
  517. VFIOMSIVector *vector = &vdev->msi_vectors[i];
  518. vector->vdev = vdev;
  519. vector->virq = -1;
  520. vector->use = true;
  521. if (event_notifier_init(&vector->interrupt, 0)) {
  522. error_report("vfio: Error: event_notifier_init failed");
  523. }
  524. qemu_set_fd_handler(event_notifier_get_fd(&vector->interrupt),
  525. vfio_msi_interrupt, NULL, vector);
  526. /*
  527. * Attempt to enable route through KVM irqchip,
  528. * default to userspace handling if unavailable.
  529. */
  530. vfio_add_kvm_msi_virq(vdev, vector, i, false);
  531. }
  532. /* Set interrupt type prior to possible interrupts */
  533. vdev->interrupt = VFIO_INT_MSI;
  534. ret = vfio_enable_vectors(vdev, false);
  535. if (ret) {
  536. if (ret < 0) {
  537. error_report("vfio: Error: Failed to setup MSI fds: %m");
  538. } else if (ret != vdev->nr_vectors) {
  539. error_report("vfio: Error: Failed to enable %d "
  540. "MSI vectors, retry with %d", vdev->nr_vectors, ret);
  541. }
  542. for (i = 0; i < vdev->nr_vectors; i++) {
  543. VFIOMSIVector *vector = &vdev->msi_vectors[i];
  544. if (vector->virq >= 0) {
  545. vfio_remove_kvm_msi_virq(vector);
  546. }
  547. qemu_set_fd_handler(event_notifier_get_fd(&vector->interrupt),
  548. NULL, NULL, NULL);
  549. event_notifier_cleanup(&vector->interrupt);
  550. }
  551. g_free(vdev->msi_vectors);
  552. vdev->msi_vectors = NULL;
  553. if (ret > 0 && ret != vdev->nr_vectors) {
  554. vdev->nr_vectors = ret;
  555. goto retry;
  556. }
  557. vdev->nr_vectors = 0;
  558. /*
  559. * Failing to setup MSI doesn't really fall within any specification.
  560. * Let's try leaving interrupts disabled and hope the guest figures
  561. * out to fall back to INTx for this device.
  562. */
  563. error_report("vfio: Error: Failed to enable MSI");
  564. vdev->interrupt = VFIO_INT_NONE;
  565. return;
  566. }
  567. trace_vfio_msi_enable(vdev->vbasedev.name, vdev->nr_vectors);
  568. }
  569. static void vfio_msi_disable_common(VFIOPCIDevice *vdev)
  570. {
  571. Error *err = NULL;
  572. int i;
  573. for (i = 0; i < vdev->nr_vectors; i++) {
  574. VFIOMSIVector *vector = &vdev->msi_vectors[i];
  575. if (vdev->msi_vectors[i].use) {
  576. if (vector->virq >= 0) {
  577. vfio_remove_kvm_msi_virq(vector);
  578. }
  579. qemu_set_fd_handler(event_notifier_get_fd(&vector->interrupt),
  580. NULL, NULL, NULL);
  581. event_notifier_cleanup(&vector->interrupt);
  582. }
  583. }
  584. g_free(vdev->msi_vectors);
  585. vdev->msi_vectors = NULL;
  586. vdev->nr_vectors = 0;
  587. vdev->interrupt = VFIO_INT_NONE;
  588. vfio_intx_enable(vdev, &err);
  589. if (err) {
  590. error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);
  591. }
  592. }
  593. static void vfio_msix_disable(VFIOPCIDevice *vdev)
  594. {
  595. int i;
  596. msix_unset_vector_notifiers(&vdev->pdev);
  597. /*
  598. * MSI-X will only release vectors if MSI-X is still enabled on the
  599. * device, check through the rest and release it ourselves if necessary.
  600. */
  601. for (i = 0; i < vdev->nr_vectors; i++) {
  602. if (vdev->msi_vectors[i].use) {
  603. vfio_msix_vector_release(&vdev->pdev, i);
  604. msix_vector_unuse(&vdev->pdev, i);
  605. }
  606. }
  607. if (vdev->nr_vectors) {
  608. vfio_disable_irqindex(&vdev->vbasedev, VFIO_PCI_MSIX_IRQ_INDEX);
  609. }
  610. vfio_msi_disable_common(vdev);
  611. memset(vdev->msix->pending, 0,
  612. BITS_TO_LONGS(vdev->msix->entries) * sizeof(unsigned long));
  613. trace_vfio_msix_disable(vdev->vbasedev.name);
  614. }
  615. static void vfio_msi_disable(VFIOPCIDevice *vdev)
  616. {
  617. vfio_disable_irqindex(&vdev->vbasedev, VFIO_PCI_MSI_IRQ_INDEX);
  618. vfio_msi_disable_common(vdev);
  619. trace_vfio_msi_disable(vdev->vbasedev.name);
  620. }
  621. static void vfio_update_msi(VFIOPCIDevice *vdev)
  622. {
  623. int i;
  624. for (i = 0; i < vdev->nr_vectors; i++) {
  625. VFIOMSIVector *vector = &vdev->msi_vectors[i];
  626. MSIMessage msg;
  627. if (!vector->use || vector->virq < 0) {
  628. continue;
  629. }
  630. msg = msi_get_message(&vdev->pdev, i);
  631. vfio_update_kvm_msi_virq(vector, msg, &vdev->pdev);
  632. }
  633. }
  634. static void vfio_pci_load_rom(VFIOPCIDevice *vdev)
  635. {
  636. struct vfio_region_info *reg_info;
  637. uint64_t size;
  638. off_t off = 0;
  639. ssize_t bytes;
  640. if (vfio_get_region_info(&vdev->vbasedev,
  641. VFIO_PCI_ROM_REGION_INDEX, &reg_info)) {
  642. error_report("vfio: Error getting ROM info: %m");
  643. return;
  644. }
  645. trace_vfio_pci_load_rom(vdev->vbasedev.name, (unsigned long)reg_info->size,
  646. (unsigned long)reg_info->offset,
  647. (unsigned long)reg_info->flags);
  648. vdev->rom_size = size = reg_info->size;
  649. vdev->rom_offset = reg_info->offset;
  650. g_free(reg_info);
  651. if (!vdev->rom_size) {
  652. vdev->rom_read_failed = true;
  653. error_report("vfio-pci: Cannot read device rom at "
  654. "%s", vdev->vbasedev.name);
  655. error_printf("Device option ROM contents are probably invalid "
  656. "(check dmesg).\nSkip option ROM probe with rombar=0, "
  657. "or load from file with romfile=\n");
  658. return;
  659. }
  660. vdev->rom = g_malloc(size);
  661. memset(vdev->rom, 0xff, size);
  662. while (size) {
  663. bytes = pread(vdev->vbasedev.fd, vdev->rom + off,
  664. size, vdev->rom_offset + off);
  665. if (bytes == 0) {
  666. break;
  667. } else if (bytes > 0) {
  668. off += bytes;
  669. size -= bytes;
  670. } else {
  671. if (errno == EINTR || errno == EAGAIN) {
  672. continue;
  673. }
  674. error_report("vfio: Error reading device ROM: %m");
  675. break;
  676. }
  677. }
  678. /*
  679. * Test the ROM signature against our device, if the vendor is correct
  680. * but the device ID doesn't match, store the correct device ID and
  681. * recompute the checksum. Intel IGD devices need this and are known
  682. * to have bogus checksums so we can't simply adjust the checksum.
  683. */
  684. if (pci_get_word(vdev->rom) == 0xaa55 &&
  685. pci_get_word(vdev->rom + 0x18) + 8 < vdev->rom_size &&
  686. !memcmp(vdev->rom + pci_get_word(vdev->rom + 0x18), "PCIR", 4)) {
  687. uint16_t vid, did;
  688. vid = pci_get_word(vdev->rom + pci_get_word(vdev->rom + 0x18) + 4);
  689. did = pci_get_word(vdev->rom + pci_get_word(vdev->rom + 0x18) + 6);
  690. if (vid == vdev->vendor_id && did != vdev->device_id) {
  691. int i;
  692. uint8_t csum, *data = vdev->rom;
  693. pci_set_word(vdev->rom + pci_get_word(vdev->rom + 0x18) + 6,
  694. vdev->device_id);
  695. data[6] = 0;
  696. for (csum = 0, i = 0; i < vdev->rom_size; i++) {
  697. csum += data[i];
  698. }
  699. data[6] = -csum;
  700. }
  701. }
  702. }
  703. static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
  704. {
  705. VFIOPCIDevice *vdev = opaque;
  706. union {
  707. uint8_t byte;
  708. uint16_t word;
  709. uint32_t dword;
  710. uint64_t qword;
  711. } val;
  712. uint64_t data = 0;
  713. /* Load the ROM lazily when the guest tries to read it */
  714. if (unlikely(!vdev->rom && !vdev->rom_read_failed)) {
  715. vfio_pci_load_rom(vdev);
  716. }
  717. memcpy(&val, vdev->rom + addr,
  718. (addr < vdev->rom_size) ? MIN(size, vdev->rom_size - addr) : 0);
  719. switch (size) {
  720. case 1:
  721. data = val.byte;
  722. break;
  723. case 2:
  724. data = le16_to_cpu(val.word);
  725. break;
  726. case 4:
  727. data = le32_to_cpu(val.dword);
  728. break;
  729. default:
  730. hw_error("vfio: unsupported read size, %d bytes\n", size);
  731. break;
  732. }
  733. trace_vfio_rom_read(vdev->vbasedev.name, addr, size, data);
  734. return data;
  735. }
  736. static void vfio_rom_write(void *opaque, hwaddr addr,
  737. uint64_t data, unsigned size)
  738. {
  739. }
  740. static const MemoryRegionOps vfio_rom_ops = {
  741. .read = vfio_rom_read,
  742. .write = vfio_rom_write,
  743. .endianness = DEVICE_LITTLE_ENDIAN,
  744. };
  745. static void vfio_pci_size_rom(VFIOPCIDevice *vdev)
  746. {
  747. uint32_t orig, size = cpu_to_le32((uint32_t)PCI_ROM_ADDRESS_MASK);
  748. off_t offset = vdev->config_offset + PCI_ROM_ADDRESS;
  749. DeviceState *dev = DEVICE(vdev);
  750. char *name;
  751. int fd = vdev->vbasedev.fd;
  752. if (vdev->pdev.romfile || !vdev->pdev.rom_bar) {
  753. /* Since pci handles romfile, just print a message and return */
  754. if (vfio_opt_rom_in_denylist(vdev) && vdev->pdev.romfile) {
  755. warn_report("Device at %s is known to cause system instability"
  756. " issues during option rom execution",
  757. vdev->vbasedev.name);
  758. error_printf("Proceeding anyway since user specified romfile\n");
  759. }
  760. return;
  761. }
  762. /*
  763. * Use the same size ROM BAR as the physical device. The contents
  764. * will get filled in later when the guest tries to read it.
  765. */
  766. if (pread(fd, &orig, 4, offset) != 4 ||
  767. pwrite(fd, &size, 4, offset) != 4 ||
  768. pread(fd, &size, 4, offset) != 4 ||
  769. pwrite(fd, &orig, 4, offset) != 4) {
  770. error_report("%s(%s) failed: %m", __func__, vdev->vbasedev.name);
  771. return;
  772. }
  773. size = ~(le32_to_cpu(size) & PCI_ROM_ADDRESS_MASK) + 1;
  774. if (!size) {
  775. return;
  776. }
  777. if (vfio_opt_rom_in_denylist(vdev)) {
  778. if (dev->opts && qdict_haskey(dev->opts, "rombar")) {
  779. warn_report("Device at %s is known to cause system instability"
  780. " issues during option rom execution",
  781. vdev->vbasedev.name);
  782. error_printf("Proceeding anyway since user specified"
  783. " non zero value for rombar\n");
  784. } else {
  785. warn_report("Rom loading for device at %s has been disabled"
  786. " due to system instability issues",
  787. vdev->vbasedev.name);
  788. error_printf("Specify rombar=1 or romfile to force\n");
  789. return;
  790. }
  791. }
  792. trace_vfio_pci_size_rom(vdev->vbasedev.name, size);
  793. name = g_strdup_printf("vfio[%s].rom", vdev->vbasedev.name);
  794. memory_region_init_io(&vdev->pdev.rom, OBJECT(vdev),
  795. &vfio_rom_ops, vdev, name, size);
  796. g_free(name);
  797. pci_register_bar(&vdev->pdev, PCI_ROM_SLOT,
  798. PCI_BASE_ADDRESS_SPACE_MEMORY, &vdev->pdev.rom);
  799. vdev->rom_read_failed = false;
  800. }
  801. void vfio_vga_write(void *opaque, hwaddr addr,
  802. uint64_t data, unsigned size)
  803. {
  804. VFIOVGARegion *region = opaque;
  805. VFIOVGA *vga = container_of(region, VFIOVGA, region[region->nr]);
  806. union {
  807. uint8_t byte;
  808. uint16_t word;
  809. uint32_t dword;
  810. uint64_t qword;
  811. } buf;
  812. off_t offset = vga->fd_offset + region->offset + addr;
  813. switch (size) {
  814. case 1:
  815. buf.byte = data;
  816. break;
  817. case 2:
  818. buf.word = cpu_to_le16(data);
  819. break;
  820. case 4:
  821. buf.dword = cpu_to_le32(data);
  822. break;
  823. default:
  824. hw_error("vfio: unsupported write size, %d bytes", size);
  825. break;
  826. }
  827. if (pwrite(vga->fd, &buf, size, offset) != size) {
  828. error_report("%s(,0x%"HWADDR_PRIx", 0x%"PRIx64", %d) failed: %m",
  829. __func__, region->offset + addr, data, size);
  830. }
  831. trace_vfio_vga_write(region->offset + addr, data, size);
  832. }
  833. uint64_t vfio_vga_read(void *opaque, hwaddr addr, unsigned size)
  834. {
  835. VFIOVGARegion *region = opaque;
  836. VFIOVGA *vga = container_of(region, VFIOVGA, region[region->nr]);
  837. union {
  838. uint8_t byte;
  839. uint16_t word;
  840. uint32_t dword;
  841. uint64_t qword;
  842. } buf;
  843. uint64_t data = 0;
  844. off_t offset = vga->fd_offset + region->offset + addr;
  845. if (pread(vga->fd, &buf, size, offset) != size) {
  846. error_report("%s(,0x%"HWADDR_PRIx", %d) failed: %m",
  847. __func__, region->offset + addr, size);
  848. return (uint64_t)-1;
  849. }
  850. switch (size) {
  851. case 1:
  852. data = buf.byte;
  853. break;
  854. case 2:
  855. data = le16_to_cpu(buf.word);
  856. break;
  857. case 4:
  858. data = le32_to_cpu(buf.dword);
  859. break;
  860. default:
  861. hw_error("vfio: unsupported read size, %d bytes", size);
  862. break;
  863. }
  864. trace_vfio_vga_read(region->offset + addr, size, data);
  865. return data;
  866. }
  867. static const MemoryRegionOps vfio_vga_ops = {
  868. .read = vfio_vga_read,
  869. .write = vfio_vga_write,
  870. .endianness = DEVICE_LITTLE_ENDIAN,
  871. };
  872. /*
  873. * Expand memory region of sub-page(size < PAGE_SIZE) MMIO BAR to page
  874. * size if the BAR is in an exclusive page in host so that we could map
  875. * this BAR to guest. But this sub-page BAR may not occupy an exclusive
  876. * page in guest. So we should set the priority of the expanded memory
  877. * region to zero in case of overlap with BARs which share the same page
  878. * with the sub-page BAR in guest. Besides, we should also recover the
  879. * size of this sub-page BAR when its base address is changed in guest
  880. * and not page aligned any more.
  881. */
  882. static void vfio_sub_page_bar_update_mapping(PCIDevice *pdev, int bar)
  883. {
  884. VFIOPCIDevice *vdev = VFIO_PCI(pdev);
  885. VFIORegion *region = &vdev->bars[bar].region;
  886. MemoryRegion *mmap_mr, *region_mr, *base_mr;
  887. PCIIORegion *r;
  888. pcibus_t bar_addr;
  889. uint64_t size = region->size;
  890. /* Make sure that the whole region is allowed to be mmapped */
  891. if (region->nr_mmaps != 1 || !region->mmaps[0].mmap ||
  892. region->mmaps[0].size != region->size) {
  893. return;
  894. }
  895. r = &pdev->io_regions[bar];
  896. bar_addr = r->addr;
  897. base_mr = vdev->bars[bar].mr;
  898. region_mr = region->mem;
  899. mmap_mr = &region->mmaps[0].mem;
  900. /* If BAR is mapped and page aligned, update to fill PAGE_SIZE */
  901. if (bar_addr != PCI_BAR_UNMAPPED &&
  902. !(bar_addr & ~qemu_real_host_page_mask)) {
  903. size = qemu_real_host_page_size;
  904. }
  905. memory_region_transaction_begin();
  906. if (vdev->bars[bar].size < size) {
  907. memory_region_set_size(base_mr, size);
  908. }
  909. memory_region_set_size(region_mr, size);
  910. memory_region_set_size(mmap_mr, size);
  911. if (size != vdev->bars[bar].size && memory_region_is_mapped(base_mr)) {
  912. memory_region_del_subregion(r->address_space, base_mr);
  913. memory_region_add_subregion_overlap(r->address_space,
  914. bar_addr, base_mr, 0);
  915. }
  916. memory_region_transaction_commit();
  917. }
  918. /*
  919. * PCI config space
  920. */
  921. uint32_t vfio_pci_read_config(PCIDevice *pdev, uint32_t addr, int len)
  922. {
  923. VFIOPCIDevice *vdev = VFIO_PCI(pdev);
  924. uint32_t emu_bits = 0, emu_val = 0, phys_val = 0, val;
  925. memcpy(&emu_bits, vdev->emulated_config_bits + addr, len);
  926. emu_bits = le32_to_cpu(emu_bits);
  927. if (emu_bits) {
  928. emu_val = pci_default_read_config(pdev, addr, len);
  929. }
  930. if (~emu_bits & (0xffffffffU >> (32 - len * 8))) {
  931. ssize_t ret;
  932. ret = pread(vdev->vbasedev.fd, &phys_val, len,
  933. vdev->config_offset + addr);
  934. if (ret != len) {
  935. error_report("%s(%s, 0x%x, 0x%x) failed: %m",
  936. __func__, vdev->vbasedev.name, addr, len);
  937. return -errno;
  938. }
  939. phys_val = le32_to_cpu(phys_val);
  940. }
  941. val = (emu_val & emu_bits) | (phys_val & ~emu_bits);
  942. trace_vfio_pci_read_config(vdev->vbasedev.name, addr, len, val);
  943. return val;
  944. }
  945. void vfio_pci_write_config(PCIDevice *pdev,
  946. uint32_t addr, uint32_t val, int len)
  947. {
  948. VFIOPCIDevice *vdev = VFIO_PCI(pdev);
  949. uint32_t val_le = cpu_to_le32(val);
  950. trace_vfio_pci_write_config(vdev->vbasedev.name, addr, val, len);
  951. /* Write everything to VFIO, let it filter out what we can't write */
  952. if (pwrite(vdev->vbasedev.fd, &val_le, len, vdev->config_offset + addr)
  953. != len) {
  954. error_report("%s(%s, 0x%x, 0x%x, 0x%x) failed: %m",
  955. __func__, vdev->vbasedev.name, addr, val, len);
  956. }
  957. /* MSI/MSI-X Enabling/Disabling */
  958. if (pdev->cap_present & QEMU_PCI_CAP_MSI &&
  959. ranges_overlap(addr, len, pdev->msi_cap, vdev->msi_cap_size)) {
  960. int is_enabled, was_enabled = msi_enabled(pdev);
  961. pci_default_write_config(pdev, addr, val, len);
  962. is_enabled = msi_enabled(pdev);
  963. if (!was_enabled) {
  964. if (is_enabled) {
  965. vfio_msi_enable(vdev);
  966. }
  967. } else {
  968. if (!is_enabled) {
  969. vfio_msi_disable(vdev);
  970. } else {
  971. vfio_update_msi(vdev);
  972. }
  973. }
  974. } else if (pdev->cap_present & QEMU_PCI_CAP_MSIX &&
  975. ranges_overlap(addr, len, pdev->msix_cap, MSIX_CAP_LENGTH)) {
  976. int is_enabled, was_enabled = msix_enabled(pdev);
  977. pci_default_write_config(pdev, addr, val, len);
  978. is_enabled = msix_enabled(pdev);
  979. if (!was_enabled && is_enabled) {
  980. vfio_msix_enable(vdev);
  981. } else if (was_enabled && !is_enabled) {
  982. vfio_msix_disable(vdev);
  983. }
  984. } else if (ranges_overlap(addr, len, PCI_BASE_ADDRESS_0, 24) ||
  985. range_covers_byte(addr, len, PCI_COMMAND)) {
  986. pcibus_t old_addr[PCI_NUM_REGIONS - 1];
  987. int bar;
  988. for (bar = 0; bar < PCI_ROM_SLOT; bar++) {
  989. old_addr[bar] = pdev->io_regions[bar].addr;
  990. }
  991. pci_default_write_config(pdev, addr, val, len);
  992. for (bar = 0; bar < PCI_ROM_SLOT; bar++) {
  993. if (old_addr[bar] != pdev->io_regions[bar].addr &&
  994. vdev->bars[bar].region.size > 0 &&
  995. vdev->bars[bar].region.size < qemu_real_host_page_size) {
  996. vfio_sub_page_bar_update_mapping(pdev, bar);
  997. }
  998. }
  999. } else {
  1000. /* Write everything to QEMU to keep emulated bits correct */
  1001. pci_default_write_config(pdev, addr, val, len);
  1002. }
  1003. }
  1004. /*
  1005. * Interrupt setup
  1006. */
  1007. static void vfio_disable_interrupts(VFIOPCIDevice *vdev)
  1008. {
  1009. /*
  1010. * More complicated than it looks. Disabling MSI/X transitions the
  1011. * device to INTx mode (if supported). Therefore we need to first
  1012. * disable MSI/X and then cleanup by disabling INTx.
  1013. */
  1014. if (vdev->interrupt == VFIO_INT_MSIX) {
  1015. vfio_msix_disable(vdev);
  1016. } else if (vdev->interrupt == VFIO_INT_MSI) {
  1017. vfio_msi_disable(vdev);
  1018. }
  1019. if (vdev->interrupt == VFIO_INT_INTx) {
  1020. vfio_intx_disable(vdev);
  1021. }
  1022. }
  1023. static int vfio_msi_setup(VFIOPCIDevice *vdev, int pos, Error **errp)
  1024. {
  1025. uint16_t ctrl;
  1026. bool msi_64bit, msi_maskbit;
  1027. int ret, entries;
  1028. Error *err = NULL;
  1029. if (pread(vdev->vbasedev.fd, &ctrl, sizeof(ctrl),
  1030. vdev->config_offset + pos + PCI_CAP_FLAGS) != sizeof(ctrl)) {
  1031. error_setg_errno(errp, errno, "failed reading MSI PCI_CAP_FLAGS");
  1032. return -errno;
  1033. }
  1034. ctrl = le16_to_cpu(ctrl);
  1035. msi_64bit = !!(ctrl & PCI_MSI_FLAGS_64BIT);
  1036. msi_maskbit = !!(ctrl & PCI_MSI_FLAGS_MASKBIT);
  1037. entries = 1 << ((ctrl & PCI_MSI_FLAGS_QMASK) >> 1);
  1038. trace_vfio_msi_setup(vdev->vbasedev.name, pos);
  1039. ret = msi_init(&vdev->pdev, pos, entries, msi_64bit, msi_maskbit, &err);
  1040. if (ret < 0) {
  1041. if (ret == -ENOTSUP) {
  1042. return 0;
  1043. }
  1044. error_propagate_prepend(errp, err, "msi_init failed: ");
  1045. return ret;
  1046. }
  1047. vdev->msi_cap_size = 0xa + (msi_maskbit ? 0xa : 0) + (msi_64bit ? 0x4 : 0);
  1048. return 0;
  1049. }
  1050. static void vfio_pci_fixup_msix_region(VFIOPCIDevice *vdev)
  1051. {
  1052. off_t start, end;
  1053. VFIORegion *region = &vdev->bars[vdev->msix->table_bar].region;
  1054. /*
  1055. * If the host driver allows mapping of a MSIX data, we are going to
  1056. * do map the entire BAR and emulate MSIX table on top of that.
  1057. */
  1058. if (vfio_has_region_cap(&vdev->vbasedev, region->nr,
  1059. VFIO_REGION_INFO_CAP_MSIX_MAPPABLE)) {
  1060. return;
  1061. }
  1062. /*
  1063. * We expect to find a single mmap covering the whole BAR, anything else
  1064. * means it's either unsupported or already setup.
  1065. */
  1066. if (region->nr_mmaps != 1 || region->mmaps[0].offset ||
  1067. region->size != region->mmaps[0].size) {
  1068. return;
  1069. }
  1070. /* MSI-X table start and end aligned to host page size */
  1071. start = vdev->msix->table_offset & qemu_real_host_page_mask;
  1072. end = REAL_HOST_PAGE_ALIGN((uint64_t)vdev->msix->table_offset +
  1073. (vdev->msix->entries * PCI_MSIX_ENTRY_SIZE));
  1074. /*
  1075. * Does the MSI-X table cover the beginning of the BAR? The whole BAR?
  1076. * NB - Host page size is necessarily a power of two and so is the PCI
  1077. * BAR (not counting EA yet), therefore if we have host page aligned
  1078. * @start and @end, then any remainder of the BAR before or after those
  1079. * must be at least host page sized and therefore mmap'able.
  1080. */
  1081. if (!start) {
  1082. if (end >= region->size) {
  1083. region->nr_mmaps = 0;
  1084. g_free(region->mmaps);
  1085. region->mmaps = NULL;
  1086. trace_vfio_msix_fixup(vdev->vbasedev.name,
  1087. vdev->msix->table_bar, 0, 0);
  1088. } else {
  1089. region->mmaps[0].offset = end;
  1090. region->mmaps[0].size = region->size - end;
  1091. trace_vfio_msix_fixup(vdev->vbasedev.name,
  1092. vdev->msix->table_bar, region->mmaps[0].offset,
  1093. region->mmaps[0].offset + region->mmaps[0].size);
  1094. }
  1095. /* Maybe it's aligned at the end of the BAR */
  1096. } else if (end >= region->size) {
  1097. region->mmaps[0].size = start;
  1098. trace_vfio_msix_fixup(vdev->vbasedev.name,
  1099. vdev->msix->table_bar, region->mmaps[0].offset,
  1100. region->mmaps[0].offset + region->mmaps[0].size);
  1101. /* Otherwise it must split the BAR */
  1102. } else {
  1103. region->nr_mmaps = 2;
  1104. region->mmaps = g_renew(VFIOMmap, region->mmaps, 2);
  1105. memcpy(&region->mmaps[1], &region->mmaps[0], sizeof(VFIOMmap));
  1106. region->mmaps[0].size = start;
  1107. trace_vfio_msix_fixup(vdev->vbasedev.name,
  1108. vdev->msix->table_bar, region->mmaps[0].offset,
  1109. region->mmaps[0].offset + region->mmaps[0].size);
  1110. region->mmaps[1].offset = end;
  1111. region->mmaps[1].size = region->size - end;
  1112. trace_vfio_msix_fixup(vdev->vbasedev.name,
  1113. vdev->msix->table_bar, region->mmaps[1].offset,
  1114. region->mmaps[1].offset + region->mmaps[1].size);
  1115. }
  1116. }
  1117. static void vfio_pci_relocate_msix(VFIOPCIDevice *vdev, Error **errp)
  1118. {
  1119. int target_bar = -1;
  1120. size_t msix_sz;
  1121. if (!vdev->msix || vdev->msix_relo == OFF_AUTOPCIBAR_OFF) {
  1122. return;
  1123. }
  1124. /* The actual minimum size of MSI-X structures */
  1125. msix_sz = (vdev->msix->entries * PCI_MSIX_ENTRY_SIZE) +
  1126. (QEMU_ALIGN_UP(vdev->msix->entries, 64) / 8);
  1127. /* Round up to host pages, we don't want to share a page */
  1128. msix_sz = REAL_HOST_PAGE_ALIGN(msix_sz);
  1129. /* PCI BARs must be a power of 2 */
  1130. msix_sz = pow2ceil(msix_sz);
  1131. if (vdev->msix_relo == OFF_AUTOPCIBAR_AUTO) {
  1132. /*
  1133. * TODO: Lookup table for known devices.
  1134. *
  1135. * Logically we might use an algorithm here to select the BAR adding
  1136. * the least additional MMIO space, but we cannot programmatically
  1137. * predict the driver dependency on BAR ordering or sizing, therefore
  1138. * 'auto' becomes a lookup for combinations reported to work.
  1139. */
  1140. if (target_bar < 0) {
  1141. error_setg(errp, "No automatic MSI-X relocation available for "
  1142. "device %04x:%04x", vdev->vendor_id, vdev->device_id);
  1143. return;
  1144. }
  1145. } else {
  1146. target_bar = (int)(vdev->msix_relo - OFF_AUTOPCIBAR_BAR0);
  1147. }
  1148. /* I/O port BARs cannot host MSI-X structures */
  1149. if (vdev->bars[target_bar].ioport) {
  1150. error_setg(errp, "Invalid MSI-X relocation BAR %d, "
  1151. "I/O port BAR", target_bar);
  1152. return;
  1153. }
  1154. /* Cannot use a BAR in the "shadow" of a 64-bit BAR */
  1155. if (!vdev->bars[target_bar].size &&
  1156. target_bar > 0 && vdev->bars[target_bar - 1].mem64) {
  1157. error_setg(errp, "Invalid MSI-X relocation BAR %d, "
  1158. "consumed by 64-bit BAR %d", target_bar, target_bar - 1);
  1159. return;
  1160. }
  1161. /* 2GB max size for 32-bit BARs, cannot double if already > 1G */
  1162. if (vdev->bars[target_bar].size > 1 * GiB &&
  1163. !vdev->bars[target_bar].mem64) {
  1164. error_setg(errp, "Invalid MSI-X relocation BAR %d, "
  1165. "no space to extend 32-bit BAR", target_bar);
  1166. return;
  1167. }
  1168. /*
  1169. * If adding a new BAR, test if we can make it 64bit. We make it
  1170. * prefetchable since QEMU MSI-X emulation has no read side effects
  1171. * and doing so makes mapping more flexible.
  1172. */
  1173. if (!vdev->bars[target_bar].size) {
  1174. if (target_bar < (PCI_ROM_SLOT - 1) &&
  1175. !vdev->bars[target_bar + 1].size) {
  1176. vdev->bars[target_bar].mem64 = true;
  1177. vdev->bars[target_bar].type = PCI_BASE_ADDRESS_MEM_TYPE_64;
  1178. }
  1179. vdev->bars[target_bar].type |= PCI_BASE_ADDRESS_MEM_PREFETCH;
  1180. vdev->bars[target_bar].size = msix_sz;
  1181. vdev->msix->table_offset = 0;
  1182. } else {
  1183. vdev->bars[target_bar].size = MAX(vdev->bars[target_bar].size * 2,
  1184. msix_sz * 2);
  1185. /*
  1186. * Due to above size calc, MSI-X always starts halfway into the BAR,
  1187. * which will always be a separate host page.
  1188. */
  1189. vdev->msix->table_offset = vdev->bars[target_bar].size / 2;
  1190. }
  1191. vdev->msix->table_bar = target_bar;
  1192. vdev->msix->pba_bar = target_bar;
  1193. /* Requires 8-byte alignment, but PCI_MSIX_ENTRY_SIZE guarantees that */
  1194. vdev->msix->pba_offset = vdev->msix->table_offset +
  1195. (vdev->msix->entries * PCI_MSIX_ENTRY_SIZE);
  1196. trace_vfio_msix_relo(vdev->vbasedev.name,
  1197. vdev->msix->table_bar, vdev->msix->table_offset);
  1198. }
  1199. /*
  1200. * We don't have any control over how pci_add_capability() inserts
  1201. * capabilities into the chain. In order to setup MSI-X we need a
  1202. * MemoryRegion for the BAR. In order to setup the BAR and not
  1203. * attempt to mmap the MSI-X table area, which VFIO won't allow, we
  1204. * need to first look for where the MSI-X table lives. So we
  1205. * unfortunately split MSI-X setup across two functions.
  1206. */
  1207. static void vfio_msix_early_setup(VFIOPCIDevice *vdev, Error **errp)
  1208. {
  1209. uint8_t pos;
  1210. uint16_t ctrl;
  1211. uint32_t table, pba;
  1212. int fd = vdev->vbasedev.fd;
  1213. VFIOMSIXInfo *msix;
  1214. pos = pci_find_capability(&vdev->pdev, PCI_CAP_ID_MSIX);
  1215. if (!pos) {
  1216. return;
  1217. }
  1218. if (pread(fd, &ctrl, sizeof(ctrl),
  1219. vdev->config_offset + pos + PCI_MSIX_FLAGS) != sizeof(ctrl)) {
  1220. error_setg_errno(errp, errno, "failed to read PCI MSIX FLAGS");
  1221. return;
  1222. }
  1223. if (pread(fd, &table, sizeof(table),
  1224. vdev->config_offset + pos + PCI_MSIX_TABLE) != sizeof(table)) {
  1225. error_setg_errno(errp, errno, "failed to read PCI MSIX TABLE");
  1226. return;
  1227. }
  1228. if (pread(fd, &pba, sizeof(pba),
  1229. vdev->config_offset + pos + PCI_MSIX_PBA) != sizeof(pba)) {
  1230. error_setg_errno(errp, errno, "failed to read PCI MSIX PBA");
  1231. return;
  1232. }
  1233. ctrl = le16_to_cpu(ctrl);
  1234. table = le32_to_cpu(table);
  1235. pba = le32_to_cpu(pba);
  1236. msix = g_malloc0(sizeof(*msix));
  1237. msix->table_bar = table & PCI_MSIX_FLAGS_BIRMASK;
  1238. msix->table_offset = table & ~PCI_MSIX_FLAGS_BIRMASK;
  1239. msix->pba_bar = pba & PCI_MSIX_FLAGS_BIRMASK;
  1240. msix->pba_offset = pba & ~PCI_MSIX_FLAGS_BIRMASK;
  1241. msix->entries = (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
  1242. /*
  1243. * Test the size of the pba_offset variable and catch if it extends outside
  1244. * of the specified BAR. If it is the case, we need to apply a hardware
  1245. * specific quirk if the device is known or we have a broken configuration.
  1246. */
  1247. if (msix->pba_offset >= vdev->bars[msix->pba_bar].region.size) {
  1248. /*
  1249. * Chelsio T5 Virtual Function devices are encoded as 0x58xx for T5
  1250. * adapters. The T5 hardware returns an incorrect value of 0x8000 for
  1251. * the VF PBA offset while the BAR itself is only 8k. The correct value
  1252. * is 0x1000, so we hard code that here.
  1253. */
  1254. if (vdev->vendor_id == PCI_VENDOR_ID_CHELSIO &&
  1255. (vdev->device_id & 0xff00) == 0x5800) {
  1256. msix->pba_offset = 0x1000;
  1257. /*
  1258. * BAIDU KUNLUN Virtual Function devices for KUNLUN AI processor
  1259. * return an incorrect value of 0x460000 for the VF PBA offset while
  1260. * the BAR itself is only 0x10000. The correct value is 0xb400.
  1261. */
  1262. } else if (vfio_pci_is(vdev, PCI_VENDOR_ID_BAIDU,
  1263. PCI_DEVICE_ID_KUNLUN_VF)) {
  1264. msix->pba_offset = 0xb400;
  1265. } else if (vdev->msix_relo == OFF_AUTOPCIBAR_OFF) {
  1266. error_setg(errp, "hardware reports invalid configuration, "
  1267. "MSIX PBA outside of specified BAR");
  1268. g_free(msix);
  1269. return;
  1270. }
  1271. }
  1272. trace_vfio_msix_early_setup(vdev->vbasedev.name, pos, msix->table_bar,
  1273. msix->table_offset, msix->entries);
  1274. vdev->msix = msix;
  1275. vfio_pci_fixup_msix_region(vdev);
  1276. vfio_pci_relocate_msix(vdev, errp);
  1277. }
  1278. static int vfio_msix_setup(VFIOPCIDevice *vdev, int pos, Error **errp)
  1279. {
  1280. int ret;
  1281. Error *err = NULL;
  1282. vdev->msix->pending = g_malloc0(BITS_TO_LONGS(vdev->msix->entries) *
  1283. sizeof(unsigned long));
  1284. ret = msix_init(&vdev->pdev, vdev->msix->entries,
  1285. vdev->bars[vdev->msix->table_bar].mr,
  1286. vdev->msix->table_bar, vdev->msix->table_offset,
  1287. vdev->bars[vdev->msix->pba_bar].mr,
  1288. vdev->msix->pba_bar, vdev->msix->pba_offset, pos,
  1289. &err);
  1290. if (ret < 0) {
  1291. if (ret == -ENOTSUP) {
  1292. warn_report_err(err);
  1293. return 0;
  1294. }
  1295. error_propagate(errp, err);
  1296. return ret;
  1297. }
  1298. /*
  1299. * The PCI spec suggests that devices provide additional alignment for
  1300. * MSI-X structures and avoid overlapping non-MSI-X related registers.
  1301. * For an assigned device, this hopefully means that emulation of MSI-X
  1302. * structures does not affect the performance of the device. If devices
  1303. * fail to provide that alignment, a significant performance penalty may
  1304. * result, for instance Mellanox MT27500 VFs:
  1305. * http://www.spinics.net/lists/kvm/msg125881.html
  1306. *
  1307. * The PBA is simply not that important for such a serious regression and
  1308. * most drivers do not appear to look at it. The solution for this is to
  1309. * disable the PBA MemoryRegion unless it's being used. We disable it
  1310. * here and only enable it if a masked vector fires through QEMU. As the
  1311. * vector-use notifier is called, which occurs on unmask, we test whether
  1312. * PBA emulation is needed and again disable if not.
  1313. */
  1314. memory_region_set_enabled(&vdev->pdev.msix_pba_mmio, false);
  1315. /*
  1316. * The emulated machine may provide a paravirt interface for MSIX setup
  1317. * so it is not strictly necessary to emulate MSIX here. This becomes
  1318. * helpful when frequently accessed MMIO registers are located in
  1319. * subpages adjacent to the MSIX table but the MSIX data containing page
  1320. * cannot be mapped because of a host page size bigger than the MSIX table
  1321. * alignment.
  1322. */
  1323. if (object_property_get_bool(OBJECT(qdev_get_machine()),
  1324. "vfio-no-msix-emulation", NULL)) {
  1325. memory_region_set_enabled(&vdev->pdev.msix_table_mmio, false);
  1326. }
  1327. return 0;
  1328. }
  1329. static void vfio_teardown_msi(VFIOPCIDevice *vdev)
  1330. {
  1331. msi_uninit(&vdev->pdev);
  1332. if (vdev->msix) {
  1333. msix_uninit(&vdev->pdev,
  1334. vdev->bars[vdev->msix->table_bar].mr,
  1335. vdev->bars[vdev->msix->pba_bar].mr);
  1336. g_free(vdev->msix->pending);
  1337. }
  1338. }
  1339. /*
  1340. * Resource setup
  1341. */
  1342. static void vfio_mmap_set_enabled(VFIOPCIDevice *vdev, bool enabled)
  1343. {
  1344. int i;
  1345. for (i = 0; i < PCI_ROM_SLOT; i++) {
  1346. vfio_region_mmaps_set_enabled(&vdev->bars[i].region, enabled);
  1347. }
  1348. }
  1349. static void vfio_bar_prepare(VFIOPCIDevice *vdev, int nr)
  1350. {
  1351. VFIOBAR *bar = &vdev->bars[nr];
  1352. uint32_t pci_bar;
  1353. int ret;
  1354. /* Skip both unimplemented BARs and the upper half of 64bit BARS. */
  1355. if (!bar->region.size) {
  1356. return;
  1357. }
  1358. /* Determine what type of BAR this is for registration */
  1359. ret = pread(vdev->vbasedev.fd, &pci_bar, sizeof(pci_bar),
  1360. vdev->config_offset + PCI_BASE_ADDRESS_0 + (4 * nr));
  1361. if (ret != sizeof(pci_bar)) {
  1362. error_report("vfio: Failed to read BAR %d (%m)", nr);
  1363. return;
  1364. }
  1365. pci_bar = le32_to_cpu(pci_bar);
  1366. bar->ioport = (pci_bar & PCI_BASE_ADDRESS_SPACE_IO);
  1367. bar->mem64 = bar->ioport ? 0 : (pci_bar & PCI_BASE_ADDRESS_MEM_TYPE_64);
  1368. bar->type = pci_bar & (bar->ioport ? ~PCI_BASE_ADDRESS_IO_MASK :
  1369. ~PCI_BASE_ADDRESS_MEM_MASK);
  1370. bar->size = bar->region.size;
  1371. }
  1372. static void vfio_bars_prepare(VFIOPCIDevice *vdev)
  1373. {
  1374. int i;
  1375. for (i = 0; i < PCI_ROM_SLOT; i++) {
  1376. vfio_bar_prepare(vdev, i);
  1377. }
  1378. }
  1379. static void vfio_bar_register(VFIOPCIDevice *vdev, int nr)
  1380. {
  1381. VFIOBAR *bar = &vdev->bars[nr];
  1382. char *name;
  1383. if (!bar->size) {
  1384. return;
  1385. }
  1386. bar->mr = g_new0(MemoryRegion, 1);
  1387. name = g_strdup_printf("%s base BAR %d", vdev->vbasedev.name, nr);
  1388. memory_region_init_io(bar->mr, OBJECT(vdev), NULL, NULL, name, bar->size);
  1389. g_free(name);
  1390. if (bar->region.size) {
  1391. memory_region_add_subregion(bar->mr, 0, bar->region.mem);
  1392. if (vfio_region_mmap(&bar->region)) {
  1393. error_report("Failed to mmap %s BAR %d. Performance may be slow",
  1394. vdev->vbasedev.name, nr);
  1395. }
  1396. }
  1397. pci_register_bar(&vdev->pdev, nr, bar->type, bar->mr);
  1398. }
  1399. static void vfio_bars_register(VFIOPCIDevice *vdev)
  1400. {
  1401. int i;
  1402. for (i = 0; i < PCI_ROM_SLOT; i++) {
  1403. vfio_bar_register(vdev, i);
  1404. }
  1405. }
  1406. static void vfio_bars_exit(VFIOPCIDevice *vdev)
  1407. {
  1408. int i;
  1409. for (i = 0; i < PCI_ROM_SLOT; i++) {
  1410. VFIOBAR *bar = &vdev->bars[i];
  1411. vfio_bar_quirk_exit(vdev, i);
  1412. vfio_region_exit(&bar->region);
  1413. if (bar->region.size) {
  1414. memory_region_del_subregion(bar->mr, bar->region.mem);
  1415. }
  1416. }
  1417. if (vdev->vga) {
  1418. pci_unregister_vga(&vdev->pdev);
  1419. vfio_vga_quirk_exit(vdev);
  1420. }
  1421. }
  1422. static void vfio_bars_finalize(VFIOPCIDevice *vdev)
  1423. {
  1424. int i;
  1425. for (i = 0; i < PCI_ROM_SLOT; i++) {
  1426. VFIOBAR *bar = &vdev->bars[i];
  1427. vfio_bar_quirk_finalize(vdev, i);
  1428. vfio_region_finalize(&bar->region);
  1429. if (bar->size) {
  1430. object_unparent(OBJECT(bar->mr));
  1431. g_free(bar->mr);
  1432. }
  1433. }
  1434. if (vdev->vga) {
  1435. vfio_vga_quirk_finalize(vdev);
  1436. for (i = 0; i < ARRAY_SIZE(vdev->vga->region); i++) {
  1437. object_unparent(OBJECT(&vdev->vga->region[i].mem));
  1438. }
  1439. g_free(vdev->vga);
  1440. }
  1441. }
  1442. /*
  1443. * General setup
  1444. */
  1445. static uint8_t vfio_std_cap_max_size(PCIDevice *pdev, uint8_t pos)
  1446. {
  1447. uint8_t tmp;
  1448. uint16_t next = PCI_CONFIG_SPACE_SIZE;
  1449. for (tmp = pdev->config[PCI_CAPABILITY_LIST]; tmp;
  1450. tmp = pdev->config[tmp + PCI_CAP_LIST_NEXT]) {
  1451. if (tmp > pos && tmp < next) {
  1452. next = tmp;
  1453. }
  1454. }
  1455. return next - pos;
  1456. }
  1457. static uint16_t vfio_ext_cap_max_size(const uint8_t *config, uint16_t pos)
  1458. {
  1459. uint16_t tmp, next = PCIE_CONFIG_SPACE_SIZE;
  1460. for (tmp = PCI_CONFIG_SPACE_SIZE; tmp;
  1461. tmp = PCI_EXT_CAP_NEXT(pci_get_long(config + tmp))) {
  1462. if (tmp > pos && tmp < next) {
  1463. next = tmp;
  1464. }
  1465. }
  1466. return next - pos;
  1467. }
  1468. static void vfio_set_word_bits(uint8_t *buf, uint16_t val, uint16_t mask)
  1469. {
  1470. pci_set_word(buf, (pci_get_word(buf) & ~mask) | val);
  1471. }
  1472. static void vfio_add_emulated_word(VFIOPCIDevice *vdev, int pos,
  1473. uint16_t val, uint16_t mask)
  1474. {
  1475. vfio_set_word_bits(vdev->pdev.config + pos, val, mask);
  1476. vfio_set_word_bits(vdev->pdev.wmask + pos, ~mask, mask);
  1477. vfio_set_word_bits(vdev->emulated_config_bits + pos, mask, mask);
  1478. }
  1479. static void vfio_set_long_bits(uint8_t *buf, uint32_t val, uint32_t mask)
  1480. {
  1481. pci_set_long(buf, (pci_get_long(buf) & ~mask) | val);
  1482. }
  1483. static void vfio_add_emulated_long(VFIOPCIDevice *vdev, int pos,
  1484. uint32_t val, uint32_t mask)
  1485. {
  1486. vfio_set_long_bits(vdev->pdev.config + pos, val, mask);
  1487. vfio_set_long_bits(vdev->pdev.wmask + pos, ~mask, mask);
  1488. vfio_set_long_bits(vdev->emulated_config_bits + pos, mask, mask);
  1489. }
  1490. static int vfio_setup_pcie_cap(VFIOPCIDevice *vdev, int pos, uint8_t size,
  1491. Error **errp)
  1492. {
  1493. uint16_t flags;
  1494. uint8_t type;
  1495. flags = pci_get_word(vdev->pdev.config + pos + PCI_CAP_FLAGS);
  1496. type = (flags & PCI_EXP_FLAGS_TYPE) >> 4;
  1497. if (type != PCI_EXP_TYPE_ENDPOINT &&
  1498. type != PCI_EXP_TYPE_LEG_END &&
  1499. type != PCI_EXP_TYPE_RC_END) {
  1500. error_setg(errp, "assignment of PCIe type 0x%x "
  1501. "devices is not currently supported", type);
  1502. return -EINVAL;
  1503. }
  1504. if (!pci_bus_is_express(pci_get_bus(&vdev->pdev))) {
  1505. PCIBus *bus = pci_get_bus(&vdev->pdev);
  1506. PCIDevice *bridge;
  1507. /*
  1508. * Traditionally PCI device assignment exposes the PCIe capability
  1509. * as-is on non-express buses. The reason being that some drivers
  1510. * simply assume that it's there, for example tg3. However when
  1511. * we're running on a native PCIe machine type, like Q35, we need
  1512. * to hide the PCIe capability. The reason for this is twofold;
  1513. * first Windows guests get a Code 10 error when the PCIe capability
  1514. * is exposed in this configuration. Therefore express devices won't
  1515. * work at all unless they're attached to express buses in the VM.
  1516. * Second, a native PCIe machine introduces the possibility of fine
  1517. * granularity IOMMUs supporting both translation and isolation.
  1518. * Guest code to discover the IOMMU visibility of a device, such as
  1519. * IOMMU grouping code on Linux, is very aware of device types and
  1520. * valid transitions between bus types. An express device on a non-
  1521. * express bus is not a valid combination on bare metal systems.
  1522. *
  1523. * Drivers that require a PCIe capability to make the device
  1524. * functional are simply going to need to have their devices placed
  1525. * on a PCIe bus in the VM.
  1526. */
  1527. while (!pci_bus_is_root(bus)) {
  1528. bridge = pci_bridge_get_device(bus);
  1529. bus = pci_get_bus(bridge);
  1530. }
  1531. if (pci_bus_is_express(bus)) {
  1532. return 0;
  1533. }
  1534. } else if (pci_bus_is_root(pci_get_bus(&vdev->pdev))) {
  1535. /*
  1536. * On a Root Complex bus Endpoints become Root Complex Integrated
  1537. * Endpoints, which changes the type and clears the LNK & LNK2 fields.
  1538. */
  1539. if (type == PCI_EXP_TYPE_ENDPOINT) {
  1540. vfio_add_emulated_word(vdev, pos + PCI_CAP_FLAGS,
  1541. PCI_EXP_TYPE_RC_END << 4,
  1542. PCI_EXP_FLAGS_TYPE);
  1543. /* Link Capabilities, Status, and Control goes away */
  1544. if (size > PCI_EXP_LNKCTL) {
  1545. vfio_add_emulated_long(vdev, pos + PCI_EXP_LNKCAP, 0, ~0);
  1546. vfio_add_emulated_word(vdev, pos + PCI_EXP_LNKCTL, 0, ~0);
  1547. vfio_add_emulated_word(vdev, pos + PCI_EXP_LNKSTA, 0, ~0);
  1548. #ifndef PCI_EXP_LNKCAP2
  1549. #define PCI_EXP_LNKCAP2 44
  1550. #endif
  1551. #ifndef PCI_EXP_LNKSTA2
  1552. #define PCI_EXP_LNKSTA2 50
  1553. #endif
  1554. /* Link 2 Capabilities, Status, and Control goes away */
  1555. if (size > PCI_EXP_LNKCAP2) {
  1556. vfio_add_emulated_long(vdev, pos + PCI_EXP_LNKCAP2, 0, ~0);
  1557. vfio_add_emulated_word(vdev, pos + PCI_EXP_LNKCTL2, 0, ~0);
  1558. vfio_add_emulated_word(vdev, pos + PCI_EXP_LNKSTA2, 0, ~0);
  1559. }
  1560. }
  1561. } else if (type == PCI_EXP_TYPE_LEG_END) {
  1562. /*
  1563. * Legacy endpoints don't belong on the root complex. Windows
  1564. * seems to be happier with devices if we skip the capability.
  1565. */
  1566. return 0;
  1567. }
  1568. } else {
  1569. /*
  1570. * Convert Root Complex Integrated Endpoints to regular endpoints.
  1571. * These devices don't support LNK/LNK2 capabilities, so make them up.
  1572. */
  1573. if (type == PCI_EXP_TYPE_RC_END) {
  1574. vfio_add_emulated_word(vdev, pos + PCI_CAP_FLAGS,
  1575. PCI_EXP_TYPE_ENDPOINT << 4,
  1576. PCI_EXP_FLAGS_TYPE);
  1577. vfio_add_emulated_long(vdev, pos + PCI_EXP_LNKCAP,
  1578. QEMU_PCI_EXP_LNKCAP_MLW(QEMU_PCI_EXP_LNK_X1) |
  1579. QEMU_PCI_EXP_LNKCAP_MLS(QEMU_PCI_EXP_LNK_2_5GT), ~0);
  1580. vfio_add_emulated_word(vdev, pos + PCI_EXP_LNKCTL, 0, ~0);
  1581. }
  1582. }
  1583. /*
  1584. * Intel 82599 SR-IOV VFs report an invalid PCIe capability version 0
  1585. * (Niantic errate #35) causing Windows to error with a Code 10 for the
  1586. * device on Q35. Fixup any such devices to report version 1. If we
  1587. * were to remove the capability entirely the guest would lose extended
  1588. * config space.
  1589. */
  1590. if ((flags & PCI_EXP_FLAGS_VERS) == 0) {
  1591. vfio_add_emulated_word(vdev, pos + PCI_CAP_FLAGS,
  1592. 1, PCI_EXP_FLAGS_VERS);
  1593. }
  1594. pos = pci_add_capability(&vdev->pdev, PCI_CAP_ID_EXP, pos, size,
  1595. errp);
  1596. if (pos < 0) {
  1597. return pos;
  1598. }
  1599. vdev->pdev.exp.exp_cap = pos;
  1600. return pos;
  1601. }
  1602. static void vfio_check_pcie_flr(VFIOPCIDevice *vdev, uint8_t pos)
  1603. {
  1604. uint32_t cap = pci_get_long(vdev->pdev.config + pos + PCI_EXP_DEVCAP);
  1605. if (cap & PCI_EXP_DEVCAP_FLR) {
  1606. trace_vfio_check_pcie_flr(vdev->vbasedev.name);
  1607. vdev->has_flr = true;
  1608. }
  1609. }
  1610. static void vfio_check_pm_reset(VFIOPCIDevice *vdev, uint8_t pos)
  1611. {
  1612. uint16_t csr = pci_get_word(vdev->pdev.config + pos + PCI_PM_CTRL);
  1613. if (!(csr & PCI_PM_CTRL_NO_SOFT_RESET)) {
  1614. trace_vfio_check_pm_reset(vdev->vbasedev.name);
  1615. vdev->has_pm_reset = true;
  1616. }
  1617. }
  1618. static void vfio_check_af_flr(VFIOPCIDevice *vdev, uint8_t pos)
  1619. {
  1620. uint8_t cap = pci_get_byte(vdev->pdev.config + pos + PCI_AF_CAP);
  1621. if ((cap & PCI_AF_CAP_TP) && (cap & PCI_AF_CAP_FLR)) {
  1622. trace_vfio_check_af_flr(vdev->vbasedev.name);
  1623. vdev->has_flr = true;
  1624. }
  1625. }
  1626. static int vfio_add_std_cap(VFIOPCIDevice *vdev, uint8_t pos, Error **errp)
  1627. {
  1628. PCIDevice *pdev = &vdev->pdev;
  1629. uint8_t cap_id, next, size;
  1630. int ret;
  1631. cap_id = pdev->config[pos];
  1632. next = pdev->config[pos + PCI_CAP_LIST_NEXT];
  1633. /*
  1634. * If it becomes important to configure capabilities to their actual
  1635. * size, use this as the default when it's something we don't recognize.
  1636. * Since QEMU doesn't actually handle many of the config accesses,
  1637. * exact size doesn't seem worthwhile.
  1638. */
  1639. size = vfio_std_cap_max_size(pdev, pos);
  1640. /*
  1641. * pci_add_capability always inserts the new capability at the head
  1642. * of the chain. Therefore to end up with a chain that matches the
  1643. * physical device, we insert from the end by making this recursive.
  1644. * This is also why we pre-calculate size above as cached config space
  1645. * will be changed as we unwind the stack.
  1646. */
  1647. if (next) {
  1648. ret = vfio_add_std_cap(vdev, next, errp);
  1649. if (ret) {
  1650. return ret;
  1651. }
  1652. } else {
  1653. /* Begin the rebuild, use QEMU emulated list bits */
  1654. pdev->config[PCI_CAPABILITY_LIST] = 0;
  1655. vdev->emulated_config_bits[PCI_CAPABILITY_LIST] = 0xff;
  1656. vdev->emulated_config_bits[PCI_STATUS] |= PCI_STATUS_CAP_LIST;
  1657. ret = vfio_add_virt_caps(vdev, errp);
  1658. if (ret) {
  1659. return ret;
  1660. }
  1661. }
  1662. /* Scale down size, esp in case virt caps were added above */
  1663. size = MIN(size, vfio_std_cap_max_size(pdev, pos));
  1664. /* Use emulated next pointer to allow dropping caps */
  1665. pci_set_byte(vdev->emulated_config_bits + pos + PCI_CAP_LIST_NEXT, 0xff);
  1666. switch (cap_id) {
  1667. case PCI_CAP_ID_MSI:
  1668. ret = vfio_msi_setup(vdev, pos, errp);
  1669. break;
  1670. case PCI_CAP_ID_EXP:
  1671. vfio_check_pcie_flr(vdev, pos);
  1672. ret = vfio_setup_pcie_cap(vdev, pos, size, errp);
  1673. break;
  1674. case PCI_CAP_ID_MSIX:
  1675. ret = vfio_msix_setup(vdev, pos, errp);
  1676. break;
  1677. case PCI_CAP_ID_PM:
  1678. vfio_check_pm_reset(vdev, pos);
  1679. vdev->pm_cap = pos;
  1680. ret = pci_add_capability(pdev, cap_id, pos, size, errp);
  1681. break;
  1682. case PCI_CAP_ID_AF:
  1683. vfio_check_af_flr(vdev, pos);
  1684. ret = pci_add_capability(pdev, cap_id, pos, size, errp);
  1685. break;
  1686. default:
  1687. ret = pci_add_capability(pdev, cap_id, pos, size, errp);
  1688. break;
  1689. }
  1690. if (ret < 0) {
  1691. error_prepend(errp,
  1692. "failed to add PCI capability 0x%x[0x%x]@0x%x: ",
  1693. cap_id, size, pos);
  1694. return ret;
  1695. }
  1696. return 0;
  1697. }
  1698. static void vfio_add_ext_cap(VFIOPCIDevice *vdev)
  1699. {
  1700. PCIDevice *pdev = &vdev->pdev;
  1701. uint32_t header;
  1702. uint16_t cap_id, next, size;
  1703. uint8_t cap_ver;
  1704. uint8_t *config;
  1705. /* Only add extended caps if we have them and the guest can see them */
  1706. if (!pci_is_express(pdev) || !pci_bus_is_express(pci_get_bus(pdev)) ||
  1707. !pci_get_long(pdev->config + PCI_CONFIG_SPACE_SIZE)) {
  1708. return;
  1709. }
  1710. /*
  1711. * pcie_add_capability always inserts the new capability at the tail
  1712. * of the chain. Therefore to end up with a chain that matches the
  1713. * physical device, we cache the config space to avoid overwriting
  1714. * the original config space when we parse the extended capabilities.
  1715. */
  1716. config = g_memdup(pdev->config, vdev->config_size);
  1717. /*
  1718. * Extended capabilities are chained with each pointing to the next, so we
  1719. * can drop anything other than the head of the chain simply by modifying
  1720. * the previous next pointer. Seed the head of the chain here such that
  1721. * we can simply skip any capabilities we want to drop below, regardless
  1722. * of their position in the chain. If this stub capability still exists
  1723. * after we add the capabilities we want to expose, update the capability
  1724. * ID to zero. Note that we cannot seed with the capability header being
  1725. * zero as this conflicts with definition of an absent capability chain
  1726. * and prevents capabilities beyond the head of the list from being added.
  1727. * By replacing the dummy capability ID with zero after walking the device
  1728. * chain, we also transparently mark extended capabilities as absent if
  1729. * no capabilities were added. Note that the PCIe spec defines an absence
  1730. * of extended capabilities to be determined by a value of zero for the
  1731. * capability ID, version, AND next pointer. A non-zero next pointer
  1732. * should be sufficient to indicate additional capabilities are present,
  1733. * which will occur if we call pcie_add_capability() below. The entire
  1734. * first dword is emulated to support this.
  1735. *
  1736. * NB. The kernel side does similar masking, so be prepared that our
  1737. * view of the device may also contain a capability ID zero in the head
  1738. * of the chain. Skip it for the same reason that we cannot seed the
  1739. * chain with a zero capability.
  1740. */
  1741. pci_set_long(pdev->config + PCI_CONFIG_SPACE_SIZE,
  1742. PCI_EXT_CAP(0xFFFF, 0, 0));
  1743. pci_set_long(pdev->wmask + PCI_CONFIG_SPACE_SIZE, 0);
  1744. pci_set_long(vdev->emulated_config_bits + PCI_CONFIG_SPACE_SIZE, ~0);
  1745. for (next = PCI_CONFIG_SPACE_SIZE; next;
  1746. next = PCI_EXT_CAP_NEXT(pci_get_long(config + next))) {
  1747. header = pci_get_long(config + next);
  1748. cap_id = PCI_EXT_CAP_ID(header);
  1749. cap_ver = PCI_EXT_CAP_VER(header);
  1750. /*
  1751. * If it becomes important to configure extended capabilities to their
  1752. * actual size, use this as the default when it's something we don't
  1753. * recognize. Since QEMU doesn't actually handle many of the config
  1754. * accesses, exact size doesn't seem worthwhile.
  1755. */
  1756. size = vfio_ext_cap_max_size(config, next);
  1757. /* Use emulated next pointer to allow dropping extended caps */
  1758. pci_long_test_and_set_mask(vdev->emulated_config_bits + next,
  1759. PCI_EXT_CAP_NEXT_MASK);
  1760. switch (cap_id) {
  1761. case 0: /* kernel masked capability */
  1762. case PCI_EXT_CAP_ID_SRIOV: /* Read-only VF BARs confuse OVMF */
  1763. case PCI_EXT_CAP_ID_ARI: /* XXX Needs next function virtualization */
  1764. case PCI_EXT_CAP_ID_REBAR: /* Can't expose read-only */
  1765. trace_vfio_add_ext_cap_dropped(vdev->vbasedev.name, cap_id, next);
  1766. break;
  1767. default:
  1768. pcie_add_capability(pdev, cap_id, cap_ver, next, size);
  1769. }
  1770. }
  1771. /* Cleanup chain head ID if necessary */
  1772. if (pci_get_word(pdev->config + PCI_CONFIG_SPACE_SIZE) == 0xFFFF) {
  1773. pci_set_word(pdev->config + PCI_CONFIG_SPACE_SIZE, 0);
  1774. }
  1775. g_free(config);
  1776. return;
  1777. }
  1778. static int vfio_add_capabilities(VFIOPCIDevice *vdev, Error **errp)
  1779. {
  1780. PCIDevice *pdev = &vdev->pdev;
  1781. int ret;
  1782. if (!(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST) ||
  1783. !pdev->config[PCI_CAPABILITY_LIST]) {
  1784. return 0; /* Nothing to add */
  1785. }
  1786. ret = vfio_add_std_cap(vdev, pdev->config[PCI_CAPABILITY_LIST], errp);
  1787. if (ret) {
  1788. return ret;
  1789. }
  1790. vfio_add_ext_cap(vdev);
  1791. return 0;
  1792. }
  1793. static void vfio_pci_pre_reset(VFIOPCIDevice *vdev)
  1794. {
  1795. PCIDevice *pdev = &vdev->pdev;
  1796. uint16_t cmd;
  1797. vfio_disable_interrupts(vdev);
  1798. /* Make sure the device is in D0 */
  1799. if (vdev->pm_cap) {
  1800. uint16_t pmcsr;
  1801. uint8_t state;
  1802. pmcsr = vfio_pci_read_config(pdev, vdev->pm_cap + PCI_PM_CTRL, 2);
  1803. state = pmcsr & PCI_PM_CTRL_STATE_MASK;
  1804. if (state) {
  1805. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  1806. vfio_pci_write_config(pdev, vdev->pm_cap + PCI_PM_CTRL, pmcsr, 2);
  1807. /* vfio handles the necessary delay here */
  1808. pmcsr = vfio_pci_read_config(pdev, vdev->pm_cap + PCI_PM_CTRL, 2);
  1809. state = pmcsr & PCI_PM_CTRL_STATE_MASK;
  1810. if (state) {
  1811. error_report("vfio: Unable to power on device, stuck in D%d",
  1812. state);
  1813. }
  1814. }
  1815. }
  1816. /*
  1817. * Stop any ongoing DMA by disconnecting I/O, MMIO, and bus master.
  1818. * Also put INTx Disable in known state.
  1819. */
  1820. cmd = vfio_pci_read_config(pdev, PCI_COMMAND, 2);
  1821. cmd &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
  1822. PCI_COMMAND_INTX_DISABLE);
  1823. vfio_pci_write_config(pdev, PCI_COMMAND, cmd, 2);
  1824. }
  1825. static void vfio_pci_post_reset(VFIOPCIDevice *vdev)
  1826. {
  1827. Error *err = NULL;
  1828. int nr;
  1829. vfio_intx_enable(vdev, &err);
  1830. if (err) {
  1831. error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);
  1832. }
  1833. for (nr = 0; nr < PCI_NUM_REGIONS - 1; ++nr) {
  1834. off_t addr = vdev->config_offset + PCI_BASE_ADDRESS_0 + (4 * nr);
  1835. uint32_t val = 0;
  1836. uint32_t len = sizeof(val);
  1837. if (pwrite(vdev->vbasedev.fd, &val, len, addr) != len) {
  1838. error_report("%s(%s) reset bar %d failed: %m", __func__,
  1839. vdev->vbasedev.name, nr);
  1840. }
  1841. }
  1842. vfio_quirk_reset(vdev);
  1843. }
  1844. static bool vfio_pci_host_match(PCIHostDeviceAddress *addr, const char *name)
  1845. {
  1846. char tmp[13];
  1847. sprintf(tmp, "%04x:%02x:%02x.%1x", addr->domain,
  1848. addr->bus, addr->slot, addr->function);
  1849. return (strcmp(tmp, name) == 0);
  1850. }
  1851. static int vfio_pci_hot_reset(VFIOPCIDevice *vdev, bool single)
  1852. {
  1853. VFIOGroup *group;
  1854. struct vfio_pci_hot_reset_info *info;
  1855. struct vfio_pci_dependent_device *devices;
  1856. struct vfio_pci_hot_reset *reset;
  1857. int32_t *fds;
  1858. int ret, i, count;
  1859. bool multi = false;
  1860. trace_vfio_pci_hot_reset(vdev->vbasedev.name, single ? "one" : "multi");
  1861. if (!single) {
  1862. vfio_pci_pre_reset(vdev);
  1863. }
  1864. vdev->vbasedev.needs_reset = false;
  1865. info = g_malloc0(sizeof(*info));
  1866. info->argsz = sizeof(*info);
  1867. ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_GET_PCI_HOT_RESET_INFO, info);
  1868. if (ret && errno != ENOSPC) {
  1869. ret = -errno;
  1870. if (!vdev->has_pm_reset) {
  1871. error_report("vfio: Cannot reset device %s, "
  1872. "no available reset mechanism.", vdev->vbasedev.name);
  1873. }
  1874. goto out_single;
  1875. }
  1876. count = info->count;
  1877. info = g_realloc(info, sizeof(*info) + (count * sizeof(*devices)));
  1878. info->argsz = sizeof(*info) + (count * sizeof(*devices));
  1879. devices = &info->devices[0];
  1880. ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_GET_PCI_HOT_RESET_INFO, info);
  1881. if (ret) {
  1882. ret = -errno;
  1883. error_report("vfio: hot reset info failed: %m");
  1884. goto out_single;
  1885. }
  1886. trace_vfio_pci_hot_reset_has_dep_devices(vdev->vbasedev.name);
  1887. /* Verify that we have all the groups required */
  1888. for (i = 0; i < info->count; i++) {
  1889. PCIHostDeviceAddress host;
  1890. VFIOPCIDevice *tmp;
  1891. VFIODevice *vbasedev_iter;
  1892. host.domain = devices[i].segment;
  1893. host.bus = devices[i].bus;
  1894. host.slot = PCI_SLOT(devices[i].devfn);
  1895. host.function = PCI_FUNC(devices[i].devfn);
  1896. trace_vfio_pci_hot_reset_dep_devices(host.domain,
  1897. host.bus, host.slot, host.function, devices[i].group_id);
  1898. if (vfio_pci_host_match(&host, vdev->vbasedev.name)) {
  1899. continue;
  1900. }
  1901. QLIST_FOREACH(group, &vfio_group_list, next) {
  1902. if (group->groupid == devices[i].group_id) {
  1903. break;
  1904. }
  1905. }
  1906. if (!group) {
  1907. if (!vdev->has_pm_reset) {
  1908. error_report("vfio: Cannot reset device %s, "
  1909. "depends on group %d which is not owned.",
  1910. vdev->vbasedev.name, devices[i].group_id);
  1911. }
  1912. ret = -EPERM;
  1913. goto out;
  1914. }
  1915. /* Prep dependent devices for reset and clear our marker. */
  1916. QLIST_FOREACH(vbasedev_iter, &group->device_list, next) {
  1917. if (!vbasedev_iter->dev->realized ||
  1918. vbasedev_iter->type != VFIO_DEVICE_TYPE_PCI) {
  1919. continue;
  1920. }
  1921. tmp = container_of(vbasedev_iter, VFIOPCIDevice, vbasedev);
  1922. if (vfio_pci_host_match(&host, tmp->vbasedev.name)) {
  1923. if (single) {
  1924. ret = -EINVAL;
  1925. goto out_single;
  1926. }
  1927. vfio_pci_pre_reset(tmp);
  1928. tmp->vbasedev.needs_reset = false;
  1929. multi = true;
  1930. break;
  1931. }
  1932. }
  1933. }
  1934. if (!single && !multi) {
  1935. ret = -EINVAL;
  1936. goto out_single;
  1937. }
  1938. /* Determine how many group fds need to be passed */
  1939. count = 0;
  1940. QLIST_FOREACH(group, &vfio_group_list, next) {
  1941. for (i = 0; i < info->count; i++) {
  1942. if (group->groupid == devices[i].group_id) {
  1943. count++;
  1944. break;
  1945. }
  1946. }
  1947. }
  1948. reset = g_malloc0(sizeof(*reset) + (count * sizeof(*fds)));
  1949. reset->argsz = sizeof(*reset) + (count * sizeof(*fds));
  1950. fds = &reset->group_fds[0];
  1951. /* Fill in group fds */
  1952. QLIST_FOREACH(group, &vfio_group_list, next) {
  1953. for (i = 0; i < info->count; i++) {
  1954. if (group->groupid == devices[i].group_id) {
  1955. fds[reset->count++] = group->fd;
  1956. break;
  1957. }
  1958. }
  1959. }
  1960. /* Bus reset! */
  1961. ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_PCI_HOT_RESET, reset);
  1962. g_free(reset);
  1963. trace_vfio_pci_hot_reset_result(vdev->vbasedev.name,
  1964. ret ? "%m" : "Success");
  1965. out:
  1966. /* Re-enable INTx on affected devices */
  1967. for (i = 0; i < info->count; i++) {
  1968. PCIHostDeviceAddress host;
  1969. VFIOPCIDevice *tmp;
  1970. VFIODevice *vbasedev_iter;
  1971. host.domain = devices[i].segment;
  1972. host.bus = devices[i].bus;
  1973. host.slot = PCI_SLOT(devices[i].devfn);
  1974. host.function = PCI_FUNC(devices[i].devfn);
  1975. if (vfio_pci_host_match(&host, vdev->vbasedev.name)) {
  1976. continue;
  1977. }
  1978. QLIST_FOREACH(group, &vfio_group_list, next) {
  1979. if (group->groupid == devices[i].group_id) {
  1980. break;
  1981. }
  1982. }
  1983. if (!group) {
  1984. break;
  1985. }
  1986. QLIST_FOREACH(vbasedev_iter, &group->device_list, next) {
  1987. if (!vbasedev_iter->dev->realized ||
  1988. vbasedev_iter->type != VFIO_DEVICE_TYPE_PCI) {
  1989. continue;
  1990. }
  1991. tmp = container_of(vbasedev_iter, VFIOPCIDevice, vbasedev);
  1992. if (vfio_pci_host_match(&host, tmp->vbasedev.name)) {
  1993. vfio_pci_post_reset(tmp);
  1994. break;
  1995. }
  1996. }
  1997. }
  1998. out_single:
  1999. if (!single) {
  2000. vfio_pci_post_reset(vdev);
  2001. }
  2002. g_free(info);
  2003. return ret;
  2004. }
  2005. /*
  2006. * We want to differentiate hot reset of multiple in-use devices vs hot reset
  2007. * of a single in-use device. VFIO_DEVICE_RESET will already handle the case
  2008. * of doing hot resets when there is only a single device per bus. The in-use
  2009. * here refers to how many VFIODevices are affected. A hot reset that affects
  2010. * multiple devices, but only a single in-use device, means that we can call
  2011. * it from our bus ->reset() callback since the extent is effectively a single
  2012. * device. This allows us to make use of it in the hotplug path. When there
  2013. * are multiple in-use devices, we can only trigger the hot reset during a
  2014. * system reset and thus from our reset handler. We separate _one vs _multi
  2015. * here so that we don't overlap and do a double reset on the system reset
  2016. * path where both our reset handler and ->reset() callback are used. Calling
  2017. * _one() will only do a hot reset for the one in-use devices case, calling
  2018. * _multi() will do nothing if a _one() would have been sufficient.
  2019. */
  2020. static int vfio_pci_hot_reset_one(VFIOPCIDevice *vdev)
  2021. {
  2022. return vfio_pci_hot_reset(vdev, true);
  2023. }
  2024. static int vfio_pci_hot_reset_multi(VFIODevice *vbasedev)
  2025. {
  2026. VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
  2027. return vfio_pci_hot_reset(vdev, false);
  2028. }
  2029. static void vfio_pci_compute_needs_reset(VFIODevice *vbasedev)
  2030. {
  2031. VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
  2032. if (!vbasedev->reset_works || (!vdev->has_flr && vdev->has_pm_reset)) {
  2033. vbasedev->needs_reset = true;
  2034. }
  2035. }
  2036. static Object *vfio_pci_get_object(VFIODevice *vbasedev)
  2037. {
  2038. VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
  2039. return OBJECT(vdev);
  2040. }
  2041. static bool vfio_msix_present(void *opaque, int version_id)
  2042. {
  2043. PCIDevice *pdev = opaque;
  2044. return msix_present(pdev);
  2045. }
  2046. const VMStateDescription vmstate_vfio_pci_config = {
  2047. .name = "VFIOPCIDevice",
  2048. .version_id = 1,
  2049. .minimum_version_id = 1,
  2050. .fields = (VMStateField[]) {
  2051. VMSTATE_PCI_DEVICE(pdev, VFIOPCIDevice),
  2052. VMSTATE_MSIX_TEST(pdev, VFIOPCIDevice, vfio_msix_present),
  2053. VMSTATE_END_OF_LIST()
  2054. }
  2055. };
  2056. static void vfio_pci_save_config(VFIODevice *vbasedev, QEMUFile *f)
  2057. {
  2058. VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
  2059. vmstate_save_state(f, &vmstate_vfio_pci_config, vdev, NULL);
  2060. }
  2061. static int vfio_pci_load_config(VFIODevice *vbasedev, QEMUFile *f)
  2062. {
  2063. VFIOPCIDevice *vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
  2064. PCIDevice *pdev = &vdev->pdev;
  2065. pcibus_t old_addr[PCI_NUM_REGIONS - 1];
  2066. int bar, ret;
  2067. for (bar = 0; bar < PCI_ROM_SLOT; bar++) {
  2068. old_addr[bar] = pdev->io_regions[bar].addr;
  2069. }
  2070. ret = vmstate_load_state(f, &vmstate_vfio_pci_config, vdev, 1);
  2071. if (ret) {
  2072. return ret;
  2073. }
  2074. vfio_pci_write_config(pdev, PCI_COMMAND,
  2075. pci_get_word(pdev->config + PCI_COMMAND), 2);
  2076. for (bar = 0; bar < PCI_ROM_SLOT; bar++) {
  2077. /*
  2078. * The address may not be changed in some scenarios
  2079. * (e.g. the VF driver isn't loaded in VM).
  2080. */
  2081. if (old_addr[bar] != pdev->io_regions[bar].addr &&
  2082. vdev->bars[bar].region.size > 0 &&
  2083. vdev->bars[bar].region.size < qemu_real_host_page_size) {
  2084. vfio_sub_page_bar_update_mapping(pdev, bar);
  2085. }
  2086. }
  2087. if (msi_enabled(pdev)) {
  2088. vfio_msi_enable(vdev);
  2089. } else if (msix_enabled(pdev)) {
  2090. vfio_msix_enable(vdev);
  2091. }
  2092. return ret;
  2093. }
  2094. static VFIODeviceOps vfio_pci_ops = {
  2095. .vfio_compute_needs_reset = vfio_pci_compute_needs_reset,
  2096. .vfio_hot_reset_multi = vfio_pci_hot_reset_multi,
  2097. .vfio_eoi = vfio_intx_eoi,
  2098. .vfio_get_object = vfio_pci_get_object,
  2099. .vfio_save_config = vfio_pci_save_config,
  2100. .vfio_load_config = vfio_pci_load_config,
  2101. };
  2102. int vfio_populate_vga(VFIOPCIDevice *vdev, Error **errp)
  2103. {
  2104. VFIODevice *vbasedev = &vdev->vbasedev;
  2105. struct vfio_region_info *reg_info;
  2106. int ret;
  2107. ret = vfio_get_region_info(vbasedev, VFIO_PCI_VGA_REGION_INDEX, &reg_info);
  2108. if (ret) {
  2109. error_setg_errno(errp, -ret,
  2110. "failed getting region info for VGA region index %d",
  2111. VFIO_PCI_VGA_REGION_INDEX);
  2112. return ret;
  2113. }
  2114. if (!(reg_info->flags & VFIO_REGION_INFO_FLAG_READ) ||
  2115. !(reg_info->flags & VFIO_REGION_INFO_FLAG_WRITE) ||
  2116. reg_info->size < 0xbffff + 1) {
  2117. error_setg(errp, "unexpected VGA info, flags 0x%lx, size 0x%lx",
  2118. (unsigned long)reg_info->flags,
  2119. (unsigned long)reg_info->size);
  2120. g_free(reg_info);
  2121. return -EINVAL;
  2122. }
  2123. vdev->vga = g_new0(VFIOVGA, 1);
  2124. vdev->vga->fd_offset = reg_info->offset;
  2125. vdev->vga->fd = vdev->vbasedev.fd;
  2126. g_free(reg_info);
  2127. vdev->vga->region[QEMU_PCI_VGA_MEM].offset = QEMU_PCI_VGA_MEM_BASE;
  2128. vdev->vga->region[QEMU_PCI_VGA_MEM].nr = QEMU_PCI_VGA_MEM;
  2129. QLIST_INIT(&vdev->vga->region[QEMU_PCI_VGA_MEM].quirks);
  2130. memory_region_init_io(&vdev->vga->region[QEMU_PCI_VGA_MEM].mem,
  2131. OBJECT(vdev), &vfio_vga_ops,
  2132. &vdev->vga->region[QEMU_PCI_VGA_MEM],
  2133. "vfio-vga-mmio@0xa0000",
  2134. QEMU_PCI_VGA_MEM_SIZE);
  2135. vdev->vga->region[QEMU_PCI_VGA_IO_LO].offset = QEMU_PCI_VGA_IO_LO_BASE;
  2136. vdev->vga->region[QEMU_PCI_VGA_IO_LO].nr = QEMU_PCI_VGA_IO_LO;
  2137. QLIST_INIT(&vdev->vga->region[QEMU_PCI_VGA_IO_LO].quirks);
  2138. memory_region_init_io(&vdev->vga->region[QEMU_PCI_VGA_IO_LO].mem,
  2139. OBJECT(vdev), &vfio_vga_ops,
  2140. &vdev->vga->region[QEMU_PCI_VGA_IO_LO],
  2141. "vfio-vga-io@0x3b0",
  2142. QEMU_PCI_VGA_IO_LO_SIZE);
  2143. vdev->vga->region[QEMU_PCI_VGA_IO_HI].offset = QEMU_PCI_VGA_IO_HI_BASE;
  2144. vdev->vga->region[QEMU_PCI_VGA_IO_HI].nr = QEMU_PCI_VGA_IO_HI;
  2145. QLIST_INIT(&vdev->vga->region[QEMU_PCI_VGA_IO_HI].quirks);
  2146. memory_region_init_io(&vdev->vga->region[QEMU_PCI_VGA_IO_HI].mem,
  2147. OBJECT(vdev), &vfio_vga_ops,
  2148. &vdev->vga->region[QEMU_PCI_VGA_IO_HI],
  2149. "vfio-vga-io@0x3c0",
  2150. QEMU_PCI_VGA_IO_HI_SIZE);
  2151. pci_register_vga(&vdev->pdev, &vdev->vga->region[QEMU_PCI_VGA_MEM].mem,
  2152. &vdev->vga->region[QEMU_PCI_VGA_IO_LO].mem,
  2153. &vdev->vga->region[QEMU_PCI_VGA_IO_HI].mem);
  2154. return 0;
  2155. }
  2156. static void vfio_populate_device(VFIOPCIDevice *vdev, Error **errp)
  2157. {
  2158. VFIODevice *vbasedev = &vdev->vbasedev;
  2159. struct vfio_region_info *reg_info;
  2160. struct vfio_irq_info irq_info = { .argsz = sizeof(irq_info) };
  2161. int i, ret = -1;
  2162. /* Sanity check device */
  2163. if (!(vbasedev->flags & VFIO_DEVICE_FLAGS_PCI)) {
  2164. error_setg(errp, "this isn't a PCI device");
  2165. return;
  2166. }
  2167. if (vbasedev->num_regions < VFIO_PCI_CONFIG_REGION_INDEX + 1) {
  2168. error_setg(errp, "unexpected number of io regions %u",
  2169. vbasedev->num_regions);
  2170. return;
  2171. }
  2172. if (vbasedev->num_irqs < VFIO_PCI_MSIX_IRQ_INDEX + 1) {
  2173. error_setg(errp, "unexpected number of irqs %u", vbasedev->num_irqs);
  2174. return;
  2175. }
  2176. for (i = VFIO_PCI_BAR0_REGION_INDEX; i < VFIO_PCI_ROM_REGION_INDEX; i++) {
  2177. char *name = g_strdup_printf("%s BAR %d", vbasedev->name, i);
  2178. ret = vfio_region_setup(OBJECT(vdev), vbasedev,
  2179. &vdev->bars[i].region, i, name);
  2180. g_free(name);
  2181. if (ret) {
  2182. error_setg_errno(errp, -ret, "failed to get region %d info", i);
  2183. return;
  2184. }
  2185. QLIST_INIT(&vdev->bars[i].quirks);
  2186. }
  2187. ret = vfio_get_region_info(vbasedev,
  2188. VFIO_PCI_CONFIG_REGION_INDEX, &reg_info);
  2189. if (ret) {
  2190. error_setg_errno(errp, -ret, "failed to get config info");
  2191. return;
  2192. }
  2193. trace_vfio_populate_device_config(vdev->vbasedev.name,
  2194. (unsigned long)reg_info->size,
  2195. (unsigned long)reg_info->offset,
  2196. (unsigned long)reg_info->flags);
  2197. vdev->config_size = reg_info->size;
  2198. if (vdev->config_size == PCI_CONFIG_SPACE_SIZE) {
  2199. vdev->pdev.cap_present &= ~QEMU_PCI_CAP_EXPRESS;
  2200. }
  2201. vdev->config_offset = reg_info->offset;
  2202. g_free(reg_info);
  2203. if (vdev->features & VFIO_FEATURE_ENABLE_VGA) {
  2204. ret = vfio_populate_vga(vdev, errp);
  2205. if (ret) {
  2206. error_append_hint(errp, "device does not support "
  2207. "requested feature x-vga\n");
  2208. return;
  2209. }
  2210. }
  2211. irq_info.index = VFIO_PCI_ERR_IRQ_INDEX;
  2212. ret = ioctl(vdev->vbasedev.fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info);
  2213. if (ret) {
  2214. /* This can fail for an old kernel or legacy PCI dev */
  2215. trace_vfio_populate_device_get_irq_info_failure(strerror(errno));
  2216. } else if (irq_info.count == 1) {
  2217. vdev->pci_aer = true;
  2218. } else {
  2219. warn_report(VFIO_MSG_PREFIX
  2220. "Could not enable error recovery for the device",
  2221. vbasedev->name);
  2222. }
  2223. }
  2224. static void vfio_put_device(VFIOPCIDevice *vdev)
  2225. {
  2226. g_free(vdev->vbasedev.name);
  2227. g_free(vdev->msix);
  2228. vfio_put_base_device(&vdev->vbasedev);
  2229. }
  2230. static void vfio_err_notifier_handler(void *opaque)
  2231. {
  2232. VFIOPCIDevice *vdev = opaque;
  2233. if (!event_notifier_test_and_clear(&vdev->err_notifier)) {
  2234. return;
  2235. }
  2236. /*
  2237. * TBD. Retrieve the error details and decide what action
  2238. * needs to be taken. One of the actions could be to pass
  2239. * the error to the guest and have the guest driver recover
  2240. * from the error. This requires that PCIe capabilities be
  2241. * exposed to the guest. For now, we just terminate the
  2242. * guest to contain the error.
  2243. */
  2244. error_report("%s(%s) Unrecoverable error detected. Please collect any data possible and then kill the guest", __func__, vdev->vbasedev.name);
  2245. vm_stop(RUN_STATE_INTERNAL_ERROR);
  2246. }
  2247. /*
  2248. * Registers error notifier for devices supporting error recovery.
  2249. * If we encounter a failure in this function, we report an error
  2250. * and continue after disabling error recovery support for the
  2251. * device.
  2252. */
  2253. static void vfio_register_err_notifier(VFIOPCIDevice *vdev)
  2254. {
  2255. Error *err = NULL;
  2256. int32_t fd;
  2257. if (!vdev->pci_aer) {
  2258. return;
  2259. }
  2260. if (event_notifier_init(&vdev->err_notifier, 0)) {
  2261. error_report("vfio: Unable to init event notifier for error detection");
  2262. vdev->pci_aer = false;
  2263. return;
  2264. }
  2265. fd = event_notifier_get_fd(&vdev->err_notifier);
  2266. qemu_set_fd_handler(fd, vfio_err_notifier_handler, NULL, vdev);
  2267. if (vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_ERR_IRQ_INDEX, 0,
  2268. VFIO_IRQ_SET_ACTION_TRIGGER, fd, &err)) {
  2269. error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);
  2270. qemu_set_fd_handler(fd, NULL, NULL, vdev);
  2271. event_notifier_cleanup(&vdev->err_notifier);
  2272. vdev->pci_aer = false;
  2273. }
  2274. }
  2275. static void vfio_unregister_err_notifier(VFIOPCIDevice *vdev)
  2276. {
  2277. Error *err = NULL;
  2278. if (!vdev->pci_aer) {
  2279. return;
  2280. }
  2281. if (vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_ERR_IRQ_INDEX, 0,
  2282. VFIO_IRQ_SET_ACTION_TRIGGER, -1, &err)) {
  2283. error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);
  2284. }
  2285. qemu_set_fd_handler(event_notifier_get_fd(&vdev->err_notifier),
  2286. NULL, NULL, vdev);
  2287. event_notifier_cleanup(&vdev->err_notifier);
  2288. }
  2289. static void vfio_req_notifier_handler(void *opaque)
  2290. {
  2291. VFIOPCIDevice *vdev = opaque;
  2292. Error *err = NULL;
  2293. if (!event_notifier_test_and_clear(&vdev->req_notifier)) {
  2294. return;
  2295. }
  2296. qdev_unplug(DEVICE(vdev), &err);
  2297. if (err) {
  2298. warn_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);
  2299. }
  2300. }
  2301. static void vfio_register_req_notifier(VFIOPCIDevice *vdev)
  2302. {
  2303. struct vfio_irq_info irq_info = { .argsz = sizeof(irq_info),
  2304. .index = VFIO_PCI_REQ_IRQ_INDEX };
  2305. Error *err = NULL;
  2306. int32_t fd;
  2307. if (!(vdev->features & VFIO_FEATURE_ENABLE_REQ)) {
  2308. return;
  2309. }
  2310. if (ioctl(vdev->vbasedev.fd,
  2311. VFIO_DEVICE_GET_IRQ_INFO, &irq_info) < 0 || irq_info.count < 1) {
  2312. return;
  2313. }
  2314. if (event_notifier_init(&vdev->req_notifier, 0)) {
  2315. error_report("vfio: Unable to init event notifier for device request");
  2316. return;
  2317. }
  2318. fd = event_notifier_get_fd(&vdev->req_notifier);
  2319. qemu_set_fd_handler(fd, vfio_req_notifier_handler, NULL, vdev);
  2320. if (vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_REQ_IRQ_INDEX, 0,
  2321. VFIO_IRQ_SET_ACTION_TRIGGER, fd, &err)) {
  2322. error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);
  2323. qemu_set_fd_handler(fd, NULL, NULL, vdev);
  2324. event_notifier_cleanup(&vdev->req_notifier);
  2325. } else {
  2326. vdev->req_enabled = true;
  2327. }
  2328. }
  2329. static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev)
  2330. {
  2331. Error *err = NULL;
  2332. if (!vdev->req_enabled) {
  2333. return;
  2334. }
  2335. if (vfio_set_irq_signaling(&vdev->vbasedev, VFIO_PCI_REQ_IRQ_INDEX, 0,
  2336. VFIO_IRQ_SET_ACTION_TRIGGER, -1, &err)) {
  2337. error_reportf_err(err, VFIO_MSG_PREFIX, vdev->vbasedev.name);
  2338. }
  2339. qemu_set_fd_handler(event_notifier_get_fd(&vdev->req_notifier),
  2340. NULL, NULL, vdev);
  2341. event_notifier_cleanup(&vdev->req_notifier);
  2342. vdev->req_enabled = false;
  2343. }
  2344. static void vfio_realize(PCIDevice *pdev, Error **errp)
  2345. {
  2346. VFIOPCIDevice *vdev = VFIO_PCI(pdev);
  2347. VFIODevice *vbasedev_iter;
  2348. VFIOGroup *group;
  2349. char *tmp, *subsys, group_path[PATH_MAX], *group_name;
  2350. Error *err = NULL;
  2351. ssize_t len;
  2352. struct stat st;
  2353. int groupid;
  2354. int i, ret;
  2355. bool is_mdev;
  2356. if (!vdev->vbasedev.sysfsdev) {
  2357. if (!(~vdev->host.domain || ~vdev->host.bus ||
  2358. ~vdev->host.slot || ~vdev->host.function)) {
  2359. error_setg(errp, "No provided host device");
  2360. error_append_hint(errp, "Use -device vfio-pci,host=DDDD:BB:DD.F "
  2361. "or -device vfio-pci,sysfsdev=PATH_TO_DEVICE\n");
  2362. return;
  2363. }
  2364. vdev->vbasedev.sysfsdev =
  2365. g_strdup_printf("/sys/bus/pci/devices/%04x:%02x:%02x.%01x",
  2366. vdev->host.domain, vdev->host.bus,
  2367. vdev->host.slot, vdev->host.function);
  2368. }
  2369. if (stat(vdev->vbasedev.sysfsdev, &st) < 0) {
  2370. error_setg_errno(errp, errno, "no such host device");
  2371. error_prepend(errp, VFIO_MSG_PREFIX, vdev->vbasedev.sysfsdev);
  2372. return;
  2373. }
  2374. vdev->vbasedev.name = g_path_get_basename(vdev->vbasedev.sysfsdev);
  2375. vdev->vbasedev.ops = &vfio_pci_ops;
  2376. vdev->vbasedev.type = VFIO_DEVICE_TYPE_PCI;
  2377. vdev->vbasedev.dev = DEVICE(vdev);
  2378. tmp = g_strdup_printf("%s/iommu_group", vdev->vbasedev.sysfsdev);
  2379. len = readlink(tmp, group_path, sizeof(group_path));
  2380. g_free(tmp);
  2381. if (len <= 0 || len >= sizeof(group_path)) {
  2382. error_setg_errno(errp, len < 0 ? errno : ENAMETOOLONG,
  2383. "no iommu_group found");
  2384. goto error;
  2385. }
  2386. group_path[len] = 0;
  2387. group_name = basename(group_path);
  2388. if (sscanf(group_name, "%d", &groupid) != 1) {
  2389. error_setg_errno(errp, errno, "failed to read %s", group_path);
  2390. goto error;
  2391. }
  2392. trace_vfio_realize(vdev->vbasedev.name, groupid);
  2393. group = vfio_get_group(groupid, pci_device_iommu_address_space(pdev), errp);
  2394. if (!group) {
  2395. goto error;
  2396. }
  2397. QLIST_FOREACH(vbasedev_iter, &group->device_list, next) {
  2398. if (strcmp(vbasedev_iter->name, vdev->vbasedev.name) == 0) {
  2399. error_setg(errp, "device is already attached");
  2400. vfio_put_group(group);
  2401. goto error;
  2402. }
  2403. }
  2404. /*
  2405. * Mediated devices *might* operate compatibly with discarding of RAM, but
  2406. * we cannot know for certain, it depends on whether the mdev vendor driver
  2407. * stays in sync with the active working set of the guest driver. Prevent
  2408. * the x-balloon-allowed option unless this is minimally an mdev device.
  2409. */
  2410. tmp = g_strdup_printf("%s/subsystem", vdev->vbasedev.sysfsdev);
  2411. subsys = realpath(tmp, NULL);
  2412. g_free(tmp);
  2413. is_mdev = subsys && (strcmp(subsys, "/sys/bus/mdev") == 0);
  2414. free(subsys);
  2415. trace_vfio_mdev(vdev->vbasedev.name, is_mdev);
  2416. if (vdev->vbasedev.ram_block_discard_allowed && !is_mdev) {
  2417. error_setg(errp, "x-balloon-allowed only potentially compatible "
  2418. "with mdev devices");
  2419. vfio_put_group(group);
  2420. goto error;
  2421. }
  2422. ret = vfio_get_device(group, vdev->vbasedev.name, &vdev->vbasedev, errp);
  2423. if (ret) {
  2424. vfio_put_group(group);
  2425. goto error;
  2426. }
  2427. vfio_populate_device(vdev, &err);
  2428. if (err) {
  2429. error_propagate(errp, err);
  2430. goto error;
  2431. }
  2432. /* Get a copy of config space */
  2433. ret = pread(vdev->vbasedev.fd, vdev->pdev.config,
  2434. MIN(pci_config_size(&vdev->pdev), vdev->config_size),
  2435. vdev->config_offset);
  2436. if (ret < (int)MIN(pci_config_size(&vdev->pdev), vdev->config_size)) {
  2437. ret = ret < 0 ? -errno : -EFAULT;
  2438. error_setg_errno(errp, -ret, "failed to read device config space");
  2439. goto error;
  2440. }
  2441. /* vfio emulates a lot for us, but some bits need extra love */
  2442. vdev->emulated_config_bits = g_malloc0(vdev->config_size);
  2443. /* QEMU can choose to expose the ROM or not */
  2444. memset(vdev->emulated_config_bits + PCI_ROM_ADDRESS, 0xff, 4);
  2445. /* QEMU can also add or extend BARs */
  2446. memset(vdev->emulated_config_bits + PCI_BASE_ADDRESS_0, 0xff, 6 * 4);
  2447. /*
  2448. * The PCI spec reserves vendor ID 0xffff as an invalid value. The
  2449. * device ID is managed by the vendor and need only be a 16-bit value.
  2450. * Allow any 16-bit value for subsystem so they can be hidden or changed.
  2451. */
  2452. if (vdev->vendor_id != PCI_ANY_ID) {
  2453. if (vdev->vendor_id >= 0xffff) {
  2454. error_setg(errp, "invalid PCI vendor ID provided");
  2455. goto error;
  2456. }
  2457. vfio_add_emulated_word(vdev, PCI_VENDOR_ID, vdev->vendor_id, ~0);
  2458. trace_vfio_pci_emulated_vendor_id(vdev->vbasedev.name, vdev->vendor_id);
  2459. } else {
  2460. vdev->vendor_id = pci_get_word(pdev->config + PCI_VENDOR_ID);
  2461. }
  2462. if (vdev->device_id != PCI_ANY_ID) {
  2463. if (vdev->device_id > 0xffff) {
  2464. error_setg(errp, "invalid PCI device ID provided");
  2465. goto error;
  2466. }
  2467. vfio_add_emulated_word(vdev, PCI_DEVICE_ID, vdev->device_id, ~0);
  2468. trace_vfio_pci_emulated_device_id(vdev->vbasedev.name, vdev->device_id);
  2469. } else {
  2470. vdev->device_id = pci_get_word(pdev->config + PCI_DEVICE_ID);
  2471. }
  2472. if (vdev->sub_vendor_id != PCI_ANY_ID) {
  2473. if (vdev->sub_vendor_id > 0xffff) {
  2474. error_setg(errp, "invalid PCI subsystem vendor ID provided");
  2475. goto error;
  2476. }
  2477. vfio_add_emulated_word(vdev, PCI_SUBSYSTEM_VENDOR_ID,
  2478. vdev->sub_vendor_id, ~0);
  2479. trace_vfio_pci_emulated_sub_vendor_id(vdev->vbasedev.name,
  2480. vdev->sub_vendor_id);
  2481. }
  2482. if (vdev->sub_device_id != PCI_ANY_ID) {
  2483. if (vdev->sub_device_id > 0xffff) {
  2484. error_setg(errp, "invalid PCI subsystem device ID provided");
  2485. goto error;
  2486. }
  2487. vfio_add_emulated_word(vdev, PCI_SUBSYSTEM_ID, vdev->sub_device_id, ~0);
  2488. trace_vfio_pci_emulated_sub_device_id(vdev->vbasedev.name,
  2489. vdev->sub_device_id);
  2490. }
  2491. /* QEMU can change multi-function devices to single function, or reverse */
  2492. vdev->emulated_config_bits[PCI_HEADER_TYPE] =
  2493. PCI_HEADER_TYPE_MULTI_FUNCTION;
  2494. /* Restore or clear multifunction, this is always controlled by QEMU */
  2495. if (vdev->pdev.cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
  2496. vdev->pdev.config[PCI_HEADER_TYPE] |= PCI_HEADER_TYPE_MULTI_FUNCTION;
  2497. } else {
  2498. vdev->pdev.config[PCI_HEADER_TYPE] &= ~PCI_HEADER_TYPE_MULTI_FUNCTION;
  2499. }
  2500. /*
  2501. * Clear host resource mapping info. If we choose not to register a
  2502. * BAR, such as might be the case with the option ROM, we can get
  2503. * confusing, unwritable, residual addresses from the host here.
  2504. */
  2505. memset(&vdev->pdev.config[PCI_BASE_ADDRESS_0], 0, 24);
  2506. memset(&vdev->pdev.config[PCI_ROM_ADDRESS], 0, 4);
  2507. vfio_pci_size_rom(vdev);
  2508. vfio_bars_prepare(vdev);
  2509. vfio_msix_early_setup(vdev, &err);
  2510. if (err) {
  2511. error_propagate(errp, err);
  2512. goto error;
  2513. }
  2514. vfio_bars_register(vdev);
  2515. ret = vfio_add_capabilities(vdev, errp);
  2516. if (ret) {
  2517. goto out_teardown;
  2518. }
  2519. if (vdev->vga) {
  2520. vfio_vga_quirk_setup(vdev);
  2521. }
  2522. for (i = 0; i < PCI_ROM_SLOT; i++) {
  2523. vfio_bar_quirk_setup(vdev, i);
  2524. }
  2525. if (!vdev->igd_opregion &&
  2526. vdev->features & VFIO_FEATURE_ENABLE_IGD_OPREGION) {
  2527. struct vfio_region_info *opregion;
  2528. if (vdev->pdev.qdev.hotplugged) {
  2529. error_setg(errp,
  2530. "cannot support IGD OpRegion feature on hotplugged "
  2531. "device");
  2532. goto out_teardown;
  2533. }
  2534. ret = vfio_get_dev_region_info(&vdev->vbasedev,
  2535. VFIO_REGION_TYPE_PCI_VENDOR_TYPE | PCI_VENDOR_ID_INTEL,
  2536. VFIO_REGION_SUBTYPE_INTEL_IGD_OPREGION, &opregion);
  2537. if (ret) {
  2538. error_setg_errno(errp, -ret,
  2539. "does not support requested IGD OpRegion feature");
  2540. goto out_teardown;
  2541. }
  2542. ret = vfio_pci_igd_opregion_init(vdev, opregion, errp);
  2543. g_free(opregion);
  2544. if (ret) {
  2545. goto out_teardown;
  2546. }
  2547. }
  2548. /* QEMU emulates all of MSI & MSIX */
  2549. if (pdev->cap_present & QEMU_PCI_CAP_MSIX) {
  2550. memset(vdev->emulated_config_bits + pdev->msix_cap, 0xff,
  2551. MSIX_CAP_LENGTH);
  2552. }
  2553. if (pdev->cap_present & QEMU_PCI_CAP_MSI) {
  2554. memset(vdev->emulated_config_bits + pdev->msi_cap, 0xff,
  2555. vdev->msi_cap_size);
  2556. }
  2557. if (vfio_pci_read_config(&vdev->pdev, PCI_INTERRUPT_PIN, 1)) {
  2558. vdev->intx.mmap_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
  2559. vfio_intx_mmap_enable, vdev);
  2560. pci_device_set_intx_routing_notifier(&vdev->pdev,
  2561. vfio_intx_routing_notifier);
  2562. vdev->irqchip_change_notifier.notify = vfio_irqchip_change;
  2563. kvm_irqchip_add_change_notifier(&vdev->irqchip_change_notifier);
  2564. ret = vfio_intx_enable(vdev, errp);
  2565. if (ret) {
  2566. goto out_deregister;
  2567. }
  2568. }
  2569. if (vdev->display != ON_OFF_AUTO_OFF) {
  2570. ret = vfio_display_probe(vdev, errp);
  2571. if (ret) {
  2572. goto out_deregister;
  2573. }
  2574. }
  2575. if (vdev->enable_ramfb && vdev->dpy == NULL) {
  2576. error_setg(errp, "ramfb=on requires display=on");
  2577. goto out_deregister;
  2578. }
  2579. if (vdev->display_xres || vdev->display_yres) {
  2580. if (vdev->dpy == NULL) {
  2581. error_setg(errp, "xres and yres properties require display=on");
  2582. goto out_deregister;
  2583. }
  2584. if (vdev->dpy->edid_regs == NULL) {
  2585. error_setg(errp, "xres and yres properties need edid support");
  2586. goto out_deregister;
  2587. }
  2588. }
  2589. if (vfio_pci_is(vdev, PCI_VENDOR_ID_NVIDIA, PCI_ANY_ID)) {
  2590. ret = vfio_pci_nvidia_v100_ram_init(vdev, errp);
  2591. if (ret && ret != -ENODEV) {
  2592. error_report("Failed to setup NVIDIA V100 GPU RAM");
  2593. }
  2594. }
  2595. if (vfio_pci_is(vdev, PCI_VENDOR_ID_IBM, PCI_ANY_ID)) {
  2596. ret = vfio_pci_nvlink2_init(vdev, errp);
  2597. if (ret && ret != -ENODEV) {
  2598. error_report("Failed to setup NVlink2 bridge");
  2599. }
  2600. }
  2601. if (!pdev->failover_pair_id) {
  2602. ret = vfio_migration_probe(&vdev->vbasedev, errp);
  2603. if (ret) {
  2604. error_report("%s: Migration disabled", vdev->vbasedev.name);
  2605. }
  2606. }
  2607. vfio_register_err_notifier(vdev);
  2608. vfio_register_req_notifier(vdev);
  2609. vfio_setup_resetfn_quirk(vdev);
  2610. return;
  2611. out_deregister:
  2612. pci_device_set_intx_routing_notifier(&vdev->pdev, NULL);
  2613. kvm_irqchip_remove_change_notifier(&vdev->irqchip_change_notifier);
  2614. out_teardown:
  2615. vfio_teardown_msi(vdev);
  2616. vfio_bars_exit(vdev);
  2617. error:
  2618. error_prepend(errp, VFIO_MSG_PREFIX, vdev->vbasedev.name);
  2619. }
  2620. static void vfio_instance_finalize(Object *obj)
  2621. {
  2622. VFIOPCIDevice *vdev = VFIO_PCI(obj);
  2623. VFIOGroup *group = vdev->vbasedev.group;
  2624. vfio_display_finalize(vdev);
  2625. vfio_bars_finalize(vdev);
  2626. g_free(vdev->emulated_config_bits);
  2627. g_free(vdev->rom);
  2628. /*
  2629. * XXX Leaking igd_opregion is not an oversight, we can't remove the
  2630. * fw_cfg entry therefore leaking this allocation seems like the safest
  2631. * option.
  2632. *
  2633. * g_free(vdev->igd_opregion);
  2634. */
  2635. vfio_put_device(vdev);
  2636. vfio_put_group(group);
  2637. }
  2638. static void vfio_exitfn(PCIDevice *pdev)
  2639. {
  2640. VFIOPCIDevice *vdev = VFIO_PCI(pdev);
  2641. vfio_unregister_req_notifier(vdev);
  2642. vfio_unregister_err_notifier(vdev);
  2643. pci_device_set_intx_routing_notifier(&vdev->pdev, NULL);
  2644. if (vdev->irqchip_change_notifier.notify) {
  2645. kvm_irqchip_remove_change_notifier(&vdev->irqchip_change_notifier);
  2646. }
  2647. vfio_disable_interrupts(vdev);
  2648. if (vdev->intx.mmap_timer) {
  2649. timer_free(vdev->intx.mmap_timer);
  2650. }
  2651. vfio_teardown_msi(vdev);
  2652. vfio_bars_exit(vdev);
  2653. vfio_migration_finalize(&vdev->vbasedev);
  2654. }
  2655. static void vfio_pci_reset(DeviceState *dev)
  2656. {
  2657. VFIOPCIDevice *vdev = VFIO_PCI(dev);
  2658. trace_vfio_pci_reset(vdev->vbasedev.name);
  2659. vfio_pci_pre_reset(vdev);
  2660. if (vdev->display != ON_OFF_AUTO_OFF) {
  2661. vfio_display_reset(vdev);
  2662. }
  2663. if (vdev->resetfn && !vdev->resetfn(vdev)) {
  2664. goto post_reset;
  2665. }
  2666. if (vdev->vbasedev.reset_works &&
  2667. (vdev->has_flr || !vdev->has_pm_reset) &&
  2668. !ioctl(vdev->vbasedev.fd, VFIO_DEVICE_RESET)) {
  2669. trace_vfio_pci_reset_flr(vdev->vbasedev.name);
  2670. goto post_reset;
  2671. }
  2672. /* See if we can do our own bus reset */
  2673. if (!vfio_pci_hot_reset_one(vdev)) {
  2674. goto post_reset;
  2675. }
  2676. /* If nothing else works and the device supports PM reset, use it */
  2677. if (vdev->vbasedev.reset_works && vdev->has_pm_reset &&
  2678. !ioctl(vdev->vbasedev.fd, VFIO_DEVICE_RESET)) {
  2679. trace_vfio_pci_reset_pm(vdev->vbasedev.name);
  2680. goto post_reset;
  2681. }
  2682. post_reset:
  2683. vfio_pci_post_reset(vdev);
  2684. }
  2685. static void vfio_instance_init(Object *obj)
  2686. {
  2687. PCIDevice *pci_dev = PCI_DEVICE(obj);
  2688. VFIOPCIDevice *vdev = VFIO_PCI(obj);
  2689. device_add_bootindex_property(obj, &vdev->bootindex,
  2690. "bootindex", NULL,
  2691. &pci_dev->qdev);
  2692. vdev->host.domain = ~0U;
  2693. vdev->host.bus = ~0U;
  2694. vdev->host.slot = ~0U;
  2695. vdev->host.function = ~0U;
  2696. vdev->nv_gpudirect_clique = 0xFF;
  2697. /* QEMU_PCI_CAP_EXPRESS initialization does not depend on QEMU command
  2698. * line, therefore, no need to wait to realize like other devices */
  2699. pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS;
  2700. }
  2701. static Property vfio_pci_dev_properties[] = {
  2702. DEFINE_PROP_PCI_HOST_DEVADDR("host", VFIOPCIDevice, host),
  2703. DEFINE_PROP_STRING("sysfsdev", VFIOPCIDevice, vbasedev.sysfsdev),
  2704. DEFINE_PROP_ON_OFF_AUTO("x-pre-copy-dirty-page-tracking", VFIOPCIDevice,
  2705. vbasedev.pre_copy_dirty_page_tracking,
  2706. ON_OFF_AUTO_ON),
  2707. DEFINE_PROP_ON_OFF_AUTO("display", VFIOPCIDevice,
  2708. display, ON_OFF_AUTO_OFF),
  2709. DEFINE_PROP_UINT32("xres", VFIOPCIDevice, display_xres, 0),
  2710. DEFINE_PROP_UINT32("yres", VFIOPCIDevice, display_yres, 0),
  2711. DEFINE_PROP_UINT32("x-intx-mmap-timeout-ms", VFIOPCIDevice,
  2712. intx.mmap_timeout, 1100),
  2713. DEFINE_PROP_BIT("x-vga", VFIOPCIDevice, features,
  2714. VFIO_FEATURE_ENABLE_VGA_BIT, false),
  2715. DEFINE_PROP_BIT("x-req", VFIOPCIDevice, features,
  2716. VFIO_FEATURE_ENABLE_REQ_BIT, true),
  2717. DEFINE_PROP_BIT("x-igd-opregion", VFIOPCIDevice, features,
  2718. VFIO_FEATURE_ENABLE_IGD_OPREGION_BIT, false),
  2719. DEFINE_PROP_BOOL("x-enable-migration", VFIOPCIDevice,
  2720. vbasedev.enable_migration, false),
  2721. DEFINE_PROP_BOOL("x-no-mmap", VFIOPCIDevice, vbasedev.no_mmap, false),
  2722. DEFINE_PROP_BOOL("x-balloon-allowed", VFIOPCIDevice,
  2723. vbasedev.ram_block_discard_allowed, false),
  2724. DEFINE_PROP_BOOL("x-no-kvm-intx", VFIOPCIDevice, no_kvm_intx, false),
  2725. DEFINE_PROP_BOOL("x-no-kvm-msi", VFIOPCIDevice, no_kvm_msi, false),
  2726. DEFINE_PROP_BOOL("x-no-kvm-msix", VFIOPCIDevice, no_kvm_msix, false),
  2727. DEFINE_PROP_BOOL("x-no-geforce-quirks", VFIOPCIDevice,
  2728. no_geforce_quirks, false),
  2729. DEFINE_PROP_BOOL("x-no-kvm-ioeventfd", VFIOPCIDevice, no_kvm_ioeventfd,
  2730. false),
  2731. DEFINE_PROP_BOOL("x-no-vfio-ioeventfd", VFIOPCIDevice, no_vfio_ioeventfd,
  2732. false),
  2733. DEFINE_PROP_UINT32("x-pci-vendor-id", VFIOPCIDevice, vendor_id, PCI_ANY_ID),
  2734. DEFINE_PROP_UINT32("x-pci-device-id", VFIOPCIDevice, device_id, PCI_ANY_ID),
  2735. DEFINE_PROP_UINT32("x-pci-sub-vendor-id", VFIOPCIDevice,
  2736. sub_vendor_id, PCI_ANY_ID),
  2737. DEFINE_PROP_UINT32("x-pci-sub-device-id", VFIOPCIDevice,
  2738. sub_device_id, PCI_ANY_ID),
  2739. DEFINE_PROP_UINT32("x-igd-gms", VFIOPCIDevice, igd_gms, 0),
  2740. DEFINE_PROP_UNSIGNED_NODEFAULT("x-nv-gpudirect-clique", VFIOPCIDevice,
  2741. nv_gpudirect_clique,
  2742. qdev_prop_nv_gpudirect_clique, uint8_t),
  2743. DEFINE_PROP_OFF_AUTO_PCIBAR("x-msix-relocation", VFIOPCIDevice, msix_relo,
  2744. OFF_AUTOPCIBAR_OFF),
  2745. /*
  2746. * TODO - support passed fds... is this necessary?
  2747. * DEFINE_PROP_STRING("vfiofd", VFIOPCIDevice, vfiofd_name),
  2748. * DEFINE_PROP_STRING("vfiogroupfd, VFIOPCIDevice, vfiogroupfd_name),
  2749. */
  2750. DEFINE_PROP_END_OF_LIST(),
  2751. };
  2752. static void vfio_pci_dev_class_init(ObjectClass *klass, void *data)
  2753. {
  2754. DeviceClass *dc = DEVICE_CLASS(klass);
  2755. PCIDeviceClass *pdc = PCI_DEVICE_CLASS(klass);
  2756. dc->reset = vfio_pci_reset;
  2757. device_class_set_props(dc, vfio_pci_dev_properties);
  2758. dc->desc = "VFIO-based PCI device assignment";
  2759. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  2760. pdc->realize = vfio_realize;
  2761. pdc->exit = vfio_exitfn;
  2762. pdc->config_read = vfio_pci_read_config;
  2763. pdc->config_write = vfio_pci_write_config;
  2764. }
  2765. static const TypeInfo vfio_pci_dev_info = {
  2766. .name = TYPE_VFIO_PCI,
  2767. .parent = TYPE_PCI_DEVICE,
  2768. .instance_size = sizeof(VFIOPCIDevice),
  2769. .class_init = vfio_pci_dev_class_init,
  2770. .instance_init = vfio_instance_init,
  2771. .instance_finalize = vfio_instance_finalize,
  2772. .interfaces = (InterfaceInfo[]) {
  2773. { INTERFACE_PCIE_DEVICE },
  2774. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  2775. { }
  2776. },
  2777. };
  2778. static Property vfio_pci_dev_nohotplug_properties[] = {
  2779. DEFINE_PROP_BOOL("ramfb", VFIOPCIDevice, enable_ramfb, false),
  2780. DEFINE_PROP_END_OF_LIST(),
  2781. };
  2782. static void vfio_pci_nohotplug_dev_class_init(ObjectClass *klass, void *data)
  2783. {
  2784. DeviceClass *dc = DEVICE_CLASS(klass);
  2785. device_class_set_props(dc, vfio_pci_dev_nohotplug_properties);
  2786. dc->hotpluggable = false;
  2787. }
  2788. static const TypeInfo vfio_pci_nohotplug_dev_info = {
  2789. .name = TYPE_VFIO_PCI_NOHOTPLUG,
  2790. .parent = TYPE_VFIO_PCI,
  2791. .instance_size = sizeof(VFIOPCIDevice),
  2792. .class_init = vfio_pci_nohotplug_dev_class_init,
  2793. };
  2794. static void register_vfio_pci_dev_type(void)
  2795. {
  2796. type_register_static(&vfio_pci_dev_info);
  2797. type_register_static(&vfio_pci_nohotplug_dev_info);
  2798. }
  2799. type_init(register_vfio_pci_dev_type)