Rev 3902 | Rev 3947 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3902 | Rev 3930 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2006 Ondrej Palkovsky |
2 | * Copyright (c) 2006 Ondrej Palkovsky |
3 | * Copyright (c) 2006 Jakub Jermar |
3 | * Copyright (c) 2006 Jakub Jermar |
4 | * All rights reserved. |
4 | * All rights reserved. |
5 | * |
5 | * |
6 | * Redistribution and use in source and binary forms, with or without |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions |
7 | * modification, are permitted provided that the following conditions |
8 | * are met: |
8 | * are met: |
9 | * |
9 | * |
10 | * - Redistributions of source code must retain the above copyright |
10 | * - Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. |
11 | * notice, this list of conditions and the following disclaimer. |
12 | * - Redistributions in binary form must reproduce the above copyright |
12 | * - Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the |
13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. |
14 | * documentation and/or other materials provided with the distribution. |
15 | * - The name of the author may not be used to endorse or promote products |
15 | * - The name of the author may not be used to endorse or promote products |
16 | * derived from this software without specific prior written permission. |
16 | * derived from this software without specific prior written permission. |
17 | * |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ |
28 | */ |
29 | 29 | ||
30 | /** @addtogroup genericipc |
30 | /** @addtogroup genericipc |
31 | * @{ |
31 | * @{ |
32 | */ |
32 | */ |
33 | /** |
33 | /** |
34 | * @file |
34 | * @file |
35 | * @brief IRQ notification framework. |
35 | * @brief IRQ notification framework. |
36 | * |
36 | * |
37 | * This framework allows applications to register to receive a notification |
37 | * This framework allows applications to register to receive a notification |
38 | * when interrupt is detected. The application may provide a simple 'top-half' |
38 | * when interrupt is detected. The application may provide a simple 'top-half' |
39 | * handler as part of its registration, which can perform simple operations |
39 | * handler as part of its registration, which can perform simple operations |
40 | * (read/write port/memory, add information to notification ipc message). |
40 | * (read/write port/memory, add information to notification ipc message). |
41 | * |
41 | * |
42 | * The structure of a notification message is as follows: |
42 | * The structure of a notification message is as follows: |
43 | * - METHOD: method as registered by the SYS_IPC_REGISTER_IRQ syscall |
43 | * - METHOD: method as registered by the SYS_IPC_REGISTER_IRQ syscall |
44 | * - ARG1: payload modified by a 'top-half' handler |
44 | * - ARG1: payload modified by a 'top-half' handler |
45 | * - ARG2: payload modified by a 'top-half' handler |
45 | * - ARG2: payload modified by a 'top-half' handler |
46 | * - ARG3: payload modified by a 'top-half' handler |
46 | * - ARG3: payload modified by a 'top-half' handler |
47 | * - in_phone_hash: interrupt counter (may be needed to assure correct order |
47 | * - in_phone_hash: interrupt counter (may be needed to assure correct order |
48 | * in multithreaded drivers) |
48 | * in multithreaded drivers) |
49 | */ |
49 | */ |
50 | 50 | ||
51 | #include <arch.h> |
51 | #include <arch.h> |
52 | #include <mm/slab.h> |
52 | #include <mm/slab.h> |
53 | #include <errno.h> |
53 | #include <errno.h> |
54 | #include <ddi/irq.h> |
54 | #include <ddi/irq.h> |
55 | #include <ipc/ipc.h> |
55 | #include <ipc/ipc.h> |
56 | #include <ipc/irq.h> |
56 | #include <ipc/irq.h> |
57 | #include <syscall/copy.h> |
57 | #include <syscall/copy.h> |
58 | #include <console/console.h> |
58 | #include <console/console.h> |
59 | #include <print.h> |
59 | #include <print.h> |
60 | 60 | ||
61 | /** Execute code associated with IRQ notification. |
61 | /** Execute code associated with IRQ notification. |
62 | * |
62 | * |
63 | * @param call Notification call. |
63 | * @param call Notification call. |
64 | * @param code Top-half pseudocode. |
64 | * @param code Top-half pseudocode. |
65 | */ |
65 | */ |
66 | static void code_execute(call_t *call, irq_code_t *code) |
66 | static void code_execute(call_t *call, irq_code_t *code) |
67 | { |
67 | { |
68 | unsigned int i; |
68 | unsigned int i; |
69 | unative_t dstval = 0; |
69 | unative_t dstval = 0; |
70 | 70 | ||
71 | if (!code) |
71 | if (!code) |
72 | return; |
72 | return; |
73 | 73 | ||
74 | for (i = 0; i < code->cmdcount; i++) { |
74 | for (i = 0; i < code->cmdcount; i++) { |
75 | switch (code->cmds[i].cmd) { |
75 | switch (code->cmds[i].cmd) { |
76 | case CMD_MEM_READ_1: |
76 | case CMD_MEM_READ_1: |
77 | dstval = *((uint8_t *) code->cmds[i].addr); |
77 | dstval = *((uint8_t *) code->cmds[i].addr); |
78 | break; |
78 | break; |
79 | case CMD_MEM_READ_2: |
79 | case CMD_MEM_READ_2: |
80 | dstval = *((uint16_t *) code->cmds[i].addr); |
80 | dstval = *((uint16_t *) code->cmds[i].addr); |
81 | break; |
81 | break; |
82 | case CMD_MEM_READ_4: |
82 | case CMD_MEM_READ_4: |
83 | dstval = *((uint32_t *) code->cmds[i].addr); |
83 | dstval = *((uint32_t *) code->cmds[i].addr); |
84 | break; |
84 | break; |
85 | case CMD_MEM_READ_8: |
85 | case CMD_MEM_READ_8: |
86 | dstval = *((uint64_t *) code->cmds[i].addr); |
86 | dstval = *((uint64_t *) code->cmds[i].addr); |
87 | break; |
87 | break; |
88 | case CMD_MEM_WRITE_1: |
88 | case CMD_MEM_WRITE_1: |
89 | *((uint8_t *) code->cmds[i].addr) = code->cmds[i].value; |
89 | *((uint8_t *) code->cmds[i].addr) = code->cmds[i].value; |
90 | break; |
90 | break; |
91 | case CMD_MEM_WRITE_2: |
91 | case CMD_MEM_WRITE_2: |
92 | *((uint16_t *) code->cmds[i].addr) = |
92 | *((uint16_t *) code->cmds[i].addr) = |
93 | code->cmds[i].value; |
93 | code->cmds[i].value; |
94 | break; |
94 | break; |
95 | case CMD_MEM_WRITE_4: |
95 | case CMD_MEM_WRITE_4: |
96 | *((uint32_t *) code->cmds[i].addr) = |
96 | *((uint32_t *) code->cmds[i].addr) = |
97 | code->cmds[i].value; |
97 | code->cmds[i].value; |
98 | break; |
98 | break; |
99 | case CMD_MEM_WRITE_8: |
99 | case CMD_MEM_WRITE_8: |
100 | *((uint64_t *) code->cmds[i].addr) = |
100 | *((uint64_t *) code->cmds[i].addr) = |
101 | code->cmds[i].value; |
101 | code->cmds[i].value; |
102 | break; |
102 | break; |
103 | case CMD_PORT_READ_1: |
103 | case CMD_PORT_READ_1: |
104 | dstval = pio_read_8((long) code->cmds[i].addr); |
104 | dstval = pio_read_8((ioport8_t *) code->cmds[i].addr); |
105 | break; |
105 | break; |
106 | case CMD_PORT_WRITE_1: |
106 | case CMD_PORT_WRITE_1: |
107 | pio_write_8((long) code->cmds[i].addr, code->cmds[i].value); |
107 | pio_write_8((ioport8_t *) code->cmds[i].addr, code->cmds[i].value); |
108 | break; |
108 | break; |
109 | default: |
109 | default: |
110 | break; |
110 | break; |
111 | } |
111 | } |
112 | if (code->cmds[i].dstarg && code->cmds[i].dstarg < |
112 | if (code->cmds[i].dstarg && code->cmds[i].dstarg < |
113 | IPC_CALL_LEN) { |
113 | IPC_CALL_LEN) { |
114 | call->data.args[code->cmds[i].dstarg] = dstval; |
114 | call->data.args[code->cmds[i].dstarg] = dstval; |
115 | } |
115 | } |
116 | } |
116 | } |
117 | } |
117 | } |
118 | 118 | ||
119 | /** Free top-half pseudocode. |
119 | /** Free top-half pseudocode. |
120 | * |
120 | * |
121 | * @param code Pointer to the top-half pseudocode. |
121 | * @param code Pointer to the top-half pseudocode. |
122 | */ |
122 | */ |
123 | static void code_free(irq_code_t *code) |
123 | static void code_free(irq_code_t *code) |
124 | { |
124 | { |
125 | if (code) { |
125 | if (code) { |
126 | free(code->cmds); |
126 | free(code->cmds); |
127 | free(code); |
127 | free(code); |
128 | } |
128 | } |
129 | } |
129 | } |
130 | 130 | ||
131 | /** Copy top-half pseudocode from userspace into the kernel. |
131 | /** Copy top-half pseudocode from userspace into the kernel. |
132 | * |
132 | * |
133 | * @param ucode Userspace address of the top-half pseudocode. |
133 | * @param ucode Userspace address of the top-half pseudocode. |
134 | * |
134 | * |
135 | * @return Kernel address of the copied pseudocode. |
135 | * @return Kernel address of the copied pseudocode. |
136 | */ |
136 | */ |
137 | static irq_code_t *code_from_uspace(irq_code_t *ucode) |
137 | static irq_code_t *code_from_uspace(irq_code_t *ucode) |
138 | { |
138 | { |
139 | irq_code_t *code; |
139 | irq_code_t *code; |
140 | irq_cmd_t *ucmds; |
140 | irq_cmd_t *ucmds; |
141 | int rc; |
141 | int rc; |
142 | 142 | ||
143 | code = malloc(sizeof(*code), 0); |
143 | code = malloc(sizeof(*code), 0); |
144 | rc = copy_from_uspace(code, ucode, sizeof(*code)); |
144 | rc = copy_from_uspace(code, ucode, sizeof(*code)); |
145 | if (rc != 0) { |
145 | if (rc != 0) { |
146 | free(code); |
146 | free(code); |
147 | return NULL; |
147 | return NULL; |
148 | } |
148 | } |
149 | 149 | ||
150 | if (code->cmdcount > IRQ_MAX_PROG_SIZE) { |
150 | if (code->cmdcount > IRQ_MAX_PROG_SIZE) { |
151 | free(code); |
151 | free(code); |
152 | return NULL; |
152 | return NULL; |
153 | } |
153 | } |
154 | ucmds = code->cmds; |
154 | ucmds = code->cmds; |
155 | code->cmds = malloc(sizeof(code->cmds[0]) * code->cmdcount, 0); |
155 | code->cmds = malloc(sizeof(code->cmds[0]) * code->cmdcount, 0); |
156 | rc = copy_from_uspace(code->cmds, ucmds, |
156 | rc = copy_from_uspace(code->cmds, ucmds, |
157 | sizeof(code->cmds[0]) * code->cmdcount); |
157 | sizeof(code->cmds[0]) * code->cmdcount); |
158 | if (rc != 0) { |
158 | if (rc != 0) { |
159 | free(code->cmds); |
159 | free(code->cmds); |
160 | free(code); |
160 | free(code); |
161 | return NULL; |
161 | return NULL; |
162 | } |
162 | } |
163 | 163 | ||
164 | return code; |
164 | return code; |
165 | } |
165 | } |
166 | 166 | ||
167 | /** Unregister task from IRQ notification. |
167 | /** Unregister task from IRQ notification. |
168 | * |
168 | * |
169 | * @param box Answerbox associated with the notification. |
169 | * @param box Answerbox associated with the notification. |
170 | * @param inr IRQ number. |
170 | * @param inr IRQ number. |
171 | * @param devno Device number. |
171 | * @param devno Device number. |
172 | */ |
172 | */ |
173 | void ipc_irq_unregister(answerbox_t *box, inr_t inr, devno_t devno) |
173 | void ipc_irq_unregister(answerbox_t *box, inr_t inr, devno_t devno) |
174 | { |
174 | { |
175 | ipl_t ipl; |
175 | ipl_t ipl; |
176 | irq_t *irq; |
176 | irq_t *irq; |
177 | 177 | ||
178 | ipl = interrupts_disable(); |
178 | ipl = interrupts_disable(); |
179 | irq = irq_find_and_lock(inr, devno); |
179 | irq = irq_find_and_lock(inr, devno); |
180 | if (irq) { |
180 | if (irq) { |
181 | if (irq->notif_cfg.answerbox == box) { |
181 | if (irq->notif_cfg.answerbox == box) { |
182 | code_free(irq->notif_cfg.code); |
182 | code_free(irq->notif_cfg.code); |
183 | irq->notif_cfg.notify = false; |
183 | irq->notif_cfg.notify = false; |
184 | irq->notif_cfg.answerbox = NULL; |
184 | irq->notif_cfg.answerbox = NULL; |
185 | irq->notif_cfg.code = NULL; |
185 | irq->notif_cfg.code = NULL; |
186 | irq->notif_cfg.method = 0; |
186 | irq->notif_cfg.method = 0; |
187 | irq->notif_cfg.counter = 0; |
187 | irq->notif_cfg.counter = 0; |
188 | 188 | ||
189 | spinlock_lock(&box->irq_lock); |
189 | spinlock_lock(&box->irq_lock); |
190 | list_remove(&irq->notif_cfg.link); |
190 | list_remove(&irq->notif_cfg.link); |
191 | spinlock_unlock(&box->irq_lock); |
191 | spinlock_unlock(&box->irq_lock); |
192 | 192 | ||
193 | spinlock_unlock(&irq->lock); |
193 | spinlock_unlock(&irq->lock); |
194 | } |
194 | } |
195 | } |
195 | } |
196 | interrupts_restore(ipl); |
196 | interrupts_restore(ipl); |
197 | } |
197 | } |
198 | 198 | ||
199 | /** Register an answerbox as a receiving end for IRQ notifications. |
199 | /** Register an answerbox as a receiving end for IRQ notifications. |
200 | * |
200 | * |
201 | * @param box Receiving answerbox. |
201 | * @param box Receiving answerbox. |
202 | * @param inr IRQ number. |
202 | * @param inr IRQ number. |
203 | * @param devno Device number. |
203 | * @param devno Device number. |
204 | * @param method Method to be associated with the notification. |
204 | * @param method Method to be associated with the notification. |
205 | * @param ucode Uspace pointer to top-half pseudocode. |
205 | * @param ucode Uspace pointer to top-half pseudocode. |
206 | * |
206 | * |
207 | * @return EBADMEM, ENOENT or EEXISTS on failure or 0 on success. |
207 | * @return EBADMEM, ENOENT or EEXISTS on failure or 0 on success. |
208 | */ |
208 | */ |
209 | int ipc_irq_register(answerbox_t *box, inr_t inr, devno_t devno, |
209 | int ipc_irq_register(answerbox_t *box, inr_t inr, devno_t devno, |
210 | unative_t method, irq_code_t *ucode) |
210 | unative_t method, irq_code_t *ucode) |
211 | { |
211 | { |
212 | ipl_t ipl; |
212 | ipl_t ipl; |
213 | irq_code_t *code; |
213 | irq_code_t *code; |
214 | irq_t *irq; |
214 | irq_t *irq; |
215 | 215 | ||
216 | if (ucode) { |
216 | if (ucode) { |
217 | code = code_from_uspace(ucode); |
217 | code = code_from_uspace(ucode); |
218 | if (!code) |
218 | if (!code) |
219 | return EBADMEM; |
219 | return EBADMEM; |
220 | } else { |
220 | } else { |
221 | code = NULL; |
221 | code = NULL; |
222 | } |
222 | } |
223 | 223 | ||
224 | ipl = interrupts_disable(); |
224 | ipl = interrupts_disable(); |
225 | irq = irq_find_and_lock(inr, devno); |
225 | irq = irq_find_and_lock(inr, devno); |
226 | if (!irq) { |
226 | if (!irq) { |
227 | interrupts_restore(ipl); |
227 | interrupts_restore(ipl); |
228 | code_free(code); |
228 | code_free(code); |
229 | return ENOENT; |
229 | return ENOENT; |
230 | } |
230 | } |
231 | 231 | ||
232 | if (irq->notif_cfg.answerbox) { |
232 | if (irq->notif_cfg.answerbox) { |
233 | spinlock_unlock(&irq->lock); |
233 | spinlock_unlock(&irq->lock); |
234 | interrupts_restore(ipl); |
234 | interrupts_restore(ipl); |
235 | code_free(code); |
235 | code_free(code); |
236 | return EEXISTS; |
236 | return EEXISTS; |
237 | } |
237 | } |
238 | 238 | ||
239 | irq->notif_cfg.notify = true; |
239 | irq->notif_cfg.notify = true; |
240 | irq->notif_cfg.answerbox = box; |
240 | irq->notif_cfg.answerbox = box; |
241 | irq->notif_cfg.method = method; |
241 | irq->notif_cfg.method = method; |
242 | irq->notif_cfg.code = code; |
242 | irq->notif_cfg.code = code; |
243 | irq->notif_cfg.counter = 0; |
243 | irq->notif_cfg.counter = 0; |
244 | 244 | ||
245 | spinlock_lock(&box->irq_lock); |
245 | spinlock_lock(&box->irq_lock); |
246 | list_append(&irq->notif_cfg.link, &box->irq_head); |
246 | list_append(&irq->notif_cfg.link, &box->irq_head); |
247 | spinlock_unlock(&box->irq_lock); |
247 | spinlock_unlock(&box->irq_lock); |
248 | 248 | ||
249 | spinlock_unlock(&irq->lock); |
249 | spinlock_unlock(&irq->lock); |
250 | interrupts_restore(ipl); |
250 | interrupts_restore(ipl); |
251 | 251 | ||
252 | return 0; |
252 | return 0; |
253 | } |
253 | } |
254 | 254 | ||
255 | /** Add a call to the proper answerbox queue. |
255 | /** Add a call to the proper answerbox queue. |
256 | * |
256 | * |
257 | * Assume irq->lock is locked. |
257 | * Assume irq->lock is locked. |
258 | * |
258 | * |
259 | * @param irq IRQ structure referencing the target answerbox. |
259 | * @param irq IRQ structure referencing the target answerbox. |
260 | * @param call IRQ notification call. |
260 | * @param call IRQ notification call. |
261 | */ |
261 | */ |
262 | static void send_call(irq_t *irq, call_t *call) |
262 | static void send_call(irq_t *irq, call_t *call) |
263 | { |
263 | { |
264 | spinlock_lock(&irq->notif_cfg.answerbox->irq_lock); |
264 | spinlock_lock(&irq->notif_cfg.answerbox->irq_lock); |
265 | list_append(&call->link, &irq->notif_cfg.answerbox->irq_notifs); |
265 | list_append(&call->link, &irq->notif_cfg.answerbox->irq_notifs); |
266 | spinlock_unlock(&irq->notif_cfg.answerbox->irq_lock); |
266 | spinlock_unlock(&irq->notif_cfg.answerbox->irq_lock); |
267 | 267 | ||
268 | waitq_wakeup(&irq->notif_cfg.answerbox->wq, WAKEUP_FIRST); |
268 | waitq_wakeup(&irq->notif_cfg.answerbox->wq, WAKEUP_FIRST); |
269 | } |
269 | } |
270 | 270 | ||
271 | /** Send notification message. |
271 | /** Send notification message. |
272 | * |
272 | * |
273 | * @param irq IRQ structure. |
273 | * @param irq IRQ structure. |
274 | * @param a1 Driver-specific payload argument. |
274 | * @param a1 Driver-specific payload argument. |
275 | * @param a2 Driver-specific payload argument. |
275 | * @param a2 Driver-specific payload argument. |
276 | * @param a3 Driver-specific payload argument. |
276 | * @param a3 Driver-specific payload argument. |
277 | * @param a4 Driver-specific payload argument. |
277 | * @param a4 Driver-specific payload argument. |
278 | * @param a5 Driver-specific payload argument. |
278 | * @param a5 Driver-specific payload argument. |
279 | */ |
279 | */ |
280 | void ipc_irq_send_msg(irq_t *irq, unative_t a1, unative_t a2, unative_t a3, |
280 | void ipc_irq_send_msg(irq_t *irq, unative_t a1, unative_t a2, unative_t a3, |
281 | unative_t a4, unative_t a5) |
281 | unative_t a4, unative_t a5) |
282 | { |
282 | { |
283 | call_t *call; |
283 | call_t *call; |
284 | 284 | ||
285 | spinlock_lock(&irq->lock); |
285 | spinlock_lock(&irq->lock); |
286 | 286 | ||
287 | if (irq->notif_cfg.answerbox) { |
287 | if (irq->notif_cfg.answerbox) { |
288 | call = ipc_call_alloc(FRAME_ATOMIC); |
288 | call = ipc_call_alloc(FRAME_ATOMIC); |
289 | if (!call) { |
289 | if (!call) { |
290 | spinlock_unlock(&irq->lock); |
290 | spinlock_unlock(&irq->lock); |
291 | return; |
291 | return; |
292 | } |
292 | } |
293 | call->flags |= IPC_CALL_NOTIF; |
293 | call->flags |= IPC_CALL_NOTIF; |
294 | IPC_SET_METHOD(call->data, irq->notif_cfg.method); |
294 | IPC_SET_METHOD(call->data, irq->notif_cfg.method); |
295 | IPC_SET_ARG1(call->data, a1); |
295 | IPC_SET_ARG1(call->data, a1); |
296 | IPC_SET_ARG2(call->data, a2); |
296 | IPC_SET_ARG2(call->data, a2); |
297 | IPC_SET_ARG3(call->data, a3); |
297 | IPC_SET_ARG3(call->data, a3); |
298 | IPC_SET_ARG4(call->data, a4); |
298 | IPC_SET_ARG4(call->data, a4); |
299 | IPC_SET_ARG5(call->data, a5); |
299 | IPC_SET_ARG5(call->data, a5); |
300 | /* Put a counter to the message */ |
300 | /* Put a counter to the message */ |
301 | call->priv = ++irq->notif_cfg.counter; |
301 | call->priv = ++irq->notif_cfg.counter; |
302 | 302 | ||
303 | send_call(irq, call); |
303 | send_call(irq, call); |
304 | } |
304 | } |
305 | spinlock_unlock(&irq->lock); |
305 | spinlock_unlock(&irq->lock); |
306 | } |
306 | } |
307 | 307 | ||
308 | /** Notify a task that an IRQ had occurred. |
308 | /** Notify a task that an IRQ had occurred. |
309 | * |
309 | * |
310 | * We expect interrupts to be disabled and the irq->lock already held. |
310 | * We expect interrupts to be disabled and the irq->lock already held. |
311 | * |
311 | * |
312 | * @param irq IRQ structure. |
312 | * @param irq IRQ structure. |
313 | */ |
313 | */ |
314 | void ipc_irq_send_notif(irq_t *irq) |
314 | void ipc_irq_send_notif(irq_t *irq) |
315 | { |
315 | { |
316 | call_t *call; |
316 | call_t *call; |
317 | 317 | ||
318 | ASSERT(irq); |
318 | ASSERT(irq); |
319 | 319 | ||
320 | if (irq->notif_cfg.answerbox) { |
320 | if (irq->notif_cfg.answerbox) { |
321 | call = ipc_call_alloc(FRAME_ATOMIC); |
321 | call = ipc_call_alloc(FRAME_ATOMIC); |
322 | if (!call) { |
322 | if (!call) { |
323 | return; |
323 | return; |
324 | } |
324 | } |
325 | call->flags |= IPC_CALL_NOTIF; |
325 | call->flags |= IPC_CALL_NOTIF; |
326 | /* Put a counter to the message */ |
326 | /* Put a counter to the message */ |
327 | call->priv = ++irq->notif_cfg.counter; |
327 | call->priv = ++irq->notif_cfg.counter; |
328 | /* Set up args */ |
328 | /* Set up args */ |
329 | IPC_SET_METHOD(call->data, irq->notif_cfg.method); |
329 | IPC_SET_METHOD(call->data, irq->notif_cfg.method); |
330 | 330 | ||
331 | /* Execute code to handle irq */ |
331 | /* Execute code to handle irq */ |
332 | code_execute(call, irq->notif_cfg.code); |
332 | code_execute(call, irq->notif_cfg.code); |
333 | 333 | ||
334 | send_call(irq, call); |
334 | send_call(irq, call); |
335 | } |
335 | } |
336 | } |
336 | } |
337 | 337 | ||
338 | /** Disconnect all IRQ notifications from an answerbox. |
338 | /** Disconnect all IRQ notifications from an answerbox. |
339 | * |
339 | * |
340 | * This function is effective because the answerbox contains |
340 | * This function is effective because the answerbox contains |
341 | * list of all irq_t structures that are registered to |
341 | * list of all irq_t structures that are registered to |
342 | * send notifications to it. |
342 | * send notifications to it. |
343 | * |
343 | * |
344 | * @param box Answerbox for which we want to carry out the cleanup. |
344 | * @param box Answerbox for which we want to carry out the cleanup. |
345 | */ |
345 | */ |
346 | void ipc_irq_cleanup(answerbox_t *box) |
346 | void ipc_irq_cleanup(answerbox_t *box) |
347 | { |
347 | { |
348 | ipl_t ipl; |
348 | ipl_t ipl; |
349 | 349 | ||
350 | loop: |
350 | loop: |
351 | ipl = interrupts_disable(); |
351 | ipl = interrupts_disable(); |
352 | spinlock_lock(&box->irq_lock); |
352 | spinlock_lock(&box->irq_lock); |
353 | 353 | ||
354 | while (box->irq_head.next != &box->irq_head) { |
354 | while (box->irq_head.next != &box->irq_head) { |
355 | link_t *cur = box->irq_head.next; |
355 | link_t *cur = box->irq_head.next; |
356 | irq_t *irq; |
356 | irq_t *irq; |
357 | DEADLOCK_PROBE_INIT(p_irqlock); |
357 | DEADLOCK_PROBE_INIT(p_irqlock); |
358 | 358 | ||
359 | irq = list_get_instance(cur, irq_t, notif_cfg.link); |
359 | irq = list_get_instance(cur, irq_t, notif_cfg.link); |
360 | if (!spinlock_trylock(&irq->lock)) { |
360 | if (!spinlock_trylock(&irq->lock)) { |
361 | /* |
361 | /* |
362 | * Avoid deadlock by trying again. |
362 | * Avoid deadlock by trying again. |
363 | */ |
363 | */ |
364 | spinlock_unlock(&box->irq_lock); |
364 | spinlock_unlock(&box->irq_lock); |
365 | interrupts_restore(ipl); |
365 | interrupts_restore(ipl); |
366 | DEADLOCK_PROBE(p_irqlock, DEADLOCK_THRESHOLD); |
366 | DEADLOCK_PROBE(p_irqlock, DEADLOCK_THRESHOLD); |
367 | goto loop; |
367 | goto loop; |
368 | } |
368 | } |
369 | 369 | ||
370 | ASSERT(irq->notif_cfg.answerbox == box); |
370 | ASSERT(irq->notif_cfg.answerbox == box); |
371 | 371 | ||
372 | list_remove(&irq->notif_cfg.link); |
372 | list_remove(&irq->notif_cfg.link); |
373 | 373 | ||
374 | /* |
374 | /* |
375 | * Don't forget to free any top-half pseudocode. |
375 | * Don't forget to free any top-half pseudocode. |
376 | */ |
376 | */ |
377 | code_free(irq->notif_cfg.code); |
377 | code_free(irq->notif_cfg.code); |
378 | 378 | ||
379 | irq->notif_cfg.notify = false; |
379 | irq->notif_cfg.notify = false; |
380 | irq->notif_cfg.answerbox = NULL; |
380 | irq->notif_cfg.answerbox = NULL; |
381 | irq->notif_cfg.code = NULL; |
381 | irq->notif_cfg.code = NULL; |
382 | irq->notif_cfg.method = 0; |
382 | irq->notif_cfg.method = 0; |
383 | irq->notif_cfg.counter = 0; |
383 | irq->notif_cfg.counter = 0; |
384 | 384 | ||
385 | spinlock_unlock(&irq->lock); |
385 | spinlock_unlock(&irq->lock); |
386 | } |
386 | } |
387 | 387 | ||
388 | spinlock_unlock(&box->irq_lock); |
388 | spinlock_unlock(&box->irq_lock); |
389 | interrupts_restore(ipl); |
389 | interrupts_restore(ipl); |
390 | } |
390 | } |
391 | 391 | ||
392 | /** @} |
392 | /** @} |
393 | */ |
393 | */ |
394 | 394 |