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