pci.c 90 KB

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