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"
|
2013-04-16 18:45:16 +04:00
|
|
|
#include "libqtest.h"
|
|
|
|
#include "libqos/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"
|
|
|
|
|
|
|
|
#include "qemu-common.h"
|
|
|
|
|
|
|
|
|
2014-09-26 13:28:08 +04:00
|
|
|
#define ACPI_PCIHP_ADDR 0xae00
|
|
|
|
#define PCI_EJ_BASE 0x0008
|
|
|
|
|
2013-04-16 18:45:16 +04:00
|
|
|
typedef struct QPCIBusPC
|
|
|
|
{
|
|
|
|
QPCIBus bus;
|
|
|
|
} QPCIBusPC;
|
|
|
|
|
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
|
|
|
{
|
2016-10-18 09:02:49 +03:00
|
|
|
return inb(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)
|
|
|
|
{
|
|
|
|
outb(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)
|
|
|
|
{
|
|
|
|
return inw(addr);
|
|
|
|
}
|
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
|
|
|
{
|
2016-10-18 09:02:49 +03:00
|
|
|
outw(addr, val);
|
|
|
|
}
|
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
|
|
|
{
|
2016-10-18 09:02:49 +03:00
|
|
|
return inl(addr);
|
|
|
|
}
|
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
|
|
|
{
|
2016-10-18 09:02:49 +03:00
|
|
|
outl(addr, val);
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
return (uint64_t)inl(addr) + ((uint64_t)inl(addr + 4) << 32);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qpci_pc_pio_writeq(QPCIBus *bus, uint32_t addr, uint64_t val)
|
|
|
|
{
|
|
|
|
outl(addr, val & 0xffffffff);
|
|
|
|
outl(addr + 4, val >> 32);
|
|
|
|
}
|
|
|
|
|
2016-10-19 06:19:47 +03:00
|
|
|
static void qpci_pc_memread(QPCIBus *bus, uint32_t addr, void *buf, size_t len)
|
|
|
|
{
|
|
|
|
memread(addr, buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qpci_pc_memwrite(QPCIBus *bus, uint32_t addr,
|
|
|
|
const void *buf, size_t len)
|
|
|
|
{
|
|
|
|
memwrite(addr, buf, len);
|
|
|
|
}
|
|
|
|
|
2013-04-16 18:45:16 +04:00
|
|
|
static uint8_t qpci_pc_config_readb(QPCIBus *bus, int devfn, uint8_t offset)
|
|
|
|
{
|
2014-03-17 20:00:38 +04:00
|
|
|
outl(0xcf8, (1U << 31) | (devfn << 8) | offset);
|
2013-04-16 18:45:16 +04:00
|
|
|
return inb(0xcfc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t qpci_pc_config_readw(QPCIBus *bus, int devfn, uint8_t offset)
|
|
|
|
{
|
2014-03-17 20:00:38 +04:00
|
|
|
outl(0xcf8, (1U << 31) | (devfn << 8) | offset);
|
2013-04-16 18:45:16 +04:00
|
|
|
return inw(0xcfc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t qpci_pc_config_readl(QPCIBus *bus, int devfn, uint8_t offset)
|
|
|
|
{
|
2014-03-17 20:00:38 +04:00
|
|
|
outl(0xcf8, (1U << 31) | (devfn << 8) | offset);
|
2013-04-16 18:45:16 +04:00
|
|
|
return inl(0xcfc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qpci_pc_config_writeb(QPCIBus *bus, int devfn, uint8_t offset, uint8_t value)
|
|
|
|
{
|
2014-03-17 20:00:38 +04:00
|
|
|
outl(0xcf8, (1U << 31) | (devfn << 8) | offset);
|
2013-04-16 18:45:16 +04:00
|
|
|
outb(0xcfc, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qpci_pc_config_writew(QPCIBus *bus, int devfn, uint8_t offset, uint16_t value)
|
|
|
|
{
|
2014-03-17 20:00:38 +04:00
|
|
|
outl(0xcf8, (1U << 31) | (devfn << 8) | offset);
|
2013-04-16 18:45:16 +04:00
|
|
|
outw(0xcfc, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qpci_pc_config_writel(QPCIBus *bus, int devfn, uint8_t offset, uint32_t value)
|
|
|
|
{
|
2014-03-17 20:00:38 +04:00
|
|
|
outl(0xcf8, (1U << 31) | (devfn << 8) | offset);
|
2013-04-16 18:45:16 +04:00
|
|
|
outl(0xcfc, value);
|
|
|
|
}
|
|
|
|
|
2017-09-11 20:19:52 +03:00
|
|
|
QPCIBus *qpci_init_pc(QTestState *qts, QGuestAllocator *alloc)
|
2013-04-16 18:45:16 +04:00
|
|
|
{
|
2017-09-11 20:19:52 +03:00
|
|
|
QPCIBusPC *ret = g_new0(QPCIBusPC, 1);
|
2013-04-16 18:45:16 +04:00
|
|
|
|
2017-09-11 20:19:52 +03:00
|
|
|
assert(qts);
|
2013-04-16 18:45:16 +04:00
|
|
|
|
2016-10-18 09:02:49 +03:00
|
|
|
ret->bus.pio_readb = qpci_pc_pio_readb;
|
|
|
|
ret->bus.pio_readw = qpci_pc_pio_readw;
|
|
|
|
ret->bus.pio_readl = qpci_pc_pio_readl;
|
2016-10-19 07:00:21 +03:00
|
|
|
ret->bus.pio_readq = qpci_pc_pio_readq;
|
2016-10-18 09:02:49 +03:00
|
|
|
|
|
|
|
ret->bus.pio_writeb = qpci_pc_pio_writeb;
|
|
|
|
ret->bus.pio_writew = qpci_pc_pio_writew;
|
|
|
|
ret->bus.pio_writel = qpci_pc_pio_writel;
|
2016-10-19 07:00:21 +03:00
|
|
|
ret->bus.pio_writeq = qpci_pc_pio_writeq;
|
2016-10-18 09:02:49 +03:00
|
|
|
|
2016-10-19 06:19:47 +03:00
|
|
|
ret->bus.memread = qpci_pc_memread;
|
|
|
|
ret->bus.memwrite = qpci_pc_memwrite;
|
|
|
|
|
2013-04-16 18:45:16 +04:00
|
|
|
ret->bus.config_readb = qpci_pc_config_readb;
|
|
|
|
ret->bus.config_readw = qpci_pc_config_readw;
|
|
|
|
ret->bus.config_readl = qpci_pc_config_readl;
|
|
|
|
|
|
|
|
ret->bus.config_writeb = qpci_pc_config_writeb;
|
|
|
|
ret->bus.config_writew = qpci_pc_config_writew;
|
|
|
|
ret->bus.config_writel = qpci_pc_config_writel;
|
|
|
|
|
2017-09-11 20:19:52 +03:00
|
|
|
ret->bus.qts = qts;
|
2016-10-19 06:06:51 +03:00
|
|
|
ret->bus.pio_alloc_ptr = 0xc000;
|
|
|
|
ret->bus.mmio_alloc_ptr = 0xE0000000;
|
|
|
|
ret->bus.mmio_limit = 0x100000000ULL;
|
2013-04-16 18:45:16 +04:00
|
|
|
|
|
|
|
return &ret->bus;
|
|
|
|
}
|
2014-08-05 01:11:23 +04:00
|
|
|
|
|
|
|
void qpci_free_pc(QPCIBus *bus)
|
|
|
|
{
|
|
|
|
QPCIBusPC *s = container_of(bus, QPCIBusPC, bus);
|
|
|
|
|
|
|
|
g_free(s);
|
|
|
|
}
|
2014-09-26 13:28:08 +04:00
|
|
|
|
|
|
|
void qpci_unplug_acpi_device_test(const char *id, uint8_t slot)
|
|
|
|
{
|
|
|
|
QDict *response;
|
|
|
|
char *cmd;
|
|
|
|
|
|
|
|
cmd = g_strdup_printf("{'execute': 'device_del',"
|
|
|
|
" 'arguments': {"
|
|
|
|
" 'id': '%s'"
|
|
|
|
"}}", id);
|
|
|
|
response = qmp(cmd);
|
|
|
|
g_free(cmd);
|
|
|
|
g_assert(response);
|
|
|
|
g_assert(!qdict_haskey(response, "error"));
|
|
|
|
QDECREF(response);
|
|
|
|
|
|
|
|
outb(ACPI_PCIHP_ADDR + PCI_EJ_BASE, 1 << slot);
|
|
|
|
|
2017-09-11 20:19:48 +03:00
|
|
|
qmp_eventwait("DEVICE_DELETED");
|
2014-09-26 13:28:08 +04:00
|
|
|
}
|