Statistics
| Branch: | Revision:

root / hw / apic.c @ cf6d64bf

History | View | Annotate | Download (26.7 kB)

1
/*
2
 *  APIC support
3
 *
4
 *  Copyright (c) 2004-2005 Fabrice Bellard
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, see <http://www.gnu.org/licenses/>
18
 */
19
#include "hw.h"
20
#include "pc.h"
21
#include "apic.h"
22
#include "pci.h"
23
#include "msix.h"
24
#include "qemu-timer.h"
25
#include "host-utils.h"
26
#include "kvm.h"
27

    
28
//#define DEBUG_APIC
29
//#define DEBUG_COALESCING
30

    
31
#ifdef DEBUG_APIC
32
#define DPRINTF(fmt, ...)                                       \
33
    do { printf("apic: " fmt , ## __VA_ARGS__); } while (0)
34
#else
35
#define DPRINTF(fmt, ...)
36
#endif
37

    
38
#ifdef DEBUG_COALESCING
39
#define DPRINTF_C(fmt, ...)                                     \
40
    do { printf("apic: " fmt , ## __VA_ARGS__); } while (0)
41
#else
42
#define DPRINTF_C(fmt, ...)
43
#endif
44

    
45
/* APIC Local Vector Table */
46
#define APIC_LVT_TIMER   0
47
#define APIC_LVT_THERMAL 1
48
#define APIC_LVT_PERFORM 2
49
#define APIC_LVT_LINT0   3
50
#define APIC_LVT_LINT1   4
51
#define APIC_LVT_ERROR   5
52
#define APIC_LVT_NB      6
53

    
54
/* APIC delivery modes */
55
#define APIC_DM_FIXED        0
56
#define APIC_DM_LOWPRI        1
57
#define APIC_DM_SMI        2
58
#define APIC_DM_NMI        4
59
#define APIC_DM_INIT        5
60
#define APIC_DM_SIPI        6
61
#define APIC_DM_EXTINT        7
62

    
63
/* APIC destination mode */
64
#define APIC_DESTMODE_FLAT        0xf
65
#define APIC_DESTMODE_CLUSTER        1
66

    
67
#define APIC_TRIGGER_EDGE  0
68
#define APIC_TRIGGER_LEVEL 1
69

    
70
#define        APIC_LVT_TIMER_PERIODIC                (1<<17)
71
#define        APIC_LVT_MASKED                        (1<<16)
72
#define        APIC_LVT_LEVEL_TRIGGER                (1<<15)
73
#define        APIC_LVT_REMOTE_IRR                (1<<14)
74
#define        APIC_INPUT_POLARITY                (1<<13)
75
#define        APIC_SEND_PENDING                (1<<12)
76

    
77
#define ESR_ILLEGAL_ADDRESS (1 << 7)
78

    
79
#define APIC_SV_ENABLE (1 << 8)
80

    
81
#define MAX_APICS 255
82
#define MAX_APIC_WORDS 8
83

    
84
/* Intel APIC constants: from include/asm/msidef.h */
85
#define MSI_DATA_VECTOR_SHIFT                0
86
#define MSI_DATA_VECTOR_MASK                0x000000ff
87
#define MSI_DATA_DELIVERY_MODE_SHIFT        8
88
#define MSI_DATA_TRIGGER_SHIFT                15
89
#define MSI_DATA_LEVEL_SHIFT                14
90
#define MSI_ADDR_DEST_MODE_SHIFT        2
91
#define MSI_ADDR_DEST_ID_SHIFT                12
92
#define        MSI_ADDR_DEST_ID_MASK                0x00ffff0
93

    
94
#define MSI_ADDR_BASE                   0xfee00000
95
#define MSI_ADDR_SIZE                   0x100000
96

    
97
struct APICState {
98
    CPUState *cpu_env;
99
    uint32_t apicbase;
100
    uint8_t id;
101
    uint8_t arb_id;
102
    uint8_t tpr;
103
    uint32_t spurious_vec;
104
    uint8_t log_dest;
105
    uint8_t dest_mode;
106
    uint32_t isr[8];  /* in service register */
107
    uint32_t tmr[8];  /* trigger mode register */
108
    uint32_t irr[8]; /* interrupt request register */
109
    uint32_t lvt[APIC_LVT_NB];
110
    uint32_t esr; /* error register */
111
    uint32_t icr[2];
112

    
113
    uint32_t divide_conf;
114
    int count_shift;
115
    uint32_t initial_count;
116
    int64_t initial_count_load_time, next_time;
117
    uint32_t idx;
118
    QEMUTimer *timer;
119
    int sipi_vector;
120
    int wait_for_sipi;
121
};
122

    
123
static int apic_io_memory;
124
static APICState *local_apics[MAX_APICS + 1];
125
static int last_apic_idx = 0;
126
static int apic_irq_delivered;
127

    
128

    
129
static void apic_set_irq(APICState *s, int vector_num, int trigger_mode);
130
static void apic_update_irq(APICState *s);
131
static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask,
132
                                      uint8_t dest, uint8_t dest_mode);
133

    
134
/* Find first bit starting from msb */
135
static int fls_bit(uint32_t value)
136
{
137
    return 31 - clz32(value);
138
}
139

    
140
/* Find first bit starting from lsb */
141
static int ffs_bit(uint32_t value)
142
{
143
    return ctz32(value);
144
}
145

    
146
static inline void set_bit(uint32_t *tab, int index)
147
{
148
    int i, mask;
149
    i = index >> 5;
150
    mask = 1 << (index & 0x1f);
151
    tab[i] |= mask;
152
}
153

    
154
static inline void reset_bit(uint32_t *tab, int index)
155
{
156
    int i, mask;
157
    i = index >> 5;
158
    mask = 1 << (index & 0x1f);
159
    tab[i] &= ~mask;
160
}
161

    
162
static inline int get_bit(uint32_t *tab, int index)
163
{
164
    int i, mask;
165
    i = index >> 5;
166
    mask = 1 << (index & 0x1f);
167
    return !!(tab[i] & mask);
168
}
169

    
170
static void apic_local_deliver(APICState *s, int vector)
171
{
172
    uint32_t lvt = s->lvt[vector];
173
    int trigger_mode;
174

    
175
    DPRINTF("%s: vector %d delivery mode %d\n", __func__, vector,
176
            (lvt >> 8) & 7);
177
    if (lvt & APIC_LVT_MASKED)
178
        return;
179

    
180
    switch ((lvt >> 8) & 7) {
181
    case APIC_DM_SMI:
182
        cpu_interrupt(s->cpu_env, CPU_INTERRUPT_SMI);
183
        break;
184

    
185
    case APIC_DM_NMI:
186
        cpu_interrupt(s->cpu_env, CPU_INTERRUPT_NMI);
187
        break;
188

    
189
    case APIC_DM_EXTINT:
190
        cpu_interrupt(s->cpu_env, CPU_INTERRUPT_HARD);
191
        break;
192

    
193
    case APIC_DM_FIXED:
194
        trigger_mode = APIC_TRIGGER_EDGE;
195
        if ((vector == APIC_LVT_LINT0 || vector == APIC_LVT_LINT1) &&
196
            (lvt & APIC_LVT_LEVEL_TRIGGER))
197
            trigger_mode = APIC_TRIGGER_LEVEL;
198
        apic_set_irq(s, lvt & 0xff, trigger_mode);
199
    }
200
}
201

    
202
void apic_deliver_pic_intr(APICState *s, int level)
203
{
204
    if (level) {
205
        apic_local_deliver(s, APIC_LVT_LINT0);
206
    } else {
207
        uint32_t lvt = s->lvt[APIC_LVT_LINT0];
208

    
209
        switch ((lvt >> 8) & 7) {
210
        case APIC_DM_FIXED:
211
            if (!(lvt & APIC_LVT_LEVEL_TRIGGER))
212
                break;
213
            reset_bit(s->irr, lvt & 0xff);
214
            /* fall through */
215
        case APIC_DM_EXTINT:
216
            cpu_reset_interrupt(s->cpu_env, CPU_INTERRUPT_HARD);
217
            break;
218
        }
219
    }
220
}
221

    
222
#define foreach_apic(apic, deliver_bitmask, code) \
223
{\
224
    int __i, __j, __mask;\
225
    for(__i = 0; __i < MAX_APIC_WORDS; __i++) {\
226
        __mask = deliver_bitmask[__i];\
227
        if (__mask) {\
228
            for(__j = 0; __j < 32; __j++) {\
229
                if (__mask & (1 << __j)) {\
230
                    apic = local_apics[__i * 32 + __j];\
231
                    if (apic) {\
232
                        code;\
233
                    }\
234
                }\
235
            }\
236
        }\
237
    }\
238
}
239

    
240
static void apic_bus_deliver(const uint32_t *deliver_bitmask,
241
                             uint8_t delivery_mode,
242
                             uint8_t vector_num, uint8_t polarity,
243
                             uint8_t trigger_mode)
244
{
245
    APICState *apic_iter;
246

    
247
    switch (delivery_mode) {
248
        case APIC_DM_LOWPRI:
249
            /* XXX: search for focus processor, arbitration */
250
            {
251
                int i, d;
252
                d = -1;
253
                for(i = 0; i < MAX_APIC_WORDS; i++) {
254
                    if (deliver_bitmask[i]) {
255
                        d = i * 32 + ffs_bit(deliver_bitmask[i]);
256
                        break;
257
                    }
258
                }
259
                if (d >= 0) {
260
                    apic_iter = local_apics[d];
261
                    if (apic_iter) {
262
                        apic_set_irq(apic_iter, vector_num, trigger_mode);
263
                    }
264
                }
265
            }
266
            return;
267

    
268
        case APIC_DM_FIXED:
269
            break;
270

    
271
        case APIC_DM_SMI:
272
            foreach_apic(apic_iter, deliver_bitmask,
273
                cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_SMI) );
274
            return;
275

    
276
        case APIC_DM_NMI:
277
            foreach_apic(apic_iter, deliver_bitmask,
278
                cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_NMI) );
279
            return;
280

    
281
        case APIC_DM_INIT:
282
            /* normal INIT IPI sent to processors */
283
            foreach_apic(apic_iter, deliver_bitmask,
284
                         cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_INIT) );
285
            return;
286

    
287
        case APIC_DM_EXTINT:
288
            /* handled in I/O APIC code */
289
            break;
290

    
291
        default:
292
            return;
293
    }
294

    
295
    foreach_apic(apic_iter, deliver_bitmask,
296
                 apic_set_irq(apic_iter, vector_num, trigger_mode) );
297
}
298

    
299
void apic_deliver_irq(uint8_t dest, uint8_t dest_mode,
300
                      uint8_t delivery_mode, uint8_t vector_num,
301
                      uint8_t polarity, uint8_t trigger_mode)
