qemu/hw/ppc/ppc4xx_pci.c

391 lines
11 KiB
C
Raw Normal View History

/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
* Copyright IBM Corp. 2008
*
* Authors: Hollis Blanchard <hollisb@us.ibm.com>
*/
/* This file implements emulation of the 32-bit PCI controller found in some
* 4xx SoCs, such as the 440EP. */
#include "qemu/osdep.h"
#include "hw/irq.h"
#include "hw/ppc/ppc.h"
#include "hw/ppc/ppc4xx.h"
#include "migration/vmstate.h"
#include "qemu/module.h"
#include "sysemu/reset.h"
#include "hw/pci/pci.h"
#include "hw/pci/pci_host.h"
#include "exec/address-spaces.h"
#include "trace.h"
#include "qom/object.h"
struct PCIMasterMap {
uint32_t la;
uint32_t ma;
uint32_t pcila;
uint32_t pciha;
};
struct PCITargetMap {
uint32_t ms;
uint32_t la;
};
typedef struct PPC4xxPCIState PPC4xxPCIState;
DECLARE_INSTANCE_CHECKER(PPC4xxPCIState, PPC4xx_PCI_HOST_BRIDGE,
TYPE_PPC4xx_PCI_HOST_BRIDGE)
#define PPC4xx_PCI_NR_PMMS 3
#define PPC4xx_PCI_NR_PTMS 2
struct PPC4xxPCIState {
PCIHostState parent_obj;
struct PCIMasterMap pmm[PPC4xx_PCI_NR_PMMS];
struct PCITargetMap ptm[PPC4xx_PCI_NR_PTMS];
qemu_irq irq[4];
MemoryRegion container;
MemoryRegion iomem;
};
#define PCIC0_CFGADDR 0x0
#define PCIC0_CFGDATA 0x4
/* PLB Memory Map (PMM) registers specify which PLB addresses are translated to
* PCI accesses. */
#define PCIL0_PMM0LA 0x0
#define PCIL0_PMM0MA 0x4
#define PCIL0_PMM0PCILA 0x8
#define PCIL0_PMM0PCIHA 0xc
#define PCIL0_PMM1LA 0x10
#define PCIL0_PMM1MA 0x14
#define PCIL0_PMM1PCILA 0x18
#define PCIL0_PMM1PCIHA 0x1c
#define PCIL0_PMM2LA 0x20
#define PCIL0_PMM2MA 0x24
#define PCIL0_PMM2PCILA 0x28
#define PCIL0_PMM2PCIHA 0x2c
/* PCI Target Map (PTM) registers specify which PCI addresses are translated to
* PLB accesses. */
#define PCIL0_PTM1MS 0x30
#define PCIL0_PTM1LA 0x34
#define PCIL0_PTM2MS 0x38
#define PCIL0_PTM2LA 0x3c
#define PCI_REG_BASE 0x800000
#define PCI_REG_SIZE 0x40
#define PCI_ALL_SIZE (PCI_REG_BASE + PCI_REG_SIZE)
static void ppc4xx_pci_reg_write4(void *opaque, hwaddr offset,
uint64_t value, unsigned size)
{
struct PPC4xxPCIState *pci = opaque;
/* We ignore all target attempts at PCI configuration, effectively
* assuming a bidirectional 1:1 mapping of PLB and PCI space. */
switch (offset) {
case PCIL0_PMM0LA:
pci->pmm[0].la = value;
break;
case PCIL0_PMM0MA:
pci->pmm[0].ma = value;
break;
case PCIL0_PMM0PCIHA:
pci->pmm[0].pciha = value;
break;
case PCIL0_PMM0PCILA:
pci->pmm[0].pcila = value;
break;
case PCIL0_PMM1LA:
pci->pmm[1].la = value;
break;
case PCIL0_PMM1MA:
pci->pmm[1].ma = value;
break;
case PCIL0_PMM1PCIHA:
pci->pmm[1].pciha = value;
break;
case PCIL0_PMM1PCILA:
pci->pmm[1].pcila = value;
break;
case PCIL0_PMM2LA:
pci->pmm[2].la = value;
break;
case PCIL0_PMM2MA:
pci->pmm[2].ma = value;
break;
case PCIL0_PMM2PCIHA:
pci->pmm[2].pciha = value;
break;
case PCIL0_PMM2PCILA:
pci->pmm[2].pcila = value;
break;
case PCIL0_PTM1MS:
pci->ptm[0].ms = value;
break;
case PCIL0_PTM1LA:
pci->ptm[0].la = value;
break;
case PCIL0_PTM2MS:
pci->ptm[1].ms = value;
break;
case PCIL0_PTM2LA:
pci->ptm[1].la = value;
break;
default:
printf("%s: unhandled PCI internal register 0x%lx\n", __func__,
(unsigned long)offset);
break;
}
}
static uint64_t ppc4xx_pci_reg_read4(void *opaque, hwaddr offset,
unsigned size)
{
struct PPC4xxPCIState *pci = opaque;
uint32_t value;
switch (offset) {
case PCIL0_PMM0LA:
value = pci->pmm[0].la;
break;
case PCIL0_PMM0MA:
value = pci->pmm[0].ma;
break;
case PCIL0_PMM0PCIHA:
value = pci->pmm[0].pciha;
break;
case PCIL0_PMM0PCILA:
value = pci->pmm[0].pcila;
break;
case PCIL0_PMM1LA:
value = pci->pmm[1].la;
break;
case PCIL0_PMM1MA:
value = pci->pmm[1].ma;
break;
case PCIL0_PMM1PCIHA:
value = pci->pmm[1].pciha;
break;
case PCIL0_PMM1PCILA:
value = pci->pmm[1].pcila;
break;
case PCIL0_PMM2LA:
value = pci->pmm[2].la;
break;
case PCIL0_PMM2MA:
value = pci->pmm[2].ma;
break;
case PCIL0_PMM2PCIHA:
value = pci->pmm[2].pciha;
break;
case PCIL0_PMM2PCILA:
value = pci->pmm[2].pcila;
break;
case PCIL0_PTM1MS:
value = pci->ptm[0].ms;
break;
case PCIL0_PTM1LA:
value = pci->ptm[0].la;
break;
case PCIL0_PTM2MS:
value = pci->ptm[1].ms;
break;
case PCIL0_PTM2LA:
value = pci->ptm[1].la;
break;
default:
printf("%s: invalid PCI internal register 0x%lx\n", __func__,
(unsigned long)offset);
value = 0;
}
return value;
}
static const MemoryRegionOps pci_reg_ops = {
.read = ppc4xx_pci_reg_read4,
.write = ppc4xx_pci_reg_write4,
.endianness = DEVICE_LITTLE_ENDIAN,
};
static void ppc4xx_pci_reset(void *opaque)
{
struct PPC4xxPCIState *pci = opaque;
memset(pci->pmm, 0, sizeof(pci->pmm));
memset(pci->ptm, 0, sizeof(pci->ptm));
}
/* On Bamboo, all pins from each slot are tied to a single board IRQ. This
* may need further refactoring for other boards. */
static int ppc4xx_pci_map_irq(PCIDevice *pci_dev, int irq_num)
{
int slot = pci_dev->devfn >> 3;
trace_ppc4xx_pci_map_irq(pci_dev->devfn, irq_num, slot);
return slot - 1;
}
static void ppc4xx_pci_set_irq(void *opaque, int irq_num, int level)
{
qemu_irq *pci_irqs = opaque;
trace_ppc4xx_pci_set_irq(irq_num);
assert(irq_num >= 0);
qemu_set_irq(pci_irqs[irq_num], level);
}
static const VMStateDescription vmstate_pci_master_map = {
.name = "pci_master_map",
.version_id = 0,
.minimum_version_id = 0,
.fields = (VMStateField[]) {
VMSTATE_UINT32(la, struct PCIMasterMap),
VMSTATE_UINT32(ma, struct PCIMasterMap),
VMSTATE_UINT32(pcila, struct PCIMasterMap),
VMSTATE_UINT32(pciha, struct PCIMasterMap),
VMSTATE_END_OF_LIST()
}
};
static const VMStateDescription vmstate_pci_target_map = {
.name = "pci_target_map",
.version_id = 0,
.minimum_version_id = 0,
.fields = (VMStateField[]) {
VMSTATE_UINT32(ms, struct PCITargetMap),
VMSTATE_UINT32(la, struct PCITargetMap),
VMSTATE_END_OF_LIST()
}
};
static const VMStateDescription vmstate_ppc4xx_pci = {
.name = "ppc4xx_pci",
.version_id = 1,
.minimum_version_id = 1,
.fields = (VMStateField[]) {
VMSTATE_STRUCT_ARRAY(pmm, PPC4xxPCIState, PPC4xx_PCI_NR_PMMS, 1,
vmstate_pci_master_map,
struct PCIMasterMap),
VMSTATE_STRUCT_ARRAY(ptm, PPC4xxPCIState, PPC4xx_PCI_NR_PTMS, 1,
vmstate_pci_target_map,
struct PCITargetMap),
VMSTATE_END_OF_LIST()
}
};
/* XXX Interrupt acknowledge cycles not supported. */
static void ppc4xx_pcihost_realize(DeviceState *dev, Error **errp)
{
SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
PPC4xxPCIState *s;
PCIHostState *h;
PCIBus *b;
int i;
h = PCI_HOST_BRIDGE(dev);
s = PPC4xx_PCI_HOST_BRIDGE(dev);
for (i = 0; i < ARRAY_SIZE(s->irq); i++) {
sysbus_init_irq(sbd, &s->irq[i]);
}
b = pci_register_root_bus(dev, NULL, ppc4xx_pci_set_irq,
ppc4xx_pci_map_irq, s->irq, get_system_memory(),
get_system_io(), 0, ARRAY_SIZE(s->irq),
TYPE_PCI_BUS);
h->bus = b;
pci_create_simple(b, 0, "ppc4xx-host-bridge");
/* XXX split into 2 memory regions, one for config space, one for regs */
memory_region_init(&s->container, OBJECT(s), "pci-container", PCI_ALL_SIZE);
memory_region_init_io(&h->conf_mem, OBJECT(s), &pci_host_conf_le_ops, h,
"pci-conf-idx", 4);
memory_region_init_io(&h->data_mem, OBJECT(s), &pci_host_data_le_ops, h,
"pci-conf-data", 4);
memory_region_init_io(&s->iomem, OBJECT(s), &pci_reg_ops, s,
"pci.reg", PCI_REG_SIZE);
memory_region_add_subregion(&s->container, PCIC0_CFGADDR, &h->conf_mem);
memory_region_add_subregion(&s->container, PCIC0_CFGDATA, &h->data_mem);
memory_region_add_subregion(&s->container, PCI_REG_BASE, &s->iomem);
sysbus_init_mmio(sbd, &s->container);
qemu_register_reset(ppc4xx_pci_reset, s);
}
static void ppc4xx_host_bridge_class_init(ObjectClass *klass, void *data)
{
PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
DeviceClass *dc = DEVICE_CLASS(klass);
dc->desc = "Host bridge";
k->vendor_id = PCI_VENDOR_ID_IBM;
k->device_id = PCI_DEVICE_ID_IBM_440GX;
k->class_id = PCI_CLASS_BRIDGE_OTHER;
/*
* PCI-facing part of the host bridge, not usable without the
* host-facing part, which can't be device_add'ed, yet.
*/
qdev: Replace cannot_instantiate_with_device_add_yet with !user_creatable cannot_instantiate_with_device_add_yet was introduced by commit efec3dd631d94160288392721a5f9c39e50fb2bc to replace no_user. It was supposed to be a temporary measure. When it was introduced, we had 54 cannot_instantiate_with_device_add_yet=true lines in the code. Today (3 years later) this number has not shrunk: we now have 57 cannot_instantiate_with_device_add_yet=true lines. I think it is safe to say it is not a temporary measure, and we won't see the flag go away soon. Instead of a long field name that misleads people to believe it is temporary, replace it a shorter and less misleading field: user_creatable. Except for code comments, changes were generated using the following Coccinelle patch: @@ expression DC; @@ ( -DC->cannot_instantiate_with_device_add_yet = false; +DC->user_creatable = true; | -DC->cannot_instantiate_with_device_add_yet = true; +DC->user_creatable = false; ) @@ typedef ObjectClass; expression dc; identifier class, data; @@ static void device_class_init(ObjectClass *class, void *data) { ... dc->hotpluggable = true; +dc->user_creatable = true; ... } @@ @@ struct DeviceClass { ... -bool cannot_instantiate_with_device_add_yet; +bool user_creatable; ... } @@ expression DC; @@ ( -!DC->cannot_instantiate_with_device_add_yet +DC->user_creatable | -DC->cannot_instantiate_with_device_add_yet +!DC->user_creatable ) Cc: Alistair Francis <alistair.francis@xilinx.com> Cc: Laszlo Ersek <lersek@redhat.com> Cc: Marcel Apfelbaum <marcel@redhat.com> Cc: Markus Armbruster <armbru@redhat.com> Cc: Peter Maydell <peter.maydell@linaro.org> Cc: Thomas Huth <thuth@redhat.com> Acked-by: Alistair Francis <alistair.francis@xilinx.com> Reviewed-by: Thomas Huth <thuth@redhat.com> Reviewed-by: Marcel Apfelbaum <marcel@redhat.com> Acked-by: Marcel Apfelbaum <marcel@redhat.com> Signed-off-by: Eduardo Habkost <ehabkost@redhat.com> Message-Id: <20170503203604.31462-2-ehabkost@redhat.com> [ehabkost: kept "TODO remove once we're there" comment] Reviewed-by: Markus Armbruster <armbru@redhat.com> Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
2017-05-03 23:35:44 +03:00
dc->user_creatable = false;
}
static const TypeInfo ppc4xx_host_bridge_info = {
.name = "ppc4xx-host-bridge",
.parent = TYPE_PCI_DEVICE,
.instance_size = sizeof(PCIDevice),
.class_init = ppc4xx_host_bridge_class_init,
pci: Add INTERFACE_CONVENTIONAL_PCI_DEVICE to Conventional PCI devices Add INTERFACE_CONVENTIONAL_PCI_DEVICE to all direct subtypes of TYPE_PCI_DEVICE, except: 1) The ones that already have INTERFACE_PCIE_DEVICE set: * base-xhci * e1000e * nvme * pvscsi * vfio-pci * virtio-pci * vmxnet3 2) base-pci-bridge Not all PCI bridges are Conventional PCI devices, so INTERFACE_CONVENTIONAL_PCI_DEVICE is added only to the subtypes that are actually Conventional PCI: * dec-21154-p2p-bridge * i82801b11-bridge * pbm-bridge * pci-bridge The direct subtypes of base-pci-bridge not touched by this patch are: * xilinx-pcie-root: Already marked as PCIe-only. * pcie-pci-bridge: Already marked as PCIe-only. * pcie-port: all non-abstract subtypes of pcie-port are already marked as PCIe-only devices. 3) megasas-base Not all megasas devices are Conventional PCI devices, so the interface names are added to the subclasses registered by megasas_register_types(), according to information in the megasas_devices[] array. "megasas-gen2" already implements INTERFACE_PCIE_DEVICE, so add INTERFACE_CONVENTIONAL_PCI_DEVICE only to "megasas". Acked-by: Alberto Garcia <berto@igalia.com> Acked-by: John Snow <jsnow@redhat.com> Acked-by: Anthony PERARD <anthony.perard@citrix.com> Signed-off-by: Eduardo Habkost <ehabkost@redhat.com> Reviewed-by: David Gibson <david@gibson.dropbear.id.au> Acked-by: David Gibson <david@gibson.dropbear.id.au> Reviewed-by: Marcel Apfelbaum <marcel@redhat.com> Reviewed-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
2017-09-27 22:56:34 +03:00
.interfaces = (InterfaceInfo[]) {
{ INTERFACE_CONVENTIONAL_PCI_DEVICE },
{ },
},
};
static void ppc4xx_pcihost_class_init(ObjectClass *klass, void *data)
{
DeviceClass *dc = DEVICE_CLASS(klass);
dc->realize = ppc4xx_pcihost_realize;
dc->vmsd = &vmstate_ppc4xx_pci;
}
static const TypeInfo ppc4xx_pcihost_info = {
.name = TYPE_PPC4xx_PCI_HOST_BRIDGE,
.parent = TYPE_PCI_HOST_BRIDGE,
.instance_size = sizeof(PPC4xxPCIState),
.class_init = ppc4xx_pcihost_class_init,
};
static void ppc4xx_pci_register_types(void)
{
type_register_static(&ppc4xx_pcihost_info);
type_register_static(&ppc4xx_host_bridge_info);
}
type_init(ppc4xx_pci_register_types)