2
0

versatile.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /*
  2. * ARM Versatile/PB PCI host controller
  3. *
  4. * Copyright (c) 2006-2009 CodeSourcery.
  5. * Written by Paul Brook
  6. *
  7. * This code is licensed under the LGPL.
  8. */
  9. #include "qemu/osdep.h"
  10. #include "hw/sysbus.h"
  11. #include "migration/vmstate.h"
  12. #include "hw/irq.h"
  13. #include "hw/pci/pci.h"
  14. #include "hw/pci/pci_bus.h"
  15. #include "hw/pci/pci_host.h"
  16. #include "hw/qdev-properties.h"
  17. #include "qemu/log.h"
  18. #include "qemu/module.h"
  19. /* Old and buggy versions of QEMU used the wrong mapping from
  20. * PCI IRQs to system interrupt lines. Unfortunately the Linux
  21. * kernel also had the corresponding bug in setting up interrupts
  22. * (so older kernels work on QEMU and not on real hardware).
  23. * We automatically detect these broken kernels and flip back
  24. * to the broken irq mapping by spotting guest writes to the
  25. * PCI_INTERRUPT_LINE register to see where the guest thinks
  26. * interrupts are going to be routed. So we start in state
  27. * ASSUME_OK on reset, and transition to either BROKEN or
  28. * FORCE_OK at the first write to an INTERRUPT_LINE register for
  29. * a slot where broken and correct interrupt mapping would differ.
  30. * Once in either BROKEN or FORCE_OK we never transition again;
  31. * this allows a newer kernel to use the INTERRUPT_LINE
  32. * registers arbitrarily once it has indicated that it isn't
  33. * broken in its init code somewhere.
  34. *
  35. * Unfortunately we have to cope with multiple different
  36. * variants on the broken kernel behaviour:
  37. * phase I (before kernel commit 1bc39ac5d) kernels assume old
  38. * QEMU behaviour, so they use IRQ 27 for all slots
  39. * phase II (1bc39ac5d and later, but before e3e92a7be6) kernels
  40. * swizzle IRQs between slots, but do it wrongly, so they
  41. * work only for every fourth PCI card, and only if (like old
  42. * QEMU) the PCI host device is at slot 0 rather than where
  43. * the h/w actually puts it
  44. * phase III (e3e92a7be6 and later) kernels still swizzle IRQs between
  45. * slots wrongly, but add a fixed offset of 64 to everything
  46. * they write to PCI_INTERRUPT_LINE.
  47. *
  48. * We live in hope of a mythical phase IV kernel which might
  49. * actually behave in ways that work on the hardware. Such a
  50. * kernel should probably start off by writing some value neither
  51. * 27 nor 91 to slot zero's PCI_INTERRUPT_LINE register to
  52. * disable the autodetection. After that it can do what it likes.
  53. *
  54. * Slot % 4 | hw | I | II | III
  55. * -------------------------------
  56. * 0 | 29 | 27 | 27 | 91
  57. * 1 | 30 | 27 | 28 | 92
  58. * 2 | 27 | 27 | 29 | 93
  59. * 3 | 28 | 27 | 30 | 94
  60. *
  61. * Since our autodetection is not perfect we also provide a
  62. * property so the user can make us start in BROKEN or FORCE_OK
  63. * on reset if they know they have a bad or good kernel.
  64. */
  65. enum {
  66. PCI_VPB_IRQMAP_ASSUME_OK,
  67. PCI_VPB_IRQMAP_BROKEN,
  68. PCI_VPB_IRQMAP_FORCE_OK,
  69. };
  70. typedef struct {
  71. PCIHostState parent_obj;
  72. qemu_irq irq[4];
  73. MemoryRegion controlregs;
  74. MemoryRegion mem_config;
  75. MemoryRegion mem_config2;
  76. /* Containers representing the PCI address spaces */
  77. MemoryRegion pci_io_space;
  78. MemoryRegion pci_mem_space;
  79. /* Alias regions into PCI address spaces which we expose as sysbus regions.
  80. * The offsets into pci_mem_space are controlled by the imap registers.
  81. */
  82. MemoryRegion pci_io_window;
  83. MemoryRegion pci_mem_window[3];
  84. PCIBus pci_bus;
  85. PCIDevice pci_dev;
  86. /* Constant for life of device: */
  87. int realview;
  88. uint32_t mem_win_size[3];
  89. uint8_t irq_mapping_prop;
  90. /* Variable state: */
  91. uint32_t imap[3];
  92. uint32_t smap[3];
  93. uint32_t selfid;
  94. uint32_t flags;
  95. uint8_t irq_mapping;
  96. } PCIVPBState;
  97. static void pci_vpb_update_window(PCIVPBState *s, int i)
  98. {
  99. /* Adjust the offset of the alias region we use for
  100. * the memory window i to account for a change in the
  101. * value of the corresponding IMAP register.
  102. * Note that the semantics of the IMAP register differ
  103. * for realview and versatile variants of the controller.
  104. */
  105. hwaddr offset;
  106. if (s->realview) {
  107. /* Top bits of register (masked according to window size) provide
  108. * top bits of PCI address.
  109. */
  110. offset = s->imap[i] & ~(s->mem_win_size[i] - 1);
  111. } else {
  112. /* Bottom 4 bits of register provide top 4 bits of PCI address */
  113. offset = s->imap[i] << 28;
  114. }
  115. memory_region_set_alias_offset(&s->pci_mem_window[i], offset);
  116. }
  117. static void pci_vpb_update_all_windows(PCIVPBState *s)
  118. {
  119. /* Update all alias windows based on the current register state */
  120. int i;
  121. for (i = 0; i < 3; i++) {
  122. pci_vpb_update_window(s, i);
  123. }
  124. }
  125. static int pci_vpb_post_load(void *opaque, int version_id)
  126. {
  127. PCIVPBState *s = opaque;
  128. pci_vpb_update_all_windows(s);
  129. return 0;
  130. }
  131. static const VMStateDescription pci_vpb_vmstate = {
  132. .name = "versatile-pci",
  133. .version_id = 1,
  134. .minimum_version_id = 1,
  135. .post_load = pci_vpb_post_load,
  136. .fields = (VMStateField[]) {
  137. VMSTATE_UINT32_ARRAY(imap, PCIVPBState, 3),
  138. VMSTATE_UINT32_ARRAY(smap, PCIVPBState, 3),
  139. VMSTATE_UINT32(selfid, PCIVPBState),
  140. VMSTATE_UINT32(flags, PCIVPBState),
  141. VMSTATE_UINT8(irq_mapping, PCIVPBState),
  142. VMSTATE_END_OF_LIST()
  143. }
  144. };
  145. #define TYPE_VERSATILE_PCI "versatile_pci"
  146. #define PCI_VPB(obj) \
  147. OBJECT_CHECK(PCIVPBState, (obj), TYPE_VERSATILE_PCI)
  148. #define TYPE_VERSATILE_PCI_HOST "versatile_pci_host"
  149. #define PCI_VPB_HOST(obj) \
  150. OBJECT_CHECK(PCIDevice, (obj), TYPE_VERSATILE_PCIHOST)
  151. typedef enum {
  152. PCI_IMAP0 = 0x0,
  153. PCI_IMAP1 = 0x4,
  154. PCI_IMAP2 = 0x8,
  155. PCI_SELFID = 0xc,
  156. PCI_FLAGS = 0x10,
  157. PCI_SMAP0 = 0x14,
  158. PCI_SMAP1 = 0x18,
  159. PCI_SMAP2 = 0x1c,
  160. } PCIVPBControlRegs;
  161. static void pci_vpb_reg_write(void *opaque, hwaddr addr,
  162. uint64_t val, unsigned size)
  163. {
  164. PCIVPBState *s = opaque;
  165. switch (addr) {
  166. case PCI_IMAP0:
  167. case PCI_IMAP1:
  168. case PCI_IMAP2:
  169. {
  170. int win = (addr - PCI_IMAP0) >> 2;
  171. s->imap[win] = val;
  172. pci_vpb_update_window(s, win);
  173. break;
  174. }
  175. case PCI_SELFID:
  176. s->selfid = val;
  177. break;
  178. case PCI_FLAGS:
  179. s->flags = val;
  180. break;
  181. case PCI_SMAP0:
  182. case PCI_SMAP1:
  183. case PCI_SMAP2:
  184. {
  185. int win = (addr - PCI_SMAP0) >> 2;
  186. s->smap[win] = val;
  187. break;
  188. }
  189. default:
  190. qemu_log_mask(LOG_GUEST_ERROR,
  191. "pci_vpb_reg_write: Bad offset %x\n", (int)addr);
  192. break;
  193. }
  194. }
  195. static uint64_t pci_vpb_reg_read(void *opaque, hwaddr addr,
  196. unsigned size)
  197. {
  198. PCIVPBState *s = opaque;
  199. switch (addr) {
  200. case PCI_IMAP0:
  201. case PCI_IMAP1:
  202. case PCI_IMAP2:
  203. {
  204. int win = (addr - PCI_IMAP0) >> 2;
  205. return s->imap[win];
  206. }
  207. case PCI_SELFID:
  208. return s->selfid;
  209. case PCI_FLAGS:
  210. return s->flags;
  211. case PCI_SMAP0:
  212. case PCI_SMAP1:
  213. case PCI_SMAP2:
  214. {
  215. int win = (addr - PCI_SMAP0) >> 2;
  216. return s->smap[win];
  217. }
  218. default:
  219. qemu_log_mask(LOG_GUEST_ERROR,
  220. "pci_vpb_reg_read: Bad offset %x\n", (int)addr);
  221. return 0;
  222. }
  223. }
  224. static const MemoryRegionOps pci_vpb_reg_ops = {
  225. .read = pci_vpb_reg_read,
  226. .write = pci_vpb_reg_write,
  227. .endianness = DEVICE_NATIVE_ENDIAN,
  228. .valid = {
  229. .min_access_size = 4,
  230. .max_access_size = 4,
  231. },
  232. };
  233. static int pci_vpb_broken_irq(int slot, int irq)
  234. {
  235. /* Determine whether this IRQ value for this slot represents a
  236. * known broken Linux kernel behaviour for this slot.
  237. * Return one of the PCI_VPB_IRQMAP_ constants:
  238. * BROKEN : if this definitely looks like a broken kernel
  239. * FORCE_OK : if this definitely looks good
  240. * ASSUME_OK : if we can't tell
  241. */
  242. slot %= PCI_NUM_PINS;
  243. if (irq == 27) {
  244. if (slot == 2) {
  245. /* Might be a Phase I kernel, or might be a fixed kernel,
  246. * since slot 2 is where we expect this IRQ.
  247. */
  248. return PCI_VPB_IRQMAP_ASSUME_OK;
  249. }
  250. /* Phase I kernel */
  251. return PCI_VPB_IRQMAP_BROKEN;
  252. }
  253. if (irq == slot + 27) {
  254. /* Phase II kernel */
  255. return PCI_VPB_IRQMAP_BROKEN;
  256. }
  257. if (irq == slot + 27 + 64) {
  258. /* Phase III kernel */
  259. return PCI_VPB_IRQMAP_BROKEN;
  260. }
  261. /* Anything else must be a fixed kernel, possibly using an
  262. * arbitrary irq map.
  263. */
  264. return PCI_VPB_IRQMAP_FORCE_OK;
  265. }
  266. static void pci_vpb_config_write(void *opaque, hwaddr addr,
  267. uint64_t val, unsigned size)
  268. {
  269. PCIVPBState *s = opaque;
  270. if (!s->realview && (addr & 0xff) == PCI_INTERRUPT_LINE
  271. && s->irq_mapping == PCI_VPB_IRQMAP_ASSUME_OK) {
  272. uint8_t devfn = addr >> 8;
  273. s->irq_mapping = pci_vpb_broken_irq(PCI_SLOT(devfn), val);
  274. }
  275. pci_data_write(&s->pci_bus, addr, val, size);
  276. }
  277. static uint64_t pci_vpb_config_read(void *opaque, hwaddr addr,
  278. unsigned size)
  279. {
  280. PCIVPBState *s = opaque;
  281. uint32_t val;
  282. val = pci_data_read(&s->pci_bus, addr, size);
  283. return val;
  284. }
  285. static const MemoryRegionOps pci_vpb_config_ops = {
  286. .read = pci_vpb_config_read,
  287. .write = pci_vpb_config_write,
  288. .endianness = DEVICE_NATIVE_ENDIAN,
  289. };
  290. static int pci_vpb_map_irq(PCIDevice *d, int irq_num)
  291. {
  292. PCIVPBState *s = container_of(pci_get_bus(d), PCIVPBState, pci_bus);
  293. if (s->irq_mapping == PCI_VPB_IRQMAP_BROKEN) {
  294. /* Legacy broken IRQ mapping for compatibility with old and
  295. * buggy Linux guests
  296. */
  297. return irq_num;
  298. }
  299. /* Slot to IRQ mapping for RealView Platform Baseboard 926 backplane
  300. * name slot IntA IntB IntC IntD
  301. * A 31 IRQ28 IRQ29 IRQ30 IRQ27
  302. * B 30 IRQ27 IRQ28 IRQ29 IRQ30
  303. * C 29 IRQ30 IRQ27 IRQ28 IRQ29
  304. * Slot C is for the host bridge; A and B the peripherals.
  305. * Our output irqs 0..3 correspond to the baseboard's 27..30.
  306. *
  307. * This mapping function takes account of an oddity in the PB926
  308. * board wiring, where the FPGA's P_nINTA input is connected to
  309. * the INTB connection on the board PCI edge connector, P_nINTB
  310. * is connected to INTC, and so on, so everything is one number
  311. * further round from where you might expect.
  312. */
  313. return pci_swizzle_map_irq_fn(d, irq_num + 2);
  314. }
  315. static int pci_vpb_rv_map_irq(PCIDevice *d, int irq_num)
  316. {
  317. /* Slot to IRQ mapping for RealView EB and PB1176 backplane
  318. * name slot IntA IntB IntC IntD
  319. * A 31 IRQ50 IRQ51 IRQ48 IRQ49
  320. * B 30 IRQ49 IRQ50 IRQ51 IRQ48
  321. * C 29 IRQ48 IRQ49 IRQ50 IRQ51
  322. * Slot C is for the host bridge; A and B the peripherals.
  323. * Our output irqs 0..3 correspond to the baseboard's 48..51.
  324. *
  325. * The PB1176 and EB boards don't have the PB926 wiring oddity
  326. * described above; P_nINTA connects to INTA, P_nINTB to INTB
  327. * and so on, which is why this mapping function is different.
  328. */
  329. return pci_swizzle_map_irq_fn(d, irq_num + 3);
  330. }
  331. static void pci_vpb_set_irq(void *opaque, int irq_num, int level)
  332. {
  333. qemu_irq *pic = opaque;
  334. qemu_set_irq(pic[irq_num], level);
  335. }
  336. static void pci_vpb_reset(DeviceState *d)
  337. {
  338. PCIVPBState *s = PCI_VPB(d);
  339. s->imap[0] = 0;
  340. s->imap[1] = 0;
  341. s->imap[2] = 0;
  342. s->smap[0] = 0;
  343. s->smap[1] = 0;
  344. s->smap[2] = 0;
  345. s->selfid = 0;
  346. s->flags = 0;
  347. s->irq_mapping = s->irq_mapping_prop;
  348. pci_vpb_update_all_windows(s);
  349. }
  350. static void pci_vpb_init(Object *obj)
  351. {
  352. PCIVPBState *s = PCI_VPB(obj);
  353. /* Window sizes for VersatilePB; realview_pci's init will override */
  354. s->mem_win_size[0] = 0x0c000000;
  355. s->mem_win_size[1] = 0x10000000;
  356. s->mem_win_size[2] = 0x10000000;
  357. }
  358. static void pci_vpb_realize(DeviceState *dev, Error **errp)
  359. {
  360. PCIVPBState *s = PCI_VPB(dev);
  361. PCIHostState *h = PCI_HOST_BRIDGE(dev);
  362. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  363. pci_map_irq_fn mapfn;
  364. int i;
  365. memory_region_init(&s->pci_io_space, OBJECT(s), "pci_io", 1ULL << 32);
  366. memory_region_init(&s->pci_mem_space, OBJECT(s), "pci_mem", 1ULL << 32);
  367. pci_root_bus_new_inplace(&s->pci_bus, sizeof(s->pci_bus), dev, "pci",
  368. &s->pci_mem_space, &s->pci_io_space,
  369. PCI_DEVFN(11, 0), TYPE_PCI_BUS);
  370. h->bus = &s->pci_bus;
  371. object_initialize(&s->pci_dev, sizeof(s->pci_dev), TYPE_VERSATILE_PCI_HOST);
  372. qdev_set_parent_bus(DEVICE(&s->pci_dev), BUS(&s->pci_bus));
  373. for (i = 0; i < 4; i++) {
  374. sysbus_init_irq(sbd, &s->irq[i]);
  375. }
  376. if (s->realview) {
  377. mapfn = pci_vpb_rv_map_irq;
  378. } else {
  379. mapfn = pci_vpb_map_irq;
  380. }
  381. pci_bus_irqs(&s->pci_bus, pci_vpb_set_irq, mapfn, s->irq, 4);
  382. /* Our memory regions are:
  383. * 0 : our control registers
  384. * 1 : PCI self config window
  385. * 2 : PCI config window
  386. * 3 : PCI IO window
  387. * 4..6 : PCI memory windows
  388. */
  389. memory_region_init_io(&s->controlregs, OBJECT(s), &pci_vpb_reg_ops, s,
  390. "pci-vpb-regs", 0x1000);
  391. sysbus_init_mmio(sbd, &s->controlregs);
  392. memory_region_init_io(&s->mem_config, OBJECT(s), &pci_vpb_config_ops, s,
  393. "pci-vpb-selfconfig", 0x1000000);
  394. sysbus_init_mmio(sbd, &s->mem_config);
  395. memory_region_init_io(&s->mem_config2, OBJECT(s), &pci_vpb_config_ops, s,
  396. "pci-vpb-config", 0x1000000);
  397. sysbus_init_mmio(sbd, &s->mem_config2);
  398. /* The window into I/O space is always into a fixed base address;
  399. * its size is the same for both realview and versatile.
  400. */
  401. memory_region_init_alias(&s->pci_io_window, OBJECT(s), "pci-vbp-io-window",
  402. &s->pci_io_space, 0, 0x100000);
  403. sysbus_init_mmio(sbd, &s->pci_io_space);
  404. /* Create the alias regions corresponding to our three windows onto
  405. * PCI memory space. The sizes vary from board to board; the base
  406. * offsets are guest controllable via the IMAP registers.
  407. */
  408. for (i = 0; i < 3; i++) {
  409. memory_region_init_alias(&s->pci_mem_window[i], OBJECT(s), "pci-vbp-window",
  410. &s->pci_mem_space, 0, s->mem_win_size[i]);
  411. sysbus_init_mmio(sbd, &s->pci_mem_window[i]);
  412. }
  413. /* TODO Remove once realize propagates to child devices. */
  414. object_property_set_bool(OBJECT(&s->pci_bus), true, "realized", errp);
  415. object_property_set_bool(OBJECT(&s->pci_dev), true, "realized", errp);
  416. }
  417. static void versatile_pci_host_realize(PCIDevice *d, Error **errp)
  418. {
  419. pci_set_word(d->config + PCI_STATUS,
  420. PCI_STATUS_66MHZ | PCI_STATUS_DEVSEL_MEDIUM);
  421. pci_set_byte(d->config + PCI_LATENCY_TIMER, 0x10);
  422. }
  423. static void versatile_pci_host_class_init(ObjectClass *klass, void *data)
  424. {
  425. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  426. DeviceClass *dc = DEVICE_CLASS(klass);
  427. k->realize = versatile_pci_host_realize;
  428. k->vendor_id = PCI_VENDOR_ID_XILINX;
  429. k->device_id = PCI_DEVICE_ID_XILINX_XC2VP30;
  430. k->class_id = PCI_CLASS_PROCESSOR_CO;
  431. /*
  432. * PCI-facing part of the host bridge, not usable without the
  433. * host-facing part, which can't be device_add'ed, yet.
  434. */
  435. dc->user_creatable = false;
  436. }
  437. static const TypeInfo versatile_pci_host_info = {
  438. .name = TYPE_VERSATILE_PCI_HOST,
  439. .parent = TYPE_PCI_DEVICE,
  440. .instance_size = sizeof(PCIDevice),
  441. .class_init = versatile_pci_host_class_init,
  442. .interfaces = (InterfaceInfo[]) {
  443. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  444. { },
  445. },
  446. };
  447. static Property pci_vpb_properties[] = {
  448. DEFINE_PROP_UINT8("broken-irq-mapping", PCIVPBState, irq_mapping_prop,
  449. PCI_VPB_IRQMAP_ASSUME_OK),
  450. DEFINE_PROP_END_OF_LIST()
  451. };
  452. static void pci_vpb_class_init(ObjectClass *klass, void *data)
  453. {
  454. DeviceClass *dc = DEVICE_CLASS(klass);
  455. dc->realize = pci_vpb_realize;
  456. dc->reset = pci_vpb_reset;
  457. dc->vmsd = &pci_vpb_vmstate;
  458. dc->props = pci_vpb_properties;
  459. }
  460. static const TypeInfo pci_vpb_info = {
  461. .name = TYPE_VERSATILE_PCI,
  462. .parent = TYPE_PCI_HOST_BRIDGE,
  463. .instance_size = sizeof(PCIVPBState),
  464. .instance_init = pci_vpb_init,
  465. .class_init = pci_vpb_class_init,
  466. };
  467. static void pci_realview_init(Object *obj)
  468. {
  469. PCIVPBState *s = PCI_VPB(obj);
  470. s->realview = 1;
  471. /* The PCI window sizes are different on Realview boards */
  472. s->mem_win_size[0] = 0x01000000;
  473. s->mem_win_size[1] = 0x04000000;
  474. s->mem_win_size[2] = 0x08000000;
  475. }
  476. static const TypeInfo pci_realview_info = {
  477. .name = "realview_pci",
  478. .parent = TYPE_VERSATILE_PCI,
  479. .instance_init = pci_realview_init,
  480. };
  481. static void versatile_pci_register_types(void)
  482. {
  483. type_register_static(&pci_vpb_info);
  484. type_register_static(&pci_realview_info);
  485. type_register_static(&versatile_pci_host_info);
  486. }
  487. type_init(versatile_pci_register_types)