pci.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355
  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 "hw/hw.h"
  25. #include "hw/pci/pci.h"
  26. #include "hw/pci/pci_bridge.h"
  27. #include "hw/pci/pci_bus.h"
  28. #include "hw/pci/pci_host.h"
  29. #include "monitor/monitor.h"
  30. #include "net/net.h"
  31. #include "sysemu/sysemu.h"
  32. #include "hw/loader.h"
  33. #include "qemu/range.h"
  34. #include "qmp-commands.h"
  35. #include "hw/pci/msi.h"
  36. #include "hw/pci/msix.h"
  37. #include "exec/address-spaces.h"
  38. #include "hw/hotplug.h"
  39. //#define DEBUG_PCI
  40. #ifdef DEBUG_PCI
  41. # define PCI_DPRINTF(format, ...) printf(format, ## __VA_ARGS__)
  42. #else
  43. # define PCI_DPRINTF(format, ...) do { } while (0)
  44. #endif
  45. static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent);
  46. static char *pcibus_get_dev_path(DeviceState *dev);
  47. static char *pcibus_get_fw_dev_path(DeviceState *dev);
  48. static void pcibus_reset(BusState *qbus);
  49. static Property pci_props[] = {
  50. DEFINE_PROP_PCI_DEVFN("addr", PCIDevice, devfn, -1),
  51. DEFINE_PROP_STRING("romfile", PCIDevice, romfile),
  52. DEFINE_PROP_UINT32("rombar", PCIDevice, rom_bar, 1),
  53. DEFINE_PROP_BIT("multifunction", PCIDevice, cap_present,
  54. QEMU_PCI_CAP_MULTIFUNCTION_BITNR, false),
  55. DEFINE_PROP_BIT("command_serr_enable", PCIDevice, cap_present,
  56. QEMU_PCI_CAP_SERR_BITNR, true),
  57. DEFINE_PROP_END_OF_LIST()
  58. };
  59. static const VMStateDescription vmstate_pcibus = {
  60. .name = "PCIBUS",
  61. .version_id = 1,
  62. .minimum_version_id = 1,
  63. .minimum_version_id_old = 1,
  64. .fields = (VMStateField[]) {
  65. VMSTATE_INT32_EQUAL(nirq, PCIBus),
  66. VMSTATE_VARRAY_INT32(irq_count, PCIBus,
  67. nirq, 0, vmstate_info_int32,
  68. int32_t),
  69. VMSTATE_END_OF_LIST()
  70. }
  71. };
  72. static void pci_bus_realize(BusState *qbus, Error **errp)
  73. {
  74. PCIBus *bus = PCI_BUS(qbus);
  75. vmstate_register(NULL, -1, &vmstate_pcibus, bus);
  76. }
  77. static void pci_bus_unrealize(BusState *qbus, Error **errp)
  78. {
  79. PCIBus *bus = PCI_BUS(qbus);
  80. vmstate_unregister(NULL, &vmstate_pcibus, bus);
  81. }
  82. static void pci_bus_class_init(ObjectClass *klass, void *data)
  83. {
  84. BusClass *k = BUS_CLASS(klass);
  85. k->print_dev = pcibus_dev_print;
  86. k->get_dev_path = pcibus_get_dev_path;
  87. k->get_fw_dev_path = pcibus_get_fw_dev_path;
  88. k->realize = pci_bus_realize;
  89. k->unrealize = pci_bus_unrealize;
  90. k->reset = pcibus_reset;
  91. }
  92. static const TypeInfo pci_bus_info = {
  93. .name = TYPE_PCI_BUS,
  94. .parent = TYPE_BUS,
  95. .instance_size = sizeof(PCIBus),
  96. .class_init = pci_bus_class_init,
  97. };
  98. static const TypeInfo pcie_bus_info = {
  99. .name = TYPE_PCIE_BUS,
  100. .parent = TYPE_PCI_BUS,
  101. };
  102. static PCIBus *pci_find_bus_nr(PCIBus *bus, int bus_num);
  103. static void pci_update_mappings(PCIDevice *d);
  104. static void pci_irq_handler(void *opaque, int irq_num, int level);
  105. static int pci_add_option_rom(PCIDevice *pdev, bool is_default_rom);
  106. static void pci_del_option_rom(PCIDevice *pdev);
  107. static uint16_t pci_default_sub_vendor_id = PCI_SUBVENDOR_ID_REDHAT_QUMRANET;
  108. static uint16_t pci_default_sub_device_id = PCI_SUBDEVICE_ID_QEMU;
  109. static QLIST_HEAD(, PCIHostState) pci_host_bridges;
  110. static int pci_bar(PCIDevice *d, int reg)
  111. {
  112. uint8_t type;
  113. if (reg != PCI_ROM_SLOT)
  114. return PCI_BASE_ADDRESS_0 + reg * 4;
  115. type = d->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
  116. return type == PCI_HEADER_TYPE_BRIDGE ? PCI_ROM_ADDRESS1 : PCI_ROM_ADDRESS;
  117. }
  118. static inline int pci_irq_state(PCIDevice *d, int irq_num)
  119. {
  120. return (d->irq_state >> irq_num) & 0x1;
  121. }
  122. static inline void pci_set_irq_state(PCIDevice *d, int irq_num, int level)
  123. {
  124. d->irq_state &= ~(0x1 << irq_num);
  125. d->irq_state |= level << irq_num;
  126. }
  127. static void pci_change_irq_level(PCIDevice *pci_dev, int irq_num, int change)
  128. {
  129. PCIBus *bus;
  130. for (;;) {
  131. bus = pci_dev->bus;
  132. irq_num = bus->map_irq(pci_dev, irq_num);
  133. if (bus->set_irq)
  134. break;
  135. pci_dev = bus->parent_dev;
  136. }
  137. bus->irq_count[irq_num] += change;
  138. bus->set_irq(bus->irq_opaque, irq_num, bus->irq_count[irq_num] != 0);
  139. }
  140. int pci_bus_get_irq_level(PCIBus *bus, int irq_num)
  141. {
  142. assert(irq_num >= 0);
  143. assert(irq_num < bus->nirq);
  144. return !!bus->irq_count[irq_num];
  145. }
  146. /* Update interrupt status bit in config space on interrupt
  147. * state change. */
  148. static void pci_update_irq_status(PCIDevice *dev)
  149. {
  150. if (dev->irq_state) {
  151. dev->config[PCI_STATUS] |= PCI_STATUS_INTERRUPT;
  152. } else {
  153. dev->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT;
  154. }
  155. }
  156. void pci_device_deassert_intx(PCIDevice *dev)
  157. {
  158. int i;
  159. for (i = 0; i < PCI_NUM_PINS; ++i) {
  160. pci_irq_handler(dev, i, 0);
  161. }
  162. }
  163. static void pci_do_device_reset(PCIDevice *dev)
  164. {
  165. int r;
  166. pci_device_deassert_intx(dev);
  167. assert(dev->irq_state == 0);
  168. /* Clear all writable bits */
  169. pci_word_test_and_clear_mask(dev->config + PCI_COMMAND,
  170. pci_get_word(dev->wmask + PCI_COMMAND) |
  171. pci_get_word(dev->w1cmask + PCI_COMMAND));
  172. pci_word_test_and_clear_mask(dev->config + PCI_STATUS,
  173. pci_get_word(dev->wmask + PCI_STATUS) |
  174. pci_get_word(dev->w1cmask + PCI_STATUS));
  175. dev->config[PCI_CACHE_LINE_SIZE] = 0x0;
  176. dev->config[PCI_INTERRUPT_LINE] = 0x0;
  177. for (r = 0; r < PCI_NUM_REGIONS; ++r) {
  178. PCIIORegion *region = &dev->io_regions[r];
  179. if (!region->size) {
  180. continue;
  181. }
  182. if (!(region->type & PCI_BASE_ADDRESS_SPACE_IO) &&
  183. region->type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
  184. pci_set_quad(dev->config + pci_bar(dev, r), region->type);
  185. } else {
  186. pci_set_long(dev->config + pci_bar(dev, r), region->type);
  187. }
  188. }
  189. pci_update_mappings(dev);
  190. msi_reset(dev);
  191. msix_reset(dev);
  192. }
  193. /*
  194. * This function is called on #RST and FLR.
  195. * FLR if PCI_EXP_DEVCTL_BCR_FLR is set
  196. */
  197. void pci_device_reset(PCIDevice *dev)
  198. {
  199. qdev_reset_all(&dev->qdev);
  200. pci_do_device_reset(dev);
  201. }
  202. /*
  203. * Trigger pci bus reset under a given bus.
  204. * Called via qbus_reset_all on RST# assert, after the devices
  205. * have been reset qdev_reset_all-ed already.
  206. */
  207. static void pcibus_reset(BusState *qbus)
  208. {
  209. PCIBus *bus = DO_UPCAST(PCIBus, qbus, qbus);
  210. int i;
  211. for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
  212. if (bus->devices[i]) {
  213. pci_do_device_reset(bus->devices[i]);
  214. }
  215. }
  216. for (i = 0; i < bus->nirq; i++) {
  217. assert(bus->irq_count[i] == 0);
  218. }
  219. }
  220. static void pci_host_bus_register(PCIBus *bus, DeviceState *parent)
  221. {
  222. PCIHostState *host_bridge = PCI_HOST_BRIDGE(parent);
  223. QLIST_INSERT_HEAD(&pci_host_bridges, host_bridge, next);
  224. }
  225. PCIBus *pci_find_primary_bus(void)
  226. {
  227. PCIBus *primary_bus = NULL;
  228. PCIHostState *host;
  229. QLIST_FOREACH(host, &pci_host_bridges, next) {
  230. if (primary_bus) {
  231. /* We have multiple root buses, refuse to select a primary */
  232. return NULL;
  233. }
  234. primary_bus = host->bus;
  235. }
  236. return primary_bus;
  237. }
  238. PCIBus *pci_device_root_bus(const PCIDevice *d)
  239. {
  240. PCIBus *bus = d->bus;
  241. while ((d = bus->parent_dev) != NULL) {
  242. bus = d->bus;
  243. }
  244. return bus;
  245. }
  246. const char *pci_root_bus_path(PCIDevice *dev)
  247. {
  248. PCIBus *rootbus = pci_device_root_bus(dev);
  249. PCIHostState *host_bridge = PCI_HOST_BRIDGE(rootbus->qbus.parent);
  250. PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_GET_CLASS(host_bridge);
  251. assert(!rootbus->parent_dev);
  252. assert(host_bridge->bus == rootbus);
  253. if (hc->root_bus_path) {
  254. return (*hc->root_bus_path)(host_bridge, rootbus);
  255. }
  256. return rootbus->qbus.name;
  257. }
  258. static void pci_bus_init(PCIBus *bus, DeviceState *parent,
  259. const char *name,
  260. MemoryRegion *address_space_mem,
  261. MemoryRegion *address_space_io,
  262. uint8_t devfn_min)
  263. {
  264. assert(PCI_FUNC(devfn_min) == 0);
  265. bus->devfn_min = devfn_min;
  266. bus->address_space_mem = address_space_mem;
  267. bus->address_space_io = address_space_io;
  268. /* host bridge */
  269. QLIST_INIT(&bus->child);
  270. pci_host_bus_register(bus, parent);
  271. }
  272. bool pci_bus_is_express(PCIBus *bus)
  273. {
  274. return object_dynamic_cast(OBJECT(bus), TYPE_PCIE_BUS);
  275. }
  276. bool pci_bus_is_root(PCIBus *bus)
  277. {
  278. return !bus->parent_dev;
  279. }
  280. void pci_bus_new_inplace(PCIBus *bus, size_t bus_size, DeviceState *parent,
  281. const char *name,
  282. MemoryRegion *address_space_mem,
  283. MemoryRegion *address_space_io,
  284. uint8_t devfn_min, const char *typename)
  285. {
  286. qbus_create_inplace(bus, bus_size, typename, parent, name);
  287. pci_bus_init(bus, parent, name, address_space_mem,
  288. address_space_io, devfn_min);
  289. }
  290. PCIBus *pci_bus_new(DeviceState *parent, const char *name,
  291. MemoryRegion *address_space_mem,
  292. MemoryRegion *address_space_io,
  293. uint8_t devfn_min, const char *typename)
  294. {
  295. PCIBus *bus;
  296. bus = PCI_BUS(qbus_create(typename, parent, name));
  297. pci_bus_init(bus, parent, name, address_space_mem,
  298. address_space_io, devfn_min);
  299. return bus;
  300. }
  301. void pci_bus_irqs(PCIBus *bus, pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
  302. void *irq_opaque, int nirq)
  303. {
  304. bus->set_irq = set_irq;
  305. bus->map_irq = map_irq;
  306. bus->irq_opaque = irq_opaque;
  307. bus->nirq = nirq;
  308. bus->irq_count = g_malloc0(nirq * sizeof(bus->irq_count[0]));
  309. }
  310. PCIBus *pci_register_bus(DeviceState *parent, const char *name,
  311. pci_set_irq_fn set_irq, pci_map_irq_fn map_irq,
  312. void *irq_opaque,
  313. MemoryRegion *address_space_mem,
  314. MemoryRegion *address_space_io,
  315. uint8_t devfn_min, int nirq, const char *typename)
  316. {
  317. PCIBus *bus;
  318. bus = pci_bus_new(parent, name, address_space_mem,
  319. address_space_io, devfn_min, typename);
  320. pci_bus_irqs(bus, set_irq, map_irq, irq_opaque, nirq);
  321. return bus;
  322. }
  323. int pci_bus_num(PCIBus *s)
  324. {
  325. if (pci_bus_is_root(s))
  326. return 0; /* pci host bridge */
  327. return s->parent_dev->config[PCI_SECONDARY_BUS];
  328. }
  329. static int get_pci_config_device(QEMUFile *f, void *pv, size_t size)
  330. {
  331. PCIDevice *s = container_of(pv, PCIDevice, config);
  332. PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(s);
  333. uint8_t *config;
  334. int i;
  335. assert(size == pci_config_size(s));
  336. config = g_malloc(size);
  337. qemu_get_buffer(f, config, size);
  338. for (i = 0; i < size; ++i) {
  339. if ((config[i] ^ s->config[i]) &
  340. s->cmask[i] & ~s->wmask[i] & ~s->w1cmask[i]) {
  341. g_free(config);
  342. return -EINVAL;
  343. }
  344. }
  345. memcpy(s->config, config, size);
  346. pci_update_mappings(s);
  347. if (pc->is_bridge) {
  348. PCIBridge *b = PCI_BRIDGE(s);
  349. pci_bridge_update_mappings(b);
  350. }
  351. memory_region_set_enabled(&s->bus_master_enable_region,
  352. pci_get_word(s->config + PCI_COMMAND)
  353. & PCI_COMMAND_MASTER);
  354. g_free(config);
  355. return 0;
  356. }
  357. /* just put buffer */
  358. static void put_pci_config_device(QEMUFile *f, void *pv, size_t size)
  359. {
  360. const uint8_t **v = pv;
  361. assert(size == pci_config_size(container_of(pv, PCIDevice, config)));
  362. qemu_put_buffer(f, *v, size);
  363. }
  364. static VMStateInfo vmstate_info_pci_config = {
  365. .name = "pci config",
  366. .get = get_pci_config_device,
  367. .put = put_pci_config_device,
  368. };
  369. static int get_pci_irq_state(QEMUFile *f, void *pv, size_t size)
  370. {
  371. PCIDevice *s = container_of(pv, PCIDevice, irq_state);
  372. uint32_t irq_state[PCI_NUM_PINS];
  373. int i;
  374. for (i = 0; i < PCI_NUM_PINS; ++i) {
  375. irq_state[i] = qemu_get_be32(f);
  376. if (irq_state[i] != 0x1 && irq_state[i] != 0) {
  377. fprintf(stderr, "irq state %d: must be 0 or 1.\n",
  378. irq_state[i]);
  379. return -EINVAL;
  380. }
  381. }
  382. for (i = 0; i < PCI_NUM_PINS; ++i) {
  383. pci_set_irq_state(s, i, irq_state[i]);
  384. }
  385. return 0;
  386. }
  387. static void put_pci_irq_state(QEMUFile *f, void *pv, size_t size)
  388. {
  389. int i;
  390. PCIDevice *s = container_of(pv, PCIDevice, irq_state);
  391. for (i = 0; i < PCI_NUM_PINS; ++i) {
  392. qemu_put_be32(f, pci_irq_state(s, i));
  393. }
  394. }
  395. static VMStateInfo vmstate_info_pci_irq_state = {
  396. .name = "pci irq state",
  397. .get = get_pci_irq_state,
  398. .put = put_pci_irq_state,
  399. };
  400. const VMStateDescription vmstate_pci_device = {
  401. .name = "PCIDevice",
  402. .version_id = 2,
  403. .minimum_version_id = 1,
  404. .minimum_version_id_old = 1,
  405. .fields = (VMStateField []) {
  406. VMSTATE_INT32_LE(version_id, PCIDevice),
  407. VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
  408. vmstate_info_pci_config,
  409. PCI_CONFIG_SPACE_SIZE),
  410. VMSTATE_BUFFER_UNSAFE_INFO(irq_state, PCIDevice, 2,
  411. vmstate_info_pci_irq_state,
  412. PCI_NUM_PINS * sizeof(int32_t)),
  413. VMSTATE_END_OF_LIST()
  414. }
  415. };
  416. const VMStateDescription vmstate_pcie_device = {
  417. .name = "PCIEDevice",
  418. .version_id = 2,
  419. .minimum_version_id = 1,
  420. .minimum_version_id_old = 1,
  421. .fields = (VMStateField []) {
  422. VMSTATE_INT32_LE(version_id, PCIDevice),
  423. VMSTATE_BUFFER_UNSAFE_INFO(config, PCIDevice, 0,
  424. vmstate_info_pci_config,
  425. PCIE_CONFIG_SPACE_SIZE),
  426. VMSTATE_BUFFER_UNSAFE_INFO(irq_state, PCIDevice, 2,
  427. vmstate_info_pci_irq_state,
  428. PCI_NUM_PINS * sizeof(int32_t)),
  429. VMSTATE_END_OF_LIST()
  430. }
  431. };
  432. static inline const VMStateDescription *pci_get_vmstate(PCIDevice *s)
  433. {
  434. return pci_is_express(s) ? &vmstate_pcie_device : &vmstate_pci_device;
  435. }
  436. void pci_device_save(PCIDevice *s, QEMUFile *f)
  437. {
  438. /* Clear interrupt status bit: it is implicit
  439. * in irq_state which we are saving.
  440. * This makes us compatible with old devices
  441. * which never set or clear this bit. */
  442. s->config[PCI_STATUS] &= ~PCI_STATUS_INTERRUPT;
  443. vmstate_save_state(f, pci_get_vmstate(s), s);
  444. /* Restore the interrupt status bit. */
  445. pci_update_irq_status(s);
  446. }
  447. int pci_device_load(PCIDevice *s, QEMUFile *f)
  448. {
  449. int ret;
  450. ret = vmstate_load_state(f, pci_get_vmstate(s), s, s->version_id);
  451. /* Restore the interrupt status bit. */
  452. pci_update_irq_status(s);
  453. return ret;
  454. }
  455. static void pci_set_default_subsystem_id(PCIDevice *pci_dev)
  456. {
  457. pci_set_word(pci_dev->config + PCI_SUBSYSTEM_VENDOR_ID,
  458. pci_default_sub_vendor_id);
  459. pci_set_word(pci_dev->config + PCI_SUBSYSTEM_ID,
  460. pci_default_sub_device_id);
  461. }
  462. /*
  463. * Parse [[<domain>:]<bus>:]<slot>, return -1 on error if funcp == NULL
  464. * [[<domain>:]<bus>:]<slot>.<func>, return -1 on error
  465. */
  466. int pci_parse_devaddr(const char *addr, int *domp, int *busp,
  467. unsigned int *slotp, unsigned int *funcp)
  468. {
  469. const char *p;
  470. char *e;
  471. unsigned long val;
  472. unsigned long dom = 0, bus = 0;
  473. unsigned int slot = 0;
  474. unsigned int func = 0;
  475. p = addr;
  476. val = strtoul(p, &e, 16);
  477. if (e == p)
  478. return -1;
  479. if (*e == ':') {
  480. bus = val;
  481. p = e + 1;
  482. val = strtoul(p, &e, 16);
  483. if (e == p)
  484. return -1;
  485. if (*e == ':') {
  486. dom = bus;
  487. bus = val;
  488. p = e + 1;
  489. val = strtoul(p, &e, 16);
  490. if (e == p)
  491. return -1;
  492. }
  493. }
  494. slot = val;
  495. if (funcp != NULL) {
  496. if (*e != '.')
  497. return -1;
  498. p = e + 1;
  499. val = strtoul(p, &e, 16);
  500. if (e == p)
  501. return -1;
  502. func = val;
  503. }
  504. /* if funcp == NULL func is 0 */
  505. if (dom > 0xffff || bus > 0xff || slot > 0x1f || func > 7)
  506. return -1;
  507. if (*e)
  508. return -1;
  509. *domp = dom;
  510. *busp = bus;
  511. *slotp = slot;
  512. if (funcp != NULL)
  513. *funcp = func;
  514. return 0;
  515. }
  516. PCIBus *pci_get_bus_devfn(int *devfnp, PCIBus *root, const char *devaddr)
  517. {
  518. int dom, bus;
  519. unsigned slot;
  520. assert(!root->parent_dev);
  521. if (!root) {
  522. fprintf(stderr, "No primary PCI bus\n");
  523. return NULL;
  524. }
  525. if (!devaddr) {
  526. *devfnp = -1;
  527. return pci_find_bus_nr(root, 0);
  528. }
  529. if (pci_parse_devaddr(devaddr, &dom, &bus, &slot, NULL) < 0) {
  530. return NULL;
  531. }
  532. if (dom != 0) {
  533. fprintf(stderr, "No support for non-zero PCI domains\n");
  534. return NULL;
  535. }
  536. *devfnp = PCI_DEVFN(slot, 0);
  537. return pci_find_bus_nr(root, bus);
  538. }
  539. static void pci_init_cmask(PCIDevice *dev)
  540. {
  541. pci_set_word(dev->cmask + PCI_VENDOR_ID, 0xffff);
  542. pci_set_word(dev->cmask + PCI_DEVICE_ID, 0xffff);
  543. dev->cmask[PCI_STATUS] = PCI_STATUS_CAP_LIST;
  544. dev->cmask[PCI_REVISION_ID] = 0xff;
  545. dev->cmask[PCI_CLASS_PROG] = 0xff;
  546. pci_set_word(dev->cmask + PCI_CLASS_DEVICE, 0xffff);
  547. dev->cmask[PCI_HEADER_TYPE] = 0xff;
  548. dev->cmask[PCI_CAPABILITY_LIST] = 0xff;
  549. }
  550. static void pci_init_wmask(PCIDevice *dev)
  551. {
  552. int config_size = pci_config_size(dev);
  553. dev->wmask[PCI_CACHE_LINE_SIZE] = 0xff;
  554. dev->wmask[PCI_INTERRUPT_LINE] = 0xff;
  555. pci_set_word(dev->wmask + PCI_COMMAND,
  556. PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER |
  557. PCI_COMMAND_INTX_DISABLE);
  558. if (dev->cap_present & QEMU_PCI_CAP_SERR) {
  559. pci_word_test_and_set_mask(dev->wmask + PCI_COMMAND, PCI_COMMAND_SERR);
  560. }
  561. memset(dev->wmask + PCI_CONFIG_HEADER_SIZE, 0xff,
  562. config_size - PCI_CONFIG_HEADER_SIZE);
  563. }
  564. static void pci_init_w1cmask(PCIDevice *dev)
  565. {
  566. /*
  567. * Note: It's okay to set w1cmask even for readonly bits as
  568. * long as their value is hardwired to 0.
  569. */
  570. pci_set_word(dev->w1cmask + PCI_STATUS,
  571. PCI_STATUS_PARITY | PCI_STATUS_SIG_TARGET_ABORT |
  572. PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_REC_MASTER_ABORT |
  573. PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_DETECTED_PARITY);
  574. }
  575. static void pci_init_mask_bridge(PCIDevice *d)
  576. {
  577. /* PCI_PRIMARY_BUS, PCI_SECONDARY_BUS, PCI_SUBORDINATE_BUS and
  578. PCI_SEC_LETENCY_TIMER */
  579. memset(d->wmask + PCI_PRIMARY_BUS, 0xff, 4);
  580. /* base and limit */
  581. d->wmask[PCI_IO_BASE] = PCI_IO_RANGE_MASK & 0xff;
  582. d->wmask[PCI_IO_LIMIT] = PCI_IO_RANGE_MASK & 0xff;
  583. pci_set_word(d->wmask + PCI_MEMORY_BASE,
  584. PCI_MEMORY_RANGE_MASK & 0xffff);
  585. pci_set_word(d->wmask + PCI_MEMORY_LIMIT,
  586. PCI_MEMORY_RANGE_MASK & 0xffff);
  587. pci_set_word(d->wmask + PCI_PREF_MEMORY_BASE,
  588. PCI_PREF_RANGE_MASK & 0xffff);
  589. pci_set_word(d->wmask + PCI_PREF_MEMORY_LIMIT,
  590. PCI_PREF_RANGE_MASK & 0xffff);
  591. /* PCI_PREF_BASE_UPPER32 and PCI_PREF_LIMIT_UPPER32 */
  592. memset(d->wmask + PCI_PREF_BASE_UPPER32, 0xff, 8);
  593. /* Supported memory and i/o types */
  594. d->config[PCI_IO_BASE] |= PCI_IO_RANGE_TYPE_16;
  595. d->config[PCI_IO_LIMIT] |= PCI_IO_RANGE_TYPE_16;
  596. pci_word_test_and_set_mask(d->config + PCI_PREF_MEMORY_BASE,
  597. PCI_PREF_RANGE_TYPE_64);
  598. pci_word_test_and_set_mask(d->config + PCI_PREF_MEMORY_LIMIT,
  599. PCI_PREF_RANGE_TYPE_64);
  600. /*
  601. * TODO: Bridges default to 10-bit VGA decoding but we currently only
  602. * implement 16-bit decoding (no alias support).
  603. */
  604. pci_set_word(d->wmask + PCI_BRIDGE_CONTROL,
  605. PCI_BRIDGE_CTL_PARITY |
  606. PCI_BRIDGE_CTL_SERR |
  607. PCI_BRIDGE_CTL_ISA |
  608. PCI_BRIDGE_CTL_VGA |
  609. PCI_BRIDGE_CTL_VGA_16BIT |
  610. PCI_BRIDGE_CTL_MASTER_ABORT |
  611. PCI_BRIDGE_CTL_BUS_RESET |
  612. PCI_BRIDGE_CTL_FAST_BACK |
  613. PCI_BRIDGE_CTL_DISCARD |
  614. PCI_BRIDGE_CTL_SEC_DISCARD |
  615. PCI_BRIDGE_CTL_DISCARD_SERR);
  616. /* Below does not do anything as we never set this bit, put here for
  617. * completeness. */
  618. pci_set_word(d->w1cmask + PCI_BRIDGE_CONTROL,
  619. PCI_BRIDGE_CTL_DISCARD_STATUS);
  620. d->cmask[PCI_IO_BASE] |= PCI_IO_RANGE_TYPE_MASK;
  621. d->cmask[PCI_IO_LIMIT] |= PCI_IO_RANGE_TYPE_MASK;
  622. pci_word_test_and_set_mask(d->cmask + PCI_PREF_MEMORY_BASE,
  623. PCI_PREF_RANGE_TYPE_MASK);
  624. pci_word_test_and_set_mask(d->cmask + PCI_PREF_MEMORY_LIMIT,
  625. PCI_PREF_RANGE_TYPE_MASK);
  626. }
  627. static int pci_init_multifunction(PCIBus *bus, PCIDevice *dev)
  628. {
  629. uint8_t slot = PCI_SLOT(dev->devfn);
  630. uint8_t func;
  631. if (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
  632. dev->config[PCI_HEADER_TYPE] |= PCI_HEADER_TYPE_MULTI_FUNCTION;
  633. }
  634. /*
  635. * multifunction bit is interpreted in two ways as follows.
  636. * - all functions must set the bit to 1.
  637. * Example: Intel X53
  638. * - function 0 must set the bit, but the rest function (> 0)
  639. * is allowed to leave the bit to 0.
  640. * Example: PIIX3(also in qemu), PIIX4(also in qemu), ICH10,
  641. *
  642. * So OS (at least Linux) checks the bit of only function 0,
  643. * and doesn't see the bit of function > 0.
  644. *
  645. * The below check allows both interpretation.
  646. */
  647. if (PCI_FUNC(dev->devfn)) {
  648. PCIDevice *f0 = bus->devices[PCI_DEVFN(slot, 0)];
  649. if (f0 && !(f0->cap_present & QEMU_PCI_CAP_MULTIFUNCTION)) {
  650. /* function 0 should set multifunction bit */
  651. error_report("PCI: single function device can't be populated "
  652. "in function %x.%x", slot, PCI_FUNC(dev->devfn));
  653. return -1;
  654. }
  655. return 0;
  656. }
  657. if (dev->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
  658. return 0;
  659. }
  660. /* function 0 indicates single function, so function > 0 must be NULL */
  661. for (func = 1; func < PCI_FUNC_MAX; ++func) {
  662. if (bus->devices[PCI_DEVFN(slot, func)]) {
  663. error_report("PCI: %x.0 indicates single function, "
  664. "but %x.%x is already populated.",
  665. slot, slot, func);
  666. return -1;
  667. }
  668. }
  669. return 0;
  670. }
  671. static void pci_config_alloc(PCIDevice *pci_dev)
  672. {
  673. int config_size = pci_config_size(pci_dev);
  674. pci_dev->config = g_malloc0(config_size);
  675. pci_dev->cmask = g_malloc0(config_size);
  676. pci_dev->wmask = g_malloc0(config_size);
  677. pci_dev->w1cmask = g_malloc0(config_size);
  678. pci_dev->used = g_malloc0(config_size);
  679. }
  680. static void pci_config_free(PCIDevice *pci_dev)
  681. {
  682. g_free(pci_dev->config);
  683. g_free(pci_dev->cmask);
  684. g_free(pci_dev->wmask);
  685. g_free(pci_dev->w1cmask);
  686. g_free(pci_dev->used);
  687. }
  688. static void do_pci_unregister_device(PCIDevice *pci_dev)
  689. {
  690. pci_dev->bus->devices[pci_dev->devfn] = NULL;
  691. pci_config_free(pci_dev);
  692. address_space_destroy(&pci_dev->bus_master_as);
  693. memory_region_destroy(&pci_dev->bus_master_enable_region);
  694. }
  695. /* -1 for devfn means auto assign */
  696. static PCIDevice *do_pci_register_device(PCIDevice *pci_dev, PCIBus *bus,
  697. const char *name, int devfn)
  698. {
  699. PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev);
  700. PCIConfigReadFunc *config_read = pc->config_read;
  701. PCIConfigWriteFunc *config_write = pc->config_write;
  702. AddressSpace *dma_as;
  703. if (devfn < 0) {
  704. for(devfn = bus->devfn_min ; devfn < ARRAY_SIZE(bus->devices);
  705. devfn += PCI_FUNC_MAX) {
  706. if (!bus->devices[devfn])
  707. goto found;
  708. }
  709. error_report("PCI: no slot/function available for %s, all in use", name);
  710. return NULL;
  711. found: ;
  712. } else if (bus->devices[devfn]) {
  713. error_report("PCI: slot %d function %d not available for %s, in use by %s",
  714. PCI_SLOT(devfn), PCI_FUNC(devfn), name, bus->devices[devfn]->name);
  715. return NULL;
  716. }
  717. pci_dev->bus = bus;
  718. pci_dev->devfn = devfn;
  719. dma_as = pci_device_iommu_address_space(pci_dev);
  720. memory_region_init_alias(&pci_dev->bus_master_enable_region,
  721. OBJECT(pci_dev), "bus master",
  722. dma_as->root, 0, memory_region_size(dma_as->root));
  723. memory_region_set_enabled(&pci_dev->bus_master_enable_region, false);
  724. address_space_init(&pci_dev->bus_master_as, &pci_dev->bus_master_enable_region,
  725. name);
  726. pstrcpy(pci_dev->name, sizeof(pci_dev->name), name);
  727. pci_dev->irq_state = 0;
  728. pci_config_alloc(pci_dev);
  729. pci_config_set_vendor_id(pci_dev->config, pc->vendor_id);
  730. pci_config_set_device_id(pci_dev->config, pc->device_id);
  731. pci_config_set_revision(pci_dev->config, pc->revision);
  732. pci_config_set_class(pci_dev->config, pc->class_id);
  733. if (!pc->is_bridge) {
  734. if (pc->subsystem_vendor_id || pc->subsystem_id) {
  735. pci_set_word(pci_dev->config + PCI_SUBSYSTEM_VENDOR_ID,
  736. pc->subsystem_vendor_id);
  737. pci_set_word(pci_dev->config + PCI_SUBSYSTEM_ID,
  738. pc->subsystem_id);
  739. } else {
  740. pci_set_default_subsystem_id(pci_dev);
  741. }
  742. } else {
  743. /* subsystem_vendor_id/subsystem_id are only for header type 0 */
  744. assert(!pc->subsystem_vendor_id);
  745. assert(!pc->subsystem_id);
  746. }
  747. pci_init_cmask(pci_dev);
  748. pci_init_wmask(pci_dev);
  749. pci_init_w1cmask(pci_dev);
  750. if (pc->is_bridge) {
  751. pci_init_mask_bridge(pci_dev);
  752. }
  753. if (pci_init_multifunction(bus, pci_dev)) {
  754. do_pci_unregister_device(pci_dev);
  755. return NULL;
  756. }
  757. if (!config_read)
  758. config_read = pci_default_read_config;
  759. if (!config_write)
  760. config_write = pci_default_write_config;
  761. pci_dev->config_read = config_read;
  762. pci_dev->config_write = config_write;
  763. bus->devices[devfn] = pci_dev;
  764. pci_dev->version_id = 2; /* Current pci device vmstate version */
  765. return pci_dev;
  766. }
  767. static void pci_unregister_io_regions(PCIDevice *pci_dev)
  768. {
  769. PCIIORegion *r;
  770. int i;
  771. for(i = 0; i < PCI_NUM_REGIONS; i++) {
  772. r = &pci_dev->io_regions[i];
  773. if (!r->size || r->addr == PCI_BAR_UNMAPPED)
  774. continue;
  775. memory_region_del_subregion(r->address_space, r->memory);
  776. }
  777. pci_unregister_vga(pci_dev);
  778. }
  779. static int pci_unregister_device(DeviceState *dev)
  780. {
  781. PCIDevice *pci_dev = PCI_DEVICE(dev);
  782. PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev);
  783. pci_unregister_io_regions(pci_dev);
  784. pci_del_option_rom(pci_dev);
  785. if (pc->exit) {
  786. pc->exit(pci_dev);
  787. }
  788. do_pci_unregister_device(pci_dev);
  789. return 0;
  790. }
  791. void pci_register_bar(PCIDevice *pci_dev, int region_num,
  792. uint8_t type, MemoryRegion *memory)
  793. {
  794. PCIIORegion *r;
  795. uint32_t addr;
  796. uint64_t wmask;
  797. pcibus_t size = memory_region_size(memory);
  798. assert(region_num >= 0);
  799. assert(region_num < PCI_NUM_REGIONS);
  800. if (size & (size-1)) {
  801. fprintf(stderr, "ERROR: PCI region size must be pow2 "
  802. "type=0x%x, size=0x%"FMT_PCIBUS"\n", type, size);
  803. exit(1);
  804. }
  805. r = &pci_dev->io_regions[region_num];
  806. r->addr = PCI_BAR_UNMAPPED;
  807. r->size = size;
  808. r->type = type;
  809. r->memory = NULL;
  810. wmask = ~(size - 1);
  811. addr = pci_bar(pci_dev, region_num);
  812. if (region_num == PCI_ROM_SLOT) {
  813. /* ROM enable bit is writable */
  814. wmask |= PCI_ROM_ADDRESS_ENABLE;
  815. }
  816. pci_set_long(pci_dev->config + addr, type);
  817. if (!(r->type & PCI_BASE_ADDRESS_SPACE_IO) &&
  818. r->type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
  819. pci_set_quad(pci_dev->wmask + addr, wmask);
  820. pci_set_quad(pci_dev->cmask + addr, ~0ULL);
  821. } else {
  822. pci_set_long(pci_dev->wmask + addr, wmask & 0xffffffff);
  823. pci_set_long(pci_dev->cmask + addr, 0xffffffff);
  824. }
  825. pci_dev->io_regions[region_num].memory = memory;
  826. pci_dev->io_regions[region_num].address_space
  827. = type & PCI_BASE_ADDRESS_SPACE_IO
  828. ? pci_dev->bus->address_space_io
  829. : pci_dev->bus->address_space_mem;
  830. }
  831. static void pci_update_vga(PCIDevice *pci_dev)
  832. {
  833. uint16_t cmd;
  834. if (!pci_dev->has_vga) {
  835. return;
  836. }
  837. cmd = pci_get_word(pci_dev->config + PCI_COMMAND);
  838. memory_region_set_enabled(pci_dev->vga_regions[QEMU_PCI_VGA_MEM],
  839. cmd & PCI_COMMAND_MEMORY);
  840. memory_region_set_enabled(pci_dev->vga_regions[QEMU_PCI_VGA_IO_LO],
  841. cmd & PCI_COMMAND_IO);
  842. memory_region_set_enabled(pci_dev->vga_regions[QEMU_PCI_VGA_IO_HI],
  843. cmd & PCI_COMMAND_IO);
  844. }
  845. void pci_register_vga(PCIDevice *pci_dev, MemoryRegion *mem,
  846. MemoryRegion *io_lo, MemoryRegion *io_hi)
  847. {
  848. assert(!pci_dev->has_vga);
  849. assert(memory_region_size(mem) == QEMU_PCI_VGA_MEM_SIZE);
  850. pci_dev->vga_regions[QEMU_PCI_VGA_MEM] = mem;
  851. memory_region_add_subregion_overlap(pci_dev->bus->address_space_mem,
  852. QEMU_PCI_VGA_MEM_BASE, mem, 1);
  853. assert(memory_region_size(io_lo) == QEMU_PCI_VGA_IO_LO_SIZE);
  854. pci_dev->vga_regions[QEMU_PCI_VGA_IO_LO] = io_lo;
  855. memory_region_add_subregion_overlap(pci_dev->bus->address_space_io,
  856. QEMU_PCI_VGA_IO_LO_BASE, io_lo, 1);
  857. assert(memory_region_size(io_hi) == QEMU_PCI_VGA_IO_HI_SIZE);
  858. pci_dev->vga_regions[QEMU_PCI_VGA_IO_HI] = io_hi;
  859. memory_region_add_subregion_overlap(pci_dev->bus->address_space_io,
  860. QEMU_PCI_VGA_IO_HI_BASE, io_hi, 1);
  861. pci_dev->has_vga = true;
  862. pci_update_vga(pci_dev);
  863. }
  864. void pci_unregister_vga(PCIDevice *pci_dev)
  865. {
  866. if (!pci_dev->has_vga) {
  867. return;
  868. }
  869. memory_region_del_subregion(pci_dev->bus->address_space_mem,
  870. pci_dev->vga_regions[QEMU_PCI_VGA_MEM]);
  871. memory_region_del_subregion(pci_dev->bus->address_space_io,
  872. pci_dev->vga_regions[QEMU_PCI_VGA_IO_LO]);
  873. memory_region_del_subregion(pci_dev->bus->address_space_io,
  874. pci_dev->vga_regions[QEMU_PCI_VGA_IO_HI]);
  875. pci_dev->has_vga = false;
  876. }
  877. pcibus_t pci_get_bar_addr(PCIDevice *pci_dev, int region_num)
  878. {
  879. return pci_dev->io_regions[region_num].addr;
  880. }
  881. static pcibus_t pci_bar_address(PCIDevice *d,
  882. int reg, uint8_t type, pcibus_t size)
  883. {
  884. pcibus_t new_addr, last_addr;
  885. int bar = pci_bar(d, reg);
  886. uint16_t cmd = pci_get_word(d->config + PCI_COMMAND);
  887. if (type & PCI_BASE_ADDRESS_SPACE_IO) {
  888. if (!(cmd & PCI_COMMAND_IO)) {
  889. return PCI_BAR_UNMAPPED;
  890. }
  891. new_addr = pci_get_long(d->config + bar) & ~(size - 1);
  892. last_addr = new_addr + size - 1;
  893. /* Check if 32 bit BAR wraps around explicitly.
  894. * TODO: make priorities correct and remove this work around.
  895. */
  896. if (last_addr <= new_addr || new_addr == 0 || last_addr >= UINT32_MAX) {
  897. return PCI_BAR_UNMAPPED;
  898. }
  899. return new_addr;
  900. }
  901. if (!(cmd & PCI_COMMAND_MEMORY)) {
  902. return PCI_BAR_UNMAPPED;
  903. }
  904. if (type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
  905. new_addr = pci_get_quad(d->config + bar);
  906. } else {
  907. new_addr = pci_get_long(d->config + bar);
  908. }
  909. /* the ROM slot has a specific enable bit */
  910. if (reg == PCI_ROM_SLOT && !(new_addr & PCI_ROM_ADDRESS_ENABLE)) {
  911. return PCI_BAR_UNMAPPED;
  912. }
  913. new_addr &= ~(size - 1);
  914. last_addr = new_addr + size - 1;
  915. /* NOTE: we do not support wrapping */
  916. /* XXX: as we cannot support really dynamic
  917. mappings, we handle specific values as invalid
  918. mappings. */
  919. if (last_addr <= new_addr || new_addr == 0 ||
  920. last_addr == PCI_BAR_UNMAPPED) {
  921. return PCI_BAR_UNMAPPED;
  922. }
  923. /* Now pcibus_t is 64bit.
  924. * Check if 32 bit BAR wraps around explicitly.
  925. * Without this, PC ide doesn't work well.
  926. * TODO: remove this work around.
  927. */
  928. if (!(type & PCI_BASE_ADDRESS_MEM_TYPE_64) && last_addr >= UINT32_MAX) {
  929. return PCI_BAR_UNMAPPED;
  930. }
  931. /*
  932. * OS is allowed to set BAR beyond its addressable
  933. * bits. For example, 32 bit OS can set 64bit bar
  934. * to >4G. Check it. TODO: we might need to support
  935. * it in the future for e.g. PAE.
  936. */
  937. if (last_addr >= HWADDR_MAX) {
  938. return PCI_BAR_UNMAPPED;
  939. }
  940. return new_addr;
  941. }
  942. static void pci_update_mappings(PCIDevice *d)
  943. {
  944. PCIIORegion *r;
  945. int i;
  946. pcibus_t new_addr;
  947. for(i = 0; i < PCI_NUM_REGIONS; i++) {
  948. r = &d->io_regions[i];
  949. /* this region isn't registered */
  950. if (!r->size)
  951. continue;
  952. new_addr = pci_bar_address(d, i, r->type, r->size);
  953. /* This bar isn't changed */
  954. if (new_addr == r->addr)
  955. continue;
  956. /* now do the real mapping */
  957. if (r->addr != PCI_BAR_UNMAPPED) {
  958. memory_region_del_subregion(r->address_space, r->memory);
  959. }
  960. r->addr = new_addr;
  961. if (r->addr != PCI_BAR_UNMAPPED) {
  962. memory_region_add_subregion_overlap(r->address_space,
  963. r->addr, r->memory, 1);
  964. }
  965. }
  966. pci_update_vga(d);
  967. }
  968. static inline int pci_irq_disabled(PCIDevice *d)
  969. {
  970. return pci_get_word(d->config + PCI_COMMAND) & PCI_COMMAND_INTX_DISABLE;
  971. }
  972. /* Called after interrupt disabled field update in config space,
  973. * assert/deassert interrupts if necessary.
  974. * Gets original interrupt disable bit value (before update). */
  975. static void pci_update_irq_disabled(PCIDevice *d, int was_irq_disabled)
  976. {
  977. int i, disabled = pci_irq_disabled(d);
  978. if (disabled == was_irq_disabled)
  979. return;
  980. for (i = 0; i < PCI_NUM_PINS; ++i) {
  981. int state = pci_irq_state(d, i);
  982. pci_change_irq_level(d, i, disabled ? -state : state);
  983. }
  984. }
  985. uint32_t pci_default_read_config(PCIDevice *d,
  986. uint32_t address, int len)
  987. {
  988. uint32_t val = 0;
  989. memcpy(&val, d->config + address, len);
  990. return le32_to_cpu(val);
  991. }
  992. void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l)
  993. {
  994. int i, was_irq_disabled = pci_irq_disabled(d);
  995. for (i = 0; i < l; val >>= 8, ++i) {
  996. uint8_t wmask = d->wmask[addr + i];
  997. uint8_t w1cmask = d->w1cmask[addr + i];
  998. assert(!(wmask & w1cmask));
  999. d->config[addr + i] = (d->config[addr + i] & ~wmask) | (val & wmask);
  1000. d->config[addr + i] &= ~(val & w1cmask); /* W1C: Write 1 to Clear */
  1001. }
  1002. if (ranges_overlap(addr, l, PCI_BASE_ADDRESS_0, 24) ||
  1003. ranges_overlap(addr, l, PCI_ROM_ADDRESS, 4) ||
  1004. ranges_overlap(addr, l, PCI_ROM_ADDRESS1, 4) ||
  1005. range_covers_byte(addr, l, PCI_COMMAND))
  1006. pci_update_mappings(d);
  1007. if (range_covers_byte(addr, l, PCI_COMMAND)) {
  1008. pci_update_irq_disabled(d, was_irq_disabled);
  1009. memory_region_set_enabled(&d->bus_master_enable_region,
  1010. pci_get_word(d->config + PCI_COMMAND)
  1011. & PCI_COMMAND_MASTER);
  1012. }
  1013. msi_write_config(d, addr, val, l);
  1014. msix_write_config(d, addr, val, l);
  1015. }
  1016. /***********************************************************/
  1017. /* generic PCI irq support */
  1018. /* 0 <= irq_num <= 3. level must be 0 or 1 */
  1019. static void pci_irq_handler(void *opaque, int irq_num, int level)
  1020. {
  1021. PCIDevice *pci_dev = opaque;
  1022. int change;
  1023. change = level - pci_irq_state(pci_dev, irq_num);
  1024. if (!change)
  1025. return;
  1026. pci_set_irq_state(pci_dev, irq_num, level);
  1027. pci_update_irq_status(pci_dev);
  1028. if (pci_irq_disabled(pci_dev))
  1029. return;
  1030. pci_change_irq_level(pci_dev, irq_num, change);
  1031. }
  1032. static inline int pci_intx(PCIDevice *pci_dev)
  1033. {
  1034. return pci_get_byte(pci_dev->config + PCI_INTERRUPT_PIN) - 1;
  1035. }
  1036. qemu_irq pci_allocate_irq(PCIDevice *pci_dev)
  1037. {
  1038. int intx = pci_intx(pci_dev);
  1039. return qemu_allocate_irq(pci_irq_handler, pci_dev, intx);
  1040. }
  1041. void pci_set_irq(PCIDevice *pci_dev, int level)
  1042. {
  1043. int intx = pci_intx(pci_dev);
  1044. pci_irq_handler(pci_dev, intx, level);
  1045. }
  1046. /* Special hooks used by device assignment */
  1047. void pci_bus_set_route_irq_fn(PCIBus *bus, pci_route_irq_fn route_intx_to_irq)
  1048. {
  1049. assert(pci_bus_is_root(bus));
  1050. bus->route_intx_to_irq = route_intx_to_irq;
  1051. }
  1052. PCIINTxRoute pci_device_route_intx_to_irq(PCIDevice *dev, int pin)
  1053. {
  1054. PCIBus *bus;
  1055. do {
  1056. bus = dev->bus;
  1057. pin = bus->map_irq(dev, pin);
  1058. dev = bus->parent_dev;
  1059. } while (dev);
  1060. if (!bus->route_intx_to_irq) {
  1061. error_report("PCI: Bug - unimplemented PCI INTx routing (%s)",
  1062. object_get_typename(OBJECT(bus->qbus.parent)));
  1063. return (PCIINTxRoute) { PCI_INTX_DISABLED, -1 };
  1064. }
  1065. return bus->route_intx_to_irq(bus->irq_opaque, pin);
  1066. }
  1067. bool pci_intx_route_changed(PCIINTxRoute *old, PCIINTxRoute *new)
  1068. {
  1069. return old->mode != new->mode || old->irq != new->irq;
  1070. }
  1071. void pci_bus_fire_intx_routing_notifier(PCIBus *bus)
  1072. {
  1073. PCIDevice *dev;
  1074. PCIBus *sec;
  1075. int i;
  1076. for (i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
  1077. dev = bus->devices[i];
  1078. if (dev && dev->intx_routing_notifier) {
  1079. dev->intx_routing_notifier(dev);
  1080. }
  1081. }
  1082. QLIST_FOREACH(sec, &bus->child, sibling) {
  1083. pci_bus_fire_intx_routing_notifier(sec);
  1084. }
  1085. }
  1086. void pci_device_set_intx_routing_notifier(PCIDevice *dev,
  1087. PCIINTxRoutingNotifier notifier)
  1088. {
  1089. dev->intx_routing_notifier = notifier;
  1090. }
  1091. /*
  1092. * PCI-to-PCI bridge specification
  1093. * 9.1: Interrupt routing. Table 9-1
  1094. *
  1095. * the PCI Express Base Specification, Revision 2.1
  1096. * 2.2.8.1: INTx interrutp signaling - Rules
  1097. * the Implementation Note
  1098. * Table 2-20
  1099. */
  1100. /*
  1101. * 0 <= pin <= 3 0 = INTA, 1 = INTB, 2 = INTC, 3 = INTD
  1102. * 0-origin unlike PCI interrupt pin register.
  1103. */
  1104. int pci_swizzle_map_irq_fn(PCIDevice *pci_dev, int pin)
  1105. {
  1106. return (pin + PCI_SLOT(pci_dev->devfn)) % PCI_NUM_PINS;
  1107. }
  1108. /***********************************************************/
  1109. /* monitor info on PCI */
  1110. typedef struct {
  1111. uint16_t class;
  1112. const char *desc;
  1113. const char *fw_name;
  1114. uint16_t fw_ign_bits;
  1115. } pci_class_desc;
  1116. static const pci_class_desc pci_class_descriptions[] =
  1117. {
  1118. { 0x0001, "VGA controller", "display"},
  1119. { 0x0100, "SCSI controller", "scsi"},
  1120. { 0x0101, "IDE controller", "ide"},
  1121. { 0x0102, "Floppy controller", "fdc"},
  1122. { 0x0103, "IPI controller", "ipi"},
  1123. { 0x0104, "RAID controller", "raid"},
  1124. { 0x0106, "SATA controller"},
  1125. { 0x0107, "SAS controller"},
  1126. { 0x0180, "Storage controller"},
  1127. { 0x0200, "Ethernet controller", "ethernet"},
  1128. { 0x0201, "Token Ring controller", "token-ring"},
  1129. { 0x0202, "FDDI controller", "fddi"},
  1130. { 0x0203, "ATM controller", "atm"},
  1131. { 0x0280, "Network controller"},
  1132. { 0x0300, "VGA controller", "display", 0x00ff},
  1133. { 0x0301, "XGA controller"},
  1134. { 0x0302, "3D controller"},
  1135. { 0x0380, "Display controller"},
  1136. { 0x0400, "Video controller", "video"},
  1137. { 0x0401, "Audio controller", "sound"},
  1138. { 0x0402, "Phone"},
  1139. { 0x0403, "Audio controller", "sound"},
  1140. { 0x0480, "Multimedia controller"},
  1141. { 0x0500, "RAM controller", "memory"},
  1142. { 0x0501, "Flash controller", "flash"},
  1143. { 0x0580, "Memory controller"},
  1144. { 0x0600, "Host bridge", "host"},
  1145. { 0x0601, "ISA bridge", "isa"},
  1146. { 0x0602, "EISA bridge", "eisa"},
  1147. { 0x0603, "MC bridge", "mca"},
  1148. { 0x0604, "PCI bridge", "pci-bridge"},
  1149. { 0x0605, "PCMCIA bridge", "pcmcia"},
  1150. { 0x0606, "NUBUS bridge", "nubus"},
  1151. { 0x0607, "CARDBUS bridge", "cardbus"},
  1152. { 0x0608, "RACEWAY bridge"},
  1153. { 0x0680, "Bridge"},
  1154. { 0x0700, "Serial port", "serial"},
  1155. { 0x0701, "Parallel port", "parallel"},
  1156. { 0x0800, "Interrupt controller", "interrupt-controller"},
  1157. { 0x0801, "DMA controller", "dma-controller"},
  1158. { 0x0802, "Timer", "timer"},
  1159. { 0x0803, "RTC", "rtc"},
  1160. { 0x0900, "Keyboard", "keyboard"},
  1161. { 0x0901, "Pen", "pen"},
  1162. { 0x0902, "Mouse", "mouse"},
  1163. { 0x0A00, "Dock station", "dock", 0x00ff},
  1164. { 0x0B00, "i386 cpu", "cpu", 0x00ff},
  1165. { 0x0c00, "Fireware contorller", "fireware"},
  1166. { 0x0c01, "Access bus controller", "access-bus"},
  1167. { 0x0c02, "SSA controller", "ssa"},
  1168. { 0x0c03, "USB controller", "usb"},
  1169. { 0x0c04, "Fibre channel controller", "fibre-channel"},
  1170. { 0x0c05, "SMBus"},
  1171. { 0, NULL}
  1172. };
  1173. static void pci_for_each_device_under_bus(PCIBus *bus,
  1174. void (*fn)(PCIBus *b, PCIDevice *d,
  1175. void *opaque),
  1176. void *opaque)
  1177. {
  1178. PCIDevice *d;
  1179. int devfn;
  1180. for(devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
  1181. d = bus->devices[devfn];
  1182. if (d) {
  1183. fn(bus, d, opaque);
  1184. }
  1185. }
  1186. }
  1187. void pci_for_each_device(PCIBus *bus, int bus_num,
  1188. void (*fn)(PCIBus *b, PCIDevice *d, void *opaque),
  1189. void *opaque)
  1190. {
  1191. bus = pci_find_bus_nr(bus, bus_num);
  1192. if (bus) {
  1193. pci_for_each_device_under_bus(bus, fn, opaque);
  1194. }
  1195. }
  1196. static const pci_class_desc *get_class_desc(int class)
  1197. {
  1198. const pci_class_desc *desc;
  1199. desc = pci_class_descriptions;
  1200. while (desc->desc && class != desc->class) {
  1201. desc++;
  1202. }
  1203. return desc;
  1204. }
  1205. static PciDeviceInfoList *qmp_query_pci_devices(PCIBus *bus, int bus_num);
  1206. static PciMemoryRegionList *qmp_query_pci_regions(const PCIDevice *dev)
  1207. {
  1208. PciMemoryRegionList *head = NULL, *cur_item = NULL;
  1209. int i;
  1210. for (i = 0; i < PCI_NUM_REGIONS; i++) {
  1211. const PCIIORegion *r = &dev->io_regions[i];
  1212. PciMemoryRegionList *region;
  1213. if (!r->size) {
  1214. continue;
  1215. }
  1216. region = g_malloc0(sizeof(*region));
  1217. region->value = g_malloc0(sizeof(*region->value));
  1218. if (r->type & PCI_BASE_ADDRESS_SPACE_IO) {
  1219. region->value->type = g_strdup("io");
  1220. } else {
  1221. region->value->type = g_strdup("memory");
  1222. region->value->has_prefetch = true;
  1223. region->value->prefetch = !!(r->type & PCI_BASE_ADDRESS_MEM_PREFETCH);
  1224. region->value->has_mem_type_64 = true;
  1225. region->value->mem_type_64 = !!(r->type & PCI_BASE_ADDRESS_MEM_TYPE_64);
  1226. }
  1227. region->value->bar = i;
  1228. region->value->address = r->addr;
  1229. region->value->size = r->size;
  1230. /* XXX: waiting for the qapi to support GSList */
  1231. if (!cur_item) {
  1232. head = cur_item = region;
  1233. } else {
  1234. cur_item->next = region;
  1235. cur_item = region;
  1236. }
  1237. }
  1238. return head;
  1239. }
  1240. static PciBridgeInfo *qmp_query_pci_bridge(PCIDevice *dev, PCIBus *bus,
  1241. int bus_num)
  1242. {
  1243. PciBridgeInfo *info;
  1244. info = g_malloc0(sizeof(*info));
  1245. info->bus.number = dev->config[PCI_PRIMARY_BUS];
  1246. info->bus.secondary = dev->config[PCI_SECONDARY_BUS];
  1247. info->bus.subordinate = dev->config[PCI_SUBORDINATE_BUS];
  1248. info->bus.io_range = g_malloc0(sizeof(*info->bus.io_range));
  1249. info->bus.io_range->base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_IO);
  1250. info->bus.io_range->limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_IO);
  1251. info->bus.memory_range = g_malloc0(sizeof(*info->bus.memory_range));
  1252. info->bus.memory_range->base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_SPACE_MEMORY);
  1253. info->bus.memory_range->limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_SPACE_MEMORY);
  1254. info->bus.prefetchable_range = g_malloc0(sizeof(*info->bus.prefetchable_range));
  1255. info->bus.prefetchable_range->base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
  1256. info->bus.prefetchable_range->limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
  1257. if (dev->config[PCI_SECONDARY_BUS] != 0) {
  1258. PCIBus *child_bus = pci_find_bus_nr(bus, dev->config[PCI_SECONDARY_BUS]);
  1259. if (child_bus) {
  1260. info->has_devices = true;
  1261. info->devices = qmp_query_pci_devices(child_bus, dev->config[PCI_SECONDARY_BUS]);
  1262. }
  1263. }
  1264. return info;
  1265. }
  1266. static PciDeviceInfo *qmp_query_pci_device(PCIDevice *dev, PCIBus *bus,
  1267. int bus_num)
  1268. {
  1269. const pci_class_desc *desc;
  1270. PciDeviceInfo *info;
  1271. uint8_t type;
  1272. int class;
  1273. info = g_malloc0(sizeof(*info));
  1274. info->bus = bus_num;
  1275. info->slot = PCI_SLOT(dev->devfn);
  1276. info->function = PCI_FUNC(dev->devfn);
  1277. class = pci_get_word(dev->config + PCI_CLASS_DEVICE);
  1278. info->class_info.q_class = class;
  1279. desc = get_class_desc(class);
  1280. if (desc->desc) {
  1281. info->class_info.has_desc = true;
  1282. info->class_info.desc = g_strdup(desc->desc);
  1283. }
  1284. info->id.vendor = pci_get_word(dev->config + PCI_VENDOR_ID);
  1285. info->id.device = pci_get_word(dev->config + PCI_DEVICE_ID);
  1286. info->regions = qmp_query_pci_regions(dev);
  1287. info->qdev_id = g_strdup(dev->qdev.id ? dev->qdev.id : "");
  1288. if (dev->config[PCI_INTERRUPT_PIN] != 0) {
  1289. info->has_irq = true;
  1290. info->irq = dev->config[PCI_INTERRUPT_LINE];
  1291. }
  1292. type = dev->config[PCI_HEADER_TYPE] & ~PCI_HEADER_TYPE_MULTI_FUNCTION;
  1293. if (type == PCI_HEADER_TYPE_BRIDGE) {
  1294. info->has_pci_bridge = true;
  1295. info->pci_bridge = qmp_query_pci_bridge(dev, bus, bus_num);
  1296. }
  1297. return info;
  1298. }
  1299. static PciDeviceInfoList *qmp_query_pci_devices(PCIBus *bus, int bus_num)
  1300. {
  1301. PciDeviceInfoList *info, *head = NULL, *cur_item = NULL;
  1302. PCIDevice *dev;
  1303. int devfn;
  1304. for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
  1305. dev = bus->devices[devfn];
  1306. if (dev) {
  1307. info = g_malloc0(sizeof(*info));
  1308. info->value = qmp_query_pci_device(dev, bus, bus_num);
  1309. /* XXX: waiting for the qapi to support GSList */
  1310. if (!cur_item) {
  1311. head = cur_item = info;
  1312. } else {
  1313. cur_item->next = info;
  1314. cur_item = info;
  1315. }
  1316. }
  1317. }
  1318. return head;
  1319. }
  1320. static PciInfo *qmp_query_pci_bus(PCIBus *bus, int bus_num)
  1321. {
  1322. PciInfo *info = NULL;
  1323. bus = pci_find_bus_nr(bus, bus_num);
  1324. if (bus) {
  1325. info = g_malloc0(sizeof(*info));
  1326. info->bus = bus_num;
  1327. info->devices = qmp_query_pci_devices(bus, bus_num);
  1328. }
  1329. return info;
  1330. }
  1331. PciInfoList *qmp_query_pci(Error **errp)
  1332. {
  1333. PciInfoList *info, *head = NULL, *cur_item = NULL;
  1334. PCIHostState *host_bridge;
  1335. QLIST_FOREACH(host_bridge, &pci_host_bridges, next) {
  1336. info = g_malloc0(sizeof(*info));
  1337. info->value = qmp_query_pci_bus(host_bridge->bus, 0);
  1338. /* XXX: waiting for the qapi to support GSList */
  1339. if (!cur_item) {
  1340. head = cur_item = info;
  1341. } else {
  1342. cur_item->next = info;
  1343. cur_item = info;
  1344. }
  1345. }
  1346. return head;
  1347. }
  1348. static const char * const pci_nic_models[] = {
  1349. "ne2k_pci",
  1350. "i82551",
  1351. "i82557b",
  1352. "i82559er",
  1353. "rtl8139",
  1354. "e1000",
  1355. "pcnet",
  1356. "virtio",
  1357. NULL
  1358. };
  1359. static const char * const pci_nic_names[] = {
  1360. "ne2k_pci",
  1361. "i82551",
  1362. "i82557b",
  1363. "i82559er",
  1364. "rtl8139",
  1365. "e1000",
  1366. "pcnet",
  1367. "virtio-net-pci",
  1368. NULL
  1369. };
  1370. /* Initialize a PCI NIC. */
  1371. /* FIXME callers should check for failure, but don't */
  1372. PCIDevice *pci_nic_init(NICInfo *nd, PCIBus *rootbus,
  1373. const char *default_model,
  1374. const char *default_devaddr)
  1375. {
  1376. const char *devaddr = nd->devaddr ? nd->devaddr : default_devaddr;
  1377. PCIBus *bus;
  1378. int devfn;
  1379. PCIDevice *pci_dev;
  1380. DeviceState *dev;
  1381. int i;
  1382. i = qemu_find_nic_model(nd, pci_nic_models, default_model);
  1383. if (i < 0)
  1384. return NULL;
  1385. bus = pci_get_bus_devfn(&devfn, rootbus, devaddr);
  1386. if (!bus) {
  1387. error_report("Invalid PCI device address %s for device %s",
  1388. devaddr, pci_nic_names[i]);
  1389. return NULL;
  1390. }
  1391. pci_dev = pci_create(bus, devfn, pci_nic_names[i]);
  1392. dev = &pci_dev->qdev;
  1393. qdev_set_nic_properties(dev, nd);
  1394. if (qdev_init(dev) < 0)
  1395. return NULL;
  1396. return pci_dev;
  1397. }
  1398. PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus,
  1399. const char *default_model,
  1400. const char *default_devaddr)
  1401. {
  1402. PCIDevice *res;
  1403. if (qemu_show_nic_models(nd->model, pci_nic_models))
  1404. exit(0);
  1405. res = pci_nic_init(nd, rootbus, default_model, default_devaddr);
  1406. if (!res)
  1407. exit(1);
  1408. return res;
  1409. }
  1410. PCIDevice *pci_vga_init(PCIBus *bus)
  1411. {
  1412. switch (vga_interface_type) {
  1413. case VGA_CIRRUS:
  1414. return pci_create_simple(bus, -1, "cirrus-vga");
  1415. case VGA_QXL:
  1416. return pci_create_simple(bus, -1, "qxl-vga");
  1417. case VGA_STD:
  1418. return pci_create_simple(bus, -1, "VGA");
  1419. case VGA_VMWARE:
  1420. return pci_create_simple(bus, -1, "vmware-svga");
  1421. case VGA_NONE:
  1422. default: /* Other non-PCI types. Checking for unsupported types is already
  1423. done in vl.c. */
  1424. return NULL;
  1425. }
  1426. }
  1427. /* Whether a given bus number is in range of the secondary
  1428. * bus of the given bridge device. */
  1429. static bool pci_secondary_bus_in_range(PCIDevice *dev, int bus_num)
  1430. {
  1431. return !(pci_get_word(dev->config + PCI_BRIDGE_CONTROL) &
  1432. PCI_BRIDGE_CTL_BUS_RESET) /* Don't walk the bus if it's reset. */ &&
  1433. dev->config[PCI_SECONDARY_BUS] < bus_num &&
  1434. bus_num <= dev->config[PCI_SUBORDINATE_BUS];
  1435. }
  1436. static PCIBus *pci_find_bus_nr(PCIBus *bus, int bus_num)
  1437. {
  1438. PCIBus *sec;
  1439. if (!bus) {
  1440. return NULL;
  1441. }
  1442. if (pci_bus_num(bus) == bus_num) {
  1443. return bus;
  1444. }
  1445. /* Consider all bus numbers in range for the host pci bridge. */
  1446. if (!pci_bus_is_root(bus) &&
  1447. !pci_secondary_bus_in_range(bus->parent_dev, bus_num)) {
  1448. return NULL;
  1449. }
  1450. /* try child bus */
  1451. for (; bus; bus = sec) {
  1452. QLIST_FOREACH(sec, &bus->child, sibling) {
  1453. assert(!pci_bus_is_root(sec));
  1454. if (sec->parent_dev->config[PCI_SECONDARY_BUS] == bus_num) {
  1455. return sec;
  1456. }
  1457. if (pci_secondary_bus_in_range(sec->parent_dev, bus_num)) {
  1458. break;
  1459. }
  1460. }
  1461. }
  1462. return NULL;
  1463. }
  1464. void pci_for_each_bus_depth_first(PCIBus *bus,
  1465. void *(*begin)(PCIBus *bus, void *parent_state),
  1466. void (*end)(PCIBus *bus, void *state),
  1467. void *parent_state)
  1468. {
  1469. PCIBus *sec;
  1470. void *state;
  1471. if (!bus) {
  1472. return;
  1473. }
  1474. if (begin) {
  1475. state = begin(bus, parent_state);
  1476. } else {
  1477. state = parent_state;
  1478. }
  1479. QLIST_FOREACH(sec, &bus->child, sibling) {
  1480. pci_for_each_bus_depth_first(sec, begin, end, state);
  1481. }
  1482. if (end) {
  1483. end(bus, state);
  1484. }
  1485. }
  1486. PCIDevice *pci_find_device(PCIBus *bus, int bus_num, uint8_t devfn)
  1487. {
  1488. bus = pci_find_bus_nr(bus, bus_num);
  1489. if (!bus)
  1490. return NULL;
  1491. return bus->devices[devfn];
  1492. }
  1493. static int pci_qdev_init(DeviceState *qdev)
  1494. {
  1495. PCIDevice *pci_dev = (PCIDevice *)qdev;
  1496. PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(pci_dev);
  1497. PCIBus *bus;
  1498. int rc;
  1499. bool is_default_rom;
  1500. /* initialize cap_present for pci_is_express() and pci_config_size() */
  1501. if (pc->is_express) {
  1502. pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS;
  1503. }
  1504. bus = PCI_BUS(qdev_get_parent_bus(qdev));
  1505. pci_dev = do_pci_register_device(pci_dev, bus,
  1506. object_get_typename(OBJECT(qdev)),
  1507. pci_dev->devfn);
  1508. if (pci_dev == NULL)
  1509. return -1;
  1510. if (pc->init) {
  1511. rc = pc->init(pci_dev);
  1512. if (rc != 0) {
  1513. do_pci_unregister_device(pci_dev);
  1514. return rc;
  1515. }
  1516. }
  1517. /* rom loading */
  1518. is_default_rom = false;
  1519. if (pci_dev->romfile == NULL && pc->romfile != NULL) {
  1520. pci_dev->romfile = g_strdup(pc->romfile);
  1521. is_default_rom = true;
  1522. }
  1523. pci_add_option_rom(pci_dev, is_default_rom);
  1524. return 0;
  1525. }
  1526. PCIDevice *pci_create_multifunction(PCIBus *bus, int devfn, bool multifunction,
  1527. const char *name)
  1528. {
  1529. DeviceState *dev;
  1530. dev = qdev_create(&bus->qbus, name);
  1531. qdev_prop_set_int32(dev, "addr", devfn);
  1532. qdev_prop_set_bit(dev, "multifunction", multifunction);
  1533. return PCI_DEVICE(dev);
  1534. }
  1535. PCIDevice *pci_create_simple_multifunction(PCIBus *bus, int devfn,
  1536. bool multifunction,
  1537. const char *name)
  1538. {
  1539. PCIDevice *dev = pci_create_multifunction(bus, devfn, multifunction, name);
  1540. qdev_init_nofail(&dev->qdev);
  1541. return dev;
  1542. }
  1543. PCIDevice *pci_create(PCIBus *bus, int devfn, const char *name)
  1544. {
  1545. return pci_create_multifunction(bus, devfn, false, name);
  1546. }
  1547. PCIDevice *pci_create_simple(PCIBus *bus, int devfn, const char *name)
  1548. {
  1549. return pci_create_simple_multifunction(bus, devfn, false, name);
  1550. }
  1551. static uint8_t pci_find_space(PCIDevice *pdev, uint8_t size)
  1552. {
  1553. int offset = PCI_CONFIG_HEADER_SIZE;
  1554. int i;
  1555. for (i = PCI_CONFIG_HEADER_SIZE; i < PCI_CONFIG_SPACE_SIZE; ++i) {
  1556. if (pdev->used[i])
  1557. offset = i + 1;
  1558. else if (i - offset + 1 == size)
  1559. return offset;
  1560. }
  1561. return 0;
  1562. }
  1563. static uint8_t pci_find_capability_list(PCIDevice *pdev, uint8_t cap_id,
  1564. uint8_t *prev_p)
  1565. {
  1566. uint8_t next, prev;
  1567. if (!(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST))
  1568. return 0;
  1569. for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]);
  1570. prev = next + PCI_CAP_LIST_NEXT)
  1571. if (pdev->config[next + PCI_CAP_LIST_ID] == cap_id)
  1572. break;
  1573. if (prev_p)
  1574. *prev_p = prev;
  1575. return next;
  1576. }
  1577. static uint8_t pci_find_capability_at_offset(PCIDevice *pdev, uint8_t offset)
  1578. {
  1579. uint8_t next, prev, found = 0;
  1580. if (!(pdev->used[offset])) {
  1581. return 0;
  1582. }
  1583. assert(pdev->config[PCI_STATUS] & PCI_STATUS_CAP_LIST);
  1584. for (prev = PCI_CAPABILITY_LIST; (next = pdev->config[prev]);
  1585. prev = next + PCI_CAP_LIST_NEXT) {
  1586. if (next <= offset && next > found) {
  1587. found = next;
  1588. }
  1589. }
  1590. return found;
  1591. }
  1592. /* Patch the PCI vendor and device ids in a PCI rom image if necessary.
  1593. This is needed for an option rom which is used for more than one device. */
  1594. static void pci_patch_ids(PCIDevice *pdev, uint8_t *ptr, int size)
  1595. {
  1596. uint16_t vendor_id;
  1597. uint16_t device_id;
  1598. uint16_t rom_vendor_id;
  1599. uint16_t rom_device_id;
  1600. uint16_t rom_magic;
  1601. uint16_t pcir_offset;
  1602. uint8_t checksum;
  1603. /* Words in rom data are little endian (like in PCI configuration),
  1604. so they can be read / written with pci_get_word / pci_set_word. */
  1605. /* Only a valid rom will be patched. */
  1606. rom_magic = pci_get_word(ptr);
  1607. if (rom_magic != 0xaa55) {
  1608. PCI_DPRINTF("Bad ROM magic %04x\n", rom_magic);
  1609. return;
  1610. }
  1611. pcir_offset = pci_get_word(ptr + 0x18);
  1612. if (pcir_offset + 8 >= size || memcmp(ptr + pcir_offset, "PCIR", 4)) {
  1613. PCI_DPRINTF("Bad PCIR offset 0x%x or signature\n", pcir_offset);
  1614. return;
  1615. }
  1616. vendor_id = pci_get_word(pdev->config + PCI_VENDOR_ID);
  1617. device_id = pci_get_word(pdev->config + PCI_DEVICE_ID);
  1618. rom_vendor_id = pci_get_word(ptr + pcir_offset + 4);
  1619. rom_device_id = pci_get_word(ptr + pcir_offset + 6);
  1620. PCI_DPRINTF("%s: ROM id %04x%04x / PCI id %04x%04x\n", pdev->romfile,
  1621. vendor_id, device_id, rom_vendor_id, rom_device_id);
  1622. checksum = ptr[6];
  1623. if (vendor_id != rom_vendor_id) {
  1624. /* Patch vendor id and checksum (at offset 6 for etherboot roms). */
  1625. checksum += (uint8_t)rom_vendor_id + (uint8_t)(rom_vendor_id >> 8);
  1626. checksum -= (uint8_t)vendor_id + (uint8_t)(vendor_id >> 8);
  1627. PCI_DPRINTF("ROM checksum %02x / %02x\n", ptr[6], checksum);
  1628. ptr[6] = checksum;
  1629. pci_set_word(ptr + pcir_offset + 4, vendor_id);
  1630. }
  1631. if (device_id != rom_device_id) {
  1632. /* Patch device id and checksum (at offset 6 for etherboot roms). */
  1633. checksum += (uint8_t)rom_device_id + (uint8_t)(rom_device_id >> 8);
  1634. checksum -= (uint8_t)device_id + (uint8_t)(device_id >> 8);
  1635. PCI_DPRINTF("ROM checksum %02x / %02x\n", ptr[6], checksum);
  1636. ptr[6] = checksum;
  1637. pci_set_word(ptr + pcir_offset + 6, device_id);
  1638. }
  1639. }
  1640. /* Add an option rom for the device */
  1641. static int pci_add_option_rom(PCIDevice *pdev, bool is_default_rom)
  1642. {
  1643. int size;
  1644. char *path;
  1645. void *ptr;
  1646. char name[32];
  1647. const VMStateDescription *vmsd;
  1648. if (!pdev->romfile)
  1649. return 0;
  1650. if (strlen(pdev->romfile) == 0)
  1651. return 0;
  1652. if (!pdev->rom_bar) {
  1653. /*
  1654. * Load rom via fw_cfg instead of creating a rom bar,
  1655. * for 0.11 compatibility.
  1656. */
  1657. int class = pci_get_word(pdev->config + PCI_CLASS_DEVICE);
  1658. if (class == 0x0300) {
  1659. rom_add_vga(pdev->romfile);
  1660. } else {
  1661. rom_add_option(pdev->romfile, -1);
  1662. }
  1663. return 0;
  1664. }
  1665. path = qemu_find_file(QEMU_FILE_TYPE_BIOS, pdev->romfile);
  1666. if (path == NULL) {
  1667. path = g_strdup(pdev->romfile);
  1668. }
  1669. size = get_image_size(path);
  1670. if (size < 0) {
  1671. error_report("%s: failed to find romfile \"%s\"",
  1672. __func__, pdev->romfile);
  1673. g_free(path);
  1674. return -1;
  1675. } else if (size == 0) {
  1676. error_report("%s: ignoring empty romfile \"%s\"",
  1677. __func__, pdev->romfile);
  1678. g_free(path);
  1679. return -1;
  1680. }
  1681. if (size & (size - 1)) {
  1682. size = 1 << qemu_fls(size);
  1683. }
  1684. vmsd = qdev_get_vmsd(DEVICE(pdev));
  1685. if (vmsd) {
  1686. snprintf(name, sizeof(name), "%s.rom", vmsd->name);
  1687. } else {
  1688. snprintf(name, sizeof(name), "%s.rom", object_get_typename(OBJECT(pdev)));
  1689. }
  1690. pdev->has_rom = true;
  1691. memory_region_init_ram(&pdev->rom, OBJECT(pdev), name, size);
  1692. vmstate_register_ram(&pdev->rom, &pdev->qdev);
  1693. ptr = memory_region_get_ram_ptr(&pdev->rom);
  1694. load_image(path, ptr);
  1695. g_free(path);
  1696. if (is_default_rom) {
  1697. /* Only the default rom images will be patched (if needed). */
  1698. pci_patch_ids(pdev, ptr, size);
  1699. }
  1700. pci_register_bar(pdev, PCI_ROM_SLOT, 0, &pdev->rom);
  1701. return 0;
  1702. }
  1703. static void pci_del_option_rom(PCIDevice *pdev)
  1704. {
  1705. if (!pdev->has_rom)
  1706. return;
  1707. vmstate_unregister_ram(&pdev->rom, &pdev->qdev);
  1708. memory_region_destroy(&pdev->rom);
  1709. pdev->has_rom = false;
  1710. }
  1711. /*
  1712. * if !offset
  1713. * Reserve space and add capability to the linked list in pci config space
  1714. *
  1715. * if offset = 0,
  1716. * Find and reserve space and add capability to the linked list
  1717. * in pci config space */
  1718. int pci_add_capability(PCIDevice *pdev, uint8_t cap_id,
  1719. uint8_t offset, uint8_t size)
  1720. {
  1721. uint8_t *config;
  1722. int i, overlapping_cap;
  1723. if (!offset) {
  1724. offset = pci_find_space(pdev, size);
  1725. if (!offset) {
  1726. return -ENOSPC;
  1727. }
  1728. } else {
  1729. /* Verify that capabilities don't overlap. Note: device assignment
  1730. * depends on this check to verify that the device is not broken.
  1731. * Should never trigger for emulated devices, but it's helpful
  1732. * for debugging these. */
  1733. for (i = offset; i < offset + size; i++) {
  1734. overlapping_cap = pci_find_capability_at_offset(pdev, i);
  1735. if (overlapping_cap) {
  1736. fprintf(stderr, "ERROR: %s:%02x:%02x.%x "
  1737. "Attempt to add PCI capability %x at offset "
  1738. "%x overlaps existing capability %x at offset %x\n",
  1739. pci_root_bus_path(pdev), pci_bus_num(pdev->bus),
  1740. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn),
  1741. cap_id, offset, overlapping_cap, i);
  1742. return -EINVAL;
  1743. }
  1744. }
  1745. }
  1746. config = pdev->config + offset;
  1747. config[PCI_CAP_LIST_ID] = cap_id;
  1748. config[PCI_CAP_LIST_NEXT] = pdev->config[PCI_CAPABILITY_LIST];
  1749. pdev->config[PCI_CAPABILITY_LIST] = offset;
  1750. pdev->config[PCI_STATUS] |= PCI_STATUS_CAP_LIST;
  1751. memset(pdev->used + offset, 0xFF, QEMU_ALIGN_UP(size, 4));
  1752. /* Make capability read-only by default */
  1753. memset(pdev->wmask + offset, 0, size);
  1754. /* Check capability by default */
  1755. memset(pdev->cmask + offset, 0xFF, size);
  1756. return offset;
  1757. }
  1758. /* Unlink capability from the pci config space. */
  1759. void pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
  1760. {
  1761. uint8_t prev, offset = pci_find_capability_list(pdev, cap_id, &prev);
  1762. if (!offset)
  1763. return;
  1764. pdev->config[prev] = pdev->config[offset + PCI_CAP_LIST_NEXT];
  1765. /* Make capability writable again */
  1766. memset(pdev->wmask + offset, 0xff, size);
  1767. memset(pdev->w1cmask + offset, 0, size);
  1768. /* Clear cmask as device-specific registers can't be checked */
  1769. memset(pdev->cmask + offset, 0, size);
  1770. memset(pdev->used + offset, 0, QEMU_ALIGN_UP(size, 4));
  1771. if (!pdev->config[PCI_CAPABILITY_LIST])
  1772. pdev->config[PCI_STATUS] &= ~PCI_STATUS_CAP_LIST;
  1773. }
  1774. uint8_t pci_find_capability(PCIDevice *pdev, uint8_t cap_id)
  1775. {
  1776. return pci_find_capability_list(pdev, cap_id, NULL);
  1777. }
  1778. static void pcibus_dev_print(Monitor *mon, DeviceState *dev, int indent)
  1779. {
  1780. PCIDevice *d = (PCIDevice *)dev;
  1781. const pci_class_desc *desc;
  1782. char ctxt[64];
  1783. PCIIORegion *r;
  1784. int i, class;
  1785. class = pci_get_word(d->config + PCI_CLASS_DEVICE);
  1786. desc = pci_class_descriptions;
  1787. while (desc->desc && class != desc->class)
  1788. desc++;
  1789. if (desc->desc) {
  1790. snprintf(ctxt, sizeof(ctxt), "%s", desc->desc);
  1791. } else {
  1792. snprintf(ctxt, sizeof(ctxt), "Class %04x", class);
  1793. }
  1794. monitor_printf(mon, "%*sclass %s, addr %02x:%02x.%x, "
  1795. "pci id %04x:%04x (sub %04x:%04x)\n",
  1796. indent, "", ctxt, pci_bus_num(d->bus),
  1797. PCI_SLOT(d->devfn), PCI_FUNC(d->devfn),
  1798. pci_get_word(d->config + PCI_VENDOR_ID),
  1799. pci_get_word(d->config + PCI_DEVICE_ID),
  1800. pci_get_word(d->config + PCI_SUBSYSTEM_VENDOR_ID),
  1801. pci_get_word(d->config + PCI_SUBSYSTEM_ID));
  1802. for (i = 0; i < PCI_NUM_REGIONS; i++) {
  1803. r = &d->io_regions[i];
  1804. if (!r->size)
  1805. continue;
  1806. monitor_printf(mon, "%*sbar %d: %s at 0x%"FMT_PCIBUS
  1807. " [0x%"FMT_PCIBUS"]\n",
  1808. indent, "",
  1809. i, r->type & PCI_BASE_ADDRESS_SPACE_IO ? "i/o" : "mem",
  1810. r->addr, r->addr + r->size - 1);
  1811. }
  1812. }
  1813. static char *pci_dev_fw_name(DeviceState *dev, char *buf, int len)
  1814. {
  1815. PCIDevice *d = (PCIDevice *)dev;
  1816. const char *name = NULL;
  1817. const pci_class_desc *desc = pci_class_descriptions;
  1818. int class = pci_get_word(d->config + PCI_CLASS_DEVICE);
  1819. while (desc->desc &&
  1820. (class & ~desc->fw_ign_bits) !=
  1821. (desc->class & ~desc->fw_ign_bits)) {
  1822. desc++;
  1823. }
  1824. if (desc->desc) {
  1825. name = desc->fw_name;
  1826. }
  1827. if (name) {
  1828. pstrcpy(buf, len, name);
  1829. } else {
  1830. snprintf(buf, len, "pci%04x,%04x",
  1831. pci_get_word(d->config + PCI_VENDOR_ID),
  1832. pci_get_word(d->config + PCI_DEVICE_ID));
  1833. }
  1834. return buf;
  1835. }
  1836. static char *pcibus_get_fw_dev_path(DeviceState *dev)
  1837. {
  1838. PCIDevice *d = (PCIDevice *)dev;
  1839. char path[50], name[33];
  1840. int off;
  1841. off = snprintf(path, sizeof(path), "%s@%x",
  1842. pci_dev_fw_name(dev, name, sizeof name),
  1843. PCI_SLOT(d->devfn));
  1844. if (PCI_FUNC(d->devfn))
  1845. snprintf(path + off, sizeof(path) + off, ",%x", PCI_FUNC(d->devfn));
  1846. return g_strdup(path);
  1847. }
  1848. static char *pcibus_get_dev_path(DeviceState *dev)
  1849. {
  1850. PCIDevice *d = container_of(dev, PCIDevice, qdev);
  1851. PCIDevice *t;
  1852. int slot_depth;
  1853. /* Path format: Domain:00:Slot.Function:Slot.Function....:Slot.Function.
  1854. * 00 is added here to make this format compatible with
  1855. * domain:Bus:Slot.Func for systems without nested PCI bridges.
  1856. * Slot.Function list specifies the slot and function numbers for all
  1857. * devices on the path from root to the specific device. */
  1858. const char *root_bus_path;
  1859. int root_bus_len;
  1860. char slot[] = ":SS.F";
  1861. int slot_len = sizeof slot - 1 /* For '\0' */;
  1862. int path_len;
  1863. char *path, *p;
  1864. int s;
  1865. root_bus_path = pci_root_bus_path(d);
  1866. root_bus_len = strlen(root_bus_path);
  1867. /* Calculate # of slots on path between device and root. */;
  1868. slot_depth = 0;
  1869. for (t = d; t; t = t->bus->parent_dev) {
  1870. ++slot_depth;
  1871. }
  1872. path_len = root_bus_len + slot_len * slot_depth;
  1873. /* Allocate memory, fill in the terminating null byte. */
  1874. path = g_malloc(path_len + 1 /* For '\0' */);
  1875. path[path_len] = '\0';
  1876. memcpy(path, root_bus_path, root_bus_len);
  1877. /* Fill in slot numbers. We walk up from device to root, so need to print
  1878. * them in the reverse order, last to first. */
  1879. p = path + path_len;
  1880. for (t = d; t; t = t->bus->parent_dev) {
  1881. p -= slot_len;
  1882. s = snprintf(slot, sizeof slot, ":%02x.%x",
  1883. PCI_SLOT(t->devfn), PCI_FUNC(t->devfn));
  1884. assert(s == slot_len);
  1885. memcpy(p, slot, slot_len);
  1886. }
  1887. return path;
  1888. }
  1889. static int pci_qdev_find_recursive(PCIBus *bus,
  1890. const char *id, PCIDevice **pdev)
  1891. {
  1892. DeviceState *qdev = qdev_find_recursive(&bus->qbus, id);
  1893. if (!qdev) {
  1894. return -ENODEV;
  1895. }
  1896. /* roughly check if given qdev is pci device */
  1897. if (object_dynamic_cast(OBJECT(qdev), TYPE_PCI_DEVICE)) {
  1898. *pdev = PCI_DEVICE(qdev);
  1899. return 0;
  1900. }
  1901. return -EINVAL;
  1902. }
  1903. int pci_qdev_find_device(const char *id, PCIDevice **pdev)
  1904. {
  1905. PCIHostState *host_bridge;
  1906. int rc = -ENODEV;
  1907. QLIST_FOREACH(host_bridge, &pci_host_bridges, next) {
  1908. int tmp = pci_qdev_find_recursive(host_bridge->bus, id, pdev);
  1909. if (!tmp) {
  1910. rc = 0;
  1911. break;
  1912. }
  1913. if (tmp != -ENODEV) {
  1914. rc = tmp;
  1915. }
  1916. }
  1917. return rc;
  1918. }
  1919. MemoryRegion *pci_address_space(PCIDevice *dev)
  1920. {
  1921. return dev->bus->address_space_mem;
  1922. }
  1923. MemoryRegion *pci_address_space_io(PCIDevice *dev)
  1924. {
  1925. return dev->bus->address_space_io;
  1926. }
  1927. static void pci_device_class_init(ObjectClass *klass, void *data)
  1928. {
  1929. DeviceClass *k = DEVICE_CLASS(klass);
  1930. k->init = pci_qdev_init;
  1931. k->exit = pci_unregister_device;
  1932. k->bus_type = TYPE_PCI_BUS;
  1933. k->props = pci_props;
  1934. }
  1935. AddressSpace *pci_device_iommu_address_space(PCIDevice *dev)
  1936. {
  1937. PCIBus *bus = PCI_BUS(dev->bus);
  1938. if (bus->iommu_fn) {
  1939. return bus->iommu_fn(bus, bus->iommu_opaque, dev->devfn);
  1940. }
  1941. if (bus->parent_dev) {
  1942. /** We are ignoring the bus master DMA bit of the bridge
  1943. * as it would complicate things such as VFIO for no good reason */
  1944. return pci_device_iommu_address_space(bus->parent_dev);
  1945. }
  1946. return &address_space_memory;
  1947. }
  1948. void pci_setup_iommu(PCIBus *bus, PCIIOMMUFunc fn, void *opaque)
  1949. {
  1950. bus->iommu_fn = fn;
  1951. bus->iommu_opaque = opaque;
  1952. }
  1953. static void pci_dev_get_w64(PCIBus *b, PCIDevice *dev, void *opaque)
  1954. {
  1955. Range *range = opaque;
  1956. PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
  1957. uint16_t cmd = pci_get_word(dev->config + PCI_COMMAND);
  1958. int i;
  1959. if (!(cmd & PCI_COMMAND_MEMORY)) {
  1960. return;
  1961. }
  1962. if (pc->is_bridge) {
  1963. pcibus_t base = pci_bridge_get_base(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
  1964. pcibus_t limit = pci_bridge_get_limit(dev, PCI_BASE_ADDRESS_MEM_PREFETCH);
  1965. base = MAX(base, 0x1ULL << 32);
  1966. if (limit >= base) {
  1967. Range pref_range;
  1968. pref_range.begin = base;
  1969. pref_range.end = limit + 1;
  1970. range_extend(range, &pref_range);
  1971. }
  1972. }
  1973. for (i = 0; i < PCI_NUM_REGIONS; ++i) {
  1974. PCIIORegion *r = &dev->io_regions[i];
  1975. Range region_range;
  1976. if (!r->size ||
  1977. (r->type & PCI_BASE_ADDRESS_SPACE_IO) ||
  1978. !(r->type & PCI_BASE_ADDRESS_MEM_TYPE_64)) {
  1979. continue;
  1980. }
  1981. region_range.begin = pci_bar_address(dev, i, r->type, r->size);
  1982. region_range.end = region_range.begin + r->size;
  1983. if (region_range.begin == PCI_BAR_UNMAPPED) {
  1984. continue;
  1985. }
  1986. region_range.begin = MAX(region_range.begin, 0x1ULL << 32);
  1987. if (region_range.end - 1 >= region_range.begin) {
  1988. range_extend(range, &region_range);
  1989. }
  1990. }
  1991. }
  1992. void pci_bus_get_w64_range(PCIBus *bus, Range *range)
  1993. {
  1994. range->begin = range->end = 0;
  1995. pci_for_each_device_under_bus(bus, pci_dev_get_w64, range);
  1996. }
  1997. static const TypeInfo pci_device_type_info = {
  1998. .name = TYPE_PCI_DEVICE,
  1999. .parent = TYPE_DEVICE,
  2000. .instance_size = sizeof(PCIDevice),
  2001. .abstract = true,
  2002. .class_size = sizeof(PCIDeviceClass),
  2003. .class_init = pci_device_class_init,
  2004. };
  2005. static void pci_register_types(void)
  2006. {
  2007. type_register_static(&pci_bus_info);
  2008. type_register_static(&pcie_bus_info);
  2009. type_register_static(&pci_device_type_info);
  2010. }
  2011. type_init(pci_register_types)