pci.c 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820
  1. /*
  2. * QEMU PCI bus manager
  3. *
  4. * Copyright (c) 2004 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qemu-common.h"
  26. #include "hw/irq.h"
  27. #include "hw/pci/pci.h"
  28. #include "hw/pci/pci_bridge.h"
  29. #include "hw/pci/pci_bus.h"
  30. #include "hw/pci/pci_host.h"
  31. #include "hw/qdev-properties.h"
  32. #include "migration/qemu-file-types.h"
  33. #include "migration/vmstate.h"
  34. #include "monitor/monitor.h"
  35. #include "net/net.h"
  36. #include "sysemu/numa.h"
  37. #include "sysemu/sysemu.h"
  38. #include "hw/loader.h"
  39. #include "qemu/error-report.h"
  40. #include "qemu/range.h"
  41. #include "trace.h"
  42. #include "hw/pci/msi.h"
  43. #include "hw/pci/msix.h"
  44. #include "exec/address-spaces.h"
  45. #include "hw/hotplug.h"
  46. #include "hw/boards.h"
  47. #include "qapi/error.h"
  48. #include "qapi/qapi-commands-misc.h"
  49. #include "qemu/cutils.h"
  50. //#define DEBUG_PCI
  51. #ifdef DEBUG_PCI
  52. # define PCI_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
  53. #else
  54. # define PCI_DPRINTF(format, ...) do { } while (0)
  55. #endif
  56. bool pci_available = true;
  57. static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent);
  58. static char *pcibus_get_dev_path(DeviceState *dev);
  59. static char *pcibus_get_fw_dev_path(DeviceState *dev);
  60. static void pcibus_reset(BusState *qbus);
  61. static Property pci_props[] = {
  62. DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1),
  63. DEFINE_PROP_STRING("romfile", PCIDevice, romfile),
  64. DEFINE_PROP_UINT32("rombar", PCIDevice, rom_bar, 1),
  65. DEFINE_PROP_BIT("multifunction", PCIDevice, cap_present,
  66. QEMU_PCI_CAP_MULTIFUNCTION_BITNR, false),
  67. DEFINE_PROP_BIT("command_serr_enable", PCIDevice, cap_present,
  68. QEMU_PCI_CAP_SERR_BITNR, true),
  69. DEFINE_PROP_BIT("x-pcie-lnksta-dllla", PCIDevice, cap_present,
  70. QEMU_PCIE_LNKSTA_DLLLA_BITNR, true),
  71. DEFINE_PROP_BIT("x-pcie-extcap-init", PCIDevice, cap_present,
  72. QEMU_PCIE_EXTCAP_INIT_BITNR, true),
  73. DEFINE_PROP_STRING("failover_pair_id", PCIDevice,
  74. failover_pair_id),
  75. DEFINE_PROP_END_OF_LIST()
  76. };
  77. static const VMStateDescription vmstate_pcibus = {
  78. .name = "PCIBUS",
  79. .version_id = 1,
  80. .minimum_version_id = 1,
  81. .fields = (VMStateField[]) {
  82. VMSTATE_INT32_EQUAL(nirq, PCIBus, NULL),
  83. VMSTATE_VARRAY_INT32(irq_count, PCIBus,
  84. nirq, 0, vmstate_info_int32,
  85. int32_t),
  86. VMSTATE_END_OF_LIST()
  87. }
  88. };
  89. static void pci_init_bus_master(PCIDevice *pci_dev)
  90. {
  91. AddressSpace *dma_as = pci_device_iommu_address_space(pci_dev);
  92. memory_region_init_alias(&pci_dev->bus_master_enable_region,
  93. OBJECT(pci_dev), "bus master",
  94. dma_as->root, 0, memory_region_size(dma_as->root));
  95. memory_region_set_enabled(&pci_dev->bus_master_enable_region, false);
  96. memory_region_add_subregion(&pci_dev->bus_master_container_region, 0,
  97. &pci_dev->bus_master_enable_region);
  98. }
  99. static void pcibus_machine_done(Notifier *notifier, void *data)
  100. {
  101. PCIBus *bus = container_of(notifier, PCIBus, machine_done);
  102. int i;
  103. for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
  104. if (bus->devices[i]) {
  105. pci_init_bus_master(bus->devices[i]);
  106. }
  107. }
  108. }
  109. static void pci_bus_realize(BusState *qbus, Error **errp)
  110. {
  111. PCIBus *bus = PCI_BUS(qbus);
  112. bus->machine_done.notify = pcibus_machine_done;
  113. qemu_add_machine_init_done_notifier(&bus->machine_done);
  114. vmstate_register(NULL, -1, &vmstate_pcibus, bus);
  115. }
  116. static void pcie_bus_realize(BusState *qbus, Error **errp)
  117. {
  118. PCIBus *bus = PCI_BUS(qbus);
  119. pci_bus_realize(qbus, errp);
  120. /*
  121. * A PCI-E bus can support extended config space if it's the root
  122. * bus, or if the bus/bridge above it does as well
  123. */
  124. if (pci_bus_is_root(bus)) {
  125. bus->flags |= PCI_BUS_EXTENDED_CONFIG_SPACE;
  126. } else {
  127. PCIBus *parent_bus = pci_get_bus(bus->parent_dev);
  128. if (pci_bus_allows_extended_config_space(parent_bus)) {
  129. bus->flags |= PCI_BUS_EXTENDED_CONFIG_SPACE;
  130. }
  131. }
  132. }
  133. static void pci_bus_unrealize(BusState *qbus, Error **errp)
  134. {
  135. PCIBus *bus = PCI_BUS(qbus);
  136. qemu_remove_machine_init_done_notifier(&bus->machine_done);
  137. vmstate_unregister(NULL, &vmstate_pcibus, bus);
  138. }
  139. static int pcibus_num(PCIBus *bus)
  140. {
  141. if (pci_bus_is_root(bus)) {
  142. return 0; /* pci host bridge */
  143. }
  144. return bus->parent_dev->config[PCI_SECONDARY_BUS];
  145. }
  146. static uint16_t pcibus_numa_node(PCIBus *bus)
  147. {
  148. return NUMA_NODE_UNASSIGNED;
  149. }
  150. static void pci_bus_class_init(ObjectClass *klass, void *data)
  151. {
  152. BusClass *k = BUS_CLASS(klass);
  153. PCIBusClass *pbc = PCI_BUS_CLASS(klass);
  154. k->print_dev = pcibus_dev_print;
  155. k->get_dev_path = pcibus_get_dev_path;
  156. k->get_fw_dev_path = pcibus_get_fw_dev_path;
  157. k->realize = pci_bus_realize;
  158. k->unrealize = pci_bus_unrealize;
  159. k->reset = pcibus_reset;
  160. pbc->bus_num = pcibus_num;
  161. pbc->numa_node = pcibus_numa_node;
  162. }
  163. static const TypeInfo pci_bus_info = {
  164. .name = TYPE_PCI_BUS,
  165. .parent = TYPE_BUS,
  166. .instance_size = sizeof(PCIBus),
  167. .class_size = sizeof(PCIBusClass),
  168. .class_init = pci_bus_class_init,
  169. };
  170. static const TypeInfo pcie_interface_info = {
  171. .name = INTERFACE_PCIE_DEVICE,
  172. .parent = TYPE_INTERFACE,
  173. };
  174. static const TypeInfo conventional_pci_interface_info = {
  175. .name = INTERFACE_CONVENTIONAL_PCI_DEVICE,
  176. .parent = TYPE_INTERFACE,
  177. };
  178. static void pcie_bus_class_init(ObjectClass *klass, void *data)
  179. {
  180. BusClass *k = BUS_CLASS(klass);
  181. k->realize = pcie_bus_realize;
  182. }
  183. static const TypeInfo pcie_bus_info = {
  184. .name = TYPE_PCIE_BUS,
  185. .parent = TYPE_PCI_BUS,
  186. .class_init = pcie_bus_class_init,
  187. };
  188. static PCIBus *pci_find_bus_nr(PCIBus *bus, int bus_num);
  189. static void pci_update_mappings(PCIDevice *d);
  190. static void pci_irq_handler(void *opaque, int irq_num, int level);
  191. static void pci_add_option_rom(PCIDevice *pdev, bool is_default_rom, Error **);
  192. static void pci_del_option_rom(PCIDevice *pdev);
  193. static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET;
  194. static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU;
  195. static QLIST_HEAD(, PCIHostState) pci_host_bridges;
  196. int pci_bar(PCIDevice *d, int reg)
  197. {
  198. uint8_t type;
  199. if (reg != PCI_ROM_SLOT)
  200. return PCI_BASE_ADDRESS_0 + reg * 4;
  201. type = d->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
  202. return type == PCI_HEADER_TYPE_BRIDGE ? PCI_ROM_ADDRESS1 : PCI_ROM_ADDRESS;
  203. }
  204. static inline int pci_irq_state(PCIDevice *d, int irq_num)
  205. {
  206. return (d->irq_state >> irq_num) & 0x1;
  207. }
  208. static inline void pci_set_irq_state(PCIDevice *d, int irq_num, int level)
  209. {
  210. d->irq_state &= ~(0x1 << irq_num);
  211. d->irq_state |= level << irq_num;
  212. }
  213. static void pci_change_irq_level(PCIDevice *pci_dev, int irq_num, int change)
  214. {
  215. PCIBus *bus;
  216. for (;;) {
  217. bus = pci_get_bus(pci_dev);
  218. irq_num = bus->map_irq(pci_dev, irq_num);
  219. if (bus->set_irq)
  220. break;
  221. pci_dev = bus->parent_dev;
  222. }
  223. bus->irq_count[irq_num] += change;
  224. bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
  225. }
  226. int pci_bus_get_irq_level(PCIBus *bus, int irq_num)
  227. {
  228. assert(irq_num >= 0);
  229. assert(irq_num < bus->nirq);
  230. return !!bus->irq_count[irq_num];
  231. }
  232. /* Update interrupt status bit in config space on interrupt
  233. * state change. */
  234. static void pci_update_irq_status(PCIDevice *dev)
  235. {
  236. if (dev->irq_state) {
  237. dev->config[PCI_STATUS] |= PCI_STATUS_INTERRUPT;
  238. } else {
  239. dev->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT;
  240. }
  241. }
  242. void pci_device_deassert_intx(PCIDevice *dev)
  243. {
  244. int i;
  245. for (i = 0; i < PCI_NUM_PINS; ++i) {
  246. pci_irq_handler(dev, i, 0);
  247. }
  248. }
  249. static void pci_do_device_reset(PCIDevice *dev)
  250. {
  251. int r;
  252. pci_device_deassert_intx(dev);
  253. assert(dev->irq_state == 0);
  254. /* Clear all writable bits */
  255. pci_word_test_and_clear_mask(dev->config + PCI_COMMAND,
  256. pci_get_word(dev->wmask + PCI_COMMAND) |
  257. pci_get_word(dev->w1cmask + PCI_COMMAND));
  258. pci_word_test_and_clear_mask(dev->config + PCI_STATUS,
  259. pci_get_word(dev->wmask + PCI_STATUS) |
  260. pci_get_word(dev->w1cmask + PCI_STATUS));
  261. dev->config[PCI_CACHE_LINE_SIZE] = 0x0;
  262. dev->config[PCI_INTERRUPT_LINE] = 0x0;
  263. for (r = 0; r < PCI_NUM_REGIONS; ++r) {
  264. PCIIORegion *region = &dev->io_regions[r];
  265. if (!region->size) {
  266. continue;
  267. }
  268. if (!(region->type & PCI_BASE_ADDRESS_SPACE_IO) &&
  269. region->type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
  270. pci_set_quad(dev->config + pci_bar(dev, r), region->type);
  271. } else {
  272. pci_set_long(dev->config + pci_bar(dev, r), region->type);
  273. }
  274. }
  275. pci_update_mappings(dev);
  276. msi_reset(dev);
  277. msix_reset(dev);
  278. }
  279. /*
  280. * This function is called on #RST and FLR.
  281. * FLR if PCI_EXP_DEVCTL_BCR_FLR is set
  282. */
  283. void pci_device_reset(PCIDevice *dev)
  284. {
  285. qdev_reset_all(&dev->qdev);
  286. pci_do_device_reset(dev);
  287. }
  288. /*
  289. * Trigger pci bus reset under a given bus.
  290. * Called via qbus_reset_all on RST# assert, after the devices
  291. * have been reset qdev_reset_all-ed already.
  292. */
  293. static void pcibus_reset(BusState *qbus)
  294. {
  295. PCIBus *bus = DO_UPCAST(PCIBus, qbus, qbus);
  296. int i;
  297. for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
  298. if (bus->devices[i]) {
  299. pci_do_device_reset(bus->devices[i]);
  300. }
  301. }
  302. for (i = 0; i < bus->nirq; i++) {
  303. assert(bus->irq_count[i] == 0);
  304. }
  305. }
  306. static void pci_host_bus_register(DeviceState *host)
  307. {
  308. PCIHostState *host_bridge = PCI_HOST_BRIDGE(host);
  309. QLIST_INSERT_HEAD(&pci_host_bridges, host_bridge, next);
  310. }
  311. static void pci_host_bus_unregister(DeviceState *host)
  312. {
  313. PCIHostState *host_bridge = PCI_HOST_BRIDGE(host);
  314. QLIST_REMOVE(host_bridge, next);
  315. }
  316. PCIBus *pci_device_root_bus(const PCIDevice *d)
  317. {
  318. PCIBus *bus = pci_get_bus(d);
  319. while (!pci_bus_is_root(bus)) {
  320. d = bus->parent_dev;
  321. assert(d != NULL);
  322. bus = pci_get_bus(d);
  323. }
  324. return bus;
  325. }
  326. const char *pci_root_bus_path(PCIDevice *dev)
  327. {
  328. PCIBus *rootbus = pci_device_root_bus(dev);
  329. PCIHostState *host_bridge = PCI_HOST_BRIDGE(rootbus->qbus.parent);
  330. PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_GET_CLASS(host_bridge);
  331. assert(host_bridge->bus == rootbus);
  332. if (hc->root_bus_path) {
  333. return (*hc->root_bus_path)(host_bridge, rootbus);
  334. }
  335. return rootbus->qbus.name;
  336. }
  337. static void pci_root_bus_init(PCIBus *bus, DeviceState *parent,
  338. MemoryRegion *address_space_mem,
  339. MemoryRegion *address_space_io,
  340. uint8_t devfn_min)
  341. {
  342. assert(PCI_FUNC(devfn_min) == 0);
  343. bus->devfn_min = devfn_min;
  344. bus->slot_reserved_mask = 0x0;
  345. bus->address_space_mem = address_space_mem;
  346. bus->address_space_io = address_space_io;
  347. bus->flags |= PCI_BUS_IS_ROOT;
  348. /* host bridge */
  349. QLIST_INIT(&bus->child);
  350. pci_host_bus_register(parent);
  351. }
  352. static void pci_bus_uninit(PCIBus *bus)
  353. {
  354. pci_host_bus_unregister(BUS(bus)->parent);
  355. }
  356. bool pci_bus_is_express(PCIBus *bus)
  357. {
  358. return object_dynamic_cast(OBJECT(bus), TYPE_PCIE_BUS);
  359. }
  360. void pci_root_bus_new_inplace(PCIBus *bus, size_t bus_size, DeviceState *parent,
  361. const char *name,
  362. MemoryRegion *address_space_mem,
  363. MemoryRegion *address_space_io,
  364. uint8_t devfn_min, const char *typename)
  365. {
  366. qbus_create_inplace(bus, bus_size, typename, parent, name);
  367. pci_root_bus_init(bus, parent, address_space_mem, address_space_io,
  368. devfn_min);
  369. }
  370. PCIBus *pci_root_bus_new(DeviceState *parent, const char *name,
  371. MemoryRegion *address_space_mem,
  372. MemoryRegion *address_space_io,
  373. uint8_t devfn_min, const char *typename)
  374. {
  375. PCIBus *bus;
  376. bus = PCI_BUS(qbus_create(typename, parent, name));
  377. pci_root_bus_init(bus, parent, address_space_mem, address_space_io,
  378. devfn_min);
  379. return bus;
  380. }
  381. void pci_root_bus_cleanup(PCIBus *bus)
  382. {
  383. pci_bus_uninit(bus);
  384. /* the caller of the unplug hotplug handler will delete this device */
  385. object_property_set_bool(OBJECT(bus), false, "realized", NULL);
  386. }
  387. void pci_bus_irqs(PCIBus *bus, pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
  388. void *irq_opaque, int nirq)
  389. {
  390. bus->set_irq = set_irq;
  391. bus->map_irq = map_irq;
  392. bus->irq_opaque = irq_opaque;
  393. bus->nirq = nirq;
  394. bus->irq_count = g_malloc0(nirq * sizeof(bus->irq_count[0]));
  395. }
  396. void pci_bus_irqs_cleanup(PCIBus *bus)
  397. {
  398. bus->set_irq = NULL;
  399. bus->map_irq = NULL;
  400. bus->irq_opaque = NULL;
  401. bus->nirq = 0;
  402. g_free(bus->irq_count);
  403. }
  404. PCIBus *pci_register_root_bus(DeviceState *parent, const char *name,
  405. pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
  406. void *irq_opaque,
  407. MemoryRegion *address_space_mem,
  408. MemoryRegion *address_space_io,
  409. uint8_t devfn_min, int nirq,
  410. const char *typename)
  411. {
  412. PCIBus *bus;
  413. bus = pci_root_bus_new(parent, name, address_space_mem,
  414. address_space_io, devfn_min, typename);
  415. pci_bus_irqs(bus, set_irq, map_irq, irq_opaque, nirq);
  416. return bus;
  417. }
  418. void pci_unregister_root_bus(PCIBus *bus)
  419. {
  420. pci_bus_irqs_cleanup(bus);
  421. pci_root_bus_cleanup(bus);
  422. }
  423. int pci_bus_num(PCIBus *s)
  424. {
  425. return PCI_BUS_GET_CLASS(s)->bus_num(s);
  426. }
  427. int pci_bus_numa_node(PCIBus *bus)
  428. {
  429. return PCI_BUS_GET_CLASS(bus)->numa_node(bus);
  430. }
  431. static int get_pci_config_device(QEMUFile *f, void *pv, size_t size,
  432. const VMStateField *field)
  433. {
  434. PCIDevice *s = container_of(pv, PCIDevice, config);
  435. PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(s);
  436. uint8_t *config;
  437. int i;
  438. assert(size == pci_config_size(s));
  439. config = g_malloc(size);
  440. qemu_get_buffer(f, config, size);
  441. for (i = 0; i < size; ++i) {
  442. if ((config[i] ^ s->config[i]) &
  443. s->cmask[i] & ~s->wmask[i] & ~s->w1cmask[i]) {
  444. error_report("%s: Bad config data: i=0x%x read: %x device: %x "
  445. "cmask: %x wmask: %x w1cmask:%x", __func__,
  446. i, config[i], s->config[i],
  447. s->cmask[i], s->wmask[i], s->w1cmask[i]);
  448. g_free(config);
  449. return -EINVAL;
  450. }
  451. }
  452. memcpy(s->config, config, size);
  453. pci_update_mappings(s);
  454. if (pc->is_bridge) {
  455. PCIBridge *b = PCI_BRIDGE(s);
  456. pci_bridge_update_mappings(b);
  457. }
  458. memory_region_set_enabled(&s->bus_master_enable_region,
  459. pci_get_word(s->config + PCI_COMMAND)
  460. & PCI_COMMAND_MASTER);
  461. g_free(config);
  462. return 0;
  463. }
  464. /* just put buffer */
  465. static int put_pci_config_device(QEMUFile *f, void *pv, size_t size,
  466. const VMStateField *field, QJSON *vmdesc)
  467. {
  468. const uint8_t **v = pv;
  469. assert(size == pci_config_size(container_of(pv, PCIDevice, config)));
  470. qemu_put_buffer(f, *v, size);
  471. return 0;
  472. }
  473. static VMStateInfo vmstate_info_pci_config = {
  474. .name = "pci config",
  475. .get = get_pci_config_device,
  476. .put = put_pci_config_device,
  477. };
  478. static int get_pci_irq_state(QEMUFile *f, void *pv, size_t size,
  479. const VMStateField *field)
  480. {
  481. PCIDevice *s = container_of(pv, PCIDevice, irq_state);
  482. uint32_t irq_state[PCI_NUM_PINS];
  483. int i;
  484. for (i = 0; i < PCI_NUM_PINS; ++i) {
  485. irq_state[i] = qemu_get_be32(f);
  486. if (irq_state[i] != 0x1 && irq_state[i] != 0) {
  487. fprintf(stderr, "irq state %d: must be 0 or 1.\n",
  488. irq_state[i]);
  489. return -EINVAL;
  490. }
  491. }
  492. for (i = 0; i < PCI_NUM_PINS; ++i) {
  493. pci_set_irq_state(s, i, irq_state[i]);
  494. }
  495. return 0;
  496. }
  497. static int put_pci_irq_state(QEMUFile *f, void *pv, size_t size,
  498. const VMStateField *field, QJSON *vmdesc)
  499. {
  500. int i;
  501. PCIDevice *s = container_of(pv, PCIDevice, irq_state);
  502. for (i = 0; i < PCI_NUM_PINS; ++i) {
  503. qemu_put_be32(f, pci_irq_state(s, i));
  504. }
  505. return 0;
  506. }
  507. static VMStateInfo vmstate_info_pci_irq_state = {
  508. .name = "pci irq state",
  509. .get = get_pci_irq_state,
  510. .put = put_pci_irq_state,
  511. };
  512. static bool migrate_is_pcie(void *opaque, int version_id)
  513. {
  514. return pci_is_express((PCIDevice *)opaque);
  515. }
  516. static bool migrate_is_not_pcie(void *opaque, int version_id)
  517. {
  518. return !pci_is_express((PCIDevice *)opaque);
  519. }
  520. const VMStateDescription vmstate_pci_device = {
  521. .name = "PCIDevice",
  522. .version_id = 2,
  523. .minimum_version_id = 1,
  524. .fields = (VMStateField[]) {
  525. VMSTATE_INT32_POSITIVE_LE(version_id, PCIDevice),
  526. VMSTATE_BUFFER_UNSAFE_INFO_TEST(config, PCIDevice,
  527. migrate_is_not_pcie,
  528. 0, vmstate_info_pci_config,
  529. PCI_CONFIG_SPACE_SIZE),
  530. VMSTATE_BUFFER_UNSAFE_INFO_TEST(config, PCIDevice,
  531. migrate_is_pcie,
  532. 0, vmstate_info_pci_config,
  533. PCIE_CONFIG_SPACE_SIZE),
  534. VMSTATE_BUFFER_UNSAFE_INFO(irq_state, PCIDevice, 2,
  535. vmstate_info_pci_irq_state,
  536. PCI_NUM_PINS * sizeof(int32_t)),
  537. VMSTATE_END_OF_LIST()
  538. }
  539. };
  540. void pci_device_save(PCIDevice *s, QEMUFile *f)
  541. {
  542. /* Clear interrupt status bit: it is implicit
  543. * in irq_state which we are saving.
  544. * This makes us compatible with old devices
  545. * which never set or clear this bit. */
  546. s->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT;
  547. vmstate_save_state(f, &vmstate_pci_device, s, NULL);
  548. /* Restore the interrupt status bit. */
  549. pci_update_irq_status(s);
  550. }
  551. int pci_device_load(PCIDevice *s, QEMUFile *f)
  552. {
  553. int ret;
  554. ret = vmstate_load_state(f, &vmstate_pci_device, s, s->version_id);
  555. /* Restore the interrupt status bit. */
  556. pci_update_irq_status(s);
  557. return ret;
  558. }
  559. static void pci_set_default_subsystem_id(PCIDevice *pci_dev)
  560. {
  561. pci_set_word(pci_dev->config + PCI_SUBSYSTEM_VENDOR_ID,
  562. pci_default_sub_vendor_id);
  563. pci_set_word(pci_dev->config + PCI_SUBSYSTEM_ID,
  564. pci_default_sub_device_id);
  565. }
  566. /*
  567. * Parse [[<domain>:]<bus>:]<slot>, return -1 on error if funcp == NULL
  568. * [[<domain>:]<bus>:]<slot>.<func>, return -1 on error
  569. */
  570. static int pci_parse_devaddr(const char *addr, int *domp, int *busp,
  571. unsigned int *slotp, unsigned int *funcp)
  572. {
  573. const char *p;
  574. char *e;
  575. unsigned long val;
  576. unsigned long dom = 0, bus = 0;
  577. unsigned int slot = 0;
  578. unsigned int func = 0;
  579. p = addr;
  580. val = strtoul(p, &e, 16);
  581. if (e == p)
  582. return -1;
  583. if (*e == ':') {
  584. bus = val;
  585. p = e + 1;
  586. val = strtoul(p, &e, 16);
  587. if (e == p)
  588. return -1;
  589. if (*e == ':') {
  590. dom = bus;
  591. bus = val;
  592. p = e + 1;
  593. val = strtoul(p, &e, 16);
  594. if (e == p)
  595. return -1;
  596. }
  597. }
  598. slot = val;
  599. if (funcp != NULL) {
  600. if (*e != '.')
  601. return -1;
  602. p = e + 1;
  603. val = strtoul(p, &e, 16);
  604. if (e == p)
  605. return -1;
  606. func = val;
  607. }
  608. /* if funcp == NULL func is 0 */
  609. if (dom > 0xffff || bus > 0xff || slot > 0x1f || func > 7)
  610. return -1;
  611. if (*e)
  612. return -1;
  613. *domp = dom;
  614. *busp = bus;
  615. *slotp = slot;
  616. if (funcp != NULL)
  617. *funcp = func;
  618. return 0;
  619. }
  620. static void pci_init_cmask(PCIDevice *dev)
  621. {
  622. pci_set_word(dev->cmask + PCI_VENDOR_ID, 0xffff);
  623. pci_set_word(dev->cmask + PCI_DEVICE_ID, 0xffff);
  624. dev->cmask[PCI_STATUS] = PCI_STATUS_CAP_LIST;
  625. dev->cmask[PCI_REVISION_ID] = 0xff;
  626. dev->cmask[PCI_CLASS_PROG] = 0xff;
  627. pci_set_word(dev->cmask + PCI_CLASS_DEVICE, 0xffff);
  628. dev->cmask[PCI_HEADER_TYPE] = 0xff;
  629. dev->cmask[PCI_CAPABILITY_LIST] = 0xff;
  630. }
  631. static void pci_init_wmask(PCIDevice *dev)
  632. {
  633. int config_size = pci_config_size(dev);
  634. dev->wmask[PCI_CACHE_LINE_SIZE] = 0xff;
  635. dev->wmask[PCI_INTERRUPT_LINE] = 0xff;
  636. pci_set_word(dev->wmask + PCI_COMMAND,
  637. PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
  638. PCI_COMMAND_INTX_DISABLE);
  639. if (dev->cap_present & QEMU_PCI_CAP_SERR) {
  640. pci_word_test_and_set_mask(dev->wmask + PCI_COMMAND, PCI_COMMAND_SERR);
  641. }
  642. memset(dev->wmask + PCI_CONFIG_HEADER_SIZE, 0xff,
  643. config_size - PCI_CONFIG_HEADER_SIZE);
  644. }
  645. static void pci_init_w1cmask(PCIDevice *dev)
  646. {
  647. /*
  648. * Note: It's okay to set w1cmask even for readonly bits as
  649. * long as their value is hardwired to 0.
  650. */
  651. pci_set_word(dev->w1cmask + PCI_STATUS,
  652. PCI_STATUS_PARITY | PCI_STATUS_SIG_TARGET_ABORT |
  653. PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_REC_MASTER_ABORT |
  654. PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_DETECTED_PARITY);
  655. }
  656. static void pci_init_mask_bridge(PCIDevice *d)
  657. {
  658. /* PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS and
  659. PCI_SEC_LETENCY_TIMER */
  660. memset(d->wmask + PCI_PRIMARY_BUS, 0xff, 4);
  661. /* base and limit */
  662. d->wmask[PCI_IO_BASE] = PCI_IO_RANGE_MASK & 0xff;
  663. d->wmask[PCI_IO_LIMIT] = PCI_IO_RANGE_MASK & 0xff;
  664. pci_set_word(d->wmask + PCI_MEMORY_BASE,
  665. PCI_MEMORY_RANGE_MASK & 0xffff);
  666. pci_set_word(d->wmask + PCI_MEMORY_LIMIT,
  667. PCI_MEMORY_RANGE_MASK & 0xffff);
  668. pci_set_word(d->wmask + PCI_PREF_MEMORY_BASE,
  669. PCI_PREF_RANGE_MASK & 0xffff);
  670. pci_set_word(d->wmask + PCI_PREF_MEMORY_LIMIT,
  671. PCI_PREF_RANGE_MASK & 0xffff);
  672. /* PCI_PREF_BASE_UPPER32 and PCI_PREF_LIMIT_UPPER32 */
  673. memset(d->wmask + PCI_PREF_BASE_UPPER32, 0xff, 8);
  674. /* Supported memory and i/o types */
  675. d->config[PCI_IO_BASE] |= PCI_IO_RANGE_TYPE_16;
  676. d->config[PCI_IO_LIMIT] |= PCI_IO_RANGE_TYPE_16;
  677. pci_word_test_and_set_mask(d->config + PCI_PREF_MEMORY_BASE,
  678. PCI_PREF_RANGE_TYPE_64);
  679. pci_word_test_and_set_mask(d->config + PCI_PREF_MEMORY_LIMIT,
  680. PCI_PREF_RANGE_TYPE_64);
  681. /*
  682. * TODO: Bridges default to 10-bit VGA decoding but we currently only
  683. * implement 16-bit decoding (no alias support).
  684. */
  685. pci_set_word(d->wmask + PCI_BRIDGE_CONTROL,
  686. PCI_BRIDGE_CTL_PARITY |
  687. PCI_BRIDGE_CTL_SERR |
  688. PCI_BRIDGE_CTL_ISA |
  689. PCI_BRIDGE_CTL_VGA |
  690. PCI_BRIDGE_CTL_VGA_16BIT |
  691. PCI_BRIDGE_CTL_MASTER_ABORT |
  692. PCI_BRIDGE_CTL_BUS_RESET |
  693. PCI_BRIDGE_CTL_FAST_BACK |
  694. PCI_BRIDGE_CTL_DISCARD |
  695. PCI_BRIDGE_CTL_SEC_DISCARD |
  696. PCI_BRIDGE_CTL_DISCARD_SERR);
  697. /* Below does not do anything as we never set this bit, put here for
  698. * completeness. */
  699. pci_set_word(d->w1cmask + PCI_BRIDGE_CONTROL,
  700. PCI_BRIDGE_CTL_DISCARD_STATUS);
  701. d->cmask[PCI_IO_BASE] |= PCI_IO_RANGE_TYPE_MASK;
  702. d->cmask[PCI_IO_LIMIT] |= PCI_IO_RANGE_TYPE_MASK;
  703. pci_word_test_and_set_mask(d->cmask + PCI_PREF_MEMORY_BASE,
  704. PCI_PREF_RANGE_TYPE_MASK);
  705. pci_word_test_and_set_mask(d->cmask + PCI_PREF_MEMORY_LIMIT,
  706. PCI_PREF_RANGE_TYPE_MASK);
  707. }
  708. static void pci_init_multifunction(PCIBus *bus, PCIDevice *dev, Error **errp)
  709. {
  710. uint8_t slot = PCI_SLOT(dev->devfn);
  711. uint8_t func;
  712. if (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
  713. dev->config[PCI_HEADER_TYPE] |= PCI_HEADER_TYPE_MULTI_FUNCTION;
  714. }
  715. /*
  716. * multifunction bit is interpreted in two ways as follows.
  717. * - all functions must set the bit to 1.
  718. * Example: Intel X53
  719. * - function 0 must set the bit, but the rest function (> 0)
  720. * is allowed to leave the bit to 0.
  721. * Example: PIIX3(also in qemu), PIIX4(also in qemu), ICH10,
  722. *
  723. * So OS (at least Linux) checks the bit of only function 0,
  724. * and doesn't see the bit of function > 0.
  725. *
  726. * The below check allows both interpretation.
  727. */
  728. if (PCI_FUNC(dev->devfn)) {
  729. PCIDevice *f0 = bus->devices[PCI_DEVFN(slot, 0)];
  730. if (f0 && !(f0->cap_present & QEMU_PCI_CAP_MULTIFUNCTION)) {
  731. /* function 0 should set multifunction bit */
  732. error_setg(errp, "PCI: single function device can't be populated "
  733. "in function %x.%x", slot, PCI_FUNC(dev->devfn));
  734. return;
  735. }
  736. return;
  737. }
  738. if (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
  739. return;
  740. }
  741. /* function 0 indicates single function, so function > 0 must be NULL */
  742. for (func = 1; func < PCI_FUNC_MAX; ++func) {
  743. if (bus->devices[PCI_DEVFN(slot, func)]) {
  744. error_setg(errp, "PCI: %x.0 indicates single function, "
  745. "but %x.%x is already populated.",
  746. slot, slot, func);
  747. return;
  748. }
  749. }
  750. }
  751. static void pci_config_alloc(PCIDevice *pci_dev)
  752. {
  753. int config_size = pci_config_size(pci_dev);
  754. pci_dev->config = g_malloc0(config_size);
  755. pci_dev->cmask = g_malloc0(config_size);
  756. pci_dev->wmask = g_malloc0(config_size);
  757. pci_dev->w1cmask = g_malloc0(config_size);
  758. pci_dev->used = g_malloc0(config_size);
  759. }
  760. static void pci_config_free(PCIDevice *pci_dev)
  761. {
  762. g_free(pci_dev->config);
  763. g_free(pci_dev->cmask);
  764. g_free(pci_dev->wmask);
  765. g_free(pci_dev->w1cmask);
  766. g_free(pci_dev->used);
  767. }
  768. static void do_pci_unregister_device(PCIDevice *pci_dev)
  769. {
  770. pci_get_bus(pci_dev)->devices[pci_dev->devfn] = NULL;
  771. pci_config_free(pci_dev);
  772. if (memory_region_is_mapped(&pci_dev->bus_master_enable_region)) {
  773. memory_region_del_subregion(&pci_dev->bus_master_container_region,
  774. &pci_dev->bus_master_enable_region);
  775. }
  776. address_space_destroy(&pci_dev->bus_master_as);
  777. }
  778. /* Extract PCIReqIDCache into BDF format */
  779. static uint16_t pci_req_id_cache_extract(PCIReqIDCache *cache)
  780. {
  781. uint8_t bus_n;
  782. uint16_t result;
  783. switch (cache->type) {
  784. case PCI_REQ_ID_BDF:
  785. result = pci_get_bdf(cache->dev);
  786. break;
  787. case PCI_REQ_ID_SECONDARY_BUS:
  788. bus_n = pci_dev_bus_num(cache->dev);
  789. result = PCI_BUILD_BDF(bus_n, 0);
  790. break;
  791. default:
  792. error_report("Invalid PCI requester ID cache type: %d",
  793. cache->type);
  794. exit(1);
  795. break;
  796. }
  797. return result;
  798. }
  799. /* Parse bridges up to the root complex and return requester ID
  800. * cache for specific device. For full PCIe topology, the cache
  801. * result would be exactly the same as getting BDF of the device.
  802. * However, several tricks are required when system mixed up with
  803. * legacy PCI devices and PCIe-to-PCI bridges.
  804. *
  805. * Here we cache the proxy device (and type) not requester ID since
  806. * bus number might change from time to time.
  807. */
  808. static PCIReqIDCache pci_req_id_cache_get(PCIDevice *dev)
  809. {
  810. PCIDevice *parent;
  811. PCIReqIDCache cache = {
  812. .dev = dev,
  813. .type = PCI_REQ_ID_BDF,
  814. };
  815. while (!pci_bus_is_root(pci_get_bus(dev))) {
  816. /* We are under PCI/PCIe bridges */
  817. parent = pci_get_bus(dev)->parent_dev;
  818. if (pci_is_express(parent)) {
  819. if (pcie_cap_get_type(parent) == PCI_EXP_TYPE_PCI_BRIDGE) {
  820. /* When we pass through PCIe-to-PCI/PCIX bridges, we
  821. * override the requester ID using secondary bus
  822. * number of parent bridge with zeroed devfn
  823. * (pcie-to-pci bridge spec chap 2.3). */
  824. cache.type = PCI_REQ_ID_SECONDARY_BUS;
  825. cache.dev = dev;
  826. }
  827. } else {
  828. /* Legacy PCI, override requester ID with the bridge's
  829. * BDF upstream. When the root complex connects to
  830. * legacy PCI devices (including buses), it can only
  831. * obtain requester ID info from directly attached
  832. * devices. If devices are attached under bridges, only
  833. * the requester ID of the bridge that is directly
  834. * attached to the root complex can be recognized. */
  835. cache.type = PCI_REQ_ID_BDF;
  836. cache.dev = parent;
  837. }
  838. dev = parent;
  839. }
  840. return cache;
  841. }
  842. uint16_t pci_requester_id(PCIDevice *dev)
  843. {
  844. return pci_req_id_cache_extract(&dev->requester_id_cache);
  845. }
  846. static bool pci_bus_devfn_available(PCIBus *bus, int devfn)
  847. {
  848. return !(bus->devices[devfn]);
  849. }
  850. static bool pci_bus_devfn_reserved(PCIBus *bus, int devfn)
  851. {
  852. return bus->slot_reserved_mask & (1UL << PCI_SLOT(devfn));
  853. }
  854. /* -1 for devfn means auto assign */
  855. static PCIDevice *do_pci_register_device(PCIDevice *pci_dev,
  856. const char *name, int devfn,
  857. Error **errp)
  858. {
  859. PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev);
  860. PCIConfigReadFunc *config_read = pc->config_read;
  861. PCIConfigWriteFunc *config_write = pc->config_write;
  862. Error *local_err = NULL;
  863. DeviceState *dev = DEVICE(pci_dev);
  864. PCIBus *bus = pci_get_bus(pci_dev);
  865. /* Only pci bridges can be attached to extra PCI root buses */
  866. if (pci_bus_is_root(bus) && bus->parent_dev && !pc->is_bridge) {
  867. error_setg(errp,
  868. "PCI: Only PCI/PCIe bridges can be plugged into %s",
  869. bus->parent_dev->name);
  870. return NULL;
  871. }
  872. if (devfn < 0) {
  873. for(devfn = bus->devfn_min ; devfn < ARRAY_SIZE(bus->devices);
  874. devfn += PCI_FUNC_MAX) {
  875. if (pci_bus_devfn_available(bus, devfn) &&
  876. !pci_bus_devfn_reserved(bus, devfn)) {
  877. goto found;
  878. }
  879. }
  880. error_setg(errp, "PCI: no slot/function available for %s, all in use "
  881. "or reserved", name);
  882. return NULL;
  883. found: ;
  884. } else if (pci_bus_devfn_reserved(bus, devfn)) {
  885. error_setg(errp, "PCI: slot %d function %d not available for %s,"
  886. " reserved",
  887. PCI_SLOT(devfn), PCI_FUNC(devfn), name);
  888. return NULL;
  889. } else if (!pci_bus_devfn_available(bus, devfn)) {
  890. error_setg(errp, "PCI: slot %d function %d not available for %s,"
  891. " in use by %s",
  892. PCI_SLOT(devfn), PCI_FUNC(devfn), name,
  893. bus->devices[devfn]->name);
  894. return NULL;
  895. } else if (dev->hotplugged &&
  896. pci_get_function_0(pci_dev)) {
  897. error_setg(errp, "PCI: slot %d function 0 already ocuppied by %s,"
  898. " new func %s cannot be exposed to guest.",
  899. PCI_SLOT(pci_get_function_0(pci_dev)->devfn),
  900. pci_get_function_0(pci_dev)->name,
  901. name);
  902. return NULL;
  903. }
  904. pci_dev->devfn = devfn;
  905. pci_dev->requester_id_cache = pci_req_id_cache_get(pci_dev);
  906. pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
  907. memory_region_init(&pci_dev->bus_master_container_region, OBJECT(pci_dev),
  908. "bus master container", UINT64_MAX);
  909. address_space_init(&pci_dev->bus_master_as,
  910. &pci_dev->bus_master_container_region, pci_dev->name);
  911. if (qdev_hotplug) {
  912. pci_init_bus_master(pci_dev);
  913. }
  914. pci_dev->irq_state = 0;
  915. pci_config_alloc(pci_dev);
  916. pci_config_set_vendor_id(pci_dev->config, pc->vendor_id);
  917. pci_config_set_device_id(pci_dev->config, pc->device_id);
  918. pci_config_set_revision(pci_dev->config, pc->revision);
  919. pci_config_set_class(pci_dev->config, pc->class_id);
  920. if (!pc->is_bridge) {
  921. if (pc->subsystem_vendor_id || pc->subsystem_id) {
  922. pci_set_word(pci_dev->config + PCI_SUBSYSTEM_VENDOR_ID,
  923. pc->subsystem_vendor_id);
  924. pci_set_word(pci_dev->config + PCI_SUBSYSTEM_ID,
  925. pc->subsystem_id);
  926. } else {
  927. pci_set_default_subsystem_id(pci_dev);
  928. }
  929. } else {
  930. /* subsystem_vendor_id/subsystem_id are only for header type 0 */
  931. assert(!pc->subsystem_vendor_id);
  932. assert(!pc->subsystem_id);
  933. }
  934. pci_init_cmask(pci_dev);
  935. pci_init_wmask(pci_dev);
  936. pci_init_w1cmask(pci_dev);
  937. if (pc->is_bridge) {
  938. pci_init_mask_bridge(pci_dev);
  939. }
  940. pci_init_multifunction(bus, pci_dev, &local_err);
  941. if (local_err) {
  942. error_propagate(errp, local_err);
  943. do_pci_unregister_device(pci_dev);
  944. return NULL;
  945. }
  946. if (!config_read)
  947. config_read = pci_default_read_config;
  948. if (!config_write)
  949. config_write = pci_default_write_config;
  950. pci_dev->config_read = config_read;
  951. pci_dev->config_write = config_write;
  952. bus->devices[devfn] = pci_dev;
  953. pci_dev->version_id = 2; /* Current pci device vmstate version */
  954. return pci_dev;
  955. }
  956. static void pci_unregister_io_regions(PCIDevice *pci_dev)
  957. {
  958. PCIIORegion *r;
  959. int i;
  960. for(i = 0; i < PCI_NUM_REGIONS; i++) {
  961. r = &pci_dev->io_regions[i];
  962. if (!r->size || r->addr == PCI_BAR_UNMAPPED)
  963. continue;
  964. memory_region_del_subregion(r->address_space, r->memory);
  965. }
  966. pci_unregister_vga(pci_dev);
  967. }
  968. static void pci_qdev_unrealize(DeviceState *dev, Error **errp)
  969. {
  970. PCIDevice *pci_dev = PCI_DEVICE(dev);
  971. PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev);
  972. pci_unregister_io_regions(pci_dev);
  973. pci_del_option_rom(pci_dev);
  974. if (pc->exit) {
  975. pc->exit(pci_dev);
  976. }
  977. pci_device_deassert_intx(pci_dev);
  978. do_pci_unregister_device(pci_dev);
  979. }
  980. void pci_register_bar(PCIDevice *pci_dev, int region_num,
  981. uint8_t type, MemoryRegion *memory)
  982. {
  983. PCIIORegion *r;
  984. uint32_t addr; /* offset in pci config space */
  985. uint64_t wmask;
  986. pcibus_t size = memory_region_size(memory);
  987. assert(region_num >= 0);
  988. assert(region_num < PCI_NUM_REGIONS);
  989. if (size & (size-1)) {
  990. error_report("ERROR: PCI region size must be pow2 "
  991. "type=0x%x, size=0x%"FMT_PCIBUS"", type, size);
  992. exit(1);
  993. }
  994. r = &pci_dev->io_regions[region_num];
  995. r->addr = PCI_BAR_UNMAPPED;
  996. r->size = size;
  997. r->type = type;
  998. r->memory = memory;
  999. r->address_space = type & PCI_BASE_ADDRESS_SPACE_IO
  1000. ? pci_get_bus(pci_dev)->address_space_io
  1001. : pci_get_bus(pci_dev)->address_space_mem;
  1002. wmask = ~(size - 1);
  1003. if (region_num == PCI_ROM_SLOT) {
  1004. /* ROM enable bit is writable */
  1005. wmask |= PCI_ROM_ADDRESS_ENABLE;
  1006. }
  1007. addr = pci_bar(pci_dev, region_num);
  1008. pci_set_long(pci_dev->config + addr, type);
  1009. if (!(r->type & PCI_BASE_ADDRESS_SPACE_IO) &&
  1010. r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
  1011. pci_set_quad(pci_dev->wmask + addr, wmask);
  1012. pci_set_quad(pci_dev->cmask + addr, ~0ULL);
  1013. } else {
  1014. pci_set_long(pci_dev->wmask + addr, wmask & 0xffffffff);
  1015. pci_set_long(pci_dev->cmask + addr, 0xffffffff);
  1016. }
  1017. }
  1018. static void pci_update_vga(PCIDevice *pci_dev)
  1019. {
  1020. uint16_t cmd;
  1021. if (!pci_dev->has_vga) {
  1022. return;
  1023. }
  1024. cmd = pci_get_word(pci_dev->config + PCI_COMMAND);
  1025. memory_region_set_enabled(pci_dev->vga_regions[QEMU_PCI_VGA_MEM],
  1026. cmd & PCI_COMMAND_MEMORY);
  1027. memory_region_set_enabled(pci_dev->vga_regions[QEMU_PCI_VGA_IO_LO],
  1028. cmd & PCI_COMMAND_IO);
  1029. memory_region_set_enabled(pci_dev->vga_regions[QEMU_PCI_VGA_IO_HI],
  1030. cmd & PCI_COMMAND_IO);
  1031. }
  1032. void pci_register_vga(PCIDevice *pci_dev, MemoryRegion *mem,
  1033. MemoryRegion *io_lo, MemoryRegion *io_hi)
  1034. {
  1035. PCIBus *bus = pci_get_bus(pci_dev);
  1036. assert(!pci_dev->has_vga);
  1037. assert(memory_region_size(mem) == QEMU_PCI_VGA_MEM_SIZE);
  1038. pci_dev->vga_regions[QEMU_PCI_VGA_MEM] = mem;
  1039. memory_region_add_subregion_overlap(bus->address_space_mem,
  1040. QEMU_PCI_VGA_MEM_BASE, mem, 1);
  1041. assert(memory_region_size(io_lo) == QEMU_PCI_VGA_IO_LO_SIZE);
  1042. pci_dev->vga_regions[QEMU_PCI_VGA_IO_LO] = io_lo;
  1043. memory_region_add_subregion_overlap(bus->address_space_io,
  1044. QEMU_PCI_VGA_IO_LO_BASE, io_lo, 1);
  1045. assert(memory_region_size(io_hi) == QEMU_PCI_VGA_IO_HI_SIZE);
  1046. pci_dev->vga_regions[QEMU_PCI_VGA_IO_HI] = io_hi;
  1047. memory_region_add_subregion_overlap(bus->address_space_io,
  1048. QEMU_PCI_VGA_IO_HI_BASE, io_hi, 1);
  1049. pci_dev->has_vga = true;
  1050. pci_update_vga(pci_dev);
  1051. }
  1052. void pci_unregister_vga(PCIDevice *pci_dev)
  1053. {
  1054. PCIBus *bus = pci_get_bus(pci_dev);
  1055. if (!pci_dev->has_vga) {
  1056. return;
  1057. }
  1058. memory_region_del_subregion(bus->address_space_mem,
  1059. pci_dev->vga_regions[QEMU_PCI_VGA_MEM]);
  1060. memory_region_del_subregion(bus->address_space_io,
  1061. pci_dev->vga_regions[QEMU_PCI_VGA_IO_LO]);
  1062. memory_region_del_subregion(bus->address_space_io,
  1063. pci_dev->vga_regions[QEMU_PCI_VGA_IO_HI]);
  1064. pci_dev->has_vga = false;
  1065. }
  1066. pcibus_t pci_get_bar_addr(PCIDevice *pci_dev, int region_num)
  1067. {
  1068. return pci_dev->io_regions[region_num].addr;
  1069. }
  1070. static pcibus_t pci_bar_address(PCIDevice *d,
  1071. int reg, uint8_t type, pcibus_t size)
  1072. {
  1073. pcibus_t new_addr, last_addr;
  1074. int bar = pci_bar(d, reg);
  1075. uint16_t cmd = pci_get_word(d->config + PCI_COMMAND);
  1076. Object *machine = qdev_get_machine();
  1077. ObjectClass *oc = object_get_class(machine);
  1078. MachineClass *mc = MACHINE_CLASS(oc);
  1079. bool allow_0_address = mc->pci_allow_0_address;
  1080. if (type & PCI_BASE_ADDRESS_SPACE_IO) {
  1081. if (!(cmd & PCI_COMMAND_IO)) {
  1082. return PCI_BAR_UNMAPPED;
  1083. }
  1084. new_addr = pci_get_long(d->config + bar) & ~(size - 1);
  1085. last_addr = new_addr + size - 1;
  1086. /* Check if 32 bit BAR wraps around explicitly.
  1087. * TODO: make priorities correct and remove this work around.
  1088. */
  1089. if (last_addr <= new_addr || last_addr >= UINT32_MAX ||
  1090. (!allow_0_address && new_addr == 0)) {
  1091. return PCI_BAR_UNMAPPED;
  1092. }
  1093. return new_addr;
  1094. }
  1095. if (!(cmd & PCI_COMMAND_MEMORY)) {
  1096. return PCI_BAR_UNMAPPED;
  1097. }
  1098. if (type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
  1099. new_addr = pci_get_quad(d->config + bar);
  1100. } else {
  1101. new_addr = pci_get_long(d->config + bar);
  1102. }
  1103. /* the ROM slot has a specific enable bit */
  1104. if (reg == PCI_ROM_SLOT && !(new_addr & PCI_ROM_ADDRESS_ENABLE)) {
  1105. return PCI_BAR_UNMAPPED;
  1106. }
  1107. new_addr &= ~(size - 1);
  1108. last_addr = new_addr + size - 1;
  1109. /* NOTE: we do not support wrapping */
  1110. /* XXX: as we cannot support really dynamic
  1111. mappings, we handle specific values as invalid
  1112. mappings. */
  1113. if (last_addr <= new_addr || last_addr == PCI_BAR_UNMAPPED ||
  1114. (!allow_0_address && new_addr == 0)) {
  1115. return PCI_BAR_UNMAPPED;
  1116. }
  1117. /* Now pcibus_t is 64bit.
  1118. * Check if 32 bit BAR wraps around explicitly.
  1119. * Without this, PC ide doesn't work well.
  1120. * TODO: remove this work around.
  1121. */
  1122. if (!(type & PCI_BASE_ADDRESS_MEM_TYPE_64) && last_addr >= UINT32_MAX) {
  1123. return PCI_BAR_UNMAPPED;
  1124. }
  1125. /*
  1126. * OS is allowed to set BAR beyond its addressable
  1127. * bits. For example, 32 bit OS can set 64bit bar
  1128. * to >4G. Check it. TODO: we might need to support
  1129. * it in the future for e.g. PAE.
  1130. */
  1131. if (last_addr >= HWADDR_MAX) {
  1132. return PCI_BAR_UNMAPPED;
  1133. }
  1134. return new_addr;
  1135. }
  1136. static void pci_update_mappings(PCIDevice *d)
  1137. {
  1138. PCIIORegion *r;
  1139. int i;
  1140. pcibus_t new_addr;
  1141. for(i = 0; i < PCI_NUM_REGIONS; i++) {
  1142. r = &d->io_regions[i];
  1143. /* this region isn't registered */
  1144. if (!r->size)
  1145. continue;
  1146. new_addr = pci_bar_address(d, i, r->type, r->size);
  1147. /* This bar isn't changed */
  1148. if (new_addr == r->addr)
  1149. continue;
  1150. /* now do the real mapping */
  1151. if (r->addr != PCI_BAR_UNMAPPED) {
  1152. trace_pci_update_mappings_del(d, pci_dev_bus_num(d),
  1153. PCI_SLOT(d->devfn),
  1154. PCI_FUNC(d->devfn),
  1155. i, r->addr, r->size);
  1156. memory_region_del_subregion(r->address_space, r->memory);
  1157. }
  1158. r->addr = new_addr;
  1159. if (r->addr != PCI_BAR_UNMAPPED) {
  1160. trace_pci_update_mappings_add(d, pci_dev_bus_num(d),
  1161. PCI_SLOT(d->devfn),
  1162. PCI_FUNC(d->devfn),
  1163. i, r->addr, r->size);
  1164. memory_region_add_subregion_overlap(r->address_space,
  1165. r->addr, r->memory, 1);
  1166. }
  1167. }
  1168. pci_update_vga(d);
  1169. }
  1170. static inline int pci_irq_disabled(PCIDevice *d)
  1171. {
  1172. return pci_get_word(d->config + PCI_COMMAND) & PCI_COMMAND_INTX_DISABLE;
  1173. }
  1174. /* Called after interrupt disabled field update in config space,
  1175. * assert/deassert interrupts if necessary.
  1176. * Gets original interrupt disable bit value (before update). */
  1177. static void pci_update_irq_disabled(PCIDevice *d, int was_irq_disabled)
  1178. {
  1179. int i, disabled = pci_irq_disabled(d);
  1180. if (disabled == was_irq_disabled)
  1181. return;
  1182. for (i = 0; i < PCI_NUM_PINS; ++i) {
  1183. int state = pci_irq_state(d, i);
  1184. pci_change_irq_level(d, i, disabled ? -state : state);
  1185. }
  1186. }
  1187. uint32_t pci_default_read_config(PCIDevice *d,
  1188. uint32_t address, int len)
  1189. {
  1190. uint32_t val = 0;
  1191. if (pci_is_express_downstream_port(d) &&
  1192. ranges_overlap(address, len, d->exp.exp_cap + PCI_EXP_LNKSTA, 2)) {
  1193. pcie_sync_bridge_lnk(d);
  1194. }
  1195. memcpy(&val, d->config + address, len);
  1196. return le32_to_cpu(val);
  1197. }
  1198. void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val_in, int l)
  1199. {
  1200. int i, was_irq_disabled = pci_irq_disabled(d);
  1201. uint32_t val = val_in;
  1202. for (i = 0; i < l; val >>= 8, ++i) {
  1203. uint8_t wmask = d->wmask[addr + i];
  1204. uint8_t w1cmask = d->w1cmask[addr + i];
  1205. assert(!(wmask & w1cmask));
  1206. d->config[addr + i] = (d->config[addr + i] & ~wmask) | (val & wmask);
  1207. d->config[addr + i] &= ~(val & w1cmask); /* W1C: Write 1 to Clear */
  1208. }
  1209. if (ranges_overlap(addr, l, PCI_BASE_ADDRESS_0, 24) ||
  1210. ranges_overlap(addr, l, PCI_ROM_ADDRESS, 4) ||
  1211. ranges_overlap(addr, l, PCI_ROM_ADDRESS1, 4) ||
  1212. range_covers_byte(addr, l, PCI_COMMAND))
  1213. pci_update_mappings(d);
  1214. if (range_covers_byte(addr, l, PCI_COMMAND)) {
  1215. pci_update_irq_disabled(d, was_irq_disabled);
  1216. memory_region_set_enabled(&d->bus_master_enable_region,
  1217. pci_get_word(d->config + PCI_COMMAND)
  1218. & PCI_COMMAND_MASTER);
  1219. }
  1220. msi_write_config(d, addr, val_in, l);
  1221. msix_write_config(d, addr, val_in, l);
  1222. }
  1223. /***********************************************************/
  1224. /* generic PCI irq support */
  1225. /* 0 <= irq_num <= 3. level must be 0 or 1 */
  1226. static void pci_irq_handler(void *opaque, int irq_num, int level)
  1227. {
  1228. PCIDevice *pci_dev = opaque;
  1229. int change;
  1230. change = level - pci_irq_state(pci_dev, irq_num);
  1231. if (!change)
  1232. return;
  1233. pci_set_irq_state(pci_dev, irq_num, level);
  1234. pci_update_irq_status(pci_dev);
  1235. if (pci_irq_disabled(pci_dev))
  1236. return;
  1237. pci_change_irq_level(pci_dev, irq_num, change);
  1238. }
  1239. static inline int pci_intx(PCIDevice *pci_dev)
  1240. {
  1241. return pci_get_byte(pci_dev->config + PCI_INTERRUPT_PIN) - 1;
  1242. }
  1243. qemu_irq pci_allocate_irq(PCIDevice *pci_dev)
  1244. {
  1245. int intx = pci_intx(pci_dev);
  1246. return qemu_allocate_irq(pci_irq_handler, pci_dev, intx);
  1247. }
  1248. void pci_set_irq(PCIDevice *pci_dev, int level)
  1249. {
  1250. int intx = pci_intx(pci_dev);
  1251. pci_irq_handler(pci_dev, intx, level);
  1252. }
  1253. /* Special hooks used by device assignment */
  1254. void pci_bus_set_route_irq_fn(PCIBus *bus, pci_route_irq_fn route_intx_to_irq)
  1255. {
  1256. assert(pci_bus_is_root(bus));
  1257. bus->route_intx_to_irq = route_intx_to_irq;
  1258. }
  1259. PCIINTxRoute pci_device_route_intx_to_irq(PCIDevice *dev, int pin)
  1260. {
  1261. PCIBus *bus;
  1262. do {
  1263. bus = pci_get_bus(dev);
  1264. pin = bus->map_irq(dev, pin);
  1265. dev = bus->parent_dev;
  1266. } while (dev);
  1267. if (!bus->route_intx_to_irq) {
  1268. error_report("PCI: Bug - unimplemented PCI INTx routing (%s)",
  1269. object_get_typename(OBJECT(bus->qbus.parent)));
  1270. return (PCIINTxRoute) { PCI_INTX_DISABLED, -1 };
  1271. }
  1272. return bus->route_intx_to_irq(bus->irq_opaque, pin);
  1273. }
  1274. bool pci_intx_route_changed(PCIINTxRoute *old, PCIINTxRoute *new)
  1275. {
  1276. return old->mode != new->mode || old->irq != new->irq;
  1277. }
  1278. void pci_bus_fire_intx_routing_notifier(PCIBus *bus)
  1279. {
  1280. PCIDevice *dev;
  1281. PCIBus *sec;
  1282. int i;
  1283. for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
  1284. dev = bus->devices[i];
  1285. if (dev && dev->intx_routing_notifier) {
  1286. dev->intx_routing_notifier(dev);
  1287. }
  1288. }
  1289. QLIST_FOREACH(sec, &bus->child, sibling) {
  1290. pci_bus_fire_intx_routing_notifier(sec);
  1291. }
  1292. }
  1293. void pci_device_set_intx_routing_notifier(PCIDevice *dev,
  1294. PCIINTxRoutingNotifier notifier)
  1295. {
  1296. dev->intx_routing_notifier = notifier;
  1297. }
  1298. /*
  1299. * PCI-to-PCI bridge specification
  1300. * 9.1: Interrupt routing. Table 9-1
  1301. *
  1302. * the PCI Express Base Specification, Revision 2.1
  1303. * 2.2.8.1: INTx interrutp signaling - Rules
  1304. * the Implementation Note
  1305. * Table 2-20
  1306. */
  1307. /*
  1308. * 0 <= pin <= 3 0 = INTA, 1 = INTB, 2 = INTC, 3 = INTD
  1309. * 0-origin unlike PCI interrupt pin register.
  1310. */
  1311. int pci_swizzle_map_irq_fn(PCIDevice *pci_dev, int pin)
  1312. {
  1313. return pci_swizzle(PCI_SLOT(pci_dev->devfn), pin);
  1314. }
  1315. /***********************************************************/
  1316. /* monitor info on PCI */
  1317. typedef struct {
  1318. uint16_t class;
  1319. const char *desc;
  1320. const char *fw_name;
  1321. uint16_t fw_ign_bits;
  1322. } pci_class_desc;
  1323. static const pci_class_desc pci_class_descriptions[] =
  1324. {
  1325. { 0x0001, "VGA controller", "display"},
  1326. { 0x0100, "SCSI controller", "scsi"},
  1327. { 0x0101, "IDE controller", "ide"},
  1328. { 0x0102, "Floppy controller", "fdc"},
  1329. { 0x0103, "IPI controller", "ipi"},
  1330. { 0x0104, "RAID controller", "raid"},
  1331. { 0x0106, "SATA controller"},
  1332. { 0x0107, "SAS controller"},
  1333. { 0x0180, "Storage controller"},
  1334. { 0x0200, "Ethernet controller", "ethernet"},
  1335. { 0x0201, "Token Ring controller", "token-ring"},
  1336. { 0x0202, "FDDI controller", "fddi"},
  1337. { 0x0203, "ATM controller", "atm"},
  1338. { 0x0280, "Network controller"},
  1339. { 0x0300, "VGA controller", "display", 0x00ff},
  1340. { 0x0301, "XGA controller"},
  1341. { 0x0302, "3D controller"},
  1342. { 0x0380, "Display controller"},
  1343. { 0x0400, "Video controller", "video"},
  1344. { 0x0401, "Audio controller", "sound"},
  1345. { 0x0402, "Phone"},
  1346. { 0x0403, "Audio controller", "sound"},
  1347. { 0x0480, "Multimedia controller"},
  1348. { 0x0500, "RAM controller", "memory"},
  1349. { 0x0501, "Flash controller", "flash"},
  1350. { 0x0580, "Memory controller"},
  1351. { 0x0600, "Host bridge", "host"},
  1352. { 0x0601, "ISA bridge", "isa"},
  1353. { 0x0602, "EISA bridge", "eisa"},
  1354. { 0x0603, "MC bridge", "mca"},
  1355. { 0x0604, "PCI bridge", "pci-bridge"},
  1356. { 0x0605, "PCMCIA bridge", "pcmcia"},
  1357. { 0x0606, "NUBUS bridge", "nubus"},
  1358. { 0x0607, "CARDBUS bridge", "cardbus"},
  1359. { 0x0608, "RACEWAY bridge"},
  1360. { 0x0680, "Bridge"},
  1361. { 0x0700, "Serial port", "serial"},
  1362. { 0x0701, "Parallel port", "parallel"},
  1363. { 0x0800, "Interrupt controller", "interrupt-controller"},
  1364. { 0x0801, "DMA controller", "dma-controller"},
  1365. { 0x0802, "Timer", "timer"},
  1366. { 0x0803, "RTC", "rtc"},
  1367. { 0x0900, "Keyboard", "keyboard"},
  1368. { 0x0901, "Pen", "pen"},
  1369. { 0x0902, "Mouse", "mouse"},
  1370. { 0x0A00, "Dock station", "dock", 0x00ff},
  1371. { 0x0B00, "i386 cpu", "cpu", 0x00ff},
  1372. { 0x0c00, "Fireware contorller", "fireware"},
  1373. { 0x0c01, "Access bus controller", "access-bus"},
  1374. { 0x0c02, "SSA controller", "ssa"},
  1375. { 0x0c03, "USB controller", "usb"},
  1376. { 0x0c04, "Fibre channel controller", "fibre-channel"},
  1377. { 0x0c05, "SMBus"},
  1378. { 0, NULL}
  1379. };
  1380. static void pci_for_each_device_under_bus_reverse(PCIBus *bus,
  1381. void (*fn)(PCIBus *b,
  1382. PCIDevice *d,
  1383. void *opaque),
  1384. void *opaque)
  1385. {
  1386. PCIDevice *d;
  1387. int devfn;
  1388. for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
  1389. d = bus->devices[ARRAY_SIZE(bus->devices) - 1 - devfn];
  1390. if (d) {
  1391. fn(bus, d, opaque);
  1392. }
  1393. }
  1394. }
  1395. void pci_for_each_device_reverse(PCIBus *bus, int bus_num,
  1396. void (*fn)(PCIBus *b, PCIDevice *d, void *opaque),
  1397. void *opaque)
  1398. {
  1399. bus = pci_find_bus_nr(bus, bus_num);
  1400. if (bus) {
  1401. pci_for_each_device_under_bus_reverse(bus, fn, opaque);
  1402. }
  1403. }
  1404. static void pci_for_each_device_under_bus(PCIBus *bus,
  1405. void (*fn)(PCIBus *b, PCIDevice *d,
  1406. void *opaque),
  1407. void *opaque)
  1408. {
  1409. PCIDevice *d;
  1410. int devfn;
  1411. for(devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
  1412. d = bus->devices[devfn];
  1413. if (d) {
  1414. fn(bus, d, opaque);
  1415. }
  1416. }
  1417. }
  1418. void pci_for_each_device(PCIBus *bus, int bus_num,
  1419. void (*fn)(PCIBus *b, PCIDevice *d, void *opaque),
  1420. void *opaque)
  1421. {
  1422. bus = pci_find_bus_nr(bus, bus_num);
  1423. if (bus) {
  1424. pci_for_each_device_under_bus(bus, fn, opaque);
  1425. }
  1426. }
  1427. static const pci_class_desc *get_class_desc(int class)
  1428. {
  1429. const pci_class_desc *desc;
  1430. desc = pci_class_descriptions;
  1431. while (desc->desc && class != desc->class) {
  1432. desc++;
  1433. }
  1434. return desc;
  1435. }
  1436. static PciDeviceInfoList *qmp_query_pci_devices(PCIBus *bus, int bus_num);
  1437. static PciMemoryRegionList *qmp_query_pci_regions(const PCIDevice *dev)
  1438. {
  1439. PciMemoryRegionList *head = NULL, *cur_item = NULL;
  1440. int i;
  1441. for (i = 0; i < PCI_NUM_REGIONS; i++) {
  1442. const PCIIORegion *r = &dev->io_regions[i];
  1443. PciMemoryRegionList *region;
  1444. if (!r->size) {
  1445. continue;
  1446. }
  1447. region = g_malloc0(sizeof(*region));
  1448. region->value = g_malloc0(sizeof(*region->value));
  1449. if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
  1450. region->value->type = g_strdup("io");
  1451. } else {
  1452. region->value->type = g_strdup("memory");
  1453. region->value->has_prefetch = true;
  1454. region->value->prefetch = !!(r->type & PCI_BASE_ADDRESS_MEM_PREFETCH);
  1455. region->value->has_mem_type_64 = true;
  1456. region->value->mem_type_64 = !!(r->type & PCI_BASE_ADDRESS_MEM_TYPE_64);
  1457. }
  1458. region->value->bar = i;
  1459. region->value->address = r->addr;
  1460. region->value->size = r->size;
  1461. /* XXX: waiting for the qapi to support GSList */
  1462. if (!cur_item) {
  1463. head = cur_item = region;
  1464. } else {
  1465. cur_item->next = region;
  1466. cur_item = region;
  1467. }
  1468. }
  1469. return head;
  1470. }
  1471. static PciBridgeInfo *qmp_query_pci_bridge(PCIDevice *dev, PCIBus *bus,
  1472. int bus_num)
  1473. {
  1474. PciBridgeInfo *info;
  1475. PciMemoryRange *range;
  1476. info = g_new0(PciBridgeInfo, 1);
  1477. info->bus = g_new0(PciBusInfo, 1);
  1478. info->bus->number = dev->config[PCI_PRIMARY_BUS];
  1479. info->bus->secondary = dev->config[PCI_SECONDARY_BUS];
  1480. info->bus->subordinate = dev->config[PCI_SUBORDINATE_BUS];
  1481. range = info->bus->io_range = g_new0(PciMemoryRange, 1);
  1482. range->base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_IO);
  1483. range->limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_IO);
  1484. range = info->bus->memory_range = g_new0(PciMemoryRange, 1);
  1485. range->base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_MEMORY);
  1486. range->limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_MEMORY);
  1487. range = info->bus->prefetchable_range = g_new0(PciMemoryRange, 1);
  1488. range->base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
  1489. range->limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
  1490. if (dev->config[PCI_SECONDARY_BUS] != 0) {
  1491. PCIBus *child_bus = pci_find_bus_nr(bus, dev->config[PCI_SECONDARY_BUS]);
  1492. if (child_bus) {
  1493. info->has_devices = true;
  1494. info->devices = qmp_query_pci_devices(child_bus, dev->config[PCI_SECONDARY_BUS]);
  1495. }
  1496. }
  1497. return info;
  1498. }
  1499. static PciDeviceInfo *qmp_query_pci_device(PCIDevice *dev, PCIBus *bus,
  1500. int bus_num)
  1501. {
  1502. const pci_class_desc *desc;
  1503. PciDeviceInfo *info;
  1504. uint8_t type;
  1505. int class;
  1506. info = g_new0(PciDeviceInfo, 1);
  1507. info->bus = bus_num;
  1508. info->slot = PCI_SLOT(dev->devfn);
  1509. info->function = PCI_FUNC(dev->devfn);
  1510. info->class_info = g_new0(PciDeviceClass, 1);
  1511. class = pci_get_word(dev->config + PCI_CLASS_DEVICE);
  1512. info->class_info->q_class = class;
  1513. desc = get_class_desc(class);
  1514. if (desc->desc) {
  1515. info->class_info->has_desc = true;
  1516. info->class_info->desc = g_strdup(desc->desc);
  1517. }
  1518. info->id = g_new0(PciDeviceId, 1);
  1519. info->id->vendor = pci_get_word(dev->config + PCI_VENDOR_ID);
  1520. info->id->device = pci_get_word(dev->config + PCI_DEVICE_ID);
  1521. info->regions = qmp_query_pci_regions(dev);
  1522. info->qdev_id = g_strdup(dev->qdev.id ? dev->qdev.id : "");
  1523. if (dev->config[PCI_INTERRUPT_PIN] != 0) {
  1524. info->has_irq = true;
  1525. info->irq = dev->config[PCI_INTERRUPT_LINE];
  1526. }
  1527. type = dev->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
  1528. if (type == PCI_HEADER_TYPE_BRIDGE) {
  1529. info->has_pci_bridge = true;
  1530. info->pci_bridge = qmp_query_pci_bridge(dev, bus, bus_num);
  1531. } else if (type == PCI_HEADER_TYPE_NORMAL) {
  1532. info->id->has_subsystem = info->id->has_subsystem_vendor = true;
  1533. info->id->subsystem = pci_get_word(dev->config + PCI_SUBSYSTEM_ID);
  1534. info->id->subsystem_vendor =
  1535. pci_get_word(dev->config + PCI_SUBSYSTEM_VENDOR_ID);
  1536. } else if (type == PCI_HEADER_TYPE_CARDBUS) {
  1537. info->id->has_subsystem = info->id->has_subsystem_vendor = true;
  1538. info->id->subsystem = pci_get_word(dev->config + PCI_CB_SUBSYSTEM_ID);
  1539. info->id->subsystem_vendor =
  1540. pci_get_word(dev->config + PCI_CB_SUBSYSTEM_VENDOR_ID);
  1541. }
  1542. return info;
  1543. }
  1544. static PciDeviceInfoList *qmp_query_pci_devices(PCIBus *bus, int bus_num)
  1545. {
  1546. PciDeviceInfoList *info, *head = NULL, *cur_item = NULL;
  1547. PCIDevice *dev;
  1548. int devfn;
  1549. for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
  1550. dev = bus->devices[devfn];
  1551. if (dev) {
  1552. info = g_malloc0(sizeof(*info));
  1553. info->value = qmp_query_pci_device(dev, bus, bus_num);
  1554. /* XXX: waiting for the qapi to support GSList */
  1555. if (!cur_item) {
  1556. head = cur_item = info;
  1557. } else {
  1558. cur_item->next = info;
  1559. cur_item = info;
  1560. }
  1561. }
  1562. }
  1563. return head;
  1564. }
  1565. static PciInfo *qmp_query_pci_bus(PCIBus *bus, int bus_num)
  1566. {
  1567. PciInfo *info = NULL;
  1568. bus = pci_find_bus_nr(bus, bus_num);
  1569. if (bus) {
  1570. info = g_malloc0(sizeof(*info));
  1571. info->bus = bus_num;
  1572. info->devices = qmp_query_pci_devices(bus, bus_num);
  1573. }
  1574. return info;
  1575. }
  1576. PciInfoList *qmp_query_pci(Error **errp)
  1577. {
  1578. PciInfoList *info, *head = NULL, *cur_item = NULL;
  1579. PCIHostState *host_bridge;
  1580. QLIST_FOREACH(host_bridge, &pci_host_bridges, next) {
  1581. info = g_malloc0(sizeof(*info));
  1582. info->value = qmp_query_pci_bus(host_bridge->bus,
  1583. pci_bus_num(host_bridge->bus));
  1584. /* XXX: waiting for the qapi to support GSList */
  1585. if (!cur_item) {
  1586. head = cur_item = info;
  1587. } else {
  1588. cur_item->next = info;
  1589. cur_item = info;
  1590. }
  1591. }
  1592. return head;
  1593. }
  1594. /* Initialize a PCI NIC. */
  1595. PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus,
  1596. const char *default_model,
  1597. const char *default_devaddr)
  1598. {
  1599. const char *devaddr = nd->devaddr ? nd->devaddr : default_devaddr;
  1600. GSList *list;
  1601. GPtrArray *pci_nic_models;
  1602. PCIBus *bus;
  1603. PCIDevice *pci_dev;
  1604. DeviceState *dev;
  1605. int devfn;
  1606. int i;
  1607. int dom, busnr;
  1608. unsigned slot;
  1609. if (nd->model && !strcmp(nd->model, "virtio")) {
  1610. g_free(nd->model);
  1611. nd->model = g_strdup("virtio-net-pci");
  1612. }
  1613. list = object_class_get_list_sorted(TYPE_PCI_DEVICE, false);
  1614. pci_nic_models = g_ptr_array_new();
  1615. while (list) {
  1616. DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, list->data,
  1617. TYPE_DEVICE);
  1618. GSList *next;
  1619. if (test_bit(DEVICE_CATEGORY_NETWORK, dc->categories) &&
  1620. dc->user_creatable) {
  1621. const char *name = object_class_get_name(list->data);
  1622. g_ptr_array_add(pci_nic_models, (gpointer)name);
  1623. }
  1624. next = list->next;
  1625. g_slist_free_1(list);
  1626. list = next;
  1627. }
  1628. g_ptr_array_add(pci_nic_models, NULL);
  1629. if (qemu_show_nic_models(nd->model, (const char **)pci_nic_models->pdata)) {
  1630. exit(0);
  1631. }
  1632. i = qemu_find_nic_model(nd, (const char **)pci_nic_models->pdata,
  1633. default_model);
  1634. if (i < 0) {
  1635. exit(1);
  1636. }
  1637. if (!rootbus) {
  1638. error_report("No primary PCI bus");
  1639. exit(1);
  1640. }
  1641. assert(!rootbus->parent_dev);
  1642. if (!devaddr) {
  1643. devfn = -1;
  1644. busnr = 0;
  1645. } else {
  1646. if (pci_parse_devaddr(devaddr, &dom, &busnr, &slot, NULL) < 0) {
  1647. error_report("Invalid PCI device address %s for device %s",
  1648. devaddr, nd->model);
  1649. exit(1);
  1650. }
  1651. if (dom != 0) {
  1652. error_report("No support for non-zero PCI domains");
  1653. exit(1);
  1654. }
  1655. devfn = PCI_DEVFN(slot, 0);
  1656. }
  1657. bus = pci_find_bus_nr(rootbus, busnr);
  1658. if (!bus) {
  1659. error_report("Invalid PCI device address %s for device %s",
  1660. devaddr, nd->model);
  1661. exit(1);
  1662. }
  1663. pci_dev = pci_create(bus, devfn, nd->model);
  1664. dev = &pci_dev->qdev;
  1665. qdev_set_nic_properties(dev, nd);
  1666. qdev_init_nofail(dev);
  1667. g_ptr_array_free(pci_nic_models, true);
  1668. return pci_dev;
  1669. }
  1670. PCIDevice *pci_vga_init(PCIBus *bus)
  1671. {
  1672. switch (vga_interface_type) {
  1673. case VGA_CIRRUS:
  1674. return pci_create_simple(bus, -1, "cirrus-vga");
  1675. case VGA_QXL:
  1676. return pci_create_simple(bus, -1, "qxl-vga");
  1677. case VGA_STD:
  1678. return pci_create_simple(bus, -1, "VGA");
  1679. case VGA_VMWARE:
  1680. return pci_create_simple(bus, -1, "vmware-svga");
  1681. case VGA_VIRTIO:
  1682. return pci_create_simple(bus, -1, "virtio-vga");
  1683. case VGA_NONE:
  1684. default: /* Other non-PCI types. Checking for unsupported types is already
  1685. done in vl.c. */
  1686. return NULL;
  1687. }
  1688. }
  1689. /* Whether a given bus number is in range of the secondary
  1690. * bus of the given bridge device. */
  1691. static bool pci_secondary_bus_in_range(PCIDevice *dev, int bus_num)
  1692. {
  1693. return !(pci_get_word(dev->config + PCI_BRIDGE_CONTROL) &
  1694. PCI_BRIDGE_CTL_BUS_RESET) /* Don't walk the bus if it's reset. */ &&
  1695. dev->config[PCI_SECONDARY_BUS] <= bus_num &&
  1696. bus_num <= dev->config[PCI_SUBORDINATE_BUS];
  1697. }
  1698. /* Whether a given bus number is in a range of a root bus */
  1699. static bool pci_root_bus_in_range(PCIBus *bus, int bus_num)
  1700. {
  1701. int i;
  1702. for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
  1703. PCIDevice *dev = bus->devices[i];
  1704. if (dev && PCI_DEVICE_GET_CLASS(dev)->is_bridge) {
  1705. if (pci_secondary_bus_in_range(dev, bus_num)) {
  1706. return true;
  1707. }
  1708. }
  1709. }
  1710. return false;
  1711. }
  1712. static PCIBus *pci_find_bus_nr(PCIBus *bus, int bus_num)
  1713. {
  1714. PCIBus *sec;
  1715. if (!bus) {
  1716. return NULL;
  1717. }
  1718. if (pci_bus_num(bus) == bus_num) {
  1719. return bus;
  1720. }
  1721. /* Consider all bus numbers in range for the host pci bridge. */
  1722. if (!pci_bus_is_root(bus) &&
  1723. !pci_secondary_bus_in_range(bus->parent_dev, bus_num)) {
  1724. return NULL;
  1725. }
  1726. /* try child bus */
  1727. for (; bus; bus = sec) {
  1728. QLIST_FOREACH(sec, &bus->child, sibling) {
  1729. if (pci_bus_num(sec) == bus_num) {
  1730. return sec;
  1731. }
  1732. /* PXB buses assumed to be children of bus 0 */
  1733. if (pci_bus_is_root(sec)) {
  1734. if (pci_root_bus_in_range(sec, bus_num)) {
  1735. break;
  1736. }
  1737. } else {
  1738. if (pci_secondary_bus_in_range(sec->parent_dev, bus_num)) {
  1739. break;
  1740. }
  1741. }
  1742. }
  1743. }
  1744. return NULL;
  1745. }
  1746. void pci_for_each_bus_depth_first(PCIBus *bus,
  1747. void *(*begin)(PCIBus *bus, void *parent_state),
  1748. void (*end)(PCIBus *bus, void *state),
  1749. void *parent_state)
  1750. {
  1751. PCIBus *sec;
  1752. void *state;
  1753. if (!bus) {
  1754. return;
  1755. }
  1756. if (begin) {
  1757. state = begin(bus, parent_state);
  1758. } else {
  1759. state = parent_state;
  1760. }
  1761. QLIST_FOREACH(sec, &bus->child, sibling) {
  1762. pci_for_each_bus_depth_first(sec, begin, end, state);
  1763. }
  1764. if (end) {
  1765. end(bus, state);
  1766. }
  1767. }
  1768. PCIDevice *pci_find_device(PCIBus *bus, int bus_num, uint8_t devfn)
  1769. {
  1770. bus = pci_find_bus_nr(bus, bus_num);
  1771. if (!bus)
  1772. return NULL;
  1773. return bus->devices[devfn];
  1774. }
  1775. static void pci_qdev_realize(DeviceState *qdev, Error **errp)
  1776. {
  1777. PCIDevice *pci_dev = (PCIDevice *)qdev;
  1778. PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev);
  1779. ObjectClass *klass = OBJECT_CLASS(pc);
  1780. Error *local_err = NULL;
  1781. bool is_default_rom;
  1782. uint16_t class_id;
  1783. /* initialize cap_present for pci_is_express() and pci_config_size(),
  1784. * Note that hybrid PCIs are not set automatically and need to manage
  1785. * QEMU_PCI_CAP_EXPRESS manually */
  1786. if (object_class_dynamic_cast(klass, INTERFACE_PCIE_DEVICE) &&
  1787. !object_class_dynamic_cast(klass, INTERFACE_CONVENTIONAL_PCI_DEVICE)) {
  1788. pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS;
  1789. }
  1790. pci_dev = do_pci_register_device(pci_dev,
  1791. object_get_typename(OBJECT(qdev)),
  1792. pci_dev->devfn, errp);
  1793. if (pci_dev == NULL)
  1794. return;
  1795. if (pc->realize) {
  1796. pc->realize(pci_dev, &local_err);
  1797. if (local_err) {
  1798. error_propagate(errp, local_err);
  1799. do_pci_unregister_device(pci_dev);
  1800. return;
  1801. }
  1802. }
  1803. if (pci_dev->failover_pair_id) {
  1804. if (!pci_bus_is_express(pci_get_bus(pci_dev))) {
  1805. error_setg(errp, "failover primary device must be on "
  1806. "PCIExpress bus");
  1807. error_propagate(errp, local_err);
  1808. pci_qdev_unrealize(DEVICE(pci_dev), NULL);
  1809. return;
  1810. }
  1811. class_id = pci_get_word(pci_dev->config + PCI_CLASS_DEVICE);
  1812. if (class_id != PCI_CLASS_NETWORK_ETHERNET) {
  1813. error_setg(errp, "failover primary device is not an "
  1814. "Ethernet device");
  1815. error_propagate(errp, local_err);
  1816. pci_qdev_unrealize(DEVICE(pci_dev), NULL);
  1817. return;
  1818. }
  1819. if (!(pci_dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION)
  1820. && (PCI_FUNC(pci_dev->devfn) == 0)) {
  1821. qdev->allow_unplug_during_migration = true;
  1822. } else {
  1823. error_setg(errp, "failover: primary device must be in its own "
  1824. "PCI slot");
  1825. error_propagate(errp, local_err);
  1826. pci_qdev_unrealize(DEVICE(pci_dev), NULL);
  1827. return;
  1828. }
  1829. qdev->allow_unplug_during_migration = true;
  1830. }
  1831. /* rom loading */
  1832. is_default_rom = false;
  1833. if (pci_dev->romfile == NULL && pc->romfile != NULL) {
  1834. pci_dev->romfile = g_strdup(pc->romfile);
  1835. is_default_rom = true;
  1836. }
  1837. pci_add_option_rom(pci_dev, is_default_rom, &local_err);
  1838. if (local_err) {
  1839. error_propagate(errp, local_err);
  1840. pci_qdev_unrealize(DEVICE(pci_dev), NULL);
  1841. return;
  1842. }
  1843. }
  1844. PCIDevice *pci_create_multifunction(PCIBus *bus, int devfn, bool multifunction,
  1845. const char *name)
  1846. {
  1847. DeviceState *dev;
  1848. dev = qdev_create(&bus->qbus, name);
  1849. qdev_prop_set_int32(dev, "addr", devfn);
  1850. qdev_prop_set_bit(dev, "multifunction", multifunction);
  1851. return PCI_DEVICE(dev);
  1852. }
  1853. PCIDevice *pci_create_simple_multifunction(PCIBus *bus, int devfn,
  1854. bool multifunction,
  1855. const char *name)
  1856. {
  1857. PCIDevice *dev = pci_create_multifunction(bus, devfn, multifunction, name);
  1858. qdev_init_nofail(&dev->qdev);
  1859. return dev;
  1860. }
  1861. PCIDevice *pci_create(PCIBus *bus, int devfn, const char *name)
  1862. {
  1863. return pci_create_multifunction(bus, devfn, false, name);
  1864. }
  1865. PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
  1866. {
  1867. return pci_create_simple_multifunction(bus, devfn, false, name);
  1868. }
  1869. static uint8_t pci_find_space(PCIDevice *pdev, uint8_t size)
  1870. {
  1871. int offset = PCI_CONFIG_HEADER_SIZE;
  1872. int i;
  1873. for (i = PCI_CONFIG_HEADER_SIZE; i < PCI_CONFIG_SPACE_SIZE; ++i) {
  1874. if (pdev->used[i])
  1875. offset = i + 1;
  1876. else if (i - offset + 1 == size)
  1877. return offset;
  1878. }
  1879. return 0;
  1880. }
  1881. static uint8_t pci_find_capability_list(PCIDevice *pdev, uint8_t cap_id,
  1882. uint8_t *prev_p)
  1883. {
  1884. uint8_t next, prev;
  1885. if (!(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST))
  1886. return 0;
  1887. for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]);
  1888. prev = next + PCI_CAP_LIST_NEXT)
  1889. if (pdev->config[next + PCI_CAP_LIST_ID] == cap_id)
  1890. break;
  1891. if (prev_p)
  1892. *prev_p = prev;
  1893. return next;
  1894. }
  1895. static uint8_t pci_find_capability_at_offset(PCIDevice *pdev, uint8_t offset)
  1896. {
  1897. uint8_t next, prev, found = 0;
  1898. if (!(pdev->used[offset])) {
  1899. return 0;
  1900. }
  1901. assert(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST);
  1902. for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]);
  1903. prev = next + PCI_CAP_LIST_NEXT) {
  1904. if (next <= offset && next > found) {
  1905. found = next;
  1906. }
  1907. }
  1908. return found;
  1909. }
  1910. /* Patch the PCI vendor and device ids in a PCI rom image if necessary.
  1911. This is needed for an option rom which is used for more than one device. */
  1912. static void pci_patch_ids(PCIDevice *pdev, uint8_t *ptr, int size)
  1913. {
  1914. uint16_t vendor_id;
  1915. uint16_t device_id;
  1916. uint16_t rom_vendor_id;
  1917. uint16_t rom_device_id;
  1918. uint16_t rom_magic;
  1919. uint16_t pcir_offset;
  1920. uint8_t checksum;
  1921. /* Words in rom data are little endian (like in PCI configuration),
  1922. so they can be read / written with pci_get_word / pci_set_word. */
  1923. /* Only a valid rom will be patched. */
  1924. rom_magic = pci_get_word(ptr);
  1925. if (rom_magic != 0xaa55) {
  1926. PCI_DPRINTF("Bad ROM magic %04x\n", rom_magic);
  1927. return;
  1928. }
  1929. pcir_offset = pci_get_word(ptr + 0x18);
  1930. if (pcir_offset + 8 >= size || memcmp(ptr + pcir_offset, "PCIR", 4)) {
  1931. PCI_DPRINTF("Bad PCIR offset 0x%x or signature\n", pcir_offset);
  1932. return;
  1933. }
  1934. vendor_id = pci_get_word(pdev->config + PCI_VENDOR_ID);
  1935. device_id = pci_get_word(pdev->config + PCI_DEVICE_ID);
  1936. rom_vendor_id = pci_get_word(ptr + pcir_offset + 4);
  1937. rom_device_id = pci_get_word(ptr + pcir_offset + 6);
  1938. PCI_DPRINTF("%s: ROM id %04x%04x / PCI id %04x%04x\n", pdev->romfile,
  1939. vendor_id, device_id, rom_vendor_id, rom_device_id);
  1940. checksum = ptr[6];
  1941. if (vendor_id != rom_vendor_id) {
  1942. /* Patch vendor id and checksum (at offset 6 for etherboot roms). */
  1943. checksum += (uint8_t)rom_vendor_id + (uint8_t)(rom_vendor_id >> 8);
  1944. checksum -= (uint8_t)vendor_id + (uint8_t)(vendor_id >> 8);
  1945. PCI_DPRINTF("ROM checksum %02x / %02x\n", ptr[6], checksum);
  1946. ptr[6] = checksum;
  1947. pci_set_word(ptr + pcir_offset + 4, vendor_id);
  1948. }
  1949. if (device_id != rom_device_id) {
  1950. /* Patch device id and checksum (at offset 6 for etherboot roms). */
  1951. checksum += (uint8_t)rom_device_id + (uint8_t)(rom_device_id >> 8);
  1952. checksum -= (uint8_t)device_id + (uint8_t)(device_id >> 8);
  1953. PCI_DPRINTF("ROM checksum %02x / %02x\n", ptr[6], checksum);
  1954. ptr[6] = checksum;
  1955. pci_set_word(ptr + pcir_offset + 6, device_id);
  1956. }
  1957. }
  1958. /* Add an option rom for the device */
  1959. static void pci_add_option_rom(PCIDevice *pdev, bool is_default_rom,
  1960. Error **errp)
  1961. {
  1962. int size;
  1963. char *path;
  1964. void *ptr;
  1965. char name[32];
  1966. const VMStateDescription *vmsd;
  1967. if (!pdev->romfile)
  1968. return;
  1969. if (strlen(pdev->romfile) == 0)
  1970. return;
  1971. if (!pdev->rom_bar) {
  1972. /*
  1973. * Load rom via fw_cfg instead of creating a rom bar,
  1974. * for 0.11 compatibility.
  1975. */
  1976. int class = pci_get_word(pdev->config + PCI_CLASS_DEVICE);
  1977. /*
  1978. * Hot-plugged devices can't use the option ROM
  1979. * if the rom bar is disabled.
  1980. */
  1981. if (DEVICE(pdev)->hotplugged) {
  1982. error_setg(errp, "Hot-plugged device without ROM bar"
  1983. " can't have an option ROM");
  1984. return;
  1985. }
  1986. if (class == 0x0300) {
  1987. rom_add_vga(pdev->romfile);
  1988. } else {
  1989. rom_add_option(pdev->romfile, -1);
  1990. }
  1991. return;
  1992. }
  1993. path = qemu_find_file(QEMU_FILE_TYPE_BIOS, pdev->romfile);
  1994. if (path == NULL) {
  1995. path = g_strdup(pdev->romfile);
  1996. }
  1997. size = get_image_size(path);
  1998. if (size < 0) {
  1999. error_setg(errp, "failed to find romfile \"%s\"", pdev->romfile);
  2000. g_free(path);
  2001. return;
  2002. } else if (size == 0) {
  2003. error_setg(errp, "romfile \"%s\" is empty", pdev->romfile);
  2004. g_free(path);
  2005. return;
  2006. }
  2007. size = pow2ceil(size);
  2008. vmsd = qdev_get_vmsd(DEVICE(pdev));
  2009. if (vmsd) {
  2010. snprintf(name, sizeof(name), "%s.rom", vmsd->name);
  2011. } else {
  2012. snprintf(name, sizeof(name), "%s.rom", object_get_typename(OBJECT(pdev)));
  2013. }
  2014. pdev->has_rom = true;
  2015. memory_region_init_rom(&pdev->rom, OBJECT(pdev), name, size, &error_fatal);
  2016. ptr = memory_region_get_ram_ptr(&pdev->rom);
  2017. if (load_image_size(path, ptr, size) < 0) {
  2018. error_setg(errp, "failed to load romfile \"%s\"", pdev->romfile);
  2019. g_free(path);
  2020. return;
  2021. }
  2022. g_free(path);
  2023. if (is_default_rom) {
  2024. /* Only the default rom images will be patched (if needed). */
  2025. pci_patch_ids(pdev, ptr, size);
  2026. }
  2027. pci_register_bar(pdev, PCI_ROM_SLOT, 0, &pdev->rom);
  2028. }
  2029. static void pci_del_option_rom(PCIDevice *pdev)
  2030. {
  2031. if (!pdev->has_rom)
  2032. return;
  2033. vmstate_unregister_ram(&pdev->rom, &pdev->qdev);
  2034. pdev->has_rom = false;
  2035. }
  2036. /*
  2037. * On success, pci_add_capability() returns a positive value
  2038. * that the offset of the pci capability.
  2039. * On failure, it sets an error and returns a negative error
  2040. * code.
  2041. */
  2042. int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,
  2043. uint8_t offset, uint8_t size,
  2044. Error **errp)
  2045. {
  2046. uint8_t *config;
  2047. int i, overlapping_cap;
  2048. if (!offset) {
  2049. offset = pci_find_space(pdev, size);
  2050. /* out of PCI config space is programming error */
  2051. assert(offset);
  2052. } else {
  2053. /* Verify that capabilities don't overlap. Note: device assignment
  2054. * depends on this check to verify that the device is not broken.
  2055. * Should never trigger for emulated devices, but it's helpful
  2056. * for debugging these. */
  2057. for (i = offset; i < offset + size; i++) {
  2058. overlapping_cap = pci_find_capability_at_offset(pdev, i);
  2059. if (overlapping_cap) {
  2060. error_setg(errp, "%s:%02x:%02x.%x "
  2061. "Attempt to add PCI capability %x at offset "
  2062. "%x overlaps existing capability %x at offset %x",
  2063. pci_root_bus_path(pdev), pci_dev_bus_num(pdev),
  2064. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
  2065. cap_id, offset, overlapping_cap, i);
  2066. return -EINVAL;
  2067. }
  2068. }
  2069. }
  2070. config = pdev->config + offset;
  2071. config[PCI_CAP_LIST_ID] = cap_id;
  2072. config[PCI_CAP_LIST_NEXT] = pdev->config[PCI_CAPABILITY_LIST];
  2073. pdev->config[PCI_CAPABILITY_LIST] = offset;
  2074. pdev->config[PCI_STATUS] |= PCI_STATUS_CAP_LIST;
  2075. memset(pdev->used + offset, 0xFF, QEMU_ALIGN_UP(size, 4));
  2076. /* Make capability read-only by default */
  2077. memset(pdev->wmask + offset, 0, size);
  2078. /* Check capability by default */
  2079. memset(pdev->cmask + offset, 0xFF, size);
  2080. return offset;
  2081. }
  2082. /* Unlink capability from the pci config space. */
  2083. void pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
  2084. {
  2085. uint8_t prev, offset = pci_find_capability_list(pdev, cap_id, &prev);
  2086. if (!offset)
  2087. return;
  2088. pdev->config[prev] = pdev->config[offset + PCI_CAP_LIST_NEXT];
  2089. /* Make capability writable again */
  2090. memset(pdev->wmask + offset, 0xff, size);
  2091. memset(pdev->w1cmask + offset, 0, size);
  2092. /* Clear cmask as device-specific registers can't be checked */
  2093. memset(pdev->cmask + offset, 0, size);
  2094. memset(pdev->used + offset, 0, QEMU_ALIGN_UP(size, 4));
  2095. if (!pdev->config[PCI_CAPABILITY_LIST])
  2096. pdev->config[PCI_STATUS] &= ~PCI_STATUS_CAP_LIST;
  2097. }
  2098. uint8_t pci_find_capability(PCIDevice *pdev, uint8_t cap_id)
  2099. {
  2100. return pci_find_capability_list(pdev, cap_id, NULL);
  2101. }
  2102. static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent)
  2103. {
  2104. PCIDevice *d = (PCIDevice *)dev;
  2105. const pci_class_desc *desc;
  2106. char ctxt[64];
  2107. PCIIORegion *r;
  2108. int i, class;
  2109. class = pci_get_word(d->config + PCI_CLASS_DEVICE);
  2110. desc = pci_class_descriptions;
  2111. while (desc->desc && class != desc->class)
  2112. desc++;
  2113. if (desc->desc) {
  2114. snprintf(ctxt, sizeof(ctxt), "%s", desc->desc);
  2115. } else {
  2116. snprintf(ctxt, sizeof(ctxt), "Class %04x", class);
  2117. }
  2118. monitor_printf(mon, "%*sclass %s, addr %02x:%02x.%x, "
  2119. "pci id %04x:%04x (sub %04x:%04x)\n",
  2120. indent, "", ctxt, pci_dev_bus_num(d),
  2121. PCI_SLOT(d->devfn), PCI_FUNC(d->devfn),
  2122. pci_get_word(d->config + PCI_VENDOR_ID),
  2123. pci_get_word(d->config + PCI_DEVICE_ID),
  2124. pci_get_word(d->config + PCI_SUBSYSTEM_VENDOR_ID),
  2125. pci_get_word(d->config + PCI_SUBSYSTEM_ID));
  2126. for (i = 0; i < PCI_NUM_REGIONS; i++) {
  2127. r = &d->io_regions[i];
  2128. if (!r->size)
  2129. continue;
  2130. monitor_printf(mon, "%*sbar %d: %s at 0x%"FMT_PCIBUS
  2131. " [0x%"FMT_PCIBUS"]\n",
  2132. indent, "",
  2133. i, r->type & PCI_BASE_ADDRESS_SPACE_IO ? "i/o" : "mem",
  2134. r->addr, r->addr + r->size - 1);
  2135. }
  2136. }
  2137. static char *pci_dev_fw_name(DeviceState *dev, char *buf, int len)
  2138. {
  2139. PCIDevice *d = (PCIDevice *)dev;
  2140. const char *name = NULL;
  2141. const pci_class_desc *desc = pci_class_descriptions;
  2142. int class = pci_get_word(d->config + PCI_CLASS_DEVICE);
  2143. while (desc->desc &&
  2144. (class & ~desc->fw_ign_bits) !=
  2145. (desc->class & ~desc->fw_ign_bits)) {
  2146. desc++;
  2147. }
  2148. if (desc->desc) {
  2149. name = desc->fw_name;
  2150. }
  2151. if (name) {
  2152. pstrcpy(buf, len, name);
  2153. } else {
  2154. snprintf(buf, len, "pci%04x,%04x",
  2155. pci_get_word(d->config + PCI_VENDOR_ID),
  2156. pci_get_word(d->config + PCI_DEVICE_ID));
  2157. }
  2158. return buf;
  2159. }
  2160. static char *pcibus_get_fw_dev_path(DeviceState *dev)
  2161. {
  2162. PCIDevice *d = (PCIDevice *)dev;
  2163. char path[50], name[33];
  2164. int off;
  2165. off = snprintf(path, sizeof(path), "%s@%x",
  2166. pci_dev_fw_name(dev, name, sizeof name),
  2167. PCI_SLOT(d->devfn));
  2168. if (PCI_FUNC(d->devfn))
  2169. snprintf(path + off, sizeof(path) + off, ",%x", PCI_FUNC(d->devfn));
  2170. return g_strdup(path);
  2171. }
  2172. static char *pcibus_get_dev_path(DeviceState *dev)
  2173. {
  2174. PCIDevice *d = container_of(dev, PCIDevice, qdev);
  2175. PCIDevice *t;
  2176. int slot_depth;
  2177. /* Path format: Domain:00:Slot.Function:Slot.Function....:Slot.Function.
  2178. * 00 is added here to make this format compatible with
  2179. * domain:Bus:Slot.Func for systems without nested PCI bridges.
  2180. * Slot.Function list specifies the slot and function numbers for all
  2181. * devices on the path from root to the specific device. */
  2182. const char *root_bus_path;
  2183. int root_bus_len;
  2184. char slot[] = ":SS.F";
  2185. int slot_len = sizeof slot - 1 /* For '\0' */;
  2186. int path_len;
  2187. char *path, *p;
  2188. int s;
  2189. root_bus_path = pci_root_bus_path(d);
  2190. root_bus_len = strlen(root_bus_path);
  2191. /* Calculate # of slots on path between device and root. */;
  2192. slot_depth = 0;
  2193. for (t = d; t; t = pci_get_bus(t)->parent_dev) {
  2194. ++slot_depth;
  2195. }
  2196. path_len = root_bus_len + slot_len * slot_depth;
  2197. /* Allocate memory, fill in the terminating null byte. */
  2198. path = g_malloc(path_len + 1 /* For '\0' */);
  2199. path[path_len] = '\0';
  2200. memcpy(path, root_bus_path, root_bus_len);
  2201. /* Fill in slot numbers. We walk up from device to root, so need to print
  2202. * them in the reverse order, last to first. */
  2203. p = path + path_len;
  2204. for (t = d; t; t = pci_get_bus(t)->parent_dev) {
  2205. p -= slot_len;
  2206. s = snprintf(slot, sizeof slot, ":%02x.%x",
  2207. PCI_SLOT(t->devfn), PCI_FUNC(t->devfn));
  2208. assert(s == slot_len);
  2209. memcpy(p, slot, slot_len);
  2210. }
  2211. return path;
  2212. }
  2213. static int pci_qdev_find_recursive(PCIBus *bus,
  2214. const char *id, PCIDevice **pdev)
  2215. {
  2216. DeviceState *qdev = qdev_find_recursive(&bus->qbus, id);
  2217. if (!qdev) {
  2218. return -ENODEV;
  2219. }
  2220. /* roughly check if given qdev is pci device */
  2221. if (object_dynamic_cast(OBJECT(qdev), TYPE_PCI_DEVICE)) {
  2222. *pdev = PCI_DEVICE(qdev);
  2223. return 0;
  2224. }
  2225. return -EINVAL;
  2226. }
  2227. int pci_qdev_find_device(const char *id, PCIDevice **pdev)
  2228. {
  2229. PCIHostState *host_bridge;
  2230. int rc = -ENODEV;
  2231. QLIST_FOREACH(host_bridge, &pci_host_bridges, next) {
  2232. int tmp = pci_qdev_find_recursive(host_bridge->bus, id, pdev);
  2233. if (!tmp) {
  2234. rc = 0;
  2235. break;
  2236. }
  2237. if (tmp != -ENODEV) {
  2238. rc = tmp;
  2239. }
  2240. }
  2241. return rc;
  2242. }
  2243. MemoryRegion *pci_address_space(PCIDevice *dev)
  2244. {
  2245. return pci_get_bus(dev)->address_space_mem;
  2246. }
  2247. MemoryRegion *pci_address_space_io(PCIDevice *dev)
  2248. {
  2249. return pci_get_bus(dev)->address_space_io;
  2250. }
  2251. static void pci_device_class_init(ObjectClass *klass, void *data)
  2252. {
  2253. DeviceClass *k = DEVICE_CLASS(klass);
  2254. k->realize = pci_qdev_realize;
  2255. k->unrealize = pci_qdev_unrealize;
  2256. k->bus_type = TYPE_PCI_BUS;
  2257. k->props = pci_props;
  2258. }
  2259. static void pci_device_class_base_init(ObjectClass *klass, void *data)
  2260. {
  2261. if (!object_class_is_abstract(klass)) {
  2262. ObjectClass *conventional =
  2263. object_class_dynamic_cast(klass, INTERFACE_CONVENTIONAL_PCI_DEVICE);
  2264. ObjectClass *pcie =
  2265. object_class_dynamic_cast(klass, INTERFACE_PCIE_DEVICE);
  2266. assert(conventional || pcie);
  2267. }
  2268. }
  2269. AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)
  2270. {
  2271. PCIBus *bus = pci_get_bus(dev);
  2272. PCIBus *iommu_bus = bus;
  2273. uint8_t devfn = dev->devfn;
  2274. while (iommu_bus && !iommu_bus->iommu_fn && iommu_bus->parent_dev) {
  2275. PCIBus *parent_bus = pci_get_bus(iommu_bus->parent_dev);
  2276. /*
  2277. * The requester ID of the provided device may be aliased, as seen from
  2278. * the IOMMU, due to topology limitations. The IOMMU relies on a
  2279. * requester ID to provide a unique AddressSpace for devices, but
  2280. * conventional PCI buses pre-date such concepts. Instead, the PCIe-
  2281. * to-PCI bridge creates and accepts transactions on behalf of down-
  2282. * stream devices. When doing so, all downstream devices are masked
  2283. * (aliased) behind a single requester ID. The requester ID used
  2284. * depends on the format of the bridge devices. Proper PCIe-to-PCI
  2285. * bridges, with a PCIe capability indicating such, follow the
  2286. * guidelines of chapter 2.3 of the PCIe-to-PCI/X bridge specification,
  2287. * where the bridge uses the seconary bus as the bridge portion of the
  2288. * requester ID and devfn of 00.0. For other bridges, typically those
  2289. * found on the root complex such as the dmi-to-pci-bridge, we follow
  2290. * the convention of typical bare-metal hardware, which uses the
  2291. * requester ID of the bridge itself. There are device specific
  2292. * exceptions to these rules, but these are the defaults that the
  2293. * Linux kernel uses when determining DMA aliases itself and believed
  2294. * to be true for the bare metal equivalents of the devices emulated
  2295. * in QEMU.
  2296. */
  2297. if (!pci_bus_is_express(iommu_bus)) {
  2298. PCIDevice *parent = iommu_bus->parent_dev;
  2299. if (pci_is_express(parent) &&
  2300. pcie_cap_get_type(parent) == PCI_EXP_TYPE_PCI_BRIDGE) {
  2301. devfn = PCI_DEVFN(0, 0);
  2302. bus = iommu_bus;
  2303. } else {
  2304. devfn = parent->devfn;
  2305. bus = parent_bus;
  2306. }
  2307. }
  2308. iommu_bus = parent_bus;
  2309. }
  2310. if (iommu_bus && iommu_bus->iommu_fn) {
  2311. return iommu_bus->iommu_fn(bus, iommu_bus->iommu_opaque, devfn);
  2312. }
  2313. return &address_space_memory;
  2314. }
  2315. void pci_setup_iommu(PCIBus *bus, PCIIOMMUFunc fn, void *opaque)
  2316. {
  2317. bus->iommu_fn = fn;
  2318. bus->iommu_opaque = opaque;
  2319. }
  2320. static void pci_dev_get_w64(PCIBus *b, PCIDevice *dev, void *opaque)
  2321. {
  2322. Range *range = opaque;
  2323. PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
  2324. uint16_t cmd = pci_get_word(dev->config + PCI_COMMAND);
  2325. int i;
  2326. if (!(cmd & PCI_COMMAND_MEMORY)) {
  2327. return;
  2328. }
  2329. if (pc->is_bridge) {
  2330. pcibus_t base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
  2331. pcibus_t limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
  2332. base = MAX(base, 0x1ULL << 32);
  2333. if (limit >= base) {
  2334. Range pref_range;
  2335. range_set_bounds(&pref_range, base, limit);
  2336. range_extend(range, &pref_range);
  2337. }
  2338. }
  2339. for (i = 0; i < PCI_NUM_REGIONS; ++i) {
  2340. PCIIORegion *r = &dev->io_regions[i];
  2341. pcibus_t lob, upb;
  2342. Range region_range;
  2343. if (!r->size ||
  2344. (r->type & PCI_BASE_ADDRESS_SPACE_IO) ||
  2345. !(r->type & PCI_BASE_ADDRESS_MEM_TYPE_64)) {
  2346. continue;
  2347. }
  2348. lob = pci_bar_address(dev, i, r->type, r->size);
  2349. upb = lob + r->size - 1;
  2350. if (lob == PCI_BAR_UNMAPPED) {
  2351. continue;
  2352. }
  2353. lob = MAX(lob, 0x1ULL << 32);
  2354. if (upb >= lob) {
  2355. range_set_bounds(&region_range, lob, upb);
  2356. range_extend(range, &region_range);
  2357. }
  2358. }
  2359. }
  2360. void pci_bus_get_w64_range(PCIBus *bus, Range *range)
  2361. {
  2362. range_make_empty(range);
  2363. pci_for_each_device_under_bus(bus, pci_dev_get_w64, range);
  2364. }
  2365. static bool pcie_has_upstream_port(PCIDevice *dev)
  2366. {
  2367. PCIDevice *parent_dev = pci_bridge_get_device(pci_get_bus(dev));
  2368. /* Device associated with an upstream port.
  2369. * As there are several types of these, it's easier to check the
  2370. * parent device: upstream ports are always connected to
  2371. * root or downstream ports.
  2372. */
  2373. return parent_dev &&
  2374. pci_is_express(parent_dev) &&
  2375. parent_dev->exp.exp_cap &&
  2376. (pcie_cap_get_type(parent_dev) == PCI_EXP_TYPE_ROOT_PORT ||
  2377. pcie_cap_get_type(parent_dev) == PCI_EXP_TYPE_DOWNSTREAM);
  2378. }
  2379. PCIDevice *pci_get_function_0(PCIDevice *pci_dev)
  2380. {
  2381. PCIBus *bus = pci_get_bus(pci_dev);
  2382. if(pcie_has_upstream_port(pci_dev)) {
  2383. /* With an upstream PCIe port, we only support 1 device at slot 0 */
  2384. return bus->devices[0];
  2385. } else {
  2386. /* Other bus types might support multiple devices at slots 0-31 */
  2387. return bus->devices[PCI_DEVFN(PCI_SLOT(pci_dev->devfn), 0)];
  2388. }
  2389. }
  2390. MSIMessage pci_get_msi_message(PCIDevice *dev, int vector)
  2391. {
  2392. MSIMessage msg;
  2393. if (msix_enabled(dev)) {
  2394. msg = msix_get_message(dev, vector);
  2395. } else if (msi_enabled(dev)) {
  2396. msg = msi_get_message(dev, vector);
  2397. } else {
  2398. /* Should never happen */
  2399. error_report("%s: unknown interrupt type", __func__);
  2400. abort();
  2401. }
  2402. return msg;
  2403. }
  2404. static const TypeInfo pci_device_type_info = {
  2405. .name = TYPE_PCI_DEVICE,
  2406. .parent = TYPE_DEVICE,
  2407. .instance_size = sizeof(PCIDevice),
  2408. .abstract = true,
  2409. .class_size = sizeof(PCIDeviceClass),
  2410. .class_init = pci_device_class_init,
  2411. .class_base_init = pci_device_class_base_init,
  2412. };
  2413. static void pci_register_types(void)
  2414. {
  2415. type_register_static(&pci_bus_info);
  2416. type_register_static(&pcie_bus_info);
  2417. type_register_static(&conventional_pci_interface_info);
  2418. type_register_static(&pcie_interface_info);
  2419. type_register_static(&pci_device_type_info);
  2420. }
  2421. type_init(pci_register_types)