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