versatile.c 17 KB

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