pci_machdep.c revision 1.31 1 /* $NetBSD: pci_machdep.c,v 1.31 2007/03/22 08:23:10 garbled Exp $ */
2
3 /*
4 * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved.
5 * Copyright (c) 1994 Charles M. Hannum. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Charles M. Hannum.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Machine-specific functions for PCI autoconfiguration.
35 *
36 * On PCs, there are two methods of generating PCI configuration cycles.
37 * We try to detect the appropriate mechanism for this machine and set
38 * up a few function pointers to access the correct method directly.
39 */
40
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: pci_machdep.c,v 1.31 2007/03/22 08:23:10 garbled Exp $");
43
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/time.h>
47 #include <sys/systm.h>
48 #include <sys/errno.h>
49 #include <sys/extent.h>
50 #include <sys/device.h>
51 #include <sys/malloc.h>
52
53 #include <uvm/uvm_extern.h>
54
55 #define _POWERPC_BUS_DMA_PRIVATE
56 #include <machine/bus.h>
57 #include <machine/intr.h>
58 #include <machine/platform.h>
59 #include <machine/pnp.h>
60
61 #include <dev/isa/isavar.h>
62
63 #include <dev/pci/pcivar.h>
64 #include <dev/pci/pcireg.h>
65 #include <dev/pci/pcidevs.h>
66 #include <dev/pci/pciconf.h>
67
68 /*
69 * PCI doesn't have any special needs; just use the generic versions
70 * of these functions.
71 */
72 struct powerpc_bus_dma_tag pci_bus_dma_tag = {
73 0, /* _bounce_thresh */
74 _bus_dmamap_create,
75 _bus_dmamap_destroy,
76 _bus_dmamap_load,
77 _bus_dmamap_load_mbuf,
78 _bus_dmamap_load_uio,
79 _bus_dmamap_load_raw,
80 _bus_dmamap_unload,
81 NULL, /* _dmamap_sync */
82 _bus_dmamem_alloc,
83 _bus_dmamem_free,
84 _bus_dmamem_map,
85 _bus_dmamem_unmap,
86 _bus_dmamem_mmap,
87 };
88
89 /* 0 == direct 1 == indirect */
90 int prep_pci_config_mode = 1;
91 extern struct prep_pci_chipset *prep_pct;
92
93 void
94 prep_pci_get_chipset_tag(pci_chipset_tag_t pc)
95 {
96 int i;
97
98 i = pci_chipset_tag_type();
99
100 if (i == PCIBridgeIndirect || i == PCIBridgeRS6K) {
101 prep_pci_config_mode = 1;
102 prep_pci_get_chipset_tag_indirect(pc);
103 } else if (i == PCIBridgeDirect) {
104 prep_pci_get_chipset_tag_direct(pc);
105 prep_pci_config_mode = 0;
106 } else
107 panic("Unknown PCI chipset tag configuration method");
108 }
109
110 int
111 prep_pci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
112 {
113 struct prep_pci_chipset_businfo *pbi;
114 prop_object_t busmax;
115
116 pbi = SIMPLEQ_FIRST(&prep_pct->pc_pbi);
117 while (busno--)
118 pbi = SIMPLEQ_NEXT(pbi, next);
119 if (pbi == NULL)
120 return 32;
121
122 busmax = prop_dictionary_get(pbi->pbi_properties,
123 "prep-pcibus-maxdevices");
124 if (busmax == NULL)
125 return 32;
126 else
127 return prop_number_integer_value(busmax);
128
129 return 32;
130 }
131
132 int
133 prep_pci_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
134 {
135 struct prep_pci_chipset_businfo *pbi;
136 prop_dictionary_t dict, devsub;
137 prop_object_t pinsub;
138 prop_number_t pbus;
139 int busno, bus, pin, line, swiz, dev, origdev, i;
140 char key[20];
141
142 pin = pa->pa_intrpin;
143 line = pa->pa_intrline;
144 bus = busno = pa->pa_bus;
145 swiz = pa->pa_intrswiz;
146 origdev = dev = pa->pa_device;
147 i = 0;
148
149 pbi = SIMPLEQ_FIRST(&prep_pct->pc_pbi);
150 while (busno--)
151 pbi = SIMPLEQ_NEXT(pbi, next);
152 KASSERT(pbi != NULL);
153
154 dict = prop_dictionary_get(pbi->pbi_properties, "prep-pci-intrmap");
155
156 if (dict != NULL)
157 i = prop_dictionary_count(dict);
158
159 if (dict == NULL || i == 0) {
160 /* We have a non-PReP bus. now it gets hard */
161 pbus = prop_dictionary_get(pbi->pbi_properties,
162 "prep-pcibus-parent");
163 if (pbus == NULL)
164 goto bad;
165 busno = prop_number_integer_value(pbus);
166 pbus = prop_dictionary_get(pbi->pbi_properties,
167 "prep-pcibus-rawdevnum");
168 dev = prop_number_integer_value(pbus);
169
170 /* now that we know the parent bus, we need to find it's pbi */
171 pbi = SIMPLEQ_FIRST(&prep_pct->pc_pbi);
172 while (busno--)
173 pbi = SIMPLEQ_NEXT(pbi, next);
174 KASSERT(pbi != NULL);
175
176 /* swizzle the pin */
177 pin = ((pin + origdev - 1) & 3) + 1;
178
179 /* now we have the pbi, ask for dict again */
180 dict = prop_dictionary_get(pbi->pbi_properties,
181 "prep-pci-intrmap");
182 if (dict == NULL)
183 goto bad;
184 }
185
186 /* No IRQ used. */
187 if (pin == 0)
188 goto bad;
189 if (pin > 4) {
190 printf("pci_intr_map: bad interrupt pin %d\n", pin);
191 goto bad;
192 }
193
194 sprintf(key, "devfunc-%d", dev);
195 devsub = prop_dictionary_get(dict, key);
196 if (devsub == NULL)
197 goto bad;
198 sprintf(key, "pin-%c", 'A' + (pin-1));
199 pinsub = prop_dictionary_get(devsub, key);
200 if (pinsub == NULL)
201 goto bad;
202 line = prop_number_integer_value(pinsub);
203
204 /*
205 * Section 6.2.4, `Miscellaneous Functions', says that 255 means
206 * `unknown' or `no connection' on a PC. We assume that a device with
207 * `no connection' either doesn't have an interrupt (in which case the
208 * pin number should be 0, and would have been noticed above), or
209 * wasn't configured by the BIOS (in which case we punt, since there's
210 * no real way we can know how the interrupt lines are mapped in the
211 * hardware).
212 *
213 * XXX
214 * Since IRQ 0 is only used by the clock, and we can't actually be sure
215 * that the BIOS did its job, we also recognize that as meaning that
216 * the BIOS has not configured the device.
217 */
218 if (line == 0 || line == 255) {
219 printf("pci_intr_map: no mapping for pin %c\n", '@' + pin);
220 goto bad;
221 } else {
222 if (line >= ICU_LEN) {
223 printf("pci_intr_map: bad interrupt line %d\n", line);
224 goto bad;
225 }
226 if (line == IRQ_SLAVE) {
227 printf("pci_intr_map: changed line 2 to line 9\n");
228 line = 9;
229 }
230 }
231
232 *ihp = line;
233 return 0;
234
235 bad:
236 *ihp = -1;
237 return 1;
238 }
239
240 const char *
241 prep_pci_intr_string(void *v, pci_intr_handle_t ih)
242 {
243 static char irqstr[8]; /* 4 + 2 + NULL + sanity */
244
245 if (ih == 0 || ih >= ICU_LEN || ih == IRQ_SLAVE)
246 panic("pci_intr_string: bogus handle 0x%x", ih);
247
248 sprintf(irqstr, "irq %d", ih);
249 return (irqstr);
250
251 }
252
253 const struct evcnt *
254 prep_pci_intr_evcnt(void *v, pci_intr_handle_t ih)
255 {
256
257 /* XXX for now, no evcnt parent reported */
258 return NULL;
259 }
260
261 void *
262 prep_pci_intr_establish(void *v, pci_intr_handle_t ih, int level,
263 int (*func)(void *), void *arg)
264 {
265
266 if (ih == 0 || ih >= ICU_LEN || ih == IRQ_SLAVE)
267 panic("pci_intr_establish: bogus handle 0x%x", ih);
268
269 return isa_intr_establish(NULL, ih, IST_LEVEL, level, func, arg);
270 }
271
272 void
273 prep_pci_intr_disestablish(void *v, void *cookie)
274 {
275
276 isa_intr_disestablish(NULL, cookie);
277 }
278
279 void
280 prep_pci_conf_interrupt(void *v, int bus, int dev, int pin,
281 int swiz, int *iline)
282 {
283 /* do nothing */
284 }
285
286 extern pcitag_t prep_pci_direct_make_tag(void *, int, int, int);
287 extern pcitag_t prep_pci_indirect_make_tag(void *, int, int, int);
288 extern pcireg_t prep_pci_direct_conf_read(void *, pcitag_t, int);
289 extern pcireg_t prep_pci_indirect_conf_read(void *, pcitag_t, int);
290
291 int
292 prep_pci_conf_hook(pci_chipset_tag_t pct, int bus, int dev, int func,
293 pcireg_t id)
294 {
295 struct prep_pci_chipset_businfo *pbi;
296 prop_number_t bmax, pbus;
297 pcitag_t tag;
298 pcireg_t class;
299
300 /*
301 * The P9100 board found in some IBM machines cannot be
302 * over-configured.
303 */
304 if (PCI_VENDOR(id) == PCI_VENDOR_WEITEK &&
305 PCI_PRODUCT(id) == PCI_PRODUCT_WEITEK_P9100)
306 return 0;
307
308 /* We have already mapped the MPIC2 if we have one, so leave it
309 alone */
310 if (PCI_VENDOR(id) == PCI_VENDOR_IBM &&
311 PCI_PRODUCT(id) == PCI_PRODUCT_IBM_MPIC2)
312 return 0;
313
314 if (PCI_VENDOR(id) == PCI_VENDOR_IBM &&
315 PCI_PRODUCT(id) == PCI_PRODUCT_IBM_MPIC)
316 return 0;
317
318 if (PCI_VENDOR(id) == PCI_VENDOR_INTEL &&
319 PCI_PRODUCT(id) == PCI_PRODUCT_INTEL_PCEB)
320 return 0;
321
322 if (PCI_VENDOR(id) == PCI_VENDOR_MOT &&
323 PCI_PRODUCT(id) == PCI_PRODUCT_MOT_RAVEN)
324 return (PCI_CONF_ALL & ~PCI_CONF_MAP_MEM);
325
326 /* NOTE, all device specific stuff must be above this line */
327 /* don't do this on the primary host bridge */
328 if (bus == 0 && dev == 0 && func == 0)
329 return PCI_CONF_DEFAULT;
330
331 if (prep_pci_config_mode) {
332 tag = prep_pci_indirect_make_tag(pct, bus, dev, func);
333 class = prep_pci_indirect_conf_read(pct, tag,
334 PCI_CLASS_REG);
335 } else {
336 tag = prep_pci_direct_make_tag(pct, bus, dev, func);
337 class = prep_pci_direct_conf_read(pct, tag,
338 PCI_CLASS_REG);
339 }
340
341 /*
342 * PCI bridges have special needs. We need to discover where they
343 * came from, and wire them appropriately.
344 */
345 if (PCI_CLASS(class) == PCI_CLASS_BRIDGE &&
346 PCI_SUBCLASS(class) == PCI_SUBCLASS_BRIDGE_PCI) {
347 pbi = malloc(sizeof(struct prep_pci_chipset_businfo), M_DEVBUF,
348 M_NOWAIT);
349 KASSERT(pbi != NULL);
350 pbi->pbi_properties = prop_dictionary_create();
351 KASSERT(pbi->pbi_properties != NULL);
352 setup_pciintr_map(pbi, bus, dev, func);
353
354 /* record the parent bus, and the parent device number */
355 pbus = prop_number_create_integer(bus);
356 prop_dictionary_set(pbi->pbi_properties, "prep-pcibus-parent",
357 pbus);
358 prop_object_release(pbus);
359 pbus = prop_number_create_integer(dev);
360 prop_dictionary_set(pbi->pbi_properties,
361 "prep-pcibus-rawdevnum", pbus);
362 prop_object_release(pbus);
363
364 /* now look for bus quirks */
365
366 if (PCI_VENDOR(id) == PCI_VENDOR_DEC &&
367 PCI_PRODUCT(id) == PCI_PRODUCT_DEC_21154) {
368 bmax = prop_number_create_integer(8);
369 KASSERT(bmax != NULL);
370 prop_dictionary_set(pbi->pbi_properties,
371 "prep-pcibus-maxdevices", bmax);
372 prop_object_release(bmax);
373 }
374
375 SIMPLEQ_INSERT_TAIL(&prep_pct->pc_pbi, pbi, next);
376 }
377
378 return (PCI_CONF_DEFAULT);
379 }
380