pci-spapr.c 6.8 KB
Newer Older
L
Laurent Vivier 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * libqos PCI bindings for SPAPR
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 * See the COPYING file in the top-level directory.
 */

#include "qemu/osdep.h"
#include "libqtest.h"
#include "libqos/pci-spapr.h"
#include "libqos/rtas.h"

#include "hw/pci/pci_regs.h"

#include "qemu-common.h"
#include "qemu/host-utils.h"


/* From include/hw/pci-host/spapr.h */

21 22 23 24
typedef struct QPCIWindow {
    uint64_t pci_base;    /* window address in PCI space */
    uint64_t size;        /* window size */
} QPCIWindow;
L
Laurent Vivier 已提交
25 26 27 28 29

typedef struct QPCIBusSPAPR {
    QPCIBus bus;
    QGuestAllocator *alloc;

30 31 32 33 34
    uint64_t buid;

    uint64_t pio_cpu_base;
    QPCIWindow pio;

35 36
    uint64_t mmio32_cpu_base;
    QPCIWindow mmio32;
L
Laurent Vivier 已提交
37 38 39 40 41 42 43 44
} QPCIBusSPAPR;

/*
 * PCI devices are always little-endian
 * SPAPR by default is big-endian
 * so PCI accessors need to swap data endianness
 */

45
static uint8_t qpci_spapr_pio_readb(QPCIBus *bus, uint32_t addr)
L
Laurent Vivier 已提交
46
{
47
    QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
48
    return qtest_readb(bus->qts, s->pio_cpu_base + addr);
L
Laurent Vivier 已提交
49 50
}

51
static void qpci_spapr_pio_writeb(QPCIBus *bus, uint32_t addr, uint8_t val)
L
Laurent Vivier 已提交
52
{
53
    QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
54
    qtest_writeb(bus->qts, s->pio_cpu_base + addr, val);
L
Laurent Vivier 已提交
55 56
}

57
static uint16_t qpci_spapr_pio_readw(QPCIBus *bus, uint32_t addr)
L
Laurent Vivier 已提交
58
{
59
    QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
60
    return bswap16(qtest_readw(bus->qts, s->pio_cpu_base + addr));
L
Laurent Vivier 已提交
61 62
}

63 64 65
static void qpci_spapr_pio_writew(QPCIBus *bus, uint32_t addr, uint16_t val)
{
    QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
66
    qtest_writew(bus->qts, s->pio_cpu_base + addr, bswap16(val));
67 68 69 70 71
}

static uint32_t qpci_spapr_pio_readl(QPCIBus *bus, uint32_t addr)
{
    QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
72
    return bswap32(qtest_readl(bus->qts, s->pio_cpu_base + addr));
73 74 75 76 77
}

static void qpci_spapr_pio_writel(QPCIBus *bus, uint32_t addr, uint32_t val)
{
    QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
78
    qtest_writel(bus->qts, s->pio_cpu_base + addr, bswap32(val));
79 80
}

D
David Gibson 已提交
81 82 83
static uint64_t qpci_spapr_pio_readq(QPCIBus *bus, uint32_t addr)
{
    QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
84
    return bswap64(qtest_readq(bus->qts, s->pio_cpu_base + addr));
D
David Gibson 已提交
85 86 87 88 89
}

static void qpci_spapr_pio_writeq(QPCIBus *bus, uint32_t addr, uint64_t val)
{
    QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
90
    qtest_writeq(bus->qts, s->pio_cpu_base + addr, bswap64(val));
D
David Gibson 已提交
91 92
}

93 94 95 96
static void qpci_spapr_memread(QPCIBus *bus, uint32_t addr,
                               void *buf, size_t len)
{
    QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
97
    qtest_memread(bus->qts, s->mmio32_cpu_base + addr, buf, len);
98 99 100 101 102 103
}

static void qpci_spapr_memwrite(QPCIBus *bus, uint32_t addr,
                                const void *buf, size_t len)
{
    QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
104
    qtest_memwrite(bus->qts, s->mmio32_cpu_base + addr, buf, len);
105 106
}

L
Laurent Vivier 已提交
107 108 109 110
static uint8_t qpci_spapr_config_readb(QPCIBus *bus, int devfn, uint8_t offset)
{
    QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
    uint32_t config_addr = (devfn << 8) | offset;
111 112
    return qrtas_ibm_read_pci_config(bus->qts, s->alloc, s->buid,
                                     config_addr, 1);
L
Laurent Vivier 已提交
113 114 115 116 117 118
}

static uint16_t qpci_spapr_config_readw(QPCIBus *bus, int devfn, uint8_t offset)
{
    QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
    uint32_t config_addr = (devfn << 8) | offset;
119 120
    return qrtas_ibm_read_pci_config(bus->qts, s->alloc, s->buid,
                                     config_addr, 2);
L
Laurent Vivier 已提交
121 122 123 124 125 126
}

