xref: /netbsd-src/sys/arch/x86/pci/pci_intr_machdep.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: pci_intr_machdep.c,v 1.27 2014/03/29 19:28:30 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997, 1998, 2009 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
35  * Copyright (c) 1994 Charles M. Hannum.  All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. All advertising materials mentioning features or use of this software
46  *    must display the following acknowledgement:
47  *	This product includes software developed by Charles M. Hannum.
48  * 4. The name of the author may not be used to endorse or promote products
49  *    derived from this software without specific prior written permission.
50  *
51  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
52  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
53  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
54  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
55  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
60  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61  */
62 
63 /*
64  * Machine-specific functions for PCI autoconfiguration.
65  *
66  * On PCs, there are two methods of generating PCI configuration cycles.
67  * We try to detect the appropriate mechanism for this machine and set
68  * up a few function pointers to access the correct method directly.
69  *
70  * The configuration method can be hard-coded in the config file by
71  * using `options PCI_CONF_MODE=N', where `N' is the configuration mode
72  * as defined section 3.6.4.1, `Generating Configuration Cycles'.
73  */
74 
75 #include <sys/cdefs.h>
76 __KERNEL_RCSID(0, "$NetBSD: pci_intr_machdep.c,v 1.27 2014/03/29 19:28:30 christos Exp $");
77 
78 #include <sys/types.h>
79 #include <sys/param.h>
80 #include <sys/time.h>
81 #include <sys/systm.h>
82 #include <sys/errno.h>
83 #include <sys/device.h>
84 #include <sys/intr.h>
85 #include <sys/malloc.h>
86 
87 #include <dev/pci/pcivar.h>
88 
89 #include "ioapic.h"
90 #include "eisa.h"
91 #include "acpica.h"
92 #include "opt_mpbios.h"
93 #include "opt_acpi.h"
94 
95 #include <machine/i82489reg.h>
96 
97 #if NIOAPIC > 0 || NACPICA > 0
98 #include <machine/i82093reg.h>
99 #include <machine/i82093var.h>
100 #include <machine/mpconfig.h>
101 #include <machine/mpbiosvar.h>
102 #include <machine/pic.h>
103 #endif
104 
105 #ifdef MPBIOS
106 #include <machine/mpbiosvar.h>
107 #endif
108 
109 #if NACPICA > 0
110 #include <machine/mpacpi.h>
111 #endif
112 
113 #define	MPSAFE_MASK	0x80000000
114 
115 int
116 pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
117 {
118 	int pin = pa->pa_intrpin;
119 	int line = pa->pa_intrline;
120 	pci_chipset_tag_t ipc, pc = pa->pa_pc;
121 #if NIOAPIC > 0 || NACPICA > 0
122 	int rawpin = pa->pa_rawintrpin;
123 	int bus, dev, func;
124 #endif
125 
126 	for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) {
127 		if ((ipc->pc_present & PCI_OVERRIDE_INTR_MAP) == 0)
128 			continue;
129 		return (*ipc->pc_ov->ov_intr_map)(ipc->pc_ctx, pa, ihp);
130 	}
131 
132 	if (pin == 0) {
133 		/* No IRQ used. */
134 		goto bad;
135 	}
136 
137 	*ihp = 0;
138 
139 	if (pin > PCI_INTERRUPT_PIN_MAX) {
140 		aprint_normal("pci_intr_map: bad interrupt pin %d\n", pin);
141 		goto bad;
142 	}
143 
144 #if NIOAPIC > 0 || NACPICA > 0
145 	KASSERT(rawpin >= PCI_INTERRUPT_PIN_A);
146 	KASSERT(rawpin <= PCI_INTERRUPT_PIN_D);
147 	pci_decompose_tag(pc, pa->pa_tag, &bus, &dev, &func);
148 	if (mp_busses != NULL) {
149 		/*
150 		 * Note: PCI_INTERRUPT_PIN_A == 1 where intr_find_mpmapping
151 		 * wants pci bus_pin encoding which uses INT_A == 0.
152 		 */
153 		if (intr_find_mpmapping(bus,
154 		    (dev << 2) | (rawpin - PCI_INTERRUPT_PIN_A), ihp) == 0) {
155 			if (APIC_IRQ_LEGACY_IRQ(*ihp) == 0)
156 				*ihp |= line;
157 			return 0;
158 		}
159 		/*
160 		 * No explicit PCI mapping found. This is not fatal,
161 		 * we'll try the ISA (or possibly EISA) mappings next.
162 		 */
163 	}
164 #endif
165 
166 	/*
167 	 * Section 6.2.4, `Miscellaneous Functions', says that 255 means
168 	 * `unknown' or `no connection' on a PC.  We assume that a device with
169 	 * `no connection' either doesn't have an interrupt (in which case the
170 	 * pin number should be 0, and would have been noticed above), or
171 	 * wasn't configured by the BIOS (in which case we punt, since there's
172 	 * no real way we can know how the interrupt lines are mapped in the
173 	 * hardware).
174 	 *
175 	 * XXX
176 	 * Since IRQ 0 is only used by the clock, and we can't actually be sure
177 	 * that the BIOS did its job, we also recognize that as meaning that
178 	 * the BIOS has not configured the device.
179 	 */
180 	if (line == 0 || line == X86_PCI_INTERRUPT_LINE_NO_CONNECTION) {
181 		aprint_normal("pci_intr_map: no mapping for pin %c (line=%02x)\n",
182 		       '@' + pin, line);
183 		goto bad;
184 	} else {
185 		if (line >= NUM_LEGACY_IRQS) {
186 			aprint_normal("pci_intr_map: bad interrupt line %d\n", line);
187 			goto bad;
188 		}
189 		if (line == 2) {
190 			aprint_normal("pci_intr_map: changed line 2 to line 9\n");
191 			line = 9;
192 		}
193 	}
194 #if NIOAPIC > 0 || NACPICA > 0
195 	if (mp_busses != NULL) {
196 		if (intr_find_mpmapping(mp_isa_bus, line, ihp) == 0) {
197 			if ((*ihp & 0xff) == 0)
198 				*ihp |= line;
199 			return 0;
200 		}
201 #if NEISA > 0
202 		if (intr_find_mpmapping(mp_eisa_bus, line, ihp) == 0) {
203 			if ((*ihp & 0xff) == 0)
204 				*ihp |= line;
205 			return 0;
206 		}
207 #endif
208 		aprint_normal("pci_intr_map: bus %d dev %d func %d pin %d; line %d\n",
209 		    bus, dev, func, pin, line);
210 		aprint_normal("pci_intr_map: no MP mapping found\n");
211 	}
212 #endif
213 
214 	*ihp = line;
215 	return 0;
216 
217 bad:
218 	*ihp = -1;
219 	return 1;
220 }
221 
222 const char *
223 pci_intr_string(pci_chipset_tag_t pc, pci_intr_handle_t ih, char *buf,
224     size_t len)
225 {
226 	pci_chipset_tag_t ipc;
227 
228 	for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) {
229 		if ((ipc->pc_present & PCI_OVERRIDE_INTR_STRING) == 0)
230 			continue;
231 		return (*ipc->pc_ov->ov_intr_string)(ipc->pc_ctx, pc, ih,
232 		    buf, len);
233 	}
234 
235 	return intr_string(ih & ~MPSAFE_MASK, buf, len);
236 }
237 
238 
239 const struct evcnt *
240 pci_intr_evcnt(pci_chipset_tag_t pc, pci_intr_handle_t ih)
241 {
242 	pci_chipset_tag_t ipc;
243 
244 	for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) {
245 		if ((ipc->pc_present & PCI_OVERRIDE_INTR_EVCNT) == 0)
246 			continue;
247 		return (*ipc->pc_ov->ov_intr_evcnt)(ipc->pc_ctx, pc, ih);
248 	}
249 
250 	/* XXX for now, no evcnt parent reported */
251 	return NULL;
252 }
253 
254 int
255 pci_intr_setattr(pci_chipset_tag_t pc, pci_intr_handle_t *ih,
256 		 int attr, uint64_t data)
257 {
258 
259 	switch (attr) {
260 	case PCI_INTR_MPSAFE:
261 		if (data) {
262 			 *ih |= MPSAFE_MASK;
263 		} else {
264 			 *ih &= ~MPSAFE_MASK;
265 		}
266 		/* XXX Set live if already mapped. */
267 		return 0;
268 	default:
269 		return ENODEV;
270 	}
271 }
272 
273 void *
274 pci_intr_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih,
275     int level, int (*func)(void *), void *arg)
276 {
277 	int pin, irq;
278 	struct pic *pic;
279 #if NIOAPIC > 0
280 	struct ioapic_softc *ioapic;
281 #endif
282 	bool mpsafe;
283 	pci_chipset_tag_t ipc;
284 
285 	for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) {
286 		if ((ipc->pc_present & PCI_OVERRIDE_INTR_ESTABLISH) == 0)
287 			continue;
288 		return (*ipc->pc_ov->ov_intr_establish)(ipc->pc_ctx,
289 		    pc, ih, level, func, arg);
290 	}
291 
292 	pic = &i8259_pic;
293 	pin = irq = (ih & ~MPSAFE_MASK);
294 	mpsafe = ((ih & MPSAFE_MASK) != 0);
295 
296 #if NIOAPIC > 0
297 	if (ih & APIC_INT_VIA_APIC) {
298 		ioapic = ioapic_find(APIC_IRQ_APIC(ih));
299 		if (ioapic == NULL) {
300 			aprint_normal("pci_intr_establish: bad ioapic %d\n",
301 			    APIC_IRQ_APIC(ih));
302 			return NULL;
303 		}
304 		pic = &ioapic->sc_pic;
305 		pin = APIC_IRQ_PIN(ih);
306 		irq = APIC_IRQ_LEGACY_IRQ(ih);
307 		if (irq < 0 || irq >= NUM_LEGACY_IRQS)
308 			irq = -1;
309 	}
310 #endif
311 
312 	return intr_establish(irq, pic, pin, IST_LEVEL, level, func, arg,
313 	    mpsafe);
314 }
315 
316 void
317 pci_intr_disestablish(pci_chipset_tag_t pc, void *cookie)
318 {
319 	pci_chipset_tag_t ipc;
320 
321 	for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) {
322 		if ((ipc->pc_present & PCI_OVERRIDE_INTR_DISESTABLISH) == 0)
323 			continue;
324 		(*ipc->pc_ov->ov_intr_disestablish)(ipc->pc_ctx, pc, cookie);
325 		return;
326 	}
327 
328 	intr_disestablish(cookie);
329 }
330 
331 #if NIOAPIC > 0
332 /*
333  * experimental support for MSI, does support a single vector,
334  * no MSI-X, 8-bit APIC IDs
335  * (while it doesn't need the ioapic technically, it borrows
336  * from its kernel support)
337  */
338 
339 /* dummies, needed by common intr_establish code */
340 static void
341 msipic_hwmask(struct pic *pic, int pin)
342 {
343 }
344 static void
345 msipic_addroute(struct pic *pic, struct cpu_info *ci,
346 		int pin, int vec, int type)
347 {
348 }
349 
350 static struct pic msi_pic = {
351 	.pic_name = "msi",
352 	.pic_type = PIC_SOFT,
353 	.pic_vecbase = 0,
354 	.pic_apicid = 0,
355 	.pic_lock = __SIMPLELOCK_UNLOCKED,
356 	.pic_hwmask = msipic_hwmask,
357 	.pic_hwunmask = msipic_hwmask,
358 	.pic_addroute = msipic_addroute,
359 	.pic_delroute = msipic_addroute,
360 	.pic_edge_stubs = ioapic_edge_stubs,
361 };
362 
363 struct msi_hdl {
364 	struct intrhand *ih;
365 	pci_chipset_tag_t pc;
366 	pcitag_t tag;
367 	int co;
368 };
369 
370 void *
371 pci_msi_establish(struct pci_attach_args *pa, int level,
372 		  int (*func)(void *), void *arg)
373 {
374 	int co;
375 	struct intrhand *ih;
376 	struct msi_hdl *msih;
377 	struct cpu_info *ci;
378 	struct intrsource *is;
379 	pcireg_t reg;
380 
381 	if (!pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_MSI, &co, 0))
382 		return NULL;
383 
384 	ih = intr_establish(-1, &msi_pic, -1, IST_EDGE, level, func, arg, 0);
385 	if (ih == NULL)
386 		return NULL;
387 
388 	msih = malloc(sizeof(*msih), M_DEVBUF, M_WAITOK);
389 	msih->ih = ih;
390 	msih->pc = pa->pa_pc;
391 	msih->tag = pa->pa_tag;
392 	msih->co = co;
393 
394 	ci = ih->ih_cpu;
395 	is = ci->ci_isources[ih->ih_slot];
396 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, co + PCI_MSI_CTL);
397 	pci_conf_write(pa->pa_pc, pa->pa_tag, co + PCI_MSI_MADDR64_LO,
398 		       LAPIC_MSIADDR_BASE |
399 		       __SHIFTIN(ci->ci_cpuid, LAPIC_MSIADDR_DSTID_MASK));
400 	if (reg & PCI_MSI_CTL_64BIT_ADDR) {
401 		pci_conf_write(pa->pa_pc, pa->pa_tag, co + PCI_MSI_MADDR64_HI,
402 		    0);
403 		/* XXX according to the manual, ASSERT is unnecessary if
404 		 * EDGE
405 		 */
406 		pci_conf_write(pa->pa_pc, pa->pa_tag, co + PCI_MSI_MDATA64,
407 		    __SHIFTIN(is->is_idtvec, LAPIC_MSIDATA_VECTOR_MASK) |
408 		    LAPIC_MSIDATA_TRGMODE_EDGE | LAPIC_MSIDATA_LEVEL_ASSERT |
409 		    LAPIC_MSIDATA_DM_FIXED);
410 	} else {
411 		/* XXX according to the manual, ASSERT is unnecessary if
412 		 * EDGE
413 		 */
414 		pci_conf_write(pa->pa_pc, pa->pa_tag, co + PCI_MSI_MDATA,
415 		    __SHIFTIN(is->is_idtvec, LAPIC_MSIDATA_VECTOR_MASK) |
416 		    LAPIC_MSIDATA_TRGMODE_EDGE | LAPIC_MSIDATA_LEVEL_ASSERT |
417 		    LAPIC_MSIDATA_DM_FIXED);
418 	}
419 	pci_conf_write(pa->pa_pc, pa->pa_tag, co + PCI_MSI_CTL,
420 	    PCI_MSI_CTL_MSI_ENABLE);
421 	return msih;
422 }
423 
424 void
425 pci_msi_disestablish(void *ih)
426 {
427 	struct msi_hdl *msih = ih;
428 
429 	pci_conf_write(msih->pc, msih->tag, msih->co + PCI_MSI_CTL, 0);
430 	intr_disestablish(msih->ih);
431 	free(msih, M_DEVBUF);
432 }
433 #endif
434