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
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"
31 #define TYPE_MACIO "macio"
32 #define MACIO(obj) OBJECT_CHECK(MacIOState, (obj), TYPE_MACIO)
34 typedef struct MacIOState
43 MemoryRegion
*pic_mem
;
44 MemoryRegion
*escc_mem
;
48 #define OLDWORLD_MACIO(obj) \
49 OBJECT_CHECK(OldWorldMacIOState, (obj), TYPE_OLDWORLD_MACIO)
51 typedef struct OldWorldMacIOState
{
53 MacIOState parent_obj
;
58 MacIONVRAMState nvram
;
62 #define NEWWORLD_MACIO(obj) \
63 OBJECT_CHECK(NewWorldMacIOState, (obj), TYPE_NEWWORLD_MACIO)
65 typedef struct NewWorldMacIOState
{
67 MacIOState parent_obj
;
74 * The mac-io has two interfaces to the ESCC. One is called "escc-legacy",
75 * while the other one is the normal, current ESCC interface.
77 * The magic below creates memory aliases to spawn the escc-legacy device
78 * purely by rerouting the respective registers to our escc region. This
79 * works because the only difference between the two memory regions is the
80 * register layout, not their semantics.
82 * Reference: ftp://ftp.software.ibm.com/rs6000/technology/spec/chrp/inwork/CHRP_IORef_1.0.pdf
84 static void macio_escc_legacy_setup(MacIOState
*macio_state
)
86 MemoryRegion
*escc_legacy
= g_new(MemoryRegion
, 1);
87 MemoryRegion
*bar
= &macio_state
->bar
;
89 static const int maps
[] = {
108 memory_region_init(escc_legacy
, OBJECT(macio_state
), "escc-legacy", 256);
109 for (i
= 0; i
< ARRAY_SIZE(maps
); i
+= 2) {
110 MemoryRegion
*port
= g_new(MemoryRegion
, 1);
111 memory_region_init_alias(port
, OBJECT(macio_state
), "escc-legacy-port",
112 macio_state
->escc_mem
, maps
[i
+1], 0x2);
113 memory_region_add_subregion(escc_legacy
, maps
[i
], port
);
116 memory_region_add_subregion(bar
, 0x12000, escc_legacy
);
119 static void macio_bar_setup(MacIOState
*macio_state
)
121 MemoryRegion
*bar
= &macio_state
->bar
;
123 if (macio_state
->escc_mem
) {
124 memory_region_add_subregion(bar
, 0x13000, macio_state
->escc_mem
);
125 macio_escc_legacy_setup(macio_state
);
129 static void macio_common_realize(PCIDevice
*d
, Error
**errp
)
131 MacIOState
*s
= MACIO(d
);
132 SysBusDevice
*sysbus_dev
;
135 object_property_set_bool(OBJECT(&s
->cuda
), true, "realized", &err
);
137 error_propagate(errp
, err
);
140 sysbus_dev
= SYS_BUS_DEVICE(&s
->cuda
);
141 memory_region_add_subregion(&s
->bar
, 0x16000,
142 sysbus_mmio_get_region(sysbus_dev
, 0));
145 pci_register_bar(d
, 0, PCI_BASE_ADDRESS_SPACE_MEMORY
, &s
->bar
);
148 static void macio_realize_ide(MacIOState
*s
, MACIOIDEState
*ide
,
149 qemu_irq irq0
, qemu_irq irq1
, int dmaid
,
152 SysBusDevice
*sysbus_dev
;
154 sysbus_dev
= SYS_BUS_DEVICE(ide
);
155 sysbus_connect_irq(sysbus_dev
, 0, irq0
);
156 sysbus_connect_irq(sysbus_dev
, 1, irq1
);
157 macio_ide_register_dma(ide
, s
->dbdma
, dmaid
);
158 object_property_set_bool(OBJECT(ide
), true, "realized", errp
);
161 static void macio_oldworld_realize(PCIDevice
*d
, Error
**errp
)
163 MacIOState
*s
= MACIO(d
);
164 OldWorldMacIOState
*os
= OLDWORLD_MACIO(d
);
166 SysBusDevice
*sysbus_dev
;
170 macio_common_realize(d
, &err
);
172 error_propagate(errp
, err
);
176 sysbus_dev
= SYS_BUS_DEVICE(&s
->cuda
);
177 sysbus_connect_irq(sysbus_dev
, 0, os
->irqs
[cur_irq
++]);
179 object_property_set_bool(OBJECT(&os
->nvram
), true, "realized", &err
);
181 error_propagate(errp
, err
);
184 sysbus_dev
= SYS_BUS_DEVICE(&os
->nvram
);
185 memory_region_add_subregion(&s
->bar
, 0x60000,
186 sysbus_mmio_get_region(sysbus_dev
, 0));
187 pmac_format_nvram_partition(&os
->nvram
, os
->nvram
.size
);
191 memory_region_add_subregion(&s
->bar
, 0x00000, s
->pic_mem
);
195 for (i
= 0; i
< ARRAY_SIZE(os
->ide
); i
++) {
196 qemu_irq irq0
= os
->irqs
[cur_irq
++];
197 qemu_irq irq1
= os
->irqs
[cur_irq
++];
199 macio_realize_ide(s
, &os
->ide
[i
], irq0
, irq1
, 0x16 + (i
* 4), &err
);
201 error_propagate(errp
, err
);
207 static void macio_init_ide(MacIOState
*s
, MACIOIDEState
*ide
, size_t ide_size
,
212 object_initialize(ide
, ide_size
, TYPE_MACIO_IDE
);
213 qdev_set_parent_bus(DEVICE(ide
), sysbus_get_default());
214 memory_region_add_subregion(&s
->bar
, 0x1f000 + ((index
+ 1) * 0x1000),
216 name
= g_strdup_printf("ide[%i]", index
);
217 object_property_add_child(OBJECT(s
), name
, OBJECT(ide
), NULL
);
221 static void macio_oldworld_init(Object
*obj
)
223 MacIOState
*s
= MACIO(obj
);
224 OldWorldMacIOState
*os
= OLDWORLD_MACIO(obj
);
228 qdev_init_gpio_out(DEVICE(obj
), os
->irqs
, ARRAY_SIZE(os
->irqs
));
230 object_initialize(&os
->nvram
, sizeof(os
->nvram
), TYPE_MACIO_NVRAM
);
231 dev
= DEVICE(&os
->nvram
);
232 qdev_prop_set_uint32(dev
, "size", 0x2000);
233 qdev_prop_set_uint32(dev
, "it_shift", 4);
235 for (i
= 0; i
< 2; i
++) {
236 macio_init_ide(s
, &os
->ide
[i
], sizeof(os
->ide
[i
]), i
);
240 static void timer_write(void *opaque
, hwaddr addr
, uint64_t value
,
245 static uint64_t timer_read(void *opaque
, hwaddr addr
, unsigned size
)
248 uint64_t systime
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
251 kltime
= muldiv64(systime
, 4194300, get_ticks_per_sec() * 4);
252 kltime
= muldiv64(kltime
, 18432000, 1048575);
259 value
= kltime
>> 32;
266 static const MemoryRegionOps timer_ops
= {
268 .write
= timer_write
,
269 .endianness
= DEVICE_LITTLE_ENDIAN
,
272 static void macio_newworld_realize(PCIDevice
*d
, Error
**errp
)
274 MacIOState
*s
= MACIO(d
);
275 NewWorldMacIOState
*ns
= NEWWORLD_MACIO(d
);
277 SysBusDevice
*sysbus_dev
;
278 MemoryRegion
*timer_memory
= NULL
;
282 macio_common_realize(d
, &err
);
284 error_propagate(errp
, err
);
288 sysbus_dev
= SYS_BUS_DEVICE(&s
->cuda
);
289 sysbus_connect_irq(sysbus_dev
, 0, ns
->irqs
[cur_irq
++]);
293 memory_region_add_subregion(&s
->bar
, 0x40000, s
->pic_mem
);
297 for (i
= 0; i
< ARRAY_SIZE(ns
->ide
); i
++) {
298 qemu_irq irq0
= ns
->irqs
[cur_irq
++];
299 qemu_irq irq1
= ns
->irqs
[cur_irq
++];
301 macio_realize_ide(s
, &ns
->ide
[i
], irq0
, irq1
, 0x16 + (i
* 4), &err
);
303 error_propagate(errp
, err
);
309 timer_memory
= g_new(MemoryRegion
, 1);
310 memory_region_init_io(timer_memory
, OBJECT(s
), &timer_ops
, NULL
, "timer",
312 memory_region_add_subregion(&s
->bar
, 0x15000, timer_memory
);
315 static void macio_newworld_init(Object
*obj
)
317 MacIOState
*s
= MACIO(obj
);
318 NewWorldMacIOState
*ns
= NEWWORLD_MACIO(obj
);
321 qdev_init_gpio_out(DEVICE(obj
), ns
->irqs
, ARRAY_SIZE(ns
->irqs
));
323 for (i
= 0; i
< 2; i
++) {
324 macio_init_ide(s
, &ns
->ide
[i
], sizeof(ns
->ide
[i
]), i
);
328 static void macio_instance_init(Object
*obj
)
330 MacIOState
*s
= MACIO(obj
);
331 MemoryRegion
*dbdma_mem
;
333 memory_region_init(&s
->bar
, obj
, "macio", 0x80000);
335 object_initialize(&s
->cuda
, sizeof(s
->cuda
), TYPE_CUDA
);
336 qdev_set_parent_bus(DEVICE(&s
->cuda
), sysbus_get_default());
337 object_property_add_child(obj
, "cuda", OBJECT(&s
->cuda
), NULL
);
339 s
->dbdma
= DBDMA_init(&dbdma_mem
);
340 memory_region_add_subregion(&s
->bar
, 0x08000, dbdma_mem
);
343 static const VMStateDescription vmstate_macio_oldworld
= {
344 .name
= "macio-oldworld",
346 .minimum_version_id
= 0,
347 .fields
= (VMStateField
[]) {
348 VMSTATE_PCI_DEVICE(parent_obj
.parent
, OldWorldMacIOState
),
349 VMSTATE_END_OF_LIST()
353 static void macio_oldworld_class_init(ObjectClass
*oc
, void *data
)
355 PCIDeviceClass
*pdc
= PCI_DEVICE_CLASS(oc
);
356 DeviceClass
*dc
= DEVICE_CLASS(oc
);
358 pdc
->realize
= macio_oldworld_realize
;
359 pdc
->device_id
= PCI_DEVICE_ID_APPLE_343S1201
;
360 dc
->vmsd
= &vmstate_macio_oldworld
;
363 static const VMStateDescription vmstate_macio_newworld
= {
364 .name
= "macio-newworld",
366 .minimum_version_id
= 0,
367 .fields
= (VMStateField
[]) {
368 VMSTATE_PCI_DEVICE(parent_obj
.parent
, NewWorldMacIOState
),
369 VMSTATE_END_OF_LIST()
373 static void macio_newworld_class_init(ObjectClass
*oc
, void *data
)
375 PCIDeviceClass
*pdc
= PCI_DEVICE_CLASS(oc
);
376 DeviceClass
*dc
= DEVICE_CLASS(oc
);
378 pdc
->realize
= macio_newworld_realize
;
379 pdc
->device_id
= PCI_DEVICE_ID_APPLE_UNI_N_KEYL
;
380 dc
->vmsd
= &vmstate_macio_newworld
;
383 static Property macio_properties
[] = {
384 DEFINE_PROP_UINT64("frequency", MacIOState
, frequency
, 0),
385 DEFINE_PROP_END_OF_LIST()
388 static void macio_class_init(ObjectClass
*klass
, void *data
)
390 PCIDeviceClass
*k
= PCI_DEVICE_CLASS(klass
);
391 DeviceClass
*dc
= DEVICE_CLASS(klass
);
393 k
->vendor_id
= PCI_VENDOR_ID_APPLE
;
394 k
->class_id
= PCI_CLASS_OTHERS
<< 8;
395 dc
->props
= macio_properties
;
398 static const TypeInfo macio_oldworld_type_info
= {
399 .name
= TYPE_OLDWORLD_MACIO
,
400 .parent
= TYPE_MACIO
,
401 .instance_size
= sizeof(OldWorldMacIOState
),
402 .instance_init
= macio_oldworld_init
,
403 .class_init
= macio_oldworld_class_init
,
406 static const TypeInfo macio_newworld_type_info
= {
407 .name
= TYPE_NEWWORLD_MACIO
,
408 .parent
= TYPE_MACIO
,
409 .instance_size
= sizeof(NewWorldMacIOState
),
410 .instance_init
= macio_newworld_init
,
411 .class_init
= macio_newworld_class_init
,
414 static const TypeInfo macio_type_info
= {
416 .parent
= TYPE_PCI_DEVICE
,
417 .instance_size
= sizeof(MacIOState
),
418 .instance_init
= macio_instance_init
,
420 .class_init
= macio_class_init
,
423 static void macio_register_types(void)
425 type_register_static(&macio_type_info
);
426 type_register_static(&macio_oldworld_type_info
);
427 type_register_static(&macio_newworld_type_info
);
430 type_init(macio_register_types
)
432 void macio_init(PCIDevice
*d
,
433 MemoryRegion
*pic_mem
,
434 MemoryRegion
*escc_mem
)
436 MacIOState
*macio_state
= MACIO(d
);
438 macio_state
->pic_mem
= pic_mem
;
439 macio_state
->escc_mem
= escc_mem
;
440 /* Note: this code is strongly inspirated from the corresponding code
442 qdev_prop_set_uint64(DEVICE(&macio_state
->cuda
), "frequency",
443 macio_state
->frequency
);
445 qdev_init_nofail(DEVICE(d
));