2 * PowerMac MacIO device emulation
4 * Copyright (c) 2005-2007 Fabrice Bellard
5 * Copyright (c) 2007 Jocelyn Mayer
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
25 #include "qemu/osdep.h"
26 #include "qapi/error.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"
37 /* Note: this code is strongly inspirated from the corresponding code
41 * The mac-io has two interfaces to the ESCC. One is called "escc-legacy",
42 * while the other one is the normal, current ESCC interface.
44 * The magic below creates memory aliases to spawn the escc-legacy device
45 * purely by rerouting the respective registers to our escc region. This
46 * works because the only difference between the two memory regions is the
47 * register layout, not their semantics.
49 * Reference: ftp://ftp.software.ibm.com/rs6000/technology/spec/chrp/inwork/CHRP_IORef_1.0.pdf
51 static void macio_escc_legacy_setup(MacIOState
*s
)
53 ESCCState
*escc
= ESCC(&s
->escc
);
54 SysBusDevice
*sbd
= SYS_BUS_DEVICE(escc
);
55 MemoryRegion
*escc_legacy
= g_new(MemoryRegion
, 1);
56 MemoryRegion
*bar
= &s
->bar
;
58 static const int maps
[] = {
59 0x00, 0x00, /* Command B */
60 0x02, 0x20, /* Command A */
61 0x04, 0x10, /* Data B */
62 0x06, 0x30, /* Data A */
63 0x08, 0x40, /* Enhancement B */
64 0x0A, 0x50, /* Enhancement A */
65 0x80, 0x80, /* Recovery count */
66 0x90, 0x90, /* Start A */
67 0xa0, 0xa0, /* Start B */
68 0xb0, 0xb0, /* Detect AB */
71 memory_region_init(escc_legacy
, OBJECT(s
), "escc-legacy", 256);
72 for (i
= 0; i
< ARRAY_SIZE(maps
); i
+= 2) {
73 MemoryRegion
*port
= g_new(MemoryRegion
, 1);
74 memory_region_init_alias(port
, OBJECT(s
), "escc-legacy-port",
75 sysbus_mmio_get_region(sbd
, 0),
77 memory_region_add_subregion(escc_legacy
, maps
[i
], port
);
80 memory_region_add_subregion(bar
, 0x12000, escc_legacy
);
83 static void macio_bar_setup(MacIOState
*s
)
85 ESCCState
*escc
= ESCC(&s
->escc
);
86 SysBusDevice
*sbd
= SYS_BUS_DEVICE(escc
);
87 MemoryRegion
*bar
= &s
->bar
;
89 memory_region_add_subregion(bar
, 0x13000, sysbus_mmio_get_region(sbd
, 0));
90 macio_escc_legacy_setup(s
);
93 static void macio_common_realize(PCIDevice
*d
, Error
**errp
)
95 MacIOState
*s
= MACIO(d
);
96 SysBusDevice
*sysbus_dev
;
99 object_property_set_bool(OBJECT(&s
->dbdma
), true, "realized", &err
);
101 error_propagate(errp
, err
);
104 sysbus_dev
= SYS_BUS_DEVICE(&s
->dbdma
);
105 memory_region_add_subregion(&s
->bar
, 0x08000,
106 sysbus_mmio_get_region(sysbus_dev
, 0));
108 qdev_prop_set_uint64(DEVICE(&s
->cuda
), "timebase-frequency",
110 object_property_set_bool(OBJECT(&s
->cuda
), true, "realized", &err
);
112 error_propagate(errp
, err
);
115 sysbus_dev
= SYS_BUS_DEVICE(&s
->cuda
);
116 memory_region_add_subregion(&s
->bar
, 0x16000,
117 sysbus_mmio_get_region(sysbus_dev
, 0));
119 qdev_prop_set_uint32(DEVICE(&s
->escc
), "disabled", 0);
120 qdev_prop_set_uint32(DEVICE(&s
->escc
), "frequency", ESCC_CLOCK
);
121 qdev_prop_set_uint32(DEVICE(&s
->escc
), "it_shift", 4);
122 qdev_prop_set_chr(DEVICE(&s
->escc
), "chrA", serial_hd(0));
123 qdev_prop_set_chr(DEVICE(&s
->escc
), "chrB", serial_hd(1));
124 qdev_prop_set_uint32(DEVICE(&s
->escc
), "chnBtype", escc_serial
);
125 qdev_prop_set_uint32(DEVICE(&s
->escc
), "chnAtype", escc_serial
);
126 object_property_set_bool(OBJECT(&s
->escc
), true, "realized", &err
);
128 error_propagate(errp
, err
);
133 pci_register_bar(d
, 0, PCI_BASE_ADDRESS_SPACE_MEMORY
, &s
->bar
);
136 static void macio_realize_ide(MacIOState
*s
, MACIOIDEState
*ide
,
137 qemu_irq irq0
, qemu_irq irq1
, int dmaid
,
140 SysBusDevice
*sysbus_dev
;
142 sysbus_dev
= SYS_BUS_DEVICE(ide
);
143 sysbus_connect_irq(sysbus_dev
, 0, irq0
);
144 sysbus_connect_irq(sysbus_dev
, 1, irq1
);
145 qdev_prop_set_uint32(DEVICE(ide
), "channel", dmaid
);
146 object_property_set_link(OBJECT(ide
), OBJECT(&s
->dbdma
), "dbdma", errp
);
147 macio_ide_register_dma(ide
);
149 object_property_set_bool(OBJECT(ide
), true, "realized", errp
);
152 static void macio_oldworld_realize(PCIDevice
*d
, Error
**errp
)
154 MacIOState
*s
= MACIO(d
);
155 OldWorldMacIOState
*os
= OLDWORLD_MACIO(d
);
156 DeviceState
*pic_dev
= DEVICE(os
->pic
);
158 SysBusDevice
*sysbus_dev
;
160 macio_common_realize(d
, &err
);
162 error_propagate(errp
, err
);
166 sysbus_dev
= SYS_BUS_DEVICE(&s
->cuda
);
167 sysbus_connect_irq(sysbus_dev
, 0, qdev_get_gpio_in(pic_dev
,
170 sysbus_dev
= SYS_BUS_DEVICE(&s
->escc
);
171 sysbus_connect_irq(sysbus_dev
, 0, qdev_get_gpio_in(pic_dev
,
172 OLDWORLD_ESCCB_IRQ
));
173 sysbus_connect_irq(sysbus_dev
, 1, qdev_get_gpio_in(pic_dev
,
174 OLDWORLD_ESCCA_IRQ
));
176 object_property_set_bool(OBJECT(&os
->nvram
), true, "realized", &err
);
178 error_propagate(errp
, err
);
181 sysbus_dev
= SYS_BUS_DEVICE(&os
->nvram
);
182 memory_region_add_subregion(&s
->bar
, 0x60000,
183 sysbus_mmio_get_region(sysbus_dev
, 0));
184 pmac_format_nvram_partition(&os
->nvram
, os
->nvram
.size
);
187 sysbus_dev
= SYS_BUS_DEVICE(os
->pic
);
188 memory_region_add_subregion(&s
->bar
, 0x0,
189 sysbus_mmio_get_region(sysbus_dev
, 0));
192 macio_realize_ide(s
, &os
->ide
[0],
193 qdev_get_gpio_in(pic_dev
, OLDWORLD_IDE0_IRQ
),
194 qdev_get_gpio_in(pic_dev
, OLDWORLD_IDE0_DMA_IRQ
),
197 error_propagate(errp
, err
);
201 macio_realize_ide(s
, &os
->ide
[1],
202 qdev_get_gpio_in(pic_dev
, OLDWORLD_IDE1_IRQ
),
203 qdev_get_gpio_in(pic_dev
, OLDWORLD_IDE1_DMA_IRQ
),
206 error_propagate(errp
, err
);
211 static void macio_init_ide(MacIOState
*s
, MACIOIDEState
*ide
, size_t ide_size
,
216 object_initialize(ide
, ide_size
, TYPE_MACIO_IDE
);
217 qdev_set_parent_bus(DEVICE(ide
), sysbus_get_default());
218 memory_region_add_subregion(&s
->bar
, 0x1f000 + ((index
+ 1) * 0x1000),
220 name
= g_strdup_printf("ide[%i]", index
);
221 object_property_add_child(OBJECT(s
), name
, OBJECT(ide
), NULL
);
225 static void macio_oldworld_init(Object
*obj
)
227 MacIOState
*s
= MACIO(obj
);
228 OldWorldMacIOState
*os
= OLDWORLD_MACIO(obj
);
232 object_property_add_link(obj
, "pic", TYPE_HEATHROW
,
233 (Object
**) &os
->pic
,
234 qdev_prop_allow_set_link_before_realize
,
237 object_initialize(&os
->nvram
, sizeof(os
->nvram
), TYPE_MACIO_NVRAM
);
238 dev
= DEVICE(&os
->nvram
);
239 qdev_prop_set_uint32(dev
, "size", 0x2000);
240 qdev_prop_set_uint32(dev
, "it_shift", 4);
242 for (i
= 0; i
< 2; i
++) {
243 macio_init_ide(s
, &os
->ide
[i
], sizeof(os
->ide
[i
]), i
);
247 static void timer_write(void *opaque
, hwaddr addr
, uint64_t value
,
250 trace_macio_timer_write(addr
, size
, value
);
253 static uint64_t timer_read(void *opaque
, hwaddr addr
, unsigned size
)
256 uint64_t systime
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
259 kltime
= muldiv64(systime
, 4194300, NANOSECONDS_PER_SECOND
* 4);
260 kltime
= muldiv64(kltime
, 18432000, 1048575);
267 value
= kltime
>> 32;
271 trace_macio_timer_read(addr
, size
, value
);
275 static const MemoryRegionOps timer_ops
= {
277 .write
= timer_write
,
278 .endianness
= DEVICE_LITTLE_ENDIAN
,
281 static void macio_newworld_realize(PCIDevice
*d
, Error
**errp
)
283 MacIOState
*s
= MACIO(d
);
284 NewWorldMacIOState
*ns
= NEWWORLD_MACIO(d
);
285 DeviceState
*pic_dev
= DEVICE(ns
->pic
);
287 SysBusDevice
*sysbus_dev
;
288 MemoryRegion
*timer_memory
= NULL
;
290 macio_common_realize(d
, &err
);
292 error_propagate(errp
, err
);
296 sysbus_dev
= SYS_BUS_DEVICE(&s
->cuda
);
297 sysbus_connect_irq(sysbus_dev
, 0, qdev_get_gpio_in(pic_dev
,
300 sysbus_dev
= SYS_BUS_DEVICE(&s
->escc
);
301 sysbus_connect_irq(sysbus_dev
, 0, qdev_get_gpio_in(pic_dev
,
302 NEWWORLD_ESCCB_IRQ
));
303 sysbus_connect_irq(sysbus_dev
, 1, qdev_get_gpio_in(pic_dev
,
304 NEWWORLD_ESCCA_IRQ
));
307 sysbus_dev
= SYS_BUS_DEVICE(ns
->pic
);
308 memory_region_add_subregion(&s
->bar
, 0x40000,
309 sysbus_mmio_get_region(sysbus_dev
, 0));
312 macio_realize_ide(s
, &ns
->ide
[0],
313 qdev_get_gpio_in(pic_dev
, NEWWORLD_IDE0_IRQ
),
314 qdev_get_gpio_in(pic_dev
, NEWWORLD_IDE0_DMA_IRQ
),
317 error_propagate(errp
, err
);
321 macio_realize_ide(s
, &ns
->ide
[1],
322 qdev_get_gpio_in(pic_dev
, NEWWORLD_IDE1_IRQ
),
323 qdev_get_gpio_in(pic_dev
, NEWWORLD_IDE1_DMA_IRQ
),
326 error_propagate(errp
, err
);
331 timer_memory
= g_new(MemoryRegion
, 1);
332 memory_region_init_io(timer_memory
, OBJECT(s
), &timer_ops
, NULL
, "timer",
334 memory_region_add_subregion(&s
->bar
, 0x15000, timer_memory
);
338 sysbus_dev
= SYS_BUS_DEVICE(&ns
->gpio
);
339 object_property_set_link(OBJECT(&ns
->gpio
), OBJECT(pic_dev
), "pic",
341 memory_region_add_subregion(&s
->bar
, 0x50,
342 sysbus_mmio_get_region(sysbus_dev
, 0));
343 object_property_set_bool(OBJECT(&ns
->gpio
), true, "realized", &err
);
347 static void macio_newworld_init(Object
*obj
)
349 MacIOState
*s
= MACIO(obj
);
350 NewWorldMacIOState
*ns
= NEWWORLD_MACIO(obj
);
353 object_property_add_link(obj
, "pic", TYPE_OPENPIC
,
354 (Object
**) &ns
->pic
,
355 qdev_prop_allow_set_link_before_realize
,
358 object_initialize(&ns
->gpio
, sizeof(ns
->gpio
), TYPE_MACIO_GPIO
);
359 qdev_set_parent_bus(DEVICE(&ns
->gpio
), sysbus_get_default());
361 for (i
= 0; i
< 2; i
++) {
362 macio_init_ide(s
, &ns
->ide
[i
], sizeof(ns
->ide
[i
]), i
);
366 static void macio_instance_init(Object
*obj
)
368 MacIOState
*s
= MACIO(obj
);
370 memory_region_init(&s
->bar
, obj
, "macio", 0x80000);
372 object_initialize(&s
->cuda
, sizeof(s
->cuda
), TYPE_CUDA
);
373 qdev_set_parent_bus(DEVICE(&s
->cuda
), sysbus_get_default());
374 object_property_add_child(obj
, "cuda", OBJECT(&s
->cuda
), NULL
);
376 object_initialize(&s
->dbdma
, sizeof(s
->dbdma
), TYPE_MAC_DBDMA
);
377 qdev_set_parent_bus(DEVICE(&s
->dbdma
), sysbus_get_default());
378 object_property_add_child(obj
, "dbdma", OBJECT(&s
->dbdma
), NULL
);
380 object_initialize(&s
->escc
, sizeof(s
->escc
), TYPE_ESCC
);
381 qdev_set_parent_bus(DEVICE(&s
->escc
), sysbus_get_default());
382 object_property_add_child(obj
, "escc", OBJECT(&s
->escc
), NULL
);
385 static const VMStateDescription vmstate_macio_oldworld
= {
386 .name
= "macio-oldworld",
388 .minimum_version_id
= 0,
389 .fields
= (VMStateField
[]) {
390 VMSTATE_PCI_DEVICE(parent_obj
.parent
, OldWorldMacIOState
),
391 VMSTATE_END_OF_LIST()
395 static void macio_oldworld_class_init(ObjectClass
*oc
, void *data
)
397 PCIDeviceClass
*pdc
= PCI_DEVICE_CLASS(oc
);
398 DeviceClass
*dc
= DEVICE_CLASS(oc
);
400 pdc
->realize
= macio_oldworld_realize
;
401 pdc
->device_id
= PCI_DEVICE_ID_APPLE_343S1201
;
402 dc
->vmsd
= &vmstate_macio_oldworld
;
405 static const VMStateDescription vmstate_macio_newworld
= {
406 .name
= "macio-newworld",
408 .minimum_version_id
= 0,
409 .fields
= (VMStateField
[]) {
410 VMSTATE_PCI_DEVICE(parent_obj
.parent
, NewWorldMacIOState
),
411 VMSTATE_END_OF_LIST()
415 static Property macio_newworld_properties
[] = {
416 DEFINE_PROP_BOOL("has-pmu", NewWorldMacIOState
, has_pmu
, false),
417 DEFINE_PROP_BOOL("has-adb", NewWorldMacIOState
, has_adb
, false),
418 DEFINE_PROP_END_OF_LIST()
421 static void macio_newworld_class_init(ObjectClass
*oc
, void *data
)
423 PCIDeviceClass
*pdc
= PCI_DEVICE_CLASS(oc
);
424 DeviceClass
*dc
= DEVICE_CLASS(oc
);
426 pdc
->realize
= macio_newworld_realize
;
427 pdc
->device_id
= PCI_DEVICE_ID_APPLE_UNI_N_KEYL
;
428 dc
->vmsd
= &vmstate_macio_newworld
;
429 dc
->props
= macio_newworld_properties
;
432 static Property macio_properties
[] = {
433 DEFINE_PROP_UINT64("frequency", MacIOState
, frequency
, 0),
434 DEFINE_PROP_END_OF_LIST()
437 static void macio_class_init(ObjectClass
*klass
, void *data
)
439 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
440 DeviceClass
*dc
= DEVICE_CLASS(klass
);
442 k
->vendor_id
= PCI_VENDOR_ID_APPLE
;
443 k
->class_id
= PCI_CLASS_OTHERS
<< 8;
444 dc
->props
= macio_properties
;
445 set_bit(DEVICE_CATEGORY_BRIDGE
, dc
->categories
);
446 /* Reason: Uses serial_hds in macio_instance_init */
447 dc
->user_creatable
= false;
450 static const TypeInfo macio_oldworld_type_info
= {
451 .name
= TYPE_OLDWORLD_MACIO
,
452 .parent
= TYPE_MACIO
,
453 .instance_size
= sizeof(OldWorldMacIOState
),
454 .instance_init
= macio_oldworld_init
,
455 .class_init
= macio_oldworld_class_init
,
458 static const TypeInfo macio_newworld_type_info
= {
459 .name
= TYPE_NEWWORLD_MACIO
,
460 .parent
= TYPE_MACIO
,
461 .instance_size
= sizeof(NewWorldMacIOState
),
462 .instance_init
= macio_newworld_init
,
463 .class_init
= macio_newworld_class_init
,
466 static const TypeInfo macio_type_info
= {
468 .parent
= TYPE_PCI_DEVICE
,
469 .instance_size
= sizeof(MacIOState
),
470 .instance_init
= macio_instance_init
,
472 .class_init
= macio_class_init
,
473 .interfaces
= (InterfaceInfo
[]) {
474 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
479 static void macio_register_types(void)
481 type_register_static(&macio_type_info
);
482 type_register_static(&macio_oldworld_type_info
);
483 type_register_static(&macio_newworld_type_info
);
486 type_init(macio_register_types
)