unin_pci.c 14.6 KB
Newer Older
P
pbrook 已提交
1 2 3 4
/*
 * QEMU Uninorth PCI host (for all Mac99 and newer machines)
 *
 * Copyright (c) 2006 Fabrice Bellard
5
 *
P
pbrook 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
P
pbrook 已提交
24
#include "hw.h"
25
#include "ppc/mac.h"
26 27
#include "pci/pci.h"
#include "pci/pci_host.h"
P
pbrook 已提交
28

29 30 31 32
/* debug UniNorth */
//#define DEBUG_UNIN

#ifdef DEBUG_UNIN
33 34
#define UNIN_DPRINTF(fmt, ...)                                  \
    do { printf("UNIN: " fmt , ## __VA_ARGS__); } while (0)
35
#else
36
#define UNIN_DPRINTF(fmt, ...)
37 38
#endif

A
Alexander Graf 已提交
39 40
static const int unin_irq_line[] = { 0x1b, 0x1c, 0x1d, 0x1e };

41 42 43 44 45 46 47 48 49 50 51 52 53 54
#define TYPE_UNI_NORTH_PCI_HOST_BRIDGE "uni-north-pci-pcihost"
#define TYPE_UNI_NORTH_AGP_HOST_BRIDGE "uni-north-agp-pcihost"
#define TYPE_UNI_NORTH_INTERNAL_PCI_HOST_BRIDGE "uni-north-internal-pci-pcihost"
#define TYPE_U3_AGP_HOST_BRIDGE "u3-agp-pcihost"

#define UNI_NORTH_PCI_HOST_BRIDGE(obj) \
    OBJECT_CHECK(UNINState, (obj), TYPE_UNI_NORTH_PCI_HOST_BRIDGE)
#define UNI_NORTH_AGP_HOST_BRIDGE(obj) \
    OBJECT_CHECK(UNINState, (obj), TYPE_UNI_NORTH_AGP_HOST_BRIDGE)
#define UNI_NORTH_INTERNAL_PCI_HOST_BRIDGE(obj) \
    OBJECT_CHECK(UNINState, (obj), TYPE_UNI_NORTH_INTERNAL_PCI_HOST_BRIDGE)
#define U3_AGP_HOST_BRIDGE(obj) \
    OBJECT_CHECK(UNINState, (obj), TYPE_U3_AGP_HOST_BRIDGE)

55
typedef struct UNINState {
A
Andreas Färber 已提交
56
    PCIHostState parent_obj;
57

58 59
    MemoryRegion pci_mmio;
    MemoryRegion pci_hole;
60
} UNINState;
P
pbrook 已提交
61

62
static int pci_unin_map_irq(PCIDevice *pci_dev, int irq_num)
P
pbrook 已提交
63
{
A
Alexander Graf 已提交
64 65 66 67 68 69
    int retval;
    int devfn = pci_dev->devfn & 0x00FFFFFF;

    retval = (((devfn >> 11) & 0x1F) + irq_num) & 3;

    return retval;
70 71
}

72
static void pci_unin_set_irq(void *opaque, int irq_num, int level)
73
{
74 75
    qemu_irq *pic = opaque;

A
Alexander Graf 已提交
76 77 78
    UNIN_DPRINTF("%s: setting INT %d = %d\n", __func__,
                 unin_irq_line[irq_num], level);
    qemu_set_irq(pic[unin_irq_line[irq_num]], level);
P
pbrook 已提交
79 80
}

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
static uint32_t unin_get_config_reg(uint32_t reg, uint32_t addr)
{
    uint32_t retval;

    if (reg & (1u << 31)) {
        /* XXX OpenBIOS compatibility hack */
        retval = reg | (addr & 3);
    } else if (reg & 1) {
        /* CFA1 style */
        retval = (reg & ~7u) | (addr & 7);
    } else {
        uint32_t slot, func;

        /* Grab CFA0 style values */
        slot = ffs(reg & 0xfffff800) - 1;
        func = (reg >> 8) & 7;

        /* ... and then convert them to x86 format */
        /* config pointer */
        retval = (reg & (0xff - 7)) | (addr & 7);
        /* slot */
        retval |= slot << 11;
        /* fn */
        retval |= func << 8;
    }


    UNIN_DPRINTF("Converted config space accessor %08x/%08x -> %08x\n",
                 reg, addr, retval);

    return retval;
}

A
Avi Kivity 已提交
114
static void unin_data_write(void *opaque, hwaddr addr,
115
                            uint64_t val, unsigned len)
116
{
117
    UNINState *s = opaque;
A
Andreas Färber 已提交
118
    PCIHostState *phb = PCI_HOST_BRIDGE(s);
119 120
    UNIN_DPRINTF("write addr %" TARGET_FMT_plx " len %d val %"PRIx64"\n",
                 addr, len, val);
A
Andreas Färber 已提交
121 122
    pci_data_write(phb->bus,
                   unin_get_config_reg(phb->config_reg, addr),
123 124 125
                   val, len);
}

A
Avi Kivity 已提交
126
static uint64_t unin_data_read(void *opaque, hwaddr addr,
127
                               unsigned len)
128
{
129
    UNINState *s = opaque;
A
Andreas Färber 已提交
130
    PCIHostState *phb = PCI_HOST_BRIDGE(s);
131 132
    uint32_t val;

A
Andreas Färber 已提交
133 134
    val = pci_data_read(phb->bus,
                        unin_get_config_reg(phb->config_reg, addr),
135
                        len);
136 137
    UNIN_DPRINTF("read addr %" TARGET_FMT_plx " len %d val %x\n",
                 addr, len, val);
138 139 140
    return val;
}

141 142 143 144 145 146
static const MemoryRegionOps unin_data_ops = {
    .read = unin_data_read,
    .write = unin_data_write,
    .endianness = DEVICE_LITTLE_ENDIAN,
};

147
static int pci_unin_main_init_device(SysBusDevice *dev)
P
pbrook 已提交
148
{
149
    PCIHostState *h;
P
pbrook 已提交
150 151 152

    /* Use values found on a real PowerMac */
    /* Uninorth main bus */
153
    h = PCI_HOST_BRIDGE(dev);
P
pbrook 已提交
154

155 156 157
    memory_region_init_io(&h->conf_mem, &pci_host_conf_le_ops,
                          dev, "pci-conf-idx", 0x1000);
    memory_region_init_io(&h->data_mem, &unin_data_ops, dev,
158
                          "pci-conf-data", 0x1000);
159 160
    sysbus_init_mmio(dev, &h->conf_mem);
    sysbus_init_mmio(dev, &h->data_mem);
161

162
    return 0;
163 164
}

165

A
Alexander Graf 已提交
166 167
static int pci_u3_agp_init_device(SysBusDevice *dev)
{
168
    PCIHostState *h;
A
Alexander Graf 已提交
169 170

    /* Uninorth U3 AGP bus */
171
    h = PCI_HOST_BRIDGE(dev);
A
Alexander Graf 已提交
172

173 174 175
    memory_region_init_io(&h->conf_mem, &pci_host_conf_le_ops,
                          dev, "pci-conf-idx", 0x1000);
    memory_region_init_io(&h->data_mem, &unin_data_ops, dev,
176
                          "pci-conf-data", 0x1000);
177 178
    sysbus_init_mmio(dev, &h->conf_mem);
    sysbus_init_mmio(dev, &h->data_mem);
A
Alexander Graf 已提交
179 180 181 182

    return 0;
}

183
static int pci_unin_agp_init_device(SysBusDevice *dev)
184
{
185
    PCIHostState *h;
186 187

    /* Uninorth AGP bus */
188
    h = PCI_HOST_BRIDGE(dev);
189 190 191 192 193 194 195

    memory_region_init_io(&h->conf_mem, &pci_host_conf_le_ops,
                          dev, "pci-conf-idx", 0x1000);
    memory_region_init_io(&h->data_mem, &pci_host_data_le_ops,
                          dev, "pci-conf-data", 0x1000);
    sysbus_init_mmio(dev, &h->conf_mem);
    sysbus_init_mmio(dev, &h->data_mem);
196
    return 0;
197 198
}

199
static int pci_unin_internal_init_device(SysBusDevice *dev)
200
{
201
    PCIHostState *h;
202 203

    /* Uninorth internal bus */
204
    h = PCI_HOST_BRIDGE(dev);
205 206 207 208 209 210 211

    memory_region_init_io(&h->conf_mem, &pci_host_conf_le_ops,
                          dev, "pci-conf-idx", 0x1000);
    memory_region_init_io(&h->data_mem, &pci_host_data_le_ops,
                          dev, "pci-conf-data", 0x1000);
    sysbus_init_mmio(dev, &h->conf_mem);
    sysbus_init_mmio(dev, &h->data_mem);
212
    return 0;
213 214
}

215 216 217
PCIBus *pci_pmac_init(qemu_irq *pic,
                      MemoryRegion *address_space_mem,
                      MemoryRegion *address_space_io)
218 219 220
{
    DeviceState *dev;
    SysBusDevice *s;
221
    PCIHostState *h;
222 223 224 225
    UNINState *d;

    /* Use values found on a real PowerMac */
    /* Uninorth main bus */
226
    dev = qdev_create(NULL, TYPE_UNI_NORTH_PCI_HOST_BRIDGE);
M
Markus Armbruster 已提交
227
    qdev_init_nofail(dev);
228
    s = SYS_BUS_DEVICE(dev);
229
    h = PCI_HOST_BRIDGE(s);
230
    d = UNI_NORTH_PCI_HOST_BRIDGE(dev);
231 232 233 234 235 236
    memory_region_init(&d->pci_mmio, "pci-mmio", 0x100000000ULL);
    memory_region_init_alias(&d->pci_hole, "pci-hole", &d->pci_mmio,
                             0x80000000ULL, 0x70000000ULL);
    memory_region_add_subregion(address_space_mem, 0x80000000ULL,
                                &d->pci_hole);

237 238 239 240 241 242
    h->bus = pci_register_bus(dev, "pci",
                              pci_unin_set_irq, pci_unin_map_irq,
                              pic,
                              &d->pci_mmio,
                              address_space_io,
                              PCI_DEVFN(11, 0), 4);
243

244
#if 0
245
    pci_create_simple(h->bus, PCI_DEVFN(11, 0), "uni-north");
246
#endif
247 248 249 250 251 252 253

    sysbus_mmio_map(s, 0, 0xf2800000);
    sysbus_mmio_map(s, 1, 0xf2c00000);

    /* DEC 21154 bridge */
#if 0
    /* XXX: not activated as PPC BIOS doesn't handle multiple buses properly */
254
    pci_create_simple(h->bus, PCI_DEVFN(12, 0), "dec-21154");
255 256 257
#endif

    /* Uninorth AGP bus */
258 259
    pci_create_simple(h->bus, PCI_DEVFN(11, 0), "uni-north-agp");
    dev = qdev_create(NULL, TYPE_UNI_NORTH_AGP_HOST_BRIDGE);
260
    qdev_init_nofail(dev);
261
    s = SYS_BUS_DEVICE(dev);
262 263
    sysbus_mmio_map(s, 0, 0xf0800000);
    sysbus_mmio_map(s, 1, 0xf0c00000);
264 265 266 267

    /* Uninorth internal bus */
#if 0
    /* XXX: not needed for now */
268
    pci_create_simple(h->bus, PCI_DEVFN(14, 0),
A
Andreas Färber 已提交
269
                      "uni-north-internal-pci");
270
    dev = qdev_create(NULL, TYPE_UNI_NORTH_INTERNAL_PCI_HOST_BRIDGE);
271
    qdev_init_nofail(dev);
272
    s = SYS_BUS_DEVICE(dev);
273 274
    sysbus_mmio_map(s, 0, 0xf4800000);
    sysbus_mmio_map(s, 1, 0xf4c00000);
275 276
#endif

277
    return h->bus;
278 279
}

280 281 282
PCIBus *pci_pmac_u3_init(qemu_irq *pic,
                         MemoryRegion *address_space_mem,
                         MemoryRegion *address_space_io)
A
Alexander Graf 已提交
283 284 285
{
    DeviceState *dev;
    SysBusDevice *s;
286
    PCIHostState *h;
A
Alexander Graf 已提交
287 288 289 290
    UNINState *d;

    /* Uninorth AGP bus */

291
    dev = qdev_create(NULL, TYPE_U3_AGP_HOST_BRIDGE);
A
Alexander Graf 已提交
292
    qdev_init_nofail(dev);
293
    s = SYS_BUS_DEVICE(dev);
294
    h = PCI_HOST_BRIDGE(dev);
295
    d = U3_AGP_HOST_BRIDGE(dev);
A
Alexander Graf 已提交
296

297 298 299 300 301 302
    memory_region_init(&d->pci_mmio, "pci-mmio", 0x100000000ULL);
    memory_region_init_alias(&d->pci_hole, "pci-hole", &d->pci_mmio,
                             0x80000000ULL, 0x70000000ULL);
    memory_region_add_subregion(address_space_mem, 0x80000000ULL,
                                &d->pci_hole);

303 304 305 306 307 308
    h->bus = pci_register_bus(dev, "pci",
                              pci_unin_set_irq, pci_unin_map_irq,
                              pic,
                              &d->pci_mmio,
                              address_space_io,
                              PCI_DEVFN(11, 0), 4);
A
Alexander Graf 已提交
309 310 311 312

    sysbus_mmio_map(s, 0, 0xf0800000);
    sysbus_mmio_map(s, 1, 0xf0c00000);

313
    pci_create_simple(h->bus, 11 << 3, "u3-agp");
A
Alexander Graf 已提交
314

315
    return h->bus;
A
Alexander Graf 已提交
316 317
}

318
static int unin_main_pci_host_init(PCIDevice *d)
319
{
P
pbrook 已提交
320 321 322
    d->config[0x0C] = 0x08; // cache_line_size
    d->config[0x0D] = 0x10; // latency_timer
    d->config[0x34] = 0x00; // capabilities_pointer
323
    return 0;
324
}
P
pbrook 已提交
325

326
static int unin_agp_pci_host_init(PCIDevice *d)
327
{
P
pbrook 已提交
328 329 330
    d->config[0x0C] = 0x08; // cache_line_size
    d->config[0x0D] = 0x10; // latency_timer
    //    d->config[0x34] = 0x80; // capabilities_pointer
331
    return 0;
332
}
P
pbrook 已提交
333

A
Alexander Graf 已提交
334 335 336 337 338 339 340 341 342
static int u3_agp_pci_host_init(PCIDevice *d)
{
    /* cache line size */
    d->config[0x0C] = 0x08;
    /* latency timer */
    d->config[0x0D] = 0x10;
    return 0;
}

343
static int unin_internal_pci_host_init(PCIDevice *d)
344
{
P
pbrook 已提交
345 346 347
    d->config[0x0C] = 0x08; // cache_line_size
    d->config[0x0D] = 0x10; // latency_timer
    d->config[0x34] = 0x00; // capabilities_pointer
348
    return 0;
349 350
}

351 352 353 354 355 356 357 358 359 360 361
static void unin_main_pci_host_class_init(ObjectClass *klass, void *data)
{
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);

    k->init      = unin_main_pci_host_init;
    k->vendor_id = PCI_VENDOR_ID_APPLE;
    k->device_id = PCI_DEVICE_ID_APPLE_UNI_N_PCI;
    k->revision  = 0x00;
    k->class_id  = PCI_CLASS_BRIDGE_HOST;
}

362
static const TypeInfo unin_main_pci_host_info = {
363
    .name = "uni-north-pci",
364 365
    .parent = TYPE_PCI_DEVICE,
    .instance_size = sizeof(PCIDevice),
366
    .class_init = unin_main_pci_host_class_init,
367 368
};

369 370 371 372 373 374 375 376 377 378 379
static void u3_agp_pci_host_class_init(ObjectClass *klass, void *data)
{
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);

    k->init      = u3_agp_pci_host_init;
    k->vendor_id = PCI_VENDOR_ID_APPLE;
    k->device_id = PCI_DEVICE_ID_APPLE_U3_AGP;
    k->revision  = 0x00;
    k->class_id  = PCI_CLASS_BRIDGE_HOST;
}

