xref: /freebsd-14-stable/sys/arm/arm/nexus.c (revision a7febc75da2f84c03d991c9c8c4562438803e5e0)
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 and I/O memory address space.
38  */
39 
40 #include "opt_platform.h"
41 
42 #include <sys/cdefs.h>
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/bus.h>
46 #include <sys/interrupt.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/module.h>
50 #include <sys/rman.h>
51 
52 #include <vm/vm.h>
53 #include <vm/pmap.h>
54 
55 #include <machine/bus.h>
56 #include <machine/pcb.h>
57 #include <machine/intr.h>
58 #include <machine/resource.h>
59 #include <machine/vmparam.h>
60 
61 #include <arm/arm/nexusvar.h>
62 
63 #ifdef FDT
64 #include <machine/fdt.h>
65 #include <dev/ofw/ofw_bus_subr.h>
66 #include "ofw_bus_if.h"
67 #endif
68 
69 static MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device");
70 
71 struct nexus_device {
72 	struct resource_list	nx_resources;
73 };
74 
75 #define DEVTONX(dev)	((struct nexus_device *)device_get_ivars(dev))
76 
77 static struct rman mem_rman;
78 static struct rman irq_rman;
79 
80 static device_probe_t		nexus_probe;
81 static device_attach_t		nexus_attach;
82 
83 static bus_add_child_t		nexus_add_child;
84 static bus_print_child_t	nexus_print_child;
85 
86 static bus_activate_resource_t	nexus_activate_resource;
87 static bus_deactivate_resource_t nexus_deactivate_resource;
88 static bus_get_rman_t		nexus_get_rman;
89 static bus_map_resource_t	nexus_map_resource;
90 static bus_unmap_resource_t	nexus_unmap_resource;
91 
92 #ifdef SMP
93 static bus_bind_intr_t		nexus_bind_intr;
94 #endif
95 static bus_config_intr_t	nexus_config_intr;
96 static bus_describe_intr_t	nexus_describe_intr;
97 static bus_setup_intr_t		nexus_setup_intr;
98 static bus_teardown_intr_t	nexus_teardown_intr;
99 
100 static bus_get_bus_tag_t	nexus_get_bus_tag;
101 static bus_get_dma_tag_t	nexus_get_dma_tag;
102 
103 #ifdef FDT
104 static ofw_bus_map_intr_t	nexus_ofw_map_intr;
105 #endif
106 
107 /*
108  * Normally NULL (which results in defaults which are handled in
109  * busdma_machdep), platform init code can use nexus_set_dma_tag() to set this
110  * to a tag that will be inherited by all busses and devices on the platform.
111  */
112 static bus_dma_tag_t nexus_dma_tag;
113 
114 static device_method_t nexus_methods[] = {
115 	/* Device interface */
116 	DEVMETHOD(device_probe,		nexus_probe),
117 	DEVMETHOD(device_attach,	nexus_attach),
118 
119 	/* Bus interface */
120 	DEVMETHOD(bus_add_child,	nexus_add_child),
121 	DEVMETHOD(bus_print_child,	nexus_print_child),
122 	DEVMETHOD(bus_activate_resource, nexus_activate_resource),
123 	DEVMETHOD(bus_adjust_resource,	bus_generic_rman_adjust_resource),
124 	DEVMETHOD(bus_alloc_resource,	bus_generic_rman_alloc_resource),
125 	DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource),
126 	DEVMETHOD(bus_get_rman,		nexus_get_rman),
127 	DEVMETHOD(bus_map_resource,	nexus_map_resource),
128 	DEVMETHOD(bus_release_resource,	bus_generic_rman_release_resource),
129 	DEVMETHOD(bus_unmap_resource,	nexus_unmap_resource),
130 #ifdef SMP
131 	DEVMETHOD(bus_bind_intr,	nexus_bind_intr),
132 #endif
133 	DEVMETHOD(bus_config_intr,	nexus_config_intr),
134 	DEVMETHOD(bus_describe_intr,	nexus_describe_intr),
135 	DEVMETHOD(bus_setup_intr,	nexus_setup_intr),
136 	DEVMETHOD(bus_teardown_intr,	nexus_teardown_intr),
137 	DEVMETHOD(bus_get_bus_tag,	nexus_get_bus_tag),
138 	DEVMETHOD(bus_get_dma_tag,	nexus_get_dma_tag),
139 #ifdef FDT
140 	/* ofw_bus interface */
141 	DEVMETHOD(ofw_bus_map_intr,	nexus_ofw_map_intr),
142 #endif
143 	DEVMETHOD_END
144 };
145 
146 static driver_t nexus_driver = {
147 	"nexus",
148 	nexus_methods,
149 	1			/* no softc */
150 };
151 
152 EARLY_DRIVER_MODULE(nexus, root, nexus_driver, 0, 0,
153     BUS_PASS_BUS + BUS_PASS_ORDER_EARLY);
154 
155 static int
nexus_probe(device_t dev)156 nexus_probe(device_t dev)
157 {
158 
159 	device_quiet(dev);	/* suppress attach message for neatness */
160 
161 	return (BUS_PROBE_DEFAULT);
162 }
163 
164 static int
nexus_attach(device_t dev)165 nexus_attach(device_t dev)
166 {
167 
168 	mem_rman.rm_start = 0;
169 	mem_rman.rm_end = BUS_SPACE_MAXADDR;
170 	mem_rman.rm_type = RMAN_ARRAY;
171 	mem_rman.rm_descr = "I/O memory addresses";
172 	if (rman_init(&mem_rman) ||
173 	    rman_manage_region(&mem_rman, 0, BUS_SPACE_MAXADDR))
174 		panic("nexus_probe mem_rman");
175 	irq_rman.rm_start = 0;
176 	irq_rman.rm_end = ~0;
177 	irq_rman.rm_type = RMAN_ARRAY;
178 	irq_rman.rm_descr = "Interrupts";
179 	if (rman_init(&irq_rman) || rman_manage_region(&irq_rman, 0, ~0))
180 		panic("nexus_attach irq_rman");
181 
182 	/* First, add ofwbus0. */
183 	device_add_child(dev, "ofwbus", 0);
184 
185 	/*
186 	 * Next, deal with the children we know about already.
187 	 */
188 	bus_generic_probe(dev);
189 	bus_generic_attach(dev);
190 
191 	return (0);
192 }
193 
194 static int
nexus_print_child(device_t bus,device_t child)195 nexus_print_child(device_t bus, device_t child)
196 {
197 	int retval = 0;
198 
199 	retval += bus_print_child_header(bus, child);
200 	retval += printf("\n");
201 
202 	return (retval);
203 }
204 
205 static device_t
nexus_add_child(device_t bus,u_int order,const char * name,int unit)206 nexus_add_child(device_t bus, u_int order, const char *name, int unit)
207 {
208 	device_t child;
209 	struct nexus_device *ndev;
210 
211 	ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO);
212 	if (!ndev)
213 		return (0);
214 	resource_list_init(&ndev->nx_resources);
215 
216 	child = device_add_child_ordered(bus, order, name, unit);
217 
218 	device_set_ivars(child, ndev);
219 
220 	return (child);
221 }
222 
223 static struct rman *
nexus_get_rman(device_t bus,int type,u_int flags)224 nexus_get_rman(device_t bus, int type, u_int flags)
225 {
226 	switch (type) {
227 	case SYS_RES_IRQ:
228 		return (&irq_rman);
229 	case SYS_RES_MEMORY:
230 	case SYS_RES_IOPORT:
231 		return (&mem_rman);
232 	default:
233 		return (NULL);
234 	}
235 }
236 
237 static bus_space_tag_t
nexus_get_bus_tag(device_t bus __unused,device_t child __unused)238 nexus_get_bus_tag(device_t bus __unused, device_t child __unused)
239 {
240 
241 #ifdef FDT
242 	return (fdtbus_bs_tag);
243 #else
244 	return ((void *)1);
245 #endif
246 }
247 
248 static bus_dma_tag_t
nexus_get_dma_tag(device_t dev,device_t child)249 nexus_get_dma_tag(device_t dev, device_t child)
250 {
251 
252 	return (nexus_dma_tag);
253 }
254 
255 void
nexus_set_dma_tag(bus_dma_tag_t tag)256 nexus_set_dma_tag(bus_dma_tag_t tag)
257 {
258 
259 	nexus_dma_tag = tag;
260 }
261 
262 static int
nexus_config_intr(device_t dev,int irq,enum intr_trigger trig,enum intr_polarity pol)263 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
264     enum intr_polarity pol)
265 {
266 	int ret = ENODEV;
267 
268 	device_printf(dev, "bus_config_intr is obsolete and not supported!\n");
269 	ret = EOPNOTSUPP;
270 	return (ret);
271 }
272 
273 static int
nexus_setup_intr(device_t dev,device_t child,struct resource * res,int flags,driver_filter_t * filt,driver_intr_t * intr,void * arg,void ** cookiep)274 nexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags,
275     driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep)
276 {
277 
278 	if ((rman_get_flags(res) & RF_SHAREABLE) == 0)
279 		flags |= INTR_EXCL;
280 
281 	return (intr_setup_irq(child, res, filt, intr, arg, flags, cookiep));
282 }
283 
284 static int
nexus_teardown_intr(device_t dev,device_t child,struct resource * r,void * ih)285 nexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
286 {
287 
288 	return (intr_teardown_irq(child, r, ih));
289 }
290 
291 static int
nexus_describe_intr(device_t dev,device_t child,struct resource * irq,void * cookie,const char * descr)292 nexus_describe_intr(device_t dev, device_t child, struct resource *irq,
293     void *cookie, const char *descr)
294 {
295 
296 	return (intr_describe_irq(child, irq, cookie, descr));
297 }
298 
299 #ifdef SMP
300 static int
nexus_bind_intr(device_t dev,device_t child,struct resource * irq,int cpu)301 nexus_bind_intr(device_t dev, device_t child, struct resource *irq, int cpu)
302 {
303 
304 	return (intr_bind_irq(child, irq, cpu));
305 }
306 #endif
307 
308 static int
nexus_activate_resource(device_t bus,device_t child,int type,int rid,struct resource * r)309 nexus_activate_resource(device_t bus, device_t child, int type, int rid,
310     struct resource *r)
311 {
312 	int err;
313 
314 	switch (type) {
315 	case SYS_RES_MEMORY:
316 	case SYS_RES_IOPORT:
317 		return (bus_generic_rman_activate_resource(bus, child, type,
318 		    rid, r));
319 	case SYS_RES_IRQ:
320 		err = rman_activate_resource(r);
321 		if (err != 0)
322 			return (err);
323 		err = intr_activate_irq(child, r);
324 		if (err != 0) {
325 			rman_deactivate_resource(r);
326 			return (err);
327 		}
328 		return (0);
329 	default:
330 		return (EINVAL);
331 	}
332 }
333 
334 static int
nexus_map_resource(device_t bus,device_t child,int type,struct resource * r,struct resource_map_request * argsp,struct resource_map * map)335 nexus_map_resource(device_t bus, device_t child, int type, struct resource *r,
336     struct resource_map_request *argsp, struct resource_map *map)
337 {
338 	struct resource_map_request args;
339 	rman_res_t length, start;
340 	int error;
341 
342 	/* Resources must be active to be mapped. */
343 	if (!(rman_get_flags(r) & RF_ACTIVE))
344 		return (ENXIO);
345 
346 	switch (type) {
347 	case SYS_RES_MEMORY:
348 	case SYS_RES_IOPORT:
349 		break;
350 	default:
351 		return (EINVAL);
352 	}
353 
354 	resource_init_map_request(&args);
355 	error = resource_validate_map_request(r, argsp, &args, &start, &length);
356 	if (error)
357 		return (error);
358 
359 #ifdef FDT
360 	error = bus_space_map(fdtbus_bs_tag, start, length, 0,
361 	    &map->r_bushandle);
362 	if (error)
363 		return (error);
364 	map->r_bustag = fdtbus_bs_tag;
365 	map->r_vaddr = (void *)map->r_bushandle;
366 #else
367 	map->r_vaddr = pmap_mapdev(start, length);
368 	if (map->r_vaddr == NULL)
369 		return (ENOMEM);
370 	map->r_bustag = (void *)1;
371 	map->r_bushandle = (bus_space_handle_t)map->r_vaddr;
372 #endif
373 	map->r_size = length;
374 	return (0);
375 }
376 
377 static int
nexus_unmap_resource(device_t bus,device_t child,int type,struct resource * r,struct resource_map * map)378 nexus_unmap_resource(device_t bus, device_t child, int type, struct resource *r,
379     struct resource_map *map)
380 {
381 
382 	switch (type) {
383 	case SYS_RES_MEMORY:
384 	case SYS_RES_IOPORT:
385 #ifdef FDT
386 		bus_space_unmap(map->r_bustag, map->r_bushandle, map->r_size);
387 #else
388 		pmap_unmapdev(map->r_vaddr, map->r_size);
389 #endif
390 		return (0);
391 	default:
392 		return (EINVAL);
393 	}
394 }
395 
396 static int
nexus_deactivate_resource(device_t bus,device_t child,int type,int rid,struct resource * r)397 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
398     struct resource *r)
399 {
400 	int error;
401 
402 	switch (type) {
403 	case SYS_RES_MEMORY:
404 	case SYS_RES_IOPORT:
405 		return (bus_generic_rman_deactivate_resource(bus, child, type,
406 		    rid, r));
407 	case SYS_RES_IRQ:
408 		error = rman_deactivate_resource(r);
409 		if (error)
410 			return (error);
411 		intr_deactivate_irq(child, r);
412 		return (0);
413 	default:
414 		return (EINVAL);
415 	}
416 }
417 
418 #ifdef FDT
419 static int
nexus_ofw_map_intr(device_t dev,device_t child,phandle_t iparent,int icells,pcell_t * intr)420 nexus_ofw_map_intr(device_t dev, device_t child, phandle_t iparent, int icells,
421     pcell_t *intr)
422 {
423 	u_int irq;
424 	struct intr_map_data_fdt *fdt_data;
425 	size_t len;
426 
427 	len = sizeof(*fdt_data) + icells * sizeof(pcell_t);
428 	fdt_data = (struct intr_map_data_fdt *)intr_alloc_map_data(
429 	    INTR_MAP_DATA_FDT, len, M_WAITOK | M_ZERO);
430 	fdt_data->iparent = iparent;
431 	fdt_data->ncells = icells;
432 	memcpy(fdt_data->cells, intr, icells * sizeof(pcell_t));
433 	irq = intr_map_irq(NULL, iparent, (struct intr_map_data *)fdt_data);
434 	return (irq);
435 }
436 #endif /* FDT */
437