[3e5a814] | 1 | /*
|
---|
[acc7ce4] | 2 | * Copyright (c) 2011 Martin Decky
|
---|
[3e5a814] | 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 |
|
---|
[acc7ce4] | 29 | /** @addtogroup apic
|
---|
[3e5a814] | 30 | * @{
|
---|
[acc7ce4] | 31 | */
|
---|
[3e5a814] | 32 |
|
---|
| 33 | /**
|
---|
[acc7ce4] | 34 | * @file apic.c
|
---|
| 35 | * @brief APIC driver.
|
---|
[3e5a814] | 36 | */
|
---|
| 37 |
|
---|
| 38 | #include <ipc/services.h>
|
---|
[acc7ce4] | 39 | #include <ipc/irc.h>
|
---|
[79ae36dd] | 40 | #include <ns.h>
|
---|
[3e5a814] | 41 | #include <sysinfo.h>
|
---|
| 42 | #include <as.h>
|
---|
| 43 | #include <ddi.h>
|
---|
| 44 | #include <bool.h>
|
---|
| 45 | #include <errno.h>
|
---|
| 46 | #include <async.h>
|
---|
[df02460] | 47 | #include <stdio.h>
|
---|
[3e5a814] | 48 |
|
---|
[acc7ce4] | 49 | #define NAME "apic"
|
---|
[3e5a814] | 50 |
|
---|
[f5d51de] | 51 | #define APIC_MAX_IRQ 15
|
---|
| 52 |
|
---|
| 53 | #define IOREGSEL (0x00U / sizeof(uint32_t))
|
---|
| 54 | #define IOWIN (0x10U / sizeof(uint32_t))
|
---|
| 55 |
|
---|
| 56 | #define IOREDTBL 0x10U
|
---|
| 57 |
|
---|
| 58 | /** I/O Register Select Register. */
|
---|
| 59 | typedef union {
|
---|
| 60 | uint32_t value;
|
---|
| 61 | struct {
|
---|
| 62 | uint8_t reg_addr; /**< APIC Register Address. */
|
---|
| 63 | unsigned int : 24; /**< Reserved. */
|
---|
| 64 | } __attribute__ ((packed));
|
---|
| 65 | } io_regsel_t;
|
---|
| 66 |
|
---|
| 67 | /** I/O Redirection Register. */
|
---|
| 68 | typedef struct io_redirection_reg {
|
---|
| 69 | union {
|
---|
| 70 | uint32_t lo;
|
---|
| 71 | struct {
|
---|
| 72 | uint8_t intvec; /**< Interrupt Vector. */
|
---|
| 73 | unsigned int delmod : 3; /**< Delivery Mode. */
|
---|
| 74 | unsigned int destmod : 1; /**< Destination mode. */
|
---|
| 75 | unsigned int delivs : 1; /**< Delivery status (RO). */
|
---|
| 76 | unsigned int intpol : 1; /**< Interrupt Input Pin Polarity. */
|
---|
| 77 | unsigned int irr : 1; /**< Remote IRR (RO). */
|
---|
| 78 | unsigned int trigger_mode : 1; /**< Trigger Mode. */
|
---|
| 79 | unsigned int masked : 1; /**< Interrupt Mask. */
|
---|
| 80 | unsigned int : 15; /**< Reserved. */
|
---|
| 81 | } __attribute__ ((packed));
|
---|
| 82 | };
|
---|
| 83 | union {
|
---|
| 84 | uint32_t hi;
|
---|
| 85 | struct {
|
---|
| 86 | unsigned int : 24; /**< Reserved. */
|
---|
| 87 | uint8_t dest : 8; /**< Destination Field. */
|
---|
| 88 | } __attribute__ ((packed));
|
---|
| 89 | };
|
---|
| 90 | } __attribute__ ((packed)) io_redirection_reg_t;
|
---|
| 91 |
|
---|
| 92 | // FIXME: get the address from the kernel
|
---|
| 93 | #define IO_APIC_BASE 0xfec00000UL
|
---|
| 94 | #define IO_APIC_SIZE 20
|
---|
| 95 |
|
---|
| 96 | ioport32_t *io_apic = NULL;
|
---|
| 97 |
|
---|
| 98 | /** Read from IO APIC register.
|
---|
| 99 | *
|
---|
| 100 | * @param address IO APIC register address.
|
---|
| 101 | *
|
---|
| 102 | * @return Content of the addressed IO APIC register.
|
---|
| 103 | *
|
---|
| 104 | */
|
---|
| 105 | static uint32_t io_apic_read(uint8_t address)
|
---|
| 106 | {
|
---|
| 107 | io_regsel_t regsel;
|
---|
| 108 |
|
---|
| 109 | regsel.value = io_apic[IOREGSEL];
|
---|
| 110 | regsel.reg_addr = address;
|
---|
| 111 | io_apic[IOREGSEL] = regsel.value;
|
---|
| 112 | return io_apic[IOWIN];
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | /** Write to IO APIC register.
|
---|
| 116 | *
|
---|
| 117 | * @param address IO APIC register address.
|
---|
| 118 | * @param val Content to be written to the addressed IO APIC register.
|
---|
| 119 | *
|
---|
| 120 | */
|
---|
| 121 | static void io_apic_write(uint8_t address, uint32_t val)
|
---|
| 122 | {
|
---|
| 123 | io_regsel_t regsel;
|
---|
| 124 |
|
---|
| 125 | regsel.value = io_apic[IOREGSEL];
|
---|
| 126 | regsel.reg_addr = address;
|
---|
| 127 | io_apic[IOREGSEL] = regsel.value;
|
---|
| 128 | io_apic[IOWIN] = val;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | static int irq_to_pin(int irq)
|
---|
| 132 | {
|
---|
| 133 | // FIXME: get the map from the kernel, even though this may work
|
---|
| 134 | // for simple cases
|
---|
[b50bf6c2] | 135 | if (irq == 0)
|
---|
| 136 | return 2;
|
---|
[f5d51de] | 137 | return irq;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[acc7ce4] | 140 | static int apic_enable_irq(sysarg_t irq)
|
---|
| 141 | {
|
---|
[f5d51de] | 142 | io_redirection_reg_t reg;
|
---|
| 143 |
|
---|
| 144 | if (irq > APIC_MAX_IRQ)
|
---|
| 145 | return ELIMIT;
|
---|
| 146 |
|
---|
| 147 | int pin = irq_to_pin(irq);
|
---|
| 148 | if (pin == -1)
|
---|
| 149 | return ENOENT;
|
---|
| 150 |
|
---|
| 151 | reg.lo = io_apic_read((uint8_t) (IOREDTBL + pin * 2));
|
---|
| 152 | reg.masked = false;
|
---|
| 153 | io_apic_write((uint8_t) (IOREDTBL + pin * 2), reg.lo);
|
---|
| 154 |
|
---|
| 155 | return EOK;
|
---|
[acc7ce4] | 156 | }
|
---|
[3e5a814] | 157 |
|
---|
[acc7ce4] | 158 | /** Handle one connection to APIC.
|
---|
| 159 | *
|
---|
| 160 | * @param iid Hash of the request that opened the connection.
|
---|
| 161 | * @param icall Call data of the request that opened the connection.
|
---|
[9934f7d] | 162 | * @param arg Local argument.
|
---|
[3e5a814] | 163 | */
|
---|
[9934f7d] | 164 | static void apic_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg)
|
---|
[3e5a814] | 165 | {
|
---|
| 166 | ipc_callid_t callid;
|
---|
| 167 | ipc_call_t call;
|
---|
[acc7ce4] | 168 |
|
---|
[3e5a814] | 169 | /*
|
---|
| 170 | * Answer the first IPC_M_CONNECT_ME_TO call.
|
---|
| 171 | */
|
---|
[ffa2c8ef] | 172 | async_answer_0(iid, EOK);
|
---|
[3e5a814] | 173 |
|
---|
[acc7ce4] | 174 | while (true) {
|
---|
[3e5a814] | 175 | callid = async_get_call(&call);
|
---|
[acc7ce4] | 176 |
|
---|
[79ae36dd] | 177 | if (!IPC_GET_IMETHOD(call)) {
|
---|
| 178 | /* The other side has hung up. */
|
---|
| 179 | async_answer_0(callid, EOK);
|
---|
| 180 | return;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[228e490] | 183 | switch (IPC_GET_IMETHOD(call)) {
|
---|
[acc7ce4] | 184 | case IRC_ENABLE_INTERRUPT:
|
---|
[ffa2c8ef] | 185 | async_answer_0(callid, apic_enable_irq(IPC_GET_ARG1(call)));
|
---|
[acc7ce4] | 186 | break;
|
---|
| 187 | case IRC_CLEAR_INTERRUPT:
|
---|
| 188 | /* Noop */
|
---|
[ffa2c8ef] | 189 | async_answer_0(callid, EOK);
|
---|
[3e5a814] | 190 | break;
|
---|
| 191 | default:
|
---|
[ffa2c8ef] | 192 | async_answer_0(callid, EINVAL);
|
---|
[3e5a814] | 193 | break;
|
---|
| 194 | }
|
---|
| 195 | }
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[acc7ce4] | 198 | /** Initialize the APIC driver.
|
---|
[3e5a814] | 199 | *
|
---|
| 200 | */
|
---|
[acc7ce4] | 201 | static bool apic_init(void)
|
---|
[3e5a814] | 202 | {
|
---|
[acc7ce4] | 203 | sysarg_t apic;
|
---|
[3e5a814] | 204 |
|
---|
[acc7ce4] | 205 | if ((sysinfo_get_value("apic", &apic) != EOK) || (!apic)) {
|
---|
[79ae36dd] | 206 | printf("%s: No APIC found\n", NAME);
|
---|
[3e5a814] | 207 | return false;
|
---|
| 208 | }
|
---|
[f5d51de] | 209 |
|
---|
[c94f643] | 210 | int rc = pio_enable((void *) IO_APIC_BASE, IO_APIC_SIZE,
|
---|
| 211 | (void **) &io_apic);
|
---|
| 212 | if (rc != EOK) {
|
---|
| 213 | printf("%s: Failed to enable PIO for APIC: %d\n", NAME, rc);
|
---|
[f5d51de] | 214 | return false;
|
---|
[c94f643] | 215 | }
|
---|
[3e5a814] | 216 |
|
---|
[acc7ce4] | 217 | async_set_client_connection(apic_connection);
|
---|
[57d129e] | 218 | service_register(SERVICE_IRC);
|
---|
[3e5a814] | 219 |
|
---|
| 220 | return true;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | int main(int argc, char **argv)
|
---|
| 224 | {
|
---|
[79ae36dd] | 225 | printf("%s: HelenOS APIC driver\n", NAME);
|
---|
[3e5a814] | 226 |
|
---|
[acc7ce4] | 227 | if (!apic_init())
|
---|
[3e5a814] | 228 | return -1;
|
---|
| 229 |
|
---|
[79ae36dd] | 230 | printf("%s: Accepting connections\n", NAME);
|
---|
| 231 | task_retval(0);
|
---|
[3e5a814] | 232 | async_manager();
|
---|
[acc7ce4] | 233 |
|
---|
[3e5a814] | 234 | /* Never reached */
|
---|
| 235 | return 0;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | /**
|
---|
| 239 | * @}
|
---|
[acc7ce4] | 240 | */
|
---|