Statistics
| Branch: | Revision:

root / hw / apic.c @ 8546b099

History | View | Annotate | Download (26.9 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 "apic.h"
21
#include "msix.h"
22
#include "qemu-timer.h"
23
#include "host-utils.h"
24
#include "sysbus.h"
25

    
26
//#define DEBUG_APIC
27
//#define DEBUG_COALESCING
28

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

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

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

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

    
61
/* APIC destination mode */
62
#define APIC_DESTMODE_FLAT        0xf
63
#define APIC_DESTMODE_CLUSTER        1
64

    
65
#define APIC_TRIGGER_EDGE  0
66
#define APIC_TRIGGER_LEVEL 1
67

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

    
75
#define ESR_ILLEGAL_ADDRESS (1 << 7)
76

    
77
#define APIC_SV_ENABLE (1 << 8)
78

    
79
#define MAX_APICS 255
80
#define MAX_APIC_WORDS 8
81

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

    
92
#define MSI_ADDR_BASE                   0xfee00000
93
#define MSI_ADDR_SIZE                   0x100000
94

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

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

    
122
static APICState *local_apics[MAX_APICS + 1];
123
static int apic_irq_delivered;
124

    
125
static void apic_set_irq(APICState *s, int vector_num, int trigger_mode);
126
static void apic_update_irq(APICState *s);
127
static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask,
128
                                      uint8_t dest, uint8_t dest_mode);
129

    
130
/* Find first bit starting from msb */
131
static int fls_bit(uint32_t value)
132
{
133
    return 31 - clz32(value);
134
}
135

    
136
/* Find first bit starting from lsb */
137
static int ffs_bit(uint32_t value)
138
{
139
    return ctz32(value);
140
}
141

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

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

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

    
166
static void apic_local_deliver(APICState *s, int vector)
167
{
168
    uint32_t lvt = s->lvt[vector];
169
    int trigger_mode;
170

    
171
    DPRINTF("%s: vector %d delivery mode %d\n", __func__, vector,
172
            (lvt >> 8) & 7);
173
    if (lvt & APIC_LVT_MASKED)
174
        return;
175

    
176
    switch ((lvt >> 8) & 7) {
177
    case APIC_DM_SMI:
178
        cpu_interrupt(s->cpu_env, CPU_INTERRUPT_SMI);
179
        break;
180

    
181
    case APIC_DM_NMI:
182
        cpu_interrupt(s->cpu_env, CPU_INTERRUPT_NMI);
183
        break;
184

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

    
189
    case APIC_DM_FIXED:
190
        trigger_mode = APIC_TRIGGER_EDGE;
191
        if ((vector == APIC_LVT_LINT0 || vector == APIC_LVT_LINT1) &&
192
            (lvt & APIC_LVT_LEVEL_TRIGGER))
193
            trigger_mode = APIC_TRIGGER_LEVEL;
194
        apic_set_irq(s, lvt & 0xff, trigger_mode);
195
    }
196
}
197

    
198
void apic_deliver_pic_intr(APICState *s, int level)
199
{
200
    if (level) {
201
        apic_local_deliver(s, APIC_LVT_LINT0);
202
    } else {
203
        uint32_t lvt = s->lvt[APIC_LVT_LINT0];
204

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

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

    
236
static void apic_bus_deliver(const uint32_t *deliver_bitmask,
237
                             uint8_t delivery_mode,
238
                             uint8_t vector_num, uint8_t polarity,
239
                             uint8_t trigger_mode)
240
{
241
    APICState *apic_iter;
242

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

    
264
        case APIC_DM_FIXED:
265
            break;
266

    
267
        case APIC_DM_SMI:
268
            foreach_apic(apic_iter, deliver_bitmask,
269
                cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_SMI) );
270
            return;
271

    
272
        case APIC_DM_NMI:
273
            foreach_apic(apic_iter, deliver_bitmask,
274
                cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_NMI) );
275
            return;
276

    
277
        case APIC_DM_INIT:
278
            /* normal INIT IPI sent to processors */
279
            foreach_apic(apic_iter, deliver_bitmask,
280
                         cpu_interrupt(apic_iter->cpu_env, CPU_INTERRUPT_INIT) );
281
            return;
282

    
283
        case APIC_DM_EXTINT:
284
            /* handled in I/O APIC code */
285
            break;
286

    
287
        default:
288
            return;
289
    }
