ppc4xx_pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License, version 2, as
  4. * published by the Free Software Foundation.
  5. *
  6. * This program is distributed in the hope that it will be useful,
  7. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. * GNU General Public License for more details.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  13. *
  14. * Copyright IBM Corp. 2008
  15. *
  16. * Authors: Hollis Blanchard <hollisb@us.ibm.com>
  17. */
  18. /*
  19. * This file implements emulation of the 32-bit PCI controller found in some
  20. * 4xx SoCs, such as the 440EP.
  21. */
  22. #include "qemu/osdep.h"
  23. #include "qemu/log.h"
  24. #include "hw/irq.h"
  25. #include "hw/ppc/ppc.h"
  26. #include "hw/ppc/ppc4xx.h"
  27. #include "migration/vmstate.h"
  28. #include "qemu/module.h"
  29. #include "sysemu/reset.h"
  30. #include "hw/pci/pci_device.h"
  31. #include "hw/pci/pci_host.h"
  32. #include "trace.h"
  33. #include "qom/object.h"
  34. struct PCIMasterMap {
  35. uint32_t la;
  36. uint32_t ma;
  37. uint32_t pcila;
  38. uint32_t pciha;
  39. };
  40. struct PCITargetMap {
  41. uint32_t ms;
  42. uint32_t la;
  43. };
  44. OBJECT_DECLARE_SIMPLE_TYPE(PPC4xxPCIState, PPC4xx_PCI_HOST_BRIDGE)
  45. #define PPC4xx_PCI_NR_PMMS 3
  46. #define PPC4xx_PCI_NR_PTMS 2
  47. #define PPC4xx_PCI_NUM_DEVS 5
  48. struct PPC4xxPCIState {
  49. PCIHostState parent_obj;
  50. struct PCIMasterMap pmm[PPC4xx_PCI_NR_PMMS];
  51. struct PCITargetMap ptm[PPC4xx_PCI_NR_PTMS];
  52. qemu_irq irq[PPC4xx_PCI_NUM_DEVS];
  53. MemoryRegion container;
  54. MemoryRegion iomem;
  55. };
  56. #define PCIC0_CFGADDR 0x0
  57. #define PCIC0_CFGDATA 0x4
  58. /*
  59. * PLB Memory Map (PMM) registers specify which PLB addresses are translated to
  60. * PCI accesses.
  61. */
  62. #define PCIL0_PMM0LA 0x0
  63. #define PCIL0_PMM0MA 0x4
  64. #define PCIL0_PMM0PCILA 0x8
  65. #define PCIL0_PMM0PCIHA 0xc
  66. #define PCIL0_PMM1LA 0x10
  67. #define PCIL0_PMM1MA 0x14
  68. #define PCIL0_PMM1PCILA 0x18
  69. #define PCIL0_PMM1PCIHA 0x1c
  70. #define PCIL0_PMM2LA 0x20
  71. #define PCIL0_PMM2MA 0x24
  72. #define PCIL0_PMM2PCILA 0x28
  73. #define PCIL0_PMM2PCIHA 0x2c
  74. /*
  75. * PCI Target Map (PTM) registers specify which PCI addresses are translated to
  76. * PLB accesses.
  77. */
  78. #define PCIL0_PTM1MS 0x30
  79. #define PCIL0_PTM1LA 0x34
  80. #define PCIL0_PTM2MS 0x38
  81. #define PCIL0_PTM2LA 0x3c
  82. #define PCI_REG_BASE 0x800000
  83. #define PCI_REG_SIZE 0x40
  84. #define PCI_ALL_SIZE (PCI_REG_BASE + PCI_REG_SIZE)
  85. static void ppc4xx_pci_reg_write4(void *opaque, hwaddr offset,
  86. uint64_t value, unsigned size)
  87. {
  88. struct PPC4xxPCIState *pci = opaque;
  89. /*
  90. * We ignore all target attempts at PCI configuration, effectively
  91. * assuming a bidirectional 1:1 mapping of PLB and PCI space.
  92. */
  93. switch (offset) {
  94. case PCIL0_PMM0LA:
  95. pci->pmm[0].la = value;
  96. break;
  97. case PCIL0_PMM0MA:
  98. pci->pmm[0].ma = value;
  99. break;
  100. case PCIL0_PMM0PCIHA:
  101. pci->pmm[0].pciha = value;
  102. break;
  103. case PCIL0_PMM0PCILA:
  104. pci->pmm[0].pcila = value;
  105. break;
  106. case PCIL0_PMM1LA:
  107. pci->pmm[1].la = value;
  108. break;
  109. case PCIL0_PMM1MA:
  110. pci->pmm[1].ma = value;
  111. break;
  112. case PCIL0_PMM1PCIHA:
  113. pci->pmm[1].pciha = value;
  114. break;
  115. case PCIL0_PMM1PCILA:
  116. pci->pmm[1].pcila = value;
  117. break;
  118. case PCIL0_PMM2LA:
  119. pci->pmm[2].la = value;
  120. break;
  121. case PCIL0_PMM2MA:
  122. pci->pmm[2].ma = value;
  123. break;
  124. case PCIL0_PMM2PCIHA:
  125. pci->pmm[2].pciha = value;
  126. break;
  127. case PCIL0_PMM2PCILA:
  128. pci->pmm[2].pcila = value;
  129. break;
  130. case PCIL0_PTM1MS:
  131. pci->ptm[0].ms = value;
  132. break;
  133. case PCIL0_PTM1LA:
  134. pci->ptm[0].la = value;
  135. break;
  136. case PCIL0_PTM2MS:
  137. pci->ptm[1].ms = value;
  138. break;
  139. case PCIL0_PTM2LA:
  140. pci->ptm[1].la = value;
  141. break;
  142. default:
  143. qemu_log_mask(LOG_GUEST_ERROR,
  144. "%s: unhandled PCI internal register 0x%" HWADDR_PRIx "\n",
  145. __func__, offset);
  146. break;
  147. }
  148. }
  149. static uint64_t ppc4xx_pci_reg_read4(void *opaque, hwaddr offset,
  150. unsigned size)
  151. {
  152. struct PPC4xxPCIState *pci = opaque;
  153. uint32_t value;
  154. switch (offset) {
  155. case PCIL0_PMM0LA:
  156. value = pci->pmm[0].la;
  157. break;
  158. case PCIL0_PMM0MA:
  159. value = pci->pmm[0].ma;
  160. break;
  161. case PCIL0_PMM0PCIHA:
  162. value = pci->pmm[0].pciha;
  163. break;
  164. case PCIL0_PMM0PCILA:
  165. value = pci->pmm[0].pcila;
  166. break;
  167. case PCIL0_PMM1LA:
  168. value = pci->pmm[1].la;
  169. break;
  170. case PCIL0_PMM1MA:
  171. value = pci->pmm[1].ma;
  172. break;
  173. case PCIL0_PMM1PCIHA:
  174. value = pci->pmm[1].pciha;
  175. break;
  176. case PCIL0_PMM1PCILA:
  177. value = pci->pmm[1].pcila;
  178. break;
  179. case PCIL0_PMM2LA:
  180. value = pci->pmm[2].la;
  181. break;
  182. case PCIL0_PMM2MA:
  183. value = pci->pmm[2].ma;
  184. break;
  185. case PCIL0_PMM2PCIHA:
  186. value = pci->pmm[2].pciha;
  187. break;
  188. case PCIL0_PMM2PCILA:
  189. value = pci->pmm[2].pcila;
  190. break;
  191. case PCIL0_PTM1MS:
  192. value = pci->ptm[0].ms;
  193. break;
  194. case PCIL0_PTM1LA:
  195. value = pci->ptm[0].la;
  196. break;
  197. case PCIL0_PTM2MS:
  198. value = pci->ptm[1].ms;
  199. break;
  200. case PCIL0_PTM2LA:
  201. value = pci->ptm[1].la;
  202. break;
  203. default:
  204. qemu_log_mask(LOG_GUEST_ERROR,
  205. "%s: invalid PCI internal register 0x%" HWADDR_PRIx "\n",
  206. __func__, offset);
  207. value = 0;
  208. }
  209. return value;
  210. }
  211. static const MemoryRegionOps pci_reg_ops = {
  212. .read = ppc4xx_pci_reg_read4,
  213. .write = ppc4xx_pci_reg_write4,
  214. .endianness = DEVICE_LITTLE_ENDIAN,
  215. };
  216. static void ppc4xx_pci_reset(void *opaque)
  217. {
  218. struct PPC4xxPCIState *pci = opaque;
  219. memset(pci->pmm, 0, sizeof(pci->pmm));
  220. memset(pci->ptm, 0, sizeof(pci->ptm));
  221. }
  222. /*
  223. * On Bamboo, all pins from each slot are tied to a single board IRQ.
  224. * This may need further refactoring for other boards.
  225. */
  226. static int ppc4xx_pci_map_irq(PCIDevice *pci_dev, int irq_num)
  227. {
  228. int slot = PCI_SLOT(pci_dev->devfn);
  229. trace_ppc4xx_pci_map_irq(pci_dev->devfn, irq_num, slot);
  230. return slot > 0 ? slot - 1 : PPC4xx_PCI_NUM_DEVS - 1;
  231. }
  232. static void ppc4xx_pci_set_irq(void *opaque, int irq_num, int level)
  233. {
  234. qemu_irq *pci_irqs = opaque;
  235. trace_ppc4xx_pci_set_irq(irq_num);
  236. assert(irq_num >= 0 && irq_num < PPC4xx_PCI_NUM_DEVS);
  237. qemu_set_irq(pci_irqs[irq_num], level);
  238. }
  239. static const VMStateDescription vmstate_pci_master_map = {
  240. .name = "pci_master_map",
  241. .version_id = 0,
  242. .minimum_version_id = 0,
  243. .fields = (VMStateField[]) {
  244. VMSTATE_UINT32(la, struct PCIMasterMap),
  245. VMSTATE_UINT32(ma, struct PCIMasterMap),
  246. VMSTATE_UINT32(pcila, struct PCIMasterMap),
  247. VMSTATE_UINT32(pciha, struct PCIMasterMap),
  248. VMSTATE_END_OF_LIST()
  249. }
  250. };
  251. static const VMStateDescription vmstate_pci_target_map = {
  252. .name = "pci_target_map",
  253. .version_id = 0,
  254. .minimum_version_id = 0,
  255. .fields = (VMStateField[]) {
  256. VMSTATE_UINT32(ms, struct PCITargetMap),
  257. VMSTATE_UINT32(la, struct PCITargetMap),
  258. VMSTATE_END_OF_LIST()
  259. }
  260. };
  261. static const VMStateDescription vmstate_ppc4xx_pci = {
  262. .name = "ppc4xx_pci",
  263. .version_id = 1,
  264. .minimum_version_id = 1,
  265. .fields = (VMStateField[]) {
  266. VMSTATE_STRUCT_ARRAY(pmm, PPC4xxPCIState, PPC4xx_PCI_NR_PMMS, 1,
  267. vmstate_pci_master_map,
  268. struct PCIMasterMap),
  269. VMSTATE_STRUCT_ARRAY(ptm, PPC4xxPCIState, PPC4xx_PCI_NR_PTMS, 1,
  270. vmstate_pci_target_map,
  271. struct PCITargetMap),
  272. VMSTATE_END_OF_LIST()
  273. }
  274. };
  275. /* XXX Interrupt acknowledge cycles not supported. */
  276. static void ppc4xx_pcihost_realize(DeviceState *dev, Error **errp)
  277. {
  278. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  279. PPC4xxPCIState *s;
  280. PCIHostState *h;
  281. PCIBus *b;
  282. int i;
  283. h = PCI_HOST_BRIDGE(dev);
  284. s = PPC4xx_PCI_HOST_BRIDGE(dev);
  285. for (i = 0; i < ARRAY_SIZE(s->irq); i++) {
  286. sysbus_init_irq(sbd, &s->irq[i]);
  287. }
  288. b = pci_register_root_bus(dev, NULL, ppc4xx_pci_set_irq,
  289. ppc4xx_pci_map_irq, s->irq, get_system_memory(),
  290. get_system_io(), 0, ARRAY_SIZE(s->irq),
  291. TYPE_PCI_BUS);
  292. h->bus = b;
  293. pci_create_simple(b, 0, "ppc4xx-host-bridge");
  294. /* XXX split into 2 memory regions, one for config space, one for regs */
  295. memory_region_init(&s->container, OBJECT(s), "pci-container", PCI_ALL_SIZE);
  296. memory_region_init_io(&h->conf_mem, OBJECT(s), &pci_host_conf_le_ops, h,
  297. "pci-conf-idx", 4);
  298. memory_region_init_io(&h->data_mem, OBJECT(s), &pci_host_data_le_ops, h,
  299. "pci-conf-data", 4);
  300. memory_region_init_io(&s->iomem, OBJECT(s), &pci_reg_ops, s,
  301. "pci.reg", PCI_REG_SIZE);
  302. memory_region_add_subregion(&s->container, PCIC0_CFGADDR, &h->conf_mem);
  303. memory_region_add_subregion(&s->container, PCIC0_CFGDATA, &h->data_mem);
  304. memory_region_add_subregion(&s->container, PCI_REG_BASE, &s->iomem);
  305. sysbus_init_mmio(sbd, &s->container);
  306. qemu_register_reset(ppc4xx_pci_reset, s);
  307. }
  308. static void ppc4xx_host_bridge_class_init(ObjectClass *klass, void *data)
  309. {
  310. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  311. DeviceClass *dc = DEVICE_CLASS(klass);
  312. dc->desc = "Host bridge";
  313. k->vendor_id = PCI_VENDOR_ID_IBM;
  314. k->device_id = PCI_DEVICE_ID_IBM_440GX;
  315. k->class_id = PCI_CLASS_BRIDGE_OTHER;
  316. /*
  317. * PCI-facing part of the host bridge, not usable without the
  318. * host-facing part, which can't be device_add'ed, yet.
  319. */
  320. dc->user_creatable = false;
  321. }
  322. static const TypeInfo ppc4xx_host_bridge_info = {
  323. .name = "ppc4xx-host-bridge",
  324. .parent = TYPE_PCI_DEVICE,
  325. .instance_size = sizeof(PCIDevice),
  326. .class_init = ppc4xx_host_bridge_class_init,
  327. .interfaces = (InterfaceInfo[]) {
  328. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  329. { },
  330. },
  331. };
  332. static void ppc4xx_pcihost_class_init(ObjectClass *klass, void *data)
  333. {
  334. DeviceClass *dc = DEVICE_CLASS(klass);
  335. dc->realize = ppc4xx_pcihost_realize;
  336. dc->vmsd = &vmstate_ppc4xx_pci;
  337. }
  338. static const TypeInfo ppc4xx_pcihost_info = {
  339. .name = TYPE_PPC4xx_PCI_HOST_BRIDGE,
  340. .parent = TYPE_PCI_HOST_BRIDGE,
  341. .instance_size = sizeof(PPC4xxPCIState),
  342. .class_init = ppc4xx_pcihost_class_init,
  343. };
  344. static void ppc4xx_pci_register_types(void)
  345. {
  346. type_register_static(&ppc4xx_pcihost_info);
  347. type_register_static(&ppc4xx_host_bridge_info);
  348. }
  349. type_init(ppc4xx_pci_register_types)