pci.c 86 KB

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