Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 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 |
Blue Swirl | 8167ee8 | 2009-07-16 20:47:01 +0000 | [diff] [blame] | 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include "sysbus.h" |
| 21 | #include "sysemu.h" |
Gerd Hoffmann | cae4956 | 2009-06-05 15:53:17 +0100 | [diff] [blame] | 22 | #include "monitor.h" |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 23 | |
Gerd Hoffmann | 10c4c98 | 2009-06-30 14:12:08 +0200 | [diff] [blame] | 24 | static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent); |
Gleb Natapov | c646f74 | 2010-12-08 13:35:00 +0200 | [diff] [blame] | 25 | static char *sysbus_get_fw_dev_path(DeviceState *dev); |
Gerd Hoffmann | 10c4c98 | 2009-06-30 14:12:08 +0200 | [diff] [blame] | 26 | |
| 27 | struct BusInfo system_bus_info = { |
| 28 | .name = "System", |
| 29 | .size = sizeof(BusState), |
| 30 | .print_dev = sysbus_dev_print, |
Gleb Natapov | c646f74 | 2010-12-08 13:35:00 +0200 | [diff] [blame] | 31 | .get_fw_dev_path = sysbus_get_fw_dev_path, |
Gerd Hoffmann | 10c4c98 | 2009-06-30 14:12:08 +0200 | [diff] [blame] | 32 | }; |
| 33 | |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 34 | void sysbus_connect_irq(SysBusDevice *dev, int n, qemu_irq irq) |
| 35 | { |
| 36 | assert(n >= 0 && n < dev->num_irq); |
Blue Swirl | 660f11b | 2009-07-31 21:16:51 +0000 | [diff] [blame] | 37 | dev->irqs[n] = NULL; |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 38 | if (dev->irqp[n]) { |
| 39 | *dev->irqp[n] = irq; |
| 40 | } |
| 41 | } |
| 42 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 43 | void sysbus_mmio_map(SysBusDevice *dev, int n, target_phys_addr_t addr) |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 44 | { |
| 45 | assert(n >= 0 && n < dev->num_mmio); |
| 46 | |
| 47 | if (dev->mmio[n].addr == addr) { |
| 48 | /* ??? region already mapped here. */ |
| 49 | return; |
| 50 | } |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 51 | if (dev->mmio[n].addr != (target_phys_addr_t)-1) { |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 52 | /* Unregister previous mapping. */ |
| 53 | cpu_register_physical_memory(dev->mmio[n].addr, dev->mmio[n].size, |
| 54 | IO_MEM_UNASSIGNED); |
| 55 | } |
| 56 | dev->mmio[n].addr = addr; |
| 57 | if (dev->mmio[n].cb) { |
| 58 | dev->mmio[n].cb(dev, addr); |
| 59 | } else { |
| 60 | cpu_register_physical_memory(addr, dev->mmio[n].size, |
| 61 | dev->mmio[n].iofunc); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | |
| 66 | /* Request an IRQ source. The actual IRQ object may be populated later. */ |
| 67 | void sysbus_init_irq(SysBusDevice *dev, qemu_irq *p) |
| 68 | { |
| 69 | int n; |
| 70 | |
| 71 | assert(dev->num_irq < QDEV_MAX_IRQ); |
| 72 | n = dev->num_irq++; |
| 73 | dev->irqp[n] = p; |
| 74 | } |
| 75 | |
| 76 | /* Pass IRQs from a target device. */ |
| 77 | void sysbus_pass_irq(SysBusDevice *dev, SysBusDevice *target) |
| 78 | { |
| 79 | int i; |
| 80 | assert(dev->num_irq == 0); |
| 81 | dev->num_irq = target->num_irq; |
| 82 | for (i = 0; i < dev->num_irq; i++) { |
| 83 | dev->irqp[i] = target->irqp[i]; |
| 84 | } |
| 85 | } |
| 86 | |
Blue Swirl | 3f7132d | 2010-10-02 14:27:41 +0000 | [diff] [blame] | 87 | void sysbus_init_mmio(SysBusDevice *dev, target_phys_addr_t size, |
| 88 | ram_addr_t iofunc) |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 89 | { |
| 90 | int n; |
| 91 | |
| 92 | assert(dev->num_mmio < QDEV_MAX_MMIO); |
| 93 | n = dev->num_mmio++; |
| 94 | dev->mmio[n].addr = -1; |
| 95 | dev->mmio[n].size = size; |
| 96 | dev->mmio[n].iofunc = iofunc; |
| 97 | } |
| 98 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 99 | void sysbus_init_mmio_cb(SysBusDevice *dev, target_phys_addr_t size, |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 100 | mmio_mapfunc cb) |
| 101 | { |
| 102 | int n; |
| 103 | |
| 104 | assert(dev->num_mmio < QDEV_MAX_MMIO); |
| 105 | n = dev->num_mmio++; |
| 106 | dev->mmio[n].addr = -1; |
| 107 | dev->mmio[n].size = size; |
| 108 | dev->mmio[n].cb = cb; |
| 109 | } |
| 110 | |
Gleb Natapov | c646f74 | 2010-12-08 13:35:00 +0200 | [diff] [blame] | 111 | void sysbus_init_ioports(SysBusDevice *dev, pio_addr_t ioport, pio_addr_t size) |
| 112 | { |
| 113 | pio_addr_t i; |
| 114 | |
| 115 | for (i = 0; i < size; i++) { |
| 116 | assert(dev->num_pio < QDEV_MAX_PIO); |
| 117 | dev->pio[dev->num_pio++] = ioport++; |
| 118 | } |
| 119 | } |
| 120 | |
Gerd Hoffmann | 81a322d | 2009-08-14 10:36:05 +0200 | [diff] [blame] | 121 | static int sysbus_device_init(DeviceState *dev, DeviceInfo *base) |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 122 | { |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 123 | SysBusDeviceInfo *info = container_of(base, SysBusDeviceInfo, qdev); |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 124 | |
Gerd Hoffmann | 81a322d | 2009-08-14 10:36:05 +0200 | [diff] [blame] | 125 | return info->init(sysbus_from_qdev(dev)); |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 126 | } |
| 127 | |
Gerd Hoffmann | 074f2ff | 2009-06-10 09:41:42 +0200 | [diff] [blame] | 128 | void sysbus_register_withprop(SysBusDeviceInfo *info) |
Paul Brook | 1431b6a | 2009-06-05 15:52:04 +0100 | [diff] [blame] | 129 | { |
| 130 | info->qdev.init = sysbus_device_init; |
Gerd Hoffmann | 10c4c98 | 2009-06-30 14:12:08 +0200 | [diff] [blame] | 131 | info->qdev.bus_info = &system_bus_info; |
Paul Brook | 1431b6a | 2009-06-05 15:52:04 +0100 | [diff] [blame] | 132 | |
Gerd Hoffmann | 074f2ff | 2009-06-10 09:41:42 +0200 | [diff] [blame] | 133 | assert(info->qdev.size >= sizeof(SysBusDevice)); |
| 134 | qdev_register(&info->qdev); |
Paul Brook | 1431b6a | 2009-06-05 15:52:04 +0100 | [diff] [blame] | 135 | } |
| 136 | |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 137 | void sysbus_register_dev(const char *name, size_t size, sysbus_initfn init) |
| 138 | { |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 139 | SysBusDeviceInfo *info; |
| 140 | |
| 141 | info = qemu_mallocz(sizeof(*info)); |
Gerd Hoffmann | 074f2ff | 2009-06-10 09:41:42 +0200 | [diff] [blame] | 142 | info->qdev.name = qemu_strdup(name); |
| 143 | info->qdev.size = size; |
Paul Brook | 02e2da4 | 2009-05-23 00:05:19 +0100 | [diff] [blame] | 144 | info->init = init; |
Gerd Hoffmann | 074f2ff | 2009-06-10 09:41:42 +0200 | [diff] [blame] | 145 | sysbus_register_withprop(info); |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | DeviceState *sysbus_create_varargs(const char *name, |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 149 | target_phys_addr_t addr, ...) |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 150 | { |
| 151 | DeviceState *dev; |
| 152 | SysBusDevice *s; |
| 153 | va_list va; |
| 154 | qemu_irq irq; |
| 155 | int n; |
| 156 | |
| 157 | dev = qdev_create(NULL, name); |
| 158 | s = sysbus_from_qdev(dev); |
Markus Armbruster | e23a1b3 | 2009-10-07 01:15:58 +0200 | [diff] [blame] | 159 | qdev_init_nofail(dev); |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 160 | if (addr != (target_phys_addr_t)-1) { |
Paul Brook | aae9460 | 2009-05-14 22:35:06 +0100 | [diff] [blame] | 161 | sysbus_mmio_map(s, 0, addr); |
| 162 | } |
| 163 | va_start(va, addr); |
| 164 | n = 0; |
| 165 | while (1) { |
| 166 | irq = va_arg(va, qemu_irq); |
| 167 | if (!irq) { |
| 168 | break; |
| 169 | } |
| 170 | sysbus_connect_irq(s, n, irq); |
| 171 | n++; |
| 172 | } |
| 173 | return dev; |
| 174 | } |
Gerd Hoffmann | cae4956 | 2009-06-05 15:53:17 +0100 | [diff] [blame] | 175 | |
Gerd Hoffmann | 10c4c98 | 2009-06-30 14:12:08 +0200 | [diff] [blame] | 176 | static void sysbus_dev_print(Monitor *mon, DeviceState *dev, int indent) |
Gerd Hoffmann | cae4956 | 2009-06-05 15:53:17 +0100 | [diff] [blame] | 177 | { |
| 178 | SysBusDevice *s = sysbus_from_qdev(dev); |
| 179 | int i; |
| 180 | |
| 181 | for (i = 0; i < s->num_mmio; i++) { |
| 182 | monitor_printf(mon, "%*smmio " TARGET_FMT_plx "/" TARGET_FMT_plx "\n", |
| 183 | indent, "", s->mmio[i].addr, s->mmio[i].size); |
| 184 | } |
| 185 | } |
Gleb Natapov | c646f74 | 2010-12-08 13:35:00 +0200 | [diff] [blame] | 186 | |
| 187 | static char *sysbus_get_fw_dev_path(DeviceState *dev) |
| 188 | { |
| 189 | SysBusDevice *s = sysbus_from_qdev(dev); |
| 190 | char path[40]; |
| 191 | int off; |
| 192 | |
| 193 | off = snprintf(path, sizeof(path), "%s", qdev_fw_name(dev)); |
| 194 | |
| 195 | if (s->num_mmio) { |
| 196 | snprintf(path + off, sizeof(path) - off, "@"TARGET_FMT_plx, |
| 197 | s->mmio[0].addr); |
| 198 | } else if (s->num_pio) { |
| 199 | snprintf(path + off, sizeof(path) - off, "@i%04x", s->pio[0]); |
| 200 | } |
| 201 | |
| 202 | return strdup(path); |
| 203 | } |