ppce500.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. * QEMU PowerPC E500 embedded processors pci controller emulation
  3. *
  4. * Copyright (C) 2009 Freescale Semiconductor, Inc. All rights reserved.
  5. *
  6. * Author: Yu Liu, <yu.liu@freescale.com>
  7. *
  8. * This file is derived from hw/ppc4xx_pci.c,
  9. * the copyright for that material belongs to the original owners.
  10. *
  11. * This is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. */
  16. #include "qemu/osdep.h"
  17. #include "hw/irq.h"
  18. #include "hw/ppc/e500-ccsr.h"
  19. #include "hw/qdev-properties.h"
  20. #include "migration/vmstate.h"
  21. #include "hw/pci/pci.h"
  22. #include "hw/pci/pci_host.h"
  23. #include "qemu/bswap.h"
  24. #include "qemu/module.h"
  25. #include "hw/pci-host/ppce500.h"
  26. #ifdef DEBUG_PCI
  27. #define pci_debug(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__)
  28. #else
  29. #define pci_debug(fmt, ...)
  30. #endif
  31. #define PCIE500_CFGADDR 0x0
  32. #define PCIE500_CFGDATA 0x4
  33. #define PCIE500_REG_BASE 0xC00
  34. #define PCIE500_ALL_SIZE 0x1000
  35. #define PCIE500_REG_SIZE (PCIE500_ALL_SIZE - PCIE500_REG_BASE)
  36. #define PCIE500_PCI_IOLEN 0x10000ULL
  37. #define PPCE500_PCI_CONFIG_ADDR 0x0
  38. #define PPCE500_PCI_CONFIG_DATA 0x4
  39. #define PPCE500_PCI_INTACK 0x8
  40. #define PPCE500_PCI_OW1 (0xC20 - PCIE500_REG_BASE)
  41. #define PPCE500_PCI_OW2 (0xC40 - PCIE500_REG_BASE)
  42. #define PPCE500_PCI_OW3 (0xC60 - PCIE500_REG_BASE)
  43. #define PPCE500_PCI_OW4 (0xC80 - PCIE500_REG_BASE)
  44. #define PPCE500_PCI_IW3 (0xDA0 - PCIE500_REG_BASE)
  45. #define PPCE500_PCI_IW2 (0xDC0 - PCIE500_REG_BASE)
  46. #define PPCE500_PCI_IW1 (0xDE0 - PCIE500_REG_BASE)
  47. #define PPCE500_PCI_GASKET_TIMR (0xE20 - PCIE500_REG_BASE)
  48. #define PCI_POTAR 0x0
  49. #define PCI_POTEAR 0x4
  50. #define PCI_POWBAR 0x8
  51. #define PCI_POWAR 0x10
  52. #define PCI_PITAR 0x0
  53. #define PCI_PIWBAR 0x8
  54. #define PCI_PIWBEAR 0xC
  55. #define PCI_PIWAR 0x10
  56. #define PPCE500_PCI_NR_POBS 5
  57. #define PPCE500_PCI_NR_PIBS 3
  58. #define PIWAR_EN 0x80000000 /* Enable */
  59. #define PIWAR_PF 0x20000000 /* prefetch */
  60. #define PIWAR_TGI_LOCAL 0x00f00000 /* target - local memory */
  61. #define PIWAR_READ_SNOOP 0x00050000
  62. #define PIWAR_WRITE_SNOOP 0x00005000
  63. #define PIWAR_SZ_MASK 0x0000003f
  64. struct pci_outbound {
  65. uint32_t potar;
  66. uint32_t potear;
  67. uint32_t powbar;
  68. uint32_t powar;
  69. MemoryRegion mem;
  70. };
  71. struct pci_inbound {
  72. uint32_t pitar;
  73. uint32_t piwbar;
  74. uint32_t piwbear;
  75. uint32_t piwar;
  76. MemoryRegion mem;
  77. };
  78. #define TYPE_PPC_E500_PCI_HOST_BRIDGE "e500-pcihost"
  79. #define PPC_E500_PCI_HOST_BRIDGE(obj) \
  80. OBJECT_CHECK(PPCE500PCIState, (obj), TYPE_PPC_E500_PCI_HOST_BRIDGE)
  81. struct PPCE500PCIState {
  82. PCIHostState parent_obj;
  83. struct pci_outbound pob[PPCE500_PCI_NR_POBS];
  84. struct pci_inbound pib[PPCE500_PCI_NR_PIBS];
  85. uint32_t gasket_time;
  86. qemu_irq irq[PCI_NUM_PINS];
  87. uint32_t irq_num[PCI_NUM_PINS];
  88. uint32_t first_slot;
  89. uint32_t first_pin_irq;
  90. AddressSpace bm_as;
  91. MemoryRegion bm;
  92. /* mmio maps */
  93. MemoryRegion container;
  94. MemoryRegion iomem;
  95. MemoryRegion pio;
  96. MemoryRegion busmem;
  97. };
  98. #define TYPE_PPC_E500_PCI_BRIDGE "e500-host-bridge"
  99. #define PPC_E500_PCI_BRIDGE(obj) \
  100. OBJECT_CHECK(PPCE500PCIBridgeState, (obj), TYPE_PPC_E500_PCI_BRIDGE)
  101. struct PPCE500PCIBridgeState {
  102. /*< private >*/
  103. PCIDevice parent;
  104. /*< public >*/
  105. MemoryRegion bar0;
  106. };
  107. typedef struct PPCE500PCIBridgeState PPCE500PCIBridgeState;
  108. typedef struct PPCE500PCIState PPCE500PCIState;
  109. static uint64_t pci_reg_read4(void *opaque, hwaddr addr,
  110. unsigned size)
  111. {
  112. PPCE500PCIState *pci = opaque;
  113. unsigned long win;
  114. uint32_t value = 0;
  115. int idx;
  116. win = addr & 0xfe0;
  117. switch (win) {
  118. case PPCE500_PCI_OW1:
  119. case PPCE500_PCI_OW2:
  120. case PPCE500_PCI_OW3:
  121. case PPCE500_PCI_OW4:
  122. idx = (addr >> 5) & 0x7;
  123. switch (addr & 0x1F) {
  124. case PCI_POTAR:
  125. value = pci->pob[idx].potar;
  126. break;
  127. case PCI_POTEAR:
  128. value = pci->pob[idx].potear;
  129. break;
  130. case PCI_POWBAR:
  131. value = pci->pob[idx].powbar;
  132. break;
  133. case PCI_POWAR:
  134. value = pci->pob[idx].powar;
  135. break;
  136. default:
  137. break;
  138. }
  139. break;
  140. case PPCE500_PCI_IW3:
  141. case PPCE500_PCI_IW2:
  142. case PPCE500_PCI_IW1:
  143. idx = ((addr >> 5) & 0x3) - 1;
  144. switch (addr & 0x1F) {
  145. case PCI_PITAR:
  146. value = pci->pib[idx].pitar;
  147. break;
  148. case PCI_PIWBAR:
  149. value = pci->pib[idx].piwbar;
  150. break;
  151. case PCI_PIWBEAR:
  152. value = pci->pib[idx].piwbear;
  153. break;
  154. case PCI_PIWAR:
  155. value = pci->pib[idx].piwar;
  156. break;
  157. default:
  158. break;
  159. };
  160. break;
  161. case PPCE500_PCI_GASKET_TIMR:
  162. value = pci->gasket_time;
  163. break;
  164. default:
  165. break;
  166. }
  167. pci_debug("%s: win:%lx(addr:" TARGET_FMT_plx ") -> value:%x\n", __func__,
  168. win, addr, value);
  169. return value;
  170. }
  171. /* DMA mapping */
  172. static void e500_update_piw(PPCE500PCIState *pci, int idx)
  173. {
  174. uint64_t tar = ((uint64_t)pci->pib[idx].pitar) << 12;
  175. uint64_t wbar = ((uint64_t)pci->pib[idx].piwbar) << 12;
  176. uint64_t war = pci->pib[idx].piwar;
  177. uint64_t size = 2ULL << (war & PIWAR_SZ_MASK);
  178. MemoryRegion *address_space_mem = get_system_memory();
  179. MemoryRegion *mem = &pci->pib[idx].mem;
  180. MemoryRegion *bm = &pci->bm;
  181. char *name;
  182. if (memory_region_is_mapped(mem)) {
  183. /* Before we modify anything, unmap and destroy the region */
  184. memory_region_del_subregion(bm, mem);
  185. object_unparent(OBJECT(mem));
  186. }
  187. if (!(war & PIWAR_EN)) {
  188. /* Not enabled, nothing to do */
  189. return;
  190. }
  191. name = g_strdup_printf("PCI Inbound Window %d", idx);
  192. memory_region_init_alias(mem, OBJECT(pci), name, address_space_mem, tar,
  193. size);
  194. memory_region_add_subregion_overlap(bm, wbar, mem, -1);
  195. g_free(name);
  196. pci_debug("%s: Added window of size=%#lx from PCI=%#lx to CPU=%#lx\n",
  197. __func__, size, wbar, tar);
  198. }
  199. /* BAR mapping */
  200. static void e500_update_pow(PPCE500PCIState *pci, int idx)
  201. {
  202. uint64_t tar = ((uint64_t)pci->pob[idx].potar) << 12;
  203. uint64_t wbar = ((uint64_t)pci->pob[idx].powbar) << 12;
  204. uint64_t war = pci->pob[idx].powar;
  205. uint64_t size = 2ULL << (war & PIWAR_SZ_MASK);
  206. MemoryRegion *mem = &pci->pob[idx].mem;
  207. MemoryRegion *address_space_mem = get_system_memory();
  208. char *name;
  209. if (memory_region_is_mapped(mem)) {
  210. /* Before we modify anything, unmap and destroy the region */
  211. memory_region_del_subregion(address_space_mem, mem);
  212. object_unparent(OBJECT(mem));
  213. }
  214. if (!(war & PIWAR_EN)) {
  215. /* Not enabled, nothing to do */
  216. return;
  217. }
  218. name = g_strdup_printf("PCI Outbound Window %d", idx);
  219. memory_region_init_alias(mem, OBJECT(pci), name, &pci->busmem, tar,
  220. size);
  221. memory_region_add_subregion(address_space_mem, wbar, mem);
  222. g_free(name);
  223. pci_debug("%s: Added window of size=%#lx from CPU=%#lx to PCI=%#lx\n",
  224. __func__, size, wbar, tar);
  225. }
  226. static void pci_reg_write4(void *opaque, hwaddr addr,
  227. uint64_t value, unsigned size)
  228. {
  229. PPCE500PCIState *pci = opaque;
  230. unsigned long win;
  231. int idx;
  232. win = addr & 0xfe0;
  233. pci_debug("%s: value:%x -> win:%lx(addr:" TARGET_FMT_plx ")\n",
  234. __func__, (unsigned)value, win, addr);
  235. switch (win) {
  236. case PPCE500_PCI_OW1:
  237. case PPCE500_PCI_OW2:
  238. case PPCE500_PCI_OW3:
  239. case PPCE500_PCI_OW4:
  240. idx = (addr >> 5) & 0x7;
  241. switch (addr & 0x1F) {
  242. case PCI_POTAR:
  243. pci->pob[idx].potar = value;
  244. e500_update_pow(pci, idx);
  245. break;
  246. case PCI_POTEAR:
  247. pci->pob[idx].potear = value;
  248. e500_update_pow(pci, idx);
  249. break;
  250. case PCI_POWBAR:
  251. pci->pob[idx].powbar = value;
  252. e500_update_pow(pci, idx);
  253. break;
  254. case PCI_POWAR:
  255. pci->pob[idx].powar = value;
  256. e500_update_pow(pci, idx);
  257. break;
  258. default:
  259. break;
  260. };
  261. break;
  262. case PPCE500_PCI_IW3:
  263. case PPCE500_PCI_IW2:
  264. case PPCE500_PCI_IW1:
  265. idx = ((addr >> 5) & 0x3) - 1;
  266. switch (addr & 0x1F) {
  267. case PCI_PITAR:
  268. pci->pib[idx].pitar = value;
  269. e500_update_piw(pci, idx);
  270. break;
  271. case PCI_PIWBAR:
  272. pci->pib[idx].piwbar = value;
  273. e500_update_piw(pci, idx);
  274. break;
  275. case PCI_PIWBEAR:
  276. pci->pib[idx].piwbear = value;
  277. e500_update_piw(pci, idx);
  278. break;
  279. case PCI_PIWAR:
  280. pci->pib[idx].piwar = value;
  281. e500_update_piw(pci, idx);
  282. break;
  283. default:
  284. break;
  285. };
  286. break;
  287. case PPCE500_PCI_GASKET_TIMR:
  288. pci->gasket_time = value;
  289. break;
  290. default:
  291. break;
  292. };
  293. }
  294. static const MemoryRegionOps e500_pci_reg_ops = {
  295. .read = pci_reg_read4,
  296. .write = pci_reg_write4,
  297. .endianness = DEVICE_BIG_ENDIAN,
  298. };
  299. static int mpc85xx_pci_map_irq(PCIDevice *pci_dev, int pin)
  300. {
  301. int devno = pci_dev->devfn >> 3;
  302. int ret;
  303. ret = ppce500_pci_map_irq_slot(devno, pin);
  304. pci_debug("%s: devfn %x irq %d -> %d devno:%x\n", __func__,
  305. pci_dev->devfn, pin, ret, devno);
  306. return ret;
  307. }
  308. static void mpc85xx_pci_set_irq(void *opaque, int pin, int level)
  309. {
  310. PPCE500PCIState *s = opaque;
  311. qemu_irq *pic = s->irq;
  312. pci_debug("%s: PCI irq %d, level:%d\n", __func__, pin , level);
  313. qemu_set_irq(pic[pin], level);
  314. }
  315. static PCIINTxRoute e500_route_intx_pin_to_irq(void *opaque, int pin)
  316. {
  317. PCIINTxRoute route;
  318. PPCE500PCIState *s = opaque;
  319. route.mode = PCI_INTX_ENABLED;
  320. route.irq = s->irq_num[pin];
  321. pci_debug("%s: PCI irq-pin = %d, irq_num= %d\n", __func__, pin, route.irq);
  322. return route;
  323. }
  324. static const VMStateDescription vmstate_pci_outbound = {
  325. .name = "pci_outbound",
  326. .version_id = 0,
  327. .minimum_version_id = 0,
  328. .fields = (VMStateField[]) {
  329. VMSTATE_UINT32(potar, struct pci_outbound),
  330. VMSTATE_UINT32(potear, struct pci_outbound),
  331. VMSTATE_UINT32(powbar, struct pci_outbound),
  332. VMSTATE_UINT32(powar, struct pci_outbound),
  333. VMSTATE_END_OF_LIST()
  334. }
  335. };
  336. static const VMStateDescription vmstate_pci_inbound = {
  337. .name = "pci_inbound",
  338. .version_id = 0,
  339. .minimum_version_id = 0,
  340. .fields = (VMStateField[]) {
  341. VMSTATE_UINT32(pitar, struct pci_inbound),
  342. VMSTATE_UINT32(piwbar, struct pci_inbound),
  343. VMSTATE_UINT32(piwbear, struct pci_inbound),
  344. VMSTATE_UINT32(piwar, struct pci_inbound),
  345. VMSTATE_END_OF_LIST()
  346. }
  347. };
  348. static const VMStateDescription vmstate_ppce500_pci = {
  349. .name = "ppce500_pci",
  350. .version_id = 1,
  351. .minimum_version_id = 1,
  352. .fields = (VMStateField[]) {
  353. VMSTATE_STRUCT_ARRAY(pob, PPCE500PCIState, PPCE500_PCI_NR_POBS, 1,
  354. vmstate_pci_outbound, struct pci_outbound),
  355. VMSTATE_STRUCT_ARRAY(pib, PPCE500PCIState, PPCE500_PCI_NR_PIBS, 1,
  356. vmstate_pci_inbound, struct pci_inbound),
  357. VMSTATE_UINT32(gasket_time, PPCE500PCIState),
  358. VMSTATE_END_OF_LIST()
  359. }
  360. };
  361. #include "exec/address-spaces.h"
  362. static void e500_pcihost_bridge_realize(PCIDevice *d, Error **errp)
  363. {
  364. PPCE500PCIBridgeState *b = PPC_E500_PCI_BRIDGE(d);
  365. PPCE500CCSRState *ccsr = CCSR(container_get(qdev_get_machine(),
  366. "/e500-ccsr"));
  367. memory_region_init_alias(&b->bar0, OBJECT(ccsr), "e500-pci-bar0", &ccsr->ccsr_space,
  368. 0, int128_get64(ccsr->ccsr_space.size));
  369. pci_register_bar(d, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &b->bar0);
  370. }
  371. static AddressSpace *e500_pcihost_set_iommu(PCIBus *bus, void *opaque,
  372. int devfn)
  373. {
  374. PPCE500PCIState *s = opaque;
  375. return &s->bm_as;
  376. }
  377. static void e500_pcihost_realize(DeviceState *dev, Error **errp)
  378. {
  379. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  380. PCIHostState *h;
  381. PPCE500PCIState *s;
  382. PCIBus *b;
  383. int i;
  384. h = PCI_HOST_BRIDGE(dev);
  385. s = PPC_E500_PCI_HOST_BRIDGE(dev);
  386. for (i = 0; i < ARRAY_SIZE(s->irq); i++) {
  387. sysbus_init_irq(sbd, &s->irq[i]);
  388. }
  389. for (i = 0; i < PCI_NUM_PINS; i++) {
  390. s->irq_num[i] = s->first_pin_irq + i;
  391. }
  392. memory_region_init(&s->pio, OBJECT(s), "pci-pio", PCIE500_PCI_IOLEN);
  393. memory_region_init(&s->busmem, OBJECT(s), "pci bus memory", UINT64_MAX);
  394. /* PIO lives at the bottom of our bus space */
  395. memory_region_add_subregion_overlap(&s->busmem, 0, &s->pio, -2);
  396. b = pci_register_root_bus(dev, NULL, mpc85xx_pci_set_irq,
  397. mpc85xx_pci_map_irq, s, &s->busmem, &s->pio,
  398. PCI_DEVFN(s->first_slot, 0), 4, TYPE_PCI_BUS);
  399. h->bus = b;
  400. /* Set up PCI view of memory */
  401. memory_region_init(&s->bm, OBJECT(s), "bm-e500", UINT64_MAX);
  402. memory_region_add_subregion(&s->bm, 0x0, &s->busmem);
  403. address_space_init(&s->bm_as, &s->bm, "pci-bm");
  404. pci_setup_iommu(b, e500_pcihost_set_iommu, s);
  405. pci_create_simple(b, 0, "e500-host-bridge");
  406. memory_region_init(&s->container, OBJECT(h), "pci-container", PCIE500_ALL_SIZE);
  407. memory_region_init_io(&h->conf_mem, OBJECT(h), &pci_host_conf_be_ops, h,
  408. "pci-conf-idx", 4);
  409. memory_region_init_io(&h->data_mem, OBJECT(h), &pci_host_data_le_ops, h,
  410. "pci-conf-data", 4);
  411. memory_region_init_io(&s->iomem, OBJECT(s), &e500_pci_reg_ops, s,
  412. "pci.reg", PCIE500_REG_SIZE);
  413. memory_region_add_subregion(&s->container, PCIE500_CFGADDR, &h->conf_mem);
  414. memory_region_add_subregion(&s->container, PCIE500_CFGDATA, &h->data_mem);
  415. memory_region_add_subregion(&s->container, PCIE500_REG_BASE, &s->iomem);
  416. sysbus_init_mmio(sbd, &s->container);
  417. pci_bus_set_route_irq_fn(b, e500_route_intx_pin_to_irq);
  418. }
  419. static void e500_host_bridge_class_init(ObjectClass *klass, void *data)
  420. {
  421. DeviceClass *dc = DEVICE_CLASS(klass);
  422. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  423. k->realize = e500_pcihost_bridge_realize;
  424. k->vendor_id = PCI_VENDOR_ID_FREESCALE;
  425. k->device_id = PCI_DEVICE_ID_MPC8533E;
  426. k->class_id = PCI_CLASS_PROCESSOR_POWERPC;
  427. dc->desc = "Host bridge";
  428. /*
  429. * PCI-facing part of the host bridge, not usable without the
  430. * host-facing part, which can't be device_add'ed, yet.
  431. */
  432. dc->user_creatable = false;
  433. }
  434. static const TypeInfo e500_host_bridge_info = {
  435. .name = "e500-host-bridge",
  436. .parent = TYPE_PCI_DEVICE,
  437. .instance_size = sizeof(PPCE500PCIBridgeState),
  438. .class_init = e500_host_bridge_class_init,
  439. .interfaces = (InterfaceInfo[]) {
  440. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  441. { },
  442. },
  443. };
  444. static Property pcihost_properties[] = {
  445. DEFINE_PROP_UINT32("first_slot", PPCE500PCIState, first_slot, 0x11),
  446. DEFINE_PROP_UINT32("first_pin_irq", PPCE500PCIState, first_pin_irq, 0x1),
  447. DEFINE_PROP_END_OF_LIST(),
  448. };
  449. static void e500_pcihost_class_init(ObjectClass *klass, void *data)
  450. {
  451. DeviceClass *dc = DEVICE_CLASS(klass);
  452. dc->realize = e500_pcihost_realize;
  453. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  454. dc->props = pcihost_properties;
  455. dc->vmsd = &vmstate_ppce500_pci;
  456. }
  457. static const TypeInfo e500_pcihost_info = {
  458. .name = TYPE_PPC_E500_PCI_HOST_BRIDGE,
  459. .parent = TYPE_PCI_HOST_BRIDGE,
  460. .instance_size = sizeof(PPCE500PCIState),
  461. .class_init = e500_pcihost_class_init,
  462. };
  463. static void e500_pci_register_types(void)
  464. {
  465. type_register_static(&e500_pcihost_info);
  466. type_register_static(&e500_host_bridge_info);
  467. }
  468. type_init(e500_pci_register_types)