unin_pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. ReadWriteHandler data_handler;
  41. } UNINState;
  42. static int pci_unin_map_irq(PCIDevice *pci_dev, int irq_num)
  43. {
  44. int retval;
  45. int devfn = pci_dev->devfn & 0x00FFFFFF;
  46. retval = (((devfn >> 11) & 0x1F) + irq_num) & 3;
  47. return retval;
  48. }
  49. static void pci_unin_set_irq(void *opaque, int irq_num, int level)
  50. {
  51. qemu_irq *pic = opaque;
  52. UNIN_DPRINTF("%s: setting INT %d = %d\n", __func__,
  53. unin_irq_line[irq_num], level);
  54. qemu_set_irq(pic[unin_irq_line[irq_num]], level);
  55. }
  56. static void pci_unin_reset(void *opaque)
  57. {
  58. }
  59. static uint32_t unin_get_config_reg(uint32_t reg, uint32_t addr)
  60. {
  61. uint32_t retval;
  62. if (reg & (1u << 31)) {
  63. /* XXX OpenBIOS compatibility hack */
  64. retval = reg | (addr & 3);
  65. } else if (reg & 1) {
  66. /* CFA1 style */
  67. retval = (reg & ~7u) | (addr & 7);
  68. } else {
  69. uint32_t slot, func;
  70. /* Grab CFA0 style values */
  71. slot = ffs(reg & 0xfffff800) - 1;
  72. func = (reg >> 8) & 7;
  73. /* ... and then convert them to x86 format */
  74. /* config pointer */
  75. retval = (reg & (0xff - 7)) | (addr & 7);
  76. /* slot */
  77. retval |= slot << 11;
  78. /* fn */
  79. retval |= func << 8;
  80. }
  81. UNIN_DPRINTF("Converted config space accessor %08x/%08x -> %08x\n",
  82. reg, addr, retval);
  83. return retval;
  84. }
  85. static void unin_data_write(ReadWriteHandler *handler,
  86. pcibus_t addr, uint32_t val, int len)
  87. {
  88. UNINState *s = container_of(handler, UNINState, data_handler);
  89. UNIN_DPRINTF("write addr %" FMT_PCIBUS " len %d val %x\n", addr, len, val);
  90. pci_data_write(s->host_state.bus,
  91. unin_get_config_reg(s->host_state.config_reg, addr),
  92. val, len);
  93. }
  94. static uint32_t unin_data_read(ReadWriteHandler *handler,
  95. pcibus_t addr, int len)
  96. {
  97. UNINState *s = container_of(handler, UNINState, data_handler);
  98. uint32_t val;
  99. val = pci_data_read(s->host_state.bus,
  100. unin_get_config_reg(s->host_state.config_reg, addr),
  101. len);
  102. UNIN_DPRINTF("read addr %" FMT_PCIBUS " len %d val %x\n", addr, len, val);
  103. return val;
  104. }
  105. static int pci_unin_main_init_device(SysBusDevice *dev)
  106. {
  107. UNINState *s;
  108. int pci_mem_config, pci_mem_data;
  109. /* Use values found on a real PowerMac */
  110. /* Uninorth main bus */
  111. s = FROM_SYSBUS(UNINState, dev);
  112. pci_mem_config = pci_host_conf_register_mmio(&s->host_state,
  113. DEVICE_LITTLE_ENDIAN);
  114. s->data_handler.read = unin_data_read;
  115. s->data_handler.write = unin_data_write;
  116. pci_mem_data = cpu_register_io_memory_simple(&s->data_handler,
  117. DEVICE_LITTLE_ENDIAN);
  118. sysbus_init_mmio(dev, 0x1000, pci_mem_config);
  119. sysbus_init_mmio(dev, 0x1000, pci_mem_data);
  120. qemu_register_reset(pci_unin_reset, &s->host_state);
  121. return 0;
  122. }
  123. static int pci_u3_agp_init_device(SysBusDevice *dev)
  124. {
  125. UNINState *s;
  126. int pci_mem_config, pci_mem_data;
  127. /* Uninorth U3 AGP bus */
  128. s = FROM_SYSBUS(UNINState, dev);
  129. pci_mem_config = pci_host_conf_register_mmio(&s->host_state,
  130. DEVICE_LITTLE_ENDIAN);
  131. s->data_handler.read = unin_data_read;
  132. s->data_handler.write = unin_data_write;
  133. pci_mem_data = cpu_register_io_memory_simple(&s->data_handler,
  134. DEVICE_LITTLE_ENDIAN);
  135. sysbus_init_mmio(dev, 0x1000, pci_mem_config);
  136. sysbus_init_mmio(dev, 0x1000, pci_mem_data);
  137. qemu_register_reset(pci_unin_reset, &s->host_state);
  138. return 0;
  139. }
  140. static int pci_unin_agp_init_device(SysBusDevice *dev)
  141. {
  142. UNINState *s;
  143. int pci_mem_config, pci_mem_data;
  144. /* Uninorth AGP bus */
  145. s = FROM_SYSBUS(UNINState, dev);
  146. pci_mem_config = pci_host_conf_register_mmio(&s->host_state,
  147. DEVICE_LITTLE_ENDIAN);
  148. pci_mem_data = pci_host_data_register_mmio(&s->host_state,
  149. DEVICE_LITTLE_ENDIAN);
  150. sysbus_init_mmio(dev, 0x1000, pci_mem_config);
  151. sysbus_init_mmio(dev, 0x1000, pci_mem_data);
  152. return 0;
  153. }
  154. static int pci_unin_internal_init_device(SysBusDevice *dev)
  155. {
  156. UNINState *s;
  157. int pci_mem_config, pci_mem_data;
  158. /* Uninorth internal bus */
  159. s = FROM_SYSBUS(UNINState, dev);
  160. pci_mem_config = pci_host_conf_register_mmio(&s->host_state,
  161. DEVICE_LITTLE_ENDIAN);
  162. pci_mem_data = pci_host_data_register_mmio(&s->host_state,
  163. DEVICE_LITTLE_ENDIAN);
  164. sysbus_init_mmio(dev, 0x1000, pci_mem_config);
  165. sysbus_init_mmio(dev, 0x1000, pci_mem_data);
  166. return 0;
  167. }
  168. PCIBus *pci_pmac_init(qemu_irq *pic)
  169. {
  170. DeviceState *dev;
  171. SysBusDevice *s;
  172. UNINState *d;
  173. /* Use values found on a real PowerMac */
  174. /* Uninorth main bus */
  175. dev = qdev_create(NULL, "uni-north");
  176. qdev_init_nofail(dev);
  177. s = sysbus_from_qdev(dev);
  178. d = FROM_SYSBUS(UNINState, s);
  179. d->host_state.bus = pci_register_bus(&d->busdev.qdev, "pci",
  180. pci_unin_set_irq, pci_unin_map_irq,
  181. pic, PCI_DEVFN(11, 0), 4);
  182. #if 0
  183. pci_create_simple(d->host_state.bus, PCI_DEVFN(11, 0), "uni-north");
  184. #endif
  185. sysbus_mmio_map(s, 0, 0xf2800000);
  186. sysbus_mmio_map(s, 1, 0xf2c00000);
  187. /* DEC 21154 bridge */
  188. #if 0
  189. /* XXX: not activated as PPC BIOS doesn't handle multiple buses properly */
  190. pci_create_simple(d->host_state.bus, PCI_DEVFN(12, 0), "dec-21154");
  191. #endif
  192. /* Uninorth AGP bus */
  193. pci_create_simple(d->host_state.bus, PCI_DEVFN(11, 0), "uni-north-agp");
  194. dev = qdev_create(NULL, "uni-north-agp");
  195. qdev_init_nofail(dev);
  196. s = sysbus_from_qdev(dev);
  197. sysbus_mmio_map(s, 0, 0xf0800000);
  198. sysbus_mmio_map(s, 1, 0xf0c00000);
  199. /* Uninorth internal bus */
  200. #if 0
  201. /* XXX: not needed for now */
  202. pci_create_simple(d->host_state.bus, PCI_DEVFN(14, 0), "uni-north-pci");
  203. dev = qdev_create(NULL, "uni-north-pci");
  204. qdev_init_nofail(dev);
  205. s = sysbus_from_qdev(dev);
  206. sysbus_mmio_map(s, 0, 0xf4800000);
  207. sysbus_mmio_map(s, 1, 0xf4c00000);
  208. #endif
  209. return d->host_state.bus;
  210. }
  211. PCIBus *pci_pmac_u3_init(qemu_irq *pic)
  212. {
  213. DeviceState *dev;
  214. SysBusDevice *s;
  215. UNINState *d;
  216. /* Uninorth AGP bus */
  217. dev = qdev_create(NULL, "u3-agp");
  218. qdev_init_nofail(dev);
  219. s = sysbus_from_qdev(dev);
  220. d = FROM_SYSBUS(UNINState, s);
  221. d->host_state.bus = pci_register_bus(&d->busdev.qdev, "pci",
  222. pci_unin_set_irq, pci_unin_map_irq,
  223. pic, PCI_DEVFN(11, 0), 4);
  224. sysbus_mmio_map(s, 0, 0xf0800000);
  225. sysbus_mmio_map(s, 1, 0xf0c00000);
  226. pci_create_simple(d->host_state.bus, 11 << 3, "u3-agp");
  227. return d->host_state.bus;
  228. }
  229. static int unin_main_pci_host_init(PCIDevice *d)
  230. {
  231. d->config[0x0C] = 0x08; // cache_line_size
  232. d->config[0x0D] = 0x10; // latency_timer
  233. d->config[0x34] = 0x00; // capabilities_pointer
  234. return 0;
  235. }
  236. static int unin_agp_pci_host_init(PCIDevice *d)
  237. {
  238. d->config[0x0C] = 0x08; // cache_line_size
  239. d->config[0x0D] = 0x10; // latency_timer
  240. // d->config[0x34] = 0x80; // capabilities_pointer
  241. return 0;
  242. }
  243. static int u3_agp_pci_host_init(PCIDevice *d)
  244. {
  245. /* cache line size */
  246. d->config[0x0C] = 0x08;
  247. /* latency timer */
  248. d->config[0x0D] = 0x10;
  249. return 0;
  250. }
  251. static int unin_internal_pci_host_init(PCIDevice *d)
  252. {
  253. d->config[0x0C] = 0x08; // cache_line_size
  254. d->config[0x0D] = 0x10; // latency_timer
  255. d->config[0x34] = 0x00; // capabilities_pointer
  256. return 0;
  257. }
  258. static PCIDeviceInfo unin_main_pci_host_info = {
  259. .qdev.name = "uni-north",
  260. .qdev.size = sizeof(PCIDevice),
  261. .init = unin_main_pci_host_init,
  262. .vendor_id = PCI_VENDOR_ID_APPLE,
  263. .device_id = PCI_DEVICE_ID_APPLE_UNI_N_PCI,
  264. .revision = 0x00,
  265. .class_id = PCI_CLASS_BRIDGE_HOST,
  266. };
  267. static PCIDeviceInfo u3_agp_pci_host_info = {
  268. .qdev.name = "u3-agp",
  269. .qdev.size = sizeof(PCIDevice),
  270. .init = u3_agp_pci_host_init,
  271. .vendor_id = PCI_VENDOR_ID_APPLE,
  272. .device_id = PCI_DEVICE_ID_APPLE_U3_AGP,
  273. .revision = 0x00,
  274. .class_id = PCI_CLASS_BRIDGE_HOST,
  275. };
  276. static PCIDeviceInfo unin_agp_pci_host_info = {
  277. .qdev.name = "uni-north-agp",
  278. .qdev.size = sizeof(PCIDevice),
  279. .init = unin_agp_pci_host_init,
  280. .vendor_id = PCI_VENDOR_ID_APPLE,
  281. .device_id = PCI_DEVICE_ID_APPLE_UNI_N_AGP,
  282. .revision = 0x00,
  283. .class_id = PCI_CLASS_BRIDGE_HOST,
  284. };
  285. static PCIDeviceInfo unin_internal_pci_host_info = {
  286. .qdev.name = "uni-north-pci",
  287. .qdev.size = sizeof(PCIDevice),
  288. .init = unin_internal_pci_host_init,
  289. .vendor_id = PCI_VENDOR_ID_APPLE,
  290. .device_id = PCI_DEVICE_ID_APPLE_UNI_N_I_PCI,
  291. .revision = 0x00,
  292. .class_id = PCI_CLASS_BRIDGE_HOST,
  293. };
  294. static void unin_register_devices(void)
  295. {
  296. sysbus_register_dev("uni-north", sizeof(UNINState),
  297. pci_unin_main_init_device);
  298. pci_qdev_register(&unin_main_pci_host_info);
  299. sysbus_register_dev("u3-agp", sizeof(UNINState),
  300. pci_u3_agp_init_device);
  301. pci_qdev_register(&u3_agp_pci_host_info);
  302. sysbus_register_dev("uni-north-agp", sizeof(UNINState),
  303. pci_unin_agp_init_device);
  304. pci_qdev_register(&unin_agp_pci_host_info);
  305. sysbus_register_dev("uni-north-pci", sizeof(UNINState),
  306. pci_unin_internal_init_device);
  307. pci_qdev_register(&unin_internal_pci_host_info);
  308. }
  309. device_init(unin_register_devices)