unbreak usb pass-through on linux.
[qemu.git] / hw / unin_pci.c
bloba202153922789931627a89d8ca83c5c1b7367901
1 /*
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
22 * THE SOFTWARE.
24 #include "hw.h"
25 #include "ppc_mac.h"
26 #include "pci.h"
28 /* debug UniNorth */
29 //#define DEBUG_UNIN
31 #ifdef DEBUG_UNIN
32 #define UNIN_DPRINTF(fmt, ...) \
33 do { printf("UNIN: " fmt , ## __VA_ARGS__); } while (0)
34 #else
35 #define UNIN_DPRINTF(fmt, ...)
36 #endif
38 typedef target_phys_addr_t pci_addr_t;
39 #include "pci_host.h"
41 typedef struct UNINState {
42 SysBusDevice busdev;
43 PCIHostState host_state;
44 } UNINState;
46 static void pci_unin_main_config_writel (void *opaque, target_phys_addr_t addr,
47 uint32_t val)
49 UNINState *s = opaque;
51 UNIN_DPRINTF("config_writel addr " TARGET_FMT_plx " val %x\n", addr, val);
52 #ifdef TARGET_WORDS_BIGENDIAN
53 val = bswap32(val);
54 #endif
56 s->host_state.config_reg = val;
59 static uint32_t pci_unin_main_config_readl (void *opaque,
60 target_phys_addr_t addr)
62 UNINState *s = opaque;
63 uint32_t val;
65 val = s->host_state.config_reg;
66 #ifdef TARGET_WORDS_BIGENDIAN
67 val = bswap32(val);
68 #endif
69 UNIN_DPRINTF("config_readl addr " TARGET_FMT_plx " val %x\n", addr, val);
71 return val;
74 static CPUWriteMemoryFunc * const pci_unin_main_config_write[] = {
75 &pci_unin_main_config_writel,
76 &pci_unin_main_config_writel,
77 &pci_unin_main_config_writel,
80 static CPUReadMemoryFunc * const pci_unin_main_config_read[] = {
81 &pci_unin_main_config_readl,
82 &pci_unin_main_config_readl,
83 &pci_unin_main_config_readl,
86 static CPUWriteMemoryFunc * const pci_unin_main_write[] = {
87 &pci_host_data_writeb,
88 &pci_host_data_writew,
89 &pci_host_data_writel,
92 static CPUReadMemoryFunc * const pci_unin_main_read[] = {
93 &pci_host_data_readb,
94 &pci_host_data_readw,
95 &pci_host_data_readl,
98 static void pci_unin_config_writel (void *opaque, target_phys_addr_t addr,
99 uint32_t val)
101 UNINState *s = opaque;
103 s->host_state.config_reg = val;
106 static uint32_t pci_unin_config_readl (void *opaque,
107 target_phys_addr_t addr)
109 UNINState *s = opaque;
111 return s->host_state.config_reg;
114 static CPUWriteMemoryFunc * const pci_unin_config_write[] = {
115 &pci_unin_config_writel,
116 &pci_unin_config_writel,
117 &pci_unin_config_writel,
120 static CPUReadMemoryFunc * const pci_unin_config_read[] = {
121 &pci_unin_config_readl,
122 &pci_unin_config_readl,
123 &pci_unin_config_readl,
126 static CPUWriteMemoryFunc * const pci_unin_write[] = {
127 &pci_host_data_writeb,
128 &pci_host_data_writew,
129 &pci_host_data_writel,
132 static CPUReadMemoryFunc * const pci_unin_read[] = {
133 &pci_host_data_readb,
134 &pci_host_data_readw,
135 &pci_host_data_readl,
138 /* Don't know if this matches real hardware, but it agrees with OHW. */
139 static int pci_unin_map_irq(PCIDevice *pci_dev, int irq_num)
141 return (irq_num + (pci_dev->devfn >> 3)) & 3;
144 static void pci_unin_set_irq(void *opaque, int irq_num, int level)
146 qemu_irq *pic = opaque;
148 qemu_set_irq(pic[irq_num + 8], level);
151 static void pci_unin_save(QEMUFile* f, void *opaque)
153 PCIDevice *d = opaque;
155 pci_device_save(d, f);
158 static int pci_unin_load(QEMUFile* f, void *opaque, int version_id)
160 PCIDevice *d = opaque;
162 if (version_id != 1)
163 return -EINVAL;
165 return pci_device_load(d, f);
168 static void pci_unin_reset(void *opaque)
172 static int pci_unin_main_init_device(SysBusDevice *dev)
174 UNINState *s;
175 int pci_mem_config, pci_mem_data;
177 /* Use values found on a real PowerMac */
178 /* Uninorth main bus */
179 s = FROM_SYSBUS(UNINState, dev);
181 pci_mem_config = cpu_register_io_memory(pci_unin_main_config_read,
182 pci_unin_main_config_write, s);
183 pci_mem_data = cpu_register_io_memory(pci_unin_main_read,
184 pci_unin_main_write, &s->host_state);
186 sysbus_init_mmio(dev, 0x1000, pci_mem_config);
187 sysbus_init_mmio(dev, 0x1000, pci_mem_data);
189 register_savevm("uninorth", 0, 1, pci_unin_save, pci_unin_load, &s->host_state);
190 qemu_register_reset(pci_unin_reset, &s->host_state);
191 pci_unin_reset(&s->host_state);
192 return 0;
195 static int pci_dec_21154_init_device(SysBusDevice *dev)
197 UNINState *s;
198 int pci_mem_config, pci_mem_data;
200 /* Uninorth bridge */
201 s = FROM_SYSBUS(UNINState, dev);
203 // XXX: s = &pci_bridge[2];
204 pci_mem_config = cpu_register_io_memory(pci_unin_config_read,
205 pci_unin_config_write, s);
206 pci_mem_data = cpu_register_io_memory(pci_unin_main_read,
207 pci_unin_main_write, &s->host_state);
208 sysbus_init_mmio(dev, 0x1000, pci_mem_config);
209 sysbus_init_mmio(dev, 0x1000, pci_mem_data);
210 return 0;
213 static int pci_unin_agp_init_device(SysBusDevice *dev)
215 UNINState *s;
216 int pci_mem_config, pci_mem_data;
218 /* Uninorth AGP bus */
219 s = FROM_SYSBUS(UNINState, dev);
221 pci_mem_config = cpu_register_io_memory(pci_unin_config_read,
222 pci_unin_config_write, s);
223 pci_mem_data = cpu_register_io_memory(pci_unin_main_read,
224 pci_unin_main_write, &s->host_state);
225 sysbus_init_mmio(dev, 0x1000, pci_mem_config);
226 sysbus_init_mmio(dev, 0x1000, pci_mem_data);
227 return 0;
230 static int pci_unin_internal_init_device(SysBusDevice *dev)
232 UNINState *s;
233 int pci_mem_config, pci_mem_data;
235 /* Uninorth internal bus */
236 s = FROM_SYSBUS(UNINState, dev);
238 pci_mem_config = cpu_register_io_memory(pci_unin_config_read,
239 pci_unin_config_write, s);
240 pci_mem_data = cpu_register_io_memory(pci_unin_read,
241 pci_unin_write, s);
242 sysbus_init_mmio(dev, 0x1000, pci_mem_config);
243 sysbus_init_mmio(dev, 0x1000, pci_mem_data);
244 return 0;
247 PCIBus *pci_pmac_init(qemu_irq *pic)
249 DeviceState *dev;
250 SysBusDevice *s;
251 UNINState *d;
253 /* Use values found on a real PowerMac */
254 /* Uninorth main bus */
255 dev = qdev_create(NULL, "Uni-north main");
256 qdev_init(dev);
257 s = sysbus_from_qdev(dev);
258 d = FROM_SYSBUS(UNINState, s);
259 d->host_state.bus = pci_register_bus(&d->busdev.qdev, "pci",
260 pci_unin_set_irq, pci_unin_map_irq,
261 pic, 11 << 3, 4);
263 pci_create_simple(d->host_state.bus, 11 << 3, "Uni-north main");
265 sysbus_mmio_map(s, 0, 0xf2800000);
266 sysbus_mmio_map(s, 1, 0xf2c00000);
268 /* DEC 21154 bridge */
269 #if 0
270 /* XXX: not activated as PPC BIOS doesn't handle multiple buses properly */
271 pci_create_simple(d->host_state.bus, 12 << 3, "DEC 21154");
272 #endif
274 /* Uninorth AGP bus */
275 pci_create_simple(d->host_state.bus, 13 << 3, "Uni-north AGP");
277 /* Uninorth internal bus */
278 #if 0
279 /* XXX: not needed for now */
280 pci_create_simple(d->host_state.bus, 14 << 3, "Uni-north internal");
281 #endif
283 return d->host_state.bus;
286 static int unin_main_pci_host_init(PCIDevice *d)
288 pci_config_set_vendor_id(d->config, PCI_VENDOR_ID_APPLE);
289 pci_config_set_device_id(d->config, PCI_DEVICE_ID_APPLE_UNI_N_PCI);
290 d->config[0x08] = 0x00; // revision
291 pci_config_set_class(d->config, PCI_CLASS_BRIDGE_HOST);
292 d->config[0x0C] = 0x08; // cache_line_size
293 d->config[0x0D] = 0x10; // latency_timer
294 d->config[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL; // header_type
295 d->config[0x34] = 0x00; // capabilities_pointer
296 return 0;
299 static int dec_21154_pci_host_init(PCIDevice *d)
301 /* pci-to-pci bridge */
302 pci_config_set_vendor_id(d->config, PCI_VENDOR_ID_DEC);
303 pci_config_set_device_id(d->config, PCI_DEVICE_ID_DEC_21154);
304 d->config[0x08] = 0x05; // revision
305 pci_config_set_class(d->config, PCI_CLASS_BRIDGE_PCI);
306 d->config[0x0C] = 0x08; // cache_line_size
307 d->config[0x0D] = 0x20; // latency_timer
308 d->config[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_BRIDGE; // header_type
310 d->config[0x18] = 0x01; // primary_bus
311 d->config[0x19] = 0x02; // secondary_bus
312 d->config[0x1A] = 0x02; // subordinate_bus
313 d->config[0x1B] = 0x20; // secondary_latency_timer
314 d->config[0x1C] = 0x11; // io_base
315 d->config[0x1D] = 0x01; // io_limit
316 d->config[0x20] = 0x00; // memory_base
317 d->config[0x21] = 0x80;
318 d->config[0x22] = 0x00; // memory_limit
319 d->config[0x23] = 0x80;
320 d->config[0x24] = 0x01; // prefetchable_memory_base
321 d->config[0x25] = 0x80;
322 d->config[0x26] = 0xF1; // prefectchable_memory_limit
323 d->config[0x27] = 0x7F;
324 // d->config[0x34] = 0xdc // capabilities_pointer
325 return 0;
328 static int unin_agp_pci_host_init(PCIDevice *d)
330 pci_config_set_vendor_id(d->config, PCI_VENDOR_ID_APPLE);
331 pci_config_set_device_id(d->config, PCI_DEVICE_ID_APPLE_UNI_N_AGP);
332 d->config[0x08] = 0x00; // revision
333 pci_config_set_class(d->config, PCI_CLASS_BRIDGE_HOST);
334 d->config[0x0C] = 0x08; // cache_line_size
335 d->config[0x0D] = 0x10; // latency_timer
336 d->config[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL; // header_type
337 // d->config[0x34] = 0x80; // capabilities_pointer
338 return 0;
341 static int unin_internal_pci_host_init(PCIDevice *d)
343 pci_config_set_vendor_id(d->config, PCI_VENDOR_ID_APPLE);
344 pci_config_set_device_id(d->config, PCI_DEVICE_ID_APPLE_UNI_N_I_PCI);
345 d->config[0x08] = 0x00; // revision
346 pci_config_set_class(d->config, PCI_CLASS_BRIDGE_HOST);
347 d->config[0x0C] = 0x08; // cache_line_size
348 d->config[0x0D] = 0x10; // latency_timer
349 d->config[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL; // header_type
350 d->config[0x34] = 0x00; // capabilities_pointer
351 return 0;
354 static PCIDeviceInfo unin_main_pci_host_info = {
355 .qdev.name = "Uni-north main",
356 .qdev.size = sizeof(PCIDevice),
357 .init = unin_main_pci_host_init,
360 static PCIDeviceInfo dec_21154_pci_host_info = {
361 .qdev.name = "DEC 21154",
362 .qdev.size = sizeof(PCIDevice),
363 .init = dec_21154_pci_host_init,
366 static PCIDeviceInfo unin_agp_pci_host_info = {
367 .qdev.name = "Uni-north AGP",
368 .qdev.size = sizeof(PCIDevice),
369 .init = unin_agp_pci_host_init,
372 static PCIDeviceInfo unin_internal_pci_host_info = {
373 .qdev.name = "Uni-north internal",
374 .qdev.size = sizeof(PCIDevice),
375 .init = unin_internal_pci_host_init,
378 static void unin_register_devices(void)
380 sysbus_register_dev("Uni-north main", sizeof(UNINState),
381 pci_unin_main_init_device);
382 pci_qdev_register(&unin_main_pci_host_info);
383 sysbus_register_dev("DEC 21154", sizeof(UNINState),
384 pci_dec_21154_init_device);
385 pci_qdev_register(&dec_21154_pci_host_info);
386 sysbus_register_dev("Uni-north AGP", sizeof(UNINState),
387 pci_unin_agp_init_device);
388 pci_qdev_register(&unin_agp_pci_host_info);
389 sysbus_register_dev("Uni-north internal", sizeof(UNINState),
390 pci_unin_internal_init_device);
391 pci_qdev_register(&unin_internal_pci_host_info);
394 device_init(unin_register_devices)