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 /* Check whether an IRQ source exists */ 88 bool sysbus_has_irq(SysBusDevice *dev, int n) 89 { 90 char *prop = g_strdup_printf("%s[%d]", SYSBUS_DEVICE_GPIO_IRQ, n); 91 ObjectProperty *r; 92 93 r = object_property_find(OBJECT(dev), prop, NULL); 94 return (r != NULL); 95 } 96 97 bool sysbus_is_irq_connected(SysBusDevice *dev, int n) 98 { 99 return !!sysbus_get_connected_irq(dev, n); 100 } 101 102 qemu_irq sysbus_get_connected_irq(SysBusDevice *dev, int n) 103 { 104 DeviceState *d = DEVICE(dev); 105 return qdev_get_gpio_out_connector(d, SYSBUS_DEVICE_GPIO_IRQ, n); 106 } 107 108 void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq) 109 { 110 qdev_connect_gpio_out_named(DEVICE(dev), SYSBUS_DEVICE_GPIO_IRQ, n, irq); 111 } 112 113 static void sysbus_mmio_map_common(SysBusDevice *dev, int n, hwaddr addr, 114 bool may_overlap, int priority) 115 { 116 assert(n >= 0 && n < dev->num_mmio); 117 118 if (dev->mmio[n].addr == addr) { 119 /* ??? region already mapped here. */ 120 return; 121 } 122 if (dev->mmio[n].addr != (hwaddr)-1) { 123 /* Unregister previous mapping. */ 124 memory_region_del_subregion(get_system_memory(), dev->mmio[n].memory); 125 } 126 dev->mmio[n].addr = addr; 127 if (may_overlap) { 128 memory_region_add_subregion_overlap(get_system_memory(), 129 addr, 130 dev->mmio[n].memory, 131 priority); 132 } 133 else { 134 memory_region_add_subregion(get_system_memory(), 135 addr, 136 dev->mmio[n].memory); 137 } 138 } 139 140 void sysbus_mmio_map(SysBusDevice *dev, int n, hwaddr addr) 141 { 142 sysbus_mmio_map_common(dev, n, addr, false, 0); 143 } 144 145 void sysbus_mmio_map_overlap(SysBusDevice *dev, int n, hwaddr addr, 146 int priority) 147 { 148 sysbus_mmio_map_common(dev, n, addr, true, priority); 149 } 150 151 /* Request an IRQ source. The actual IRQ object may be populated later. */ 152 void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p) 153 { 154 qdev_init_gpio_out_named(DEVICE(dev), p, SYSBUS_DEVICE_GPIO_IRQ, 1); 155 } 156 157 /* Pass IRQs from a target device. */ 158 void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target) 159 { 160 qdev_pass_gpios(DEVICE(target), DEVICE(dev), SYSBUS_DEVICE_GPIO_IRQ); 161 } 162 163 void sysbus_init_mmio(SysBusDevice *dev, MemoryRegion *memory) 164 { 165 int n; 166 167 assert(dev->num_mmio < QDEV_MAX_MMIO); 168 n = dev->num_mmio++; 169 dev->mmio[n].addr = -1; 170 dev->mmio[n].memory = memory; 171 } 172 173 MemoryRegion *sysbus_mmio_get_region(SysBusDevice *dev, int n) 174 { 175 return dev->mmio[n].memory; 176 } 177 178 void sysbus_init_ioports(SysBusDevice *dev, pio_addr_t ioport, pio_addr_t size) 179 { 180 pio_addr_t i; 181 182 for (i = 0; i < size; i++) { 183 assert(dev->num_pio < QDEV_MAX_PIO); 184 dev->pio[dev->num_pio++] = ioport++; 185 } 186 } 187 188 static int sysbus_device_init(DeviceState *dev) 189 { 190 SysBusDevice *sd = SYS_BUS_DEVICE(dev); 191 SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(sd); 192 193 if (!sbc->init) { 194 return 0; 195 } 196 return sbc->init(sd); 197 } 198 199 DeviceState *sysbus_create_varargs(const char *name, 200 hwaddr addr, ...) 201 { 202 DeviceState *dev; 203 SysBusDevice *s; 204 va_list va; 205 qemu_irq irq; 206 int n; 207 208 dev = qdev_create(NULL, name); 209 s = SYS_BUS_DEVICE(dev); 210 qdev_init_nofail(dev); 211 if (addr != (hwaddr)-1) { 212 sysbus_mmio_map(s, 0, addr); 213 } 214 va_start(va, addr); 215 n = 0; 216 while (1) { 217 irq = va_arg(va, qemu_irq); 218 if (!irq) { 219 break; 220 } 221 sysbus_connect_irq(s, n, irq); 222 n++; 223 } 224 va_end(va); 225 return dev; 226 } 227 228 DeviceState *sysbus_try_create_varargs(const char *name, 229 hwaddr addr, ...) 230 { 231 DeviceState *dev; 232 SysBusDevice *s; 233 va_list va; 234 qemu_irq irq; 235 int n; 236 237 dev = qdev_try_create(NULL, name); 238 if (!dev) { 239 return NULL; 240 } 241 s = SYS_BUS_DEVICE(dev); 242 qdev_init_nofail(dev); 243 if (addr != (hwaddr)-1) { 244 sysbus_mmio_map(s, 0, addr); 245 } 246 va_start(va, addr); 247 n = 0; 248 while (1) { 249 irq = va_arg(va, qemu_irq); 250 if (!irq) { 251 break; 252 } 253 sysbus_connect_irq(s, n, irq); 254 n++; 255 } 256 va_end(va); 257 return dev; 258 } 259 260 static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent) 261 { 262 SysBusDevice *s = SYS_BUS_DEVICE(dev); 263 hwaddr size; 264 int i; 265 266 for (i = 0; i < s->num_mmio; i++) { 267 size = memory_region_size(s->mmio[i].memory); 268 monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n", 269 indent, "", s->mmio[i].addr, size); 270 } 271 } 272 273 static char *sysbus_get_fw_dev_path(DeviceState *dev) 274 { 275 SysBusDevice *s = SYS_BUS_DEVICE(dev); 276 char path[40]; 277 int off; 278 279 off = snprintf(path, sizeof(path), "%s", qdev_fw_name(dev)); 280 281 if (s->num_mmio) { 282 snprintf(path + off, sizeof(path) - off, "@"TARGET_FMT_plx, 283 s->mmio[0].addr); 284 } else if (s->num_pio) { 285 snprintf(path + off, sizeof(path) - off, "@i%04x", s->pio[0]); 286 } 287 288 return g_strdup(path); 289 } 290 291 void sysbus_add_io(SysBusDevice *dev, hwaddr addr, 292 MemoryRegion *mem) 293 { 294 memory_region_add_subregion(get_system_io(), addr, mem); 295 } 296 297 MemoryRegion *sysbus_address_space(SysBusDevice *dev) 298 { 299 return get_system_memory(); 300 } 301 302 static void sysbus_device_class_init(ObjectClass *klass, void *data) 303 { 304 DeviceClass *k = DEVICE_CLASS(klass); 305 k->init = sysbus_device_init; 306 k->bus_type = TYPE_SYSTEM_BUS; 307 } 308 309 static const TypeInfo sysbus_device_type_info = { 310 .name = TYPE_SYS_BUS_DEVICE, 311 .parent = TYPE_DEVICE, 312 .instance_size = sizeof(SysBusDevice), 313 .abstract = true, 314 .class_size = sizeof(SysBusDeviceClass), 315 .class_init = sysbus_device_class_init, 316 }; 317 318 /* This is a nasty hack to allow passing a NULL bus to qdev_create. */ 319 static BusState *main_system_bus; 320 321 static void main_system_bus_create(void) 322 { 323 /* assign main_system_bus before qbus_create_inplace() 324 * in order to make "if (bus != sysbus_get_default())" work */ 325 main_system_bus = g_malloc0(system_bus_info.instance_size); 326 qbus_create_inplace(main_system_bus, system_bus_info.instance_size, 327 TYPE_SYSTEM_BUS, NULL, "main-system-bus"); 328 OBJECT(main_system_bus)->free = g_free; 329 object_property_add_child(container_get(qdev_get_machine(), 330 "/unattached"), 331 "sysbus", OBJECT(main_system_bus), NULL); 332 } 333 334 BusState *sysbus_get_default(void) 335 { 336 if (!main_system_bus) { 337 main_system_bus_create(); 338 } 339 return main_system_bus; 340 } 341 342 static void sysbus_register_types(void) 343 { 344 type_register_static(&system_bus_info); 345 type_register_static(&sysbus_device_type_info); 346 } 347 348 type_init(sysbus_register_types) 349