pci_machdep_ofw.c revision 1.8 1 /* $NetBSD: pci_machdep_ofw.c,v 1.8 2008/01/17 23:42:59 garbled Exp $ */
2
3 /*-
4 * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Tim Rightnour
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Generic OFW routines for pci_machdep
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: pci_machdep_ofw.c,v 1.8 2008/01/17 23:42:59 garbled Exp $");
45
46 #include <sys/types.h>
47 #include <sys/param.h>
48 #include <sys/time.h>
49 #include <sys/systm.h>
50 #include <sys/errno.h>
51 #include <sys/device.h>
52 #include <sys/malloc.h>
53
54 #include <uvm/uvm_extern.h>
55
56 #include <machine/bus.h>
57
58 #include <machine/autoconf.h>
59 #include <machine/pio.h>
60 #include <machine/intr.h>
61
62 #include <dev/pci/pcivar.h>
63 #include <dev/pci/pcireg.h>
64 #include <dev/pci/ppbreg.h>
65 #include <dev/pci/pcidevs.h>
66 #include <dev/pci/pciconf.h>
67
68 #include <dev/ofw/openfirm.h>
69 #include <dev/ofw/ofw_pci.h>
70
71 pcitag_t genppc_pci_indirect_make_tag(void *, int, int, int);
72 void genppc_pci_indirect_decompose_tag(void *, pcitag_t, int *, int *, int *);
73
74 ofw_pic_node_t picnodes[8];
75 int nrofpics = 0;
76
77 int
78 genofw_find_picnode(int node)
79 {
80 int i;
81
82 for (i = 0; i < 8; i++)
83 if (node == picnodes[i].node)
84 return i;
85 return -1;
86 }
87
88 void
89 genofw_find_ofpics(int startnode)
90 {
91 int node, iparent, child, iranges[6], irgot=0, i;
92 uint32_t reg[12];
93 char name[32];
94
95 for (node = startnode; node; node = OF_peer(node)) {
96 if ((child = OF_child(node)) != 0)
97 genofw_find_ofpics(child);
98 memset(name, 0, sizeof(name));
99 if (OF_getprop(node, "name", name, sizeof(name)) == -1)
100 continue;
101 if (strncmp(name, "interrupt-controller", 20) == 0)
102 goto foundic;
103
104 if (OF_getprop(node, "interrupt-controller", name,
105 sizeof(name)) > -1)
106 goto foundic;
107
108 if (OF_getprop(node, "device_type", name, sizeof(name)) == -1)
109 continue;
110 if (strncmp(name, "interrupt-controller", 20) == 0)
111 goto foundic;
112
113 /* if we didn't find one, skip to the next */
114 continue;
115 foundic:
116 picnodes[nrofpics].node = node;
117 if (OF_getprop(node, "interrupt-parent", &iparent,
118 sizeof(iparent)) == sizeof(iparent))
119 picnodes[nrofpics].parent = iparent;
120 if (OF_getprop(node, "#interrupt-cells", &iparent,
121 sizeof(iparent)) == sizeof(iparent))
122 picnodes[nrofpics].cells = iparent;
123 else
124 picnodes[nrofpics].cells = 1;
125
126 picnodes[nrofpics].intrs = 0;
127 irgot = OF_getprop(node, "interrupt-ranges", iranges,
128 sizeof(int)*6); /* XXX is this ok? */
129 if (irgot >= sizeof(int)) {
130 for (i=0; i < irgot/4; i++)
131 if (!picnodes[nrofpics].intrs)
132 picnodes[nrofpics].intrs = iranges[i];
133 }
134
135 irgot = OF_getprop(node, "reg", reg, sizeof(reg));
136
137 if (!picnodes[nrofpics].intrs)
138 picnodes[nrofpics].intrs = 16;
139
140 if (nrofpics > 0)
141 picnodes[nrofpics].offset = picnodes[nrofpics-1].offset
142 + picnodes[nrofpics-1].intrs;
143 else
144 picnodes[nrofpics].offset = 0;
145 OF_getprop(node, "device_type", name, sizeof(name));
146 if (strcmp(name, "open-pic") == 0)
147 picnodes[nrofpics].type = PICNODE_TYPE_OPENPIC;
148 if (strcmp(name, "interrupt-controller") == 0) {
149 OF_getprop(node, "compatible", name, sizeof(name));
150 if (strcmp(name, "heathrow") == 0)
151 picnodes[nrofpics].type = PICNODE_TYPE_HEATHROW;
152 if (strcmp(name, "chrp,iic") == 0) {
153 picnodes[nrofpics].type = PICNODE_TYPE_8259;
154 if (irgot >= 9 * sizeof(uint32_t) &&
155 reg[7] == 0x4d0)
156 picnodes[nrofpics].type =
157 PICNODE_TYPE_IVR;
158 }
159 }
160 if (strlen(name) == 0) {
161 /* probably a Pegasos, assume 8259 */
162 picnodes[nrofpics].type = PICNODE_TYPE_8259;
163 }
164 nrofpics++;
165 }
166 }
167
168 /* Fix up the various picnode offsets */
169 void
170 genofw_fixup_picnode_offsets(void)
171 {
172 int i, curoff;
173
174 curoff=0;
175
176 for (i=0; i < nrofpics; i++) {
177 if (picnodes[i].type == PICNODE_TYPE_8259 ||
178 picnodes[i].type == PICNODE_TYPE_IVR) {
179 picnodes[i].offset = 0;
180 curoff = picnodes[i].intrs;
181 }
182 }
183 for (i=0; i < nrofpics; i++) {
184 /* now skip the 8259 */
185 if (picnodes[i].type == PICNODE_TYPE_8259)
186 continue;
187 if (picnodes[i].type == PICNODE_TYPE_IVR)
188 continue;
189
190 picnodes[i].offset = curoff;
191 curoff += picnodes[i].intrs;
192 }
193 }
194
195 /* we are given a pci devnode, and dig from there */
196 void
197 genofw_setup_pciintr_map(void *v, struct genppc_pci_chipset_businfo *pbi,
198 int pcinode)
199 {
200 int node;
201 u_int32_t map[160];
202 int parent, len;
203 int curdev, foundirqs=0;
204 int i, reclen, nrofpcidevs=0;
205 u_int32_t acells, icells, pcells;
206 prop_dictionary_t dict;
207 prop_dictionary_t sub=0;
208 pci_chipset_tag_t pc = (pci_chipset_tag_t)v;
209
210 len = OF_getprop(pcinode, "interrupt-map", map, sizeof(map));
211 if (len == -1)
212 goto nomap;
213
214 if (OF_getprop(pcinode, "#address-cells", &acells,
215 sizeof(acells)) == -1)
216 acells = 1;
217 if (OF_getprop(pcinode, "#interrupt-cells", &icells,
218 sizeof(icells)) == -1)
219 icells = 1;
220
221 parent = map[acells+icells];
222 if (OF_getprop(parent, "#interrupt-cells", &pcells,
223 sizeof(pcells)) == -1)
224 pcells = 1;
225
226 reclen = acells+pcells+icells+1;
227 nrofpcidevs = len / (reclen * sizeof(int));
228
229 dict = prop_dictionary_create_with_capacity(nrofpcidevs*2);
230 KASSERT(dict != NULL);
231
232 curdev = -1;
233 prop_dictionary_set(pbi->pbi_properties, "ofw-pci-intrmap", dict);
234 for (i = 0; i < nrofpcidevs; i++) {
235 prop_number_t intr_num;
236 int dev, pin, pic, func;
237 char key[20];
238
239 pic = genofw_find_picnode(map[i*reclen + acells + icells]);
240 KASSERT(pic != -1);
241 dev = (map[i*reclen] >> 8) / 0x8;
242 func = (map[i*reclen] >> 8) % 0x8;
243 if (curdev != dev)
244 sub = prop_dictionary_create_with_capacity(4);
245 pin = map[i*reclen + acells];
246 intr_num = prop_number_create_integer(map[i*reclen + acells + icells + 1] + picnodes[pic].offset);
247 sprintf(key, "pin-%c", 'A' + (pin-1));
248 prop_dictionary_set(sub, key, intr_num);
249 prop_object_release(intr_num);
250 /* should we care about level? */
251
252 sprintf(key, "devfunc-%d", dev*0x8 + func);
253 prop_dictionary_set(dict, key, sub);
254 if (curdev != dev) {
255 prop_object_release(sub);
256 curdev = dev;
257 }
258 }
259 /* the mapping is complete */
260 prop_object_release(dict);
261 aprint_debug("%s\n", prop_dictionary_externalize(pbi->pbi_properties));
262 return;
263
264 nomap:
265 /* so, we have one of those annoying machines that doesn't provide
266 * a nice simple map of interrupts. We get to do this the hard
267 * way instead. Lucky us.
268 */
269 for (node = OF_child(pcinode), nrofpcidevs=0; node;
270 node = OF_peer(node))
271 nrofpcidevs++;
272 dict = prop_dictionary_create_with_capacity(nrofpcidevs*2);
273 KASSERT(dict != NULL);
274 prop_dictionary_set(pbi->pbi_properties, "ofw-pci-intrmap", dict);
275
276 for (node = OF_child(pcinode); node; node = OF_peer(node)) {
277 uint32_t irqs[4], reg[5];
278 prop_number_t intr_num;
279 int dev, pin, func;
280 char key[20];
281
282 /* walk the bus looking for pci devices and map them */
283 if (OF_getprop(node, "AAPL,interrupts", irqs, 4) > 0) {
284 dev = 0;
285 if (OF_getprop(node, "reg", reg, 5) > 0) {
286 dev = ((reg[0] & 0x0000ff00) >> 8) / 0x8;
287 func = ((reg[0] & 0x0000ff00) >> 8) % 0x8;
288 } else if (OF_getprop(node, "assigned-addresses",
289 reg, 5) > 0) {
290 dev = ((reg[0] & 0x0000ff00) >> 8) / 0x8;
291 func = ((reg[0] & 0x0000ff00) >> 8) % 0x8;
292 }
293 if (dev == 0) {
294 aprint_error("cannot figure out device num "
295 "for node 0x%x\n", node);
296 continue;
297 }
298 sub = prop_dictionary_create_with_capacity(4);
299 if (OF_getprop(node, "interrupts", &pin, 4) < 0)
300 pin = 1;
301 intr_num = prop_number_create_integer(irqs[0]);
302 sprintf(key, "pin-%c", 'A' + (pin-1));
303 prop_dictionary_set(sub, key, intr_num);
304 prop_object_release(intr_num);
305 sprintf(key, "devfunc-%d", dev*0x8 + func);
306 prop_dictionary_set(dict, key, sub);
307 prop_object_release(sub);
308 foundirqs++;
309 }
310 }
311 if (foundirqs)
312 return;
313
314 /*
315 * If we got this far, we have a super-annoying OFW.
316 * They didn't bother to fill in any interrupt properties anywhere,
317 * so we pray that they filled in the ones on the pci devices.
318 */
319 for (node = OF_child(pcinode); node; node = OF_peer(node)) {
320 uint32_t reg[5], irq;
321 prop_number_t intr_num;
322 pcitag_t tag;
323 int dev, pin, func;
324 char key[20];
325
326 if (OF_getprop(node, "reg", reg, 5) > 0) {
327 dev = ((reg[0] & 0x0000ff00) >> 8) / 0x8;
328 func = ((reg[0] & 0x0000ff00) >> 8) % 0x8;
329
330 tag = pci_make_tag(pc, pc->pc_bus, dev, func);
331 irq = PCI_INTERRUPT_LINE(pci_conf_read(pc, tag,
332 PCI_INTERRUPT_REG));
333 if (irq == 255)
334 irq = 0;
335
336 sub = prop_dictionary_create_with_capacity(4);
337 if (OF_getprop(node, "interrupts", &pin, 4) < 0)
338 pin = 1;
339 intr_num = prop_number_create_integer(irq);
340 sprintf(key, "pin-%c", 'A' + (pin-1));
341 prop_dictionary_set(sub, key, intr_num);
342 prop_object_release(intr_num);
343 sprintf(key, "devfunc-%d", dev*0x8 + func);
344 prop_dictionary_set(dict, key, sub);
345 prop_object_release(sub);
346 }
347 }
348 aprint_debug("%s\n", prop_dictionary_externalize(pbi->pbi_properties));
349 }
350
351 int
352 genofw_find_node_by_devfunc(int startnode, int bus, int dev, int func)
353 {
354 int node, sz, p=0;
355 uint32_t reg;
356
357 for (node = startnode; node; node = p) {
358 sz = OF_getprop(node, "reg", ®, sizeof(reg));
359 if (sz != sizeof(reg))
360 continue;
361 if (OFW_PCI_PHYS_HI_BUS(reg) == bus &&
362 OFW_PCI_PHYS_HI_DEVICE(reg) == dev &&
363 OFW_PCI_PHYS_HI_FUNCTION(reg) == func)
364 return node;
365 if ((p = OF_child(node)))
366 continue;
367 while (node) {
368 if ((p = OF_peer(node)))
369 break;
370 node = OF_parent(node);
371 }
372 }
373 /* couldn't find it */
374 return -1;
375 }
376
377 int
378 genofw_pci_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
379 {
380 struct genppc_pci_chipset_businfo *pbi;
381 prop_dictionary_t dict, devsub;
382 prop_object_t pinsub;
383 prop_number_t pbus;
384 int busno, bus, pin, line, swiz, dev, origdev, func, i;
385 char key[20];
386
387 pin = pa->pa_intrpin;
388 line = pa->pa_intrline;
389 bus = busno = pa->pa_bus;
390 swiz = pa->pa_intrswiz;
391 origdev = dev = pa->pa_device;
392 func = pa->pa_function;
393 i = 0;
394
395 pbi = SIMPLEQ_FIRST(&pa->pa_pc->pc_pbi);
396 while (busno--)
397 pbi = SIMPLEQ_NEXT(pbi, next);
398 KASSERT(pbi != NULL);
399
400 dict = prop_dictionary_get(pbi->pbi_properties, "ofw-pci-intrmap");
401
402 if (dict != NULL)
403 i = prop_dictionary_count(dict);
404
405 if (dict == NULL || i == 0) {
406 /* We have an unmapped bus, now it gets hard */
407 pbus = prop_dictionary_get(pbi->pbi_properties,
408 "ofw-pcibus-parent");
409 if (pbus == NULL)
410 goto bad;
411 busno = prop_number_integer_value(pbus);
412 pbus = prop_dictionary_get(pbi->pbi_properties,
413 "ofw-pcibus-rawdevnum");
414 dev = prop_number_integer_value(pbus);
415
416 /* now that we know the parent bus, we need to find it's pbi */
417 pbi = SIMPLEQ_FIRST(&pa->pa_pc->pc_pbi);
418 while (busno--)
419 pbi = SIMPLEQ_NEXT(pbi, next);
420 KASSERT(pbi != NULL);
421
422 /* swizzle the pin */
423 pin = ((pin + origdev - 1) & 3) + 1;
424
425 /* now we have the pbi, ask for dict again */
426 dict = prop_dictionary_get(pbi->pbi_properties,
427 "ofw-pci-intrmap");
428 if (dict == NULL)
429 goto bad;
430 }
431
432 /* No IRQ used. */
433 if (pin == 0)
434 goto bad;
435 if (pin > 4) {
436 aprint_error("pci_intr_map: bad interrupt pin %d\n", pin);
437 goto bad;
438 }
439
440 sprintf(key, "devfunc-%d", dev*0x8 + func);
441 devsub = prop_dictionary_get(dict, key);
442 if (devsub == NULL)
443 goto bad;
444 sprintf(key, "pin-%c", 'A' + (pin-1));
445 pinsub = prop_dictionary_get(devsub, key);
446 if (pinsub == NULL)
447 goto bad;
448 line = prop_number_integer_value(pinsub);
449
450 if (line == 0 || line == 255) {
451 aprint_error("pci_intr_map: no mapping for pin %c\n",'@' + pin);
452 goto bad;
453 }
454
455 *ihp = line;
456 return 0;
457
458 bad:
459 *ihp = -1;
460 return 1;
461 }
462
463 int
464 genofw_pci_conf_hook(pci_chipset_tag_t pct, int bus, int dev, int func,
465 pcireg_t id)
466 {
467 struct genppc_pci_chipset_businfo *pbi;
468 prop_number_t pbus;
469 pcitag_t tag;
470 pcireg_t class;
471 int node;
472
473 /* We have already mapped MPIC's if we have them, so leave them alone */
474 if (PCI_VENDOR(id) == PCI_VENDOR_IBM &&
475 PCI_PRODUCT(id) == PCI_PRODUCT_IBM_MPIC2)
476 return 0;
477
478 if (PCI_VENDOR(id) == PCI_VENDOR_IBM &&
479 PCI_PRODUCT(id) == PCI_PRODUCT_IBM_MPIC)
480 return 0;
481
482 /* I highly doubt there are any CHRP ravens, but just in case */
483 if (PCI_VENDOR(id) == PCI_VENDOR_MOT &&
484 PCI_PRODUCT(id) == PCI_PRODUCT_MOT_RAVEN)
485 return (PCI_CONF_ALL & ~PCI_CONF_MAP_MEM);
486
487 /*
488 * Pegasos2 specific stuff.
489 */
490 if (strncmp(model_name, "Pegasos2", 8) == 0) {
491
492 /* we want to leave viaide(4) alone */
493 if (PCI_VENDOR(id) == PCI_VENDOR_VIATECH &&
494 PCI_PRODUCT(id) == PCI_PRODUCT_VIATECH_VT82C586A_IDE)
495 return 0;
496
497 /* we want to leave fwochi(4) without mem space */
498 if (PCI_VENDOR(id) == PCI_VENDOR_VIATECH &&
499 PCI_PRODUCT(id) == PCI_PRODUCT_VIATECH_VT6306)
500 return (PCI_CONF_ALL & ~PCI_CONF_MAP_MEM);
501 }
502
503 /* NOTE, all device specific stuff must be above this line */
504 /* don't do this on the primary host bridge */
505 if (bus == 0 && dev == 0 && func == 0)
506 return PCI_CONF_DEFAULT;
507
508 tag = genppc_pci_indirect_make_tag(pct, bus, dev, func);
509 class = genppc_pci_indirect_conf_read(pct, tag, PCI_CLASS_REG);
510
511 /*
512 * PCI bridges have special needs. We need to discover where they
513 * came from, and wire them appropriately.
514 */
515 if (PCI_CLASS(class) == PCI_CLASS_BRIDGE &&
516 PCI_SUBCLASS(class) == PCI_SUBCLASS_BRIDGE_PCI) {
517 pbi = malloc(sizeof(struct genppc_pci_chipset_businfo),
518 M_DEVBUF, M_NOWAIT);
519 KASSERT(pbi != NULL);
520 pbi->pbi_properties = prop_dictionary_create();
521 KASSERT(pbi->pbi_properties != NULL);
522 node = genofw_find_node_by_devfunc(pct->pc_node, bus, dev,
523 func);
524 if (node == -1) {
525 aprint_error("Cannot find node for device "
526 "bus %d dev %d func %d\n", bus, dev, func);
527 prop_object_release(pbi->pbi_properties);
528 free(pbi, M_DEVBUF);
529 return (PCI_CONF_DEFAULT);
530 }
531 genofw_setup_pciintr_map((void *)pct, pbi, node);
532
533 /* record the parent bus, and the parent device number */
534 pbus = prop_number_create_integer(bus);
535 prop_dictionary_set(pbi->pbi_properties, "ofw-pcibus-parent",
536 pbus);
537 prop_object_release(pbus);
538 pbus = prop_number_create_integer(dev);
539 prop_dictionary_set(pbi->pbi_properties, "ofw-pcibus-rawdevnum",
540 pbus);
541 prop_object_release(pbus);
542
543 SIMPLEQ_INSERT_TAIL(&pct->pc_pbi, pbi, next);
544 }
545
546 return (PCI_CONF_DEFAULT);
547 }
548