2013-04-16 16:45:16 +02:00
|
|
|
/*
|
|
|
|
* libqos PCI bindings for PC
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2012-2013
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*/
|
|
|
|
|
2016-02-08 19:08:51 +01:00
|
|
|
#include "qemu/osdep.h"
|
2013-04-16 16:45:16 +02:00
|
|
|
#include "libqtest.h"
|
|
|
|
#include "libqos/pci-pc.h"
|
2018-02-01 12:18:39 +01:00
|
|
|
#include "qapi/qmp/qdict.h"
|
2013-04-16 16:45:16 +02:00
|
|
|
#include "hw/pci/pci_regs.h"
|
|
|
|
|
|
|
|
#include "qemu-common.h"
|
|
|
|
|
2014-09-26 11:28:08 +02:00
|
|
|
#define ACPI_PCIHP_ADDR 0xae00
|
|
|
|
#define PCI_EJ_BASE 0x0008
|
|
|
|
|
2016-10-18 08:02:49 +02:00
|
|
|
static uint8_t qpci_pc_pio_readb(QPCIBus *bus, uint32_t addr)
|
2013-04-16 16:45:16 +02:00
|
|
|
{
|
2018-11-06 20:15:38 +01:00
|
|
|
return qtest_inb(bus->qts, addr);
|
2013-04-16 16:45:16 +02:00
|
|
|
}
|
|
|
|
|
2016-10-18 08:02:49 +02:00
|
|
|
static void qpci_pc_pio_writeb(QPCIBus *bus, uint32_t addr, uint8_t val)
|
|
|
|
{
|
2018-11-06 20:15:38 +01:00
|
|
|
qtest_outb(bus->qts, addr, val);
|
2013-04-16 16:45:16 +02:00
|
|
|
}
|
|
|
|
|
2016-10-18 08:02:49 +02:00
|
|
|
static uint16_t qpci_pc_pio_readw(QPCIBus *bus, uint32_t addr)
|
|
|
|
{
|
2018-11-06 20:15:38 +01:00
|
|
|
return qtest_inw(bus->qts, addr);
|
2016-10-18 08:02:49 +02:00
|
|
|
}
|
2013-04-16 16:45:16 +02:00
|
|
|
|
2016-10-18 08:02:49 +02:00
|
|
|
static void qpci_pc_pio_writew(QPCIBus *bus, uint32_t addr, uint16_t val)
|
2013-04-16 16:45:16 +02:00
|
|
|
{
|
2018-11-06 20:15:38 +01:00
|
|
|
qtest_outw(bus->qts, addr, val);
|
2016-10-18 08:02:49 +02:00
|
|
|
}
|
2013-04-16 16:45:16 +02:00
|
|
|
|
2016-10-18 08:02:49 +02:00
|
|
|
static uint32_t qpci_pc_pio_readl(QPCIBus *bus, uint32_t addr)
|
2013-04-16 16:45:16 +02:00
|
|
|
{
|
2018-11-06 20:15:38 +01:00
|
|
|
return qtest_inl(bus->qts, addr);
|
2016-10-18 08:02:49 +02:00
|
|
|
}
|
2013-04-16 16:45:16 +02:00
|
|
|
|
2016-10-18 08:02:49 +02:00
|
|
|
static void qpci_pc_pio_writel(QPCIBus *bus, uint32_t addr, uint32_t val)
|
2013-04-16 16:45:16 +02:00
|
|
|
{
|
2018-11-06 20:15:38 +01:00
|
|
|
qtest_outl(bus->qts, addr, val);
|
2016-10-18 08:02:49 +02:00
|
|
|
}
|
2013-04-16 16:45:16 +02:00
|
|
|
|
2016-10-19 06:00:21 +02:00
|
|
|
static uint64_t qpci_pc_pio_readq(QPCIBus *bus, uint32_t addr)
|
|
|
|
{
|
2018-11-06 20:15:38 +01:00
|
|
|
return (uint64_t)qtest_inl(bus->qts, addr) +
|
|
|
|
((uint64_t)qtest_inl(bus->qts, addr + 4) << 32);
|
2016-10-19 06:00:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void qpci_pc_pio_writeq(QPCIBus *bus, uint32_t addr, uint64_t val)
|
|
|
|
{
|
2018-11-06 20:15:38 +01:00
|
|
|
qtest_outl(bus->qts, addr, val & 0xffffffff);
|
|
|
|
qtest_outl(bus->qts, addr + 4, val >> 32);
|
2016-10-19 06:00:21 +02:00
|
|
|
}
|
|
|
|
|
2016-10-19 05:19:47 +02:00
|
|
|
static void qpci_pc_memread(QPCIBus *bus, uint32_t addr, void *buf, size_t len)
|
|
|
|
{
|
2018-11-06 20:15:38 +01:00
|
|
|
qtest_memread(bus->qts, addr, buf, len);
|
2016-10-19 05:19:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void qpci_pc_memwrite(QPCIBus *bus, uint32_t addr,
|
|
|
|
const void *buf, size_t len)
|
|
|
|
{
|
2018-11-06 20:15:38 +01:00
|
|
|
qtest_memwrite(bus->qts, addr, buf, len);
|
2016-10-19 05:19:47 +02:00
|
|
|
}
|
|
|
|
|
2013-04-16 16:45:16 +02:00
|
|
|
static uint8_t qpci_pc_config_readb(QPCIBus *bus, int devfn, uint8_t offset)
|
|
|
|
{
|
2018-11-06 20:15:38 +01:00
|
|
|
qtest_outl(bus->qts, 0xcf8, (1U << 31) | (devfn << 8) | offset);
|
|
|
|
return qtest_inb(bus->qts, 0xcfc);
|
2013-04-16 16:45:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t qpci_pc_config_readw(QPCIBus *bus, int devfn, uint8_t offset)
|
|
|
|
{
|
2018-11-06 20:15:38 +01:00
|
|
|
qtest_outl(bus->qts, 0xcf8, (1U << 31) | (devfn << 8) | offset);
|
|
|
|
return qtest_inw(bus->qts, 0xcfc);
|
2013-04-16 16:45:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t qpci_pc_config_readl(QPCIBus *bus, int devfn, uint8_t offset)
|
|
|
|
{
|
2018-11-06 20:15:38 +01:00
|
|
|
qtest_outl(bus->qts, 0xcf8, (1U << 31) | (devfn << 8) | offset);
|
|
|
|
return qtest_inl(bus->qts, 0xcfc);
|
2013-04-16 16:45:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void qpci_pc_config_writeb(QPCIBus *bus, int devfn, uint8_t offset, uint8_t value)
|
|
|
|
{
|
2018-11-06 20:15:38 +01:00
|
|
|
qtest_outl(bus->qts, 0xcf8, (1U << 31) | (devfn << 8) | offset);
|
|
|
|
qtest_outb(bus->qts, 0xcfc, value);
|
2013-04-16 16:45:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void qpci_pc_config_writew(QPCIBus *bus, int devfn, uint8_t offset, uint16_t value)
|
|
|
|
{
|
2018-11-06 20:15:38 +01:00
|
|
|
qtest_outl(bus->qts, 0xcf8, (1U << 31) | (devfn << 8) | offset);
|
|
|
|
qtest_outw(bus->qts, 0xcfc, value);
|
2013-04-16 16:45:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void qpci_pc_config_writel(QPCIBus *bus, int devfn, uint8_t offset, uint32_t value)
|
|
|
|
{
|
2018-11-06 20:15:38 +01:00
|
|
|
qtest_outl(bus->qts, 0xcf8, (1U << 31) | (devfn << 8) | offset);
|
|
|
|
qtest_outl(bus->qts, 0xcfc, value);
|
2013-04-16 16:45:16 +02:00
|
|
|
}
|
|
|
|
|
2018-07-03 16:53:10 +02:00
|
|
|
static void *qpci_pc_get_driver(void *obj, const char *interface)
|
2013-04-16 16:45:16 +02:00
|
|
|
{
|
2018-07-03 16:53:10 +02:00
|
|
|
QPCIBusPC *qpci = obj;
|
|
|
|
if (!g_strcmp0(interface, "pci-bus")) {
|
|
|
|
return &qpci->bus;
|
|
|
|
}
|
|
|
|
fprintf(stderr, "%s not present in pci-bus-pc\n", interface);
|
|
|
|
g_assert_not_reached();
|
|
|
|
}
|
2013-04-16 16:45:16 +02:00
|
|
|
|
2018-07-03 16:53:10 +02:00
|
|
|
void qpci_init_pc(QPCIBusPC *qpci, QTestState *qts, QGuestAllocator *alloc)
|
|
|
|
{
|
2017-09-11 19:19:52 +02:00
|
|
|
assert(qts);
|
2013-04-16 16:45:16 +02:00
|
|
|
|
2018-08-09 12:44:56 +02:00
|
|
|
/* tests can use pci-bus */
|
2019-03-23 15:26:35 +01:00
|
|
|
qpci->bus.has_buggy_msi = false;
|
2018-08-09 12:44:56 +02:00
|
|
|
|
2018-07-03 16:53:10 +02:00
|
|
|
qpci->bus.pio_readb = qpci_pc_pio_readb;
|
|
|
|
qpci->bus.pio_readw = qpci_pc_pio_readw;
|
|
|
|
qpci->bus.pio_readl = qpci_pc_pio_readl;
|
|
|
|
qpci->bus.pio_readq = qpci_pc_pio_readq;
|
|
|
|
|
|
|
|
qpci->bus.pio_writeb = qpci_pc_pio_writeb;
|
|
|
|
qpci->bus.pio_writew = qpci_pc_pio_writew;
|
|
|
|
qpci->bus.pio_writel = qpci_pc_pio_writel;
|
|
|
|
qpci->bus.pio_writeq = qpci_pc_pio_writeq;
|
|
|
|
|
|
|
|
qpci->bus.memread = qpci_pc_memread;
|
|
|
|
qpci->bus.memwrite = qpci_pc_memwrite;
|
2016-10-18 08:02:49 +02:00
|
|
|
|
2018-07-03 16:53:10 +02:00
|
|
|
qpci->bus.config_readb = qpci_pc_config_readb;
|
|
|
|
qpci->bus.config_readw = qpci_pc_config_readw;
|
|
|
|
qpci->bus.config_readl = qpci_pc_config_readl;
|
2016-10-18 08:02:49 +02:00
|
|
|
|
2018-07-03 16:53:10 +02:00
|
|
|
qpci->bus.config_writeb = qpci_pc_config_writeb;
|
|
|
|
qpci->bus.config_writew = qpci_pc_config_writew;
|
|
|
|
qpci->bus.config_writel = qpci_pc_config_writel;
|
2016-10-19 05:19:47 +02:00
|
|
|
|
2018-07-03 16:53:10 +02:00
|
|
|
qpci->bus.qts = qts;
|
|
|
|
qpci->bus.pio_alloc_ptr = 0xc000;
|
|
|
|
qpci->bus.mmio_alloc_ptr = 0xE0000000;
|
|
|
|
qpci->bus.mmio_limit = 0x100000000ULL;
|
2013-04-16 16:45:16 +02:00
|
|
|
|
2018-07-03 16:53:10 +02:00
|
|
|
qpci->obj.get_driver = qpci_pc_get_driver;
|
|
|
|
}
|
2013-04-16 16:45:16 +02:00
|
|
|
|
2018-07-03 16:53:10 +02:00
|
|
|
QPCIBus *qpci_new_pc(QTestState *qts, QGuestAllocator *alloc)
|
|
|
|
{
|
|
|
|
QPCIBusPC *qpci = g_new0(QPCIBusPC, 1);
|
|
|
|
qpci_init_pc(qpci, qts, alloc);
|
2013-04-16 16:45:16 +02:00
|
|
|
|
2018-07-03 16:53:10 +02:00
|
|
|
return &qpci->bus;
|
2013-04-16 16:45:16 +02:00
|
|
|
}
|
2014-08-04 23:11:23 +02:00
|
|
|
|
|
|
|
void qpci_free_pc(QPCIBus *bus)
|
|
|
|
{
|
2018-07-03 16:53:10 +02:00
|
|
|
QPCIBusPC *s;
|
|
|
|
|
|
|
|
if (!bus) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
s = container_of(bus, QPCIBusPC, bus);
|
2014-08-04 23:11:23 +02:00
|
|
|
|
|
|
|
g_free(s);
|
|
|
|
}
|
2014-09-26 11:28:08 +02:00
|
|
|
|
2019-04-09 14:25:57 +02:00
|
|
|
void qpci_unplug_acpi_device_test(QTestState *qts, const char *id, uint8_t slot)
|
2014-09-26 11:28:08 +02:00
|
|
|
{
|
|
|
|
QDict *response;
|
2018-08-06 08:53:33 +02:00
|
|
|
|
2019-04-09 14:25:57 +02:00
|
|
|
response = qtest_qmp(qts, "{'execute': 'device_del',"
|
|
|
|
" 'arguments': {'id': %s}}", id);
|
2014-09-26 11:28:08 +02:00
|
|
|
g_assert(response);
|
|
|
|
g_assert(!qdict_haskey(response, "error"));
|
2018-04-19 17:01:43 +02:00
|
|
|
qobject_unref(response);
|
2014-09-26 11:28:08 +02:00
|
|
|
|
2019-04-09 14:25:57 +02:00
|
|
|
qtest_outb(qts, ACPI_PCIHP_ADDR + PCI_EJ_BASE, 1 << slot);
|
2014-09-26 11:28:08 +02:00
|
|
|
|
2019-04-09 14:25:57 +02:00
|
|
|
qtest_qmp_eventwait(qts, "DEVICE_DELETED");
|
2014-09-26 11:28:08 +02:00
|
|
|
}
|
2018-07-03 16:53:10 +02:00
|
|
|
|
|
|
|
static void qpci_pc_register_nodes(void)
|
|
|
|
{
|
|
|
|
qos_node_create_driver("pci-bus-pc", NULL);
|
|
|
|
qos_node_produces("pci-bus-pc", "pci-bus");
|
|
|
|
}
|
|
|
|
|
|
|
|
libqos_init(qpci_pc_register_nodes);
|