xref: /freebsd/sys/riscv/riscv/nexus.c (revision 2b833162)
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 RISC-V 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 #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/intr.h>
59 
60 #ifdef FDT
61 #include <dev/ofw/ofw_bus_subr.h>
62 #include <dev/ofw/openfirm.h>
63 #include "ofw_bus_if.h"
64 #endif
65 
66 extern struct bus_space memmap_bus;
67 
68 static MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device");
69 
70 struct nexus_device {
71 	struct resource_list	nx_resources;
72 };
73 
74 #define DEVTONX(dev)	((struct nexus_device *)device_get_ivars(dev))
75 
76 static struct rman mem_rman;
77 static struct rman irq_rman;
78 
79 static device_probe_t		nexus_fdt_probe;
80 static device_attach_t		nexus_attach;
81 
82 static bus_add_child_t		nexus_add_child;
83 static bus_print_child_t	nexus_print_child;
84 
85 static bus_activate_resource_t	nexus_activate_resource;
86 static bus_adjust_resource_t	nexus_adjust_resource;
87 static bus_alloc_resource_t	nexus_alloc_resource;
88 static bus_deactivate_resource_t nexus_deactivate_resource;
89 static bus_get_resource_list_t	nexus_get_reslist;
90 static bus_map_resource_t	nexus_map_resource;
91 static bus_set_resource_t	nexus_set_resource;
92 static bus_release_resource_t	nexus_release_resource;
93 
94 static bus_config_intr_t	nexus_config_intr;
95 static bus_setup_intr_t		nexus_setup_intr;
96 static bus_teardown_intr_t	nexus_teardown_intr;
97 
98 static bus_get_bus_tag_t	nexus_get_bus_tag;
99 
100 static ofw_bus_map_intr_t	nexus_ofw_map_intr;
101 
102 static device_method_t nexus_methods[] = {
103 	/* Device interface */
104 	DEVMETHOD(device_probe,		nexus_fdt_probe),
105 	DEVMETHOD(device_attach,	nexus_attach),
106 
107 	/* OFW interface */
108 	DEVMETHOD(ofw_bus_map_intr,	nexus_ofw_map_intr),
109 
110 	/* Bus interface */
111 	DEVMETHOD(bus_add_child,	nexus_add_child),
112 	DEVMETHOD(bus_print_child,	nexus_print_child),
113 	DEVMETHOD(bus_activate_resource, nexus_activate_resource),
114 	DEVMETHOD(bus_adjust_resource,	nexus_adjust_resource),
115 	DEVMETHOD(bus_alloc_resource,	nexus_alloc_resource),
116 	DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource),
117 	DEVMETHOD(bus_get_resource_list, nexus_get_reslist),
118 	DEVMETHOD(bus_map_resource,	nexus_map_resource),
119 	DEVMETHOD(bus_set_resource,	nexus_set_resource),
120 	DEVMETHOD(bus_release_resource,	nexus_release_resource),
121 	DEVMETHOD(bus_config_intr,	nexus_config_intr),
122 	DEVMETHOD(bus_setup_intr,	nexus_setup_intr),
123 	DEVMETHOD(bus_teardown_intr,	nexus_teardown_intr),
124 	DEVMETHOD(bus_get_bus_tag,	nexus_get_bus_tag),
125 
126 	DEVMETHOD_END
127 };
128 
129 static driver_t nexus_fdt_driver = {
130 	"nexus",
131 	nexus_methods,
132 	1			/* no softc */
133 };
134 
135 EARLY_DRIVER_MODULE(nexus_fdt, root, nexus_fdt_driver, 0, 0,
136     BUS_PASS_BUS + BUS_PASS_ORDER_FIRST);
137 
138 static int
139 nexus_fdt_probe(device_t dev)
140 {
141 
142 	device_quiet(dev);
143 	return (BUS_PROBE_DEFAULT);
144 }
145 
146 static int
147 nexus_attach(device_t dev)
148 {
149 
150 	mem_rman.rm_start = 0;
151 	mem_rman.rm_end = BUS_SPACE_MAXADDR;
152 	mem_rman.rm_type = RMAN_ARRAY;
153 	mem_rman.rm_descr = "I/O memory addresses";
154 	if (rman_init(&mem_rman) ||
155 	    rman_manage_region(&mem_rman, 0, BUS_SPACE_MAXADDR))
156 		panic("nexus_attach mem_rman");
157 	irq_rman.rm_start = 0;
158 	irq_rman.rm_end = ~0;
159 	irq_rman.rm_type = RMAN_ARRAY;
160 	irq_rman.rm_descr = "Interrupts";
161 	if (rman_init(&irq_rman) || rman_manage_region(&irq_rman, 0, ~0))
162 		panic("nexus_attach irq_rman");
163 
164 	/*
165 	 * Add direct children of nexus. Devices will be probed and attached
166 	 * through ofwbus0.
167 	 */
168 	nexus_add_child(dev, 0, "timer", 0);
169 	nexus_add_child(dev, 1, "rcons", 0);
170 	nexus_add_child(dev, 2, "ofwbus", 0);
171 
172 	bus_generic_probe(dev);
173 	bus_generic_attach(dev);
174 
175 	return (0);
176 }
177 
178 static int
179 nexus_print_child(device_t bus, device_t child)
180 {
181 	int retval = 0;
182 
183 	retval += bus_print_child_header(bus, child);
184 	retval += printf("\n");
185 
186 	return (retval);
187 }
188 
189 static device_t
190 nexus_add_child(device_t bus, u_int order, const char *name, int unit)
191 {
192 	device_t child;
193 	struct nexus_device *ndev;
194 
195 	ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO);
196 	if (!ndev)
197 		return (0);
198 	resource_list_init(&ndev->nx_resources);
199 
200 	child = device_add_child_ordered(bus, order, name, unit);
201 
202 	device_set_ivars(child, ndev);
203 
204 	return (child);
205 }
206 
207 /*
208  * Allocate a resource on behalf of child.  NB: child is usually going to be a
209  * child of one of our descendants, not a direct child of nexus0.
210  */
211 static struct resource *
212 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
213     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
214 {
215 	struct nexus_device *ndev = DEVTONX(child);
216 	struct resource *rv;
217 	struct resource_list_entry *rle;
218 	struct rman *rm;
219 	int needactivate = flags & RF_ACTIVE;
220 
221 	/*
222 	 * If this is an allocation of the "default" range for a given
223 	 * RID, and we know what the resources for this device are
224 	 * (ie. they aren't maintained by a child bus), then work out
225 	 * the start/end values.
226 	 */
227 	if (RMAN_IS_DEFAULT_RANGE(start, end) && (count == 1)) {
228 		if (device_get_parent(child) != bus || ndev == NULL)
229 			return (NULL);
230 		rle = resource_list_find(&ndev->nx_resources, type, *rid);
231 		if (rle == NULL)
232 			return (NULL);
233 		start = rle->start;
234 		end = rle->end;
235 		count = rle->count;
236 	}
237 
238 	switch (type) {
239 	case SYS_RES_IRQ:
240 		rm = &irq_rman;
241 		break;
242 
243 	case SYS_RES_MEMORY:
244 	case SYS_RES_IOPORT:
245 		rm = &mem_rman;
246 		break;
247 
248 	default:
249 		return (NULL);
250 	}
251 
252 	rv = rman_reserve_resource(rm, start, end, count, flags, child);
253 	if (rv == NULL)
254 		return (NULL);
255 
256 	rman_set_rid(rv, *rid);
257 	rman_set_bushandle(rv, rman_get_start(rv));
258 
259 	if (needactivate) {
260 		if (bus_activate_resource(child, type, *rid, rv)) {
261 			rman_release_resource(rv);
262 			return (NULL);
263 		}
264 	}
265 
266 	return (rv);
267 }
268 
269 static int
270 nexus_adjust_resource(device_t bus __unused, device_t child __unused, int type,
271     struct resource *r, rman_res_t start, rman_res_t end)
272 {
273 	struct rman *rm;
274 
275 	switch (type) {
276 	case SYS_RES_IRQ:
277 		rm = &irq_rman;
278 		break;
279 	case SYS_RES_MEMORY:
280 		rm = &mem_rman;
281 		break;
282 	default:
283 		return (EINVAL);
284 	}
285 	if (rman_is_region_manager(r, rm) == 0)
286 		return (EINVAL);
287 	return (rman_adjust_resource(r, start, end));
288 }
289 
290 static int
291 nexus_release_resource(device_t bus, device_t child, int type, int rid,
292     struct resource *res)
293 {
294 	int error;
295 
296 	if (rman_get_flags(res) & RF_ACTIVE) {
297 		error = bus_deactivate_resource(child, type, rid, res);
298 		if (error != 0)
299 			return (error);
300 	}
301 	return (rman_release_resource(res));
302 }
303 
304 static int
305 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
306     enum intr_polarity pol)
307 {
308 
309 	return (EOPNOTSUPP);
310 }
311 
312 static int
313 nexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags,
314     driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep)
315 {
316 	int error;
317 
318 	if ((rman_get_flags(res) & RF_SHAREABLE) == 0)
319 		flags |= INTR_EXCL;
320 
321 	/* We depend here on rman_activate_resource() being idempotent. */
322 	error = rman_activate_resource(res);
323 	if (error != 0)
324 		return (error);
325 
326 	error = intr_setup_irq(child, res, filt, intr, arg, flags, cookiep);
327 
328 	return (error);
329 }
330 
331 static int
332 nexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
333 {
334 
335 	return (intr_teardown_irq(child, r, ih));
336 }
337 
338 static bus_space_tag_t
339 nexus_get_bus_tag(device_t bus __unused, device_t child __unused)
340 {
341 
342 	return (&memmap_bus);
343 }
344 
345 static int
346 nexus_activate_resource(device_t bus, device_t child, int type, int rid,
347     struct resource *r)
348 {
349 	struct resource_map map;
350 	int err;
351 
352 	if ((err = rman_activate_resource(r)) != 0)
353 		return (err);
354 
355 	/*
356 	 * If this is a memory resource, map it into the kernel.
357 	 */
358 	switch (type) {
359 	case SYS_RES_IOPORT:
360 	case SYS_RES_MEMORY:
361 		if ((rman_get_flags(r) & RF_UNMAPPED) == 0) {
362 			err = nexus_map_resource(bus, child, type, r, NULL,
363 			    &map);
364 			if (err != 0) {
365 				rman_deactivate_resource(r);
366 				return (err);
367 			}
368 
369 			rman_set_mapping(r, &map);
370 		}
371 		break;
372 	case SYS_RES_IRQ:
373 		err = intr_activate_irq(child, r);
374 		if (err != 0) {
375 			rman_deactivate_resource(r);
376 			return (err);
377 		}
378 	}
379 	return (0);
380 }
381 
382 static struct resource_list *
383 nexus_get_reslist(device_t dev, device_t child)
384 {
385 	struct nexus_device *ndev = DEVTONX(child);
386 
387 	return (&ndev->nx_resources);
388 }
389 
390 static int
391 nexus_set_resource(device_t dev, device_t child, int type, int rid,
392     rman_res_t start, rman_res_t count)
393 {
394 	struct nexus_device	*ndev = DEVTONX(child);
395 	struct resource_list	*rl = &ndev->nx_resources;
396 
397 	/* XXX this should return a success/failure indicator */
398 	resource_list_add(rl, type, rid, start, start + count - 1, count);
399 
400 	return (0);
401 }
402 
403 static int
404 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
405     struct resource *r)
406 {
407 	bus_size_t psize;
408 	bus_space_handle_t vaddr;
409 
410 	if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
411 		psize = (bus_size_t)rman_get_size(r);
412 		vaddr = rman_get_bushandle(r);
413 
414 		if (vaddr != 0) {
415 			bus_space_unmap(&memmap_bus, vaddr, psize);
416 			rman_set_virtual(r, NULL);
417 			rman_set_bushandle(r, 0);
418 		}
419 	} else if (type == SYS_RES_IRQ) {
420 		intr_deactivate_irq(child, r);
421 	}
422 
423 	return (rman_deactivate_resource(r));
424 }
425 
426 static int
427 nexus_map_resource(device_t bus, device_t child, int type, struct resource *r,
428     struct resource_map_request *argsp, struct resource_map *map)
429 {
430 	struct resource_map_request args;
431 	rman_res_t end, length, start;
432 
433 	/* Resources must be active to be mapped. */
434 	if ((rman_get_flags(r) & RF_ACTIVE) == 0)
435 		return (ENXIO);
436 
437 	/* Mappings are only supported on I/O and memory resources. */
438 	switch (type) {
439 	case SYS_RES_IOPORT:
440 	case SYS_RES_MEMORY:
441 		break;
442 	default:
443 		return (EINVAL);
444 	}
445 
446 	resource_init_map_request(&args);
447 	if (argsp != NULL)
448 		bcopy(argsp, &args, imin(argsp->size, args.size));
449 	start = rman_get_start(r) + args.offset;
450 	if (args.length == 0)
451 		length = rman_get_size(r);
452 	else
453 		length = args.length;
454 	end = start + length - 1;
455 	if (start > rman_get_end(r) || start < rman_get_start(r))
456 		return (EINVAL);
457 	if (end > rman_get_end(r) || end < start)
458 		return (EINVAL);
459 
460 	map->r_vaddr = pmap_mapdev(start, length);
461 	map->r_bustag = &memmap_bus;
462 	map->r_size = length;
463 
464 	/*
465 	 * The handle is the virtual address.
466 	 */
467 	map->r_bushandle = (bus_space_handle_t)map->r_vaddr;
468 	return (0);
469 }
470 
471 static int
472 nexus_ofw_map_intr(device_t dev, device_t child, phandle_t iparent, int icells,
473     pcell_t *intr)
474 {
475 	struct intr_map_data_fdt *fdt_data;
476 	size_t len;
477 	u_int irq;
478 
479 	len = sizeof(*fdt_data) + icells * sizeof(pcell_t);
480 	fdt_data = (struct intr_map_data_fdt *)intr_alloc_map_data(
481 	    INTR_MAP_DATA_FDT, len, M_WAITOK | M_ZERO);
482 	fdt_data->iparent = iparent;
483 	fdt_data->ncells = icells;
484 	memcpy(fdt_data->cells, intr, icells * sizeof(pcell_t));
485 	irq = intr_map_irq(NULL, iparent, (struct intr_map_data *)fdt_data);
486 
487 	return (irq);
488 }
489