pci_machdep_ofw.c revision 1.2 1 /* $NetBSD: pci_machdep_ofw.c,v 1.2 2007/10/17 19:56:44 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.2 2007/10/17 19:56:44 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[2];
92 char name[32];
93
94 for (node = startnode; node; node = OF_peer(node)) {
95 if ((child = OF_child(node)) != 0)
96 genofw_find_ofpics(child);
97 memset(name, 0, sizeof(name));
98 if (OF_getprop(node, "name", name, sizeof(name)) == -1)
99 continue;
100 if (strncmp(name, "interrupt-controller", 20) == 0)
101 goto foundic;
102 if (OF_getprop(node, "interrupt-controller", name,
103 sizeof(name)) > -1)
104 goto foundic;
105 if (OF_getprop(node, "device-type", name, sizeof(name)) == -1)
106 continue;
107 if (strncmp(name, "interrupt-controller", 20) == 0)
108 goto foundic;
109 foundic:
110 picnodes[nrofpics].node = node;
111 if (OF_getprop(node, "interrupt-parent", &iparent,
112 sizeof(iparent)) == sizeof(iparent))
113 picnodes[nrofpics].parent = iparent;
114 if (OF_getprop(node, "#interrupt-cells", &iparent,
115 sizeof(iparent)) == sizeof(iparent))
116 picnodes[nrofpics].cells = iparent;
117 else
118 picnodes[nrofpics].cells = 1;
119 if (OF_getprop(node, "interrupt-ranges", iranges,
120 sizeof(int)*2) == sizeof(int)*2)
121 picnodes[nrofpics].intrs = iranges[1];
122 else
123 picnodes[nrofpics].intrs = 16;
124 if (nrofpics > 0)
125 picnodes[nrofpics].offset = picnodes[nrofpics-1].offset
126 + picnodes[nrofpics-1].intrs;
127 else
128 picnodes[nrofpics].offset = 0;
129 OF_getprop(node, "device-type", name, sizeof(name));
130 if (strcmp(name, "open-pic") == 0)
131 picnodes[nrofpics].type = PICNODE_TYPE_OPENPIC;
132 if (strcmp(name, "interrupt-controller") == 0) {
133 OF_getprop(node, "compatible", name, sizeof(name));
134 if (strcmp(name, "heathrow") != 0)
135 picnodes[nrofpics].type = PICNODE_TYPE_HEATHROW;
136 if (strcmp(name, "chrp,iic") != 0)
137 picnodes[nrofpics].type = PICNODE_TYPE_8259;
138 }
139 nrofpics++;
140 }
141 }
142
143 /* Fix up the various picnode offsets */
144 void
145 genofw_fixup_picnode_offsets(void)
146 {
147 int i, curoff;
148
149 curoff=0;
150
151 for (i=0; i < nrofpics; i++) {
152 if (picnodes[i].type == PICNODE_TYPE_8259) {
153 picnodes[i].offset = 0;
154 curoff = picnodes[i].intrs;
155 }
156 }
157 for (i=0; i < nrofpics; i++) {
158 /* now skip the 8259 */
159 if (picnodes[i].type == PICNODE_TYPE_8259)
160 continue;
161 picnodes[i].offset = curoff;
162 curoff += picnodes[i].intrs;
163 }
164 }
165
166 /* we are given a pci devnode, and dig from there */
167 void
168 genofw_setup_pciintr_map(struct genppc_pci_chipset_businfo *pbi, int pcinode)
169 {
170 int node;
171 u_int32_t map[160];
172 int parent, len;
173 int curdev;
174 int i, reclen, nrofpcidevs=0;
175 u_int32_t acells, icells, pcells;
176 prop_dictionary_t dict;
177 prop_dictionary_t sub=0;
178
179 len = OF_getprop(pcinode, "interrupt-map", map, sizeof(map));
180 if (len == -1)
181 goto nomap;
182
183 if (OF_getprop(pcinode, "#address-cells", &acells,
184 sizeof(acells)) == -1)
185 acells = 1;
186 if (OF_getprop(pcinode, "#interrupt-cells", &icells,
187 sizeof(icells)) == -1)
188 icells = 1;
189
190 parent = map[acells+icells+1];
191 if (OF_getprop(parent, "#interrupt-cells", &pcells,
192 sizeof(pcells)) == -1)
193 pcells = 1;
194
195 reclen = acells+pcells+icells+1;
196 nrofpcidevs = len / (reclen * sizeof(int));
197
198 dict = prop_dictionary_create_with_capacity(nrofpcidevs*2);
199 KASSERT(dict != NULL);
200
201 curdev = -1;
202 prop_dictionary_set(pbi->pbi_properties, "ofw-pci-intrmap", dict);
203 for (i = 0; i < nrofpcidevs; i++) {
204 prop_number_t intr_num;
205 int dev, pin, pic;
206 char key[20];
207
208 pic = genofw_find_picnode(map[i*reclen + acells + icells]);
209 KASSERT(pic != -1);
210 dev = (map[i*reclen] >> 8) / 0x8;
211 if (curdev != dev)
212 sub = prop_dictionary_create_with_capacity(4);
213 pin = map[i*reclen + acells];
214 intr_num = prop_number_create_integer(map[i*reclen + acells + icells + 1] + picnodes[pic].offset);
215 sprintf(key, "pin-%c", 'A' + pin);
216 prop_dictionary_set(sub, key, intr_num);
217 prop_object_release(intr_num);
218 /* should we care about level? */
219
220 sprintf(key, "devfunc-%d", dev);
221 prop_dictionary_set(dict, key, sub);
222 if (curdev != dev) {
223 prop_object_release(sub);
224 curdev = dev;
225 }
226 }
227 /* the mapping is complete */
228 prop_object_release(dict);
229 return;
230
231 nomap:
232 /* so, we have one of those annoying machines that doesn't provide
233 * a nice simple map of interrupts. We get to do this the hard
234 * way instead. Lucky us.
235 */
236 for (node = OF_child(pcinode), nrofpcidevs=0; node;
237 node = OF_peer(node))
238 nrofpcidevs++;
239 dict = prop_dictionary_create_with_capacity(nrofpcidevs*2);
240 KASSERT(dict != NULL);
241 prop_dictionary_set(pbi->pbi_properties, "ofw-pci-intrmap", dict);
242
243 for (node = OF_child(pcinode); node; node = OF_peer(node)) {
244 uint32_t irqs[4], reg[5];
245 prop_number_t intr_num;
246 int dev, pin;
247 char key[20];
248
249 /* walk the bus looking for pci devices and map them */
250 if (OF_getprop(node, "AAPL,interrupts", irqs, 4) > 0) {
251 dev = 0;
252 if (OF_getprop(node, "reg", reg, 5) > 0)
253 dev = ((reg[0] & 0x0000ff00) >> 8) / 0x8;
254 else if (OF_getprop(node, "assigned-addresses",
255 reg, 5) > 0)
256 dev = ((reg[0] & 0x0000ff00) >> 8) / 0x8;
257 if (dev == 0) {
258 aprint_error("cannot figure out device num "
259 "for node 0x%x\n", node);
260 continue;
261 }
262 sub = prop_dictionary_create_with_capacity(4);
263 if (OF_getprop(node, "interrupts", &pin, 4) < 0)
264 pin = 1;
265 intr_num = prop_number_create_integer(irqs[0]);
266 sprintf(key, "pin-%c", 'A' + pin);
267 prop_dictionary_set(sub, key, intr_num);
268 prop_object_release(intr_num);
269 sprintf(key, "devfunc-%d", dev);
270 prop_dictionary_set(dict, key, sub);
271 prop_object_release(sub);
272 }
273 }
274 aprint_normal("%s\n", prop_dictionary_externalize(pbi->pbi_properties));
275 }
276
277 int
278 genofw_find_node_by_devfunc(int startnode, int bus, int dev, int func)
279 {
280 int node, sz, p=0;
281 uint32_t reg;
282
283 for (node = startnode; node; node = p) {
284 sz = OF_getprop(node, "reg", ®, sizeof(reg));
285 if (sz != sizeof(reg))
286 continue;
287 if (OFW_PCI_PHYS_HI_BUS(reg) == bus &&
288 OFW_PCI_PHYS_HI_DEVICE(reg) == dev &&
289 OFW_PCI_PHYS_HI_FUNCTION(reg) == func)
290 return node;
291 if ((p = OF_child(node)))
292 continue;
293 while (node) {
294 if ((p = OF_peer(node)))
295 break;
296 node = OF_parent(node);
297 }
298 }
299 /* couldn't find it */
300 return -1;
301 }
302
303 int
304 genofw_pci_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
305 {
306 struct genppc_pci_chipset_businfo *pbi;
307 prop_dictionary_t dict, devsub;
308 prop_object_t pinsub;
309 prop_number_t pbus;
310 int busno, bus, pin, line, swiz, dev, origdev, i;
311 char key[20];
312
313 pin = pa->pa_intrpin;
314 line = pa->pa_intrline;
315 bus = busno = pa->pa_bus;
316 swiz = pa->pa_intrswiz;
317 origdev = dev = pa->pa_device;
318 i = 0;
319
320 pbi = SIMPLEQ_FIRST(&pa->pa_pc->pc_pbi);
321 while (busno--)
322 pbi = SIMPLEQ_NEXT(pbi, next);
323 KASSERT(pbi != NULL);
324
325 dict = prop_dictionary_get(pbi->pbi_properties, "ofw-pci-intrmap");
326
327 if (dict != NULL)
328 i = prop_dictionary_count(dict);
329
330 if (dict == NULL || i == 0) {
331 /* We have an unmapped bus, now it gets hard */
332 pbus = prop_dictionary_get(pbi->pbi_properties,
333 "ofw-pcibus-parent");
334 if (pbus == NULL)
335 goto bad;
336 busno = prop_number_integer_value(pbus);
337 pbus = prop_dictionary_get(pbi->pbi_properties,
338 "ofw-pcibus-rawdevnum");
339 dev = prop_number_integer_value(pbus);
340
341 /* now that we know the parent bus, we need to find it's pbi */
342 pbi = SIMPLEQ_FIRST(&pa->pa_pc->pc_pbi);
343 while (busno--)
344 pbi = SIMPLEQ_NEXT(pbi, next);
345 KASSERT(pbi != NULL);
346
347 /* swizzle the pin */
348 pin = ((pin + origdev - 1) & 3) + 1;
349
350 /* now we have the pbi, ask for dict again */
351 dict = prop_dictionary_get(pbi->pbi_properties,
352 "ofw-pci-intrmap");
353 if (dict == NULL)
354 goto bad;
355 }
356
357 /* No IRQ used. */
358 if (pin == 0)
359 goto bad;
360 if (pin > 4) {
361 aprint_error("pci_intr_map: bad interrupt pin %d\n", pin);
362 goto bad;
363 }
364
365 sprintf(key, "devfunc-%d", dev);
366 devsub = prop_dictionary_get(dict, key);
367 if (devsub == NULL)
368 goto bad;
369 sprintf(key, "pin-%c", 'A' + (pin-1));
370 pinsub = prop_dictionary_get(devsub, key);
371 if (pinsub == NULL)
372 goto bad;
373 line = prop_number_integer_value(pinsub);
374
375 if (line == 0 || line == 255) {
376 aprint_error("pci_intr_map: no mapping for pin %c\n",'@' + pin);
377 goto bad;
378 }
379
380 *ihp = line;
381 return 0;
382
383 bad:
384 *ihp = -1;
385 return 1;
386 }
387
388 int
389 genofw_pci_conf_hook(pci_chipset_tag_t pct, int bus, int dev, int func,
390 pcireg_t id)
391 {
392 struct genppc_pci_chipset_businfo *pbi;
393 prop_number_t pbus;
394 pcitag_t tag;
395 pcireg_t class;
396 int node;
397
398 /* We have already mapped MPIC's if we have them, so leave them alone */
399 if (PCI_VENDOR(id) == PCI_VENDOR_IBM &&
400 PCI_PRODUCT(id) == PCI_PRODUCT_IBM_MPIC2)
401 return 0;
402
403 if (PCI_VENDOR(id) == PCI_VENDOR_IBM &&
404 PCI_PRODUCT(id) == PCI_PRODUCT_IBM_MPIC)
405 return 0;
406
407 /* I highly doubt there are any CHRP ravens, but just in case */
408 if (PCI_VENDOR(id) == PCI_VENDOR_MOT &&
409 PCI_PRODUCT(id) == PCI_PRODUCT_MOT_RAVEN)
410 return (PCI_CONF_ALL & ~PCI_CONF_MAP_MEM);
411
412 /* NOTE, all device specific stuff must be above this line */
413 /* don't do this on the primary host bridge */
414 if (bus == 0 && dev == 0 && func == 0)
415 return PCI_CONF_DEFAULT;
416
417 tag = genppc_pci_indirect_make_tag(pct, bus, dev, func);
418 class = genppc_pci_indirect_conf_read(pct, tag, PCI_CLASS_REG);
419
420 /*
421 * PCI bridges have special needs. We need to discover where they
422 * came from, and wire them appropriately.
423 */
424 if (PCI_CLASS(class) == PCI_CLASS_BRIDGE &&
425 PCI_SUBCLASS(class) == PCI_SUBCLASS_BRIDGE_PCI) {
426 pbi = malloc(sizeof(struct genppc_pci_chipset_businfo),
427 M_DEVBUF, M_NOWAIT);
428 KASSERT(pbi != NULL);
429 pbi->pbi_properties = prop_dictionary_create();
430 KASSERT(pbi->pbi_properties != NULL);
431 node = genofw_find_node_by_devfunc(pct->pc_node, bus, dev,
432 func);
433 if (node == -1) {
434 aprint_error("Cannot find node for device "
435 "bus %d dev %d func %d\n", bus, dev, func);
436 prop_object_release(pbi->pbi_properties);
437 free(pbi, M_DEVBUF);
438 return (PCI_CONF_DEFAULT);
439 }
440 genofw_setup_pciintr_map(pbi, node);
441
442 /* record the parent bus, and the parent device number */
443 pbus = prop_number_create_integer(bus);
444 prop_dictionary_set(pbi->pbi_properties, "ofw-pcibus-parent",
445 pbus);
446 prop_object_release(pbus);
447 pbus = prop_number_create_integer(dev);
448 prop_dictionary_set(pbi->pbi_properties, "ofw-pcibus-rawdevnum",
449 pbus);
450 prop_object_release(pbus);
451
452 SIMPLEQ_INSERT_TAIL(&pct->pc_pbi, pbi, next);
453 }
454
455 return (PCI_CONF_DEFAULT);
456 }
457