1 /* $NetBSD: pci_machdep.c,v 1.36 2009/07/03 13:15:22 drochner Exp $ */
4 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
34 * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved.
35 * Copyright (c) 1994 Charles M. Hannum. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by Charles M. Hannum.
48 * 4. The name of the author may not be used to endorse or promote products
49 * derived from this software without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
52 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
53 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
54 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
55 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
60 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64 * Machine-specific functions for PCI autoconfiguration.
66 * On PCs, there are two methods of generating PCI configuration cycles.
67 * We try to detect the appropriate mechanism for this machine and set
68 * up a few function pointers to access the correct method directly.
70 * The configuration method can be hard-coded in the config file by
71 * using `options PCI_CONF_MODE=N', where `N' is the configuration mode
72 * as defined section 3.6.4.1, `Generating Configuration Cycles'.
75 #include <sys/cdefs.h>
76 __KERNEL_RCSID(0, "$NetBSD: pci_machdep.c,v 1.36 2009/07/03 13:15:22 drochner Exp $");
78 #include <sys/types.h>
79 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/errno.h>
83 #include <sys/device.h>
86 #include <uvm/uvm_extern.h>
88 #include <machine/bus_private.h>
90 #include <machine/pio.h>
91 #include <machine/lock.h>
93 #include <dev/isa/isareg.h>
94 #include <dev/isa/isavar.h>
95 #include <dev/pci/pcivar.h>
96 #include <dev/pci/pcireg.h>
97 #include <dev/pci/pcidevs.h>
100 #include "opt_mpbios.h"
101 #include "opt_acpi.h"
104 #include <machine/mpbiosvar.h>
108 #include <machine/mpacpi.h>
111 #include <machine/mpconfig.h>
113 #include "opt_pci_conf_mode.h"
116 #include "opt_xbox.h"
118 #include <machine/xbox.h>
124 static void pci_bridge_hook(pci_chipset_tag_t
, pcitag_t
, void *);
125 struct pci_bridge_hook_arg
{
126 void (*func
)(pci_chipset_tag_t
, pcitag_t
, void *);
131 __cpu_simple_lock_t pci_conf_lock
= __SIMPLELOCK_UNLOCKED
;
133 #define PCI_CONF_LOCK(s) \
136 __cpu_simple_lock(&pci_conf_lock); \
139 #define PCI_CONF_UNLOCK(s) \
141 __cpu_simple_unlock(&pci_conf_lock); \
145 #define PCI_MODE1_ENABLE 0x80000000UL
146 #define PCI_MODE1_ADDRESS_REG 0x0cf8
147 #define PCI_MODE1_DATA_REG 0x0cfc
149 #define PCI_MODE2_ENABLE_REG 0x0cf8
150 #define PCI_MODE2_FORWARD_REG 0x0cfa
152 #define _m1tag(b, d, f) \
153 (PCI_MODE1_ENABLE | ((b) << 16) | ((d) << 11) | ((f) << 8))
154 #define _qe(bus, dev, fcn, vend, prod) \
155 {_m1tag(bus, dev, fcn), PCI_ID_CODE(vend, prod)}
159 } pcim1_quirk_tbl
[] = {
160 _qe(0, 0, 0, PCI_VENDOR_COMPAQ
, PCI_PRODUCT_COMPAQ_TRIFLEX1
),
161 /* XXX Triflex2 not tested */
162 _qe(0, 0, 0, PCI_VENDOR_COMPAQ
, PCI_PRODUCT_COMPAQ_TRIFLEX2
),
163 _qe(0, 0, 0, PCI_VENDOR_COMPAQ
, PCI_PRODUCT_COMPAQ_TRIFLEX4
),
164 /* Triton needed for Connectix Virtual PC */
165 _qe(0, 0, 0, PCI_VENDOR_INTEL
, PCI_PRODUCT_INTEL_82437FX
),
166 /* Connectix Virtual PC 5 has a 440BX */
167 _qe(0, 0, 0, PCI_VENDOR_INTEL
, PCI_PRODUCT_INTEL_82443BX_NOAGP
),
168 /* Parallels Desktop for Mac */
169 _qe(0, 2, 0, PCI_VENDOR_PARALLELS
, PCI_PRODUCT_PARALLELS_VIDEO
),
170 _qe(0, 3, 0, PCI_VENDOR_PARALLELS
, PCI_PRODUCT_PARALLELS_TOOLS
),
172 _qe(0, 0, 0, PCI_VENDOR_SIS
, PCI_PRODUCT_SIS_740
),
174 _qe(0, 0, 0, PCI_VENDOR_SIS
, PCI_PRODUCT_SIS_741
),
175 {0, 0xffffffff} /* patchable */
182 * PCI doesn't have any special needs; just use the generic versions
183 * of these functions.
185 struct x86_bus_dma_tag pci_bus_dma_tag
= {
186 0, /* tag_needs_free */
187 #if defined(_LP64) || defined(PAE)
188 PCI32_DMA_BOUNCE_THRESHOLD
, /* bounce_thresh */
189 ISA_DMA_BOUNCE_THRESHOLD
, /* bounce_alloclo */
190 PCI32_DMA_BOUNCE_THRESHOLD
, /* bounce_allochi */
196 NULL
, /* _may_bounce */
200 _bus_dmamap_load_mbuf
,
201 _bus_dmamap_load_uio
,
202 _bus_dmamap_load_raw
,
210 _bus_dmatag_subregion
,
215 struct x86_bus_dma_tag pci_bus_dma64_tag
= {
216 0, /* tag_needs_free */
220 NULL
, /* _may_bounce */
224 _bus_dmamap_load_mbuf
,
225 _bus_dmamap_load_uio
,
226 _bus_dmamap_load_raw
,
234 _bus_dmatag_subregion
,
240 pci_attach_hook(device_t parent
, device_t self
, struct pcibus_attach_args
*pba
)
243 if (pba
->pba_bus
== 0)
244 aprint_normal(": configuration mode %d", pci_mode
);
246 mpbios_pci_attach_hook(parent
, self
, pba
);
249 mpacpi_pci_attach_hook(parent
, self
, pba
);
254 pci_bus_maxdevs(pci_chipset_tag_t pc
, int busno
)
257 #if defined(__i386__) && defined(XBOX)
259 * Scanning above the first device is fatal on the Microsoft Xbox.
260 * If busno=1, only allow for one device.
262 if (arch_i386_is_xbox
) {
271 * Bus number is irrelevant. If Configuration Mechanism 2 is in
272 * use, can only have devices 0-15 on any bus. If Configuration
273 * Mechanism 1 is in use, can have devices 0-32 (i.e. the `normal'
283 pci_make_tag(pci_chipset_tag_t pc
, int bus
, int device
, int function
)
287 #ifndef PCI_CONF_MODE
294 panic("pci_make_tag: mode not configured");
298 #if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 1)
299 #ifndef PCI_CONF_MODE
302 if (bus
>= 256 || device
>= 32 || function
>= 8)
303 panic("pci_make_tag: bad request");
305 tag
.mode1
= PCI_MODE1_ENABLE
|
306 (bus
<< 16) | (device
<< 11) | (function
<< 8);
310 #if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 2)
311 #ifndef PCI_CONF_MODE
314 if (bus
>= 256 || device
>= 16 || function
>= 8)
315 panic("pci_make_tag: bad request");
317 tag
.mode2
.port
= 0xc000 | (device
<< 8);
318 tag
.mode2
.enable
= 0xf0 | (function
<< 1);
319 tag
.mode2
.forward
= bus
;
325 pci_decompose_tag(pci_chipset_tag_t pc
, pcitag_t tag
,
326 int *bp
, int *dp
, int *fp
)
329 #ifndef PCI_CONF_MODE
336 panic("pci_decompose_tag: mode not configured");
340 #if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 1)
341 #ifndef PCI_CONF_MODE
345 *bp
= (tag
.mode1
>> 16) & 0xff;
347 *dp
= (tag
.mode1
>> 11) & 0x1f;
349 *fp
= (tag
.mode1
>> 8) & 0x7;
353 #if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 2)
354 #ifndef PCI_CONF_MODE
358 *bp
= tag
.mode2
.forward
& 0xff;
360 *dp
= (tag
.mode2
.port
>> 8) & 0xf;
362 *fp
= (tag
.mode2
.enable
>> 1) & 0x7;
367 pci_conf_read( pci_chipset_tag_t pc
, pcitag_t tag
,
373 KASSERT((reg
& 0x3) == 0);
374 #if defined(__i386__) && defined(XBOX)
375 if (arch_i386_is_xbox
) {
377 pci_decompose_tag(pc
, tag
, &bus
, &dev
, &fn
);
378 if (bus
== 0 && dev
== 0 && (fn
== 1 || fn
== 2))
383 #ifndef PCI_CONF_MODE
390 panic("pci_conf_read: mode not configured");
394 #if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 1)
395 #ifndef PCI_CONF_MODE
399 outl(PCI_MODE1_ADDRESS_REG
, tag
.mode1
| reg
);
400 data
= inl(PCI_MODE1_DATA_REG
);
401 outl(PCI_MODE1_ADDRESS_REG
, 0);
406 #if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 2)
407 #ifndef PCI_CONF_MODE
411 outb(PCI_MODE2_ENABLE_REG
, tag
.mode2
.enable
);
412 outb(PCI_MODE2_FORWARD_REG
, tag
.mode2
.forward
);
413 data
= inl(tag
.mode2
.port
| reg
);
414 outb(PCI_MODE2_ENABLE_REG
, 0);
421 pci_conf_write(pci_chipset_tag_t pc
, pcitag_t tag
, int reg
,
426 KASSERT((reg
& 0x3) == 0);
427 #if defined(__i386__) && defined(XBOX)
428 if (arch_i386_is_xbox
) {
430 pci_decompose_tag(pc
, tag
, &bus
, &dev
, &fn
);
431 if (bus
== 0 && dev
== 0 && (fn
== 1 || fn
== 2))
436 #ifndef PCI_CONF_MODE
443 panic("pci_conf_write: mode not configured");
447 #if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 1)
448 #ifndef PCI_CONF_MODE
452 outl(PCI_MODE1_ADDRESS_REG
, tag
.mode1
| reg
);
453 outl(PCI_MODE1_DATA_REG
, data
);
454 outl(PCI_MODE1_ADDRESS_REG
, 0);
459 #if !defined(PCI_CONF_MODE) || (PCI_CONF_MODE == 2)
460 #ifndef PCI_CONF_MODE
464 outb(PCI_MODE2_ENABLE_REG
, tag
.mode2
.enable
);
465 outb(PCI_MODE2_FORWARD_REG
, tag
.mode2
.forward
);
466 outl(tag
.mode2
.port
| reg
, data
);
467 outb(PCI_MODE2_ENABLE_REG
, 0);
473 pci_mode_detect(void)
477 #if (PCI_CONF_MODE == 1) || (PCI_CONF_MODE == 2)
478 return (pci_mode
= PCI_CONF_MODE
);
480 #error Invalid PCI configuration mode.
491 * We try to divine which configuration mode the host bridge wants.
494 sav
= inl(PCI_MODE1_ADDRESS_REG
);
496 pci_mode
= 1; /* assume this for now */
498 * catch some known buggy implementations of mode 1
500 for (i
= 0; i
< __arraycount(pcim1_quirk_tbl
); i
++) {
503 if (!pcim1_quirk_tbl
[i
].tag
)
505 t
.mode1
= pcim1_quirk_tbl
[i
].tag
;
506 idreg
= pci_conf_read(0, t
, PCI_ID_REG
); /* needs "pci_mode" */
507 if (idreg
== pcim1_quirk_tbl
[i
].id
) {
509 printf("known mode 1 PCI chipset (%08x)\n",
517 * Strong check for standard compliant mode 1:
518 * 1. bit 31 ("enable") can be set
519 * 2. byte/word access does not affect register
521 outl(PCI_MODE1_ADDRESS_REG
, PCI_MODE1_ENABLE
);
522 outb(PCI_MODE1_ADDRESS_REG
+ 3, 0);
523 outw(PCI_MODE1_ADDRESS_REG
+ 2, 0);
524 val
= inl(PCI_MODE1_ADDRESS_REG
);
525 if ((val
& 0x80fffffc) != PCI_MODE1_ENABLE
) {
527 printf("pci_mode_detect: mode 1 enable failed (%x)\n",
532 outl(PCI_MODE1_ADDRESS_REG
, 0);
533 val
= inl(PCI_MODE1_ADDRESS_REG
);
534 if ((val
& 0x80fffffc) != 0)
538 outl(PCI_MODE1_ADDRESS_REG
, sav
);
541 * This mode 2 check is quite weak (and known to give false
542 * positives on some Compaq machines).
543 * However, this doesn't matter, because this is the
544 * last test, and simply no PCI devices will be found if
547 outb(PCI_MODE2_ENABLE_REG
, 0);
548 outb(PCI_MODE2_FORWARD_REG
, 0);
549 if (inb(PCI_MODE2_ENABLE_REG
) != 0 ||
550 inb(PCI_MODE2_FORWARD_REG
) != 0)
552 return (pci_mode
= 2);
555 return (pci_mode
= 0);
560 * Determine which flags should be passed to the primary PCI bus's
561 * autoconfiguration node. We use this to detect broken chipsets
562 * which cannot safely use memory-mapped device access.
567 int rval
= PCI_FLAGS_IO_ENABLED
| PCI_FLAGS_MEM_ENABLED
|
568 PCI_FLAGS_MRL_OKAY
| PCI_FLAGS_MRM_OKAY
| PCI_FLAGS_MWI_OKAY
;
569 int device
, maxndevs
;
573 maxndevs
= pci_bus_maxdevs(NULL
, 0);
575 for (device
= 0; device
< maxndevs
; device
++) {
576 tag
= pci_make_tag(NULL
, 0, device
, 0);
577 id
= pci_conf_read(NULL
, tag
, PCI_ID_REG
);
579 /* Invalid vendor ID value? */
580 if (PCI_VENDOR(id
) == PCI_VENDOR_INVALID
)
582 /* XXX Not invalid, but we've done this ~forever. */
583 if (PCI_VENDOR(id
) == 0)
586 switch (PCI_VENDOR(id
)) {
588 switch (PCI_PRODUCT(id
)) {
589 case PCI_PRODUCT_SIS_85C496
:
599 printf("Warning: broken PCI-Host bridge detected; "
600 "disabling memory-mapped access\n");
601 rval
&= ~(PCI_FLAGS_MEM_ENABLED
|PCI_FLAGS_MRL_OKAY
|PCI_FLAGS_MRM_OKAY
|
607 pci_device_foreach(pci_chipset_tag_t pc
, int maxbus
,
608 void (*func
)(pci_chipset_tag_t
, pcitag_t
, void *), void *context
)
610 pci_device_foreach_min(pc
, 0, maxbus
, func
, context
);
614 pci_device_foreach_min(pci_chipset_tag_t pc
, int minbus
, int maxbus
,
615 void (*func
)(pci_chipset_tag_t
, pcitag_t
, void *), void *context
)
617 const struct pci_quirkdata
*qd
;
618 int bus
, device
, function
, maxdevs
, nfuncs
;
622 for (bus
= minbus
; bus
<= maxbus
; bus
++) {
623 maxdevs
= pci_bus_maxdevs(pc
, bus
);
624 for (device
= 0; device
< maxdevs
; device
++) {
625 tag
= pci_make_tag(pc
, bus
, device
, 0);
626 id
= pci_conf_read(pc
, tag
, PCI_ID_REG
);
628 /* Invalid vendor ID value? */
629 if (PCI_VENDOR(id
) == PCI_VENDOR_INVALID
)
631 /* XXX Not invalid, but we've done this ~forever. */
632 if (PCI_VENDOR(id
) == 0)
635 qd
= pci_lookup_quirkdata(PCI_VENDOR(id
),
638 bhlcr
= pci_conf_read(pc
, tag
, PCI_BHLC_REG
);
639 if (PCI_HDRTYPE_MULTIFN(bhlcr
) ||
641 (qd
->quirks
& PCI_QUIRK_MULTIFUNCTION
) != 0))
646 for (function
= 0; function
< nfuncs
; function
++) {
647 tag
= pci_make_tag(pc
, bus
, device
, function
);
648 id
= pci_conf_read(pc
, tag
, PCI_ID_REG
);
650 /* Invalid vendor ID value? */
651 if (PCI_VENDOR(id
) == PCI_VENDOR_INVALID
)
654 * XXX Not invalid, but we've done this
657 if (PCI_VENDOR(id
) == 0)
659 (*func
)(pc
, tag
, context
);
666 pci_bridge_foreach(pci_chipset_tag_t pc
, int minbus
, int maxbus
,
667 void (*func
)(pci_chipset_tag_t
, pcitag_t
, void *), void *ctx
)
669 struct pci_bridge_hook_arg bridge_hook
;
671 bridge_hook
.func
= func
;
672 bridge_hook
.arg
= ctx
;
674 pci_device_foreach_min(pc
, minbus
, maxbus
, pci_bridge_hook
,
679 pci_bridge_hook(pci_chipset_tag_t pc
, pcitag_t tag
, void *ctx
)
681 struct pci_bridge_hook_arg
*bridge_hook
= (void *)ctx
;
684 reg
= pci_conf_read(pc
, tag
, PCI_CLASS_REG
);
685 if (PCI_CLASS(reg
) == PCI_CLASS_BRIDGE
&&
686 (PCI_SUBCLASS(reg
) == PCI_SUBCLASS_BRIDGE_PCI
||
687 PCI_SUBCLASS(reg
) == PCI_SUBCLASS_BRIDGE_CARDBUS
)) {
688 (*bridge_hook
->func
)(pc
, tag
, bridge_hook
->arg
);