380
static const TypeInfo u3_agp_pci_host_info = {
381
    .name = "u3-agp",
382 383
    .parent = TYPE_PCI_DEVICE,
    .instance_size = sizeof(PCIDevice),
384
    .class_init = u3_agp_pci_host_class_init,
A
Alexander Graf 已提交
385 386
};

387 388 389 390 391 392 393 394 395 396 397
static void unin_agp_pci_host_class_init(ObjectClass *klass, void *data)
{
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);

    k->init      = unin_agp_pci_host_init;
    k->vendor_id = PCI_VENDOR_ID_APPLE;
    k->device_id = PCI_DEVICE_ID_APPLE_UNI_N_AGP;
    k->revision  = 0x00;
    k->class_id  = PCI_CLASS_BRIDGE_HOST;
}

398
static const TypeInfo unin_agp_pci_host_info = {
399
    .name = "uni-north-agp",
400 401
    .parent = TYPE_PCI_DEVICE,
    .instance_size = sizeof(PCIDevice),
402
    .class_init = unin_agp_pci_host_class_init,
403 404
};

405 406 407 408 409 410 411 412 413 414 415
static void unin_internal_pci_host_class_init(ObjectClass *klass, void *data)
{
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);

    k->init      = unin_internal_pci_host_init;
    k->vendor_id = PCI_VENDOR_ID_APPLE;
    k->device_id = PCI_DEVICE_ID_APPLE_UNI_N_I_PCI;
    k->revision  = 0x00;
    k->class_id  = PCI_CLASS_BRIDGE_HOST;
}

