summaryrefslogtreecommitdiff
path: root/src/i8259.c
blob: aed70680d9869f15a89cd637fa85ffdbca066d30 (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
/*
 * Copyright (c) 2017 Richard Braun.
 * Copyright (c) 2017 Jerko Lenstra.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 *
 * IRQ means Interrupt ReQuest. They're used by external hardware to signal
 * the CPU, and in turn the OS, that an external event has happened and
 * requires processing. The usual model is shown in the image at
 * https://upload.wikimedia.org/wikipedia/commons/thumb/1/15/PIC_Hardware_interrupt_path.svg/300px-PIC_Hardware_interrupt_path.svg.png.
 *
 * This driver implements IRQ handling on the Intel 8259 PIC. The IBM PC/AT
 * actually uses 2 of these PICs for external interrupt handling, as shown
 * in https://masherz.files.wordpress.com/2010/08/217.jpg. The public
 * interface completely hides this detail and considers all given IRQs
 * as logical indexes, used to find the corresponding PIC (master or slave)
 * and the local IRQ on that PIC.
 *
 * 8259 datasheet :
 *   https://pdos.csail.mit.edu/6.828/2010/readings/hardware/8259A.pdf
 */

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

#include <lib/macros.h>

#include "cpu.h"
#include "i8259.h"
#include "io.h"

#define I8259_IRQ_CASCADE   2        /* IRQ used for cascading on the master */
#define I8259_NR_IRQS       8

/*
 * Initialization Control Word 1 bits.
 */
#define I8259_ICW1_ICW4     0x01    /* State that a 4th ICW will be sent */
#define I8259_ICW1_INIT     0x10    /* This bit must be set */

/*
 * Initialization Control Word 4 bits.
 */
#define I8259_ICW4_8086     0x01    /* 8086 mode, as x86 is still compatible
                                       with the old 8086 processor */

#define I8259_OCW2_EOI      0x20    /* End of interrupt control word */

enum {
    I8259_PIC_ID_MASTER,
    I8259_PIC_ID_SLAVE,
    I8259_NR_PICS
};

/*
 * Intel 8259 programmable interrupt controller.
 */
struct i8259_pic {
    uint16_t cmd_port;      /* Command I/O port of the PIC */
    uint16_t data_port;     /* Data I/O port of the PIC */
    uint8_t imr;            /* Cached value of the IMR register */
    bool master;            /* True if this PIC is the master */
};

/*
 * Static instances of PIC objects.
 */
static struct i8259_pic i8259_pics[] = {
    [I8259_PIC_ID_MASTER] = {
        .cmd_port = 0x20,
        .data_port = 0x21,
        .imr = 0xff,
        .master = true,
    },
    [I8259_PIC_ID_SLAVE] = {
        .cmd_port = 0xa0,
        .data_port = 0xa1,
        .imr = 0xff,
        .master = false,
    },
};

static struct i8259_pic *
i8259_get_pic(unsigned int id)
{
    assert(id < ARRAY_SIZE(i8259_pics));
    return &i8259_pics[id];
}

static int
i8259_convert_global_irq(unsigned int irq, struct i8259_pic **pic,
                         unsigned int *local_irq)
{
    int error;

    if (irq < I8259_NR_IRQS) {
        *pic = i8259_get_pic(I8259_PIC_ID_MASTER);

        if (local_irq) {
            *local_irq = irq;
        }

        error = 0;
    } else if (irq < (I8259_NR_IRQS * I8259_NR_PICS)) {
        *pic = i8259_get_pic(I8259_PIC_ID_SLAVE);

        if (local_irq) {
            *local_irq = irq - I8259_NR_IRQS;
        }

        error = 0;
    } else {
        *local_irq = 0;
        error = EINVAL;
    }

    return error;
}

static void
i8259_pic_write_cmd(const struct i8259_pic *pic, uint8_t byte)
{
    io_write(pic->cmd_port, byte);
}

static void
i8259_pic_write_data(const struct i8259_pic *pic, uint8_t byte)
{
    io_write(pic->data_port, byte);
}

static void
i8259_pic_apply_imr(const struct i8259_pic *pic)
{
    io_write(pic->data_port, pic->imr);
}

static void
i8259_pic_enable_irq(struct i8259_pic *pic, unsigned int irq)
{
    assert(irq < I8259_NR_IRQS);

    pic->imr &= ~(1 << irq);
    i8259_pic_apply_imr(pic);
}

static void
i8259_pic_disable_irq(struct i8259_pic *pic, unsigned int irq)
{
    assert(irq < I8259_NR_IRQS);

    pic->imr |= (1 << irq);
    i8259_pic_apply_imr(pic);
}

static void
i8259_pic_eoi(struct i8259_pic *pic)
{
    io_write(pic->cmd_port, I8259_OCW2_EOI);
}

void
i8259_setup(void)
{
    struct i8259_pic *master, *slave;

    master = i8259_get_pic(I8259_PIC_ID_MASTER);
    slave = i8259_get_pic(I8259_PIC_ID_SLAVE);

    i8259_pic_write_cmd(master, I8259_ICW1_INIT | I8259_ICW1_ICW4);
    i8259_pic_write_cmd(slave, I8259_ICW1_INIT | I8259_ICW1_ICW4);
    i8259_pic_write_data(master, CPU_IDT_VECT_IRQ_BASE);
    i8259_pic_write_data(slave, CPU_IDT_VECT_IRQ_BASE + I8259_NR_IRQS);
    i8259_pic_write_data(master, 1 << I8259_IRQ_CASCADE);
    i8259_pic_write_data(slave, I8259_IRQ_CASCADE);
    i8259_pic_write_data(master, I8259_ICW4_8086);
    i8259_pic_write_data(slave, I8259_ICW4_8086);

    i8259_pic_enable_irq(master, I8259_IRQ_CASCADE);
    i8259_pic_apply_imr(master);
    i8259_pic_apply_imr(slave);
}

void
i8259_irq_enable(unsigned int irq)
{
    struct i8259_pic *pic;
    unsigned int local_irq;
    int error;

    error = i8259_convert_global_irq(irq, &pic, &local_irq);
    assert(!error);
    i8259_pic_enable_irq(pic, local_irq);
}

void
i8259_irq_disable(unsigned int irq)
{
    struct i8259_pic *pic;
    unsigned int local_irq;
    int error;

    error = i8259_convert_global_irq(irq, &pic, &local_irq);
    assert(!error);
    i8259_pic_disable_irq(pic, local_irq);
}

void
i8259_irq_eoi(unsigned int irq)
{
    struct i8259_pic *pic;
    int error;

    assert(!cpu_intr_enabled());

    error = i8259_convert_global_irq(irq, &pic, NULL);
    assert(!error);

    if (!pic->master) {
        /*
         * The order in which EOI messages are sent (master then slave or the
         * reverse) is irrelevant :
         *  - If the slave is sent the EOI message first, it may raise another
         *    interrupt right away, in which case it will be pending at the
         *    master until the latter is sent the EOI message too.
         *  - If the master is sent the EOI message first, it may raise another
         *    interrupt right away, in which case it will be pending at the
         *    processor until interrupts are reenabled, assuming that this
         *    function is called with interrupts disabled, and that interrupts
         *    remain disabled until control is returned to the interrupted
         *    thread.
         */
        i8259_pic_eoi(i8259_get_pic(I8259_PIC_ID_MASTER));
    }

    i8259_pic_eoi(pic);
}