source: mainline/uspace/drv/intctl/apic/apic.c@ 001778c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 001778c was b446b02, checked in by Jiri Svoboda <jiri@…>, 8 years ago

Enumerate APIC and i8259 via DDF.

  • Property mode set to 100644
File size: 6.4 KB
Line 
1/*
2 * Copyright (c) 2011 Martin Decky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup apic
30 * @{
31 */
32
33/**
34 * @file apic.c
35 * @brief APIC driver.
36 */
37
38#include <ipc/irc.h>
39#include <loc.h>
40#include <sysinfo.h>
41#include <as.h>
42#include <ddf/driver.h>
43#include <ddf/log.h>
44#include <ddi.h>
45#include <stdbool.h>
46#include <errno.h>
47#include <async.h>
48#include <stdio.h>
49
50#include "apic.h"
51
52#define NAME "apic"
53
54#define APIC_MAX_IRQ 15
55
56#define IOREGSEL (0x00U / sizeof(uint32_t))
57#define IOWIN (0x10U / sizeof(uint32_t))
58
59#define IOREDTBL 0x10U
60
61/** I/O Register Select Register. */
62typedef union {
63 uint32_t value;
64 struct {
65 uint8_t reg_addr; /**< APIC Register Address. */
66 unsigned int : 24; /**< Reserved. */
67 } __attribute__ ((packed));
68} io_regsel_t;
69
70/** I/O Redirection Register. */
71typedef struct io_redirection_reg {
72 union {
73 uint32_t lo;
74 struct {
75 uint8_t intvec; /**< Interrupt Vector. */
76 unsigned int delmod : 3; /**< Delivery Mode. */
77 unsigned int destmod : 1; /**< Destination mode. */
78 unsigned int delivs : 1; /**< Delivery status (RO). */
79 unsigned int intpol : 1; /**< Interrupt Input Pin Polarity. */
80 unsigned int irr : 1; /**< Remote IRR (RO). */
81 unsigned int trigger_mode : 1; /**< Trigger Mode. */
82 unsigned int masked : 1; /**< Interrupt Mask. */
83 unsigned int : 15; /**< Reserved. */
84 } __attribute__ ((packed));
85 };
86 union {
87 uint32_t hi;
88 struct {
89 unsigned int : 24; /**< Reserved. */
90 uint8_t dest : 8; /**< Destination Field. */
91 } __attribute__ ((packed));
92 };
93} __attribute__ ((packed)) io_redirection_reg_t;
94
95#define IO_APIC_SIZE 20
96
97/** Read from IO APIC register.
98 *
99 * @param apic APIC
100 * @param address IO APIC register address.
101 *
102 * @return Content of the addressed IO APIC register.
103 *
104 */
105static uint32_t io_apic_read(apic_t *apic, uint8_t address)
106{
107 io_regsel_t regsel;
108
109 regsel.value = pio_read_32(&apic->regs[IOREGSEL]);
110 regsel.reg_addr = address;
111 pio_write_32(&apic->regs[IOREGSEL], regsel.value);
112 return pio_read_32(&apic->regs[IOWIN]);
113}
114
115/** Write to IO APIC register.
116 *
117 * @param apic APIC
118 * @param address IO APIC register address.
119 * @param val Content to be written to the addressed IO APIC register.
120 *
121 */
122static void io_apic_write(apic_t *apic, uint8_t address, uint32_t val)
123{
124 io_regsel_t regsel;
125
126 regsel.value = pio_read_32(&apic->regs[IOREGSEL]);
127 regsel.reg_addr = address;
128 pio_write_32(&apic->regs[IOREGSEL], regsel.value);
129 pio_write_32(&apic->regs[IOWIN], val);
130}
131
132static int irq_to_pin(int irq)
133{
134 // FIXME: get the map from the kernel, even though this may work
135 // for simple cases
136 if (irq == 0)
137 return 2;
138 return irq;
139}
140
141static int apic_enable_irq(apic_t *apic, sysarg_t irq)
142{
143 io_redirection_reg_t reg;
144
145 if (irq > APIC_MAX_IRQ)
146 return ELIMIT;
147
148 int pin = irq_to_pin(irq);
149 if (pin == -1)
150 return ENOENT;
151
152 reg.lo = io_apic_read(apic, (uint8_t) (IOREDTBL + pin * 2));
153 reg.masked = false;
154 io_apic_write(apic, (uint8_t) (IOREDTBL + pin * 2), reg.lo);
155
156 return EOK;
157}
158
159/** Handle one connection to APIC.
160 *
161 * @param iid Hash of the request that opened the connection.
162 * @param icall Call data of the request that opened the connection.
163 * @param arg Local argument.
164 */
165static void apic_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
166{
167 ipc_callid_t callid;
168 ipc_call_t call;
169 apic_t *apic;
170
171 /*
172 * Answer the first IPC_M_CONNECT_ME_TO call.
173 */
174 async_answer_0(iid, EOK);
175
176 apic = (apic_t *)ddf_dev_data_get(ddf_fun_get_dev((ddf_fun_t *)arg));
177
178 while (true) {
179 callid = async_get_call(&call);
180
181 if (!IPC_GET_IMETHOD(call)) {
182 /* The other side has hung up. */
183 async_answer_0(callid, EOK);
184 return;
185 }
186
187 switch (IPC_GET_IMETHOD(call)) {
188 case IRC_ENABLE_INTERRUPT:
189 async_answer_0(callid, apic_enable_irq(apic,
190 IPC_GET_ARG1(call)));
191 break;
192 case IRC_DISABLE_INTERRUPT:
193 /* XXX TODO */
194 async_answer_0(callid, EOK);
195 break;
196 case IRC_CLEAR_INTERRUPT:
197 /* Noop */
198 async_answer_0(callid, EOK);
199 break;
200 default:
201 async_answer_0(callid, EINVAL);
202 break;
203 }
204 }
205}
206
207/** Add APIC device. */
208int apic_add(apic_t *apic, apic_res_t *res)
209{
210 sysarg_t have_apic;
211 ddf_fun_t *fun_a = NULL;
212 void *regs;
213 int rc;
214
215 if ((sysinfo_get_value("apic", &have_apic) != EOK) || (!have_apic)) {
216 printf("%s: No APIC found\n", NAME);
217 return ENOTSUP;
218 }
219
220 rc = pio_enable((void *) res->base, IO_APIC_SIZE, &regs);
221 if (rc != EOK) {
222 printf("%s: Failed to enable PIO for APIC: %d\n", NAME, rc);
223 return EIO;
224 }
225
226 apic->regs = (ioport32_t *)regs;
227
228 fun_a = ddf_fun_create(apic->dev, fun_exposed, "a");
229 if (fun_a == NULL) {
230 ddf_msg(LVL_ERROR, "Failed creating function 'a'.");
231 rc = ENOMEM;
232 goto error;
233 }
234
235 ddf_fun_set_conn_handler(fun_a, apic_connection);
236
237 rc = ddf_fun_bind(fun_a);
238 if (rc != EOK) {
239 ddf_msg(LVL_ERROR, "Failed binding function 'a'. (%d)", rc);
240 goto error;
241 }
242
243 rc = ddf_fun_add_to_category(fun_a, "irc");
244 if (rc != EOK)
245 goto error;
246
247 return EOK;
248error:
249 if (fun_a != NULL)
250 ddf_fun_destroy(fun_a);
251 return rc;
252}
253
254/** Remove APIC device */
255int apic_remove(apic_t *apic)
256{
257 return ENOTSUP;
258}
259
260/** APIC device gone */
261int apic_gone(apic_t *apic)
262{
263 return ENOTSUP;
264}
265
266/**
267 * @}
268 */
Note: See TracBrowser for help on using the repository browser.