]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/sparc64/ebus/ebus.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / sys / sparc64 / ebus / ebus.c
1 /*-
2  * Copyright (c) 1999, 2000 Matthew R. Green
3  * Copyright (c) 2009 by Marius Strobl <marius@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *      from: NetBSD: ebus.c,v 1.52 2008/05/29 14:51:26 mrg Exp
30  */
31 /*-
32  * Copyright (c) 2001 Thomas Moestl <tmm@FreeBSD.org>
33  * All rights reserved.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  * 1. Redistributions of source code must retain the above copyright
39  *    notice, this list of conditions and the following disclaimer.
40  * 2. Redistributions in binary form must reproduce the above copyright
41  *    notice, this list of conditions and the following disclaimer in the
42  *    documentation and/or other materials provided with the distribution.
43  * 3. The name of the author may not be used to endorse or promote products
44  *    derived from this software without specific prior written permission.
45  *
46  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
47  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
48  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
49  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
50  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
51  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
52  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
53  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
54  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56  * SUCH DAMAGE.
57  */
58
59 #include <sys/cdefs.h>
60 __FBSDID("$FreeBSD$");
61
62 /*
63  * Driver for JBus to EBus and PCI to EBus bridges
64  */
65
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/bus.h>
69 #include <sys/kernel.h>
70 #include <sys/malloc.h>
71 #include <sys/module.h>
72
73 #include <sys/rman.h>
74
75 #include <dev/ofw/ofw_bus.h>
76 #include <dev/ofw/ofw_bus_subr.h>
77 #include <dev/ofw/openfirm.h>
78
79 #include <machine/bus.h>
80 #ifndef SUN4V
81 #include <machine/bus_common.h>
82 #endif
83 #include <machine/intr_machdep.h>
84 #include <machine/resource.h>
85
86 #include <dev/pci/pcireg.h>
87 #include <dev/pci/pcivar.h>
88
89 #include <sparc64/pci/ofw_pci.h>
90
91 /*
92  * The register, interrupt map and for the PCI variant also the ranges
93  * properties are identical to the ISA ones.
94  */
95 #include <sparc64/isa/ofw_isa.h>
96
97 struct ebus_nexus_ranges {
98         uint32_t        child_hi;
99         uint32_t        child_lo;
100         uint32_t        phys_hi;
101         uint32_t        phys_lo;
102         uint32_t        size;
103 };
104
105 struct ebus_devinfo {
106         struct ofw_bus_devinfo  edi_obdinfo;
107         struct resource_list    edi_rl;
108 };
109
110 struct ebus_rinfo {
111         int                     eri_rtype;
112         struct rman             eri_rman;
113         struct resource         *eri_res;
114 };
115
116 struct ebus_softc {
117         void                    *sc_range;
118         struct ebus_rinfo       *sc_rinfo;
119
120         u_int                   sc_flags;
121 #define EBUS_PCI                (1 << 0)
122
123         int                     sc_nrange;
124
125         struct ofw_bus_iinfo    sc_iinfo;
126
127 #ifndef SUN4V
128         uint32_t                sc_ign;
129 #endif
130 };
131
132 static device_probe_t ebus_nexus_probe;
133 static device_attach_t ebus_nexus_attach;
134 static device_probe_t ebus_pci_probe;
135 static device_attach_t ebus_pci_attach;
136 static bus_print_child_t ebus_print_child;
137 static bus_probe_nomatch_t ebus_probe_nomatch;
138 static bus_alloc_resource_t ebus_alloc_resource;
139 static bus_release_resource_t ebus_release_resource;
140 static bus_setup_intr_t ebus_setup_intr;
141 static bus_get_resource_list_t ebus_get_resource_list;
142 static ofw_bus_get_devinfo_t ebus_get_devinfo;
143
144 static int ebus_attach(device_t dev, struct ebus_softc *sc, phandle_t node);
145 static struct ebus_devinfo *ebus_setup_dinfo(device_t dev,
146     struct ebus_softc *sc, phandle_t node);
147 static void ebus_destroy_dinfo(struct ebus_devinfo *edi);
148 static int ebus_print_res(struct ebus_devinfo *edi);
149
150 static devclass_t ebus_devclass;
151
152 static device_method_t ebus_nexus_methods[] = {
153         /* Device interface */
154         DEVMETHOD(device_probe,         ebus_nexus_probe),
155         DEVMETHOD(device_attach,        ebus_nexus_attach),
156         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
157         DEVMETHOD(device_suspend,       bus_generic_suspend),
158         DEVMETHOD(device_resume,        bus_generic_resume),
159
160         /* Bus interface */
161         DEVMETHOD(bus_print_child,      ebus_print_child),
162         DEVMETHOD(bus_probe_nomatch,    ebus_probe_nomatch),
163         DEVMETHOD(bus_alloc_resource,   ebus_alloc_resource),
164         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
165         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
166         DEVMETHOD(bus_release_resource, ebus_release_resource),
167         DEVMETHOD(bus_setup_intr,       ebus_setup_intr),
168         DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
169         DEVMETHOD(bus_get_resource,     bus_generic_rl_get_resource),
170         DEVMETHOD(bus_get_resource_list, ebus_get_resource_list),
171         DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
172
173         /* ofw_bus interface */
174         DEVMETHOD(ofw_bus_get_devinfo,  ebus_get_devinfo),
175         DEVMETHOD(ofw_bus_get_compat,   ofw_bus_gen_get_compat),
176         DEVMETHOD(ofw_bus_get_model,    ofw_bus_gen_get_model),
177         DEVMETHOD(ofw_bus_get_name,     ofw_bus_gen_get_name),
178         DEVMETHOD(ofw_bus_get_node,     ofw_bus_gen_get_node),
179         DEVMETHOD(ofw_bus_get_type,     ofw_bus_gen_get_type),
180
181         KOBJMETHOD_END
182 };
183
184 static driver_t ebus_nexus_driver = {
185         "ebus",
186         ebus_nexus_methods,
187         sizeof(struct ebus_softc),
188 };
189
190 /*
191  * NB: we rely on the interrupt controllers of the accompanying PCI-Express
192  * bridge to be registered as the nexus variant of the EBus bridges doesn't
193  * employ its own one.
194  */
195 EARLY_DRIVER_MODULE(ebus, nexus, ebus_nexus_driver, ebus_devclass, 0, 0,
196     BUS_PASS_BUS + 1);
197 MODULE_DEPEND(ebus, nexus, 1, 1, 1);
198
199 static device_method_t ebus_pci_methods[] = {
200         /* Device interface */
201         DEVMETHOD(device_probe,         ebus_pci_probe),
202         DEVMETHOD(device_attach,        ebus_pci_attach),
203         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
204         DEVMETHOD(device_suspend,       bus_generic_suspend),
205         DEVMETHOD(device_resume,        bus_generic_resume),
206
207         /* Bus interface */
208         DEVMETHOD(bus_print_child,      ebus_print_child),
209         DEVMETHOD(bus_probe_nomatch,    ebus_probe_nomatch),
210         DEVMETHOD(bus_alloc_resource,   ebus_alloc_resource),
211         DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
212         DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
213         DEVMETHOD(bus_release_resource, ebus_release_resource),
214         DEVMETHOD(bus_setup_intr,       bus_generic_setup_intr),
215         DEVMETHOD(bus_teardown_intr,    bus_generic_teardown_intr),
216         DEVMETHOD(bus_get_resource,     bus_generic_rl_get_resource),
217         DEVMETHOD(bus_get_resource_list, ebus_get_resource_list),
218         DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
219
220         /* ofw_bus interface */
221         DEVMETHOD(ofw_bus_get_devinfo,  ebus_get_devinfo),
222         DEVMETHOD(ofw_bus_get_compat,   ofw_bus_gen_get_compat),
223         DEVMETHOD(ofw_bus_get_model,    ofw_bus_gen_get_model),
224         DEVMETHOD(ofw_bus_get_name,     ofw_bus_gen_get_name),
225         DEVMETHOD(ofw_bus_get_node,     ofw_bus_gen_get_node),
226         DEVMETHOD(ofw_bus_get_type,     ofw_bus_gen_get_type),
227
228         KOBJMETHOD_END
229 };
230
231 static driver_t ebus_pci_driver = {
232         "ebus",
233         ebus_pci_methods,
234         sizeof(struct ebus_softc),
235 };
236
237 EARLY_DRIVER_MODULE(ebus, pci, ebus_pci_driver, ebus_devclass, 0, 0,
238     BUS_PASS_BUS);
239 MODULE_DEPEND(ebus, pci, 1, 1, 1);
240 MODULE_VERSION(ebus, 1);
241
242 static int
243 ebus_nexus_probe(device_t dev)
244 {
245         const char* compat;
246
247         compat = ofw_bus_get_compat(dev);
248         if (compat != NULL && strcmp(ofw_bus_get_name(dev), "ebus") == 0 &&
249             strcmp(compat, "jbus-ebus") == 0) {
250                 device_set_desc(dev, "JBus-EBus bridge");
251                 return (BUS_PROBE_GENERIC);
252         }
253         return (ENXIO);
254 }
255
256 static int
257 ebus_pci_probe(device_t dev)
258 {
259
260         if (pci_get_class(dev) != PCIC_BRIDGE ||
261             pci_get_vendor(dev) != 0x108e ||
262             strcmp(ofw_bus_get_name(dev), "ebus") != 0)
263                 return (ENXIO);
264
265         if (pci_get_device(dev) == 0x1000)
266                 device_set_desc(dev, "PCI-EBus2 bridge");
267         else if (pci_get_device(dev) == 0x1100)
268                 device_set_desc(dev, "PCI-EBus3 bridge");
269         else
270                 return (ENXIO);
271         return (BUS_PROBE_GENERIC);
272 }
273
274 static int
275 ebus_nexus_attach(device_t dev)
276 {
277         struct ebus_softc *sc;
278         phandle_t node;
279
280         sc = device_get_softc(dev);
281         node = ofw_bus_get_node(dev);
282
283 #ifndef SUN4V
284         if (OF_getprop(node, "portid", &sc->sc_ign,
285             sizeof(sc->sc_ign)) == -1) {
286                 device_printf(dev, "could not determine IGN");
287                 return (ENXIO);
288         }
289 #endif
290
291         sc->sc_nrange = OF_getprop_alloc(node, "ranges",
292             sizeof(struct ebus_nexus_ranges), &sc->sc_range);
293         if (sc->sc_nrange == -1) {
294                 printf("%s: could not get ranges property\n", __func__);
295                 return (ENXIO);
296         }
297         return (ebus_attach(dev, sc, node));
298 }
299
300 static int
301 ebus_pci_attach(device_t dev)
302 {
303         struct ebus_softc *sc;
304         struct ebus_rinfo *eri;
305         struct resource *res;
306         phandle_t node;
307         int i, rnum, rid;
308
309         sc = device_get_softc(dev);
310         sc->sc_flags |= EBUS_PCI;
311
312         pci_write_config(dev, PCIR_COMMAND,
313             pci_read_config(dev, PCIR_COMMAND, 2) | PCIM_CMD_SERRESPEN |
314             PCIM_CMD_PERRESPEN | PCIM_CMD_BUSMASTEREN | PCIM_CMD_MEMEN, 2);
315         pci_write_config(dev, PCIR_CACHELNSZ, 16 /* 64 bytes */, 1);
316         pci_write_config(dev, PCIR_LATTIMER, 64 /* 64 PCI cycles */, 1);
317
318         node = ofw_bus_get_node(dev);
319         sc->sc_nrange = OF_getprop_alloc(node, "ranges",
320             sizeof(struct isa_ranges), &sc->sc_range);
321         if (sc->sc_nrange == -1) {
322                 printf("%s: could not get ranges property\n", __func__);
323                 return (ENXIO);
324         }
325
326         sc->sc_rinfo = malloc(sizeof(*sc->sc_rinfo) * sc->sc_nrange, M_DEVBUF,
327             M_WAITOK | M_ZERO);
328
329         /* For every range, there must be a matching resource. */
330         for (rnum = 0; rnum < sc->sc_nrange; rnum++) {
331                 eri = &sc->sc_rinfo[rnum];
332                 eri->eri_rtype = ofw_isa_range_restype(
333                     &((struct isa_ranges *)sc->sc_range)[rnum]);
334                 rid = PCIR_BAR(rnum);
335                 res = bus_alloc_resource_any(dev, eri->eri_rtype, &rid,
336                     RF_ACTIVE);
337                 if (res == NULL) {
338                         printf("%s: failed to allocate range resource!\n",
339                             __func__);
340                         goto fail;
341                 }
342                 eri->eri_res = res;
343                 eri->eri_rman.rm_type = RMAN_ARRAY;
344                 eri->eri_rman.rm_descr = "EBus range";
345                 if (rman_init(&eri->eri_rman) != 0) {
346                         printf("%s: failed to initialize rman!", __func__);
347                         goto fail;
348                 }
349                 if (rman_manage_region(&eri->eri_rman, rman_get_start(res),
350                     rman_get_end(res)) != 0) {
351                         printf("%s: failed to register region!", __func__);
352                         rman_fini(&eri->eri_rman);
353                         goto fail;
354                 }
355         }
356         return (ebus_attach(dev, sc, node));
357
358  fail:
359         for (i = rnum; i >= 0; i--) {
360                 eri = &sc->sc_rinfo[i];
361                 if (i < rnum)
362                         rman_fini(&eri->eri_rman);
363                 if (eri->eri_res != 0) {
364                         bus_release_resource(dev, eri->eri_rtype,
365                             PCIR_BAR(rnum), eri->eri_res);
366                 }
367         }
368         free(sc->sc_rinfo, M_DEVBUF);
369         free(sc->sc_range, M_OFWPROP);
370         return (ENXIO);
371 }
372
373 static int
374 ebus_attach(device_t dev, struct ebus_softc *sc, phandle_t node)
375 {
376         struct ebus_devinfo *edi;
377         device_t cdev;
378
379         ofw_bus_setup_iinfo(node, &sc->sc_iinfo, sizeof(ofw_isa_intr_t));
380
381         /*
382          * Now attach our children.
383          */
384         for (node = OF_child(node); node > 0; node = OF_peer(node)) {
385                 if ((edi = ebus_setup_dinfo(dev, sc, node)) == NULL)
386                         continue;
387                 if ((cdev = device_add_child(dev, NULL, -1)) == NULL) {
388                         device_printf(dev, "<%s>: device_add_child failed\n",
389                             edi->edi_obdinfo.obd_name);
390                         ebus_destroy_dinfo(edi);
391                         continue;
392                 }
393                 device_set_ivars(cdev, edi);
394         }
395         return (bus_generic_attach(dev));
396 }
397
398 static int
399 ebus_print_child(device_t dev, device_t child)
400 {
401         int retval;
402
403         retval = bus_print_child_header(dev, child);
404         retval += ebus_print_res(device_get_ivars(child));
405         retval += bus_print_child_footer(dev, child);
406         return (retval);
407 }
408
409 static void
410 ebus_probe_nomatch(device_t dev, device_t child)
411 {
412
413         device_printf(dev, "<%s>", ofw_bus_get_name(child));
414         ebus_print_res(device_get_ivars(child));
415         printf(" (no driver attached)\n");
416 }
417
418 static struct resource *
419 ebus_alloc_resource(device_t bus, device_t child, int type, int *rid,
420     u_long start, u_long end, u_long count, u_int flags)
421 {
422         struct ebus_softc *sc;
423         struct resource_list *rl;
424         struct resource_list_entry *rle = NULL;
425         struct resource *res;
426         struct ebus_rinfo *ri;
427         struct ebus_nexus_ranges *enr;
428         bus_space_tag_t bt;
429         bus_space_handle_t bh;
430         uint64_t cend, cstart, offset;
431         int i, isdefault, passthrough, ridx, rv;
432
433         isdefault = (start == 0UL && end == ~0UL);
434         passthrough = (device_get_parent(child) != bus);
435         sc = device_get_softc(bus);
436         rl = BUS_GET_RESOURCE_LIST(bus, child);
437         switch (type) {
438         case SYS_RES_MEMORY:
439                 KASSERT(!(isdefault && passthrough),
440                     ("%s: passthrough of default allocation", __func__));
441                 if (!passthrough) {
442                         rle = resource_list_find(rl, type, *rid);
443                         if (rle == NULL)
444                                 return (NULL);
445                         KASSERT(rle->res == NULL,
446                             ("%s: resource entry is busy", __func__));
447                         if (isdefault) {
448                                 start = rle->start;
449                                 count = ulmax(count, rle->count);
450                                 end = ulmax(rle->end, start + count - 1);
451                         }
452                 }
453
454                 res = NULL;
455                 if ((sc->sc_flags & EBUS_PCI) != 0) {
456                         /*
457                          * Map EBus ranges to PCI ranges.  This may include
458                          * changing the allocation type.
459                          */
460                         (void)ofw_isa_range_map(sc->sc_range, sc->sc_nrange,
461                             &start, &end, &ridx);
462                         ri = &sc->sc_rinfo[ridx];
463                         res = rman_reserve_resource(&ri->eri_rman, start, end,
464                             count, flags, child);
465                         if (res == NULL)
466                                 return (NULL);
467                         rman_set_rid(res, *rid);
468                         bt = rman_get_bustag(ri->eri_res);
469                         rman_set_bustag(res, bt);
470                         rv = bus_space_subregion(bt,
471                             rman_get_bushandle(ri->eri_res),
472                             rman_get_start(res) - rman_get_start(ri->eri_res),
473                             count, &bh);
474                         if (rv != 0) {
475                                 rman_release_resource(res);
476                                 return (NULL);
477                         }
478                         rman_set_bushandle(res, bh);
479                 } else {
480                         /* Map EBus ranges to nexus ranges. */
481                         for (i = 0; i < sc->sc_nrange; i++) {
482                                 enr = &((struct ebus_nexus_ranges *)
483                                     sc->sc_range)[i];
484                                 cstart = (((uint64_t)enr->child_hi) << 32) |
485                                     enr->child_lo;
486                                 cend = cstart + enr->size - 1;
487                                 if (start >= cstart && end <= cend) {
488                                         offset =
489                                             (((uint64_t)enr->phys_hi) << 32) |
490                                             enr->phys_lo;
491                                         start += offset - cstart;
492                                         end += offset - cstart;
493                                         res = bus_generic_alloc_resource(bus,
494                                             child, type, rid, start, end,
495                                             count, flags);
496                                         break;
497                                 }
498                         }
499
500                 }
501                 if (!passthrough)
502                         rle->res = res;
503                 return (res);
504         case SYS_RES_IRQ:
505                 return (resource_list_alloc(rl, bus, child, type, rid, start,
506                     end, count, flags));
507         }
508         return (NULL);
509 }
510
511 static int
512 ebus_release_resource(device_t bus, device_t child, int type, int rid,
513     struct resource *res)
514 {
515         struct ebus_softc *sc;
516         struct resource_list *rl;
517         struct resource_list_entry *rle;
518         int passthrough, rv;
519
520         passthrough = (device_get_parent(child) != bus);
521         rl = BUS_GET_RESOURCE_LIST(bus, child);
522         switch (type) {
523         case SYS_RES_MEMORY:
524                 sc = device_get_softc(bus);
525                 if ((sc->sc_flags & EBUS_PCI) == 0)
526                         return (resource_list_release(rl, bus, child, type,
527                             rid, res));
528                 if ((rv = rman_release_resource(res)) != 0)
529                         return (rv);
530                 if (!passthrough) {
531                         rle = resource_list_find(rl, type, rid);
532                         KASSERT(rle != NULL,
533                             ("%s: resource entry not found!", __func__));
534                         KASSERT(rle->res != NULL,
535                            ("%s: resource entry is not busy", __func__));
536                         rle->res = NULL;
537                 }
538                 break;
539         case SYS_RES_IRQ:
540                 return (resource_list_release(rl, bus, child, type, rid, res));
541         default:
542                 panic("%s: unsupported resource type %d", __func__, type);
543         }
544         return (0);
545 }
546
547 static int
548 ebus_setup_intr(device_t dev, device_t child, struct resource *ires,
549     int flags, driver_filter_t *filt, driver_intr_t *intr, void *arg,
550     void **cookiep)
551 {
552 #ifndef SUN4V
553         struct ebus_softc *sc;
554         u_long vec;
555
556         sc = device_get_softc(dev);
557         if ((sc->sc_flags & EBUS_PCI) == 0) {
558                 /*
559                  * Make sure the vector is fully specified.  This isn't
560                  * necessarily the case with the PCI variant.
561                  */
562                 vec = rman_get_start(ires);
563                 if (INTIGN(vec) != sc->sc_ign) {
564                         device_printf(dev,
565                             "invalid interrupt vector 0x%lx\n", vec);
566                         return (EINVAL);
567                 }
568
569                 /*
570                  * As we rely on the interrupt controllers of the
571                  * accompanying PCI-Express bridge ensure at least
572                  * something is registered for this vector.
573                  */
574                 if (intr_vectors[vec].iv_ic == NULL) {
575                         device_printf(dev,
576                             "invalid interrupt controller for vector 0x%lx\n",
577                             vec);
578                         return (EINVAL);
579                 }
580         }
581 #endif
582         return (bus_generic_setup_intr(dev, child, ires, flags, filt, intr,
583             arg, cookiep));
584 }
585
586 static struct resource_list *
587 ebus_get_resource_list(device_t dev, device_t child)
588 {
589         struct ebus_devinfo *edi;
590
591         edi = device_get_ivars(child);
592         return (&edi->edi_rl);
593 }
594
595 static const struct ofw_bus_devinfo *
596 ebus_get_devinfo(device_t bus, device_t dev)
597 {
598         struct ebus_devinfo *edi;
599
600         edi = device_get_ivars(dev);
601         return (&edi->edi_obdinfo);
602 }
603
604 static struct ebus_devinfo *
605 ebus_setup_dinfo(device_t dev, struct ebus_softc *sc, phandle_t node)
606 {
607         struct isa_regs reg, *regs;
608         ofw_isa_intr_t intr, *intrs;
609         struct ebus_devinfo *edi;
610         uint64_t start;
611         uint32_t rintr;
612         int i, nintr, nreg, rv;
613         uint8_t maskbuf[sizeof(reg) + sizeof(intr)];
614
615         edi = malloc(sizeof(*edi), M_DEVBUF, M_ZERO | M_WAITOK);
616         if (ofw_bus_gen_setup_devinfo(&edi->edi_obdinfo, node) != 0) {
617                 free(edi, M_DEVBUF);
618                 return (NULL);
619         }
620         resource_list_init(&edi->edi_rl);
621         nreg = OF_getprop_alloc(node, "reg", sizeof(*regs), (void **)&regs);
622         if (nreg == -1) {
623                 device_printf(dev, "<%s>: incomplete\n",
624                     edi->edi_obdinfo.obd_name);
625                 ebus_destroy_dinfo(edi);
626                 return (NULL);
627         }
628         for (i = 0; i < nreg; i++) {
629                 start = ISA_REG_PHYS(regs + i);
630                 (void)resource_list_add(&edi->edi_rl, SYS_RES_MEMORY, i,
631                     start, start + regs[i].size - 1, regs[i].size);
632         }
633         free(regs, M_OFWPROP);
634
635         nintr = OF_getprop_alloc(node, "interrupts",  sizeof(*intrs),
636             (void **)&intrs);
637         if (nintr == -1)
638                 return (edi);
639         for (i = 0; i < nintr; i++) {
640                 rv = 0;
641                 if ((sc->sc_flags & EBUS_PCI) != 0) {
642                         rintr = ofw_isa_route_intr(dev, node, &sc->sc_iinfo,
643                             intrs[i]);
644                 } else {
645                         intr = intrs[i];
646                         rv = ofw_bus_lookup_imap(node, &sc->sc_iinfo, &reg,
647                             sizeof(reg), &intr, sizeof(intr), &rintr,
648                             sizeof(rintr), NULL, maskbuf);
649 #ifndef SUN4V
650                         if (rv != 0)
651                                 rintr = INTMAP_VEC(sc->sc_ign, rintr);
652 #endif
653                 }
654                 if ((sc->sc_flags & EBUS_PCI) == 0 ? rv == 0 :
655                     rintr == PCI_INVALID_IRQ) {
656                         device_printf(dev,
657                             "<%s>: could not map EBus interrupt %d\n",
658                             edi->edi_obdinfo.obd_name, intrs[i]);
659                         continue;
660                 }
661                 (void)resource_list_add(&edi->edi_rl, SYS_RES_IRQ, i, rintr,
662                     rintr, 1);
663         }
664         free(intrs, M_OFWPROP);
665         return (edi);
666 }
667
668 static void
669 ebus_destroy_dinfo(struct ebus_devinfo *edi)
670 {
671
672         resource_list_free(&edi->edi_rl);
673         ofw_bus_gen_destroy_devinfo(&edi->edi_obdinfo);
674         free(edi, M_DEVBUF);
675 }
676
677 static int
678 ebus_print_res(struct ebus_devinfo *edi)
679 {
680         int retval;
681
682         retval = 0;
683         retval += resource_list_print_type(&edi->edi_rl, "addr", SYS_RES_MEMORY,
684             "%#lx");
685         retval += resource_list_print_type(&edi->edi_rl, "irq", SYS_RES_IRQ,
686             "%ld");
687         return (retval);
688 }