290

    
291
    foreach_apic(apic_iter, deliver_bitmask,
292
                 apic_set_irq(apic_iter, vector_num, trigger_mode) );
293
}
294

    
295
void apic_deliver_irq(uint8_t dest, uint8_t dest_mode,
296
                      uint8_t delivery_mode, uint8_t vector_num,
297
                      uint8_t polarity, uint8_t trigger_mode)
298
{
299
    uint32_t deliver_bitmask[MAX_APIC_WORDS];
300

    
301
    DPRINTF("%s: dest %d dest_mode %d delivery_mode %d vector %d"
302
            " polarity %d trigger_mode %d\n", __func__, dest, dest_mode,
303
            delivery_mode, vector_num, polarity, trigger_mode);
304
    apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode);
305
    apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, polarity,
306
                     trigger_mode);
307
}
308

    
309
void cpu_set_apic_base(APICState *s, uint64_t val)
310
{
311
    DPRINTF("cpu_set_apic_base: %016" PRIx64 "\n", val);
312
    if (!s)
313
        return;
314
    s->apicbase = (val & 0xfffff000) |
315
        (s->apicbase & (MSR_IA32_APICBASE_BSP | MSR_IA32_APICBASE_ENABLE));
316
    /* if disabled, cannot be enabled again */
317
    if (!(val & MSR_IA32_APICBASE_ENABLE)) {
318
        s->apicbase &= ~MSR_IA32_APICBASE_ENABLE;
319
        cpu_clear_apic_feature(s->cpu_env);
320
        s->spurious_vec &= ~APIC_SV_ENABLE;
321
    }
322
}
323

    
324
uint64_t cpu_get_apic_base(APICState *s)
325
{
326
    DPRINTF("cpu_get_apic_base: %016" PRIx64 "\n",
327
            s ? (uint64_t)s->apicbase: 0);
328
    return s ? s->apicbase : 0;
329
}
330

    
331
void cpu_set_apic_tpr(APICState *s, uint8_t val)
332
{
333
    if (!s)
334
        return;
335
    s->tpr = (val & 0x0f) << 4;
336
    apic_update_irq(s);
337
}
338

    
339
uint8_t cpu_get_apic_tpr(APICState *s)
340
{
341
    return s ? s->tpr >> 4 : 0;
342
}
343

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

    
356
static int apic_get_ppr(APICState *s)
357
{
358
    int tpr, isrv, ppr;
359

    
360
    tpr = (s->tpr >> 4);
361
    isrv = get_highest_priority_int(s->isr);
362
    if (isrv < 0)
363
        isrv = 0;
364
    isrv >>= 4;
365
    if (tpr >= isrv)
366
        ppr = s->tpr;
367
    else
368
        ppr = isrv << 4;
369
    return ppr;
370
}
371

    
372
static int apic_get_arb_pri(APICState *s)
373
{
374
    /* XXX: arbitration */
375
    return 0;
376
}
377

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

    
393
void apic_reset_irq_delivered(void)
394
{
395
    DPRINTF_C("%s: old coalescing %d\n", __func__, apic_irq_delivered);
396
    apic_irq_delivered = 0;
397
}
398

    
399
int apic_get_irq_delivered(void)
400
{
401
    DPRINTF_C("%s: returning coalescing %d\n", __func__, apic_irq_delivered);
402
    return apic_irq_delivered;
403
}
404

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

    
410
    set_bit(s->irr, vector_num);
411
    if (trigger_mode)
412
        set_bit(s->tmr, vector_num);
413
    else
414
        reset_bit(s->tmr, vector_num);
415
    apic_update_irq(s);
