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