prep.c 12.7 KB
Newer Older
P
pbrook 已提交
1 2 3 4
/*
 * QEMU PREP PCI host
 *
 * Copyright (c) 2006 Fabrice Bellard
5
 * Copyright (c) 2011-2013 Andreas Färber
6
 *
P
pbrook 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * 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
Peter Maydell 已提交
26
#include "qemu/osdep.h"
27 28 29 30
#include "hw/hw.h"
#include "hw/pci/pci.h"
#include "hw/pci/pci_bus.h"
#include "hw/pci/pci_host.h"
P
Paolo Bonzini 已提交
31
#include "hw/i386/pc.h"
32
#include "hw/loader.h"
33
#include "exec/address-spaces.h"
34
#include "elf.h"
P
pbrook 已提交
35

36
#define TYPE_RAVEN_PCI_DEVICE "raven"
37 38
#define TYPE_RAVEN_PCI_HOST_BRIDGE "raven-pcihost"

39 40 41 42 43
#define RAVEN_PCI_DEVICE(obj) \
    OBJECT_CHECK(RavenPCIState, (obj), TYPE_RAVEN_PCI_DEVICE)

typedef struct RavenPCIState {
    PCIDevice dev;
44 45 46 47

    uint32_t elf_machine;
    char *bios_name;
    MemoryRegion bios;
48 49
} RavenPCIState;

50 51 52
#define RAVEN_PCI_HOST_BRIDGE(obj) \
    OBJECT_CHECK(PREPPCIState, (obj), TYPE_RAVEN_PCI_HOST_BRIDGE)

53
typedef struct PRePPCIState {
A
Andreas Färber 已提交
54
    PCIHostState parent_obj;
55

56
    qemu_irq irq[PCI_NUM_PINS];
57
    PCIBus pci_bus;
58
    AddressSpace pci_io_as;
59
    MemoryRegion pci_io;
60
    MemoryRegion pci_io_non_contiguous;
61
    MemoryRegion pci_memory;
62
    MemoryRegion pci_intack;
63 64 65 66
    MemoryRegion bm;
    MemoryRegion bm_ram_alias;
    MemoryRegion bm_pci_memory_alias;
    AddressSpace bm_as;
67
    RavenPCIState pci_dev;
68 69

    int contiguous_map;
70
} PREPPCIState;
P
pbrook 已提交
71

72 73
#define BIOS_SIZE (1024 * 1024)

74
static inline uint32_t raven_pci_io_config(hwaddr addr)
P
pbrook 已提交
75 76 77
{
    int i;

78 79
    for (i = 0; i < 11; i++) {
        if ((addr & (1 << (11 + i))) != 0) {
P
pbrook 已提交
80
            break;
81
        }
P
pbrook 已提交
82 83 84 85
    }
    return (addr & 0x7ff) |  (i << 11);
}

86 87
static void raven_pci_io_write(void *opaque, hwaddr addr,
                               uint64_t val, unsigned int size)
P
pbrook 已提交
88 89
{
    PREPPCIState *s = opaque;
A
Andreas Färber 已提交
90
    PCIHostState *phb = PCI_HOST_BRIDGE(s);
91
    pci_data_write(phb->bus, raven_pci_io_config(addr), val, size);
P
pbrook 已提交
92 93
}

94 95
static uint64_t raven_pci_io_read(void *opaque, hwaddr addr,
                                  unsigned int size)
P
pbrook 已提交
96 97
{
    PREPPCIState *s = opaque;
A
Andreas Färber 已提交
98
    PCIHostState *phb = PCI_HOST_BRIDGE(s);
99
    return pci_data_read(phb->bus, raven_pci_io_config(addr), size);
P
pbrook 已提交
100 101
}

102 103 104
static const MemoryRegionOps raven_pci_io_ops = {
    .read = raven_pci_io_read,
    .write = raven_pci_io_write,
105
    .endianness = DEVICE_LITTLE_ENDIAN,
P
pbrook 已提交
106 107
};

108 109
static uint64_t raven_intack_read(void *opaque, hwaddr addr,
                                  unsigned int size)
110 111 112 113
{
    return pic_read_irq(isa_pic);
}

114 115
static const MemoryRegionOps raven_intack_ops = {
    .read = raven_intack_read,
116 117 118 119 120
    .valid = {
        .max_access_size = 1,
    },
};

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
static inline hwaddr raven_io_address(PREPPCIState *s,
                                      hwaddr addr)
{
    if (s->contiguous_map == 0) {
        /* 64 KB contiguous space for IOs */
        addr &= 0xFFFF;
    } else {
        /* 8 MB non-contiguous space for IOs */
        addr = (addr & 0x1F) | ((addr & 0x007FFF000) >> 7);
    }

    /* FIXME: handle endianness switch */

    return addr;
}

