xref: /qemu/hw/misc/mips_itu.c (revision 48e06b64)
134fa7e83SLeon Alrae /*
234fa7e83SLeon Alrae  * Inter-Thread Communication Unit emulation.
334fa7e83SLeon Alrae  *
434fa7e83SLeon Alrae  * Copyright (c) 2016 Imagination Technologies
534fa7e83SLeon Alrae  *
634fa7e83SLeon Alrae  * This library is free software; you can redistribute it and/or
734fa7e83SLeon Alrae  * modify it under the terms of the GNU Lesser General Public
834fa7e83SLeon Alrae  * License as published by the Free Software Foundation; either
9d136ecc0SChetan Pant  * version 2.1 of the License, or (at your option) any later version.
1034fa7e83SLeon Alrae  *
1134fa7e83SLeon Alrae  * This library is distributed in the hope that it will be useful,
1234fa7e83SLeon Alrae  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1334fa7e83SLeon Alrae  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1434fa7e83SLeon Alrae  * Lesser General Public License for more details.
1534fa7e83SLeon Alrae  *
1634fa7e83SLeon Alrae  * You should have received a copy of the GNU Lesser General Public
1734fa7e83SLeon Alrae  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
1834fa7e83SLeon Alrae  */
1934fa7e83SLeon Alrae 
2034fa7e83SLeon Alrae #include "qemu/osdep.h"
21be01029eSPhilippe Mathieu-Daudé #include "qemu/units.h"
22921e1a2aSPhilippe Mathieu-Daudé #include "qemu/log.h"
230b8fa32fSMarkus Armbruster #include "qemu/module.h"
2434fa7e83SLeon Alrae #include "qapi/error.h"
25b5570da7SPhilippe Mathieu-Daudé #include "hw/core/cpu.h"
2634fa7e83SLeon Alrae #include "hw/misc/mips_itu.h"
27a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
28b5570da7SPhilippe Mathieu-Daudé #include "target/mips/cpu.h"
2934fa7e83SLeon Alrae 
3034fa7e83SLeon Alrae #define ITC_TAG_ADDRSPACE_SZ (ITC_ADDRESSMAP_NUM * 8)
3134fa7e83SLeon Alrae /* Initialize as 4kB area to fit all 32 cells with default 128B grain.
3234fa7e83SLeon Alrae    Storage may be resized by the software. */
3334fa7e83SLeon Alrae #define ITC_STORAGE_ADDRSPACE_SZ 0x1000
3434fa7e83SLeon Alrae 
3534fa7e83SLeon Alrae #define ITC_FIFO_NUM_MAX 16
3634fa7e83SLeon Alrae #define ITC_SEMAPH_NUM_MAX 16
3734fa7e83SLeon Alrae #define ITC_AM1_NUMENTRIES_OFS 20
3834fa7e83SLeon Alrae 
3940dc9dc3SLeon Alrae #define ITC_CELL_PV_MAX_VAL 0xFFFF
4040dc9dc3SLeon Alrae 
415924c869SLeon Alrae #define ITC_CELL_TAG_FIFO_DEPTH 28
425924c869SLeon Alrae #define ITC_CELL_TAG_FIFO_PTR 18
435924c869SLeon Alrae #define ITC_CELL_TAG_FIFO 17
445924c869SLeon Alrae #define ITC_CELL_TAG_T 16
455924c869SLeon Alrae #define ITC_CELL_TAG_F 1
465924c869SLeon Alrae #define ITC_CELL_TAG_E 0
475924c869SLeon Alrae 
4834fa7e83SLeon Alrae #define ITC_AM0_BASE_ADDRESS_MASK 0xFFFFFC00ULL
4934fa7e83SLeon Alrae #define ITC_AM0_EN_MASK 0x1
5034fa7e83SLeon Alrae 
5134fa7e83SLeon Alrae #define ITC_AM1_ADDR_MASK_MASK 0x1FC00
5234fa7e83SLeon Alrae #define ITC_AM1_ENTRY_GRAIN_MASK 0x7
5334fa7e83SLeon Alrae 
545924c869SLeon Alrae typedef enum ITCView {
555924c869SLeon Alrae     ITCVIEW_BYPASS  = 0,
565924c869SLeon Alrae     ITCVIEW_CONTROL = 1,
575924c869SLeon Alrae     ITCVIEW_EF_SYNC = 2,
585924c869SLeon Alrae     ITCVIEW_EF_TRY  = 3,
595924c869SLeon Alrae     ITCVIEW_PV_SYNC = 4,
60e5345d96SYongbok Kim     ITCVIEW_PV_TRY  = 5,
61e5345d96SYongbok Kim     ITCVIEW_PV_ICR0 = 15,
625924c869SLeon Alrae } ITCView;
635924c869SLeon Alrae 
64e5345d96SYongbok Kim #define ITC_ICR0_CELL_NUM        16
65e5345d96SYongbok Kim #define ITC_ICR0_BLK_GRAIN       8
66e5345d96SYongbok Kim #define ITC_ICR0_BLK_GRAIN_MASK  0x7
67e5345d96SYongbok Kim #define ITC_ICR0_ERR_AXI         2
68e5345d96SYongbok Kim #define ITC_ICR0_ERR_PARITY      1
69e5345d96SYongbok Kim #define ITC_ICR0_ERR_EXEC        0
70e5345d96SYongbok Kim 
mips_itu_get_tag_region(MIPSITUState * itu)7134fa7e83SLeon Alrae MemoryRegion *mips_itu_get_tag_region(MIPSITUState *itu)
7234fa7e83SLeon Alrae {
7334fa7e83SLeon Alrae     return &itu->tag_io;
7434fa7e83SLeon Alrae }
7534fa7e83SLeon Alrae 
itc_tag_read(void * opaque,hwaddr addr,unsigned size)7634fa7e83SLeon Alrae static uint64_t itc_tag_read(void *opaque, hwaddr addr, unsigned size)
7734fa7e83SLeon Alrae {
7834fa7e83SLeon Alrae     MIPSITUState *tag = (MIPSITUState *)opaque;
7934fa7e83SLeon Alrae     uint64_t index = addr >> 3;
8034fa7e83SLeon Alrae 
81f2eb665aSLeon Alrae     if (index >= ITC_ADDRESSMAP_NUM) {
8234fa7e83SLeon Alrae         qemu_log_mask(LOG_GUEST_ERROR, "Read 0x%" PRIx64 "\n", addr);
83f2eb665aSLeon Alrae         return 0;
8434fa7e83SLeon Alrae     }
8534fa7e83SLeon Alrae 
86f2eb665aSLeon Alrae     return tag->ITCAddressMap[index];
8734fa7e83SLeon Alrae }
8834fa7e83SLeon Alrae 
itc_reconfigure(MIPSITUState * tag)89*77599a69SPhilippe Mathieu-Daudé static void itc_reconfigure(MIPSITUState *tag)
9034fa7e83SLeon Alrae {
9134fa7e83SLeon Alrae     uint64_t *am = &tag->ITCAddressMap[0];
9234fa7e83SLeon Alrae     MemoryRegion *mr = &tag->storage_io;
9334fa7e83SLeon Alrae     hwaddr address = am[0] & ITC_AM0_BASE_ADDRESS_MASK;
94be01029eSPhilippe Mathieu-Daudé     uint64_t size = (1 * KiB) + (am[1] & ITC_AM1_ADDR_MASK_MASK);
9534fa7e83SLeon Alrae     bool is_enabled = (am[0] & ITC_AM0_EN_MASK) != 0;
9634fa7e83SLeon Alrae 
9734fa7e83SLeon Alrae     memory_region_transaction_begin();
9834fa7e83SLeon Alrae     if (!(size & (size - 1))) {
9934fa7e83SLeon Alrae         memory_region_set_size(mr, size);
10034fa7e83SLeon Alrae     }
10134fa7e83SLeon Alrae     memory_region_set_address(mr, address);
10234fa7e83SLeon Alrae     memory_region_set_enabled(mr, is_enabled);
10334fa7e83SLeon Alrae     memory_region_transaction_commit();
10434fa7e83SLeon Alrae }
10534fa7e83SLeon Alrae 
itc_tag_write(void * opaque,hwaddr addr,uint64_t data,unsigned size)10634fa7e83SLeon Alrae static void itc_tag_write(void *opaque, hwaddr addr,
10734fa7e83SLeon Alrae                           uint64_t data, unsigned size)
10834fa7e83SLeon Alrae {
10934fa7e83SLeon Alrae     MIPSITUState *tag = (MIPSITUState *)opaque;
11034fa7e83SLeon Alrae     uint64_t *am = &tag->ITCAddressMap[0];
11134fa7e83SLeon Alrae     uint64_t am_old, mask;
11234fa7e83SLeon Alrae     uint64_t index = addr >> 3;
11334fa7e83SLeon Alrae 
11434fa7e83SLeon Alrae     switch (index) {
11534fa7e83SLeon Alrae     case 0:
11634fa7e83SLeon Alrae         mask = ITC_AM0_BASE_ADDRESS_MASK | ITC_AM0_EN_MASK;
11734fa7e83SLeon Alrae         break;
11834fa7e83SLeon Alrae     case 1:
11934fa7e83SLeon Alrae         mask = ITC_AM1_ADDR_MASK_MASK | ITC_AM1_ENTRY_GRAIN_MASK;
12034fa7e83SLeon Alrae         break;
12134fa7e83SLeon Alrae     default:
12234fa7e83SLeon Alrae         qemu_log_mask(LOG_GUEST_ERROR, "Bad write 0x%" PRIx64 "\n", addr);
12334fa7e83SLeon Alrae         return;
12434fa7e83SLeon Alrae     }
12534fa7e83SLeon Alrae 
12634fa7e83SLeon Alrae     am_old = am[index];
12734fa7e83SLeon Alrae     am[index] = (data & mask) | (am_old & ~mask);
12834fa7e83SLeon Alrae     if (am_old != am[index]) {
12934fa7e83SLeon Alrae         itc_reconfigure(tag);
13034fa7e83SLeon Alrae     }
13134fa7e83SLeon Alrae }
13234fa7e83SLeon Alrae 
13334fa7e83SLeon Alrae static const MemoryRegionOps itc_tag_ops = {
13434fa7e83SLeon Alrae     .read = itc_tag_read,
13534fa7e83SLeon Alrae     .write = itc_tag_write,
13634fa7e83SLeon Alrae     .impl = {
13734fa7e83SLeon Alrae         .max_access_size = 8,
13834fa7e83SLeon Alrae     },
13934fa7e83SLeon Alrae     .endianness = DEVICE_NATIVE_ENDIAN,
14034fa7e83SLeon Alrae };
14134fa7e83SLeon Alrae 
get_num_cells(MIPSITUState * s)14234fa7e83SLeon Alrae static inline uint32_t get_num_cells(MIPSITUState *s)
14334fa7e83SLeon Alrae {
14434fa7e83SLeon Alrae     return s->num_fifo + s->num_semaphores;
14534fa7e83SLeon Alrae }
14634fa7e83SLeon Alrae 
get_itc_view(hwaddr addr)1475924c869SLeon Alrae static inline ITCView get_itc_view(hwaddr addr)
1485924c869SLeon Alrae {
1495924c869SLeon Alrae     return (addr >> 3) & 0xf;
1505924c869SLeon Alrae }
1515924c869SLeon Alrae 
get_cell_stride_shift(const MIPSITUState * s)1525924c869SLeon Alrae static inline int get_cell_stride_shift(const MIPSITUState *s)
1535924c869SLeon Alrae {
1545924c869SLeon Alrae     /* Minimum interval (for EntryGain = 0) is 128 B */
1555924c869SLeon Alrae     return 7 + (s->ITCAddressMap[1] & ITC_AM1_ENTRY_GRAIN_MASK);
1565924c869SLeon Alrae }
1575924c869SLeon Alrae 
get_cell(MIPSITUState * s,hwaddr addr)1585924c869SLeon Alrae static inline ITCStorageCell *get_cell(MIPSITUState *s,
1595924c869SLeon Alrae                                        hwaddr addr)
1605924c869SLeon Alrae {
1615924c869SLeon Alrae     uint32_t cell_idx = addr >> get_cell_stride_shift(s);
1625924c869SLeon Alrae     uint32_t num_cells = get_num_cells(s);
1635924c869SLeon Alrae 
1645924c869SLeon Alrae     if (cell_idx >= num_cells) {
1655924c869SLeon Alrae         cell_idx = num_cells - 1;
1665924c869SLeon Alrae     }
1675924c869SLeon Alrae 
1685924c869SLeon Alrae     return &s->cell[cell_idx];
1695924c869SLeon Alrae }
1705924c869SLeon Alrae 
wake_blocked_threads(ITCStorageCell * c)1714051089dSLeon Alrae static void wake_blocked_threads(ITCStorageCell *c)
1724051089dSLeon Alrae {
1734051089dSLeon Alrae     CPUState *cs;
1744051089dSLeon Alrae     CPU_FOREACH(cs) {
1754051089dSLeon Alrae         if (cs->halted && (c->blocked_threads & (1ULL << cs->cpu_index))) {
1764051089dSLeon Alrae             cpu_interrupt(cs, CPU_INTERRUPT_WAKE);
1774051089dSLeon Alrae         }
1784051089dSLeon Alrae     }
1794051089dSLeon Alrae     c->blocked_threads = 0;
1804051089dSLeon Alrae }
1814051089dSLeon Alrae 
1828905770bSMarc-André Lureau static G_NORETURN
block_thread_and_exit(ITCStorageCell * c)1838905770bSMarc-André Lureau void block_thread_and_exit(ITCStorageCell *c)
1844051089dSLeon Alrae {
1854051089dSLeon Alrae     c->blocked_threads |= 1ULL << current_cpu->cpu_index;
1864051089dSLeon Alrae     current_cpu->halted = 1;
1874051089dSLeon Alrae     current_cpu->exception_index = EXCP_HLT;
188afd46fcaSPavel Dovgalyuk     cpu_loop_exit_restore(current_cpu, current_cpu->mem_io_pc);
1894051089dSLeon Alrae }
1904051089dSLeon Alrae 
19125a611e3SLeon Alrae /* ITC Bypass View */
19225a611e3SLeon Alrae 
view_bypass_read(ITCStorageCell * c)19325a611e3SLeon Alrae static inline uint64_t view_bypass_read(ITCStorageCell *c)
19425a611e3SLeon Alrae {
19525a611e3SLeon Alrae     if (c->tag.FIFO) {
19625a611e3SLeon Alrae         return c->data[c->fifo_out];
19725a611e3SLeon Alrae     } else {
19825a611e3SLeon Alrae         return c->data[0];
19925a611e3SLeon Alrae     }
20025a611e3SLeon Alrae }
20125a611e3SLeon Alrae 
view_bypass_write(ITCStorageCell * c,uint64_t val)20225a611e3SLeon Alrae static inline void view_bypass_write(ITCStorageCell *c, uint64_t val)
20325a611e3SLeon Alrae {
20425a611e3SLeon Alrae     if (c->tag.FIFO && (c->tag.FIFOPtr > 0)) {
20525a611e3SLeon Alrae         int idx = (c->fifo_out + c->tag.FIFOPtr - 1) % ITC_CELL_DEPTH;
20625a611e3SLeon Alrae         c->data[idx] = val;
20725a611e3SLeon Alrae     }
20825a611e3SLeon Alrae 
20925a611e3SLeon Alrae     /* ignore a write to the semaphore cell */
21025a611e3SLeon Alrae }
21125a611e3SLeon Alrae 
2125924c869SLeon Alrae /* ITC Control View */
2135924c869SLeon Alrae 
view_control_read(ITCStorageCell * c)2145924c869SLeon Alrae static inline uint64_t view_control_read(ITCStorageCell *c)
2155924c869SLeon Alrae {
2165924c869SLeon Alrae     return ((uint64_t)c->tag.FIFODepth << ITC_CELL_TAG_FIFO_DEPTH) |
2175924c869SLeon Alrae            (c->tag.FIFOPtr << ITC_CELL_TAG_FIFO_PTR) |
2185924c869SLeon Alrae            (c->tag.FIFO << ITC_CELL_TAG_FIFO) |
2195924c869SLeon Alrae            (c->tag.T << ITC_CELL_TAG_T) |
2205924c869SLeon Alrae            (c->tag.E << ITC_CELL_TAG_E) |
2215924c869SLeon Alrae            (c->tag.F << ITC_CELL_TAG_F);
2225924c869SLeon Alrae }
2235924c869SLeon Alrae 
view_control_write(ITCStorageCell * c,uint64_t val)2245924c869SLeon Alrae static inline void view_control_write(ITCStorageCell *c, uint64_t val)
2255924c869SLeon Alrae {
2265924c869SLeon Alrae     c->tag.T = (val >> ITC_CELL_TAG_T) & 1;
2275924c869SLeon Alrae     c->tag.E = (val >> ITC_CELL_TAG_E) & 1;
2285924c869SLeon Alrae     c->tag.F = (val >> ITC_CELL_TAG_F) & 1;
2295924c869SLeon Alrae 
2305924c869SLeon Alrae     if (c->tag.E) {
2315924c869SLeon Alrae         c->tag.FIFOPtr = 0;
2325924c869SLeon Alrae     }
2335924c869SLeon Alrae }
2345924c869SLeon Alrae 
2354051089dSLeon Alrae /* ITC Empty/Full View */
2364051089dSLeon Alrae 
view_ef_common_read(ITCStorageCell * c,bool blocking)2374051089dSLeon Alrae static uint64_t view_ef_common_read(ITCStorageCell *c, bool blocking)
2384051089dSLeon Alrae {
2394051089dSLeon Alrae     uint64_t ret = 0;
2404051089dSLeon Alrae 
2414051089dSLeon Alrae     if (!c->tag.FIFO) {
2424051089dSLeon Alrae         return 0;
2434051089dSLeon Alrae     }
2444051089dSLeon Alrae 
2454051089dSLeon Alrae     c->tag.F = 0;
2464051089dSLeon Alrae 
2474051089dSLeon Alrae     if (blocking && c->tag.E) {
2484051089dSLeon Alrae         block_thread_and_exit(c);
2494051089dSLeon Alrae     }
2504051089dSLeon Alrae 
2514051089dSLeon Alrae     if (c->blocked_threads) {
2524051089dSLeon Alrae         wake_blocked_threads(c);
2534051089dSLeon Alrae     }
2544051089dSLeon Alrae 
2554051089dSLeon Alrae     if (c->tag.FIFOPtr > 0) {
2564051089dSLeon Alrae         ret = c->data[c->fifo_out];
2574051089dSLeon Alrae         c->fifo_out = (c->fifo_out + 1) % ITC_CELL_DEPTH;
2584051089dSLeon Alrae         c->tag.FIFOPtr--;
2594051089dSLeon Alrae     }
2604051089dSLeon Alrae 
2614051089dSLeon Alrae     if (c->tag.FIFOPtr == 0) {
2624051089dSLeon Alrae         c->tag.E = 1;
2634051089dSLeon Alrae     }
2644051089dSLeon Alrae 
2654051089dSLeon Alrae     return ret;
2664051089dSLeon Alrae }
2674051089dSLeon Alrae 
view_ef_sync_read(ITCStorageCell * c)2684051089dSLeon Alrae static uint64_t view_ef_sync_read(ITCStorageCell *c)
2694051089dSLeon Alrae {
2704051089dSLeon Alrae     return view_ef_common_read(c, true);
2714051089dSLeon Alrae }
2724051089dSLeon Alrae 
view_ef_try_read(ITCStorageCell * c)2734051089dSLeon Alrae static uint64_t view_ef_try_read(ITCStorageCell *c)
2744051089dSLeon Alrae {
2754051089dSLeon Alrae     return view_ef_common_read(c, false);
2764051089dSLeon Alrae }
2774051089dSLeon Alrae 
view_ef_common_write(ITCStorageCell * c,uint64_t val,bool blocking)2784051089dSLeon Alrae static inline void view_ef_common_write(ITCStorageCell *c, uint64_t val,
2794051089dSLeon Alrae                                         bool blocking)
2804051089dSLeon Alrae {
2814051089dSLeon Alrae     if (!c->tag.FIFO) {
2824051089dSLeon Alrae         return;
2834051089dSLeon Alrae     }
2844051089dSLeon Alrae 
2854051089dSLeon Alrae     c->tag.E = 0;
2864051089dSLeon Alrae 
2874051089dSLeon Alrae     if (blocking && c->tag.F) {
2884051089dSLeon Alrae         block_thread_and_exit(c);
2894051089dSLeon Alrae     }
2904051089dSLeon Alrae 
2914051089dSLeon Alrae     if (c->blocked_threads) {
2924051089dSLeon Alrae         wake_blocked_threads(c);
2934051089dSLeon Alrae     }
2944051089dSLeon Alrae 
2954051089dSLeon Alrae     if (c->tag.FIFOPtr < ITC_CELL_DEPTH) {
2964051089dSLeon Alrae         int idx = (c->fifo_out + c->tag.FIFOPtr) % ITC_CELL_DEPTH;
2974051089dSLeon Alrae         c->data[idx] = val;
2984051089dSLeon Alrae         c->tag.FIFOPtr++;
2994051089dSLeon Alrae     }
3004051089dSLeon Alrae 
3014051089dSLeon Alrae     if (c->tag.FIFOPtr == ITC_CELL_DEPTH) {
3024051089dSLeon Alrae         c->tag.F = 1;
3034051089dSLeon Alrae     }
3044051089dSLeon Alrae }
3054051089dSLeon Alrae 
view_ef_sync_write(ITCStorageCell * c,uint64_t val)3064051089dSLeon Alrae static void view_ef_sync_write(ITCStorageCell *c, uint64_t val)
3074051089dSLeon Alrae {
3084051089dSLeon Alrae     view_ef_common_write(c, val, true);
3094051089dSLeon Alrae }
3104051089dSLeon Alrae 
view_ef_try_write(ITCStorageCell * c,uint64_t val)3114051089dSLeon Alrae static void view_ef_try_write(ITCStorageCell *c, uint64_t val)
3124051089dSLeon Alrae {
3134051089dSLeon Alrae     view_ef_common_write(c, val, false);
3144051089dSLeon Alrae }
3154051089dSLeon Alrae 
31640dc9dc3SLeon Alrae /* ITC P/V View */
31740dc9dc3SLeon Alrae 
view_pv_common_read(ITCStorageCell * c,bool blocking)31840dc9dc3SLeon Alrae static uint64_t view_pv_common_read(ITCStorageCell *c, bool blocking)
31940dc9dc3SLeon Alrae {
32040dc9dc3SLeon Alrae     uint64_t ret = c->data[0];
32140dc9dc3SLeon Alrae 
32240dc9dc3SLeon Alrae     if (c->tag.FIFO) {
32340dc9dc3SLeon Alrae         return 0;
32440dc9dc3SLeon Alrae     }
32540dc9dc3SLeon Alrae 
32640dc9dc3SLeon Alrae     if (c->data[0] > 0) {
32740dc9dc3SLeon Alrae         c->data[0]--;
32840dc9dc3SLeon Alrae     } else if (blocking) {
32940dc9dc3SLeon Alrae         block_thread_and_exit(c);
33040dc9dc3SLeon Alrae     }
33140dc9dc3SLeon Alrae 
33240dc9dc3SLeon Alrae     return ret;
33340dc9dc3SLeon Alrae }
33440dc9dc3SLeon Alrae 
view_pv_sync_read(ITCStorageCell * c)33540dc9dc3SLeon Alrae static uint64_t view_pv_sync_read(ITCStorageCell *c)
33640dc9dc3SLeon Alrae {
33740dc9dc3SLeon Alrae     return view_pv_common_read(c, true);
33840dc9dc3SLeon Alrae }
33940dc9dc3SLeon Alrae 
view_pv_try_read(ITCStorageCell * c)34040dc9dc3SLeon Alrae static uint64_t view_pv_try_read(ITCStorageCell *c)
34140dc9dc3SLeon Alrae {
34240dc9dc3SLeon Alrae     return view_pv_common_read(c, false);
34340dc9dc3SLeon Alrae }
34440dc9dc3SLeon Alrae 
view_pv_common_write(ITCStorageCell * c)34540dc9dc3SLeon Alrae static inline void view_pv_common_write(ITCStorageCell *c)
34640dc9dc3SLeon Alrae {
34740dc9dc3SLeon Alrae     if (c->tag.FIFO) {
34840dc9dc3SLeon Alrae         return;
34940dc9dc3SLeon Alrae     }
35040dc9dc3SLeon Alrae 
35140dc9dc3SLeon Alrae     if (c->data[0] < ITC_CELL_PV_MAX_VAL) {
35240dc9dc3SLeon Alrae         c->data[0]++;
35340dc9dc3SLeon Alrae     }
35440dc9dc3SLeon Alrae 
35540dc9dc3SLeon Alrae     if (c->blocked_threads) {
35640dc9dc3SLeon Alrae         wake_blocked_threads(c);
35740dc9dc3SLeon Alrae     }
35840dc9dc3SLeon Alrae }
35940dc9dc3SLeon Alrae 
view_pv_sync_write(ITCStorageCell * c)36040dc9dc3SLeon Alrae static void view_pv_sync_write(ITCStorageCell *c)
36140dc9dc3SLeon Alrae {
36240dc9dc3SLeon Alrae     view_pv_common_write(c);
36340dc9dc3SLeon Alrae }
36440dc9dc3SLeon Alrae 
view_pv_try_write(ITCStorageCell * c)36540dc9dc3SLeon Alrae static void view_pv_try_write(ITCStorageCell *c)
36640dc9dc3SLeon Alrae {
36740dc9dc3SLeon Alrae     view_pv_common_write(c);
36840dc9dc3SLeon Alrae }
36940dc9dc3SLeon Alrae 
raise_exception(int excp)37040cd7180SYongbok Kim static void raise_exception(int excp)
37140cd7180SYongbok Kim {
37240cd7180SYongbok Kim     current_cpu->exception_index = excp;
37340cd7180SYongbok Kim     cpu_loop_exit(current_cpu);
37440cd7180SYongbok Kim }
37540cd7180SYongbok Kim 
itc_storage_read(void * opaque,hwaddr addr,unsigned size)3765924c869SLeon Alrae static uint64_t itc_storage_read(void *opaque, hwaddr addr, unsigned size)
3775924c869SLeon Alrae {
3785924c869SLeon Alrae     MIPSITUState *s = (MIPSITUState *)opaque;
3795924c869SLeon Alrae     ITCStorageCell *cell = get_cell(s, addr);
3805924c869SLeon Alrae     ITCView view = get_itc_view(addr);
3815924c869SLeon Alrae     uint64_t ret = -1;
3825924c869SLeon Alrae 
38340cd7180SYongbok Kim     switch (size) {
38440cd7180SYongbok Kim     case 1:
38540cd7180SYongbok Kim     case 2:
38640cd7180SYongbok Kim         s->icr0 |= 1 << ITC_ICR0_ERR_AXI;
38740cd7180SYongbok Kim         raise_exception(EXCP_DBE);
38840cd7180SYongbok Kim         return 0;
38940cd7180SYongbok Kim     }
39040cd7180SYongbok Kim 
3915924c869SLeon Alrae     switch (view) {
39225a611e3SLeon Alrae     case ITCVIEW_BYPASS:
39325a611e3SLeon Alrae         ret = view_bypass_read(cell);
39425a611e3SLeon Alrae         break;
3955924c869SLeon Alrae     case ITCVIEW_CONTROL:
3965924c869SLeon Alrae         ret = view_control_read(cell);
3975924c869SLeon Alrae         break;
3984051089dSLeon Alrae     case ITCVIEW_EF_SYNC:
3994051089dSLeon Alrae         ret = view_ef_sync_read(cell);
4004051089dSLeon Alrae         break;
4014051089dSLeon Alrae     case ITCVIEW_EF_TRY:
4024051089dSLeon Alrae         ret = view_ef_try_read(cell);
4034051089dSLeon Alrae         break;
40440dc9dc3SLeon Alrae     case ITCVIEW_PV_SYNC:
40540dc9dc3SLeon Alrae         ret = view_pv_sync_read(cell);
40640dc9dc3SLeon Alrae         break;
40740dc9dc3SLeon Alrae     case ITCVIEW_PV_TRY:
40840dc9dc3SLeon Alrae         ret = view_pv_try_read(cell);
40940dc9dc3SLeon Alrae         break;
410e5345d96SYongbok Kim     case ITCVIEW_PV_ICR0:
411e5345d96SYongbok Kim         ret = s->icr0;
412e5345d96SYongbok Kim         break;
4135924c869SLeon Alrae     default:
4145924c869SLeon Alrae         qemu_log_mask(LOG_GUEST_ERROR,
4155924c869SLeon Alrae                       "itc_storage_read: Bad ITC View %d\n", (int)view);
4165924c869SLeon Alrae         break;
4175924c869SLeon Alrae     }
4185924c869SLeon Alrae 
4195924c869SLeon Alrae     return ret;
4205924c869SLeon Alrae }
4215924c869SLeon Alrae 
itc_storage_write(void * opaque,hwaddr addr,uint64_t data,unsigned size)4225924c869SLeon Alrae static void itc_storage_write(void *opaque, hwaddr addr, uint64_t data,
4235924c869SLeon Alrae                               unsigned size)
4245924c869SLeon Alrae {
4255924c869SLeon Alrae     MIPSITUState *s = (MIPSITUState *)opaque;
4265924c869SLeon Alrae     ITCStorageCell *cell = get_cell(s, addr);
4275924c869SLeon Alrae     ITCView view = get_itc_view(addr);
4285924c869SLeon Alrae 
42940cd7180SYongbok Kim     switch (size) {
43040cd7180SYongbok Kim     case 1:
43140cd7180SYongbok Kim     case 2:
43240cd7180SYongbok Kim         s->icr0 |= 1 << ITC_ICR0_ERR_AXI;
43340cd7180SYongbok Kim         raise_exception(EXCP_DBE);
43440cd7180SYongbok Kim         return;
43540cd7180SYongbok Kim     }
43640cd7180SYongbok Kim 
4375924c869SLeon Alrae     switch (view) {
43825a611e3SLeon Alrae     case ITCVIEW_BYPASS:
43925a611e3SLeon Alrae         view_bypass_write(cell, data);
44025a611e3SLeon Alrae         break;
4415924c869SLeon Alrae     case ITCVIEW_CONTROL:
4425924c869SLeon Alrae         view_control_write(cell, data);
4435924c869SLeon Alrae         break;
4444051089dSLeon Alrae     case ITCVIEW_EF_SYNC:
4454051089dSLeon Alrae         view_ef_sync_write(cell, data);
4464051089dSLeon Alrae         break;
4474051089dSLeon Alrae     case ITCVIEW_EF_TRY:
4484051089dSLeon Alrae         view_ef_try_write(cell, data);
4494051089dSLeon Alrae         break;
45040dc9dc3SLeon Alrae     case ITCVIEW_PV_SYNC:
45140dc9dc3SLeon Alrae         view_pv_sync_write(cell);
45240dc9dc3SLeon Alrae         break;
45340dc9dc3SLeon Alrae     case ITCVIEW_PV_TRY:
45440dc9dc3SLeon Alrae         view_pv_try_write(cell);
45540dc9dc3SLeon Alrae         break;
456e5345d96SYongbok Kim     case ITCVIEW_PV_ICR0:
457e5345d96SYongbok Kim         if (data & 0x7) {
458e5345d96SYongbok Kim             /* clear ERROR bits */
459e5345d96SYongbok Kim             s->icr0 &= ~(data & 0x7);
460e5345d96SYongbok Kim         }
461e5345d96SYongbok Kim         /* set BLK_GRAIN */
462e5345d96SYongbok Kim         s->icr0 &= ~0x700;
463e5345d96SYongbok Kim         s->icr0 |= data & 0x700;
464e5345d96SYongbok Kim         break;
4655924c869SLeon Alrae     default:
4665924c869SLeon Alrae         qemu_log_mask(LOG_GUEST_ERROR,
4675924c869SLeon Alrae                       "itc_storage_write: Bad ITC View %d\n", (int)view);
4685924c869SLeon Alrae         break;
4695924c869SLeon Alrae     }
4705924c869SLeon Alrae 
4715924c869SLeon Alrae }
4725924c869SLeon Alrae 
47334fa7e83SLeon Alrae static const MemoryRegionOps itc_storage_ops = {
4745924c869SLeon Alrae     .read = itc_storage_read,
4755924c869SLeon Alrae     .write = itc_storage_write,
47634fa7e83SLeon Alrae     .endianness = DEVICE_NATIVE_ENDIAN,
47734fa7e83SLeon Alrae };
47834fa7e83SLeon Alrae 
itc_reset_cells(MIPSITUState * s)47934fa7e83SLeon Alrae static void itc_reset_cells(MIPSITUState *s)
48034fa7e83SLeon Alrae {
48134fa7e83SLeon Alrae     int i;
48234fa7e83SLeon Alrae 
48334fa7e83SLeon Alrae     memset(s->cell, 0, get_num_cells(s) * sizeof(s->cell[0]));
48434fa7e83SLeon Alrae 
48534fa7e83SLeon Alrae     for (i = 0; i < s->num_fifo; i++) {
48634fa7e83SLeon Alrae         s->cell[i].tag.E = 1;
48734fa7e83SLeon Alrae         s->cell[i].tag.FIFO = 1;
48834fa7e83SLeon Alrae         s->cell[i].tag.FIFODepth = ITC_CELL_DEPTH_SHIFT;
48934fa7e83SLeon Alrae     }
49034fa7e83SLeon Alrae }
49134fa7e83SLeon Alrae 
mips_itu_init(Object * obj)49234fa7e83SLeon Alrae static void mips_itu_init(Object *obj)
49334fa7e83SLeon Alrae {
49434fa7e83SLeon Alrae     SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
49534fa7e83SLeon Alrae     MIPSITUState *s = MIPS_ITU(obj);
49634fa7e83SLeon Alrae 
49734fa7e83SLeon Alrae     memory_region_init_io(&s->storage_io, OBJECT(s), &itc_storage_ops, s,
49834fa7e83SLeon Alrae                           "mips-itc-storage", ITC_STORAGE_ADDRSPACE_SZ);
49934fa7e83SLeon Alrae     sysbus_init_mmio(sbd, &s->storage_io);
50034fa7e83SLeon Alrae 
50134fa7e83SLeon Alrae     memory_region_init_io(&s->tag_io, OBJECT(s), &itc_tag_ops, s,
50234fa7e83SLeon Alrae                           "mips-itc-tag", ITC_TAG_ADDRSPACE_SZ);
50334fa7e83SLeon Alrae }
50434fa7e83SLeon Alrae 
mips_itu_realize(DeviceState * dev,Error ** errp)50534fa7e83SLeon Alrae static void mips_itu_realize(DeviceState *dev, Error **errp)
50634fa7e83SLeon Alrae {
50734fa7e83SLeon Alrae     MIPSITUState *s = MIPS_ITU(dev);
50834fa7e83SLeon Alrae 
50934fa7e83SLeon Alrae     if (s->num_fifo > ITC_FIFO_NUM_MAX) {
51034fa7e83SLeon Alrae         error_setg(errp, "Exceed maximum number of FIFO cells: %d",
51134fa7e83SLeon Alrae                    s->num_fifo);
51234fa7e83SLeon Alrae         return;
51334fa7e83SLeon Alrae     }
51434fa7e83SLeon Alrae     if (s->num_semaphores > ITC_SEMAPH_NUM_MAX) {
51534fa7e83SLeon Alrae         error_setg(errp, "Exceed maximum number of Semaphore cells: %d",
51634fa7e83SLeon Alrae                    s->num_semaphores);
51734fa7e83SLeon Alrae         return;
51834fa7e83SLeon Alrae     }
5194c921e3fSPhilippe Mathieu-Daudé 
52034fa7e83SLeon Alrae     s->cell = g_new(ITCStorageCell, get_num_cells(s));
52134fa7e83SLeon Alrae }
52234fa7e83SLeon Alrae 
mips_itu_reset(DeviceState * dev)52334fa7e83SLeon Alrae static void mips_itu_reset(DeviceState *dev)
52434fa7e83SLeon Alrae {
52534fa7e83SLeon Alrae     MIPSITUState *s = MIPS_ITU(dev);
52634fa7e83SLeon Alrae 
52734fa7e83SLeon Alrae     s->ITCAddressMap[0] = 0;
52834fa7e83SLeon Alrae     s->ITCAddressMap[1] =
52934fa7e83SLeon Alrae             ((ITC_STORAGE_ADDRSPACE_SZ - 1) & ITC_AM1_ADDR_MASK_MASK) |
53034fa7e83SLeon Alrae             (get_num_cells(s) << ITC_AM1_NUMENTRIES_OFS);
53134fa7e83SLeon Alrae     itc_reconfigure(s);
53234fa7e83SLeon Alrae 
53334fa7e83SLeon Alrae     itc_reset_cells(s);
53434fa7e83SLeon Alrae }
53534fa7e83SLeon Alrae 
53634fa7e83SLeon Alrae static Property mips_itu_properties[] = {
53710997f2dSPhilippe Mathieu-Daudé     DEFINE_PROP_UINT32("num-fifo", MIPSITUState, num_fifo,
53834fa7e83SLeon Alrae                       ITC_FIFO_NUM_MAX),
53910997f2dSPhilippe Mathieu-Daudé     DEFINE_PROP_UINT32("num-semaphores", MIPSITUState, num_semaphores,
54034fa7e83SLeon Alrae                       ITC_SEMAPH_NUM_MAX),
54134fa7e83SLeon Alrae     DEFINE_PROP_END_OF_LIST(),
54234fa7e83SLeon Alrae };
54334fa7e83SLeon Alrae 
mips_itu_class_init(ObjectClass * klass,void * data)54434fa7e83SLeon Alrae static void mips_itu_class_init(ObjectClass *klass, void *data)
54534fa7e83SLeon Alrae {
54634fa7e83SLeon Alrae     DeviceClass *dc = DEVICE_CLASS(klass);
54734fa7e83SLeon Alrae 
5484f67d30bSMarc-André Lureau     device_class_set_props(dc, mips_itu_properties);
54934fa7e83SLeon Alrae     dc->realize = mips_itu_realize;
55034fa7e83SLeon Alrae     dc->reset = mips_itu_reset;
55134fa7e83SLeon Alrae }
55234fa7e83SLeon Alrae 
55334fa7e83SLeon Alrae static const TypeInfo mips_itu_info = {
55434fa7e83SLeon Alrae     .name          = TYPE_MIPS_ITU,
55534fa7e83SLeon Alrae     .parent        = TYPE_SYS_BUS_DEVICE,
55634fa7e83SLeon Alrae     .instance_size = sizeof(MIPSITUState),
55734fa7e83SLeon Alrae     .instance_init = mips_itu_init,
55834fa7e83SLeon Alrae     .class_init    = mips_itu_class_init,
55934fa7e83SLeon Alrae };
56034fa7e83SLeon Alrae 
mips_itu_register_types(void)56134fa7e83SLeon Alrae static void mips_itu_register_types(void)
56234fa7e83SLeon Alrae {
56334fa7e83SLeon Alrae     type_register_static(&mips_itu_info);
56434fa7e83SLeon Alrae }
56534fa7e83SLeon Alrae 
56634fa7e83SLeon Alrae type_init(mips_itu_register_types)
567