| 1 | /*
|
|---|
| 2 | * Copyright (c) 2006 Jakub Jermar
|
|---|
| 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 genericddi
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #ifndef KERN_IRQ_H_
|
|---|
| 36 | #define KERN_IRQ_H_
|
|---|
| 37 |
|
|---|
| 38 | #ifdef KERNEL
|
|---|
| 39 |
|
|---|
| 40 | #include <typedefs.h>
|
|---|
| 41 | #include <adt/list.h>
|
|---|
| 42 | #include <adt/hash_table.h>
|
|---|
| 43 | #include <synch/spinlock.h>
|
|---|
| 44 | #include <proc/task.h>
|
|---|
| 45 | #include <ipc/ipc.h>
|
|---|
| 46 |
|
|---|
| 47 | #endif /* KERNEL */
|
|---|
| 48 |
|
|---|
| 49 | typedef enum {
|
|---|
| 50 | /** Read 1 byte from the I/O space. */
|
|---|
| 51 | CMD_PIO_READ_8 = 1,
|
|---|
| 52 | /** Read 2 bytes from the I/O space. */
|
|---|
| 53 | CMD_PIO_READ_16,
|
|---|
| 54 | /** Read 4 bytes from the I/O space. */
|
|---|
| 55 | CMD_PIO_READ_32,
|
|---|
| 56 | /** Write 1 byte to the I/O space. */
|
|---|
| 57 | CMD_PIO_WRITE_8,
|
|---|
| 58 | /** Write 2 bytes to the I/O space. */
|
|---|
| 59 | CMD_PIO_WRITE_16,
|
|---|
| 60 | /** Write 4 bytes to the I/O space. */
|
|---|
| 61 | CMD_PIO_WRITE_32,
|
|---|
| 62 |
|
|---|
| 63 | /**
|
|---|
| 64 | * Perform a bit test on the source argument and store the result into
|
|---|
| 65 | * the destination argument.
|
|---|
| 66 | */
|
|---|
| 67 | CMD_BTEST,
|
|---|
| 68 |
|
|---|
| 69 | /**
|
|---|
| 70 | * Predicate the execution of the following N commands by the boolean
|
|---|
| 71 | * value of the source argument.
|
|---|
| 72 | */
|
|---|
| 73 | CMD_PREDICATE,
|
|---|
| 74 |
|
|---|
| 75 | /** Accept the interrupt. */
|
|---|
| 76 | CMD_ACCEPT,
|
|---|
| 77 | /** Decline the interrupt. */
|
|---|
| 78 | CMD_DECLINE,
|
|---|
| 79 | CMD_LAST
|
|---|
| 80 | } irq_cmd_type;
|
|---|
| 81 |
|
|---|
| 82 | typedef struct {
|
|---|
| 83 | irq_cmd_type cmd;
|
|---|
| 84 | void *addr;
|
|---|
| 85 | uint32_t value;
|
|---|
| 86 | uintptr_t srcarg;
|
|---|
| 87 | uintptr_t dstarg;
|
|---|
| 88 | } irq_cmd_t;
|
|---|
| 89 |
|
|---|
| 90 | typedef struct {
|
|---|
| 91 | size_t cmdcount;
|
|---|
| 92 | irq_cmd_t *cmds;
|
|---|
| 93 | } irq_code_t;
|
|---|
| 94 |
|
|---|
| 95 | #ifdef KERNEL
|
|---|
| 96 |
|
|---|
| 97 | typedef enum {
|
|---|
| 98 | IRQ_DECLINE, /**< Decline to service. */
|
|---|
| 99 | IRQ_ACCEPT /**< Accept to service. */
|
|---|
| 100 | } irq_ownership_t;
|
|---|
| 101 |
|
|---|
| 102 | typedef enum {
|
|---|
| 103 | IRQ_TRIGGER_LEVEL = 1,
|
|---|
| 104 | IRQ_TRIGGER_EDGE
|
|---|
| 105 | } irq_trigger_t;
|
|---|
| 106 |
|
|---|
| 107 | struct irq;
|
|---|
| 108 | typedef void (* irq_handler_t)(struct irq *);
|
|---|
| 109 |
|
|---|
| 110 | /** Type for function used to clear the interrupt. */
|
|---|
| 111 | typedef void (* cir_t)(void *, inr_t);
|
|---|
| 112 |
|
|---|
| 113 | /** IPC notification config structure.
|
|---|
| 114 | *
|
|---|
| 115 | * Primarily, this structure is encapsulated in the irq_t structure.
|
|---|
| 116 | * It is protected by irq_t::lock.
|
|---|
| 117 | *
|
|---|
| 118 | */
|
|---|
| 119 | typedef struct {
|
|---|
| 120 | /** When false, notifications are not sent. */
|
|---|
| 121 | bool notify;
|
|---|
| 122 | /** Answerbox for notifications. */
|
|---|
| 123 | answerbox_t *answerbox;
|
|---|
| 124 | /** Method to be used for the notification. */
|
|---|
| 125 | unative_t method;
|
|---|
| 126 | /** Arguments that will be sent if the IRQ is claimed. */
|
|---|
| 127 | uint32_t scratch[IPC_CALL_LEN];
|
|---|
| 128 | /** Top-half pseudocode. */
|
|---|
| 129 | irq_code_t *code;
|
|---|
| 130 | /** Counter. */
|
|---|
| 131 | size_t counter;
|
|---|
| 132 |
|
|---|
| 133 | /**
|
|---|
| 134 | * Link between IRQs that are notifying the same answerbox. The list is
|
|---|
| 135 | * protected by the answerbox irq_lock.
|
|---|
| 136 | */
|
|---|
| 137 | link_t link;
|
|---|
| 138 | } ipc_notif_cfg_t;
|
|---|
| 139 |
|
|---|
| 140 | /** Structure representing one device IRQ.
|
|---|
| 141 | *
|
|---|
| 142 | * If one device has multiple interrupts, there will be multiple irq_t
|
|---|
| 143 | * instantions with the same devno.
|
|---|
| 144 | *
|
|---|
| 145 | */
|
|---|
| 146 | typedef struct irq {
|
|---|
| 147 | /** Hash table link. */
|
|---|
| 148 | link_t link;
|
|---|
| 149 |
|
|---|
| 150 | /** Lock protecting everything in this structure
|
|---|
| 151 | * except the link member. When both the IRQ
|
|---|
| 152 | * hash table lock and this lock are to be acquired,
|
|---|
| 153 | * this lock must not be taken first.
|
|---|
| 154 | */
|
|---|
| 155 | IRQ_SPINLOCK_DECLARE(lock);
|
|---|
| 156 |
|
|---|
| 157 | /** Send EOI before processing the interrupt.
|
|---|
| 158 | * This is essential for timer interrupt which
|
|---|
| 159 | * has to be acknowledged before doing preemption
|
|---|
| 160 | * to make sure another timer interrupt will
|
|---|
| 161 | * be eventually generated.
|
|---|
| 162 | */
|
|---|
| 163 | bool preack;
|
|---|
| 164 |
|
|---|
| 165 | /** Unique device number. -1 if not yet assigned. */
|
|---|
| 166 | devno_t devno;
|
|---|
| 167 |
|
|---|
| 168 | /** Actual IRQ number. -1 if not yet assigned. */
|
|---|
| 169 | inr_t inr;
|
|---|
| 170 | /** Trigger level of the IRQ. */
|
|---|
| 171 | irq_trigger_t trigger;
|
|---|
| 172 | /** Claim ownership of the IRQ. */
|
|---|
| 173 | irq_ownership_t (* claim)(struct irq *);
|
|---|
| 174 | /** Handler for this IRQ and device. */
|
|---|
| 175 | irq_handler_t handler;
|
|---|
| 176 | /** Instance argument for the handler and the claim function. */
|
|---|
| 177 | void *instance;
|
|---|
| 178 |
|
|---|
| 179 | /** Clear interrupt routine. */
|
|---|
| 180 | cir_t cir;
|
|---|
| 181 | /** First argument to the clear interrupt routine. */
|
|---|
| 182 | void *cir_arg;
|
|---|
| 183 |
|
|---|
| 184 | /** Notification configuration structure. */
|
|---|
| 185 | ipc_notif_cfg_t notif_cfg;
|
|---|
| 186 | } irq_t;
|
|---|
| 187 |
|
|---|
| 188 | IRQ_SPINLOCK_EXTERN(irq_uspace_hash_table_lock);
|
|---|
| 189 | extern hash_table_t irq_uspace_hash_table;
|
|---|
| 190 |
|
|---|
| 191 | extern void irq_init(size_t, size_t);
|
|---|
| 192 | extern void irq_initialize(irq_t *);
|
|---|
| 193 | extern void irq_register(irq_t *);
|
|---|
| 194 | extern irq_t *irq_dispatch_and_lock(inr_t);
|
|---|
| 195 |
|
|---|
| 196 | #endif /* KERNEL */
|
|---|
| 197 |
|
|---|
| 198 | #endif
|
|---|
| 199 |
|
|---|
| 200 | /** @}
|
|---|
| 201 | */
|
|---|