2013-04-16 18:45:16 +04: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 21:08:51 +03:00
|
|
|
#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-pc.h"
|
2018-02-01 14:18:39 +03:00
|
|
|
#include "qapi/qmp/qdict.h"
|
2013-04-16 18:45:16 +04:00
|
|
|
#include "hw/pci/pci_regs.h"
|
|
|
|
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2013-04-16 18:45:16 +04:00
|
|
|
|
2014-09-26 13:28:08 +04:00
|
|
|
#define ACPI_PCIHP_ADDR 0xae00
|
|
|
|
#define PCI_EJ_BASE 0x0008
|
|
|
|
|
2016-10-18 09:02:49 +03:00
|
|
|
static uint8_t qpci_pc_pio_readb(QPCIBus *bus, uint32_t addr)
|
2013-04-16 18:45:16 +04:00
|
|
|
{
|
2018-11-06 22:15:38 +03:00
|
|
|
return qtest_inb(bus->qts, addr);
|
2013-04-16 18:45:16 +04:00
|
|
|
}
|
|
|
|
|
2016-10-18 09:02:49 +03:00
|
|
|
static void qpci_pc_pio_writeb(QPCIBus *bus, uint32_t addr, uint8_t val)
|
|
|
|
{
|
2018-11-06 22:15:38 +03:00
|
|
|
qtest_outb(bus->qts, addr, val);
|
2013-04-16 18:45:16 +04:00
|
|
|
}
|
|
|
|
|
2016-10-18 09:02:49 +03:00
|
|
|
static uint16_t qpci_pc_pio_readw(QPCIBus *bus, uint32_t addr)
|
|
|
|
{
|
2018-11-06 22:15:38 +03:00
|
|
|
return qtest_inw(bus->qts, addr);
|
2016-10-18 09:02:49 +03:00
|
|
|
}
|
2013-04-16 18:45:16 +04:00
|
|
|
|
2016-10-18 09:02:49 +03:00
|
|
|
static void qpci_pc_pio_writew(QPCIBus *bus, uint32_t addr, uint16_t val)
|
2013-04-16 18:45:16 +04:00
|
|
|
{
|
2018-11-06 22:15:38 +03:00
|
|
|
qtest_outw(bus->qts, addr, val);
|
2016-10-18 09:02:49 +03:00
|
|
|
}
|
2013-04-16 18:45:16 +04:00
|
|
|
|
2016-10-18 09:02:49 +03:00
|
|
|
static uint32_t qpci_pc_pio_readl(QPCIBus *bus, uint32_t addr)
|
2013-04-16 18:45:16 +04:00
|
|
|
{
|
2018-11-06 22:15:38 +03:00
|
|
|
return qtest_inl(bus->qts, addr);
|
2016-10-18 09:02:49 +03:00
|
|
|
}
|
2013-04-16 18:45:16 +04:00
|
|
|
|
2016-10-18 09:02:49 +03:00
|
|
|
static void qpci_pc_pio_writel(QPCIBus *bus, uint32_t addr, uint32_t val)
|
2013-04-16 18:45:16 +04:00
|
|
|
{
|
2018-11-06 22:15:38 +03:00
|
|
|
qtest_outl(bus->qts, addr, val);
|
2016-10-18 09:02:49 +03:00
|
|
|
}
|
2013-04-16 18:45:16 +04:00
|
|
|
|
2016-10-19 07:00:21 +03:00
|
|
|
static uint64_t qpci_pc_pio_readq(QPCIBus *bus, uint32_t addr)
|
|
|
|
{
|
2018-11-06 22:15:38 +03:00
|
|
|
return (uint64_t)qtest_inl(bus->qts, addr) +
|
|
|
|
((uint64_t)qtest_inl(bus->qts, addr + 4) << 32);
|
2016-10-19 07:00:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void qpci_pc_pio_writeq(QPCIBus *bus, uint32_t addr, uint64_t val)
|
|
|
|
{
|
2018-11-06 22:15:38 +03:00
|
|
|
qtest_outl(bus->qts, addr, val & 0xffffffff);
|
|
|
|
qtest_outl(bus->qts, addr + 4, val >> 32);
|
2016-10-19 07:00:21 +03:00
|
|
|
}
|
|
|
|
|
2016-10-19 06:19:47 +03:00
|
|
|
static void qpci_pc_memread(QPCIBus *bus, uint32_t addr, void *buf, size_t len)
|
|
|
|
{
|
2018-11-06 22:15:38 +03:00
|
|
|
qtest_memread(bus->qts, addr, buf, len);
|
2016-10-19 06:19:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void qpci_pc_memwrite(QPCIBus *bus, uint32_t addr,
|
|
|
|
const void *buf, size_t len)
|
|
|
|
{
|
2018-11-06 22:15:38 +03:00
|
|
|
qtest_memwrite(bus->qts, addr, buf, len);
|
2016-10-19 06:19:47 +03:00
|
|
|
}
|
|
|
|
|
2013-04-16 18:45:16 +04:00
|
|
|
static uint8_t qpci_pc_config_readb(QPCIBus *bus, int devfn, uint8_t offset)
|
|
|
|
{
|
2018-11-06 22:15:38 +03:00
|
|
|
qtest_outl(bus->qts, 0xcf8, (1U << 31) | (devfn << 8) | offset);
|
|
|
|
return qtest_inb(bus->qts, 0xcfc);
|
2013-04-16 18:45:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t qpci_pc_config_readw(QPCIBus *bus, int devfn, uint8_t offset)
|
|
|
|
{
|
2018-11-06 22:15:38 +03:00
|
|
|
qtest_outl(bus->qts, 0xcf8, (1U << 31) | (devfn << 8) | offset);
|
|
|
|
return qtest_inw(bus->qts, 0xcfc);
|
2013-04-16 18:45:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t qpci_pc_config_readl(QPCIBus *bus, int devfn, uint8_t offset)
|
|
|
|
{
|
2018-11-06 22:15:38 +03:00
|
|
|
qtest_outl(bus->qts, 0xcf8, (1U << 31) | (devfn << 8) | offset);
|
|
|
|
return qtest_inl(bus->qts, 0xcfc);
|
2013-04-16 18:45:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void qpci_pc_config_writeb(QPCIBus *bus, int devfn, uint8_t offset, uint8_t value)
|
|
|
|
{
|
2018-11-06 22:15:38 +03:00
|
|
|
qtest_outl(bus->qts, 0xcf8, (1U << 31) | (devfn << 8) | offset);
|
|
|
|
qtest_outb(bus->qts, 0xcfc, value);
|
2013-04-16 18:45:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void qpci_pc_config_writew(QPCIBus *bus, int devfn, uint8_t offset, uint16_t value)
|
|
|
|
{
|
2018-11-06 22:15:38 +03:00
|
|
|
qtest_outl(bus->qts, 0xcf8, (1U << 31) | (devfn << 8) | offset);
|
|
|
|
qtest_outw(bus->qts, 0xcfc, value);
|
2013-04-16 18:45:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void qpci_pc_config_writel(QPCIBus *bus, int devfn, uint8_t offset, uint32_t value)
|
|
|
|
{
|
2018-11-06 22:15:38 +03:00
|
|
|
qtest_outl(bus->qts, 0xcf8, (1U << 31) | (devfn << 8) | offset);
|
|
|
|
qtest_outl(bus->qts, 0xcfc, value);
|
2013-04-16 18:45:16 +04:00
|
|
|
}
|
|
|
|
|
2018-07-03 17:53:10 +03:00
|
|
|
static void *qpci_pc_get_driver(void *obj, const char *interface)
|
2013-04-16 18:45:16 +04:00
|
|
|
{
|
2018-07-03 17:53:10 +03: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 18:45:16 +04:00
|
|
|
|
2018-07-03 17:53:10 +03:00
|
|
|
void qpci_init_pc(QPCIBusPC *qpci, QTestState *qts, QGuestAllocator *alloc)
|
|
|
|
{
|
2017-09-11 20:19:52 +03:00
|
|
|
assert(qts);
|
2013-04-16 18:45:16 +04:00
|
|
|
|
2018-08-09 13:44:56 +03:00
|
|
|
/* tests can use pci-bus */
|
2019-03-23 17:26:35 +03:00
|
|
|
qpci->bus.has_buggy_msi = false;
|
2018-08-09 13:44:56 +03:00
|
|
|
|
2018-07-03 17:53:10 +03: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 09:02:49 +03:00
|
|
|
|
2018-07-03 17:53:10 +03: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 09:02:49 +03:00
|
|
|
|
2018-07-03 17:53:10 +03: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 06:19:47 +03:00
|
|
|
|
2018-07-03 17:53:10 +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-03 17:53:10 +03:00
|
|
|
qpci->bus.mmio_alloc_ptr = 0xE0000000;
|
|
|
|
qpci->bus.mmio_limit = 0x100000000ULL;
|
2013-04-16 18:45:16 +04:00
|
|
|
|
2018-07-03 17:53:10 +03:00
|
|
|
qpci->obj.get_driver = qpci_pc_get_driver;
|
|
|
|
}
|
2013-04-16 18:45:16 +04:00
|
|
|
|
2018-07-03 17:53:10 +03:00
|
|
|
QPCIBus *qpci_new_pc(QTestState *qts, QGuestAllocator *alloc)
|
|
|
|
{
|
|
|
|
QPCIBusPC *qpci = g_new0(QPCIBusPC, 1);
|
|
|
|
qpci_init_pc(qpci, qts, alloc);
|
2013-04-16 18:45:16 +04:00
|
|
|
|
2018-07-03 17:53:10 +03:00
|
|
|
return &qpci->bus;
|
2013-04-16 18:45:16 +04:00
|
|
|
}
|
2014-08-05 01:11:23 +04:00
|
|
|
|
|
|
|
void qpci_free_pc(QPCIBus *bus)
|
|
|
|
{
|
2018-07-03 17:53:10 +03:00
|
|
|
QPCIBusPC *s;
|
|
|
|
|
|
|
|
if (!bus) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
s = container_of(bus, QPCIBusPC, bus);
|
2014-08-05 01:11:23 +04:00
|
|
|
|
|
|
|
g_free(s);
|
|
|
|
}
|
2014-09-26 13:28:08 +04:00
|
|
|
|
2019-04-09 15:25:57 +03:00
|
|
|
void qpci_unplug_acpi_device_test(QTestState *qts, const char *id, uint8_t slot)
|
2014-09-26 13:28:08 +04:00
|
|
|
{
|
|
|
|
QDict *response;
|
2018-08-06 09:53:33 +03:00
|
|
|
|
2019-04-09 15:25:57 +03:00
|
|
|
response = qtest_qmp(qts, "{'execute': 'device_del',"
|
|
|
|
" 'arguments': {'id': %s}}", id);
|
2014-09-26 13:28:08 +04:00
|
|
|
g_assert(response);
|
|
|
|
g_assert(!qdict_haskey(response, "error"));
|
2018-04-19 18:01:43 +03:00
|
|
|
qobject_unref(response);
|
2014-09-26 13:28:08 +04:00
|
|
|
|
2020-06-23 19:17:59 +03:00
|
|
|
qtest_outl(qts, ACPI_PCIHP_ADDR + PCI_EJ_BASE, 1 << slot);
|
2014-09-26 13:28:08 +04:00
|
|
|
|
2019-04-09 15:25:57 +03:00
|
|
|
qtest_qmp_eventwait(qts, "DEVICE_DELETED");
|
2014-09-26 13:28:08 +04:00
|
|
|
}
|
2018-07-03 17:53:10 +03: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);
|