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