root / hw / unin_pci.c @ 0f921197
History | View | Annotate | Download (11.6 kB)
1 |
/*
|
---|---|
2 |
* QEMU Uninorth PCI host (for all Mac99 and newer machines)
|
3 |
*
|
4 |
* Copyright (c) 2006 Fabrice Bellard
|
5 |
*
|
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:
|
12 |
*
|
13 |
* The above copyright notice and this permission notice shall be included in
|
14 |
* all copies or substantial portions of the Software.
|
15 |
*
|
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.
|
23 |
*/
|
24 |
#include "hw.h" |
25 |
#include "ppc_mac.h" |
26 |
#include "pci.h" |
27 |
#include "pci_host.h" |
28 |
|
29 |
/* debug UniNorth */
|
30 |
//#define DEBUG_UNIN
|
31 |
|
32 |
#ifdef DEBUG_UNIN
|
33 |
#define UNIN_DPRINTF(fmt, ...) \
|
34 |
do { printf("UNIN: " fmt , ## __VA_ARGS__); } while (0) |
35 |
#else
|
36 |
#define UNIN_DPRINTF(fmt, ...)
|
37 |
#endif
|
38 |
|
39 |
typedef struct UNINState { |
40 |
SysBusDevice busdev; |
41 |
PCIHostState host_state; |
42 |
ReadWriteHandler data_handler; |
43 |
} UNINState; |
44 |
|
45 |
/* Don't know if this matches real hardware, but it agrees with OHW. */
|
46 |
static int pci_unin_map_irq(PCIDevice *pci_dev, int irq_num) |
47 |
{ |
48 |
return (irq_num + (pci_dev->devfn >> 3)) & 3; |
49 |
} |
50 |
|
51 |
static void pci_unin_set_irq(void *opaque, int irq_num, int level) |
52 |
{ |
53 |
qemu_irq *pic = opaque; |
54 |
|
55 |
qemu_set_irq(pic[irq_num + 8], level);
|
56 |
} |
57 |
|
58 |
static void pci_unin_save(QEMUFile* f, void *opaque) |
59 |
{ |
60 |
PCIDevice *d = opaque; |
61 |
|
62 |
pci_device_save(d, f); |
63 |
} |
64 |
|
65 |
static int pci_unin_load(QEMUFile* f, void *opaque, int version_id) |
66 |
{ |
67 |
PCIDevice *d = opaque; |
68 |
|
69 |
if (version_id != 1) |
70 |
return -EINVAL;
|
71 |
|
72 |
return pci_device_load(d, f);
|
73 |
} |
74 |
|
75 |
static void pci_unin_reset(void *opaque) |
76 |
{ |
77 |
} |
78 |
|
79 |
static uint32_t unin_get_config_reg(uint32_t reg, uint32_t addr)
|
80 |
{ |
81 |
uint32_t retval; |
82 |
|
83 |
if (reg & (1u << 31)) { |
84 |
/* XXX OpenBIOS compatibility hack */
|
85 |
retval = reg | (addr & 3);
|
86 |
} else if (reg & 1) { |
87 |
/* CFA1 style */
|
88 |
retval = (reg & ~7u) | (addr & 7); |
89 |
} else {
|
90 |
uint32_t slot, func; |
91 |
|
92 |
/* Grab CFA0 style values */
|
93 |
slot = ffs(reg & 0xfffff800) - 1; |
94 |
func = (reg >> 8) & 7; |
95 |
|
96 |
/* ... and then convert them to x86 format */
|
97 |
/* config pointer */
|
98 |
retval = (reg & (0xff - 7)) | (addr & 7); |
99 |
/* slot */
|
100 |
retval |= slot << 11;
|
101 |
/* fn */
|
102 |
retval |= func << 8;
|
103 |
} |
104 |
|
105 |
|
106 |
UNIN_DPRINTF("Converted config space accessor %08x/%08x -> %08x\n",
|
107 |
reg, addr, retval); |
108 |
|
109 |
return retval;
|
110 |
} |
111 |
|
112 |
static void unin_data_write(ReadWriteHandler *handler, |
113 |
pcibus_t addr, uint32_t val, int len)
|
114 |
{ |
115 |
UNINState *s = container_of(handler, UNINState, data_handler); |
116 |
#ifdef TARGET_WORDS_BIGENDIAN
|
117 |
val = qemu_bswap_len(val, len); |
118 |
#endif
|
119 |
UNIN_DPRINTF("write addr %" FMT_PCIBUS " len %d val %x\n", addr, len, val); |
120 |
pci_data_write(s->host_state.bus, |
121 |
unin_get_config_reg(s->host_state.config_reg, addr), |
122 |
val, len); |
123 |
} |
124 |
|
125 |
static uint32_t unin_data_read(ReadWriteHandler *handler,
|
126 |
pcibus_t addr, int len)
|
127 |
{ |
128 |
UNINState *s = container_of(handler, UNINState, data_handler); |
129 |
uint32_t val; |
130 |
|
131 |
val = pci_data_read(s->host_state.bus, |
132 |
unin_get_config_reg(s->host_state.config_reg, addr), |
133 |
len); |
134 |
UNIN_DPRINTF("read addr %" FMT_PCIBUS " len %d val %x\n", addr, len, val); |
135 |
#ifdef TARGET_WORDS_BIGENDIAN
|
136 |
val = qemu_bswap_len(val, len); |
137 |
#endif
|
138 |
return val;
|
139 |
} |
140 |
|
141 |
static int pci_unin_main_init_device(SysBusDevice *dev) |
142 |
{ |
143 |
UNINState *s; |
144 |
int pci_mem_config, pci_mem_data;
|
145 |
|
146 |
/* Use values found on a real PowerMac */
|
147 |
/* Uninorth main bus */
|
148 |
s = FROM_SYSBUS(UNINState, dev); |
149 |
|
150 |
pci_mem_config = pci_host_conf_register_mmio(&s->host_state); |
151 |
s->data_handler.read = unin_data_read; |
152 |
s->data_handler.write = unin_data_write; |
153 |
pci_mem_data = cpu_register_io_memory_simple(&s->data_handler); |
154 |
sysbus_init_mmio(dev, 0x1000, pci_mem_config);
|
155 |
sysbus_init_mmio(dev, 0x1000, pci_mem_data);
|
156 |
|
157 |
register_savevm("uninorth", 0, 1, pci_unin_save, pci_unin_load, &s->host_state); |
158 |
qemu_register_reset(pci_unin_reset, &s->host_state); |
159 |
return 0; |
160 |
} |
161 |
|
162 |
static int pci_u3_agp_init_device(SysBusDevice *dev) |
163 |
{ |
164 |
UNINState *s; |
165 |
int pci_mem_config, pci_mem_data;
|
166 |
|
167 |
/* Uninorth U3 AGP bus */
|
168 |
s = FROM_SYSBUS(UNINState, dev); |
169 |
|
170 |
pci_mem_config = pci_host_conf_register_mmio(&s->host_state); |
171 |
s->data_handler.read = unin_data_read; |
172 |
s->data_handler.write = unin_data_write; |
173 |
pci_mem_data = cpu_register_io_memory_simple(&s->data_handler); |
174 |
sysbus_init_mmio(dev, 0x1000, pci_mem_config);
|
175 |
sysbus_init_mmio(dev, 0x1000, pci_mem_data);
|
176 |
|
177 |
register_savevm("uninorth", 0, 1, pci_unin_save, pci_unin_load, &s->host_state); |
178 |
qemu_register_reset(pci_unin_reset, &s->host_state); |
179 |
|
180 |
return 0; |
181 |
} |
182 |
|
183 |
static int pci_unin_agp_init_device(SysBusDevice *dev) |
184 |
{ |
185 |
UNINState *s; |
186 |
int pci_mem_config, pci_mem_data;
|
187 |
|
188 |
/* Uninorth AGP bus */
|
189 |
s = FROM_SYSBUS(UNINState, dev); |
190 |
|
191 |
pci_mem_config = pci_host_conf_register_mmio_noswap(&s->host_state); |
192 |
pci_mem_data = pci_host_data_register_mmio(&s->host_state); |
193 |
sysbus_init_mmio(dev, 0x1000, pci_mem_config);
|
194 |
sysbus_init_mmio(dev, 0x1000, pci_mem_data);
|
195 |
return 0; |
196 |
} |
197 |
|
198 |
static int pci_unin_internal_init_device(SysBusDevice *dev) |
199 |
{ |
200 |
UNINState *s; |
201 |
int pci_mem_config, pci_mem_data;
|
202 |
|
203 |
/* Uninorth internal bus */
|
204 |
s = FROM_SYSBUS(UNINState, dev); |
205 |
|
206 |
pci_mem_config = pci_host_conf_register_mmio_noswap(&s->host_state); |
207 |
pci_mem_data = pci_host_data_register_mmio(&s->host_state); |
208 |
sysbus_init_mmio(dev, 0x1000, pci_mem_config);
|
209 |
sysbus_init_mmio(dev, 0x1000, pci_mem_data);
|
210 |
return 0; |
211 |
} |
212 |
|
213 |
PCIBus *pci_pmac_init(qemu_irq *pic) |
214 |
{ |
215 |
DeviceState *dev; |
216 |
SysBusDevice *s; |
217 |
UNINState *d; |
218 |
|
219 |
/* Use values found on a real PowerMac */
|
220 |
/* Uninorth main bus */
|
221 |
dev = qdev_create(NULL, "uni-north"); |
222 |
qdev_init_nofail(dev); |
223 |
s = sysbus_from_qdev(dev); |
224 |
d = FROM_SYSBUS(UNINState, s); |
225 |
d->host_state.bus = pci_register_bus(&d->busdev.qdev, "pci",
|
226 |
pci_unin_set_irq, pci_unin_map_irq, |
227 |
pic, 11 << 3, 4); |
228 |
|
229 |
#if 0
|
230 |
pci_create_simple(d->host_state.bus, 11 << 3, "uni-north");
|
231 |
#endif
|
232 |
|
233 |
sysbus_mmio_map(s, 0, 0xf2800000); |
234 |
sysbus_mmio_map(s, 1, 0xf2c00000); |
235 |
|
236 |
/* DEC 21154 bridge */
|
237 |
#if 0
|
238 |
/* XXX: not activated as PPC BIOS doesn't handle multiple buses properly */
|
239 |
pci_create_simple(d->host_state.bus, 12 << 3, "dec-21154");
|
240 |
#endif
|
241 |
|
242 |
/* Uninorth AGP bus */
|
243 |
pci_create_simple(d->host_state.bus, 11 << 3, "uni-north-agp"); |
244 |
dev = qdev_create(NULL, "uni-north-agp"); |
245 |
qdev_init_nofail(dev); |
246 |
s = sysbus_from_qdev(dev); |
247 |
sysbus_mmio_map(s, 0, 0xf0800000); |
248 |
sysbus_mmio_map(s, 1, 0xf0c00000); |
249 |
|
250 |
/* Uninorth internal bus */
|
251 |
#if 0
|
252 |
/* XXX: not needed for now */
|
253 |
pci_create_simple(d->host_state.bus, 14 << 3, "uni-north-pci");
|
254 |
dev = qdev_create(NULL, "uni-north-pci");
|
255 |
qdev_init_nofail(dev);
|
256 |
s = sysbus_from_qdev(dev);
|
257 |
sysbus_mmio_map(s, 0, 0xf4800000);
|
258 |
sysbus_mmio_map(s, 1, 0xf4c00000);
|
259 |
#endif
|
260 |
|
261 |
return d->host_state.bus;
|
262 |
} |
263 |
|
264 |
PCIBus *pci_pmac_u3_init(qemu_irq *pic) |
265 |
{ |
266 |
DeviceState *dev; |
267 |
SysBusDevice *s; |
268 |
UNINState *d; |
269 |
|
270 |
/* Uninorth AGP bus */
|
271 |
|
272 |
dev = qdev_create(NULL, "u3-agp"); |
273 |
qdev_init_nofail(dev); |
274 |
s = sysbus_from_qdev(dev); |
275 |
d = FROM_SYSBUS(UNINState, s); |
276 |
|
277 |
d->host_state.bus = pci_register_bus(&d->busdev.qdev, "pci",
|
278 |
pci_unin_set_irq, pci_unin_map_irq, |
279 |
pic, 11 << 3, 4); |
280 |
|
281 |
sysbus_mmio_map(s, 0, 0xf0800000); |
282 |
sysbus_mmio_map(s, 1, 0xf0c00000); |
283 |
|
284 |
pci_create_simple(d->host_state.bus, 11 << 3, "u3-agp"); |
285 |
|
286 |
return d->host_state.bus;
|
287 |
} |
288 |
|
289 |
static int unin_main_pci_host_init(PCIDevice *d) |
290 |
{ |
291 |
pci_config_set_vendor_id(d->config, PCI_VENDOR_ID_APPLE); |
292 |
pci_config_set_device_id(d->config, PCI_DEVICE_ID_APPLE_UNI_N_PCI); |
293 |
d->config[0x08] = 0x00; // revision |
294 |
pci_config_set_class(d->config, PCI_CLASS_BRIDGE_HOST); |
295 |
d->config[0x0C] = 0x08; // cache_line_size |
296 |
d->config[0x0D] = 0x10; // latency_timer |
297 |
d->config[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL; // header_type
|
298 |
d->config[0x34] = 0x00; // capabilities_pointer |
299 |
return 0; |
300 |
} |
301 |
|
302 |
static int unin_agp_pci_host_init(PCIDevice *d) |
303 |
{ |
304 |
pci_config_set_vendor_id(d->config, PCI_VENDOR_ID_APPLE); |
305 |
pci_config_set_device_id(d->config, PCI_DEVICE_ID_APPLE_UNI_N_AGP); |
306 |
d->config[0x08] = 0x00; // revision |
307 |
pci_config_set_class(d->config, PCI_CLASS_BRIDGE_HOST); |
308 |
d->config[0x0C] = 0x08; // cache_line_size |
309 |
d->config[0x0D] = 0x10; // latency_timer |
310 |
d->config[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL; // header_type
|
311 |
// d->config[0x34] = 0x80; // capabilities_pointer
|
312 |
return 0; |
313 |
} |
314 |
|
315 |
static int u3_agp_pci_host_init(PCIDevice *d) |
316 |
{ |
317 |
pci_config_set_vendor_id(d->config, PCI_VENDOR_ID_APPLE); |
318 |
pci_config_set_device_id(d->config, PCI_DEVICE_ID_APPLE_U3_AGP); |
319 |
/* revision */
|
320 |
d->config[0x08] = 0x00; |
321 |
pci_config_set_class(d->config, PCI_CLASS_BRIDGE_HOST); |
322 |
/* cache line size */
|
323 |
d->config[0x0C] = 0x08; |
324 |
/* latency timer */
|
325 |
d->config[0x0D] = 0x10; |
326 |
d->config[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL; |
327 |
return 0; |
328 |
} |
329 |
|
330 |
static int unin_internal_pci_host_init(PCIDevice *d) |
331 |
{ |
332 |
pci_config_set_vendor_id(d->config, PCI_VENDOR_ID_APPLE); |
333 |
pci_config_set_device_id(d->config, PCI_DEVICE_ID_APPLE_UNI_N_I_PCI); |
334 |
d->config[0x08] = 0x00; // revision |
335 |
pci_config_set_class(d->config, PCI_CLASS_BRIDGE_HOST); |
336 |
d->config[0x0C] = 0x08; // cache_line_size |
337 |
d->config[0x0D] = 0x10; // latency_timer |
338 |
d->config[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL; // header_type
|
339 |
d->config[0x34] = 0x00; // capabilities_pointer |
340 |
return 0; |
341 |
} |
342 |
|
343 |
static PCIDeviceInfo unin_main_pci_host_info = {
|
344 |
.qdev.name = "uni-north",
|
345 |
.qdev.size = sizeof(PCIDevice),
|
346 |
.init = unin_main_pci_host_init, |
347 |
}; |
348 |
|
349 |
static PCIDeviceInfo u3_agp_pci_host_info = {
|
350 |
.qdev.name = "u3-agp",
|
351 |
.qdev.size = sizeof(PCIDevice),
|
352 |
.init = u3_agp_pci_host_init, |
353 |
}; |
354 |
|
355 |
static PCIDeviceInfo unin_agp_pci_host_info = {
|
356 |
.qdev.name = "uni-north-agp",
|
357 |
.qdev.size = sizeof(PCIDevice),
|
358 |
.init = unin_agp_pci_host_init, |
359 |
}; |
360 |
|
361 |
static PCIDeviceInfo unin_internal_pci_host_info = {
|
362 |
.qdev.name = "uni-north-pci",
|
363 |
.qdev.size = sizeof(PCIDevice),
|
364 |
.init = unin_internal_pci_host_init, |
365 |
}; |
366 |
|
367 |
static void unin_register_devices(void) |
368 |
{ |
369 |
sysbus_register_dev("uni-north", sizeof(UNINState), |
370 |
pci_unin_main_init_device); |
371 |
pci_qdev_register(&unin_main_pci_host_info); |
372 |
sysbus_register_dev("u3-agp", sizeof(UNINState), |
373 |
pci_u3_agp_init_device); |
374 |
pci_qdev_register(&u3_agp_pci_host_info); |
375 |
sysbus_register_dev("uni-north-agp", sizeof(UNINState), |
376 |
pci_unin_agp_init_device); |
377 |
pci_qdev_register(&unin_agp_pci_host_info); |
378 |
sysbus_register_dev("uni-north-pci", sizeof(UNINState), |
379 |
pci_unin_internal_init_device); |
380 |
pci_qdev_register(&unin_internal_pci_host_info); |
381 |
} |
382 |
|
383 |
device_init(unin_register_devices) |