pci_intr_fixup.c revision 1.3 1 1.3 uch /* $NetBSD: pci_intr_fixup.c,v 1.3 1999/12/13 15:42:05 uch 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 int old_irq;
96 1.1 thorpej SIMPLEQ_ENTRY(pciintr_link_map) list;
97 1.1 thorpej };
98 1.1 thorpej
99 1.1 thorpej pciintr_icu_tag_t pciintr_icu_tag;
100 1.1 thorpej pciintr_icu_handle_t pciintr_icu_handle;
101 1.1 thorpej
102 1.1 thorpej struct pciintr_link_map *pciintr_link_lookup_pin
103 1.1 thorpej __P((struct pcibios_intr_routing *, int));
104 1.1 thorpej struct pciintr_link_map *pciintr_link_lookup_link __P((int));
105 1.1 thorpej struct pciintr_link_map *pciintr_link_alloc __P((struct pcibios_intr_routing *,
106 1.1 thorpej int));
107 1.1 thorpej struct pcibios_intr_routing *pciintr_pir_lookup __P((int, int));
108 1.1 thorpej int pciintr_link_init __P((void));
109 1.1 thorpej int pciintr_link_fixup __P((void));
110 1.1 thorpej int pciintr_link_route __P((u_int16_t *));
111 1.1 thorpej int pciintr_irq_release __P((u_int16_t *));
112 1.1 thorpej int pciintr_header_fixup __P((pci_chipset_tag_t));
113 1.1 thorpej
114 1.1 thorpej SIMPLEQ_HEAD(, pciintr_link_map) pciintr_link_map_list;
115 1.1 thorpej
116 1.1 thorpej const struct pciintr_icu_table {
117 1.1 thorpej pci_vendor_id_t piit_vendor;
118 1.1 thorpej pci_product_id_t piit_product;
119 1.1 thorpej int (*piit_init) __P((pci_chipset_tag_t,
120 1.1 thorpej bus_space_tag_t, pcitag_t, pciintr_icu_tag_t *,
121 1.1 thorpej pciintr_icu_handle_t *));
122 1.1 thorpej } pciintr_icu_table[] = {
123 1.1 thorpej { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82371MX,
124 1.1 thorpej piix_init },
125 1.1 thorpej { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82371AB_ISA,
126 1.1 thorpej piix_init },
127 1.1 thorpej { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82371FB_ISA,
128 1.1 thorpej piix_init },
129 1.1 thorpej { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82371SB_ISA,
130 1.1 thorpej piix_init },
131 1.1 thorpej
132 1.1 thorpej { PCI_VENDOR_OPTI, PCI_PRODUCT_OPTI_82C558,
133 1.1 thorpej opti82c558_init },
134 1.1 thorpej { PCI_VENDOR_OPTI, PCI_PRODUCT_OPTI_82C700,
135 1.1 thorpej opti82c700_init },
136 1.1 thorpej
137 1.1 thorpej { PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VT82C586_ISA,
138 1.1 thorpej via82c586_init, },
139 1.1 thorpej
140 1.1 thorpej { PCI_VENDOR_SIS, PCI_PRODUCT_SIS_85C503,
141 1.1 thorpej sis85c503_init },
142 1.1 thorpej
143 1.1 thorpej { 0, 0,
144 1.1 thorpej NULL },
145 1.1 thorpej };
146 1.1 thorpej
147 1.1 thorpej const struct pciintr_icu_table *pciintr_icu_lookup __P((pcireg_t));
148 1.1 thorpej
149 1.1 thorpej const struct pciintr_icu_table *
150 1.1 thorpej pciintr_icu_lookup(id)
151 1.1 thorpej pcireg_t id;
152 1.1 thorpej {
153 1.1 thorpej const struct pciintr_icu_table *piit;
154 1.1 thorpej
155 1.1 thorpej for (piit = pciintr_icu_table;
156 1.1 thorpej piit->piit_init != NULL;
157 1.1 thorpej piit++) {
158 1.1 thorpej if (PCI_VENDOR(id) == piit->piit_vendor &&
159 1.1 thorpej PCI_PRODUCT(id) == piit->piit_product)
160 1.1 thorpej return (piit);
161 1.1 thorpej }
162 1.1 thorpej
163 1.1 thorpej return (NULL);
164 1.1 thorpej }
165 1.1 thorpej
166 1.1 thorpej struct pciintr_link_map *
167 1.1 thorpej pciintr_link_lookup_pin(pir, pin)
168 1.1 thorpej struct pcibios_intr_routing *pir;
169 1.1 thorpej int pin;
170 1.1 thorpej {
171 1.1 thorpej
172 1.1 thorpej return (pciintr_link_lookup_link(pir->linkmap[pin].link));
173 1.1 thorpej }
174 1.1 thorpej
175 1.1 thorpej struct pciintr_link_map *
176 1.1 thorpej pciintr_link_lookup_link(link)
177 1.1 thorpej int link;
178 1.1 thorpej {
179 1.1 thorpej struct pciintr_link_map *l;
180 1.1 thorpej
181 1.1 thorpej for (l = SIMPLEQ_FIRST(&pciintr_link_map_list); l != NULL;
182 1.1 thorpej l = SIMPLEQ_NEXT(l, list)) {
183 1.1 thorpej if (l->link == link)
184 1.1 thorpej return (l);
185 1.1 thorpej }
186 1.1 thorpej
187 1.1 thorpej return (NULL);
188 1.1 thorpej }
189 1.1 thorpej
190 1.1 thorpej struct pciintr_link_map *
191 1.1 thorpej pciintr_link_alloc(pir, pin)
192 1.1 thorpej struct pcibios_intr_routing *pir;
193 1.1 thorpej int pin;
194 1.1 thorpej {
195 1.1 thorpej struct pciintr_link_map *l, *lstart;
196 1.1 thorpej
197 1.1 thorpej l = malloc(sizeof(*l), M_DEVBUF, M_NOWAIT);
198 1.1 thorpej if (l == NULL)
199 1.1 thorpej panic("pciintr_link_alloc");
200 1.1 thorpej
201 1.1 thorpej memset(l, 0, sizeof(*l));
202 1.1 thorpej
203 1.1 thorpej l->link = pir->linkmap[pin].link;
204 1.1 thorpej l->bitmap = pir->linkmap[pin].bitmap;
205 1.1 thorpej
206 1.1 thorpej lstart = SIMPLEQ_FIRST(&pciintr_link_map_list);
207 1.1 thorpej if (lstart == NULL || lstart->link < l->link)
208 1.1 thorpej SIMPLEQ_INSERT_TAIL(&pciintr_link_map_list, l, list);
209 1.1 thorpej else
210 1.1 thorpej SIMPLEQ_INSERT_HEAD(&pciintr_link_map_list, l, list);
211 1.1 thorpej
212 1.1 thorpej return (l);
213 1.1 thorpej }
214 1.1 thorpej
215 1.1 thorpej struct pcibios_intr_routing *
216 1.1 thorpej pciintr_pir_lookup(bus, device)
217 1.1 thorpej int bus, device;
218 1.1 thorpej {
219 1.1 thorpej struct pcibios_intr_routing *pir;
220 1.1 thorpej int entry;
221 1.1 thorpej
222 1.1 thorpej if (pcibios_pir_table == NULL)
223 1.1 thorpej return (NULL);
224 1.1 thorpej
225 1.1 thorpej for (entry = 0; entry < pcibios_pir_table_nentries; entry++) {
226 1.1 thorpej pir = &pcibios_pir_table[entry];
227 1.1 thorpej if (pir->bus == bus && ((pir->device >> 3) & 0x1f) == device)
228 1.1 thorpej return (pir);
229 1.1 thorpej }
230 1.1 thorpej
231 1.1 thorpej return (NULL);
232 1.1 thorpej }
233 1.1 thorpej
234 1.1 thorpej int
235 1.1 thorpej pciintr_link_init()
236 1.1 thorpej {
237 1.1 thorpej int entry, pin, error, link, clink;
238 1.1 thorpej struct pcibios_intr_routing *pir;
239 1.1 thorpej struct pciintr_link_map *l;
240 1.1 thorpej
241 1.1 thorpej if (pcibios_pir_table == NULL) {
242 1.1 thorpej /* No PIR table; can't do anything. */
243 1.1 thorpej printf("pciintr_link_init: no PIR table\n");
244 1.1 thorpej return (1);
245 1.1 thorpej }
246 1.1 thorpej
247 1.1 thorpej error = 0;
248 1.1 thorpej SIMPLEQ_INIT(&pciintr_link_map_list);
249 1.1 thorpej
250 1.1 thorpej for (entry = 0; entry < pcibios_pir_table_nentries; entry++) {
251 1.1 thorpej pir = &pcibios_pir_table[entry];
252 1.1 thorpej for (pin = 0; pin < 4; pin++) {
253 1.1 thorpej link = pir->linkmap[pin].link;
254 1.1 thorpej if (link == 0) {
255 1.1 thorpej /* No connection for this pin. */
256 1.1 thorpej continue;
257 1.1 thorpej }
258 1.1 thorpej
259 1.1 thorpej /*
260 1.1 thorpej * Check the link value by asking the ICU for
261 1.1 thorpej * the canonical link value.
262 1.1 thorpej */
263 1.1 thorpej if (pciintr_icu_getclink(pciintr_icu_tag,
264 1.1 thorpej pciintr_icu_handle, link, &clink) != 0) {
265 1.1 thorpej /*
266 1.2 thorpej * Table entry is bogus. Just ignore it.
267 1.1 thorpej */
268 1.2 thorpej #ifdef PCIINTR_DEBUG
269 1.1 thorpej printf("pciintr_link_init: bad table entry: "
270 1.1 thorpej "bus %d device %d link 0x%02x\n",
271 1.1 thorpej pir->bus, (pir->device >> 3 & 0x1f), link);
272 1.2 thorpej #endif
273 1.1 thorpej continue;
274 1.1 thorpej }
275 1.1 thorpej
276 1.1 thorpej /*
277 1.1 thorpej * Multiple devices may be wired to the same
278 1.1 thorpej * interrupt; check to see if we've seen this
279 1.1 thorpej * one already. If not, allocate a new link
280 1.1 thorpej * map entry and stuff it in the map.
281 1.1 thorpej */
282 1.1 thorpej l = pciintr_link_lookup_pin(pir, pin);
283 1.1 thorpej if (l == NULL)
284 1.1 thorpej (void) pciintr_link_alloc(pir, pin);
285 1.1 thorpej }
286 1.1 thorpej }
287 1.1 thorpej
288 1.1 thorpej return (error);
289 1.1 thorpej }
290 1.1 thorpej
291 1.1 thorpej int
292 1.1 thorpej pciintr_link_fixup()
293 1.1 thorpej {
294 1.1 thorpej struct pciintr_link_map *l;
295 1.1 thorpej u_int16_t pciirq, bitmap;
296 1.1 thorpej int i, j, cnt, irq;
297 1.1 thorpej
298 1.1 thorpej /*
299 1.1 thorpej * First stage: Attempt to connect PIRQs which aren't
300 1.1 thorpej * yet connected.
301 1.1 thorpej */
302 1.3 uch pciirq = 0;
303 1.3 uch
304 1.1 thorpej for (l = SIMPLEQ_FIRST(&pciintr_link_map_list); l != NULL;
305 1.1 thorpej l = SIMPLEQ_NEXT(l, list)) {
306 1.1 thorpej /*
307 1.1 thorpej * Get the canonical link value for this entry.
308 1.1 thorpej */
309 1.1 thorpej if (pciintr_icu_getclink(pciintr_icu_tag, pciintr_icu_handle,
310 1.1 thorpej l->link, &l->clink) != 0) {
311 1.1 thorpej /*
312 1.1 thorpej * ICU doesn't understand this link value.
313 1.1 thorpej */
314 1.1 thorpej #ifdef PCIINTR_DEBUG
315 1.1 thorpej printf("pciintr_link_fixup: link 0x%02x invalid\n",
316 1.1 thorpej l->link);
317 1.1 thorpej #endif
318 1.1 thorpej l->clink = -1;
319 1.1 thorpej continue;
320 1.1 thorpej }
321 1.1 thorpej
322 1.1 thorpej /*
323 1.1 thorpej * Determine if this PIRQ is mapped to an IRQ.
324 1.1 thorpej */
325 1.1 thorpej if (pciintr_icu_get_intr(pciintr_icu_tag, pciintr_icu_handle,
326 1.1 thorpej l->clink, &irq) != 0) {
327 1.1 thorpej /*
328 1.1 thorpej * ICU doesn't understand this PIRQ value.
329 1.1 thorpej */
330 1.1 thorpej l->clink = -1;
331 1.1 thorpej #ifdef PCIINTR_DEBUG
332 1.1 thorpej printf("pciintr_link_fixup: PIRQ %d invalid\n",
333 1.1 thorpej l->clink);
334 1.1 thorpej #endif
335 1.1 thorpej continue;
336 1.1 thorpej }
337 1.1 thorpej
338 1.1 thorpej if (irq == 0xff) {
339 1.1 thorpej /*
340 1.1 thorpej * Interrupt isn't connected. Attempt to assign
341 1.1 thorpej * it to an IRQ.
342 1.1 thorpej */
343 1.1 thorpej #ifdef PCIINTR_DEBUG
344 1.1 thorpej printf("pciintr_link_fixup: PIRQ %d not connected",
345 1.1 thorpej l->clink);
346 1.1 thorpej #endif
347 1.1 thorpej bitmap = l->bitmap;
348 1.1 thorpej for (i = 0, j = 0xff, cnt = 0; i < 16; i++)
349 1.1 thorpej if (bitmap & (1 << i))
350 1.1 thorpej j = i, cnt++;
351 1.1 thorpej /*
352 1.1 thorpej * Just do the easy case now; we'll defer the
353 1.1 thorpej * harder ones to Stage 2.
354 1.1 thorpej */
355 1.1 thorpej if (cnt == 1) {
356 1.1 thorpej l->irq = j;
357 1.1 thorpej l->old_irq = irq;
358 1.1 thorpej l->fixup_stage = 1;
359 1.1 thorpej pciirq |= 1 << j;
360 1.1 thorpej #ifdef PCIINTR_DEBUG
361 1.1 thorpej printf(", assigning IRQ %d", l->irq);
362 1.1 thorpej #endif
363 1.1 thorpej }
364 1.1 thorpej #ifdef PCIINTR_DEBUG
365 1.1 thorpej printf("\n");
366 1.1 thorpej #endif
367 1.1 thorpej } else {
368 1.1 thorpej /*
369 1.1 thorpej * Interrupt is already connected. Don't do
370 1.1 thorpej * anything to it.
371 1.1 thorpej */
372 1.1 thorpej l->irq = irq;
373 1.1 thorpej pciirq |= 1 << irq;
374 1.1 thorpej #ifdef PCIINTR_DEBUG
375 1.1 thorpej printf("pciintr_link_fixup: PIRQ %d already connected "
376 1.1 thorpej "to IRQ %d\n", l->clink, l->irq);
377 1.1 thorpej #endif
378 1.1 thorpej }
379 1.1 thorpej }
380 1.1 thorpej
381 1.1 thorpej /*
382 1.1 thorpej * Stage 2: Attempt to connect PIRQs which we didn't
383 1.1 thorpej * connect in Stage 1.
384 1.1 thorpej */
385 1.1 thorpej for (l = SIMPLEQ_FIRST(&pciintr_link_map_list); l != NULL;
386 1.1 thorpej l = SIMPLEQ_NEXT(l, list)) {
387 1.1 thorpej if (l->irq == 0) {
388 1.1 thorpej bitmap = l->bitmap;
389 1.1 thorpej for (i = 0; i < 16; i++) {
390 1.1 thorpej if ((pciirq & (1 << i)) != 0 &&
391 1.1 thorpej (bitmap & (1 << i)) != 0) {
392 1.1 thorpej /*
393 1.1 thorpej * This IRQ is a valid PCI
394 1.1 thorpej * IRQ already connected to
395 1.1 thorpej * another PIRQ, and also an
396 1.1 thorpej * IRQ our PIRQ can use; connect
397 1.1 thorpej * it up!
398 1.1 thorpej */
399 1.1 thorpej l->irq = i;
400 1.1 thorpej l->old_irq = 0xff;
401 1.1 thorpej l->fixup_stage = 2;
402 1.1 thorpej #ifdef PCIINTR_DEBUG
403 1.1 thorpej printf("pciintr_link_fixup: assigning "
404 1.1 thorpej "IRQ %d to PIRQ %d\n", l->irq,
405 1.1 thorpej l->clink);
406 1.1 thorpej #endif
407 1.1 thorpej break;
408 1.1 thorpej }
409 1.1 thorpej }
410 1.1 thorpej }
411 1.1 thorpej }
412 1.1 thorpej
413 1.1 thorpej /*
414 1.1 thorpej * Stage 3: Allow the user to specify interrupt routing
415 1.1 thorpej * information, overriding what we've done above.
416 1.1 thorpej */
417 1.1 thorpej /* XXX Not implemented. */
418 1.1 thorpej
419 1.1 thorpej return (0);
420 1.1 thorpej }
421 1.1 thorpej
422 1.1 thorpej int
423 1.1 thorpej pciintr_link_route(pciirq)
424 1.1 thorpej u_int16_t *pciirq;
425 1.1 thorpej {
426 1.1 thorpej struct pciintr_link_map *l;
427 1.1 thorpej int rv = 0;
428 1.1 thorpej
429 1.1 thorpej *pciirq = 0;
430 1.1 thorpej
431 1.1 thorpej for (l = SIMPLEQ_FIRST(&pciintr_link_map_list); l != NULL;
432 1.1 thorpej l = SIMPLEQ_NEXT(l, list)) {
433 1.1 thorpej if (pciintr_icu_set_intr(pciintr_icu_tag, pciintr_icu_handle,
434 1.1 thorpej l->clink, l->irq) != 0 ||
435 1.1 thorpej pciintr_icu_set_trigger(pciintr_icu_tag, pciintr_icu_handle,
436 1.1 thorpej l->irq, IST_LEVEL) != 0) {
437 1.1 thorpej printf("pciintr_link_route: route of PIRQ %d -> IRQ %d"
438 1.1 thorpej " failed\n", l->clink, l->irq);
439 1.1 thorpej rv = 1;
440 1.1 thorpej } else {
441 1.1 thorpej /*
442 1.1 thorpej * Succssfully routed interrupt. Mark this as
443 1.1 thorpej * a PCI interrupt.
444 1.1 thorpej */
445 1.1 thorpej *pciirq |= (1 << l->irq);
446 1.1 thorpej }
447 1.1 thorpej }
448 1.1 thorpej
449 1.1 thorpej return (rv);
450 1.1 thorpej }
451 1.1 thorpej
452 1.1 thorpej int
453 1.1 thorpej pciintr_irq_release(pciirq)
454 1.1 thorpej u_int16_t *pciirq;
455 1.1 thorpej {
456 1.1 thorpej int i;
457 1.1 thorpej
458 1.1 thorpej for (i = 0; i < 16; i++) {
459 1.1 thorpej if ((*pciirq & (1 << i)) == 0)
460 1.1 thorpej (void) pciintr_icu_set_trigger(pciintr_icu_tag,
461 1.1 thorpej pciintr_icu_handle, i, IST_EDGE);
462 1.1 thorpej }
463 1.1 thorpej
464 1.1 thorpej return (0);
465 1.1 thorpej }
466 1.1 thorpej
467 1.1 thorpej int
468 1.1 thorpej pciintr_header_fixup(pc)
469 1.1 thorpej pci_chipset_tag_t pc;
470 1.1 thorpej {
471 1.1 thorpej const struct pci_quirkdata *qd;
472 1.1 thorpej struct pcibios_intr_routing *pir;
473 1.1 thorpej struct pciintr_link_map *l;
474 1.1 thorpej int pin, bus, device, function, maxdevs, nfuncs, irq, link;
475 1.1 thorpej pcireg_t id, bhlcr, intr;
476 1.1 thorpej pcitag_t tag;
477 1.1 thorpej
478 1.1 thorpej #ifdef PCIBIOSVERBOSE
479 1.1 thorpej printf("--------------------------------------------\n");
480 1.1 thorpej printf(" device vendor product pin PIRQ IRQ stage\n");
481 1.1 thorpej printf("--------------------------------------------\n");
482 1.1 thorpej #endif
483 1.1 thorpej
484 1.1 thorpej for (bus = 0; bus <= pcibios_max_bus; bus++) {
485 1.1 thorpej maxdevs = pci_bus_maxdevs(pc, bus);
486 1.1 thorpej for (device = 0; device < maxdevs; device++) {
487 1.1 thorpej tag = pci_make_tag(pc, bus, device, 0);
488 1.1 thorpej id = pci_conf_read(pc, tag, PCI_ID_REG);
489 1.1 thorpej
490 1.1 thorpej /* Invalid vendor ID value? */
491 1.1 thorpej if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
492 1.1 thorpej continue;
493 1.1 thorpej /* XXX Not invalid, but we've done this ~forever. */
494 1.1 thorpej if (PCI_VENDOR(id) == 0)
495 1.1 thorpej continue;
496 1.1 thorpej
497 1.1 thorpej qd = pci_lookup_quirkdata(PCI_VENDOR(id),
498 1.1 thorpej PCI_PRODUCT(id));
499 1.1 thorpej
500 1.1 thorpej bhlcr = pci_conf_read(pc, tag, PCI_BHLC_REG);
501 1.1 thorpej if (PCI_HDRTYPE_MULTIFN(bhlcr) ||
502 1.1 thorpej (qd != NULL &&
503 1.1 thorpej (qd->quirks & PCI_QUIRK_MULTIFUNCTION) != 0))
504 1.1 thorpej nfuncs = 8;
505 1.1 thorpej else
506 1.1 thorpej nfuncs = 1;
507 1.1 thorpej
508 1.1 thorpej for (function = 0; function < nfuncs; function++) {
509 1.1 thorpej tag = pci_make_tag(pc, bus, device, function);
510 1.1 thorpej id = pci_conf_read(pc, tag, PCI_ID_REG);
511 1.1 thorpej intr = pci_conf_read(pc, tag,
512 1.1 thorpej PCI_INTERRUPT_REG);
513 1.1 thorpej
514 1.1 thorpej /* Invalid vendor ID value? */
515 1.1 thorpej if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
516 1.1 thorpej continue;
517 1.1 thorpej /*
518 1.1 thorpej * XXX Not invalid, but we've done this
519 1.1 thorpej * ~forever.
520 1.1 thorpej */
521 1.1 thorpej if (PCI_VENDOR(id) == 0)
522 1.1 thorpej continue;
523 1.1 thorpej
524 1.1 thorpej pin = PCI_INTERRUPT_PIN(intr);
525 1.1 thorpej irq = PCI_INTERRUPT_LINE(intr);
526 1.1 thorpej
527 1.1 thorpej if (pin == 0) {
528 1.1 thorpej /*
529 1.1 thorpej * No interrupt used.
530 1.1 thorpej */
531 1.1 thorpej continue;
532 1.1 thorpej }
533 1.1 thorpej
534 1.1 thorpej pir = pciintr_pir_lookup(bus, device);
535 1.1 thorpej if (pir == NULL ||
536 1.1 thorpej (link = pir->linkmap[pin - 1].link) == 0) {
537 1.1 thorpej /*
538 1.1 thorpej * Interrupt not connected; no
539 1.1 thorpej * need to change.
540 1.1 thorpej */
541 1.1 thorpej continue;
542 1.1 thorpej }
543 1.1 thorpej
544 1.1 thorpej l = pciintr_link_lookup_link(link);
545 1.1 thorpej if (l == NULL) {
546 1.1 thorpej /*
547 1.1 thorpej * No link map entry?!
548 1.1 thorpej */
549 1.1 thorpej printf("pciintr_header_fixup: no entry "
550 1.1 thorpej "for link 0x%02x (%d:%d:%d:%c)\n",
551 1.1 thorpej link, bus, device, function,
552 1.1 thorpej '@' + pin);
553 1.1 thorpej continue;
554 1.1 thorpej }
555 1.1 thorpej
556 1.1 thorpej /*
557 1.1 thorpej * IRQs 14 and 15 are reserved for
558 1.1 thorpej * PCI IDE interrupts; don't muck
559 1.1 thorpej * with them.
560 1.1 thorpej */
561 1.1 thorpej if (irq == 14 || irq == 15)
562 1.1 thorpej continue;
563 1.1 thorpej
564 1.1 thorpej #ifdef PCIBIOSVERBOSE
565 1.1 thorpej printf("%03d:%02d:%d 0x%04x 0x%04x %c "
566 1.1 thorpej "0x%02x %02d %d\n",
567 1.1 thorpej bus, device, function,
568 1.1 thorpej PCI_VENDOR(id), PCI_PRODUCT(id),
569 1.1 thorpej '@' + pin, l->clink, l->irq,
570 1.1 thorpej l->fixup_stage);
571 1.1 thorpej #endif
572 1.1 thorpej
573 1.1 thorpej intr &= ~(PCI_INTERRUPT_LINE_MASK <<
574 1.1 thorpej PCI_INTERRUPT_LINE_SHIFT);
575 1.1 thorpej intr |= (l->irq << PCI_INTERRUPT_LINE_SHIFT);
576 1.1 thorpej pci_conf_write(pc, tag, PCI_INTERRUPT_REG,
577 1.1 thorpej intr);
578 1.1 thorpej }
579 1.1 thorpej }
580 1.1 thorpej }
581 1.1 thorpej
582 1.1 thorpej #ifdef PCIBIOSVERBOSE
583 1.1 thorpej printf("--------------------------------------------\n");
584 1.1 thorpej #endif
585 1.1 thorpej
586 1.1 thorpej return (0);
587 1.1 thorpej }
588 1.1 thorpej
589 1.1 thorpej int
590 1.1 thorpej pci_intr_fixup(pc, iot, pciirq)
591 1.1 thorpej pci_chipset_tag_t pc;
592 1.1 thorpej bus_space_tag_t iot;
593 1.1 thorpej u_int16_t *pciirq;
594 1.1 thorpej {
595 1.1 thorpej const struct pciintr_icu_table *piit = NULL;
596 1.1 thorpej pcitag_t icutag;
597 1.1 thorpej pcireg_t icuid;
598 1.1 thorpej
599 1.1 thorpej /*
600 1.1 thorpej * Attempt to initialize our PCI interrupt router. If
601 1.1 thorpej * the PIR Table is present in ROM, use the location
602 1.1 thorpej * specified by the PIR Table, and use the compat ID,
603 1.1 thorpej * if present. Otherwise, we have to look for the router
604 1.1 thorpej * ourselves (the PCI-ISA bridge).
605 1.1 thorpej */
606 1.1 thorpej if (pcibios_pir_header.signature != 0) {
607 1.1 thorpej icutag = pci_make_tag(pc, pcibios_pir_header.router_bus,
608 1.1 thorpej (pcibios_pir_header.router_devfunc >> 3) & 0x1f,
609 1.1 thorpej pcibios_pir_header.router_devfunc & 7);
610 1.1 thorpej icuid = pcibios_pir_header.compat_router;
611 1.1 thorpej if (icuid == 0 ||
612 1.1 thorpej (piit = pciintr_icu_lookup(icuid)) == NULL) {
613 1.1 thorpej /*
614 1.1 thorpej * No compat ID, or don't know the compat ID? Read
615 1.1 thorpej * it from the configuration header.
616 1.1 thorpej */
617 1.1 thorpej icuid = pci_conf_read(pc, icutag, PCI_ID_REG);
618 1.1 thorpej }
619 1.1 thorpej if (piit == NULL)
620 1.1 thorpej piit = pciintr_icu_lookup(icuid);
621 1.1 thorpej } else {
622 1.1 thorpej int device, maxdevs = pci_bus_maxdevs(pc, 0);
623 1.1 thorpej
624 1.1 thorpej /*
625 1.1 thorpej * Search configuration space for a known interrupt
626 1.1 thorpej * router.
627 1.1 thorpej */
628 1.1 thorpej for (device = 0; device < maxdevs; device++) {
629 1.1 thorpej icutag = pci_make_tag(pc, 0, device, 0);
630 1.1 thorpej icuid = pci_conf_read(pc, icutag, PCI_ID_REG);
631 1.1 thorpej
632 1.1 thorpej /* Invalid vendor ID value? */
633 1.1 thorpej if (PCI_VENDOR(icuid) == PCI_VENDOR_INVALID)
634 1.1 thorpej continue;
635 1.1 thorpej /* XXX Not invalid, but we've done this ~forever. */
636 1.1 thorpej if (PCI_VENDOR(icuid) == 0)
637 1.1 thorpej continue;
638 1.1 thorpej
639 1.1 thorpej piit = pciintr_icu_lookup(icuid);
640 1.1 thorpej if (piit != NULL)
641 1.1 thorpej break;
642 1.1 thorpej }
643 1.1 thorpej }
644 1.1 thorpej
645 1.1 thorpej if (piit == NULL) {
646 1.1 thorpej printf("pci_intr_fixup: no compatible PCI ICU found\n");
647 1.1 thorpej return (-1); /* non-fatal */
648 1.1 thorpej }
649 1.1 thorpej
650 1.1 thorpej /*
651 1.1 thorpej * Initialize the PCI ICU.
652 1.1 thorpej */
653 1.1 thorpej if ((*piit->piit_init)(pc, iot, icutag, &pciintr_icu_tag,
654 1.1 thorpej &pciintr_icu_handle) != 0)
655 1.1 thorpej return (-1); /* non-fatal */
656 1.1 thorpej
657 1.1 thorpej /*
658 1.1 thorpej * Initialize the PCI interrupt link map.
659 1.1 thorpej */
660 1.1 thorpej if (pciintr_link_init())
661 1.1 thorpej return (-1); /* non-fatal */
662 1.1 thorpej
663 1.1 thorpej /*
664 1.1 thorpej * Fix up the link->IRQ mappings.
665 1.1 thorpej */
666 1.1 thorpej if (pciintr_link_fixup() != 0)
667 1.1 thorpej return (-1); /* non-fatal */
668 1.1 thorpej
669 1.1 thorpej /*
670 1.1 thorpej * Now actually program the PCI ICU with the new
671 1.1 thorpej * routing information.
672 1.1 thorpej */
673 1.1 thorpej if (pciintr_link_route(pciirq) != 0)
674 1.1 thorpej return (1); /* fatal */
675 1.1 thorpej
676 1.1 thorpej /*
677 1.1 thorpej * Now that we've routed all of the PIRQs, rewrite the PCI
678 1.1 thorpej * configuration headers to reflect the new mapping.
679 1.1 thorpej */
680 1.1 thorpej if (pciintr_header_fixup(pc) != 0)
681 1.1 thorpej return (1); /* fatal */
682 1.1 thorpej
683 1.1 thorpej /*
684 1.1 thorpej * Free any unused PCI IRQs for ISA devices.
685 1.1 thorpej */
686 1.1 thorpej if (pciintr_irq_release(pciirq) != 0)
687 1.1 thorpej return (-1); /* non-fatal */
688 1.1 thorpej
689 1.1 thorpej /*
690 1.1 thorpej * All done!
691 1.1 thorpej */
692 1.1 thorpej return (0); /* success! */
693 1.1 thorpej }
694