FreeBSD/Linux Kernel Cross Reference
sys/mips/mips/nexus.c
1 /*-
2 * Copyright 1998 Massachusetts Institute of Technology
3 *
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
14 * warranty.
15 *
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
27 * SUCH DAMAGE.
28 *
29 */
30
31 /*
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.
38 */
39 #include "opt_platform.h"
40
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/bus.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/module.h>
50 #include <sys/rman.h>
51 #include <sys/interrupt.h>
52
53 #include <vm/vm.h>
54 #include <vm/pmap.h>
55
56 #include <machine/bus.h>
57 #include <machine/resource.h>
58 #include <machine/vmparam.h>
59
60 #ifdef INTRNG
61 #include <machine/intr.h>
62 #else
63 #include <machine/intr_machdep.h>
64 #endif
65
66 #ifdef FDT
67 #include <dev/ofw/ofw_bus_subr.h>
68 #include <dev/ofw/openfirm.h>
69 #include "ofw_bus_if.h"
70 #endif
71
72 #undef NEXUS_DEBUG
73 #ifdef NEXUS_DEBUG
74 #define dprintf printf
75 #else
76 #define dprintf(x, arg...)
77 #endif /* NEXUS_DEBUG */
78
79 #ifdef INTRNG
80 #define NUM_MIPS_IRQS intr_nirq /* Any INTRNG-mapped IRQ */
81 #else
82 #define NUM_MIPS_IRQS 6 /* HW IRQs only */
83 #endif
84
85 static MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device");
86
87 struct nexus_device {
88 struct resource_list nx_resources;
89 };
90
91 #define DEVTONX(dev) ((struct nexus_device *)device_get_ivars(dev))
92
93 static struct rman irq_rman;
94 static struct rman mem_rman;
95
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 *,
105 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,
110 struct resource *);
111 static int nexus_set_resource(device_t, device_t, int, int, rman_res_t,
112 rman_res_t);
113 static int nexus_activate_resource(device_t, device_t, int, int,
114 struct resource *);
115 static int nexus_deactivate_resource(device_t, device_t, int, int,
116 struct resource *);
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 *,
122 void *);
123 #ifdef INTRNG
124 #ifdef SMP
125 static int nexus_bind_intr(device_t, device_t, struct resource *, int);
126 #endif
127 #ifdef FDT
128 static int nexus_ofw_map_intr(device_t dev, device_t child,
129 phandle_t iparent, int icells, pcell_t *intr);
130 #endif
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);
135 #endif
136
137 static device_method_t nexus_methods[] = {
138 /* Device interface */
139 DEVMETHOD(device_probe, nexus_probe),
140 DEVMETHOD(device_attach, nexus_attach),
141
142 /* Bus interface */
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),
156 #ifdef INTRNG
157 DEVMETHOD(bus_config_intr, nexus_config_intr),
158 DEVMETHOD(bus_describe_intr, nexus_describe_intr),
159 #ifdef SMP
160 DEVMETHOD(bus_bind_intr, nexus_bind_intr),
161 #endif
162 #ifdef FDT
163 DEVMETHOD(ofw_bus_map_intr, nexus_ofw_map_intr),
164 #endif
165 #endif
166 { 0, 0 }
167 };
168
169 static driver_t nexus_driver = {
170 "nexus",
171 nexus_methods,
172 1 /* no softc */
173 };
174 static devclass_t nexus_devclass;
175
176 static int
177 nexus_probe(device_t dev)
178 {
179
180 device_set_desc(dev, "MIPS32 root nexus");
181
182 irq_rman.rm_start = 0;
183 irq_rman.rm_end = NUM_MIPS_IRQS - 1;
184 irq_rman.rm_type = RMAN_ARRAY;
185 irq_rman.rm_descr = "Hardware IRQs";
186 if (rman_init(&irq_rman) != 0 ||
187 rman_manage_region(&irq_rman, 0, NUM_MIPS_IRQS - 1) != 0) {
188 panic("%s: irq_rman", __func__);
189 }
190
191 mem_rman.rm_start = 0;
192 mem_rman.rm_end = BUS_SPACE_MAXADDR;
193 mem_rman.rm_type = RMAN_ARRAY;
194 mem_rman.rm_descr = "Memory addresses";
195 if (rman_init(&mem_rman) != 0 ||
196 rman_manage_region(&mem_rman, 0, BUS_SPACE_MAXADDR) != 0) {
197 panic("%s: mem_rman", __func__);
198 }
199
200 return (0);
201 }
202
203 static int
204 nexus_attach(device_t dev)
205 {
206 #if defined(INTRNG) && !defined(FDT)
207 int error;
208
209 if ((error = mips_pic_map_fixed_intrs()))
210 return (error);
211 #endif
212
213 bus_generic_probe(dev);
214 bus_enumerate_hinted_children(dev);
215 bus_generic_attach(dev);
216
217 return (0);
218 }
219
220 static int
221 nexus_print_child(device_t bus, device_t child)
222 {
223 int retval = 0;
224
225 retval += bus_print_child_header(bus, child);
226 retval += nexus_print_all_resources(child);
227 if (device_get_flags(child))
228 retval += printf(" flags %#x", device_get_flags(child));
229 retval += printf(" on %s\n", device_get_nameunit(bus));
230
231 return (retval);
232 }
233
234 static int
235 nexus_print_all_resources(device_t dev)
236 {
237 struct nexus_device *ndev = DEVTONX(dev);
238 struct resource_list *rl = &ndev->nx_resources;
239 int retval = 0;
240
241 if (STAILQ_FIRST(rl))
242 retval += printf(" at");
243
244 retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#jx");
245 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd");
246
247 return (retval);
248 }
249
250 static device_t
251 nexus_add_child(device_t bus, u_int order, const char *name, int unit)
252 {
253 device_t child;
254 struct nexus_device *ndev;
255
256 ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO);
257 if (!ndev)
258 return (0);
259 resource_list_init(&ndev->nx_resources);
260
261 child = device_add_child_ordered(bus, order, name, unit);
262 if (child == NULL) {
263 device_printf(bus, "failed to add child: %s%d\n", name, unit);
264 return (0);
265 }
266
267 /* should we free this in nexus_child_detached? */
268 device_set_ivars(child, ndev);
269
270 return (child);
271 }
272
273 /*
274 * Allocate a resource on behalf of child. NB: child is usually going to be a
275 * child of one of our descendants, not a direct child of nexus0.
276 * (Exceptions include footbridge.)
277 */
278 static struct resource *
279 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
280 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
281 {
282 struct nexus_device *ndev = DEVTONX(child);
283 struct resource *rv;
284 struct resource_list_entry *rle;
285 struct rman *rm;
286 int isdefault, needactivate, passthrough;
287
288 dprintf("%s: entry (%p, %p, %d, %p, %p, %p, %jd, %d)\n",
289 __func__, bus, child, type, rid, (void *)(intptr_t)start,
290 (void *)(intptr_t)end, count, flags);
291 dprintf("%s: requested rid is %d\n", __func__, *rid);
292
293 isdefault = (RMAN_IS_DEFAULT_RANGE(start, end) && count == 1);
294 needactivate = flags & RF_ACTIVE;
295 passthrough = (device_get_parent(child) != bus);
296 rle = NULL;
297
298 /*
299 * If this is an allocation of the "default" range for a given RID,
300 * and we know what the resources for this device are (ie. they aren't
301 * maintained by a child bus), then work out the start/end values.
302 */
303 if (!passthrough && isdefault) {
304 rle = resource_list_find(&ndev->nx_resources, type, *rid);
305 if (rle == NULL)
306 return (NULL);
307 if (rle->res != NULL) {
308 panic("%s: resource entry is busy", __func__);
309 }
310 start = rle->start;
311 end = rle->end;
312 count = rle->count;
313 }
314
315 switch (type) {
316 case SYS_RES_IRQ:
317 rm = &irq_rman;
318 break;
319 case SYS_RES_MEMORY:
320 rm = &mem_rman;
321 break;
322 default:
323 printf("%s: unknown resource type %d\n", __func__, type);
324 return (0);
325 }
326
327 rv = rman_reserve_resource(rm, start, end, count, flags, child);
328 if (rv == NULL) {
329 printf("%s: could not reserve resource for %s\n", __func__,
330 device_get_nameunit(child));
331 return (0);
332 }
333
334 rman_set_rid(rv, *rid);
335
336 if (needactivate) {
337 if (bus_activate_resource(child, type, *rid, rv)) {
338 printf("%s: could not activate resource\n", __func__);
339 rman_release_resource(rv);
340 return (0);
341 }
342 }
343
344 return (rv);
345 }
346
347 static struct resource_list *
348 nexus_get_reslist(device_t dev, device_t child)
349 {
350 struct nexus_device *ndev = DEVTONX(child);
351
352 return (&ndev->nx_resources);
353 }
354
355 static int
356 nexus_set_resource(device_t dev, device_t child, int type, int rid,
357 rman_res_t start, rman_res_t count)
358 {
359 struct nexus_device *ndev = DEVTONX(child);
360 struct resource_list *rl = &ndev->nx_resources;
361 struct resource_list_entry *rle;
362
363 dprintf("%s: entry (%p, %p, %d, %d, %p, %jd)\n",
364 __func__, dev, child, type, rid, (void *)(intptr_t)start, count);
365
366 rle = resource_list_add(rl, type, rid, start, start + count - 1,
367 count);
368 if (rle == NULL)
369 return (ENXIO);
370
371 return (0);
372 }
373
374 static int
375 nexus_get_resource(device_t dev, device_t child, int type, int rid,
376 rman_res_t *startp, rman_res_t *countp)
377 {
378 struct nexus_device *ndev = DEVTONX(child);
379 struct resource_list *rl = &ndev->nx_resources;
380 struct resource_list_entry *rle;
381
382 rle = resource_list_find(rl, type, rid);
383 if (!rle)
384 return(ENOENT);
385 if (startp)
386 *startp = rle->start;
387 if (countp)
388 *countp = rle->count;
389 return (0);
390 }
391
392 static void
393 nexus_delete_resource(device_t dev, device_t child, int type, int rid)
394 {
395 struct nexus_device *ndev = DEVTONX(child);
396 struct resource_list *rl = &ndev->nx_resources;
397
398 dprintf("%s: entry\n", __func__);
399
400 resource_list_delete(rl, type, rid);
401 }
402
403 static int
404 nexus_release_resource(device_t bus, device_t child, int type, int rid,
405 struct resource *r)
406 {
407 dprintf("%s: entry\n", __func__);
408
409 if (rman_get_flags(r) & RF_ACTIVE) {
410 int error = bus_deactivate_resource(child, type, rid, r);
411 if (error)
412 return error;
413 }
414
415 return (rman_release_resource(r));
416 }
417
418 static int
419 nexus_activate_resource(device_t bus, device_t child, int type, int rid,
420 struct resource *r)
421 {
422 void *vaddr;
423 vm_paddr_t paddr;
424 vm_size_t psize;
425 int err;
426
427 /*
428 * If this is a memory resource, use pmap_mapdev to map it.
429 */
430 if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
431 paddr = rman_get_start(r);
432 psize = rman_get_size(r);
433 rman_set_bustag(r, mips_bus_space_generic);
434 err = bus_space_map(rman_get_bustag(r), paddr, psize, 0,
435 (bus_space_handle_t *)&vaddr);
436 if (err != 0) {
437 rman_deactivate_resource(r);
438 return (err);
439 }
440 rman_set_virtual(r, vaddr);
441 rman_set_bushandle(r, (bus_space_handle_t)(uintptr_t)vaddr);
442 } else if (type == SYS_RES_IRQ) {
443 #ifdef INTRNG
444 err = mips_pic_activate_intr(child, r);
445 if (err != 0) {
446 rman_deactivate_resource(r);
447 return (err);
448 }
449 #endif
450 }
451
452 return (rman_activate_resource(r));
453 }
454
455 static int
456 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
457 struct resource *r)
458 {
459 bus_space_handle_t vaddr;
460 bus_size_t psize;
461
462 vaddr = rman_get_bushandle(r);
463
464 if (type == SYS_RES_MEMORY && vaddr != 0) {
465 psize = (bus_size_t)rman_get_size(r);
466 bus_space_unmap(rman_get_bustag(r), vaddr, psize);
467 rman_set_virtual(r, NULL);
468 rman_set_bushandle(r, 0);
469 } else if (type == SYS_RES_IRQ) {
470 #ifdef INTRNG
471 mips_pic_deactivate_intr(child, r);
472 #endif
473 }
474
475 return (rman_deactivate_resource(r));
476 }
477
478 static int
479 nexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags,
480 driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep)
481 {
482 #ifdef INTRNG
483 return (intr_setup_irq(child, res, filt, intr, arg, flags, cookiep));
484 #else
485 int irq;
486 register_t s;
487
488 s = intr_disable();
489 irq = rman_get_start(res);
490 if (irq >= NUM_MIPS_IRQS) {
491 intr_restore(s);
492 return (0);
493 }
494
495 cpu_establish_hardintr(device_get_nameunit(child), filt, intr, arg,
496 irq, flags, cookiep);
497 intr_restore(s);
498
499 return (0);
500 #endif
501 }
502
503 static int
504 nexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
505 {
506
507 #ifdef INTRNG
508 return (intr_teardown_irq(child, r, ih));
509 #else
510 printf("Unimplemented %s at %s:%d\n", __func__, __FILE__, __LINE__);
511 return (0);
512 #endif
513 }
514
515 #ifdef INTRNG
516 static int
517 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
518 enum intr_polarity pol)
519 {
520
521 device_printf(dev, "bus_config_intr is obsolete and not supported!\n");
522 return (EOPNOTSUPP);
523 }
524
525 static int
526 nexus_describe_intr(device_t dev, device_t child, struct resource *irq,
527 void *cookie, const char *descr)
528 {
529
530 return (intr_describe_irq(child, irq, cookie, descr));
531 }
532
533 #ifdef SMP
534 static int
535 nexus_bind_intr(device_t dev, device_t child, struct resource *irq, int cpu)
536 {
537
538 return (intr_bind_irq(child, irq, cpu));
539 }
540 #endif
541
542 #ifdef FDT
543 static int
544 nexus_ofw_map_intr(device_t dev, device_t child, phandle_t iparent, int icells,
545 pcell_t *intr)
546 {
547 u_int irq;
548 struct intr_map_data_fdt *fdt_data;
549 size_t len;
550
551 len = sizeof(*fdt_data) + icells * sizeof(pcell_t);
552 fdt_data = (struct intr_map_data_fdt *)intr_alloc_map_data(
553 INTR_MAP_DATA_FDT, len, M_WAITOK | M_ZERO);
554 fdt_data->iparent = iparent;
555 fdt_data->ncells = icells;
556 memcpy(fdt_data->cells, intr, icells * sizeof(pcell_t));
557 irq = intr_map_irq(NULL, iparent, (struct intr_map_data *)fdt_data);
558 return (irq);
559 }
560 #endif
561 #endif /* INTRNG */
562
563 static void
564 nexus_hinted_child(device_t bus, const char *dname, int dunit)
565 {
566 device_t child;
567 long maddr;
568 int msize;
569 int order;
570 int result;
571 int irq;
572 int mem_hints_count;
573
574 if ((resource_int_value(dname, dunit, "order", &order)) != 0)
575 order = 1000;
576 child = BUS_ADD_CHILD(bus, order, dname, dunit);
577 if (child == NULL)
578 return;
579
580 /*
581 * Set hard-wired resources for hinted child using
582 * specific RIDs.
583 */
584 mem_hints_count = 0;
585 if (resource_long_value(dname, dunit, "maddr", &maddr) == 0)
586 mem_hints_count++;
587 if (resource_int_value(dname, dunit, "msize", &msize) == 0)
588 mem_hints_count++;
589
590 /* check if all info for mem resource has been provided */
591 if ((mem_hints_count > 0) && (mem_hints_count < 2)) {
592 printf("Either maddr or msize hint is missing for %s%d\n",
593 dname, dunit);
594 }
595 else if (mem_hints_count) {
596 dprintf("%s: discovered hinted child %s at maddr %p(%d)\n",
597 __func__, device_get_nameunit(child),
598 (void *)(intptr_t)maddr, msize);
599
600 result = bus_set_resource(child, SYS_RES_MEMORY, 0,
601 (u_long) maddr, msize);
602 if (result != 0) {
603 device_printf(bus,
604 "warning: bus_set_resource() failed\n");
605 }
606 }
607
608 if (resource_int_value(dname, dunit, "irq", &irq) == 0) {
609 result = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
610 if (result != 0)
611 device_printf(bus,
612 "warning: bus_set_resource() failed\n");
613 }
614 }
615
616 EARLY_DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0,
617 BUS_PASS_BUS + BUS_PASS_ORDER_EARLY);
Cache object: aee8352bdb5ae1ed31a278345be9e76c
|