versatile.c 16 KB

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