302
{
303
    uint32_t deliver_bitmask[MAX_APIC_WORDS];
304

    
305
    DPRINTF("%s: dest %d dest_mode %d delivery_mode %d vector %d"
306
            " polarity %d trigger_mode %d\n", __func__, dest, dest_mode,
307
            delivery_mode, vector_num, polarity, trigger_mode);
308
    apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode);
309
    apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, polarity,
310
                     trigger_mode);
311
}
312

    
313
void cpu_set_apic_base(CPUState *env, uint64_t val)
314
{
315
    APICState *s = env->apic_state;
316

    
317
    DPRINTF("cpu_set_apic_base: %016" PRIx64 "\n", val);
318
    if (!s)
319
        return;
320
    s->apicbase = (val & 0xfffff000) |
321
        (s->apicbase & (MSR_IA32_APICBASE_BSP | MSR_IA32_APICBASE_ENABLE));
322
    /* if disabled, cannot be enabled again */
323
    if (!(val & MSR_IA32_APICBASE_ENABLE)) {
324
        s->apicbase &= ~MSR_IA32_APICBASE_ENABLE;
325
        env->cpuid_features &= ~CPUID_APIC;
326
        s->spurious_vec &= ~APIC_SV_ENABLE;
327
    }
328
}
329

    
330
uint64_t cpu_get_apic_base(CPUState *env)
331
{
332
    APICState *s = env->apic_state;
333

    
334
    DPRINTF("cpu_get_apic_base: %016" PRIx64 "\n",
335
            s ? (uint64_t)s->apicbase: 0);
336
    return s ? s->apicbase : 0;
337
}
338

    
339
void cpu_set_apic_tpr(CPUX86State *env, uint8_t val)
340
{
341
    APICState *s = env->apic_state;
342
    if (!s)
343
        return;
344
    s->tpr = (val & 0x0f) << 4;
345
    apic_update_irq(s);
346
}
347

    
348
uint8_t cpu_get_apic_tpr(CPUX86State *env)
349
{
350
    APICState *s = env->apic_state;
351
    return s ? s->tpr >> 4 : 0;
352
}
353

    
354
/* return -1 if no bit is set */
355
static int get_highest_priority_int(uint32_t *tab)
356
{
357
    int i;
358
    for(i = 7; i >= 0; i--) {
359
        if (tab[i] != 0) {
360
            return i * 32 + fls_bit(tab[i]);
361
        }
362
    }
363
    return -1;
364
}
365

    
366
static int apic_get_ppr(APICState *s)
367
{
368
    int tpr, isrv, ppr;
369

    
370
    tpr = (s->tpr >> 4);
371
    isrv = get_highest_priority_int(s->isr);
372
    if (isrv < 0)
373
        isrv = 0;
374
    isrv >>= 4;
375
    if (tpr >= isrv)
376
        ppr = s->tpr;
377
    else
378
        ppr = isrv << 4;
379
    return ppr;
380
}
381

    
382
static int apic_get_arb_pri(APICState *s)
383
{
384
    /* XXX: arbitration */
385
    return 0;
386
}
387

    
388
/* signal the CPU if an irq is pending */
389
static void apic_update_irq(APICState *s)
390
{
391
    int irrv, ppr;
392
    if (!(s->spurious_vec & APIC_SV_ENABLE))
393
        return;
394
    irrv = get_highest_priority_int(s->irr);
395
    if (irrv < 0)
396
        return;
397
    ppr = apic_get_ppr(s);
398
    if (ppr && (irrv & 0xf0) <= (ppr & 0xf0))
399
        return;
400
    cpu_interrupt(s->cpu_env, CPU_INTERRUPT_HARD);
401
}
402

    
403
void apic_reset_irq_delivered(void)
404
{
405
    DPRINTF_C("%s: old coalescing %d\n", __func__, apic_irq_delivered);
406
    apic_irq_delivered = 0;
407
}
408

    
409
int apic_get_irq_delivered(void)
410
{
411
    DPRINTF_C("%s: returning coalescing %d\n", __func__, apic_irq_delivered);
412
    return apic_irq_delivered;
413
}
414

    
415
static void apic_set_irq(APICState *s, int vector_num, int trigger_mode)
416
{
417
    apic_irq_delivered += !get_bit(s->irr, vector_num);
418
    DPRINTF_C("%s: coalescing %d\n", __func__, apic_irq_delivered);
419

    
420
    set_bit(s->irr, vector_num);
421
    if (trigger_mode)
422
        set_bit(s->tmr, vector_num);
423
    else
424
        reset_bit(s->tmr, vector_num);
425
    apic_update_irq(s);
426
}
427

    
428
static void apic_eoi(APICState *s)
429
{
430
    int isrv;
431
    isrv = get_highest_priority_int(s->isr);
432
    if (isrv < 0)
433
        return;
434
    reset_bit(s->isr, isrv);
435
    /* XXX: send the EOI packet to the APIC bus to allow the I/O APIC to
436
            set the remote IRR bit for level triggered interrupts. */
437
    apic_update_irq(s);
438
}
439

    
440
static int apic_find_dest(uint8_t dest)
441
{
442
    APICState *apic = local_apics[dest];
443
    int i;
444

    
445
    if (apic && apic->id == dest)
446
        return dest;  /* shortcut in case apic->id == apic->idx */
447

    
448
    for (i = 0; i < MAX_APICS; i++) {
449
        apic = local_apics[i];
450
        if (apic && apic->id == dest)
451
            return i;
452
    }
453

    
454
    return -1;
455
}
456

    
457
static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask,
458
                                      uint8_t dest, uint8_t dest_mode)
