Statistics
| Branch: | Revision:

root / hw / apic.c @ 4a942cea

History | View | Annotate | Download (26.5 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(APICState *s, uint64_t val)
314
{
315
    DPRINTF("cpu_set_apic_base: %016" PRIx64 "\n", val);
316
    if (!s)
317
        return;
318
    s->apicbase = (val & 0xfffff000) |
319
        (s->apicbase & (MSR_IA32_APICBASE_BSP | MSR_IA32_APICBASE_ENABLE));
320
    /* if disabled, cannot be enabled again */
321
    if (!(val & MSR_IA32_APICBASE_ENABLE)) {
322
        s->apicbase &= ~MSR_IA32_APICBASE_ENABLE;
323
        s->cpu_env->cpuid_features &= ~CPUID_APIC;
324
        s->spurious_vec &= ~APIC_SV_ENABLE;
325
    }
326
}
327

    
328
uint64_t cpu_get_apic_base(APICState *s)
329
{
330
    DPRINTF("cpu_get_apic_base: %016" PRIx64 "\n",
331
            s ? (uint64_t)s->apicbase: 0);
332
    return s ? s->apicbase : 0;
333
}
334

    
335
void cpu_set_apic_tpr(APICState *s, uint8_t val)
336
{
337
    if (!s)
338
        return;
339
    s->tpr = (val & 0x0f) << 4;
340
    apic_update_irq(s);
341
}
342

    
343
uint8_t cpu_get_apic_tpr(APICState *s)
344
{
345
    return s ? s->tpr >> 4 : 0;
346
}
347

    
348
/* return -1 if no bit is set */
349
static int get_highest_priority_int(uint32_t *tab)
350
{
351
    int i;
352
    for(i = 7; i >= 0; i--) {
353
        if (tab[i] != 0) {
354
            return i * 32 + fls_bit(tab[i]);
355
        }
356
    }
357
    return -1;
358
}
359

    
360
static int apic_get_ppr(APICState *s)
361
{
362
    int tpr, isrv, ppr;
363

    
364
    tpr = (s->tpr >> 4);
365
    isrv = get_highest_priority_int(s->isr);
366
    if (isrv < 0)
367
        isrv = 0;
368
    isrv >>= 4;
369
    if (tpr >= isrv)
370
        ppr = s->tpr;
371
    else
372
        ppr = isrv << 4;
373
    return ppr;
374
}
375

    
376
static int apic_get_arb_pri(APICState *s)
377
{
378
    /* XXX: arbitration */
379
    return 0;
380
}
381

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

    
397
void apic_reset_irq_delivered(void)
398
{
399
    DPRINTF_C("%s: old coalescing %d\n", __func__, apic_irq_delivered);
400
    apic_irq_delivered = 0;
401
}
402

    
403
int apic_get_irq_delivered(void)
404
{
405
    DPRINTF_C("%s: returning coalescing %d\n", __func__, apic_irq_delivered);
406
    return apic_irq_delivered;
407
}
408

    
409
static void apic_set_irq(APICState *s, int vector_num, int trigger_mode)
410
{
411
    apic_irq_delivered += !get_bit(s->irr, vector_num);
412
    DPRINTF_C("%s: coalescing %d\n", __func__, apic_irq_delivered);
413

    
414
    set_bit(s->irr, vector_num);
415
    if (trigger_mode)
416
        set_bit(s->tmr, vector_num);
417
    else
418
        reset_bit(s->tmr, vector_num);
419
    apic_update_irq(s);
420
}
421

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

    
434
static int apic_find_dest(uint8_t dest)
435
{
436
    APICState *apic = local_apics[dest];
437
    int i;
438

    
439
    if (apic && apic->id == dest)
440
        return dest;  /* shortcut in case apic->id == apic->idx */
441

    
442
    for (i = 0; i < MAX_APICS; i++) {
443
        apic = local_apics[i];
444
        if (apic && apic->id == dest)
445
            return i;
446
    }
447

    
448
    return -1;
449
}
450

    
451
static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask,
452
                                      uint8_t dest, uint8_t dest_mode)
