Home | History | Annotate | Line # | Download | only in pic
intr.c revision 1.2
      1  1.2  garbled /*	$NetBSD: intr.c,v 1.2 2007/10/17 19:56:45 garbled Exp $ */
      2  1.2  garbled 
      3  1.2  garbled /*-
      4  1.2  garbled  * Copyright (c) 2007 Michael Lorenz
      5  1.2  garbled  * All rights reserved.
      6  1.2  garbled  *
      7  1.2  garbled  * Redistribution and use in source and binary forms, with or without
      8  1.2  garbled  * modification, are permitted provided that the following conditions
      9  1.2  garbled  * are met:
     10  1.2  garbled  * 1. Redistributions of source code must retain the above copyright
     11  1.2  garbled  *    notice, this list of conditions and the following disclaimer.
     12  1.2  garbled  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.2  garbled  *    notice, this list of conditions and the following disclaimer in the
     14  1.2  garbled  *    documentation and/or other materials provided with the distribution.
     15  1.2  garbled  * 3. Neither the name of The NetBSD Foundation nor the names of its
     16  1.2  garbled  *    contributors may be used to endorse or promote products derived
     17  1.2  garbled  *    from this software without specific prior written permission.
     18  1.2  garbled  *
     19  1.2  garbled  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.2  garbled  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.2  garbled  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.2  garbled  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.2  garbled  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.2  garbled  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.2  garbled  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.2  garbled  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.2  garbled  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.2  garbled  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.2  garbled  * POSSIBILITY OF SUCH DAMAGE.
     30  1.2  garbled  */
     31  1.2  garbled 
     32  1.2  garbled #include <sys/cdefs.h>
     33  1.2  garbled __KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.2 2007/10/17 19:56:45 garbled Exp $");
     34  1.2  garbled 
     35  1.2  garbled #include "opt_multiprocessor.h"
     36  1.2  garbled 
     37  1.2  garbled #include <sys/param.h>
     38  1.2  garbled #include <sys/malloc.h>
     39  1.2  garbled #include <sys/kernel.h>
     40  1.2  garbled 
     41  1.2  garbled #include <uvm/uvm_extern.h>
     42  1.2  garbled 
     43  1.2  garbled #include <arch/powerpc/pic/picvar.h>
     44  1.2  garbled #include "opt_pic.h"
     45  1.2  garbled #include "opt_interrupt.h"
     46  1.2  garbled #if defined(PIC_I8259) || defined (PIC_PREPIVR)
     47  1.2  garbled #include <machine/isa_machdep.h>
     48  1.2  garbled #endif
     49  1.2  garbled 
     50  1.2  garbled #ifdef MULTIPROCESSOR
     51  1.2  garbled #include <arch/powerpc/pic/ipivar.h>
     52  1.2  garbled #endif
     53  1.2  garbled 
     54  1.2  garbled #define MAX_PICS	8	/* 8 PICs ought to be enough for everyone */
     55  1.2  garbled 
     56  1.2  garbled #define NVIRQ		32	/* 32 virtual IRQs */
     57  1.2  garbled #define NIRQ		128	/* up to 128 HW IRQs */
     58  1.2  garbled 
     59  1.2  garbled #define HWIRQ_MAX	(NVIRQ - 4 - 1)
     60  1.2  garbled #define HWIRQ_MASK	0x0fffffff
     61  1.2  garbled #define	LEGAL_VIRQ(x)	((x) >= 0 && (x) < NVIRQ)
     62  1.2  garbled 
     63  1.2  garbled struct pic_ops *pics[MAX_PICS];
     64  1.2  garbled int num_pics = 0;
     65  1.2  garbled int max_base = 0;
     66  1.2  garbled uint8_t	virq[NIRQ];
     67  1.2  garbled int	virq_max = 0;
     68  1.2  garbled int	imask[NIPL];
     69  1.2  garbled int	primary_pic = 0;
     70  1.2  garbled 
     71  1.2  garbled static int	fakeintr(void *);
     72  1.2  garbled static int	mapirq(uint32_t);
     73  1.2  garbled static void	intr_calculatemasks(void);
     74  1.2  garbled static struct pic_ops *find_pic_by_irq(int);
     75  1.2  garbled 
     76  1.2  garbled static struct intr_source intrsources[NVIRQ];
     77  1.2  garbled 
     78  1.2  garbled void
     79  1.2  garbled pic_init(void)
     80  1.2  garbled {
     81  1.2  garbled 	int i;
     82  1.2  garbled 
     83  1.2  garbled 	for (i = 0; i < NIRQ; i++)
     84  1.2  garbled 		virq[i] = 0;
     85  1.2  garbled 	memset(intrsources, 0, sizeof(intrsources));
     86  1.2  garbled }
     87  1.2  garbled 
     88  1.2  garbled int
     89  1.2  garbled pic_add(struct pic_ops *pic)
     90  1.2  garbled {
     91  1.2  garbled 
     92  1.2  garbled 	if (num_pics >= MAX_PICS)
     93  1.2  garbled 		return -1;
     94  1.2  garbled 
     95  1.2  garbled 	pics[num_pics] = pic;
     96  1.2  garbled 	pic->pic_intrbase = max_base;
     97  1.2  garbled 	max_base += pic->pic_numintrs;
     98  1.2  garbled 	num_pics++;
     99  1.2  garbled 
    100  1.2  garbled 	return pic->pic_intrbase;
    101  1.2  garbled }
    102  1.2  garbled 
    103  1.2  garbled void
    104  1.2  garbled pic_finish_setup(void)
    105  1.2  garbled {
    106  1.2  garbled 	struct pic_ops *pic;
    107  1.2  garbled 	int i;
    108  1.2  garbled 
    109  1.2  garbled 	for (i = 0; i < num_pics; i++) {
    110  1.2  garbled 		pic = pics[i];
    111  1.2  garbled 		if (pic->pic_finish_setup != NULL)
    112  1.2  garbled 			pic->pic_finish_setup(pic);
    113  1.2  garbled 	}
    114  1.2  garbled }
    115  1.2  garbled 
    116  1.2  garbled static struct pic_ops *
    117  1.2  garbled find_pic_by_irq(int irq)
    118  1.2  garbled {
    119  1.2  garbled 	struct pic_ops *current;
    120  1.2  garbled 	int base = 0;
    121  1.2  garbled 
    122  1.2  garbled 	while (base < num_pics) {
    123  1.2  garbled 
    124  1.2  garbled 		current = pics[base];
    125  1.2  garbled 		if ((irq >= current->pic_intrbase) &&
    126  1.2  garbled 		    (irq < (current->pic_intrbase + current->pic_numintrs))) {
    127  1.2  garbled 
    128  1.2  garbled 			return current;
    129  1.2  garbled 		}
    130  1.2  garbled 		base++;
    131  1.2  garbled 	}
    132  1.2  garbled 	return NULL;
    133  1.2  garbled }
    134  1.2  garbled 
    135  1.2  garbled static int
    136  1.2  garbled fakeintr(void *arg)
    137  1.2  garbled {
    138  1.2  garbled 
    139  1.2  garbled 	return 0;
    140  1.2  garbled }
    141  1.2  garbled 
    142  1.2  garbled /*
    143  1.2  garbled  * Register an interrupt handler.
    144  1.2  garbled  */
    145  1.2  garbled void *
    146  1.2  garbled intr_establish(int hwirq, int type, int level, int (*ih_fun)(void *),
    147  1.2  garbled     void *ih_arg)
    148  1.2  garbled {
    149  1.2  garbled 	struct intrhand **p, *q, *ih;
    150  1.2  garbled 	struct intr_source *is;
    151  1.2  garbled 	struct pic_ops *pic;
    152  1.2  garbled 	static struct intrhand fakehand;
    153  1.2  garbled 	int irq, maxlevel = level;
    154  1.2  garbled 
    155  1.2  garbled 	if (maxlevel == IPL_NONE)
    156  1.2  garbled 		maxlevel = IPL_HIGH;
    157  1.2  garbled 
    158  1.2  garbled 	if (hwirq >= max_base) {
    159  1.2  garbled 
    160  1.2  garbled 		panic("%s: bogus IRQ %d, max is %d", __func__, hwirq,
    161  1.2  garbled 		    max_base - 1);
    162  1.2  garbled 	}
    163  1.2  garbled 
    164  1.2  garbled 	pic = find_pic_by_irq(hwirq);
    165  1.2  garbled 	if (pic == NULL) {
    166  1.2  garbled 
    167  1.2  garbled 		panic("%s: cannot find a pic for IRQ %d", __func__, hwirq);
    168  1.2  garbled 	}
    169  1.2  garbled 
    170  1.2  garbled 	irq = mapirq(hwirq);
    171  1.2  garbled 
    172  1.2  garbled 	/* no point in sleeping unless someone can free memory. */
    173  1.2  garbled 	ih = malloc(sizeof *ih, M_DEVBUF, cold ? M_NOWAIT : M_WAITOK);
    174  1.2  garbled 	if (ih == NULL)
    175  1.2  garbled 		panic("intr_establish: can't malloc handler info");
    176  1.2  garbled 
    177  1.2  garbled 	if (!LEGAL_VIRQ(irq) || type == IST_NONE)
    178  1.2  garbled 		panic("intr_establish: bogus irq (%d) or type (%d)", irq, type);
    179  1.2  garbled 
    180  1.2  garbled 	is = &intrsources[irq];
    181  1.2  garbled 
    182  1.2  garbled 	switch (is->is_type) {
    183  1.2  garbled 	case IST_NONE:
    184  1.2  garbled 		is->is_type = type;
    185  1.2  garbled 		break;
    186  1.2  garbled 	case IST_EDGE:
    187  1.2  garbled 	case IST_LEVEL:
    188  1.2  garbled 		if (type == is->is_type)
    189  1.2  garbled 			break;
    190  1.2  garbled 	case IST_PULSE:
    191  1.2  garbled 		if (type != IST_NONE)
    192  1.2  garbled 			panic("intr_establish: can't share %s with %s",
    193  1.2  garbled 			    intr_typename(is->is_type),
    194  1.2  garbled 			    intr_typename(type));
    195  1.2  garbled 		break;
    196  1.2  garbled 	}
    197  1.2  garbled 	if (is->is_hand == NULL) {
    198  1.2  garbled 		snprintf(is->is_source, sizeof(is->is_source), "irq %d",
    199  1.2  garbled 		    is->is_hwirq);
    200  1.2  garbled 		evcnt_attach_dynamic(&is->is_ev, EVCNT_TYPE_INTR, NULL,
    201  1.2  garbled 		    pic->pic_name, is->is_source);
    202  1.2  garbled 	}
    203  1.2  garbled 
    204  1.2  garbled 	/*
    205  1.2  garbled 	 * Figure out where to put the handler.
    206  1.2  garbled 	 * This is O(N^2), but we want to preserve the order, and N is
    207  1.2  garbled 	 * generally small.
    208  1.2  garbled 	 */
    209  1.2  garbled 	for (p = &is->is_hand; (q = *p) != NULL; p = &q->ih_next) {
    210  1.2  garbled 
    211  1.2  garbled 		maxlevel = max(maxlevel, q->ih_level);
    212  1.2  garbled 	}
    213  1.2  garbled 
    214  1.2  garbled 	/*
    215  1.2  garbled 	 * Actually install a fake handler momentarily, since we might be doing
    216  1.2  garbled 	 * this with interrupts enabled and don't want the real routine called
    217  1.2  garbled 	 * until masking is set up.
    218  1.2  garbled 	 */
    219  1.2  garbled 	fakehand.ih_level = level;
    220  1.2  garbled 	fakehand.ih_fun = fakeintr;
    221  1.2  garbled 	*p = &fakehand;
    222  1.2  garbled 
    223  1.2  garbled 	/*
    224  1.2  garbled 	 * Poke the real handler in now.
    225  1.2  garbled 	 */
    226  1.2  garbled 	ih->ih_fun = ih_fun;
    227  1.2  garbled 	ih->ih_arg = ih_arg;
    228  1.2  garbled 	ih->ih_next = NULL;
    229  1.2  garbled 	ih->ih_level = level;
    230  1.2  garbled 	ih->ih_irq = irq;
    231  1.2  garbled 	*p = ih;
    232  1.2  garbled 
    233  1.2  garbled 	if (pic->pic_establish_irq != NULL)
    234  1.2  garbled 		pic->pic_establish_irq(pic, hwirq - pic->pic_intrbase,
    235  1.2  garbled 		    is->is_type, maxlevel);
    236  1.2  garbled 
    237  1.2  garbled 	/*
    238  1.2  garbled 	 * now that the handler is established we're actually ready to
    239  1.2  garbled 	 * calculate the masks
    240  1.2  garbled 	 */
    241  1.2  garbled 	intr_calculatemasks();
    242  1.2  garbled 
    243  1.2  garbled 
    244  1.2  garbled 	return ih;
    245  1.2  garbled }
    246  1.2  garbled 
    247  1.2  garbled void
    248  1.2  garbled dummy_pic_establish_intr(struct pic_ops *pic, int irq, int type, int pri)
    249  1.2  garbled {
    250  1.2  garbled }
    251  1.2  garbled 
    252  1.2  garbled /*
    253  1.2  garbled  * Deregister an interrupt handler.
    254  1.2  garbled  */
    255  1.2  garbled void
    256  1.2  garbled intr_disestablish(void *arg)
    257  1.2  garbled {
    258  1.2  garbled 	struct intrhand *ih = arg;
    259  1.2  garbled 	int irq = ih->ih_irq;
    260  1.2  garbled 	struct intr_source *is = &intrsources[irq];
    261  1.2  garbled 	struct intrhand **p, *q;
    262  1.2  garbled 
    263  1.2  garbled 	if (!LEGAL_VIRQ(irq))
    264  1.2  garbled 		panic("intr_disestablish: bogus irq %d", irq);
    265  1.2  garbled 
    266  1.2  garbled 	/*
    267  1.2  garbled 	 * Remove the handler from the chain.
    268  1.2  garbled 	 * This is O(n^2), too.
    269  1.2  garbled 	 */
    270  1.2  garbled 	for (p = &is->is_hand; (q = *p) != NULL && q != ih; p = &q->ih_next)
    271  1.2  garbled 		;
    272  1.2  garbled 	if (q)
    273  1.2  garbled 		*p = q->ih_next;
    274  1.2  garbled 	else
    275  1.2  garbled 		panic("intr_disestablish: handler not registered");
    276  1.2  garbled 	free((void *)ih, M_DEVBUF);
    277  1.2  garbled 
    278  1.2  garbled 	intr_calculatemasks();
    279  1.2  garbled 
    280  1.2  garbled 	if (is->is_hand == NULL) {
    281  1.2  garbled 		is->is_type = IST_NONE;
    282  1.2  garbled 		evcnt_detach(&is->is_ev);
    283  1.2  garbled 	}
    284  1.2  garbled }
    285  1.2  garbled 
    286  1.2  garbled /*
    287  1.2  garbled  * Map max_base irqs into 32 (bits).
    288  1.2  garbled  */
    289  1.2  garbled static int
    290  1.2  garbled mapirq(uint32_t irq)
    291  1.2  garbled {
    292  1.2  garbled 	struct pic_ops *pic;
    293  1.2  garbled 	int v;
    294  1.2  garbled 
    295  1.2  garbled 	if (irq >= max_base)
    296  1.2  garbled 		panic("invalid irq %d", irq);
    297  1.2  garbled 
    298  1.2  garbled 	if ((pic = find_pic_by_irq(irq)) == NULL)
    299  1.2  garbled 		panic("%s: cannot find PIC for IRQ %d", __func__, irq);
    300  1.2  garbled 
    301  1.2  garbled 	if (virq[irq])
    302  1.2  garbled 		return virq[irq];
    303  1.2  garbled 
    304  1.2  garbled 	virq_max++;
    305  1.2  garbled 	v = virq_max;
    306  1.2  garbled 	if (v > HWIRQ_MAX)
    307  1.2  garbled 		panic("virq overflow");
    308  1.2  garbled 
    309  1.2  garbled 	intrsources[v].is_hwirq = irq;
    310  1.2  garbled 	intrsources[v].is_pic = pic;
    311  1.2  garbled 	virq[irq] = v;
    312  1.2  garbled #ifdef PIC_DEBUG
    313  1.2  garbled 	printf("mapping irq %d to virq %d\n", irq, v);
    314  1.2  garbled #endif
    315  1.2  garbled 	return v;
    316  1.2  garbled }
    317  1.2  garbled 
    318  1.2  garbled static const char * const intr_typenames[] = {
    319  1.2  garbled    [IST_NONE]  = "none",
    320  1.2  garbled    [IST_PULSE] = "pulsed",
    321  1.2  garbled    [IST_EDGE]  = "edge-triggered",
    322  1.2  garbled    [IST_LEVEL] = "level-triggered",
    323  1.2  garbled };
    324  1.2  garbled 
    325  1.2  garbled const char *
    326  1.2  garbled intr_typename(int type)
    327  1.2  garbled {
    328  1.2  garbled 	KASSERT((unsigned int) type < __arraycount(intr_typenames));
    329  1.2  garbled 	KASSERT(intr_typenames[type] != NULL);
    330  1.2  garbled 	return intr_typenames[type];
    331  1.2  garbled }
    332  1.2  garbled 
    333  1.2  garbled /*
    334  1.2  garbled  * Recalculate the interrupt masks from scratch.
    335  1.2  garbled  * We could code special registry and deregistry versions of this function that
    336  1.2  garbled  * would be faster, but the code would be nastier, and we don't expect this to
    337  1.2  garbled  * happen very much anyway.
    338  1.2  garbled  */
    339  1.2  garbled static void
    340  1.2  garbled intr_calculatemasks(void)
    341  1.2  garbled {
    342  1.2  garbled 	struct intr_source *is;
    343  1.2  garbled 	struct intrhand *q;
    344  1.2  garbled 	struct pic_ops *current;
    345  1.2  garbled 	int irq, level, i, base;
    346  1.2  garbled 
    347  1.2  garbled 	/* First, figure out which levels each IRQ uses. */
    348  1.2  garbled 	for (irq = 0, is = intrsources; irq < NVIRQ; irq++, is++) {
    349  1.2  garbled 		register int levels = 0;
    350  1.2  garbled 		for (q = is->is_hand; q; q = q->ih_next)
    351  1.2  garbled 			levels |= 1 << q->ih_level;
    352  1.2  garbled 		is->is_level = levels;
    353  1.2  garbled 	}
    354  1.2  garbled 
    355  1.2  garbled 	/* Then figure out which IRQs use each level. */
    356  1.2  garbled 	for (level = 0; level < NIPL; level++) {
    357  1.2  garbled 		register int irqs = 0;
    358  1.2  garbled 		for (irq = 0, is = intrsources; irq < NVIRQ; irq++, is++)
    359  1.2  garbled 			if (is->is_level & (1 << level))
    360  1.2  garbled 				irqs |= 1 << irq;
    361  1.2  garbled 		imask[level] = irqs;
    362  1.2  garbled 	}
    363  1.2  garbled 
    364  1.2  garbled 	/*
    365  1.2  garbled 	 * IPL_CLOCK should mask clock interrupt even if interrupt handler
    366  1.2  garbled 	 * is not registered.
    367  1.2  garbled 	 */
    368  1.2  garbled 	imask[IPL_CLOCK] |= 1 << SPL_CLOCK;
    369  1.2  garbled 
    370  1.2  garbled 	/*
    371  1.2  garbled 	 * Initialize soft interrupt masks to block themselves.
    372  1.2  garbled 	 */
    373  1.2  garbled 	imask[IPL_SOFTCLOCK] = 1 << SIR_CLOCK;
    374  1.2  garbled 	imask[IPL_SOFTNET] = 1 << SIR_NET;
    375  1.2  garbled 	imask[IPL_SOFTSERIAL] = 1 << SIR_SERIAL;
    376  1.2  garbled 
    377  1.2  garbled 	/*
    378  1.2  garbled 	 * IPL_NONE is used for hardware interrupts that are never blocked,
    379  1.2  garbled 	 * and do not block anything else.
    380  1.2  garbled 	 */
    381  1.2  garbled 	imask[IPL_NONE] = 0;
    382  1.2  garbled 
    383  1.2  garbled #ifdef SLOPPY_IPLS
    384  1.2  garbled 	/*
    385  1.2  garbled 	 * Enforce a sloppy hierarchy as in spl(9)
    386  1.2  garbled 	 */
    387  1.2  garbled 	/* everything above softclock must block softclock */
    388  1.2  garbled 	for (i = IPL_SOFTCLOCK; i < NIPL; i++)
    389  1.2  garbled 		imask[i] |= imask[IPL_SOFTCLOCK];
    390  1.2  garbled 
    391  1.2  garbled 	/* everything above softnet must block softnet */
    392  1.2  garbled 	for (i = IPL_SOFTNET; i < NIPL; i++)
    393  1.2  garbled 		imask[i] |= imask[IPL_SOFTNET];
    394  1.2  garbled 
    395  1.2  garbled 	/* IPL_TTY must block softserial */
    396  1.2  garbled 	imask[IPL_TTY] |= imask[IPL_SOFTSERIAL];
    397  1.2  garbled 
    398  1.2  garbled 	/* IPL_VM must block net, block IO and tty */
    399  1.2  garbled 	imask[IPL_VM] |= (imask[IPL_NET] | imask[IPL_BIO] | imask[IPL_TTY]);
    400  1.2  garbled 
    401  1.2  garbled 	/* IPL_SERIAL must block IPL_TTY */
    402  1.2  garbled 	imask[IPL_SERIAL] |= imask[IPL_TTY];
    403  1.2  garbled 
    404  1.2  garbled 	/* IPL_HIGH must block all other priority levels */
    405  1.2  garbled 	for (i = IPL_NONE; i < IPL_HIGH; i++)
    406  1.2  garbled 		imask[IPL_HIGH] |= imask[i];
    407  1.2  garbled #else	/* !SLOPPY_IPLS */
    408  1.2  garbled 	/*
    409  1.2  garbled 	 * strict hierarchy - all IPLs block everything blocked by any lower
    410  1.2  garbled 	 * IPL
    411  1.2  garbled 	 */
    412  1.2  garbled 	for (i = 1; i < NIPL; i++)
    413  1.2  garbled 		imask[i] |= imask[i - 1];
    414  1.2  garbled #endif	/* !SLOPPY_IPLS */
    415  1.2  garbled 
    416  1.2  garbled #ifdef DEBUG_IPL
    417  1.2  garbled 	for (i = 0; i < NIPL; i++) {
    418  1.2  garbled 		printf("%2d: %08x\n", i, imask[i]);
    419  1.2  garbled 	}
    420  1.2  garbled #endif
    421  1.2  garbled 
    422  1.2  garbled 	/* And eventually calculate the complete masks. */
    423  1.2  garbled 	for (irq = 0, is = intrsources; irq < NVIRQ; irq++, is++) {
    424  1.2  garbled 		register int irqs = 1 << irq;
    425  1.2  garbled 		for (q = is->is_hand; q; q = q->ih_next)
    426  1.2  garbled 			irqs |= imask[q->ih_level];
    427  1.2  garbled 		is->is_mask = irqs;
    428  1.2  garbled 	}
    429  1.2  garbled 
    430  1.2  garbled 	/* Lastly, enable IRQs actually in use. */
    431  1.2  garbled 	for (base = 0; base < num_pics; base++) {
    432  1.2  garbled 		current = pics[base];
    433  1.2  garbled 		for (i = 0; i < current->pic_numintrs; i++)
    434  1.2  garbled 			current->pic_disable_irq(current, i);
    435  1.2  garbled 	}
    436  1.2  garbled 
    437  1.2  garbled 	for (irq = 0, is = intrsources; irq < NVIRQ; irq++, is++) {
    438  1.2  garbled 		if (is->is_hand)
    439  1.2  garbled 			pic_enable_irq(is->is_hwirq);
    440  1.2  garbled 	}
    441  1.2  garbled }
    442  1.2  garbled 
    443  1.2  garbled void
    444  1.2  garbled pic_enable_irq(int num)
    445  1.2  garbled {
    446  1.2  garbled 	struct pic_ops *current;
    447  1.2  garbled 	int type;
    448  1.2  garbled 
    449  1.2  garbled 	current = find_pic_by_irq(num);
    450  1.2  garbled 	if (current == NULL)
    451  1.2  garbled 		panic("%s: bogus IRQ %d", __func__, num);
    452  1.2  garbled 	type = intrsources[virq[num]].is_type;
    453  1.2  garbled 	current->pic_enable_irq(current, num - current->pic_intrbase, type);
    454  1.2  garbled }
    455  1.2  garbled 
    456  1.2  garbled void
    457  1.2  garbled pic_mark_pending(int irq)
    458  1.2  garbled {
    459  1.2  garbled 	struct cpu_info * const ci = curcpu();
    460  1.2  garbled 	int v, msr;
    461  1.2  garbled 
    462  1.2  garbled 	v = virq[irq];
    463  1.2  garbled 	if (v == 0)
    464  1.2  garbled 		printf("IRQ %d maps to 0\n", irq);
    465  1.2  garbled 
    466  1.2  garbled 	msr = mfmsr();
    467  1.2  garbled 	mtmsr(msr & ~PSL_EE);
    468  1.2  garbled 	ci->ci_ipending |= 1 << v;
    469  1.2  garbled 	mtmsr(msr);
    470  1.2  garbled }
    471  1.2  garbled 
    472  1.2  garbled void
    473  1.2  garbled pic_do_pending_int(void)
    474  1.2  garbled {
    475  1.2  garbled 	struct cpu_info * const ci = curcpu();
    476  1.2  garbled 	struct intr_source *is;
    477  1.2  garbled 	struct intrhand *ih;
    478  1.2  garbled 	struct pic_ops *pic;
    479  1.2  garbled 	int irq;
    480  1.2  garbled 	int pcpl;
    481  1.2  garbled 	int hwpend;
    482  1.2  garbled 	int emsr, dmsr;
    483  1.2  garbled 
    484  1.2  garbled 	if (ci->ci_iactive)
    485  1.2  garbled 		return;
    486  1.2  garbled 
    487  1.2  garbled 	ci->ci_iactive = 1;
    488  1.2  garbled 	emsr = mfmsr();
    489  1.2  garbled 	KASSERT(emsr & PSL_EE);
    490  1.2  garbled 	dmsr = emsr & ~PSL_EE;
    491  1.2  garbled 	mtmsr(dmsr);
    492  1.2  garbled 
    493  1.2  garbled 	pcpl = ci->ci_cpl;
    494  1.2  garbled again:
    495  1.2  garbled 
    496  1.2  garbled 	/* Do now unmasked pendings */
    497  1.2  garbled 	while ((hwpend = (ci->ci_ipending & ~pcpl & HWIRQ_MASK)) != 0) {
    498  1.2  garbled 		irq = 31 - cntlzw(hwpend);
    499  1.2  garbled 		KASSERT(irq <= virq_max);
    500  1.2  garbled 		ci->ci_ipending &= ~(1 << irq);
    501  1.2  garbled 		if (irq == 0) {
    502  1.2  garbled 			printf("VIRQ0");
    503  1.2  garbled 			continue;
    504  1.2  garbled 		}
    505  1.2  garbled 		is = &intrsources[irq];
    506  1.2  garbled 		pic = is->is_pic;
    507  1.2  garbled 
    508  1.2  garbled 		splraise(is->is_mask);
    509  1.2  garbled 		mtmsr(emsr);
    510  1.2  garbled 		KERNEL_LOCK(1, NULL);
    511  1.2  garbled 		ih = is->is_hand;
    512  1.2  garbled 		while (ih) {
    513  1.2  garbled #ifdef DIAGNOSTIC
    514  1.2  garbled 			if (!ih->ih_fun) {
    515  1.2  garbled 				printf("NULL interrupt handler!\n");
    516  1.2  garbled 				panic("irq %02d, hwirq %02d, is %p\n",
    517  1.2  garbled 					irq, is->is_hwirq, is);
    518  1.2  garbled 			}
    519  1.2  garbled #endif
    520  1.2  garbled 			(*ih->ih_fun)(ih->ih_arg);
    521  1.2  garbled 			ih = ih->ih_next;
    522  1.2  garbled 		}
    523  1.2  garbled 		KERNEL_UNLOCK_ONE(NULL);
    524  1.2  garbled 		mtmsr(dmsr);
    525  1.2  garbled 		ci->ci_cpl = pcpl;
    526  1.2  garbled 
    527  1.2  garbled 		is->is_ev.ev_count++;
    528  1.2  garbled 		pic->pic_reenable_irq(pic, is->is_hwirq - pic->pic_intrbase,
    529  1.2  garbled 		    is->is_type);
    530  1.2  garbled 	}
    531  1.2  garbled 
    532  1.2  garbled 	if ((ci->ci_ipending & ~pcpl) & (1 << SIR_SERIAL)) {
    533  1.2  garbled 		ci->ci_ipending &= ~(1 << SIR_SERIAL);
    534  1.2  garbled 		splsoftserial();
    535  1.2  garbled 		mtmsr(emsr);
    536  1.2  garbled 		KERNEL_LOCK(1, NULL);
    537  1.2  garbled 
    538  1.2  garbled 		softintr__run(IPL_SOFTSERIAL);
    539  1.2  garbled 
    540  1.2  garbled 		KERNEL_UNLOCK_ONE(NULL);
    541  1.2  garbled 		mtmsr(dmsr);
    542  1.2  garbled 		ci->ci_cpl = pcpl;
    543  1.2  garbled 		ci->ci_ev_softserial.ev_count++;
    544  1.2  garbled 		goto again;
    545  1.2  garbled 	}
    546  1.2  garbled 	if ((ci->ci_ipending & ~pcpl) & (1 << SIR_NET)) {
    547  1.2  garbled 
    548  1.2  garbled 		ci->ci_ipending &= ~(1 << SIR_NET);
    549  1.2  garbled 		splsoftnet();
    550  1.2  garbled 
    551  1.2  garbled 		mtmsr(emsr);
    552  1.2  garbled 		KERNEL_LOCK(1, NULL);
    553  1.2  garbled 
    554  1.2  garbled 		softintr__run(IPL_SOFTNET);
    555  1.2  garbled 
    556  1.2  garbled 		KERNEL_UNLOCK_ONE(NULL);
    557  1.2  garbled 		mtmsr(dmsr);
    558  1.2  garbled 		ci->ci_cpl = pcpl;
    559  1.2  garbled 		ci->ci_ev_softnet.ev_count++;
    560  1.2  garbled 		goto again;
    561  1.2  garbled 	}
    562  1.2  garbled 	if ((ci->ci_ipending & ~pcpl) & (1 << SIR_CLOCK)) {
    563  1.2  garbled 		ci->ci_ipending &= ~(1 << SIR_CLOCK);
    564  1.2  garbled 		splsoftclock();
    565  1.2  garbled 		mtmsr(emsr);
    566  1.2  garbled 		KERNEL_LOCK(1, NULL);
    567  1.2  garbled 
    568  1.2  garbled 		softintr__run(IPL_SOFTCLOCK);
    569  1.2  garbled 
    570  1.2  garbled 		KERNEL_UNLOCK_ONE(NULL);
    571  1.2  garbled 		mtmsr(dmsr);
    572  1.2  garbled 		ci->ci_cpl = pcpl;
    573  1.2  garbled 		ci->ci_ev_softclock.ev_count++;
    574  1.2  garbled 		goto again;
    575  1.2  garbled 	}
    576  1.2  garbled 
    577  1.2  garbled 	ci->ci_cpl = pcpl;	/* Don't use splx... we are here already! */
    578  1.2  garbled 	ci->ci_iactive = 0;
    579  1.2  garbled 	mtmsr(emsr);
    580  1.2  garbled }
    581  1.2  garbled 
    582  1.2  garbled int
    583  1.2  garbled pic_handle_intr(void *cookie)
    584  1.2  garbled {
    585  1.2  garbled 	struct pic_ops *pic = cookie;
    586  1.2  garbled 	struct cpu_info *ci = curcpu();
    587  1.2  garbled 	struct intr_source *is;
    588  1.2  garbled 	struct intrhand *ih;
    589  1.2  garbled 	int irq, realirq;
    590  1.2  garbled 	int pcpl, msr, r_imen, bail;
    591  1.2  garbled 
    592  1.2  garbled 	realirq = pic->pic_get_irq(pic);
    593  1.2  garbled 	if (realirq == 255)
    594  1.2  garbled 		return 0;
    595  1.2  garbled 
    596  1.2  garbled 	msr = mfmsr();
    597  1.2  garbled 	pcpl = ci->ci_cpl;
    598  1.2  garbled 
    599  1.2  garbled start:
    600  1.2  garbled 
    601  1.2  garbled #ifdef MULTIPROCESSOR
    602  1.2  garbled 	/* THIS IS WRONG XXX */
    603  1.2  garbled 	while (realirq == ipiops.ppc_ipi_vector) {
    604  1.2  garbled 		ppcipi_intr(NULL);
    605  1.2  garbled 		pic->pic_ack_irq(pic, realirq);
    606  1.2  garbled 		realirq = pic->pic_get_irq(pic);
    607  1.2  garbled 	}
    608  1.2  garbled 	if (realirq == 255) {
    609  1.2  garbled 		return 0;
    610  1.2  garbled 	}
    611  1.2  garbled #endif
    612  1.2  garbled 
    613  1.2  garbled 	irq = virq[realirq + pic->pic_intrbase];
    614  1.2  garbled #ifdef PIC_DEBUG
    615  1.2  garbled 	if (irq == 0) {
    616  1.2  garbled 		printf("%s: %d virq 0\n", pic->pic_name, realirq);
    617  1.2  garbled 		goto boo;
    618  1.2  garbled 	}
    619  1.2  garbled #endif /* PIC_DEBUG */
    620  1.2  garbled 	KASSERT(realirq < pic->pic_numintrs);
    621  1.2  garbled 	r_imen = 1 << irq;
    622  1.2  garbled 	is = &intrsources[irq];
    623  1.2  garbled 
    624  1.2  garbled 	if ((pcpl & r_imen) != 0) {
    625  1.2  garbled 
    626  1.2  garbled 		ci->ci_ipending |= r_imen; /* Masked! Mark this as pending */
    627  1.2  garbled 		pic->pic_disable_irq(pic, realirq);
    628  1.2  garbled 	} else {
    629  1.2  garbled 
    630  1.2  garbled 		/* this interrupt is no longer pending */
    631  1.2  garbled 		ci->ci_ipending &= ~r_imen;
    632  1.2  garbled 
    633  1.2  garbled 		splraise(is->is_mask);
    634  1.2  garbled 		mtmsr(msr | PSL_EE);
    635  1.2  garbled 		KERNEL_LOCK(1, NULL);
    636  1.2  garbled 		ih = is->is_hand;
    637  1.2  garbled 		bail = 0;
    638  1.2  garbled 		while ((ih != NULL) && (bail < 10)) {
    639  1.2  garbled 			if (ih->ih_fun == NULL)
    640  1.2  garbled 				panic("bogus handler for IRQ %s %d",
    641  1.2  garbled 				    pic->pic_name, realirq);
    642  1.2  garbled 			(*ih->ih_fun)(ih->ih_arg);
    643  1.2  garbled 			ih = ih->ih_next;
    644  1.2  garbled 			bail++;
    645  1.2  garbled 		}
    646  1.2  garbled 		KERNEL_UNLOCK_ONE(NULL);
    647  1.2  garbled 		mtmsr(msr);
    648  1.2  garbled 		ci->ci_cpl = pcpl;
    649  1.2  garbled 
    650  1.2  garbled 		uvmexp.intrs++;
    651  1.2  garbled 		is->is_ev.ev_count++;
    652  1.2  garbled 	}
    653  1.2  garbled #ifdef PIC_DEBUG
    654  1.2  garbled boo:
    655  1.2  garbled #endif /* PIC_DEBUG */
    656  1.2  garbled 	pic->pic_ack_irq(pic, realirq);
    657  1.2  garbled 	realirq = pic->pic_get_irq(pic);
    658  1.2  garbled 	if (realirq != 255)
    659  1.2  garbled 		goto start;
    660  1.2  garbled 
    661  1.2  garbled 	mtmsr(msr | PSL_EE);
    662  1.2  garbled 	splx(pcpl);	/* Process pendings. */
    663  1.2  garbled 	mtmsr(msr);
    664  1.2  garbled 
    665  1.2  garbled 	return 0;
    666  1.2  garbled }
    667  1.2  garbled 
    668  1.2  garbled void
    669  1.2  garbled pic_ext_intr(void)
    670  1.2  garbled {
    671  1.2  garbled 
    672  1.2  garbled 	KASSERT(pics[primary_pic] != NULL);
    673  1.2  garbled 	pic_handle_intr(pics[primary_pic]);
    674  1.2  garbled 
    675  1.2  garbled 	return;
    676  1.2  garbled 
    677  1.2  garbled }
    678  1.2  garbled 
    679  1.2  garbled int
    680  1.2  garbled splraise(int ncpl)
    681  1.2  garbled {
    682  1.2  garbled 	struct cpu_info *ci = curcpu();
    683  1.2  garbled 	int ocpl;
    684  1.2  garbled 
    685  1.2  garbled 	__asm volatile("sync; eieio");	/* don't reorder.... */
    686  1.2  garbled 
    687  1.2  garbled 	ocpl = ci->ci_cpl;
    688  1.2  garbled 	ci->ci_cpl = ocpl | ncpl;
    689  1.2  garbled 	__asm volatile("sync; eieio");	/* reorder protect */
    690  1.2  garbled 	return ocpl;
    691  1.2  garbled }
    692  1.2  garbled 
    693  1.2  garbled void
    694  1.2  garbled splx(int ncpl)
    695  1.2  garbled {
    696  1.2  garbled 	struct cpu_info *ci = curcpu();
    697  1.2  garbled 
    698  1.2  garbled 	__asm volatile("sync; eieio");	/* reorder protect */
    699  1.2  garbled 	ci->ci_cpl = ncpl;
    700  1.2  garbled 	if (ci->ci_ipending & ~ncpl)
    701  1.2  garbled 		pic_do_pending_int();
    702  1.2  garbled 	__asm volatile("sync; eieio");	/* reorder protect */
    703  1.2  garbled }
    704  1.2  garbled 
    705  1.2  garbled int
    706  1.2  garbled spllower(int ncpl)
    707  1.2  garbled {
    708  1.2  garbled 	struct cpu_info *ci = curcpu();
    709  1.2  garbled 	int ocpl;
    710  1.2  garbled 
    711  1.2  garbled 	__asm volatile("sync; eieio");	/* reorder protect */
    712  1.2  garbled 	ocpl = ci->ci_cpl;
    713  1.2  garbled 	ci->ci_cpl = ncpl;
    714  1.2  garbled 	if (ci->ci_ipending & ~ncpl)
    715  1.2  garbled 		pic_do_pending_int();
    716  1.2  garbled 	__asm volatile("sync; eieio");	/* reorder protect */
    717  1.2  garbled 	return ocpl;
    718  1.2  garbled }
    719  1.2  garbled 
    720  1.2  garbled /* Following code should be implemented with lwarx/stwcx to avoid
    721  1.2  garbled  * the disable/enable. i need to read the manual once more.... */
    722  1.2  garbled void
    723  1.2  garbled softintr(int ipl)
    724  1.2  garbled {
    725  1.2  garbled 	int msrsave;
    726  1.2  garbled 
    727  1.2  garbled 	msrsave = mfmsr();
    728  1.2  garbled 	mtmsr(msrsave & ~PSL_EE);
    729  1.2  garbled 	curcpu()->ci_ipending |= 1 << ipl;
    730  1.2  garbled 	mtmsr(msrsave);
    731  1.2  garbled }
    732  1.2  garbled 
    733  1.2  garbled void
    734  1.2  garbled genppc_cpu_configure(void)
    735  1.2  garbled {
    736  1.2  garbled 	aprint_normal("biomask %x netmask %x ttymask %x\n",
    737  1.2  garbled 	    imask[IPL_BIO] & 0x1fffffff,
    738  1.2  garbled 	    imask[IPL_NET] & 0x1fffffff,
    739  1.2  garbled 	    imask[IPL_TTY] & 0x1fffffff);
    740  1.2  garbled 
    741  1.2  garbled 	spl0();
    742  1.2  garbled }
    743  1.2  garbled 
    744  1.2  garbled #if defined(PIC_PREPIVR) || defined(PIC_I8259)
    745  1.2  garbled /*
    746  1.2  garbled  * isa_intr_alloc needs to be done here, because it needs direct access to
    747  1.2  garbled  * the various interrupt handler structures.
    748  1.2  garbled  */
    749  1.2  garbled 
    750  1.2  garbled int
    751  1.2  garbled genppc_isa_intr_alloc(isa_chipset_tag_t ic, struct pic_ops *pic,
    752  1.2  garbled     int mask, int type, int *irq_p)
    753  1.2  garbled {
    754  1.2  garbled 	int irq, vi;
    755  1.2  garbled 	int maybe_irq = -1;
    756  1.2  garbled 	int shared_depth = 0;
    757  1.2  garbled 	struct intr_source *is;
    758  1.2  garbled 
    759  1.2  garbled 	if (pic == NULL)
    760  1.2  garbled 		return 1;
    761  1.2  garbled 
    762  1.2  garbled 	for (irq = 0; (mask != 0 && irq < pic->pic_numintrs);
    763  1.2  garbled 	     mask >>= 1, irq++) {
    764  1.2  garbled 		if ((mask & 1) == 0)
    765  1.2  garbled 			continue;
    766  1.2  garbled 		vi = virq[irq + pic->pic_intrbase];
    767  1.2  garbled 		if (!vi) {
    768  1.2  garbled 			*irq_p = irq;
    769  1.2  garbled 			return 0;
    770  1.2  garbled 		}
    771  1.2  garbled 		is = &intrsources[vi];
    772  1.2  garbled 		if (is->is_type == IST_NONE) {
    773  1.2  garbled 			*irq_p = irq;
    774  1.2  garbled 			return 0;
    775  1.2  garbled 		}
    776  1.2  garbled 		/* Level interrupts can be shared */
    777  1.2  garbled 		if (type == IST_LEVEL && is->is_type == IST_LEVEL) {
    778  1.2  garbled 			struct intrhand *ih = is->is_hand;
    779  1.2  garbled 			int depth;
    780  1.2  garbled 
    781  1.2  garbled 			if (maybe_irq == -1) {
    782  1.2  garbled 				maybe_irq = irq;
    783  1.2  garbled 				continue;
    784  1.2  garbled 			}
    785  1.2  garbled 			for (depth = 0; ih != NULL; ih = ih->ih_next)
    786  1.2  garbled 				depth++;
    787  1.2  garbled 			if (depth < shared_depth) {
    788  1.2  garbled 				maybe_irq = irq;
    789  1.2  garbled 				shared_depth = depth;
    790  1.2  garbled 			}
    791  1.2  garbled 		}
    792  1.2  garbled 	}
    793  1.2  garbled 	if (maybe_irq != -1) {
    794  1.2  garbled 		*irq_p = maybe_irq;
    795  1.2  garbled 		return 0;
    796  1.2  garbled 	}
    797  1.2  garbled 	return 1;
    798  1.2  garbled }
    799  1.2  garbled #endif
    800