pci.c 65 KB

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