416
static const TypeInfo unin_internal_pci_host_info = {
417
    .name = "uni-north-internal-pci",
418 419
    .parent = TYPE_PCI_DEVICE,
    .instance_size = sizeof(PCIDevice),
420
    .class_init = unin_internal_pci_host_class_init,
421 422
};

423 424 425 426 427 428 429
static void pci_unin_main_class_init(ObjectClass *klass, void *data)
{
    SysBusDeviceClass *sbc = SYS_BUS_DEVICE_CLASS(klass);

    sbc->init = pci_unin_main_init_device;
}

430
static const TypeInfo pci_unin_main_info = {
431
    .name          = TYPE_UNI_NORTH_PCI_HOST_BRIDGE,
432
    .parent        = TYPE_PCI_HOST_BRIDGE,
433 434
    .instance_size = sizeof(UNINState),
    .class_init    = pci_unin_main_class_init,
A
Andreas Färber 已提交
435 436
};

437 438 439 440 441 442 443
static void pci_u3_agp_class_init(ObjectClass *klass, void *data)
{
    SysBusDeviceClass *sbc = SYS_BUS_DEVICE_CLASS(klass);

    sbc->init = pci_u3_agp_init_device;
}

444
static const TypeInfo pci_u3_agp_info = {
445
    .name          = TYPE_U3_AGP_HOST_BRIDGE,
446
    .parent        = TYPE_PCI_HOST_BRIDGE,
447 448
    .instance_size = sizeof(UNINState),
    .class_init    = pci_u3_agp_class_init,
A
Andreas Färber 已提交
449 450
};

