|
| 1 | +/* |
| 2 | + * Data Scratch Pad RAM |
| 3 | + * |
| 4 | + * Copyright (c) 2017 Imagination Technologies |
| 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 "qemu/osdep.h" |
| 21 | +#include "qapi/error.h" |
| 22 | +#include "cpu.h" |
| 23 | +#include "qemu/log.h" |
| 24 | +#include "exec/exec-all.h" |
| 25 | +#include "hw/hw.h" |
| 26 | +#include "hw/sysbus.h" |
| 27 | +#include "sysemu/sysemu.h" |
| 28 | +#include "hw/misc/mips_dspram.h" |
| 29 | + |
| 30 | +static void raise_exception(int excp) |
| 31 | +{ |
| 32 | + current_cpu->exception_index = excp; |
| 33 | + cpu_loop_exit(current_cpu); |
| 34 | +} |
| 35 | + |
| 36 | +static uint64_t dspram_read(void *opaque, hwaddr addr, unsigned size) |
| 37 | +{ |
| 38 | + MIPSDSPRAMState *s = (MIPSDSPRAMState *)opaque; |
| 39 | + |
| 40 | + switch (size) { |
| 41 | + case 1: |
| 42 | + case 2: |
| 43 | + raise_exception(EXCP_AdEL); |
| 44 | + return 0; |
| 45 | + case 4: |
| 46 | + return *(uint32_t *) &s->ramblock[addr % (2 << s->size)]; |
| 47 | + case 8: |
| 48 | + return *(uint64_t *) &s->ramblock[addr % (2 << s->size)]; |
| 49 | + } |
| 50 | + return 0; |
| 51 | +} |
| 52 | + |
| 53 | +static void dspram_write(void *opaque, hwaddr addr, uint64_t data, |
| 54 | + unsigned size) |
| 55 | +{ |
| 56 | + MIPSDSPRAMState *s = (MIPSDSPRAMState *)opaque; |
| 57 | + |
| 58 | + switch (size) { |
| 59 | + case 1: |
| 60 | + case 2: |
| 61 | + raise_exception(EXCP_AdES); |
| 62 | + return; |
| 63 | + case 4: |
| 64 | + *(uint32_t *) &s->ramblock[addr % (2 << s->size)] = (uint32_t) data; |
| 65 | + break; |
| 66 | + case 8: |
| 67 | + *(uint64_t *) &s->ramblock[addr % (2 << s->size)] = data; |
| 68 | + break; |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +void dspram_reconfigure(MIPSDSPRAMState *dspram) |
| 73 | +{ |
| 74 | + MemoryRegion *mr = &dspram->mr; |
| 75 | + hwaddr address; |
| 76 | + bool is_enabled; |
| 77 | + |
| 78 | + address = ((*(uint64_t *) dspram->saar) & 0xFFFFFFFFE000ULL) << 4; |
| 79 | + is_enabled = *(uint64_t *) dspram->saar & 1; |
| 80 | + |
| 81 | + memory_region_transaction_begin(); |
| 82 | + memory_region_set_size(mr, (2 << dspram->size)); |
| 83 | + memory_region_set_address(mr, address); |
| 84 | + memory_region_set_enabled(mr, is_enabled); |
| 85 | + memory_region_transaction_commit(); |
| 86 | +} |
| 87 | + |
| 88 | +static const MemoryRegionOps dspram_ops = { |
| 89 | + .read = dspram_read, |
| 90 | + .write = dspram_write, |
| 91 | + .endianness = DEVICE_NATIVE_ENDIAN, |
| 92 | + .valid = { |
| 93 | + .unaligned = false, |
| 94 | + } |
| 95 | +}; |
| 96 | + |
| 97 | +static void mips_dspram_init(Object *obj) |
| 98 | +{ |
| 99 | + SysBusDevice *sbd = SYS_BUS_DEVICE(obj); |
| 100 | + MIPSDSPRAMState *s = MIPS_DSPRAM(obj); |
| 101 | + |
| 102 | + memory_region_init_io(&s->mr, OBJECT(s), &dspram_ops, s, |
| 103 | + "mips-dspram", (2 << s->size)); |
| 104 | + sysbus_init_mmio(sbd, &s->mr); |
| 105 | +} |
| 106 | + |
| 107 | +static void mips_dspram_realize(DeviceState *dev, Error **errp) |
| 108 | +{ |
| 109 | + MIPSDSPRAMState *s = MIPS_DSPRAM(dev); |
| 110 | + |
| 111 | + /* some error handling here */ |
| 112 | + |
| 113 | + s->ramblock = g_malloc0(2 << s->size); |
| 114 | +} |
| 115 | + |
| 116 | +static void mips_dspram_reset(DeviceState *dev) |
| 117 | +{ |
| 118 | + MIPSDSPRAMState *s = MIPS_DSPRAM(dev); |
| 119 | + |
| 120 | + *(uint64_t *) s->saar = s->size << 1; |
| 121 | + memset(s->ramblock, 0, (2 << s->size)); |
| 122 | +} |
| 123 | + |
| 124 | +static Property mips_dspram_properties[] = { |
| 125 | + DEFINE_PROP_PTR("saar", MIPSDSPRAMState, saar), |
| 126 | + /* default DSPRAM size is 64 KB */ |
| 127 | + DEFINE_PROP_SIZE("size", MIPSDSPRAMState, size, 0x10), |
| 128 | + DEFINE_PROP_END_OF_LIST(), |
| 129 | +}; |
| 130 | + |
| 131 | +static void mips_dspram_class_init(ObjectClass *klass, void *data) |
| 132 | +{ |
| 133 | + DeviceClass *dc = DEVICE_CLASS(klass); |
| 134 | + |
| 135 | + dc->props = mips_dspram_properties; |
| 136 | + dc->realize = mips_dspram_realize; |
| 137 | + dc->reset = mips_dspram_reset; |
| 138 | +} |
| 139 | + |
| 140 | +static const TypeInfo mips_dspram_info = { |
| 141 | + .name = TYPE_MIPS_DSPRAM, |
| 142 | + .parent = TYPE_SYS_BUS_DEVICE, |
| 143 | + .instance_size = sizeof(MIPSDSPRAMState), |
| 144 | + .instance_init = mips_dspram_init, |
| 145 | + .class_init = mips_dspram_class_init, |
| 146 | +}; |
| 147 | + |
| 148 | +static void mips_dspram_register_types(void) |
| 149 | +{ |
| 150 | + type_register_static(&mips_dspram_info); |
| 151 | +} |
| 152 | + |
| 153 | +type_init(mips_dspram_register_types) |
| 154 | + |
0 commit comments