Fri Jul 24 06:49:58 2015 UTC ()
fix pci_intr_alloc(..., NULL, 0). reported nonaka@n.o


(knakahara)
diff -r1.34 -r1.35 src/sys/arch/x86/pci/pci_intr_machdep.c

cvs diff -r1.34 -r1.35 src/sys/arch/x86/pci/pci_intr_machdep.c (switch to unified diff)

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