pci.c revision 1.40.6.1 1 /* $NetBSD: pci.c,v 1.40.6.1 2000/06/27 14:32:06 he Exp $ */
2
3 /*
4 * Copyright (c) 1995, 1996, 1997, 1998
5 * Christopher G. Demetriou. All rights reserved.
6 * Copyright (c) 1994 Charles M. 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 M. 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 #ifdef PCI_CONFIG_DUMP
47 int pci_config_dump = 1;
48 #else
49 int pci_config_dump = 0;
50 #endif
51
52 int pcimatch __P((struct device *, struct cfdata *, void *));
53 void pciattach __P((struct device *, struct device *, void *));
54
55 struct pci_softc {
56 struct device sc_dev;
57 bus_space_tag_t sc_iot, sc_memt;
58 bus_dma_tag_t sc_dmat;
59 pci_chipset_tag_t sc_pc;
60 int sc_bus, sc_maxndevs;
61 u_int sc_intrswiz;
62 pcitag_t sc_intrtag;
63 int sc_flags;
64 };
65
66 struct cfattach pci_ca = {
67 sizeof(struct pci_softc), pcimatch, pciattach
68 };
69
70 void pci_probe_bus __P((struct device *));
71 int pciprint __P((void *, const char *));
72 int pcisubmatch __P((struct device *, struct cfdata *, void *));
73
74 /*
75 * Important note about PCI-ISA bridges:
76 *
77 * Callbacks are used to configure these devices so that ISA/EISA bridges
78 * can attach their child busses after PCI configuration is done.
79 *
80 * This works because:
81 * (1) there can be at most one ISA/EISA bridge per PCI bus, and
82 * (2) any ISA/EISA bridges must be attached to primary PCI
83 * busses (i.e. bus zero).
84 *
85 * That boils down to: there can only be one of these outstanding
86 * at a time, it is cleared when configuring PCI bus 0 before any
87 * subdevices have been found, and it is run after all subdevices
88 * of PCI bus 0 have been found.
89 *
90 * This is needed because there are some (legacy) PCI devices which
91 * can show up as ISA/EISA devices as well (the prime example of which
92 * are VGA controllers). If you attach ISA from a PCI-ISA/EISA bridge,
93 * and the bridge is seen before the video board is, the board can show
94 * up as an ISA device, and that can (bogusly) complicate the PCI device's
95 * attach code, or make the PCI device not be properly attached at all.
96 *
97 * We use the generic config_defer() facility to achieve this.
98 */
99
100 int
101 pcimatch(parent, cf, aux)
102 struct device *parent;
103 struct cfdata *cf;
104 void *aux;
105 {
106 struct pcibus_attach_args *pba = aux;
107
108 if (strcmp(pba->pba_busname, cf->cf_driver->cd_name))
109 return (0);
110
111 /* Check the locators */
112 if (cf->pcibuscf_bus != PCIBUS_UNK_BUS &&
113 cf->pcibuscf_bus != pba->pba_bus)
114 return (0);
115
116 /* sanity */
117 if (pba->pba_bus < 0 || pba->pba_bus > 255)
118 return (0);
119
120 /*
121 * XXX check other (hardware?) indicators
122 */
123
124 return 1;
125 }
126
127 void
128 pci_probe_bus(self)
129 struct device *self;
130 {
131 struct pci_softc *sc = (struct pci_softc *)self;
132 bus_space_tag_t iot, memt;
133 pci_chipset_tag_t pc;
134 const struct pci_quirkdata *qd;
135 int bus, device, maxndevs, function, nfunctions;
136
137 iot = sc->sc_iot;
138 memt = sc->sc_memt;
139 pc = sc->sc_pc;
140 bus = sc->sc_bus;
141 maxndevs = sc->sc_maxndevs;
142
143 for (device = 0; device < maxndevs; device++) {
144 pcitag_t tag;
145 pcireg_t id, class, intr, bhlcr, csr;
146 struct pci_attach_args pa;
147 int pin;
148
149 tag = pci_make_tag(pc, bus, device, 0);
150 id = pci_conf_read(pc, tag, PCI_ID_REG);
151
152 /* Invalid vendor ID value? */
153 if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
154 continue;
155 /* XXX Not invalid, but we've done this ~forever. */
156 if (PCI_VENDOR(id) == 0)
157 continue;
158
159 qd = pci_lookup_quirkdata(PCI_VENDOR(id), PCI_PRODUCT(id));
160
161 bhlcr = pci_conf_read(pc, tag, PCI_BHLC_REG);
162 if (PCI_HDRTYPE_MULTIFN(bhlcr) ||
163 (qd != NULL &&
164 (qd->quirks & PCI_QUIRK_MULTIFUNCTION) != 0))
165 nfunctions = 8;
166 else
167 nfunctions = 1;
168
169 for (function = 0; function < nfunctions; function++) {
170 tag = pci_make_tag(pc, bus, device, function);
171 id = pci_conf_read(pc, tag, PCI_ID_REG);
172 csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
173 class = pci_conf_read(pc, tag, PCI_CLASS_REG);
174 intr = pci_conf_read(pc, tag, PCI_INTERRUPT_REG);
175
176 /* Invalid vendor ID value? */
177 if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
178 continue;
179 /* XXX Not invalid, but we've done this ~forever. */
180 if (PCI_VENDOR(id) == 0)
181 continue;
182
183 pa.pa_iot = iot;
184 pa.pa_memt = memt;
185 pa.pa_dmat = sc->sc_dmat;
186 pa.pa_pc = pc;
187 pa.pa_device = device;
188 pa.pa_function = function;
189 pa.pa_tag = tag;
190 pa.pa_id = id;
191 pa.pa_class = class;
192
193 /* set up memory and I/O enable flags as appropriate */
194 pa.pa_flags = 0;
195 if ((sc->sc_flags & PCI_FLAGS_IO_ENABLED) &&
196 (csr & PCI_COMMAND_IO_ENABLE))
197 pa.pa_flags |= PCI_FLAGS_IO_ENABLED;
198 if ((sc->sc_flags & PCI_FLAGS_MEM_ENABLED) &&
199 (csr & PCI_COMMAND_MEM_ENABLE))
200 pa.pa_flags |= PCI_FLAGS_MEM_ENABLED;
201
202 if (bus == 0) {
203 pa.pa_intrswiz = 0;
204 pa.pa_intrtag = tag;
205 } else {
206 pa.pa_intrswiz = sc->sc_intrswiz + device;
207 pa.pa_intrtag = sc->sc_intrtag;
208 }
209 pin = PCI_INTERRUPT_PIN(intr);
210 if (pin == PCI_INTERRUPT_PIN_NONE) {
211 /* no interrupt */
212 pa.pa_intrpin = 0;
213 } else {
214 /*
215 * swizzle it based on the number of
216 * busses we're behind and our device
217 * number.
218 */
219 pa.pa_intrpin = /* XXX */
220 ((pin + pa.pa_intrswiz - 1) % 4) + 1;
221 }
222 pa.pa_intrline = PCI_INTERRUPT_LINE(intr);
223
224 config_found_sm(self, &pa, pciprint, pcisubmatch);
225 }
226 }
227 }
228
229 void
230 pciattach(parent, self, aux)
231 struct device *parent, *self;
232 void *aux;
233 {
234 struct pcibus_attach_args *pba = aux;
235 struct pci_softc *sc = (struct pci_softc *)self;
236 int io_enabled, mem_enabled;
237
238 pci_attach_hook(parent, self, pba);
239 printf("\n");
240
241 io_enabled = (pba->pba_flags & PCI_FLAGS_IO_ENABLED);
242 mem_enabled = (pba->pba_flags & PCI_FLAGS_MEM_ENABLED);
243
244 if (io_enabled == 0 && mem_enabled == 0) {
245 printf("%s: no spaces enabled!\n", self->dv_xname);
246 return;
247 }
248
249 printf("%s: ", self->dv_xname);
250 if (io_enabled)
251 printf("i/o enabled");
252 if (mem_enabled) {
253 if (io_enabled)
254 printf(", ");
255 printf("memory enabled");
256 }
257 printf("\n");
258
259 sc->sc_iot = pba->pba_iot;
260 sc->sc_memt = pba->pba_memt;
261 sc->sc_dmat = pba->pba_dmat;
262 sc->sc_pc = pba->pba_pc;
263 sc->sc_bus = pba->pba_bus;
264 sc->sc_maxndevs = pci_bus_maxdevs(pba->pba_pc, pba->pba_bus);
265 sc->sc_intrswiz = pba->pba_intrswiz;
266 sc->sc_intrtag = pba->pba_intrtag;
267 sc->sc_flags = pba->pba_flags;
268
269 pci_probe_bus(self);
270 }
271
272 int
273 pciprint(aux, pnp)
274 void *aux;
275 const char *pnp;
276 {
277 register struct pci_attach_args *pa = aux;
278 char devinfo[256];
279 const struct pci_quirkdata *qd;
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 (pci_config_dump) {
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 }
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 int
337 pci_get_capability(pc, tag, capid, offset, value)
338 pci_chipset_tag_t pc;
339 pcitag_t tag;
340 int capid;
341 int *offset;
342 pcireg_t *value;
343 {
344 pcireg_t reg;
345 unsigned int ofs;
346
347 reg = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
348 if (!(reg & PCI_STATUS_CAPLIST_SUPPORT))
349 return (0);
350
351 ofs = PCI_CAPLIST_PTR(pci_conf_read(pc, tag, PCI_CAPLISTPTR_REG));
352 while (ofs != 0) {
353 #ifdef DIAGNOSTIC
354 if ((ofs & 3) || (ofs < 0x40))
355 panic("pci_get_capability");
356 #endif
357 reg = pci_conf_read(pc, tag, ofs);
358 if (PCI_CAPLIST_CAP(reg) == capid) {
359 if (offset)
360 *offset = ofs;
361 if (value)
362 *value = reg;
363 return (1);
364 }
365 ofs = PCI_CAPLIST_NEXT(reg);
366 }
367
368 return (0);
369 }
370