xref: /netbsd-src/sys/arch/xen/x86/pintr.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	NetBSD: intr.c,v 1.15 2004/04/10 14:49:55 kochi Exp 	*/
2 
3 /*
4  * Copyright 2002 (c) Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Written by Frank van der Linden for Wasabi Systems, Inc.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed for the NetBSD Project by
20  *      Wasabi Systems, Inc.
21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22  *    or promote products derived from this software without specific prior
23  *    written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 /*-
39  * Copyright (c) 1991 The Regents of the University of California.
40  * All rights reserved.
41  *
42  * This code is derived from software contributed to Berkeley by
43  * William Jolitz.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  * 1. Redistributions of source code must retain the above copyright
49  *    notice, this list of conditions and the following disclaimer.
50  * 2. Redistributions in binary form must reproduce the above copyright
51  *    notice, this list of conditions and the following disclaimer in the
52  *    documentation and/or other materials provided with the distribution.
53  * 3. Neither the name of the University nor the names of its contributors
54  *    may be used to endorse or promote products derived from this software
55  *    without specific prior written permission.
56  *
57  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67  * SUCH DAMAGE.
68  *
69  *     @(#)isa.c       7.2 (Berkeley) 5/13/91
70  */
71 /*-
72  * Copyright (c) 1993, 1994 Charles Hannum.
73  *
74  * Redistribution and use in source and binary forms, with or without
75  * modification, are permitted provided that the following conditions
76  * are met:
77  * 1. Redistributions of source code must retain the above copyright
78  *    notice, this list of conditions and the following disclaimer.
79  * 2. Redistributions in binary form must reproduce the above copyright
80  *    notice, this list of conditions and the following disclaimer in the
81  *    documentation and/or other materials provided with the distribution.
82  * 3. All advertising materials mentioning features or use of this software
83  *    must display the following acknowledgement:
84  *     This product includes software developed by the University of
85  *     California, Berkeley and its contributors.
86  * 4. Neither the name of the University nor the names of its contributors
87  *    may be used to endorse or promote products derived from this software
88  *    without specific prior written permission.
89  *
90  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
91  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
92  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
93  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
94  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
95  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
96  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
97  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
98  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
99  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
100  * SUCH DAMAGE.
101  *
102  *     @(#)isa.c       7.2 (Berkeley) 5/13/91
103  */
104 
105 #include <sys/cdefs.h>
106 __KERNEL_RCSID(0, "$NetBSD: pintr.c,v 1.3 2018/02/17 18:51:53 maxv Exp $");
107 
108 #include "opt_multiprocessor.h"
109 #include "opt_xen.h"
110 #include "isa.h"
111 #include "pci.h"
112 
113 #include <sys/param.h>
114 #include <sys/systm.h>
115 #include <sys/kernel.h>
116 #include <sys/syslog.h>
117 #include <sys/device.h>
118 #include <sys/malloc.h>
119 #include <sys/proc.h>
120 #include <sys/errno.h>
121 #include <sys/cpu.h>
122 
123 #include <uvm/uvm_extern.h>
124 
125 #include <machine/i8259.h>
126 #include <machine/pio.h>
127 #include <xen/evtchn.h>
128 #include <xen/intr.h>
129 
130 #include "acpica.h"
131 #include "ioapic.h"
132 #include "opt_mpbios.h"
133 
134 #if NIOAPIC > 0
135 /* XXX: todo - compat with lapic.c and XEN for x2apic */
136 bool x2apic_mode __read_mostly = false;
137 /* for x86/i8259.c */
138 struct intrstub legacy_stubs[NUM_LEGACY_IRQS] = {{0,0}};
139 /* for x86/ioapic.c */
140 struct intrstub ioapic_edge_stubs[MAX_INTR_SOURCES] = {{0,0}};
141 struct intrstub ioapic_level_stubs[MAX_INTR_SOURCES] = {{0,0}};
142 struct intrstub x2apic_edge_stubs[MAX_INTR_SOURCES] = {{0,0}};
143 struct intrstub x2apic_level_stubs[MAX_INTR_SOURCES] = {{0,0}};
144 #include <machine/i82093var.h>
145 int irq2port[NR_EVENT_CHANNELS] = {0}; /* actually port + 1, so that 0 is invaid */
146 int irq2vect[256] = {0};
147 int vect2irq[256] = {0};
148 #endif /* NIOAPIC */
149 #if NACPICA > 0
150 #include <machine/mpconfig.h>
151 #include <machine/mpacpi.h>
152 #endif
153 #ifdef MPBIOS
154 #include <machine/mpbiosvar.h>
155 #endif
156 
157 #if NPCI > 0
158 #include <dev/pci/ppbreg.h>
159 #endif
160 
161 #if defined(DOM0OPS) || NPCI > 0
162 int
163 xen_pirq_alloc(intr_handle_t *pirq, int type)
164 {
165 	physdev_op_t op;
166 	int irq = *pirq;
167 #if NIOAPIC > 0
168 	extern struct cpu_info phycpu_info_primary; /* XXX */
169 	/*
170 	 * The hypervisor has already allocated vectors and IRQs for the
171 	 * devices. Reusing the same IRQ doesn't work because as we bind
172 	 * them for each devices, we can't then change the route entry
173 	 * of the next device if this one used this IRQ. The easiest is
174 	 * to allocate IRQs top-down, starting with a high number.
175 	 * 250 and 230 have been tried, but got rejected by Xen.
176 	 *
177 	 * Xen 3.5 also rejects 200. Try out all values until Xen accepts
178 	 * or none is available.
179 	 */
180 	static int xen_next_irq = 200;
181 	struct ioapic_softc *ioapic = ioapic_find(APIC_IRQ_APIC(*pirq));
182 	struct pic *pic = &ioapic->sc_pic;
183 	int pin = APIC_IRQ_PIN(*pirq);
184 
185 	if (*pirq & APIC_INT_VIA_APIC) {
186 		irq = vect2irq[ioapic->sc_pins[pin].ip_vector];
187 		if (ioapic->sc_pins[pin].ip_vector == 0 || irq == 0) {
188 			/* allocate IRQ */
189 			irq = APIC_IRQ_LEGACY_IRQ(*pirq);
190 			if (irq <= 0 || irq > 15)
191 				irq = xen_next_irq--;
192 retry:
193 			/* allocate vector and route interrupt */
194 			op.cmd = PHYSDEVOP_ASSIGN_VECTOR;
195 			op.u.irq_op.irq = irq;
196 			if (HYPERVISOR_physdev_op(&op) < 0) {
197 				irq = xen_next_irq--;
198 				if (xen_next_irq == 15)
199 					panic("PHYSDEVOP_ASSIGN_VECTOR irq %d", irq);
200 				goto retry;
201 			}
202 			KASSERT(irq2vect[irq] == 0);
203 			irq2vect[irq] = op.u.irq_op.vector;
204 			KASSERT(vect2irq[op.u.irq_op.vector] == 0);
205 			vect2irq[op.u.irq_op.vector] = irq;
206 			pic->pic_addroute(pic, &phycpu_info_primary, pin,
207 			    op.u.irq_op.vector, type);
208 		}
209 		*pirq &= ~0xff;
210 		*pirq |= irq;
211 	} else
212 #endif /* NIOAPIC */
213 	{
214 		if (irq2port[irq] == 0) {
215 			op.cmd = PHYSDEVOP_ASSIGN_VECTOR;
216 			op.u.irq_op.irq = irq;
217 			if (HYPERVISOR_physdev_op(&op) < 0) {
218 				panic("PHYSDEVOP_ASSIGN_VECTOR irq %d", irq);
219 			}
220 			KASSERT(irq2vect[irq] == 0);
221 			irq2vect[irq] = op.u.irq_op.vector;
222 			KASSERT(vect2irq[op.u.irq_op.vector] == 0);
223 			vect2irq[op.u.irq_op.vector] = irq;
224 			KASSERT(irq2port[irq] == 0);
225 			irq2port[irq] = bind_pirq_to_evtch(irq) + 1;
226 		}
227 	}
228 	KASSERT(irq2port[irq] > 0);
229 	return (irq2port[irq] - 1);
230 }
231 #endif /* defined(DOM0OPS) || NPCI > 0 */
232