459
{
460
    APICState *apic_iter;
461
    int i;
462

    
463
    if (dest_mode == 0) {
464
        if (dest == 0xff) {
465
            memset(deliver_bitmask, 0xff, MAX_APIC_WORDS * sizeof(uint32_t));
466
        } else {
467
            int idx = apic_find_dest(dest);
468
            memset(deliver_bitmask, 0x00, MAX_APIC_WORDS * sizeof(uint32_t));
469
            if (idx >= 0)
470
                set_bit(deliver_bitmask, idx);
471
        }
472
    } else {
473
        /* XXX: cluster mode */
474
        memset(deliver_bitmask, 0x00, MAX_APIC_WORDS * sizeof(uint32_t));
475
        for(i = 0; i < MAX_APICS; i++) {
476
            apic_iter = local_apics[i];
477
            if (apic_iter) {
478
                if (apic_iter->dest_mode == 0xf) {
479
                    if (dest & apic_iter->log_dest)
480
                        set_bit(deliver_bitmask, i);
481
                } else if (apic_iter->dest_mode == 0x0) {
482
                    if ((dest & 0xf0) == (apic_iter->log_dest & 0xf0) &&
483
                        (dest & apic_iter->log_dest & 0x0f)) {
484
                        set_bit(deliver_bitmask, i);
485
                    }
486
                }
487
            }
488
        }
489
    }
490
}
491

    
492

    
493
void apic_init_reset(CPUState *env)
494
{
495
    APICState *s = env->apic_state;
496
    int i;
497

    
498
    if (!s)
499
        return;
500

    
501
    s->tpr = 0;
502
    s->spurious_vec = 0xff;
503
    s->log_dest = 0;
504
    s->dest_mode = 0xf;
505
    memset(s->isr, 0, sizeof(s->isr));
506
    memset(s->tmr, 0, sizeof(s->tmr));
507
    memset(s->irr, 0, sizeof(s->irr));
508
    for(i = 0; i < APIC_LVT_NB; i++)
509
        s->lvt[i] = 1 << 16; /* mask LVT */
510
    s->esr = 0;
511
    memset(s->icr, 0, sizeof(s->icr));
512
    s->divide_conf = 0;
513
    s->count_shift = 0;
514
    s->initial_count = 0;
515
    s->initial_count_load_time = 0;
516
    s->next_time = 0;
517
    s->wait_for_sipi = 1;
518

    
519
    env->halted = !(s->apicbase & MSR_IA32_APICBASE_BSP);
520
}
521

    
522
static void apic_startup(APICState *s, int vector_num)
523
{
524
    s->sipi_vector = vector_num;
525
    cpu_interrupt(s->cpu_env, CPU_INTERRUPT_SIPI);
526
}
527

    
528
void apic_sipi(CPUState *env)
529
{
530
    APICState *s = env->apic_state;
531

    
532
    cpu_reset_interrupt(env, CPU_INTERRUPT_SIPI);
533

    
534
    if (!s->wait_for_sipi)
535
        return;
536

    
537
    env->eip = 0;
538
    cpu_x86_load_seg_cache(env, R_CS, s->sipi_vector << 8, s->sipi_vector << 12,
539
                           env->segs[R_CS].limit, env->segs[R_CS].flags);
540
    env->halted = 0;
541
    s->wait_for_sipi = 0;
542
}
543

    
544
static void apic_deliver(APICState *s, uint8_t dest, uint8_t dest_mode,
545
                         uint8_t delivery_mode, uint8_t vector_num,
546
                         uint8_t polarity, uint8_t trigger_mode)
