pci_intr_machdep.c revision 1.37 1 1.37 knakahar /* $NetBSD: pci_intr_machdep.c,v 1.37 2015/08/17 06:16:03 knakahara Exp $ */
2 1.1 bouyer
3 1.1 bouyer /*-
4 1.13 ad * Copyright (c) 1997, 1998, 2009 The NetBSD Foundation, Inc.
5 1.1 bouyer * All rights reserved.
6 1.1 bouyer *
7 1.1 bouyer * This code is derived from software contributed to The NetBSD Foundation
8 1.1 bouyer * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.1 bouyer * NASA Ames Research Center.
10 1.1 bouyer *
11 1.1 bouyer * Redistribution and use in source and binary forms, with or without
12 1.1 bouyer * modification, are permitted provided that the following conditions
13 1.1 bouyer * are met:
14 1.1 bouyer * 1. Redistributions of source code must retain the above copyright
15 1.1 bouyer * notice, this list of conditions and the following disclaimer.
16 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 bouyer * notice, this list of conditions and the following disclaimer in the
18 1.1 bouyer * documentation and/or other materials provided with the distribution.
19 1.1 bouyer *
20 1.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1 bouyer * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1 bouyer * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1 bouyer * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1 bouyer * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1 bouyer * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1 bouyer * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 bouyer * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1 bouyer * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1 bouyer * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 bouyer * POSSIBILITY OF SUCH DAMAGE.
31 1.1 bouyer */
32 1.1 bouyer
33 1.1 bouyer /*
34 1.1 bouyer * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved.
35 1.1 bouyer * Copyright (c) 1994 Charles M. Hannum. All rights reserved.
36 1.1 bouyer *
37 1.1 bouyer * Redistribution and use in source and binary forms, with or without
38 1.1 bouyer * modification, are permitted provided that the following conditions
39 1.1 bouyer * are met:
40 1.1 bouyer * 1. Redistributions of source code must retain the above copyright
41 1.1 bouyer * notice, this list of conditions and the following disclaimer.
42 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
43 1.1 bouyer * notice, this list of conditions and the following disclaimer in the
44 1.1 bouyer * documentation and/or other materials provided with the distribution.
45 1.1 bouyer * 3. All advertising materials mentioning features or use of this software
46 1.1 bouyer * must display the following acknowledgement:
47 1.1 bouyer * This product includes software developed by Charles M. Hannum.
48 1.1 bouyer * 4. The name of the author may not be used to endorse or promote products
49 1.1 bouyer * derived from this software without specific prior written permission.
50 1.1 bouyer *
51 1.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
52 1.1 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
53 1.1 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
54 1.1 bouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
55 1.1 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56 1.1 bouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 1.1 bouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 1.1 bouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 1.1 bouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
60 1.1 bouyer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 1.1 bouyer */
62 1.1 bouyer
63 1.1 bouyer /*
64 1.1 bouyer * Machine-specific functions for PCI autoconfiguration.
65 1.1 bouyer *
66 1.1 bouyer * On PCs, there are two methods of generating PCI configuration cycles.
67 1.1 bouyer * We try to detect the appropriate mechanism for this machine and set
68 1.1 bouyer * up a few function pointers to access the correct method directly.
69 1.1 bouyer *
70 1.1 bouyer * The configuration method can be hard-coded in the config file by
71 1.1 bouyer * using `options PCI_CONF_MODE=N', where `N' is the configuration mode
72 1.1 bouyer * as defined section 3.6.4.1, `Generating Configuration Cycles'.
73 1.1 bouyer */
74 1.1 bouyer
75 1.1 bouyer #include <sys/cdefs.h>
76 1.37 knakahar __KERNEL_RCSID(0, "$NetBSD: pci_intr_machdep.c,v 1.37 2015/08/17 06:16:03 knakahara Exp $");
77 1.1 bouyer
78 1.1 bouyer #include <sys/types.h>
79 1.1 bouyer #include <sys/param.h>
80 1.1 bouyer #include <sys/time.h>
81 1.1 bouyer #include <sys/systm.h>
82 1.28 knakahar #include <sys/cpu.h>
83 1.1 bouyer #include <sys/errno.h>
84 1.1 bouyer #include <sys/device.h>
85 1.7 ad #include <sys/intr.h>
86 1.28 knakahar #include <sys/kmem.h>
87 1.20 drochner #include <sys/malloc.h>
88 1.1 bouyer
89 1.1 bouyer #include <dev/pci/pcivar.h>
90 1.1 bouyer
91 1.1 bouyer #include "ioapic.h"
92 1.1 bouyer #include "eisa.h"
93 1.14 jmcneill #include "acpica.h"
94 1.1 bouyer #include "opt_mpbios.h"
95 1.2 christos #include "opt_acpi.h"
96 1.1 bouyer
97 1.26 dyoung #include <machine/i82489reg.h>
98 1.26 dyoung
99 1.14 jmcneill #if NIOAPIC > 0 || NACPICA > 0
100 1.22 dyoung #include <machine/i82093reg.h>
101 1.1 bouyer #include <machine/i82093var.h>
102 1.2 christos #include <machine/mpconfig.h>
103 1.1 bouyer #include <machine/mpbiosvar.h>
104 1.1 bouyer #include <machine/pic.h>
105 1.31 knakahar #include <x86/pci/pci_msi_machdep.h>
106 1.1 bouyer #endif
107 1.1 bouyer
108 1.1 bouyer #ifdef MPBIOS
109 1.1 bouyer #include <machine/mpbiosvar.h>
110 1.1 bouyer #endif
111 1.1 bouyer
112 1.14 jmcneill #if NACPICA > 0
113 1.1 bouyer #include <machine/mpacpi.h>
114 1.1 bouyer #endif
115 1.1 bouyer
116 1.1 bouyer int
117 1.19 dyoung pci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
118 1.1 bouyer {
119 1.29 knakahar pci_intr_pin_t pin = pa->pa_intrpin;
120 1.29 knakahar pci_intr_line_t line = pa->pa_intrline;
121 1.23 dyoung pci_chipset_tag_t ipc, pc = pa->pa_pc;
122 1.14 jmcneill #if NIOAPIC > 0 || NACPICA > 0
123 1.29 knakahar pci_intr_pin_t rawpin = pa->pa_rawintrpin;
124 1.1 bouyer int bus, dev, func;
125 1.1 bouyer #endif
126 1.1 bouyer
127 1.23 dyoung for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) {
128 1.23 dyoung if ((ipc->pc_present & PCI_OVERRIDE_INTR_MAP) == 0)
129 1.23 dyoung continue;
130 1.23 dyoung return (*ipc->pc_ov->ov_intr_map)(ipc->pc_ctx, pa, ihp);
131 1.16 dyoung }
132 1.15 dyoung
133 1.1 bouyer if (pin == 0) {
134 1.1 bouyer /* No IRQ used. */
135 1.1 bouyer goto bad;
136 1.1 bouyer }
137 1.1 bouyer
138 1.2 christos *ihp = 0;
139 1.2 christos
140 1.1 bouyer if (pin > PCI_INTERRUPT_PIN_MAX) {
141 1.13 ad aprint_normal("pci_intr_map: bad interrupt pin %d\n", pin);
142 1.1 bouyer goto bad;
143 1.1 bouyer }
144 1.1 bouyer
145 1.14 jmcneill #if NIOAPIC > 0 || NACPICA > 0
146 1.24 yamt KASSERT(rawpin >= PCI_INTERRUPT_PIN_A);
147 1.24 yamt KASSERT(rawpin <= PCI_INTERRUPT_PIN_D);
148 1.1 bouyer pci_decompose_tag(pc, pa->pa_tag, &bus, &dev, &func);
149 1.1 bouyer if (mp_busses != NULL) {
150 1.25 yamt /*
151 1.25 yamt * Note: PCI_INTERRUPT_PIN_A == 1 where intr_find_mpmapping
152 1.25 yamt * wants pci bus_pin encoding which uses INT_A == 0.
153 1.25 yamt */
154 1.24 yamt if (intr_find_mpmapping(bus,
155 1.24 yamt (dev << 2) | (rawpin - PCI_INTERRUPT_PIN_A), ihp) == 0) {
156 1.24 yamt if (APIC_IRQ_LEGACY_IRQ(*ihp) == 0)
157 1.2 christos *ihp |= line;
158 1.1 bouyer return 0;
159 1.1 bouyer }
160 1.1 bouyer /*
161 1.1 bouyer * No explicit PCI mapping found. This is not fatal,
162 1.1 bouyer * we'll try the ISA (or possibly EISA) mappings next.
163 1.1 bouyer */
164 1.1 bouyer }
165 1.1 bouyer #endif
166 1.1 bouyer
167 1.1 bouyer /*
168 1.1 bouyer * Section 6.2.4, `Miscellaneous Functions', says that 255 means
169 1.1 bouyer * `unknown' or `no connection' on a PC. We assume that a device with
170 1.1 bouyer * `no connection' either doesn't have an interrupt (in which case the
171 1.1 bouyer * pin number should be 0, and would have been noticed above), or
172 1.1 bouyer * wasn't configured by the BIOS (in which case we punt, since there's
173 1.1 bouyer * no real way we can know how the interrupt lines are mapped in the
174 1.1 bouyer * hardware).
175 1.1 bouyer *
176 1.1 bouyer * XXX
177 1.1 bouyer * Since IRQ 0 is only used by the clock, and we can't actually be sure
178 1.1 bouyer * that the BIOS did its job, we also recognize that as meaning that
179 1.1 bouyer * the BIOS has not configured the device.
180 1.1 bouyer */
181 1.1 bouyer if (line == 0 || line == X86_PCI_INTERRUPT_LINE_NO_CONNECTION) {
182 1.13 ad aprint_normal("pci_intr_map: no mapping for pin %c (line=%02x)\n",
183 1.1 bouyer '@' + pin, line);
184 1.1 bouyer goto bad;
185 1.1 bouyer } else {
186 1.1 bouyer if (line >= NUM_LEGACY_IRQS) {
187 1.13 ad aprint_normal("pci_intr_map: bad interrupt line %d\n", line);
188 1.1 bouyer goto bad;
189 1.1 bouyer }
190 1.1 bouyer if (line == 2) {
191 1.13 ad aprint_normal("pci_intr_map: changed line 2 to line 9\n");
192 1.1 bouyer line = 9;
193 1.1 bouyer }
194 1.1 bouyer }
195 1.14 jmcneill #if NIOAPIC > 0 || NACPICA > 0
196 1.1 bouyer if (mp_busses != NULL) {
197 1.1 bouyer if (intr_find_mpmapping(mp_isa_bus, line, ihp) == 0) {
198 1.2 christos if ((*ihp & 0xff) == 0)
199 1.2 christos *ihp |= line;
200 1.1 bouyer return 0;
201 1.1 bouyer }
202 1.1 bouyer #if NEISA > 0
203 1.1 bouyer if (intr_find_mpmapping(mp_eisa_bus, line, ihp) == 0) {
204 1.2 christos if ((*ihp & 0xff) == 0)
205 1.2 christos *ihp |= line;
206 1.1 bouyer return 0;
207 1.1 bouyer }
208 1.1 bouyer #endif
209 1.13 ad aprint_normal("pci_intr_map: bus %d dev %d func %d pin %d; line %d\n",
210 1.1 bouyer bus, dev, func, pin, line);
211 1.13 ad aprint_normal("pci_intr_map: no MP mapping found\n");
212 1.1 bouyer }
213 1.1 bouyer #endif
214 1.1 bouyer
215 1.1 bouyer *ihp = line;
216 1.1 bouyer return 0;
217 1.1 bouyer
218 1.1 bouyer bad:
219 1.1 bouyer *ihp = -1;
220 1.1 bouyer return 1;
221 1.1 bouyer }
222 1.1 bouyer
223 1.1 bouyer const char *
224 1.27 christos pci_intr_string(pci_chipset_tag_t pc, pci_intr_handle_t ih, char *buf,
225 1.27 christos size_t len)
226 1.1 bouyer {
227 1.23 dyoung pci_chipset_tag_t ipc;
228 1.17 dyoung
229 1.23 dyoung for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) {
230 1.23 dyoung if ((ipc->pc_present & PCI_OVERRIDE_INTR_STRING) == 0)
231 1.23 dyoung continue;
232 1.27 christos return (*ipc->pc_ov->ov_intr_string)(ipc->pc_ctx, pc, ih,
233 1.27 christos buf, len);
234 1.16 dyoung }
235 1.15 dyoung
236 1.33 knakahar if (INT_VIA_MSI(ih))
237 1.33 knakahar return x86_pci_msi_string(pc, ih, buf, len);
238 1.33 knakahar
239 1.27 christos return intr_string(ih & ~MPSAFE_MASK, buf, len);
240 1.1 bouyer }
241 1.1 bouyer
242 1.1 bouyer
243 1.1 bouyer const struct evcnt *
244 1.6 christos pci_intr_evcnt(pci_chipset_tag_t pc, pci_intr_handle_t ih)
245 1.1 bouyer {
246 1.23 dyoung pci_chipset_tag_t ipc;
247 1.1 bouyer
248 1.23 dyoung for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) {
249 1.23 dyoung if ((ipc->pc_present & PCI_OVERRIDE_INTR_EVCNT) == 0)
250 1.23 dyoung continue;
251 1.23 dyoung return (*ipc->pc_ov->ov_intr_evcnt)(ipc->pc_ctx, pc, ih);
252 1.16 dyoung }
253 1.15 dyoung
254 1.1 bouyer /* XXX for now, no evcnt parent reported */
255 1.1 bouyer return NULL;
256 1.1 bouyer }
257 1.1 bouyer
258 1.11 ad int
259 1.11 ad pci_intr_setattr(pci_chipset_tag_t pc, pci_intr_handle_t *ih,
260 1.11 ad int attr, uint64_t data)
261 1.11 ad {
262 1.11 ad
263 1.11 ad switch (attr) {
264 1.11 ad case PCI_INTR_MPSAFE:
265 1.11 ad if (data) {
266 1.11 ad *ih |= MPSAFE_MASK;
267 1.11 ad } else {
268 1.11 ad *ih &= ~MPSAFE_MASK;
269 1.11 ad }
270 1.11 ad /* XXX Set live if already mapped. */
271 1.11 ad return 0;
272 1.11 ad default:
273 1.11 ad return ENODEV;
274 1.11 ad }
275 1.11 ad }
276 1.11 ad
277 1.1 bouyer void *
278 1.37 knakahar pci_intr_establish_xname(pci_chipset_tag_t pc, pci_intr_handle_t ih,
279 1.37 knakahar int level, int (*func)(void *), void *arg, const char *xname)
280 1.1 bouyer {
281 1.1 bouyer int pin, irq;
282 1.1 bouyer struct pic *pic;
283 1.12 drochner #if NIOAPIC > 0
284 1.12 drochner struct ioapic_softc *ioapic;
285 1.12 drochner #endif
286 1.11 ad bool mpsafe;
287 1.23 dyoung pci_chipset_tag_t ipc;
288 1.1 bouyer
289 1.23 dyoung for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) {
290 1.23 dyoung if ((ipc->pc_present & PCI_OVERRIDE_INTR_ESTABLISH) == 0)
291 1.23 dyoung continue;
292 1.23 dyoung return (*ipc->pc_ov->ov_intr_establish)(ipc->pc_ctx,
293 1.23 dyoung pc, ih, level, func, arg);
294 1.16 dyoung }
295 1.15 dyoung
296 1.31 knakahar if (INT_VIA_MSI(ih)) {
297 1.31 knakahar if (MSI_INT_IS_MSIX(ih))
298 1.37 knakahar return x86_pci_msix_establish(pc, ih, level, func, arg,
299 1.37 knakahar xname);
300 1.31 knakahar else
301 1.37 knakahar return x86_pci_msi_establish(pc, ih, level, func, arg,
302 1.37 knakahar xname);
303 1.31 knakahar }
304 1.31 knakahar
305 1.1 bouyer pic = &i8259_pic;
306 1.29 knakahar pin = irq = APIC_IRQ_LEGACY_IRQ(ih);
307 1.11 ad mpsafe = ((ih & MPSAFE_MASK) != 0);
308 1.1 bouyer
309 1.1 bouyer #if NIOAPIC > 0
310 1.1 bouyer if (ih & APIC_INT_VIA_APIC) {
311 1.12 drochner ioapic = ioapic_find(APIC_IRQ_APIC(ih));
312 1.12 drochner if (ioapic == NULL) {
313 1.13 ad aprint_normal("pci_intr_establish: bad ioapic %d\n",
314 1.1 bouyer APIC_IRQ_APIC(ih));
315 1.1 bouyer return NULL;
316 1.1 bouyer }
317 1.12 drochner pic = &ioapic->sc_pic;
318 1.1 bouyer pin = APIC_IRQ_PIN(ih);
319 1.1 bouyer irq = APIC_IRQ_LEGACY_IRQ(ih);
320 1.1 bouyer if (irq < 0 || irq >= NUM_LEGACY_IRQS)
321 1.1 bouyer irq = -1;
322 1.1 bouyer }
323 1.1 bouyer #endif
324 1.1 bouyer
325 1.37 knakahar return intr_establish_xname(irq, pic, pin, IST_LEVEL, level, func, arg,
326 1.37 knakahar mpsafe, xname);
327 1.37 knakahar }
328 1.37 knakahar
329 1.37 knakahar void *
330 1.37 knakahar pci_intr_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih,
331 1.37 knakahar int level, int (*func)(void *), void *arg)
332 1.37 knakahar {
333 1.37 knakahar
334 1.37 knakahar return pci_intr_establish_xname(pc, ih, level, func, arg, "unknown");
335 1.1 bouyer }
336 1.1 bouyer
337 1.1 bouyer void
338 1.6 christos pci_intr_disestablish(pci_chipset_tag_t pc, void *cookie)
339 1.1 bouyer {
340 1.23 dyoung pci_chipset_tag_t ipc;
341 1.1 bouyer
342 1.23 dyoung for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) {
343 1.23 dyoung if ((ipc->pc_present & PCI_OVERRIDE_INTR_DISESTABLISH) == 0)
344 1.23 dyoung continue;
345 1.23 dyoung (*ipc->pc_ov->ov_intr_disestablish)(ipc->pc_ctx, pc, cookie);
346 1.23 dyoung return;
347 1.15 dyoung }
348 1.15 dyoung
349 1.31 knakahar /* MSI/MSI-X processing is switched in intr_disestablish(). */
350 1.1 bouyer intr_disestablish(cookie);
351 1.1 bouyer }
352 1.20 drochner
353 1.20 drochner #if NIOAPIC > 0
354 1.34 knakahar pci_intr_type_t
355 1.34 knakahar pci_intr_type(pci_intr_handle_t ih)
356 1.34 knakahar {
357 1.34 knakahar
358 1.34 knakahar if (INT_VIA_MSI(ih)) {
359 1.34 knakahar if (MSI_INT_IS_MSIX(ih))
360 1.34 knakahar return PCI_INTR_TYPE_MSIX;
361 1.34 knakahar else
362 1.34 knakahar return PCI_INTR_TYPE_MSI;
363 1.34 knakahar } else {
364 1.34 knakahar return PCI_INTR_TYPE_INTX;
365 1.34 knakahar }
366 1.34 knakahar }
367 1.34 knakahar
368 1.32 knakahar static void
369 1.32 knakahar x86_pci_intx_release(pci_chipset_tag_t pc, pci_intr_handle_t *pih)
370 1.32 knakahar {
371 1.32 knakahar char intrstr_buf[INTRIDBUF];
372 1.32 knakahar const char *intrstr;
373 1.32 knakahar
374 1.32 knakahar intrstr = pci_intr_string(NULL, *pih, intrstr_buf, sizeof(intrstr_buf));
375 1.32 knakahar mutex_enter(&cpu_lock);
376 1.32 knakahar intr_free_io_intrsource(intrstr);
377 1.32 knakahar mutex_exit(&cpu_lock);
378 1.32 knakahar
379 1.32 knakahar kmem_free(pih, sizeof(*pih));
380 1.32 knakahar }
381 1.32 knakahar
382 1.30 knakahar int
383 1.30 knakahar pci_intx_alloc(const struct pci_attach_args *pa, pci_intr_handle_t **pih)
384 1.20 drochner {
385 1.30 knakahar struct intrsource *isp;
386 1.30 knakahar pci_intr_handle_t *handle;
387 1.30 knakahar int error;
388 1.30 knakahar char intrstr_buf[INTRIDBUF];
389 1.30 knakahar const char *intrstr;
390 1.30 knakahar
391 1.30 knakahar handle = kmem_zalloc(sizeof(*handle), KM_SLEEP);
392 1.30 knakahar if (handle == NULL) {
393 1.30 knakahar aprint_normal("cannot allocate pci_intr_handle_t\n");
394 1.30 knakahar return ENOMEM;
395 1.30 knakahar }
396 1.30 knakahar
397 1.30 knakahar if (pci_intr_map(pa, handle) != 0) {
398 1.30 knakahar aprint_normal("cannot set up pci_intr_handle_t\n");
399 1.30 knakahar error = EINVAL;
400 1.30 knakahar goto error;
401 1.30 knakahar }
402 1.30 knakahar
403 1.30 knakahar intrstr = pci_intr_string(pa->pa_pc, *handle,
404 1.30 knakahar intrstr_buf, sizeof(intrstr_buf));
405 1.30 knakahar mutex_enter(&cpu_lock);
406 1.30 knakahar isp = intr_allocate_io_intrsource(intrstr);
407 1.30 knakahar mutex_exit(&cpu_lock);
408 1.30 knakahar if (isp == NULL) {
409 1.30 knakahar aprint_normal("can't allocate io_intersource\n");
410 1.30 knakahar error = ENOMEM;
411 1.30 knakahar goto error;
412 1.30 knakahar }
413 1.20 drochner
414 1.30 knakahar *pih = handle;
415 1.30 knakahar return 0;
416 1.20 drochner
417 1.30 knakahar error:
418 1.30 knakahar kmem_free(handle, sizeof(*handle));
419 1.30 knakahar return error;
420 1.20 drochner }
421 1.20 drochner
422 1.34 knakahar /*
423 1.34 knakahar * Interrupt handler allocation utility. This function calls each allocation
424 1.34 knakahar * function as specified by arguments.
425 1.34 knakahar * Currently callee functions are pci_intx_alloc(), pci_msi_alloc_exact(),
426 1.34 knakahar * and pci_msix_alloc_exact().
427 1.34 knakahar * pa : pci_attach_args
428 1.34 knakahar * ihps : interrupt handlers
429 1.34 knakahar * counts : The array of number of required interrupt handlers.
430 1.34 knakahar * It is overwritten by allocated the number of handlers.
431 1.34 knakahar * CAUTION: The size of counts[] must be PCI_INTR_TYPE_SIZE.
432 1.34 knakahar * max_type : "max" type of using interrupts. See below.
433 1.34 knakahar * e.g.
434 1.34 knakahar * If you want to use 5 MSI-X, 1 MSI, or INTx, you use "counts" as
435 1.34 knakahar * int counts[PCI_INTR_TYPE_SIZE];
436 1.34 knakahar * counts[PCI_INTR_TYPE_MSIX] = 5;
437 1.34 knakahar * counts[PCI_INTR_TYPE_MSI] = 1;
438 1.34 knakahar * counts[PCI_INTR_TYPE_INTX] = 1;
439 1.34 knakahar * error = pci_intr_alloc(pa, ihps, counts, PCI_INTR_TYPE_MSIX);
440 1.34 knakahar *
441 1.34 knakahar * If you want to use hardware max number MSI-X or 1 MSI,
442 1.34 knakahar * and not to use INTx, you use "counts" as
443 1.34 knakahar * int counts[PCI_INTR_TYPE_SIZE];
444 1.34 knakahar * counts[PCI_INTR_TYPE_MSIX] = -1;
445 1.34 knakahar * counts[PCI_INTR_TYPE_MSI] = 1;
446 1.34 knakahar * counts[PCI_INTR_TYPE_INTX] = 0;
447 1.34 knakahar * error = pci_intr_alloc(pa, ihps, counts, PCI_INTR_TYPE_MSIX);
448 1.34 knakahar *
449 1.34 knakahar * If you want to use 3 MSI or INTx, you can use "counts" as
450 1.34 knakahar * int counts[PCI_INTR_TYPE_SIZE];
451 1.34 knakahar * counts[PCI_INTR_TYPE_MSI] = 3;
452 1.34 knakahar * counts[PCI_INTR_TYPE_INTX] = 1;
453 1.34 knakahar * error = pci_intr_alloc(pa, ihps, counts, PCI_INTR_TYPE_MSI);
454 1.34 knakahar *
455 1.34 knakahar * If you want to use 1 MSI or INTx (probably most general usage),
456 1.34 knakahar * you can simply use this API like
457 1.34 knakahar * below
458 1.34 knakahar * error = pci_intr_alloc(pa, ihps, NULL, 0);
459 1.34 knakahar * ^ ignored
460 1.34 knakahar */
461 1.34 knakahar int
462 1.34 knakahar pci_intr_alloc(const struct pci_attach_args *pa, pci_intr_handle_t **ihps,
463 1.34 knakahar int *counts, pci_intr_type_t max_type)
464 1.34 knakahar {
465 1.34 knakahar int error;
466 1.34 knakahar int intx_count, msi_count, msix_count;
467 1.34 knakahar
468 1.34 knakahar intx_count = msi_count = msix_count = 0;
469 1.34 knakahar if (counts == NULL) { /* simple pattern */
470 1.34 knakahar msi_count = 1;
471 1.34 knakahar intx_count = 1;
472 1.34 knakahar } else {
473 1.34 knakahar switch(max_type) {
474 1.34 knakahar case PCI_INTR_TYPE_MSIX:
475 1.34 knakahar msix_count = counts[PCI_INTR_TYPE_MSIX];
476 1.34 knakahar /* FALLTHROUGH */
477 1.34 knakahar case PCI_INTR_TYPE_MSI:
478 1.34 knakahar msi_count = counts[PCI_INTR_TYPE_MSI];
479 1.34 knakahar /* FALLTHROUGH */
480 1.34 knakahar case PCI_INTR_TYPE_INTX:
481 1.34 knakahar intx_count = counts[PCI_INTR_TYPE_INTX];
482 1.34 knakahar break;
483 1.34 knakahar default:
484 1.34 knakahar return EINVAL;
485 1.34 knakahar }
486 1.34 knakahar }
487 1.34 knakahar
488 1.35 knakahar if (counts != NULL)
489 1.35 knakahar memset(counts, 0, sizeof(counts[0]) * PCI_INTR_TYPE_SIZE);
490 1.34 knakahar error = EINVAL;
491 1.34 knakahar
492 1.34 knakahar /* try MSI-X */
493 1.34 knakahar if (msix_count == -1) /* use hardware max */
494 1.36 msaitoh msix_count = pci_msix_count(pa->pa_pc, pa->pa_tag);
495 1.34 knakahar if (msix_count > 0) {
496 1.34 knakahar error = pci_msix_alloc_exact(pa, ihps, msix_count);
497 1.34 knakahar if (error == 0) {
498 1.35 knakahar KASSERTMSG(counts != NULL,
499 1.35 knakahar "If MSI-X is used, counts must not be NULL.");
500 1.34 knakahar counts[PCI_INTR_TYPE_MSIX] = msix_count;
501 1.34 knakahar goto out;
502 1.34 knakahar }
503 1.34 knakahar }
504 1.34 knakahar
505 1.34 knakahar /* try MSI */
506 1.34 knakahar if (msi_count == -1) /* use hardware max */
507 1.36 msaitoh msi_count = pci_msi_count(pa->pa_pc, pa->pa_tag);
508 1.34 knakahar if (msi_count > 0) {
509 1.34 knakahar error = pci_msi_alloc_exact(pa, ihps, msi_count);
510 1.34 knakahar if (error == 0) {
511 1.35 knakahar if (counts != NULL)
512 1.34 knakahar counts[PCI_INTR_TYPE_MSI] = msi_count;
513 1.35 knakahar goto out;
514 1.34 knakahar }
515 1.34 knakahar }
516 1.34 knakahar
517 1.34 knakahar /* try INTx */
518 1.34 knakahar if (intx_count != 0) { /* The number of INTx is always 1. */
519 1.34 knakahar error = pci_intx_alloc(pa, ihps);
520 1.34 knakahar if (error == 0) {
521 1.34 knakahar if (counts != NULL)
522 1.34 knakahar counts[PCI_INTR_TYPE_INTX] = 1;
523 1.34 knakahar }
524 1.34 knakahar }
525 1.34 knakahar
526 1.34 knakahar out:
527 1.34 knakahar return error;
528 1.34 knakahar }
529 1.34 knakahar
530 1.31 knakahar void
531 1.31 knakahar pci_intr_release(pci_chipset_tag_t pc, pci_intr_handle_t *pih, int count)
532 1.31 knakahar {
533 1.31 knakahar if (pih == NULL)
534 1.31 knakahar return;
535 1.31 knakahar
536 1.31 knakahar if (INT_VIA_MSI(*pih)) {
537 1.31 knakahar if (MSI_INT_IS_MSIX(*pih))
538 1.31 knakahar return x86_pci_msix_release(pc, pih, count);
539 1.31 knakahar else
540 1.31 knakahar return x86_pci_msi_release(pc, pih, count);
541 1.31 knakahar } else {
542 1.31 knakahar KASSERT(count == 1);
543 1.31 knakahar return x86_pci_intx_release(pc, pih);
544 1.31 knakahar }
545 1.31 knakahar
546 1.31 knakahar }
547 1.20 drochner #endif
548