ppc4xx_pci.c 11 KB

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