pci_machdep.c revision 1.30 1 /* $NetBSD: pci_machdep.c,v 1.30 2007/03/21 01:34:32 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.30 2007/03/21 01:34:32 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, 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 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 if (dict != NULL)
156 i = prop_dictionary_count(dict);
157
158 if (dict == NULL || i == 0) {
159 /* We have a non-PReP bus. now it gets hard */
160 pbus = prop_dictionary_get(pbi->pbi_properties,
161 "prep-pcibus-parent");
162 if (pbus == NULL)
163 goto bad;
164 busno = prop_number_integer_value(pbus);
165 pbus = prop_dictionary_get(pbi->pbi_properties,
166 "prep-pcibus-rawdevnum");
167 dev = prop_number_integer_value(pbus);
168
169 /* now that we know the parent bus, we need to find it's pbi */
170 pbi = SIMPLEQ_FIRST(&prep_pct->pc_pbi);
171 while (busno--)
172 pbi = SIMPLEQ_NEXT(pbi, next);
173 KASSERT(pbi != NULL);
174
175 /* set the pin to the rawpin */
176 pin = pa->pa_rawintrpin;
177 /* now we have the pbi, ask for dict again */
178 dict = prop_dictionary_get(pbi->pbi_properties,
179 "prep-pci-intrmap");
180 if (dict == NULL)
181 goto bad;
182 }
183
184 /* No IRQ used. */
185 if (pin == 0)
186 goto bad;
187 if (pin > 4) {
188 printf("pci_intr_map: bad interrupt pin %d\n", pin);
189 goto bad;
190 }
191
192 sprintf(key, "devfunc-%d", dev);
193 devsub = prop_dictionary_get(dict, key);
194 if (devsub == NULL)
195 goto bad;
196 sprintf(key, "pin-%c", 'A' + (pin-1));
197 pinsub = prop_dictionary_get(devsub, key);
198 if (pinsub == NULL)
199 goto bad;
200 line = prop_number_integer_value(pinsub);
201
202 /*
203 * Section 6.2.4, `Miscellaneous Functions', says that 255 means
204 * `unknown' or `no connection' on a PC. We assume that a device with
205 * `no connection' either doesn't have an interrupt (in which case the
206 * pin number should be 0, and would have been noticed above), or
207 * wasn't configured by the BIOS (in which case we punt, since there's
208 * no real way we can know how the interrupt lines are mapped in the
209 * hardware).
210 *
211 * XXX
212 * Since IRQ 0 is only used by the clock, and we can't actually be sure
213 * that the BIOS did its job, we also recognize that as meaning that
214 * the BIOS has not configured the device.
215 */
216 if (line == 0 || line == 255) {
217 printf("pci_intr_map: no mapping for pin %c\n", '@' + pin);
218 goto bad;
219 } else {
220 if (line >= ICU_LEN) {
221 printf("pci_intr_map: bad interrupt line %d\n", line);
222 goto bad;
223 }
224 if (line == IRQ_SLAVE) {
225 printf("pci_intr_map: changed line 2 to line 9\n");
226 line = 9;
227 }
228 }
229
230 *ihp = line;
231 return 0;
232
233 bad:
234 *ihp = -1;
235 return 1;
236 }
237
238 const char *
239 prep_pci_intr_string(void *v, pci_intr_handle_t ih)
240 {
241 static char irqstr[8]; /* 4 + 2 + NULL + sanity */
242
243 if (ih == 0 || ih >= ICU_LEN || ih == IRQ_SLAVE)
244 panic("pci_intr_string: bogus handle 0x%x", ih);
245
246 sprintf(irqstr, "irq %d", ih);
247 return (irqstr);
248
249 }
250
251 const struct evcnt *
252 prep_pci_intr_evcnt(void *v, pci_intr_handle_t ih)
253 {
254
255 /* XXX for now, no evcnt parent reported */
256 return NULL;
257 }
258
259 void *
260 prep_pci_intr_establish(void *v, pci_intr_handle_t ih, int level,
261 int (*func)(void *), void *arg)
262 {
263
264 if (ih == 0 || ih >= ICU_LEN || ih == IRQ_SLAVE)
265 panic("pci_intr_establish: bogus handle 0x%x", ih);
266
267 return isa_intr_establish(NULL, ih, IST_LEVEL, level, func, arg);
268 }
269
270 void
271 prep_pci_intr_disestablish(void *v, void *cookie)
272 {
273
274 isa_intr_disestablish(NULL, cookie);
275 }
276
277 void
278 prep_pci_conf_interrupt(void *v, int bus, int dev, int pin,
279 int swiz, int *iline)
280 {
281 /* do nothing */
282 }
283
284 extern pcitag_t prep_pci_direct_make_tag(void *, int, int, int);
285 extern pcitag_t prep_pci_indirect_make_tag(void *, int, int, int);
286 extern pcireg_t prep_pci_direct_conf_read(void *, pcitag_t, int);
287 extern pcireg_t prep_pci_indirect_conf_read(void *, pcitag_t, int);
288
289 int
290 prep_pci_conf_hook(pci_chipset_tag_t pct, int bus, int dev, int func,
291 pcireg_t id)
292 {
293 struct prep_pci_chipset_businfo *pbi;
294 prop_number_t bmax, pbus;
295 pcitag_t tag;
296 pcireg_t class;
297
298 /*
299 * The P9100 board found in some IBM machines cannot be
300 * over-configured.
301 */
302 if (PCI_VENDOR(id) == PCI_VENDOR_WEITEK &&
303 PCI_PRODUCT(id) == PCI_PRODUCT_WEITEK_P9100)
304 return 0;
305
306 /* We have already mapped the MPIC2 if we have one, so leave it
307 alone */
308 if (PCI_VENDOR(id) == PCI_VENDOR_IBM &&
309 PCI_PRODUCT(id) == PCI_PRODUCT_IBM_MPIC2)
310 return 0;
311
312 if (PCI_VENDOR(id) == PCI_VENDOR_IBM &&
313 PCI_PRODUCT(id) == PCI_PRODUCT_IBM_MPIC)
314 return 0;
315
316 if (PCI_VENDOR(id) == PCI_VENDOR_INTEL &&
317 PCI_PRODUCT(id) == PCI_PRODUCT_INTEL_PCEB)
318 return 0;
319
320 if (PCI_VENDOR(id) == PCI_VENDOR_MOT &&
321 PCI_PRODUCT(id) == PCI_PRODUCT_MOT_RAVEN)
322 return (PCI_CONF_ALL & ~PCI_CONF_MAP_MEM);
323
324 /* NOTE, all device specific stuff must be above this line */
325 /* don't do this on the primary host bridge */
326 if (bus == 0 && dev == 0 && func == 0)
327 return PCI_CONF_DEFAULT;
328
329 if (prep_pci_config_mode) {
330 tag = prep_pci_indirect_make_tag(pct, bus, dev, func);
331 class = prep_pci_indirect_conf_read(pct, tag,
332 PCI_CLASS_REG);
333 } else {
334 tag = prep_pci_direct_make_tag(pct, bus, dev, func);
335 class = prep_pci_direct_conf_read(pct, tag,
336 PCI_CLASS_REG);
337 }
338
339 /*
340 * PCI bridges have special needs. We need to discover where they
341 * came from, and wire them appropriately.
342 */
343 if (PCI_CLASS(class) == PCI_CLASS_BRIDGE &&
344 PCI_SUBCLASS(class) == PCI_SUBCLASS_BRIDGE_PCI) {
345 pbi = malloc(sizeof(struct prep_pci_chipset_businfo), M_DEVBUF,
346 M_NOWAIT);
347 KASSERT(pbi != NULL);
348 pbi->pbi_properties = prop_dictionary_create();
349 KASSERT(pbi->pbi_properties != NULL);
350 setup_pciintr_map(pbi, bus, dev, func);
351
352 /* record the parent bus, and the parent device number */
353 pbus = prop_number_create_integer(bus);
354 prop_dictionary_set(pbi->pbi_properties, "prep-pcibus-parent",
355 pbus);
356 prop_object_release(pbus);
357 pbus = prop_number_create_integer(dev);
358 prop_dictionary_set(pbi->pbi_properties,
359 "prep-pcibus-rawdevnum", pbus);
360 prop_object_release(pbus);
361
362 /* now look for bus quirks */
363
364 if (PCI_VENDOR(id) == PCI_VENDOR_DEC &&
365 PCI_PRODUCT(id) == PCI_PRODUCT_DEC_21154) {
366 bmax = prop_number_create_integer(8);
367 KASSERT(bmax != NULL);
368 prop_dictionary_set(pbi->pbi_properties,
369 "prep-pcibus-maxdevices", bmax);
370 prop_object_release(bmax);
371 }
372
373 SIMPLEQ_INSERT_TAIL(&prep_pct->pc_pbi, pbi, next);
374 }
375
376 return (PCI_CONF_DEFAULT);
377 }
378