2015-02-13 06:46:07 +01:00
|
|
|
/*
|
|
|
|
* QEMU Generic PCI Express Bridge Emulation
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015 Alexander Graf <agraf@suse.de>
|
|
|
|
*
|
|
|
|
* Code loosely based on q35.c.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* Check out these documents for more information on the device:
|
|
|
|
*
|
|
|
|
* http://www.kernel.org/doc/Documentation/devicetree/bindings/pci/host-generic-pci.txt
|
|
|
|
* http://www.firmware.org/1275/practice/imap/imap0_9d.pdf
|
|
|
|
*/
|
2019-05-23 16:35:07 +02:00
|
|
|
|
2016-01-26 19:17:15 +01:00
|
|
|
#include "qemu/osdep.h"
|
2019-04-30 21:15:52 +02:00
|
|
|
#include "qapi/error.h"
|
2019-08-12 07:23:42 +02:00
|
|
|
#include "hw/irq.h"
|
2015-02-13 06:46:07 +01:00
|
|
|
#include "hw/pci-host/gpex.h"
|
2019-08-12 07:23:51 +02:00
|
|
|
#include "hw/qdev-properties.h"
|
2019-08-12 07:23:45 +02:00
|
|
|
#include "migration/vmstate.h"
|
2019-05-23 16:35:07 +02:00
|
|
|
#include "qemu/module.h"
|
2015-02-13 06:46:07 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* GPEX host
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void gpex_set_irq(void *opaque, int irq_num, int level)
|
|
|
|
{
|
|
|
|
GPEXHost *s = opaque;
|
|
|
|
|
|
|
|
qemu_set_irq(s->irq[irq_num], level);
|
|
|
|
}
|
|
|
|
|
2017-09-14 19:43:18 +02:00
|
|
|
int gpex_set_irq_num(GPEXHost *s, int index, int gsi)
|
|
|
|
{
|
|
|
|
if (index >= GPEX_NUM_IRQS) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->irq_num[index] = gsi;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-14 19:43:19 +02:00
|
|
|
static PCIINTxRoute gpex_route_intx_pin_to_irq(void *opaque, int pin)
|
|
|
|
{
|
|
|
|
PCIINTxRoute route;
|
|
|
|
GPEXHost *s = opaque;
|
2017-10-31 12:50:52 +01:00
|
|
|
int gsi = s->irq_num[pin];
|
2017-09-14 19:43:19 +02:00
|
|
|
|
2017-10-31 12:50:52 +01:00
|
|
|
route.irq = gsi;
|
|
|
|
if (gsi < 0) {
|
|
|
|
route.mode = PCI_INTX_DISABLED;
|
|
|
|
} else {
|
|
|
|
route.mode = PCI_INTX_ENABLED;
|
|
|
|
}
|
2017-09-14 19:43:19 +02:00
|
|
|
|
|
|
|
return route;
|
|
|
|
}
|
|
|
|
|
2015-02-13 06:46:07 +01:00
|
|
|
static void gpex_host_realize(DeviceState *dev, Error **errp)
|
|
|
|
{
|
|
|
|
PCIHostState *pci = PCI_HOST_BRIDGE(dev);
|
|
|
|
GPEXHost *s = GPEX_HOST(dev);
|
|
|
|
SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
|
|
|
|
PCIExpressHost *pex = PCIE_HOST_BRIDGE(dev);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
pcie_host_mmcfg_init(pex, PCIE_MMCFG_SIZE_MAX);
|
2021-03-25 17:33:15 +01:00
|
|
|
sysbus_init_mmio(sbd, &pex->mmio);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note that the MemoryRegions io_mmio and io_ioport that we pass
|
|
|
|
* to pci_register_root_bus() are not the same as the
|
|
|
|
* MemoryRegions io_mmio_window and io_ioport_window that we
|
|
|
|
* expose as SysBus MRs. The difference is in the behaviour of
|
|
|
|
* accesses to addresses where no PCI device has been mapped.
|
|
|
|
*
|
|
|
|
* io_mmio and io_ioport are the underlying PCI view of the PCI
|
|
|
|
* address space, and when a PCI device does a bus master access
|
|
|
|
* to a bad address this is reported back to it as a transaction
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
* io_mmio_window and io_ioport_window implement "unmapped
|
|
|
|
* addresses read as -1 and ignore writes"; this is traditional
|
|
|
|
* x86 PC behaviour, which is not mandated by the PCI spec proper
|
|
|
|
* but expected by much PCI-using guest software, including Linux.
|
|
|
|
*
|
|
|
|
* In the interests of not being unnecessarily surprising, we
|
|
|
|
* implement it in the gpex PCI host controller, by providing the
|
|
|
|
* _window MRs, which are containers with io ops that implement
|
|
|
|
* the 'background' behaviour and which hold the real PCI MRs as
|
|
|
|
* subregions.
|
|
|
|
*/
|
2015-02-13 06:46:07 +01:00
|
|
|
memory_region_init(&s->io_mmio, OBJECT(s), "gpex_mmio", UINT64_MAX);
|
|
|
|
memory_region_init(&s->io_ioport, OBJECT(s), "gpex_ioport", 64 * 1024);
|
|
|
|
|
2021-03-25 17:33:15 +01:00
|
|
|
if (s->allow_unmapped_accesses) {
|
|
|
|
memory_region_init_io(&s->io_mmio_window, OBJECT(s),
|
|
|
|
&unassigned_io_ops, OBJECT(s),
|
|
|
|
"gpex_mmio_window", UINT64_MAX);
|
|
|
|
memory_region_init_io(&s->io_ioport_window, OBJECT(s),
|
|
|
|
&unassigned_io_ops, OBJECT(s),
|
|
|
|
"gpex_ioport_window", 64 * 1024);
|
|
|
|
|
|
|
|
memory_region_add_subregion(&s->io_mmio_window, 0, &s->io_mmio);
|
|
|
|
memory_region_add_subregion(&s->io_ioport_window, 0, &s->io_ioport);
|
|
|
|
sysbus_init_mmio(sbd, &s->io_mmio_window);
|
|
|
|
sysbus_init_mmio(sbd, &s->io_ioport_window);
|
|
|
|
} else {
|
|
|
|
sysbus_init_mmio(sbd, &s->io_mmio);
|
|
|
|
sysbus_init_mmio(sbd, &s->io_ioport);
|
|
|
|
}
|
|
|
|
|
2015-02-13 06:46:07 +01:00
|
|
|
for (i = 0; i < GPEX_NUM_IRQS; i++) {
|
|
|
|
sysbus_init_irq(sbd, &s->irq[i]);
|
2017-10-31 12:50:52 +01:00
|
|
|
s->irq_num[i] = -1;
|
2015-02-13 06:46:07 +01:00
|
|
|
}
|
|
|
|
|
2017-11-29 09:46:22 +01:00
|
|
|
pci->bus = pci_register_root_bus(dev, "pcie.0", gpex_set_irq,
|
|
|
|
pci_swizzle_map_irq_fn, s, &s->io_mmio,
|
|
|
|
&s->io_ioport, 0, 4, TYPE_PCIE_BUS);
|
2015-02-13 06:46:07 +01:00
|
|
|
|
2017-09-14 19:43:19 +02:00
|
|
|
pci_bus_set_route_irq_fn(pci->bus, gpex_route_intx_pin_to_irq);
|
qdev: Convert uses of qdev_set_parent_bus() with Coccinelle
In addition to the qdev_create() patterns converted so far, we have a
qdev_set_parent_bus() pattern. Mostly when we embed a device in a
parent device rather than allocating it on the heap.
This pattern also puts devices in the dangerous "no QOM parent, but
plugged into bus" state I explained in recent commit "qdev: New
qdev_new(), qdev_realize(), etc."
Apply same solution: convert to qdev_realize(). Coccinelle script:
@@
expression dev, bus, errp;
symbol true;
@@
- qdev_set_parent_bus(DEVICE(dev), bus);
...
- object_property_set_bool(OBJECT(dev), true, "realized", errp);
+ qdev_realize(DEVICE(dev), bus, errp);
@ depends on !(file in "qdev-monitor.c") && !(file in "hw/core/qdev.c")@
expression dev, bus, errp;
symbol true;
@@
- qdev_set_parent_bus(dev, bus);
...
- object_property_set_bool(OBJECT(dev), true, "realized", errp);
+ qdev_realize(dev, bus, errp);
@@
expression dev, bus;
symbol true;
@@
- qdev_set_parent_bus(DEVICE(dev), bus);
...
- qdev_init_nofail(DEVICE(dev));
+ qdev_realize(DEVICE(dev), bus, &error_fatal);
Unconverted uses of qdev_set_parent_bus() remain. They'll be
converted later in this series.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20200610053247.1583243-12-armbru@redhat.com>
[Also convert new hw/virtio/vhost-user-vsock-pci.c]
2020-06-10 07:32:00 +02:00
|
|
|
qdev_realize(DEVICE(&s->gpex_root), BUS(pci->bus), &error_fatal);
|
2015-02-13 06:46:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *gpex_host_root_bus_path(PCIHostState *host_bridge,
|
|
|
|
PCIBus *rootbus)
|
|
|
|
{
|
|
|
|
return "0000:00";
|
|
|
|
}
|
|
|
|
|
2021-03-25 17:33:15 +01:00
|
|
|
static Property gpex_host_properties[] = {
|
|
|
|
/*
|
|
|
|
* Permit CPU accesses to unmapped areas of the PIO and MMIO windows
|
|
|
|
* (discarding writes and returning -1 for reads) rather than aborting.
|
|
|
|
*/
|
|
|
|
DEFINE_PROP_BOOL("allow-unmapped-accesses", GPEXHost,
|
|
|
|
allow_unmapped_accesses, true),
|
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
2015-02-13 06:46:07 +01:00
|
|
|
static void gpex_host_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
|
|
|
PCIHostBridgeClass *hc = PCI_HOST_BRIDGE_CLASS(klass);
|
|
|
|
|
|
|
|
hc->root_bus_path = gpex_host_root_bus_path;
|
|
|
|
dc->realize = gpex_host_realize;
|
|
|
|
set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
|
|
|
|
dc->fw_name = "pci";
|
2021-03-25 17:33:15 +01:00
|
|
|
device_class_set_props(dc, gpex_host_properties);
|
2015-02-13 06:46:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void gpex_host_initfn(Object *obj)
|
|
|
|
{
|
|
|
|
GPEXHost *s = GPEX_HOST(obj);
|
|
|
|
GPEXRootState *root = &s->gpex_root;
|
|
|
|
|
qom: Less verbose object_initialize_child()
All users of object_initialize_child() pass the obvious child size
argument. Almost all pass &error_abort and no properties. Tiresome.
Rename object_initialize_child() to
object_initialize_child_with_props() to free the name. New
convenience wrapper object_initialize_child() automates the size
argument, and passes &error_abort and no properties.
Rename object_initialize_childv() to
object_initialize_child_with_propsv() for consistency.
Convert callers with this Coccinelle script:
@@
expression parent, propname, type;
expression child, size;
symbol error_abort;
@@
- object_initialize_child(parent, propname, OBJECT(child), size, type, &error_abort, NULL)
+ object_initialize_child(parent, propname, child, size, type, &error_abort, NULL)
@@
expression parent, propname, type;
expression child;
symbol error_abort;
@@
- object_initialize_child(parent, propname, child, sizeof(*child), type, &error_abort, NULL)
+ object_initialize_child(parent, propname, child, type)
@@
expression parent, propname, type;
expression child;
symbol error_abort;
@@
- object_initialize_child(parent, propname, &child, sizeof(child), type, &error_abort, NULL)
+ object_initialize_child(parent, propname, &child, type)
@@
expression parent, propname, type;
expression child, size, err;
expression list props;
@@
- object_initialize_child(parent, propname, child, size, type, err, props)
+ object_initialize_child_with_props(parent, propname, child, size, type, err, props)
Note that Coccinelle chokes on ARMSSE typedef vs. macro in
hw/arm/armsse.c. Worked around by temporarily renaming the macro for
the spatch run.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Acked-by: Alistair Francis <alistair.francis@wdc.com>
[Rebased: machine opentitan is new (commit fe0fe4735e7)]
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20200610053247.1583243-37-armbru@redhat.com>
2020-06-10 07:32:25 +02:00
|
|
|
object_initialize_child(obj, "gpex_root", root, TYPE_GPEX_ROOT_DEVICE);
|
2017-06-07 18:36:11 +02:00
|
|
|
qdev_prop_set_int32(DEVICE(root), "addr", PCI_DEVFN(0, 0));
|
2015-02-13 06:46:07 +01:00
|
|
|
qdev_prop_set_bit(DEVICE(root), "multifunction", false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo gpex_host_info = {
|
|
|
|
.name = TYPE_GPEX_HOST,
|
|
|
|
.parent = TYPE_PCIE_HOST_BRIDGE,
|
|
|
|
.instance_size = sizeof(GPEXHost),
|
|
|
|
.instance_init = gpex_host_initfn,
|
|
|
|
.class_init = gpex_host_class_init,
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* GPEX Root D0:F0
|
|
|
|
*/
|
|
|
|
|
|
|
|
static const VMStateDescription vmstate_gpex_root = {
|
|
|
|
.name = "gpex_root",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
|
|
|
.fields = (VMStateField[]) {
|
|
|
|
VMSTATE_PCI_DEVICE(parent_obj, GPEXRootState),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static void gpex_root_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
|
|
|
|
|
|
|
set_bit(DEVICE_CATEGORY_BRIDGE, dc->categories);
|
|
|
|
dc->desc = "QEMU generic PCIe host bridge";
|
|
|
|
dc->vmsd = &vmstate_gpex_root;
|
|
|
|
k->vendor_id = PCI_VENDOR_ID_REDHAT;
|
|
|
|
k->device_id = PCI_DEVICE_ID_REDHAT_PCIE_HOST;
|
|
|
|
k->revision = 0;
|
|
|
|
k->class_id = PCI_CLASS_BRIDGE_HOST;
|
|
|
|
/*
|
|
|
|
* PCI-facing part of the host bridge, not usable without the
|
|
|
|
* host-facing part, which can't be device_add'ed, yet.
|
|
|
|
*/
|
2017-05-03 22:35:44 +02:00
|
|
|
dc->user_creatable = false;
|
2015-02-13 06:46:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo gpex_root_info = {
|
|
|
|
.name = TYPE_GPEX_ROOT_DEVICE,
|
|
|
|
.parent = TYPE_PCI_DEVICE,
|
|
|
|
.instance_size = sizeof(GPEXRootState),
|
|
|
|
.class_init = gpex_root_class_init,
|
2017-09-27 21:56:34 +02:00
|
|
|
.interfaces = (InterfaceInfo[]) {
|
|
|
|
{ INTERFACE_CONVENTIONAL_PCI_DEVICE },
|
|
|
|
{ },
|
|
|
|
},
|
2015-02-13 06:46:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static void gpex_register(void)
|
|
|
|
{
|
|
|
|
type_register_static(&gpex_root_info);
|
|
|
|
type_register_static(&gpex_host_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(gpex_register)
|