451 452 453 454 455 456 457
static void pci_unin_agp_class_init(ObjectClass *klass, void *data)
{
    SysBusDeviceClass *sbc = SYS_BUS_DEVICE_CLASS(klass);

    sbc->init = pci_unin_agp_init_device;
}

458
static const TypeInfo pci_unin_agp_info = {
459
    .name          = TYPE_UNI_NORTH_AGP_HOST_BRIDGE,
460
    .parent        = TYPE_PCI_HOST_BRIDGE,
461 462
    .instance_size = sizeof(UNINState),
    .class_init    = pci_unin_agp_class_init,
A
Andreas Färber 已提交
463 464
};

465 466 467 468 469 470 471
static void pci_unin_internal_class_init(ObjectClass *klass, void *data)
{
    SysBusDeviceClass *sbc = SYS_BUS_DEVICE_CLASS(klass);

    sbc->init = pci_unin_internal_init_device;
}

472
static const TypeInfo pci_unin_internal_info = {
473
    .name          = TYPE_UNI_NORTH_INTERNAL_PCI_HOST_BRIDGE,
474
    .parent        = TYPE_PCI_HOST_BRIDGE,
475 476
    .instance_size = sizeof(UNINState),
    .class_init    = pci_unin_internal_class_init,
A
Andreas Färber 已提交
477 478
};

A
Andreas Färber 已提交
479
static void unin_register_types(void)
480
{
481 482 483 484 485 486 487 488 489
    type_register_static(&unin_main_pci_host_info);
    type_register_static(&u3_agp_pci_host_info);
    type_register_static(&unin_agp_pci_host_info);
    type_register_static(&unin_internal_pci_host_info);

    type_register_static(&pci_unin_main_info);
    type_register_static(&pci_u3_agp_info);
    type_register_static(&pci_unin_agp_info);
    type_register_static(&pci_unin_internal_info);
P
pbrook 已提交
490
}
491

A
Andreas Färber 已提交
492
type_init(unin_register_types)