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 "hw/sysbus.h" 21 #include "monitor/monitor.h" 22 #include "exec/address-spaces.h" 23 24 static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent); 25 static char *sysbus_get_fw_dev_path(DeviceState *dev); 26 27 typedef struct SysBusFind { 28 void *opaque; 29 FindSysbusDeviceFunc *func; 30 } SysBusFind; 31 32 /* Run func() for every sysbus device, traverse the tree for everything else */ 33 static int find_sysbus_device(Object *obj, void *opaque) 34 { 35 SysBusFind *find = opaque; 36 Object *dev; 37 SysBusDevice *sbdev; 38 39 dev = object_dynamic_cast(obj, TYPE_SYS_BUS_DEVICE); 40 sbdev = (SysBusDevice *)dev; 41 42 if (!sbdev) { 43 /* Container, traverse it for children */ 44 return object_child_foreach(obj, find_sysbus_device, opaque); 45 } 46 47 find->func(sbdev, find->opaque); 48 49 return 0; 50 } 51 52 /* 53 * Loop through all dynamically created sysbus devices and call 54 * func() for each instance. 55 */ 56 void foreach_dynamic_sysbus_device(FindSysbusDeviceFunc *func, void *opaque) 57 { 58 Object *container; 59 SysBusFind find = { 60 .func = func, 61 .opaque = opaque, 62 }; 63 64 /* Loop through all sysbus devices that were spawened outside the machine */ 65 container = container_get(qdev_get_machine(), "/peripheral"); 66 find_sysbus_device(container, &find); 67 container = container_get(qdev_get_machine(), "/peripheral-anon"); 68 find_sysbus_device(container, &find); 69 } 70 71 72 static void system_bus_class_init(ObjectClass *klass, void *data) 73 { 74 BusClass *k = BUS_CLASS(klass); 75 76 k->print_dev = sysbus_dev_print; 77 k->get_fw_dev_path = sysbus_get_fw_dev_path; 78 } 79 80 static const TypeInfo system_bus_info = { 81 .name = TYPE_SYSTEM_BUS, 82 .parent = TYPE_BUS, 83 .instance_size = sizeof(BusState), 84 .class_init = system_bus_class_init, 85 }; 86 87 void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq) 88 { 89 qdev_connect_gpio_out_named(DEVICE(dev), SYSBUS_DEVICE_GPIO_IRQ, n, irq); 90 } 91 92 static void sysbus_mmio_map_common(SysBusDevice *dev, int n, hwaddr addr, 93 bool may_overlap, int priority) 94 { 95 assert(n >= 0 && n < dev->num_mmio); 96 97 if (dev->mmio[n].addr == addr) { 98 /* ??? region already mapped here. */ 99 return; 100 } 101 if (dev->mmio[n].addr != (hwaddr)-1) { 102 /* Unregister previous mapping. */ 103 memory_region_del_subregion(get_system_memory(), dev->mmio[n].memory); 104 } 105 dev->mmio[n].addr = addr; 106 if (may_overlap) { 107 memory_region_add_subregion_overlap(get_system_memory(), 108 addr, 109 dev->mmio[n].memory, 110 priority); 111 } 112 else { 113 memory_region_add_subregion(get_system_memory(), 114 addr, 115 dev->mmio[n].memory); 116 } 117 } 118 119 void sysbus_mmio_map(SysBusDevice *dev, int n, hwaddr addr) 120 { 121 sysbus_mmio_map_common(dev, n, addr, false, 0); 122 } 123 124 void sysbus_mmio_map_overlap(SysBusDevice *dev, int n, hwaddr addr, 125 int priority) 126 { 127 sysbus_mmio_map_common(dev, n, addr, true, priority); 128 } 129 130 /* Request an IRQ source. The actual IRQ object may be populated later. */ 131 void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p) 132 { 133 qdev_init_gpio_out_named(DEVICE(dev), p, SYSBUS_DEVICE_GPIO_IRQ, 1); 134 } 135 136 /* Pass IRQs from a target device. */ 137 void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target) 138 { 139 qdev_pass_gpios(DEVICE(target), DEVICE(dev), SYSBUS_DEVICE_GPIO_IRQ); 140 } 141 142 void sysbus_init_mmio(SysBusDevice *dev, MemoryRegion *memory) 143 { 144 int n; 145 146 assert(dev->num_mmio < QDEV_MAX_MMIO); 147 n = dev->num_mmio++; 148 dev->mmio[n].addr = -1; 149 dev->mmio[n].memory = memory; 150 } 151 152 MemoryRegion *sysbus_mmio_get_region(SysBusDevice *dev, int n) 153 { 154 return dev->mmio[n].memory; 155 } 156 157 void sysbus_init_ioports(SysBusDevice *dev, pio_addr_t ioport, pio_addr_t size) 158 { 159 pio_addr_t i; 160 161 for (i = 0; i < size; i++) { 162 assert(dev->num_pio < QDEV_MAX_PIO); 163 dev->pio[dev->num_pio++] = ioport++; 164 } 165 } 166 167 static int sysbus_device_init(DeviceState *dev) 168 { 169 SysBusDevice *sd = SYS_BUS_DEVICE(dev); 170 SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(sd); 171 172 if (!sbc->init) { 173 return 0; 174 } 175 return sbc->init(sd); 176 } 177 178 DeviceState *sysbus_create_varargs(const char *name, 179 hwaddr addr, ...) 180 { 181 DeviceState *dev; 182 SysBusDevice *s; 183 va_list va; 184 qemu_irq irq; 185 int n; 186 187 dev = qdev_create(NULL, name); 188 s = SYS_BUS_DEVICE(dev); 189 qdev_init_nofail(dev); 190 if (addr != (hwaddr)-1) { 191 sysbus_mmio_map(s, 0, addr); 192 } 193 va_start(va, addr); 194 n = 0; 195 while (1) { 196 irq = va_arg(va, qemu_irq); 197 if (!irq) { 198 break; 199 } 200 sysbus_connect_irq(s, n, irq); 201 n++; 202 } 203 va_end(va); 204 return dev; 205 } 206 207 DeviceState *sysbus_try_create_varargs(const char *name, 208 hwaddr addr, ...) 209 { 210 DeviceState *dev; 211 SysBusDevice *s; 212 va_list va; 213 qemu_irq irq; 214 int n; 215 216 dev = qdev_try_create(NULL, name); 217 if (!dev) { 218 return NULL; 219 } 220 s = SYS_BUS_DEVICE(dev); 221 qdev_init_nofail(dev); 222 if (addr != (hwaddr)-1) { 223 sysbus_mmio_map(s, 0, addr); 224 } 225 va_start(va, addr); 226 n = 0; 227 while (1) { 228 irq = va_arg(va, qemu_irq); 229 if (!irq) { 230 break; 231 } 232 sysbus_connect_irq(s, n, irq); 233 n++; 234 } 235 va_end(va); 236 return dev; 237 } 238 239 static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent) 240 { 241 SysBusDevice *s = SYS_BUS_DEVICE(dev); 242 hwaddr size; 243 int i; 244 245 for (i = 0; i < s->num_mmio; i++) { 246 size = memory_region_size(s->mmio[i].memory); 247 monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n", 248 indent, "", s->mmio[i].addr, size); 249 } 250 } 251 252 static char *sysbus_get_fw_dev_path(DeviceState *dev) 253 { 254 SysBusDevice *s = SYS_BUS_DEVICE(dev); 255 char path[40]; 256 int off; 257 258 off = snprintf(path, sizeof(path), "%s", qdev_fw_name(dev)); 259 260 if (s->num_mmio) { 261 snprintf(path + off, sizeof(path) - off, "@"TARGET_FMT_plx, 262 s->mmio[0].addr); 263 } else if (s->num_pio) { 264 snprintf(path + off, sizeof(path) - off, "@i%04x", s->pio[0]); 265 } 266 267 return g_strdup(path); 268 } 269 270 void sysbus_add_io(SysBusDevice *dev, hwaddr addr, 271 MemoryRegion *mem) 272 { 273 memory_region_add_subregion(get_system_io(), addr, mem); 274 } 275 276 MemoryRegion *sysbus_address_space(SysBusDevice *dev) 277 { 278 return get_system_memory(); 279 } 280 281 static void sysbus_device_class_init(ObjectClass *klass, void *data) 282 { 283 DeviceClass *k = DEVICE_CLASS(klass); 284 k->init = sysbus_device_init; 285 k->bus_type = TYPE_SYSTEM_BUS; 286 } 287 288 static const TypeInfo sysbus_device_type_info = { 289 .name = TYPE_SYS_BUS_DEVICE, 290 .parent = TYPE_DEVICE, 291 .instance_size = sizeof(SysBusDevice), 292 .abstract = true, 293 .class_size = sizeof(SysBusDeviceClass), 294 .class_init = sysbus_device_class_init, 295 }; 296 297 /* This is a nasty hack to allow passing a NULL bus to qdev_create. */ 298 static BusState *main_system_bus; 299 300 static void main_system_bus_create(void) 301 { 302 /* assign main_system_bus before qbus_create_inplace() 303 * in order to make "if (bus != sysbus_get_default())" work */ 304 main_system_bus = g_malloc0(system_bus_info.instance_size); 305 qbus_create_inplace(main_system_bus, system_bus_info.instance_size, 306 TYPE_SYSTEM_BUS, NULL, "main-system-bus"); 307 OBJECT(main_system_bus)->free = g_free; 308 object_property_add_child(container_get(qdev_get_machine(), 309 "/unattached"), 310 "sysbus", OBJECT(main_system_bus), NULL); 311 } 312 313 BusState *sysbus_get_default(void) 314 { 315 if (!main_system_bus) { 316 main_system_bus_create(); 317 } 318 return main_system_bus; 319 } 320 321 static void sysbus_register_types(void) 322 { 323 type_register_static(&system_bus_info); 324 type_register_static(&sysbus_device_type_info); 325 } 326 327 type_init(sysbus_register_types) 328