xref: /qemu/hw/misc/macio/macio.c (revision 7ee9edfd)
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 "qemu/osdep.h"
26 #include "qapi/error.h"
27 #include "hw/hw.h"
28 #include "hw/ppc/mac.h"
29 #include "hw/misc/macio/cuda.h"
30 #include "hw/pci/pci.h"
31 #include "hw/ppc/mac_dbdma.h"
32 #include "hw/char/escc.h"
33 #include "hw/misc/macio/macio.h"
34 #include "hw/intc/heathrow_pic.h"
35 
36 /* Note: this code is strongly inspirated from the corresponding code
37  * in PearPC */
38 
39 /*
40  * The mac-io has two interfaces to the ESCC. One is called "escc-legacy",
41  * while the other one is the normal, current ESCC interface.
42  *
43  * The magic below creates memory aliases to spawn the escc-legacy device
44  * purely by rerouting the respective registers to our escc region. This
45  * works because the only difference between the two memory regions is the
46  * register layout, not their semantics.
47  *
48  * Reference: ftp://ftp.software.ibm.com/rs6000/technology/spec/chrp/inwork/CHRP_IORef_1.0.pdf
49  */
50 static void macio_escc_legacy_setup(MacIOState *s)
51 {
52     ESCCState *escc = ESCC(&s->escc);
53     SysBusDevice *sbd = SYS_BUS_DEVICE(escc);
54     MemoryRegion *escc_legacy = g_new(MemoryRegion, 1);
55     MemoryRegion *bar = &s->bar;
56     int i;
57     static const int maps[] = {
58         0x00, 0x00, /* Command B */
59         0x02, 0x20, /* Command A */
60         0x04, 0x10, /* Data B */
61         0x06, 0x30, /* Data A */
62         0x08, 0x40, /* Enhancement B */
63         0x0A, 0x50, /* Enhancement A */
64         0x80, 0x80, /* Recovery count */
65         0x90, 0x90, /* Start A */
66         0xa0, 0xa0, /* Start B */
67         0xb0, 0xb0, /* Detect AB */
68     };
69 
70     memory_region_init(escc_legacy, OBJECT(s), "escc-legacy", 256);
71     for (i = 0; i < ARRAY_SIZE(maps); i += 2) {
72         MemoryRegion *port = g_new(MemoryRegion, 1);
73         memory_region_init_alias(port, OBJECT(s), "escc-legacy-port",
74                                  sysbus_mmio_get_region(sbd, 0),
75                                  maps[i + 1], 0x2);
76         memory_region_add_subregion(escc_legacy, maps[i], port);
77     }
78 
79     memory_region_add_subregion(bar, 0x12000, escc_legacy);
80 }
81 
82 static void macio_bar_setup(MacIOState *s)
83 {
84     ESCCState *escc = ESCC(&s->escc);
85     SysBusDevice *sbd = SYS_BUS_DEVICE(escc);
86     MemoryRegion *bar = &s->bar;
87 
88     memory_region_add_subregion(bar, 0x13000, sysbus_mmio_get_region(sbd, 0));
89     macio_escc_legacy_setup(s);
90 }
91 
92 static void macio_common_realize(PCIDevice *d, Error **errp)
93 {
94     MacIOState *s = MACIO(d);
95     SysBusDevice *sysbus_dev;
96     Error *err = NULL;
97 
98     object_property_set_bool(OBJECT(&s->dbdma), true, "realized", &err);
99     if (err) {
100         error_propagate(errp, err);
101         return;
102     }
103     sysbus_dev = SYS_BUS_DEVICE(&s->dbdma);
104     memory_region_add_subregion(&s->bar, 0x08000,
105                                 sysbus_mmio_get_region(sysbus_dev, 0));
106 
107     qdev_prop_set_uint64(DEVICE(&s->cuda), "timebase-frequency",
108                          s->frequency);
109     object_property_set_bool(OBJECT(&s->cuda), true, "realized", &err);
110     if (err) {
111         error_propagate(errp, err);
112         return;
113     }
114     sysbus_dev = SYS_BUS_DEVICE(&s->cuda);
115     memory_region_add_subregion(&s->bar, 0x16000,
116                                 sysbus_mmio_get_region(sysbus_dev, 0));
117 
118     qdev_prop_set_uint32(DEVICE(&s->escc), "disabled", 0);
119     qdev_prop_set_uint32(DEVICE(&s->escc), "frequency", ESCC_CLOCK);
120     qdev_prop_set_uint32(DEVICE(&s->escc), "it_shift", 4);
121     qdev_prop_set_chr(DEVICE(&s->escc), "chrA", serial_hd(0));
122     qdev_prop_set_chr(DEVICE(&s->escc), "chrB", serial_hd(1));
123     qdev_prop_set_uint32(DEVICE(&s->escc), "chnBtype", escc_serial);
124     qdev_prop_set_uint32(DEVICE(&s->escc), "chnAtype", escc_serial);
125     object_property_set_bool(OBJECT(&s->escc), true, "realized", &err);
126     if (err) {
127         error_propagate(errp, err);
128         return;
129     }
130 
131     macio_bar_setup(s);
132     pci_register_bar(d, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->bar);
133 }
134 
135 static void macio_realize_ide(MacIOState *s, MACIOIDEState *ide,
136                               qemu_irq irq0, qemu_irq irq1, int dmaid,
137                               Error **errp)
138 {
139     SysBusDevice *sysbus_dev;
140 
141     sysbus_dev = SYS_BUS_DEVICE(ide);
142     sysbus_connect_irq(sysbus_dev, 0, irq0);
143     sysbus_connect_irq(sysbus_dev, 1, irq1);
144     qdev_prop_set_uint32(DEVICE(ide), "channel", dmaid);
145     object_property_set_link(OBJECT(ide), OBJECT(&s->dbdma), "dbdma", errp);
146     macio_ide_register_dma(ide);
147 
148     object_property_set_bool(OBJECT(ide), true, "realized", errp);
149 }
150 
151 static void macio_oldworld_realize(PCIDevice *d, Error **errp)
152 {
153     MacIOState *s = MACIO(d);
154     OldWorldMacIOState *os = OLDWORLD_MACIO(d);
155     DeviceState *pic_dev = DEVICE(os->pic);
156     Error *err = NULL;
157     SysBusDevice *sysbus_dev;
158 
159     macio_common_realize(d, &err);
160     if (err) {
161         error_propagate(errp, err);
162         return;
163     }
164 
165     sysbus_dev = SYS_BUS_DEVICE(&s->cuda);
166     sysbus_connect_irq(sysbus_dev, 0, qdev_get_gpio_in(pic_dev,
167                                                        OLDWORLD_CUDA_IRQ));
168 
169     sysbus_dev = SYS_BUS_DEVICE(&s->escc);
170     sysbus_connect_irq(sysbus_dev, 0, qdev_get_gpio_in(pic_dev,
171                                                        OLDWORLD_ESCCB_IRQ));
172     sysbus_connect_irq(sysbus_dev, 1, qdev_get_gpio_in(pic_dev,
173                                                        OLDWORLD_ESCCA_IRQ));
174 
175     object_property_set_bool(OBJECT(&os->nvram), true, "realized", &err);
176     if (err) {
177         error_propagate(errp, err);
178         return;
179     }
180     sysbus_dev = SYS_BUS_DEVICE(&os->nvram);
181     memory_region_add_subregion(&s->bar, 0x60000,
182                                 sysbus_mmio_get_region(sysbus_dev, 0));
183     pmac_format_nvram_partition(&os->nvram, os->nvram.size);
184 
185     /* Heathrow PIC */
186     sysbus_dev = SYS_BUS_DEVICE(os->pic);
187     memory_region_add_subregion(&s->bar, 0x0,
188                                 sysbus_mmio_get_region(sysbus_dev, 0));
189 
190     /* IDE buses */
191     macio_realize_ide(s, &os->ide[0],
192                       qdev_get_gpio_in(pic_dev, OLDWORLD_IDE0_IRQ),
193                       qdev_get_gpio_in(pic_dev, OLDWORLD_IDE0_DMA_IRQ),
194                       0x16, &err);
195     if (err) {
196         error_propagate(errp, err);
197         return;
198     }
199 
200     macio_realize_ide(s, &os->ide[1],
201                       qdev_get_gpio_in(pic_dev, OLDWORLD_IDE1_IRQ),
202                       qdev_get_gpio_in(pic_dev, OLDWORLD_IDE1_DMA_IRQ),
203                       0x1a, &err);
204     if (err) {
205         error_propagate(errp, err);
206         return;
207     }
208 }
209 
210 static void macio_init_ide(MacIOState *s, MACIOIDEState *ide, size_t ide_size,
211                            int index)
212 {
213     gchar *name;
214 
215     object_initialize(ide, ide_size, TYPE_MACIO_IDE);
216     qdev_set_parent_bus(DEVICE(ide), sysbus_get_default());
217     memory_region_add_subregion(&s->bar, 0x1f000 + ((index + 1) * 0x1000),
218                                 &ide->mem);
219     name = g_strdup_printf("ide[%i]", index);
220     object_property_add_child(OBJECT(s), name, OBJECT(ide), NULL);
221     g_free(name);
222 }
223 
224 static void macio_oldworld_init(Object *obj)
225 {
226     MacIOState *s = MACIO(obj);
227     OldWorldMacIOState *os = OLDWORLD_MACIO(obj);
228     DeviceState *dev;
229     int i;
230 
231     object_property_add_link(obj, "pic", TYPE_HEATHROW,
232                              (Object **) &os->pic,
233                              qdev_prop_allow_set_link_before_realize,
234                              0, NULL);
235 
236     object_initialize(&os->nvram, sizeof(os->nvram), TYPE_MACIO_NVRAM);
237     dev = DEVICE(&os->nvram);
238     qdev_prop_set_uint32(dev, "size", 0x2000);
239     qdev_prop_set_uint32(dev, "it_shift", 4);
240 
241     for (i = 0; i < 2; i++) {
242         macio_init_ide(s, &os->ide[i], sizeof(os->ide[i]), i);
243     }
244 }
245 
246 static void timer_write(void *opaque, hwaddr addr, uint64_t value,
247                        unsigned size)
248 {
249 }
250 
251 static uint64_t timer_read(void *opaque, hwaddr addr, unsigned size)
252 {
253     uint32_t value = 0;
254     uint64_t systime = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
255     uint64_t kltime;
256 
257     kltime = muldiv64(systime, 4194300, NANOSECONDS_PER_SECOND * 4);
258     kltime = muldiv64(kltime, 18432000, 1048575);
259 
260     switch (addr) {
261     case 0x38:
262         value = kltime;
263         break;
264     case 0x3c:
265         value = kltime >> 32;
266         break;
267     }
268 
269     return value;
270 }
271 
272 static const MemoryRegionOps timer_ops = {
273     .read = timer_read,
274     .write = timer_write,
275     .endianness = DEVICE_LITTLE_ENDIAN,
276 };
277 
278 static void macio_newworld_realize(PCIDevice *d, Error **errp)
279 {
280     MacIOState *s = MACIO(d);
281     NewWorldMacIOState *ns = NEWWORLD_MACIO(d);
282     Error *err = NULL;
283     SysBusDevice *sysbus_dev;
284     MemoryRegion *timer_memory = NULL;
285     int i;
286     int cur_irq = 0;
287 
288     macio_common_realize(d, &err);
289     if (err) {
290         error_propagate(errp, err);
291         return;
292     }
293 
294     sysbus_dev = SYS_BUS_DEVICE(&s->cuda);
295     sysbus_connect_irq(sysbus_dev, 0, ns->irqs[cur_irq++]);
296 
297     sysbus_dev = SYS_BUS_DEVICE(&s->escc);
298     sysbus_connect_irq(sysbus_dev, 0, ns->irqs[cur_irq++]);
299     sysbus_connect_irq(sysbus_dev, 1, ns->irqs[cur_irq++]);
300 
301     /* OpenPIC */
302     sysbus_dev = SYS_BUS_DEVICE(ns->pic);
303     memory_region_add_subregion(&s->bar, 0x40000,
304                                 sysbus_mmio_get_region(sysbus_dev, 0));
305 
306     /* IDE buses */
307     for (i = 0; i < ARRAY_SIZE(ns->ide); i++) {
308         qemu_irq irq0 = ns->irqs[cur_irq++];
309         qemu_irq irq1 = ns->irqs[cur_irq++];
310 
311         macio_realize_ide(s, &ns->ide[i], irq0, irq1, 0x16 + (i * 4), &err);
312         if (err) {
313             error_propagate(errp, err);
314             return;
315         }
316     }
317 
318     /* Timer */
319     timer_memory = g_new(MemoryRegion, 1);
320     memory_region_init_io(timer_memory, OBJECT(s), &timer_ops, NULL, "timer",
321                           0x1000);
322     memory_region_add_subregion(&s->bar, 0x15000, timer_memory);
323 }
324 
325 static void macio_newworld_init(Object *obj)
326 {
327     MacIOState *s = MACIO(obj);
328     NewWorldMacIOState *ns = NEWWORLD_MACIO(obj);
329     int i;
330 
331     qdev_init_gpio_out(DEVICE(obj), ns->irqs, ARRAY_SIZE(ns->irqs));
332 
333     object_property_add_link(obj, "pic", TYPE_OPENPIC,
334                              (Object **) &ns->pic,
335                              qdev_prop_allow_set_link_before_realize,
336                              0, NULL);
337 
338     for (i = 0; i < 2; i++) {
339         macio_init_ide(s, &ns->ide[i], sizeof(ns->ide[i]), i);
340     }
341 }
342 
343 static void macio_instance_init(Object *obj)
344 {
345     MacIOState *s = MACIO(obj);
346 
347     memory_region_init(&s->bar, obj, "macio", 0x80000);
348 
349     object_initialize(&s->cuda, sizeof(s->cuda), TYPE_CUDA);
350     qdev_set_parent_bus(DEVICE(&s->cuda), sysbus_get_default());
351     object_property_add_child(obj, "cuda", OBJECT(&s->cuda), NULL);
352 
353     object_initialize(&s->dbdma, sizeof(s->dbdma), TYPE_MAC_DBDMA);
354     qdev_set_parent_bus(DEVICE(&s->dbdma), sysbus_get_default());
355     object_property_add_child(obj, "dbdma", OBJECT(&s->dbdma), NULL);
356 
357     object_initialize(&s->escc, sizeof(s->escc), TYPE_ESCC);
358     qdev_set_parent_bus(DEVICE(&s->escc), sysbus_get_default());
359     object_property_add_child(obj, "escc", OBJECT(&s->escc), NULL);
360 }
361 
362 static const VMStateDescription vmstate_macio_oldworld = {
363     .name = "macio-oldworld",
364     .version_id = 0,
365     .minimum_version_id = 0,
366     .fields = (VMStateField[]) {
367         VMSTATE_PCI_DEVICE(parent_obj.parent, OldWorldMacIOState),
368         VMSTATE_END_OF_LIST()
369     }
370 };
371 
372 static void macio_oldworld_class_init(ObjectClass *oc, void *data)
373 {
374     PCIDeviceClass *pdc = PCI_DEVICE_CLASS(oc);
375     DeviceClass *dc = DEVICE_CLASS(oc);
376 
377     pdc->realize = macio_oldworld_realize;
378     pdc->device_id = PCI_DEVICE_ID_APPLE_343S1201;
379     dc->vmsd = &vmstate_macio_oldworld;
380 }
381 
382 static const VMStateDescription vmstate_macio_newworld = {
383     .name = "macio-newworld",
384     .version_id = 0,
385     .minimum_version_id = 0,
386     .fields = (VMStateField[]) {
387         VMSTATE_PCI_DEVICE(parent_obj.parent, NewWorldMacIOState),
388         VMSTATE_END_OF_LIST()
389     }
390 };
391 
392 static void macio_newworld_class_init(ObjectClass *oc, void *data)
393 {
394     PCIDeviceClass *pdc = PCI_DEVICE_CLASS(oc);
395     DeviceClass *dc = DEVICE_CLASS(oc);
396 
397     pdc->realize = macio_newworld_realize;
398     pdc->device_id = PCI_DEVICE_ID_APPLE_UNI_N_KEYL;
399     dc->vmsd = &vmstate_macio_newworld;
400 }
401 
402 static Property macio_properties[] = {
403     DEFINE_PROP_UINT64("frequency", MacIOState, frequency, 0),
404     DEFINE_PROP_END_OF_LIST()
405 };
406 
407 static void macio_class_init(ObjectClass *klass, void *data)
408 {
409     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
410     DeviceClass *dc = DEVICE_CLASS(klass);
411 
412     k->vendor_id = PCI_VENDOR_ID_APPLE;
413     k->class_id = PCI_CLASS_OTHERS << 8;
414     dc->props = macio_properties;
415     set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
416     /* Reason: Uses serial_hds in macio_instance_init */
417     dc->user_creatable = false;
418 }
419 
420 static const TypeInfo macio_oldworld_type_info = {
421     .name          = TYPE_OLDWORLD_MACIO,
422     .parent        = TYPE_MACIO,
423     .instance_size = sizeof(OldWorldMacIOState),
424     .instance_init = macio_oldworld_init,
425     .class_init    = macio_oldworld_class_init,
426 };
427 
428 static const TypeInfo macio_newworld_type_info = {
429     .name          = TYPE_NEWWORLD_MACIO,
430     .parent        = TYPE_MACIO,
431     .instance_size = sizeof(NewWorldMacIOState),
432     .instance_init = macio_newworld_init,
433     .class_init    = macio_newworld_class_init,
434 };
435 
436 static const TypeInfo macio_type_info = {
437     .name          = TYPE_MACIO,
438     .parent        = TYPE_PCI_DEVICE,
439     .instance_size = sizeof(MacIOState),
440     .instance_init = macio_instance_init,
441     .abstract      = true,
442     .class_init    = macio_class_init,
443     .interfaces = (InterfaceInfo[]) {
444         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
445         { },
446     },
447 };
448 
449 static void macio_register_types(void)
450 {
451     type_register_static(&macio_type_info);
452     type_register_static(&macio_oldworld_type_info);
453     type_register_static(&macio_newworld_type_info);
454 }
455 
456 type_init(macio_register_types)
457