1 /* 2 * ARMV7M System emulation. 3 * 4 * Copyright (c) 2006-2007 CodeSourcery. 5 * Written by Paul Brook 6 * 7 * This code is licensed under the GPL. 8 */ 9 10 #include "qemu/osdep.h" 11 #include "hw/arm/armv7m.h" 12 #include "qapi/error.h" 13 #include "cpu.h" 14 #include "hw/sysbus.h" 15 #include "hw/arm/boot.h" 16 #include "hw/loader.h" 17 #include "elf.h" 18 #include "sysemu/qtest.h" 19 #include "sysemu/reset.h" 20 #include "qemu/error-report.h" 21 #include "qemu/module.h" 22 #include "exec/address-spaces.h" 23 #include "target/arm/idau.h" 24 25 /* Bitbanded IO. Each word corresponds to a single bit. */ 26 27 /* Get the byte address of the real memory for a bitband access. */ 28 static inline hwaddr bitband_addr(BitBandState *s, hwaddr offset) 29 { 30 return s->base | (offset & 0x1ffffff) >> 5; 31 } 32 33 static MemTxResult bitband_read(void *opaque, hwaddr offset, 34 uint64_t *data, unsigned size, MemTxAttrs attrs) 35 { 36 BitBandState *s = opaque; 37 uint8_t buf[4]; 38 MemTxResult res; 39 int bitpos, bit; 40 hwaddr addr; 41 42 assert(size <= 4); 43 44 /* Find address in underlying memory and round down to multiple of size */ 45 addr = bitband_addr(s, offset) & (-size); 46 res = address_space_read(&s->source_as, addr, attrs, buf, size); 47 if (res) { 48 return res; 49 } 50 /* Bit position in the N bytes read... */ 51 bitpos = (offset >> 2) & ((size * 8) - 1); 52 /* ...converted to byte in buffer and bit in byte */ 53 bit = (buf[bitpos >> 3] >> (bitpos & 7)) & 1; 54 *data = bit; 55 return MEMTX_OK; 56 } 57 58 static MemTxResult bitband_write(void *opaque, hwaddr offset, uint64_t value, 59 unsigned size, MemTxAttrs attrs) 60 { 61 BitBandState *s = opaque; 62 uint8_t buf[4]; 63 MemTxResult res; 64 int bitpos, bit; 65 hwaddr addr; 66 67 assert(size <= 4); 68 69 /* Find address in underlying memory and round down to multiple of size */ 70 addr = bitband_addr(s, offset) & (-size); 71 res = address_space_read(&s->source_as, addr, attrs, buf, size); 72 if (res) { 73 return res; 74 } 75 /* Bit position in the N bytes read... */ 76 bitpos = (offset >> 2) & ((size * 8) - 1); 77 /* ...converted to byte in buffer and bit in byte */ 78 bit = 1 << (bitpos & 7); 79 if (value & 1) { 80 buf[bitpos >> 3] |= bit; 81 } else { 82 buf[bitpos >> 3] &= ~bit; 83 } 84 return address_space_write(&s->source_as, addr, attrs, buf, size); 85 } 86 87 static const MemoryRegionOps bitband_ops = { 88 .read_with_attrs = bitband_read, 89 .write_with_attrs = bitband_write, 90 .endianness = DEVICE_NATIVE_ENDIAN, 91 .impl.min_access_size = 1, 92 .impl.max_access_size = 4, 93 .valid.min_access_size = 1, 94 .valid.max_access_size = 4, 95 }; 96 97 static void bitband_init(Object *obj) 98 { 99 BitBandState *s = BITBAND(obj); 100 SysBusDevice *dev = SYS_BUS_DEVICE(obj); 101 102 memory_region_init_io(&s->iomem, obj, &bitband_ops, s, 103 "bitband", 0x02000000); 104 sysbus_init_mmio(dev, &s->iomem); 105 } 106 107 static void bitband_realize(DeviceState *dev, Error **errp) 108 { 109 BitBandState *s = BITBAND(dev); 110 111 if (!s->source_memory) { 112 error_setg(errp, "source-memory property not set"); 113 return; 114 } 115 116 address_space_init(&s->source_as, s->source_memory, "bitband-source"); 117 } 118 119 /* Board init. */ 120 121 static const hwaddr bitband_input_addr[ARMV7M_NUM_BITBANDS] = { 122 0x20000000, 0x40000000 123 }; 124 125 static const hwaddr bitband_output_addr[ARMV7M_NUM_BITBANDS] = { 126 0x22000000, 0x42000000 127 }; 128 129 static void armv7m_instance_init(Object *obj) 130 { 131 ARMv7MState *s = ARMV7M(obj); 132 int i; 133 134 /* Can't init the cpu here, we don't yet know which model to use */ 135 136 memory_region_init(&s->container, obj, "armv7m-container", UINT64_MAX); 137 138 sysbus_init_child_obj(obj, "nvnic", &s->nvic, sizeof(s->nvic), TYPE_NVIC); 139 object_property_add_alias(obj, "num-irq", 140 OBJECT(&s->nvic), "num-irq", &error_abort); 141 142 for (i = 0; i < ARRAY_SIZE(s->bitband); i++) { 143 sysbus_init_child_obj(obj, "bitband[*]", &s->bitband[i], 144 sizeof(s->bitband[i]), TYPE_BITBAND); 145 } 146 } 147 148 static void armv7m_realize(DeviceState *dev, Error **errp) 149 { 150 ARMv7MState *s = ARMV7M(dev); 151 SysBusDevice *sbd; 152 Error *err = NULL; 153 int i; 154 155 if (!s->board_memory) { 156 error_setg(errp, "memory property was not set"); 157 return; 158 } 159 160 memory_region_add_subregion_overlap(&s->container, 0, s->board_memory, -1); 161 162 s->cpu = ARM_CPU(object_new_with_props(s->cpu_type, OBJECT(s), "cpu", 163 &err, NULL)); 164 if (err != NULL) { 165 error_propagate(errp, err); 166 return; 167 } 168 169 object_property_set_link(OBJECT(s->cpu), OBJECT(&s->container), "memory", 170 &error_abort); 171 if (object_property_find(OBJECT(s->cpu), "idau", NULL)) { 172 object_property_set_link(OBJECT(s->cpu), s->idau, "idau", &err); 173 if (err != NULL) { 174 error_propagate(errp, err); 175 return; 176 } 177 } 178 if (object_property_find(OBJECT(s->cpu), "init-svtor", NULL)) { 179 object_property_set_uint(OBJECT(s->cpu), s->init_svtor, 180 "init-svtor", &err); 181 if (err != NULL) { 182 error_propagate(errp, err); 183 return; 184 } 185 } 186 if (object_property_find(OBJECT(s->cpu), "start-powered-off", NULL)) { 187 object_property_set_bool(OBJECT(s->cpu), s->start_powered_off, 188 "start-powered-off", &err); 189 if (err != NULL) { 190 error_propagate(errp, err); 191 return; 192 } 193 } 194 if (object_property_find(OBJECT(s->cpu), "vfp", NULL)) { 195 object_property_set_bool(OBJECT(s->cpu), s->vfp, 196 "vfp", &err); 197 if (err != NULL) { 198 error_propagate(errp, err); 199 return; 200 } 201 } 202 if (object_property_find(OBJECT(s->cpu), "dsp", NULL)) { 203 object_property_set_bool(OBJECT(s->cpu), s->dsp, 204 "dsp", &err); 205 if (err != NULL) { 206 error_propagate(errp, err); 207 return; 208 } 209 } 210 211 /* 212 * Tell the CPU where the NVIC is; it will fail realize if it doesn't 213 * have one. Similarly, tell the NVIC where its CPU is. 214 */ 215 s->cpu->env.nvic = &s->nvic; 216 s->nvic.cpu = s->cpu; 217 218 object_property_set_bool(OBJECT(s->cpu), true, "realized", &err); 219 if (err != NULL) { 220 error_propagate(errp, err); 221 return; 222 } 223 224 /* Note that we must realize the NVIC after the CPU */ 225 object_property_set_bool(OBJECT(&s->nvic), true, "realized", &err); 226 if (err != NULL) { 227 error_propagate(errp, err); 228 return; 229 } 230 231 /* Alias the NVIC's input and output GPIOs as our own so the board 232 * code can wire them up. (We do this in realize because the 233 * NVIC doesn't create the input GPIO array until realize.) 234 */ 235 qdev_pass_gpios(DEVICE(&s->nvic), dev, NULL); 236 qdev_pass_gpios(DEVICE(&s->nvic), dev, "SYSRESETREQ"); 237 qdev_pass_gpios(DEVICE(&s->nvic), dev, "NMI"); 238 239 /* Wire the NVIC up to the CPU */ 240 sbd = SYS_BUS_DEVICE(&s->nvic); 241 sysbus_connect_irq(sbd, 0, 242 qdev_get_gpio_in(DEVICE(s->cpu), ARM_CPU_IRQ)); 243 244 memory_region_add_subregion(&s->container, 0xe000e000, 245 sysbus_mmio_get_region(sbd, 0)); 246 247 if (s->enable_bitband) { 248 for (i = 0; i < ARRAY_SIZE(s->bitband); i++) { 249 Object *obj = OBJECT(&s->bitband[i]); 250 SysBusDevice *sbd = SYS_BUS_DEVICE(&s->bitband[i]); 251 252 object_property_set_int(obj, bitband_input_addr[i], "base", &err); 253 if (err != NULL) { 254 error_propagate(errp, err); 255 return; 256 } 257 object_property_set_link(obj, OBJECT(s->board_memory), 258 "source-memory", &error_abort); 259 object_property_set_bool(obj, true, "realized", &err); 260 if (err != NULL) { 261 error_propagate(errp, err); 262 return; 263 } 264 265 memory_region_add_subregion(&s->container, bitband_output_addr[i], 266 sysbus_mmio_get_region(sbd, 0)); 267 } 268 } 269 } 270 271 static Property armv7m_properties[] = { 272 DEFINE_PROP_STRING("cpu-type", ARMv7MState, cpu_type), 273 DEFINE_PROP_LINK("memory", ARMv7MState, board_memory, TYPE_MEMORY_REGION, 274 MemoryRegion *), 275 DEFINE_PROP_LINK("idau", ARMv7MState, idau, TYPE_IDAU_INTERFACE, Object *), 276 DEFINE_PROP_UINT32("init-svtor", ARMv7MState, init_svtor, 0), 277 DEFINE_PROP_BOOL("enable-bitband", ARMv7MState, enable_bitband, false), 278 DEFINE_PROP_BOOL("start-powered-off", ARMv7MState, start_powered_off, 279 false), 280 DEFINE_PROP_BOOL("vfp", ARMv7MState, vfp, true), 281 DEFINE_PROP_BOOL("dsp", ARMv7MState, dsp, true), 282 DEFINE_PROP_END_OF_LIST(), 283 }; 284 285 static void armv7m_class_init(ObjectClass *klass, void *data) 286 { 287 DeviceClass *dc = DEVICE_CLASS(klass); 288 289 dc->realize = armv7m_realize; 290 dc->props = armv7m_properties; 291 } 292 293 static const TypeInfo armv7m_info = { 294 .name = TYPE_ARMV7M, 295 .parent = TYPE_SYS_BUS_DEVICE, 296 .instance_size = sizeof(ARMv7MState), 297 .instance_init = armv7m_instance_init, 298 .class_init = armv7m_class_init, 299 }; 300 301 static void armv7m_reset(void *opaque) 302 { 303 ARMCPU *cpu = opaque; 304 305 cpu_reset(CPU(cpu)); 306 } 307 308 void armv7m_load_kernel(ARMCPU *cpu, const char *kernel_filename, int mem_size) 309 { 310 int image_size; 311 uint64_t entry; 312 uint64_t lowaddr; 313 int big_endian; 314 AddressSpace *as; 315 int asidx; 316 CPUState *cs = CPU(cpu); 317 318 #ifdef TARGET_WORDS_BIGENDIAN 319 big_endian = 1; 320 #else 321 big_endian = 0; 322 #endif 323 324 if (arm_feature(&cpu->env, ARM_FEATURE_EL3)) { 325 asidx = ARMASIdx_S; 326 } else { 327 asidx = ARMASIdx_NS; 328 } 329 as = cpu_get_address_space(cs, asidx); 330 331 if (kernel_filename) { 332 image_size = load_elf_as(kernel_filename, NULL, NULL, NULL, 333 &entry, &lowaddr, 334 NULL, big_endian, EM_ARM, 1, 0, as); 335 if (image_size < 0) { 336 image_size = load_image_targphys_as(kernel_filename, 0, 337 mem_size, as); 338 lowaddr = 0; 339 } 340 if (image_size < 0) { 341 error_report("Could not load kernel '%s'", kernel_filename); 342 exit(1); 343 } 344 } 345 346 /* CPU objects (unlike devices) are not automatically reset on system 347 * reset, so we must always register a handler to do so. Unlike 348 * A-profile CPUs, we don't need to do anything special in the 349 * handler to arrange that it starts correctly. 350 * This is arguably the wrong place to do this, but it matches the 351 * way A-profile does it. Note that this means that every M profile 352 * board must call this function! 353 */ 354 qemu_register_reset(armv7m_reset, cpu); 355 } 356 357 static Property bitband_properties[] = { 358 DEFINE_PROP_UINT32("base", BitBandState, base, 0), 359 DEFINE_PROP_LINK("source-memory", BitBandState, source_memory, 360 TYPE_MEMORY_REGION, MemoryRegion *), 361 DEFINE_PROP_END_OF_LIST(), 362 }; 363 364 static void bitband_class_init(ObjectClass *klass, void *data) 365 { 366 DeviceClass *dc = DEVICE_CLASS(klass); 367 368 dc->realize = bitband_realize; 369 dc->props = bitband_properties; 370 } 371 372 static const TypeInfo bitband_info = { 373 .name = TYPE_BITBAND, 374 .parent = TYPE_SYS_BUS_DEVICE, 375 .instance_size = sizeof(BitBandState), 376 .instance_init = bitband_init, 377 .class_init = bitband_class_init, 378 }; 379 380 static void armv7m_register_types(void) 381 { 382 type_register_static(&bitband_info); 383 type_register_static(&armv7m_info); 384 } 385 386 type_init(armv7m_register_types) 387