macio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * PowerMac MacIO device emulation
  3. *
  4. * Copyright (c) 2005-2007 Fabrice Bellard
  5. * Copyright (c) 2007 Jocelyn Mayer
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "hw/hw.h"
  26. #include "hw/ppc/mac.h"
  27. #include "hw/pci/pci.h"
  28. #include "hw/ppc/mac_dbdma.h"
  29. #include "hw/char/escc.h"
  30. #define TYPE_MACIO "macio"
  31. #define MACIO(obj) OBJECT_CHECK(MacIOState, (obj), TYPE_MACIO)
  32. typedef struct MacIOState
  33. {
  34. /*< private >*/
  35. PCIDevice parent;
  36. /*< public >*/
  37. MemoryRegion bar;
  38. CUDAState cuda;
  39. void *dbdma;
  40. MemoryRegion *pic_mem;
  41. MemoryRegion *escc_mem;
  42. } MacIOState;
  43. #define OLDWORLD_MACIO(obj) \
  44. OBJECT_CHECK(OldWorldMacIOState, (obj), TYPE_OLDWORLD_MACIO)
  45. typedef struct OldWorldMacIOState {
  46. /*< private >*/
  47. MacIOState parent_obj;
  48. /*< public >*/
  49. qemu_irq irqs[5];
  50. MacIONVRAMState nvram;
  51. MACIOIDEState ide[2];
  52. } OldWorldMacIOState;
  53. #define NEWWORLD_MACIO(obj) \
  54. OBJECT_CHECK(NewWorldMacIOState, (obj), TYPE_NEWWORLD_MACIO)
  55. typedef struct NewWorldMacIOState {
  56. /*< private >*/
  57. MacIOState parent_obj;
  58. /*< public >*/
  59. qemu_irq irqs[5];
  60. MACIOIDEState ide[2];
  61. } NewWorldMacIOState;
  62. /*
  63. * The mac-io has two interfaces to the ESCC. One is called "escc-legacy",
  64. * while the other one is the normal, current ESCC interface.
  65. *
  66. * The magic below creates memory aliases to spawn the escc-legacy device
  67. * purely by rerouting the respective registers to our escc region. This
  68. * works because the only difference between the two memory regions is the
  69. * register layout, not their semantics.
  70. *
  71. * Reference: ftp://ftp.software.ibm.com/rs6000/technology/spec/chrp/inwork/CHRP_IORef_1.0.pdf
  72. */
  73. static void macio_escc_legacy_setup(MacIOState *macio_state)
  74. {
  75. MemoryRegion *escc_legacy = g_new(MemoryRegion, 1);
  76. MemoryRegion *bar = &macio_state->bar;
  77. int i;
  78. static const int maps[] = {
  79. 0x00, 0x00,
  80. 0x02, 0x20,
  81. 0x04, 0x10,
  82. 0x06, 0x30,
  83. 0x08, 0x40,
  84. 0x0A, 0x50,
  85. 0x60, 0x60,
  86. 0x70, 0x70,
  87. 0x80, 0x70,
  88. 0x90, 0x80,
  89. 0xA0, 0x90,
  90. 0xB0, 0xA0,
  91. 0xC0, 0xB0,
  92. 0xD0, 0xC0,
  93. 0xE0, 0xD0,
  94. 0xF0, 0xE0,
  95. };
  96. memory_region_init(escc_legacy, NULL, "escc-legacy", 256);
  97. for (i = 0; i < ARRAY_SIZE(maps); i += 2) {
  98. MemoryRegion *port = g_new(MemoryRegion, 1);
  99. memory_region_init_alias(port, NULL, "escc-legacy-port",
  100. macio_state->escc_mem, maps[i+1], 0x2);
  101. memory_region_add_subregion(escc_legacy, maps[i], port);
  102. }
  103. memory_region_add_subregion(bar, 0x12000, escc_legacy);
  104. }
  105. static void macio_bar_setup(MacIOState *macio_state)
  106. {
  107. MemoryRegion *bar = &macio_state->bar;
  108. if (macio_state->escc_mem) {
  109. memory_region_add_subregion(bar, 0x13000, macio_state->escc_mem);
  110. macio_escc_legacy_setup(macio_state);
  111. }
  112. }
  113. static int macio_common_initfn(PCIDevice *d)
  114. {
  115. MacIOState *s = MACIO(d);
  116. SysBusDevice *sysbus_dev;
  117. int ret;
  118. d->config[0x3d] = 0x01; // interrupt on pin 1
  119. ret = qdev_init(DEVICE(&s->cuda));
  120. if (ret < 0) {
  121. return ret;
  122. }
  123. sysbus_dev = SYS_BUS_DEVICE(&s->cuda);
  124. memory_region_add_subregion(&s->bar, 0x16000,
  125. sysbus_mmio_get_region(sysbus_dev, 0));
  126. macio_bar_setup(s);
  127. pci_register_bar(d, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->bar);
  128. return 0;
  129. }
  130. static int macio_initfn_ide(MacIOState *s, MACIOIDEState *ide, qemu_irq irq0,
  131. qemu_irq irq1, int dmaid)
  132. {
  133. SysBusDevice *sysbus_dev;
  134. sysbus_dev = SYS_BUS_DEVICE(ide);
  135. sysbus_connect_irq(sysbus_dev, 0, irq0);
  136. sysbus_connect_irq(sysbus_dev, 1, irq1);
  137. macio_ide_register_dma(ide, s->dbdma, dmaid);
  138. return qdev_init(DEVICE(ide));
  139. }
  140. static int macio_oldworld_initfn(PCIDevice *d)
  141. {
  142. MacIOState *s = MACIO(d);
  143. OldWorldMacIOState *os = OLDWORLD_MACIO(d);
  144. SysBusDevice *sysbus_dev;
  145. int i;
  146. int cur_irq = 0;
  147. int ret = macio_common_initfn(d);
  148. if (ret < 0) {
  149. return ret;
  150. }
  151. sysbus_dev = SYS_BUS_DEVICE(&s->cuda);
  152. sysbus_connect_irq(sysbus_dev, 0, os->irqs[cur_irq++]);
  153. ret = qdev_init(DEVICE(&os->nvram));
  154. if (ret < 0) {
  155. return ret;
  156. }
  157. sysbus_dev = SYS_BUS_DEVICE(&os->nvram);
  158. memory_region_add_subregion(&s->bar, 0x60000,
  159. sysbus_mmio_get_region(sysbus_dev, 0));
  160. pmac_format_nvram_partition(&os->nvram, os->nvram.size);
  161. if (s->pic_mem) {
  162. /* Heathrow PIC */
  163. memory_region_add_subregion(&s->bar, 0x00000, s->pic_mem);
  164. }
  165. /* IDE buses */
  166. for (i = 0; i < ARRAY_SIZE(os->ide); i++) {
  167. qemu_irq irq0 = os->irqs[cur_irq++];
  168. qemu_irq irq1 = os->irqs[cur_irq++];
  169. ret = macio_initfn_ide(s, &os->ide[i], irq0, irq1, 0x16 + (i * 4));
  170. if (ret < 0) {
  171. return ret;
  172. }
  173. }
  174. return 0;
  175. }
  176. static void macio_init_ide(MacIOState *s, MACIOIDEState *ide, size_t ide_size,
  177. int index)
  178. {
  179. gchar *name;
  180. object_initialize(ide, ide_size, TYPE_MACIO_IDE);
  181. qdev_set_parent_bus(DEVICE(ide), sysbus_get_default());
  182. memory_region_add_subregion(&s->bar, 0x1f000 + ((index + 1) * 0x1000),
  183. &ide->mem);
  184. name = g_strdup_printf("ide[%i]", index);
  185. object_property_add_child(OBJECT(s), name, OBJECT(ide), NULL);
  186. g_free(name);
  187. }
  188. static void macio_oldworld_init(Object *obj)
  189. {
  190. MacIOState *s = MACIO(obj);
  191. OldWorldMacIOState *os = OLDWORLD_MACIO(obj);
  192. DeviceState *dev;
  193. int i;
  194. qdev_init_gpio_out(DEVICE(obj), os->irqs, ARRAY_SIZE(os->irqs));
  195. object_initialize(&os->nvram, sizeof(os->nvram), TYPE_MACIO_NVRAM);
  196. dev = DEVICE(&os->nvram);
  197. qdev_prop_set_uint32(dev, "size", 0x2000);
  198. qdev_prop_set_uint32(dev, "it_shift", 4);
  199. for (i = 0; i < 2; i++) {
  200. macio_init_ide(s, &os->ide[i], sizeof(os->ide[i]), i);
  201. }
  202. }
  203. static void timer_write(void *opaque, hwaddr addr, uint64_t value,
  204. unsigned size)
  205. {
  206. }
  207. static uint64_t timer_read(void *opaque, hwaddr addr, unsigned size)
  208. {
  209. uint32_t value = 0;
  210. switch (addr) {
  211. case 0x38:
  212. value = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  213. break;
  214. case 0x3c:
  215. value = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) >> 32;
  216. break;
  217. }
  218. return value;
  219. }
  220. static const MemoryRegionOps timer_ops = {
  221. .read = timer_read,
  222. .write = timer_write,
  223. .endianness = DEVICE_NATIVE_ENDIAN,
  224. };
  225. static int macio_newworld_initfn(PCIDevice *d)
  226. {
  227. MacIOState *s = MACIO(d);
  228. NewWorldMacIOState *ns = NEWWORLD_MACIO(d);
  229. SysBusDevice *sysbus_dev;
  230. MemoryRegion *timer_memory = g_new(MemoryRegion, 1);
  231. int i;
  232. int cur_irq = 0;
  233. int ret = macio_common_initfn(d);
  234. if (ret < 0) {
  235. return ret;
  236. }
  237. sysbus_dev = SYS_BUS_DEVICE(&s->cuda);
  238. sysbus_connect_irq(sysbus_dev, 0, ns->irqs[cur_irq++]);
  239. if (s->pic_mem) {
  240. /* OpenPIC */
  241. memory_region_add_subregion(&s->bar, 0x40000, s->pic_mem);
  242. }
  243. /* IDE buses */
  244. for (i = 0; i < ARRAY_SIZE(ns->ide); i++) {
  245. qemu_irq irq0 = ns->irqs[cur_irq++];
  246. qemu_irq irq1 = ns->irqs[cur_irq++];
  247. ret = macio_initfn_ide(s, &ns->ide[i], irq0, irq1, 0x16 + (i * 4));
  248. if (ret < 0) {
  249. return ret;
  250. }
  251. }
  252. /* Timer */
  253. memory_region_init_io(timer_memory, OBJECT(s), &timer_ops, NULL, "timer",
  254. 0x1000);
  255. memory_region_add_subregion(&s->bar, 0x15000, timer_memory);
  256. return 0;
  257. }
  258. static void macio_newworld_init(Object *obj)
  259. {
  260. MacIOState *s = MACIO(obj);
  261. NewWorldMacIOState *ns = NEWWORLD_MACIO(obj);
  262. int i;
  263. qdev_init_gpio_out(DEVICE(obj), ns->irqs, ARRAY_SIZE(ns->irqs));
  264. for (i = 0; i < 2; i++) {
  265. macio_init_ide(s, &ns->ide[i], sizeof(ns->ide[i]), i);
  266. }
  267. }
  268. static void macio_instance_init(Object *obj)
  269. {
  270. MacIOState *s = MACIO(obj);
  271. MemoryRegion *dbdma_mem;
  272. memory_region_init(&s->bar, NULL, "macio", 0x80000);
  273. object_initialize(&s->cuda, sizeof(s->cuda), TYPE_CUDA);
  274. qdev_set_parent_bus(DEVICE(&s->cuda), sysbus_get_default());
  275. object_property_add_child(obj, "cuda", OBJECT(&s->cuda), NULL);
  276. s->dbdma = DBDMA_init(&dbdma_mem);
  277. memory_region_add_subregion(&s->bar, 0x08000, dbdma_mem);
  278. }
  279. static void macio_oldworld_class_init(ObjectClass *oc, void *data)
  280. {
  281. PCIDeviceClass *pdc = PCI_DEVICE_CLASS(oc);
  282. pdc->init = macio_oldworld_initfn;
  283. pdc->device_id = PCI_DEVICE_ID_APPLE_343S1201;
  284. }
  285. static void macio_newworld_class_init(ObjectClass *oc, void *data)
  286. {
  287. PCIDeviceClass *pdc = PCI_DEVICE_CLASS(oc);
  288. pdc->init = macio_newworld_initfn;
  289. pdc->device_id = PCI_DEVICE_ID_APPLE_UNI_N_KEYL;
  290. }
  291. static void macio_class_init(ObjectClass *klass, void *data)
  292. {
  293. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  294. k->vendor_id = PCI_VENDOR_ID_APPLE;
  295. k->class_id = PCI_CLASS_OTHERS << 8;
  296. }
  297. static const TypeInfo macio_oldworld_type_info = {
  298. .name = TYPE_OLDWORLD_MACIO,
  299. .parent = TYPE_MACIO,
  300. .instance_size = sizeof(OldWorldMacIOState),
  301. .instance_init = macio_oldworld_init,
  302. .class_init = macio_oldworld_class_init,
  303. };
  304. static const TypeInfo macio_newworld_type_info = {
  305. .name = TYPE_NEWWORLD_MACIO,
  306. .parent = TYPE_MACIO,
  307. .instance_size = sizeof(NewWorldMacIOState),
  308. .instance_init = macio_newworld_init,
  309. .class_init = macio_newworld_class_init,
  310. };
  311. static const TypeInfo macio_type_info = {
  312. .name = TYPE_MACIO,
  313. .parent = TYPE_PCI_DEVICE,
  314. .instance_size = sizeof(MacIOState),
  315. .instance_init = macio_instance_init,
  316. .abstract = true,
  317. .class_init = macio_class_init,
  318. };
  319. static void macio_register_types(void)
  320. {
  321. type_register_static(&macio_type_info);
  322. type_register_static(&macio_oldworld_type_info);
  323. type_register_static(&macio_newworld_type_info);
  324. }
  325. type_init(macio_register_types)
  326. void macio_init(PCIDevice *d,
  327. MemoryRegion *pic_mem,
  328. MemoryRegion *escc_mem)
  329. {
  330. MacIOState *macio_state = MACIO(d);
  331. macio_state->pic_mem = pic_mem;
  332. macio_state->escc_mem = escc_mem;
  333. /* Note: this code is strongly inspirated from the corresponding code
  334. in PearPC */
  335. qdev_init_nofail(DEVICE(d));
  336. }