2
0

unin_pci.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * QEMU Uninorth PCI host (for all Mac99 and newer machines)
  3. *
  4. * Copyright (c) 2006 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "hw.h"
  25. #include "ppc_mac.h"
  26. #include "pci.h"
  27. #include "pci_host.h"
  28. /* debug UniNorth */
  29. //#define DEBUG_UNIN
  30. #ifdef DEBUG_UNIN
  31. #define UNIN_DPRINTF(fmt, ...) \
  32. do { printf("UNIN: " fmt , ## __VA_ARGS__); } while (0)
  33. #else
  34. #define UNIN_DPRINTF(fmt, ...)
  35. #endif
  36. static const int unin_irq_line[] = { 0x1b, 0x1c, 0x1d, 0x1e };
  37. typedef struct UNINState {
  38. SysBusDevice busdev;
  39. PCIHostState host_state;
  40. MemoryRegion pci_mmio;
  41. MemoryRegion pci_hole;
  42. } UNINState;
  43. static int pci_unin_map_irq(PCIDevice *pci_dev, int irq_num)
  44. {
  45. int retval;
  46. int devfn = pci_dev->devfn & 0x00FFFFFF;
  47. retval = (((devfn >> 11) & 0x1F) + irq_num) & 3;
  48. return retval;
  49. }
  50. static void pci_unin_set_irq(void *opaque, int irq_num, int level)
  51. {
  52. qemu_irq *pic = opaque;
  53. UNIN_DPRINTF("%s: setting INT %d = %d\n", __func__,
  54. unin_irq_line[irq_num], level);
  55. qemu_set_irq(pic[unin_irq_line[irq_num]], level);
  56. }
  57. static void pci_unin_reset(void *opaque)
  58. {
  59. }
  60. static uint32_t unin_get_config_reg(uint32_t reg, uint32_t addr)
  61. {
  62. uint32_t retval;
  63. if (reg & (1u << 31)) {
  64. /* XXX OpenBIOS compatibility hack */
  65. retval = reg | (addr & 3);
  66. } else if (reg & 1) {
  67. /* CFA1 style */
  68. retval = (reg & ~7u) | (addr & 7);
  69. } else {
  70. uint32_t slot, func;
  71. /* Grab CFA0 style values */
  72. slot = ffs(reg & 0xfffff800) - 1;
  73. func = (reg >> 8) & 7;
  74. /* ... and then convert them to x86 format */
  75. /* config pointer */
  76. retval = (reg & (0xff - 7)) | (addr & 7);
  77. /* slot */
  78. retval |= slot << 11;
  79. /* fn */
  80. retval |= func << 8;
  81. }
  82. UNIN_DPRINTF("Converted config space accessor %08x/%08x -> %08x\n",
  83. reg, addr, retval);
  84. return retval;
  85. }
  86. static void unin_data_write(void *opaque, target_phys_addr_t addr,
  87. uint64_t val, unsigned len)
  88. {
  89. UNINState *s = opaque;
  90. UNIN_DPRINTF("write addr %" TARGET_FMT_plx " len %d val %"PRIx64"\n",
  91. addr, len, val);
  92. pci_data_write(s->host_state.bus,
  93. unin_get_config_reg(s->host_state.config_reg, addr),
  94. val, len);
  95. }
  96. static uint64_t unin_data_read(void *opaque, target_phys_addr_t addr,
  97. unsigned len)
  98. {
  99. UNINState *s = opaque;
  100. uint32_t val;
  101. val = pci_data_read(s->host_state.bus,
  102. unin_get_config_reg(s->host_state.config_reg, addr),
  103. len);
  104. UNIN_DPRINTF("read addr %" TARGET_FMT_plx " len %d val %x\n",
  105. addr, len, val);
  106. return val;
  107. }
  108. static const MemoryRegionOps unin_data_ops = {
  109. .read = unin_data_read,
  110. .write = unin_data_write,
  111. .endianness = DEVICE_LITTLE_ENDIAN,
  112. };
  113. static int pci_unin_main_init_device(SysBusDevice *dev)
  114. {
  115. UNINState *s;
  116. /* Use values found on a real PowerMac */
  117. /* Uninorth main bus */
  118. s = FROM_SYSBUS(UNINState, dev);
  119. memory_region_init_io(&s->host_state.conf_mem, &pci_host_conf_le_ops,
  120. &s->host_state, "pci-conf-idx", 0x1000);
  121. memory_region_init_io(&s->host_state.data_mem, &unin_data_ops, s,
  122. "pci-conf-data", 0x1000);
  123. sysbus_init_mmio_region(dev, &s->host_state.conf_mem);
  124. sysbus_init_mmio_region(dev, &s->host_state.data_mem);
  125. qemu_register_reset(pci_unin_reset, &s->host_state);
  126. return 0;
  127. }
  128. static int pci_u3_agp_init_device(SysBusDevice *dev)
  129. {
  130. UNINState *s;
  131. /* Uninorth U3 AGP bus */
  132. s = FROM_SYSBUS(UNINState, dev);
  133. memory_region_init_io(&s->host_state.conf_mem, &pci_host_conf_le_ops,
  134. &s->host_state, "pci-conf-idx", 0x1000);
  135. memory_region_init_io(&s->host_state.data_mem, &unin_data_ops, s,
  136. "pci-conf-data", 0x1000);
  137. sysbus_init_mmio_region(dev, &s->host_state.conf_mem);
  138. sysbus_init_mmio_region(dev, &s->host_state.data_mem);
  139. qemu_register_reset(pci_unin_reset, &s->host_state);
  140. return 0;
  141. }
  142. static int pci_unin_agp_init_device(SysBusDevice *dev)
  143. {
  144. UNINState *s;
  145. /* Uninorth AGP bus */
  146. s = FROM_SYSBUS(UNINState, dev);
  147. memory_region_init_io(&s->host_state.conf_mem, &pci_host_conf_le_ops,
  148. &s->host_state, "pci-conf-idx", 0x1000);
  149. memory_region_init_io(&s->host_state.data_mem, &pci_host_data_le_ops,
  150. &s->host_state, "pci-conf-data", 0x1000);
  151. sysbus_init_mmio_region(dev, &s->host_state.conf_mem);
  152. sysbus_init_mmio_region(dev, &s->host_state.data_mem);
  153. return 0;
  154. }
  155. static int pci_unin_internal_init_device(SysBusDevice *dev)
  156. {
  157. UNINState *s;
  158. /* Uninorth internal bus */
  159. s = FROM_SYSBUS(UNINState, dev);
  160. memory_region_init_io(&s->host_state.conf_mem, &pci_host_conf_le_ops,
  161. &s->host_state, "pci-conf-idx", 0x1000);
  162. memory_region_init_io(&s->host_state.data_mem, &pci_host_data_le_ops,
  163. &s->host_state, "pci-conf-data", 0x1000);
  164. sysbus_init_mmio_region(dev, &s->host_state.conf_mem);
  165. sysbus_init_mmio_region(dev, &s->host_state.data_mem);
  166. return 0;
  167. }
  168. PCIBus *pci_pmac_init(qemu_irq *pic,
  169. MemoryRegion *address_space_mem,
  170. MemoryRegion *address_space_io)
  171. {
  172. DeviceState *dev;
  173. SysBusDevice *s;
  174. UNINState *d;
  175. /* Use values found on a real PowerMac */
  176. /* Uninorth main bus */
  177. dev = qdev_create(NULL, "uni-north");
  178. qdev_init_nofail(dev);
  179. s = sysbus_from_qdev(dev);
  180. d = FROM_SYSBUS(UNINState, s);
  181. memory_region_init(&d->pci_mmio, "pci-mmio", 0x100000000ULL);
  182. memory_region_init_alias(&d->pci_hole, "pci-hole", &d->pci_mmio,
  183. 0x80000000ULL, 0x70000000ULL);
  184. memory_region_add_subregion(address_space_mem, 0x80000000ULL,
  185. &d->pci_hole);
  186. d->host_state.bus = pci_register_bus(&d->busdev.qdev, "pci",
  187. pci_unin_set_irq, pci_unin_map_irq,
  188. pic,
  189. &d->pci_mmio,
  190. address_space_io,
  191. PCI_DEVFN(11, 0), 4);
  192. #if 0
  193. pci_create_simple(d->host_state.bus, PCI_DEVFN(11, 0), "uni-north");
  194. #endif
  195. sysbus_mmio_map(s, 0, 0xf2800000);
  196. sysbus_mmio_map(s, 1, 0xf2c00000);
  197. /* DEC 21154 bridge */
  198. #if 0
  199. /* XXX: not activated as PPC BIOS doesn't handle multiple buses properly */
  200. pci_create_simple(d->host_state.bus, PCI_DEVFN(12, 0), "dec-21154");
  201. #endif
  202. /* Uninorth AGP bus */
  203. pci_create_simple(d->host_state.bus, PCI_DEVFN(11, 0), "uni-north-agp");
  204. dev = qdev_create(NULL, "uni-north-agp");
  205. qdev_init_nofail(dev);
  206. s = sysbus_from_qdev(dev);
  207. sysbus_mmio_map(s, 0, 0xf0800000);
  208. sysbus_mmio_map(s, 1, 0xf0c00000);
  209. /* Uninorth internal bus */
  210. #if 0
  211. /* XXX: not needed for now */
  212. pci_create_simple(d->host_state.bus, PCI_DEVFN(14, 0), "uni-north-pci");
  213. dev = qdev_create(NULL, "uni-north-pci");
  214. qdev_init_nofail(dev);
  215. s = sysbus_from_qdev(dev);
  216. sysbus_mmio_map(s, 0, 0xf4800000);
  217. sysbus_mmio_map(s, 1, 0xf4c00000);
  218. #endif
  219. return d->host_state.bus;
  220. }
  221. PCIBus *pci_pmac_u3_init(qemu_irq *pic,
  222. MemoryRegion *address_space_mem,
  223. MemoryRegion *address_space_io)
  224. {
  225. DeviceState *dev;
  226. SysBusDevice *s;
  227. UNINState *d;
  228. /* Uninorth AGP bus */
  229. dev = qdev_create(NULL, "u3-agp");
  230. qdev_init_nofail(dev);
  231. s = sysbus_from_qdev(dev);
  232. d = FROM_SYSBUS(UNINState, s);
  233. memory_region_init(&d->pci_mmio, "pci-mmio", 0x100000000ULL);
  234. memory_region_init_alias(&d->pci_hole, "pci-hole", &d->pci_mmio,
  235. 0x80000000ULL, 0x70000000ULL);
  236. memory_region_add_subregion(address_space_mem, 0x80000000ULL,
  237. &d->pci_hole);
  238. d->host_state.bus = pci_register_bus(&d->busdev.qdev, "pci",
  239. pci_unin_set_irq, pci_unin_map_irq,
  240. pic,
  241. &d->pci_mmio,
  242. address_space_io,
  243. PCI_DEVFN(11, 0), 4);
  244. sysbus_mmio_map(s, 0, 0xf0800000);
  245. sysbus_mmio_map(s, 1, 0xf0c00000);
  246. pci_create_simple(d->host_state.bus, 11 << 3, "u3-agp");
  247. return d->host_state.bus;
  248. }
  249. static int unin_main_pci_host_init(PCIDevice *d)
  250. {
  251. d->config[0x0C] = 0x08; // cache_line_size
  252. d->config[0x0D] = 0x10; // latency_timer
  253. d->config[0x34] = 0x00; // capabilities_pointer
  254. return 0;
  255. }
  256. static int unin_agp_pci_host_init(PCIDevice *d)
  257. {
  258. d->config[0x0C] = 0x08; // cache_line_size
  259. d->config[0x0D] = 0x10; // latency_timer
  260. // d->config[0x34] = 0x80; // capabilities_pointer
  261. return 0;
  262. }
  263. static int u3_agp_pci_host_init(PCIDevice *d)
  264. {
  265. /* cache line size */
  266. d->config[0x0C] = 0x08;
  267. /* latency timer */
  268. d->config[0x0D] = 0x10;
  269. return 0;
  270. }
  271. static int unin_internal_pci_host_init(PCIDevice *d)
  272. {
  273. d->config[0x0C] = 0x08; // cache_line_size
  274. d->config[0x0D] = 0x10; // latency_timer
  275. d->config[0x34] = 0x00; // capabilities_pointer
  276. return 0;
  277. }
  278. static PCIDeviceInfo unin_main_pci_host_info = {
  279. .qdev.name = "uni-north",
  280. .qdev.size = sizeof(PCIDevice),
  281. .init = unin_main_pci_host_init,
  282. .vendor_id = PCI_VENDOR_ID_APPLE,
  283. .device_id = PCI_DEVICE_ID_APPLE_UNI_N_PCI,
  284. .revision = 0x00,
  285. .class_id = PCI_CLASS_BRIDGE_HOST,
  286. };
  287. static PCIDeviceInfo u3_agp_pci_host_info = {
  288. .qdev.name = "u3-agp",
  289. .qdev.size = sizeof(PCIDevice),
  290. .init = u3_agp_pci_host_init,
  291. .vendor_id = PCI_VENDOR_ID_APPLE,
  292. .device_id = PCI_DEVICE_ID_APPLE_U3_AGP,
  293. .revision = 0x00,
  294. .class_id = PCI_CLASS_BRIDGE_HOST,
  295. };
  296. static PCIDeviceInfo unin_agp_pci_host_info = {
  297. .qdev.name = "uni-north-agp",
  298. .qdev.size = sizeof(PCIDevice),
  299. .init = unin_agp_pci_host_init,
  300. .vendor_id = PCI_VENDOR_ID_APPLE,
  301. .device_id = PCI_DEVICE_ID_APPLE_UNI_N_AGP,
  302. .revision = 0x00,
  303. .class_id = PCI_CLASS_BRIDGE_HOST,
  304. };
  305. static PCIDeviceInfo unin_internal_pci_host_info = {
  306. .qdev.name = "uni-north-pci",
  307. .qdev.size = sizeof(PCIDevice),
  308. .init = unin_internal_pci_host_init,
  309. .vendor_id = PCI_VENDOR_ID_APPLE,
  310. .device_id = PCI_DEVICE_ID_APPLE_UNI_N_I_PCI,
  311. .revision = 0x00,
  312. .class_id = PCI_CLASS_BRIDGE_HOST,
  313. };
  314. static void unin_register_devices(void)
  315. {
  316. sysbus_register_dev("uni-north", sizeof(UNINState),
  317. pci_unin_main_init_device);
  318. pci_qdev_register(&unin_main_pci_host_info);
  319. sysbus_register_dev("u3-agp", sizeof(UNINState),
  320. pci_u3_agp_init_device);
  321. pci_qdev_register(&u3_agp_pci_host_info);
  322. sysbus_register_dev("uni-north-agp", sizeof(UNINState),
  323. pci_unin_agp_init_device);
  324. pci_qdev_register(&unin_agp_pci_host_info);
  325. sysbus_register_dev("uni-north-pci", sizeof(UNINState),
  326. pci_unin_internal_init_device);
  327. pci_qdev_register(&unin_internal_pci_host_info);
  328. }
  329. device_init(unin_register_devices)