547
{
548
    uint32_t deliver_bitmask[MAX_APIC_WORDS];
549
    int dest_shorthand = (s->icr[0] >> 18) & 3;
550
    APICState *apic_iter;
551

    
552
    switch (dest_shorthand) {
553
    case 0:
554
        apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode);
555
        break;
556
    case 1:
557
        memset(deliver_bitmask, 0x00, sizeof(deliver_bitmask));
558
        set_bit(deliver_bitmask, s->idx);
559
        break;
560
    case 2:
561
        memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask));
562
        break;
563
    case 3:
564
        memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask));
565
        reset_bit(deliver_bitmask, s->idx);
566
        break;
567
    }
568

    
569
    switch (delivery_mode) {
570
        case APIC_DM_INIT:
571
            {
572
                int trig_mode = (s->icr[0] >> 15) & 1;
573
                int level = (s->icr[0] >> 14) & 1;
574
                if (level == 0 && trig_mode == 1) {
575
                    foreach_apic(apic_iter, deliver_bitmask,
576
                                 apic_iter->arb_id = apic_iter->id );
577
                    return;
578
                }
579
            }
580
            break;
581

    
582
        case APIC_DM_SIPI:
583
            foreach_apic(apic_iter, deliver_bitmask,
584
                         apic_startup(apic_iter, vector_num) );
585
            return;
586
    }
