pci.c 64 KB

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