1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28#include "qemu/osdep.h"
29#include "cpu.h"
30#include "hw/hw.h"
31#include "qemu/log.h"
32#include "qemu/timer.h"
33
34void check_interrupts(CPUXtensaState *env)
35{
36 CPUState *cs = CPU(xtensa_env_get_cpu(env));
37 int minlevel = xtensa_get_cintlevel(env);
38 uint32_t int_set_enabled = env->sregs[INTSET] & env->sregs[INTENABLE];
39 int level;
40
41 for (level = env->config->nlevel; level > minlevel; --level) {
42 if (env->config->level_mask[level] & int_set_enabled) {
43 env->pending_irq_level = level;
44 cpu_interrupt(cs, CPU_INTERRUPT_HARD);
45 qemu_log_mask(CPU_LOG_INT,
46 "%s level = %d, cintlevel = %d, "
47 "pc = %08x, a0 = %08x, ps = %08x, "
48 "intset = %08x, intenable = %08x, "
49 "ccount = %08x\n",
50 __func__, level, xtensa_get_cintlevel(env),
51 env->pc, env->regs[0], env->sregs[PS],
52 env->sregs[INTSET], env->sregs[INTENABLE],
53 env->sregs[CCOUNT]);
54 return;
55 }
56 }
57 env->pending_irq_level = 0;
58 cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD);
59}
60
61static void xtensa_set_irq(void *opaque, int irq, int active)
62{
63 CPUXtensaState *env = opaque;
64
65 if (irq >= env->config->ninterrupt) {
66 qemu_log("%s: bad IRQ %d\n", __func__, irq);
67 } else {
68 uint32_t irq_bit = 1 << irq;
69
70 if (active) {
71 env->sregs[INTSET] |= irq_bit;
72 } else if (env->config->interrupt[irq].inttype == INTTYPE_LEVEL) {
73 env->sregs[INTSET] &= ~irq_bit;
74 }
75
76 check_interrupts(env);
77 }
78}
79
80void xtensa_timer_irq(CPUXtensaState *env, uint32_t id, uint32_t active)
81{
82 qemu_set_irq(env->irq_inputs[env->config->timerint[id]], active);
83}
84
85static void xtensa_ccompare_cb(void *opaque)
86{
87 XtensaCcompareTimer *ccompare = opaque;
88 CPUXtensaState *env = ccompare->env;
89 unsigned i = ccompare - env->ccompare;
90
91 xtensa_timer_irq(env, i, 1);
92}
93
94void xtensa_irq_init(CPUXtensaState *env)
95{
96 env->irq_inputs = (void **)qemu_allocate_irqs(
97 xtensa_set_irq, env, env->config->ninterrupt);
98 if (xtensa_option_enabled(env->config, XTENSA_OPTION_TIMER_INTERRUPT)) {
99 unsigned i;
100
101 env->time_base = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
102 env->ccount_base = env->sregs[CCOUNT];
103 for (i = 0; i < env->config->nccompare; ++i) {
104 env->ccompare[i].env = env;
105 env->ccompare[i].timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
106 xtensa_ccompare_cb, env->ccompare + i);
107 }
108 }
109}
110
111void *xtensa_get_extint(CPUXtensaState *env, unsigned extint)
112{
113 if (extint < env->config->nextint) {
114 unsigned irq = env->config->extint[extint];
115 return env->irq_inputs[irq];
116 } else {
117 qemu_log("%s: trying to acquire invalid external interrupt %d\n",
118 __func__, extint);
119 return NULL;
120 }
121}
122