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