]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sparc64/sparc64/nexus.c
This commit was generated by cvs2svn to compensate for changes in r167817,
[FreeBSD/FreeBSD.git] / sys / sparc64 / sparc64 / nexus.c
1 /*-
2  * Copyright 1998 Massachusetts Institute of Technology
3  * Copyright 2001 by Thomas Moestl <tmm@FreeBSD.org>.
4  * Copyright 2006 by Marius Strobl <marius@FreeBSD.org>.
5  * All rights reserved.
6  *
7  * Permission to use, copy, modify, and distribute this software and
8  * its documentation for any purpose and without fee is hereby
9  * granted, provided that both the above copyright notice and this
10  * permission notice appear in all copies, that both the above
11  * copyright notice and this permission notice appear in all
12  * supporting documentation, and that the name of M.I.T. not be used
13  * in advertising or publicity pertaining to distribution of the
14  * software without specific, written prior permission.  M.I.T. makes
15  * no representations about the suitability of this software for any
16  * purpose.  It is provided "as is" without express or implied
17  * warranty.
18  *
19  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
20  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
21  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
23  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *      from: FreeBSD: src/sys/i386/i386/nexus.c,v 1.43 2001/02/09
33  */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/bus.h>
41 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
44
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
47 #include <dev/ofw/openfirm.h>
48
49 #include <machine/bus.h>
50 #include <machine/bus_common.h>
51 #include <machine/intr_machdep.h>
52 #include <machine/ofw_nexus.h>
53 #include <machine/resource.h>
54 #include <machine/ver.h>
55
56 #include <sys/rman.h>
57
58 /*
59  * The nexus (which is a pseudo-bus actually) iterates over the nodes that
60  * hang from the Open Firmware root node and adds them as devices to this bus
61  * (except some special nodes which are excluded) so that drivers can be
62  * attached to them.
63  *
64  * Additionally, interrupt setup/teardown and some resource management are
65  * done at this level.
66  *
67  * Maybe this code should get into dev/ofw to some extent, as some of it should
68  * work for all Open Firmware based machines...
69  */
70
71 struct nexus_devinfo {
72         struct ofw_bus_devinfo  ndi_obdinfo;
73         struct resource_list    ndi_rl;
74 };
75
76 struct nexus_softc {
77         struct rman     sc_intr_rman;
78         struct rman     sc_mem_rman;
79 };
80
81 static device_probe_t nexus_probe;
82 static device_attach_t nexus_attach;
83 static bus_print_child_t nexus_print_child;
84 static bus_add_child_t nexus_add_child;
85 static bus_probe_nomatch_t nexus_probe_nomatch;
86 static bus_setup_intr_t nexus_setup_intr;
87 static bus_teardown_intr_t nexus_teardown_intr;
88 static bus_alloc_resource_t nexus_alloc_resource;
89 static bus_activate_resource_t nexus_activate_resource;
90 static bus_deactivate_resource_t nexus_deactivate_resource;
91 static bus_release_resource_t nexus_release_resource;
92 static bus_get_resource_list_t nexus_get_resource_list;
93 static bus_get_dma_tag_t nexus_get_dma_tag;
94 static ofw_bus_get_devinfo_t nexus_get_devinfo;
95
96 static int nexus_inlist(const char *, const char **);
97 static struct nexus_devinfo * nexus_setup_dinfo(device_t, phandle_t);
98 static void nexus_destroy_dinfo(struct nexus_devinfo *);
99 static int nexus_print_res(struct nexus_devinfo *);
100
101 static device_method_t nexus_methods[] = {
102         /* Device interface */
103         DEVMETHOD(device_probe,         nexus_probe),
104         DEVMETHOD(device_attach,        nexus_attach),
105         DEVMETHOD(device_detach,        bus_generic_detach),
106         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
107         DEVMETHOD(device_suspend,       bus_generic_suspend),
108         DEVMETHOD(device_resume,        bus_generic_resume),
109
110         /* Bus interface */
111         DEVMETHOD(bus_print_child,      nexus_print_child),
112         DEVMETHOD(bus_probe_nomatch,    nexus_probe_nomatch),
113         DEVMETHOD(bus_read_ivar,        bus_generic_read_ivar),
114         DEVMETHOD(bus_write_ivar,       bus_generic_write_ivar),
115         DEVMETHOD(bus_add_child,        nexus_add_child),
116         DEVMETHOD(bus_alloc_resource,   nexus_alloc_resource),
117         DEVMETHOD(bus_activate_resource,        nexus_activate_resource),
118         DEVMETHOD(bus_deactivate_resource,      nexus_deactivate_resource),
119         DEVMETHOD(bus_release_resource, nexus_release_resource),
120         DEVMETHOD(bus_setup_intr,       nexus_setup_intr),
121         DEVMETHOD(bus_teardown_intr,    nexus_teardown_intr),
122         DEVMETHOD(bus_get_resource,     bus_generic_rl_get_resource),
123         DEVMETHOD(bus_get_resource_list, nexus_get_resource_list),
124         DEVMETHOD(bus_get_dma_tag,      nexus_get_dma_tag),
125
126         /* ofw_bus interface */
127         DEVMETHOD(ofw_bus_get_devinfo,  nexus_get_devinfo),
128         DEVMETHOD(ofw_bus_get_compat,   ofw_bus_gen_get_compat),
129         DEVMETHOD(ofw_bus_get_model,    ofw_bus_gen_get_model),
130         DEVMETHOD(ofw_bus_get_name,     ofw_bus_gen_get_name),
131         DEVMETHOD(ofw_bus_get_node,     ofw_bus_gen_get_node),
132         DEVMETHOD(ofw_bus_get_type,     ofw_bus_gen_get_type),
133
134         { 0, 0 }
135 };
136
137 static devclass_t nexus_devclass;
138
139 DEFINE_CLASS_0(nexus, nexus_driver, nexus_methods, sizeof(struct nexus_softc));
140 DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0);
141
142 static const char *nexus_excl_name[] = {
143         "aliases",
144         "associations",
145         "chosen",
146         "counter-timer",        /* No separate device; handled by psycho/sbus */
147         "memory",
148         "openprom",
149         "options",
150         "packages",
151         "rsc",
152         "virtual-memory",
153         NULL
154 };
155
156 static const char *nexus_excl_type[] = {
157         "cpu",
158         NULL
159 };
160
161 extern struct bus_space_tag nexus_bustag;
162 extern struct bus_dma_tag nexus_dmatag;
163
164 static int
165 nexus_inlist(const char *name, const char **list)
166 {
167         int i;
168
169         if (name == NULL)
170                 return (0);
171         for (i = 0; list[i] != NULL; i++)
172                 if (strcmp(name, list[i]) == 0)
173                         return (1);
174         return (0);
175 }
176
177 #define NEXUS_EXCLUDED(name, type)                                      \
178         (nexus_inlist((name), nexus_excl_name) ||                       \
179         ((type) != NULL && nexus_inlist((type), nexus_excl_type)))
180
181 static int
182 nexus_probe(device_t dev)
183 {
184
185         /* Nexus does always match. */
186         device_set_desc(dev, "Open Firmware Nexus device");
187         return (0);
188 }
189
190 static int
191 nexus_attach(device_t dev)
192 {
193         struct nexus_devinfo *ndi;
194         struct nexus_softc *sc;
195         device_t cdev;
196         phandle_t node;
197
198         node = OF_peer(0);
199         if (node == -1)
200                 panic("%s: OF_peer failed.", __func__);
201
202         sc = device_get_softc(dev);
203         sc->sc_intr_rman.rm_type = RMAN_ARRAY;
204         sc->sc_intr_rman.rm_descr = "Interrupts";
205         sc->sc_mem_rman.rm_type = RMAN_ARRAY;
206         sc->sc_mem_rman.rm_descr = "Device Memory";
207         if (rman_init(&sc->sc_intr_rman) != 0 ||
208             rman_init(&sc->sc_mem_rman) != 0 ||
209             rman_manage_region(&sc->sc_intr_rman, 0, IV_MAX - 1) != 0 ||
210             rman_manage_region(&sc->sc_mem_rman, 0ULL, ~0ULL) != 0)
211                 panic("%s: failed to set up rmans.", __func__);
212
213         /*
214          * Allow devices to identify.
215          */
216         bus_generic_probe(dev);
217
218         /*
219          * Now walk the OFW tree and attach top-level devices.
220          */
221         for (node = OF_child(node); node > 0; node = OF_peer(node)) {
222                 if ((ndi = nexus_setup_dinfo(dev, node)) == NULL)
223                         continue;
224                 cdev = device_add_child(dev, NULL, -1);
225                 if (cdev == NULL) {
226                         device_printf(dev, "<%s>: device_add_child failed\n",
227                             ndi->ndi_obdinfo.obd_name);
228                         nexus_destroy_dinfo(ndi);
229                         continue;
230                 }
231                 device_set_ivars(cdev, ndi);
232         }
233         return (bus_generic_attach(dev));
234 }
235
236 static device_t
237 nexus_add_child(device_t dev, int order, const char *name, int unit)
238 {
239         device_t cdev;
240         struct nexus_devinfo *ndi;
241
242         cdev = device_add_child_ordered(dev, order, name, unit);
243         if (cdev == NULL)
244                 return (NULL);
245
246         ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
247         ndi->ndi_obdinfo.obd_node = -1;
248         ndi->ndi_obdinfo.obd_name = strdup(name, M_OFWPROP);
249         resource_list_init(&ndi->ndi_rl);
250         device_set_ivars(cdev, ndi);
251
252         return (cdev);
253 }
254
255 static int
256 nexus_print_child(device_t dev, device_t child)
257 {
258         int rv;
259
260         rv = bus_print_child_header(dev, child);
261         rv += nexus_print_res(device_get_ivars(child));
262         rv += bus_print_child_footer(dev, child);
263         return (rv);
264 }
265
266 static void
267 nexus_probe_nomatch(device_t dev, device_t child)
268 {
269         const char *type;
270
271         device_printf(dev, "<%s>", ofw_bus_get_name(child));
272         nexus_print_res(device_get_ivars(child));
273         type = ofw_bus_get_type(child);
274         printf(" type %s (no driver attached)\n",
275             type != NULL ? type : "unknown");
276 }
277
278 static int
279 nexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags,
280     driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep)
281 {
282         int error;
283
284         if (res == NULL)
285                 panic("%s: NULL interrupt resource!", __func__);
286
287         if ((rman_get_flags(res) & RF_SHAREABLE) == 0)
288                 flags |= INTR_EXCL;
289
290         /* We depend here on rman_activate_resource() being idempotent. */
291         error = rman_activate_resource(res);
292         if (error)
293                 return (error);
294
295         error = inthand_add(device_get_nameunit(child), rman_get_start(res),
296             filt, intr, arg, flags, cookiep);
297
298         /*
299          * XXX in case of the AFB/FFB interrupt and a Psycho, Sabre or U2S
300          * bridge enable the interrupt in the respective bridge.
301          */
302
303         return (error);
304 }
305
306 static int
307 nexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
308 {
309
310         inthand_remove(rman_get_start(r), ih);
311         return (0);
312 }
313
314 static struct resource *
315 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
316     u_long start, u_long end, u_long count, u_int flags)
317 {
318         struct nexus_softc *sc;
319         struct rman *rm;
320         struct resource *rv;
321         struct resource_list_entry *rle;
322         int isdefault, needactivate, passthrough;
323
324         isdefault = (start == 0UL && end == ~0UL);
325         needactivate = flags & RF_ACTIVE;
326         passthrough = (device_get_parent(child) != bus);
327         sc = device_get_softc(bus);
328         rle = NULL;
329
330         if (!passthrough) {
331                 rle = resource_list_find(BUS_GET_RESOURCE_LIST(bus, child),
332                     type, *rid);
333                 if (rle == NULL)
334                         return (NULL);
335                 if (rle->res != NULL)
336                         panic("%s: resource entry is busy", __func__);
337                 if (isdefault) {
338                         start = rle->start;
339                         count = ulmax(count, rle->count);
340                         end = ulmax(rle->end, start + count - 1);
341                 }
342         }
343
344         switch (type) {
345         case SYS_RES_IRQ:
346                 rm = &sc->sc_intr_rman;
347                 break;
348         case SYS_RES_MEMORY:
349                 rm = &sc->sc_mem_rman;
350                 break;
351         default:
352                 return (NULL);
353         }
354
355         flags &= ~RF_ACTIVE;
356         rv = rman_reserve_resource(rm, start, end, count, flags, child);
357         if (rv == NULL)
358                 return (NULL);
359         rman_set_rid(rv, *rid);
360         if (type == SYS_RES_MEMORY) {
361                 rman_set_bustag(rv, &nexus_bustag);
362                 rman_set_bushandle(rv, rman_get_start(rv));
363         }
364
365         if (needactivate) {
366                 if (bus_activate_resource(child, type, *rid, rv) != 0) {
367                         rman_release_resource(rv);
368                         return (NULL);
369                 }
370         }
371
372         if (!passthrough) {
373                 rle->res = rv;
374                 rle->start = rman_get_start(rv);
375                 rle->end = rman_get_end(rv);
376                 rle->count = rle->end - rle->start + 1;
377         }
378
379         return (rv);
380 }
381
382 static int
383 nexus_activate_resource(device_t bus, device_t child, int type, int rid,
384     struct resource *r)
385 {
386
387         /* Not much to be done yet... */
388         return (rman_activate_resource(r));
389 }
390
391 static int
392 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
393     struct resource *r)
394 {
395
396         /* Not much to be done yet... */
397         return (rman_deactivate_resource(r));
398 }
399
400 static int
401 nexus_release_resource(device_t bus, device_t child, int type, int rid,
402     struct resource *r)
403 {
404         int error;
405
406         if (rman_get_flags(r) & RF_ACTIVE) {
407                 error = bus_deactivate_resource(child, type, rid, r);
408                 if (error)
409                         return (error);
410         }
411         return (rman_release_resource(r));
412 }
413
414 static struct resource_list *
415 nexus_get_resource_list(device_t dev, device_t child)
416 {
417         struct nexus_devinfo *ndi;
418
419         ndi = device_get_ivars(child);
420         return (&ndi->ndi_rl);
421 }
422
423 static bus_dma_tag_t
424 nexus_get_dma_tag(device_t bus, device_t child)
425 {
426
427         return (&nexus_dmatag);
428 }
429
430 static const struct ofw_bus_devinfo *
431 nexus_get_devinfo(device_t dev, device_t child)
432 {
433         struct nexus_devinfo *ndi;
434
435         ndi = device_get_ivars(child);
436         return (&ndi->ndi_obdinfo);
437 }
438
439 static struct nexus_devinfo *
440 nexus_setup_dinfo(device_t dev, phandle_t node)
441 {
442         struct nexus_devinfo *ndi;
443         struct nexus_regs *reg;
444         bus_addr_t phys;
445         bus_size_t size;
446         uint32_t ign;
447         uint32_t *intr;
448         int i;
449         int nintr;
450         int nreg;
451
452         ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
453         if (ofw_bus_gen_setup_devinfo(&ndi->ndi_obdinfo, node) != 0) {
454                 free(ndi, M_DEVBUF);
455                 return (NULL);
456         }
457         if (NEXUS_EXCLUDED(ndi->ndi_obdinfo.obd_name,
458             ndi->ndi_obdinfo.obd_type)) {
459                 ofw_bus_gen_destroy_devinfo(&ndi->ndi_obdinfo);
460                 free(ndi, M_DEVBUF);
461                 return (NULL);
462         }
463         resource_list_init(&ndi->ndi_rl);
464         nreg = OF_getprop_alloc(node, "reg", sizeof(*reg), (void **)&reg);
465         if (nreg == -1) {
466                 device_printf(dev, "<%s>: incomplete\n",
467                     ndi->ndi_obdinfo.obd_name);
468                 goto fail;
469         }
470         for (i = 0; i < nreg; i++) {
471                 phys = NEXUS_REG_PHYS(&reg[i]);
472                 size = NEXUS_REG_SIZE(&reg[i]);
473                 resource_list_add(&ndi->ndi_rl, SYS_RES_MEMORY, i, phys,
474                     phys + size - 1, size);
475         }
476         free(reg, M_OFWPROP);
477
478         nintr = OF_getprop_alloc(node, "interrupts",  sizeof(*intr),
479             (void **)&intr);
480         if (nintr > 0) {
481                 if (OF_getprop(node, cpu_impl < CPU_IMPL_ULTRASPARCIII ?
482                     "upa-portid" : "portid", &ign, sizeof(ign)) <= 0) {
483                         device_printf(dev, "<%s>: could not determine portid\n",
484                             ndi->ndi_obdinfo.obd_name);
485                         free(intr, M_OFWPROP);
486                         goto fail;
487                 }
488
489                 /* XXX 7-bit MID on Starfire */
490                 ign = (ign << INTMAP_IGN_SHIFT) & INTMAP_IGN_MASK;
491                 for (i = 0; i < nintr; i++) {
492                         intr[i] |= ign;
493                         resource_list_add(&ndi->ndi_rl, SYS_RES_IRQ, i, intr[i],
494                             intr[i], 1);
495                 }
496                 free(intr, M_OFWPROP);
497         }
498
499         return (ndi);
500
501  fail:
502         nexus_destroy_dinfo(ndi);
503         return (NULL);
504 }
505
506 static void
507 nexus_destroy_dinfo(struct nexus_devinfo *ndi)
508 {
509
510         resource_list_free(&ndi->ndi_rl);
511         ofw_bus_gen_destroy_devinfo(&ndi->ndi_obdinfo);
512         free(ndi, M_DEVBUF);
513 }
514
515 static int
516 nexus_print_res(struct nexus_devinfo *ndi)
517 {
518         int rv;
519
520         rv = 0;
521         rv += resource_list_print_type(&ndi->ndi_rl, "mem", SYS_RES_MEMORY,
522             "%#lx");
523         rv += resource_list_print_type(&ndi->ndi_rl, "irq", SYS_RES_IRQ,
524             "%ld");
525         return (rv);
526 }