1/*
2 *  System (CPU) Bus device support code
3 *
4 *  Copyright (c) 2009 CodeSourcery
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
19 */
20
21#include "exec/cpu-common.h"
22#include "hw/sysbus.h"
23#include "sysemu/sysemu.h"
24#include "monitor/monitor.h"
25
26void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq)
27{
28    assert(n >= 0 && n < dev->num_irq);
29    dev->irqs[n] = 0;
30    if (dev->irqp[n]) {
31        *dev->irqp[n] = irq;
32    }
33}
34
35void sysbus_mmio_map(SysBusDevice *dev, int n, hwaddr addr)
36{
37    assert(n >= 0 && n < dev->num_mmio);
38
39    if (dev->mmio[n].addr == addr) {
40        /* ??? region already mapped here.  */
41        return;
42    }
43    if (dev->mmio[n].addr != (hwaddr)-1) {
44        /* Unregister previous mapping.  */
45        cpu_register_physical_memory(dev->mmio[n].addr, dev->mmio[n].size,
46                                     IO_MEM_UNASSIGNED);
47    }
48    dev->mmio[n].addr = addr;
49    if (dev->mmio[n].cb) {
50        dev->mmio[n].cb(dev, addr);
51    } else {
52        cpu_register_physical_memory(addr, dev->mmio[n].size,
53                                     dev->mmio[n].iofunc);
54    }
55}
56
57
58/* Request an IRQ source.  The actual IRQ object may be populated later.  */
59void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
60{
61    int n;
62
63    assert(dev->num_irq < QDEV_MAX_IRQ);
64    n = dev->num_irq++;
65    dev->irqp[n] = p;
66}
67
68/* Pass IRQs from a target device.  */
69void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
70{
71    int i;
72    assert(dev->num_irq == 0);
73    dev->num_irq = target->num_irq;
74    for (i = 0; i < dev->num_irq; i++) {
75        dev->irqp[i] = target->irqp[i];
76    }
77}
78
79void sysbus_init_mmio(SysBusDevice *dev, hwaddr size, int iofunc)
80{
81    int n;
82
83    assert(dev->num_mmio < QDEV_MAX_MMIO);
84    n = dev->num_mmio++;
85    dev->mmio[n].addr = -1;
86    dev->mmio[n].size = size;
87    dev->mmio[n].iofunc = iofunc;
88}
89
90void sysbus_init_mmio_cb(SysBusDevice *dev, hwaddr size,
91                         mmio_mapfunc cb)
92{
93    int n;
94
95    assert(dev->num_mmio < QDEV_MAX_MMIO);
96    n = dev->num_mmio++;
97    dev->mmio[n].addr = -1;
98    dev->mmio[n].size = size;
99    dev->mmio[n].cb = cb;
100}
101
102static void sysbus_device_init(DeviceState *dev, DeviceInfo *base)
103{
104    SysBusDeviceInfo *info = container_of(base, SysBusDeviceInfo, qdev);
105
106    info->init(sysbus_from_qdev(dev));
107}
108
109void sysbus_register_withprop(SysBusDeviceInfo *info)
110{
111    info->qdev.init = sysbus_device_init;
112    info->qdev.bus_type = BUS_TYPE_SYSTEM;
113
114    assert(info->qdev.size >= sizeof(SysBusDevice));
115    qdev_register(&info->qdev);
116}
117
118void sysbus_register_dev(const char *name, size_t size, sysbus_initfn init)
119{
120    SysBusDeviceInfo *info;
121
122    info = g_malloc0(sizeof(*info));
123    info->qdev.name = g_strdup(name);
124    info->qdev.size = size;
125    info->init = init;
126    sysbus_register_withprop(info);
127}
128
129DeviceState *sysbus_create_varargs(const char *name,
130                                   hwaddr addr, ...)
131{
132    DeviceState *dev;
133    SysBusDevice *s;
134    va_list va;
135    qemu_irq irq;
136    int n;
137
138    dev = qdev_create(NULL, name);
139    s = sysbus_from_qdev(dev);
140    qdev_init(dev);
141    if (addr != (hwaddr)-1) {
142        sysbus_mmio_map(s, 0, addr);
143    }
144    va_start(va, addr);
145    n = 0;
146    while (1) {
147        irq = va_arg(va, qemu_irq);
148        if (!irq) {
149            break;
150        }
151        sysbus_connect_irq(s, n, irq);
152        n++;
153    }
154    return dev;
155}
156
157void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
158{
159    SysBusDevice *s = sysbus_from_qdev(dev);
160    int i;
161
162    for (i = 0; i < s->num_mmio; i++) {
163        monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n",
164                       indent, "", s->mmio[i].addr, s->mmio[i].size);
165    }
166}
167