pci.c 87 KB

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