2 * Inter-Thread Communication Unit emulation.
4 * Copyright (c) 2016 Imagination Technologies
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.
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.
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/>.
20 #include "qemu/osdep.h"
21 #include "qapi/error.h"
23 #include "hw/sysbus.h"
24 #include "sysemu/sysemu.h"
25 #include "hw/misc/mips_itu.h"
27 #define ITC_TAG_ADDRSPACE_SZ (ITC_ADDRESSMAP_NUM * 8)
28 /* Initialize as 4kB area to fit all 32 cells with default 128B grain.
29 Storage may be resized by the software. */
30 #define ITC_STORAGE_ADDRSPACE_SZ 0x1000
32 #define ITC_FIFO_NUM_MAX 16
33 #define ITC_SEMAPH_NUM_MAX 16
34 #define ITC_AM1_NUMENTRIES_OFS 20
36 #define ITC_CELL_TAG_FIFO_DEPTH 28
37 #define ITC_CELL_TAG_FIFO_PTR 18
38 #define ITC_CELL_TAG_FIFO 17
39 #define ITC_CELL_TAG_T 16
40 #define ITC_CELL_TAG_F 1
41 #define ITC_CELL_TAG_E 0
43 #define ITC_AM0_BASE_ADDRESS_MASK 0xFFFFFC00ULL
44 #define ITC_AM0_EN_MASK 0x1
46 #define ITC_AM1_ADDR_MASK_MASK 0x1FC00
47 #define ITC_AM1_ENTRY_GRAIN_MASK 0x7
49 typedef enum ITCView {
58 MemoryRegion *mips_itu_get_tag_region(MIPSITUState *itu)
63 static uint64_t itc_tag_read(void *opaque, hwaddr addr, unsigned size)
65 MIPSITUState *tag = (MIPSITUState *)opaque;
66 uint64_t index = addr >> 3;
70 case 0 ... ITC_ADDRESSMAP_NUM:
71 ret = tag->ITCAddressMap[index];
74 qemu_log_mask(LOG_GUEST_ERROR, "Read 0x%" PRIx64 "\n", addr);
81 static void itc_reconfigure(MIPSITUState *tag)
83 uint64_t *am = &tag->ITCAddressMap[0];
84 MemoryRegion *mr = &tag->storage_io;
85 hwaddr address = am[0] & ITC_AM0_BASE_ADDRESS_MASK;
86 uint64_t size = (1 << 10) + (am[1] & ITC_AM1_ADDR_MASK_MASK);
87 bool is_enabled = (am[0] & ITC_AM0_EN_MASK) != 0;
89 memory_region_transaction_begin();
90 if (!(size & (size - 1))) {
91 memory_region_set_size(mr, size);
93 memory_region_set_address(mr, address);
94 memory_region_set_enabled(mr, is_enabled);
95 memory_region_transaction_commit();
98 static void itc_tag_write(void *opaque, hwaddr addr,
99 uint64_t data, unsigned size)
101 MIPSITUState *tag = (MIPSITUState *)opaque;
102 uint64_t *am = &tag->ITCAddressMap[0];
103 uint64_t am_old, mask;
104 uint64_t index = addr >> 3;
108 mask = ITC_AM0_BASE_ADDRESS_MASK | ITC_AM0_EN_MASK;
111 mask = ITC_AM1_ADDR_MASK_MASK | ITC_AM1_ENTRY_GRAIN_MASK;
114 qemu_log_mask(LOG_GUEST_ERROR, "Bad write 0x%" PRIx64 "\n", addr);
119 am[index] = (data & mask) | (am_old & ~mask);
120 if (am_old != am[index]) {
121 itc_reconfigure(tag);
125 static const MemoryRegionOps itc_tag_ops = {
126 .read = itc_tag_read,
127 .write = itc_tag_write,
129 .max_access_size = 8,
131 .endianness = DEVICE_NATIVE_ENDIAN,
134 static inline uint32_t get_num_cells(MIPSITUState *s)
136 return s->num_fifo + s->num_semaphores;
139 static inline ITCView get_itc_view(hwaddr addr)
141 return (addr >> 3) & 0xf;
144 static inline int get_cell_stride_shift(const MIPSITUState *s)
146 /* Minimum interval (for EntryGain = 0) is 128 B */
147 return 7 + (s->ITCAddressMap[1] & ITC_AM1_ENTRY_GRAIN_MASK);
150 static inline ITCStorageCell *get_cell(MIPSITUState *s,
153 uint32_t cell_idx = addr >> get_cell_stride_shift(s);
154 uint32_t num_cells = get_num_cells(s);
156 if (cell_idx >= num_cells) {
157 cell_idx = num_cells - 1;
160 return &s->cell[cell_idx];
163 static void wake_blocked_threads(ITCStorageCell *c)
167 if (cs->halted && (c->blocked_threads & (1ULL << cs->cpu_index))) {
168 cpu_interrupt(cs, CPU_INTERRUPT_WAKE);
171 c->blocked_threads = 0;
174 static void QEMU_NORETURN block_thread_and_exit(ITCStorageCell *c)
176 c->blocked_threads |= 1ULL << current_cpu->cpu_index;
177 cpu_restore_state(current_cpu, current_cpu->mem_io_pc);
178 current_cpu->halted = 1;
179 current_cpu->exception_index = EXCP_HLT;
180 cpu_loop_exit(current_cpu);
183 /* ITC Control View */
185 static inline uint64_t view_control_read(ITCStorageCell *c)
187 return ((uint64_t)c->tag.FIFODepth << ITC_CELL_TAG_FIFO_DEPTH) |
188 (c->tag.FIFOPtr << ITC_CELL_TAG_FIFO_PTR) |
189 (c->tag.FIFO << ITC_CELL_TAG_FIFO) |
190 (c->tag.T << ITC_CELL_TAG_T) |
191 (c->tag.E << ITC_CELL_TAG_E) |
192 (c->tag.F << ITC_CELL_TAG_F);
195 static inline void view_control_write(ITCStorageCell *c, uint64_t val)
197 c->tag.T = (val >> ITC_CELL_TAG_T) & 1;
198 c->tag.E = (val >> ITC_CELL_TAG_E) & 1;
199 c->tag.F = (val >> ITC_CELL_TAG_F) & 1;
206 /* ITC Empty/Full View */
208 static uint64_t view_ef_common_read(ITCStorageCell *c, bool blocking)
218 if (blocking && c->tag.E) {
219 block_thread_and_exit(c);
222 if (c->blocked_threads) {
223 wake_blocked_threads(c);
226 if (c->tag.FIFOPtr > 0) {
227 ret = c->data[c->fifo_out];
228 c->fifo_out = (c->fifo_out + 1) % ITC_CELL_DEPTH;
232 if (c->tag.FIFOPtr == 0) {
239 static uint64_t view_ef_sync_read(ITCStorageCell *c)
241 return view_ef_common_read(c, true);
244 static uint64_t view_ef_try_read(ITCStorageCell *c)
246 return view_ef_common_read(c, false);
249 static inline void view_ef_common_write(ITCStorageCell *c, uint64_t val,
258 if (blocking && c->tag.F) {
259 block_thread_and_exit(c);
262 if (c->blocked_threads) {
263 wake_blocked_threads(c);
266 if (c->tag.FIFOPtr < ITC_CELL_DEPTH) {
267 int idx = (c->fifo_out + c->tag.FIFOPtr) % ITC_CELL_DEPTH;
272 if (c->tag.FIFOPtr == ITC_CELL_DEPTH) {
277 static void view_ef_sync_write(ITCStorageCell *c, uint64_t val)
279 view_ef_common_write(c, val, true);
282 static void view_ef_try_write(ITCStorageCell *c, uint64_t val)
284 view_ef_common_write(c, val, false);
287 static uint64_t itc_storage_read(void *opaque, hwaddr addr, unsigned size)
289 MIPSITUState *s = (MIPSITUState *)opaque;
290 ITCStorageCell *cell = get_cell(s, addr);
291 ITCView view = get_itc_view(addr);
295 case ITCVIEW_CONTROL:
296 ret = view_control_read(cell);
298 case ITCVIEW_EF_SYNC:
299 ret = view_ef_sync_read(cell);
302 ret = view_ef_try_read(cell);
305 qemu_log_mask(LOG_GUEST_ERROR,
306 "itc_storage_read: Bad ITC View %d\n", (int)view);
313 static void itc_storage_write(void *opaque, hwaddr addr, uint64_t data,
316 MIPSITUState *s = (MIPSITUState *)opaque;
317 ITCStorageCell *cell = get_cell(s, addr);
318 ITCView view = get_itc_view(addr);
321 case ITCVIEW_CONTROL:
322 view_control_write(cell, data);
324 case ITCVIEW_EF_SYNC:
325 view_ef_sync_write(cell, data);
328 view_ef_try_write(cell, data);
331 qemu_log_mask(LOG_GUEST_ERROR,
332 "itc_storage_write: Bad ITC View %d\n", (int)view);
338 static const MemoryRegionOps itc_storage_ops = {
339 .read = itc_storage_read,
340 .write = itc_storage_write,
341 .endianness = DEVICE_NATIVE_ENDIAN,
344 static void itc_reset_cells(MIPSITUState *s)
348 memset(s->cell, 0, get_num_cells(s) * sizeof(s->cell[0]));
350 for (i = 0; i < s->num_fifo; i++) {
351 s->cell[i].tag.E = 1;
352 s->cell[i].tag.FIFO = 1;
353 s->cell[i].tag.FIFODepth = ITC_CELL_DEPTH_SHIFT;
357 static void mips_itu_init(Object *obj)
359 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
360 MIPSITUState *s = MIPS_ITU(obj);
362 memory_region_init_io(&s->storage_io, OBJECT(s), &itc_storage_ops, s,
363 "mips-itc-storage", ITC_STORAGE_ADDRSPACE_SZ);
364 sysbus_init_mmio(sbd, &s->storage_io);
366 memory_region_init_io(&s->tag_io, OBJECT(s), &itc_tag_ops, s,
367 "mips-itc-tag", ITC_TAG_ADDRSPACE_SZ);
370 static void mips_itu_realize(DeviceState *dev, Error **errp)
372 MIPSITUState *s = MIPS_ITU(dev);
374 if (s->num_fifo > ITC_FIFO_NUM_MAX) {
375 error_setg(errp, "Exceed maximum number of FIFO cells: %d",
379 if (s->num_semaphores > ITC_SEMAPH_NUM_MAX) {
380 error_setg(errp, "Exceed maximum number of Semaphore cells: %d",
385 s->cell = g_new(ITCStorageCell, get_num_cells(s));
388 static void mips_itu_reset(DeviceState *dev)
390 MIPSITUState *s = MIPS_ITU(dev);
392 s->ITCAddressMap[0] = 0;
393 s->ITCAddressMap[1] =
394 ((ITC_STORAGE_ADDRSPACE_SZ - 1) & ITC_AM1_ADDR_MASK_MASK) |
395 (get_num_cells(s) << ITC_AM1_NUMENTRIES_OFS);
401 static Property mips_itu_properties[] = {
402 DEFINE_PROP_INT32("num-fifo", MIPSITUState, num_fifo,
404 DEFINE_PROP_INT32("num-semaphores", MIPSITUState, num_semaphores,
406 DEFINE_PROP_END_OF_LIST(),
409 static void mips_itu_class_init(ObjectClass *klass, void *data)
411 DeviceClass *dc = DEVICE_CLASS(klass);
413 dc->props = mips_itu_properties;
414 dc->realize = mips_itu_realize;
415 dc->reset = mips_itu_reset;
418 static const TypeInfo mips_itu_info = {
419 .name = TYPE_MIPS_ITU,
420 .parent = TYPE_SYS_BUS_DEVICE,
421 .instance_size = sizeof(MIPSITUState),
422 .instance_init = mips_itu_init,
423 .class_init = mips_itu_class_init,
426 static void mips_itu_register_types(void)
428 type_register_static(&mips_itu_info);
431 type_init(mips_itu_register_types)