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