pci.c 87 KB

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