pci_machdep.c revision 1.12 1 1.12 christos /* $NetBSD: pci_machdep.c,v 1.12 2005/11/16 16:08:36 christos Exp $ */
2 1.1 fvdl
3 1.1 fvdl /*-
4 1.1 fvdl * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
5 1.1 fvdl * All rights reserved.
6 1.1 fvdl *
7 1.1 fvdl * This code is derived from software contributed to The NetBSD Foundation
8 1.1 fvdl * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.1 fvdl * NASA Ames Research Center.
10 1.1 fvdl *
11 1.1 fvdl * Redistribution and use in source and binary forms, with or without
12 1.1 fvdl * modification, are permitted provided that the following conditions
13 1.1 fvdl * are met:
14 1.1 fvdl * 1. Redistributions of source code must retain the above copyright
15 1.1 fvdl * notice, this list of conditions and the following disclaimer.
16 1.1 fvdl * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 fvdl * notice, this list of conditions and the following disclaimer in the
18 1.1 fvdl * documentation and/or other materials provided with the distribution.
19 1.1 fvdl * 3. All advertising materials mentioning features or use of this software
20 1.1 fvdl * must display the following acknowledgement:
21 1.1 fvdl * This product includes software developed by the NetBSD
22 1.1 fvdl * Foundation, Inc. and its contributors.
23 1.1 fvdl * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.1 fvdl * contributors may be used to endorse or promote products derived
25 1.1 fvdl * from this software without specific prior written permission.
26 1.1 fvdl *
27 1.1 fvdl * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.1 fvdl * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.1 fvdl * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.1 fvdl * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.1 fvdl * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.1 fvdl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.1 fvdl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.1 fvdl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.1 fvdl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.1 fvdl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.1 fvdl * POSSIBILITY OF SUCH DAMAGE.
38 1.1 fvdl */
39 1.1 fvdl
40 1.1 fvdl /*
41 1.1 fvdl * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved.
42 1.1 fvdl * Copyright (c) 1994 Charles M. Hannum. All rights reserved.
43 1.1 fvdl *
44 1.1 fvdl * Redistribution and use in source and binary forms, with or without
45 1.1 fvdl * modification, are permitted provided that the following conditions
46 1.1 fvdl * are met:
47 1.1 fvdl * 1. Redistributions of source code must retain the above copyright
48 1.1 fvdl * notice, this list of conditions and the following disclaimer.
49 1.1 fvdl * 2. Redistributions in binary form must reproduce the above copyright
50 1.1 fvdl * notice, this list of conditions and the following disclaimer in the
51 1.1 fvdl * documentation and/or other materials provided with the distribution.
52 1.1 fvdl * 3. All advertising materials mentioning features or use of this software
53 1.1 fvdl * must display the following acknowledgement:
54 1.1 fvdl * This product includes software developed by Charles M. Hannum.
55 1.1 fvdl * 4. The name of the author may not be used to endorse or promote products
56 1.1 fvdl * derived from this software without specific prior written permission.
57 1.1 fvdl *
58 1.1 fvdl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
59 1.1 fvdl * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
60 1.1 fvdl * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
61 1.1 fvdl * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
62 1.1 fvdl * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
63 1.1 fvdl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
64 1.1 fvdl * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
65 1.1 fvdl * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
66 1.1 fvdl * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
67 1.1 fvdl * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68 1.1 fvdl */
69 1.1 fvdl
70 1.1 fvdl /*
71 1.1 fvdl * Machine-specific functions for PCI autoconfiguration.
72 1.1 fvdl *
73 1.1 fvdl * On PCs, there are two methods of generating PCI configuration cycles.
74 1.1 fvdl * We try to detect the appropriate mechanism for this machine and set
75 1.1 fvdl * up a few function pointers to access the correct method directly.
76 1.1 fvdl *
77 1.1 fvdl * The configuration method can be hard-coded in the config file by
78 1.1 fvdl * using `options PCI_CONF_MODE=N', where `N' is the configuration mode
79 1.1 fvdl * as defined section 3.6.4.1, `Generating Configuration Cycles'.
80 1.1 fvdl */
81 1.1 fvdl
82 1.1 fvdl #include <sys/cdefs.h>
83 1.12 christos __KERNEL_RCSID(0, "$NetBSD: pci_machdep.c,v 1.12 2005/11/16 16:08:36 christos Exp $");
84 1.1 fvdl
85 1.1 fvdl #include <sys/types.h>
86 1.1 fvdl #include <sys/param.h>
87 1.1 fvdl #include <sys/time.h>
88 1.1 fvdl #include <sys/systm.h>
89 1.1 fvdl #include <sys/errno.h>
90 1.1 fvdl #include <sys/device.h>
91 1.1 fvdl #include <sys/lock.h>
92 1.1 fvdl
93 1.1 fvdl #include <uvm/uvm_extern.h>
94 1.1 fvdl
95 1.1 fvdl #include <machine/bus.h>
96 1.10 yamt #include <machine/bus_private.h>
97 1.1 fvdl
98 1.1 fvdl #include <machine/pio.h>
99 1.1 fvdl #include <machine/intr.h>
100 1.1 fvdl
101 1.3 fvdl #include <dev/isa/isareg.h>
102 1.1 fvdl #include <dev/isa/isavar.h>
103 1.1 fvdl #include <dev/pci/pcivar.h>
104 1.1 fvdl #include <dev/pci/pcireg.h>
105 1.1 fvdl #include <dev/pci/pcidevs.h>
106 1.1 fvdl
107 1.1 fvdl #include "ioapic.h"
108 1.2 fvdl #include "eisa.h"
109 1.4 fvdl #include "opt_mpbios.h"
110 1.4 fvdl #include "opt_mpacpi.h"
111 1.1 fvdl
112 1.1 fvdl #if NIOAPIC > 0
113 1.1 fvdl #include <machine/i82093var.h>
114 1.1 fvdl #include <machine/mpbiosvar.h>
115 1.6 fvdl #include <machine/pic.h>
116 1.1 fvdl #endif
117 1.1 fvdl
118 1.4 fvdl #ifdef MPBIOS
119 1.4 fvdl #include <machine/mpbiosvar.h>
120 1.4 fvdl #endif
121 1.4 fvdl
122 1.4 fvdl #ifdef MPACPI
123 1.4 fvdl #include <machine/mpacpi.h>
124 1.4 fvdl #endif
125 1.4 fvdl
126 1.1 fvdl #include "opt_pci_conf_mode.h"
127 1.1 fvdl
128 1.1 fvdl int pci_mode = -1;
129 1.1 fvdl
130 1.11 sekiya static void pci_bridge_hook(pci_chipset_tag_t, pcitag_t, void *);
131 1.11 sekiya struct pci_bridge_hook_arg {
132 1.11 sekiya void (*func)(pci_chipset_tag_t, pcitag_t, void *);
133 1.11 sekiya void *arg;
134 1.11 sekiya };
135 1.11 sekiya
136 1.11 sekiya
137 1.1 fvdl struct simplelock pci_conf_slock = SIMPLELOCK_INITIALIZER;
138 1.1 fvdl
139 1.1 fvdl #define PCI_CONF_LOCK(s) \
140 1.1 fvdl do { \
141 1.1 fvdl (s) = splhigh(); \
142 1.1 fvdl simple_lock(&pci_conf_slock); \
143 1.1 fvdl } while (0)
144 1.1 fvdl
145 1.1 fvdl #define PCI_CONF_UNLOCK(s) \
146 1.1 fvdl do { \
147 1.1 fvdl simple_unlock(&pci_conf_slock); \
148 1.1 fvdl splx((s)); \
149 1.1 fvdl } while (0)
150 1.1 fvdl
151 1.1 fvdl #define PCI_MODE1_ENABLE 0x80000000UL
152 1.1 fvdl #define PCI_MODE1_ADDRESS_REG 0x0cf8
153 1.1 fvdl #define PCI_MODE1_DATA_REG 0x0cfc
154 1.1 fvdl
155 1.1 fvdl #define PCI_MODE2_ENABLE_REG 0x0cf8
156 1.1 fvdl #define PCI_MODE2_FORWARD_REG 0x0cfa
157 1.1 fvdl
158 1.1 fvdl #define _m1tag(b, d, f) \
159 1.1 fvdl (PCI_MODE1_ENABLE | ((b) << 16) | ((d) << 11) | ((f) << 8))
160 1.1 fvdl #define _qe(bus, dev, fcn, vend, prod) \
161 1.1 fvdl {_m1tag(bus, dev, fcn), PCI_ID_CODE(vend, prod)}
162 1.1 fvdl struct {
163 1.1 fvdl u_int32_t tag;
164 1.1 fvdl pcireg_t id;
165 1.1 fvdl } pcim1_quirk_tbl[] = {
166 1.1 fvdl _qe(0, 0, 0, PCI_VENDOR_COMPAQ, PCI_PRODUCT_COMPAQ_TRIFLEX1),
167 1.1 fvdl /* XXX Triflex2 not tested */
168 1.1 fvdl _qe(0, 0, 0, PCI_VENDOR_COMPAQ, PCI_PRODUCT_COMPAQ_TRIFLEX2),
169 1.1 fvdl _qe(0, 0, 0, PCI_VENDOR_COMPAQ, PCI_PRODUCT_COMPAQ_TRIFLEX4),
170 1.1 fvdl /* Triton needed for Connectix Virtual PC */
171 1.1 fvdl _qe(0, 0, 0, PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82437FX),
172 1.1 fvdl /* Connectix Virtual PC 5 has a 440BX */
173 1.1 fvdl _qe(0, 0, 0, PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82443BX_NOAGP),
174 1.12 christos /* SIS 741 */
175 1.12 christos _qe(0, 0, 0, PCI_VENDOR_SIS, PCI_PRODUCT_SIS_741),
176 1.1 fvdl {0, 0xffffffff} /* patchable */
177 1.1 fvdl };
178 1.1 fvdl #undef _m1tag
179 1.1 fvdl #undef _id
180 1.1 fvdl #undef _qe
181 1.1 fvdl
182 1.1 fvdl /*
183 1.1 fvdl * PCI doesn't have any special needs; just use the generic versions
184 1.1 fvdl * of these functions.
185 1.1 fvdl */
186 1.1 fvdl struct x86_bus_dma_tag pci_bus_dma_tag = {
187 1.3 fvdl #if defined(_LP64) || defined(PAE)
188 1.3 fvdl PCI32_DMA_BOUNCE_THRESHOLD, /* bounce_thresh */
189 1.3 fvdl ISA_DMA_BOUNCE_THRESHOLD, /* bounce_alloclo */
190 1.3 fvdl PCI32_DMA_BOUNCE_THRESHOLD, /* bounce_allochi */
191 1.3 fvdl #else
192 1.3 fvdl 0,
193 1.3 fvdl 0,
194 1.3 fvdl 0,
195 1.3 fvdl #endif
196 1.3 fvdl NULL, /* _may_bounce */
197 1.1 fvdl _bus_dmamap_create,
198 1.1 fvdl _bus_dmamap_destroy,
199 1.1 fvdl _bus_dmamap_load,
200 1.1 fvdl _bus_dmamap_load_mbuf,
201 1.1 fvdl _bus_dmamap_load_uio,
202 1.1 fvdl _bus_dmamap_load_raw,
203 1.1 fvdl _bus_dmamap_unload,
204 1.3 fvdl #if defined(_LP64) || defined(PAE)
205 1.3 fvdl _bus_dmamap_sync,
206 1.3 fvdl #else
207 1.3 fvdl NULL,
208 1.3 fvdl #endif
209 1.1 fvdl _bus_dmamem_alloc,
210 1.1 fvdl _bus_dmamem_free,
211 1.1 fvdl _bus_dmamem_map,
212 1.1 fvdl _bus_dmamem_unmap,
213 1.1 fvdl _bus_dmamem_mmap,
214 1.1 fvdl };
215 1.5 fvdl
216 1.5 fvdl #ifdef _LP64
217 1.5 fvdl struct x86_bus_dma_tag pci_bus_dma64_tag = {
218 1.5 fvdl 0,
219 1.5 fvdl 0,
220 1.5 fvdl 0,
221 1.5 fvdl NULL, /* _may_bounce */
222 1.5 fvdl _bus_dmamap_create,
223 1.5 fvdl _bus_dmamap_destroy,
224 1.5 fvdl _bus_dmamap_load,
225 1.5 fvdl _bus_dmamap_load_mbuf,
226 1.5 fvdl _bus_dmamap_load_uio,
227 1.5 fvdl _bus_dmamap_load_raw,
228 1.5 fvdl _bus_dmamap_unload,
229 1.5 fvdl NULL,
230 1.5 fvdl _bus_dmamem_alloc,
231 1.5 fvdl _bus_dmamem_free,
232 1.5 fvdl _bus_dmamem_map,
233 1.5 fvdl _bus_dmamem_unmap,
234 1.5 fvdl _bus_dmamem_mmap,
235 1.5 fvdl };
236 1.5 fvdl #endif
237 1.1 fvdl
238 1.1 fvdl void
239 1.1 fvdl pci_attach_hook(parent, self, pba)
240 1.1 fvdl struct device *parent, *self;
241 1.1 fvdl struct pcibus_attach_args *pba;
242 1.1 fvdl {
243 1.1 fvdl
244 1.1 fvdl if (pba->pba_bus == 0)
245 1.1 fvdl printf(": configuration mode %d", pci_mode);
246 1.4 fvdl #ifdef MPBIOS
247 1.4 fvdl mpbios_pci_attach_hook(parent, self, pba);
248 1.4 fvdl #endif
249 1.4 fvdl #ifdef MPACPI
250 1.4 fvdl mpacpi_pci_attach_hook(parent, self, pba);
251 1.4 fvdl #endif
252 1.1 fvdl }
253 1.1 fvdl
254 1.1 fvdl int
255 1.1 fvdl pci_bus_maxdevs(pc, busno)
256 1.1 fvdl pci_chipset_tag_t pc;
257 1.1 fvdl int busno;
258 1.1 fvdl {
259 1.1 fvdl
260 1.1 fvdl /*
261 1.1 fvdl * Bus number is irrelevant. If Configuration Mechanism 2 is in
262 1.1 fvdl * use, can only have devices 0-15 on any bus. If Configuration
263 1.1 fvdl * Mechanism 1 is in use, can have devices 0-32 (i.e. the `normal'
264 1.1 fvdl * range).
265 1.1 fvdl */
266 1.1 fvdl if (pci_mode == 2)
267 1.1 fvdl return (16);
268 1.1 fvdl else
269 1.1 fvdl return (32);
270 1.1 fvdl }
271 1.1 fvdl
272 1.1 fvdl pcitag_t
273 1.1 fvdl pci_make_tag(pc, bus, device, function)
274 1.1 fvdl pci_chipset_tag_t pc;
275 1.1 fvdl int bus, device, function;
276 1.1 fvdl {
277 1.1 fvdl pcitag_t tag;
278 1.1 fvdl
279 1.1 fvdl #ifndef PCI_CONF_MODE
280 1.1 fvdl switch (pci_mode) {
281 1.1 fvdl case 1:
282 1.1 fvdl goto mode1;
283 1.1 fvdl case 2:
284 1.1 fvdl goto mode2;
285 1.1 fvdl default:
286 1.1 fvdl panic("pci_make_tag: mode not configured");
287 1.1 fvdl }
288 1.1 fvdl #endif
289 1.1 fvdl
290 1.1 fvdl #if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 1)
291 1.1 fvdl #ifndef PCI_CONF_MODE
292 1.1 fvdl mode1:
293 1.1 fvdl #endif
294 1.1 fvdl if (bus >= 256 || device >= 32 || function >= 8)
295 1.1 fvdl panic("pci_make_tag: bad request");
296 1.1 fvdl
297 1.1 fvdl tag.mode1 = PCI_MODE1_ENABLE |
298 1.1 fvdl (bus << 16) | (device << 11) | (function << 8);
299 1.1 fvdl return tag;
300 1.1 fvdl #endif
301 1.1 fvdl
302 1.1 fvdl #if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 2)
303 1.1 fvdl #ifndef PCI_CONF_MODE
304 1.1 fvdl mode2:
305 1.1 fvdl #endif
306 1.1 fvdl if (bus >= 256 || device >= 16 || function >= 8)
307 1.1 fvdl panic("pci_make_tag: bad request");
308 1.1 fvdl
309 1.1 fvdl tag.mode2.port = 0xc000 | (device << 8);
310 1.1 fvdl tag.mode2.enable = 0xf0 | (function << 1);
311 1.1 fvdl tag.mode2.forward = bus;
312 1.1 fvdl return tag;
313 1.1 fvdl #endif
314 1.1 fvdl }
315 1.1 fvdl
316 1.1 fvdl void
317 1.1 fvdl pci_decompose_tag(pc, tag, bp, dp, fp)
318 1.1 fvdl pci_chipset_tag_t pc;
319 1.1 fvdl pcitag_t tag;
320 1.1 fvdl int *bp, *dp, *fp;
321 1.1 fvdl {
322 1.1 fvdl
323 1.1 fvdl #ifndef PCI_CONF_MODE
324 1.1 fvdl switch (pci_mode) {
325 1.1 fvdl case 1:
326 1.1 fvdl goto mode1;
327 1.1 fvdl case 2:
328 1.1 fvdl goto mode2;
329 1.1 fvdl default:
330 1.1 fvdl panic("pci_decompose_tag: mode not configured");
331 1.1 fvdl }
332 1.1 fvdl #endif
333 1.1 fvdl
334 1.1 fvdl #if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 1)
335 1.1 fvdl #ifndef PCI_CONF_MODE
336 1.1 fvdl mode1:
337 1.1 fvdl #endif
338 1.1 fvdl if (bp != NULL)
339 1.1 fvdl *bp = (tag.mode1 >> 16) & 0xff;
340 1.1 fvdl if (dp != NULL)
341 1.1 fvdl *dp = (tag.mode1 >> 11) & 0x1f;
342 1.1 fvdl if (fp != NULL)
343 1.1 fvdl *fp = (tag.mode1 >> 8) & 0x7;
344 1.1 fvdl return;
345 1.1 fvdl #endif
346 1.1 fvdl
347 1.1 fvdl #if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 2)
348 1.1 fvdl #ifndef PCI_CONF_MODE
349 1.1 fvdl mode2:
350 1.1 fvdl #endif
351 1.1 fvdl if (bp != NULL)
352 1.1 fvdl *bp = tag.mode2.forward & 0xff;
353 1.1 fvdl if (dp != NULL)
354 1.1 fvdl *dp = (tag.mode2.port >> 8) & 0xf;
355 1.1 fvdl if (fp != NULL)
356 1.1 fvdl *fp = (tag.mode2.enable >> 1) & 0x7;
357 1.1 fvdl #endif
358 1.1 fvdl }
359 1.1 fvdl
360 1.1 fvdl pcireg_t
361 1.1 fvdl pci_conf_read(pc, tag, reg)
362 1.1 fvdl pci_chipset_tag_t pc;
363 1.1 fvdl pcitag_t tag;
364 1.1 fvdl int reg;
365 1.1 fvdl {
366 1.1 fvdl pcireg_t data;
367 1.1 fvdl int s;
368 1.1 fvdl
369 1.1 fvdl #ifndef PCI_CONF_MODE
370 1.1 fvdl switch (pci_mode) {
371 1.1 fvdl case 1:
372 1.1 fvdl goto mode1;
373 1.1 fvdl case 2:
374 1.1 fvdl goto mode2;
375 1.1 fvdl default:
376 1.1 fvdl panic("pci_conf_read: mode not configured");
377 1.1 fvdl }
378 1.1 fvdl #endif
379 1.1 fvdl
380 1.1 fvdl #if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 1)
381 1.1 fvdl #ifndef PCI_CONF_MODE
382 1.1 fvdl mode1:
383 1.1 fvdl #endif
384 1.1 fvdl PCI_CONF_LOCK(s);
385 1.1 fvdl outl(PCI_MODE1_ADDRESS_REG, tag.mode1 | reg);
386 1.1 fvdl data = inl(PCI_MODE1_DATA_REG);
387 1.1 fvdl outl(PCI_MODE1_ADDRESS_REG, 0);
388 1.1 fvdl PCI_CONF_UNLOCK(s);
389 1.1 fvdl return data;
390 1.1 fvdl #endif
391 1.1 fvdl
392 1.1 fvdl #if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 2)
393 1.1 fvdl #ifndef PCI_CONF_MODE
394 1.1 fvdl mode2:
395 1.1 fvdl #endif
396 1.1 fvdl PCI_CONF_LOCK(s);
397 1.1 fvdl outb(PCI_MODE2_ENABLE_REG, tag.mode2.enable);
398 1.1 fvdl outb(PCI_MODE2_FORWARD_REG, tag.mode2.forward);
399 1.1 fvdl data = inl(tag.mode2.port | reg);
400 1.1 fvdl outb(PCI_MODE2_ENABLE_REG, 0);
401 1.1 fvdl PCI_CONF_UNLOCK(s);
402 1.1 fvdl return data;
403 1.1 fvdl #endif
404 1.1 fvdl }
405 1.1 fvdl
406 1.1 fvdl void
407 1.1 fvdl pci_conf_write(pc, tag, reg, data)
408 1.1 fvdl pci_chipset_tag_t pc;
409 1.1 fvdl pcitag_t tag;
410 1.1 fvdl int reg;
411 1.1 fvdl pcireg_t data;
412 1.1 fvdl {
413 1.1 fvdl int s;
414 1.1 fvdl
415 1.1 fvdl #ifndef PCI_CONF_MODE
416 1.1 fvdl switch (pci_mode) {
417 1.1 fvdl case 1:
418 1.1 fvdl goto mode1;
419 1.1 fvdl case 2:
420 1.1 fvdl goto mode2;
421 1.1 fvdl default:
422 1.1 fvdl panic("pci_conf_write: mode not configured");
423 1.1 fvdl }
424 1.1 fvdl #endif
425 1.1 fvdl
426 1.1 fvdl #if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 1)
427 1.1 fvdl #ifndef PCI_CONF_MODE
428 1.1 fvdl mode1:
429 1.1 fvdl #endif
430 1.1 fvdl PCI_CONF_LOCK(s);
431 1.1 fvdl outl(PCI_MODE1_ADDRESS_REG, tag.mode1 | reg);
432 1.1 fvdl outl(PCI_MODE1_DATA_REG, data);
433 1.1 fvdl outl(PCI_MODE1_ADDRESS_REG, 0);
434 1.1 fvdl PCI_CONF_UNLOCK(s);
435 1.1 fvdl return;
436 1.1 fvdl #endif
437 1.1 fvdl
438 1.1 fvdl #if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 2)
439 1.1 fvdl #ifndef PCI_CONF_MODE
440 1.1 fvdl mode2:
441 1.1 fvdl #endif
442 1.1 fvdl PCI_CONF_LOCK(s);
443 1.1 fvdl outb(PCI_MODE2_ENABLE_REG, tag.mode2.enable);
444 1.1 fvdl outb(PCI_MODE2_FORWARD_REG, tag.mode2.forward);
445 1.1 fvdl outl(tag.mode2.port | reg, data);
446 1.1 fvdl outb(PCI_MODE2_ENABLE_REG, 0);
447 1.1 fvdl PCI_CONF_UNLOCK(s);
448 1.1 fvdl #endif
449 1.1 fvdl }
450 1.1 fvdl
451 1.1 fvdl int
452 1.1 fvdl pci_mode_detect()
453 1.1 fvdl {
454 1.1 fvdl
455 1.1 fvdl #ifdef PCI_CONF_MODE
456 1.1 fvdl #if (PCI_CONF_MODE == 1) || (PCI_CONF_MODE == 2)
457 1.1 fvdl return (pci_mode = PCI_CONF_MODE);
458 1.1 fvdl #else
459 1.1 fvdl #error Invalid PCI configuration mode.
460 1.1 fvdl #endif
461 1.1 fvdl #else
462 1.1 fvdl u_int32_t sav, val;
463 1.1 fvdl int i;
464 1.1 fvdl pcireg_t idreg;
465 1.1 fvdl
466 1.1 fvdl if (pci_mode != -1)
467 1.1 fvdl return pci_mode;
468 1.1 fvdl
469 1.1 fvdl /*
470 1.1 fvdl * We try to divine which configuration mode the host bridge wants.
471 1.1 fvdl */
472 1.1 fvdl
473 1.1 fvdl sav = inl(PCI_MODE1_ADDRESS_REG);
474 1.1 fvdl
475 1.1 fvdl pci_mode = 1; /* assume this for now */
476 1.1 fvdl /*
477 1.1 fvdl * catch some known buggy implementations of mode 1
478 1.1 fvdl */
479 1.1 fvdl for (i = 0; i < sizeof(pcim1_quirk_tbl) / sizeof(pcim1_quirk_tbl[0]);
480 1.1 fvdl i++) {
481 1.1 fvdl pcitag_t t;
482 1.1 fvdl
483 1.1 fvdl if (!pcim1_quirk_tbl[i].tag)
484 1.1 fvdl break;
485 1.1 fvdl t.mode1 = pcim1_quirk_tbl[i].tag;
486 1.1 fvdl idreg = pci_conf_read(0, t, PCI_ID_REG); /* needs "pci_mode" */
487 1.1 fvdl if (idreg == pcim1_quirk_tbl[i].id) {
488 1.1 fvdl #ifdef DEBUG
489 1.1 fvdl printf("known mode 1 PCI chipset (%08x)\n",
490 1.1 fvdl idreg);
491 1.1 fvdl #endif
492 1.1 fvdl return (pci_mode);
493 1.1 fvdl }
494 1.1 fvdl }
495 1.1 fvdl
496 1.1 fvdl /*
497 1.1 fvdl * Strong check for standard compliant mode 1:
498 1.1 fvdl * 1. bit 31 ("enable") can be set
499 1.1 fvdl * 2. byte/word access does not affect register
500 1.1 fvdl */
501 1.1 fvdl outl(PCI_MODE1_ADDRESS_REG, PCI_MODE1_ENABLE);
502 1.1 fvdl outb(PCI_MODE1_ADDRESS_REG + 3, 0);
503 1.1 fvdl outw(PCI_MODE1_ADDRESS_REG + 2, 0);
504 1.1 fvdl val = inl(PCI_MODE1_ADDRESS_REG);
505 1.1 fvdl if ((val & 0x80fffffc) != PCI_MODE1_ENABLE) {
506 1.1 fvdl #ifdef DEBUG
507 1.1 fvdl printf("pci_mode_detect: mode 1 enable failed (%x)\n",
508 1.1 fvdl val);
509 1.1 fvdl #endif
510 1.1 fvdl goto not1;
511 1.1 fvdl }
512 1.1 fvdl outl(PCI_MODE1_ADDRESS_REG, 0);
513 1.1 fvdl val = inl(PCI_MODE1_ADDRESS_REG);
514 1.1 fvdl if ((val & 0x80fffffc) != 0)
515 1.1 fvdl goto not1;
516 1.1 fvdl return (pci_mode);
517 1.1 fvdl not1:
518 1.1 fvdl outl(PCI_MODE1_ADDRESS_REG, sav);
519 1.1 fvdl
520 1.1 fvdl /*
521 1.1 fvdl * This mode 2 check is quite weak (and known to give false
522 1.1 fvdl * positives on some Compaq machines).
523 1.1 fvdl * However, this doesn't matter, because this is the
524 1.1 fvdl * last test, and simply no PCI devices will be found if
525 1.1 fvdl * this happens.
526 1.1 fvdl */
527 1.1 fvdl outb(PCI_MODE2_ENABLE_REG, 0);
528 1.1 fvdl outb(PCI_MODE2_FORWARD_REG, 0);
529 1.1 fvdl if (inb(PCI_MODE2_ENABLE_REG) != 0 ||
530 1.1 fvdl inb(PCI_MODE2_FORWARD_REG) != 0)
531 1.1 fvdl goto not2;
532 1.1 fvdl return (pci_mode = 2);
533 1.1 fvdl not2:
534 1.1 fvdl
535 1.1 fvdl return (pci_mode = 0);
536 1.1 fvdl #endif
537 1.1 fvdl }
538 1.1 fvdl
539 1.1 fvdl int
540 1.1 fvdl pci_intr_map(pa, ihp)
541 1.1 fvdl struct pci_attach_args *pa;
542 1.1 fvdl pci_intr_handle_t *ihp;
543 1.1 fvdl {
544 1.1 fvdl int pin = pa->pa_intrpin;
545 1.1 fvdl int line = pa->pa_intrline;
546 1.1 fvdl #if NIOAPIC > 0
547 1.1 fvdl int rawpin = pa->pa_rawintrpin;
548 1.1 fvdl pci_chipset_tag_t pc = pa->pa_pc;
549 1.1 fvdl int bus, dev, func;
550 1.1 fvdl #endif
551 1.1 fvdl
552 1.1 fvdl if (pin == 0) {
553 1.1 fvdl /* No IRQ used. */
554 1.1 fvdl goto bad;
555 1.1 fvdl }
556 1.1 fvdl
557 1.1 fvdl if (pin > PCI_INTERRUPT_PIN_MAX) {
558 1.1 fvdl printf("pci_intr_map: bad interrupt pin %d\n", pin);
559 1.1 fvdl goto bad;
560 1.1 fvdl }
561 1.1 fvdl
562 1.1 fvdl #if NIOAPIC > 0
563 1.1 fvdl pci_decompose_tag(pc, pa->pa_tag, &bus, &dev, &func);
564 1.1 fvdl if (mp_busses != NULL) {
565 1.9 fvdl if (intr_find_mpmapping(bus, (dev<<2)|(rawpin-1), ihp) == 0) {
566 1.1 fvdl *ihp |= line;
567 1.1 fvdl return 0;
568 1.1 fvdl }
569 1.1 fvdl /*
570 1.1 fvdl * No explicit PCI mapping found. This is not fatal,
571 1.1 fvdl * we'll try the ISA (or possibly EISA) mappings next.
572 1.1 fvdl */
573 1.1 fvdl }
574 1.1 fvdl #endif
575 1.1 fvdl
576 1.1 fvdl /*
577 1.1 fvdl * Section 6.2.4, `Miscellaneous Functions', says that 255 means
578 1.1 fvdl * `unknown' or `no connection' on a PC. We assume that a device with
579 1.1 fvdl * `no connection' either doesn't have an interrupt (in which case the
580 1.1 fvdl * pin number should be 0, and would have been noticed above), or
581 1.1 fvdl * wasn't configured by the BIOS (in which case we punt, since there's
582 1.1 fvdl * no real way we can know how the interrupt lines are mapped in the
583 1.1 fvdl * hardware).
584 1.1 fvdl *
585 1.1 fvdl * XXX
586 1.1 fvdl * Since IRQ 0 is only used by the clock, and we can't actually be sure
587 1.1 fvdl * that the BIOS did its job, we also recognize that as meaning that
588 1.1 fvdl * the BIOS has not configured the device.
589 1.1 fvdl */
590 1.1 fvdl if (line == 0 || line == X86_PCI_INTERRUPT_LINE_NO_CONNECTION) {
591 1.1 fvdl printf("pci_intr_map: no mapping for pin %c (line=%02x)\n",
592 1.1 fvdl '@' + pin, line);
593 1.1 fvdl goto bad;
594 1.1 fvdl } else {
595 1.1 fvdl if (line >= NUM_LEGACY_IRQS) {
596 1.1 fvdl printf("pci_intr_map: bad interrupt line %d\n", line);
597 1.1 fvdl goto bad;
598 1.1 fvdl }
599 1.1 fvdl if (line == 2) {
600 1.1 fvdl printf("pci_intr_map: changed line 2 to line 9\n");
601 1.1 fvdl line = 9;
602 1.1 fvdl }
603 1.1 fvdl }
604 1.1 fvdl #if NIOAPIC > 0
605 1.1 fvdl if (mp_busses != NULL) {
606 1.9 fvdl if (intr_find_mpmapping(mp_isa_bus, line, ihp) == 0) {
607 1.1 fvdl *ihp |= line;
608 1.1 fvdl return 0;
609 1.1 fvdl }
610 1.1 fvdl #if NEISA > 0
611 1.9 fvdl if (intr_find_mpmapping(mp_eisa_bus, line, ihp) == 0) {
612 1.1 fvdl *ihp |= line;
613 1.1 fvdl return 0;
614 1.1 fvdl }
615 1.1 fvdl #endif
616 1.1 fvdl printf("pci_intr_map: bus %d dev %d func %d pin %d; line %d\n",
617 1.1 fvdl bus, dev, func, pin, line);
618 1.1 fvdl printf("pci_intr_map: no MP mapping found\n");
619 1.1 fvdl }
620 1.1 fvdl #endif
621 1.1 fvdl
622 1.1 fvdl *ihp = line;
623 1.1 fvdl return 0;
624 1.1 fvdl
625 1.1 fvdl bad:
626 1.1 fvdl *ihp = -1;
627 1.1 fvdl return 1;
628 1.1 fvdl }
629 1.1 fvdl
630 1.1 fvdl const char *
631 1.1 fvdl pci_intr_string(pc, ih)
632 1.1 fvdl pci_chipset_tag_t pc;
633 1.1 fvdl pci_intr_handle_t ih;
634 1.1 fvdl {
635 1.7 fvdl return intr_string(ih);
636 1.7 fvdl }
637 1.1 fvdl
638 1.1 fvdl
639 1.1 fvdl const struct evcnt *
640 1.1 fvdl pci_intr_evcnt(pc, ih)
641 1.1 fvdl pci_chipset_tag_t pc;
642 1.1 fvdl pci_intr_handle_t ih;
643 1.1 fvdl {
644 1.1 fvdl
645 1.1 fvdl /* XXX for now, no evcnt parent reported */
646 1.1 fvdl return NULL;
647 1.1 fvdl }
648 1.1 fvdl
649 1.1 fvdl void *
650 1.1 fvdl pci_intr_establish(pc, ih, level, func, arg)
651 1.1 fvdl pci_chipset_tag_t pc;
652 1.1 fvdl pci_intr_handle_t ih;
653 1.1 fvdl int level, (*func) __P((void *));
654 1.1 fvdl void *arg;
655 1.1 fvdl {
656 1.1 fvdl int pin, irq;
657 1.1 fvdl struct pic *pic;
658 1.1 fvdl
659 1.1 fvdl pic = &i8259_pic;
660 1.1 fvdl pin = irq = ih;
661 1.1 fvdl
662 1.1 fvdl #if NIOAPIC > 0
663 1.1 fvdl if (ih & APIC_INT_VIA_APIC) {
664 1.1 fvdl pic = (struct pic *)ioapic_find(APIC_IRQ_APIC(ih));
665 1.1 fvdl if (pic == NULL) {
666 1.1 fvdl printf("pci_intr_establish: bad ioapic %d\n",
667 1.1 fvdl APIC_IRQ_APIC(ih));
668 1.1 fvdl return NULL;
669 1.1 fvdl }
670 1.1 fvdl pin = APIC_IRQ_PIN(ih);
671 1.1 fvdl irq = APIC_IRQ_LEGACY_IRQ(ih);
672 1.1 fvdl if (irq < 0 || irq >= NUM_LEGACY_IRQS)
673 1.1 fvdl irq = -1;
674 1.1 fvdl }
675 1.1 fvdl #endif
676 1.1 fvdl
677 1.1 fvdl return intr_establish(irq, pic, pin, IST_LEVEL, level, func, arg);
678 1.1 fvdl }
679 1.1 fvdl
680 1.1 fvdl void
681 1.1 fvdl pci_intr_disestablish(pc, cookie)
682 1.1 fvdl pci_chipset_tag_t pc;
683 1.1 fvdl void *cookie;
684 1.1 fvdl {
685 1.1 fvdl
686 1.1 fvdl intr_disestablish(cookie);
687 1.1 fvdl }
688 1.1 fvdl
689 1.1 fvdl /*
690 1.1 fvdl * Determine which flags should be passed to the primary PCI bus's
691 1.1 fvdl * autoconfiguration node. We use this to detect broken chipsets
692 1.1 fvdl * which cannot safely use memory-mapped device access.
693 1.1 fvdl */
694 1.1 fvdl int
695 1.1 fvdl pci_bus_flags()
696 1.1 fvdl {
697 1.1 fvdl int rval = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED |
698 1.1 fvdl PCI_FLAGS_MRL_OKAY | PCI_FLAGS_MRM_OKAY | PCI_FLAGS_MWI_OKAY;
699 1.1 fvdl int device, maxndevs;
700 1.1 fvdl pcitag_t tag;
701 1.1 fvdl pcireg_t id;
702 1.1 fvdl
703 1.1 fvdl maxndevs = pci_bus_maxdevs(NULL, 0);
704 1.1 fvdl
705 1.1 fvdl for (device = 0; device < maxndevs; device++) {
706 1.1 fvdl tag = pci_make_tag(NULL, 0, device, 0);
707 1.1 fvdl id = pci_conf_read(NULL, tag, PCI_ID_REG);
708 1.1 fvdl
709 1.1 fvdl /* Invalid vendor ID value? */
710 1.1 fvdl if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
711 1.1 fvdl continue;
712 1.1 fvdl /* XXX Not invalid, but we've done this ~forever. */
713 1.1 fvdl if (PCI_VENDOR(id) == 0)
714 1.1 fvdl continue;
715 1.1 fvdl
716 1.1 fvdl switch (PCI_VENDOR(id)) {
717 1.1 fvdl case PCI_VENDOR_SIS:
718 1.1 fvdl switch (PCI_PRODUCT(id)) {
719 1.1 fvdl case PCI_PRODUCT_SIS_85C496:
720 1.1 fvdl goto disable_mem;
721 1.1 fvdl }
722 1.1 fvdl break;
723 1.1 fvdl }
724 1.1 fvdl }
725 1.1 fvdl
726 1.1 fvdl return (rval);
727 1.1 fvdl
728 1.1 fvdl disable_mem:
729 1.1 fvdl printf("Warning: broken PCI-Host bridge detected; "
730 1.1 fvdl "disabling memory-mapped access\n");
731 1.1 fvdl rval &= ~(PCI_FLAGS_MEM_ENABLED|PCI_FLAGS_MRL_OKAY|PCI_FLAGS_MRM_OKAY|
732 1.1 fvdl PCI_FLAGS_MWI_OKAY);
733 1.1 fvdl return (rval);
734 1.1 fvdl }
735 1.11 sekiya
736 1.11 sekiya void
737 1.11 sekiya pci_device_foreach(pci_chipset_tag_t pc, int maxbus,
738 1.11 sekiya void (*func)(pci_chipset_tag_t, pcitag_t, void *), void *context)
739 1.11 sekiya {
740 1.11 sekiya pci_device_foreach_min(pc, 0, maxbus, func, context);
741 1.11 sekiya }
742 1.11 sekiya
743 1.11 sekiya void
744 1.11 sekiya pci_device_foreach_min(pci_chipset_tag_t pc, int minbus, int maxbus,
745 1.11 sekiya void (*func)(pci_chipset_tag_t, pcitag_t, void *), void *context)
746 1.11 sekiya {
747 1.11 sekiya const struct pci_quirkdata *qd;
748 1.11 sekiya int bus, device, function, maxdevs, nfuncs;
749 1.11 sekiya pcireg_t id, bhlcr;
750 1.11 sekiya pcitag_t tag;
751 1.11 sekiya
752 1.11 sekiya for (bus = minbus; bus <= maxbus; bus++) {
753 1.11 sekiya maxdevs = pci_bus_maxdevs(pc, bus);
754 1.11 sekiya for (device = 0; device < maxdevs; device++) {
755 1.11 sekiya tag = pci_make_tag(pc, bus, device, 0);
756 1.11 sekiya id = pci_conf_read(pc, tag, PCI_ID_REG);
757 1.11 sekiya
758 1.11 sekiya /* Invalid vendor ID value? */
759 1.11 sekiya if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
760 1.11 sekiya continue;
761 1.11 sekiya /* XXX Not invalid, but we've done this ~forever. */
762 1.11 sekiya if (PCI_VENDOR(id) == 0)
763 1.11 sekiya continue;
764 1.11 sekiya
765 1.11 sekiya qd = pci_lookup_quirkdata(PCI_VENDOR(id),
766 1.11 sekiya PCI_PRODUCT(id));
767 1.11 sekiya
768 1.11 sekiya bhlcr = pci_conf_read(pc, tag, PCI_BHLC_REG);
769 1.11 sekiya if (PCI_HDRTYPE_MULTIFN(bhlcr) ||
770 1.11 sekiya (qd != NULL &&
771 1.11 sekiya (qd->quirks & PCI_QUIRK_MULTIFUNCTION) != 0))
772 1.11 sekiya nfuncs = 8;
773 1.11 sekiya else
774 1.11 sekiya nfuncs = 1;
775 1.11 sekiya
776 1.11 sekiya for (function = 0; function < nfuncs; function++) {
777 1.11 sekiya tag = pci_make_tag(pc, bus, device, function);
778 1.11 sekiya id = pci_conf_read(pc, tag, PCI_ID_REG);
779 1.11 sekiya
780 1.11 sekiya /* Invalid vendor ID value? */
781 1.11 sekiya if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
782 1.11 sekiya continue;
783 1.11 sekiya /*
784 1.11 sekiya * XXX Not invalid, but we've done this
785 1.11 sekiya * ~forever.
786 1.11 sekiya */
787 1.11 sekiya if (PCI_VENDOR(id) == 0)
788 1.11 sekiya continue;
789 1.11 sekiya (*func)(pc, tag, context);
790 1.11 sekiya }
791 1.11 sekiya }
792 1.11 sekiya }
793 1.11 sekiya }
794 1.11 sekiya
795 1.11 sekiya void
796 1.11 sekiya pci_bridge_foreach(pci_chipset_tag_t pc, int minbus, int maxbus,
797 1.11 sekiya void (*func)(pci_chipset_tag_t, pcitag_t, void *), void *ctx)
798 1.11 sekiya {
799 1.11 sekiya struct pci_bridge_hook_arg bridge_hook;
800 1.11 sekiya
801 1.11 sekiya bridge_hook.func = func;
802 1.11 sekiya bridge_hook.arg = ctx;
803 1.11 sekiya
804 1.11 sekiya pci_device_foreach_min(pc, minbus, maxbus, pci_bridge_hook,
805 1.11 sekiya &bridge_hook);
806 1.11 sekiya }
807 1.11 sekiya
808 1.11 sekiya static void
809 1.11 sekiya pci_bridge_hook(pci_chipset_tag_t pc, pcitag_t tag, void *ctx)
810 1.11 sekiya {
811 1.11 sekiya struct pci_bridge_hook_arg *bridge_hook = (void *)ctx;
812 1.11 sekiya pcireg_t reg;
813 1.11 sekiya
814 1.11 sekiya reg = pci_conf_read(pc, tag, PCI_CLASS_REG);
815 1.11 sekiya if (PCI_CLASS(reg) == PCI_CLASS_BRIDGE &&
816 1.11 sekiya (PCI_SUBCLASS(reg) == PCI_SUBCLASS_BRIDGE_PCI ||
817 1.11 sekiya PCI_SUBCLASS(reg) == PCI_SUBCLASS_BRIDGE_CARDBUS)) {
818 1.11 sekiya (*bridge_hook->func)(pc, tag, bridge_hook->arg);
819 1.11 sekiya }
820 1.11 sekiya }
821 1.11 sekiya
822 1.11 sekiya
823