2009-05-14 23:35:06 +02:00
|
|
|
/*
|
|
|
|
* System (CPU) Bus device support code
|
|
|
|
*
|
|
|
|
* Copyright (c) 2009 CodeSourcery
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2009-07-16 22:47:01 +02:00
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
2009-05-14 23:35:06 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sysbus.h"
|
2012-12-17 18:19:49 +01:00
|
|
|
#include "monitor/monitor.h"
|
2012-12-17 18:19:49 +01:00
|
|
|
#include "exec/address-spaces.h"
|
2009-05-14 23:35:06 +02:00
|
|
|
|
2009-06-30 14:12:08 +02:00
|
|
|
static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent);
|
2010-12-08 12:35:00 +01:00
|
|
|
static char *sysbus_get_fw_dev_path(DeviceState *dev);
|
2009-06-30 14:12:08 +02:00
|
|
|
|
2012-05-02 09:00:20 +02:00
|
|
|
static void system_bus_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
BusClass *k = BUS_CLASS(klass);
|
|
|
|
|
|
|
|
k->print_dev = sysbus_dev_print;
|
|
|
|
k->get_fw_dev_path = sysbus_get_fw_dev_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo system_bus_info = {
|
|
|
|
.name = TYPE_SYSTEM_BUS,
|
|
|
|
.parent = TYPE_BUS,
|
|
|
|
.instance_size = sizeof(BusState),
|
|
|
|
.class_init = system_bus_class_init,
|
2009-06-30 14:12:08 +02:00
|
|
|
};
|
|
|
|
|
2009-05-14 23:35:06 +02:00
|
|
|
void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq)
|
|
|
|
{
|
|
|
|
assert(n >= 0 && n < dev->num_irq);
|
2009-07-31 23:16:51 +02:00
|
|
|
dev->irqs[n] = NULL;
|
2009-05-14 23:35:06 +02:00
|
|
|
if (dev->irqp[n]) {
|
|
|
|
*dev->irqp[n] = irq;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-23 12:30:10 +02:00
|
|
|
void sysbus_mmio_map(SysBusDevice *dev, int n, hwaddr addr)
|
2009-05-14 23:35:06 +02:00
|
|
|
{
|
|
|
|
assert(n >= 0 && n < dev->num_mmio);
|
|
|
|
|
|
|
|
if (dev->mmio[n].addr == addr) {
|
|
|
|
/* ??? region already mapped here. */
|
|
|
|
return;
|
|
|
|
}
|
2012-10-23 12:30:10 +02:00
|
|
|
if (dev->mmio[n].addr != (hwaddr)-1) {
|
2009-05-14 23:35:06 +02:00
|
|
|
/* Unregister previous mapping. */
|
2011-12-19 21:33:59 +01:00
|
|
|
memory_region_del_subregion(get_system_memory(), dev->mmio[n].memory);
|
2009-05-14 23:35:06 +02:00
|
|
|
}
|
|
|
|
dev->mmio[n].addr = addr;
|
2011-12-19 21:33:59 +01:00
|
|
|
memory_region_add_subregion(get_system_memory(),
|
|
|
|
addr,
|
|
|
|
dev->mmio[n].memory);
|
2009-05-14 23:35:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Request an IRQ source. The actual IRQ object may be populated later. */
|
|
|
|
void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
assert(dev->num_irq < QDEV_MAX_IRQ);
|
|
|
|
n = dev->num_irq++;
|
|
|
|
dev->irqp[n] = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Pass IRQs from a target device. */
|
|
|
|
void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
assert(dev->num_irq == 0);
|
|
|
|
dev->num_irq = target->num_irq;
|
|
|
|
for (i = 0; i < dev->num_irq; i++) {
|
|
|
|
dev->irqp[i] = target->irqp[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-27 10:38:10 +01:00
|
|
|
void sysbus_init_mmio(SysBusDevice *dev, MemoryRegion *memory)
|
2011-07-26 13:26:21 +02:00
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
assert(dev->num_mmio < QDEV_MAX_MMIO);
|
|
|
|
n = dev->num_mmio++;
|
|
|
|
dev->mmio[n].addr = -1;
|
|
|
|
dev->mmio[n].memory = memory;
|
|
|
|
}
|
|
|
|
|
2011-08-28 18:22:17 +02:00
|
|
|
MemoryRegion *sysbus_mmio_get_region(SysBusDevice *dev, int n)
|
|
|
|
{
|
|
|
|
return dev->mmio[n].memory;
|
|
|
|
}
|
|
|
|
|
2010-12-08 12:35:00 +01:00
|
|
|
void sysbus_init_ioports(SysBusDevice *dev, pio_addr_t ioport, pio_addr_t size)
|
|
|
|
{
|
|
|
|
pio_addr_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
assert(dev->num_pio < QDEV_MAX_PIO);
|
|
|
|
dev->pio[dev->num_pio++] = ioport++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-09 22:02:56 +01:00
|
|
|
static int sysbus_device_init(DeviceState *dev)
|
2009-05-14 23:35:06 +02:00
|
|
|
{
|
2012-01-24 20:12:29 +01:00
|
|
|
SysBusDevice *sd = SYS_BUS_DEVICE(dev);
|
|
|
|
SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(sd);
|
2009-05-14 23:35:06 +02:00
|
|
|
|
2012-01-24 20:12:29 +01:00
|
|
|
return sbc->init(sd);
|
2009-05-14 23:35:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DeviceState *sysbus_create_varargs(const char *name,
|
2012-10-23 12:30:10 +02:00
|
|
|
hwaddr addr, ...)
|
2009-05-14 23:35:06 +02:00
|
|
|
{
|
|
|
|
DeviceState *dev;
|
|
|
|
SysBusDevice *s;
|
|
|
|
va_list va;
|
|
|
|
qemu_irq irq;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
dev = qdev_create(NULL, name);
|
2013-01-20 02:47:33 +01:00
|
|
|
s = SYS_BUS_DEVICE(dev);
|
2009-10-07 01:15:58 +02:00
|
|
|
qdev_init_nofail(dev);
|
2012-10-23 12:30:10 +02:00
|
|
|
if (addr != (hwaddr)-1) {
|
2009-05-14 23:35:06 +02:00
|
|
|
sysbus_mmio_map(s, 0, addr);
|
|
|
|
}
|
|
|
|
va_start(va, addr);
|
|
|
|
n = 0;
|
|
|
|
while (1) {
|
2011-02-05 15:34:56 +01:00
|
|
|
irq = va_arg(va, qemu_irq);
|
|
|
|
if (!irq) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sysbus_connect_irq(s, n, irq);
|
|
|
|
n++;
|
|
|
|
}
|
2011-10-28 10:52:25 +02:00
|
|
|
va_end(va);
|
2011-02-05 15:34:56 +01:00
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceState *sysbus_try_create_varargs(const char *name,
|
2012-10-23 12:30:10 +02:00
|
|
|
hwaddr addr, ...)
|
2011-02-05 15:34:56 +01:00
|
|
|
{
|
|
|
|
DeviceState *dev;
|
|
|
|
SysBusDevice *s;
|
|
|
|
va_list va;
|
|
|
|
qemu_irq irq;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
dev = qdev_try_create(NULL, name);
|
|
|
|
if (!dev) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-01-20 02:47:33 +01:00
|
|
|
s = SYS_BUS_DEVICE(dev);
|
2011-02-05 15:34:56 +01:00
|
|
|
qdev_init_nofail(dev);
|
2012-10-23 12:30:10 +02:00
|
|
|
if (addr != (hwaddr)-1) {
|
2011-02-05 15:34:56 +01:00
|
|
|
sysbus_mmio_map(s, 0, addr);
|
|
|
|
}
|
|
|
|
va_start(va, addr);
|
|
|
|
n = 0;
|
|
|
|
while (1) {
|
2009-05-14 23:35:06 +02:00
|
|
|
irq = va_arg(va, qemu_irq);
|
|
|
|
if (!irq) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sysbus_connect_irq(s, n, irq);
|
|
|
|
n++;
|
|
|
|
}
|
2011-10-28 10:52:25 +02:00
|
|
|
va_end(va);
|
2009-05-14 23:35:06 +02:00
|
|
|
return dev;
|
|
|
|
}
|
2009-06-05 16:53:17 +02:00
|
|
|
|
2009-06-30 14:12:08 +02:00
|
|
|
static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
|
2009-06-05 16:53:17 +02:00
|
|
|
{
|
2013-01-20 02:47:33 +01:00
|
|
|
SysBusDevice *s = SYS_BUS_DEVICE(dev);
|
2012-10-23 12:30:10 +02:00
|
|
|
hwaddr size;
|
2009-06-05 16:53:17 +02:00
|
|
|
int i;
|
|
|
|
|
2011-01-31 16:20:41 +01:00
|
|
|
monitor_printf(mon, "%*sirq %d\n", indent, "", s->num_irq);
|
2009-06-05 16:53:17 +02:00
|
|
|
for (i = 0; i < s->num_mmio; i++) {
|
2011-12-19 21:33:59 +01:00
|
|
|
size = memory_region_size(s->mmio[i].memory);
|
2009-06-05 16:53:17 +02:00
|
|
|
monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n",
|
2011-11-27 10:32:34 +01:00
|
|
|
indent, "", s->mmio[i].addr, size);
|
2009-06-05 16:53:17 +02:00
|
|
|
}
|
|
|
|
}
|
2010-12-08 12:35:00 +01:00
|
|
|
|
|
|
|
static char *sysbus_get_fw_dev_path(DeviceState *dev)
|
|
|
|
{
|
2013-01-20 02:47:33 +01:00
|
|
|
SysBusDevice *s = SYS_BUS_DEVICE(dev);
|
2010-12-08 12:35:00 +01:00
|
|
|
char path[40];
|
|
|
|
int off;
|
|
|
|
|
|
|
|
off = snprintf(path, sizeof(path), "%s", qdev_fw_name(dev));
|
|
|
|
|
|
|
|
if (s->num_mmio) {
|
|
|
|
snprintf(path + off, sizeof(path) - off, "@"TARGET_FMT_plx,
|
|
|
|
s->mmio[0].addr);
|
|
|
|
} else if (s->num_pio) {
|
|
|
|
snprintf(path + off, sizeof(path) - off, "@i%04x", s->pio[0]);
|
|
|
|
}
|
|
|
|
|
scsi, pci, qdev, isa-bus, sysbus: don't let *_get_fw_dev_path return NULL
Use g_strdup rather than strdup, because the sole caller
(qdev_get_fw_dev_path_helper) assumes it gets non-NULL, and dereferences
it. Besides, in that caller, the allocated buffer is already freed with
g_free, so it's better to allocate with a matching g_strdup.
In one case, (scsi-bus.c) it was trivial, so I replaced an snprintf+
g_strdup combination with an equivalent g_strdup_printf use.
Signed-off-by: Jim Meyering <meyering@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
2012-10-04 13:09:44 +02:00
|
|
|
return g_strdup(path);
|
2010-12-08 12:35:00 +01:00
|
|
|
}
|
2011-07-24 16:12:11 +02:00
|
|
|
|
2012-10-23 12:30:10 +02:00
|
|
|
void sysbus_add_memory(SysBusDevice *dev, hwaddr addr,
|
2011-07-24 16:12:11 +02:00
|
|
|
MemoryRegion *mem)
|
|
|
|
{
|
|
|
|
memory_region_add_subregion(get_system_memory(), addr, mem);
|
|
|
|
}
|
|
|
|
|
2012-10-23 12:30:10 +02:00
|
|
|
void sysbus_add_memory_overlap(SysBusDevice *dev, hwaddr addr,
|
2011-07-25 14:02:17 +02:00
|
|
|
MemoryRegion *mem, unsigned priority)
|
|
|
|
{
|
|
|
|
memory_region_add_subregion_overlap(get_system_memory(), addr, mem,
|
|
|
|
priority);
|
|
|
|
}
|
|
|
|
|
2011-07-24 16:12:11 +02:00
|
|
|
void sysbus_del_memory(SysBusDevice *dev, MemoryRegion *mem)
|
|
|
|
{
|
|
|
|
memory_region_del_subregion(get_system_memory(), mem);
|
|
|
|
}
|
|
|
|
|
2012-10-23 12:30:10 +02:00
|
|
|
void sysbus_add_io(SysBusDevice *dev, hwaddr addr,
|
2011-07-24 16:12:11 +02:00
|
|
|
MemoryRegion *mem)
|
|
|
|
{
|
|
|
|
memory_region_add_subregion(get_system_io(), addr, mem);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sysbus_del_io(SysBusDevice *dev, MemoryRegion *mem)
|
|
|
|
{
|
|
|
|
memory_region_del_subregion(get_system_io(), mem);
|
|
|
|
}
|
2011-12-08 14:57:34 +01:00
|
|
|
|
|
|
|
MemoryRegion *sysbus_address_space(SysBusDevice *dev)
|
|
|
|
{
|
|
|
|
return get_system_memory();
|
|
|
|
}
|
2012-01-24 20:12:29 +01:00
|
|
|
|
2011-12-08 04:34:16 +01:00
|
|
|
static void sysbus_device_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *k = DEVICE_CLASS(klass);
|
|
|
|
k->init = sysbus_device_init;
|
2012-05-02 09:00:20 +02:00
|
|
|
k->bus_type = TYPE_SYSTEM_BUS;
|
2011-12-08 04:34:16 +01:00
|
|
|
}
|
|
|
|
|
2013-01-10 16:19:07 +01:00
|
|
|
static const TypeInfo sysbus_device_type_info = {
|
2012-01-24 20:12:29 +01:00
|
|
|
.name = TYPE_SYS_BUS_DEVICE,
|
|
|
|
.parent = TYPE_DEVICE,
|
|
|
|
.instance_size = sizeof(SysBusDevice),
|
|
|
|
.abstract = true,
|
|
|
|
.class_size = sizeof(SysBusDeviceClass),
|
2011-12-08 04:34:16 +01:00
|
|
|
.class_init = sysbus_device_class_init,
|
2012-01-24 20:12:29 +01:00
|
|
|
};
|
|
|
|
|
2012-05-02 12:06:55 +02:00
|
|
|
/* This is a nasty hack to allow passing a NULL bus to qdev_create. */
|
|
|
|
static BusState *main_system_bus;
|
|
|
|
|
|
|
|
static void main_system_bus_create(void)
|
|
|
|
{
|
|
|
|
/* assign main_system_bus before qbus_create_inplace()
|
|
|
|
* in order to make "if (bus != sysbus_get_default())" work */
|
2012-05-02 09:00:20 +02:00
|
|
|
main_system_bus = g_malloc0(system_bus_info.instance_size);
|
|
|
|
qbus_create_inplace(main_system_bus, TYPE_SYSTEM_BUS, NULL,
|
2012-05-02 12:06:55 +02:00
|
|
|
"main-system-bus");
|
2012-11-23 09:47:15 +01:00
|
|
|
OBJECT(main_system_bus)->free = g_free;
|
2012-05-02 10:39:01 +02:00
|
|
|
object_property_add_child(container_get(qdev_get_machine(),
|
|
|
|
"/unattached"),
|
|
|
|
"sysbus", OBJECT(main_system_bus), NULL);
|
2012-05-02 12:06:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BusState *sysbus_get_default(void)
|
|
|
|
{
|
|
|
|
if (!main_system_bus) {
|
|
|
|
main_system_bus_create();
|
|
|
|
}
|
|
|
|
return main_system_bus;
|
|
|
|
}
|
|
|
|
|
2012-02-09 15:20:55 +01:00
|
|
|
static void sysbus_register_types(void)
|
2012-01-24 20:12:29 +01:00
|
|
|
{
|
2012-05-02 09:00:20 +02:00
|
|
|
type_register_static(&system_bus_info);
|
2012-01-24 20:12:29 +01:00
|
|
|
type_register_static(&sysbus_device_type_info);
|
|
|
|
}
|
|
|
|
|
2012-02-09 15:20:55 +01:00
|
|
|
type_init(sysbus_register_types)
|