summaryrefslogtreecommitdiff
path: root/arch/x86/machine/ioapic.c
blob: f90e1209521c3dd3caab481169f1b71e3834d5f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
 * Copyright (c) 2017 Richard Braun.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>

#include <kern/init.h>
#include <kern/intr.h>
#include <kern/kmem.h>
#include <kern/log.h>
#include <kern/macros.h>
#include <kern/panic.h>
#include <kern/spinlock.h>
#include <machine/cpu.h>
#include <machine/ioapic.h>
#include <machine/lapic.h>
#include <machine/pic.h>
#include <machine/trap.h>
#include <vm/vm_kmem.h>

#define IOAPIC_REG_VERSION              0x01
#define IOAPIC_REG_IOREDTBL             0x10

#define IOAPIC_VERSION_VERSION_MASK     0x000000ff
#define IOAPIC_VERSION_VERSION_SHIFT    0
#define IOAPIC_VERSION_MAXREDIR_MASK    0x00ff0000
#define IOAPIC_VERSION_MAXREDIR_SHIFT   16

#define IOAPIC_ENTLOW_FIXED_DEST        0x00000
#define IOAPIC_ENTLOW_PHYS_DELIVERY     0x00000
#define IOAPIC_ENTLOW_ACTIVE_LOW        0x02000
#define IOAPIC_ENTLOW_LEVEL             0x08000
#define IOAPIC_ENTLOW_INTRMASK          0x10000

#define IOAPIC_MAX_ENTRIES 24

struct ioapic_map {
    uint8_t regsel;
    uint8_t _reserved0;
    uint8_t _reserved1;
    uint8_t _reserved2;
    uint32_t _reserved3;
    uint32_t _reserved4;
    uint32_t _reserved5;
    uint32_t win;
};

/*
 * Interrupt source override descriptor.
 */
struct ioapic_iso {
    uint32_t gsi;
    uint8_t source;
    bool active_high;
    bool edge_triggered;
};

struct ioapic {
    struct spinlock lock;
    unsigned int id;
    unsigned int apic_id;
    unsigned int version;
    volatile struct ioapic_map *map;
    unsigned int first_gsi;
    unsigned int last_gsi;
};

static unsigned int ioapic_nr_devs;

static struct ioapic_iso ioapic_isos[PIC_MAX_INTR + 1];
static unsigned int ioapic_nr_isos;

static void
ioapic_iso_init(struct ioapic_iso *iso, uint8_t source, uint32_t gsi,
                bool active_high, bool edge_triggered)
{
    iso->source = source;
    iso->gsi = gsi;
    iso->active_high = active_high;
    iso->edge_triggered = edge_triggered;
}

static struct ioapic_iso * __init
ioapic_alloc_iso(void)
{
    struct ioapic_iso *iso;

    if (ioapic_nr_isos >= ARRAY_SIZE(ioapic_isos)) {
        log_err("ioapic: too many interrupt overrides");
        return NULL;
    }

    iso = &ioapic_isos[ioapic_nr_isos];
    ioapic_nr_isos++;
    return iso;
}

static struct ioapic_iso *
ioapic_lookup_iso(unsigned int intr)
{
    struct ioapic_iso *iso;
    unsigned int i;

    for (i = 0; i < ioapic_nr_isos; i++) {
        iso = &ioapic_isos[i];

        if (intr == iso->source) {
            return iso;
        }
    }

    return NULL;
}

static uint32_t
ioapic_read(struct ioapic *ioapic, uint8_t reg)
{
    ioapic->map->regsel = reg;
    return ioapic->map->win;
}

static void
ioapic_write(struct ioapic *ioapic, uint8_t reg, uint32_t value)
{
    ioapic->map->regsel = reg;
    ioapic->map->win = value;
}

static void
ioapic_write_entry_low(struct ioapic *ioapic, unsigned int id, uint32_t value)
{
    assert(id < IOAPIC_MAX_ENTRIES);
    ioapic_write(ioapic, IOAPIC_REG_IOREDTBL + (id * 2), value);
}

static void
ioapic_write_entry_high(struct ioapic *ioapic, unsigned int id, uint32_t value)
{
    assert(id < IOAPIC_MAX_ENTRIES);
    ioapic_write(ioapic, IOAPIC_REG_IOREDTBL + (id * 2) + 1, value);
}

static void
ioapic_intr(struct trap_frame *frame)
{
    intr_handle(frame->vector - TRAP_INTR_FIRST);
}

