1 /* $NetBSD: pic.c,v 1.3 2008/04/28 20:23:14 martin Exp $ */
3 * Copyright (c) 2008 The NetBSD Foundation, Inc.
6 * This code is derived from software contributed to The NetBSD Foundation
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: pic.c,v 1.3 2008/04/28 20:23:14 martin Exp $");
34 #include <sys/param.h>
35 #include <sys/evcnt.h>
36 #include <sys/atomic.h>
37 #include <sys/malloc.h>
38 #include <sys/mallocvar.h>
39 #include <sys/atomic.h>
41 #include <arm/armreg.h>
43 #include <arm/cpufunc.h>
45 #include <arm/pic/picvar.h>
47 MALLOC_DEFINE(M_INTRSOURCE
, "intrsource", "interrupt source");
50 pic_find_pending_irqs_by_ipl(struct pic_softc
*, size_t, uint32_t, int);
51 static struct pic_softc
*
52 pic_list_find_pic_by_pending_ipl(uint32_t);
54 pic_deliver_irqs(struct pic_softc
*, int, void *);
56 pic_list_deliver_irqs(register_t
, int, void *);
58 struct pic_softc
*pic_list
[PIC_MAXPICS
];
60 #error PIC_MAXPICS > 32 not supported
62 volatile uint32_t pic_blocked_pics
;
63 volatile uint32_t pic_pending_pics
;
64 volatile uint32_t pic_pending_ipls
;
65 struct intrsource
*pic_sources
[PIC_MAXMAXSOURCES
];
66 struct intrsource
*pic__iplsources
[PIC_MAXMAXSOURCES
];
67 struct intrsource
**pic_iplsource
[NIPL
] = {
68 [0 ... NIPL
-1] = pic__iplsources
,
70 size_t pic_ipl_offset
[NIPL
+1];
71 size_t pic_sourcebase
;
72 static struct evcnt pic_deferral_ev
=
73 EVCNT_INITIALIZER(EVCNT_TYPE_MISC
, NULL
, "deferred", "intr");
74 EVCNT_ATTACH_STATIC(pic_deferral_ev
);
79 pic_handle_intr(void *arg
)
81 struct pic_softc
* const pic
= arg
;
84 rv
= (*pic
->pic_ops
->pic_find_pending_irqs
)(pic
);
90 pic_mark_pending_source(struct pic_softc
*pic
, struct intrsource
*is
)
92 const uint32_t ipl_mask
= __BIT(is
->is_ipl
);
94 atomic_or_32(&pic
->pic_pending_irqs
[is
->is_irq
>> 5],
95 __BIT(is
->is_irq
& 0x1f));
97 atomic_or_32(&pic
->pic_pending_ipls
, ipl_mask
);
98 atomic_or_32(&pic_pending_ipls
, ipl_mask
);
99 atomic_or_32(&pic_pending_pics
, __BIT(pic
->pic_id
));
103 pic_mark_pending(struct pic_softc
*pic
, int irq
)
105 struct intrsource
* const is
= pic
->pic_sources
[irq
];
107 KASSERT(irq
< pic
->pic_maxsources
);
110 pic_mark_pending_source(pic
, is
);
114 pic_mark_pending_sources(struct pic_softc
*pic
, size_t irq_base
,
117 struct intrsource
** const isbase
= &pic
->pic_sources
[irq_base
];
118 struct intrsource
*is
;
119 volatile uint32_t *ipending
= &pic
->pic_pending_irqs
[irq_base
>> 5];
120 uint32_t ipl_mask
= 0;
125 KASSERT((irq_base
& 31) == 0);
127 (*pic
->pic_ops
->pic_block_irqs
)(pic
, irq_base
, pending
);
129 atomic_or_32(ipending
, pending
);
130 while (pending
!= 0) {
131 int n
= ffs(pending
);
136 KASSERT(irq_base
<= is
->is_irq
&& is
->is_irq
< irq_base
+ 32);
137 pending
&= ~__BIT(n
);
138 ipl_mask
|= __BIT(is
->is_ipl
);
141 atomic_or_32(&pic
->pic_pending_ipls
, ipl_mask
);
142 atomic_or_32(&pic_pending_ipls
, ipl_mask
);
143 atomic_or_32(&pic_pending_pics
, __BIT(pic
->pic_id
));
149 pic_find_pending_irqs_by_ipl(struct pic_softc
*pic
, size_t irq_base
,
150 uint32_t pending
, int ipl
)
152 uint32_t ipl_irq_mask
= 0;
156 int irq
= ffs(pending
);
160 irq_mask
= __BIT(irq
);
161 KASSERT(pic
->pic_sources
[irq_base
+ irq
] != NULL
);
162 if (pic
->pic_sources
[irq_base
+ irq
]->is_ipl
== ipl
)
163 ipl_irq_mask
|= irq_mask
;
165 pending
&= ~irq_mask
;
170 pic_dispatch(struct intrsource
*is
, void *frame
)
174 if (__predict_false(is
->is_arg
== NULL
)
175 && __predict_true(frame
!= NULL
)) {
176 rv
= (*is
->is_func
)(frame
);
177 } else if (__predict_true(is
->is_arg
!= NULL
)) {
178 rv
= (*is
->is_func
)(is
->is_arg
);
180 pic_deferral_ev
.ev_count
++;
183 is
->is_ev
.ev_count
++;
187 pic_deliver_irqs(struct pic_softc
*pic
, int ipl
, void *frame
)
189 const uint32_t ipl_mask
= __BIT(ipl
);
190 struct intrsource
*is
;
191 volatile uint32_t *ipending
= pic
->pic_pending_irqs
;
192 volatile uint32_t *iblocked
= pic
->pic_blocked_irqs
;
194 #if PIC_MAXSOURCES > 32
197 uint32_t pending_irqs
;
198 uint32_t blocked_irqs
;
200 bool progress
= false;
202 KASSERT(pic
->pic_pending_ipls
& ipl_mask
);
205 #if PIC_MAXSOURCES > 32
210 pending_irqs
= pic_find_pending_irqs_by_ipl(pic
, irq_base
,
212 KASSERT((pending_irqs
& *ipending
) == pending_irqs
);
213 KASSERT((pending_irqs
& ~(*ipending
)) == 0);
214 if (pending_irqs
== 0) {
215 #if PIC_MAXSOURCES > 32
217 if (__predict_true(irq_count
>= pic
->pic_maxsources
))
222 if (irq_base
>= pic
->pic_maxsources
) {
223 ipending
= pic
->pic_pending_irqs
;
224 iblocked
= pic
->pic_blocked_irqs
;
232 blocked_irqs
= pending_irqs
;
234 irq
= ffs(pending_irqs
) - 1;
237 atomic_and_32(ipending
, ~__BIT(irq
));
238 is
= pic
->pic_sources
[irq_base
+ irq
];
241 pic_dispatch(is
, frame
);
245 blocked_irqs
&= ~__BIT(irq
);
247 pending_irqs
= pic_find_pending_irqs_by_ipl(pic
,
248 irq_base
, *ipending
, ipl
);
249 } while (pending_irqs
);
251 atomic_or_32(iblocked
, blocked_irqs
);
252 atomic_or_32(&pic_blocked_pics
, __BIT(pic
->pic_id
));
258 * Since interrupts are disabled, we don't have to be too careful
261 if (atomic_and_32_nv(&pic
->pic_pending_ipls
, ~ipl_mask
) == 0)
262 atomic_and_32(&pic_pending_pics
, ~__BIT(pic
->pic_id
));
266 pic_list_unblock_irqs(void)
268 uint32_t blocked_pics
= pic_blocked_pics
;
270 pic_blocked_pics
= 0;
272 struct pic_softc
*pic
;
273 #if PIC_MAXSOURCES > 32
274 volatile uint32_t *iblocked
;
279 int pic_id
= ffs(blocked_pics
);
283 pic
= pic_list
[pic_id
];
284 KASSERT(pic
!= NULL
);
285 #if PIC_MAXSOURCES > 32
286 for (irq_base
= 0, iblocked
= pic
->pic_blocked_irqs
;
287 irq_base
< pic
->pic_maxsources
;
288 irq_base
+= 32, iblocked
++) {
289 if ((blocked
= *iblocked
) != 0) {
290 (*pic
->pic_ops
->pic_unblock_irqs
)(pic
,
292 atomic_and_32(iblocked
, ~blocked
);
296 KASSERT(pic
->pic_blocked_irqs
[0] != 0);
297 (*pic
->pic_ops
->pic_unblock_irqs
)(pic
,
298 0, pic
->pic_blocked_irqs
[0]);
299 pic
->pic_blocked_irqs
[0] = 0;
301 blocked_pics
&= ~__BIT(pic_id
);
307 pic_list_find_pic_by_pending_ipl(uint32_t ipl_mask
)
309 uint32_t pending_pics
= pic_pending_pics
;
310 struct pic_softc
*pic
;
313 int pic_id
= ffs(pending_pics
);
317 pic
= pic_list
[pic_id
];
318 KASSERT(pic
!= NULL
);
319 if (pic
->pic_pending_ipls
& ipl_mask
)
321 pending_pics
&= ~__BIT(pic_id
);
326 pic_list_deliver_irqs(register_t psw
, int ipl
, void *frame
)
328 const uint32_t ipl_mask
= __BIT(ipl
);
329 struct pic_softc
*pic
;
331 while ((pic
= pic_list_find_pic_by_pending_ipl(ipl_mask
)) != NULL
) {
332 pic_deliver_irqs(pic
, ipl
, frame
);
333 KASSERT((pic
->pic_pending_ipls
& ipl_mask
) == 0);
335 atomic_and_32(&pic_pending_ipls
, ~ipl_mask
);
339 pic_do_pending_ints(register_t psw
, int newipl
, void *frame
)
341 struct cpu_info
* const ci
= curcpu();
342 if (__predict_false(newipl
== IPL_HIGH
))
344 while ((pic_pending_ipls
& ~__BIT(newipl
)) > __BIT(newipl
)) {
345 KASSERT(pic_pending_ipls
< __BIT(NIPL
));
347 int ipl
= 31 - __builtin_clz(pic_pending_ipls
);
353 pic_list_deliver_irqs(psw
, ipl
, frame
);
354 pic_list_unblock_irqs();
357 if (ci
->ci_cpl
!= newipl
)
359 #ifdef __HAVE_FAST_SOFTINTS
365 pic_add(struct pic_softc
*pic
, int irqbase
)
367 int slot
, maybe_slot
= -1;
369 for (slot
= 0; slot
< PIC_MAXPICS
; slot
++) {
370 struct pic_softc
* const xpic
= pic_list
[slot
];
378 if (irqbase
< 0 || xpic
->pic_irqbase
< 0)
380 if (irqbase
>= xpic
->pic_irqbase
+ xpic
->pic_maxsources
)
382 if (irqbase
+ pic
->pic_maxsources
<= xpic
->pic_irqbase
)
384 panic("pic_add: pic %s (%zu sources @ irq %u) conflicts"
385 " with pic %s (%zu sources @ irq %u)",
386 pic
->pic_name
, pic
->pic_maxsources
, irqbase
,
387 xpic
->pic_name
, xpic
->pic_maxsources
, xpic
->pic_irqbase
);
391 printf("%s: pic_sourcebase=%zu pic_maxsources=%zu\n",
392 pic
->pic_name
, pic_sourcebase
, pic
->pic_maxsources
);
394 KASSERT(pic_sourcebase
+ pic
->pic_maxsources
<= PIC_MAXMAXSOURCES
);
396 pic
->pic_sources
= &pic_sources
[pic_sourcebase
];
397 pic
->pic_irqbase
= irqbase
;
398 pic_sourcebase
+= pic
->pic_maxsources
;
400 pic_list
[slot
] = pic
;
404 pic_alloc_irq(struct pic_softc
*pic
)
408 for (irq
= 0; irq
< pic
->pic_maxsources
; irq
++) {
409 if (pic
->pic_sources
[irq
] == NULL
)
417 pic_establish_intr(struct pic_softc
*pic
, int irq
, int ipl
, int type
,
418 int (*func
)(void *), void *arg
)
420 struct intrsource
*is
;
423 if (pic
->pic_sources
[irq
]) {
424 printf("pic_establish_intr: pic %s irq %d already present\n",
429 is
= malloc(sizeof(*is
), M_INTRSOURCE
, M_NOWAIT
|M_ZERO
);
440 if (pic
->pic_ops
->pic_source_name
)
441 (*pic
->pic_ops
->pic_source_name
)(pic
, irq
, is
->is_source
,
442 sizeof(is
->is_source
));
444 snprintf(is
->is_source
, sizeof(is
->is_source
), "irq %d", irq
);
446 evcnt_attach_dynamic(&is
->is_ev
, EVCNT_TYPE_INTR
, NULL
,
447 pic
->pic_name
, is
->is_source
);
449 pic
->pic_sources
[irq
] = is
;
452 * First try to use an existing slot which is empty.
454 for (off
= pic_ipl_offset
[ipl
]; off
< pic_ipl_offset
[ipl
+1]; off
++) {
455 if (pic__iplsources
[off
] == NULL
) {
456 is
->is_iplidx
= off
- pic_ipl_offset
[ipl
];
457 pic__iplsources
[off
] = is
;
463 * Move up all the sources by one.
466 off
= pic_ipl_offset
[ipl
+1];
467 memmove(&pic__iplsources
[off
+1], &pic__iplsources
[off
],
468 sizeof(pic__iplsources
[0]) * (pic_ipl_offset
[NIPL
] - off
));
472 * Advance the offset of all IPLs higher than this. Include an
473 * extra one as well. Thus the number of sources per ipl is
474 * pic_ipl_offset[ipl+1] - pic_ipl_offset[ipl].
476 for (nipl
= ipl
+ 1; nipl
<= NIPL
; nipl
++)
477 pic_ipl_offset
[nipl
]++;
480 * Insert into the previously made position at the end of this IPL's
483 off
= pic_ipl_offset
[ipl
+ 1] - 1;
484 is
->is_iplidx
= off
- pic_ipl_offset
[ipl
];
485 pic__iplsources
[off
] = is
;
487 (*pic
->pic_ops
->pic_establish_irq
)(pic
, is
);
489 (*pic
->pic_ops
->pic_unblock_irqs
)(pic
, is
->is_irq
& ~0x1f,
490 __BIT(is
->is_irq
& 0x1f));
497 pic_disestablish_source(struct intrsource
*is
)
499 struct pic_softc
* const pic
= is
->is_pic
;
500 const int irq
= is
->is_irq
;
502 (*pic
->pic_ops
->pic_block_irqs
)(pic
, irq
& ~31, __BIT(irq
));
503 pic
->pic_sources
[irq
] = NULL
;
504 pic__iplsources
[pic_ipl_offset
[is
->is_ipl
] + is
->is_iplidx
] = NULL
;
505 evcnt_detach(&is
->is_ev
);
507 free(is
, M_INTRSOURCE
);
511 _splraise(int newipl
)
513 struct cpu_info
* const ci
= curcpu();
514 const int oldipl
= ci
->ci_cpl
;
515 KASSERT(newipl
< NIPL
);
516 if (newipl
> ci
->ci_cpl
)
521 _spllower(int newipl
)
523 struct cpu_info
* const ci
= curcpu();
524 const int oldipl
= ci
->ci_cpl
;
525 KASSERT(panicstr
|| newipl
<= ci
->ci_cpl
);
526 if (newipl
< ci
->ci_cpl
) {
527 register_t psw
= disable_interrupts(I32_bit
);
528 pic_do_pending_ints(psw
, newipl
, NULL
);
529 restore_interrupts(psw
);
537 struct cpu_info
* const ci
= curcpu();
538 KASSERT(savedipl
< NIPL
);
539 if (savedipl
< ci
->ci_cpl
) {
540 register_t psw
= disable_interrupts(I32_bit
);
541 pic_do_pending_ints(psw
, savedipl
, NULL
);
542 restore_interrupts(psw
);
544 ci
->ci_cpl
= savedipl
;
548 intr_establish(int irq
, int ipl
, int type
, int (*func
)(void *), void *arg
)
552 for (slot
= 0; slot
< PIC_MAXPICS
; slot
++) {
553 struct pic_softc
* const pic
= pic_list
[slot
];
554 if (pic
== NULL
|| pic
->pic_irqbase
< 0)
556 if (pic
->pic_irqbase
<= irq
557 && irq
< pic
->pic_irqbase
+ pic
->pic_maxsources
) {
558 return pic_establish_intr(pic
, irq
- pic
->pic_irqbase
,
559 ipl
, type
, func
, arg
);
567 intr_disestablish(void *ih
)
569 struct intrsource
* const is
= ih
;
570 pic_disestablish_source(is
);