xref: /openbmc/qemu/hw/misc/macio/macio.c (revision 89854803)
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     DeviceState *pic_dev = DEVICE(ns->pic);
283     Error *err = NULL;
284     SysBusDevice *sysbus_dev;
285     MemoryRegion *timer_memory = NULL;
286 
287     macio_common_realize(d, &err);
288     if (err) {
289         error_propagate(errp, err);
290         return;
291     }
292 
293     sysbus_dev = SYS_BUS_DEVICE(&s->cuda);
294     sysbus_connect_irq(sysbus_dev, 0, qdev_get_gpio_in(pic_dev,
295                                                        NEWWORLD_CUDA_IRQ));
296 
297     sysbus_dev = SYS_BUS_DEVICE(&s->escc);
298     sysbus_connect_irq(sysbus_dev, 0, qdev_get_gpio_in(pic_dev,
299                                                        NEWWORLD_ESCCB_IRQ));
300     sysbus_connect_irq(sysbus_dev, 1, qdev_get_gpio_in(pic_dev,
301                                                        NEWWORLD_ESCCA_IRQ));
302 
303     /* OpenPIC */
304     sysbus_dev = SYS_BUS_DEVICE(ns->pic);
305     memory_region_add_subregion(&s->bar, 0x40000,
306                                 sysbus_mmio_get_region(sysbus_dev, 0));
307 
308     /* IDE buses */
309     macio_realize_ide(s, &ns->ide[0],
310                       qdev_get_gpio_in(pic_dev, NEWWORLD_IDE0_IRQ),
311                       qdev_get_gpio_in(pic_dev, NEWWORLD_IDE0_DMA_IRQ),
312                       0x16, &err);
313     if (err) {
314         error_propagate(errp, err);
315         return;
316     }
317 
318     macio_realize_ide(s, &ns->ide[1],
319                       qdev_get_gpio_in(pic_dev, NEWWORLD_IDE1_IRQ),
320                       qdev_get_gpio_in(pic_dev, NEWWORLD_IDE1_DMA_IRQ),
321                       0x1a, &err);
322     if (err) {
323         error_propagate(errp, err);
324         return;
325     }
326 
327     /* Timer */
328     timer_memory = g_new(MemoryRegion, 1);
329     memory_region_init_io(timer_memory, OBJECT(s), &timer_ops, NULL, "timer",
330                           0x1000);
331     memory_region_add_subregion(&s->bar, 0x15000, timer_memory);
332 }
333 
334 static void macio_newworld_init(Object *obj)
335 {
336     MacIOState *s = MACIO(obj);
337     NewWorldMacIOState *ns = NEWWORLD_MACIO(obj);
338     int i;
339 
340     object_property_add_link(obj, "pic", TYPE_OPENPIC,
341                              (Object **) &ns->pic,
342                              qdev_prop_allow_set_link_before_realize,
343                              0, NULL);
344 
345     for (i = 0; i < 2; i++) {
346         macio_init_ide(s, &ns->ide[i], sizeof(ns->ide[i]), i);
347     }
348 }
349 
350 static void macio_instance_init(Object *obj)
351 {
352     MacIOState *s = MACIO(obj);
353 
354     memory_region_init(&s->bar, obj, "macio", 0x80000);
355 
356     object_initialize(&s->cuda, sizeof(s->cuda), TYPE_CUDA);
357     qdev_set_parent_bus(DEVICE(&s->cuda), sysbus_get_default());
358     object_property_add_child(obj, "cuda", OBJECT(&s->cuda), NULL);
359 
360     object_initialize(&s->dbdma, sizeof(s->dbdma), TYPE_MAC_DBDMA);
361     qdev_set_parent_bus(DEVICE(&s->dbdma), sysbus_get_default());
362     object_property_add_child(obj, "dbdma", OBJECT(&s->dbdma), NULL);
363 
364     object_initialize(&s->escc, sizeof(s->escc), TYPE_ESCC);
365     qdev_set_parent_bus(DEVICE(&s->escc), sysbus_get_default());
366     object_property_add_child(obj, "escc", OBJECT(&s->escc), NULL);
367 }
368 
369 static const VMStateDescription vmstate_macio_oldworld = {
370     .name = "macio-oldworld",
371     .version_id = 0,
372     .minimum_version_id = 0,
373     .fields = (VMStateField[]) {
374         VMSTATE_PCI_DEVICE(parent_obj.parent, OldWorldMacIOState),
375         VMSTATE_END_OF_LIST()
376     }
377 };
378 
379 static void macio_oldworld_class_init(ObjectClass *oc, void *data)
380 {
381     PCIDeviceClass *pdc = PCI_DEVICE_CLASS(oc);
382     DeviceClass *dc = DEVICE_CLASS(oc);
383 
384     pdc->realize = macio_oldworld_realize;
385     pdc->device_id = PCI_DEVICE_ID_APPLE_343S1201;
386     dc->vmsd = &vmstate_macio_oldworld;
387 }
388 
389 static const VMStateDescription vmstate_macio_newworld = {
390     .name = "macio-newworld",
391     .version_id = 0,
392     .minimum_version_id = 0,
393     .fields = (VMStateField[]) {
394         VMSTATE_PCI_DEVICE(parent_obj.parent, NewWorldMacIOState),
395         VMSTATE_END_OF_LIST()
396     }
397 };
398 
399 static void macio_newworld_class_init(ObjectClass *oc, void *data)
400 {
401     PCIDeviceClass *pdc = PCI_DEVICE_CLASS(oc);
402     DeviceClass *dc = DEVICE_CLASS(oc);
403 
404     pdc->realize = macio_newworld_realize;
405     pdc->device_id = PCI_DEVICE_ID_APPLE_UNI_N_KEYL;
406     dc->vmsd = &vmstate_macio_newworld;
407 }
408 
409 static Property macio_properties[] = {
410     DEFINE_PROP_UINT64("frequency", MacIOState, frequency, 0),
411     DEFINE_PROP_END_OF_LIST()
412 };
413 
414 static void macio_class_init(ObjectClass *klass, void *data)
415 {
416     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
417     DeviceClass *dc = DEVICE_CLASS(klass);
418 
419     k->vendor_id = PCI_VENDOR_ID_APPLE;
420     k->class_id = PCI_CLASS_OTHERS << 8;
421     dc->props = macio_properties;
422     set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
423     /* Reason: Uses serial_hds in macio_instance_init */
424     dc->user_creatable = false;
425 }
426 
427 static const TypeInfo macio_oldworld_type_info = {
428     .name          = TYPE_OLDWORLD_MACIO,
429     .parent        = TYPE_MACIO,
430     .instance_size = sizeof(OldWorldMacIOState),
431     .instance_init = macio_oldworld_init,
432     .class_init    = macio_oldworld_class_init,
433 };
434 
435 static const TypeInfo macio_newworld_type_info = {
436     .name          = TYPE_NEWWORLD_MACIO,
437     .parent        = TYPE_MACIO,
438     .instance_size = sizeof(NewWorldMacIOState),
439     .instance_init = macio_newworld_init,
440     .class_init    = macio_newworld_class_init,
441 };
442 
443 static const TypeInfo macio_type_info = {
444     .name          = TYPE_MACIO,
445     .parent        = TYPE_PCI_DEVICE,
446     .instance_size = sizeof(MacIOState),
447     .instance_init = macio_instance_init,
448     .abstract      = true,
449     .class_init    = macio_class_init,
450     .interfaces = (InterfaceInfo[]) {
451         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
452         { },
453     },
454 };
455 
456 static void macio_register_types(void)
457 {
458     type_register_static(&macio_type_info);
459     type_register_static(&macio_oldworld_type_info);
460     type_register_static(&macio_newworld_type_info);
461 }
462 
463 type_init(macio_register_types)
464