416
}
417

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

    
430
static int apic_find_dest(uint8_t dest)
431
{
432
    APICState *apic = local_apics[dest];
433
    int i;
434

    
435
    if (apic && apic->id == dest)
436
        return dest;  /* shortcut in case apic->id == apic->idx */
437

    
438
    for (i = 0; i < MAX_APICS; i++) {
439
        apic = local_apics[i];
440
        if (apic && apic->id == dest)
441
            return i;
442
    }
443

    
444
    return -1;
445
}
446

    
447
static void apic_get_delivery_bitmask(uint32_t *deliver_bitmask,
448
                                      uint8_t dest, uint8_t dest_mode)
449
{
450
    APICState *apic_iter;
451
    int i;
452

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

    
482

    
483
void apic_init_reset(APICState *s)
484
{
485
    int i;
486

    
487
    if (!s)
488
        return;
489

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

    
509
static void apic_startup(APICState *s, int vector_num)
510
{
511
    s->sipi_vector = vector_num;
512
    cpu_interrupt(s->cpu_env, CPU_INTERRUPT_SIPI);
513
}
514

    
515
void apic_sipi(APICState *s)
516
{
517
    cpu_reset_interrupt(s->cpu_env, CPU_INTERRUPT_SIPI);
518

    
519
    if (!s->wait_for_sipi)
520
        return;
521
    cpu_x86_load_seg_cache_sipi(s->cpu_env, s->sipi_vector);
522
    s->wait_for_sipi = 0;
523
}
524

    
525
static void apic_deliver(APICState *s, uint8_t dest, uint8_t dest_mode,
526
                         uint8_t delivery_mode, uint8_t vector_num,
527
                         uint8_t polarity, uint8_t trigger_mode)
528
{
529
    uint32_t deliver_bitmask[MAX_APIC_WORDS];
530
    int dest_shorthand = (s->icr[0] >> 18) & 3;
531
    APICState *apic_iter;
532

    
533
    switch (dest_shorthand) {
534
    case 0:
535
        apic_get_delivery_bitmask(deliver_bitmask, dest, dest_mode);
536
        break;
537
    case 1:
538
        memset(deliver_bitmask, 0x00, sizeof(deliver_bitmask));
539
        set_bit(deliver_bitmask, s->idx);
540
        break;
541
    case 2:
542
        memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask));
543
        break;
544
    case 3:
545
        memset(deliver_bitmask, 0xff, sizeof(deliver_bitmask));
546
        reset_bit(deliver_bitmask, s->idx);
547
        break;
548
    }
549

    
550
    switch (delivery_mode) {
551
        case APIC_DM_INIT:
552
            {
553
                int trig_mode = (s->icr[0] >> 15) & 1;
554
                int level = (s->icr[0] >> 14) & 1;
555
                if (level == 0 && trig_mode == 1) {
556
                    foreach_apic(apic_iter, deliver_bitmask,
557
                                 apic_iter->arb_id = apic_iter->id );
558
                    return;
559
                }
560
            }
561
            break;
562

    
563
        case APIC_DM_SIPI:
564
            foreach_apic(apic_iter, deliver_bitmask,
565
                         apic_startup(apic_iter, vector_num) );
566
            return;
567
    }
568

    
569
    apic_bus_deliver(deliver_bitmask, delivery_mode, vector_num, polarity,
570
                     trigger_mode);
571
}
572

    
573
int apic_get_interrupt(APICState *s)
574
{
575
    int intno;
576

    
577
    /* if the APIC is installed or enabled, we let the 8259 handle the
578
       IRQs */
579
    if (!s)
580
        return -1;
581
    if (!(s->spurious_vec & APIC_SV_ENABLE))
582
        return -1;
583

    
584
    /* XXX: spurious IRQ handling */
585
    intno = get_highest_priority_int(s->irr);
586
    if (intno < 0)
587
        return -1;
588
    if (s->tpr && intno <= s->tpr)
589
        return s->spurious_vec & 0xff;
590
    reset_bit(s->irr, intno);
591
    set_bit(s->isr, intno);
592
    apic_update_irq(s);
593
    return intno;
594
}
595

    
596
int apic_accept_pic_intr(APICState *s)
597
{
598
    uint32_t lvt0;
599

    
600
    if (!s)
601
        return -1;
602

    
603
    lvt0 = s->lvt[APIC_LVT_LINT0];
604

    
605
    if ((s->apicbase & MSR_IA32_APICBASE_ENABLE) == 0 ||
606
        (lvt0 & APIC_LVT_MASKED) == 0)
607
        return 1;
608

    
609
    return 0;
610
}
611

    
612
static uint32_t apic_get_current_count(APICState *s)
613
{
614
    int64_t d;
615
    uint32_t val;
616
    d = (qemu_get_clock(vm_clock) - s->initial_count_load_time) >>
617
        s->count_shift;
618
    if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
619
        /* periodic */
620
        val = s->initial_count - (d % ((uint64_t)s->initial_count + 1));
621
    } else {
622
        if (d >= s->initial_count)
623
            val = 0;
624
        else
625
            val = s->initial_count - d;
626
    }
