pci_bus_fixup.c revision 1.1.22.2 1 1.1.22.2 yamt /* $NetBSD: pci_bus_fixup.c,v 1.1.22.2 2009/05/04 08:12:10 yamt Exp $ */
2 1.1.22.2 yamt
3 1.1.22.2 yamt /*
4 1.1.22.2 yamt * Copyright (c) 1999, by UCHIYAMA Yasushi
5 1.1.22.2 yamt * All rights reserved.
6 1.1.22.2 yamt *
7 1.1.22.2 yamt * Redistribution and use in source and binary forms, with or without
8 1.1.22.2 yamt * modification, are permitted provided that the following conditions
9 1.1.22.2 yamt * are met:
10 1.1.22.2 yamt * 1. Redistributions of source code must retain the above copyright
11 1.1.22.2 yamt * notice, this list of conditions and the following disclaimer.
12 1.1.22.2 yamt * 2. The name of the developer may NOT be used to endorse or promote products
13 1.1.22.2 yamt * derived from this software without specific prior written permission.
14 1.1.22.2 yamt *
15 1.1.22.2 yamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 1.1.22.2 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 1.1.22.2 yamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 1.1.22.2 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 1.1.22.2 yamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.1.22.2 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 1.1.22.2 yamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1.22.2 yamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.1.22.2 yamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1.22.2 yamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1.22.2 yamt * SUCH DAMAGE.
26 1.1.22.2 yamt */
27 1.1.22.2 yamt
28 1.1.22.2 yamt /*
29 1.1.22.2 yamt * PCI bus renumbering support.
30 1.1.22.2 yamt */
31 1.1.22.2 yamt
32 1.1.22.2 yamt #include <sys/cdefs.h>
33 1.1.22.2 yamt __KERNEL_RCSID(0, "$NetBSD: pci_bus_fixup.c,v 1.1.22.2 2009/05/04 08:12:10 yamt Exp $");
34 1.1.22.2 yamt
35 1.1.22.2 yamt #include <sys/param.h>
36 1.1.22.2 yamt #include <sys/systm.h>
37 1.1.22.2 yamt #include <sys/kernel.h>
38 1.1.22.2 yamt
39 1.1.22.2 yamt #include <machine/bus.h>
40 1.1.22.2 yamt
41 1.1.22.2 yamt #include <dev/pci/pcireg.h>
42 1.1.22.2 yamt #include <dev/pci/pcivar.h>
43 1.1.22.2 yamt #include <dev/pci/pcidevs.h>
44 1.1.22.2 yamt #include <dev/pci/ppbreg.h>
45 1.1.22.2 yamt
46 1.1.22.2 yamt #include <x86/pci/pci_bus_fixup.h>
47 1.1.22.2 yamt
48 1.1.22.2 yamt /* this array lists the parent for each bus number */
49 1.1.22.2 yamt int pci_bus_parent[256];
50 1.1.22.2 yamt
51 1.1.22.2 yamt /* this array lists the pcitag to program each bridge */
52 1.1.22.2 yamt pcitag_t pci_bus_tag[256];
53 1.1.22.2 yamt
54 1.1.22.2 yamt static void pci_bridge_reset(pci_chipset_tag_t, pcitag_t, void *);
55 1.1.22.2 yamt
56 1.1.22.2 yamt int
57 1.1.22.2 yamt pci_bus_fixup(pci_chipset_tag_t pc, int bus)
58 1.1.22.2 yamt {
59 1.1.22.2 yamt static int bus_total;
60 1.1.22.2 yamt static int bridge_cnt;
61 1.1.22.2 yamt int device, maxdevs, function, nfuncs, bridge, bus_max, bus_sub;
62 1.1.22.2 yamt const struct pci_quirkdata *qd;
63 1.1.22.2 yamt pcireg_t reg;
64 1.1.22.2 yamt pcitag_t tag;
65 1.1.22.2 yamt
66 1.1.22.2 yamt bus_max = bus;
67 1.1.22.2 yamt bus_sub = 0;
68 1.1.22.2 yamt
69 1.1.22.2 yamt if (++bus_total > 256)
70 1.1.22.2 yamt panic("pci_bus_fixup: more than 256 PCI busses?");
71 1.1.22.2 yamt
72 1.1.22.2 yamt /* Reset bridge configuration on this bus */
73 1.1.22.2 yamt pci_bridge_foreach(pc, bus, bus, pci_bridge_reset, 0);
74 1.1.22.2 yamt
75 1.1.22.2 yamt maxdevs = pci_bus_maxdevs(pc, bus);
76 1.1.22.2 yamt
77 1.1.22.2 yamt for (device = 0; device < maxdevs; device++) {
78 1.1.22.2 yamt tag = pci_make_tag(pc, bus, device, 0);
79 1.1.22.2 yamt reg = pci_conf_read(pc, tag, PCI_ID_REG);
80 1.1.22.2 yamt
81 1.1.22.2 yamt /* Invalid vendor ID value? */
82 1.1.22.2 yamt if (PCI_VENDOR(reg) == PCI_VENDOR_INVALID)
83 1.1.22.2 yamt continue;
84 1.1.22.2 yamt /* XXX Not invalid, but we've done this ~forever. */
85 1.1.22.2 yamt if (PCI_VENDOR(reg) == 0)
86 1.1.22.2 yamt continue;
87 1.1.22.2 yamt
88 1.1.22.2 yamt qd = pci_lookup_quirkdata(PCI_VENDOR(reg), PCI_PRODUCT(reg));
89 1.1.22.2 yamt
90 1.1.22.2 yamt reg = pci_conf_read(pc, tag, PCI_BHLC_REG);
91 1.1.22.2 yamt if (PCI_HDRTYPE_MULTIFN(reg) ||
92 1.1.22.2 yamt (qd != NULL &&
93 1.1.22.2 yamt (qd->quirks & PCI_QUIRK_MULTIFUNCTION) != 0))
94 1.1.22.2 yamt nfuncs = 8;
95 1.1.22.2 yamt else
96 1.1.22.2 yamt nfuncs = 1;
97 1.1.22.2 yamt
98 1.1.22.2 yamt for (function = 0; function < nfuncs; function++) {
99 1.1.22.2 yamt tag = pci_make_tag(pc, bus, device, function);
100 1.1.22.2 yamt reg = pci_conf_read(pc, tag, PCI_ID_REG);
101 1.1.22.2 yamt
102 1.1.22.2 yamt /* Invalid vendor ID value? */
103 1.1.22.2 yamt if (PCI_VENDOR(reg) == PCI_VENDOR_INVALID)
104 1.1.22.2 yamt continue;
105 1.1.22.2 yamt /* XXX Not invalid, but we've done this ~forever. */
106 1.1.22.2 yamt if (PCI_VENDOR(reg) == 0)
107 1.1.22.2 yamt continue;
108 1.1.22.2 yamt
109 1.1.22.2 yamt aprint_debug("PCI fixup examining %02x:%02x\n",
110 1.1.22.2 yamt PCI_VENDOR(reg), PCI_PRODUCT(reg));
111 1.1.22.2 yamt
112 1.1.22.2 yamt reg = pci_conf_read(pc, tag, PCI_CLASS_REG);
113 1.1.22.2 yamt if (PCI_CLASS(reg) == PCI_CLASS_BRIDGE &&
114 1.1.22.2 yamt (PCI_SUBCLASS(reg) == PCI_SUBCLASS_BRIDGE_PCI ||
115 1.1.22.2 yamt PCI_SUBCLASS(reg) == PCI_SUBCLASS_BRIDGE_CARDBUS)) {
116 1.1.22.2 yamt /* Assign the bridge #. */
117 1.1.22.2 yamt bridge = bridge_cnt++;
118 1.1.22.2 yamt
119 1.1.22.2 yamt /* Assign the bridge's secondary bus #. */
120 1.1.22.2 yamt bus_max++;
121 1.1.22.2 yamt
122 1.1.22.2 yamt reg = pci_conf_read(pc, tag, PPB_REG_BUSINFO);
123 1.1.22.2 yamt reg &= 0xff000000;
124 1.1.22.2 yamt reg |= bus | (bus_max << 8) | (0xff << 16);
125 1.1.22.2 yamt pci_conf_write(pc, tag, PPB_REG_BUSINFO, reg);
126 1.1.22.2 yamt
127 1.1.22.2 yamt /* Scan subordinate bus. */
128 1.1.22.2 yamt bus_sub = pci_bus_fixup(pc, bus_max);
129 1.1.22.2 yamt
130 1.1.22.2 yamt /* Configure the bridge. */
131 1.1.22.2 yamt reg &= 0xff000000;
132 1.1.22.2 yamt reg |= bus | (bus_max << 8) | (bus_sub << 16);
133 1.1.22.2 yamt pci_conf_write(pc, tag, PPB_REG_BUSINFO, reg);
134 1.1.22.2 yamt
135 1.1.22.2 yamt /* record relationship */
136 1.1.22.2 yamt pci_bus_parent[bus_max]=bus;
137 1.1.22.2 yamt
138 1.1.22.2 yamt pci_bus_tag[bus_max]=tag;
139 1.1.22.2 yamt
140 1.1.22.2 yamt aprint_debug("PCI bridge %d: primary %d, "
141 1.1.22.2 yamt "secondary %d, subordinate %d\n",
142 1.1.22.2 yamt bridge, bus, bus_max, bus_sub);
143 1.1.22.2 yamt
144 1.1.22.2 yamt /* Next bridge's secondary bus #. */
145 1.1.22.2 yamt bus_max = (bus_sub > bus_max) ?
146 1.1.22.2 yamt bus_sub : bus_max;
147 1.1.22.2 yamt }
148 1.1.22.2 yamt }
149 1.1.22.2 yamt }
150 1.1.22.2 yamt
151 1.1.22.2 yamt return (bus_max); /* last # of subordinate bus */
152 1.1.22.2 yamt }
153 1.1.22.2 yamt
154 1.1.22.2 yamt /* Reset bus-bridge configuration */
155 1.1.22.2 yamt void
156 1.1.22.2 yamt pci_bridge_reset(pci_chipset_tag_t pc, pcitag_t tag, void *ctx)
157 1.1.22.2 yamt {
158 1.1.22.2 yamt pcireg_t reg;
159 1.1.22.2 yamt
160 1.1.22.2 yamt reg = pci_conf_read(pc, tag, PPB_REG_BUSINFO);
161 1.1.22.2 yamt reg &= 0xff000000;
162 1.1.22.2 yamt reg |= 0x00ffffff; /* max bus # */
163 1.1.22.2 yamt pci_conf_write(pc, tag, PPB_REG_BUSINFO, reg);
164 1.1.22.2 yamt }
165