2009-05-15 01:35:06 +04: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-17 00:47:01 +04:00
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
2009-05-15 01:35:06 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sysbus.h"
|
2009-06-05 18:53:17 +04:00
|
|
|
#include "monitor.h"
|
2011-07-26 15:26:21 +04:00
|
|
|
#include "exec-memory.h"
|
2009-05-15 01:35:06 +04:00
|
|
|
|
2009-06-30 16:12:08 +04:00
|
|
|
static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent);
|
2010-12-08 14:35:00 +03:00
|
|
|
static char *sysbus_get_fw_dev_path(DeviceState *dev);
|
2009-06-30 16:12:08 +04:00
|
|
|
|
|
|
|
struct BusInfo system_bus_info = {
|
|
|
|
.name = "System",
|
|
|
|
.size = sizeof(BusState),
|
|
|
|
.print_dev = sysbus_dev_print,
|
2010-12-08 14:35:00 +03:00
|
|
|
.get_fw_dev_path = sysbus_get_fw_dev_path,
|
2009-06-30 16:12:08 +04:00
|
|
|
};
|
|
|
|
|
2009-05-15 01:35:06 +04:00
|
|
|
void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq)
|
|
|
|
{
|
|
|
|
assert(n >= 0 && n < dev->num_irq);
|
2009-08-01 01:16:51 +04:00
|
|
|
dev->irqs[n] = NULL;
|
2009-05-15 01:35:06 +04:00
|
|
|
if (dev->irqp[n]) {
|
|
|
|
*dev->irqp[n] = irq;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-02 01:12:16 +04:00
|
|
|
void sysbus_mmio_map(SysBusDevice *dev, int n, target_phys_addr_t addr)
|
2009-05-15 01:35:06 +04:00
|
|
|
{
|
|
|
|
assert(n >= 0 && n < dev->num_mmio);
|
|
|
|
|
|
|
|
if (dev->mmio[n].addr == addr) {
|
|
|
|
/* ??? region already mapped here. */
|
|
|
|
return;
|
|
|
|
}
|
2009-10-02 01:12:16 +04:00
|
|
|
if (dev->mmio[n].addr != (target_phys_addr_t)-1) {
|
2009-05-15 01:35:06 +04:00
|
|
|
/* Unregister previous mapping. */
|
2011-12-20 00:33:59 +04:00
|
|
|
memory_region_del_subregion(get_system_memory(), dev->mmio[n].memory);
|
2009-05-15 01:35:06 +04:00
|
|
|
}
|
|
|
|
dev->mmio[n].addr = addr;
|
2011-12-20 00:33:59 +04:00
|
|
|
memory_region_add_subregion(get_system_memory(),
|
|
|
|
addr,
|
|
|
|
dev->mmio[n].memory);
|
2009-05-15 01:35:06 +04: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 13:38:10 +04:00
|
|
|
void sysbus_init_mmio(SysBusDevice *dev, MemoryRegion *memory)
|
2011-07-26 15:26:21 +04: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 20:22:17 +04:00
|
|
|
MemoryRegion *sysbus_mmio_get_region(SysBusDevice *dev, int n)
|
|
|
|
{
|
|
|
|
return dev->mmio[n].memory;
|
|
|
|
}
|
|
|
|
|
2010-12-08 14:35:00 +03: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++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-14 12:36:05 +04:00
|
|
|
static int sysbus_device_init(DeviceState *dev, DeviceInfo *base)
|
2009-05-15 01:35:06 +04:00
|
|
|
{
|
2009-05-23 03:05:19 +04:00
|
|
|
SysBusDeviceInfo *info = container_of(base, SysBusDeviceInfo, qdev);
|
2009-05-15 01:35:06 +04:00
|
|
|
|
2009-08-14 12:36:05 +04:00
|
|
|
return info->init(sysbus_from_qdev(dev));
|
2009-05-15 01:35:06 +04:00
|
|
|
}
|
|
|
|
|
2009-06-10 11:41:42 +04:00
|
|
|
void sysbus_register_withprop(SysBusDeviceInfo *info)
|
2009-05-15 01:35:06 +04:00
|
|
|
{
|
2009-05-23 03:05:19 +04:00
|
|
|
info->qdev.init = sysbus_device_init;
|
2009-06-30 16:12:08 +04:00
|
|
|
info->qdev.bus_info = &system_bus_info;
|
2009-05-23 03:05:19 +04:00
|
|
|
|
2009-06-10 11:41:42 +04:00
|
|
|
assert(info->qdev.size >= sizeof(SysBusDevice));
|
|
|
|
qdev_register(&info->qdev);
|
2009-05-15 01:35:06 +04:00
|
|
|
}
|
|
|
|
|
2009-06-05 18:52:04 +04:00
|
|
|
void sysbus_register_dev(const char *name, size_t size, sysbus_initfn init)
|
|
|
|
{
|
|
|
|
SysBusDeviceInfo *info;
|
|
|
|
|
2011-08-21 07:09:37 +04:00
|
|
|
info = g_malloc0(sizeof(*info));
|
|
|
|
info->qdev.name = g_strdup(name);
|
2009-06-10 11:41:42 +04:00
|
|
|
info->qdev.size = size;
|
2009-06-05 18:52:04 +04:00
|
|
|
info->init = init;
|
2009-06-10 11:41:42 +04:00
|
|
|
sysbus_register_withprop(info);
|
2009-06-05 18:52:04 +04:00
|
|
|
}
|
|
|
|
|
2009-05-15 01:35:06 +04:00
|
|
|
DeviceState *sysbus_create_varargs(const char *name,
|
2009-10-02 01:12:16 +04:00
|
|
|
target_phys_addr_t addr, ...)
|
2009-05-15 01:35:06 +04:00
|
|
|
{
|
|
|
|
DeviceState *dev;
|
|
|
|
SysBusDevice *s;
|
|
|
|
va_list va;
|
|
|
|
qemu_irq irq;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
dev = qdev_create(NULL, name);
|
|
|
|
s = sysbus_from_qdev(dev);
|
2009-10-07 03:15:58 +04:00
|
|
|
qdev_init_nofail(dev);
|
2009-10-02 01:12:16 +04:00
|
|
|
if (addr != (target_phys_addr_t)-1) {
|
2009-05-15 01:35:06 +04:00
|
|
|
sysbus_mmio_map(s, 0, addr);
|
|
|
|
}
|
|
|
|
va_start(va, addr);
|
|
|
|
n = 0;
|
|
|
|
while (1) {
|
2011-02-05 17:34:56 +03:00
|
|
|
irq = va_arg(va, qemu_irq);
|
|
|
|
if (!irq) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sysbus_connect_irq(s, n, irq);
|
|
|
|
n++;
|
|
|
|
}
|
2011-10-28 12:52:25 +04:00
|
|
|
va_end(va);
|
2011-02-05 17:34:56 +03:00
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceState *sysbus_try_create_varargs(const char *name,
|
|
|
|
target_phys_addr_t addr, ...)
|
|
|
|
{
|
|
|
|
DeviceState *dev;
|
|
|
|
SysBusDevice *s;
|
|
|
|
va_list va;
|
|
|
|
qemu_irq irq;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
dev = qdev_try_create(NULL, name);
|
|
|
|
if (!dev) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
s = sysbus_from_qdev(dev);
|
|
|
|
qdev_init_nofail(dev);
|
|
|
|
if (addr != (target_phys_addr_t)-1) {
|
|
|
|
sysbus_mmio_map(s, 0, addr);
|
|
|
|
}
|
|
|
|
va_start(va, addr);
|
|
|
|
n = 0;
|
|
|
|
while (1) {
|
2009-05-15 01:35:06 +04:00
|
|
|
irq = va_arg(va, qemu_irq);
|
|
|
|
if (!irq) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sysbus_connect_irq(s, n, irq);
|
|
|
|
n++;
|
|
|
|
}
|
2011-10-28 12:52:25 +04:00
|
|
|
va_end(va);
|
2009-05-15 01:35:06 +04:00
|
|
|
return dev;
|
|
|
|
}
|
2009-06-05 18:53:17 +04:00
|
|
|
|
2009-06-30 16:12:08 +04:00
|
|
|
static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
|
2009-06-05 18:53:17 +04:00
|
|
|
{
|
|
|
|
SysBusDevice *s = sysbus_from_qdev(dev);
|
2011-11-27 13:32:34 +04:00
|
|
|
target_phys_addr_t size;
|
2009-06-05 18:53:17 +04:00
|
|
|
int i;
|
|
|
|
|
2011-01-31 18:20:41 +03:00
|
|
|
monitor_printf(mon, "%*sirq %d\n", indent, "", s->num_irq);
|
2009-06-05 18:53:17 +04:00
|
|
|
for (i = 0; i < s->num_mmio; i++) {
|
2011-12-20 00:33:59 +04:00
|
|
|
size = memory_region_size(s->mmio[i].memory);
|
2009-06-05 18:53:17 +04:00
|
|
|
monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n",
|
2011-11-27 13:32:34 +04:00
|
|
|
indent, "", s->mmio[i].addr, size);
|
2009-06-05 18:53:17 +04:00
|
|
|
}
|
|
|
|
}
|
2010-12-08 14:35:00 +03:00
|
|
|
|
|
|
|
static char *sysbus_get_fw_dev_path(DeviceState *dev)
|
|
|
|
{
|
|
|
|
SysBusDevice *s = sysbus_from_qdev(dev);
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return strdup(path);
|
|
|
|
}
|
2011-07-24 18:12:11 +04:00
|
|
|
|
|
|
|
void sysbus_add_memory(SysBusDevice *dev, target_phys_addr_t addr,
|
|
|
|
MemoryRegion *mem)
|
|
|
|
{
|
|
|
|
memory_region_add_subregion(get_system_memory(), addr, mem);
|
|
|
|
}
|
|
|
|
|
2011-07-25 16:02:17 +04:00
|
|
|
void sysbus_add_memory_overlap(SysBusDevice *dev, target_phys_addr_t addr,
|
|
|
|
MemoryRegion *mem, unsigned priority)
|
|
|
|
{
|
|
|
|
memory_region_add_subregion_overlap(get_system_memory(), addr, mem,
|
|
|
|
priority);
|
|
|
|
}
|
|
|
|
|
2011-07-24 18:12:11 +04:00
|
|
|
void sysbus_del_memory(SysBusDevice *dev, MemoryRegion *mem)
|
|
|
|
{
|
|
|
|
memory_region_del_subregion(get_system_memory(), mem);
|
|
|
|
}
|
|
|
|
|
|
|
|
void sysbus_add_io(SysBusDevice *dev, target_phys_addr_t addr,
|
|
|
|
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 17:57:34 +04:00
|
|
|
|
|
|
|
MemoryRegion *sysbus_address_space(SysBusDevice *dev)
|
|
|
|
{
|
|
|
|
return get_system_memory();
|
|
|
|
}
|