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