nexus.c revision 295755
1139735Simp/*-
2129198Scognet * Copyright 1998 Massachusetts Institute of Technology
3129198Scognet *
4129198Scognet * Permission to use, copy, modify, and distribute this software and
5129198Scognet * its documentation for any purpose and without fee is hereby
6129198Scognet * granted, provided that both the above copyright notice and this
7129198Scognet * permission notice appear in all copies, that both the above
8129198Scognet * copyright notice and this permission notice appear in all
9129198Scognet * supporting documentation, and that the name of M.I.T. not be used
10129198Scognet * in advertising or publicity pertaining to distribution of the
11129198Scognet * software without specific, written prior permission.  M.I.T. makes
12129198Scognet * no representations about the suitability of this software for any
13129198Scognet * purpose.  It is provided "as is" without express or implied
14129198Scognet * warranty.
15182934Sraj *
16129198Scognet * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
17129198Scognet * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18129198Scognet * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19129198Scognet * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20129198Scognet * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21129198Scognet * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22129198Scognet * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23129198Scognet * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24129198Scognet * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25129198Scognet * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26129198Scognet * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27129198Scognet * SUCH DAMAGE.
28129198Scognet *
29129198Scognet */
30129198Scognet
31129198Scognet/*
32129198Scognet * This code implements a `root nexus' for Arm Architecture
33129198Scognet * machines.  The function of the root nexus is to serve as an
34129198Scognet * attachment point for both processors and buses, and to manage
35129198Scognet * resources which are common to all of them.  In particular,
36129198Scognet * this code implements the core resource managers for interrupt
37129198Scognet * requests, DMA requests (which rightfully should be a part of the
38129198Scognet * ISA code but it's easier to do it here for now), I/O port addresses,
39129198Scognet * and I/O memory address space.
40129198Scognet */
41129198Scognet
42276032Sandrew#include "opt_platform.h"
43276032Sandrew
44129198Scognet#include <sys/cdefs.h>
45129198Scognet__FBSDID("$FreeBSD: head/sys/arm/arm/nexus.c 295755 2016-02-18 13:00:04Z zbb $");
46129198Scognet
47129198Scognet#include <sys/param.h>
48129198Scognet#include <sys/systm.h>
49129198Scognet#include <sys/bus.h>
50129198Scognet#include <sys/kernel.h>
51129198Scognet#include <sys/malloc.h>
52129198Scognet#include <sys/module.h>
53129198Scognet#include <machine/bus.h>
54129198Scognet#include <sys/rman.h>
55129198Scognet#include <sys/interrupt.h>
56129198Scognet
57129198Scognet#include <machine/vmparam.h>
58129198Scognet#include <machine/pcb.h>
59129198Scognet#include <vm/vm.h>
60129198Scognet#include <vm/pmap.h>
61129198Scognet
62129198Scognet#include <machine/resource.h>
63129198Scognet#include <machine/intr.h>
64129198Scognet
65257702Snwhitehorn#ifdef FDT
66257702Snwhitehorn#include <machine/fdt.h>
67260281Snwhitehorn#include "ofw_bus_if.h"
68261513Snwhitehorn#endif
69261513Snwhitehorn
70129198Scognetstatic MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device");
71129198Scognet
72129198Scognetstruct nexus_device {
73129198Scognet	struct resource_list	nx_resources;
74129198Scognet};
75129198Scognet
76129198Scognet#define DEVTONX(dev)	((struct nexus_device *)device_get_ivars(dev))
77129198Scognet
78129198Scognetstatic struct rman mem_rman;
79129198Scognet
80129198Scognetstatic	int nexus_probe(device_t);
81129198Scognetstatic	int nexus_attach(device_t);
82129198Scognetstatic	int nexus_print_child(device_t, device_t);
83212413Savgstatic	device_t nexus_add_child(device_t, u_int, const char *, int);
84129198Scognetstatic	struct resource *nexus_alloc_resource(device_t, device_t, int, int *,
85294883Sjhibbits    rman_res_t, rman_res_t, rman_res_t, u_int);
86129198Scognetstatic	int nexus_activate_resource(device_t, device_t, int, int,
87182934Sraj    struct resource *);
88295755Szbbstatic bus_space_tag_t nexus_get_bus_tag(device_t, device_t);
89289529Sian#ifdef ARM_INTRNG
90289529Sian#ifdef SMP
91289529Sianstatic	int nexus_bind_intr(device_t, device_t, struct resource *, int);
92289529Sian#endif
93289529Sian#endif
94260161Szbbstatic int nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
95260161Szbb    enum intr_polarity pol);
96289529Sian#ifdef ARM_INTRNG
97289529Sianstatic	int nexus_describe_intr(device_t dev, device_t child,
98289529Sian    struct resource *irq, void *cookie, const char *descr);
99289529Sian#endif
100257702Snwhitehornstatic	int nexus_deactivate_resource(device_t, device_t, int, int,
101257702Snwhitehorn    struct resource *);
102271484Sjhbstatic int nexus_release_resource(device_t, device_t, int, int,
103271484Sjhb    struct resource *);
104257702Snwhitehorn
105182934Srajstatic int nexus_setup_intr(device_t dev, device_t child, struct resource *res,
106182934Sraj    int flags, driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep);
107182934Srajstatic int nexus_teardown_intr(device_t, device_t, struct resource *, void *);
108147166Scognet
109260281Snwhitehorn#ifdef FDT
110260281Snwhitehornstatic int nexus_ofw_map_intr(device_t dev, device_t child, phandle_t iparent,
111261351Snwhitehorn    int icells, pcell_t *intr);
112260281Snwhitehorn#endif
113260281Snwhitehorn
114129198Scognetstatic device_method_t nexus_methods[] = {
115129198Scognet	/* Device interface */
116129198Scognet	DEVMETHOD(device_probe,		nexus_probe),
117129198Scognet	DEVMETHOD(device_attach,	nexus_attach),
118129198Scognet	/* Bus interface */
119129198Scognet	DEVMETHOD(bus_print_child,	nexus_print_child),
120129198Scognet	DEVMETHOD(bus_add_child,	nexus_add_child),
121129198Scognet	DEVMETHOD(bus_alloc_resource,	nexus_alloc_resource),
122129198Scognet	DEVMETHOD(bus_activate_resource,	nexus_activate_resource),
123260161Szbb	DEVMETHOD(bus_config_intr,	nexus_config_intr),
124257702Snwhitehorn	DEVMETHOD(bus_deactivate_resource,	nexus_deactivate_resource),
125271484Sjhb	DEVMETHOD(bus_release_resource,	nexus_release_resource),
126129198Scognet	DEVMETHOD(bus_setup_intr,	nexus_setup_intr),
127147166Scognet	DEVMETHOD(bus_teardown_intr,	nexus_teardown_intr),
128295755Szbb	DEVMETHOD(bus_get_bus_tag,	nexus_get_bus_tag),
129289529Sian#ifdef ARM_INTRNG
130289529Sian	DEVMETHOD(bus_describe_intr,	nexus_describe_intr),
131289529Sian#ifdef SMP
132289529Sian	DEVMETHOD(bus_bind_intr,	nexus_bind_intr),
133289529Sian#endif
134289529Sian#endif
135260281Snwhitehorn#ifdef FDT
136260281Snwhitehorn	DEVMETHOD(ofw_bus_map_intr,	nexus_ofw_map_intr),
137260281Snwhitehorn#endif
138129198Scognet	{ 0, 0 }
139129198Scognet};
140129198Scognet
141257702Snwhitehornstatic devclass_t nexus_devclass;
142129198Scognetstatic driver_t nexus_driver = {
143129198Scognet	"nexus",
144129198Scognet	nexus_methods,
145129198Scognet	1			/* no softc */
146129198Scognet};
147283366SandrewEARLY_DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0,
148269606Sian    BUS_PASS_BUS + BUS_PASS_ORDER_EARLY);
149129198Scognet
150129198Scognetstatic int
151129198Scognetnexus_probe(device_t dev)
152129198Scognet{
153209129Sraj
154129198Scognet	device_quiet(dev);	/* suppress attach message for neatness */
155182934Sraj
156209129Sraj	return (BUS_PROBE_DEFAULT);
157129198Scognet}
158129198Scognet
159129198Scognetstatic int
160129198Scognetnexus_attach(device_t dev)
161129198Scognet{
162182934Sraj
163209232Sraj	mem_rman.rm_start = 0;
164221218Sjhb	mem_rman.rm_end = ~0ul;
165209232Sraj	mem_rman.rm_type = RMAN_ARRAY;
166209232Sraj	mem_rman.rm_descr = "I/O memory addresses";
167221218Sjhb	if (rman_init(&mem_rman) || rman_manage_region(&mem_rman, 0, ~0))
168209232Sraj		panic("nexus_probe mem_rman");
169209232Sraj
170129198Scognet	/*
171129198Scognet	 * First, deal with the children we know about already
172129198Scognet	 */
173129198Scognet	bus_generic_probe(dev);
174129198Scognet	bus_generic_attach(dev);
175182934Sraj
176182934Sraj	return (0);
177129198Scognet}
178129198Scognet
179129198Scognetstatic int
180129198Scognetnexus_print_child(device_t bus, device_t child)
181129198Scognet{
182129198Scognet	int retval = 0;
183182934Sraj
184129198Scognet	retval += bus_print_child_header(bus, child);
185235907Sgber	retval += printf("\n");
186182934Sraj
187129198Scognet	return (retval);
188129198Scognet}
189129198Scognet
190129198Scognetstatic device_t
191212413Savgnexus_add_child(device_t bus, u_int order, const char *name, int unit)
192129198Scognet{
193182934Sraj	device_t child;
194129198Scognet	struct nexus_device *ndev;
195182934Sraj
196129198Scognet	ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO);
197129198Scognet	if (!ndev)
198182934Sraj		return (0);
199129198Scognet	resource_list_init(&ndev->nx_resources);
200129198Scognet
201129198Scognet	child = device_add_child_ordered(bus, order, name, unit);
202182934Sraj
203129198Scognet	/* should we free this in nexus_child_detached? */
204129198Scognet	device_set_ivars(child, ndev);
205182934Sraj
206182934Sraj	return (child);
207129198Scognet}
208129198Scognet
209129198Scognet
210129198Scognet/*
211129198Scognet * Allocate a resource on behalf of child.  NB: child is usually going to be a
212129198Scognet * child of one of our descendants, not a direct child of nexus0.
213129198Scognet * (Exceptions include footbridge.)
214129198Scognet */
215129198Scognetstatic struct resource *
216129198Scognetnexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
217294883Sjhibbits    rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
218129198Scognet{
219129198Scognet	struct resource *rv;
220129198Scognet	struct rman *rm;
221129198Scognet	int needactivate = flags & RF_ACTIVE;
222129198Scognet
223271484Sjhb	flags &= ~RF_ACTIVE;
224271484Sjhb
225129198Scognet	switch (type) {
226129198Scognet	case SYS_RES_MEMORY:
227257702Snwhitehorn	case SYS_RES_IOPORT:
228129198Scognet		rm = &mem_rman;
229129198Scognet		break;
230182934Sraj
231129198Scognet	default:
232271484Sjhb		return (NULL);
233129198Scognet	}
234129198Scognet
235129198Scognet	rv = rman_reserve_resource(rm, start, end, count, flags, child);
236129198Scognet	if (rv == 0)
237271484Sjhb		return (NULL);
238129198Scognet
239157891Simp	rman_set_rid(rv, *rid);
240182934Sraj
241129198Scognet	if (needactivate) {
242129198Scognet		if (bus_activate_resource(child, type, *rid, rv)) {
243129198Scognet			rman_release_resource(rv);
244182934Sraj			return (0);
245129198Scognet		}
246129198Scognet	}
247182934Sraj
248182934Sraj	return (rv);
249129198Scognet}
250129198Scognet
251257702Snwhitehornstatic int
252271484Sjhbnexus_release_resource(device_t bus, device_t child, int type, int rid,
253271484Sjhb    struct resource *res)
254271484Sjhb{
255271484Sjhb	int error;
256283366Sandrew
257271484Sjhb	if (rman_get_flags(res) & RF_ACTIVE) {
258271484Sjhb		error = bus_deactivate_resource(child, type, rid, res);
259271484Sjhb		if (error)
260271484Sjhb			return (error);
261271484Sjhb	}
262271484Sjhb	return (rman_release_resource(res));
263271484Sjhb}
264271484Sjhb
265295755Szbbstatic bus_space_tag_t
266295755Szbbnexus_get_bus_tag(device_t bus __unused, device_t child __unused)
267295755Szbb{
268295755Szbb
269295755Szbb#ifdef FDT
270295755Szbb		return(fdtbus_bs_tag);
271295755Szbb#else
272295755Szbb		return((void *)1);
273295755Szbb#endif
274295755Szbb}
275295755Szbb
276271484Sjhbstatic int
277260161Szbbnexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
278260161Szbb    enum intr_polarity pol)
279260161Szbb{
280260161Szbb	int ret = ENODEV;
281260161Szbb
282289529Sian#ifdef ARM_INTRNG
283292426Sadrian	ret = intr_irq_config(irq, trig, pol);
284289529Sian#else
285260161Szbb	if (arm_config_irq)
286260161Szbb		ret = (*arm_config_irq)(irq, trig, pol);
287289529Sian#endif
288260161Szbb	return (ret);
289260161Szbb}
290260161Szbb
291260161Szbbstatic int
292257702Snwhitehornnexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags,
293257702Snwhitehorn    driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep)
294257702Snwhitehorn{
295257702Snwhitehorn	int irq;
296129198Scognet
297257702Snwhitehorn	if ((rman_get_flags(res) & RF_SHAREABLE) == 0)
298257702Snwhitehorn		flags |= INTR_EXCL;
299257702Snwhitehorn
300257702Snwhitehorn	for (irq = rman_get_start(res); irq <= rman_get_end(res); irq++) {
301289529Sian#ifdef ARM_INTRNG
302292426Sadrian		intr_irq_add_handler(child, filt, intr, arg, irq, flags,
303289529Sian		    cookiep);
304289529Sian#else
305257702Snwhitehorn		arm_setup_irqhandler(device_get_nameunit(child),
306257702Snwhitehorn		    filt, intr, arg, irq, flags, cookiep);
307257702Snwhitehorn		arm_unmask_irq(irq);
308289529Sian#endif
309257702Snwhitehorn	}
310257702Snwhitehorn	return (0);
311257702Snwhitehorn}
312257702Snwhitehorn
313129198Scognetstatic int
314257702Snwhitehornnexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
315257702Snwhitehorn{
316257702Snwhitehorn
317289529Sian#ifdef ARM_INTRNG
318292426Sadrian	return (intr_irq_remove_handler(child, rman_get_start(r), ih));
319289529Sian#else
320257702Snwhitehorn	return (arm_remove_irqhandler(rman_get_start(r), ih));
321289529Sian#endif
322257702Snwhitehorn}
323257702Snwhitehorn
324289529Sian#ifdef ARM_INTRNG
325289529Sianstatic int
326289529Siannexus_describe_intr(device_t dev, device_t child, struct resource *irq,
327289529Sian    void *cookie, const char *descr)
328289529Sian{
329257702Snwhitehorn
330292426Sadrian	return (intr_irq_describe(rman_get_start(irq), cookie, descr));
331289529Sian}
332289529Sian
333289529Sian#ifdef SMP
334257702Snwhitehornstatic int
335289529Siannexus_bind_intr(device_t dev, device_t child, struct resource *irq, int cpu)
336289529Sian{
337289529Sian
338292426Sadrian	return (intr_irq_bind(rman_get_start(irq), cpu));
339289529Sian}
340289529Sian#endif
341289529Sian#endif
342289529Sian
343289529Sianstatic int
344129198Scognetnexus_activate_resource(device_t bus, device_t child, int type, int rid,
345182934Sraj    struct resource *r)
346129198Scognet{
347260283Sian	int err;
348260283Sian	bus_addr_t paddr;
349260283Sian	bus_size_t psize;
350260283Sian	bus_space_handle_t vaddr;
351260283Sian
352260283Sian	if ((err = rman_activate_resource(r)) != 0)
353260283Sian		return (err);
354260283Sian
355129198Scognet	/*
356129198Scognet	 * If this is a memory resource, map it into the kernel.
357129198Scognet	 */
358257702Snwhitehorn	if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
359260283Sian		paddr = (bus_addr_t)rman_get_start(r);
360260283Sian		psize = (bus_size_t)rman_get_size(r);
361257702Snwhitehorn#ifdef FDT
362260283Sian		err = bus_space_map(fdtbus_bs_tag, paddr, psize, 0, &vaddr);
363260283Sian		if (err != 0) {
364260283Sian			rman_deactivate_resource(r);
365260283Sian			return (err);
366260283Sian		}
367257702Snwhitehorn		rman_set_bustag(r, fdtbus_bs_tag);
368257702Snwhitehorn#else
369260283Sian		vaddr = (bus_space_handle_t)pmap_mapdev((vm_offset_t)paddr,
370260283Sian		    (vm_size_t)psize);
371260283Sian		if (vaddr == 0) {
372260283Sian			rman_deactivate_resource(r);
373260283Sian			return (ENOMEM);
374260283Sian		}
375257702Snwhitehorn		rman_set_bustag(r, (void *)1);
376257702Snwhitehorn#endif
377260283Sian		rman_set_virtual(r, (void *)vaddr);
378260283Sian		rman_set_bushandle(r, vaddr);
379129198Scognet	}
380260283Sian	return (0);
381129198Scognet}
382129198Scognet
383257702Snwhitehornstatic int
384257702Snwhitehornnexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
385260283Sian    struct resource *r)
386257702Snwhitehorn{
387260283Sian	bus_size_t psize;
388260283Sian	bus_space_handle_t vaddr;
389257702Snwhitehorn
390260283Sian	psize = (bus_size_t)rman_get_size(r);
391260283Sian	vaddr = rman_get_bushandle(r);
392260283Sian
393260283Sian	if (vaddr != 0) {
394260283Sian#ifdef FDT
395260283Sian		bus_space_unmap(fdtbus_bs_tag, vaddr, psize);
396260283Sian#else
397260283Sian		pmap_unmapdev((vm_offset_t)vaddr, (vm_size_t)psize);
398260283Sian#endif
399260283Sian		rman_set_virtual(r, NULL);
400260283Sian		rman_set_bushandle(r, 0);
401260283Sian	}
402260283Sian
403260283Sian	return (rman_deactivate_resource(r));
404257702Snwhitehorn}
405257702Snwhitehorn
406260281Snwhitehorn#ifdef FDT
407260281Snwhitehornstatic int
408261351Snwhitehornnexus_ofw_map_intr(device_t dev, device_t child, phandle_t iparent, int icells,
409261351Snwhitehorn    pcell_t *intr)
410260281Snwhitehorn{
411260281Snwhitehorn
412292426Sadrian	return (intr_fdt_map_irq(iparent, intr, icells));
413260281Snwhitehorn}
414260281Snwhitehorn#endif
415