2 * QEMU Uninorth PCI host (for all Mac99 and newer machines)
4 * Copyright (c) 2006 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33 #define UNIN_DPRINTF(fmt, ...) \
34 do { printf("UNIN: " fmt , ## __VA_ARGS__); } while (0)
36 #define UNIN_DPRINTF(fmt, ...)
39 static const int unin_irq_line
[] = { 0x1b, 0x1c, 0x1d, 0x1e };
41 typedef struct UNINState
{
43 PCIHostState host_state
;
44 ReadWriteHandler data_handler
;
47 static int pci_unin_map_irq(PCIDevice
*pci_dev
, int irq_num
)
50 int devfn
= pci_dev
->devfn
& 0x00FFFFFF;
52 retval
= (((devfn
>> 11) & 0x1F) + irq_num
) & 3;
57 static void pci_unin_set_irq(void *opaque
, int irq_num
, int level
)
59 qemu_irq
*pic
= opaque
;
61 UNIN_DPRINTF("%s: setting INT %d = %d\n", __func__
,
62 unin_irq_line
[irq_num
], level
);
63 qemu_set_irq(pic
[unin_irq_line
[irq_num
]], level
);
66 static void pci_unin_save(QEMUFile
* f
, void *opaque
)
68 PCIDevice
*d
= opaque
;
70 pci_device_save(d
, f
);
73 static int pci_unin_load(QEMUFile
* f
, void *opaque
, int version_id
)
75 PCIDevice
*d
= opaque
;
80 return pci_device_load(d
, f
);
83 static void pci_unin_reset(void *opaque
)
87 static uint32_t unin_get_config_reg(uint32_t reg
, uint32_t addr
)
91 if (reg
& (1u << 31)) {
92 /* XXX OpenBIOS compatibility hack */
93 retval
= reg
| (addr
& 3);
96 retval
= (reg
& ~7u) | (addr
& 7);
100 /* Grab CFA0 style values */
101 slot
= ffs(reg
& 0xfffff800) - 1;
102 func
= (reg
>> 8) & 7;
104 /* ... and then convert them to x86 format */
106 retval
= (reg
& (0xff - 7)) | (addr
& 7);
108 retval
|= slot
<< 11;
114 UNIN_DPRINTF("Converted config space accessor %08x/%08x -> %08x\n",
120 static void unin_data_write(ReadWriteHandler
*handler
,
121 pcibus_t addr
, uint32_t val
, int len
)
123 UNINState
*s
= container_of(handler
, UNINState
, data_handler
);
124 val
= qemu_bswap_len(val
, len
);
125 UNIN_DPRINTF("write addr %" FMT_PCIBUS
" len %d val %x\n", addr
, len
, val
);
126 pci_data_write(s
->host_state
.bus
,
127 unin_get_config_reg(s
->host_state
.config_reg
, addr
),
131 static uint32_t unin_data_read(ReadWriteHandler
*handler
,
132 pcibus_t addr
, int len
)
134 UNINState
*s
= container_of(handler
, UNINState
, data_handler
);
137 val
= pci_data_read(s
->host_state
.bus
,
138 unin_get_config_reg(s
->host_state
.config_reg
, addr
),
140 UNIN_DPRINTF("read addr %" FMT_PCIBUS
" len %d val %x\n", addr
, len
, val
);
141 val
= qemu_bswap_len(val
, len
);
145 static int pci_unin_main_init_device(SysBusDevice
*dev
)
148 int pci_mem_config
, pci_mem_data
;
150 /* Use values found on a real PowerMac */
151 /* Uninorth main bus */
152 s
= FROM_SYSBUS(UNINState
, dev
);
154 pci_mem_config
= pci_host_conf_register_mmio(&s
->host_state
, 1);
155 s
->data_handler
.read
= unin_data_read
;
156 s
->data_handler
.write
= unin_data_write
;
157 pci_mem_data
= cpu_register_io_memory_simple(&s
->data_handler
);
158 sysbus_init_mmio(dev
, 0x1000, pci_mem_config
);
159 sysbus_init_mmio(dev
, 0x1000, pci_mem_data
);
161 register_savevm(&dev
->qdev
, "uninorth", 0, 1,
162 pci_unin_save
, pci_unin_load
, &s
->host_state
);
163 qemu_register_reset(pci_unin_reset
, &s
->host_state
);
167 static int pci_u3_agp_init_device(SysBusDevice
*dev
)
170 int pci_mem_config
, pci_mem_data
;
172 /* Uninorth U3 AGP bus */
173 s
= FROM_SYSBUS(UNINState
, dev
);
175 pci_mem_config
= pci_host_conf_register_mmio(&s
->host_state
, 1);
176 s
->data_handler
.read
= unin_data_read
;
177 s
->data_handler
.write
= unin_data_write
;
178 pci_mem_data
= cpu_register_io_memory_simple(&s
->data_handler
);
179 sysbus_init_mmio(dev
, 0x1000, pci_mem_config
);
180 sysbus_init_mmio(dev
, 0x1000, pci_mem_data
);
182 register_savevm(&dev
->qdev
, "uninorth", 0, 1,
183 pci_unin_save
, pci_unin_load
, &s
->host_state
);
184 qemu_register_reset(pci_unin_reset
, &s
->host_state
);
189 static int pci_unin_agp_init_device(SysBusDevice
*dev
)
192 int pci_mem_config
, pci_mem_data
;
194 /* Uninorth AGP bus */
195 s
= FROM_SYSBUS(UNINState
, dev
);
197 pci_mem_config
= pci_host_conf_register_mmio(&s
->host_state
, 0);
198 pci_mem_data
= pci_host_data_register_mmio(&s
->host_state
, 1);
199 sysbus_init_mmio(dev
, 0x1000, pci_mem_config
);
200 sysbus_init_mmio(dev
, 0x1000, pci_mem_data
);
204 static int pci_unin_internal_init_device(SysBusDevice
*dev
)
207 int pci_mem_config
, pci_mem_data
;
209 /* Uninorth internal bus */
210 s
= FROM_SYSBUS(UNINState
, dev
);
212 pci_mem_config
= pci_host_conf_register_mmio(&s
->host_state
, 0);
213 pci_mem_data
= pci_host_data_register_mmio(&s
->host_state
, 1);
214 sysbus_init_mmio(dev
, 0x1000, pci_mem_config
);
215 sysbus_init_mmio(dev
, 0x1000, pci_mem_data
);
219 PCIBus
*pci_pmac_init(qemu_irq
*pic
)
225 /* Use values found on a real PowerMac */
226 /* Uninorth main bus */
227 dev
= qdev_create(NULL
, "uni-north");
228 qdev_init_nofail(dev
);
229 s
= sysbus_from_qdev(dev
);
230 d
= FROM_SYSBUS(UNINState
, s
);
231 d
->host_state
.bus
= pci_register_bus(&d
->busdev
.qdev
, "pci",
232 pci_unin_set_irq
, pci_unin_map_irq
,
233 pic
, PCI_DEVFN(11, 0), 4);
236 pci_create_simple(d
->host_state
.bus
, PCI_DEVFN(11, 0), "uni-north");
239 sysbus_mmio_map(s
, 0, 0xf2800000);
240 sysbus_mmio_map(s
, 1, 0xf2c00000);
242 /* DEC 21154 bridge */
244 /* XXX: not activated as PPC BIOS doesn't handle multiple buses properly */
245 pci_create_simple(d
->host_state
.bus
, PCI_DEVFN(12, 0), "dec-21154");
248 /* Uninorth AGP bus */
249 pci_create_simple(d
->host_state
.bus
, PCI_DEVFN(11, 0), "uni-north-agp");
250 dev
= qdev_create(NULL
, "uni-north-agp");
251 qdev_init_nofail(dev
);
252 s
= sysbus_from_qdev(dev
);
253 sysbus_mmio_map(s
, 0, 0xf0800000);
254 sysbus_mmio_map(s
, 1, 0xf0c00000);
256 /* Uninorth internal bus */
258 /* XXX: not needed for now */
259 pci_create_simple(d
->host_state
.bus
, PCI_DEVFN(14, 0), "uni-north-pci");
260 dev
= qdev_create(NULL
, "uni-north-pci");
261 qdev_init_nofail(dev
);
262 s
= sysbus_from_qdev(dev
);
263 sysbus_mmio_map(s
, 0, 0xf4800000);
264 sysbus_mmio_map(s
, 1, 0xf4c00000);
267 return d
->host_state
.bus
;
270 PCIBus
*pci_pmac_u3_init(qemu_irq
*pic
)
276 /* Uninorth AGP bus */
278 dev
= qdev_create(NULL
, "u3-agp");
279 qdev_init_nofail(dev
);
280 s
= sysbus_from_qdev(dev
);
281 d
= FROM_SYSBUS(UNINState
, s
);
283 d
->host_state
.bus
= pci_register_bus(&d
->busdev
.qdev
, "pci",
284 pci_unin_set_irq
, pci_unin_map_irq
,
285 pic
, PCI_DEVFN(11, 0), 4);
287 sysbus_mmio_map(s
, 0, 0xf0800000);
288 sysbus_mmio_map(s
, 1, 0xf0c00000);
290 pci_create_simple(d
->host_state
.bus
, 11 << 3, "u3-agp");
292 return d
->host_state
.bus
;
295 static int unin_main_pci_host_init(PCIDevice
*d
)
297 pci_config_set_vendor_id(d
->config
, PCI_VENDOR_ID_APPLE
);
298 pci_config_set_device_id(d
->config
, PCI_DEVICE_ID_APPLE_UNI_N_PCI
);
299 d
->config
[0x08] = 0x00; // revision
300 pci_config_set_class(d
->config
, PCI_CLASS_BRIDGE_HOST
);
301 d
->config
[0x0C] = 0x08; // cache_line_size
302 d
->config
[0x0D] = 0x10; // latency_timer
303 d
->config
[0x34] = 0x00; // capabilities_pointer
307 static int unin_agp_pci_host_init(PCIDevice
*d
)
309 pci_config_set_vendor_id(d
->config
, PCI_VENDOR_ID_APPLE
);
310 pci_config_set_device_id(d
->config
, PCI_DEVICE_ID_APPLE_UNI_N_AGP
);
311 d
->config
[0x08] = 0x00; // revision
312 pci_config_set_class(d
->config
, PCI_CLASS_BRIDGE_HOST
);
313 d
->config
[0x0C] = 0x08; // cache_line_size
314 d
->config
[0x0D] = 0x10; // latency_timer
315 // d->config[0x34] = 0x80; // capabilities_pointer
319 static int u3_agp_pci_host_init(PCIDevice
*d
)
321 pci_config_set_vendor_id(d
->config
, PCI_VENDOR_ID_APPLE
);
322 pci_config_set_device_id(d
->config
, PCI_DEVICE_ID_APPLE_U3_AGP
);
324 d
->config
[0x08] = 0x00;
325 pci_config_set_class(d
->config
, PCI_CLASS_BRIDGE_HOST
);
326 /* cache line size */
327 d
->config
[0x0C] = 0x08;
329 d
->config
[0x0D] = 0x10;
333 static int unin_internal_pci_host_init(PCIDevice
*d
)
335 pci_config_set_vendor_id(d
->config
, PCI_VENDOR_ID_APPLE
);
336 pci_config_set_device_id(d
->config
, PCI_DEVICE_ID_APPLE_UNI_N_I_PCI
);
337 d
->config
[0x08] = 0x00; // revision
338 pci_config_set_class(d
->config
, PCI_CLASS_BRIDGE_HOST
);
339 d
->config
[0x0C] = 0x08; // cache_line_size
340 d
->config
[0x0D] = 0x10; // latency_timer
341 d
->config
[0x34] = 0x00; // capabilities_pointer
345 static PCIDeviceInfo unin_main_pci_host_info
= {
346 .qdev
.name
= "uni-north",
347 .qdev
.size
= sizeof(PCIDevice
),
348 .init
= unin_main_pci_host_init
,
351 static PCIDeviceInfo u3_agp_pci_host_info
= {
352 .qdev
.name
= "u3-agp",
353 .qdev
.size
= sizeof(PCIDevice
),
354 .init
= u3_agp_pci_host_init
,
357 static PCIDeviceInfo unin_agp_pci_host_info
= {
358 .qdev
.name
= "uni-north-agp",
359 .qdev
.size
= sizeof(PCIDevice
),
360 .init
= unin_agp_pci_host_init
,
363 static PCIDeviceInfo unin_internal_pci_host_info
= {
364 .qdev
.name
= "uni-north-pci",
365 .qdev
.size
= sizeof(PCIDevice
),
366 .init
= unin_internal_pci_host_init
,
369 static void unin_register_devices(void)
371 sysbus_register_dev("uni-north", sizeof(UNINState
),
372 pci_unin_main_init_device
);
373 pci_qdev_register(&unin_main_pci_host_info
);
374 sysbus_register_dev("u3-agp", sizeof(UNINState
),
375 pci_u3_agp_init_device
);
376 pci_qdev_register(&u3_agp_pci_host_info
);
377 sysbus_register_dev("uni-north-agp", sizeof(UNINState
),
378 pci_unin_agp_init_device
);
379 pci_qdev_register(&unin_agp_pci_host_info
);
380 sysbus_register_dev("uni-north-pci", sizeof(UNINState
),
381 pci_unin_internal_init_device
);
382 pci_qdev_register(&unin_internal_pci_host_info
);
385 device_init(unin_register_devices
)