587

    
588
    apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, polarity,
589
                     trigger_mode);
590
}
591

    
592
int apic_get_interrupt(APICState *s)
593
{
594
    int intno;
595

    
596
    /* if the APIC is installed or enabled, we let the 8259 handle the
597
       IRQs */
598
    if (!s)
599
        return -1;
600
    if (!(s->spurious_vec & APIC_SV_ENABLE))
601
        return -1;
602

    
603
    /* XXX: spurious IRQ handling */
604
    intno = get_highest_priority_int(s->irr);
605
    if (intno < 0)
606
        return -1;
607
    if (s->tpr && intno <= s->tpr)
608
        return s->spurious_vec & 0xff;
609
    reset_bit(s->irr, intno);
610
    set_bit(s->isr, intno);
611
    apic_update_irq(s);
612
    return intno;
613
}
614

    
615
int apic_accept_pic_intr(APICState *s)
616
{
617
    uint32_t lvt0;
618

    
619
    if (!s)
620
        return -1;
621

    
622
    lvt0 = s->lvt[APIC_LVT_LINT0];
623

    
624
    if ((s->apicbase & MSR_IA32_APICBASE_ENABLE) == 0 ||
625
        (lvt0 & APIC_LVT_MASKED) == 0)
626
        return 1;
627

    
628
    return 0;
629
}
630

    
631
static uint32_t apic_get_current_count(APICState *s)
632
{
633
    int64_t d;
634
    uint32_t val;
635
    d = (qemu_get_clock(vm_clock) - s->initial_count_load_time) >>
636
        s->count_shift;
637
    if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
638
        /* periodic */
639
        val = s->initial_count - (d % ((uint64_t)s->initial_count + 1));
640
    } else {
641
        if (d >= s->initial_count)
642
            val = 0;
643
        else
644
            val = s->initial_count - d;
645
    }
