| 
									
										
										
										
											2011-09-06 03:55:25 +04:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2011, Max Filippov, Open Source and Linux Lab. | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Redistribution and use in source and binary forms, with or without | 
					
						
							|  |  |  |  * modification, are permitted provided that the following conditions are met: | 
					
						
							|  |  |  |  *     * Redistributions of source code must retain the above copyright | 
					
						
							|  |  |  |  *       notice, this list of conditions and the following disclaimer. | 
					
						
							|  |  |  |  *     * Redistributions in binary form must reproduce the above copyright | 
					
						
							|  |  |  |  *       notice, this list of conditions and the following disclaimer in the | 
					
						
							|  |  |  |  *       documentation and/or other materials provided with the distribution. | 
					
						
							|  |  |  |  *     * Neither the name of the Open Source and Linux Lab nor the | 
					
						
							|  |  |  |  *       names of its contributors may be used to endorse or promote products | 
					
						
							|  |  |  |  *       derived from this software without specific prior written permission. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
					
						
							|  |  |  |  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
					
						
							|  |  |  |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 
					
						
							|  |  |  |  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY | 
					
						
							|  |  |  |  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 
					
						
							|  |  |  |  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 
					
						
							|  |  |  |  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | 
					
						
							|  |  |  |  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
					
						
							|  |  |  |  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | 
					
						
							|  |  |  |  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-26 18:17:21 +00:00
										 |  |  | #include "qemu/osdep.h"
 | 
					
						
							| 
									
										
										
										
											2016-03-15 16:58:45 +01:00
										 |  |  | #include "cpu.h"
 | 
					
						
							| 
									
										
										
										
											2013-02-04 15:40:22 +01:00
										 |  |  | #include "hw/hw.h"
 | 
					
						
							| 
									
										
										
										
											2012-12-17 18:20:00 +01:00
										 |  |  | #include "qemu/log.h"
 | 
					
						
							|  |  |  | #include "qemu/timer.h"
 | 
					
						
							| 
									
										
										
										
											2011-09-06 03:55:25 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-14 01:38:24 +01:00
										 |  |  | void xtensa_advance_ccount(CPUXtensaState *env, uint32_t d) | 
					
						
							| 
									
										
										
										
											2011-09-06 03:55:48 +04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-09-25 16:12:31 +04:00
										 |  |  |     uint32_t old_ccount = env->sregs[CCOUNT] + 1; | 
					
						
							| 
									
										
										
										
											2011-09-06 03:55:48 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     env->sregs[CCOUNT] += d; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (xtensa_option_enabled(env->config, XTENSA_OPTION_TIMER_INTERRUPT)) { | 
					
						
							|  |  |  |         int i; | 
					
						
							|  |  |  |         for (i = 0; i < env->config->nccompare; ++i) { | 
					
						
							| 
									
										
										
										
											2014-09-25 16:12:31 +04:00
										 |  |  |             if (env->sregs[CCOMPARE + i] - old_ccount < d) { | 
					
						
							| 
									
										
										
										
											2011-09-06 03:55:48 +04:00
										 |  |  |                 xtensa_timer_irq(env, i, 1); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-14 01:38:24 +01:00
										 |  |  | void check_interrupts(CPUXtensaState *env) | 
					
						
							| 
									
										
										
										
											2011-09-06 03:55:48 +04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-01-17 18:51:17 +01:00
										 |  |  |     CPUState *cs = CPU(xtensa_env_get_cpu(env)); | 
					
						
							| 
									
										
										
										
											2011-09-06 03:55:48 +04:00
										 |  |  |     int minlevel = xtensa_get_cintlevel(env); | 
					
						
							|  |  |  |     uint32_t int_set_enabled = env->sregs[INTSET] & env->sregs[INTENABLE]; | 
					
						
							|  |  |  |     int level; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-21 16:03:08 +01:00
										 |  |  |     /* If the CPU is halted advance CCOUNT according to the QEMU_CLOCK_VIRTUAL time
 | 
					
						
							| 
									
										
										
										
											2011-09-06 03:55:48 +04:00
										 |  |  |      * elapsed since the moment when it was advanced last time. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2013-01-17 18:51:17 +01:00
										 |  |  |     if (cs->halted) { | 
					
						
							| 
									
										
										
										
											2013-08-21 16:03:08 +01:00
										 |  |  |         int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); | 
					
						
							| 
									
										
										
										
											2011-09-06 03:55:48 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         xtensa_advance_ccount(env, | 
					
						
							|  |  |  |                 muldiv64(now - env->halt_clock, | 
					
						
							|  |  |  |                     env->config->clock_freq_khz, 1000000)); | 
					
						
							|  |  |  |         env->halt_clock = now; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     for (level = env->config->nlevel; level > minlevel; --level) { | 
					
						
							|  |  |  |         if (env->config->level_mask[level] & int_set_enabled) { | 
					
						
							|  |  |  |             env->pending_irq_level = level; | 
					
						
							| 
									
										
										
										
											2013-01-18 15:03:43 +01:00
										 |  |  |             cpu_interrupt(cs, CPU_INTERRUPT_HARD); | 
					
						
							| 
									
										
										
										
											2011-09-06 03:55:48 +04:00
										 |  |  |             qemu_log_mask(CPU_LOG_INT, | 
					
						
							|  |  |  |                     "%s level = %d, cintlevel = %d, " | 
					
						
							|  |  |  |                     "pc = %08x, a0 = %08x, ps = %08x, " | 
					
						
							|  |  |  |                     "intset = %08x, intenable = %08x, " | 
					
						
							|  |  |  |                     "ccount = %08x\n", | 
					
						
							|  |  |  |                     __func__, level, xtensa_get_cintlevel(env), | 
					
						
							|  |  |  |                     env->pc, env->regs[0], env->sregs[PS], | 
					
						
							|  |  |  |                     env->sregs[INTSET], env->sregs[INTENABLE], | 
					
						
							|  |  |  |                     env->sregs[CCOUNT]); | 
					
						
							|  |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     env->pending_irq_level = 0; | 
					
						
							| 
									
										
										
										
											2013-01-17 22:30:20 +01:00
										 |  |  |     cpu_reset_interrupt(cs, CPU_INTERRUPT_HARD); | 
					
						
							| 
									
										
										
										
											2011-09-06 03:55:48 +04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void xtensa_set_irq(void *opaque, int irq, int active) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2012-03-14 01:38:24 +01:00
										 |  |  |     CPUXtensaState *env = opaque; | 
					
						
							| 
									
										
										
										
											2011-09-06 03:55:48 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (irq >= env->config->ninterrupt) { | 
					
						
							|  |  |  |         qemu_log("%s: bad IRQ %d\n", __func__, irq); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         uint32_t irq_bit = 1 << irq; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (active) { | 
					
						
							|  |  |  |             env->sregs[INTSET] |= irq_bit; | 
					
						
							|  |  |  |         } else if (env->config->interrupt[irq].inttype == INTTYPE_LEVEL) { | 
					
						
							|  |  |  |             env->sregs[INTSET] &= ~irq_bit; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         check_interrupts(env); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-14 01:38:24 +01:00
										 |  |  | void xtensa_timer_irq(CPUXtensaState *env, uint32_t id, uint32_t active) | 
					
						
							| 
									
										
										
										
											2011-09-06 03:55:48 +04:00
										 |  |  | { | 
					
						
							|  |  |  |     qemu_set_irq(env->irq_inputs[env->config->timerint[id]], active); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-14 01:38:24 +01:00
										 |  |  | void xtensa_rearm_ccompare_timer(CPUXtensaState *env) | 
					
						
							| 
									
										
										
										
											2011-10-10 06:25:04 +04:00
										 |  |  | { | 
					
						
							|  |  |  |     int i; | 
					
						
							|  |  |  |     uint32_t wake_ccount = env->sregs[CCOUNT] - 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for (i = 0; i < env->config->nccompare; ++i) { | 
					
						
							|  |  |  |         if (env->sregs[CCOMPARE + i] - env->sregs[CCOUNT] < | 
					
						
							|  |  |  |                 wake_ccount - env->sregs[CCOUNT]) { | 
					
						
							|  |  |  |             wake_ccount = env->sregs[CCOMPARE + i]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     env->wake_ccount = wake_ccount; | 
					
						
							| 
									
										
										
										
											2013-08-21 16:03:08 +01:00
										 |  |  |     timer_mod(env->ccompare_timer, env->halt_clock + | 
					
						
							| 
									
										
										
										
											2016-05-09 15:24:58 +02:00
										 |  |  |             (uint64_t)(wake_ccount - env->sregs[CCOUNT]) * | 
					
						
							|  |  |  |             1000000 / env->config->clock_freq_khz); | 
					
						
							| 
									
										
										
										
											2011-10-10 06:25:04 +04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-06 03:55:48 +04:00
										 |  |  | static void xtensa_ccompare_cb(void *opaque) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2012-05-03 06:41:02 +02:00
										 |  |  |     XtensaCPU *cpu = opaque; | 
					
						
							|  |  |  |     CPUXtensaState *env = &cpu->env; | 
					
						
							| 
									
										
										
										
											2013-01-17 18:51:17 +01:00
										 |  |  |     CPUState *cs = CPU(cpu); | 
					
						
							| 
									
										
										
										
											2011-10-10 06:25:04 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-17 18:51:17 +01:00
										 |  |  |     if (cs->halted) { | 
					
						
							| 
									
										
										
										
											2013-08-21 16:03:08 +01:00
										 |  |  |         env->halt_clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); | 
					
						
							| 
									
										
										
										
											2011-10-10 06:25:04 +04:00
										 |  |  |         xtensa_advance_ccount(env, env->wake_ccount - env->sregs[CCOUNT]); | 
					
						
							| 
									
										
										
										
											2013-01-17 18:51:17 +01:00
										 |  |  |         if (!cpu_has_work(cs)) { | 
					
						
							| 
									
										
										
										
											2011-10-10 06:25:04 +04:00
										 |  |  |             env->sregs[CCOUNT] = env->wake_ccount + 1; | 
					
						
							|  |  |  |             xtensa_rearm_ccompare_timer(env); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2011-09-06 03:55:48 +04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-14 01:38:24 +01:00
										 |  |  | void xtensa_irq_init(CPUXtensaState *env) | 
					
						
							| 
									
										
										
										
											2011-09-06 03:55:48 +04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-05-03 06:41:02 +02:00
										 |  |  |     XtensaCPU *cpu = xtensa_env_get_cpu(env); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-06 03:55:48 +04:00
										 |  |  |     env->irq_inputs = (void **)qemu_allocate_irqs( | 
					
						
							|  |  |  |             xtensa_set_irq, env, env->config->ninterrupt); | 
					
						
							|  |  |  |     if (xtensa_option_enabled(env->config, XTENSA_OPTION_TIMER_INTERRUPT) && | 
					
						
							|  |  |  |             env->config->nccompare > 0) { | 
					
						
							|  |  |  |         env->ccompare_timer = | 
					
						
							| 
									
										
										
										
											2013-08-21 16:03:08 +01:00
										 |  |  |             timer_new_ns(QEMU_CLOCK_VIRTUAL, &xtensa_ccompare_cb, cpu); | 
					
						
							| 
									
										
										
										
											2011-09-06 03:55:48 +04:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2011-10-16 02:56:03 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-14 01:38:24 +01:00
										 |  |  | void *xtensa_get_extint(CPUXtensaState *env, unsigned extint) | 
					
						
							| 
									
										
										
										
											2011-10-16 02:56:03 +04:00
										 |  |  | { | 
					
						
							|  |  |  |     if (extint < env->config->nextint) { | 
					
						
							|  |  |  |         unsigned irq = env->config->extint[extint]; | 
					
						
							|  |  |  |         return env->irq_inputs[irq]; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         qemu_log("%s: trying to acquire invalid external interrupt %d\n", | 
					
						
							|  |  |  |                 __func__, extint); | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |