ppc4xx_pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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.h"
  24. #include "pci_host.h"
  25. #undef DEBUG
  26. #ifdef DEBUG
  27. #define DPRINTF(fmt, ...) do { printf(fmt, ## __VA_ARGS__); } while (0)
  28. #else
  29. #define DPRINTF(fmt, ...)
  30. #endif /* DEBUG */
  31. struct PCIMasterMap {
  32. uint32_t la;
  33. uint32_t ma;
  34. uint32_t pcila;
  35. uint32_t pciha;
  36. };
  37. struct PCITargetMap {
  38. uint32_t ms;
  39. uint32_t la;
  40. };
  41. #define PPC4xx_PCI_NR_PMMS 3
  42. #define PPC4xx_PCI_NR_PTMS 2
  43. struct PPC4xxPCIState {
  44. struct PCIMasterMap pmm[PPC4xx_PCI_NR_PMMS];
  45. struct PCITargetMap ptm[PPC4xx_PCI_NR_PTMS];
  46. PCIHostState pci_state;
  47. PCIDevice *pci_dev;
  48. };
  49. typedef struct PPC4xxPCIState PPC4xxPCIState;
  50. #define PCIC0_CFGADDR 0x0
  51. #define PCIC0_CFGDATA 0x4
  52. /* PLB Memory Map (PMM) registers specify which PLB addresses are translated to
  53. * PCI accesses. */
  54. #define PCIL0_PMM0LA 0x0
  55. #define PCIL0_PMM0MA 0x4
  56. #define PCIL0_PMM0PCILA 0x8
  57. #define PCIL0_PMM0PCIHA 0xc
  58. #define PCIL0_PMM1LA 0x10
  59. #define PCIL0_PMM1MA 0x14
  60. #define PCIL0_PMM1PCILA 0x18
  61. #define PCIL0_PMM1PCIHA 0x1c
  62. #define PCIL0_PMM2LA 0x20
  63. #define PCIL0_PMM2MA 0x24
  64. #define PCIL0_PMM2PCILA 0x28
  65. #define PCIL0_PMM2PCIHA 0x2c
  66. /* PCI Target Map (PTM) registers specify which PCI addresses are translated to
  67. * PLB accesses. */
  68. #define PCIL0_PTM1MS 0x30
  69. #define PCIL0_PTM1LA 0x34
  70. #define PCIL0_PTM2MS 0x38
  71. #define PCIL0_PTM2LA 0x3c
  72. #define PCI_REG_SIZE 0x40
  73. static uint32_t pci4xx_cfgaddr_readl(void *opaque, target_phys_addr_t addr)
  74. {
  75. PPC4xxPCIState *ppc4xx_pci = opaque;
  76. return ppc4xx_pci->pci_state.config_reg;
  77. }
  78. static CPUReadMemoryFunc * const pci4xx_cfgaddr_read[] = {
  79. &pci4xx_cfgaddr_readl,
  80. &pci4xx_cfgaddr_readl,
  81. &pci4xx_cfgaddr_readl,
  82. };
  83. static void pci4xx_cfgaddr_writel(void *opaque, target_phys_addr_t addr,
  84. uint32_t value)
  85. {
  86. PPC4xxPCIState *ppc4xx_pci = opaque;
  87. ppc4xx_pci->pci_state.config_reg = value & ~0x3;
  88. }
  89. static CPUWriteMemoryFunc * const pci4xx_cfgaddr_write[] = {
  90. &pci4xx_cfgaddr_writel,
  91. &pci4xx_cfgaddr_writel,
  92. &pci4xx_cfgaddr_writel,
  93. };
  94. static void ppc4xx_pci_reg_write4(void *opaque, target_phys_addr_t offset,
  95. uint32_t value)
  96. {
  97. struct PPC4xxPCIState *pci = opaque;
  98. /* We ignore all target attempts at PCI configuration, effectively
  99. * assuming a bidirectional 1:1 mapping of PLB and PCI space. */
  100. switch (offset) {
  101. case PCIL0_PMM0LA:
  102. pci->pmm[0].la = value;
  103. break;
  104. case PCIL0_PMM0MA:
  105. pci->pmm[0].ma = value;
  106. break;
  107. case PCIL0_PMM0PCIHA:
  108. pci->pmm[0].pciha = value;
  109. break;
  110. case PCIL0_PMM0PCILA:
  111. pci->pmm[0].pcila = value;
  112. break;
  113. case PCIL0_PMM1LA:
  114. pci->pmm[1].la = value;
  115. break;
  116. case PCIL0_PMM1MA:
  117. pci->pmm[1].ma = value;
  118. break;
  119. case PCIL0_PMM1PCIHA:
  120. pci->pmm[1].pciha = value;
  121. break;
  122. case PCIL0_PMM1PCILA:
  123. pci->pmm[1].pcila = value;
  124. break;
  125. case PCIL0_PMM2LA:
  126. pci->pmm[2].la = value;
  127. break;
  128. case PCIL0_PMM2MA:
  129. pci->pmm[2].ma = value;
  130. break;
  131. case PCIL0_PMM2PCIHA:
  132. pci->pmm[2].pciha = value;
  133. break;
  134. case PCIL0_PMM2PCILA:
  135. pci->pmm[2].pcila = value;
  136. break;
  137. case PCIL0_PTM1MS:
  138. pci->ptm[0].ms = value;
  139. break;
  140. case PCIL0_PTM1LA:
  141. pci->ptm[0].la = value;
  142. break;
  143. case PCIL0_PTM2MS:
  144. pci->ptm[1].ms = value;
  145. break;
  146. case PCIL0_PTM2LA:
  147. pci->ptm[1].la = value;
  148. break;
  149. default:
  150. printf("%s: unhandled PCI internal register 0x%lx\n", __func__,
  151. (unsigned long)offset);
  152. break;
  153. }
  154. }
  155. static uint32_t ppc4xx_pci_reg_read4(void *opaque, target_phys_addr_t offset)
  156. {
  157. struct PPC4xxPCIState *pci = opaque;
  158. uint32_t value;
  159. switch (offset) {
  160. case PCIL0_PMM0LA:
  161. value = pci->pmm[0].la;
  162. break;
  163. case PCIL0_PMM0MA:
  164. value = pci->pmm[0].ma;
  165. break;
  166. case PCIL0_PMM0PCIHA:
  167. value = pci->pmm[0].pciha;
  168. break;
  169. case PCIL0_PMM0PCILA:
  170. value = pci->pmm[0].pcila;
  171. break;
  172. case PCIL0_PMM1LA:
  173. value = pci->pmm[1].la;
  174. break;
  175. case PCIL0_PMM1MA:
  176. value = pci->pmm[1].ma;
  177. break;
  178. case PCIL0_PMM1PCIHA:
  179. value = pci->pmm[1].pciha;
  180. break;
  181. case PCIL0_PMM1PCILA:
  182. value = pci->pmm[1].pcila;
  183. break;
  184. case PCIL0_PMM2LA:
  185. value = pci->pmm[2].la;
  186. break;
  187. case PCIL0_PMM2MA:
  188. value = pci->pmm[2].ma;
  189. break;
  190. case PCIL0_PMM2PCIHA:
  191. value = pci->pmm[2].pciha;
  192. break;
  193. case PCIL0_PMM2PCILA:
  194. value = pci->pmm[2].pcila;
  195. break;
  196. case PCIL0_PTM1MS:
  197. value = pci->ptm[0].ms;
  198. break;
  199. case PCIL0_PTM1LA:
  200. value = pci->ptm[0].la;
  201. break;
  202. case PCIL0_PTM2MS:
  203. value = pci->ptm[1].ms;
  204. break;
  205. case PCIL0_PTM2LA:
  206. value = pci->ptm[1].la;
  207. break;
  208. default:
  209. printf("%s: invalid PCI internal register 0x%lx\n", __func__,
  210. (unsigned long)offset);
  211. value = 0;
  212. }
  213. return value;
  214. }
  215. static CPUReadMemoryFunc * const pci_reg_read[] = {
  216. &ppc4xx_pci_reg_read4,
  217. &ppc4xx_pci_reg_read4,
  218. &ppc4xx_pci_reg_read4,
  219. };
  220. static CPUWriteMemoryFunc * const pci_reg_write[] = {
  221. &ppc4xx_pci_reg_write4,
  222. &ppc4xx_pci_reg_write4,
  223. &ppc4xx_pci_reg_write4,
  224. };
  225. static void ppc4xx_pci_reset(void *opaque)
  226. {
  227. struct PPC4xxPCIState *pci = opaque;
  228. memset(pci->pmm, 0, sizeof(pci->pmm));
  229. memset(pci->ptm, 0, sizeof(pci->ptm));
  230. }
  231. /* On Bamboo, all pins from each slot are tied to a single board IRQ. This
  232. * may need further refactoring for other boards. */
  233. static int ppc4xx_pci_map_irq(PCIDevice *pci_dev, int irq_num)
  234. {
  235. int slot = pci_dev->devfn >> 3;
  236. DPRINTF("%s: devfn %x irq %d -> %d\n", __func__,
  237. pci_dev->devfn, irq_num, slot);
  238. return slot - 1;
  239. }
  240. static void ppc4xx_pci_set_irq(void *opaque, int irq_num, int level)
  241. {
  242. qemu_irq *pci_irqs = opaque;
  243. DPRINTF("%s: PCI irq %d\n", __func__, irq_num);
  244. qemu_set_irq(pci_irqs[irq_num], level);
  245. }
  246. static const VMStateDescription vmstate_pci_master_map = {
  247. .name = "pci_master_map",
  248. .version_id = 0,
  249. .minimum_version_id = 0,
  250. .minimum_version_id_old = 0,
  251. .fields = (VMStateField[]) {
  252. VMSTATE_UINT32(la, struct PCIMasterMap),
  253. VMSTATE_UINT32(ma, struct PCIMasterMap),
  254. VMSTATE_UINT32(pcila, struct PCIMasterMap),
  255. VMSTATE_UINT32(pciha, struct PCIMasterMap),
  256. VMSTATE_END_OF_LIST()
  257. }
  258. };
  259. static const VMStateDescription vmstate_pci_target_map = {
  260. .name = "pci_target_map",
  261. .version_id = 0,
  262. .minimum_version_id = 0,
  263. .minimum_version_id_old = 0,
  264. .fields = (VMStateField[]) {
  265. VMSTATE_UINT32(ms, struct PCITargetMap),
  266. VMSTATE_UINT32(la, struct PCITargetMap),
  267. VMSTATE_END_OF_LIST()
  268. }
  269. };
  270. static const VMStateDescription vmstate_ppc4xx_pci = {
  271. .name = "ppc4xx_pci",
  272. .version_id = 1,
  273. .minimum_version_id = 1,
  274. .minimum_version_id_old = 1,
  275. .fields = (VMStateField[]) {
  276. VMSTATE_PCI_DEVICE_POINTER(pci_dev, PPC4xxPCIState),
  277. VMSTATE_STRUCT_ARRAY(pmm, PPC4xxPCIState, PPC4xx_PCI_NR_PMMS, 1,
  278. vmstate_pci_master_map,
  279. struct PCIMasterMap),
  280. VMSTATE_STRUCT_ARRAY(ptm, PPC4xxPCIState, PPC4xx_PCI_NR_PTMS, 1,
  281. vmstate_pci_target_map,
  282. struct PCITargetMap),
  283. VMSTATE_END_OF_LIST()
  284. }
  285. };
  286. /* XXX Interrupt acknowledge cycles not supported. */
  287. PCIBus *ppc4xx_pci_init(CPUState *env, qemu_irq pci_irqs[4],
  288. target_phys_addr_t config_space,
  289. target_phys_addr_t int_ack,
  290. target_phys_addr_t special_cycle,
  291. target_phys_addr_t registers)
  292. {
  293. PPC4xxPCIState *controller;
  294. int index;
  295. static int ppc4xx_pci_id;
  296. uint8_t *pci_conf;
  297. controller = qemu_mallocz(sizeof(PPC4xxPCIState));
  298. controller->pci_state.bus = pci_register_bus(NULL, "pci",
  299. ppc4xx_pci_set_irq,
  300. ppc4xx_pci_map_irq,
  301. pci_irqs, 0, 4);
  302. controller->pci_dev = pci_register_device(controller->pci_state.bus,
  303. "host bridge", sizeof(PCIDevice),
  304. 0, NULL, NULL);
  305. pci_conf = controller->pci_dev->config;
  306. pci_config_set_vendor_id(pci_conf, PCI_VENDOR_ID_IBM);
  307. pci_config_set_device_id(pci_conf, PCI_DEVICE_ID_IBM_440GX);
  308. pci_config_set_class(pci_conf, PCI_CLASS_BRIDGE_OTHER);
  309. /* CFGADDR */
  310. index = cpu_register_io_memory(pci4xx_cfgaddr_read,
  311. pci4xx_cfgaddr_write, controller,
  312. DEVICE_LITTLE_ENDIAN);
  313. if (index < 0)
  314. goto free;
  315. cpu_register_physical_memory(config_space + PCIC0_CFGADDR, 4, index);
  316. /* CFGDATA */
  317. index = pci_host_data_register_mmio(&controller->pci_state, 1);
  318. if (index < 0)
  319. goto free;
  320. cpu_register_physical_memory(config_space + PCIC0_CFGDATA, 4, index);
  321. /* Internal registers */
  322. index = cpu_register_io_memory(pci_reg_read, pci_reg_write, controller,
  323. DEVICE_LITTLE_ENDIAN);
  324. if (index < 0)
  325. goto free;
  326. cpu_register_physical_memory(registers, PCI_REG_SIZE, index);
  327. qemu_register_reset(ppc4xx_pci_reset, controller);
  328. /* XXX load/save code not tested. */
  329. vmstate_register(&controller->pci_dev->qdev, ppc4xx_pci_id++,
  330. &vmstate_ppc4xx_pci, controller);
  331. return controller->pci_state.bus;
  332. free:
  333. printf("%s error\n", __func__);
  334. qemu_free(controller);
  335. return NULL;
  336. }