453
{
454
    APICState *apic_iter;
455
    int i;
456

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

    
486

    
487
void apic_init_reset(APICState *s)
488
{
489
    int i;
490

    
491
    if (!s)
492
        return;
493

    
494
    s->tpr = 0;
495
    s->spurious_vec = 0xff;
496
    s->log_dest = 0;
497
    s->dest_mode = 0xf;
498
    memset(s->isr, 0, sizeof(s->isr));
499
    memset(s->tmr, 0, sizeof(s->tmr));
500
    memset(s->irr, 0, sizeof(s->irr));
501
    for(i = 0; i < APIC_LVT_NB; i++)
502
        s->lvt[i] = 1 << 16; /* mask LVT */
503
    s->esr = 0;
504
    memset(s->icr, 0, sizeof(s->icr));
505
    s->divide_conf = 0;
506
    s->count_shift = 0;
507
    s->initial_count = 0;
508
    s->initial_count_load_time = 0;
509
    s->next_time = 0;
510
    s->wait_for_sipi = 1;
511

    
512
    s->cpu_env->halted = !(s->apicbase & MSR_IA32_APICBASE_BSP);
513
}
514

    
515
static void apic_startup(APICState *s, int vector_num)
516
{
517
    s->sipi_vector = vector_num;
518
    cpu_interrupt(s->cpu_env, CPU_INTERRUPT_SIPI);
519
}
520

    
521
void apic_sipi(APICState *s)
522
{
523
    cpu_reset_interrupt(s->cpu_env, CPU_INTERRUPT_SIPI);
524

    
525
    if (!s->wait_for_sipi)
526
        return;
527

    
528
    s->cpu_env->eip = 0;
529
    cpu_x86_load_seg_cache(s->cpu_env, R_CS, s->sipi_vector << 8,
530
                           s->sipi_vector << 12,
531
                           s->cpu_env->segs[R_CS].limit,
532
                           s->cpu_env->segs[R_CS].flags);
533
    s->cpu_env->halted = 0;
534
    s->wait_for_sipi = 0;
535
}
536

    
537
static void apic_deliver(APICState *s, uint8_t dest, uint8_t dest_mode,
538
                         uint8_t delivery_mode, uint8_t vector_num,
539
                         uint8_t polarity, uint8_t trigger_mode)
540
{
541
    uint32_t deliver_bitmask[MAX_APIC_WORDS];
542
    int dest_shorthand = (s->icr[0] >> 18) & 3;
543
    APICState *apic_iter;
544

    
545
    switch (dest_shorthand) {
546
    case 0:
547
        apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode);
548
        break;
549
    case 1:
550
        memset(deliver_bitmask, 0x00, sizeof(deliver_bitmask));
551
        set_bit(deliver_bitmask, s->idx);
552
        break;
553
    case 2:
554
        memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask));
555
        break;
556
    case 3:
557
        memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask));
558
        reset_bit(deliver_bitmask, s->idx);
559
        break;
560
    }
561

    
562
    switch (delivery_mode) {
563
        case APIC_DM_INIT:
564
            {
565
                int trig_mode = (s->icr[0] >> 15) & 1;
566
                int level = (s->icr[0] >> 14) & 1;
567
                if (level == 0 && trig_mode == 1) {
568
                    foreach_apic(apic_iter, deliver_bitmask,
569
                                 apic_iter->arb_id = apic_iter->id );
570
                    return;
571
                }
572
            }
573
            break;
574

    
575
        case APIC_DM_SIPI:
576
            foreach_apic(apic_iter, deliver_bitmask,
577
                         apic_startup(apic_iter, vector_num) );
578
            return;
579
    }
580

    
581
    apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, polarity,
582
                     trigger_mode);
583
}
584

    
585
int apic_get_interrupt(APICState *s)
586
{
587
    int intno;
588

    
589
    /* if the APIC is installed or enabled, we let the 8259 handle the
590
       IRQs */
591
    if (!s)
592
        return -1;
593
    if (!(s->spurious_vec & APIC_SV_ENABLE))
594
        return -1;
595

    
596
    /* XXX: spurious IRQ handling */
597
    intno = get_highest_priority_int(s->irr);
598
    if (intno < 0)
599
        return -1;
600
    if (s->tpr && intno <= s->tpr)
601
        return s->spurious_vec & 0xff;
602
    reset_bit(s->irr, intno);
603
    set_bit(s->isr, intno);
604
    apic_update_irq(s);
605
    return intno;
606
}
607

    
608
int apic_accept_pic_intr(APICState *s)
609
{
610
    uint32_t lvt0;
611

    
612
    if (!s)
613
        return -1;
614

    
615
    lvt0 = s->lvt[APIC_LVT_LINT0];
616

    
617
    if ((s->apicbase & MSR_IA32_APICBASE_ENABLE) == 0 ||
618
        (lvt0 & APIC_LVT_MASKED) == 0)
619
        return 1;
620

    
621
    return 0;
622
}
623

    
624
static uint32_t apic_get_current_count(APICState *s)
625
{
626
    int64_t d;
627
    uint32_t val;
628
    d = (qemu_get_clock(vm_clock) - s->initial_count_load_time) >>
629
        s->count_shift;
630
    if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
631
        /* periodic */
632
        val = s->initial_count - (d % ((uint64_t)s->initial_count + 1));
633
    } else {
634
        if (d >= s->initial_count)
635
            val = 0;
636
        else
637
            val = s->initial_count - d;
638
    }
639
    return val;
640
}
641

    
642
static void apic_timer_update(APICState *s, int64_t current_time)
643
{
644
    int64_t next_time, d;
645

    
646
    if (!(s->lvt[APIC_LVT_TIMER] & APIC_LVT_MASKED)) {
647
        d = (current_time - s->initial_count_load_time) >>
648
            s->count_shift;
649
        if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
650
            if (!s->initial_count)
651
                goto no_timer;
652
            d = ((d / ((uint64_t)s->initial_count + 1)) + 1) * ((uint64_t)s->initial_count + 1);
653
        } else {
654
            if (d >= s->initial_count)
655
                goto no_timer;
656
            d = (uint64_t)s->initial_count + 1;
657
        }
658
        next_time = s->initial_count_load_time + (d << s->count_shift);
659
        qemu_mod_timer(s->timer, next_time);
660
        s->next_time = next_time;
661
    } else {
662
    no_timer:
663
        qemu_del_timer(s->timer);
664
    }
665
}
666

    
667
static void apic_timer(void *opaque)
668
{
669
    APICState *s = opaque;
670

    
671
    apic_local_deliver(s, APIC_LVT_TIMER);
672
    apic_timer_update(s, s->next_time);
673
}
674

    
675
static uint32_t apic_mem_readb(void *opaque, target_phys_addr_t addr)
676
{
677
    return 0;
678
}
679

    
680
static uint32_t apic_mem_readw(void *opaque, target_phys_addr_t addr)
681
{
682
    return 0;
683
}
684

    
685
static void apic_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
686
{
687
}
688

    
689
static void apic_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
690
{
691
}
692

    
693
static uint32_t apic_mem_readl(void *opaque, target_phys_addr_t addr)
694
{
695
    CPUState *env;
696
    APICState *s;
697
    uint32_t val;
698
    int index;
699

    
700
    env = cpu_single_env;
701
    if (!env)
702
        return 0;
703
    s = env->apic_state;
704

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

    
772
static void apic_send_msi(target_phys_addr_t addr, uint32 data)
773
{
774
    uint8_t dest = (addr & MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT;
775
    uint8_t vector = (data & MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT;
776
    uint8_t dest_mode = (addr >> MSI_ADDR_DEST_MODE_SHIFT) & 0x1;
777
    uint8_t trigger_mode = (data >> MSI_DATA_TRIGGER_SHIFT) & 0x1;
778
    uint8_t delivery = (data >> MSI_DATA_DELIVERY_MODE_SHIFT) & 0x7;
779
    /* XXX: Ignore redirection hint. */
780
    apic_deliver_irq(dest, dest_mode, delivery, vector, 0, trigger_mode);
781
}
782

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

    
798
    env = cpu_single_env;
799
    if (!env)
800
        return;
801
    s = env->apic_state;
802

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

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

    
874
/* This function is only used for old state version 1 and 2 */
875
static int apic_load_old(QEMUFile *f, void *opaque, int version_id)
876
{
877
    APICState *s = opaque;
878
    int i;
879

    
880
    if (version_id > 2)
881
        return -EINVAL;
882

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

    
908
    if (version_id >= 2)
909
        qemu_get_timer(f, s->timer);
910
    return 0;
911
}
912

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

    
943
static void apic_reset(void *opaque)
944
{
945
    APICState *s = opaque;
946
    int bsp;
947

    
948
    bsp = cpu_is_bsp(s->cpu_env);
949
    s->apicbase = 0xfee00000 |
950
        (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;
951

    
952
    cpu_reset(s->cpu_env);
953
    apic_init_reset(s);
954

    
955
    if (bsp) {
956
        /*
957
         * LINT0 delivery mode on CPU #0 is set to ExtInt at initialization
958
         * time typically by BIOS, so PIC interrupt can be delivered to the
959
         * processor when local APIC is enabled.
960
         */
961
        s->lvt[APIC_LVT_LINT0] = 0x700;
962
    }
963
}
964

    
965
static CPUReadMemoryFunc * const apic_mem_read[3] = {
966
    apic_mem_readb,
967
    apic_mem_readw,
968
    apic_mem_readl,
969
};
970

    
971
static CPUWriteMemoryFunc * const apic_mem_write[3] = {
972
    apic_mem_writeb,
973
    apic_mem_writew,
974
    apic_mem_writel,
975
};
976

    
977
int apic_init(CPUState *env)
978
{
979
    APICState *s;
980

    
981
    if (last_apic_idx >= MAX_APICS)
982
        return -1;
983
    s = qemu_mallocz(sizeof(APICState));
984
    env->apic_state = s;
985
    s->idx = last_apic_idx++;
986
    s->id = env->cpuid_apic_id;
987
    s->cpu_env = env;
988

    
989
    msix_supported = 1;
990

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

    
1003
    vmstate_register(s->idx, &vmstate_apic, s);
1004
    qemu_register_reset(apic_reset, s);
1005

    
1006
    local_apics[s->idx] = s;
1007
    return 0;
1008
}