pci.c 66 KB

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