pci_intr_fixup.c revision 1.5.6.4 1 1.5.6.4 he /* $NetBSD: pci_intr_fixup.c,v 1.5.6.4 2001/10/27 20:29:30 he Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.1 thorpej * NASA Ames Research Center.
10 1.1 thorpej *
11 1.1 thorpej * Redistribution and use in source and binary forms, with or without
12 1.1 thorpej * modification, are permitted provided that the following conditions
13 1.1 thorpej * are met:
14 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.1 thorpej * notice, this list of conditions and the following disclaimer.
16 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.1 thorpej * documentation and/or other materials provided with the distribution.
19 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
20 1.1 thorpej * must display the following acknowledgement:
21 1.1 thorpej * This product includes software developed by the NetBSD
22 1.1 thorpej * Foundation, Inc. and its contributors.
23 1.1 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.1 thorpej * contributors may be used to endorse or promote products derived
25 1.1 thorpej * from this software without specific prior written permission.
26 1.1 thorpej *
27 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
38 1.1 thorpej */
39 1.1 thorpej
40 1.1 thorpej /*
41 1.1 thorpej * Copyright (c) 1999, by UCHIYAMA Yasushi
42 1.1 thorpej * All rights reserved.
43 1.1 thorpej *
44 1.1 thorpej * Redistribution and use in source and binary forms, with or without
45 1.1 thorpej * modification, are permitted provided that the following conditions
46 1.1 thorpej * are met:
47 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
48 1.1 thorpej * notice, this list of conditions and the following disclaimer.
49 1.1 thorpej * 2. The name of the developer may NOT be used to endorse or promote products
50 1.1 thorpej * derived from this software without specific prior written permission.
51 1.1 thorpej *
52 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
53 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
56 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 1.1 thorpej * SUCH DAMAGE.
63 1.1 thorpej */
64 1.1 thorpej
65 1.1 thorpej /*
66 1.1 thorpej * PCI Interrupt Router support.
67 1.1 thorpej */
68 1.1 thorpej
69 1.1 thorpej #include "opt_pcibios.h"
70 1.1 thorpej
71 1.1 thorpej #include <sys/param.h>
72 1.1 thorpej #include <sys/systm.h>
73 1.1 thorpej #include <sys/kernel.h>
74 1.1 thorpej #include <sys/malloc.h>
75 1.1 thorpej #include <sys/queue.h>
76 1.1 thorpej #include <sys/device.h>
77 1.1 thorpej
78 1.1 thorpej #include <machine/bus.h>
79 1.1 thorpej #include <machine/intr.h>
80 1.1 thorpej
81 1.1 thorpej #include <dev/pci/pcireg.h>
82 1.1 thorpej #include <dev/pci/pcivar.h>
83 1.1 thorpej #include <dev/pci/pcidevs.h>
84 1.1 thorpej
85 1.1 thorpej #include <i386/isa/icu.h>
86 1.1 thorpej #include <i386/pci/pci_intr_fixup.h>
87 1.1 thorpej #include <i386/pci/pcibios.h>
88 1.1 thorpej
89 1.1 thorpej struct pciintr_link_map {
90 1.1 thorpej int link;
91 1.1 thorpej int clink;
92 1.1 thorpej int irq;
93 1.1 thorpej u_int16_t bitmap;
94 1.1 thorpej int fixup_stage;
95 1.1 thorpej SIMPLEQ_ENTRY(pciintr_link_map) list;
96 1.1 thorpej };
97 1.1 thorpej
98 1.5.6.2 soda pciintr_icu_tag_t pciintr_icu_tag = NULL;
99 1.1 thorpej pciintr_icu_handle_t pciintr_icu_handle;
100 1.1 thorpej
101 1.5.6.2 soda #ifdef PCIBIOS_IRQS_HINT
102 1.5.6.2 soda int pcibios_irqs_hint = PCIBIOS_IRQS_HINT;
103 1.5.6.2 soda #endif
104 1.5.6.2 soda
105 1.5.6.2 soda struct pciintr_link_map *pciintr_link_lookup __P((int));
106 1.1 thorpej struct pciintr_link_map *pciintr_link_alloc __P((struct pcibios_intr_routing *,
107 1.1 thorpej int));
108 1.1 thorpej struct pcibios_intr_routing *pciintr_pir_lookup __P((int, int));
109 1.5.6.2 soda static int pciintr_bitmap_count_irq __P((int, int *));
110 1.5.6.2 soda static int pciintr_bitmap_find_lowest_irq __P((int, int *));
111 1.1 thorpej int pciintr_link_init __P((void));
112 1.5.6.2 soda #ifdef PCIBIOS_INTR_GUESS
113 1.5.6.2 soda int pciintr_guess_irq __P((void));
114 1.5.6.2 soda #endif
115 1.1 thorpej int pciintr_link_fixup __P((void));
116 1.1 thorpej int pciintr_link_route __P((u_int16_t *));
117 1.1 thorpej int pciintr_irq_release __P((u_int16_t *));
118 1.1 thorpej int pciintr_header_fixup __P((pci_chipset_tag_t));
119 1.5 uch void pciintr_do_header_fixup __P((pci_chipset_tag_t, pcitag_t));
120 1.1 thorpej
121 1.1 thorpej SIMPLEQ_HEAD(, pciintr_link_map) pciintr_link_map_list;
122 1.1 thorpej
123 1.1 thorpej const struct pciintr_icu_table {
124 1.1 thorpej pci_vendor_id_t piit_vendor;
125 1.1 thorpej pci_product_id_t piit_product;
126 1.1 thorpej int (*piit_init) __P((pci_chipset_tag_t,
127 1.1 thorpej bus_space_tag_t, pcitag_t, pciintr_icu_tag_t *,
128 1.1 thorpej pciintr_icu_handle_t *));
129 1.1 thorpej } pciintr_icu_table[] = {
130 1.1 thorpej { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82371MX,
131 1.1 thorpej piix_init },
132 1.1 thorpej { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82371AB_ISA,
133 1.1 thorpej piix_init },
134 1.1 thorpej { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82371FB_ISA,
135 1.1 thorpej piix_init },
136 1.1 thorpej { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82371SB_ISA,
137 1.1 thorpej piix_init },
138 1.1 thorpej
139 1.1 thorpej { PCI_VENDOR_OPTI, PCI_PRODUCT_OPTI_82C558,
140 1.1 thorpej opti82c558_init },
141 1.1 thorpej { PCI_VENDOR_OPTI, PCI_PRODUCT_OPTI_82C700,
142 1.1 thorpej opti82c700_init },
143 1.1 thorpej
144 1.1 thorpej { PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT82C586_ISA,
145 1.5.6.3 he via82c586_init },
146 1.5.6.3 he { PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT82C686A_ISA,
147 1.5.6.3 he via82c586_init },
148 1.1 thorpej
149 1.1 thorpej { PCI_VENDOR_SIS, PCI_PRODUCT_SIS_85C503,
150 1.1 thorpej sis85c503_init },
151 1.5.6.4 he
152 1.5.6.4 he { PCI_VENDOR_ALI, PCI_PRODUCT_ALI_M1543,
153 1.5.6.4 he ali1543_init },
154 1.1 thorpej
155 1.1 thorpej { 0, 0,
156 1.1 thorpej NULL },
157 1.1 thorpej };
158 1.1 thorpej
159 1.1 thorpej const struct pciintr_icu_table *pciintr_icu_lookup __P((pcireg_t));
160 1.1 thorpej
161 1.1 thorpej const struct pciintr_icu_table *
162 1.1 thorpej pciintr_icu_lookup(id)
163 1.1 thorpej pcireg_t id;
164 1.1 thorpej {
165 1.1 thorpej const struct pciintr_icu_table *piit;
166 1.1 thorpej
167 1.1 thorpej for (piit = pciintr_icu_table;
168 1.1 thorpej piit->piit_init != NULL;
169 1.1 thorpej piit++) {
170 1.1 thorpej if (PCI_VENDOR(id) == piit->piit_vendor &&
171 1.1 thorpej PCI_PRODUCT(id) == piit->piit_product)
172 1.1 thorpej return (piit);
173 1.1 thorpej }
174 1.1 thorpej
175 1.1 thorpej return (NULL);
176 1.1 thorpej }
177 1.1 thorpej
178 1.1 thorpej struct pciintr_link_map *
179 1.5.6.2 soda pciintr_link_lookup(link)
180 1.1 thorpej int link;
181 1.1 thorpej {
182 1.1 thorpej struct pciintr_link_map *l;
183 1.1 thorpej
184 1.1 thorpej for (l = SIMPLEQ_FIRST(&pciintr_link_map_list); l != NULL;
185 1.1 thorpej l = SIMPLEQ_NEXT(l, list)) {
186 1.1 thorpej if (l->link == link)
187 1.1 thorpej return (l);
188 1.1 thorpej }
189 1.1 thorpej
190 1.1 thorpej return (NULL);
191 1.1 thorpej }
192 1.1 thorpej
193 1.1 thorpej struct pciintr_link_map *
194 1.1 thorpej pciintr_link_alloc(pir, pin)
195 1.1 thorpej struct pcibios_intr_routing *pir;
196 1.1 thorpej int pin;
197 1.1 thorpej {
198 1.5.6.2 soda int link = pir->linkmap[pin].link, clink, irq;
199 1.1 thorpej struct pciintr_link_map *l, *lstart;
200 1.1 thorpej
201 1.5.6.2 soda if (pciintr_icu_tag != NULL) { /* compatible PCI ICU found */
202 1.5.6.2 soda /*
203 1.5.6.2 soda * Get the canonical link value for this entry.
204 1.5.6.2 soda */
205 1.5.6.2 soda if (pciintr_icu_getclink(pciintr_icu_tag, pciintr_icu_handle,
206 1.5.6.2 soda link, &clink) != 0) {
207 1.5.6.2 soda /*
208 1.5.6.2 soda * ICU doesn't understand the link value.
209 1.5.6.2 soda * Just ignore this PIR entry.
210 1.5.6.2 soda */
211 1.5.6.2 soda #ifdef DIAGNOSTIC
212 1.5.6.2 soda printf("pciintr_link_alloc: bus %d device %d: "
213 1.5.6.2 soda "link 0x%02x invalid\n",
214 1.5.6.2 soda pir->bus, PIR_DEVFUNC_DEVICE(pir->device), link);
215 1.5.6.2 soda #endif
216 1.5.6.2 soda return (NULL);
217 1.5.6.2 soda }
218 1.5.6.2 soda
219 1.5.6.2 soda /*
220 1.5.6.2 soda * Check the link value by asking the ICU for the
221 1.5.6.2 soda * canonical link value.
222 1.5.6.2 soda * Also, determine if this PIRQ is mapped to an IRQ.
223 1.5.6.2 soda */
224 1.5.6.2 soda if (pciintr_icu_get_intr(pciintr_icu_tag, pciintr_icu_handle,
225 1.5.6.2 soda clink, &irq) != 0) {
226 1.5.6.2 soda /*
227 1.5.6.2 soda * ICU doesn't understand the canonical link value.
228 1.5.6.2 soda * Just ignore this PIR entry.
229 1.5.6.2 soda */
230 1.5.6.2 soda #ifdef DIAGNOSTIC
231 1.5.6.2 soda printf("pciintr_link_alloc: "
232 1.5.6.2 soda "bus %d device %d link 0x%02x: "
233 1.5.6.2 soda "PIRQ 0x%02x invalid\n",
234 1.5.6.2 soda pir->bus, PIR_DEVFUNC_DEVICE(pir->device), link,
235 1.5.6.2 soda clink);
236 1.5.6.2 soda #endif
237 1.5.6.2 soda return (NULL);
238 1.5.6.2 soda }
239 1.5.6.2 soda }
240 1.5.6.2 soda
241 1.1 thorpej l = malloc(sizeof(*l), M_DEVBUF, M_NOWAIT);
242 1.1 thorpej if (l == NULL)
243 1.1 thorpej panic("pciintr_link_alloc");
244 1.1 thorpej
245 1.1 thorpej memset(l, 0, sizeof(*l));
246 1.1 thorpej
247 1.5.6.2 soda l->link = link;
248 1.1 thorpej l->bitmap = pir->linkmap[pin].bitmap;
249 1.5.6.2 soda if (pciintr_icu_tag != NULL) { /* compatible PCI ICU found */
250 1.5.6.2 soda l->clink = clink;
251 1.5.6.2 soda l->irq = irq; /* maybe I386_PCI_INTERRUPT_LINE_NO_CONNECTION */
252 1.5.6.2 soda } else {
253 1.5.6.2 soda l->clink = link; /* only for PCIBIOSVERBOSE diagnostic */
254 1.5.6.2 soda l->irq = I386_PCI_INTERRUPT_LINE_NO_CONNECTION;
255 1.5.6.2 soda }
256 1.1 thorpej
257 1.1 thorpej lstart = SIMPLEQ_FIRST(&pciintr_link_map_list);
258 1.1 thorpej if (lstart == NULL || lstart->link < l->link)
259 1.1 thorpej SIMPLEQ_INSERT_TAIL(&pciintr_link_map_list, l, list);
260 1.1 thorpej else
261 1.1 thorpej SIMPLEQ_INSERT_HEAD(&pciintr_link_map_list, l, list);
262 1.1 thorpej
263 1.1 thorpej return (l);
264 1.1 thorpej }
265 1.1 thorpej
266 1.1 thorpej struct pcibios_intr_routing *
267 1.1 thorpej pciintr_pir_lookup(bus, device)
268 1.1 thorpej int bus, device;
269 1.1 thorpej {
270 1.1 thorpej struct pcibios_intr_routing *pir;
271 1.1 thorpej int entry;
272 1.1 thorpej
273 1.1 thorpej if (pcibios_pir_table == NULL)
274 1.1 thorpej return (NULL);
275 1.1 thorpej
276 1.1 thorpej for (entry = 0; entry < pcibios_pir_table_nentries; entry++) {
277 1.1 thorpej pir = &pcibios_pir_table[entry];
278 1.5.6.2 soda if (pir->bus == bus &&
279 1.5.6.2 soda PIR_DEVFUNC_DEVICE(pir->device) == device)
280 1.1 thorpej return (pir);
281 1.1 thorpej }
282 1.1 thorpej
283 1.1 thorpej return (NULL);
284 1.1 thorpej }
285 1.1 thorpej
286 1.5.6.2 soda static int
287 1.5.6.2 soda pciintr_bitmap_count_irq(irq_bitmap, irqp)
288 1.5.6.2 soda int irq_bitmap, *irqp;
289 1.5.6.2 soda {
290 1.5.6.2 soda int i, bit, count = 0, irq = I386_PCI_INTERRUPT_LINE_NO_CONNECTION;
291 1.5.6.2 soda
292 1.5.6.2 soda if (irq_bitmap != 0) {
293 1.5.6.2 soda for (i = 0, bit = 1; i < 16; i++, bit <<= 1) {
294 1.5.6.2 soda if (irq_bitmap & bit) {
295 1.5.6.2 soda irq = i;
296 1.5.6.2 soda count++;
297 1.5.6.2 soda }
298 1.5.6.2 soda }
299 1.5.6.2 soda }
300 1.5.6.2 soda *irqp = irq;
301 1.5.6.2 soda return (count);
302 1.5.6.2 soda }
303 1.5.6.2 soda
304 1.5.6.2 soda static int
305 1.5.6.2 soda pciintr_bitmap_find_lowest_irq(irq_bitmap, irqp)
306 1.5.6.2 soda int irq_bitmap, *irqp;
307 1.5.6.2 soda {
308 1.5.6.2 soda int i, bit;
309 1.5.6.2 soda
310 1.5.6.2 soda if (irq_bitmap != 0) {
311 1.5.6.2 soda for (i = 0, bit = 1; i < 16; i++, bit <<= 1) {
312 1.5.6.2 soda if (irq_bitmap & bit) {
313 1.5.6.2 soda *irqp = i;
314 1.5.6.2 soda return (1); /* found */
315 1.5.6.2 soda }
316 1.5.6.2 soda }
317 1.5.6.2 soda }
318 1.5.6.2 soda return (0); /* not found */
319 1.5.6.2 soda }
320 1.5.6.2 soda
321 1.1 thorpej int
322 1.1 thorpej pciintr_link_init()
323 1.1 thorpej {
324 1.5.6.2 soda int entry, pin, link;
325 1.1 thorpej struct pcibios_intr_routing *pir;
326 1.1 thorpej struct pciintr_link_map *l;
327 1.1 thorpej
328 1.1 thorpej if (pcibios_pir_table == NULL) {
329 1.1 thorpej /* No PIR table; can't do anything. */
330 1.1 thorpej printf("pciintr_link_init: no PIR table\n");
331 1.1 thorpej return (1);
332 1.1 thorpej }
333 1.1 thorpej
334 1.1 thorpej SIMPLEQ_INIT(&pciintr_link_map_list);
335 1.1 thorpej
336 1.1 thorpej for (entry = 0; entry < pcibios_pir_table_nentries; entry++) {
337 1.1 thorpej pir = &pcibios_pir_table[entry];
338 1.5.6.2 soda for (pin = 0; pin < PCI_INTERRUPT_PIN_MAX; pin++) {
339 1.1 thorpej link = pir->linkmap[pin].link;
340 1.1 thorpej if (link == 0) {
341 1.1 thorpej /* No connection for this pin. */
342 1.1 thorpej continue;
343 1.1 thorpej }
344 1.1 thorpej /*
345 1.1 thorpej * Multiple devices may be wired to the same
346 1.1 thorpej * interrupt; check to see if we've seen this
347 1.1 thorpej * one already. If not, allocate a new link
348 1.1 thorpej * map entry and stuff it in the map.
349 1.1 thorpej */
350 1.5.6.2 soda l = pciintr_link_lookup(link);
351 1.5.6.2 soda if (l == NULL) {
352 1.1 thorpej (void) pciintr_link_alloc(pir, pin);
353 1.5.6.2 soda } else if (pir->linkmap[pin].bitmap != l->bitmap) {
354 1.5.6.2 soda /*
355 1.5.6.2 soda * violates PCI IRQ Routing Table Specification
356 1.5.6.2 soda */
357 1.5.6.2 soda #ifdef DIAGNOSTIC
358 1.5.6.2 soda printf("pciintr_link_init: "
359 1.5.6.2 soda "bus %d device %d link 0x%02x: "
360 1.5.6.2 soda "bad irq bitmap 0x%04x, "
361 1.5.6.2 soda "should be 0x%04x\n",
362 1.5.6.2 soda pir->bus, PIR_DEVFUNC_DEVICE(pir->device),
363 1.5.6.2 soda link, pir->linkmap[pin].bitmap, l->bitmap);
364 1.5.6.2 soda #endif
365 1.5.6.2 soda /* safer value. */
366 1.5.6.2 soda l->bitmap &= pir->linkmap[pin].bitmap;
367 1.5.6.2 soda /* XXX - or, should ignore this entry? */
368 1.5.6.2 soda }
369 1.5.6.2 soda }
370 1.5.6.2 soda }
371 1.5.6.2 soda
372 1.5.6.2 soda return (0);
373 1.5.6.2 soda }
374 1.5.6.2 soda
375 1.5.6.2 soda #ifdef PCIBIOS_INTR_GUESS
376 1.5.6.2 soda /*
377 1.5.6.2 soda * No compatible PCI ICU found.
378 1.5.6.2 soda * Hopes the BIOS already setup the ICU.
379 1.5.6.2 soda */
380 1.5.6.2 soda int
381 1.5.6.2 soda pciintr_guess_irq()
382 1.5.6.2 soda {
383 1.5.6.2 soda struct pciintr_link_map *l;
384 1.5.6.2 soda int irq, guessed = 0;
385 1.5.6.2 soda
386 1.5.6.2 soda /*
387 1.5.6.2 soda * Stage 1: If only one IRQ is available for the link, use it.
388 1.5.6.2 soda */
389 1.5.6.2 soda for (l = SIMPLEQ_FIRST(&pciintr_link_map_list); l != NULL;
390 1.5.6.2 soda l = SIMPLEQ_NEXT(l, list)) {
391 1.5.6.2 soda if (l->irq != I386_PCI_INTERRUPT_LINE_NO_CONNECTION)
392 1.5.6.2 soda continue;
393 1.5.6.2 soda if (pciintr_bitmap_count_irq(l->bitmap, &irq) == 1) {
394 1.5.6.2 soda l->irq = irq;
395 1.5.6.2 soda l->fixup_stage = 1;
396 1.5.6.2 soda #ifdef PCIINTR_DEBUG
397 1.5.6.2 soda printf("pciintr_guess_irq (stage 1): "
398 1.5.6.2 soda "guessing PIRQ 0x%02x to be IRQ %d\n",
399 1.5.6.2 soda l->clink, l->irq);
400 1.5.6.2 soda #endif
401 1.5.6.2 soda guessed = 1;
402 1.1 thorpej }
403 1.1 thorpej }
404 1.1 thorpej
405 1.5.6.2 soda return (guessed ? 0 : -1);
406 1.1 thorpej }
407 1.5.6.2 soda #endif /* PCIBIOS_INTR_GUESS */
408 1.1 thorpej
409 1.1 thorpej int
410 1.1 thorpej pciintr_link_fixup()
411 1.1 thorpej {
412 1.1 thorpej struct pciintr_link_map *l;
413 1.5.6.2 soda int irq;
414 1.5.6.2 soda u_int16_t pciirq = 0;
415 1.1 thorpej
416 1.1 thorpej /*
417 1.1 thorpej * First stage: Attempt to connect PIRQs which aren't
418 1.1 thorpej * yet connected.
419 1.1 thorpej */
420 1.1 thorpej for (l = SIMPLEQ_FIRST(&pciintr_link_map_list); l != NULL;
421 1.1 thorpej l = SIMPLEQ_NEXT(l, list)) {
422 1.5.6.2 soda if (l->irq != I386_PCI_INTERRUPT_LINE_NO_CONNECTION) {
423 1.1 thorpej /*
424 1.5.6.2 soda * Interrupt is already connected. Don't do
425 1.5.6.2 soda * anything to it.
426 1.5.6.2 soda * In this case, l->fixup_stage == 0.
427 1.1 thorpej */
428 1.5.6.2 soda pciirq |= 1 << l->irq;
429 1.1 thorpej #ifdef PCIINTR_DEBUG
430 1.5.6.2 soda printf("pciintr_link_fixup: PIRQ 0x%02x already "
431 1.5.6.2 soda "connected to IRQ %d\n", l->clink, l->irq);
432 1.1 thorpej #endif
433 1.1 thorpej continue;
434 1.1 thorpej }
435 1.1 thorpej /*
436 1.5.6.2 soda * Interrupt isn't connected. Attempt to assign it to an IRQ.
437 1.1 thorpej */
438 1.1 thorpej #ifdef PCIINTR_DEBUG
439 1.5.6.2 soda printf("pciintr_link_fixup: PIRQ 0x%02x not connected",
440 1.5.6.2 soda l->clink);
441 1.1 thorpej #endif
442 1.5.6.2 soda /*
443 1.5.6.2 soda * Just do the easy case now; we'll defer the harder ones
444 1.5.6.2 soda * to Stage 2.
445 1.5.6.2 soda */
446 1.5.6.2 soda if (pciintr_bitmap_count_irq(l->bitmap, &irq) == 1) {
447 1.1 thorpej l->irq = irq;
448 1.5.6.2 soda l->fixup_stage = 1;
449 1.1 thorpej pciirq |= 1 << irq;
450 1.1 thorpej #ifdef PCIINTR_DEBUG
451 1.5.6.2 soda printf(", assigning IRQ %d", l->irq);
452 1.1 thorpej #endif
453 1.1 thorpej }
454 1.5.6.2 soda #ifdef PCIINTR_DEBUG
455 1.5.6.2 soda printf("\n");
456 1.5.6.2 soda #endif
457 1.1 thorpej }
458 1.4 augustss
459 1.1 thorpej /*
460 1.1 thorpej * Stage 2: Attempt to connect PIRQs which we didn't
461 1.1 thorpej * connect in Stage 1.
462 1.1 thorpej */
463 1.1 thorpej for (l = SIMPLEQ_FIRST(&pciintr_link_map_list); l != NULL;
464 1.1 thorpej l = SIMPLEQ_NEXT(l, list)) {
465 1.5.6.2 soda if (l->irq != I386_PCI_INTERRUPT_LINE_NO_CONNECTION)
466 1.5 uch continue;
467 1.5.6.2 soda if (pciintr_bitmap_find_lowest_irq(l->bitmap & pciirq,
468 1.5.6.2 soda &l->irq)) {
469 1.5.6.2 soda /*
470 1.5.6.2 soda * This IRQ is a valid PCI IRQ already
471 1.5.6.2 soda * connected to another PIRQ, and also an
472 1.5.6.2 soda * IRQ our PIRQ can use; connect it up!
473 1.5.6.2 soda */
474 1.5.6.2 soda l->fixup_stage = 2;
475 1.1 thorpej #ifdef PCIINTR_DEBUG
476 1.5.6.2 soda printf("pciintr_link_fixup (stage 2): "
477 1.5.6.2 soda "assigning IRQ %d to PIRQ 0x%02x\n",
478 1.5.6.2 soda l->irq, l->clink);
479 1.1 thorpej #endif
480 1.1 thorpej }
481 1.1 thorpej }
482 1.1 thorpej
483 1.5 uch #ifdef PCIBIOS_IRQS_HINT
484 1.1 thorpej /*
485 1.5 uch * Stage 3: The worst case. I need configuration hint that
486 1.5 uch * user supplied a mask for the PCI irqs
487 1.1 thorpej */
488 1.5 uch for (l = SIMPLEQ_FIRST(&pciintr_link_map_list); l != NULL;
489 1.5 uch l = SIMPLEQ_NEXT(l, list)) {
490 1.5.6.2 soda if (l->irq != I386_PCI_INTERRUPT_LINE_NO_CONNECTION)
491 1.5 uch continue;
492 1.5.6.2 soda if (pciintr_bitmap_find_lowest_irq(
493 1.5.6.2 soda l->bitmap & pcibios_irqs_hint, &l->irq)) {
494 1.5.6.2 soda l->fixup_stage = 3;
495 1.5 uch #ifdef PCIINTR_DEBUG
496 1.5.6.2 soda printf("pciintr_link_fixup (stage 3): "
497 1.5.6.2 soda "assigning IRQ %d to PIRQ 0x%02x\n",
498 1.5.6.2 soda l->irq, l->clink);
499 1.5 uch #endif
500 1.5 uch }
501 1.5 uch }
502 1.5 uch #endif /* PCIBIOS_IRQS_HINT */
503 1.1 thorpej
504 1.1 thorpej return (0);
505 1.1 thorpej }
506 1.1 thorpej
507 1.1 thorpej int
508 1.1 thorpej pciintr_link_route(pciirq)
509 1.1 thorpej u_int16_t *pciirq;
510 1.1 thorpej {
511 1.1 thorpej struct pciintr_link_map *l;
512 1.1 thorpej int rv = 0;
513 1.1 thorpej
514 1.1 thorpej *pciirq = 0;
515 1.1 thorpej
516 1.1 thorpej for (l = SIMPLEQ_FIRST(&pciintr_link_map_list); l != NULL;
517 1.1 thorpej l = SIMPLEQ_NEXT(l, list)) {
518 1.5.6.2 soda if (l->fixup_stage == 0) {
519 1.5.6.2 soda if (l->irq == I386_PCI_INTERRUPT_LINE_NO_CONNECTION) {
520 1.5.6.2 soda /* Appropriate interrupt was not found. */
521 1.5.6.2 soda #ifdef DIAGNOSTIC
522 1.5.6.2 soda printf("pciintr_link_route: "
523 1.5.6.2 soda "PIRQ 0x%02x: no IRQ, try "
524 1.5.6.2 soda "\"options PCIBIOS_IRQS_HINT=0x%04x\"\n",
525 1.5.6.2 soda l->clink,
526 1.5.6.2 soda /* suggest irq 9/10/11, if possible */
527 1.5.6.2 soda (l->bitmap & 0x0e00) ? (l->bitmap & 0x0e00)
528 1.5.6.2 soda : l->bitmap);
529 1.5.6.2 soda #endif
530 1.5.6.2 soda } else {
531 1.5.6.2 soda /* BIOS setting has no problem */
532 1.5.6.2 soda #ifdef PCIINTR_DEBUG
533 1.5.6.2 soda printf("pciintr_link_route: "
534 1.5.6.2 soda "route of PIRQ 0x%02x -> "
535 1.5.6.2 soda "IRQ %d preserved BIOS setting\n",
536 1.5.6.2 soda l->clink, l->irq);
537 1.5.6.2 soda #endif
538 1.5.6.2 soda *pciirq |= (1 << l->irq);
539 1.5.6.2 soda }
540 1.5.6.2 soda continue; /* nothing to do. */
541 1.5.6.2 soda }
542 1.5.6.2 soda
543 1.1 thorpej if (pciintr_icu_set_intr(pciintr_icu_tag, pciintr_icu_handle,
544 1.1 thorpej l->clink, l->irq) != 0 ||
545 1.5.6.2 soda pciintr_icu_set_trigger(pciintr_icu_tag,
546 1.5.6.2 soda pciintr_icu_handle,
547 1.1 thorpej l->irq, IST_LEVEL) != 0) {
548 1.5.6.2 soda printf("pciintr_link_route: route of PIRQ 0x%02x -> "
549 1.5.6.2 soda "IRQ %d failed\n", l->clink, l->irq);
550 1.1 thorpej rv = 1;
551 1.1 thorpej } else {
552 1.1 thorpej /*
553 1.1 thorpej * Succssfully routed interrupt. Mark this as
554 1.1 thorpej * a PCI interrupt.
555 1.1 thorpej */
556 1.1 thorpej *pciirq |= (1 << l->irq);
557 1.1 thorpej }
558 1.1 thorpej }
559 1.1 thorpej
560 1.1 thorpej return (rv);
561 1.1 thorpej }
562 1.1 thorpej
563 1.1 thorpej int
564 1.1 thorpej pciintr_irq_release(pciirq)
565 1.1 thorpej u_int16_t *pciirq;
566 1.1 thorpej {
567 1.5.6.2 soda int i, bit;
568 1.1 thorpej
569 1.5.6.2 soda for (i = 0, bit = 1; i < 16; i++, bit <<= 1) {
570 1.5.6.2 soda if ((*pciirq & bit) == 0)
571 1.1 thorpej (void) pciintr_icu_set_trigger(pciintr_icu_tag,
572 1.1 thorpej pciintr_icu_handle, i, IST_EDGE);
573 1.1 thorpej }
574 1.1 thorpej
575 1.1 thorpej return (0);
576 1.1 thorpej }
577 1.1 thorpej
578 1.1 thorpej int
579 1.1 thorpej pciintr_header_fixup(pc)
580 1.1 thorpej pci_chipset_tag_t pc;
581 1.1 thorpej {
582 1.5.6.2 soda PCIBIOS_PRINTV(("------------------------------------------\n"));
583 1.5.6.2 soda PCIBIOS_PRINTV((" device vendor product pin PIRQ IRQ stage\n"));
584 1.5.6.2 soda PCIBIOS_PRINTV(("------------------------------------------\n"));
585 1.5.6.1 mycroft pci_device_foreach(pc, pcibios_max_bus, pciintr_do_header_fixup);
586 1.5.6.2 soda PCIBIOS_PRINTV(("------------------------------------------\n"));
587 1.1 thorpej
588 1.5 uch return (0);
589 1.5 uch }
590 1.1 thorpej
591 1.5 uch void
592 1.5 uch pciintr_do_header_fixup(pc, tag)
593 1.5 uch pci_chipset_tag_t pc;
594 1.5 uch pcitag_t tag;
595 1.5 uch {
596 1.5 uch struct pcibios_intr_routing *pir;
597 1.5 uch struct pciintr_link_map *l;
598 1.5 uch int pin, irq, link;
599 1.5 uch int bus, device, function;
600 1.5 uch pcireg_t intr, id;
601 1.5 uch
602 1.5 uch pci_decompose_tag(pc, tag, &bus, &device, &function);
603 1.5 uch id = pci_conf_read(pc, tag, PCI_ID_REG);
604 1.5 uch
605 1.5 uch intr = pci_conf_read(pc, tag, PCI_INTERRUPT_REG);
606 1.5 uch pin = PCI_INTERRUPT_PIN(intr);
607 1.5 uch irq = PCI_INTERRUPT_LINE(intr);
608 1.1 thorpej
609 1.5 uch if (pin == 0) {
610 1.5 uch /*
611 1.5 uch * No interrupt used.
612 1.5 uch */
613 1.5 uch return;
614 1.5 uch }
615 1.1 thorpej
616 1.5 uch pir = pciintr_pir_lookup(bus, device);
617 1.5 uch if (pir == NULL || (link = pir->linkmap[pin - 1].link) == 0) {
618 1.5 uch /*
619 1.5 uch * Interrupt not connected; no
620 1.5 uch * need to change.
621 1.5 uch */
622 1.5 uch return;
623 1.5 uch }
624 1.1 thorpej
625 1.5.6.2 soda l = pciintr_link_lookup(link);
626 1.5 uch if (l == NULL) {
627 1.5.6.2 soda #ifdef PCIINTR_DEBUG
628 1.5 uch /*
629 1.5.6.2 soda * No link map entry.
630 1.5.6.2 soda * Probably pciintr_icu_getclink() or pciintr_icu_get_intr()
631 1.5.6.2 soda * was failed.
632 1.5 uch */
633 1.5 uch printf("pciintr_header_fixup: no entry for link 0x%02x "
634 1.5 uch "(%d:%d:%d:%c)\n", link, bus, device, function,
635 1.5 uch '@' + pin);
636 1.5.6.2 soda #endif
637 1.5 uch return;
638 1.1 thorpej }
639 1.5.6.2 soda
640 1.5.6.2 soda #ifdef PCIBIOSVERBOSE
641 1.5.6.2 soda if (pcibiosverbose) {
642 1.5.6.2 soda printf("%03d:%02d:%d 0x%04x 0x%04x %c 0x%02x",
643 1.5.6.2 soda bus, device, function, PCI_VENDOR(id), PCI_PRODUCT(id),
644 1.5.6.2 soda '@' + pin, l->clink);
645 1.5.6.2 soda if (l->irq == I386_PCI_INTERRUPT_LINE_NO_CONNECTION)
646 1.5.6.2 soda printf(" -");
647 1.5.6.2 soda else
648 1.5.6.2 soda printf(" %3d", l->irq);
649 1.5.6.2 soda printf(" %d ", l->fixup_stage);
650 1.5.6.2 soda }
651 1.5.6.2 soda #endif
652 1.5 uch
653 1.5 uch /*
654 1.5 uch * IRQs 14 and 15 are reserved for PCI IDE interrupts; don't muck
655 1.5 uch * with them.
656 1.5 uch */
657 1.5.6.2 soda if (irq == 14 || irq == 15) {
658 1.5.6.2 soda PCIBIOS_PRINTV((" WARNING: ignored\n"));
659 1.5 uch return;
660 1.5.6.2 soda }
661 1.1 thorpej
662 1.5.6.2 soda if (l->irq == I386_PCI_INTERRUPT_LINE_NO_CONNECTION) {
663 1.5.6.2 soda /* Appropriate interrupt was not found. */
664 1.5.6.2 soda if (pciintr_icu_tag == NULL &&
665 1.5.6.2 soda irq != 0 && irq != I386_PCI_INTERRUPT_LINE_NO_CONNECTION) {
666 1.5.6.2 soda /*
667 1.5.6.2 soda * Do not print warning,
668 1.5.6.2 soda * if no compatible PCI ICU found,
669 1.5.6.2 soda * but the irq is already assigned by BIOS.
670 1.5.6.2 soda */
671 1.5.6.2 soda PCIBIOS_PRINTV(("\n"));
672 1.5.6.2 soda } else {
673 1.5.6.2 soda PCIBIOS_PRINTV((" WARNING: missing IRQ\n"));
674 1.5.6.2 soda }
675 1.5.6.2 soda return;
676 1.5.6.2 soda }
677 1.5.6.2 soda
678 1.5.6.2 soda if (l->irq == irq) {
679 1.5.6.2 soda /* don't have to reconfigure */
680 1.5.6.2 soda PCIBIOS_PRINTV((" already assigned\n"));
681 1.5.6.2 soda return;
682 1.5.6.2 soda }
683 1.5.6.2 soda
684 1.5.6.2 soda if (irq == 0 || irq == I386_PCI_INTERRUPT_LINE_NO_CONNECTION) {
685 1.5.6.2 soda PCIBIOS_PRINTV((" fixed up\n"));
686 1.5.6.2 soda } else {
687 1.5.6.2 soda /* routed by BIOS, but inconsistent */
688 1.5.6.2 soda #ifdef PCIBIOS_INTR_FIXUP_FORCE
689 1.5.6.2 soda /* believe PCI IRQ Routing table */
690 1.5.6.2 soda PCIBIOS_PRINTV((" WARNING: overriding irq %d\n", irq));
691 1.5.6.2 soda #else
692 1.5.6.2 soda /* believe PCI Interrupt Configuration Register (default) */
693 1.5.6.2 soda PCIBIOS_PRINTV((" WARNING: preserving irq %d\n", irq));
694 1.5.6.2 soda return;
695 1.1 thorpej #endif
696 1.5.6.2 soda }
697 1.1 thorpej
698 1.5 uch intr &= ~(PCI_INTERRUPT_LINE_MASK << PCI_INTERRUPT_LINE_SHIFT);
699 1.5 uch intr |= (l->irq << PCI_INTERRUPT_LINE_SHIFT);
700 1.5 uch pci_conf_write(pc, tag, PCI_INTERRUPT_REG, intr);
701 1.1 thorpej }
702 1.1 thorpej
703 1.1 thorpej int
704 1.1 thorpej pci_intr_fixup(pc, iot, pciirq)
705 1.1 thorpej pci_chipset_tag_t pc;
706 1.1 thorpej bus_space_tag_t iot;
707 1.1 thorpej u_int16_t *pciirq;
708 1.1 thorpej {
709 1.1 thorpej const struct pciintr_icu_table *piit = NULL;
710 1.1 thorpej pcitag_t icutag;
711 1.1 thorpej pcireg_t icuid;
712 1.1 thorpej
713 1.1 thorpej /*
714 1.1 thorpej * Attempt to initialize our PCI interrupt router. If
715 1.1 thorpej * the PIR Table is present in ROM, use the location
716 1.1 thorpej * specified by the PIR Table, and use the compat ID,
717 1.1 thorpej * if present. Otherwise, we have to look for the router
718 1.1 thorpej * ourselves (the PCI-ISA bridge).
719 1.1 thorpej */
720 1.1 thorpej if (pcibios_pir_header.signature != 0) {
721 1.1 thorpej icutag = pci_make_tag(pc, pcibios_pir_header.router_bus,
722 1.5.6.2 soda PIR_DEVFUNC_DEVICE(pcibios_pir_header.router_devfunc),
723 1.5.6.2 soda PIR_DEVFUNC_FUNCTION(pcibios_pir_header.router_devfunc));
724 1.1 thorpej icuid = pcibios_pir_header.compat_router;
725 1.1 thorpej if (icuid == 0 ||
726 1.1 thorpej (piit = pciintr_icu_lookup(icuid)) == NULL) {
727 1.1 thorpej /*
728 1.1 thorpej * No compat ID, or don't know the compat ID? Read
729 1.1 thorpej * it from the configuration header.
730 1.1 thorpej */
731 1.1 thorpej icuid = pci_conf_read(pc, icutag, PCI_ID_REG);
732 1.1 thorpej }
733 1.1 thorpej if (piit == NULL)
734 1.1 thorpej piit = pciintr_icu_lookup(icuid);
735 1.1 thorpej } else {
736 1.1 thorpej int device, maxdevs = pci_bus_maxdevs(pc, 0);
737 1.1 thorpej
738 1.1 thorpej /*
739 1.1 thorpej * Search configuration space for a known interrupt
740 1.1 thorpej * router.
741 1.1 thorpej */
742 1.1 thorpej for (device = 0; device < maxdevs; device++) {
743 1.1 thorpej icutag = pci_make_tag(pc, 0, device, 0);
744 1.1 thorpej icuid = pci_conf_read(pc, icutag, PCI_ID_REG);
745 1.1 thorpej
746 1.1 thorpej /* Invalid vendor ID value? */
747 1.1 thorpej if (PCI_VENDOR(icuid) == PCI_VENDOR_INVALID)
748 1.1 thorpej continue;
749 1.1 thorpej /* XXX Not invalid, but we've done this ~forever. */
750 1.1 thorpej if (PCI_VENDOR(icuid) == 0)
751 1.1 thorpej continue;
752 1.1 thorpej
753 1.1 thorpej piit = pciintr_icu_lookup(icuid);
754 1.1 thorpej if (piit != NULL)
755 1.1 thorpej break;
756 1.1 thorpej }
757 1.1 thorpej }
758 1.1 thorpej
759 1.1 thorpej if (piit == NULL) {
760 1.5.6.2 soda printf("pci_intr_fixup: no compatible PCI ICU found");
761 1.5.6.2 soda if (pcibios_pir_header.signature != 0 && icuid != 0)
762 1.5.6.2 soda printf(": ICU vendor 0x%04x product 0x%04x",
763 1.5.6.2 soda PCI_VENDOR(icuid), PCI_PRODUCT(icuid));
764 1.5.6.2 soda printf("\n");
765 1.5.6.2 soda #ifdef PCIBIOS_INTR_GUESS
766 1.5.6.2 soda if (pciintr_link_init())
767 1.5.6.2 soda return (-1); /* non-fatal */
768 1.5.6.2 soda if (pciintr_guess_irq())
769 1.5.6.2 soda return (-1); /* non-fatal */
770 1.5.6.2 soda if (pciintr_header_fixup(pc))
771 1.5.6.2 soda return (1); /* fatal */
772 1.5.6.2 soda return (0); /* success! */
773 1.5.6.2 soda #else
774 1.1 thorpej return (-1); /* non-fatal */
775 1.5.6.2 soda #endif
776 1.1 thorpej }
777 1.1 thorpej
778 1.1 thorpej /*
779 1.1 thorpej * Initialize the PCI ICU.
780 1.1 thorpej */
781 1.1 thorpej if ((*piit->piit_init)(pc, iot, icutag, &pciintr_icu_tag,
782 1.1 thorpej &pciintr_icu_handle) != 0)
783 1.1 thorpej return (-1); /* non-fatal */
784 1.1 thorpej
785 1.1 thorpej /*
786 1.1 thorpej * Initialize the PCI interrupt link map.
787 1.1 thorpej */
788 1.1 thorpej if (pciintr_link_init())
789 1.1 thorpej return (-1); /* non-fatal */
790 1.1 thorpej
791 1.1 thorpej /*
792 1.1 thorpej * Fix up the link->IRQ mappings.
793 1.1 thorpej */
794 1.1 thorpej if (pciintr_link_fixup() != 0)
795 1.1 thorpej return (-1); /* non-fatal */
796 1.1 thorpej
797 1.1 thorpej /*
798 1.1 thorpej * Now actually program the PCI ICU with the new
799 1.1 thorpej * routing information.
800 1.1 thorpej */
801 1.1 thorpej if (pciintr_link_route(pciirq) != 0)
802 1.1 thorpej return (1); /* fatal */
803 1.1 thorpej
804 1.1 thorpej /*
805 1.1 thorpej * Now that we've routed all of the PIRQs, rewrite the PCI
806 1.1 thorpej * configuration headers to reflect the new mapping.
807 1.1 thorpej */
808 1.1 thorpej if (pciintr_header_fixup(pc) != 0)
809 1.1 thorpej return (1); /* fatal */
810 1.1 thorpej
811 1.1 thorpej /*
812 1.1 thorpej * Free any unused PCI IRQs for ISA devices.
813 1.1 thorpej */
814 1.1 thorpej if (pciintr_irq_release(pciirq) != 0)
815 1.1 thorpej return (-1); /* non-fatal */
816 1.1 thorpej
817 1.1 thorpej /*
818 1.1 thorpej * All done!
819 1.1 thorpej */
820 1.1 thorpej return (0); /* success! */
821 1.1 thorpej }
822