apb_pci.c 10.7 KB
Newer Older
P
pbrook 已提交
1 2 3 4
/*
 * QEMU Ultrasparc APB PCI host
 *
 * 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

B
Fix APB  
blueswir1 已提交
25
/* XXX This file and most of its contents are somewhat misnamed.  The
P
pbrook 已提交
26 27 28
   Ultrasparc PCI host is called the PCI Bus Module (PBM).  The APB is
   the secondary PCI bridge.  */

B
Blue Swirl 已提交
29
#include "sysbus.h"
P
pbrook 已提交
30
#include "pci.h"
31
#include "pci_host.h"
32
#include "apb_pci.h"
B
Fix APB  
blueswir1 已提交
33 34 35 36 37

/* debug APB */
//#define DEBUG_APB

#ifdef DEBUG_APB
38 39
#define APB_DPRINTF(fmt, ...) \
do { printf("APB: " fmt , ## __VA_ARGS__); } while (0)
B
Fix APB  
blueswir1 已提交
40
#else
41
#define APB_DPRINTF(fmt, ...)
B
Fix APB  
blueswir1 已提交
42 43
#endif

B
Blue Swirl 已提交
44 45 46 47 48 49 50 51 52
/*
 * Chipset docs:
 * PBM: "UltraSPARC IIi User's Manual",
 * http://www.sun.com/processors/manuals/805-0087.pdf
 *
 * APB: "Advanced PCI Bridge (APB) User's Manual",
 * http://www.sun.com/processors/manuals/805-1251.pdf
 */

B
Blue Swirl 已提交
53 54 55 56
typedef struct APBState {
    SysBusDevice busdev;
    PCIHostState host_state;
} APBState;
P
pbrook 已提交
57

A
Anthony Liguori 已提交
58
static void apb_config_writel (void *opaque, target_phys_addr_t addr,
B
blueswir1 已提交
59
                               uint32_t val)
P
pbrook 已提交
60 61 62 63 64 65 66 67 68
{
    //PCIBus *s = opaque;

    switch (addr & 0x3f) {
    case 0x00: // Control/Status
    case 0x10: // AFSR
    case 0x18: // AFAR
    case 0x20: // Diagnostic
    case 0x28: // Target address space
B
blueswir1 已提交
69
        // XXX
P
pbrook 已提交
70
    default:
B
blueswir1 已提交
71
        break;
P
pbrook 已提交
72 73 74 75
    }
}

static uint32_t apb_config_readl (void *opaque,
A
Anthony Liguori 已提交
76
                                  target_phys_addr_t addr)
P
pbrook 已提交
77 78 79 80 81 82 83 84 85 86
{
    //PCIBus *s = opaque;
    uint32_t val;

    switch (addr & 0x3f) {
    case 0x00: // Control/Status
    case 0x10: // AFSR
    case 0x18: // AFAR
    case 0x20: // Diagnostic
    case 0x28: // Target address space
B
blueswir1 已提交
87
        // XXX
P
pbrook 已提交
88
    default:
B
blueswir1 已提交
89 90
        val = 0;
        break;
P
pbrook 已提交
91 92 93 94
    }
    return val;
}

95
static CPUWriteMemoryFunc * const apb_config_write[] = {
P
pbrook 已提交
96 97 98 99 100
    &apb_config_writel,
    &apb_config_writel,
    &apb_config_writel,
};

101
static CPUReadMemoryFunc * const apb_config_read[] = {
P
pbrook 已提交
102 103 104 105 106
    &apb_config_readl,
    &apb_config_readl,
    &apb_config_readl,
};

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
static void apb_pci_config_write(APBState *s, target_phys_addr_t addr,
                                 uint32_t val, int size)
{
    APB_DPRINTF("%s: addr " TARGET_FMT_lx " val %x\n", __func__, addr, val);
    pci_data_write(s->host_state.bus, (addr & 0x00ffffff) | (1u << 31), val,
                   size);
}

static uint32_t apb_pci_config_read(APBState *s, target_phys_addr_t addr,
                                    int size)
{
    uint32_t ret;

    ret = pci_data_read(s->host_state.bus, (addr & 0x00ffffff) | (1u << 31),
                        size);
    APB_DPRINTF("%s: addr " TARGET_FMT_lx " -> %x\n", __func__, addr, ret);
    return ret;
}

static void apb_pci_config_writel(void *opaque, target_phys_addr_t addr,
                                  uint32_t val)
{
    APBState *s = opaque;

B
Blue Swirl 已提交
131
    apb_pci_config_write(s, addr, bswap32(val), 4);
132 133 134 135 136 137 138
}

static void apb_pci_config_writew(void *opaque, target_phys_addr_t addr,
                                  uint32_t val)
{
    APBState *s = opaque;

B
Blue Swirl 已提交
139
    apb_pci_config_write(s, addr, bswap16(val), 2);
140 141 142 143 144 145 146 147 148 149 150 151 152 153
}

static void apb_pci_config_writeb(void *opaque, target_phys_addr_t addr,
                                  uint32_t val)
{
    APBState *s = opaque;

    apb_pci_config_write(s, addr, val, 1);
}

static uint32_t apb_pci_config_readl(void *opaque, target_phys_addr_t addr)
{
    APBState *s = opaque;

B
Blue Swirl 已提交
154
    return bswap32(apb_pci_config_read(s, addr, 4));
155 156 157 158 159 160
}

static uint32_t apb_pci_config_readw(void *opaque, target_phys_addr_t addr)
{
    APBState *s = opaque;

B
Blue Swirl 已提交
161
    return bswap16(apb_pci_config_read(s, addr, 2));
162 163 164 165 166 167 168 169 170 171 172
}

static uint32_t apb_pci_config_readb(void *opaque, target_phys_addr_t addr)
{
    APBState *s = opaque;

    return apb_pci_config_read(s, addr, 1);
}

static CPUWriteMemoryFunc * const apb_pci_config_writes[] = {
    &apb_pci_config_writeb,
173 174
    &apb_pci_config_writew,
    &apb_pci_config_writel,
175 176 177 178
};

static CPUReadMemoryFunc * const apb_pci_config_reads[] = {
    &apb_pci_config_readb,
179 180
    &apb_pci_config_readw,
    &apb_pci_config_readl,
181 182
};

A
Anthony Liguori 已提交
183
static void pci_apb_iowriteb (void *opaque, target_phys_addr_t addr,
P
pbrook 已提交
184 185
                                  uint32_t val)
{
186
    cpu_outb(addr & IOPORTS_MASK, val);
P
pbrook 已提交
187 188
}

A
Anthony Liguori 已提交
189
static void pci_apb_iowritew (void *opaque, target_phys_addr_t addr,
P
pbrook 已提交
190 191
                                  uint32_t val)
{
B
Blue Swirl 已提交
192
    cpu_outw(addr & IOPORTS_MASK, bswap16(val));
P
pbrook 已提交
193 194
}

A
Anthony Liguori 已提交
195
static void pci_apb_iowritel (void *opaque, target_phys_addr_t addr,
P
pbrook 已提交
196 197
                                uint32_t val)
{
B
Blue Swirl 已提交
198
    cpu_outl(addr & IOPORTS_MASK, bswap32(val));
P
pbrook 已提交
199 200
}

A
Anthony Liguori 已提交
201
static uint32_t pci_apb_ioreadb (void *opaque, target_phys_addr_t addr)
P
pbrook 已提交
202 203 204
{
    uint32_t val;

205
    val = cpu_inb(addr & IOPORTS_MASK);
P
pbrook 已提交
206 207 208
    return val;
}

A
Anthony Liguori 已提交
209
static uint32_t pci_apb_ioreadw (void *opaque, target_phys_addr_t addr)
P
pbrook 已提交
210 211 212
{
    uint32_t val;

B
Blue Swirl 已提交
213
    val = bswap16(cpu_inw(addr & IOPORTS_MASK));
P
pbrook 已提交
214 215 216
    return val;
}

A
Anthony Liguori 已提交
217
static uint32_t pci_apb_ioreadl (void *opaque, target_phys_addr_t addr)
P
pbrook 已提交
218 219 220
{
    uint32_t val;

B
Blue Swirl 已提交
221
    val = bswap32(cpu_inl(addr & IOPORTS_MASK));
P
pbrook 已提交
222 223 224
    return val;
}

225
static CPUWriteMemoryFunc * const pci_apb_iowrite[] = {
P
pbrook 已提交
226 227 228 229 230
    &pci_apb_iowriteb,
    &pci_apb_iowritew,
    &pci_apb_iowritel,
};

231
static CPUReadMemoryFunc * const pci_apb_ioread[] = {
P
pbrook 已提交
232 233 234 235 236
    &pci_apb_ioreadb,
    &pci_apb_ioreadw,
    &pci_apb_ioreadl,
};

P
pbrook 已提交
237
/* The APB host has an IRQ line for each IRQ line of each slot.  */
238
static int pci_apb_map_irq(PCIDevice *pci_dev, int irq_num)
P
pbrook 已提交
239
{
P
pbrook 已提交
240 241 242 243 244 245 246 247 248 249 250
    return ((pci_dev->devfn & 0x18) >> 1) + irq_num;
}

static int pci_pbm_map_irq(PCIDevice *pci_dev, int irq_num)
{
    int bus_offset;
    if (pci_dev->devfn & 1)
        bus_offset = 16;
    else
        bus_offset = 0;
    return bus_offset + irq_num;
251 252
}

253
static void pci_apb_set_irq(void *opaque, int irq_num, int level)
254
{
255 256
    qemu_irq *pic = opaque;

P
pbrook 已提交
257
    /* PCI IRQ map onto the first 32 INO.  */
P
pbrook 已提交
258
    qemu_set_irq(pic[irq_num], level);
P
pbrook 已提交
259 260
}

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
static void apb_pci_bridge_init(PCIBus *b)
{
    PCIDevice *dev = pci_bridge_get_device(b);

    /*
     * command register:
     * According to PCI bridge spec, after reset
     *   bus master bit is off
     *   memory space enable bit is off
     * According to manual (805-1251.pdf).
     *   the reset value should be zero unless the boot pin is tied high
     *   (which is true) and thus it should be PCI_COMMAND_MEMORY.
     */
    pci_set_word(dev->config + PCI_COMMAND,
                 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
    dev->config[PCI_LATENCY_TIMER] = 0x10;
    dev->config[PCI_HEADER_TYPE] |= PCI_HEADER_TYPE_MULTI_FUNCTION;
}

A
Anthony Liguori 已提交
280 281
PCIBus *pci_apb_init(target_phys_addr_t special_base,
                     target_phys_addr_t mem_base,
B
blueswir1 已提交
282
                     qemu_irq *pic, PCIBus **bus2, PCIBus **bus3)
P
pbrook 已提交
283
{
B
Blue Swirl 已提交
284 285 286
    DeviceState *dev;
    SysBusDevice *s;
    APBState *d;
P
pbrook 已提交
287

P
pbrook 已提交
288
    /* Ultrasparc PBM main bus */
B
Blue Swirl 已提交
289
    dev = qdev_create(NULL, "pbm");
M
Markus Armbruster 已提交
290
    qdev_init_nofail(dev);
B
Blue Swirl 已提交
291 292
    s = sysbus_from_qdev(dev);
    /* apb_config */
293
    sysbus_mmio_map(s, 0, special_base);
B
Blue Swirl 已提交
294 295
    /* pci_ioport */
    sysbus_mmio_map(s, 1, special_base + 0x2000000ULL);
B
Blue Swirl 已提交
296
    /* pci_config */
B
Blue Swirl 已提交
297 298
    sysbus_mmio_map(s, 2, special_base + 0x1000000ULL);
    /* mem_data */
B
Blue Swirl 已提交
299
    sysbus_mmio_map(s, 3, mem_base);
B
Blue Swirl 已提交
300
    d = FROM_SYSBUS(APBState, s);
301
    d->host_state.bus = pci_register_bus(&d->busdev.qdev, "pci",
B
Blue Swirl 已提交
302 303
                                         pci_apb_set_irq, pci_pbm_map_irq, pic,
                                         0, 32);
304 305
    pci_bus_set_mem_base(d->host_state.bus, mem_base);

B
Blue Swirl 已提交
306 307
    pci_create_simple(d->host_state.bus, 0, "pbm");
    /* APB secondary busses */
308 309 310
    *bus2 = pci_bridge_init(d->host_state.bus, PCI_DEVFN(1, 0),
                            PCI_VENDOR_ID_SUN, PCI_DEVICE_ID_SUN_SIMBA,
                            pci_apb_map_irq,
B
Blue Swirl 已提交
311
                            "Advanced PCI Bus secondary bridge 1");
312 313
    apb_pci_bridge_init(*bus2);

314 315 316
    *bus3 = pci_bridge_init(d->host_state.bus, PCI_DEVFN(1, 1),
                            PCI_VENDOR_ID_SUN, PCI_DEVICE_ID_SUN_SIMBA,
                            pci_apb_map_irq,
B
Blue Swirl 已提交
317
                            "Advanced PCI Bus secondary bridge 2");
318
    apb_pci_bridge_init(*bus3);
P
pbrook 已提交
319

B
Blue Swirl 已提交
320 321 322
    return d->host_state.bus;
}

323
static int pci_pbm_init_device(SysBusDevice *dev)
B
Blue Swirl 已提交
324 325 326
{

    APBState *s;
B
Blue Swirl 已提交
327
    int pci_mem_data, apb_config, pci_ioport, pci_config;
B
Blue Swirl 已提交
328 329 330

    s = FROM_SYSBUS(APBState, dev);
    /* apb_config */
331
    apb_config = cpu_register_io_memory(apb_config_read,
B
blueswir1 已提交
332
                                        apb_config_write, s);
333
    sysbus_init_mmio(dev, 0x10000ULL, apb_config);
B
Blue Swirl 已提交
334
    /* pci_ioport */
335
    pci_ioport = cpu_register_io_memory(pci_apb_ioread,
P
pbrook 已提交
336
                                          pci_apb_iowrite, s);
B
Blue Swirl 已提交
337
    sysbus_init_mmio(dev, 0x10000ULL, pci_ioport);
338 339 340 341
    /* pci_config */
    pci_config = cpu_register_io_memory(apb_pci_config_reads,
                                        apb_pci_config_writes, s);
    sysbus_init_mmio(dev, 0x1000000ULL, pci_config);
B
Blue Swirl 已提交
342
    /* mem_data */
343
    pci_mem_data = pci_host_data_register_mmio(&s->host_state);
B
Blue Swirl 已提交
344
    sysbus_init_mmio(dev, 0x10000000ULL, pci_mem_data);
345
    return 0;
B
Blue Swirl 已提交
346
}
P
pbrook 已提交
347

348
static int pbm_pci_host_init(PCIDevice *d)
B
Blue Swirl 已提交
349
{
350 351
    pci_config_set_vendor_id(d->config, PCI_VENDOR_ID_SUN);
    pci_config_set_device_id(d->config, PCI_DEVICE_ID_SUN_SABRE);
P
pbrook 已提交
352 353 354 355 356 357
    d->config[0x04] = 0x06; // command = bus master, pci mem
    d->config[0x05] = 0x00;
    d->config[0x06] = 0xa0; // status = fast back-to-back, 66MHz, no error
    d->config[0x07] = 0x03; // status = medium devsel
    d->config[0x08] = 0x00; // revision
    d->config[0x09] = 0x00; // programming i/f
358
    pci_config_set_class(d->config, PCI_CLASS_BRIDGE_HOST);
P
pbrook 已提交
359
    d->config[0x0D] = 0x10; // latency_timer
360
    d->config[PCI_HEADER_TYPE] = PCI_HEADER_TYPE_NORMAL; // header_type
361
    return 0;
B
Blue Swirl 已提交
362
}
P
pbrook 已提交
363

B
Blue Swirl 已提交
364 365 366 367
static PCIDeviceInfo pbm_pci_host_info = {
    .qdev.name = "pbm",
    .qdev.size = sizeof(PCIDevice),
    .init      = pbm_pci_host_init,
B
Blue Swirl 已提交
368
    .header_type  = PCI_HEADER_TYPE_BRIDGE,
B
Blue Swirl 已提交
369 370 371 372 373 374
};

static void pbm_register_devices(void)
{
    sysbus_register_dev("pbm", sizeof(APBState), pci_pbm_init_device);
    pci_qdev_register(&pbm_pci_host_info);
P
pbrook 已提交
375
}
B
Blue Swirl 已提交
376 377

device_init(pbm_register_devices)