pci_machdep.c revision 1.16 1 1.16 christos /* $NetBSD: pci_machdep.c,v 1.16 2006/07/04 00:30:23 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.16 christos __KERNEL_RCSID(0, "$NetBSD: pci_machdep.c,v 1.16 2006/07/04 00:30:23 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
100 1.3 fvdl #include <dev/isa/isareg.h>
101 1.1 fvdl #include <dev/isa/isavar.h>
102 1.1 fvdl #include <dev/pci/pcivar.h>
103 1.1 fvdl #include <dev/pci/pcireg.h>
104 1.1 fvdl #include <dev/pci/pcidevs.h>
105 1.1 fvdl
106 1.16 christos #include "acpi.h"
107 1.14 bouyer #include "opt_mpbios.h"
108 1.16 christos #include "opt_acpi.h"
109 1.14 bouyer
110 1.14 bouyer #ifdef MPBIOS
111 1.14 bouyer #include <machine/mpbiosvar.h>
112 1.14 bouyer #endif
113 1.14 bouyer
114 1.16 christos #if NACPI > 0
115 1.14 bouyer #include <machine/mpacpi.h>
116 1.14 bouyer #endif
117 1.14 bouyer
118 1.16 christos #include <machine/mpconfig.h>
119 1.16 christos
120 1.1 fvdl #include "opt_pci_conf_mode.h"
121 1.1 fvdl
122 1.1 fvdl int pci_mode = -1;
123 1.1 fvdl
124 1.11 sekiya static void pci_bridge_hook(pci_chipset_tag_t, pcitag_t, void *);
125 1.11 sekiya struct pci_bridge_hook_arg {
126 1.11 sekiya void (*func)(pci_chipset_tag_t, pcitag_t, void *);
127 1.11 sekiya void *arg;
128 1.11 sekiya };
129 1.11 sekiya
130 1.11 sekiya
131 1.1 fvdl struct simplelock pci_conf_slock = SIMPLELOCK_INITIALIZER;
132 1.1 fvdl
133 1.1 fvdl #define PCI_CONF_LOCK(s) \
134 1.1 fvdl do { \
135 1.1 fvdl (s) = splhigh(); \
136 1.1 fvdl simple_lock(&pci_conf_slock); \
137 1.1 fvdl } while (0)
138 1.1 fvdl
139 1.1 fvdl #define PCI_CONF_UNLOCK(s) \
140 1.1 fvdl do { \
141 1.1 fvdl simple_unlock(&pci_conf_slock); \
142 1.1 fvdl splx((s)); \
143 1.1 fvdl } while (0)
144 1.1 fvdl
145 1.1 fvdl #define PCI_MODE1_ENABLE 0x80000000UL
146 1.1 fvdl #define PCI_MODE1_ADDRESS_REG 0x0cf8
147 1.1 fvdl #define PCI_MODE1_DATA_REG 0x0cfc
148 1.1 fvdl
149 1.1 fvdl #define PCI_MODE2_ENABLE_REG 0x0cf8
150 1.1 fvdl #define PCI_MODE2_FORWARD_REG 0x0cfa
151 1.1 fvdl
152 1.1 fvdl #define _m1tag(b, d, f) \
153 1.1 fvdl (PCI_MODE1_ENABLE | ((b) << 16) | ((d) << 11) | ((f) << 8))
154 1.1 fvdl #define _qe(bus, dev, fcn, vend, prod) \
155 1.1 fvdl {_m1tag(bus, dev, fcn), PCI_ID_CODE(vend, prod)}
156 1.1 fvdl struct {
157 1.1 fvdl u_int32_t tag;
158 1.1 fvdl pcireg_t id;
159 1.1 fvdl } pcim1_quirk_tbl[] = {
160 1.1 fvdl _qe(0, 0, 0, PCI_VENDOR_COMPAQ, PCI_PRODUCT_COMPAQ_TRIFLEX1),
161 1.1 fvdl /* XXX Triflex2 not tested */
162 1.1 fvdl _qe(0, 0, 0, PCI_VENDOR_COMPAQ, PCI_PRODUCT_COMPAQ_TRIFLEX2),
163 1.1 fvdl _qe(0, 0, 0, PCI_VENDOR_COMPAQ, PCI_PRODUCT_COMPAQ_TRIFLEX4),
164 1.1 fvdl /* Triton needed for Connectix Virtual PC */
165 1.1 fvdl _qe(0, 0, 0, PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82437FX),
166 1.1 fvdl /* Connectix Virtual PC 5 has a 440BX */
167 1.1 fvdl _qe(0, 0, 0, PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82443BX_NOAGP),
168 1.15 soren /* Parallels Desktop for Mac */
169 1.15 soren _qe(0, 2, 0, PCI_VENDOR_PARALLELS, PCI_PRODUCT_PARALLELS_VIDEO),
170 1.15 soren _qe(0, 3, 0, PCI_VENDOR_PARALLELS, PCI_PRODUCT_PARALLELS_TOOLS),
171 1.12 christos /* SIS 741 */
172 1.12 christos _qe(0, 0, 0, PCI_VENDOR_SIS, PCI_PRODUCT_SIS_741),
173 1.1 fvdl {0, 0xffffffff} /* patchable */
174 1.1 fvdl };
175 1.1 fvdl #undef _m1tag
176 1.1 fvdl #undef _id
177 1.1 fvdl #undef _qe
178 1.1 fvdl
179 1.1 fvdl /*
180 1.1 fvdl * PCI doesn't have any special needs; just use the generic versions
181 1.1 fvdl * of these functions.
182 1.1 fvdl */
183 1.1 fvdl struct x86_bus_dma_tag pci_bus_dma_tag = {
184 1.3 fvdl #if defined(_LP64) || defined(PAE)
185 1.3 fvdl PCI32_DMA_BOUNCE_THRESHOLD, /* bounce_thresh */
186 1.3 fvdl ISA_DMA_BOUNCE_THRESHOLD, /* bounce_alloclo */
187 1.3 fvdl PCI32_DMA_BOUNCE_THRESHOLD, /* bounce_allochi */
188 1.3 fvdl #else
189 1.3 fvdl 0,
190 1.3 fvdl 0,
191 1.3 fvdl 0,
192 1.3 fvdl #endif
193 1.3 fvdl NULL, /* _may_bounce */
194 1.1 fvdl _bus_dmamap_create,
195 1.1 fvdl _bus_dmamap_destroy,
196 1.1 fvdl _bus_dmamap_load,
197 1.1 fvdl _bus_dmamap_load_mbuf,
198 1.1 fvdl _bus_dmamap_load_uio,
199 1.1 fvdl _bus_dmamap_load_raw,
200 1.1 fvdl _bus_dmamap_unload,
201 1.3 fvdl #if defined(_LP64) || defined(PAE)
202 1.3 fvdl _bus_dmamap_sync,
203 1.3 fvdl #else
204 1.3 fvdl NULL,
205 1.3 fvdl #endif
206 1.1 fvdl _bus_dmamem_alloc,
207 1.1 fvdl _bus_dmamem_free,
208 1.1 fvdl _bus_dmamem_map,
209 1.1 fvdl _bus_dmamem_unmap,
210 1.1 fvdl _bus_dmamem_mmap,
211 1.1 fvdl };
212 1.5 fvdl
213 1.5 fvdl #ifdef _LP64
214 1.5 fvdl struct x86_bus_dma_tag pci_bus_dma64_tag = {
215 1.5 fvdl 0,
216 1.5 fvdl 0,
217 1.5 fvdl 0,
218 1.5 fvdl NULL, /* _may_bounce */
219 1.5 fvdl _bus_dmamap_create,
220 1.5 fvdl _bus_dmamap_destroy,
221 1.5 fvdl _bus_dmamap_load,
222 1.5 fvdl _bus_dmamap_load_mbuf,
223 1.5 fvdl _bus_dmamap_load_uio,
224 1.5 fvdl _bus_dmamap_load_raw,
225 1.5 fvdl _bus_dmamap_unload,
226 1.5 fvdl NULL,
227 1.5 fvdl _bus_dmamem_alloc,
228 1.5 fvdl _bus_dmamem_free,
229 1.5 fvdl _bus_dmamem_map,
230 1.5 fvdl _bus_dmamem_unmap,
231 1.5 fvdl _bus_dmamem_mmap,
232 1.5 fvdl };
233 1.5 fvdl #endif
234 1.1 fvdl
235 1.1 fvdl void
236 1.1 fvdl pci_attach_hook(parent, self, pba)
237 1.1 fvdl struct device *parent, *self;
238 1.1 fvdl struct pcibus_attach_args *pba;
239 1.1 fvdl {
240 1.1 fvdl
241 1.1 fvdl if (pba->pba_bus == 0)
242 1.1 fvdl printf(": configuration mode %d", pci_mode);
243 1.4 fvdl #ifdef MPBIOS
244 1.4 fvdl mpbios_pci_attach_hook(parent, self, pba);
245 1.4 fvdl #endif
246 1.16 christos #if NACPI > 0
247 1.4 fvdl mpacpi_pci_attach_hook(parent, self, pba);
248 1.4 fvdl #endif
249 1.1 fvdl }
250 1.1 fvdl
251 1.1 fvdl int
252 1.1 fvdl pci_bus_maxdevs(pc, busno)
253 1.1 fvdl pci_chipset_tag_t pc;
254 1.1 fvdl int busno;
255 1.1 fvdl {
256 1.1 fvdl
257 1.1 fvdl /*
258 1.1 fvdl * Bus number is irrelevant. If Configuration Mechanism 2 is in
259 1.1 fvdl * use, can only have devices 0-15 on any bus. If Configuration
260 1.1 fvdl * Mechanism 1 is in use, can have devices 0-32 (i.e. the `normal'
261 1.1 fvdl * range).
262 1.1 fvdl */
263 1.1 fvdl if (pci_mode == 2)
264 1.1 fvdl return (16);
265 1.1 fvdl else
266 1.1 fvdl return (32);
267 1.1 fvdl }
268 1.1 fvdl
269 1.1 fvdl pcitag_t
270 1.1 fvdl pci_make_tag(pc, bus, device, function)
271 1.1 fvdl pci_chipset_tag_t pc;
272 1.1 fvdl int bus, device, function;
273 1.1 fvdl {
274 1.1 fvdl pcitag_t tag;
275 1.1 fvdl
276 1.1 fvdl #ifndef PCI_CONF_MODE
277 1.1 fvdl switch (pci_mode) {
278 1.1 fvdl case 1:
279 1.1 fvdl goto mode1;
280 1.1 fvdl case 2:
281 1.1 fvdl goto mode2;
282 1.1 fvdl default:
283 1.1 fvdl panic("pci_make_tag: mode not configured");
284 1.1 fvdl }
285 1.1 fvdl #endif
286 1.1 fvdl
287 1.1 fvdl #if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 1)
288 1.1 fvdl #ifndef PCI_CONF_MODE
289 1.1 fvdl mode1:
290 1.1 fvdl #endif
291 1.1 fvdl if (bus >= 256 || device >= 32 || function >= 8)
292 1.1 fvdl panic("pci_make_tag: bad request");
293 1.1 fvdl
294 1.1 fvdl tag.mode1 = PCI_MODE1_ENABLE |
295 1.1 fvdl (bus << 16) | (device << 11) | (function << 8);
296 1.1 fvdl return tag;
297 1.1 fvdl #endif
298 1.1 fvdl
299 1.1 fvdl #if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 2)
300 1.1 fvdl #ifndef PCI_CONF_MODE
301 1.1 fvdl mode2:
302 1.1 fvdl #endif
303 1.1 fvdl if (bus >= 256 || device >= 16 || function >= 8)
304 1.1 fvdl panic("pci_make_tag: bad request");
305 1.1 fvdl
306 1.1 fvdl tag.mode2.port = 0xc000 | (device << 8);
307 1.1 fvdl tag.mode2.enable = 0xf0 | (function << 1);
308 1.1 fvdl tag.mode2.forward = bus;
309 1.1 fvdl return tag;
310 1.1 fvdl #endif
311 1.1 fvdl }
312 1.1 fvdl
313 1.1 fvdl void
314 1.1 fvdl pci_decompose_tag(pc, tag, bp, dp, fp)
315 1.1 fvdl pci_chipset_tag_t pc;
316 1.1 fvdl pcitag_t tag;
317 1.1 fvdl int *bp, *dp, *fp;
318 1.1 fvdl {
319 1.1 fvdl
320 1.1 fvdl #ifndef PCI_CONF_MODE
321 1.1 fvdl switch (pci_mode) {
322 1.1 fvdl case 1:
323 1.1 fvdl goto mode1;
324 1.1 fvdl case 2:
325 1.1 fvdl goto mode2;
326 1.1 fvdl default:
327 1.1 fvdl panic("pci_decompose_tag: mode not configured");
328 1.1 fvdl }
329 1.1 fvdl #endif
330 1.1 fvdl
331 1.1 fvdl #if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 1)
332 1.1 fvdl #ifndef PCI_CONF_MODE
333 1.1 fvdl mode1:
334 1.1 fvdl #endif
335 1.1 fvdl if (bp != NULL)
336 1.1 fvdl *bp = (tag.mode1 >> 16) & 0xff;
337 1.1 fvdl if (dp != NULL)
338 1.1 fvdl *dp = (tag.mode1 >> 11) & 0x1f;
339 1.1 fvdl if (fp != NULL)
340 1.1 fvdl *fp = (tag.mode1 >> 8) & 0x7;
341 1.1 fvdl return;
342 1.1 fvdl #endif
343 1.1 fvdl
344 1.1 fvdl #if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 2)
345 1.1 fvdl #ifndef PCI_CONF_MODE
346 1.1 fvdl mode2:
347 1.1 fvdl #endif
348 1.1 fvdl if (bp != NULL)
349 1.1 fvdl *bp = tag.mode2.forward & 0xff;
350 1.1 fvdl if (dp != NULL)
351 1.1 fvdl *dp = (tag.mode2.port >> 8) & 0xf;
352 1.1 fvdl if (fp != NULL)
353 1.1 fvdl *fp = (tag.mode2.enable >> 1) & 0x7;
354 1.1 fvdl #endif
355 1.1 fvdl }
356 1.1 fvdl
357 1.1 fvdl pcireg_t
358 1.1 fvdl pci_conf_read(pc, tag, reg)
359 1.1 fvdl pci_chipset_tag_t pc;
360 1.1 fvdl pcitag_t tag;
361 1.1 fvdl int reg;
362 1.1 fvdl {
363 1.1 fvdl pcireg_t data;
364 1.1 fvdl int s;
365 1.1 fvdl
366 1.1 fvdl #ifndef PCI_CONF_MODE
367 1.1 fvdl switch (pci_mode) {
368 1.1 fvdl case 1:
369 1.1 fvdl goto mode1;
370 1.1 fvdl case 2:
371 1.1 fvdl goto mode2;
372 1.1 fvdl default:
373 1.1 fvdl panic("pci_conf_read: mode not configured");
374 1.1 fvdl }
375 1.1 fvdl #endif
376 1.1 fvdl
377 1.1 fvdl #if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 1)
378 1.1 fvdl #ifndef PCI_CONF_MODE
379 1.1 fvdl mode1:
380 1.1 fvdl #endif
381 1.1 fvdl PCI_CONF_LOCK(s);
382 1.1 fvdl outl(PCI_MODE1_ADDRESS_REG, tag.mode1 | reg);
383 1.1 fvdl data = inl(PCI_MODE1_DATA_REG);
384 1.1 fvdl outl(PCI_MODE1_ADDRESS_REG, 0);
385 1.1 fvdl PCI_CONF_UNLOCK(s);
386 1.1 fvdl return data;
387 1.1 fvdl #endif
388 1.1 fvdl
389 1.1 fvdl #if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 2)
390 1.1 fvdl #ifndef PCI_CONF_MODE
391 1.1 fvdl mode2:
392 1.1 fvdl #endif
393 1.1 fvdl PCI_CONF_LOCK(s);
394 1.1 fvdl outb(PCI_MODE2_ENABLE_REG, tag.mode2.enable);
395 1.1 fvdl outb(PCI_MODE2_FORWARD_REG, tag.mode2.forward);
396 1.1 fvdl data = inl(tag.mode2.port | reg);
397 1.1 fvdl outb(PCI_MODE2_ENABLE_REG, 0);
398 1.1 fvdl PCI_CONF_UNLOCK(s);
399 1.1 fvdl return data;
400 1.1 fvdl #endif
401 1.1 fvdl }
402 1.1 fvdl
403 1.1 fvdl void
404 1.1 fvdl pci_conf_write(pc, tag, reg, data)
405 1.1 fvdl pci_chipset_tag_t pc;
406 1.1 fvdl pcitag_t tag;
407 1.1 fvdl int reg;
408 1.1 fvdl pcireg_t data;
409 1.1 fvdl {
410 1.1 fvdl int s;
411 1.1 fvdl
412 1.1 fvdl #ifndef PCI_CONF_MODE
413 1.1 fvdl switch (pci_mode) {
414 1.1 fvdl case 1:
415 1.1 fvdl goto mode1;
416 1.1 fvdl case 2:
417 1.1 fvdl goto mode2;
418 1.1 fvdl default:
419 1.1 fvdl panic("pci_conf_write: mode not configured");
420 1.1 fvdl }
421 1.1 fvdl #endif
422 1.1 fvdl
423 1.1 fvdl #if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 1)
424 1.1 fvdl #ifndef PCI_CONF_MODE
425 1.1 fvdl mode1:
426 1.1 fvdl #endif
427 1.1 fvdl PCI_CONF_LOCK(s);
428 1.1 fvdl outl(PCI_MODE1_ADDRESS_REG, tag.mode1 | reg);
429 1.1 fvdl outl(PCI_MODE1_DATA_REG, data);
430 1.1 fvdl outl(PCI_MODE1_ADDRESS_REG, 0);
431 1.1 fvdl PCI_CONF_UNLOCK(s);
432 1.1 fvdl return;
433 1.1 fvdl #endif
434 1.1 fvdl
435 1.1 fvdl #if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 2)
436 1.1 fvdl #ifndef PCI_CONF_MODE
437 1.1 fvdl mode2:
438 1.1 fvdl #endif
439 1.1 fvdl PCI_CONF_LOCK(s);
440 1.1 fvdl outb(PCI_MODE2_ENABLE_REG, tag.mode2.enable);
441 1.1 fvdl outb(PCI_MODE2_FORWARD_REG, tag.mode2.forward);
442 1.1 fvdl outl(tag.mode2.port | reg, data);
443 1.1 fvdl outb(PCI_MODE2_ENABLE_REG, 0);
444 1.1 fvdl PCI_CONF_UNLOCK(s);
445 1.1 fvdl #endif
446 1.1 fvdl }
447 1.1 fvdl
448 1.1 fvdl int
449 1.1 fvdl pci_mode_detect()
450 1.1 fvdl {
451 1.1 fvdl
452 1.1 fvdl #ifdef PCI_CONF_MODE
453 1.1 fvdl #if (PCI_CONF_MODE == 1) || (PCI_CONF_MODE == 2)
454 1.1 fvdl return (pci_mode = PCI_CONF_MODE);
455 1.1 fvdl #else
456 1.1 fvdl #error Invalid PCI configuration mode.
457 1.1 fvdl #endif
458 1.1 fvdl #else
459 1.1 fvdl u_int32_t sav, val;
460 1.1 fvdl int i;
461 1.1 fvdl pcireg_t idreg;
462 1.1 fvdl
463 1.1 fvdl if (pci_mode != -1)
464 1.1 fvdl return pci_mode;
465 1.1 fvdl
466 1.1 fvdl /*
467 1.1 fvdl * We try to divine which configuration mode the host bridge wants.
468 1.1 fvdl */
469 1.1 fvdl
470 1.1 fvdl sav = inl(PCI_MODE1_ADDRESS_REG);
471 1.1 fvdl
472 1.1 fvdl pci_mode = 1; /* assume this for now */
473 1.1 fvdl /*
474 1.1 fvdl * catch some known buggy implementations of mode 1
475 1.1 fvdl */
476 1.1 fvdl for (i = 0; i < sizeof(pcim1_quirk_tbl) / sizeof(pcim1_quirk_tbl[0]);
477 1.1 fvdl i++) {
478 1.1 fvdl pcitag_t t;
479 1.1 fvdl
480 1.1 fvdl if (!pcim1_quirk_tbl[i].tag)
481 1.1 fvdl break;
482 1.1 fvdl t.mode1 = pcim1_quirk_tbl[i].tag;
483 1.1 fvdl idreg = pci_conf_read(0, t, PCI_ID_REG); /* needs "pci_mode" */
484 1.1 fvdl if (idreg == pcim1_quirk_tbl[i].id) {
485 1.1 fvdl #ifdef DEBUG
486 1.1 fvdl printf("known mode 1 PCI chipset (%08x)\n",
487 1.1 fvdl idreg);
488 1.1 fvdl #endif
489 1.1 fvdl return (pci_mode);
490 1.1 fvdl }
491 1.1 fvdl }
492 1.1 fvdl
493 1.1 fvdl /*
494 1.1 fvdl * Strong check for standard compliant mode 1:
495 1.1 fvdl * 1. bit 31 ("enable") can be set
496 1.1 fvdl * 2. byte/word access does not affect register
497 1.1 fvdl */
498 1.1 fvdl outl(PCI_MODE1_ADDRESS_REG, PCI_MODE1_ENABLE);
499 1.1 fvdl outb(PCI_MODE1_ADDRESS_REG + 3, 0);
500 1.1 fvdl outw(PCI_MODE1_ADDRESS_REG + 2, 0);
501 1.1 fvdl val = inl(PCI_MODE1_ADDRESS_REG);
502 1.1 fvdl if ((val & 0x80fffffc) != PCI_MODE1_ENABLE) {
503 1.1 fvdl #ifdef DEBUG
504 1.1 fvdl printf("pci_mode_detect: mode 1 enable failed (%x)\n",
505 1.1 fvdl val);
506 1.1 fvdl #endif
507 1.1 fvdl goto not1;
508 1.1 fvdl }
509 1.1 fvdl outl(PCI_MODE1_ADDRESS_REG, 0);
510 1.1 fvdl val = inl(PCI_MODE1_ADDRESS_REG);
511 1.1 fvdl if ((val & 0x80fffffc) != 0)
512 1.1 fvdl goto not1;
513 1.1 fvdl return (pci_mode);
514 1.1 fvdl not1:
515 1.1 fvdl outl(PCI_MODE1_ADDRESS_REG, sav);
516 1.1 fvdl
517 1.1 fvdl /*
518 1.1 fvdl * This mode 2 check is quite weak (and known to give false
519 1.1 fvdl * positives on some Compaq machines).
520 1.1 fvdl * However, this doesn't matter, because this is the
521 1.1 fvdl * last test, and simply no PCI devices will be found if
522 1.1 fvdl * this happens.
523 1.1 fvdl */
524 1.1 fvdl outb(PCI_MODE2_ENABLE_REG, 0);
525 1.1 fvdl outb(PCI_MODE2_FORWARD_REG, 0);
526 1.1 fvdl if (inb(PCI_MODE2_ENABLE_REG) != 0 ||
527 1.1 fvdl inb(PCI_MODE2_FORWARD_REG) != 0)
528 1.1 fvdl goto not2;
529 1.1 fvdl return (pci_mode = 2);
530 1.1 fvdl not2:
531 1.1 fvdl
532 1.1 fvdl return (pci_mode = 0);
533 1.1 fvdl #endif
534 1.1 fvdl }
535 1.1 fvdl
536 1.1 fvdl /*
537 1.1 fvdl * Determine which flags should be passed to the primary PCI bus's
538 1.1 fvdl * autoconfiguration node. We use this to detect broken chipsets
539 1.1 fvdl * which cannot safely use memory-mapped device access.
540 1.1 fvdl */
541 1.1 fvdl int
542 1.1 fvdl pci_bus_flags()
543 1.1 fvdl {
544 1.1 fvdl int rval = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED |
545 1.1 fvdl PCI_FLAGS_MRL_OKAY | PCI_FLAGS_MRM_OKAY | PCI_FLAGS_MWI_OKAY;
546 1.1 fvdl int device, maxndevs;
547 1.1 fvdl pcitag_t tag;
548 1.1 fvdl pcireg_t id;
549 1.1 fvdl
550 1.1 fvdl maxndevs = pci_bus_maxdevs(NULL, 0);
551 1.1 fvdl
552 1.1 fvdl for (device = 0; device < maxndevs; device++) {
553 1.1 fvdl tag = pci_make_tag(NULL, 0, device, 0);
554 1.1 fvdl id = pci_conf_read(NULL, tag, PCI_ID_REG);
555 1.1 fvdl
556 1.1 fvdl /* Invalid vendor ID value? */
557 1.1 fvdl if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
558 1.1 fvdl continue;
559 1.1 fvdl /* XXX Not invalid, but we've done this ~forever. */
560 1.1 fvdl if (PCI_VENDOR(id) == 0)
561 1.1 fvdl continue;
562 1.1 fvdl
563 1.1 fvdl switch (PCI_VENDOR(id)) {
564 1.1 fvdl case PCI_VENDOR_SIS:
565 1.1 fvdl switch (PCI_PRODUCT(id)) {
566 1.1 fvdl case PCI_PRODUCT_SIS_85C496:
567 1.1 fvdl goto disable_mem;
568 1.1 fvdl }
569 1.1 fvdl break;
570 1.1 fvdl }
571 1.1 fvdl }
572 1.1 fvdl
573 1.1 fvdl return (rval);
574 1.1 fvdl
575 1.1 fvdl disable_mem:
576 1.1 fvdl printf("Warning: broken PCI-Host bridge detected; "
577 1.1 fvdl "disabling memory-mapped access\n");
578 1.1 fvdl rval &= ~(PCI_FLAGS_MEM_ENABLED|PCI_FLAGS_MRL_OKAY|PCI_FLAGS_MRM_OKAY|
579 1.1 fvdl PCI_FLAGS_MWI_OKAY);
580 1.1 fvdl return (rval);
581 1.1 fvdl }
582 1.11 sekiya
583 1.11 sekiya void
584 1.11 sekiya pci_device_foreach(pci_chipset_tag_t pc, int maxbus,
585 1.11 sekiya void (*func)(pci_chipset_tag_t, pcitag_t, void *), void *context)
586 1.11 sekiya {
587 1.11 sekiya pci_device_foreach_min(pc, 0, maxbus, func, context);
588 1.11 sekiya }
589 1.11 sekiya
590 1.11 sekiya void
591 1.11 sekiya pci_device_foreach_min(pci_chipset_tag_t pc, int minbus, int maxbus,
592 1.11 sekiya void (*func)(pci_chipset_tag_t, pcitag_t, void *), void *context)
593 1.11 sekiya {
594 1.11 sekiya const struct pci_quirkdata *qd;
595 1.11 sekiya int bus, device, function, maxdevs, nfuncs;
596 1.11 sekiya pcireg_t id, bhlcr;
597 1.11 sekiya pcitag_t tag;
598 1.11 sekiya
599 1.11 sekiya for (bus = minbus; bus <= maxbus; bus++) {
600 1.11 sekiya maxdevs = pci_bus_maxdevs(pc, bus);
601 1.11 sekiya for (device = 0; device < maxdevs; device++) {
602 1.11 sekiya tag = pci_make_tag(pc, bus, device, 0);
603 1.11 sekiya id = pci_conf_read(pc, tag, PCI_ID_REG);
604 1.11 sekiya
605 1.11 sekiya /* Invalid vendor ID value? */
606 1.11 sekiya if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
607 1.11 sekiya continue;
608 1.11 sekiya /* XXX Not invalid, but we've done this ~forever. */
609 1.11 sekiya if (PCI_VENDOR(id) == 0)
610 1.11 sekiya continue;
611 1.11 sekiya
612 1.11 sekiya qd = pci_lookup_quirkdata(PCI_VENDOR(id),
613 1.11 sekiya PCI_PRODUCT(id));
614 1.11 sekiya
615 1.11 sekiya bhlcr = pci_conf_read(pc, tag, PCI_BHLC_REG);
616 1.11 sekiya if (PCI_HDRTYPE_MULTIFN(bhlcr) ||
617 1.11 sekiya (qd != NULL &&
618 1.11 sekiya (qd->quirks & PCI_QUIRK_MULTIFUNCTION) != 0))
619 1.11 sekiya nfuncs = 8;
620 1.11 sekiya else
621 1.11 sekiya nfuncs = 1;
622 1.11 sekiya
623 1.11 sekiya for (function = 0; function < nfuncs; function++) {
624 1.11 sekiya tag = pci_make_tag(pc, bus, device, function);
625 1.11 sekiya id = pci_conf_read(pc, tag, PCI_ID_REG);
626 1.11 sekiya
627 1.11 sekiya /* Invalid vendor ID value? */
628 1.11 sekiya if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
629 1.11 sekiya continue;
630 1.11 sekiya /*
631 1.11 sekiya * XXX Not invalid, but we've done this
632 1.11 sekiya * ~forever.
633 1.11 sekiya */
634 1.11 sekiya if (PCI_VENDOR(id) == 0)
635 1.11 sekiya continue;
636 1.11 sekiya (*func)(pc, tag, context);
637 1.11 sekiya }
638 1.11 sekiya }
639 1.11 sekiya }
640 1.11 sekiya }
641 1.11 sekiya
642 1.11 sekiya void
643 1.11 sekiya pci_bridge_foreach(pci_chipset_tag_t pc, int minbus, int maxbus,
644 1.11 sekiya void (*func)(pci_chipset_tag_t, pcitag_t, void *), void *ctx)
645 1.11 sekiya {
646 1.11 sekiya struct pci_bridge_hook_arg bridge_hook;
647 1.11 sekiya
648 1.11 sekiya bridge_hook.func = func;
649 1.11 sekiya bridge_hook.arg = ctx;
650 1.11 sekiya
651 1.11 sekiya pci_device_foreach_min(pc, minbus, maxbus, pci_bridge_hook,
652 1.11 sekiya &bridge_hook);
653 1.11 sekiya }
654 1.11 sekiya
655 1.11 sekiya static void
656 1.11 sekiya pci_bridge_hook(pci_chipset_tag_t pc, pcitag_t tag, void *ctx)
657 1.11 sekiya {
658 1.11 sekiya struct pci_bridge_hook_arg *bridge_hook = (void *)ctx;
659 1.11 sekiya pcireg_t reg;
660 1.11 sekiya
661 1.11 sekiya reg = pci_conf_read(pc, tag, PCI_CLASS_REG);
662 1.11 sekiya if (PCI_CLASS(reg) == PCI_CLASS_BRIDGE &&
663 1.11 sekiya (PCI_SUBCLASS(reg) == PCI_SUBCLASS_BRIDGE_PCI ||
664 1.11 sekiya PCI_SUBCLASS(reg) == PCI_SUBCLASS_BRIDGE_CARDBUS)) {
665 1.11 sekiya (*bridge_hook->func)(pc, tag, bridge_hook->arg);
666 1.11 sekiya }
667 1.11 sekiya }
668