static struct ioapic * __init
ioapic_create(unsigned int apic_id, uintptr_t addr, unsigned int gsi_base)
{
    struct ioapic *ioapic;
    unsigned int i, nr_gsis;
    uint32_t value;

    ioapic = kmem_alloc(sizeof(*ioapic));

    if (ioapic == NULL) {
        panic("ioapic: unable to allocate memory for controller");
    }

    spinlock_init(&ioapic->lock);
    ioapic->id = ioapic_nr_devs;
    ioapic->apic_id = apic_id;
    ioapic->first_gsi = gsi_base;

    ioapic->map = vm_kmem_map_pa(addr, sizeof(*ioapic->map), NULL, NULL);

    if (ioapic->map == NULL) {
        panic("ioapic: unable to map register window in kernel map");
    }

    value = ioapic_read(ioapic, IOAPIC_REG_VERSION);
    ioapic->version = (value & IOAPIC_VERSION_VERSION_MASK)
                      >> IOAPIC_VERSION_VERSION_SHIFT;
    nr_gsis = ((value & IOAPIC_VERSION_MAXREDIR_MASK)
               >> IOAPIC_VERSION_MAXREDIR_SHIFT) + 1;
    ioapic->last_gsi = ioapic->first_gsi + nr_gsis - 1;

    /* XXX This assumes that interrupts are mapped 1:1 to traps */
    if (ioapic->last_gsi > (TRAP_INTR_LAST - TRAP_INTR_FIRST)) {
        panic("ioapic: invalid interrupt range");
    }

    for (i = ioapic->first_gsi; i < ioapic->last_gsi; i++) {
        trap_register(TRAP_INTR_FIRST + i, ioapic_intr);
    }

    log_info("ioapic%u: version:%#x gsis:%u-%u", ioapic->id,
             ioapic->version, ioapic->first_gsi, ioapic->last_gsi);

    ioapic_nr_devs++;
    return ioapic;
}

static bool
ioapic_has_gsi(const struct ioapic *ioapic, unsigned int gsi)
{
    return ((gsi >= ioapic->first_gsi) && (gsi <= ioapic->last_gsi));
}

static unsigned int
ioapic_compute_id(const struct ioapic *ioapic, unsigned int gsi)
{
    assert(ioapic_has_gsi(ioapic, gsi));
    return gsi - ioapic->first_gsi;
}

static void
ioapic_compute_entry(uint32_t *highp, uint32_t *lowp,
                     unsigned int apic_id, unsigned int intr,
                     bool active_high, bool edge_triggered)
{
    assert(apic_id < 16);
    assert(intr < (TRAP_NR_VECTORS - TRAP_INTR_FIRST));

    *highp = apic_id << 24;
    *lowp = (!edge_triggered ? IOAPIC_ENTLOW_LEVEL : 0)
            | (!active_high ? IOAPIC_ENTLOW_ACTIVE_LOW : 0)
            | IOAPIC_ENTLOW_PHYS_DELIVERY
            | IOAPIC_ENTLOW_FIXED_DEST
            | (TRAP_INTR_FIRST + intr);
}

static void
ioapic_enable(void *priv, unsigned int intr, unsigned int cpu)
{
    bool active_high, edge_triggered;
    const struct ioapic_iso *iso;
    uint32_t high, low, gsi;
    struct ioapic *ioapic;
    unsigned long flags;
    unsigned int id;

    iso = ioapic_lookup_iso(intr);

    /* XXX These are defaults that should work with architectural devices */
    if (iso == NULL) {
        active_high = true;
        edge_triggered = true;
        gsi = intr;
    } else {
        active_high = iso->active_high;
        edge_triggered = iso->edge_triggered;
        gsi = iso->gsi;
    }

    ioapic = priv;
    id = ioapic_compute_id(ioapic, gsi);
    ioapic_compute_entry(&high, &low, cpu_apic_id(cpu), intr,
                         active_high, edge_triggered);

    spinlock_lock_intr_save(&ioapic->lock, &flags);
    ioapic_write_entry_high(ioapic, id, high);
    ioapic_write_entry_low(ioapic, id, low);
    spinlock_unlock_intr_restore(&ioapic->lock, flags);
}

static void
ioapic_disable(void *priv, unsigned int intr)
{
    struct ioapic *ioapic;
    unsigned long flags;
    unsigned int id;

    ioapic = priv;
    id = ioapic_compute_id(ioapic, intr);

    spinlock_lock_intr_save(&ioapic->lock, &flags);
    ioapic_write_entry_low(ioapic, id, IOAPIC_ENTLOW_INTRMASK);
    spinlock_unlock_intr_restore(&ioapic->lock, flags);
}

static void
ioapic_eoi(void *priv, unsigned int intr)
{
    (void)priv;
    (void)intr;

    lapic_eoi();
}

static const struct intr_ops ioapic_ops = {
    .enable = ioapic_enable,
    .disable = ioapic_disable,
    .eoi = ioapic_eoi,
};

void __init
ioapic_setup(void)
{
    ioapic_nr_devs = 0;
    ioapic_nr_isos = 0;
}

void __init
ioapic_register(unsigned int apic_id, uintptr_t addr, unsigned int gsi_base)
{
    struct ioapic *ioapic;

    ioapic = ioapic_create(apic_id, addr, gsi_base);

    /*
     * XXX This assumes that any interrupt override source is included
     * in the GSI range.
     */
    intr_register_ctl(&ioapic_ops, ioapic, ioapic->first_gsi, ioapic->last_gsi);
}

void __init
ioapic_override(uint8_t source, uint32_t gsi,
                bool active_high, bool edge_triggered)
{
    struct ioapic_iso *iso;

    iso = ioapic_alloc_iso();

    if (iso == NULL) {
        return;
    }

    ioapic_iso_init(iso, source, gsi, active_high, edge_triggered);
}