pci_intr_machdep.c revision 1.28 1 /* $NetBSD: pci_intr_machdep.c,v 1.28 2015/04/27 06:42:52 knakahara 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.28 2015/04/27 06:42:52 knakahara 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/cpu.h>
83 #include <sys/errno.h>
84 #include <sys/device.h>
85 #include <sys/intr.h>
86 #include <sys/kmem.h>
87 #include <sys/malloc.h>
88
89 #include <dev/pci/pcivar.h>
90
91 #include "ioapic.h"
92 #include "eisa.h"
93 #include "acpica.h"
94 #include "opt_mpbios.h"
95 #include "opt_acpi.h"
96
97 #include <machine/i82489reg.h>
98
99 #if NIOAPIC > 0 || NACPICA > 0
100 #include <machine/i82093reg.h>
101 #include <machine/i82093var.h>
102 #include <machine/mpconfig.h>
103 #include <machine/mpbiosvar.h>
104 #include <machine/pic.h>
105 #endif
106
107 #ifdef MPBIOS
108 #include <machine/mpbiosvar.h>
109 #endif
110
111 #if NACPICA > 0
112 #include <machine/mpacpi.h>
113 #endif
114
115 #define MPSAFE_MASK 0x80000000
116
117 int
118 pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
119 {
120 int pin = pa->pa_intrpin;
121 int line = pa->pa_intrline;
122 pci_chipset_tag_t ipc, pc = pa->pa_pc;
123 #if NIOAPIC > 0 || NACPICA > 0
124 int rawpin = pa->pa_rawintrpin;
125 int bus, dev, func;
126 #endif
127
128 for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) {
129 if ((ipc->pc_present & PCI_OVERRIDE_INTR_MAP) == 0)
130 continue;
131 return (*ipc->pc_ov->ov_intr_map)(ipc->pc_ctx, pa, ihp);
132 }
133
134 if (pin == 0) {
135 /* No IRQ used. */
136 goto bad;
137 }
138
139 *ihp = 0;
140
141 if (pin > PCI_INTERRUPT_PIN_MAX) {
142 aprint_normal("pci_intr_map: bad interrupt pin %d\n", pin);
143 goto bad;
144 }
145
146 #if NIOAPIC > 0 || NACPICA > 0
147 KASSERT(rawpin >= PCI_INTERRUPT_PIN_A);
148 KASSERT(rawpin <= PCI_INTERRUPT_PIN_D);
149 pci_decompose_tag(pc, pa->pa_tag, &bus, &dev, &func);
150 if (mp_busses != NULL) {
151 /*
152 * Note: PCI_INTERRUPT_PIN_A == 1 where intr_find_mpmapping
153 * wants pci bus_pin encoding which uses INT_A == 0.
154 */
155 if (intr_find_mpmapping(bus,
156 (dev << 2) | (rawpin - PCI_INTERRUPT_PIN_A), ihp) == 0) {
157 if (APIC_IRQ_LEGACY_IRQ(*ihp) == 0)
158 *ihp |= line;
159 return 0;
160 }
161 /*
162 * No explicit PCI mapping found. This is not fatal,
163 * we'll try the ISA (or possibly EISA) mappings next.
164 */
165 }
166 #endif
167
168 /*
169 * Section 6.2.4, `Miscellaneous Functions', says that 255 means
170 * `unknown' or `no connection' on a PC. We assume that a device with
171 * `no connection' either doesn't have an interrupt (in which case the
172 * pin number should be 0, and would have been noticed above), or
173 * wasn't configured by the BIOS (in which case we punt, since there's
174 * no real way we can know how the interrupt lines are mapped in the
175 * hardware).
176 *
177 * XXX
178 * Since IRQ 0 is only used by the clock, and we can't actually be sure
179 * that the BIOS did its job, we also recognize that as meaning that
180 * the BIOS has not configured the device.
181 */
182 if (line == 0 || line == X86_PCI_INTERRUPT_LINE_NO_CONNECTION) {
183 aprint_normal("pci_intr_map: no mapping for pin %c (line=%02x)\n",
184 '@' + pin, line);
185 goto bad;
186 } else {
187 if (line >= NUM_LEGACY_IRQS) {
188 aprint_normal("pci_intr_map: bad interrupt line %d\n", line);
189 goto bad;
190 }
191 if (line == 2) {
192 aprint_normal("pci_intr_map: changed line 2 to line 9\n");
193 line = 9;
194 }
195 }
196 #if NIOAPIC > 0 || NACPICA > 0
197 if (mp_busses != NULL) {
198 if (intr_find_mpmapping(mp_isa_bus, line, ihp) == 0) {
199 if ((*ihp & 0xff) == 0)
200 *ihp |= line;
201 return 0;
202 }
203 #if NEISA > 0
204 if (intr_find_mpmapping(mp_eisa_bus, line, ihp) == 0) {
205 if ((*ihp & 0xff) == 0)
206 *ihp |= line;
207 return 0;
208 }
209 #endif
210 aprint_normal("pci_intr_map: bus %d dev %d func %d pin %d; line %d\n",
211 bus, dev, func, pin, line);
212 aprint_normal("pci_intr_map: no MP mapping found\n");
213 }
214 #endif
215
216 *ihp = line;
217 return 0;
218
219 bad:
220 *ihp = -1;
221 return 1;
222 }
223
224 const char *
225 pci_intr_string(pci_chipset_tag_t pc, pci_intr_handle_t ih, char *buf,
226 size_t len)
227 {
228 pci_chipset_tag_t ipc;
229
230 for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) {
231 if ((ipc->pc_present & PCI_OVERRIDE_INTR_STRING) == 0)
232 continue;
233 return (*ipc->pc_ov->ov_intr_string)(ipc->pc_ctx, pc, ih,
234 buf, len);
235 }
236
237 return intr_string(ih & ~MPSAFE_MASK, buf, len);
238 }
239
240
241 const struct evcnt *
242 pci_intr_evcnt(pci_chipset_tag_t pc, pci_intr_handle_t ih)
243 {
244 pci_chipset_tag_t ipc;
245
246 for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) {
247 if ((ipc->pc_present & PCI_OVERRIDE_INTR_EVCNT) == 0)
248 continue;
249 return (*ipc->pc_ov->ov_intr_evcnt)(ipc->pc_ctx, pc, ih);
250 }
251
252 /* XXX for now, no evcnt parent reported */
253 return NULL;
254 }
255
256 int
257 pci_intr_setattr(pci_chipset_tag_t pc, pci_intr_handle_t *ih,
258 int attr, uint64_t data)
259 {
260
261 switch (attr) {
262 case PCI_INTR_MPSAFE:
263 if (data) {
264 *ih |= MPSAFE_MASK;
265 } else {
266 *ih &= ~MPSAFE_MASK;
267 }
268 /* XXX Set live if already mapped. */
269 return 0;
270 default:
271 return ENODEV;
272 }
273 }
274
275 void *
276 pci_intr_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih,
277 int level, int (*func)(void *), void *arg)
278 {
279 int pin, irq;
280 struct pic *pic;
281 #if NIOAPIC > 0
282 struct ioapic_softc *ioapic;
283 #endif
284 bool mpsafe;
285 pci_chipset_tag_t ipc;
286
287 for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) {
288 if ((ipc->pc_present & PCI_OVERRIDE_INTR_ESTABLISH) == 0)
289 continue;
290 return (*ipc->pc_ov->ov_intr_establish)(ipc->pc_ctx,
291 pc, ih, level, func, arg);
292 }
293
294 pic = &i8259_pic;
295 pin = irq = (ih & ~MPSAFE_MASK);
296 mpsafe = ((ih & MPSAFE_MASK) != 0);
297
298 #if NIOAPIC > 0
299 if (ih & APIC_INT_VIA_APIC) {
300 ioapic = ioapic_find(APIC_IRQ_APIC(ih));
301 if (ioapic == NULL) {
302 aprint_normal("pci_intr_establish: bad ioapic %d\n",
303 APIC_IRQ_APIC(ih));
304 return NULL;
305 }
306 pic = &ioapic->sc_pic;
307 pin = APIC_IRQ_PIN(ih);
308 irq = APIC_IRQ_LEGACY_IRQ(ih);
309 if (irq < 0 || irq >= NUM_LEGACY_IRQS)
310 irq = -1;
311 }
312 #endif
313
314 return intr_establish(irq, pic, pin, IST_LEVEL, level, func, arg,
315 mpsafe);
316 }
317
318 void
319 pci_intr_disestablish(pci_chipset_tag_t pc, void *cookie)
320 {
321 pci_chipset_tag_t ipc;
322
323 for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) {
324 if ((ipc->pc_present & PCI_OVERRIDE_INTR_DISESTABLISH) == 0)
325 continue;
326 (*ipc->pc_ov->ov_intr_disestablish)(ipc->pc_ctx, pc, cookie);
327 return;
328 }
329
330 intr_disestablish(cookie);
331 }
332
333 int
334 pci_intr_distribute(void *cookie, const kcpuset_t *newset, kcpuset_t *oldset)
335 {
336
337 /* XXX Is pc_ov->ov_intr_distribute required? */
338
339 return intr_distribute(cookie, newset, oldset);
340 }
341
342 #if NIOAPIC > 0
343 /*
344 * experimental support for MSI, does support a single vector,
345 * no MSI-X, 8-bit APIC IDs
346 * (while it doesn't need the ioapic technically, it borrows
347 * from its kernel support)
348 */
349
350 /* dummies, needed by common intr_establish code */
351 static void
352 msipic_hwmask(struct pic *pic, int pin)
353 {
354 }
355 static void
356 msipic_addroute(struct pic *pic, struct cpu_info *ci,
357 int pin, int vec, int type)
358 {
359 }
360
361 static struct pic msi_pic = {
362 .pic_name = "msi",
363 .pic_type = PIC_SOFT,
364 .pic_vecbase = 0,
365 .pic_apicid = 0,
366 .pic_lock = __SIMPLELOCK_UNLOCKED,
367 .pic_hwmask = msipic_hwmask,
368 .pic_hwunmask = msipic_hwmask,
369 .pic_addroute = msipic_addroute,
370 .pic_delroute = msipic_addroute,
371 .pic_edge_stubs = ioapic_edge_stubs,
372 };
373
374 struct msi_hdl {
375 struct intrhand *ih;
376 pci_chipset_tag_t pc;
377 pcitag_t tag;
378 int co;
379 };
380
381 void *
382 pci_msi_establish(struct pci_attach_args *pa, int level,
383 int (*func)(void *), void *arg)
384 {
385 int co;
386 struct intrhand *ih;
387 struct msi_hdl *msih;
388 struct cpu_info *ci;
389 struct intrsource *is;
390 pcireg_t reg;
391
392 if (!pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_MSI, &co, 0))
393 return NULL;
394
395 ih = intr_establish(-1, &msi_pic, -1, IST_EDGE, level, func, arg, 0);
396 if (ih == NULL)
397 return NULL;
398
399 msih = malloc(sizeof(*msih), M_DEVBUF, M_WAITOK);
400 msih->ih = ih;
401 msih->pc = pa->pa_pc;
402 msih->tag = pa->pa_tag;
403 msih->co = co;
404
405 ci = ih->ih_cpu;
406 is = ci->ci_isources[ih->ih_slot];
407 reg = pci_conf_read(pa->pa_pc, pa->pa_tag, co + PCI_MSI_CTL);
408 pci_conf_write(pa->pa_pc, pa->pa_tag, co + PCI_MSI_MADDR64_LO,
409 LAPIC_MSIADDR_BASE |
410 __SHIFTIN(ci->ci_cpuid, LAPIC_MSIADDR_DSTID_MASK));
411 if (reg & PCI_MSI_CTL_64BIT_ADDR) {
412 pci_conf_write(pa->pa_pc, pa->pa_tag, co + PCI_MSI_MADDR64_HI,
413 0);
414 /* XXX according to the manual, ASSERT is unnecessary if
415 * EDGE
416 */
417 pci_conf_write(pa->pa_pc, pa->pa_tag, co + PCI_MSI_MDATA64,
418 __SHIFTIN(is->is_idtvec, LAPIC_MSIDATA_VECTOR_MASK) |
419 LAPIC_MSIDATA_TRGMODE_EDGE | LAPIC_MSIDATA_LEVEL_ASSERT |
420 LAPIC_MSIDATA_DM_FIXED);
421 } else {
422 /* XXX according to the manual, ASSERT is unnecessary if
423 * EDGE
424 */
425 pci_conf_write(pa->pa_pc, pa->pa_tag, co + PCI_MSI_MDATA,
426 __SHIFTIN(is->is_idtvec, LAPIC_MSIDATA_VECTOR_MASK) |
427 LAPIC_MSIDATA_TRGMODE_EDGE | LAPIC_MSIDATA_LEVEL_ASSERT |
428 LAPIC_MSIDATA_DM_FIXED);
429 }
430 pci_conf_write(pa->pa_pc, pa->pa_tag, co + PCI_MSI_CTL,
431 PCI_MSI_CTL_MSI_ENABLE);
432 return msih;
433 }
434
435 void
436 pci_msi_disestablish(void *ih)
437 {
438 struct msi_hdl *msih = ih;
439
440 pci_conf_write(msih->pc, msih->tag, msih->co + PCI_MSI_CTL, 0);
441 intr_disestablish(msih->ih);
442 free(msih, M_DEVBUF);
443 }
444 #endif
445