646
    return val;
647
}
648

    
649
static void apic_timer_update(APICState *s, int64_t current_time)
650
{
651
    int64_t next_time, d;
652

    
653
    if (!(s->lvt[APIC_LVT_TIMER] & APIC_LVT_MASKED)) {
654
        d = (current_time - s->initial_count_load_time) >>
655
            s->count_shift;
656
        if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
657
            if (!s->initial_count)
658
                goto no_timer;
659
            d = ((d / ((uint64_t)s->initial_count + 1)) + 1) * ((uint64_t)s->initial_count + 1);
660
        } else {
661
            if (d >= s->initial_count)
662
                goto no_timer;
663
            d = (uint64_t)s->initial_count + 1;
664
        }
665
        next_time = s->initial_count_load_time + (d << s->count_shift);
666
        qemu_mod_timer(s->timer, next_time);
667
        s->next_time = next_time;
668
    } else {
669
    no_timer:
670
        qemu_del_timer(s->timer);
671
    }
672
}
673

    
674
static void apic_timer(void *opaque)
675
{
676
    APICState *s = opaque;
677

    
678
    apic_local_deliver(s, APIC_LVT_TIMER);
679
    apic_timer_update(s, s->next_time);
680
}
681

    
682
static uint32_t apic_mem_readb(void *opaque, target_phys_addr_t addr)
683
{
684
    return 0;
685
}
686

    
687
static uint32_t apic_mem_readw(void *opaque, target_phys_addr_t addr)
688
{
689
    return 0;
690
}
691

    
692
static void apic_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
693
{
694
}
695

    
696
static void apic_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
697
{
698
}
699

    
700
static uint32_t apic_mem_readl(void *opaque, target_phys_addr_t addr)
701
{
702
    CPUState *env;
703
    APICState *s;
704
    uint32_t val;
705
    int index;
706

    
707
    env = cpu_single_env;
708
    if (!env)
709
        return 0;
710
    s = env->apic_state;
711

    
712
    index = (addr >> 4) & 0xff;
713
    switch(index) {
714
    case 0x02: /* id */
715
        val = s->id << 24;
716
        break;
717
    case 0x03: /* version */
718
        val = 0x11 | ((APIC_LVT_NB - 1) << 16); /* version 0x11 */
719
        break;
720
    case 0x08:
721
        val = s->tpr;
722
        break;
723
    case 0x09:
724
        val = apic_get_arb_pri(s);
725
        break;
726
    case 0x0a:
727
        /* ppr */
728
        val = apic_get_ppr(s);
729
        break;
730
    case 0x0b:
731
        val = 0;
732
        break;
733
    case 0x0d:
734
        val = s->log_dest << 24;
735
        break;
736
    case 0x0e:
737
        val = s->dest_mode << 28;
738
        break;
739
    case 0x0f:
740
        val = s->spurious_vec;
741
        break;
742
    case 0x10 ... 0x17:
743
        val = s->isr[index & 7];
744
        break;
745
    case 0x18 ... 0x1f:
746
        val = s->tmr[index & 7];
747
        break;
748
    case 0x20 ... 0x27:
749
        val = s->irr[index & 7];
750
        break;
751
    case 0x28:
752
        val = s->esr;
753
        break;
754
    case 0x30:
755
    case 0x31:
756
        val = s->icr[index & 1];
757
        break;
758
    case 0x32 ... 0x37:
759
        val = s->lvt[index - 0x32];
760
        break;
761
    case 0x38:
762
        val = s->initial_count;
763
        break;
764
    case 0x39:
765
        val = apic_get_current_count(s);
766
        break;
767
    case 0x3e:
768
        val = s->divide_conf;
769
        break;
770
    default:
771
        s->esr |= ESR_ILLEGAL_ADDRESS;
772
        val = 0;
773
        break;
774
    }
775
    DPRINTF("read: " TARGET_FMT_plx " = %08x\n", addr, val);
776
    return val;
777
}
778

    
779
static void apic_send_msi(target_phys_addr_t addr, uint32 data)
780
{
781
    uint8_t dest = (addr & MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT;
782
    uint8_t vector = (data & MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT;
783
    uint8_t dest_mode = (addr >> MSI_ADDR_DEST_MODE_SHIFT) & 0x1;
784
    uint8_t trigger_mode = (data >> MSI_DATA_TRIGGER_SHIFT) & 0x1;
785
    uint8_t delivery = (data >> MSI_DATA_DELIVERY_MODE_SHIFT) & 0x7;
786
    /* XXX: Ignore redirection hint. */
787
    apic_deliver_irq(dest, dest_mode, delivery, vector, 0, trigger_mode);
788
}
789

    
790
static void apic_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
791
{
792
    CPUState *env;
793
    APICState *s;
794
    int index = (addr >> 4) & 0xff;
795
    if (addr > 0xfff || !index) {
796
        /* MSI and MMIO APIC are at the same memory location,
797
         * but actually not on the global bus: MSI is on PCI bus
798
         * APIC is connected directly to the CPU.
799
         * Mapping them on the global bus happens to work because
800
         * MSI registers are reserved in APIC MMIO and vice versa. */
801
        apic_send_msi(addr, val);
802
        return;
803
    }
804

    
805
    env = cpu_single_env;
806
    if (!env)
807
        return;
808
    s = env->apic_state;
809

    
810
    DPRINTF("write: " TARGET_FMT_plx " = %08x\n", addr, val);
811

    
812
    switch(index) {
813
    case 0x02:
814
        s->id = (val >> 24);
815
        break;
816
    case 0x03:
817
        break;
818
    case 0x08:
819
        s->tpr = val;
820
        apic_update_irq(s);
821
        break;
822
    case 0x09:
823
    case 0x0a:
824
        break;
825
    case 0x0b: /* EOI */
826
        apic_eoi(s);
827
        break;
828
    case 0x0d:
829
        s->log_dest = val >> 24;
830
        break;
831
    case 0x0e:
832
        s->dest_mode = val >> 28;
833
        break;
834
    case 0x0f:
835
        s->spurious_vec = val & 0x1ff;
836
        apic_update_irq(s);
837
        break;
838
    case 0x10 ... 0x17:
839
    case 0x18 ... 0x1f:
840
    case 0x20 ... 0x27:
841
    case 0x28:
842
        break;
843
    case 0x30:
844
        s->icr[0] = val;
845
        apic_deliver(s, (s->icr[1] >> 24) & 0xff, (s->icr[0] >> 11) & 1,
846
                     (s->icr[0] >> 8) & 7, (s->icr[0] & 0xff),
847
                     (s->icr[0] >> 14) & 1, (s->icr[0] >> 15) & 1);
848
        break;
849
    case 0x31:
850
        s->icr[1] = val;
851
        break;
852
    case 0x32 ... 0x37:
853
        {
854
            int n = index - 0x32;
855
            s->lvt[n] = val;
856
            if (n == APIC_LVT_TIMER)
857
                apic_timer_update(s, qemu_get_clock(vm_clock));
858
        }
859
        break;
860
    case 0x38:
861
        s->initial_count = val;
862
        s->initial_count_load_time = qemu_get_clock(vm_clock);
863
        apic_timer_update(s, s->initial_count_load_time);
864
        break;
865
    case 0x39:
866
        break;
867
    case 0x3e:
868
        {
869
            int v;
870
            s->divide_conf = val & 0xb;
871
            v = (s->divide_conf & 3) | ((s->divide_conf >> 1) & 4);
872
            s->count_shift = (v + 1) & 7;
873
        }
874
        break;
875
    default:
876
        s->esr |= ESR_ILLEGAL_ADDRESS;
877
        break;
878
    }
879
}
880

    
881
/* This function is only used for old state version 1 and 2 */
882
static int apic_load_old(QEMUFile *f, void *opaque, int version_id)
883
{
884
    APICState *s = opaque;
885
    int i;
886

    
887
    if (version_id > 2)
888
        return -EINVAL;
889

    
890
    /* XXX: what if the base changes? (registered memory regions) */
891
    qemu_get_be32s(f, &s->apicbase);
892
    qemu_get_8s(f, &s->id);
893
    qemu_get_8s(f, &s->arb_id);
894
    qemu_get_8s(f, &s->tpr);
895
    qemu_get_be32s(f, &s->spurious_vec);
896
    qemu_get_8s(f, &s->log_dest);
897
    qemu_get_8s(f, &s->dest_mode);
898
    for (i = 0; i < 8; i++) {
899
        qemu_get_be32s(f, &s->isr[i]);
900
        qemu_get_be32s(f, &s->tmr[i]);
901
        qemu_get_be32s(f, &s->irr[i]);
902
    }
903
    for (i = 0; i < APIC_LVT_NB; i++) {
904
        qemu_get_be32s(f, &s->lvt[i]);
905
    }
906
    qemu_get_be32s(f, &s->esr);
907
    qemu_get_be32s(f, &s->icr[0]);
908
    qemu_get_be32s(f, &s->icr[1]);
909
    qemu_get_be32s(f, &s->divide_conf);
910
    s->count_shift=qemu_get_be32(f);
911
    qemu_get_be32s(f, &s->initial_count);
912
    s->initial_count_load_time=qemu_get_be64(f);
913
    s->next_time=qemu_get_be64(f);
914

    
915
    if (version_id >= 2)
916
        qemu_get_timer(f, s->timer);
917
    return 0;
918
}
919

    
920
static const VMStateDescription vmstate_apic = {
921
    .name = "apic",
922
    .version_id = 3,
923
    .minimum_version_id = 3,
924
    .minimum_version_id_old = 1,
925
    .load_state_old = apic_load_old,
926
    .fields      = (VMStateField []) {
927
        VMSTATE_UINT32(apicbase, APICState),
928
        VMSTATE_UINT8(id, APICState),
929
        VMSTATE_UINT8(arb_id, APICState),
930
        VMSTATE_UINT8(tpr, APICState),
931
        VMSTATE_UINT32(spurious_vec, APICState),
932
        VMSTATE_UINT8(log_dest, APICState),
933
        VMSTATE_UINT8(dest_mode, APICState),
934
        VMSTATE_UINT32_ARRAY(isr, APICState, 8),
935
        VMSTATE_UINT32_ARRAY(tmr, APICState, 8),
936
        VMSTATE_UINT32_ARRAY(irr, APICState, 8),
937
        VMSTATE_UINT32_ARRAY(lvt, APICState, APIC_LVT_NB),
938
        VMSTATE_UINT32(esr, APICState),
939
        VMSTATE_UINT32_ARRAY(icr, APICState, 2),
940
        VMSTATE_UINT32(divide_conf, APICState),
941
        VMSTATE_INT32(count_shift, APICState),
942
        VMSTATE_UINT32(initial_count, APICState),
943
        VMSTATE_INT64(initial_count_load_time, APICState),
944
        VMSTATE_INT64(next_time, APICState),
945
        VMSTATE_TIMER(timer, APICState),
946
        VMSTATE_END_OF_LIST()
947
    }
948
};
949

    
950
static void apic_reset(void *opaque)
951
{
952
    APICState *s = opaque;
953
    int bsp;
954

    
955
    bsp = cpu_is_bsp(s->cpu_env);
956
    s->apicbase = 0xfee00000 |
957
        (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;
958

    
959
    cpu_reset(s->cpu_env);
960
    apic_init_reset(s->cpu_env);
961

    
962
    if (bsp) {
963
        /*
964
         * LINT0 delivery mode on CPU #0 is set to ExtInt at initialization
965
         * time typically by BIOS, so PIC interrupt can be delivered to the
966
         * processor when local APIC is enabled.
967
         */
968
        s->lvt[APIC_LVT_LINT0] = 0x700;
969
    }
970
}
971

    
972
static CPUReadMemoryFunc * const apic_mem_read[3] = {
973
    apic_mem_readb,
974
    apic_mem_readw,
975
    apic_mem_readl,
976
};
977

    
978
static CPUWriteMemoryFunc * const apic_mem_write[3] = {
979
    apic_mem_writeb,
980
    apic_mem_writew,
981
    apic_mem_writel,
982
};
983

    
984
int apic_init(CPUState *env)
985
{
986
    APICState *s;
987

    
988
    if (last_apic_idx >= MAX_APICS)
989
        return -1;
990
    s = qemu_mallocz(sizeof(APICState));
991
    env->apic_state = s;
992
    s->idx = last_apic_idx++;
993
    s->id = env->cpuid_apic_id;
994
    s->cpu_env = env;
995

    
996
    msix_supported = 1;
997

    
998
    /* XXX: mapping more APICs at the same memory location */
999
    if (apic_io_memory == 0) {
1000
        /* NOTE: the APIC is directly connected to the CPU - it is not
1001
           on the global memory bus. */
1002
        apic_io_memory = cpu_register_io_memory(apic_mem_read,
1003
                                                apic_mem_write, NULL);
1004
        /* XXX: what if the base changes? */
1005
        cpu_register_physical_memory(MSI_ADDR_BASE, MSI_ADDR_SIZE,
1006
                                     apic_io_memory);
1007
    }
1008
    s->timer = qemu_new_timer(vm_clock, apic_timer, s);
1009

    
1010
    vmstate_register(s->idx, &vmstate_apic, s);
1011
    qemu_register_reset(apic_reset, s);
1012

    
1013
    local_apics[s->idx] = s;
1014
    return 0;
1015
}