pci.c 60 KB

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