2016-09-29 12:32:44 +02:00
|
|
|
/*
|
|
|
|
* 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 */
|
|
|
|
|
2016-10-12 04:30:07 +02:00
|
|
|
typedef struct QPCIWindow {
|
|
|
|
uint64_t pci_base; /* window address in PCI space */
|
|
|
|
uint64_t size; /* window size */
|
|
|
|
} QPCIWindow;
|
2016-09-29 12:32:44 +02:00
|
|
|
|
|
|
|
typedef struct QPCIBusSPAPR {
|
|
|
|
QPCIBus bus;
|
|
|
|
QGuestAllocator *alloc;
|
|
|
|
|
2016-10-12 04:30:07 +02:00
|
|
|
uint64_t buid;
|
|
|
|
|
|
|
|
uint64_t pio_cpu_base;
|
|
|
|
QPCIWindow pio;
|
|
|
|
|
2016-10-12 05:07:04 +02:00
|
|
|
uint64_t mmio32_cpu_base;
|
|
|
|
QPCIWindow mmio32;
|
2016-09-29 12:32:44 +02:00
|
|
|
} QPCIBusSPAPR;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* PCI devices are always little-endian
|
|
|
|
* SPAPR by default is big-endian
|
|
|
|
* so PCI accessors need to swap data endianness
|
|
|
|
*/
|
|
|
|
|
2016-10-18 08:02:49 +02:00
|
|
|
static uint8_t qpci_spapr_pio_readb(QPCIBus *bus, uint32_t addr)
|
2016-09-29 12:32:44 +02:00
|
|
|
{
|
2016-10-12 04:30:07 +02:00
|
|
|
QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
|
2016-10-18 08:02:49 +02:00
|
|
|
return readb(s->pio_cpu_base + addr);
|
2016-09-29 12:32:44 +02:00
|
|
|
}
|
|
|
|
|
2016-10-18 08:02:49 +02:00
|
|
|
static void qpci_spapr_pio_writeb(QPCIBus *bus, uint32_t addr, uint8_t val)
|
2016-09-29 12:32:44 +02:00
|
|
|
{
|
2016-10-12 04:30:07 +02:00
|
|
|
QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
|
2016-10-18 08:02:49 +02:00
|
|
|
writeb(s->pio_cpu_base + addr, val);
|
2016-09-29 12:32:44 +02:00
|
|
|
}
|
|
|
|
|
2016-10-18 08:02:49 +02:00
|
|
|
static uint16_t qpci_spapr_pio_readw(QPCIBus *bus, uint32_t addr)
|
2016-09-29 12:32:44 +02:00
|
|
|
{
|
2016-10-12 04:30:07 +02:00
|
|
|
QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
|
2016-10-18 08:02:49 +02:00
|
|
|
return bswap16(readw(s->pio_cpu_base + addr));
|
2016-09-29 12:32:44 +02:00
|
|
|
}
|
|
|
|
|
2016-10-18 08:02:49 +02:00
|
|
|
static void qpci_spapr_pio_writew(QPCIBus *bus, uint32_t addr, uint16_t val)
|
|
|
|
{
|
|
|
|
QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
|
|
|
|
writew(s->pio_cpu_base + addr, bswap16(val));
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t qpci_spapr_pio_readl(QPCIBus *bus, uint32_t addr)
|
|
|
|
{
|
|
|
|
QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
|
|
|
|
return bswap32(readl(s->pio_cpu_base + addr));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qpci_spapr_pio_writel(QPCIBus *bus, uint32_t addr, uint32_t val)
|
|
|
|
{
|
|
|
|
QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
|
|
|
|
writel(s->pio_cpu_base + addr, bswap32(val));
|
|
|
|
}
|
|
|
|
|
2016-10-19 06:00:21 +02:00
|
|
|
static uint64_t qpci_spapr_pio_readq(QPCIBus *bus, uint32_t addr)
|
|
|
|
{
|
|
|
|
QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
|
|
|
|
return bswap64(readq(s->pio_cpu_base + addr));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qpci_spapr_pio_writeq(QPCIBus *bus, uint32_t addr, uint64_t val)
|
|
|
|
{
|
|
|
|
QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
|
|
|
|
writeq(s->pio_cpu_base + addr, bswap64(val));
|
|
|
|
}
|
|
|
|
|
2016-10-19 05:19:47 +02:00
|
|
|
static void qpci_spapr_memread(QPCIBus *bus, uint32_t addr,
|
|
|
|
void *buf, size_t len)
|
|
|
|
{
|
|
|
|
QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
|
|
|
|
memread(s->mmio32_cpu_base + addr, buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qpci_spapr_memwrite(QPCIBus *bus, uint32_t addr,
|
|
|
|
const void *buf, size_t len)
|
|
|
|
{
|
|
|
|
QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
|
|
|
|
memwrite(s->mmio32_cpu_base + addr, buf, len);
|
|
|
|
}
|
|
|
|
|
2016-09-29 12:32:44 +02:00
|
|
|
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;
|
2016-10-12 04:30:07 +02:00
|
|
|
return qrtas_ibm_read_pci_config(s->alloc, s->buid, config_addr, 1);
|
2016-09-29 12:32:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2016-10-12 04:30:07 +02:00
|
|
|
return qrtas_ibm_read_pci_config(s->alloc, s->buid, config_addr, 2);
|
2016-09-29 12:32:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2016-10-12 04:30:07 +02:00
|
|
|
return qrtas_ibm_read_pci_config(s->alloc, s->buid, config_addr, 4);
|
2016-09-29 12:32:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2016-10-12 04:30:07 +02:00
|
|
|
qrtas_ibm_write_pci_config(s->alloc, s->buid, config_addr, 1, value);
|
2016-09-29 12:32:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2016-10-12 04:30:07 +02:00
|
|
|
qrtas_ibm_write_pci_config(s->alloc, s->buid, config_addr, 2, value);
|
2016-09-29 12:32:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2016-10-12 04:30:07 +02:00
|
|
|
qrtas_ibm_write_pci_config(s->alloc, s->buid, config_addr, 4, value);
|
2016-09-29 12:32:44 +02:00
|
|
|
}
|
|
|
|
|
2016-10-16 03:04:15 +02:00
|
|
|
#define SPAPR_PCI_BASE (1ULL << 45)
|
|
|
|
|
2016-10-12 05:07:04 +02:00
|
|
|
#define SPAPR_PCI_MMIO32_WIN_SIZE 0x80000000 /* 2 GiB */
|
2016-10-12 04:30:07 +02:00
|
|
|
#define SPAPR_PCI_IO_WIN_SIZE 0x10000
|
|
|
|
|
2017-09-11 19:19:52 +02:00
|
|
|
QPCIBus *qpci_init_spapr(QTestState *qts, QGuestAllocator *alloc)
|
2016-09-29 12:32:44 +02:00
|
|
|
{
|
2017-09-11 19:19:52 +02:00
|
|
|
QPCIBusSPAPR *ret = g_new0(QPCIBusSPAPR, 1);
|
2016-09-29 12:32:44 +02:00
|
|
|
|
2017-09-11 19:19:52 +02:00
|
|
|
assert(qts);
|
2016-09-29 12:32:44 +02:00
|
|
|
|
|
|
|
ret->alloc = alloc;
|
|
|
|
|
2016-10-18 08:02:49 +02:00
|
|
|
ret->bus.pio_readb = qpci_spapr_pio_readb;
|
|
|
|
ret->bus.pio_readw = qpci_spapr_pio_readw;
|
|
|
|
ret->bus.pio_readl = qpci_spapr_pio_readl;
|
2016-10-19 06:00:21 +02:00
|
|
|
ret->bus.pio_readq = qpci_spapr_pio_readq;
|
2016-10-18 08:02:49 +02:00
|
|
|
|
|
|
|
ret->bus.pio_writeb = qpci_spapr_pio_writeb;
|
|
|
|
ret->bus.pio_writew = qpci_spapr_pio_writew;
|
|
|
|
ret->bus.pio_writel = qpci_spapr_pio_writel;
|
2016-10-19 06:00:21 +02:00
|
|
|
ret->bus.pio_writeq = qpci_spapr_pio_writeq;
|
2016-10-18 08:02:49 +02:00
|
|
|
|
2016-10-19 05:19:47 +02:00
|
|
|
ret->bus.memread = qpci_spapr_memread;
|
|
|
|
ret->bus.memwrite = qpci_spapr_memwrite;
|
|
|
|
|
2016-09-29 12:32:44 +02:00
|
|
|
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;
|
|
|
|
|
2016-10-12 04:30:07 +02:00
|
|
|
/* 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;
|
|
|
|
|
2016-10-16 03:04:15 +02:00
|
|
|
ret->pio_cpu_base = SPAPR_PCI_BASE;
|
2016-10-12 04:30:07 +02:00
|
|
|
ret->pio.pci_base = 0;
|
|
|
|
ret->pio.size = SPAPR_PCI_IO_WIN_SIZE;
|
|
|
|
|
2016-10-12 05:07:04 +02:00
|
|
|
/* 32-bit portion of the MMIO window is at PCI address 2..4 GiB */
|
2017-01-05 16:29:46 +01:00
|
|
|
ret->mmio32_cpu_base = SPAPR_PCI_BASE;
|
|
|
|
ret->mmio32.pci_base = SPAPR_PCI_MMIO32_WIN_SIZE;
|
2016-10-12 05:07:04 +02:00
|
|
|
ret->mmio32.size = SPAPR_PCI_MMIO32_WIN_SIZE;
|
2016-10-12 04:30:07 +02:00
|
|
|
|
2017-09-11 19:19:52 +02:00
|
|
|
ret->bus.qts = qts;
|
2016-10-19 05:06:51 +02:00
|
|
|
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;
|
2016-09-29 12:32:44 +02:00
|
|
|
|
|
|
|
return &ret->bus;
|
|
|
|
}
|
|
|
|
|
|
|
|
void qpci_free_spapr(QPCIBus *bus)
|
|
|
|
{
|
|
|
|
QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
|
|
|
|
|
|
|
|
g_free(s);
|
|
|
|
}
|