Home | History | Annotate | Line # | Download | only in x86
xen_intr.c revision 1.9.64.1
      1  1.9.64.1  pgoyette /*	$NetBSD: xen_intr.c,v 1.9.64.1 2018/12/26 14:01:46 pgoyette Exp $	*/
      2       1.2    bouyer 
      3       1.2    bouyer /*-
      4       1.2    bouyer  * Copyright (c) 1998, 2001 The NetBSD Foundation, Inc.
      5       1.2    bouyer  * All rights reserved.
      6       1.2    bouyer  *
      7       1.2    bouyer  * This code is derived from software contributed to The NetBSD Foundation
      8       1.2    bouyer  * by Charles M. Hannum, and by Jason R. Thorpe.
      9       1.2    bouyer  *
     10       1.2    bouyer  * Redistribution and use in source and binary forms, with or without
     11       1.2    bouyer  * modification, are permitted provided that the following conditions
     12       1.2    bouyer  * are met:
     13       1.2    bouyer  * 1. Redistributions of source code must retain the above copyright
     14       1.2    bouyer  *    notice, this list of conditions and the following disclaimer.
     15       1.2    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.2    bouyer  *    notice, this list of conditions and the following disclaimer in the
     17       1.2    bouyer  *    documentation and/or other materials provided with the distribution.
     18       1.2    bouyer  *
     19       1.2    bouyer  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.2    bouyer  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.2    bouyer  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.2    bouyer  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.2    bouyer  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.2    bouyer  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.2    bouyer  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.2    bouyer  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.2    bouyer  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.2    bouyer  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.2    bouyer  * POSSIBILITY OF SUCH DAMAGE.
     30       1.2    bouyer  */
     31       1.2    bouyer 
     32       1.2    bouyer #include <sys/cdefs.h>
     33  1.9.64.1  pgoyette __KERNEL_RCSID(0, "$NetBSD: xen_intr.c,v 1.9.64.1 2018/12/26 14:01:46 pgoyette Exp $");
     34       1.2    bouyer 
     35       1.2    bouyer #include <sys/param.h>
     36  1.9.64.1  pgoyette #include <sys/kernel.h>
     37  1.9.64.1  pgoyette #include <sys/kmem.h>
     38  1.9.64.1  pgoyette 
     39  1.9.64.1  pgoyette #include <sys/cpu.h>
     40  1.9.64.1  pgoyette 
     41  1.9.64.1  pgoyette #include <xen/evtchn.h>
     42  1.9.64.1  pgoyette 
     43  1.9.64.1  pgoyette #include <uvm/uvm.h>
     44       1.2    bouyer 
     45       1.2    bouyer #include <machine/cpu.h>
     46       1.2    bouyer #include <machine/intr.h>
     47       1.2    bouyer 
     48  1.9.64.1  pgoyette #include "acpica.h"
     49  1.9.64.1  pgoyette #include "ioapic.h"
     50  1.9.64.1  pgoyette #include "lapic.h"
     51  1.9.64.1  pgoyette #include "pci.h"
     52  1.9.64.1  pgoyette 
     53  1.9.64.1  pgoyette #if NACPICA > 0
     54  1.9.64.1  pgoyette #include <dev/acpi/acpivar.h>
     55  1.9.64.1  pgoyette #endif
     56  1.9.64.1  pgoyette 
     57  1.9.64.1  pgoyette #if NIOAPIC > 0 || NACPICA > 0
     58  1.9.64.1  pgoyette #include <machine/i82093var.h>
     59  1.9.64.1  pgoyette #endif
     60  1.9.64.1  pgoyette 
     61  1.9.64.1  pgoyette #if NLAPIC > 0
     62  1.9.64.1  pgoyette #include <machine/i82489var.h>
     63  1.9.64.1  pgoyette #endif
     64  1.9.64.1  pgoyette 
     65  1.9.64.1  pgoyette #if NPCI > 0
     66  1.9.64.1  pgoyette #include <dev/pci/ppbreg.h>
     67  1.9.64.1  pgoyette #endif
     68  1.9.64.1  pgoyette 
     69  1.9.64.1  pgoyette void xen_disable_intr(void);
     70  1.9.64.1  pgoyette void xen_enable_intr(void);
     71  1.9.64.1  pgoyette u_long xen_read_psl(void);
     72  1.9.64.1  pgoyette void xen_write_psl(u_long);
     73  1.9.64.1  pgoyette 
     74       1.2    bouyer /*
     75       1.2    bouyer  * Add a mask to cpl, and return the old value of cpl.
     76       1.2    bouyer  */
     77       1.2    bouyer int
     78       1.2    bouyer splraise(int nlevel)
     79       1.2    bouyer {
     80       1.2    bouyer 	int olevel;
     81       1.2    bouyer 	struct cpu_info *ci = curcpu();
     82       1.2    bouyer 
     83       1.2    bouyer 	olevel = ci->ci_ilevel;
     84       1.2    bouyer 	if (nlevel > olevel)
     85       1.2    bouyer 		ci->ci_ilevel = nlevel;
     86       1.2    bouyer 	__insn_barrier();
     87       1.2    bouyer 	return (olevel);
     88       1.2    bouyer }
     89       1.2    bouyer 
     90       1.2    bouyer /*
     91       1.2    bouyer  * Restore a value to cpl (unmasking interrupts).  If any unmasked
     92       1.2    bouyer  * interrupts are pending, call Xspllower() to process them.
     93       1.2    bouyer  */
     94       1.2    bouyer void
     95       1.2    bouyer spllower(int nlevel)
     96       1.2    bouyer {
     97       1.2    bouyer 	struct cpu_info *ci = curcpu();
     98  1.9.64.1  pgoyette 	uint32_t xmask;
     99       1.2    bouyer 	u_long psl;
    100       1.2    bouyer 
    101       1.8    bouyer 	if (ci->ci_ilevel <= nlevel)
    102       1.8    bouyer 		return;
    103       1.8    bouyer 
    104       1.2    bouyer 	__insn_barrier();
    105       1.2    bouyer 
    106  1.9.64.1  pgoyette 	xmask = XUNMASK(ci, nlevel);
    107  1.9.64.1  pgoyette 	psl = xen_read_psl();
    108  1.9.64.1  pgoyette 	xen_disable_intr();
    109  1.9.64.1  pgoyette 	if (ci->ci_xpending & xmask) {
    110       1.7    bouyer 		KASSERT(psl == 0);
    111       1.2    bouyer 		Xspllower(nlevel);
    112       1.2    bouyer 		/* Xspllower does enable_intr() */
    113       1.2    bouyer 	} else {
    114       1.2    bouyer 		ci->ci_ilevel = nlevel;
    115  1.9.64.1  pgoyette 		xen_write_psl(psl);
    116       1.2    bouyer 	}
    117       1.2    bouyer }
    118       1.2    bouyer 
    119       1.2    bouyer void
    120  1.9.64.1  pgoyette xen_disable_intr(void)
    121       1.2    bouyer {
    122       1.2    bouyer 	__cli();
    123       1.2    bouyer }
    124       1.2    bouyer 
    125       1.2    bouyer void
    126  1.9.64.1  pgoyette xen_enable_intr(void)
    127       1.2    bouyer {
    128       1.2    bouyer 	__sti();
    129       1.2    bouyer }
    130       1.2    bouyer 
    131       1.2    bouyer u_long
    132  1.9.64.1  pgoyette xen_read_psl(void)
    133       1.2    bouyer {
    134       1.2    bouyer 
    135       1.4    cegger 	return (curcpu()->ci_vcpu->evtchn_upcall_mask);
    136       1.2    bouyer }
    137       1.2    bouyer 
    138       1.2    bouyer void
    139  1.9.64.1  pgoyette xen_write_psl(u_long psl)
    140       1.2    bouyer {
    141       1.4    cegger 	struct cpu_info *ci = curcpu();
    142       1.2    bouyer 
    143       1.4    cegger 	ci->ci_vcpu->evtchn_upcall_mask = psl;
    144       1.9       jym 	xen_rmb();
    145       1.4    cegger 	if (ci->ci_vcpu->evtchn_upcall_pending && psl == 0) {
    146       1.2    bouyer 	    	hypervisor_force_callback();
    147       1.2    bouyer 	}
    148       1.2    bouyer }
    149  1.9.64.1  pgoyette 
    150  1.9.64.1  pgoyette void *
    151  1.9.64.1  pgoyette xen_intr_establish(int legacy_irq, struct pic *pic, int pin,
    152  1.9.64.1  pgoyette     int type, int level, int (*handler)(void *), void *arg,
    153  1.9.64.1  pgoyette     bool known_mpsafe)
    154  1.9.64.1  pgoyette {
    155  1.9.64.1  pgoyette 
    156  1.9.64.1  pgoyette 	return xen_intr_establish_xname(legacy_irq, pic, pin, type, level,
    157  1.9.64.1  pgoyette 	    handler, arg, known_mpsafe, "XEN");
    158  1.9.64.1  pgoyette }
    159  1.9.64.1  pgoyette 
    160  1.9.64.1  pgoyette void *
    161  1.9.64.1  pgoyette xen_intr_establish_xname(int legacy_irq, struct pic *pic, int pin,
    162  1.9.64.1  pgoyette     int type, int level, int (*handler)(void *), void *arg,
    163  1.9.64.1  pgoyette     bool known_mpsafe, const char *xname)
    164  1.9.64.1  pgoyette {
    165  1.9.64.1  pgoyette 	const char *intrstr;
    166  1.9.64.1  pgoyette 	char intrstr_buf[INTRIDBUF];
    167  1.9.64.1  pgoyette 
    168  1.9.64.1  pgoyette 	if (pic->pic_type == PIC_XEN) {
    169  1.9.64.1  pgoyette 		struct intrhand *rih;
    170  1.9.64.1  pgoyette 
    171  1.9.64.1  pgoyette 		/*
    172  1.9.64.1  pgoyette 		 * event_set_handler interprets `level != IPL_VM' to
    173  1.9.64.1  pgoyette 		 * mean MP-safe, so we require the caller to match that
    174  1.9.64.1  pgoyette 		 * for the moment.
    175  1.9.64.1  pgoyette 		 */
    176  1.9.64.1  pgoyette 		KASSERT(known_mpsafe == (level != IPL_VM));
    177  1.9.64.1  pgoyette 
    178  1.9.64.1  pgoyette 		intrstr = intr_create_intrid(legacy_irq, pic, pin, intrstr_buf,
    179  1.9.64.1  pgoyette 		    sizeof(intrstr_buf));
    180  1.9.64.1  pgoyette 
    181  1.9.64.1  pgoyette 		event_set_handler(pin, handler, arg, level, intrstr, xname);
    182  1.9.64.1  pgoyette 
    183  1.9.64.1  pgoyette 		rih = kmem_zalloc(sizeof(*rih), cold ? KM_NOSLEEP : KM_SLEEP);
    184  1.9.64.1  pgoyette 		if (rih == NULL) {
    185  1.9.64.1  pgoyette 			printf("%s: can't allocate handler info\n", __func__);
    186  1.9.64.1  pgoyette 			return NULL;
    187  1.9.64.1  pgoyette 		}
    188  1.9.64.1  pgoyette 
    189  1.9.64.1  pgoyette 		/*
    190  1.9.64.1  pgoyette 		 * XXX:
    191  1.9.64.1  pgoyette 		 * This is just a copy for API conformance.
    192  1.9.64.1  pgoyette 		 * The real ih is lost in the innards of
    193  1.9.64.1  pgoyette 		 * event_set_handler(); where the details of
    194  1.9.64.1  pgoyette 		 * biglock_wrapper etc are taken care of.
    195  1.9.64.1  pgoyette 		 * All that goes away when we nuke event_set_handler()
    196  1.9.64.1  pgoyette 		 * et. al. and unify with x86/intr.c
    197  1.9.64.1  pgoyette 		 */
    198  1.9.64.1  pgoyette 		rih->ih_pin = pin; /* port */
    199  1.9.64.1  pgoyette 		rih->ih_fun = rih->ih_realfun = handler;
    200  1.9.64.1  pgoyette 		rih->ih_arg = rih->ih_realarg = arg;
    201  1.9.64.1  pgoyette 		rih->pic_type = pic->pic_type;
    202  1.9.64.1  pgoyette 		return rih;
    203  1.9.64.1  pgoyette 	} 	/* Else we assume pintr */
    204  1.9.64.1  pgoyette 
    205  1.9.64.1  pgoyette #if NPCI > 0 || NISA > 0
    206  1.9.64.1  pgoyette 	struct pintrhand *pih;
    207  1.9.64.1  pgoyette 	int gsi;
    208  1.9.64.1  pgoyette 	int vector, evtchn;
    209  1.9.64.1  pgoyette 
    210  1.9.64.1  pgoyette 	KASSERTMSG(legacy_irq == -1 || (0 <= legacy_irq && legacy_irq < NUM_XEN_IRQS),
    211  1.9.64.1  pgoyette 	    "bad legacy IRQ value: %d", legacy_irq);
    212  1.9.64.1  pgoyette 	KASSERTMSG(!(legacy_irq == -1 && pic == &i8259_pic),
    213  1.9.64.1  pgoyette 	    "non-legacy IRQon i8259 ");
    214  1.9.64.1  pgoyette 
    215  1.9.64.1  pgoyette 	gsi = xen_pic_to_gsi(pic, pin);
    216  1.9.64.1  pgoyette 
    217  1.9.64.1  pgoyette 	intrstr = intr_create_intrid(gsi, pic, pin, intrstr_buf,
    218  1.9.64.1  pgoyette 	    sizeof(intrstr_buf));
    219  1.9.64.1  pgoyette 
    220  1.9.64.1  pgoyette 	vector = xen_vec_alloc(gsi);
    221  1.9.64.1  pgoyette 
    222  1.9.64.1  pgoyette 	if (irq2port[gsi] == 0) {
    223  1.9.64.1  pgoyette 		extern struct cpu_info phycpu_info_primary; /* XXX */
    224  1.9.64.1  pgoyette 		struct cpu_info *ci = &phycpu_info_primary;
    225  1.9.64.1  pgoyette 
    226  1.9.64.1  pgoyette 		pic->pic_addroute(pic, ci, pin, vector, type);
    227  1.9.64.1  pgoyette 
    228  1.9.64.1  pgoyette 		evtchn = bind_pirq_to_evtch(gsi);
    229  1.9.64.1  pgoyette 		KASSERT(evtchn > 0);
    230  1.9.64.1  pgoyette 		KASSERT(evtchn < NR_EVENT_CHANNELS);
    231  1.9.64.1  pgoyette 		irq2port[gsi] = evtchn + 1;
    232  1.9.64.1  pgoyette 		xen_atomic_set_bit(&ci->ci_evtmask[0], evtchn);
    233  1.9.64.1  pgoyette 	} else {
    234  1.9.64.1  pgoyette 		/*
    235  1.9.64.1  pgoyette 		 * Shared interrupt - we can't rebind.
    236  1.9.64.1  pgoyette 		 * The port is shared instead.
    237  1.9.64.1  pgoyette 		 */
    238  1.9.64.1  pgoyette 		evtchn = irq2port[gsi] - 1;
    239  1.9.64.1  pgoyette 	}
    240  1.9.64.1  pgoyette 
    241  1.9.64.1  pgoyette 	pih = pirq_establish(gsi, evtchn, handler, arg, level,
    242  1.9.64.1  pgoyette 			     intrstr, xname);
    243  1.9.64.1  pgoyette 	pih->pic_type = pic->pic_type;
    244  1.9.64.1  pgoyette 	return pih;
    245  1.9.64.1  pgoyette #endif /* NPCI > 0 || NISA > 0 */
    246  1.9.64.1  pgoyette 
    247  1.9.64.1  pgoyette 	/* FALLTHROUGH */
    248  1.9.64.1  pgoyette 	return NULL;
    249  1.9.64.1  pgoyette }
    250  1.9.64.1  pgoyette 
    251  1.9.64.1  pgoyette /*
    252  1.9.64.1  pgoyette  * Deregister an interrupt handler.
    253  1.9.64.1  pgoyette  */
    254  1.9.64.1  pgoyette void
    255  1.9.64.1  pgoyette xen_intr_disestablish(struct intrhand *ih)
    256  1.9.64.1  pgoyette {
    257  1.9.64.1  pgoyette 
    258  1.9.64.1  pgoyette 	if (ih->pic_type == PIC_XEN) {
    259  1.9.64.1  pgoyette 		event_remove_handler(ih->ih_pin, ih->ih_realfun,
    260  1.9.64.1  pgoyette 		    ih->ih_realarg);
    261  1.9.64.1  pgoyette 		kmem_free(ih, sizeof(*ih));
    262  1.9.64.1  pgoyette 		return;
    263  1.9.64.1  pgoyette 	}
    264  1.9.64.1  pgoyette #if defined(DOM0OPS)
    265  1.9.64.1  pgoyette 	/*
    266  1.9.64.1  pgoyette 	 * Cache state, to prevent a use after free situation with
    267  1.9.64.1  pgoyette 	 * ih.
    268  1.9.64.1  pgoyette 	 */
    269  1.9.64.1  pgoyette 
    270  1.9.64.1  pgoyette 	struct pintrhand *pih = (struct pintrhand *)ih;
    271  1.9.64.1  pgoyette 
    272  1.9.64.1  pgoyette 	int pirq = pih->pirq;
    273  1.9.64.1  pgoyette 	int port = pih->evtch;
    274  1.9.64.1  pgoyette 	KASSERT(irq2port[pirq] != 0);
    275  1.9.64.1  pgoyette 
    276  1.9.64.1  pgoyette 	pirq_disestablish(pih);
    277  1.9.64.1  pgoyette 
    278  1.9.64.1  pgoyette 	if (evtsource[port] == NULL) {
    279  1.9.64.1  pgoyette 			/*
    280  1.9.64.1  pgoyette 			 * Last handler was removed by
    281  1.9.64.1  pgoyette 			 * event_remove_handler().
    282  1.9.64.1  pgoyette 			 *
    283  1.9.64.1  pgoyette 			 * We can safely unbind the pirq now.
    284  1.9.64.1  pgoyette 			 */
    285  1.9.64.1  pgoyette 
    286  1.9.64.1  pgoyette 			port = unbind_pirq_from_evtch(pirq);
    287  1.9.64.1  pgoyette 			KASSERT(port == pih->evtch);
    288  1.9.64.1  pgoyette 			irq2port[pirq] = 0;
    289  1.9.64.1  pgoyette 	}
    290  1.9.64.1  pgoyette #endif
    291  1.9.64.1  pgoyette 	return;
    292  1.9.64.1  pgoyette }
    293  1.9.64.1  pgoyette 
    294  1.9.64.1  pgoyette /* MI interface for kern_cpu.c */
    295  1.9.64.1  pgoyette void xen_cpu_intr_redistribute(void);
    296  1.9.64.1  pgoyette 
    297  1.9.64.1  pgoyette void
    298  1.9.64.1  pgoyette xen_cpu_intr_redistribute(void)
    299  1.9.64.1  pgoyette {
    300  1.9.64.1  pgoyette 	KASSERT(mutex_owned(&cpu_lock));
    301  1.9.64.1  pgoyette 	KASSERT(mp_online);
    302  1.9.64.1  pgoyette 
    303  1.9.64.1  pgoyette 	return;
    304  1.9.64.1  pgoyette }
    305  1.9.64.1  pgoyette 
    306  1.9.64.1  pgoyette /* MD - called by x86/cpu.c */
    307  1.9.64.1  pgoyette #if defined(INTRSTACKSIZE)
    308  1.9.64.1  pgoyette static inline bool
    309  1.9.64.1  pgoyette redzone_const_or_false(bool x)
    310  1.9.64.1  pgoyette {
    311  1.9.64.1  pgoyette #ifdef DIAGNOSTIC
    312  1.9.64.1  pgoyette 	return x;
    313  1.9.64.1  pgoyette #else
    314  1.9.64.1  pgoyette 	return false;
    315  1.9.64.1  pgoyette #endif /* !DIAGNOSTIC */
    316  1.9.64.1  pgoyette }
    317  1.9.64.1  pgoyette 
    318  1.9.64.1  pgoyette static inline int
    319  1.9.64.1  pgoyette redzone_const_or_zero(int x)
    320  1.9.64.1  pgoyette {
    321  1.9.64.1  pgoyette 	return redzone_const_or_false(true) ? x : 0;
    322  1.9.64.1  pgoyette }
    323  1.9.64.1  pgoyette #endif
    324  1.9.64.1  pgoyette 
    325  1.9.64.1  pgoyette void
    326  1.9.64.1  pgoyette cpu_intr_init(struct cpu_info *ci)
    327  1.9.64.1  pgoyette {
    328  1.9.64.1  pgoyette 	int i; /* XXX: duplicate */
    329  1.9.64.1  pgoyette 
    330  1.9.64.1  pgoyette 	ci->ci_xunmask[0] = 0xfffffffe;
    331  1.9.64.1  pgoyette 	for (i = 1; i < NIPL; i++)
    332  1.9.64.1  pgoyette 		ci->ci_xunmask[i] = ci->ci_xunmask[i - 1] & ~(1 << i);
    333  1.9.64.1  pgoyette 
    334  1.9.64.1  pgoyette #if defined(INTRSTACKSIZE)
    335  1.9.64.1  pgoyette 	vaddr_t istack;
    336  1.9.64.1  pgoyette 
    337  1.9.64.1  pgoyette 	/*
    338  1.9.64.1  pgoyette 	 * If the red zone is activated, protect both the top and
    339  1.9.64.1  pgoyette 	 * the bottom of the stack with an unmapped page.
    340  1.9.64.1  pgoyette 	 */
    341  1.9.64.1  pgoyette 	istack = uvm_km_alloc(kernel_map,
    342  1.9.64.1  pgoyette 	    INTRSTACKSIZE + redzone_const_or_zero(2 * PAGE_SIZE), 0,
    343  1.9.64.1  pgoyette 	    UVM_KMF_WIRED|UVM_KMF_ZERO);
    344  1.9.64.1  pgoyette 	if (redzone_const_or_false(true)) {
    345  1.9.64.1  pgoyette 		pmap_kremove(istack, PAGE_SIZE);
    346  1.9.64.1  pgoyette 		pmap_kremove(istack + INTRSTACKSIZE + PAGE_SIZE, PAGE_SIZE);
    347  1.9.64.1  pgoyette 		pmap_update(pmap_kernel());
    348  1.9.64.1  pgoyette 	}
    349  1.9.64.1  pgoyette 
    350  1.9.64.1  pgoyette 	/*
    351  1.9.64.1  pgoyette 	 * 33 used to be 1.  Arbitrarily reserve 32 more register_t's
    352  1.9.64.1  pgoyette 	 * of space for ddb(4) to examine some subroutine arguments
    353  1.9.64.1  pgoyette 	 * and to hunt for the next stack frame.
    354  1.9.64.1  pgoyette 	 */
    355  1.9.64.1  pgoyette 	ci->ci_intrstack = (char *)istack + redzone_const_or_zero(PAGE_SIZE) +
    356  1.9.64.1  pgoyette 	    INTRSTACKSIZE - 33 * sizeof(register_t);
    357  1.9.64.1  pgoyette #endif
    358  1.9.64.1  pgoyette 
    359  1.9.64.1  pgoyette 	ci->ci_idepth = -1;
    360  1.9.64.1  pgoyette }
    361  1.9.64.1  pgoyette 
    362  1.9.64.1  pgoyette /*
    363  1.9.64.1  pgoyette  * Everything below from here is duplicated from x86/intr.c
    364  1.9.64.1  pgoyette  * When intr.c and xen_intr.c are unified, these will need to be
    365  1.9.64.1  pgoyette  * merged.
    366  1.9.64.1  pgoyette  */
    367  1.9.64.1  pgoyette 
    368  1.9.64.1  pgoyette u_int xen_cpu_intr_count(struct cpu_info *ci);
    369  1.9.64.1  pgoyette 
    370  1.9.64.1  pgoyette u_int
    371  1.9.64.1  pgoyette xen_cpu_intr_count(struct cpu_info *ci)
    372  1.9.64.1  pgoyette {
    373  1.9.64.1  pgoyette 
    374  1.9.64.1  pgoyette 	KASSERT(ci->ci_nintrhand >= 0);
    375  1.9.64.1  pgoyette 
    376  1.9.64.1  pgoyette 	return ci->ci_nintrhand;
    377  1.9.64.1  pgoyette }
    378  1.9.64.1  pgoyette 
    379  1.9.64.1  pgoyette static const char *
    380  1.9.64.1  pgoyette xen_intr_string(int port, char *buf, size_t len, struct pic *pic)
    381  1.9.64.1  pgoyette {
    382  1.9.64.1  pgoyette 	KASSERT(pic->pic_type == PIC_XEN);
    383  1.9.64.1  pgoyette 
    384  1.9.64.1  pgoyette 	KASSERT(port >= 0);
    385  1.9.64.1  pgoyette 	KASSERT(port < NR_EVENT_CHANNELS);
    386  1.9.64.1  pgoyette 
    387  1.9.64.1  pgoyette 	snprintf(buf, len, "%s channel %d", pic->pic_name, port);
    388  1.9.64.1  pgoyette 
    389  1.9.64.1  pgoyette 	return buf;
    390  1.9.64.1  pgoyette }
    391  1.9.64.1  pgoyette 
    392  1.9.64.1  pgoyette static const char *
    393  1.9.64.1  pgoyette legacy_intr_string(int ih, char *buf, size_t len, struct pic *pic)
    394  1.9.64.1  pgoyette {
    395  1.9.64.1  pgoyette 	int legacy_irq;
    396  1.9.64.1  pgoyette 
    397  1.9.64.1  pgoyette 	KASSERT(pic->pic_type == PIC_I8259);
    398  1.9.64.1  pgoyette #if NLAPIC > 0
    399  1.9.64.1  pgoyette 	KASSERT(APIC_IRQ_ISLEGACY(ih));
    400  1.9.64.1  pgoyette 
    401  1.9.64.1  pgoyette 	legacy_irq = APIC_IRQ_LEGACY_IRQ(ih);
    402  1.9.64.1  pgoyette #else
    403  1.9.64.1  pgoyette 	legacy_irq = ih;
    404  1.9.64.1  pgoyette #endif
    405  1.9.64.1  pgoyette 	KASSERT(legacy_irq >= 0 && legacy_irq < 16);
    406  1.9.64.1  pgoyette 
    407  1.9.64.1  pgoyette 	snprintf(buf, len, "%s pin %d", pic->pic_name, legacy_irq);
    408  1.9.64.1  pgoyette 
    409  1.9.64.1  pgoyette 	return buf;
    410  1.9.64.1  pgoyette }
    411  1.9.64.1  pgoyette 
    412  1.9.64.1  pgoyette const char *
    413  1.9.64.1  pgoyette intr_string(intr_handle_t ih, char *buf, size_t len)
    414  1.9.64.1  pgoyette {
    415  1.9.64.1  pgoyette #if NIOAPIC > 0
    416  1.9.64.1  pgoyette 	struct ioapic_softc *pic;
    417  1.9.64.1  pgoyette #endif
    418  1.9.64.1  pgoyette 
    419  1.9.64.1  pgoyette 	if (ih == 0)
    420  1.9.64.1  pgoyette 		panic("%s: bogus handle 0x%" PRIx64, __func__, ih);
    421  1.9.64.1  pgoyette 
    422  1.9.64.1  pgoyette #if NIOAPIC > 0
    423  1.9.64.1  pgoyette 	if (ih & APIC_INT_VIA_APIC) {
    424  1.9.64.1  pgoyette 		pic = ioapic_find(APIC_IRQ_APIC(ih));
    425  1.9.64.1  pgoyette 		if (pic != NULL) {
    426  1.9.64.1  pgoyette 			snprintf(buf, len, "%s pin %d",
    427  1.9.64.1  pgoyette 			    device_xname(pic->sc_dev), APIC_IRQ_PIN(ih));
    428  1.9.64.1  pgoyette 		} else {
    429  1.9.64.1  pgoyette 			snprintf(buf, len,
    430  1.9.64.1  pgoyette 			    "apic %d int %d (irq %d)",
    431  1.9.64.1  pgoyette 			    APIC_IRQ_APIC(ih),
    432  1.9.64.1  pgoyette 			    APIC_IRQ_PIN(ih),
    433  1.9.64.1  pgoyette 			    APIC_IRQ_LEGACY_IRQ(ih));
    434  1.9.64.1  pgoyette 		}
    435  1.9.64.1  pgoyette 	} else
    436  1.9.64.1  pgoyette 		snprintf(buf, len, "irq %d", APIC_IRQ_LEGACY_IRQ(ih));
    437  1.9.64.1  pgoyette 
    438  1.9.64.1  pgoyette #elif NLAPIC > 0
    439  1.9.64.1  pgoyette 	snprintf(buf, len, "irq %d" APIC_IRQ_LEGACY_IRQ(ih));
    440  1.9.64.1  pgoyette #else
    441  1.9.64.1  pgoyette 	snprintf(buf, len, "irq %d", (int) ih);
    442  1.9.64.1  pgoyette #endif
    443  1.9.64.1  pgoyette 	return buf;
    444  1.9.64.1  pgoyette 
    445  1.9.64.1  pgoyette }
    446  1.9.64.1  pgoyette 
    447  1.9.64.1  pgoyette /*
    448  1.9.64.1  pgoyette  * Create an interrupt id such as "ioapic0 pin 9". This interrupt id is used
    449  1.9.64.1  pgoyette  * by MI code and intrctl(8).
    450  1.9.64.1  pgoyette  */
    451  1.9.64.1  pgoyette const char *
    452  1.9.64.1  pgoyette intr_create_intrid(int legacy_irq, struct pic *pic, int pin, char *buf, size_t len)
    453  1.9.64.1  pgoyette {
    454  1.9.64.1  pgoyette 	int ih = 0;
    455  1.9.64.1  pgoyette 
    456  1.9.64.1  pgoyette #if NPCI > 0
    457  1.9.64.1  pgoyette #if defined(__HAVE_PCI_MSI_MSIX)
    458  1.9.64.1  pgoyette 	if ((pic->pic_type == PIC_MSI) || (pic->pic_type == PIC_MSIX)) {
    459  1.9.64.1  pgoyette 		uint64_t pih;
    460  1.9.64.1  pgoyette 		int dev, vec;
    461  1.9.64.1  pgoyette 
    462  1.9.64.1  pgoyette 		dev = msipic_get_devid(pic);
    463  1.9.64.1  pgoyette 		vec = pin;
    464  1.9.64.1  pgoyette 		pih = __SHIFTIN((uint64_t)dev, MSI_INT_DEV_MASK)
    465  1.9.64.1  pgoyette 			| __SHIFTIN((uint64_t)vec, MSI_INT_VEC_MASK)
    466  1.9.64.1  pgoyette 			| APIC_INT_VIA_MSI;
    467  1.9.64.1  pgoyette 		if (pic->pic_type == PIC_MSI)
    468  1.9.64.1  pgoyette 			MSI_INT_MAKE_MSI(pih);
    469  1.9.64.1  pgoyette 		else if (pic->pic_type == PIC_MSIX)
    470  1.9.64.1  pgoyette 			MSI_INT_MAKE_MSIX(pih);
    471  1.9.64.1  pgoyette 
    472  1.9.64.1  pgoyette 		return x86_pci_msi_string(NULL, pih, buf, len);
    473  1.9.64.1  pgoyette 	}
    474  1.9.64.1  pgoyette #endif /* __HAVE_PCI_MSI_MSIX */
    475  1.9.64.1  pgoyette #endif
    476  1.9.64.1  pgoyette 
    477  1.9.64.1  pgoyette 	if (pic->pic_type == PIC_XEN) {
    478  1.9.64.1  pgoyette 		ih = pin;	/* Port == pin */
    479  1.9.64.1  pgoyette 		return xen_intr_string(pin, buf, len, pic);
    480  1.9.64.1  pgoyette 	}
    481  1.9.64.1  pgoyette 
    482  1.9.64.1  pgoyette 	/*
    483  1.9.64.1  pgoyette 	 * If the device is pci, "legacy_irq" is alway -1. Least 8 bit of "ih"
    484  1.9.64.1  pgoyette 	 * is only used in intr_string() to show the irq number.
    485  1.9.64.1  pgoyette 	 * If the device is "legacy"(such as floppy), it should not use
    486  1.9.64.1  pgoyette 	 * intr_string().
    487  1.9.64.1  pgoyette 	 */
    488  1.9.64.1  pgoyette 	if (pic->pic_type == PIC_I8259) {
    489  1.9.64.1  pgoyette 		ih = legacy_irq;
    490  1.9.64.1  pgoyette 		return legacy_intr_string(ih, buf, len, pic);
    491  1.9.64.1  pgoyette 	}
    492  1.9.64.1  pgoyette 
    493  1.9.64.1  pgoyette #if NIOAPIC > 0 || NACPICA > 0
    494  1.9.64.1  pgoyette 	ih = ((pic->pic_apicid << APIC_INT_APIC_SHIFT) & APIC_INT_APIC_MASK)
    495  1.9.64.1  pgoyette 	    | ((pin << APIC_INT_PIN_SHIFT) & APIC_INT_PIN_MASK);
    496  1.9.64.1  pgoyette 	if (pic->pic_type == PIC_IOAPIC) {
    497  1.9.64.1  pgoyette 		ih |= APIC_INT_VIA_APIC;
    498  1.9.64.1  pgoyette 	}
    499  1.9.64.1  pgoyette 	ih |= pin;
    500  1.9.64.1  pgoyette 	return intr_string(ih, buf, len);
    501  1.9.64.1  pgoyette #endif
    502  1.9.64.1  pgoyette 
    503  1.9.64.1  pgoyette 	return NULL; /* No pic found! */
    504  1.9.64.1  pgoyette }
    505  1.9.64.1  pgoyette 
    506  1.9.64.1  pgoyette __weak_alias(x86_disable_intr, xen_disable_intr);
    507  1.9.64.1  pgoyette __weak_alias(x86_enable_intr, xen_enable_intr);
    508  1.9.64.1  pgoyette __weak_alias(x86_read_psl, xen_read_psl);
    509  1.9.64.1  pgoyette __weak_alias(x86_write_psl, xen_write_psl);
    510  1.9.64.1  pgoyette 
    511  1.9.64.1  pgoyette __weak_alias(intr_establish, xen_intr_establish);
    512  1.9.64.1  pgoyette __weak_alias(intr_establish_xname, xen_intr_establish_xname);
    513  1.9.64.1  pgoyette __weak_alias(intr_disestablish, xen_intr_disestablish);
    514  1.9.64.1  pgoyette __weak_alias(cpu_intr_redistribute, xen_cpu_intr_redistribute);
    515  1.9.64.1  pgoyette __weak_alias(cpu_intr_count, xen_cpu_intr_count);
    516  1.9.64.1  pgoyette 
    517