Home | History | Annotate | Line # | Download | only in x86
xen_intr.c revision 1.9.66.2
      1  1.9.66.2    martin /*	$NetBSD: xen_intr.c,v 1.9.66.2 2020/04/08 14:07:59 martin 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.66.2    martin __KERNEL_RCSID(0, "$NetBSD: xen_intr.c,v 1.9.66.2 2020/04/08 14:07:59 martin Exp $");
     34  1.9.66.2    martin 
     35  1.9.66.2    martin #include "opt_multiprocessor.h"
     36       1.2    bouyer 
     37       1.2    bouyer #include <sys/param.h>
     38  1.9.66.1  christos #include <sys/kernel.h>
     39  1.9.66.1  christos #include <sys/kmem.h>
     40  1.9.66.1  christos #include <sys/cpu.h>
     41  1.9.66.2    martin #include <sys/device.h>
     42  1.9.66.1  christos 
     43  1.9.66.1  christos #include <xen/evtchn.h>
     44  1.9.66.1  christos #include <xen/xenfunc.h>
     45  1.9.66.1  christos 
     46  1.9.66.1  christos #include <uvm/uvm.h>
     47       1.2    bouyer 
     48       1.2    bouyer #include <machine/cpu.h>
     49       1.2    bouyer #include <machine/intr.h>
     50       1.2    bouyer 
     51  1.9.66.1  christos #include "acpica.h"
     52  1.9.66.1  christos #include "ioapic.h"
     53  1.9.66.1  christos #include "lapic.h"
     54  1.9.66.1  christos #include "pci.h"
     55  1.9.66.1  christos 
     56  1.9.66.1  christos #if NACPICA > 0
     57  1.9.66.1  christos #include <dev/acpi/acpivar.h>
     58  1.9.66.1  christos #endif
     59  1.9.66.1  christos 
     60  1.9.66.1  christos #if NIOAPIC > 0 || NACPICA > 0
     61  1.9.66.1  christos #include <machine/i82093var.h>
     62  1.9.66.1  christos #endif
     63  1.9.66.1  christos 
     64  1.9.66.1  christos #if NLAPIC > 0
     65  1.9.66.1  christos #include <machine/i82489var.h>
     66  1.9.66.1  christos #endif
     67  1.9.66.1  christos 
     68  1.9.66.1  christos #if NPCI > 0
     69  1.9.66.1  christos #include <dev/pci/ppbreg.h>
     70  1.9.66.1  christos #endif
     71       1.2    bouyer 
     72  1.9.66.2    martin #if defined(MULTIPROCESSOR)
     73  1.9.66.2    martin static const char *xen_ipi_names[XEN_NIPIS] = XEN_IPI_NAMES;
     74  1.9.66.2    martin #endif
     75  1.9.66.2    martin 
     76       1.2    bouyer /*
     77       1.2    bouyer  * Restore a value to cpl (unmasking interrupts).  If any unmasked
     78       1.2    bouyer  * interrupts are pending, call Xspllower() to process them.
     79       1.2    bouyer  */
     80  1.9.66.1  christos void xen_spllower(int nlevel);
     81  1.9.66.1  christos 
     82       1.2    bouyer void
     83  1.9.66.1  christos xen_spllower(int nlevel)
     84       1.2    bouyer {
     85       1.2    bouyer 	struct cpu_info *ci = curcpu();
     86  1.9.66.1  christos 	uint32_t xmask;
     87       1.2    bouyer 	u_long psl;
     88       1.2    bouyer 
     89       1.8    bouyer 	if (ci->ci_ilevel <= nlevel)
     90       1.8    bouyer 		return;
     91       1.8    bouyer 
     92       1.2    bouyer 	__insn_barrier();
     93       1.2    bouyer 
     94  1.9.66.1  christos 	xmask = XUNMASK(ci, nlevel);
     95  1.9.66.1  christos 	psl = xen_read_psl();
     96       1.2    bouyer 	x86_disable_intr();
     97  1.9.66.1  christos 	if (ci->ci_xpending & xmask) {
     98       1.7    bouyer 		KASSERT(psl == 0);
     99       1.2    bouyer 		Xspllower(nlevel);
    100       1.2    bouyer 		/* Xspllower does enable_intr() */
    101       1.2    bouyer 	} else {
    102       1.2    bouyer 		ci->ci_ilevel = nlevel;
    103  1.9.66.1  christos 		xen_write_psl(psl);
    104       1.2    bouyer 	}
    105       1.2    bouyer }
    106       1.2    bouyer 
    107  1.9.66.1  christos 
    108  1.9.66.1  christos #if !defined(XENPVHVM)
    109       1.2    bouyer void
    110       1.2    bouyer x86_disable_intr(void)
    111       1.2    bouyer {
    112  1.9.66.1  christos 	curcpu()->ci_vcpu->evtchn_upcall_mask = 1;
    113  1.9.66.1  christos 	x86_lfence();
    114       1.2    bouyer }
    115       1.2    bouyer 
    116       1.2    bouyer void
    117       1.2    bouyer x86_enable_intr(void)
    118       1.2    bouyer {
    119  1.9.66.1  christos 	volatile struct vcpu_info *_vci = curcpu()->ci_vcpu;
    120  1.9.66.1  christos 	__insn_barrier();
    121  1.9.66.1  christos 	_vci->evtchn_upcall_mask = 0;
    122  1.9.66.1  christos 	x86_lfence(); /* unmask then check (avoid races) */
    123  1.9.66.1  christos 	if (__predict_false(_vci->evtchn_upcall_pending))
    124  1.9.66.1  christos 		hypervisor_force_callback();
    125       1.2    bouyer }
    126       1.2    bouyer 
    127  1.9.66.1  christos #endif /* !XENPVHVM */
    128  1.9.66.1  christos 
    129       1.2    bouyer u_long
    130  1.9.66.1  christos xen_read_psl(void)
    131       1.2    bouyer {
    132       1.2    bouyer 
    133       1.4    cegger 	return (curcpu()->ci_vcpu->evtchn_upcall_mask);
    134       1.2    bouyer }
    135       1.2    bouyer 
    136       1.2    bouyer void
    137  1.9.66.1  christos xen_write_psl(u_long psl)
    138       1.2    bouyer {
    139       1.4    cegger 	struct cpu_info *ci = curcpu();
    140       1.2    bouyer 
    141       1.4    cegger 	ci->ci_vcpu->evtchn_upcall_mask = psl;
    142       1.9       jym 	xen_rmb();
    143       1.4    cegger 	if (ci->ci_vcpu->evtchn_upcall_pending && psl == 0) {
    144       1.2    bouyer 	    	hypervisor_force_callback();
    145       1.2    bouyer 	}
    146       1.2    bouyer }
    147  1.9.66.1  christos 
    148  1.9.66.1  christos void *
    149  1.9.66.1  christos xen_intr_establish(int legacy_irq, struct pic *pic, int pin,
    150  1.9.66.1  christos     int type, int level, int (*handler)(void *), void *arg,
    151  1.9.66.1  christos     bool known_mpsafe)
    152  1.9.66.1  christos {
    153  1.9.66.1  christos 
    154  1.9.66.1  christos 	return xen_intr_establish_xname(legacy_irq, pic, pin, type, level,
    155  1.9.66.1  christos 	    handler, arg, known_mpsafe, "XEN");
    156  1.9.66.1  christos }
    157  1.9.66.1  christos 
    158  1.9.66.1  christos void *
    159  1.9.66.1  christos xen_intr_establish_xname(int legacy_irq, struct pic *pic, int pin,
    160  1.9.66.1  christos     int type, int level, int (*handler)(void *), void *arg,
    161  1.9.66.1  christos     bool known_mpsafe, const char *xname)
    162  1.9.66.1  christos {
    163  1.9.66.1  christos 	const char *intrstr;
    164  1.9.66.1  christos 	char intrstr_buf[INTRIDBUF];
    165  1.9.66.1  christos 
    166  1.9.66.1  christos 	if (pic->pic_type == PIC_XEN) {
    167  1.9.66.1  christos 		struct intrhand *rih;
    168  1.9.66.1  christos 
    169  1.9.66.1  christos 		intrstr = intr_create_intrid(legacy_irq, pic, pin, intrstr_buf,
    170  1.9.66.1  christos 		    sizeof(intrstr_buf));
    171  1.9.66.1  christos 
    172  1.9.66.2    martin 		event_set_handler(pin, handler, arg, level, intrstr, xname,
    173  1.9.66.2    martin 		    known_mpsafe);
    174  1.9.66.1  christos 
    175  1.9.66.1  christos 		rih = kmem_zalloc(sizeof(*rih), cold ? KM_NOSLEEP : KM_SLEEP);
    176  1.9.66.1  christos 		if (rih == NULL) {
    177  1.9.66.1  christos 			printf("%s: can't allocate handler info\n", __func__);
    178  1.9.66.1  christos 			return NULL;
    179  1.9.66.1  christos 		}
    180  1.9.66.1  christos 
    181  1.9.66.1  christos 		/*
    182  1.9.66.1  christos 		 * XXX:
    183  1.9.66.1  christos 		 * This is just a copy for API conformance.
    184  1.9.66.1  christos 		 * The real ih is lost in the innards of
    185  1.9.66.1  christos 		 * event_set_handler(); where the details of
    186  1.9.66.1  christos 		 * biglock_wrapper etc are taken care of.
    187  1.9.66.1  christos 		 * All that goes away when we nuke event_set_handler()
    188  1.9.66.1  christos 		 * et. al. and unify with x86/intr.c
    189  1.9.66.1  christos 		 */
    190  1.9.66.1  christos 		rih->ih_pin = pin; /* port */
    191  1.9.66.1  christos 		rih->ih_fun = rih->ih_realfun = handler;
    192  1.9.66.1  christos 		rih->ih_arg = rih->ih_realarg = arg;
    193  1.9.66.1  christos 		rih->pic_type = pic->pic_type;
    194  1.9.66.1  christos 		return rih;
    195  1.9.66.1  christos 	} 	/* Else we assume pintr */
    196  1.9.66.1  christos 
    197  1.9.66.1  christos #if (NPCI > 0 || NISA > 0) && defined(XENPV) /* XXX: support PVHVM pirq */
    198  1.9.66.1  christos 	struct pintrhand *pih;
    199  1.9.66.1  christos 	int gsi;
    200  1.9.66.1  christos 	int vector, evtchn;
    201  1.9.66.1  christos 
    202  1.9.66.1  christos 	KASSERTMSG(legacy_irq == -1 || (0 <= legacy_irq && legacy_irq < NUM_XEN_IRQS),
    203  1.9.66.1  christos 	    "bad legacy IRQ value: %d", legacy_irq);
    204  1.9.66.1  christos 	KASSERTMSG(!(legacy_irq == -1 && pic == &i8259_pic),
    205  1.9.66.1  christos 	    "non-legacy IRQon i8259 ");
    206  1.9.66.1  christos 
    207  1.9.66.1  christos 	gsi = xen_pic_to_gsi(pic, pin);
    208  1.9.66.1  christos 
    209  1.9.66.1  christos 	intrstr = intr_create_intrid(gsi, pic, pin, intrstr_buf,
    210  1.9.66.1  christos 	    sizeof(intrstr_buf));
    211  1.9.66.1  christos 
    212  1.9.66.1  christos 	vector = xen_vec_alloc(gsi);
    213  1.9.66.1  christos 
    214  1.9.66.1  christos 	if (irq2port[gsi] == 0) {
    215  1.9.66.1  christos 		extern struct cpu_info phycpu_info_primary; /* XXX */
    216  1.9.66.1  christos 		struct cpu_info *ci = &phycpu_info_primary;
    217  1.9.66.1  christos 
    218  1.9.66.1  christos 		pic->pic_addroute(pic, ci, pin, vector, type);
    219  1.9.66.1  christos 
    220  1.9.66.1  christos 		evtchn = bind_pirq_to_evtch(gsi);
    221  1.9.66.1  christos 		KASSERT(evtchn > 0);
    222  1.9.66.1  christos 		KASSERT(evtchn < NR_EVENT_CHANNELS);
    223  1.9.66.1  christos 		irq2port[gsi] = evtchn + 1;
    224  1.9.66.1  christos 		xen_atomic_set_bit(&ci->ci_evtmask[0], evtchn);
    225  1.9.66.1  christos 	} else {
    226  1.9.66.1  christos 		/*
    227  1.9.66.1  christos 		 * Shared interrupt - we can't rebind.
    228  1.9.66.1  christos 		 * The port is shared instead.
    229  1.9.66.1  christos 		 */
    230  1.9.66.1  christos 		evtchn = irq2port[gsi] - 1;
    231  1.9.66.1  christos 	}
    232  1.9.66.1  christos 
    233  1.9.66.1  christos 	pih = pirq_establish(gsi, evtchn, handler, arg, level,
    234  1.9.66.1  christos 			     intrstr, xname);
    235  1.9.66.1  christos 	pih->pic_type = pic->pic_type;
    236  1.9.66.1  christos 	return pih;
    237  1.9.66.1  christos #endif /* NPCI > 0 || NISA > 0 */
    238  1.9.66.1  christos 
    239  1.9.66.1  christos 	/* FALLTHROUGH */
    240  1.9.66.1  christos 	return NULL;
    241  1.9.66.1  christos }
    242  1.9.66.1  christos 
    243  1.9.66.1  christos /*
    244  1.9.66.2    martin  * Mask an interrupt source.
    245  1.9.66.2    martin  */
    246  1.9.66.2    martin void
    247  1.9.66.2    martin xen_intr_mask(struct intrhand *ih)
    248  1.9.66.2    martin {
    249  1.9.66.2    martin 	/* XXX */
    250  1.9.66.2    martin 	panic("xen_intr_mask: not yet implemented.");
    251  1.9.66.2    martin }
    252  1.9.66.2    martin 
    253  1.9.66.2    martin /*
    254  1.9.66.2    martin  * Unmask an interrupt source.
    255  1.9.66.2    martin  */
    256  1.9.66.2    martin void
    257  1.9.66.2    martin xen_intr_unmask(struct intrhand *ih)
    258  1.9.66.2    martin {
    259  1.9.66.2    martin 	/* XXX */
    260  1.9.66.2    martin 	panic("xen_intr_unmask: not yet implemented.");
    261  1.9.66.2    martin }
    262  1.9.66.2    martin 
    263  1.9.66.2    martin /*
    264  1.9.66.1  christos  * Deregister an interrupt handler.
    265  1.9.66.1  christos  */
    266  1.9.66.1  christos void
    267  1.9.66.1  christos xen_intr_disestablish(struct intrhand *ih)
    268  1.9.66.1  christos {
    269  1.9.66.1  christos 
    270  1.9.66.1  christos 	if (ih->pic_type == PIC_XEN) {
    271  1.9.66.1  christos 		event_remove_handler(ih->ih_pin, ih->ih_realfun,
    272  1.9.66.1  christos 		    ih->ih_realarg);
    273  1.9.66.1  christos 		kmem_free(ih, sizeof(*ih));
    274  1.9.66.1  christos 		return;
    275  1.9.66.1  christos 	}
    276  1.9.66.1  christos #if defined(DOM0OPS)
    277  1.9.66.1  christos 	/*
    278  1.9.66.1  christos 	 * Cache state, to prevent a use after free situation with
    279  1.9.66.1  christos 	 * ih.
    280  1.9.66.1  christos 	 */
    281  1.9.66.1  christos 
    282  1.9.66.1  christos 	struct pintrhand *pih = (struct pintrhand *)ih;
    283  1.9.66.1  christos 
    284  1.9.66.1  christos 	int pirq = pih->pirq;
    285  1.9.66.1  christos 	int port = pih->evtch;
    286  1.9.66.1  christos 	KASSERT(irq2port[pirq] != 0);
    287  1.9.66.1  christos 
    288  1.9.66.1  christos 	pirq_disestablish(pih);
    289  1.9.66.1  christos 
    290  1.9.66.1  christos 	if (evtsource[port] == NULL) {
    291  1.9.66.1  christos 			/*
    292  1.9.66.1  christos 			 * Last handler was removed by
    293  1.9.66.1  christos 			 * event_remove_handler().
    294  1.9.66.1  christos 			 *
    295  1.9.66.1  christos 			 * We can safely unbind the pirq now.
    296  1.9.66.1  christos 			 */
    297  1.9.66.1  christos 
    298  1.9.66.1  christos 			port = unbind_pirq_from_evtch(pirq);
    299  1.9.66.1  christos 			KASSERT(port == pih->evtch);
    300  1.9.66.1  christos 			irq2port[pirq] = 0;
    301  1.9.66.1  christos 	}
    302  1.9.66.1  christos #endif
    303  1.9.66.1  christos 	return;
    304  1.9.66.1  christos }
    305  1.9.66.1  christos 
    306  1.9.66.1  christos /* MI interface for kern_cpu.c */
    307  1.9.66.1  christos void xen_cpu_intr_redistribute(void);
    308  1.9.66.1  christos 
    309  1.9.66.1  christos void
    310  1.9.66.1  christos xen_cpu_intr_redistribute(void)
    311  1.9.66.1  christos {
    312  1.9.66.1  christos 	KASSERT(mutex_owned(&cpu_lock));
    313  1.9.66.1  christos 	KASSERT(mp_online);
    314  1.9.66.1  christos 
    315  1.9.66.1  christos 	return;
    316  1.9.66.1  christos }
    317  1.9.66.1  christos 
    318  1.9.66.1  christos /* MD - called by x86/cpu.c */
    319  1.9.66.1  christos #if defined(INTRSTACKSIZE)
    320  1.9.66.1  christos static inline bool
    321  1.9.66.1  christos redzone_const_or_false(bool x)
    322  1.9.66.1  christos {
    323  1.9.66.1  christos #ifdef DIAGNOSTIC
    324  1.9.66.1  christos 	return x;
    325  1.9.66.1  christos #else
    326  1.9.66.1  christos 	return false;
    327  1.9.66.1  christos #endif /* !DIAGNOSTIC */
    328  1.9.66.1  christos }
    329  1.9.66.1  christos 
    330  1.9.66.1  christos static inline int
    331  1.9.66.1  christos redzone_const_or_zero(int x)
    332  1.9.66.1  christos {
    333  1.9.66.1  christos 	return redzone_const_or_false(true) ? x : 0;
    334  1.9.66.1  christos }
    335  1.9.66.1  christos #endif
    336  1.9.66.1  christos 
    337  1.9.66.1  christos void xen_cpu_intr_init(struct cpu_info *);
    338  1.9.66.1  christos void
    339  1.9.66.1  christos xen_cpu_intr_init(struct cpu_info *ci)
    340  1.9.66.1  christos {
    341  1.9.66.1  christos 	int i; /* XXX: duplicate */
    342  1.9.66.1  christos 
    343  1.9.66.1  christos 	ci->ci_xunmask[0] = 0xfffffffe;
    344  1.9.66.1  christos 	for (i = 1; i < NIPL; i++)
    345  1.9.66.1  christos 		ci->ci_xunmask[i] = ci->ci_xunmask[i - 1] & ~(1 << i);
    346  1.9.66.1  christos 
    347  1.9.66.1  christos #if defined(INTRSTACKSIZE)
    348  1.9.66.1  christos 	vaddr_t istack;
    349  1.9.66.1  christos 
    350  1.9.66.1  christos 	/*
    351  1.9.66.1  christos 	 * If the red zone is activated, protect both the top and
    352  1.9.66.1  christos 	 * the bottom of the stack with an unmapped page.
    353  1.9.66.1  christos 	 */
    354  1.9.66.1  christos 	istack = uvm_km_alloc(kernel_map,
    355  1.9.66.1  christos 	    INTRSTACKSIZE + redzone_const_or_zero(2 * PAGE_SIZE), 0,
    356  1.9.66.1  christos 	    UVM_KMF_WIRED|UVM_KMF_ZERO);
    357  1.9.66.1  christos 	if (redzone_const_or_false(true)) {
    358  1.9.66.1  christos 		pmap_kremove(istack, PAGE_SIZE);
    359  1.9.66.1  christos 		pmap_kremove(istack + INTRSTACKSIZE + PAGE_SIZE, PAGE_SIZE);
    360  1.9.66.1  christos 		pmap_update(pmap_kernel());
    361  1.9.66.1  christos 	}
    362  1.9.66.1  christos 
    363  1.9.66.1  christos 	/*
    364  1.9.66.1  christos 	 * 33 used to be 1.  Arbitrarily reserve 32 more register_t's
    365  1.9.66.1  christos 	 * of space for ddb(4) to examine some subroutine arguments
    366  1.9.66.1  christos 	 * and to hunt for the next stack frame.
    367  1.9.66.1  christos 	 */
    368  1.9.66.1  christos 	ci->ci_intrstack = (char *)istack + redzone_const_or_zero(PAGE_SIZE) +
    369  1.9.66.1  christos 	    INTRSTACKSIZE - 33 * sizeof(register_t);
    370  1.9.66.1  christos #endif
    371  1.9.66.1  christos 
    372  1.9.66.2    martin #ifdef MULTIPROCESSOR
    373  1.9.66.2    martin 	for (i = 0; i < XEN_NIPIS; i++)
    374  1.9.66.2    martin 		evcnt_attach_dynamic(&ci->ci_ipi_events[i], EVCNT_TYPE_MISC,
    375  1.9.66.2    martin 		    NULL, device_xname(ci->ci_dev), xen_ipi_names[i]);
    376  1.9.66.2    martin #endif
    377  1.9.66.2    martin 
    378  1.9.66.1  christos 	ci->ci_idepth = -1;
    379  1.9.66.1  christos }
    380  1.9.66.1  christos 
    381  1.9.66.1  christos /*
    382  1.9.66.1  christos  * Everything below from here is duplicated from x86/intr.c
    383  1.9.66.1  christos  * When intr.c and xen_intr.c are unified, these will need to be
    384  1.9.66.1  christos  * merged.
    385  1.9.66.1  christos  */
    386  1.9.66.1  christos 
    387  1.9.66.1  christos u_int xen_cpu_intr_count(struct cpu_info *ci);
    388  1.9.66.1  christos 
    389  1.9.66.1  christos u_int
    390  1.9.66.1  christos xen_cpu_intr_count(struct cpu_info *ci)
    391  1.9.66.1  christos {
    392  1.9.66.1  christos 
    393  1.9.66.1  christos 	KASSERT(ci->ci_nintrhand >= 0);
    394  1.9.66.1  christos 
    395  1.9.66.1  christos 	return ci->ci_nintrhand;
    396  1.9.66.1  christos }
    397  1.9.66.1  christos 
    398  1.9.66.1  christos static const char *
    399  1.9.66.1  christos xen_intr_string(int port, char *buf, size_t len, struct pic *pic)
    400  1.9.66.1  christos {
    401  1.9.66.1  christos 	KASSERT(pic->pic_type == PIC_XEN);
    402  1.9.66.1  christos 
    403  1.9.66.1  christos 	KASSERT(port >= 0);
    404  1.9.66.1  christos 	KASSERT(port < NR_EVENT_CHANNELS);
    405  1.9.66.1  christos 
    406  1.9.66.1  christos 	snprintf(buf, len, "%s channel %d", pic->pic_name, port);
    407  1.9.66.1  christos 
    408  1.9.66.1  christos 	return buf;
    409  1.9.66.1  christos }
    410  1.9.66.1  christos 
    411  1.9.66.1  christos static const char *
    412  1.9.66.1  christos legacy_intr_string(int ih, char *buf, size_t len, struct pic *pic)
    413  1.9.66.1  christos {
    414  1.9.66.1  christos 	int legacy_irq;
    415  1.9.66.1  christos 
    416  1.9.66.1  christos 	KASSERT(pic->pic_type == PIC_I8259);
    417  1.9.66.1  christos #if NLAPIC > 0
    418  1.9.66.1  christos 	KASSERT(APIC_IRQ_ISLEGACY(ih));
    419  1.9.66.1  christos 
    420  1.9.66.1  christos 	legacy_irq = APIC_IRQ_LEGACY_IRQ(ih);
    421  1.9.66.1  christos #else
    422  1.9.66.1  christos 	legacy_irq = ih;
    423  1.9.66.1  christos #endif
    424  1.9.66.1  christos 	KASSERT(legacy_irq >= 0 && legacy_irq < 16);
    425  1.9.66.1  christos 
    426  1.9.66.1  christos 	snprintf(buf, len, "%s pin %d", pic->pic_name, legacy_irq);
    427  1.9.66.1  christos 
    428  1.9.66.1  christos 	return buf;
    429  1.9.66.1  christos }
    430  1.9.66.1  christos 
    431  1.9.66.1  christos const char * xintr_string(intr_handle_t ih, char *buf, size_t len);
    432  1.9.66.1  christos 
    433  1.9.66.1  christos const char *
    434  1.9.66.1  christos xintr_string(intr_handle_t ih, char *buf, size_t len)
    435  1.9.66.1  christos {
    436  1.9.66.1  christos #if NIOAPIC > 0
    437  1.9.66.1  christos 	struct ioapic_softc *pic;
    438  1.9.66.1  christos #endif
    439  1.9.66.1  christos 
    440  1.9.66.1  christos 	if (ih == 0)
    441  1.9.66.1  christos 		panic("%s: bogus handle 0x%" PRIx64, __func__, ih);
    442  1.9.66.1  christos 
    443  1.9.66.1  christos #if NIOAPIC > 0
    444  1.9.66.1  christos 	if (ih & APIC_INT_VIA_APIC) {
    445  1.9.66.1  christos 		pic = ioapic_find(APIC_IRQ_APIC(ih));
    446  1.9.66.1  christos 		if (pic != NULL) {
    447  1.9.66.1  christos 			snprintf(buf, len, "%s pin %d",
    448  1.9.66.1  christos 			    device_xname(pic->sc_dev), APIC_IRQ_PIN(ih));
    449  1.9.66.1  christos 		} else {
    450  1.9.66.1  christos 			snprintf(buf, len,
    451  1.9.66.1  christos 			    "apic %d int %d (irq %d)",
    452  1.9.66.1  christos 			    APIC_IRQ_APIC(ih),
    453  1.9.66.1  christos 			    APIC_IRQ_PIN(ih),
    454  1.9.66.1  christos 			    APIC_IRQ_LEGACY_IRQ(ih));
    455  1.9.66.1  christos 		}
    456  1.9.66.1  christos 	} else
    457  1.9.66.1  christos 		snprintf(buf, len, "irq %d", APIC_IRQ_LEGACY_IRQ(ih));
    458  1.9.66.1  christos 
    459  1.9.66.1  christos #elif NLAPIC > 0
    460  1.9.66.1  christos 	snprintf(buf, len, "irq %d", APIC_IRQ_LEGACY_IRQ(ih));
    461  1.9.66.1  christos #else
    462  1.9.66.1  christos 	snprintf(buf, len, "irq %d", (int) ih);
    463  1.9.66.1  christos #endif
    464  1.9.66.1  christos 	return buf;
    465  1.9.66.1  christos 
    466  1.9.66.1  christos }
    467  1.9.66.1  christos 
    468  1.9.66.1  christos /*
    469  1.9.66.1  christos  * Create an interrupt id such as "ioapic0 pin 9". This interrupt id is used
    470  1.9.66.1  christos  * by MI code and intrctl(8).
    471  1.9.66.1  christos  */
    472  1.9.66.1  christos const char * xen_intr_create_intrid(int legacy_irq, struct pic *pic,
    473  1.9.66.1  christos     int pin, char *buf, size_t len);
    474  1.9.66.1  christos 
    475  1.9.66.1  christos const char *
    476  1.9.66.1  christos xen_intr_create_intrid(int legacy_irq, struct pic *pic, int pin, char *buf, size_t len)
    477  1.9.66.1  christos {
    478  1.9.66.1  christos 	int ih = 0;
    479  1.9.66.1  christos 
    480  1.9.66.1  christos #if NPCI > 0
    481  1.9.66.1  christos #if defined(__HAVE_PCI_MSI_MSIX)
    482  1.9.66.1  christos 	if ((pic->pic_type == PIC_MSI) || (pic->pic_type == PIC_MSIX)) {
    483  1.9.66.1  christos 		uint64_t pih;
    484  1.9.66.1  christos 		int dev, vec;
    485  1.9.66.1  christos 
    486  1.9.66.1  christos 		dev = msipic_get_devid(pic);
    487  1.9.66.1  christos 		vec = pin;
    488  1.9.66.1  christos 		pih = __SHIFTIN((uint64_t)dev, MSI_INT_DEV_MASK)
    489  1.9.66.1  christos 			| __SHIFTIN((uint64_t)vec, MSI_INT_VEC_MASK)
    490  1.9.66.1  christos 			| APIC_INT_VIA_MSI;
    491  1.9.66.1  christos 		if (pic->pic_type == PIC_MSI)
    492  1.9.66.1  christos 			MSI_INT_MAKE_MSI(pih);
    493  1.9.66.1  christos 		else if (pic->pic_type == PIC_MSIX)
    494  1.9.66.1  christos 			MSI_INT_MAKE_MSIX(pih);
    495  1.9.66.1  christos 
    496  1.9.66.1  christos 		return x86_pci_msi_string(NULL, pih, buf, len);
    497  1.9.66.1  christos 	}
    498  1.9.66.1  christos #endif /* __HAVE_PCI_MSI_MSIX */
    499  1.9.66.1  christos #endif
    500  1.9.66.1  christos 
    501  1.9.66.1  christos 	if (pic->pic_type == PIC_XEN) {
    502  1.9.66.1  christos 		ih = pin;	/* Port == pin */
    503  1.9.66.1  christos 		return xen_intr_string(pin, buf, len, pic);
    504  1.9.66.1  christos 	}
    505  1.9.66.1  christos 
    506  1.9.66.1  christos 	/*
    507  1.9.66.1  christos 	 * If the device is pci, "legacy_irq" is alway -1. Least 8 bit of "ih"
    508  1.9.66.1  christos 	 * is only used in intr_string() to show the irq number.
    509  1.9.66.1  christos 	 * If the device is "legacy"(such as floppy), it should not use
    510  1.9.66.1  christos 	 * intr_string().
    511  1.9.66.1  christos 	 */
    512  1.9.66.1  christos 	if (pic->pic_type == PIC_I8259) {
    513  1.9.66.1  christos 		ih = legacy_irq;
    514  1.9.66.1  christos 		return legacy_intr_string(ih, buf, len, pic);
    515  1.9.66.1  christos 	}
    516  1.9.66.1  christos 
    517  1.9.66.1  christos #if NIOAPIC > 0 || NACPICA > 0
    518  1.9.66.1  christos 	ih = ((pic->pic_apicid << APIC_INT_APIC_SHIFT) & APIC_INT_APIC_MASK)
    519  1.9.66.1  christos 	    | ((pin << APIC_INT_PIN_SHIFT) & APIC_INT_PIN_MASK);
    520  1.9.66.1  christos 	if (pic->pic_type == PIC_IOAPIC) {
    521  1.9.66.1  christos 		ih |= APIC_INT_VIA_APIC;
    522  1.9.66.1  christos 	}
    523  1.9.66.1  christos 	ih |= pin;
    524  1.9.66.1  christos 	return intr_string(ih, buf, len);
    525  1.9.66.1  christos #endif
    526  1.9.66.1  christos 
    527  1.9.66.1  christos 	return NULL; /* No pic found! */
    528  1.9.66.1  christos }
    529  1.9.66.1  christos 
    530  1.9.66.1  christos #if !defined(XENPVHVM)
    531  1.9.66.1  christos __strong_alias(spllower, xen_spllower);
    532  1.9.66.1  christos __strong_alias(x86_read_psl, xen_read_psl);
    533  1.9.66.1  christos __strong_alias(x86_write_psl, xen_write_psl);
    534  1.9.66.1  christos 
    535  1.9.66.1  christos __strong_alias(intr_string, xintr_string);
    536  1.9.66.1  christos __strong_alias(intr_create_intrid, xen_intr_create_intrid);
    537  1.9.66.1  christos __strong_alias(intr_establish, xen_intr_establish);
    538  1.9.66.1  christos __strong_alias(intr_establish_xname, xen_intr_establish_xname);
    539  1.9.66.2    martin __strong_alias(intr_mask, xen_intr_mask);
    540  1.9.66.2    martin __strong_alias(intr_unmask, xen_intr_unmask);
    541  1.9.66.1  christos __strong_alias(intr_disestablish, xen_intr_disestablish);
    542  1.9.66.1  christos __strong_alias(cpu_intr_redistribute, xen_cpu_intr_redistribute);
    543  1.9.66.1  christos __strong_alias(cpu_intr_count, xen_cpu_intr_count);
    544  1.9.66.1  christos __strong_alias(cpu_intr_init, xen_cpu_intr_init);
    545  1.9.66.1  christos #endif /* !XENPVHVM */
    546