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