static uint32_t qpci_spapr_config_readl(QPCIBus *bus, int devfn, uint8_t offset)
{
    QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
    uint32_t config_addr = (devfn << 8) | offset;
127 128
    return qrtas_ibm_read_pci_config(bus->qts, s->alloc, s->buid,
                                     config_addr, 4);
L
Laurent Vivier 已提交
129 130 131 132 133 134 135
}

static void qpci_spapr_config_writeb(QPCIBus *bus, int devfn, uint8_t offset,
                                     uint8_t value)
{
    QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
    uint32_t config_addr = (devfn << 8) | offset;
136 137
    qrtas_ibm_write_pci_config(bus->qts, s->alloc, s->buid,
                               config_addr, 1, value);
L
Laurent Vivier 已提交
138 139 140 141 142 143 144
}

static void qpci_spapr_config_writew(QPCIBus *bus, int devfn, uint8_t offset,
                                     uint16_t value)
{
    QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
    uint32_t config_addr = (devfn << 8) | offset;
145 146
    qrtas_ibm_write_pci_config(bus->qts, s->alloc, s->buid,
                               config_addr, 2, value);
L
Laurent Vivier 已提交
147 148 149 150 151 152 153
}

static void qpci_spapr_config_writel(QPCIBus *bus, int devfn, uint8_t offset,
                                     uint32_t value)
{
    QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
    uint32_t config_addr = (devfn << 8) | offset;
154 155
    qrtas_ibm_write_pci_config(bus->qts, s->alloc, s->buid,
                               config_addr, 4, value);
L
Laurent Vivier 已提交
156 157
}

158 159
#define SPAPR_PCI_BASE               (1ULL << 45)

160
#define SPAPR_PCI_MMIO32_WIN_SIZE    0x80000000 /* 2 GiB */
161 162
#define SPAPR_PCI_IO_WIN_SIZE        0x10000

163
QPCIBus *qpci_init_spapr(QTestState *qts, QGuestAllocator *alloc)
L
Laurent Vivier 已提交
164
{
165
    QPCIBusSPAPR *ret = g_new0(QPCIBusSPAPR, 1);
L
Laurent Vivier 已提交
166

167
    assert(qts);
L
Laurent Vivier 已提交
168 169 170

    ret->alloc = alloc;

171 172 173
    ret->bus.pio_readb = qpci_spapr_pio_readb;
    ret->bus.pio_readw = qpci_spapr_pio_readw;
    ret->bus.pio_readl = qpci_spapr_pio_readl;
D
David Gibson 已提交
174
    ret->bus.pio_readq = qpci_spapr_pio_readq;
175 176 177 178

    ret->bus.pio_writeb = qpci_spapr_pio_writeb;
    ret->bus.pio_writew = qpci_spapr_pio_writew;
    ret->bus.pio_writel = qpci_spapr_pio_writel;
D
David Gibson 已提交
179
    ret->bus.pio_writeq = qpci_spapr_pio_writeq;
180

181 182 183
    ret->bus.memread = qpci_spapr_memread;
    ret->bus.memwrite = qpci_spapr_memwrite;

L
Laurent Vivier 已提交
184 185 186 187 188 189 190 191
    ret->bus.config_readb = qpci_spapr_config_readb;
    ret->bus.config_readw = qpci_spapr_config_readw;
    ret->bus.config_readl = qpci_spapr_config_readl;

    ret->bus.config_writeb = qpci_spapr_config_writeb;
    ret->bus.config_writew = qpci_spapr_config_writew;
    ret->bus.config_writel = qpci_spapr_config_writel;

192 193 194 195 196
    /* FIXME: We assume the default location of the PHB for now.
     * Ideally we'd parse the device tree deposited in the guest to
     * get the window locations */
    ret->buid = 0x800000020000000ULL;

197
    ret->pio_cpu_base = SPAPR_PCI_BASE;
198 199 200
    ret->pio.pci_base = 0;
    ret->pio.size = SPAPR_PCI_IO_WIN_SIZE;

201
    /* 32-bit portion of the MMIO window is at PCI address 2..4 GiB */
L
Laurent Vivier 已提交
202 203
    ret->mmio32_cpu_base = SPAPR_PCI_BASE;
    ret->mmio32.pci_base = SPAPR_PCI_MMIO32_WIN_SIZE;
204
    ret->mmio32.size = SPAPR_PCI_MMIO32_WIN_SIZE;
205

206
    ret->bus.qts = qts;
207 208 209
    ret->bus.pio_alloc_ptr = 0xc000;
    ret->bus.mmio_alloc_ptr = ret->mmio32.pci_base;
    ret->bus.mmio_limit = ret->mmio32.pci_base + ret->mmio32.size;
L
Laurent Vivier 已提交
210 211 212 213 214 215 216 217 218 219

    return &ret->bus;
}

void qpci_free_spapr(QPCIBus *bus)
{
    QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);

    g_free(s);
}