xref: /netbsd-src/sys/arch/arm/pic/pic.c (revision a5847cc334d9a7029f6352b847e9e8d71a0f9e0c)
1 /*	$NetBSD: pic.c,v 1.8 2011/03/11 03:16:14 bsh Exp $	*/
2 /*-
3  * Copyright (c) 2008 The NetBSD Foundation, Inc.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to The NetBSD Foundation
7  * by Matt Thomas.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
17  *
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.
29  */
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: pic.c,v 1.8 2011/03/11 03:16:14 bsh Exp $");
32 
33 #define _INTR_PRIVATE
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>
40 
41 #include <arm/armreg.h>
42 #include <arm/cpu.h>
43 #include <arm/cpufunc.h>
44 
45 #include <arm/pic/picvar.h>
46 
47 MALLOC_DEFINE(M_INTRSOURCE, "intrsource", "interrupt source");
48 
49 static uint32_t
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);
53 static void
54 	pic_deliver_irqs(struct pic_softc *, int, void *);
55 static void
56 	pic_list_deliver_irqs(register_t, int, void *);
57 
58 struct pic_softc *pic_list[PIC_MAXPICS];
59 #if PIC_MAXPICS > 32
60 #error PIC_MAXPICS > 32 not supported
61 #endif
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,
69 };
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);
75 
76 
77 
78 int
79 pic_handle_intr(void *arg)
80 {
81 	struct pic_softc * const pic = arg;
82 	int rv;
83 
84 	rv = (*pic->pic_ops->pic_find_pending_irqs)(pic);
85 
86 	return rv > 0;
87 }
88 
89 void
90 pic_mark_pending_source(struct pic_softc *pic, struct intrsource *is)
91 {
92 	const uint32_t ipl_mask = __BIT(is->is_ipl);
93 
94 	atomic_or_32(&pic->pic_pending_irqs[is->is_irq >> 5],
95 	    __BIT(is->is_irq & 0x1f));
96 
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));
100 }
101 
102 void
103 pic_mark_pending(struct pic_softc *pic, int irq)
104 {
105 	struct intrsource * const is = pic->pic_sources[irq];
106 
107 	KASSERT(irq < pic->pic_maxsources);
108 	KASSERT(is != NULL);
109 
110 	pic_mark_pending_source(pic, is);
111 }
112 
113 uint32_t
114 pic_mark_pending_sources(struct pic_softc *pic, size_t irq_base,
115 	uint32_t pending)
116 {
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;
121 
122 	if (pending == 0)
123 		return ipl_mask;
124 
125 	KASSERT((irq_base & 31) == 0);
126 
127 	(*pic->pic_ops->pic_block_irqs)(pic, irq_base, pending);
128 
129 	atomic_or_32(ipending, pending);
130         while (pending != 0) {
131 		int n = ffs(pending);
132 		if (n-- == 0)
133 			break;
134 		is = isbase[n];
135 		KASSERT(is != NULL);
136 		KASSERT(irq_base <= is->is_irq && is->is_irq < irq_base + 32);
137 		pending &= ~__BIT(n);
138 		ipl_mask |= __BIT(is->is_ipl);
139 	}
140 
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));
144 
145 	return ipl_mask;
146 }
147 
148 uint32_t
149 pic_find_pending_irqs_by_ipl(struct pic_softc *pic, size_t irq_base,
150 	uint32_t pending, int ipl)
151 {
152 	uint32_t ipl_irq_mask = 0;
153 	uint32_t irq_mask;
154 
155 	for (;;) {
156 		int irq = ffs(pending);
157 		if (irq-- == 0)
158 			return ipl_irq_mask;
159 
160 		irq_mask = __BIT(irq);
161 #if 1
162     		KASSERT(pic->pic_sources[irq_base + irq] != NULL);
163 #else
164 		if (pic->pic_sources[irq_base + irq] == NULL) {
165 			aprint_error("stray interrupt? irq_base=%zu irq=%d\n",
166 			    irq_base, irq);
167 		} else
168 #endif
169 		if (pic->pic_sources[irq_base + irq]->is_ipl == ipl)
170 			ipl_irq_mask |= irq_mask;
171 
172 		pending &= ~irq_mask;
173 	}
174 }
175 
176 void
177 pic_dispatch(struct intrsource *is, void *frame)
178 {
179 	int rv;
180 
181 	if (__predict_false(is->is_arg == NULL)
182 	    && __predict_true(frame != NULL)) {
183 		rv = (*is->is_func)(frame);
184 	} else if (__predict_true(is->is_arg != NULL)) {
185 		rv = (*is->is_func)(is->is_arg);
186 	} else {
187 		pic_deferral_ev.ev_count++;
188 		return;
189 	}
190 	is->is_ev.ev_count++;
191 }
192 
193 void
194 pic_deliver_irqs(struct pic_softc *pic, int ipl, void *frame)
195 {
196 	const uint32_t ipl_mask = __BIT(ipl);
197 	struct intrsource *is;
198 	volatile uint32_t *ipending = pic->pic_pending_irqs;
199 	volatile uint32_t *iblocked = pic->pic_blocked_irqs;
200 	size_t irq_base;
201 #if PIC_MAXSOURCES > 32
202 	size_t irq_count;
203 	int poi = 0;		/* Possibility of interrupting */
204 #endif
205 	uint32_t pending_irqs;
206 	uint32_t blocked_irqs;
207 	int irq;
208 	bool progress = false;
209 
210 	KASSERT(pic->pic_pending_ipls & ipl_mask);
211 
212 	irq_base = 0;
213 #if PIC_MAXSOURCES > 32
214 	irq_count = 0;
215 #endif
216 
217 	for (;;) {
218 		pending_irqs = pic_find_pending_irqs_by_ipl(pic, irq_base,
219 		    *ipending, ipl);
220 		KASSERT((pending_irqs & *ipending) == pending_irqs);
221 		KASSERT((pending_irqs & ~(*ipending)) == 0);
222 		if (pending_irqs == 0) {
223 #if PIC_MAXSOURCES > 32
224 			irq_count += 32;
225 			if (__predict_true(irq_count >= pic->pic_maxsources)) {
226 				if (!poi)
227 					/*Interrupt at this level was handled.*/
228 					break;
229 				irq_base = 0;
230 				irq_count = 0;
231 				poi = 0;
232 				ipending = pic->pic_pending_irqs;
233 				iblocked = pic->pic_blocked_irqs;
234 			} else {
235 				irq_base += 32;
236 				ipending++;
237 				iblocked++;
238 				KASSERT(irq_base <= pic->pic_maxsources);
239 			}
240 			continue;
241 #else
242 			break;
243 #endif
244 		}
245 		progress = true;
246 		blocked_irqs = 0;
247 		do {
248 			irq = ffs(pending_irqs) - 1;
249 			KASSERT(irq >= 0);
250 
251 			atomic_and_32(ipending, ~__BIT(irq));
252 			is = pic->pic_sources[irq_base + irq];
253 			if (is != NULL) {
254 				cpsie(I32_bit);
255 				pic_dispatch(is, frame);
256 				cpsid(I32_bit);
257 #if PIC_MAXSOURCES > 32
258 				/*
259 				 * There is a possibility of interrupting
260 				 * from cpsie() to cpsid().
261 				 */
262 				poi = 1;
263 #endif
264 				blocked_irqs |= __BIT(irq);
265 			} else {
266 				KASSERT(0);
267 			}
268 			pending_irqs = pic_find_pending_irqs_by_ipl(pic,
269 			    irq_base, *ipending, ipl);
270 		} while (pending_irqs);
271 		if (blocked_irqs) {
272 			atomic_or_32(iblocked, blocked_irqs);
273 			atomic_or_32(&pic_blocked_pics, __BIT(pic->pic_id));
274 		}
275 	}
276 
277 	KASSERT(progress);
278 	/*
279 	 * Since interrupts are disabled, we don't have to be too careful
280 	 * about these.
281 	 */
282 	if (atomic_and_32_nv(&pic->pic_pending_ipls, ~ipl_mask) == 0)
283 		atomic_and_32(&pic_pending_pics, ~__BIT(pic->pic_id));
284 }
285 
286 static void
287 pic_list_unblock_irqs(void)
288 {
289 	uint32_t blocked_pics = pic_blocked_pics;
290 
291 	pic_blocked_pics = 0;
292 	for (;;) {
293 		struct pic_softc *pic;
294 #if PIC_MAXSOURCES > 32
295 		volatile uint32_t *iblocked;
296 		uint32_t blocked;
297 		size_t irq_base;
298 #endif
299 
300 		int pic_id = ffs(blocked_pics);
301 		if (pic_id-- == 0)
302 			return;
303 
304 		pic = pic_list[pic_id];
305 		KASSERT(pic != NULL);
306 #if PIC_MAXSOURCES > 32
307 		for (irq_base = 0, iblocked = pic->pic_blocked_irqs;
308 		     irq_base < pic->pic_maxsources;
309 		     irq_base += 32, iblocked++) {
310 			if ((blocked = *iblocked) != 0) {
311 				(*pic->pic_ops->pic_unblock_irqs)(pic,
312 				    irq_base, blocked);
313 				atomic_and_32(iblocked, ~blocked);
314 			}
315 		}
316 #else
317 		KASSERT(pic->pic_blocked_irqs[0] != 0);
318 		(*pic->pic_ops->pic_unblock_irqs)(pic,
319 		    0, pic->pic_blocked_irqs[0]);
320 		pic->pic_blocked_irqs[0] = 0;
321 #endif
322 		blocked_pics &= ~__BIT(pic_id);
323 	}
324 }
325 
326 
327 struct pic_softc *
328 pic_list_find_pic_by_pending_ipl(uint32_t ipl_mask)
329 {
330 	uint32_t pending_pics = pic_pending_pics;
331 	struct pic_softc *pic;
332 
333 	for (;;) {
334 		int pic_id = ffs(pending_pics);
335 		if (pic_id-- == 0)
336 			return NULL;
337 
338 		pic = pic_list[pic_id];
339 		KASSERT(pic != NULL);
340 		if (pic->pic_pending_ipls & ipl_mask)
341 			return pic;
342 		pending_pics &= ~__BIT(pic_id);
343 	}
344 }
345 
346 void
347 pic_list_deliver_irqs(register_t psw, int ipl, void *frame)
348 {
349 	const uint32_t ipl_mask = __BIT(ipl);
350 	struct pic_softc *pic;
351 
352 	while ((pic = pic_list_find_pic_by_pending_ipl(ipl_mask)) != NULL) {
353 		pic_deliver_irqs(pic, ipl, frame);
354 		KASSERT((pic->pic_pending_ipls & ipl_mask) == 0);
355 	}
356 	atomic_and_32(&pic_pending_ipls, ~ipl_mask);
357 }
358 
359 void
360 pic_do_pending_ints(register_t psw, int newipl, void *frame)
361 {
362 	struct cpu_info * const ci = curcpu();
363 	if (__predict_false(newipl == IPL_HIGH))
364 		return;
365 	while ((pic_pending_ipls & ~__BIT(newipl)) > __BIT(newipl)) {
366 		KASSERT(pic_pending_ipls < __BIT(NIPL));
367 		for (;;) {
368 			int ipl = 31 - __builtin_clz(pic_pending_ipls);
369 			KASSERT(ipl < NIPL);
370 			if (ipl <= newipl)
371 				break;
372 
373 			ci->ci_cpl = ipl;
374 			pic_list_deliver_irqs(psw, ipl, frame);
375 			pic_list_unblock_irqs();
376 		}
377 	}
378 	if (ci->ci_cpl != newipl)
379 		ci->ci_cpl = newipl;
380 #ifdef __HAVE_FAST_SOFTINTS
381 	cpu_dosoftints();
382 #endif
383 }
384 
385 void
386 pic_add(struct pic_softc *pic, int irqbase)
387 {
388 	int slot, maybe_slot = -1;
389 
390 	for (slot = 0; slot < PIC_MAXPICS; slot++) {
391 		struct pic_softc * const xpic = pic_list[slot];
392 		if (xpic == NULL) {
393 			if (maybe_slot < 0)
394 				maybe_slot = slot;
395 			if (irqbase < 0)
396 				break;
397 			continue;
398 		}
399 		if (irqbase < 0 || xpic->pic_irqbase < 0)
400 			continue;
401 		if (irqbase >= xpic->pic_irqbase + xpic->pic_maxsources)
402 			continue;
403 		if (irqbase + pic->pic_maxsources <= xpic->pic_irqbase)
404 			continue;
405 		panic("pic_add: pic %s (%zu sources @ irq %u) conflicts"
406 		    " with pic %s (%zu sources @ irq %u)",
407 		    pic->pic_name, pic->pic_maxsources, irqbase,
408 		    xpic->pic_name, xpic->pic_maxsources, xpic->pic_irqbase);
409 	}
410 	slot = maybe_slot;
411 #if 0
412 	printf("%s: pic_sourcebase=%zu pic_maxsources=%zu\n",
413 	    pic->pic_name, pic_sourcebase, pic->pic_maxsources);
414 #endif
415 	KASSERT(pic_sourcebase + pic->pic_maxsources <= PIC_MAXMAXSOURCES);
416 
417 	pic->pic_sources = &pic_sources[pic_sourcebase];
418 	pic->pic_irqbase = irqbase;
419 	pic_sourcebase += pic->pic_maxsources;
420 	pic->pic_id = slot;
421 	pic_list[slot] = pic;
422 }
423 
424 int
425 pic_alloc_irq(struct pic_softc *pic)
426 {
427 	int irq;
428 
429 	for (irq = 0; irq < pic->pic_maxsources; irq++) {
430 		if (pic->pic_sources[irq] == NULL)
431 			return irq;
432 	}
433 
434 	return -1;
435 }
436 
437 void *
438 pic_establish_intr(struct pic_softc *pic, int irq, int ipl, int type,
439 	int (*func)(void *), void *arg)
440 {
441 	struct intrsource *is;
442 	int off, nipl;
443 
444 	if (pic->pic_sources[irq]) {
445 		printf("pic_establish_intr: pic %s irq %d already present\n",
446 		    pic->pic_name, irq);
447 		return NULL;
448 	}
449 
450 	is = malloc(sizeof(*is), M_INTRSOURCE, M_NOWAIT|M_ZERO);
451 	if (is == NULL)
452 		return NULL;
453 
454 	is->is_pic = pic;
455 	is->is_irq = irq;
456 	is->is_ipl = ipl;
457 	is->is_type = type;
458 	is->is_func = func;
459 	is->is_arg = arg;
460 
461 	if (pic->pic_ops->pic_source_name)
462 		(*pic->pic_ops->pic_source_name)(pic, irq, is->is_source,
463 		    sizeof(is->is_source));
464 	else
465 		snprintf(is->is_source, sizeof(is->is_source), "irq %d", irq);
466 
467 	evcnt_attach_dynamic(&is->is_ev, EVCNT_TYPE_INTR, NULL,
468 	    pic->pic_name, is->is_source);
469 
470 	pic->pic_sources[irq] = is;
471 
472 	/*
473 	 * First try to use an existing slot which is empty.
474 	 */
475 	for (off = pic_ipl_offset[ipl]; off < pic_ipl_offset[ipl+1]; off++) {
476 		if (pic__iplsources[off] == NULL) {
477 			is->is_iplidx = off - pic_ipl_offset[ipl];
478 			pic__iplsources[off] = is;
479 			return is;
480 		}
481 	}
482 
483 	/*
484 	 * Move up all the sources by one.
485  	 */
486 	if (ipl < NIPL) {
487 		off = pic_ipl_offset[ipl+1];
488 		memmove(&pic__iplsources[off+1], &pic__iplsources[off],
489 		    sizeof(pic__iplsources[0]) * (pic_ipl_offset[NIPL] - off));
490 	}
491 
492 	/*
493 	 * Advance the offset of all IPLs higher than this.  Include an
494 	 * extra one as well.  Thus the number of sources per ipl is
495 	 * pic_ipl_offset[ipl+1] - pic_ipl_offset[ipl].
496 	 */
497 	for (nipl = ipl + 1; nipl <= NIPL; nipl++)
498 		pic_ipl_offset[nipl]++;
499 
500 	/*
501 	 * Insert into the previously made position at the end of this IPL's
502 	 * sources.
503 	 */
504 	off = pic_ipl_offset[ipl + 1] - 1;
505 	is->is_iplidx = off - pic_ipl_offset[ipl];
506 	pic__iplsources[off] = is;
507 
508 	(*pic->pic_ops->pic_establish_irq)(pic, is);
509 
510 	(*pic->pic_ops->pic_unblock_irqs)(pic, is->is_irq & ~0x1f,
511 	    __BIT(is->is_irq & 0x1f));
512 
513 	/* We're done. */
514 	return is;
515 }
516 
517 void
518 pic_disestablish_source(struct intrsource *is)
519 {
520 	struct pic_softc * const pic = is->is_pic;
521 	const int irq = is->is_irq;
522 
523 	(*pic->pic_ops->pic_block_irqs)(pic, irq & ~31, __BIT(irq));
524 	pic->pic_sources[irq] = NULL;
525 	pic__iplsources[pic_ipl_offset[is->is_ipl] + is->is_iplidx] = NULL;
526 	evcnt_detach(&is->is_ev);
527 
528 	free(is, M_INTRSOURCE);
529 }
530 
531 void *
532 intr_establish(int irq, int ipl, int type, int (*func)(void *), void *arg)
533 {
534 	int slot;
535 
536 	for (slot = 0; slot < PIC_MAXPICS; slot++) {
537 		struct pic_softc * const pic = pic_list[slot];
538 		if (pic == NULL || pic->pic_irqbase < 0)
539 			continue;
540 		if (pic->pic_irqbase <= irq
541 		    && irq < pic->pic_irqbase + pic->pic_maxsources) {
542 			return pic_establish_intr(pic, irq - pic->pic_irqbase,
543 			    ipl, type, func, arg);
544 		}
545 	}
546 
547 	return NULL;
548 }
549 
550 void
551 intr_disestablish(void *ih)
552 {
553 	struct intrsource * const is = ih;
554 	pic_disestablish_source(is);
555 }
556