627
    return val;
628
}
629

    
630
static void apic_timer_update(APICState *s, int64_t current_time)
631
{
632
    int64_t next_time, d;
633

    
634
    if (!(s->lvt[APIC_LVT_TIMER] & APIC_LVT_MASKED)) {
635
        d = (current_time - s->initial_count_load_time) >>
636
            s->count_shift;
637
        if (s->lvt[APIC_LVT_TIMER] & APIC_LVT_TIMER_PERIODIC) {
638
            if (!s->initial_count)
639
                goto no_timer;
640
            d = ((d / ((uint64_t)s->initial_count + 1)) + 1) * ((uint64_t)s->initial_count + 1);
641
        } else {
642
            if (d >= s->initial_count)
643
                goto no_timer;
644
            d = (uint64_t)s->initial_count + 1;
645
        }
646
        next_time = s->initial_count_load_time + (d << s->count_shift);
647
        qemu_mod_timer(s->timer, next_time);
648
        s->next_time = next_time;
649
    } else {
650
    no_timer:
651
        qemu_del_timer(s->timer);
652
    }
653
}
654

    
655
static void apic_timer(void *opaque)
656
{
657
    APICState *s = opaque;
658

    
659
    apic_local_deliver(s, APIC_LVT_TIMER);
660
    apic_timer_update(s, s->next_time);
661
}
662

    
663
static uint32_t apic_mem_readb(void *opaque, target_phys_addr_t addr)
664
{
665
    return 0;
666
}
667

    
668
static uint32_t apic_mem_readw(void *opaque, target_phys_addr_t addr)
669
{
670
    return 0;
671
}
672

    
673
static void apic_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
674
{
675
}
676

    
677
static void apic_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
678
{
679
}
680

    
681
static uint32_t apic_mem_readl(void *opaque, target_phys_addr_t addr)
682
{
683
    APICState *s;
684
    uint32_t val;
685
    int index;
686

    
687
    s = cpu_get_current_apic();
688
    if (!s) {
689
        return 0;
690
    }
691

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

    
759
static void apic_send_msi(target_phys_addr_t addr, uint32 data)
760
{
761
    uint8_t dest = (addr & MSI_ADDR_DEST_ID_MASK) >> MSI_ADDR_DEST_ID_SHIFT;
762
    uint8_t vector = (data & MSI_DATA_VECTOR_MASK) >> MSI_DATA_VECTOR_SHIFT;
763
    uint8_t dest_mode = (addr >> MSI_ADDR_DEST_MODE_SHIFT) & 0x1;
764
    uint8_t trigger_mode = (data >> MSI_DATA_TRIGGER_SHIFT) & 0x1;
765
    uint8_t delivery = (data >> MSI_DATA_DELIVERY_MODE_SHIFT) & 0x7;
766
    /* XXX: Ignore redirection hint. */
767
    apic_deliver_irq(dest, dest_mode, delivery, vector, 0, trigger_mode);
768
}
769

    
770
static void apic_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
771
{
772
    APICState *s;
773
    int index = (addr >> 4) & 0xff;
774
    if (addr > 0xfff || !index) {
775
        /* MSI and MMIO APIC are at the same memory location,
776
         * but actually not on the global bus: MSI is on PCI bus
777
         * APIC is connected directly to the CPU.
778
         * Mapping them on the global bus happens to work because
779
         * MSI registers are reserved in APIC MMIO and vice versa. */
780
        apic_send_msi(addr, val);
781
        return;
782
    }
783

    
784
    s = cpu_get_current_apic();
785
    if (!s) {
786
        return;
787
    }
788

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

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

    
860
/* This function is only used for old state version 1 and 2 */
861
static int apic_load_old(QEMUFile *f, void *opaque, int version_id)
862
{
863
    APICState *s = opaque;
864
    int i;
865

    
866
    if (version_id > 2)
867
        return -EINVAL;
868

    
869
    /* XXX: what if the base changes? (registered memory regions) */
870
    qemu_get_be32s(f, &s->apicbase);
871
    qemu_get_8s(f, &s->id);
872
    qemu_get_8s(f, &s->arb_id);
873
    qemu_get_8s(f, &s->tpr);
874
    qemu_get_be32s(f, &s->spurious_vec);
875
    qemu_get_8s(f, &s->log_dest);
876
    qemu_get_8s(f, &s->dest_mode);
877
    for (i = 0; i < 8; i++) {
878
        qemu_get_be32s(f, &s->isr[i]);
879
        qemu_get_be32s(f, &s->tmr[i]);
880
        qemu_get_be32s(f, &s->irr[i]);
881
    }
882
    for (i = 0; i < APIC_LVT_NB; i++) {
883
        qemu_get_be32s(f, &s->lvt[i]);
884
    }
885
    qemu_get_be32s(f, &s->esr);
886
    qemu_get_be32s(f, &s->icr[0]);
887
    qemu_get_be32s(f, &s->icr[1]);
888
    qemu_get_be32s(f, &s->divide_conf);
889
    s->count_shift=qemu_get_be32(f);
890
    qemu_get_be32s(f, &s->initial_count);
891
    s->initial_count_load_time=qemu_get_be64(f);
892
    s->next_time=qemu_get_be64(f);
893

    
894
    if (version_id >= 2)
895
        qemu_get_timer(f, s->timer);
896
    return 0;
897
}
898

    
899
static const VMStateDescription vmstate_apic = {
900
    .name = "apic",
901
    .version_id = 3,
902
    .minimum_version_id = 3,
903
    .minimum_version_id_old = 1,
904
    .load_state_old = apic_load_old,
905
    .fields      = (VMStateField []) {
906
        VMSTATE_UINT32(apicbase, APICState),
907
        VMSTATE_UINT8(id, APICState),
908
        VMSTATE_UINT8(arb_id, APICState),
909
        VMSTATE_UINT8(tpr, APICState),
910
        VMSTATE_UINT32(spurious_vec, APICState),
911
        VMSTATE_UINT8(log_dest, APICState),
912
        VMSTATE_UINT8(dest_mode, APICState),
913
        VMSTATE_UINT32_ARRAY(isr, APICState, 8),
914
        VMSTATE_UINT32_ARRAY(tmr, APICState, 8),
915
        VMSTATE_UINT32_ARRAY(irr, APICState, 8),
916
        VMSTATE_UINT32_ARRAY(lvt, APICState, APIC_LVT_NB),
917
        VMSTATE_UINT32(esr, APICState),
918
        VMSTATE_UINT32_ARRAY(icr, APICState, 2),
919
        VMSTATE_UINT32(divide_conf, APICState),
920
        VMSTATE_INT32(count_shift, APICState),
921
        VMSTATE_UINT32(initial_count, APICState),
922
        VMSTATE_INT64(initial_count_load_time, APICState),
923
        VMSTATE_INT64(next_time, APICState),
924
        VMSTATE_TIMER(timer, APICState),
925
        VMSTATE_END_OF_LIST()
926
    }
927
};
928

    
929
static void apic_reset(DeviceState *d)
930
{
931
    APICState *s = DO_UPCAST(APICState, busdev.qdev, d);
932
    int bsp;
933

    
934
    bsp = cpu_is_bsp(s->cpu_env);
935
    s->apicbase = 0xfee00000 |
936
        (bsp ? MSR_IA32_APICBASE_BSP : 0) | MSR_IA32_APICBASE_ENABLE;
937

    
938
    apic_init_reset(s);
939

    
940
    if (bsp) {
941
        /*
942
         * LINT0 delivery mode on CPU #0 is set to ExtInt at initialization
943
         * time typically by BIOS, so PIC interrupt can be delivered to the
944
         * processor when local APIC is enabled.
945
         */
946
        s->lvt[APIC_LVT_LINT0] = 0x700;
947
    }
948
}
949

    
950
static CPUReadMemoryFunc * const apic_mem_read[3] = {
951
    apic_mem_readb,
952
    apic_mem_readw,
953
    apic_mem_readl,
954
};
955

    
956
static CPUWriteMemoryFunc * const apic_mem_write[3] = {
957
    apic_mem_writeb,
958
    apic_mem_writew,
959
    apic_mem_writel,
960
};
961

    
962
APICState *apic_init(void *env, uint8_t apic_id)
963
{
964
    DeviceState *dev;
965
    SysBusDevice *d;
966
    APICState *s;
967
    static int apic_mapped;
968

    
969
    dev = qdev_create(NULL, "apic");
970
    qdev_prop_set_uint8(dev, "id", apic_id);
971
    qdev_prop_set_ptr(dev, "cpu_env", env);
972
    qdev_init_nofail(dev);
973
    d = sysbus_from_qdev(dev);
974

    
975
    /* XXX: mapping more APICs at the same memory location */
976
    if (apic_mapped == 0) {
977
        /* NOTE: the APIC is directly connected to the CPU - it is not
978
           on the global memory bus. */
979
        /* XXX: what if the base changes? */
980
        sysbus_mmio_map(d, 0, MSI_ADDR_BASE);
981
        apic_mapped = 1;
982
    }
983

    
984
    msix_supported = 1;
985

    
986
    s = DO_UPCAST(APICState, busdev.qdev, dev);
987

    
988
    return s;
989
}
990

    
991
static int apic_init1(SysBusDevice *dev)
992
{
993
    APICState *s = FROM_SYSBUS(APICState, dev);
994
    int apic_io_memory;
995
    static int last_apic_idx;
996

    
997
    if (last_apic_idx >= MAX_APICS) {
998
        return -1;
999
    }
1000
    apic_io_memory = cpu_register_io_memory(apic_mem_read,
1001
                                            apic_mem_write, NULL);
1002
    sysbus_init_mmio(dev, MSI_ADDR_SIZE, apic_io_memory);
1003

    
1004
    s->timer = qemu_new_timer(vm_clock, apic_timer, s);
1005
    s->idx = last_apic_idx++;
1006
    local_apics[s->idx] = s;
1007
    return 0;
1008
}
1009

    
1010
static SysBusDeviceInfo apic_info = {
1011
    .init = apic_init1,
1012
    .qdev.name = "apic",
1013
    .qdev.size = sizeof(APICState),
1014
    .qdev.vmsd = &vmstate_apic,
1015
    .qdev.reset = apic_reset,
1016
    .qdev.no_user = 1,
1017
    .qdev.props = (Property[]) {
1018
        DEFINE_PROP_UINT8("id", APICState, id, -1),
1019
        DEFINE_PROP_PTR("cpu_env", APICState, cpu_env),
1020
        DEFINE_PROP_END_OF_LIST(),
1021
    }
1022
};
1023

    
1024
static void apic_register_devices(void)
1025
{
1026
    sysbus_register_withprop(&apic_info);
1027
}
1028

    
1029
device_init(apic_register_devices)