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