ppc4xx_pci.c 11 KB

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