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