2
0

ppce500.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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 "hw/hw.h"
  17. #include "hw/ppc/e500-ccsr.h"
  18. #include "hw/pci/pci.h"
  19. #include "hw/pci/pci_host.h"
  20. #include "qemu/bswap.h"
  21. #include "hw/pci-host/ppce500.h"
  22. #ifdef DEBUG_PCI
  23. #define pci_debug(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__)
  24. #else
  25. #define pci_debug(fmt, ...)
  26. #endif
  27. #define PCIE500_CFGADDR 0x0
  28. #define PCIE500_CFGDATA 0x4
  29. #define PCIE500_REG_BASE 0xC00
  30. #define PCIE500_ALL_SIZE 0x1000
  31. #define PCIE500_REG_SIZE (PCIE500_ALL_SIZE - PCIE500_REG_BASE)
  32. #define PCIE500_PCI_IOLEN 0x10000ULL
  33. #define PPCE500_PCI_CONFIG_ADDR 0x0
  34. #define PPCE500_PCI_CONFIG_DATA 0x4
  35. #define PPCE500_PCI_INTACK 0x8
  36. #define PPCE500_PCI_OW1 (0xC20 - PCIE500_REG_BASE)
  37. #define PPCE500_PCI_OW2 (0xC40 - PCIE500_REG_BASE)
  38. #define PPCE500_PCI_OW3 (0xC60 - PCIE500_REG_BASE)
  39. #define PPCE500_PCI_OW4 (0xC80 - PCIE500_REG_BASE)
  40. #define PPCE500_PCI_IW3 (0xDA0 - PCIE500_REG_BASE)
  41. #define PPCE500_PCI_IW2 (0xDC0 - PCIE500_REG_BASE)
  42. #define PPCE500_PCI_IW1 (0xDE0 - PCIE500_REG_BASE)
  43. #define PPCE500_PCI_GASKET_TIMR (0xE20 - PCIE500_REG_BASE)
  44. #define PCI_POTAR 0x0
  45. #define PCI_POTEAR 0x4
  46. #define PCI_POWBAR 0x8
  47. #define PCI_POWAR 0x10
  48. #define PCI_PITAR 0x0
  49. #define PCI_PIWBAR 0x8
  50. #define PCI_PIWBEAR 0xC
  51. #define PCI_PIWAR 0x10
  52. #define PPCE500_PCI_NR_POBS 5
  53. #define PPCE500_PCI_NR_PIBS 3
  54. struct pci_outbound {
  55. uint32_t potar;
  56. uint32_t potear;
  57. uint32_t powbar;
  58. uint32_t powar;
  59. };
  60. struct pci_inbound {
  61. uint32_t pitar;
  62. uint32_t piwbar;
  63. uint32_t piwbear;
  64. uint32_t piwar;
  65. };
  66. #define TYPE_PPC_E500_PCI_HOST_BRIDGE "e500-pcihost"
  67. #define PPC_E500_PCI_HOST_BRIDGE(obj) \
  68. OBJECT_CHECK(PPCE500PCIState, (obj), TYPE_PPC_E500_PCI_HOST_BRIDGE)
  69. struct PPCE500PCIState {
  70. PCIHostState parent_obj;
  71. struct pci_outbound pob[PPCE500_PCI_NR_POBS];
  72. struct pci_inbound pib[PPCE500_PCI_NR_PIBS];
  73. uint32_t gasket_time;
  74. qemu_irq irq[PCI_NUM_PINS];
  75. uint32_t irq_num[PCI_NUM_PINS];
  76. uint32_t first_slot;
  77. uint32_t first_pin_irq;
  78. /* mmio maps */
  79. MemoryRegion container;
  80. MemoryRegion iomem;
  81. MemoryRegion pio;
  82. };
  83. #define TYPE_PPC_E500_PCI_BRIDGE "e500-host-bridge"
  84. #define PPC_E500_PCI_BRIDGE(obj) \
  85. OBJECT_CHECK(PPCE500PCIBridgeState, (obj), TYPE_PPC_E500_PCI_BRIDGE)
  86. struct PPCE500PCIBridgeState {
  87. /*< private >*/
  88. PCIDevice parent;
  89. /*< public >*/
  90. MemoryRegion bar0;
  91. };
  92. typedef struct PPCE500PCIBridgeState PPCE500PCIBridgeState;
  93. typedef struct PPCE500PCIState PPCE500PCIState;
  94. static uint64_t pci_reg_read4(void *opaque, hwaddr addr,
  95. unsigned size)
  96. {
  97. PPCE500PCIState *pci = opaque;
  98. unsigned long win;
  99. uint32_t value = 0;
  100. int idx;
  101. win = addr & 0xfe0;
  102. switch (win) {
  103. case PPCE500_PCI_OW1:
  104. case PPCE500_PCI_OW2:
  105. case PPCE500_PCI_OW3:
  106. case PPCE500_PCI_OW4:
  107. idx = (addr >> 5) & 0x7;
  108. switch (addr & 0xC) {
  109. case PCI_POTAR:
  110. value = pci->pob[idx].potar;
  111. break;
  112. case PCI_POTEAR:
  113. value = pci->pob[idx].potear;
  114. break;
  115. case PCI_POWBAR:
  116. value = pci->pob[idx].powbar;
  117. break;
  118. case PCI_POWAR:
  119. value = pci->pob[idx].powar;
  120. break;
  121. default:
  122. break;
  123. }
  124. break;
  125. case PPCE500_PCI_IW3:
  126. case PPCE500_PCI_IW2:
  127. case PPCE500_PCI_IW1:
  128. idx = ((addr >> 5) & 0x3) - 1;
  129. switch (addr & 0xC) {
  130. case PCI_PITAR:
  131. value = pci->pib[idx].pitar;
  132. break;
  133. case PCI_PIWBAR:
  134. value = pci->pib[idx].piwbar;
  135. break;
  136. case PCI_PIWBEAR:
  137. value = pci->pib[idx].piwbear;
  138. break;
  139. case PCI_PIWAR:
  140. value = pci->pib[idx].piwar;
  141. break;
  142. default:
  143. break;
  144. };
  145. break;
  146. case PPCE500_PCI_GASKET_TIMR:
  147. value = pci->gasket_time;
  148. break;
  149. default:
  150. break;
  151. }
  152. pci_debug("%s: win:%lx(addr:" TARGET_FMT_plx ") -> value:%x\n", __func__,
  153. win, addr, value);
  154. return value;
  155. }
  156. static void pci_reg_write4(void *opaque, hwaddr addr,
  157. uint64_t value, unsigned size)
  158. {
  159. PPCE500PCIState *pci = opaque;
  160. unsigned long win;
  161. int idx;
  162. win = addr & 0xfe0;
  163. pci_debug("%s: value:%x -> win:%lx(addr:" TARGET_FMT_plx ")\n",
  164. __func__, (unsigned)value, win, addr);
  165. switch (win) {
  166. case PPCE500_PCI_OW1:
  167. case PPCE500_PCI_OW2:
  168. case PPCE500_PCI_OW3:
  169. case PPCE500_PCI_OW4:
  170. idx = (addr >> 5) & 0x7;
  171. switch (addr & 0xC) {
  172. case PCI_POTAR:
  173. pci->pob[idx].potar = value;
  174. break;
  175. case PCI_POTEAR:
  176. pci->pob[idx].potear = value;
  177. break;
  178. case PCI_POWBAR:
  179. pci->pob[idx].powbar = value;
  180. break;
  181. case PCI_POWAR:
  182. pci->pob[idx].powar = value;
  183. break;
  184. default:
  185. break;
  186. };
  187. break;
  188. case PPCE500_PCI_IW3:
  189. case PPCE500_PCI_IW2:
  190. case PPCE500_PCI_IW1:
  191. idx = ((addr >> 5) & 0x3) - 1;
  192. switch (addr & 0xC) {
  193. case PCI_PITAR:
  194. pci->pib[idx].pitar = value;
  195. break;
  196. case PCI_PIWBAR:
  197. pci->pib[idx].piwbar = value;
  198. break;
  199. case PCI_PIWBEAR:
  200. pci->pib[idx].piwbear = value;
  201. break;
  202. case PCI_PIWAR:
  203. pci->pib[idx].piwar = value;
  204. break;
  205. default:
  206. break;
  207. };
  208. break;
  209. case PPCE500_PCI_GASKET_TIMR:
  210. pci->gasket_time = value;
  211. break;
  212. default:
  213. break;
  214. };
  215. }
  216. static const MemoryRegionOps e500_pci_reg_ops = {
  217. .read = pci_reg_read4,
  218. .write = pci_reg_write4,
  219. .endianness = DEVICE_BIG_ENDIAN,
  220. };
  221. static int mpc85xx_pci_map_irq(PCIDevice *pci_dev, int pin)
  222. {
  223. int devno = pci_dev->devfn >> 3;
  224. int ret;
  225. ret = ppce500_pci_map_irq_slot(devno, pin);
  226. pci_debug("%s: devfn %x irq %d -> %d devno:%x\n", __func__,
  227. pci_dev->devfn, pin, ret, devno);
  228. return ret;
  229. }
  230. static void mpc85xx_pci_set_irq(void *opaque, int pin, int level)
  231. {
  232. PPCE500PCIState *s = opaque;
  233. qemu_irq *pic = s->irq;
  234. pci_debug("%s: PCI irq %d, level:%d\n", __func__, pin , level);
  235. qemu_set_irq(pic[pin], level);
  236. }
  237. static PCIINTxRoute e500_route_intx_pin_to_irq(void *opaque, int pin)
  238. {
  239. PCIINTxRoute route;
  240. PPCE500PCIState *s = opaque;
  241. route.mode = PCI_INTX_ENABLED;
  242. route.irq = s->irq_num[pin];
  243. pci_debug("%s: PCI irq-pin = %d, irq_num= %d\n", __func__, pin, route.irq);
  244. return route;
  245. }
  246. static const VMStateDescription vmstate_pci_outbound = {
  247. .name = "pci_outbound",
  248. .version_id = 0,
  249. .minimum_version_id = 0,
  250. .fields = (VMStateField[]) {
  251. VMSTATE_UINT32(potar, struct pci_outbound),
  252. VMSTATE_UINT32(potear, struct pci_outbound),
  253. VMSTATE_UINT32(powbar, struct pci_outbound),
  254. VMSTATE_UINT32(powar, struct pci_outbound),
  255. VMSTATE_END_OF_LIST()
  256. }
  257. };
  258. static const VMStateDescription vmstate_pci_inbound = {
  259. .name = "pci_inbound",
  260. .version_id = 0,
  261. .minimum_version_id = 0,
  262. .fields = (VMStateField[]) {
  263. VMSTATE_UINT32(pitar, struct pci_inbound),
  264. VMSTATE_UINT32(piwbar, struct pci_inbound),
  265. VMSTATE_UINT32(piwbear, struct pci_inbound),
  266. VMSTATE_UINT32(piwar, struct pci_inbound),
  267. VMSTATE_END_OF_LIST()
  268. }
  269. };
  270. static const VMStateDescription vmstate_ppce500_pci = {
  271. .name = "ppce500_pci",
  272. .version_id = 1,
  273. .minimum_version_id = 1,
  274. .fields = (VMStateField[]) {
  275. VMSTATE_STRUCT_ARRAY(pob, PPCE500PCIState, PPCE500_PCI_NR_POBS, 1,
  276. vmstate_pci_outbound, struct pci_outbound),
  277. VMSTATE_STRUCT_ARRAY(pib, PPCE500PCIState, PPCE500_PCI_NR_PIBS, 1,
  278. vmstate_pci_inbound, struct pci_inbound),
  279. VMSTATE_UINT32(gasket_time, PPCE500PCIState),
  280. VMSTATE_END_OF_LIST()
  281. }
  282. };
  283. #include "exec/address-spaces.h"
  284. static int e500_pcihost_bridge_initfn(PCIDevice *d)
  285. {
  286. PPCE500PCIBridgeState *b = PPC_E500_PCI_BRIDGE(d);
  287. PPCE500CCSRState *ccsr = CCSR(container_get(qdev_get_machine(),
  288. "/e500-ccsr"));
  289. pci_config_set_class(d->config, PCI_CLASS_BRIDGE_PCI);
  290. d->config[PCI_HEADER_TYPE] =
  291. (d->config[PCI_HEADER_TYPE] & PCI_HEADER_TYPE_MULTI_FUNCTION) |
  292. PCI_HEADER_TYPE_BRIDGE;
  293. memory_region_init_alias(&b->bar0, OBJECT(ccsr), "e500-pci-bar0", &ccsr->ccsr_space,
  294. 0, int128_get64(ccsr->ccsr_space.size));
  295. pci_register_bar(d, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &b->bar0);
  296. return 0;
  297. }
  298. static int e500_pcihost_initfn(SysBusDevice *dev)
  299. {
  300. PCIHostState *h;
  301. PPCE500PCIState *s;
  302. PCIBus *b;
  303. int i;
  304. MemoryRegion *address_space_mem = get_system_memory();
  305. h = PCI_HOST_BRIDGE(dev);
  306. s = PPC_E500_PCI_HOST_BRIDGE(dev);
  307. for (i = 0; i < ARRAY_SIZE(s->irq); i++) {
  308. sysbus_init_irq(dev, &s->irq[i]);
  309. }
  310. for (i = 0; i < PCI_NUM_PINS; i++) {
  311. s->irq_num[i] = s->first_pin_irq + i;
  312. }
  313. memory_region_init(&s->pio, OBJECT(s), "pci-pio", PCIE500_PCI_IOLEN);
  314. b = pci_register_bus(DEVICE(dev), NULL, mpc85xx_pci_set_irq,
  315. mpc85xx_pci_map_irq, s, address_space_mem,
  316. &s->pio, PCI_DEVFN(s->first_slot, 0), 4, TYPE_PCI_BUS);
  317. h->bus = b;
  318. pci_create_simple(b, 0, "e500-host-bridge");
  319. memory_region_init(&s->container, OBJECT(h), "pci-container", PCIE500_ALL_SIZE);
  320. memory_region_init_io(&h->conf_mem, OBJECT(h), &pci_host_conf_be_ops, h,
  321. "pci-conf-idx", 4);
  322. memory_region_init_io(&h->data_mem, OBJECT(h), &pci_host_data_le_ops, h,
  323. "pci-conf-data", 4);
  324. memory_region_init_io(&s->iomem, OBJECT(s), &e500_pci_reg_ops, s,
  325. "pci.reg", PCIE500_REG_SIZE);
  326. memory_region_add_subregion(&s->container, PCIE500_CFGADDR, &h->conf_mem);
  327. memory_region_add_subregion(&s->container, PCIE500_CFGDATA, &h->data_mem);
  328. memory_region_add_subregion(&s->container, PCIE500_REG_BASE, &s->iomem);
  329. sysbus_init_mmio(dev, &s->container);
  330. sysbus_init_mmio(dev, &s->pio);
  331. pci_bus_set_route_irq_fn(b, e500_route_intx_pin_to_irq);
  332. return 0;
  333. }
  334. static void e500_host_bridge_class_init(ObjectClass *klass, void *data)
  335. {
  336. DeviceClass *dc = DEVICE_CLASS(klass);
  337. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  338. k->init = e500_pcihost_bridge_initfn;
  339. k->vendor_id = PCI_VENDOR_ID_FREESCALE;
  340. k->device_id = PCI_DEVICE_ID_MPC8533E;
  341. k->class_id = PCI_CLASS_PROCESSOR_POWERPC;
  342. dc->desc = "Host bridge";
  343. /*
  344. * PCI-facing part of the host bridge, not usable without the
  345. * host-facing part, which can't be device_add'ed, yet.
  346. */
  347. dc->cannot_instantiate_with_device_add_yet = true;
  348. }
  349. static const TypeInfo e500_host_bridge_info = {
  350. .name = "e500-host-bridge",
  351. .parent = TYPE_PCI_DEVICE,
  352. .instance_size = sizeof(PPCE500PCIBridgeState),
  353. .class_init = e500_host_bridge_class_init,
  354. };
  355. static Property pcihost_properties[] = {
  356. DEFINE_PROP_UINT32("first_slot", PPCE500PCIState, first_slot, 0x11),
  357. DEFINE_PROP_UINT32("first_pin_irq", PPCE500PCIState, first_pin_irq, 0x1),
  358. DEFINE_PROP_END_OF_LIST(),
  359. };
  360. static void e500_pcihost_class_init(ObjectClass *klass, void *data)
  361. {
  362. DeviceClass *dc = DEVICE_CLASS(klass);
  363. SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
  364. k->init = e500_pcihost_initfn;
  365. set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
  366. dc->props = pcihost_properties;
  367. dc->vmsd = &vmstate_ppce500_pci;
  368. }
  369. static const TypeInfo e500_pcihost_info = {
  370. .name = TYPE_PPC_E500_PCI_HOST_BRIDGE,
  371. .parent = TYPE_PCI_HOST_BRIDGE,
  372. .instance_size = sizeof(PPCE500PCIState),
  373. .class_init = e500_pcihost_class_init,
  374. };
  375. static void e500_pci_register_types(void)
  376. {
  377. type_register_static(&e500_pcihost_info);
  378. type_register_static(&e500_host_bridge_info);
  379. }
  380. type_init(e500_pci_register_types)