ppc4xx_pci.c 11 KB

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