2
0

versatile.c 17 KB

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