pci.c revision 1.37 1 /* $NetBSD: pci.c,v 1.37 1998/05/31 06:05:28 cgd Exp $ */
2
3 /*
4 * Copyright (c) 1995, 1996, 1997, 1998
5 * Christopher G. Demetriou. All rights reserved.
6 * Copyright (c) 1994 Charles Hannum. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Charles Hannum.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * PCI bus autoconfiguration.
36 */
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/device.h>
41
42 #include <dev/pci/pcireg.h>
43 #include <dev/pci/pcivar.h>
44 #include <dev/pci/pcidevs.h>
45
46 int pcimatch __P((struct device *, struct cfdata *, void *));
47 void pciattach __P((struct device *, struct device *, void *));
48
49 struct pci_softc {
50 struct device sc_dev;
51 bus_space_tag_t sc_iot, sc_memt;
52 bus_dma_tag_t sc_dmat;
53 pci_chipset_tag_t sc_pc;
54 int sc_bus, sc_maxndevs;
55 u_int sc_intrswiz;
56 pcitag_t sc_intrtag;
57 int sc_flags;
58 };
59
60 struct cfattach pci_ca = {
61 sizeof(struct pci_softc), pcimatch, pciattach
62 };
63
64 void pci_probe_bus __P((struct device *));
65 int pciprint __P((void *, const char *));
66 int pcisubmatch __P((struct device *, struct cfdata *, void *));
67
68 /*
69 * Callback so that ISA/EISA bridges can attach their child busses
70 * after PCI configuration is done.
71 *
72 * This works because:
73 * (1) there can be at most one ISA/EISA bridge per PCI bus, and
74 * (2) any ISA/EISA bridges must be attached to primary PCI
75 * busses (i.e. bus zero).
76 *
77 * That boils down to: there can only be one of these outstanding
78 * at a time, it is cleared when configuring PCI bus 0 before any
79 * subdevices have been found, and it is run after all subdevices
80 * of PCI bus 0 have been found.
81 *
82 * This is needed because there are some (legacy) PCI devices which
83 * can show up as ISA/EISA devices as well (the prime example of which
84 * are VGA controllers). If you attach ISA from a PCI-ISA/EISA bridge,
85 * and the bridge is seen before the video board is, the board can show
86 * up as an ISA device, and that can (bogusly) complicate the PCI device's
87 * attach code, or make the PCI device not be properly attached at all.
88 */
89 static void (*pci_isa_bridge_callback) __P((void *));
90 static void *pci_isa_bridge_callback_arg;
91
92 int
93 pcimatch(parent, cf, aux)
94 struct device *parent;
95 struct cfdata *cf;
96 void *aux;
97 {
98 struct pcibus_attach_args *pba = aux;
99
100 if (strcmp(pba->pba_busname, cf->cf_driver->cd_name))
101 return (0);
102
103 /* Check the locators */
104 if (cf->pcibuscf_bus != PCIBUS_UNK_BUS &&
105 cf->pcibuscf_bus != pba->pba_bus)
106 return (0);
107
108 /* sanity */
109 if (pba->pba_bus < 0 || pba->pba_bus > 255)
110 return (0);
111
112 /*
113 * XXX check other (hardware?) indicators
114 */
115
116 return 1;
117 }
118
119 void
120 pci_probe_bus(self)
121 struct device *self;
122 {
123 struct pci_softc *sc = (struct pci_softc *)self;
124 bus_space_tag_t iot, memt;
125 pci_chipset_tag_t pc;
126 const struct pci_quirkdata *qd;
127 int bus, device, maxndevs, function, nfunctions;
128
129 iot = sc->sc_iot;
130 memt = sc->sc_memt;
131 pc = sc->sc_pc;
132 bus = sc->sc_bus;
133 maxndevs = sc->sc_maxndevs;
134
135 if (bus == 0)
136 pci_isa_bridge_callback = NULL;
137
138 for (device = 0; device < maxndevs; device++) {
139 pcitag_t tag;
140 pcireg_t id, class, intr, bhlcr, csr;
141 struct pci_attach_args pa;
142 int pin;
143
144 tag = pci_make_tag(pc, bus, device, 0);
145 id = pci_conf_read(pc, tag, PCI_ID_REG);
146
147 /* Invalid vendor ID value? */
148 if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
149 continue;
150 /* XXX Not invalid, but we've done this ~forever. */
151 if (PCI_VENDOR(id) == 0)
152 continue;
153
154 qd = pci_lookup_quirkdata(PCI_VENDOR(id), PCI_PRODUCT(id));
155
156 bhlcr = pci_conf_read(pc, tag, PCI_BHLC_REG);
157 if (PCI_HDRTYPE_MULTIFN(bhlcr) ||
158 (qd != NULL &&
159 (qd->quirks & PCI_QUIRK_MULTIFUNCTION) != 0))
160 nfunctions = 8;
161 else
162 nfunctions = 1;
163
164 for (function = 0; function < nfunctions; function++) {
165 tag = pci_make_tag(pc, bus, device, function);
166 id = pci_conf_read(pc, tag, PCI_ID_REG);
167 csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
168 class = pci_conf_read(pc, tag, PCI_CLASS_REG);
169 intr = pci_conf_read(pc, tag, PCI_INTERRUPT_REG);
170
171 /* Invalid vendor ID value? */
172 if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
173 continue;
174 /* XXX Not invalid, but we've done this ~forever. */
175 if (PCI_VENDOR(id) == 0)
176 continue;
177
178 pa.pa_iot = iot;
179 pa.pa_memt = memt;
180 pa.pa_dmat = sc->sc_dmat;
181 pa.pa_pc = pc;
182 pa.pa_device = device;
183 pa.pa_function = function;
184 pa.pa_tag = tag;
185 pa.pa_id = id;
186 pa.pa_class = class;
187
188 /* set up memory and I/O enable flags as appropriate */
189 pa.pa_flags = 0;
190 if ((sc->sc_flags & PCI_FLAGS_IO_ENABLED) &&
191 (csr & PCI_COMMAND_IO_ENABLE))
192 pa.pa_flags |= PCI_FLAGS_IO_ENABLED;
193 if ((sc->sc_flags & PCI_FLAGS_MEM_ENABLED) &&
194 (csr & PCI_COMMAND_MEM_ENABLE))
195 pa.pa_flags |= PCI_FLAGS_MEM_ENABLED;
196
197 if (bus == 0) {
198 pa.pa_intrswiz = 0;
199 pa.pa_intrtag = tag;
200 } else {
201 pa.pa_intrswiz = sc->sc_intrswiz + device;
202 pa.pa_intrtag = sc->sc_intrtag;
203 }
204 pin = PCI_INTERRUPT_PIN(intr);
205 if (pin == PCI_INTERRUPT_PIN_NONE) {
206 /* no interrupt */
207 pa.pa_intrpin = 0;
208 } else {
209 /*
210 * swizzle it based on the number of
211 * busses we're behind and our device
212 * number.
213 */
214 pa.pa_intrpin = /* XXX */
215 ((pin + pa.pa_intrswiz - 1) % 4) + 1;
216 }
217 pa.pa_intrline = PCI_INTERRUPT_LINE(intr);
218
219 config_found_sm(self, &pa, pciprint, pcisubmatch);
220 }
221 }
222
223 if (bus == 0 && pci_isa_bridge_callback != NULL)
224 (*pci_isa_bridge_callback)(pci_isa_bridge_callback_arg);
225 }
226
227 void
228 pciattach(parent, self, aux)
229 struct device *parent, *self;
230 void *aux;
231 {
232 struct pcibus_attach_args *pba = aux;
233 struct pci_softc *sc = (struct pci_softc *)self;
234 int io_enabled, mem_enabled;
235
236 pci_attach_hook(parent, self, pba);
237 printf("\n");
238
239 io_enabled = (pba->pba_flags & PCI_FLAGS_IO_ENABLED);
240 mem_enabled = (pba->pba_flags & PCI_FLAGS_MEM_ENABLED);
241
242 if (io_enabled == 0 && mem_enabled == 0) {
243 printf("%s: no spaces enabled!\n", self->dv_xname);
244 return;
245 }
246
247 printf("%s: ", self->dv_xname);
248 if (io_enabled)
249 printf("i/o enabled");
250 if (mem_enabled) {
251 if (io_enabled)
252 printf(", ");
253 printf("memory enabled");
254 }
255 printf("\n");
256
257 sc->sc_iot = pba->pba_iot;
258 sc->sc_memt = pba->pba_memt;
259 sc->sc_dmat = pba->pba_dmat;
260 sc->sc_pc = pba->pba_pc;
261 sc->sc_bus = pba->pba_bus;
262 sc->sc_maxndevs = pci_bus_maxdevs(pba->pba_pc, pba->pba_bus);
263 sc->sc_intrswiz = pba->pba_intrswiz;
264 sc->sc_intrtag = pba->pba_intrtag;
265 sc->sc_flags = pba->pba_flags;
266
267 pci_probe_bus(self);
268 }
269
270 int
271 pciprint(aux, pnp)
272 void *aux;
273 const char *pnp;
274 {
275 register struct pci_attach_args *pa = aux;
276 char devinfo[256];
277 #if 0
278 const struct pci_quirkdata *qd;
279 #endif
280
281 if (pnp) {
282 pci_devinfo(pa->pa_id, pa->pa_class, 1, devinfo);
283 printf("%s at %s", devinfo, pnp);
284 }
285 printf(" dev %d function %d", pa->pa_device, pa->pa_function);
286 #if 0
287 printf(": ");
288 pci_conf_print(pa->pa_pc, pa->pa_tag, NULL);
289 if (!pnp)
290 pci_devinfo(pa->pa_id, pa->pa_class, 1, devinfo);
291 printf("%s at %s", devinfo, pnp ? pnp : "?");
292 printf(" dev %d function %d (", pa->pa_device, pa->pa_function);
293 #ifdef __i386__
294 printf("tag %#lx, intrtag %#lx, intrswiz %#lx, intrpin %#lx",
295 *(long *)&pa->pa_tag, *(long *)&pa->pa_intrtag,
296 (long)pa->pa_intrswiz, (long)pa->pa_intrpin);
297 #else
298 printf("tag %#lx, intrtag %#lx, intrswiz %#lx, intrpin %#lx",
299 (long)pa->pa_tag, (long)pa->pa_intrtag, (long)pa->pa_intrswiz,
300 (long)pa->pa_intrpin);
301 #endif
302 printf(", i/o %s, mem %s,",
303 pa->pa_flags & PCI_FLAGS_IO_ENABLED ? "on" : "off",
304 pa->pa_flags & PCI_FLAGS_MEM_ENABLED ? "on" : "off");
305 qd = pci_lookup_quirkdata(PCI_VENDOR(pa->pa_id),
306 PCI_PRODUCT(pa->pa_id));
307 if (qd == NULL) {
308 printf(" no quirks");
309 } else {
310 bitmask_snprintf(qd->quirks,
311 "\20\1multifn", devinfo, sizeof (devinfo));
312 printf(" quirks %s", devinfo);
313 }
314 printf(")");
315 #endif
316 return (UNCONF);
317 }
318
319 int
320 pcisubmatch(parent, cf, aux)
321 struct device *parent;
322 struct cfdata *cf;
323 void *aux;
324 {
325 struct pci_attach_args *pa = aux;
326
327 if (cf->pcicf_dev != PCI_UNK_DEV &&
328 cf->pcicf_dev != pa->pa_device)
329 return 0;
330 if (cf->pcicf_function != PCI_UNK_FUNCTION &&
331 cf->pcicf_function != pa->pa_function)
332 return 0;
333 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
334 }
335
336 void
337 set_pci_isa_bridge_callback(fn, arg)
338 void (*fn) __P((void *));
339 void *arg;
340 {
341
342 if (pci_isa_bridge_callback != NULL)
343 panic("set_pci_isa_bridge_callback");
344 pci_isa_bridge_callback = fn;
345 pci_isa_bridge_callback_arg = arg;
346 }
347