2016-09-29 13:32:44 +03: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"
|
2022-03-30 12:39:05 +03:00
|
|
|
#include "../libqtest.h"
|
2020-08-04 21:00:40 +03:00
|
|
|
#include "pci-spapr.h"
|
|
|
|
#include "rtas.h"
|
|
|
|
#include "qgraph.h"
|
2016-09-29 13:32:44 +03:00
|
|
|
|
|
|
|
#include "hw/pci/pci_regs.h"
|
|
|
|
|
|
|
|
#include "qemu/host-utils.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2016-09-29 13:32:44 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* PCI devices are always little-endian
|
|
|
|
* SPAPR by default is big-endian
|
|
|
|
* so PCI accessors need to swap data endianness
|
|
|
|
*/
|
|
|
|
|
2016-10-18 09:02:49 +03:00
|
|
|
static uint8_t qpci_spapr_pio_readb(QPCIBus *bus, uint32_t addr)
|
2016-09-29 13:32:44 +03:00
|
|
|
{
|
2016-10-12 05:30:07 +03:00
|
|
|
QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
|
2018-11-06 22:15:38 +03:00
|
|
|
return qtest_readb(bus->qts, s->pio_cpu_base + addr);
|
2016-09-29 13:32:44 +03:00
|
|
|
}
|
|
|
|
|
2016-10-18 09:02:49 +03:00
|
|
|
static void qpci_spapr_pio_writeb(QPCIBus *bus, uint32_t addr, uint8_t val)
|
2016-09-29 13:32:44 +03:00
|
|
|
{
|
2016-10-12 05:30:07 +03:00
|
|
|
QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
|
2018-11-06 22:15:38 +03:00
|
|
|
qtest_writeb(bus->qts, s->pio_cpu_base + addr, val);
|
2016-09-29 13:32:44 +03:00
|
|
|
}
|
|
|
|
|
2016-10-18 09:02:49 +03:00
|
|
|
static uint16_t qpci_spapr_pio_readw(QPCIBus *bus, uint32_t addr)
|
2016-09-29 13:32:44 +03:00
|
|
|
{
|
2016-10-12 05:30:07 +03:00
|
|
|
QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
|
2018-11-06 22:15:38 +03:00
|
|
|
return bswap16(qtest_readw(bus->qts, s->pio_cpu_base + addr));
|
2016-09-29 13:32:44 +03:00
|
|
|
}
|
|
|
|
|
2016-10-18 09:02:49 +03:00
|
|
|
static void qpci_spapr_pio_writew(QPCIBus *bus, uint32_t addr, uint16_t val)
|
|
|
|
{
|
|
|
|
QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
|
2018-11-06 22:15:38 +03:00
|
|
|
qtest_writew(bus->qts, s->pio_cpu_base + addr, bswap16(val));
|
2016-10-18 09:02:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t qpci_spapr_pio_readl(QPCIBus *bus, uint32_t addr)
|
|
|
|
{
|
|
|
|
QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
|
2018-11-06 22:15:38 +03:00
|
|
|
return bswap32(qtest_readl(bus->qts, s->pio_cpu_base + addr));
|
2016-10-18 09:02:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void qpci_spapr_pio_writel(QPCIBus *bus, uint32_t addr, uint32_t val)
|
|
|
|
{
|
|
|
|
QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
|
2018-11-06 22:15:38 +03:00
|
|
|
qtest_writel(bus->qts, s->pio_cpu_base + addr, bswap32(val));
|
2016-10-18 09:02:49 +03:00
|
|
|
}
|
|
|
|
|
2016-10-19 07:00:21 +03:00
|
|
|
static uint64_t qpci_spapr_pio_readq(QPCIBus *bus, uint32_t addr)
|
|
|
|
{
|
|
|
|
QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
|
2018-11-06 22:15:38 +03:00
|
|
|
return bswap64(qtest_readq(bus->qts, s->pio_cpu_base + addr));
|
2016-10-19 07:00:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void qpci_spapr_pio_writeq(QPCIBus *bus, uint32_t addr, uint64_t val)
|
|
|
|
{
|
|
|
|
QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
|
2018-11-06 22:15:38 +03:00
|
|
|
qtest_writeq(bus->qts, s->pio_cpu_base + addr, bswap64(val));
|
2016-10-19 07:00:21 +03:00
|
|
|
}
|
|
|
|
|
2016-10-19 06:19:47 +03:00
|
|
|
static void qpci_spapr_memread(QPCIBus *bus, uint32_t addr,
|
|
|
|
void *buf, size_t len)
|
|
|
|
{
|
|
|
|
QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
|
2018-11-06 22:15:38 +03:00
|
|
|
qtest_memread(bus->qts, s->mmio32_cpu_base + addr, buf, len);
|
2016-10-19 06:19:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void qpci_spapr_memwrite(QPCIBus *bus, uint32_t addr,
|
|
|
|
const void *buf, size_t len)
|
|
|
|
{
|
|
|
|
QPCIBusSPAPR *s = container_of(bus, QPCIBusSPAPR, bus);
|
2018-11-06 22:15:38 +03:00
|
|
|
qtest_memwrite(bus->qts, s->mmio32_cpu_base + addr, buf, len);
|
2016-10-19 06:19:47 +03:00
|
|
|
}
|
|
|
|
|
2016-09-29 13:32:44 +03: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;
|
2017-09-11 20:19:58 +03:00
|
|
|
return qrtas_ibm_read_pci_config(bus->qts, s->alloc, s->buid,
|
|
|
|
config_addr, 1);
|
2016-09-29 13:32:44 +03: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;
|
2017-09-11 20:19:58 +03:00
|
|
|
return qrtas_ibm_read_pci_config(bus->qts, s->alloc, s->buid,
|
|
|
|
config_addr, 2);
|
2016-09-29 13:32:44 +03: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;
|
2017-09-11 20:19:58 +03:00
|
|
|
return qrtas_ibm_read_pci_config(bus->qts, s->alloc, s->buid,
|
|
|
|
config_addr, 4);
|
2016-09-29 13:32:44 +03: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;
|
2017-09-11 20:19:58 +03:00
|
|
|
qrtas_ibm_write_pci_config(bus->qts, s->alloc, s->buid,
|
|
|
|
config_addr, 1, value);
|
2016-09-29 13:32:44 +03: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;
|
2017-09-11 20:19:58 +03:00
|
|
|
qrtas_ibm_write_pci_config(bus->qts, s->alloc, s->buid,
|
|
|
|
config_addr, 2, value);
|
2016-09-29 13:32:44 +03: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;
|
2017-09-11 20:19:58 +03:00
|
|
|
qrtas_ibm_write_pci_config(bus->qts, s->alloc, s->buid,
|
|
|
|
config_addr, 4, value);
|
2016-09-29 13:32:44 +03:00
|
|
|
}
|
|
|
|
|
2016-10-16 04:04:15 +03:00
|
|
|
#define SPAPR_PCI_BASE (1ULL << 45)
|
|
|
|
|
2016-10-12 06:07:04 +03:00
|
|
|
#define SPAPR_PCI_MMIO32_WIN_SIZE 0x80000000 /* 2 GiB */
|
2016-10-12 05:30:07 +03:00
|
|
|
#define SPAPR_PCI_IO_WIN_SIZE 0x10000
|
|
|
|
|
2018-07-10 17:09:26 +03:00
|
|
|
static void *qpci_spapr_get_driver(void *obj, const char *interface)
|
2016-09-29 13:32:44 +03:00
|
|
|
{
|
2018-07-10 17:09:26 +03:00
|
|
|
QPCIBusSPAPR *qpci = obj;
|
|
|
|
if (!g_strcmp0(interface, "pci-bus")) {
|
|
|
|
return &qpci->bus;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "%s not present in pci-bus-spapr", interface);
|
|
|
|
g_assert_not_reached();
|
|
|
|
}
|
2016-09-29 13:32:44 +03:00
|
|
|
|
2018-07-10 17:09:26 +03:00
|
|
|
void qpci_init_spapr(QPCIBusSPAPR *qpci, QTestState *qts,
|
|
|
|
QGuestAllocator *alloc)
|
|
|
|
{
|
2017-09-11 20:19:52 +03:00
|
|
|
assert(qts);
|
2016-09-29 13:32:44 +03:00
|
|
|
|
2018-08-09 13:44:56 +03:00
|
|
|
/* tests cannot use spapr, needs to be fixed first */
|
2019-03-23 17:26:36 +03:00
|
|
|
qpci->bus.has_buggy_msi = true;
|
2018-08-09 13:44:56 +03:00
|
|
|
|
2018-07-10 17:09:26 +03:00
|
|
|
qpci->alloc = alloc;
|
2016-09-29 13:32:44 +03:00
|
|
|
|
2018-07-10 17:09:26 +03:00
|
|
|
qpci->bus.pio_readb = qpci_spapr_pio_readb;
|
|
|
|
qpci->bus.pio_readw = qpci_spapr_pio_readw;
|
|
|
|
qpci->bus.pio_readl = qpci_spapr_pio_readl;
|
|
|
|
qpci->bus.pio_readq = qpci_spapr_pio_readq;
|
2016-10-18 09:02:49 +03:00
|
|
|
|
2018-07-10 17:09:26 +03:00
|
|
|
qpci->bus.pio_writeb = qpci_spapr_pio_writeb;
|
|
|
|
qpci->bus.pio_writew = qpci_spapr_pio_writew;
|
|
|
|
qpci->bus.pio_writel = qpci_spapr_pio_writel;
|
|
|
|
qpci->bus.pio_writeq = qpci_spapr_pio_writeq;
|
2016-10-18 09:02:49 +03:00
|
|
|
|
2018-07-10 17:09:26 +03:00
|
|
|
qpci->bus.memread = qpci_spapr_memread;
|
|
|
|
qpci->bus.memwrite = qpci_spapr_memwrite;
|
2016-10-19 06:19:47 +03:00
|
|
|
|
2018-07-10 17:09:26 +03:00
|
|
|
qpci->bus.config_readb = qpci_spapr_config_readb;
|
|
|
|
qpci->bus.config_readw = qpci_spapr_config_readw;
|
|
|
|
qpci->bus.config_readl = qpci_spapr_config_readl;
|
2016-09-29 13:32:44 +03:00
|
|
|
|
2018-07-10 17:09:26 +03:00
|
|
|
qpci->bus.config_writeb = qpci_spapr_config_writeb;
|
|
|
|
qpci->bus.config_writew = qpci_spapr_config_writew;
|
|
|
|
qpci->bus.config_writel = qpci_spapr_config_writel;
|
2016-09-29 13:32:44 +03:00
|
|
|
|
2016-10-12 05:30:07 +03: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 */
|
2018-07-10 17:09:26 +03:00
|
|
|
qpci->buid = 0x800000020000000ULL;
|
2016-10-12 05:30:07 +03:00
|
|
|
|
2018-07-10 17:09:26 +03:00
|
|
|
qpci->pio_cpu_base = SPAPR_PCI_BASE;
|
|
|
|
qpci->pio.pci_base = 0;
|
|
|
|
qpci->pio.size = SPAPR_PCI_IO_WIN_SIZE;
|
2016-10-12 05:30:07 +03:00
|
|
|
|
2016-10-12 06:07:04 +03:00
|
|
|
/* 32-bit portion of the MMIO window is at PCI address 2..4 GiB */
|
2018-07-10 17:09:26 +03:00
|
|
|
qpci->mmio32_cpu_base = SPAPR_PCI_BASE;
|
|
|
|
qpci->mmio32.pci_base = SPAPR_PCI_MMIO32_WIN_SIZE;
|
|
|
|
qpci->mmio32.size = SPAPR_PCI_MMIO32_WIN_SIZE;
|
2016-10-12 05:30:07 +03:00
|
|
|
|
2018-07-10 17:09:26 +03:00
|
|
|
qpci->bus.qts = qts;
|
|
|
|
qpci->bus.pio_alloc_ptr = 0xc000;
|
2022-05-04 18:20:23 +03:00
|
|
|
qpci->bus.pio_limit = 0x10000;
|
2018-07-10 17:09:26 +03:00
|
|
|
qpci->bus.mmio_alloc_ptr = qpci->mmio32.pci_base;
|
|
|
|
qpci->bus.mmio_limit = qpci->mmio32.pci_base + qpci->mmio32.size;
|
2016-09-29 13:32:44 +03:00
|
|
|
|
2018-07-10 17:09:26 +03:00
|
|
|
qpci->obj.get_driver = qpci_spapr_get_driver;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPCIBus *qpci_new_spapr(QTestState *qts, QGuestAllocator *alloc)
|
|
|
|
{
|
|
|
|
QPCIBusSPAPR *qpci = g_new0(QPCIBusSPAPR, 1);
|
|
|
|
qpci_init_spapr(qpci, qts, alloc);
|
|
|
|
|
|
|
|
return &qpci->bus;
|
2016-09-29 13:32:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void qpci_free_spapr(QPCIBus *bus)
|
|
|
|
{
|
2018-07-10 17:09:26 +03:00
|
|
|
QPCIBusSPAPR *s;
|
|
|
|
|
|
|
|
if (!bus) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
s = container_of(bus, QPCIBusSPAPR, bus);
|
2016-09-29 13:32:44 +03:00
|
|
|
|
|
|
|
g_free(s);
|
|
|
|
}
|
2018-07-10 17:09:26 +03:00
|
|
|
|
|
|
|
static void qpci_spapr_register_nodes(void)
|
|
|
|
{
|
|
|
|
qos_node_create_driver("pci-bus-spapr", NULL);
|
|
|
|
qos_node_produces("pci-bus-spapr", "pci-bus");
|
|
|
|
}
|
|
|
|
|
|
|
|
libqos_init(qpci_spapr_register_nodes);
|