pci.c 105 KB

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