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