xref: /openbmc/qemu/target/sparc/int32_helper.c (revision 5650b5497e92e6136a633ec6291c81ab8fc610a0)
1fcf5ef2aSThomas Huth /*
2fcf5ef2aSThomas Huth  * Sparc32 interrupt helpers
3fcf5ef2aSThomas Huth  *
4fcf5ef2aSThomas Huth  *  Copyright (c) 2003-2005 Fabrice Bellard
5fcf5ef2aSThomas Huth  *
6fcf5ef2aSThomas Huth  * This library is free software; you can redistribute it and/or
7fcf5ef2aSThomas Huth  * modify it under the terms of the GNU Lesser General Public
8fcf5ef2aSThomas Huth  * License as published by the Free Software Foundation; either
9*5650b549SChetan Pant  * version 2.1 of the License, or (at your option) any later version.
10fcf5ef2aSThomas Huth  *
11fcf5ef2aSThomas Huth  * This library is distributed in the hope that it will be useful,
12fcf5ef2aSThomas Huth  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13fcf5ef2aSThomas Huth  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14fcf5ef2aSThomas Huth  * Lesser General Public License for more details.
15fcf5ef2aSThomas Huth  *
16fcf5ef2aSThomas Huth  * You should have received a copy of the GNU Lesser General Public
17fcf5ef2aSThomas Huth  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18fcf5ef2aSThomas Huth  */
19fcf5ef2aSThomas Huth 
20fcf5ef2aSThomas Huth #include "qemu/osdep.h"
21fcf5ef2aSThomas Huth #include "cpu.h"
22fcf5ef2aSThomas Huth #include "trace.h"
23fcf5ef2aSThomas Huth #include "exec/log.h"
2454d31236SMarkus Armbruster #include "sysemu/runstate.h"
25fcf5ef2aSThomas Huth 
26fcf5ef2aSThomas Huth 
27fcf5ef2aSThomas Huth static const char * const excp_names[0x80] = {
28fcf5ef2aSThomas Huth     [TT_TFAULT] = "Instruction Access Fault",
29fcf5ef2aSThomas Huth     [TT_ILL_INSN] = "Illegal Instruction",
30fcf5ef2aSThomas Huth     [TT_PRIV_INSN] = "Privileged Instruction",
31fcf5ef2aSThomas Huth     [TT_NFPU_INSN] = "FPU Disabled",
32fcf5ef2aSThomas Huth     [TT_WIN_OVF] = "Window Overflow",
33fcf5ef2aSThomas Huth     [TT_WIN_UNF] = "Window Underflow",
34fcf5ef2aSThomas Huth     [TT_UNALIGNED] = "Unaligned Memory Access",
35fcf5ef2aSThomas Huth     [TT_FP_EXCP] = "FPU Exception",
36fcf5ef2aSThomas Huth     [TT_DFAULT] = "Data Access Fault",
37fcf5ef2aSThomas Huth     [TT_TOVF] = "Tag Overflow",
38fcf5ef2aSThomas Huth     [TT_EXTINT | 0x1] = "External Interrupt 1",
39fcf5ef2aSThomas Huth     [TT_EXTINT | 0x2] = "External Interrupt 2",
40fcf5ef2aSThomas Huth     [TT_EXTINT | 0x3] = "External Interrupt 3",
41fcf5ef2aSThomas Huth     [TT_EXTINT | 0x4] = "External Interrupt 4",
42fcf5ef2aSThomas Huth     [TT_EXTINT | 0x5] = "External Interrupt 5",
43fcf5ef2aSThomas Huth     [TT_EXTINT | 0x6] = "External Interrupt 6",
44fcf5ef2aSThomas Huth     [TT_EXTINT | 0x7] = "External Interrupt 7",
45fcf5ef2aSThomas Huth     [TT_EXTINT | 0x8] = "External Interrupt 8",
46fcf5ef2aSThomas Huth     [TT_EXTINT | 0x9] = "External Interrupt 9",
47fcf5ef2aSThomas Huth     [TT_EXTINT | 0xa] = "External Interrupt 10",
48fcf5ef2aSThomas Huth     [TT_EXTINT | 0xb] = "External Interrupt 11",
49fcf5ef2aSThomas Huth     [TT_EXTINT | 0xc] = "External Interrupt 12",
50fcf5ef2aSThomas Huth     [TT_EXTINT | 0xd] = "External Interrupt 13",
51fcf5ef2aSThomas Huth     [TT_EXTINT | 0xe] = "External Interrupt 14",
52fcf5ef2aSThomas Huth     [TT_EXTINT | 0xf] = "External Interrupt 15",
53fcf5ef2aSThomas Huth     [TT_CODE_ACCESS] = "Instruction Access Error",
54fcf5ef2aSThomas Huth     [TT_DATA_ACCESS] = "Data Access Error",
55fcf5ef2aSThomas Huth     [TT_DIV_ZERO] = "Division By Zero",
56fcf5ef2aSThomas Huth     [TT_NCP_INSN] = "Coprocessor Disabled",
57fcf5ef2aSThomas Huth };
58fcf5ef2aSThomas Huth 
5986e8c353SPhilippe Mathieu-Daudé static const char *excp_name_str(int32_t exception_index)
6086e8c353SPhilippe Mathieu-Daudé {
6186e8c353SPhilippe Mathieu-Daudé     if (exception_index < 0 || exception_index >= ARRAY_SIZE(excp_names)) {
6286e8c353SPhilippe Mathieu-Daudé         return "Unknown";
6386e8c353SPhilippe Mathieu-Daudé     }
6486e8c353SPhilippe Mathieu-Daudé     return excp_names[exception_index];
6586e8c353SPhilippe Mathieu-Daudé }
6686e8c353SPhilippe Mathieu-Daudé 
67fcf5ef2aSThomas Huth void sparc_cpu_do_interrupt(CPUState *cs)
68fcf5ef2aSThomas Huth {
69fcf5ef2aSThomas Huth     SPARCCPU *cpu = SPARC_CPU(cs);
70fcf5ef2aSThomas Huth     CPUSPARCState *env = &cpu->env;
71fcf5ef2aSThomas Huth     int cwp, intno = cs->exception_index;
72fcf5ef2aSThomas Huth 
73fcf5ef2aSThomas Huth     /* Compute PSR before exposing state.  */
74fcf5ef2aSThomas Huth     if (env->cc_op != CC_OP_FLAGS) {
75fcf5ef2aSThomas Huth         cpu_get_psr(env);
76fcf5ef2aSThomas Huth     }
77fcf5ef2aSThomas Huth 
78fcf5ef2aSThomas Huth     if (qemu_loglevel_mask(CPU_LOG_INT)) {
79fcf5ef2aSThomas Huth         static int count;
80fcf5ef2aSThomas Huth         const char *name;
81fcf5ef2aSThomas Huth 
82fcf5ef2aSThomas Huth         if (intno < 0 || intno >= 0x100) {
83fcf5ef2aSThomas Huth             name = "Unknown";
84fcf5ef2aSThomas Huth         } else if (intno >= 0x80) {
85fcf5ef2aSThomas Huth             name = "Trap Instruction";
86fcf5ef2aSThomas Huth         } else {
8786e8c353SPhilippe Mathieu-Daudé             name = excp_name_str(intno);
88fcf5ef2aSThomas Huth         }
89fcf5ef2aSThomas Huth 
90fcf5ef2aSThomas Huth         qemu_log("%6d: %s (v=%02x)\n", count, name, intno);
91fcf5ef2aSThomas Huth         log_cpu_state(cs, 0);
92fcf5ef2aSThomas Huth #if 0
93fcf5ef2aSThomas Huth         {
94fcf5ef2aSThomas Huth             int i;
95fcf5ef2aSThomas Huth             uint8_t *ptr;
96fcf5ef2aSThomas Huth 
97fcf5ef2aSThomas Huth             qemu_log("       code=");
98fcf5ef2aSThomas Huth             ptr = (uint8_t *)env->pc;
99fcf5ef2aSThomas Huth             for (i = 0; i < 16; i++) {
100fcf5ef2aSThomas Huth                 qemu_log(" %02x", ldub(ptr + i));
101fcf5ef2aSThomas Huth             }
102fcf5ef2aSThomas Huth             qemu_log("\n");
103fcf5ef2aSThomas Huth         }
104fcf5ef2aSThomas Huth #endif
105fcf5ef2aSThomas Huth         count++;
106fcf5ef2aSThomas Huth     }
107fcf5ef2aSThomas Huth #if !defined(CONFIG_USER_ONLY)
108fcf5ef2aSThomas Huth     if (env->psret == 0) {
109fcf5ef2aSThomas Huth         if (cs->exception_index == 0x80 &&
110576e1c4cSIgor Mammedov             env->def.features & CPU_FEATURE_TA0_SHUTDOWN) {
111cf83f140SEric Blake             qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
112fcf5ef2aSThomas Huth         } else {
11386e8c353SPhilippe Mathieu-Daudé             cpu_abort(cs, "Trap 0x%02x (%s) while interrupts disabled, "
11486e8c353SPhilippe Mathieu-Daudé                           "Error state",
11586e8c353SPhilippe Mathieu-Daudé                       cs->exception_index, excp_name_str(cs->exception_index));
116fcf5ef2aSThomas Huth         }
117fcf5ef2aSThomas Huth         return;
118fcf5ef2aSThomas Huth     }
119fcf5ef2aSThomas Huth #endif
120fcf5ef2aSThomas Huth     env->psret = 0;
121fcf5ef2aSThomas Huth     cwp = cpu_cwp_dec(env, env->cwp - 1);
122fcf5ef2aSThomas Huth     cpu_set_cwp(env, cwp);
123fcf5ef2aSThomas Huth     env->regwptr[9] = env->pc;
124fcf5ef2aSThomas Huth     env->regwptr[10] = env->npc;
125fcf5ef2aSThomas Huth     env->psrps = env->psrs;
126fcf5ef2aSThomas Huth     env->psrs = 1;
127fcf5ef2aSThomas Huth     env->tbr = (env->tbr & TBR_BASE_MASK) | (intno << 4);
128fcf5ef2aSThomas Huth     env->pc = env->tbr;
129fcf5ef2aSThomas Huth     env->npc = env->pc + 4;
130fcf5ef2aSThomas Huth     cs->exception_index = -1;
131fcf5ef2aSThomas Huth 
132fcf5ef2aSThomas Huth #if !defined(CONFIG_USER_ONLY)
133fcf5ef2aSThomas Huth     /* IRQ acknowledgment */
134fcf5ef2aSThomas Huth     if ((intno & ~15) == TT_EXTINT && env->qemu_irq_ack != NULL) {
135fcf5ef2aSThomas Huth         env->qemu_irq_ack(env, env->irq_manager, intno);
136fcf5ef2aSThomas Huth     }
137fcf5ef2aSThomas Huth #endif
138fcf5ef2aSThomas Huth }
139fcf5ef2aSThomas Huth 
140fcf5ef2aSThomas Huth #if !defined(CONFIG_USER_ONLY)
141fcf5ef2aSThomas Huth static void leon3_cache_control_int(CPUSPARCState *env)
142fcf5ef2aSThomas Huth {
143fcf5ef2aSThomas Huth     uint32_t state = 0;
144fcf5ef2aSThomas Huth 
145fcf5ef2aSThomas Huth     if (env->cache_control & CACHE_CTRL_IF) {
146fcf5ef2aSThomas Huth         /* Instruction cache state */
147fcf5ef2aSThomas Huth         state = env->cache_control & CACHE_STATE_MASK;
148fcf5ef2aSThomas Huth         if (state == CACHE_ENABLED) {
149fcf5ef2aSThomas Huth             state = CACHE_FROZEN;
150fcf5ef2aSThomas Huth             trace_int_helper_icache_freeze();
151fcf5ef2aSThomas Huth         }
152fcf5ef2aSThomas Huth 
153fcf5ef2aSThomas Huth         env->cache_control &= ~CACHE_STATE_MASK;
154fcf5ef2aSThomas Huth         env->cache_control |= state;
155fcf5ef2aSThomas Huth     }
156fcf5ef2aSThomas Huth 
157fcf5ef2aSThomas Huth     if (env->cache_control & CACHE_CTRL_DF) {
158fcf5ef2aSThomas Huth         /* Data cache state */
159fcf5ef2aSThomas Huth         state = (env->cache_control >> 2) & CACHE_STATE_MASK;
160fcf5ef2aSThomas Huth         if (state == CACHE_ENABLED) {
161fcf5ef2aSThomas Huth             state = CACHE_FROZEN;
162fcf5ef2aSThomas Huth             trace_int_helper_dcache_freeze();
163fcf5ef2aSThomas Huth         }
164fcf5ef2aSThomas Huth 
165fcf5ef2aSThomas Huth         env->cache_control &= ~(CACHE_STATE_MASK << 2);
166fcf5ef2aSThomas Huth         env->cache_control |= (state << 2);
167fcf5ef2aSThomas Huth     }
168fcf5ef2aSThomas Huth }
169fcf5ef2aSThomas Huth 
170fcf5ef2aSThomas Huth void leon3_irq_manager(CPUSPARCState *env, void *irq_manager, int intno)
171fcf5ef2aSThomas Huth {
172fcf5ef2aSThomas Huth     leon3_irq_ack(irq_manager, intno);
173fcf5ef2aSThomas Huth     leon3_cache_control_int(env);
174fcf5ef2aSThomas Huth }
175fcf5ef2aSThomas Huth #endif
176