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