]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/sun4v/sun4v/nexus.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / sun4v / sun4v / 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/intr_machdep.h>
51 #include <machine/nexusvar.h>
52 #include <machine/ofw_nexus.h>
53 #include <machine/resource.h>
54
55 #include <machine/hypervisorvar.h>
56 #include <machine/hv_api.h>
57
58 #include <sys/rman.h>
59
60 /*
61  * The nexus (which is a pseudo-bus actually) iterates over the nodes that
62  * hang from the Open Firmware root node and adds them as devices to this bus
63  * (except some special nodes which are excluded) so that drivers can be
64  * attached to them.
65  *
66  * Additionally, interrupt setup/teardown and some resource management are
67  * done at this level.
68  *
69  * Maybe this code should get into dev/ofw to some extent, as some of it should
70  * work for all Open Firmware based machines...
71  */
72
73 struct nexus_devinfo {
74         struct ofw_bus_devinfo  ndi_obdinfo;
75         struct resource_list    ndi_rl;
76         struct rman             ndi_intr_rman;
77
78         devhandle_t             ndi_devhandle;
79 };
80
81 struct nexus_softc {
82         struct rman     sc_mem_rman;
83 };
84
85 static device_probe_t nexus_probe;
86 static device_attach_t nexus_attach;
87 static bus_print_child_t nexus_print_child;
88 static bus_add_child_t nexus_add_child;
89 static bus_probe_nomatch_t nexus_probe_nomatch;
90 static bus_read_ivar_t nexus_read_ivar;
91 static bus_setup_intr_t nexus_setup_intr;
92 static bus_teardown_intr_t nexus_teardown_intr;
93 static bus_alloc_resource_t nexus_alloc_resource;
94 static bus_get_resource_list_t nexus_get_resource_list;
95 static bus_activate_resource_t nexus_activate_resource;
96 static bus_deactivate_resource_t nexus_deactivate_resource;
97 static bus_release_resource_t nexus_release_resource;
98 static ofw_bus_get_devinfo_t nexus_get_devinfo;
99
100 static int nexus_inlist(const char *, const char **);
101 static struct nexus_devinfo * nexus_setup_dinfo(device_t, phandle_t);
102 static void nexus_destroy_dinfo(struct nexus_devinfo *);
103 static int nexus_print_res(struct nexus_devinfo *);
104
105 static device_method_t nexus_methods[] = {
106         /* Device interface */
107         DEVMETHOD(device_probe,         nexus_probe),
108         DEVMETHOD(device_attach,        nexus_attach),
109         DEVMETHOD(device_detach,        bus_generic_detach),
110         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
111         DEVMETHOD(device_suspend,       bus_generic_suspend),
112         DEVMETHOD(device_resume,        bus_generic_resume),
113
114         /* Bus interface */
115         DEVMETHOD(bus_print_child,      nexus_print_child),
116         DEVMETHOD(bus_probe_nomatch,    nexus_probe_nomatch),
117         DEVMETHOD(bus_read_ivar,        nexus_read_ivar),
118         DEVMETHOD(bus_write_ivar,       bus_generic_write_ivar),
119         DEVMETHOD(bus_add_child,        nexus_add_child),
120         DEVMETHOD(bus_alloc_resource,   nexus_alloc_resource),
121         DEVMETHOD(bus_activate_resource,        nexus_activate_resource),
122         DEVMETHOD(bus_deactivate_resource,      nexus_deactivate_resource),
123         DEVMETHOD(bus_release_resource, nexus_release_resource),
124         DEVMETHOD(bus_setup_intr,       nexus_setup_intr),
125         DEVMETHOD(bus_teardown_intr,    nexus_teardown_intr),
126         DEVMETHOD(bus_get_resource,     bus_generic_rl_get_resource),
127         DEVMETHOD(bus_get_resource_list, nexus_get_resource_list),
128
129         /* ofw_bus interface */
130         DEVMETHOD(ofw_bus_get_devinfo,  nexus_get_devinfo),
131         DEVMETHOD(ofw_bus_get_compat,   ofw_bus_gen_get_compat),
132         DEVMETHOD(ofw_bus_get_model,    ofw_bus_gen_get_model),
133         DEVMETHOD(ofw_bus_get_name,     ofw_bus_gen_get_name),
134         DEVMETHOD(ofw_bus_get_node,     ofw_bus_gen_get_node),
135         DEVMETHOD(ofw_bus_get_type,     ofw_bus_gen_get_type),
136
137         { 0, 0 }
138 };
139
140 static devclass_t nexus_devclass;
141
142 DEFINE_CLASS_0(nexus, nexus_driver, nexus_methods, sizeof(struct nexus_softc));
143 DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0);
144
145 static const char *nexus_excl_name[] = {
146         "aliases",
147         "associations",
148         "chosen",
149         "counter-timer",        /* No separate device; handled by psycho/sbus */
150         "memory",
151         "openprom",
152         "options",
153         "packages",
154         "rsc",
155         "virtual-memory",
156         NULL
157 };
158
159 static const char *nexus_excl_type[] = {
160         "cpu",
161         NULL
162 };
163
164 extern struct bus_space_tag nexus_bustag;
165
166 #define SUN4V_REG_SPEC2CFG_HDL(x)       ((x >> 32) & ~(0xfull << 28))
167
168 static int
169 nexus_inlist(const char *name, const char **list)
170 {
171         int i;
172
173         if (name == NULL)
174                 return (0);
175         for (i = 0; list[i] != NULL; i++)
176                 if (strcmp(name, list[i]) == 0)
177                         return (1);
178         return (0);
179 }
180
181 #define NEXUS_EXCLUDED(name, type)                                      \
182         (nexus_inlist((name), nexus_excl_name) ||                       \
183         ((type) != NULL && nexus_inlist((type), nexus_excl_type)))
184
185 static int
186 nexus_probe(device_t dev)
187 {
188
189         /* Nexus does always match. */
190         device_set_desc(dev, "Open Firmware Nexus device");
191         return (0);
192 }
193
194 static int
195 nexus_attach(device_t dev)
196 {
197         struct nexus_devinfo *ndi;
198         struct nexus_softc *sc;
199         device_t cdev;
200         phandle_t node;
201
202         node = OF_peer(0);
203         if (node == -1)
204                 panic("%s: OF_peer failed.", __func__);
205
206         sc = device_get_softc(dev);
207         sc->sc_mem_rman.rm_type = RMAN_ARRAY;
208         sc->sc_mem_rman.rm_descr = "Device Memory";
209         if (rman_init(&sc->sc_mem_rman) != 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         ndi->ndi_intr_rman.rm_type = RMAN_ARRAY;
251         ndi->ndi_intr_rman.rm_descr = "Interrupts";
252         if (rman_init(&ndi->ndi_intr_rman) != 0 ||
253             rman_manage_region(&ndi->ndi_intr_rman, 0, IV_MAX - 1) != 0)
254                 panic("%s: failed to set up rmans.", __func__);
255
256         device_set_ivars(cdev, ndi);
257
258         return (cdev);
259 }
260
261 static int
262 nexus_print_child(device_t dev, device_t child)
263 {
264         int rv;
265
266         rv = bus_print_child_header(dev, child);
267         rv += nexus_print_res(device_get_ivars(child));
268         rv += bus_print_child_footer(dev, child);
269         return (rv);
270 }
271
272 static void
273 nexus_probe_nomatch(device_t dev, device_t child)
274 {
275         const char *type;
276
277         device_printf(dev, "<%s>", ofw_bus_get_name(child));
278         nexus_print_res(device_get_ivars(child));
279         type = ofw_bus_get_type(child);
280         printf(" type %s (no driver attached)\n",
281             type != NULL ? type : "unknown");
282 }
283
284 static int
285 nexus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
286 {
287         struct nexus_devinfo *ndi;
288
289         ndi = device_get_ivars(child);
290
291         switch (which) {
292         case NEXUS_IVAR_DEVHANDLE:
293                 *(uint64_t *)result = ndi->ndi_devhandle;
294                 break;
295
296         default:
297                 return (ENOENT);
298         }
299
300         return (0);
301 }
302
303 #ifdef SUN4V
304
305 static int
306 nexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags,
307     driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep)
308 {
309         struct nexus_devinfo *ndi;
310         device_t ichild;
311         uint64_t ihdl;
312         uint64_t ino;
313         int error, cpuid;
314
315         /* we need the devinfo from the immediate child */
316         ichild = child;
317         while (device_get_parent(ichild) != dev)
318                 ichild = device_get_parent(ichild);
319
320         ndi = device_get_ivars(ichild);
321
322         if (res == NULL)
323                 panic("%s: NULL interrupt resource!", __func__);
324 #ifdef DEBUG
325         printf("dev=%s child=%s\n", ofw_bus_get_name(dev), ofw_bus_get_name(child));
326 #endif
327         ino = rman_get_start(res);
328 #ifdef DEBUG
329         printf("child=%s reg=0x%lx ino=0x%lx\n", ofw_bus_get_name(child),
330                ndi->ndi_devhandle, ino);
331 #endif
332
333         if (hv_intr_devino_to_sysino(ndi->ndi_devhandle, (uint32_t)ino,
334             &ihdl) != H_EOK) {
335                 error = ENXIO;
336                 goto fail;
337         }
338
339         if ((rman_get_flags(res) & RF_SHAREABLE) == 0)
340                 flags |= INTR_EXCL;
341
342         /* We depend here on rman_activate_resource() being idempotent. */
343         if ((error = rman_activate_resource(res)))
344                 goto fail;
345
346         error = inthand_add(device_get_nameunit(child), ihdl,
347                            filt, intr, arg, flags, cookiep);
348
349         cpuid = 0;
350         if (hv_intr_settarget(ihdl, cpuid) != H_EOK) {
351                 error = ENXIO;
352                 goto fail;
353         }
354         if (hv_intr_setstate(ihdl, HV_INTR_IDLE_STATE) != H_EOK) {
355                 error = ENXIO;
356                 goto fail;
357         }
358         if (hv_intr_setenabled(ihdl, HV_INTR_ENABLED) != H_EOK) {
359                 error = ENXIO;
360                 goto fail;
361         }
362
363
364 fail:
365
366         return (error);
367 }
368
369 static int
370 nexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
371 {
372
373         inthand_remove(rman_get_start(r), ih);
374         return (0);
375 }
376
377 #else
378
379 static int
380 nexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags,
381     driver_intr_t *intr, void *arg, void **cookiep)
382 {
383         int error;
384
385         if (res == NULL)
386                 panic("%s: NULL interrupt resource!", __func__);
387
388         if ((rman_get_flags(res) & RF_SHAREABLE) == 0)
389                 flags |= INTR_EXCL;
390
391         /* We depend here on rman_activate_resource() being idempotent. */
392         error = rman_activate_resource(res);
393         if (error)
394                 return (error);
395
396         error = inthand_add(device_get_nameunit(child), rman_get_start(res),
397             intr, arg, flags, cookiep);
398
399         return (error);
400 }
401
402 static int
403 nexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
404 {
405
406         inthand_remove(rman_get_start(r), ih);
407         return (0);
408 }
409
410 #endif
411
412 static struct resource *
413 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
414     u_long start, u_long end, u_long count, u_int flags)
415 {
416         struct nexus_softc *sc;
417         struct rman *rm;
418         struct resource *rv;
419         struct resource_list_entry *rle;
420         struct nexus_devinfo *ndi;
421         device_t subord;
422         int isdefault, needactivate, passthrough;
423
424         isdefault = (start == 0UL && end == ~0UL);
425         needactivate = flags & RF_ACTIVE;
426         passthrough = (device_get_parent(child) != bus);
427         sc = device_get_softc(bus);
428         rle = NULL;
429
430         if (!passthrough) {
431                 rle = resource_list_find(BUS_GET_RESOURCE_LIST(bus, child),
432                     type, *rid);
433                 if (rle == NULL)
434                         return (NULL);
435                 if (rle->res != NULL)
436                         panic("%s: resource entry is busy", __func__);
437                 if (isdefault) {
438                         start = rle->start;
439                         count = ulmax(count, rle->count);
440                         end = ulmax(rle->end, start + count - 1);
441                 }
442         }
443
444         switch (type) {
445         case SYS_RES_IRQ:
446                 /* find the immediate subordinate */
447                 subord = child;
448                 while (device_get_parent(subord) != bus)
449                         subord = device_get_parent(subord);
450                 ndi = device_get_ivars(subord);
451                 rm = &ndi->ndi_intr_rman;
452                 break;
453         case SYS_RES_MEMORY:
454                 rm = &sc->sc_mem_rman;
455                 break;
456         default:
457                 return (NULL);
458         }
459
460         flags &= ~RF_ACTIVE;
461         rv = rman_reserve_resource(rm, start, end, count, flags, child);
462         if (type == SYS_RES_IRQ && rv == NULL) {
463                 printf("%s: start: %ld, end: %ld, flags: %#x\n", __func__,
464                     start, end, flags);
465         }
466         if (rv == NULL)
467                 return (NULL);
468         if (type == SYS_RES_MEMORY) {
469                 rman_set_bustag(rv, &nexus_bustag);
470                 rman_set_bushandle(rv, rman_get_start(rv));
471         }
472
473         if (needactivate) {
474                 if (bus_activate_resource(child, type, *rid, rv) != 0) {
475                         rman_release_resource(rv);
476                         return (NULL);
477                 }
478         }
479
480         if (!passthrough)
481                 rle->res = rv;
482
483         return (rv);
484 }
485
486 static int
487 nexus_activate_resource(device_t bus, device_t child, int type, int rid,
488     struct resource *r)
489 {
490
491         /* Not much to be done yet... */
492         return (rman_activate_resource(r));
493 }
494
495 static int
496 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
497     struct resource *r)
498 {
499
500         /* Not much to be done yet... */
501         return (rman_deactivate_resource(r));
502 }
503
504 static int
505 nexus_release_resource(device_t bus, device_t child, int type, int rid,
506                        struct resource *r)
507 {
508         int error;
509
510         if (rman_get_flags(r) & RF_ACTIVE) {
511                 error = bus_deactivate_resource(child, type, rid, r);
512                 if (error)
513                         return (error);
514         }
515         return (rman_release_resource(r));
516 }
517
518 static struct resource_list *
519 nexus_get_resource_list(device_t dev, device_t child)
520 {
521         struct nexus_devinfo *ndi;
522
523         ndi = device_get_ivars(child);
524         return (&ndi->ndi_rl);
525 }
526
527 static const struct ofw_bus_devinfo *
528 nexus_get_devinfo(device_t dev, device_t child)
529 {
530         struct nexus_devinfo *ndi;
531
532         ndi = device_get_ivars(child);
533         return (&ndi->ndi_obdinfo);
534 }
535
536 static struct nexus_devinfo *
537 nexus_setup_dinfo(device_t dev, phandle_t node)
538 {
539         struct nexus_devinfo *ndi;
540         struct nexus_regs *reg;
541         bus_addr_t phys;
542         uint32_t naddrcell, nintrcell;
543         uint32_t *intr;
544         int i, rid;
545         int nintr;
546         int nreg;
547
548         ndi = malloc(sizeof(*ndi), M_DEVBUF, M_WAITOK | M_ZERO);
549         if (ofw_bus_gen_setup_devinfo(&ndi->ndi_obdinfo, node) != 0) {
550                 free(ndi, M_DEVBUF);
551                 return (NULL);
552         }
553         if (NEXUS_EXCLUDED(ndi->ndi_obdinfo.obd_name,
554             ndi->ndi_obdinfo.obd_type)) {
555                 ofw_bus_gen_destroy_devinfo(&ndi->ndi_obdinfo);
556                 free(ndi, M_DEVBUF);
557                 return (NULL);
558         }
559         resource_list_init(&ndi->ndi_rl);
560         nreg = OF_getprop_alloc(node, "reg", sizeof(*reg), (void **)&reg);
561         if (nreg != 1) {
562                 device_printf(dev, "<%s>: incomplete: %d\n",
563                     ndi->ndi_obdinfo.obd_name, nreg);
564                 nexus_destroy_dinfo(ndi);
565                 return (NULL);
566         }
567         phys = NEXUS_REG_PHYS(&reg[0]);
568         ndi->ndi_devhandle = SUN4V_REG_SPEC2CFG_HDL(phys);
569         free(reg, M_OFWPROP);
570
571         if (OF_getprop(node, "#address-cells", &naddrcell,
572             sizeof naddrcell) != sizeof naddrcell || OF_getprop(node,
573             "#interrupt-cells", &nintrcell, sizeof nintrcell) !=
574             sizeof nintrcell || nintrcell != 1)
575                 panic("can't get cell sizes"); /* or invalid intr cell size */
576
577         nintr = OF_getprop_alloc(node, "interrupt-map",  sizeof(*intr),
578             (void **)&intr);
579         for (i = 0; i < nintr; rid++, i += naddrcell + nintrcell + 2) {
580                 /*
581                  * interrupt-map is:
582                  * addr,intr,phandle,parent's intr
583                  */
584                 rid = intr[i + naddrcell];
585                 /* XXX - technically, we need to pull the parent's
586                  * #interrupt-cells, but we'll assume it's 1 like we enforce
587                  * nintercell to be above.
588                  */
589                 resource_list_add(&ndi->ndi_rl, SYS_RES_IRQ, rid,
590                     intr[i + naddrcell + nintrcell + 1],
591                     intr[i + naddrcell + nintrcell + 1], 1);
592         }
593         free(intr, M_OFWPROP);
594
595         ndi->ndi_intr_rman.rm_type = RMAN_ARRAY;
596         ndi->ndi_intr_rman.rm_descr = "Interrupts";
597         if (rman_init(&ndi->ndi_intr_rman) != 0 ||
598             rman_manage_region(&ndi->ndi_intr_rman, 0, IV_MAX - 1) != 0)
599                 panic("%s: failed to set up rmans.", __func__);
600
601         return (ndi);
602 }
603
604 static void
605 nexus_destroy_dinfo(struct nexus_devinfo *ndi)
606 {
607
608         resource_list_free(&ndi->ndi_rl);
609         ofw_bus_gen_destroy_devinfo(&ndi->ndi_obdinfo);
610         free(ndi, M_DEVBUF);
611 }
612
613 static int
614 nexus_print_res(struct nexus_devinfo *ndi)
615 {
616         int rv;
617
618         rv = 0;
619         rv += resource_list_print_type(&ndi->ndi_rl, "mem", SYS_RES_MEMORY,
620             "%#lx");
621         rv += resource_list_print_type(&ndi->ndi_rl, "irq", SYS_RES_IRQ,
622             "%ld");
623         return (rv);
624 }
625
626