| 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.1 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 "qemu/osdep.h" |
| 21 | #include "qapi/error.h" |
| 22 | #include "hw/core/sysbus.h" |
| 23 | #include "monitor/monitor.h" |
| 24 | #include "monitor/hmp.h" |
| 25 | #include "system/address-spaces.h" |
| 26 | |
| 27 | #ifdef CONFIG_HMP |
| 28 | static void sysbus_dev_print(MonitorHMP *hmp, DeviceState *dev, int indent); |
| 29 | #endif |
| 30 | static char *sysbus_get_fw_dev_path(DeviceState *dev); |
| 31 | |
| 32 | typedef struct SysBusFind { |
| 33 | void *opaque; |
| 34 | FindSysbusDeviceFunc *func; |
| 35 | } SysBusFind; |
| 36 | |
| 37 | /* Run func() for every sysbus device, traverse the tree for everything else */ |
| 38 | static int find_sysbus_device(Object *obj, void *opaque) |
| 39 | { |
| 40 | SysBusFind *find = opaque; |
| 41 | Object *dev; |
| 42 | SysBusDevice *sbdev; |
| 43 | |
| 44 | dev = object_dynamic_cast(obj, TYPE_SYS_BUS_DEVICE); |
| 45 | sbdev = (SysBusDevice *)dev; |
| 46 | |
| 47 | if (!sbdev) { |
| 48 | /* Container, traverse it for children */ |
| 49 | return object_child_foreach(obj, find_sysbus_device, opaque); |
| 50 | } |
| 51 | |
| 52 | find->func(sbdev, find->opaque); |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Loop through all dynamically created sysbus devices and call |
| 59 | * func() for each instance. |
| 60 | */ |
| 61 | void foreach_dynamic_sysbus_device(FindSysbusDeviceFunc *func, void *opaque) |
| 62 | { |
| 63 | Object *container; |
| 64 | SysBusFind find = { |
| 65 | .func = func, |
| 66 | .opaque = opaque, |
| 67 | }; |
| 68 | |
| 69 | /* Loop through all sysbus devices that were spawned outside the machine */ |
| 70 | container = machine_get_container("peripheral"); |
| 71 | find_sysbus_device(container, &find); |
| 72 | container = machine_get_container("peripheral-anon"); |
| 73 | find_sysbus_device(container, &find); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | static void system_bus_class_init(ObjectClass *klass, const void *data) |
| 78 | { |
| 79 | BusClass *k = BUS_CLASS(klass); |
| 80 | |
| 81 | #ifdef CONFIG_HMP |
| 82 | k->print_dev = sysbus_dev_print; |
| 83 | #endif |
| 84 | k->get_fw_dev_path = sysbus_get_fw_dev_path; |
| 85 | } |
| 86 | |
| 87 | /* Check whether an IRQ source exists */ |
| 88 | bool sysbus_has_irq(const 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); |
| 94 | g_free(prop); |
| 95 | |
| 96 | return (r != NULL); |
| 97 | } |
| 98 | |
| 99 | bool sysbus_is_irq_connected(const SysBusDevice *dev, int n) |
| 100 | { |
| 101 | return !!sysbus_get_connected_irq(dev, n); |
| 102 | } |
| 103 | |
| 104 | qemu_irq sysbus_get_connected_irq(const SysBusDevice *dev, int n) |
| 105 | { |
| 106 | DeviceState *d = DEVICE(dev); |
| 107 | return qdev_get_gpio_out_connector(d, SYSBUS_DEVICE_GPIO_IRQ, n); |
| 108 | } |
| 109 | |
| 110 | void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq) |
| 111 | { |
| 112 | qdev_connect_gpio_out_named(DEVICE(dev), SYSBUS_DEVICE_GPIO_IRQ, n, irq); |
| 113 | } |
| 114 | |
| 115 | /* Check whether an MMIO region exists */ |
| 116 | bool sysbus_has_mmio(const SysBusDevice *dev, unsigned int n) |
| 117 | { |
| 118 | return (n < dev->num_mmio); |
| 119 | } |
| 120 | |
| 121 | static void sysbus_mmio_map_common(SysBusDevice *dev, int n, hwaddr addr, |
| 122 | bool may_overlap, int priority) |
| 123 | { |
| 124 | assert(n >= 0 && n < dev->num_mmio); |
| 125 | |
| 126 | if (dev->mmio[n].addr == addr) { |
| 127 | /* ??? region already mapped here. */ |
| 128 | return; |
| 129 | } |
| 130 | if (dev->mmio[n].addr != (hwaddr)-1) { |
| 131 | /* Unregister previous mapping. */ |
| 132 | memory_region_del_subregion(get_system_memory(), dev->mmio[n].memory); |
| 133 | } |
| 134 | dev->mmio[n].addr = addr; |
| 135 | if (may_overlap) { |
| 136 | memory_region_add_subregion_overlap(get_system_memory(), |
| 137 | addr, |
| 138 | dev->mmio[n].memory, |
| 139 | priority); |
| 140 | } |
| 141 | else { |
| 142 | memory_region_add_subregion(get_system_memory(), |
| 143 | addr, |
| 144 | dev->mmio[n].memory); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | void sysbus_mmio_map(SysBusDevice *dev, int n, hwaddr addr) |
| 149 | { |
| 150 | sysbus_mmio_map_common(dev, n, addr, false, 0); |
| 151 | } |
| 152 | |
| 153 | int sysbus_mmio_map_name(SysBusDevice *dev, const char *name, hwaddr addr) |
| 154 | { |
| 155 | for (int i = 0; i < dev->num_mmio; i++) { |
| 156 | if (!strcmp(dev->mmio[i].memory->name, name)) { |
| 157 | sysbus_mmio_map(dev, i, addr); |
| 158 | return i; |
| 159 | } |
| 160 | } |
| 161 | return -1; |
| 162 | } |
| 163 | |
| 164 | void sysbus_mmio_map_overlap(SysBusDevice *dev, int n, hwaddr addr, |
| 165 | int priority) |
| 166 | { |
| 167 | sysbus_mmio_map_common(dev, n, addr, true, priority); |
| 168 | } |
| 169 | |
| 170 | /* Request an IRQ source. The actual IRQ object may be populated later. */ |
| 171 | void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p) |
| 172 | { |
| 173 | qdev_init_gpio_out_named(DEVICE(dev), p, SYSBUS_DEVICE_GPIO_IRQ, 1); |
| 174 | } |
| 175 | |
| 176 | /* Pass IRQs from a target device. */ |
| 177 | void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target) |
| 178 | { |
| 179 | qdev_pass_gpios(DEVICE(target), DEVICE(dev), SYSBUS_DEVICE_GPIO_IRQ); |
| 180 | } |
| 181 | |
| 182 | void sysbus_init_mmio(SysBusDevice *dev, MemoryRegion *memory) |
| 183 | { |
| 184 | int n; |
| 185 | |
| 186 | assert(dev->num_mmio < QDEV_MAX_MMIO); |
| 187 | n = dev->num_mmio++; |
| 188 | dev->mmio[n].addr = -1; |
| 189 | dev->mmio[n].memory = memory; |
| 190 | } |
| 191 | |
| 192 | MemoryRegion *sysbus_mmio_get_region(const SysBusDevice *dev, int n) |
| 193 | { |
| 194 | assert(n >= 0 && n < QDEV_MAX_MMIO); |
| 195 | return dev->mmio[n].memory; |
| 196 | } |
| 197 | |
| 198 | void sysbus_init_ioports(SysBusDevice *dev, uint32_t ioport, uint32_t size) |
| 199 | { |
| 200 | uint32_t i; |
| 201 | |
| 202 | for (i = 0; i < size; i++) { |
| 203 | assert(dev->num_pio < QDEV_MAX_PIO); |
| 204 | dev->pio[dev->num_pio++] = ioport++; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /* The purpose of preserving this empty realize function |
| 209 | * is to prevent the parent_realize field of some subclasses |
| 210 | * from being set to NULL to break the normal init/realize |
| 211 | * of some devices. |
| 212 | */ |
| 213 | static void sysbus_device_realize(DeviceState *dev, Error **errp) |
| 214 | { |
| 215 | } |
| 216 | |
| 217 | DeviceState *sysbus_create_varargs(const char *name, |
| 218 | hwaddr addr, ...) |
| 219 | { |
| 220 | DeviceState *dev; |
| 221 | SysBusDevice *s; |
| 222 | va_list va; |
| 223 | qemu_irq irq; |
| 224 | int n; |
| 225 | |
| 226 | dev = qdev_new(name); |
| 227 | s = SYS_BUS_DEVICE(dev); |
| 228 | sysbus_realize_and_unref(s, &error_fatal); |
| 229 | if (addr != (hwaddr)-1) { |
| 230 | sysbus_mmio_map(s, 0, addr); |
| 231 | } |
| 232 | va_start(va, addr); |
| 233 | n = 0; |
| 234 | while (1) { |
| 235 | irq = va_arg(va, qemu_irq); |
| 236 | if (!irq) { |
| 237 | break; |
| 238 | } |
| 239 | sysbus_connect_irq(s, n, irq); |
| 240 | n++; |
| 241 | } |
| 242 | va_end(va); |
| 243 | return dev; |
| 244 | } |
| 245 | |
| 246 | bool sysbus_realize(SysBusDevice *dev, Error **errp) |
| 247 | { |
| 248 | return qdev_realize(DEVICE(dev), sysbus_get_default(), errp); |
| 249 | } |
| 250 | |
| 251 | bool sysbus_realize_and_unref(SysBusDevice *dev, Error **errp) |
| 252 | { |
| 253 | return qdev_realize_and_unref(DEVICE(dev), sysbus_get_default(), errp); |
| 254 | } |
| 255 | |
| 256 | #ifdef CONFIG_HMP |
| 257 | static void sysbus_dev_print(MonitorHMP *hmp, DeviceState *dev, int indent) |
| 258 | { |
| 259 | SysBusDevice *s = SYS_BUS_DEVICE(dev); |
| 260 | hwaddr size; |
| 261 | int i; |
| 262 | |
| 263 | for (i = 0; i < s->num_mmio; i++) { |
| 264 | size = memory_region_size(s->mmio[i].memory); |
| 265 | monitor_hmp_printf(hmp, "%*smmio " HWADDR_FMT_plx "/" HWADDR_FMT_plx "\n", |
| 266 | indent, "", s->mmio[i].addr, size); |
| 267 | } |
| 268 | } |
| 269 | #endif |
| 270 | |
| 271 | static char *sysbus_get_fw_dev_path(DeviceState *dev) |
| 272 | { |
| 273 | SysBusDevice *s = SYS_BUS_DEVICE(dev); |
| 274 | SysBusDeviceClass *sbc = SYS_BUS_DEVICE_GET_CLASS(s); |
| 275 | char *addr, *fw_dev_path; |
| 276 | |
| 277 | if (sbc->explicit_ofw_unit_address) { |
| 278 | addr = sbc->explicit_ofw_unit_address(s); |
| 279 | if (addr) { |
| 280 | fw_dev_path = g_strdup_printf("%s@%s", qdev_fw_name(dev), addr); |
| 281 | g_free(addr); |
| 282 | return fw_dev_path; |
| 283 | } |
| 284 | } |
| 285 | if (s->num_mmio) { |
| 286 | return g_strdup_printf("%s@" HWADDR_FMT_plx, qdev_fw_name(dev), |
| 287 | s->mmio[0].addr); |
| 288 | } |
| 289 | if (s->num_pio) { |
| 290 | return g_strdup_printf("%s@i%04x", qdev_fw_name(dev), s->pio[0]); |
| 291 | } |
| 292 | return g_strdup(qdev_fw_name(dev)); |
| 293 | } |
| 294 | |
| 295 | static void sysbus_device_class_init(ObjectClass *klass, const void *data) |
| 296 | { |
| 297 | DeviceClass *k = DEVICE_CLASS(klass); |
| 298 | k->realize = sysbus_device_realize; |
| 299 | k->bus_type = TYPE_SYSTEM_BUS; |
| 300 | /* |
| 301 | * device_add plugs devices into a suitable bus. For "real" buses, |
| 302 | * that actually connects the device. For sysbus, the connections |
| 303 | * need to be made separately, and device_add can't do that. The |
| 304 | * device would be left unconnected, and will probably not work |
| 305 | * |
| 306 | * However, a few machines can handle device_add/-device with |
| 307 | * a few specific sysbus devices. In those cases, the device |
| 308 | * subclass needs to override it and set user_creatable=true. |
| 309 | */ |
| 310 | k->user_creatable = false; |
| 311 | } |
| 312 | |
| 313 | static BusState *main_system_bus; |
| 314 | |
| 315 | static void main_system_bus_create(void) |
| 316 | { |
| 317 | /* |
| 318 | * assign main_system_bus before qbus_init() |
| 319 | * in order to make "if (bus != sysbus_get_default())" work |
| 320 | */ |
| 321 | main_system_bus = g_new0(BusState, 1); |
| 322 | qbus_init(main_system_bus, sizeof(BusState), |
| 323 | TYPE_SYSTEM_BUS, NULL, "main-system-bus"); |
| 324 | OBJECT(main_system_bus)->free = g_free; |
| 325 | } |
| 326 | |
| 327 | BusState *sysbus_get_default(void) |
| 328 | { |
| 329 | if (!main_system_bus) { |
| 330 | main_system_bus_create(); |
| 331 | } |
| 332 | return main_system_bus; |
| 333 | } |
| 334 | |
| 335 | static void dynamic_sysbus_device_class_init(ObjectClass *klass, |
| 336 | const void *data) |
| 337 | { |
| 338 | DeviceClass *k = DEVICE_CLASS(klass); |
| 339 | |
| 340 | k->user_creatable = true; |
| 341 | k->hotpluggable = false; |
| 342 | } |
| 343 | |
| 344 | static const TypeInfo sysbus_types[] = { |
| 345 | { |
| 346 | .name = TYPE_SYSTEM_BUS, |
| 347 | .parent = TYPE_BUS, |
| 348 | .instance_size = sizeof(BusState), |
| 349 | .class_init = system_bus_class_init, |
| 350 | }, |
| 351 | { |
| 352 | .name = TYPE_SYS_BUS_DEVICE, |
| 353 | .parent = TYPE_DEVICE, |
| 354 | .instance_size = sizeof(SysBusDevice), |
| 355 | .abstract = true, |
| 356 | .class_size = sizeof(SysBusDeviceClass), |
| 357 | .class_init = sysbus_device_class_init, |
| 358 | }, |
| 359 | { |
| 360 | .name = TYPE_DYNAMIC_SYS_BUS_DEVICE, |
| 361 | .parent = TYPE_SYS_BUS_DEVICE, |
| 362 | .class_init = dynamic_sysbus_device_class_init, |
| 363 | .abstract = true, |
| 364 | } |
| 365 | }; |
| 366 | |
| 367 | DEFINE_TYPES(sysbus_types) |