xen_intr.c revision 1.14 1 /* $NetBSD: xen_intr.c,v 1.14 2019/02/12 08:04:53 cherry 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.14 2019/02/12 08:04:53 cherry 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 /*
70 * Restore a value to cpl (unmasking interrupts). If any unmasked
71 * interrupts are pending, call Xspllower() to process them.
72 */
73 void xen_spllower(int nlevel);
74
75 void
76 xen_spllower(int nlevel)
77 {
78 struct cpu_info *ci = curcpu();
79 uint32_t xmask;
80 u_long psl;
81
82 if (ci->ci_ilevel <= nlevel)
83 return;
84
85 __insn_barrier();
86
87 xmask = XUNMASK(ci, nlevel);
88 psl = xen_read_psl();
89 xen_disable_intr();
90 if (ci->ci_xpending & xmask) {
91 KASSERT(psl == 0);
92 Xspllower(nlevel);
93 /* Xspllower does enable_intr() */
94 } else {
95 ci->ci_ilevel = nlevel;
96 xen_write_psl(psl);
97 }
98 }
99
100 void
101 xen_disable_intr(void)
102 {
103 __cli();
104 }
105
106 void
107 xen_enable_intr(void)
108 {
109 __sti();
110 }
111
112 u_long
113 xen_read_psl(void)
114 {
115
116 return (curcpu()->ci_vcpu->evtchn_upcall_mask);
117 }
118
119 void
120 xen_write_psl(u_long psl)
121 {
122 struct cpu_info *ci = curcpu();
123
124 ci->ci_vcpu->evtchn_upcall_mask = psl;
125 xen_rmb();
126 if (ci->ci_vcpu->evtchn_upcall_pending && psl == 0) {
127 hypervisor_force_callback();
128 }
129 }
130
131 void *
132 xen_intr_establish(int legacy_irq, struct pic *pic, int pin,
133 int type, int level, int (*handler)(void *), void *arg,
134 bool known_mpsafe)
135 {
136
137 return xen_intr_establish_xname(legacy_irq, pic, pin, type, level,
138 handler, arg, known_mpsafe, "XEN");
139 }
140
141 void *
142 xen_intr_establish_xname(int legacy_irq, struct pic *pic, int pin,
143 int type, int level, int (*handler)(void *), void *arg,
144 bool known_mpsafe, const char *xname)
145 {
146 const char *intrstr;
147 char intrstr_buf[INTRIDBUF];
148
149 if (pic->pic_type == PIC_XEN) {
150 struct intrhand *rih;
151
152 /*
153 * event_set_handler interprets `level != IPL_VM' to
154 * mean MP-safe, so we require the caller to match that
155 * for the moment.
156 */
157 KASSERT(known_mpsafe == (level != IPL_VM));
158
159 intrstr = intr_create_intrid(legacy_irq, pic, pin, intrstr_buf,
160 sizeof(intrstr_buf));
161
162 event_set_handler(pin, handler, arg, level, intrstr, xname);
163
164 rih = kmem_zalloc(sizeof(*rih), cold ? KM_NOSLEEP : KM_SLEEP);
165 if (rih == NULL) {
166 printf("%s: can't allocate handler info\n", __func__);
167 return NULL;
168 }
169
170 /*
171 * XXX:
172 * This is just a copy for API conformance.
173 * The real ih is lost in the innards of
174 * event_set_handler(); where the details of
175 * biglock_wrapper etc are taken care of.
176 * All that goes away when we nuke event_set_handler()
177 * et. al. and unify with x86/intr.c
178 */
179 rih->ih_pin = pin; /* port */
180 rih->ih_fun = rih->ih_realfun = handler;
181 rih->ih_arg = rih->ih_realarg = arg;
182 rih->pic_type = pic->pic_type;
183 return rih;
184 } /* Else we assume pintr */
185
186 #if (NPCI > 0 || NISA > 0) && defined(XENPV) /* XXX: support PVHVM pirq */
187 struct pintrhand *pih;
188 int gsi;
189 int vector, evtchn;
190
191 KASSERTMSG(legacy_irq == -1 || (0 <= legacy_irq && legacy_irq < NUM_XEN_IRQS),
192 "bad legacy IRQ value: %d", legacy_irq);
193 KASSERTMSG(!(legacy_irq == -1 && pic == &i8259_pic),
194 "non-legacy IRQon i8259 ");
195
196 gsi = xen_pic_to_gsi(pic, pin);
197
198 intrstr = intr_create_intrid(gsi, pic, pin, intrstr_buf,
199 sizeof(intrstr_buf));
200
201 vector = xen_vec_alloc(gsi);
202
203 if (irq2port[gsi] == 0) {
204 extern struct cpu_info phycpu_info_primary; /* XXX */
205 struct cpu_info *ci = &phycpu_info_primary;
206
207 pic->pic_addroute(pic, ci, pin, vector, type);
208
209 evtchn = bind_pirq_to_evtch(gsi);
210 KASSERT(evtchn > 0);
211 KASSERT(evtchn < NR_EVENT_CHANNELS);
212 irq2port[gsi] = evtchn + 1;
213 xen_atomic_set_bit(&ci->ci_evtmask[0], evtchn);
214 } else {
215 /*
216 * Shared interrupt - we can't rebind.
217 * The port is shared instead.
218 */
219 evtchn = irq2port[gsi] - 1;
220 }
221
222 pih = pirq_establish(gsi, evtchn, handler, arg, level,
223 intrstr, xname);
224 pih->pic_type = pic->pic_type;
225 return pih;
226 #endif /* NPCI > 0 || NISA > 0 */
227
228 /* FALLTHROUGH */
229 return NULL;
230 }
231
232 /*
233 * Deregister an interrupt handler.
234 */
235 void
236 xen_intr_disestablish(struct intrhand *ih)
237 {
238
239 if (ih->pic_type == PIC_XEN) {
240 event_remove_handler(ih->ih_pin, ih->ih_realfun,
241 ih->ih_realarg);
242 kmem_free(ih, sizeof(*ih));
243 return;
244 }
245 #if defined(DOM0OPS)
246 /*
247 * Cache state, to prevent a use after free situation with
248 * ih.
249 */
250
251 struct pintrhand *pih = (struct pintrhand *)ih;
252
253 int pirq = pih->pirq;
254 int port = pih->evtch;
255 KASSERT(irq2port[pirq] != 0);
256
257 pirq_disestablish(pih);
258
259 if (evtsource[port] == NULL) {
260 /*
261 * Last handler was removed by
262 * event_remove_handler().
263 *
264 * We can safely unbind the pirq now.
265 */
266
267 port = unbind_pirq_from_evtch(pirq);
268 KASSERT(port == pih->evtch);
269 irq2port[pirq] = 0;
270 }
271 #endif
272 return;
273 }
274
275 /* MI interface for kern_cpu.c */
276 void xen_cpu_intr_redistribute(void);
277
278 void
279 xen_cpu_intr_redistribute(void)
280 {
281 KASSERT(mutex_owned(&cpu_lock));
282 KASSERT(mp_online);
283
284 return;
285 }
286
287 /* MD - called by x86/cpu.c */
288 #if defined(INTRSTACKSIZE)
289 static inline bool
290 redzone_const_or_false(bool x)
291 {
292 #ifdef DIAGNOSTIC
293 return x;
294 #else
295 return false;
296 #endif /* !DIAGNOSTIC */
297 }
298
299 static inline int
300 redzone_const_or_zero(int x)
301 {
302 return redzone_const_or_false(true) ? x : 0;
303 }
304 #endif
305
306 void xen_cpu_intr_init(struct cpu_info *);
307 void
308 xen_cpu_intr_init(struct cpu_info *ci)
309 {
310 int i; /* XXX: duplicate */
311
312 ci->ci_xunmask[0] = 0xfffffffe;
313 for (i = 1; i < NIPL; i++)
314 ci->ci_xunmask[i] = ci->ci_xunmask[i - 1] & ~(1 << i);
315
316 #if defined(INTRSTACKSIZE)
317 vaddr_t istack;
318
319 /*
320 * If the red zone is activated, protect both the top and
321 * the bottom of the stack with an unmapped page.
322 */
323 istack = uvm_km_alloc(kernel_map,
324 INTRSTACKSIZE + redzone_const_or_zero(2 * PAGE_SIZE), 0,
325 UVM_KMF_WIRED|UVM_KMF_ZERO);
326 if (redzone_const_or_false(true)) {
327 pmap_kremove(istack, PAGE_SIZE);
328 pmap_kremove(istack + INTRSTACKSIZE + PAGE_SIZE, PAGE_SIZE);
329 pmap_update(pmap_kernel());
330 }
331
332 /*
333 * 33 used to be 1. Arbitrarily reserve 32 more register_t's
334 * of space for ddb(4) to examine some subroutine arguments
335 * and to hunt for the next stack frame.
336 */
337 ci->ci_intrstack = (char *)istack + redzone_const_or_zero(PAGE_SIZE) +
338 INTRSTACKSIZE - 33 * sizeof(register_t);
339 #endif
340
341 ci->ci_idepth = -1;
342 }
343
344 /*
345 * Everything below from here is duplicated from x86/intr.c
346 * When intr.c and xen_intr.c are unified, these will need to be
347 * merged.
348 */
349
350 u_int xen_cpu_intr_count(struct cpu_info *ci);
351
352 u_int
353 xen_cpu_intr_count(struct cpu_info *ci)
354 {
355
356 KASSERT(ci->ci_nintrhand >= 0);
357
358 return ci->ci_nintrhand;
359 }
360
361 static const char *
362 xen_intr_string(int port, char *buf, size_t len, struct pic *pic)
363 {
364 KASSERT(pic->pic_type == PIC_XEN);
365
366 KASSERT(port >= 0);
367 KASSERT(port < NR_EVENT_CHANNELS);
368
369 snprintf(buf, len, "%s channel %d", pic->pic_name, port);
370
371 return buf;
372 }
373
374 static const char *
375 legacy_intr_string(int ih, char *buf, size_t len, struct pic *pic)
376 {
377 int legacy_irq;
378
379 KASSERT(pic->pic_type == PIC_I8259);
380 #if NLAPIC > 0
381 KASSERT(APIC_IRQ_ISLEGACY(ih));
382
383 legacy_irq = APIC_IRQ_LEGACY_IRQ(ih);
384 #else
385 legacy_irq = ih;
386 #endif
387 KASSERT(legacy_irq >= 0 && legacy_irq < 16);
388
389 snprintf(buf, len, "%s pin %d", pic->pic_name, legacy_irq);
390
391 return buf;
392 }
393
394 const char * xintr_string(intr_handle_t ih, char *buf, size_t len);
395
396 const char *
397 xintr_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 * xen_intr_create_intrid(int legacy_irq, struct pic *pic,
436 int pin, char *buf, size_t len);
437
438 const char *
439 xen_intr_create_intrid(int legacy_irq, struct pic *pic, int pin, char *buf, size_t len)
440 {
441 int ih = 0;
442
443 #if NPCI > 0
444 #if defined(__HAVE_PCI_MSI_MSIX)
445 if ((pic->pic_type == PIC_MSI) || (pic->pic_type == PIC_MSIX)) {
446 uint64_t pih;
447 int dev, vec;
448
449 dev = msipic_get_devid(pic);
450 vec = pin;
451 pih = __SHIFTIN((uint64_t)dev, MSI_INT_DEV_MASK)
452 | __SHIFTIN((uint64_t)vec, MSI_INT_VEC_MASK)
453 | APIC_INT_VIA_MSI;
454 if (pic->pic_type == PIC_MSI)
455 MSI_INT_MAKE_MSI(pih);
456 else if (pic->pic_type == PIC_MSIX)
457 MSI_INT_MAKE_MSIX(pih);
458
459 return x86_pci_msi_string(NULL, pih, buf, len);
460 }
461 #endif /* __HAVE_PCI_MSI_MSIX */
462 #endif
463
464 if (pic->pic_type == PIC_XEN) {
465 ih = pin; /* Port == pin */
466 return xen_intr_string(pin, buf, len, pic);
467 }
468
469 /*
470 * If the device is pci, "legacy_irq" is alway -1. Least 8 bit of "ih"
471 * is only used in intr_string() to show the irq number.
472 * If the device is "legacy"(such as floppy), it should not use
473 * intr_string().
474 */
475 if (pic->pic_type == PIC_I8259) {
476 ih = legacy_irq;
477 return legacy_intr_string(ih, buf, len, pic);
478 }
479
480 #if NIOAPIC > 0 || NACPICA > 0
481 ih = ((pic->pic_apicid << APIC_INT_APIC_SHIFT) & APIC_INT_APIC_MASK)
482 | ((pin << APIC_INT_PIN_SHIFT) & APIC_INT_PIN_MASK);
483 if (pic->pic_type == PIC_IOAPIC) {
484 ih |= APIC_INT_VIA_APIC;
485 }
486 ih |= pin;
487 return intr_string(ih, buf, len);
488 #endif
489
490 return NULL; /* No pic found! */
491 }
492
493 #if !defined(XENPVHVM)
494 __strong_alias(spllower, xen_spllower);
495 __strong_alias(x86_disable_intr, xen_disable_intr);
496 __strong_alias(x86_enable_intr, xen_enable_intr);
497 __strong_alias(x86_read_psl, xen_read_psl);
498 __strong_alias(x86_write_psl, xen_write_psl);
499
500 __strong_alias(intr_string, xintr_string);
501 __strong_alias(intr_create_intrid, xen_intr_create_intrid);
502 __strong_alias(intr_establish, xen_intr_establish);
503 __strong_alias(intr_establish_xname, xen_intr_establish_xname);
504 __strong_alias(intr_disestablish, xen_intr_disestablish);
505 __strong_alias(cpu_intr_redistribute, xen_cpu_intr_redistribute);
506 __strong_alias(cpu_intr_count, xen_cpu_intr_count);
507 __strong_alias(cpu_intr_init, xen_cpu_intr_init);
508 #endif /* !XENPVHVM */
509