pci.c 62 KB

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