16
16
* You should have received a copy of the GNU Lesser General Public
17
17
* License along with this library; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
22
22
#include "qemu-timer.h"
23
#include "host-utils.h"
24
25
//#define DEBUG_APIC
25
26
//#define DEBUG_IOAPIC
99
100
static int apic_io_memory;
100
101
static APICState *local_apics[MAX_APICS + 1];
101
102
static int last_apic_id = 0;
103
static int apic_irq_delivered;
103
106
static void apic_init_ipi(APICState *s);
104
107
static void apic_set_irq(APICState *s, int vector_num, int trigger_mode);
105
108
static void apic_update_irq(APICState *s);
107
/* Find first bit starting from msb. Return 0 if value = 0 */
110
/* Find first bit starting from msb */
108
111
static int fls_bit(uint32_t value)
110
unsigned int ret = 0;
112
#if defined(HOST_I386)
113
__asm__ __volatile__ ("bsr %1, %0\n" : "+r" (ret) : "rm" (value));
117
value >>= 16, ret = 16;
119
value >>= 8, ret += 8;
121
value >>= 4, ret += 4;
123
value >>= 2, ret += 2;
124
return ret + (value >> 1);
113
return 31 - clz32(value);
128
/* Find first bit starting from lsb. Return 0 if value = 0 */
116
/* Find first bit starting from lsb */
129
117
static int ffs_bit(uint32_t value)
131
unsigned int ret = 0;
133
#if defined(HOST_I386)
134
__asm__ __volatile__ ("bsf %1, %0\n" : "+r" (ret) : "rm" (value));
139
if (!(value & 0xffff))
140
value >>= 16, ret = 16;
142
value >>= 8, ret += 8;
144
value >>= 4, ret += 4;
146
value >>= 2, ret += 2;
153
122
static inline void set_bit(uint32_t *tab, int index)
138
static inline int get_bit(uint32_t *tab, int index)
142
mask = 1 << (index & 0x1f);
143
return !!(tab[i] & mask);
146
static void apic_local_deliver(CPUState *env, int vector)
148
APICState *s = env->apic_state;
149
uint32_t lvt = s->lvt[vector];
152
if (lvt & APIC_LVT_MASKED)
155
switch ((lvt >> 8) & 7) {
157
cpu_interrupt(env, CPU_INTERRUPT_SMI);
161
cpu_interrupt(env, CPU_INTERRUPT_NMI);
165
cpu_interrupt(env, CPU_INTERRUPT_HARD);
169
trigger_mode = APIC_TRIGGER_EDGE;
170
if ((vector == APIC_LVT_LINT0 || vector == APIC_LVT_LINT1) &&
171
(lvt & APIC_LVT_LEVEL_TRIGGER))
172
trigger_mode = APIC_TRIGGER_LEVEL;
173
apic_set_irq(s, lvt & 0xff, trigger_mode);
177
void apic_deliver_pic_intr(CPUState *env, int level)
180
apic_local_deliver(env, APIC_LVT_LINT0);
182
APICState *s = env->apic_state;
183
uint32_t lvt = s->lvt[APIC_LVT_LINT0];
185
switch ((lvt >> 8) & 7) {
187
if (!(lvt & APIC_LVT_LEVEL_TRIGGER))
189
reset_bit(s->irr, lvt & 0xff);
192
cpu_reset_interrupt(env, CPU_INTERRUPT_HARD);
169
198
#define foreach_apic(apic, deliver_bitmask, code) \
171
200
int __i, __j, __mask;\
218
247
case APIC_DM_SMI:
248
foreach_apic(apic_iter, deliver_bitmask,
249
cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_SMI) );
219
252
case APIC_DM_NMI:
253
foreach_apic(apic_iter, deliver_bitmask,
254
cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_NMI) );
222
257
case APIC_DM_INIT:
223
258
/* normal INIT IPI sent to processors */
324
359
cpu_interrupt(s->cpu_env, CPU_INTERRUPT_HARD);
362
void apic_reset_irq_delivered(void)
364
apic_irq_delivered = 0;
367
int apic_get_irq_delivered(void)
369
return apic_irq_delivered;
327
372
static void apic_set_irq(APICState *s, int vector_num, int trigger_mode)
374
apic_irq_delivered += !get_bit(s->irr, vector_num);
329
376
set_bit(s->irr, vector_num);
330
377
if (trigger_mode)
331
378
set_bit(s->tmr, vector_num);
400
447
s->initial_count = 0;
401
448
s->initial_count_load_time = 0;
402
449
s->next_time = 0;
451
cpu_reset(s->cpu_env);
453
if (!(s->apicbase & MSR_IA32_APICBASE_BSP))
454
s->cpu_env->halted = 1;
405
457
/* send a SIPI message to the CPU to start it */
406
458
static void apic_startup(APICState *s, int vector_num)
408
460
CPUState *env = s->cpu_env;
409
if (!(env->hflags & HF_HALTED_MASK))
412
464
cpu_x86_load_seg_cache(env, R_CS, vector_num << 8, vector_num << 12,
414
env->hflags &= ~HF_HALTED_MASK;
417
469
static void apic_deliver(APICState *s, uint8_t dest, uint8_t dest_mode,
497
549
lvt0 = s->lvt[APIC_LVT_LINT0];
500
((s->apicbase & MSR_IA32_APICBASE_ENABLE) == 0 ||
501
((lvt0 & APIC_LVT_MASKED) == 0 &&
502
((lvt0 >> 8) & 0x7) == APIC_DM_EXTINT)))
551
if ((s->apicbase & MSR_IA32_APICBASE_ENABLE) == 0 ||
552
(lvt0 & APIC_LVT_MASKED) == 0)
531
581
d = (current_time - s->initial_count_load_time) >>
533
583
if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
584
if (!s->initial_count)
534
586
d = ((d / ((uint64_t)s->initial_count + 1)) + 1) * ((uint64_t)s->initial_count + 1);
536
588
if (d >= s->initial_count)
551
603
APICState *s = opaque;
553
if (!(s->lvt[APIC_LVT_TIMER] & APIC_LVT_MASKED)) {
554
apic_set_irq(s, s->lvt[APIC_LVT_TIMER] & 0xff, APIC_TRIGGER_EDGE);
605
apic_local_deliver(s->cpu_env, APIC_LVT_TIMER);
556
606
apic_timer_update(s, s->next_time);
810
863
static void apic_reset(void *opaque)
812
865
APICState *s = opaque;
867
s->apicbase = 0xfee00000 |
868
(s->id ? 0 : MSR_IA32_APICBASE_BSP) | MSR_IA32_APICBASE_ENABLE;
813
870
apic_init_ipi(s);
816
* LINT0 delivery mode is set to ExtInt at initialization time
817
* typically by BIOS, so PIC interrupt can be delivered to the
818
* processor when local APIC is enabled.
820
s->lvt[APIC_LVT_LINT0] = 0x700;
874
* LINT0 delivery mode on CPU #0 is set to ExtInt at initialization
875
* time typically by BIOS, so PIC interrupt can be delivered to the
876
* processor when local APIC is enabled.
878
s->lvt[APIC_LVT_LINT0] = 0x700;
823
882
static CPUReadMemoryFunc *apic_mem_read[3] = {
839
898
if (last_apic_id >= MAX_APICS)
841
900
s = qemu_mallocz(sizeof(APICState));
844
901
env->apic_state = s;
846
902
s->id = last_apic_id++;
847
903
env->cpuid_apic_id = s->id;
848
904
s->cpu_env = env;
849
s->apicbase = 0xfee00000 |
850
(s->id ? 0 : MSR_IA32_APICBASE_BSP) | MSR_IA32_APICBASE_ENABLE;
853
* LINT0 delivery mode is set to ExtInt at initialization time
854
* typically by BIOS, so PIC interrupt can be delivered to the
855
* processor when local APIC is enabled.
857
s->lvt[APIC_LVT_LINT0] = 0x700;
859
908
/* XXX: mapping more APICs at the same memory location */
860
909
if (apic_io_memory == 0) {
917
966
IOAPICState *s = opaque;
968
/* ISA IRQs map to GSI 1-1 except for IRQ0 which maps
969
* to GSI 2. GSI maps to ioapic 1-1. This is not
970
* the cleanest way of doing it but it should work. */
919
975
if (vector >= 0 && vector < IOAPIC_NUM_PINS) {
920
976
uint32_t mask = 1 << vector;
921
977
uint64_t entry = s->ioredtbl[vector];