static uint64_t raven_io_read(void *opaque, hwaddr addr,
                              unsigned int size)
{
    PREPPCIState *s = opaque;
    uint8_t buf[4];

    addr = raven_io_address(s, addr);
144 145
    address_space_read(&s->pci_io_as, addr + 0x80000000,
                       MEMTXATTRS_UNSPECIFIED, buf, size);
146 147 148 149

    if (size == 1) {
        return buf[0];
    } else if (size == 2) {
150
        return lduw_le_p(buf);
151
    } else if (size == 4) {
152
        return ldl_le_p(buf);
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    } else {
        g_assert_not_reached();
    }
}

static void raven_io_write(void *opaque, hwaddr addr,
                           uint64_t val, unsigned int size)
{
    PREPPCIState *s = opaque;
    uint8_t buf[4];

    addr = raven_io_address(s, addr);

    if (size == 1) {
        buf[0] = val;
    } else if (size == 2) {
169
        stw_le_p(buf, val);
170
    } else if (size == 4) {
171
        stl_le_p(buf, val);
172 173 174 175
    } else {
        g_assert_not_reached();
    }

176 177
    address_space_write(&s->pci_io_as, addr + 0x80000000,
                        MEMTXATTRS_UNSPECIFIED, buf, size);
178 179 180 181 182 183 184 185 186 187
}

static const MemoryRegionOps raven_io_ops = {
    .read = raven_io_read,
    .write = raven_io_write,
    .endianness = DEVICE_LITTLE_ENDIAN,
    .impl.max_access_size = 4,
    .valid.unaligned = true,
};

188
static int raven_map_irq(PCIDevice *pci_dev, int irq_num)
P
pbrook 已提交
189
{
P
pbrook 已提交
190
    return (irq_num + (pci_dev->devfn >> 3)) & 1;
191 192
}

193
static void raven_set_irq(void *opaque, int irq_num, int level)
194
{
195 196
    qemu_irq *pic = opaque;

197
    qemu_set_irq(pic[irq_num] , level);
P
pbrook 已提交
198 199
}

200 201 202 203 204 205 206 207
static AddressSpace *raven_pcihost_set_iommu(PCIBus *bus, void *opaque,
                                             int devfn)
{
    PREPPCIState *s = opaque;

    return &s->bm_as;
}

208 209 210 211 212 213 214
static void raven_change_gpio(void *opaque, int n, int level)
{
    PREPPCIState *s = opaque;

    s->contiguous_map = level;
}

215
static void raven_pcihost_realizefn(DeviceState *d, Error **errp)
P
pbrook 已提交
216
{
217
    SysBusDevice *dev = SYS_BUS_DEVICE(d);
218
    PCIHostState *h = PCI_HOST_BRIDGE(dev);
219
    PREPPCIState *s = RAVEN_PCI_HOST_BRIDGE(dev);
220 221 222
    MemoryRegion *address_space_mem = get_system_memory();
    int i;

223
    for (i = 0; i < PCI_NUM_PINS; i++) {
224 225
        sysbus_init_irq(dev, &s->irq[i]);
    }
P
pbrook 已提交
226

227 228
    qdev_init_gpio_in(d, raven_change_gpio, 1);

229 230
    pci_bus_irqs(&s->pci_bus, raven_set_irq, raven_map_irq, s->irq,
                 PCI_NUM_PINS);
P
pbrook 已提交
231

232 233
    memory_region_init_io(&h->conf_mem, OBJECT(h), &pci_host_conf_le_ops, s,
                          "pci-conf-idx", 4);
234
    memory_region_add_subregion(&s->pci_io, 0xcf8, &h->conf_mem);
235

236 237
    memory_region_init_io(&h->data_mem, OBJECT(h), &pci_host_data_le_ops, s,
                          "pci-conf-data", 4);
238
    memory_region_add_subregion(&s->pci_io, 0xcfc, &h->data_mem);
P
pbrook 已提交
239

240 241
    memory_region_init_io(&h->mmcfg, OBJECT(s), &raven_pci_io_ops, s,
                          "pciio", 0x00400000);
242
    memory_region_add_subregion(address_space_mem, 0x80800000, &h->mmcfg);
P
pbrook 已提交
243

244
    memory_region_init_io(&s->pci_intack, OBJECT(s), &raven_intack_ops, s,
245 246
                          "pci-intack", 1);
    memory_region_add_subregion(address_space_mem, 0xbffffff0, &s->pci_intack);
247

248
    /* TODO Remove once realize propagates to child devices. */
249
    object_property_set_bool(OBJECT(&s->pci_dev), true, "realized", errp);
250 251 252 253 254 255 256 257 258
}

static void raven_pcihost_initfn(Object *obj)
{
    PCIHostState *h = PCI_HOST_BRIDGE(obj);
    PREPPCIState *s = RAVEN_PCI_HOST_BRIDGE(obj);
    MemoryRegion *address_space_mem = get_system_memory();
    DeviceState *pci_dev;

259
    memory_region_init(&s->pci_io, obj, "pci-io", 0x3f800000);
260 261
    memory_region_init_io(&s->pci_io_non_contiguous, obj, &raven_io_ops, s,
                          "pci-io-non-contiguous", 0x00800000);
262
    memory_region_init(&s->pci_memory, obj, "pci-memory", 0x3f000000);
263
    address_space_init(&s->pci_io_as, &s->pci_io, "raven-io");
264 265

    /* CPU address space */
266
    memory_region_add_subregion(address_space_mem, 0x80000000, &s->pci_io);
267 268
    memory_region_add_subregion_overlap(address_space_mem, 0x80000000,
                                        &s->pci_io_non_contiguous, 1);
269
    memory_region_add_subregion(address_space_mem, 0xc0000000, &s->pci_memory);
270
    pci_bus_new_inplace(&s->pci_bus, sizeof(s->pci_bus), DEVICE(obj), NULL,
271
                        &s->pci_memory, &s->pci_io, 0, TYPE_PCI_BUS);
272

273 274 275 276 277 278 279 280 281 282 283 284
    /* Bus master address space */
    memory_region_init(&s->bm, obj, "bm-raven", UINT32_MAX);
    memory_region_init_alias(&s->bm_pci_memory_alias, obj, "bm-pci-memory",
                             &s->pci_memory, 0,
                             memory_region_size(&s->pci_memory));
    memory_region_init_alias(&s->bm_ram_alias, obj, "bm-system",
                             get_system_memory(), 0, 0x80000000);
    memory_region_add_subregion(&s->bm, 0         , &s->bm_pci_memory_alias);
    memory_region_add_subregion(&s->bm, 0x80000000, &s->bm_ram_alias);
    address_space_init(&s->bm_as, &s->bm, "raven-bm");
    pci_setup_iommu(&s->pci_bus, raven_pcihost_set_iommu, s);

285 286
    h->bus = &s->pci_bus;

287
    object_initialize(&s->pci_dev, sizeof(s->pci_dev), TYPE_RAVEN_PCI_DEVICE);
288 289 290 291 292
    pci_dev = DEVICE(&s->pci_dev);
    qdev_set_parent_bus(pci_dev, BUS(&s->pci_bus));
    object_property_set_int(OBJECT(&s->pci_dev), PCI_DEVFN(0, 0), "addr",
                            NULL);
    qdev_prop_set_bit(pci_dev, "multifunction", false);
293 294
}

