2 * Copyright 1998 Massachusetts Institute of Technology
4 * Permission to use, copy, modify, and distribute this software and
5 * its documentation for any purpose and without fee is hereby
6 * granted, provided that both the above copyright notice and this
7 * permission notice appear in all copies, that both the above
8 * copyright notice and this permission notice appear in all
9 * supporting documentation, and that the name of M.I.T. not be used
10 * in advertising or publicity pertaining to distribution of the
11 * software without specific, written prior permission. M.I.T. makes
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied
16 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
17 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * This code implements a `root nexus' for MIPS Architecture
33 * machines. The function of the root nexus is to serve as an
34 * attachment point for both processors and buses, and to manage
35 * resources which are common to all of them. In particular,
36 * this code implements the core resource managers for interrupt
37 * requests and memory address space.
39 #include "opt_platform.h"
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
45 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/module.h>
51 #include <sys/interrupt.h>
56 #include <machine/bus.h>
57 #include <machine/resource.h>
58 #include <machine/vmparam.h>
61 #include <machine/intr.h>
63 #include <machine/intr_machdep.h>
67 #include <dev/ofw/ofw_bus_subr.h>
68 #include <dev/ofw/openfirm.h>
69 #include "ofw_bus_if.h"
74 #define dprintf printf
76 #define dprintf(x, arg...)
77 #endif /* NEXUS_DEBUG */
80 #define NUM_MIPS_IRQS NIRQ /* Any INTRNG-mapped IRQ */
82 #define NUM_MIPS_IRQS 6 /* HW IRQs only */
85 static MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device");
88 struct resource_list nx_resources;
91 #define DEVTONX(dev) ((struct nexus_device *)device_get_ivars(dev))
93 static struct rman irq_rman;
94 static struct rman mem_rman;
96 static struct resource *
97 nexus_alloc_resource(device_t, device_t, int, int *, rman_res_t,
98 rman_res_t, rman_res_t, u_int);
99 static device_t nexus_add_child(device_t, u_int, const char *, int);
100 static int nexus_attach(device_t);
101 static void nexus_delete_resource(device_t, device_t, int, int);
102 static struct resource_list *
103 nexus_get_reslist(device_t, device_t);
104 static int nexus_get_resource(device_t, device_t, int, int, rman_res_t *,
106 static int nexus_print_child(device_t, device_t);
107 static int nexus_print_all_resources(device_t dev);
108 static int nexus_probe(device_t);
109 static int nexus_release_resource(device_t, device_t, int, int,
111 static int nexus_set_resource(device_t, device_t, int, int, rman_res_t,
113 static int nexus_activate_resource(device_t, device_t, int, int,
115 static int nexus_deactivate_resource(device_t, device_t, int, int,
117 static void nexus_hinted_child(device_t, const char *, int);
118 static int nexus_setup_intr(device_t dev, device_t child,
119 struct resource *res, int flags, driver_filter_t *filt,
120 driver_intr_t *intr, void *arg, void **cookiep);
121 static int nexus_teardown_intr(device_t, device_t, struct resource *,
125 static int nexus_bind_intr(device_t, device_t, struct resource *, int);
128 static int nexus_ofw_map_intr(device_t dev, device_t child,
129 phandle_t iparent, int icells, pcell_t *intr);
131 static int nexus_describe_intr(device_t dev, device_t child,
132 struct resource *irq, void *cookie, const char *descr);
133 static int nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
134 enum intr_polarity pol);
137 static device_method_t nexus_methods[] = {
138 /* Device interface */
139 DEVMETHOD(device_probe, nexus_probe),
140 DEVMETHOD(device_attach, nexus_attach),
143 DEVMETHOD(bus_add_child, nexus_add_child),
144 DEVMETHOD(bus_alloc_resource, nexus_alloc_resource),
145 DEVMETHOD(bus_delete_resource, nexus_delete_resource),
146 DEVMETHOD(bus_get_resource, nexus_get_resource),
147 DEVMETHOD(bus_get_resource_list, nexus_get_reslist),
148 DEVMETHOD(bus_print_child, nexus_print_child),
149 DEVMETHOD(bus_release_resource, nexus_release_resource),
150 DEVMETHOD(bus_set_resource, nexus_set_resource),
151 DEVMETHOD(bus_setup_intr, nexus_setup_intr),
152 DEVMETHOD(bus_teardown_intr, nexus_teardown_intr),
153 DEVMETHOD(bus_activate_resource,nexus_activate_resource),
154 DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource),
155 DEVMETHOD(bus_hinted_child, nexus_hinted_child),
157 DEVMETHOD(bus_config_intr, nexus_config_intr),
158 DEVMETHOD(bus_describe_intr, nexus_describe_intr),
160 DEVMETHOD(bus_bind_intr, nexus_bind_intr),
163 DEVMETHOD(ofw_bus_map_intr, nexus_ofw_map_intr),
170 static driver_t nexus_driver = {
175 static devclass_t nexus_devclass;
178 nexus_probe(device_t dev)
181 device_set_desc(dev, "MIPS32 root nexus");
183 irq_rman.rm_start = 0;
184 irq_rman.rm_end = NUM_MIPS_IRQS - 1;
185 irq_rman.rm_type = RMAN_ARRAY;
186 irq_rman.rm_descr = "Hardware IRQs";
187 if (rman_init(&irq_rman) != 0 ||
188 rman_manage_region(&irq_rman, 0, NUM_MIPS_IRQS - 1) != 0) {
189 panic("%s: irq_rman", __func__);
192 mem_rman.rm_start = 0;
193 mem_rman.rm_end = BUS_SPACE_MAXADDR;
194 mem_rman.rm_type = RMAN_ARRAY;
195 mem_rman.rm_descr = "Memory addresses";
196 if (rman_init(&mem_rman) != 0 ||
197 rman_manage_region(&mem_rman, 0, BUS_SPACE_MAXADDR) != 0) {
198 panic("%s: mem_rman", __func__);
205 nexus_attach(device_t dev)
207 #if defined(INTRNG) && !defined(FDT)
210 if ((error = mips_pic_map_fixed_intrs()))
214 bus_generic_probe(dev);
215 bus_enumerate_hinted_children(dev);
216 bus_generic_attach(dev);
222 nexus_print_child(device_t bus, device_t child)
226 retval += bus_print_child_header(bus, child);
227 retval += nexus_print_all_resources(child);
228 if (device_get_flags(child))
229 retval += printf(" flags %#x", device_get_flags(child));
230 retval += printf(" on %s\n", device_get_nameunit(bus));
236 nexus_print_all_resources(device_t dev)
238 struct nexus_device *ndev = DEVTONX(dev);
239 struct resource_list *rl = &ndev->nx_resources;
242 if (STAILQ_FIRST(rl))
243 retval += printf(" at");
245 retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#jx");
246 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd");
252 nexus_add_child(device_t bus, u_int order, const char *name, int unit)
255 struct nexus_device *ndev;
257 ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO);
260 resource_list_init(&ndev->nx_resources);
262 child = device_add_child_ordered(bus, order, name, unit);
264 device_printf(bus, "failed to add child: %s%d\n", name, unit);
268 /* should we free this in nexus_child_detached? */
269 device_set_ivars(child, ndev);
275 * Allocate a resource on behalf of child. NB: child is usually going to be a
276 * child of one of our descendants, not a direct child of nexus0.
277 * (Exceptions include footbridge.)
279 static struct resource *
280 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
281 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
283 struct nexus_device *ndev = DEVTONX(child);
285 struct resource_list_entry *rle;
287 int isdefault, needactivate, passthrough;
289 dprintf("%s: entry (%p, %p, %d, %p, %p, %p, %jd, %d)\n",
290 __func__, bus, child, type, rid, (void *)(intptr_t)start,
291 (void *)(intptr_t)end, count, flags);
292 dprintf("%s: requested rid is %d\n", __func__, *rid);
294 isdefault = (RMAN_IS_DEFAULT_RANGE(start, end) && count == 1);
295 needactivate = flags & RF_ACTIVE;
296 passthrough = (device_get_parent(child) != bus);
300 * If this is an allocation of the "default" range for a given RID,
301 * and we know what the resources for this device are (ie. they aren't
302 * maintained by a child bus), then work out the start/end values.
304 if (!passthrough && isdefault) {
305 rle = resource_list_find(&ndev->nx_resources, type, *rid);
308 if (rle->res != NULL) {
309 panic("%s: resource entry is busy", __func__);
324 printf("%s: unknown resource type %d\n", __func__, type);
328 rv = rman_reserve_resource(rm, start, end, count, flags, child);
330 printf("%s: could not reserve resource for %s\n", __func__,
331 device_get_nameunit(child));
335 rman_set_rid(rv, *rid);
338 if (bus_activate_resource(child, type, *rid, rv)) {
339 printf("%s: could not activate resource\n", __func__);
340 rman_release_resource(rv);
348 static struct resource_list *
349 nexus_get_reslist(device_t dev, device_t child)
351 struct nexus_device *ndev = DEVTONX(child);
353 return (&ndev->nx_resources);
357 nexus_set_resource(device_t dev, device_t child, int type, int rid,
358 rman_res_t start, rman_res_t count)
360 struct nexus_device *ndev = DEVTONX(child);
361 struct resource_list *rl = &ndev->nx_resources;
362 struct resource_list_entry *rle;
364 dprintf("%s: entry (%p, %p, %d, %d, %p, %jd)\n",
365 __func__, dev, child, type, rid, (void *)(intptr_t)start, count);
367 rle = resource_list_add(rl, type, rid, start, start + count - 1,
376 nexus_get_resource(device_t dev, device_t child, int type, int rid,
377 rman_res_t *startp, rman_res_t *countp)
379 struct nexus_device *ndev = DEVTONX(child);
380 struct resource_list *rl = &ndev->nx_resources;
381 struct resource_list_entry *rle;
383 rle = resource_list_find(rl, type, rid);
387 *startp = rle->start;
389 *countp = rle->count;
394 nexus_delete_resource(device_t dev, device_t child, int type, int rid)
396 struct nexus_device *ndev = DEVTONX(child);
397 struct resource_list *rl = &ndev->nx_resources;
399 dprintf("%s: entry\n", __func__);
401 resource_list_delete(rl, type, rid);
405 nexus_release_resource(device_t bus, device_t child, int type, int rid,
408 dprintf("%s: entry\n", __func__);
410 if (rman_get_flags(r) & RF_ACTIVE) {
411 int error = bus_deactivate_resource(child, type, rid, r);
416 return (rman_release_resource(r));
420 nexus_activate_resource(device_t bus, device_t child, int type, int rid,
429 * If this is a memory resource, use pmap_mapdev to map it.
431 if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
432 paddr = rman_get_start(r);
433 psize = rman_get_size(r);
434 rman_set_bustag(r, mips_bus_space_generic);
435 err = bus_space_map(rman_get_bustag(r), paddr, psize, 0,
436 (bus_space_handle_t *)&vaddr);
438 rman_deactivate_resource(r);
441 rman_set_virtual(r, vaddr);
442 rman_set_bushandle(r, (bus_space_handle_t)(uintptr_t)vaddr);
443 } else if (type == SYS_RES_IRQ) {
445 err = mips_pic_activate_intr(child, r);
447 rman_deactivate_resource(r);
453 return (rman_activate_resource(r));
457 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
460 bus_space_handle_t vaddr;
463 vaddr = rman_get_bushandle(r);
465 if (type == SYS_RES_MEMORY && vaddr != 0) {
466 psize = (bus_size_t)rman_get_size(r);
467 bus_space_unmap(rman_get_bustag(r), vaddr, psize);
468 rman_set_virtual(r, NULL);
469 rman_set_bushandle(r, 0);
470 } else if (type == SYS_RES_IRQ) {
472 mips_pic_deactivate_intr(child, r);
476 return (rman_deactivate_resource(r));
480 nexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags,
481 driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep)
484 return (intr_setup_irq(child, res, filt, intr, arg, flags, cookiep));
490 irq = rman_get_start(res);
491 if (irq >= NUM_MIPS_IRQS) {
496 cpu_establish_hardintr(device_get_nameunit(child), filt, intr, arg,
497 irq, flags, cookiep);
505 nexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
509 return (intr_teardown_irq(child, r, ih));
511 printf("Unimplemented %s at %s:%d\n", __func__, __FILE__, __LINE__);
518 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
519 enum intr_polarity pol)
522 device_printf(dev, "bus_config_intr is obsolete and not supported!\n");
527 nexus_describe_intr(device_t dev, device_t child, struct resource *irq,
528 void *cookie, const char *descr)
531 return (intr_describe_irq(child, irq, cookie, descr));
536 nexus_bind_intr(device_t dev, device_t child, struct resource *irq, int cpu)
539 return (intr_bind_irq(child, irq, cpu));
545 nexus_ofw_map_intr(device_t dev, device_t child, phandle_t iparent, int icells,
549 struct intr_map_data_fdt *fdt_data;
552 len = sizeof(*fdt_data) + icells * sizeof(pcell_t);
553 fdt_data = (struct intr_map_data_fdt *)intr_alloc_map_data(
554 INTR_MAP_DATA_FDT, len, M_WAITOK | M_ZERO);
555 fdt_data->iparent = iparent;
556 fdt_data->ncells = icells;
557 memcpy(fdt_data->cells, intr, icells * sizeof(pcell_t));
558 irq = intr_map_irq(NULL, iparent, (struct intr_map_data *)fdt_data);
565 nexus_hinted_child(device_t bus, const char *dname, int dunit)
575 if ((resource_int_value(dname, dunit, "order", &order)) != 0)
577 child = BUS_ADD_CHILD(bus, order, dname, dunit);
582 * Set hard-wired resources for hinted child using
586 if (resource_long_value(dname, dunit, "maddr", &maddr) == 0)
588 if (resource_int_value(dname, dunit, "msize", &msize) == 0)
591 /* check if all info for mem resource has been provided */
592 if ((mem_hints_count > 0) && (mem_hints_count < 2)) {
593 printf("Either maddr or msize hint is missing for %s%d\n",
596 else if (mem_hints_count) {
597 dprintf("%s: discovered hinted child %s at maddr %p(%d)\n",
598 __func__, device_get_nameunit(child),
599 (void *)(intptr_t)maddr, msize);
601 result = bus_set_resource(child, SYS_RES_MEMORY, 0,
602 (u_long) maddr, msize);
605 "warning: bus_set_resource() failed\n");
609 if (resource_int_value(dname, dunit, "irq", &irq) == 0) {
610 result = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
613 "warning: bus_set_resource() failed\n");
617 EARLY_DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0,
618 BUS_PASS_BUS + BUS_PASS_ORDER_EARLY);