ppce500_pci.c 12 KB

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