295
static void raven_realize(PCIDevice *d, Error **errp)
296
{
297 298 299 300
    RavenPCIState *s = RAVEN_PCI_DEVICE(d);
    char *filename;
    int bios_size = -1;

P
pbrook 已提交
301 302 303 304
    d->config[0x0C] = 0x08; // cache_line_size
    d->config[0x0D] = 0x10; // latency_timer
    d->config[0x34] = 0x00; // capabilities_pointer

305
    memory_region_init_ram(&s->bios, OBJECT(s), "bios", BIOS_SIZE,
306
                           &error_fatal);
307 308 309 310 311 312 313 314 315
    memory_region_set_readonly(&s->bios, true);
    memory_region_add_subregion(get_system_memory(), (uint32_t)(-BIOS_SIZE),
                                &s->bios);
    vmstate_register_ram_global(&s->bios);
    if (s->bios_name) {
        filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, s->bios_name);
        if (filename) {
            if (s->elf_machine != EM_NONE) {
                bios_size = load_elf(filename, NULL, NULL, NULL,
316
                                     NULL, NULL, 1, s->elf_machine, 0, 0);
317 318 319 320 321 322 323 324 325 326 327 328 329
            }
            if (bios_size < 0) {
                bios_size = get_image_size(filename);
                if (bios_size > 0 && bios_size <= BIOS_SIZE) {
                    hwaddr bios_addr;
                    bios_size = (bios_size + 0xfff) & ~0xfff;
                    bios_addr = (uint32_t)(-BIOS_SIZE);
                    bios_size = load_image_targphys(filename, bios_addr,
                                                    bios_size);
                }
            }
        }
        if (bios_size < 0 || bios_size > BIOS_SIZE) {
330
            /* FIXME should error_setg() */
331 332
            hw_error("qemu: could not load bios image '%s'\n", s->bios_name);
        }
333
        g_free(filename);
334
    }
P
pbrook 已提交
335
}
336 337 338 339 340 341 342 343 344 345 346

static const VMStateDescription vmstate_raven = {
    .name = "raven",
    .version_id = 0,
    .minimum_version_id = 0,
    .fields = (VMStateField[]) {
        VMSTATE_PCI_DEVICE(dev, RavenPCIState),
        VMSTATE_END_OF_LIST()
    },
};

347 348 349
static void raven_class_init(ObjectClass *klass, void *data)
{
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
350
    DeviceClass *dc = DEVICE_CLASS(klass);
351

352
    k->realize = raven_realize;
353 354 355 356
    k->vendor_id = PCI_VENDOR_ID_MOTOROLA;
    k->device_id = PCI_DEVICE_ID_MOTOROLA_RAVEN;
    k->revision = 0x00;
    k->class_id = PCI_CLASS_BRIDGE_HOST;
357 358
    dc->desc = "PReP Host Bridge - Motorola Raven";
    dc->vmsd = &vmstate_raven;
359
    /*
360 361 362
     * Reason: PCI-facing part of the host bridge, not usable without
     * the host-facing part, which can't be device_add'ed, yet.
     * Reason: realize() method uses hw_error().
363 364
     */
    dc->cannot_instantiate_with_device_add_yet = true;
365 366
}

367
static const TypeInfo raven_info = {
368
    .name = TYPE_RAVEN_PCI_DEVICE,
369 370
    .parent = TYPE_PCI_DEVICE,
    .instance_size = sizeof(RavenPCIState),
371
    .class_init = raven_class_init,
372 373
};

374 375 376 377 378 379 380
static Property raven_pcihost_properties[] = {
    DEFINE_PROP_UINT32("elf-machine", PREPPCIState, pci_dev.elf_machine,
                       EM_NONE),
    DEFINE_PROP_STRING("bios-name", PREPPCIState, pci_dev.bios_name),
    DEFINE_PROP_END_OF_LIST()
};

381 382
static void raven_pcihost_class_init(ObjectClass *klass, void *data)
{
383
    DeviceClass *dc = DEVICE_CLASS(klass);
384

385
    set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
386
    dc->realize = raven_pcihost_realizefn;
387
    dc->props = raven_pcihost_properties;
388
    dc->fw_name = "pci";
389 390
}

391
static const TypeInfo raven_pcihost_info = {
392
    .name = TYPE_RAVEN_PCI_HOST_BRIDGE,
393
    .parent = TYPE_PCI_HOST_BRIDGE,
394
    .instance_size = sizeof(PREPPCIState),
395
    .instance_init = raven_pcihost_initfn,
396
    .class_init = raven_pcihost_class_init,
397 398
};

A
Andreas Färber 已提交
399
static void raven_register_types(void)
400
{
401 402
    type_register_static(&raven_pcihost_info);
    type_register_static(&raven_info);
403 404
}

A
Andreas Färber 已提交
405
type_init(raven_register_types)