pcibios.c revision 1.22 1 1.22 perry /* $NetBSD: pcibios.c,v 1.22 2005/02/03 21:35:44 perry 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 * Interface to the PCI BIOS and PCI Interrupt Routing table.
67 1.1 thorpej */
68 1.7 lukem
69 1.7 lukem #include <sys/cdefs.h>
70 1.22 perry __KERNEL_RCSID(0, "$NetBSD: pcibios.c,v 1.22 2005/02/03 21:35:44 perry Exp $");
71 1.1 thorpej
72 1.1 thorpej #include "opt_pcibios.h"
73 1.1 thorpej
74 1.1 thorpej #include <sys/param.h>
75 1.1 thorpej #include <sys/systm.h>
76 1.1 thorpej #include <sys/device.h>
77 1.1 thorpej #include <sys/malloc.h>
78 1.1 thorpej
79 1.1 thorpej #include <dev/isa/isareg.h>
80 1.1 thorpej #include <machine/isa_machdep.h>
81 1.1 thorpej
82 1.1 thorpej #include <dev/pci/pcireg.h>
83 1.1 thorpej #include <dev/pci/pcivar.h>
84 1.3 uch #include <dev/pci/pcidevs.h>
85 1.1 thorpej
86 1.1 thorpej #include <i386/pci/pcibios.h>
87 1.1 thorpej #ifdef PCIBIOS_INTR_FIXUP
88 1.1 thorpej #include <i386/pci/pci_intr_fixup.h>
89 1.1 thorpej #endif
90 1.2 thorpej #ifdef PCIBIOS_BUS_FIXUP
91 1.2 thorpej #include <i386/pci/pci_bus_fixup.h>
92 1.2 thorpej #endif
93 1.3 uch #ifdef PCIBIOS_ADDR_FIXUP
94 1.3 uch #include <i386/pci/pci_addr_fixup.h>
95 1.3 uch #endif
96 1.1 thorpej
97 1.1 thorpej #include <machine/bios32.h>
98 1.1 thorpej
99 1.4 soda #ifdef PCIBIOSVERBOSE
100 1.4 soda int pcibiosverbose = 1;
101 1.4 soda #endif
102 1.4 soda
103 1.1 thorpej int pcibios_present;
104 1.1 thorpej
105 1.1 thorpej struct pcibios_pir_header pcibios_pir_header;
106 1.1 thorpej struct pcibios_intr_routing *pcibios_pir_table;
107 1.1 thorpej int pcibios_pir_table_nentries;
108 1.1 thorpej int pcibios_max_bus;
109 1.1 thorpej
110 1.1 thorpej struct bios32_entry pcibios_entry;
111 1.1 thorpej
112 1.16 kochi void pcibios_pir_init(void);
113 1.1 thorpej
114 1.16 kochi int pcibios_get_status(u_int32_t *, u_int32_t *, u_int32_t *,
115 1.16 kochi u_int32_t *, u_int32_t *, u_int32_t *, u_int32_t *);
116 1.16 kochi int pcibios_get_intr_routing(struct pcibios_intr_routing *,
117 1.16 kochi int *, u_int16_t *);
118 1.1 thorpej
119 1.16 kochi int pcibios_return_code(u_int16_t, const char *);
120 1.1 thorpej
121 1.16 kochi void pcibios_print_exclirq(void);
122 1.18 christos
123 1.18 christos #ifdef PCIBIOS_LIBRETTO_FIXUP
124 1.18 christos /* for Libretto L2/L3 hack */
125 1.18 christos static void pcibios_fixup_pir_table(void);
126 1.18 christos static void pcibios_fixup_pir_table_mask(struct pcibios_linkmap *);
127 1.18 christos
128 1.18 christos struct pcibios_linkmap pir_mask[] = {
129 1.18 christos { 2, 0x0040 },
130 1.18 christos { 7, 0x0080 },
131 1.18 christos { 8, 0x0020 },
132 1.18 christos { 0, 0x0000 }
133 1.18 christos };
134 1.18 christos #endif
135 1.18 christos
136 1.20 augustss #ifdef PCIBIOS_SHARP_MM20_FIXUP
137 1.20 augustss static void pcibios_mm20_fixup(void);
138 1.20 augustss #endif
139 1.20 augustss
140 1.1 thorpej #ifdef PCIINTR_DEBUG
141 1.16 kochi void pcibios_print_pir_table(void);
142 1.1 thorpej #endif
143 1.1 thorpej
144 1.1 thorpej #define PCI_IRQ_TABLE_START 0xf0000
145 1.1 thorpej #define PCI_IRQ_TABLE_END 0xfffff
146 1.1 thorpej
147 1.8 uch static void pci_bridge_hook(pci_chipset_tag_t, pcitag_t, void *);
148 1.8 uch struct pci_bridge_hook_arg {
149 1.8 uch void (*func)(pci_chipset_tag_t, pcitag_t, void *);
150 1.8 uch void *arg;
151 1.8 uch };
152 1.8 uch
153 1.1 thorpej void
154 1.22 perry pcibios_init(void)
155 1.1 thorpej {
156 1.1 thorpej struct bios32_entry_info ei;
157 1.1 thorpej u_int32_t rev_maj, rev_min, mech1, mech2, scmech1, scmech2;
158 1.1 thorpej
159 1.1 thorpej if (bios32_service(BIOS32_MAKESIG('$', 'P', 'C', 'I'),
160 1.1 thorpej &pcibios_entry, &ei) == 0) {
161 1.1 thorpej /*
162 1.1 thorpej * No PCI BIOS found; will fall back on old
163 1.1 thorpej * mechanism.
164 1.1 thorpej */
165 1.1 thorpej return;
166 1.1 thorpej }
167 1.1 thorpej
168 1.1 thorpej /*
169 1.1 thorpej * We've located the PCI BIOS service; get some information
170 1.1 thorpej * about it.
171 1.1 thorpej */
172 1.1 thorpej if (pcibios_get_status(&rev_maj, &rev_min, &mech1, &mech2,
173 1.1 thorpej &scmech1, &scmech2, &pcibios_max_bus) != PCIBIOS_SUCCESS) {
174 1.1 thorpej /*
175 1.1 thorpej * We can't use the PCI BIOS; will fall back on old
176 1.1 thorpej * mechanism.
177 1.1 thorpej */
178 1.1 thorpej return;
179 1.1 thorpej }
180 1.1 thorpej
181 1.1 thorpej printf("PCI BIOS rev. %d.%d found at 0x%lx\n", rev_maj, rev_min >> 4,
182 1.1 thorpej ei.bei_entry);
183 1.1 thorpej #ifdef PCIBIOSVERBOSE
184 1.1 thorpej printf("pcibios: config mechanism %s%s, special cycles %s%s, "
185 1.1 thorpej "last bus %d\n",
186 1.1 thorpej mech1 ? "[1]" : "[x]",
187 1.1 thorpej mech2 ? "[2]" : "[x]",
188 1.1 thorpej scmech1 ? "[1]" : "[x]",
189 1.1 thorpej scmech2 ? "[2]" : "[x]",
190 1.1 thorpej pcibios_max_bus);
191 1.1 thorpej
192 1.1 thorpej #endif
193 1.1 thorpej
194 1.1 thorpej /*
195 1.1 thorpej * The PCI BIOS tells us the config mechanism; fill it in now
196 1.1 thorpej * so that pci_mode_detect() doesn't have to look for it.
197 1.1 thorpej */
198 1.1 thorpej pci_mode = mech1 ? 1 : 2;
199 1.1 thorpej
200 1.1 thorpej pcibios_present = 1;
201 1.1 thorpej
202 1.1 thorpej /*
203 1.1 thorpej * Find the PCI IRQ Routing table.
204 1.1 thorpej */
205 1.1 thorpej pcibios_pir_init();
206 1.1 thorpej
207 1.1 thorpej #ifdef PCIBIOS_INTR_FIXUP
208 1.1 thorpej if (pcibios_pir_table != NULL) {
209 1.1 thorpej int rv;
210 1.1 thorpej u_int16_t pciirq;
211 1.1 thorpej
212 1.1 thorpej /*
213 1.1 thorpej * Fixup interrupt routing.
214 1.1 thorpej */
215 1.11 fvdl rv = pci_intr_fixup(NULL, X86_BUS_SPACE_IO, &pciirq);
216 1.1 thorpej switch (rv) {
217 1.1 thorpej case -1:
218 1.1 thorpej /* Non-fatal error. */
219 1.1 thorpej printf("Warning: unable to fix up PCI interrupt "
220 1.1 thorpej "routing\n");
221 1.1 thorpej break;
222 1.1 thorpej
223 1.1 thorpej case 1:
224 1.1 thorpej /* Fatal error. */
225 1.1 thorpej panic("pcibios_init: interrupt fixup failed");
226 1.1 thorpej break;
227 1.1 thorpej }
228 1.1 thorpej
229 1.1 thorpej /*
230 1.1 thorpej * XXX Clear `pciirq' from the ISA interrupt allocation
231 1.1 thorpej * XXX mask.
232 1.1 thorpej */
233 1.1 thorpej }
234 1.2 thorpej #endif
235 1.2 thorpej
236 1.2 thorpej #ifdef PCIBIOS_BUS_FIXUP
237 1.2 thorpej pcibios_max_bus = pci_bus_fixup(NULL, 0);
238 1.2 thorpej #ifdef PCIBIOSVERBOSE
239 1.2 thorpej printf("PCI bus #%d is the last bus\n", pcibios_max_bus);
240 1.2 thorpej #endif
241 1.1 thorpej #endif
242 1.3 uch
243 1.3 uch #ifdef PCIBIOS_ADDR_FIXUP
244 1.5 uch pci_addr_fixup(NULL, pcibios_max_bus);
245 1.3 uch #endif
246 1.1 thorpej }
247 1.1 thorpej
248 1.1 thorpej void
249 1.22 perry pcibios_pir_init(void)
250 1.1 thorpej {
251 1.19 kochi char *devinfo;
252 1.1 thorpej paddr_t pa;
253 1.1 thorpej caddr_t p;
254 1.1 thorpej unsigned char cksum;
255 1.1 thorpej u_int16_t tablesize;
256 1.1 thorpej u_int8_t rev_maj, rev_min;
257 1.1 thorpej int i;
258 1.1 thorpej
259 1.1 thorpej for (pa = PCI_IRQ_TABLE_START; pa < PCI_IRQ_TABLE_END; pa += 16) {
260 1.1 thorpej p = (caddr_t)ISA_HOLE_VADDR(pa);
261 1.9 christos if (*(int *)p != BIOS32_MAKESIG('$', 'P', 'I', 'R')) {
262 1.9 christos /*
263 1.12 drochner * XXX: Some laptops (Toshiba/Libretto L series)
264 1.9 christos * use _PIR instead of $PIR. So we try that too.
265 1.9 christos */
266 1.9 christos if (*(int *)p != BIOS32_MAKESIG('_', 'P', 'I', 'R'))
267 1.9 christos continue;
268 1.9 christos }
269 1.1 thorpej
270 1.1 thorpej rev_min = *(p + 4);
271 1.1 thorpej rev_maj = *(p + 5);
272 1.1 thorpej tablesize = *(u_int16_t *)(p + 6);
273 1.1 thorpej
274 1.1 thorpej cksum = 0;
275 1.1 thorpej for (i = 0; i < tablesize; i++)
276 1.1 thorpej cksum += *(unsigned char *)(p + i);
277 1.1 thorpej
278 1.1 thorpej printf("PCI IRQ Routing Table rev. %d.%d found at 0x%lx, "
279 1.1 thorpej "size %d bytes (%d entries)\n", rev_maj, rev_min, pa,
280 1.1 thorpej tablesize, (tablesize - 32) / 16);
281 1.1 thorpej
282 1.1 thorpej if (cksum != 0) {
283 1.1 thorpej printf("pcibios_pir_init: bad IRQ table checksum\n");
284 1.1 thorpej continue;
285 1.1 thorpej }
286 1.1 thorpej
287 1.1 thorpej if (tablesize < 32 || (tablesize % 16) != 0) {
288 1.1 thorpej printf("pcibios_pir_init: bad IRQ table size\n");
289 1.1 thorpej continue;
290 1.1 thorpej }
291 1.1 thorpej
292 1.1 thorpej if (rev_maj != 1 || rev_min != 0) {
293 1.1 thorpej printf("pcibios_pir_init: unsupported IRQ table "
294 1.1 thorpej "version\n");
295 1.1 thorpej continue;
296 1.1 thorpej }
297 1.1 thorpej
298 1.1 thorpej /*
299 1.1 thorpej * We can handle this table! Make a copy of it.
300 1.1 thorpej */
301 1.1 thorpej memcpy(&pcibios_pir_header, p, 32);
302 1.1 thorpej pcibios_pir_table = malloc(tablesize - 32, M_DEVBUF,
303 1.1 thorpej M_NOWAIT);
304 1.1 thorpej if (pcibios_pir_table == NULL) {
305 1.1 thorpej printf("pcibios_pir_init: no memory for $PIR\n");
306 1.1 thorpej return;
307 1.1 thorpej }
308 1.1 thorpej memcpy(pcibios_pir_table, p + 32, tablesize - 32);
309 1.1 thorpej pcibios_pir_table_nentries = (tablesize - 32) / 16;
310 1.1 thorpej
311 1.1 thorpej printf("PCI Interrupt Router at %03d:%02d:%01d",
312 1.1 thorpej pcibios_pir_header.router_bus,
313 1.4 soda PIR_DEVFUNC_DEVICE(pcibios_pir_header.router_devfunc),
314 1.4 soda PIR_DEVFUNC_FUNCTION(pcibios_pir_header.router_devfunc));
315 1.1 thorpej if (pcibios_pir_header.compat_router != 0) {
316 1.19 kochi devinfo = malloc(256, M_DEVBUF, M_NOWAIT);
317 1.19 kochi if (devinfo) {
318 1.19 kochi pci_devinfo(pcibios_pir_header.compat_router,
319 1.19 kochi 0, 0, devinfo, 256);
320 1.19 kochi printf(" (%s compatible)", devinfo);
321 1.19 kochi free(devinfo, M_DEVBUF);
322 1.19 kochi }
323 1.1 thorpej }
324 1.1 thorpej printf("\n");
325 1.1 thorpej pcibios_print_exclirq();
326 1.18 christos
327 1.18 christos #ifdef PCIBIOS_LIBRETTO_FIXUP
328 1.18 christos /* for Libretto L2/L3 hack */
329 1.18 christos pcibios_fixup_pir_table();
330 1.18 christos #endif
331 1.20 augustss #ifdef PCIBIOS_SHARP_MM20_FIXUP
332 1.20 augustss pcibios_mm20_fixup();
333 1.20 augustss #endif
334 1.1 thorpej #ifdef PCIINTR_DEBUG
335 1.1 thorpej pcibios_print_pir_table();
336 1.1 thorpej #endif
337 1.1 thorpej return;
338 1.1 thorpej }
339 1.1 thorpej
340 1.1 thorpej /*
341 1.1 thorpej * If there was no PIR table found, try using the PCI BIOS
342 1.1 thorpej * Get Interrupt Routing call.
343 1.1 thorpej *
344 1.1 thorpej * XXX The interface to this call sucks; just allocate enough
345 1.1 thorpej * XXX room for 32 entries.
346 1.1 thorpej */
347 1.1 thorpej pcibios_pir_table_nentries = 32;
348 1.1 thorpej pcibios_pir_table = malloc(pcibios_pir_table_nentries *
349 1.1 thorpej sizeof(*pcibios_pir_table), M_DEVBUF, M_NOWAIT);
350 1.1 thorpej if (pcibios_pir_table == NULL) {
351 1.1 thorpej printf("pcibios_pir_init: no memory for $PIR\n");
352 1.1 thorpej return;
353 1.1 thorpej }
354 1.1 thorpej if (pcibios_get_intr_routing(pcibios_pir_table,
355 1.1 thorpej &pcibios_pir_table_nentries,
356 1.1 thorpej &pcibios_pir_header.exclusive_irq) != PCIBIOS_SUCCESS) {
357 1.1 thorpej printf("No PCI IRQ Routing information available.\n");
358 1.1 thorpej free(pcibios_pir_table, M_DEVBUF);
359 1.1 thorpej pcibios_pir_table = NULL;
360 1.1 thorpej pcibios_pir_table_nentries = 0;
361 1.1 thorpej return;
362 1.1 thorpej }
363 1.1 thorpej printf("PCI BIOS has %d Interrupt Routing table entries\n",
364 1.1 thorpej pcibios_pir_table_nentries);
365 1.1 thorpej pcibios_print_exclirq();
366 1.18 christos
367 1.18 christos #ifdef PCIBIOS_LIBRETTO_FIXUP
368 1.18 christos /* for Libretto L2/L3 hack */
369 1.18 christos pcibios_fixup_pir_table();
370 1.18 christos #endif
371 1.20 augustss #ifdef PCIBIOS_SHARP_MM20_FIXUP
372 1.20 augustss pcibios_mm20_fixup();
373 1.20 augustss #endif
374 1.1 thorpej #ifdef PCIINTR_DEBUG
375 1.1 thorpej pcibios_print_pir_table();
376 1.1 thorpej #endif
377 1.1 thorpej }
378 1.1 thorpej
379 1.1 thorpej int
380 1.16 kochi pcibios_get_status(u_int32_t *rev_maj, u_int32_t *rev_min,
381 1.16 kochi u_int32_t *mech1, u_int32_t *mech2, u_int32_t *scmech1, u_int32_t *scmech2,
382 1.16 kochi u_int32_t *maxbus)
383 1.1 thorpej {
384 1.1 thorpej u_int16_t ax, bx, cx;
385 1.1 thorpej u_int32_t edx;
386 1.1 thorpej int rv;
387 1.1 thorpej
388 1.10 fvdl __asm __volatile("lcall *(%%edi) ; \
389 1.1 thorpej jc 1f ; \
390 1.1 thorpej xor %%ah, %%ah ; \
391 1.1 thorpej 1:"
392 1.1 thorpej : "=a" (ax), "=b" (bx), "=c" (cx), "=d" (edx)
393 1.1 thorpej : "0" (0xb101), "D" (&pcibios_entry));
394 1.1 thorpej
395 1.1 thorpej rv = pcibios_return_code(ax, "pcibios_get_status");
396 1.1 thorpej if (rv != PCIBIOS_SUCCESS)
397 1.1 thorpej return (rv);
398 1.1 thorpej
399 1.1 thorpej if (edx != BIOS32_MAKESIG('P', 'C', 'I', ' '))
400 1.1 thorpej return (PCIBIOS_SERVICE_NOT_PRESENT); /* XXX */
401 1.1 thorpej
402 1.1 thorpej /*
403 1.1 thorpej * Fill in the various pieces if info we're looking for.
404 1.1 thorpej */
405 1.1 thorpej *mech1 = ax & 1;
406 1.1 thorpej *mech2 = ax & (1 << 1);
407 1.1 thorpej *scmech1 = ax & (1 << 4);
408 1.1 thorpej *scmech2 = ax & (1 << 5);
409 1.1 thorpej *rev_maj = (bx >> 8) & 0xff;
410 1.1 thorpej *rev_min = bx & 0xff;
411 1.1 thorpej *maxbus = cx & 0xff;
412 1.1 thorpej
413 1.1 thorpej return (PCIBIOS_SUCCESS);
414 1.1 thorpej }
415 1.1 thorpej
416 1.1 thorpej int
417 1.16 kochi pcibios_get_intr_routing(struct pcibios_intr_routing *table,
418 1.16 kochi int *nentries, u_int16_t *exclirq)
419 1.1 thorpej {
420 1.1 thorpej u_int16_t ax, bx;
421 1.1 thorpej int rv;
422 1.1 thorpej struct {
423 1.1 thorpej u_int16_t size;
424 1.1 thorpej caddr_t offset;
425 1.1 thorpej u_int16_t segment;
426 1.1 thorpej } __attribute__((__packed__)) args;
427 1.1 thorpej
428 1.1 thorpej args.size = *nentries * sizeof(*table);
429 1.1 thorpej args.offset = (caddr_t)table;
430 1.1 thorpej args.segment = GSEL(GDATA_SEL, SEL_KPL);
431 1.1 thorpej
432 1.1 thorpej memset(table, 0, args.size);
433 1.1 thorpej
434 1.10 fvdl __asm __volatile("lcall *(%%esi) ; \
435 1.1 thorpej jc 1f ; \
436 1.1 thorpej xor %%ah, %%ah ; \
437 1.1 thorpej 1: movw %w2, %%ds ; \
438 1.1 thorpej movw %w2, %%es"
439 1.1 thorpej : "=a" (ax), "=b" (bx)
440 1.1 thorpej : "r" GSEL(GDATA_SEL, SEL_KPL), "0" (0xb10e), "1" (0),
441 1.1 thorpej "D" (&args), "S" (&pcibios_entry));
442 1.1 thorpej
443 1.1 thorpej rv = pcibios_return_code(ax, "pcibios_get_intr_routing");
444 1.1 thorpej if (rv != PCIBIOS_SUCCESS)
445 1.1 thorpej return (rv);
446 1.1 thorpej
447 1.1 thorpej *nentries = args.size / sizeof(*table);
448 1.1 thorpej *exclirq = bx;
449 1.1 thorpej
450 1.1 thorpej return (PCIBIOS_SUCCESS);
451 1.1 thorpej }
452 1.1 thorpej
453 1.1 thorpej int
454 1.16 kochi pcibios_return_code(u_int16_t ax, const char *func)
455 1.1 thorpej {
456 1.1 thorpej const char *errstr;
457 1.1 thorpej int rv = ax >> 8;
458 1.1 thorpej
459 1.1 thorpej switch (rv) {
460 1.1 thorpej case PCIBIOS_SUCCESS:
461 1.1 thorpej return (PCIBIOS_SUCCESS);
462 1.1 thorpej
463 1.1 thorpej case PCIBIOS_SERVICE_NOT_PRESENT:
464 1.1 thorpej errstr = "service not present";
465 1.1 thorpej break;
466 1.1 thorpej
467 1.1 thorpej case PCIBIOS_FUNCTION_NOT_SUPPORTED:
468 1.1 thorpej errstr = "function not supported";
469 1.1 thorpej break;
470 1.1 thorpej
471 1.1 thorpej case PCIBIOS_BAD_VENDOR_ID:
472 1.1 thorpej errstr = "bad vendor ID";
473 1.1 thorpej break;
474 1.1 thorpej
475 1.1 thorpej case PCIBIOS_DEVICE_NOT_FOUND:
476 1.1 thorpej errstr = "device not found";
477 1.1 thorpej break;
478 1.1 thorpej
479 1.1 thorpej case PCIBIOS_BAD_REGISTER_NUMBER:
480 1.1 thorpej errstr = "bad register number";
481 1.1 thorpej break;
482 1.1 thorpej
483 1.1 thorpej case PCIBIOS_SET_FAILED:
484 1.1 thorpej errstr = "set failed";
485 1.1 thorpej break;
486 1.1 thorpej
487 1.1 thorpej case PCIBIOS_BUFFER_TOO_SMALL:
488 1.1 thorpej errstr = "buffer too small";
489 1.1 thorpej break;
490 1.1 thorpej
491 1.1 thorpej default:
492 1.1 thorpej printf("%s: unknown return code 0x%x\n", func, rv);
493 1.1 thorpej return (rv);
494 1.1 thorpej }
495 1.1 thorpej
496 1.1 thorpej printf("%s: %s\n", func, errstr);
497 1.1 thorpej return (rv);
498 1.1 thorpej }
499 1.1 thorpej
500 1.1 thorpej void
501 1.22 perry pcibios_print_exclirq(void)
502 1.1 thorpej {
503 1.1 thorpej int i;
504 1.1 thorpej
505 1.1 thorpej if (pcibios_pir_header.exclusive_irq) {
506 1.1 thorpej printf("PCI Exclusive IRQs:");
507 1.1 thorpej for (i = 0; i < 16; i++) {
508 1.1 thorpej if (pcibios_pir_header.exclusive_irq & (1 << i))
509 1.1 thorpej printf(" %d", i);
510 1.1 thorpej }
511 1.1 thorpej printf("\n");
512 1.1 thorpej }
513 1.1 thorpej }
514 1.1 thorpej
515 1.18 christos #ifdef PCIBIOS_LIBRETTO_FIXUP
516 1.18 christos /* for Libretto L2/L3 hack */
517 1.18 christos static void
518 1.22 perry pcibios_fixup_pir_table(void)
519 1.18 christos {
520 1.18 christos struct pcibios_linkmap *m;
521 1.18 christos
522 1.18 christos for (m = pir_mask; m->link != 0; m++)
523 1.18 christos pcibios_fixup_pir_table_mask(m);
524 1.18 christos }
525 1.18 christos
526 1.18 christos void
527 1.22 perry pcibios_fixup_pir_table_mask(struct pcibios_linkmap *mask)
528 1.18 christos {
529 1.18 christos int i, j;
530 1.18 christos
531 1.18 christos for (i = 0; i < pcibios_pir_table_nentries; i++) {
532 1.18 christos for (j = 0; j < 4; j++) {
533 1.18 christos if (pcibios_pir_table[i].linkmap[j].link == mask->link) {
534 1.18 christos pcibios_pir_table[i].linkmap[j].bitmap
535 1.18 christos &= mask->bitmap;
536 1.18 christos }
537 1.18 christos }
538 1.18 christos }
539 1.18 christos }
540 1.18 christos #endif
541 1.18 christos
542 1.1 thorpej #ifdef PCIINTR_DEBUG
543 1.1 thorpej void
544 1.22 perry pcibios_print_pir_table(void)
545 1.1 thorpej {
546 1.1 thorpej int i, j;
547 1.1 thorpej
548 1.1 thorpej for (i = 0; i < pcibios_pir_table_nentries; i++) {
549 1.1 thorpej printf("PIR Entry %d:\n", i);
550 1.1 thorpej printf("\tBus: %d Device: %d\n",
551 1.1 thorpej pcibios_pir_table[i].bus,
552 1.4 soda PIR_DEVFUNC_DEVICE(pcibios_pir_table[i].device));
553 1.1 thorpej for (j = 0; j < 4; j++) {
554 1.1 thorpej printf("\t\tINT%c: link 0x%02x bitmap 0x%04x\n",
555 1.1 thorpej 'A' + j,
556 1.1 thorpej pcibios_pir_table[i].linkmap[j].link,
557 1.1 thorpej pcibios_pir_table[i].linkmap[j].bitmap);
558 1.1 thorpej }
559 1.1 thorpej }
560 1.1 thorpej }
561 1.1 thorpej #endif
562 1.3 uch
563 1.6 mcr void
564 1.16 kochi pci_device_foreach(pci_chipset_tag_t pc, int maxbus,
565 1.16 kochi void (*func)(pci_chipset_tag_t, pcitag_t, void *), void *context)
566 1.6 mcr {
567 1.16 kochi pci_device_foreach_min(pc, 0, maxbus, func, context);
568 1.6 mcr }
569 1.6 mcr
570 1.3 uch void
571 1.16 kochi pci_device_foreach_min(pci_chipset_tag_t pc, int minbus, int maxbus,
572 1.16 kochi void (*func)(pci_chipset_tag_t, pcitag_t, void *), void *context)
573 1.3 uch {
574 1.3 uch const struct pci_quirkdata *qd;
575 1.3 uch int bus, device, function, maxdevs, nfuncs;
576 1.3 uch pcireg_t id, bhlcr;
577 1.3 uch pcitag_t tag;
578 1.3 uch
579 1.6 mcr for (bus = minbus; bus <= maxbus; bus++) {
580 1.3 uch maxdevs = pci_bus_maxdevs(pc, bus);
581 1.3 uch for (device = 0; device < maxdevs; device++) {
582 1.3 uch tag = pci_make_tag(pc, bus, device, 0);
583 1.3 uch id = pci_conf_read(pc, tag, PCI_ID_REG);
584 1.3 uch
585 1.3 uch /* Invalid vendor ID value? */
586 1.3 uch if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
587 1.3 uch continue;
588 1.3 uch /* XXX Not invalid, but we've done this ~forever. */
589 1.3 uch if (PCI_VENDOR(id) == 0)
590 1.3 uch continue;
591 1.3 uch
592 1.3 uch qd = pci_lookup_quirkdata(PCI_VENDOR(id),
593 1.3 uch PCI_PRODUCT(id));
594 1.3 uch
595 1.3 uch bhlcr = pci_conf_read(pc, tag, PCI_BHLC_REG);
596 1.3 uch if (PCI_HDRTYPE_MULTIFN(bhlcr) ||
597 1.3 uch (qd != NULL &&
598 1.3 uch (qd->quirks & PCI_QUIRK_MULTIFUNCTION) != 0))
599 1.3 uch nfuncs = 8;
600 1.3 uch else
601 1.3 uch nfuncs = 1;
602 1.3 uch
603 1.3 uch for (function = 0; function < nfuncs; function++) {
604 1.3 uch tag = pci_make_tag(pc, bus, device, function);
605 1.3 uch id = pci_conf_read(pc, tag, PCI_ID_REG);
606 1.3 uch
607 1.3 uch /* Invalid vendor ID value? */
608 1.3 uch if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
609 1.3 uch continue;
610 1.3 uch /*
611 1.3 uch * XXX Not invalid, but we've done this
612 1.3 uch * ~forever.
613 1.3 uch */
614 1.3 uch if (PCI_VENDOR(id) == 0)
615 1.3 uch continue;
616 1.6 mcr (*func)(pc, tag, context);
617 1.3 uch }
618 1.3 uch }
619 1.8 uch }
620 1.8 uch }
621 1.8 uch
622 1.8 uch void
623 1.8 uch pci_bridge_foreach(pci_chipset_tag_t pc, int minbus, int maxbus,
624 1.8 uch void (*func)(pci_chipset_tag_t, pcitag_t, void *), void *ctx)
625 1.8 uch {
626 1.8 uch struct pci_bridge_hook_arg bridge_hook;
627 1.8 uch
628 1.8 uch bridge_hook.func = func;
629 1.8 uch bridge_hook.arg = ctx;
630 1.8 uch
631 1.8 uch pci_device_foreach_min(pc, minbus, maxbus, pci_bridge_hook,
632 1.8 uch &bridge_hook);
633 1.8 uch }
634 1.8 uch
635 1.8 uch void
636 1.8 uch pci_bridge_hook(pci_chipset_tag_t pc, pcitag_t tag, void *ctx)
637 1.8 uch {
638 1.8 uch struct pci_bridge_hook_arg *bridge_hook = (void *)ctx;
639 1.8 uch pcireg_t reg;
640 1.8 uch
641 1.8 uch reg = pci_conf_read(pc, tag, PCI_CLASS_REG);
642 1.8 uch if (PCI_CLASS(reg) == PCI_CLASS_BRIDGE &&
643 1.8 uch (PCI_SUBCLASS(reg) == PCI_SUBCLASS_BRIDGE_PCI ||
644 1.8 uch PCI_SUBCLASS(reg) == PCI_SUBCLASS_BRIDGE_CARDBUS)) {
645 1.8 uch (*bridge_hook->func)(pc, tag, bridge_hook->arg);
646 1.3 uch }
647 1.3 uch }
648 1.20 augustss
649 1.20 augustss #ifdef PCIBIOS_SHARP_MM20_FIXUP
650 1.20 augustss /*
651 1.20 augustss * This is a gross hack to get the interrupt from the EHCI controller
652 1.20 augustss * working on a Sharp MM20. The BIOS is just incredibly buggy.
653 1.20 augustss *
654 1.20 augustss * The story thus far:
655 1.20 augustss * The modern way to route the interrupt is to use ACPI. But using
656 1.20 augustss * ACPI fails with an error message about an uninitialized local
657 1.20 augustss * variable in the AML code. (It works in Windows, but fails in NetBSD
658 1.20 augustss * and Linux.)
659 1.20 augustss *
660 1.20 augustss * The second attempt is to use PCI Interrupt Routing table. But this
661 1.20 augustss * fails because the table does not contain any information about the
662 1.20 augustss * interrupt from the EHCI controller. This is probably due to the fact
663 1.20 augustss * that the table is compatible with ALi M1543, but the MM20 has an ALi M1563.
664 1.20 augustss * The M1563 has additional interrupt lines. The ali1543.c code also
665 1.20 augustss * cannot handle the M1653's extended interrupts. And fixing this is
666 1.20 augustss * difficult since getting a data sheet from ALi requires signing an NDA.
667 1.20 augustss *
668 1.20 augustss * The third attempt is to use a BIOS call to route the interrupt
669 1.20 augustss * (as FreeBSD does) with manually generated information. But the BIOS call
670 1.20 augustss * fails because the BIOS code is not quite position independent. It makes
671 1.20 augustss * some assumption about where the code segment register points.
672 1.20 augustss *
673 1.20 augustss * So the solution is to use the third attempt, but with a patched version
674 1.20 augustss * of the BIOS.
675 1.20 augustss * -- lennart (at) augustsson.net
676 1.20 augustss */
677 1.20 augustss
678 1.20 augustss #define BIOS32_START 0xe0000
679 1.20 augustss #define BIOS32_SIZE 0x20000
680 1.20 augustss
681 1.20 augustss static char pcibios_shadow[BIOS32_SIZE];
682 1.20 augustss static struct bios32_entry pcibios_entry_shadow;
683 1.20 augustss
684 1.20 augustss /*
685 1.20 augustss * Copy BIOS and zap offending instruction.
686 1.20 augustss * The bad instruction is
687 1.20 augustss * mov %cs:0x63c(%ebx),%ah
688 1.20 augustss * NetBSD does not have the code segment set up for this to work.
689 1.20 augustss * Using the value 0xff for the table entry seems to work.
690 1.20 augustss * The replacement is
691 1.20 augustss * mov $0xff,%ah; nop; nop; nop; nop; nop
692 1.20 augustss */
693 1.20 augustss static void
694 1.20 augustss pcibios_copy_bios(void)
695 1.20 augustss {
696 1.20 augustss u_int8_t *bad_instr;
697 1.20 augustss
698 1.20 augustss memcpy(pcibios_shadow, ISA_HOLE_VADDR(BIOS32_START), BIOS32_SIZE);
699 1.20 augustss pcibios_entry_shadow = pcibios_entry;
700 1.20 augustss pcibios_entry_shadow.offset =
701 1.20 augustss (void*)((u_long)pcibios_shadow +
702 1.20 augustss (u_long)pcibios_entry.offset -
703 1.20 augustss (u_long)ISA_HOLE_VADDR(BIOS32_START));
704 1.20 augustss
705 1.20 augustss bad_instr = (u_int8_t *)pcibios_entry_shadow.offset + 0x499;
706 1.20 augustss if (*bad_instr != 0x2e)
707 1.20 augustss panic("bad bios");
708 1.20 augustss bad_instr[0] = 0xb4; bad_instr[1] = 0xff; /* mov $0xff,%ah */
709 1.20 augustss bad_instr[2] = 0x90; /* nop */
710 1.20 augustss bad_instr[3] = 0x90; /* nop */
711 1.20 augustss bad_instr[4] = 0x90; /* nop */
712 1.20 augustss bad_instr[5] = 0x90; /* nop */
713 1.20 augustss bad_instr[6] = 0x90; /* nop */
714 1.20 augustss }
715 1.20 augustss
716 1.20 augustss /*
717 1.20 augustss * Call BIOS to route an interrupt.
718 1.20 augustss * The PCI device is identified by bus,device,func.
719 1.20 augustss * The interrupt is on pin PIN (A-D) and interrupt IRQ.
720 1.20 augustss * BIOS knows the magic for the interrupt controller.
721 1.20 augustss */
722 1.20 augustss static int
723 1.20 augustss pcibios_biosroute(int bus, int device, int func, int pin, int irq)
724 1.20 augustss {
725 1.20 augustss u_int16_t ax, bx, cx;
726 1.20 augustss int rv;
727 1.20 augustss
728 1.20 augustss printf("pcibios_biosroute: b,d,f=%d,%d,%d pin=%x irq=%d\n",
729 1.20 augustss bus, device, func, pin+0xa, irq);
730 1.20 augustss
731 1.20 augustss bx = (bus << 8) | (device << 3) | func;
732 1.20 augustss cx = (irq << 8) | (0xa + pin);
733 1.20 augustss
734 1.20 augustss __asm __volatile("lcall *(%%esi) ; \
735 1.20 augustss jc 1f ; \
736 1.20 augustss xor %%ah, %%ah ; \
737 1.20 augustss 1: movw %w1, %%ds ; \
738 1.20 augustss movw %w1, %%es"
739 1.20 augustss : "=a" (ax)
740 1.20 augustss : "r" GSEL(GDATA_SEL, SEL_KPL), "0" (0xb10f),
741 1.20 augustss "b" (bx), "c" (cx),
742 1.20 augustss "S" (&pcibios_entry_shadow));
743 1.20 augustss
744 1.20 augustss rv = pcibios_return_code(ax, "pcibios_biosroute");
745 1.20 augustss
746 1.20 augustss return rv;
747 1.20 augustss }
748 1.20 augustss
749 1.21 augustss #define MM20_PCI_BUS 0
750 1.21 augustss #define MM20_PCI_EHCI_DEV 15
751 1.21 augustss #define MM20_PCI_EHCI_FUNC 3
752 1.21 augustss #define MM20_PCI_EHCI_PIN 3
753 1.21 augustss #define MM20_PCI_EHCI_INTR 11
754 1.21 augustss #define MM20_PCI_ISA_DEV 3
755 1.21 augustss #define MM20_PCI_ISA_FUNC 0
756 1.21 augustss
757 1.20 augustss static void
758 1.20 augustss pcibios_mm20_fixup(void)
759 1.20 augustss {
760 1.21 augustss pci_chipset_tag_t pc;
761 1.21 augustss pcitag_t tag;
762 1.21 augustss
763 1.20 augustss /* Copy BIOS */
764 1.20 augustss pcibios_copy_bios();
765 1.20 augustss /* Route the interrupt for the EHCI controller. */
766 1.21 augustss (void)pcibios_biosroute(MM20_PCI_BUS,
767 1.21 augustss MM20_PCI_EHCI_DEV,
768 1.21 augustss MM20_PCI_EHCI_FUNC,
769 1.21 augustss MM20_PCI_EHCI_PIN,
770 1.21 augustss MM20_PCI_EHCI_INTR);
771 1.21 augustss
772 1.21 augustss /* Fake some tags. */
773 1.21 augustss pc = NULL;
774 1.21 augustss tag = pci_make_tag(pc, MM20_PCI_BUS, MM20_PCI_EHCI_DEV,
775 1.21 augustss MM20_PCI_EHCI_FUNC);
776 1.21 augustss /* Set interrupt register in EHCI controller */
777 1.21 augustss pci_conf_write(pc, tag, 0x3c, 0x50000400 + MM20_PCI_EHCI_INTR);
778 1.21 augustss tag = pci_make_tag(pc, MM20_PCI_BUS, MM20_PCI_ISA_DEV,
779 1.21 augustss MM20_PCI_ISA_FUNC);
780 1.21 augustss /* Set some unknown registers in the ISA bridge. */
781 1.21 augustss pci_conf_write(pc, tag, 0x58, 0xd87f5300);
782 1.21 augustss pci_conf_write(pc, tag, 0x74, 0x00000009);
783 1.20 augustss }
784 1.20 augustss
785 1.20 augustss #endif /* PCIBIOS_SHARP_MM20_FIXUP */
786