]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/arm/nexus.c
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r301441, and update
[FreeBSD/FreeBSD.git] / sys / arm / arm / 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 Arm 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, DMA requests (which rightfully should be a part of the
38  * ISA code but it's easier to do it here for now), I/O port addresses,
39  * and I/O memory address space.
40  */
41
42 #include "opt_platform.h"
43
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/bus.h>
50 #include <sys/kernel.h>
51 #include <sys/malloc.h>
52 #include <sys/module.h>
53 #include <machine/bus.h>
54 #include <sys/rman.h>
55 #include <sys/interrupt.h>
56
57 #include <machine/vmparam.h>
58 #include <machine/pcb.h>
59 #include <vm/vm.h>
60 #include <vm/pmap.h>
61
62 #include <machine/resource.h>
63 #include <machine/intr.h>
64
65 #ifdef FDT
66 #include <machine/fdt.h>
67 #include <dev/ofw/ofw_bus_subr.h>
68 #include "ofw_bus_if.h"
69 #endif
70
71 static MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device");
72
73 struct nexus_device {
74         struct resource_list    nx_resources;
75 };
76
77 #define DEVTONX(dev)    ((struct nexus_device *)device_get_ivars(dev))
78
79 static struct rman mem_rman;
80
81 static  int nexus_probe(device_t);
82 static  int nexus_attach(device_t);
83 static  int nexus_print_child(device_t, device_t);
84 static  device_t nexus_add_child(device_t, u_int, const char *, int);
85 static  struct resource *nexus_alloc_resource(device_t, device_t, int, int *,
86     rman_res_t, rman_res_t, rman_res_t, u_int);
87 static  int nexus_activate_resource(device_t, device_t, int, int,
88     struct resource *);
89 static bus_space_tag_t nexus_get_bus_tag(device_t, device_t);
90 #ifdef INTRNG
91 #ifdef SMP
92 static  int nexus_bind_intr(device_t, device_t, struct resource *, int);
93 #endif
94 #endif
95 static int nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
96     enum intr_polarity pol);
97 #ifdef INTRNG
98 static  int nexus_describe_intr(device_t dev, device_t child,
99     struct resource *irq, void *cookie, const char *descr);
100 #endif
101 static  int nexus_deactivate_resource(device_t, device_t, int, int,
102     struct resource *);
103 static int nexus_release_resource(device_t, device_t, int, int,
104     struct resource *);
105
106 static int nexus_setup_intr(device_t dev, device_t child, struct resource *res,
107     int flags, driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep);
108 static int nexus_teardown_intr(device_t, device_t, struct resource *, void *);
109
110 #ifdef FDT
111 static int nexus_ofw_map_intr(device_t dev, device_t child, phandle_t iparent,
112     int icells, pcell_t *intr);
113 #endif
114
115 static device_method_t nexus_methods[] = {
116         /* Device interface */
117         DEVMETHOD(device_probe,         nexus_probe),
118         DEVMETHOD(device_attach,        nexus_attach),
119         /* Bus interface */
120         DEVMETHOD(bus_print_child,      nexus_print_child),
121         DEVMETHOD(bus_add_child,        nexus_add_child),
122         DEVMETHOD(bus_alloc_resource,   nexus_alloc_resource),
123         DEVMETHOD(bus_activate_resource,        nexus_activate_resource),
124         DEVMETHOD(bus_config_intr,      nexus_config_intr),
125         DEVMETHOD(bus_deactivate_resource,      nexus_deactivate_resource),
126         DEVMETHOD(bus_release_resource, nexus_release_resource),
127         DEVMETHOD(bus_setup_intr,       nexus_setup_intr),
128         DEVMETHOD(bus_teardown_intr,    nexus_teardown_intr),
129         DEVMETHOD(bus_get_bus_tag,      nexus_get_bus_tag),
130 #ifdef INTRNG
131         DEVMETHOD(bus_describe_intr,    nexus_describe_intr),
132 #ifdef SMP
133         DEVMETHOD(bus_bind_intr,        nexus_bind_intr),
134 #endif
135 #endif
136 #ifdef FDT
137         DEVMETHOD(ofw_bus_map_intr,     nexus_ofw_map_intr),
138 #endif
139         { 0, 0 }
140 };
141
142 static devclass_t nexus_devclass;
143 static driver_t nexus_driver = {
144         "nexus",
145         nexus_methods,
146         1                       /* no softc */
147 };
148 EARLY_DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0,
149     BUS_PASS_BUS + BUS_PASS_ORDER_EARLY);
150
151 static int
152 nexus_probe(device_t dev)
153 {
154
155         device_quiet(dev);      /* suppress attach message for neatness */
156
157         return (BUS_PROBE_DEFAULT);
158 }
159
160 static int
161 nexus_attach(device_t dev)
162 {
163
164         mem_rman.rm_start = 0;
165         mem_rman.rm_end = BUS_SPACE_MAXADDR;
166         mem_rman.rm_type = RMAN_ARRAY;
167         mem_rman.rm_descr = "I/O memory addresses";
168         if (rman_init(&mem_rman) ||
169             rman_manage_region(&mem_rman, 0, BUS_SPACE_MAXADDR))
170                 panic("nexus_probe mem_rman");
171
172         /*
173          * First, deal with the children we know about already
174          */
175         bus_generic_probe(dev);
176         bus_generic_attach(dev);
177
178         return (0);
179 }
180
181 static int
182 nexus_print_child(device_t bus, device_t child)
183 {
184         int retval = 0;
185
186         retval += bus_print_child_header(bus, child);
187         retval += printf("\n");
188
189         return (retval);
190 }
191
192 static device_t
193 nexus_add_child(device_t bus, u_int order, const char *name, int unit)
194 {
195         device_t child;
196         struct nexus_device *ndev;
197
198         ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO);
199         if (!ndev)
200                 return (0);
201         resource_list_init(&ndev->nx_resources);
202
203         child = device_add_child_ordered(bus, order, name, unit);
204
205         /* should we free this in nexus_child_detached? */
206         device_set_ivars(child, ndev);
207
208         return (child);
209 }
210
211
212 /*
213  * Allocate a resource on behalf of child.  NB: child is usually going to be a
214  * child of one of our descendants, not a direct child of nexus0.
215  * (Exceptions include footbridge.)
216  */
217 static struct resource *
218 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
219     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
220 {
221         struct resource *rv;
222         struct rman *rm;
223         int needactivate = flags & RF_ACTIVE;
224
225         flags &= ~RF_ACTIVE;
226
227         switch (type) {
228         case SYS_RES_MEMORY:
229         case SYS_RES_IOPORT:
230                 rm = &mem_rman;
231                 break;
232
233         default:
234                 return (NULL);
235         }
236
237         rv = rman_reserve_resource(rm, start, end, count, flags, child);
238         if (rv == NULL)
239                 return (NULL);
240
241         rman_set_rid(rv, *rid);
242
243         if (needactivate) {
244                 if (bus_activate_resource(child, type, *rid, rv)) {
245                         rman_release_resource(rv);
246                         return (0);
247                 }
248         }
249
250         return (rv);
251 }
252
253 static int
254 nexus_release_resource(device_t bus, device_t child, int type, int rid,
255     struct resource *res)
256 {
257         int error;
258
259         if (rman_get_flags(res) & RF_ACTIVE) {
260                 error = bus_deactivate_resource(child, type, rid, res);
261                 if (error)
262                         return (error);
263         }
264         return (rman_release_resource(res));
265 }
266
267 static bus_space_tag_t
268 nexus_get_bus_tag(device_t bus __unused, device_t child __unused)
269 {
270
271 #ifdef FDT
272                 return(fdtbus_bs_tag);
273 #else
274                 return((void *)1);
275 #endif
276 }
277
278 static int
279 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
280     enum intr_polarity pol)
281 {
282         int ret = ENODEV;
283
284 #ifdef INTRNG
285         device_printf(dev, "bus_config_intr is obsolete and not supported!\n");
286         ret = EOPNOTSUPP;
287 #else
288         if (arm_config_irq)
289                 ret = (*arm_config_irq)(irq, trig, pol);
290 #endif
291         return (ret);
292 }
293
294 static int
295 nexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags,
296     driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep)
297 {
298 #ifndef INTRNG
299         int irq;
300 #endif
301
302         if ((rman_get_flags(res) & RF_SHAREABLE) == 0)
303                 flags |= INTR_EXCL;
304
305 #ifdef INTRNG
306         return(intr_setup_irq(child, res, filt, intr, arg, flags, cookiep));
307 #else
308         for (irq = rman_get_start(res); irq <= rman_get_end(res); irq++) {
309                 arm_setup_irqhandler(device_get_nameunit(child),
310                     filt, intr, arg, irq, flags, cookiep);
311                 arm_unmask_irq(irq);
312         }
313         return (0);
314 #endif
315 }
316
317 static int
318 nexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
319 {
320
321 #ifdef INTRNG
322         return (intr_teardown_irq(child, r, ih));
323 #else
324         return (arm_remove_irqhandler(rman_get_start(r), ih));
325 #endif
326 }
327
328 #ifdef INTRNG
329 static int
330 nexus_describe_intr(device_t dev, device_t child, struct resource *irq,
331     void *cookie, const char *descr)
332 {
333
334         return (intr_describe_irq(child, irq, cookie, descr));
335 }
336
337 #ifdef SMP
338 static int
339 nexus_bind_intr(device_t dev, device_t child, struct resource *irq, int cpu)
340 {
341
342         return (intr_bind_irq(child, irq, cpu));
343 }
344 #endif
345 #endif
346
347 static int
348 nexus_activate_resource(device_t bus, device_t child, int type, int rid,
349     struct resource *r)
350 {
351         int err;
352         bus_addr_t paddr;
353         bus_size_t psize;
354         bus_space_handle_t vaddr;
355
356         if ((err = rman_activate_resource(r)) != 0)
357                 return (err);
358
359         /*
360          * If this is a memory resource, map it into the kernel.
361          */
362         if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
363                 paddr = (bus_addr_t)rman_get_start(r);
364                 psize = (bus_size_t)rman_get_size(r);
365 #ifdef FDT
366                 err = bus_space_map(fdtbus_bs_tag, paddr, psize, 0, &vaddr);
367                 if (err != 0) {
368                         rman_deactivate_resource(r);
369                         return (err);
370                 }
371                 rman_set_bustag(r, fdtbus_bs_tag);
372 #else
373                 vaddr = (bus_space_handle_t)pmap_mapdev((vm_offset_t)paddr,
374                     (vm_size_t)psize);
375                 if (vaddr == 0) {
376                         rman_deactivate_resource(r);
377                         return (ENOMEM);
378                 }
379                 rman_set_bustag(r, (void *)1);
380 #endif
381                 rman_set_virtual(r, (void *)vaddr);
382                 rman_set_bushandle(r, vaddr);
383                 return (0);
384         } else if (type == SYS_RES_IRQ) {
385 #ifdef INTRNG
386                 err = intr_activate_irq(child, r);
387                 if (err != 0) {
388                         rman_deactivate_resource(r);
389                         return (err);
390                 }
391 #endif
392         }
393         return (0);
394 }
395
396 static int
397 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
398     struct resource *r)
399 {
400         bus_size_t psize;
401         bus_space_handle_t vaddr;
402
403         if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
404                 psize = (bus_size_t)rman_get_size(r);
405                 vaddr = rman_get_bushandle(r);
406
407                 if (vaddr != 0) {
408 #ifdef FDT
409                         bus_space_unmap(fdtbus_bs_tag, vaddr, psize);
410 #else
411                         pmap_unmapdev((vm_offset_t)vaddr, (vm_size_t)psize);
412 #endif
413                         rman_set_virtual(r, NULL);
414                         rman_set_bushandle(r, 0);
415                 }
416         } else if (type == SYS_RES_IRQ) {
417 #ifdef INTRNG
418                 intr_deactivate_irq(child, r);
419 #endif
420         }
421
422         return (rman_deactivate_resource(r));
423 }
424
425 #ifdef FDT
426 static int
427 nexus_ofw_map_intr(device_t dev, device_t child, phandle_t iparent, int icells,
428     pcell_t *intr)
429 {
430 #ifndef INTRNG
431         return (intr_fdt_map_irq(iparent, intr, icells));
432 #else
433         u_int irq;
434         struct intr_map_data_fdt *fdt_data;
435         size_t len;
436
437         len = sizeof(*fdt_data) + icells * sizeof(pcell_t);
438         fdt_data = (struct intr_map_data_fdt *)intr_alloc_map_data(
439             INTR_MAP_DATA_FDT, len, M_WAITOK | M_ZERO);
440         fdt_data->iparent = iparent;
441         fdt_data->ncells = icells;
442         memcpy(fdt_data->cells, intr, icells * sizeof(pcell_t));
443         irq = intr_map_irq(NULL, iparent, (struct intr_map_data *)fdt_data);
444         return (irq);
445 #endif /* INTRNG */
446 }
447 #endif /* FDT */
448