Fix OpenSolaris build breaking typos
[qemu] / hw / sysbus.c
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, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "sysbus.h"
21 #include "sysemu.h"
22 #include "monitor.h"
23
24 static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent);
25
26 struct BusInfo system_bus_info = {
27     .name       = "System",
28     .size       = sizeof(BusState),
29     .print_dev  = sysbus_dev_print,
30 };
31
32 void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq)
33 {
34     assert(n >= 0 && n < dev->num_irq);
35     dev->irqs[n] = NULL;
36     if (dev->irqp[n]) {
37         *dev->irqp[n] = irq;
38     }
39 }
40
41 void sysbus_mmio_map(SysBusDevice *dev, int n, target_phys_addr_t addr)
42 {
43     assert(n >= 0 && n < dev->num_mmio);
44
45     if (dev->mmio[n].addr == addr) {
46         /* ??? region already mapped here.  */
47         return;
48     }
49     if (dev->mmio[n].addr != (target_phys_addr_t)-1) {
50         /* Unregister previous mapping.  */
51         cpu_register_physical_memory(dev->mmio[n].addr, dev->mmio[n].size,
52                                      IO_MEM_UNASSIGNED);
53     }
54     dev->mmio[n].addr = addr;
55     if (dev->mmio[n].cb) {
56         dev->mmio[n].cb(dev, addr);
57     } else {
58         cpu_register_physical_memory(addr, dev->mmio[n].size,
59                                      dev->mmio[n].iofunc);
60     }
61 }
62
63
64 /* Request an IRQ source.  The actual IRQ object may be populated later.  */
65 void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p)
66 {
67     int n;
68
69     assert(dev->num_irq < QDEV_MAX_IRQ);
70     n = dev->num_irq++;
71     dev->irqp[n] = p;
72 }
73
74 /* Pass IRQs from a target device.  */
75 void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target)
76 {
77     int i;
78     assert(dev->num_irq == 0);
79     dev->num_irq = target->num_irq;
80     for (i = 0; i < dev->num_irq; i++) {
81         dev->irqp[i] = target->irqp[i];
82     }
83 }
84
85 void sysbus_init_mmio(SysBusDevice *dev, target_phys_addr_t size, int iofunc)
86 {
87     int n;
88
89     assert(dev->num_mmio < QDEV_MAX_MMIO);
90     n = dev->num_mmio++;
91     dev->mmio[n].addr = -1;
92     dev->mmio[n].size = size;
93     dev->mmio[n].iofunc = iofunc;
94 }
95
96 void sysbus_init_mmio_cb(SysBusDevice *dev, target_phys_addr_t size,
97                          mmio_mapfunc cb)
98 {
99     int n;
100
101     assert(dev->num_mmio < QDEV_MAX_MMIO);
102     n = dev->num_mmio++;
103     dev->mmio[n].addr = -1;
104     dev->mmio[n].size = size;
105     dev->mmio[n].cb = cb;
106 }
107
108 static int sysbus_device_init(DeviceState *dev, DeviceInfo *base)
109 {
110     SysBusDeviceInfo *info = container_of(base, SysBusDeviceInfo, qdev);
111
112     return info->init(sysbus_from_qdev(dev));
113 }
114
115 void sysbus_register_withprop(SysBusDeviceInfo *info)
116 {
117     info->qdev.init = sysbus_device_init;
118     info->qdev.bus_info = &system_bus_info;
119
120     assert(info->qdev.size >= sizeof(SysBusDevice));
121     qdev_register(&info->qdev);
122 }
123
124 void sysbus_register_dev(const char *name, size_t size, sysbus_initfn init)
125 {
126     SysBusDeviceInfo *info;
127
128     info = qemu_mallocz(sizeof(*info));
129     info->qdev.name = qemu_strdup(name);
130     info->qdev.size = size;
131     info->init = init;
132     sysbus_register_withprop(info);
133 }
134
135 DeviceState *sysbus_create_varargs(const char *name,
136                                    target_phys_addr_t addr, ...)
137 {
138     DeviceState *dev;
139     SysBusDevice *s;
140     va_list va;
141     qemu_irq irq;
142     int n;
143
144     dev = qdev_create(NULL, name);
145     s = sysbus_from_qdev(dev);
146     qdev_init(dev);
147     if (addr != (target_phys_addr_t)-1) {
148         sysbus_mmio_map(s, 0, addr);
149     }
150     va_start(va, addr);
151     n = 0;
152     while (1) {
153         irq = va_arg(va, qemu_irq);
154         if (!irq) {
155             break;
156         }
157         sysbus_connect_irq(s, n, irq);
158         n++;
159     }
160     return dev;
161 }
162
163 static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent)
164 {
165     SysBusDevice *s = sysbus_from_qdev(dev);
166     int i;
167
168     for (i = 0; i < s->num_mmio; i++) {
169         monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n",
170                        indent, "", s->mmio[i].addr, s->mmio[i].size);
171     }
172 }