xref: /freebsd/sys/powerpc/powerpc/nexus.c (revision e17f5b1d)
1 /*-
2  * Copyright 1998 Massachusetts Institute of Technology
3  * Copyright 2001 by Thomas Moestl <tmm@FreeBSD.org>.
4  * Copyright 2006 by Marius Strobl <marius@FreeBSD.org>.
5  * All rights reserved.
6  *
7  * Permission to use, copy, modify, and distribute this software and
8  * its documentation for any purpose and without fee is hereby
9  * granted, provided that both the above copyright notice and this
10  * permission notice appear in all copies, that both the above
11  * copyright notice and this permission notice appear in all
12  * supporting documentation, and that the name of M.I.T. not be used
13  * in advertising or publicity pertaining to distribution of the
14  * software without specific, written prior permission.  M.I.T. makes
15  * no representations about the suitability of this software for any
16  * purpose.  It is provided "as is" without express or implied
17  * warranty.
18  *
19  * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''.  M.I.T. DISCLAIMS
20  * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
21  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
23  * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * 	from: FreeBSD: src/sys/i386/i386/nexus.c,v 1.43 2001/02/09
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/bus.h>
41 #include <sys/kdb.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/pcpu.h>
46 #include <sys/rman.h>
47 #include <sys/smp.h>
48 
49 #include <vm/vm.h>
50 #include <vm/pmap.h>
51 
52 #include <dev/ofw/ofw_bus.h>
53 #include <dev/ofw/ofw_bus_subr.h>
54 #include <dev/ofw/openfirm.h>
55 
56 #include <machine/bus.h>
57 #include <machine/intr_machdep.h>
58 #include <machine/resource.h>
59 
60 /*
61  * The nexus handles root-level resource allocation requests and interrupt
62  * mapping. All direct subdevices of nexus are attached by DEVICE_IDENTIFY().
63  */
64 
65 static device_probe_t nexus_probe;
66 static device_attach_t nexus_attach;
67 static bus_setup_intr_t nexus_setup_intr;
68 static bus_teardown_intr_t nexus_teardown_intr;
69 static bus_activate_resource_t nexus_activate_resource;
70 static bus_deactivate_resource_t nexus_deactivate_resource;
71 static bus_space_tag_t nexus_get_bus_tag(device_t, device_t);
72 static int nexus_get_cpus(device_t, device_t, enum cpu_sets, size_t,
73     cpuset_t *);
74 #ifdef SMP
75 static bus_bind_intr_t nexus_bind_intr;
76 #endif
77 static bus_config_intr_t nexus_config_intr;
78 static ofw_bus_map_intr_t nexus_ofw_map_intr;
79 
80 static device_method_t nexus_methods[] = {
81 	/* Device interface */
82 	DEVMETHOD(device_probe,		nexus_probe),
83 	DEVMETHOD(device_attach,	nexus_attach),
84 
85 	/* Bus interface */
86 	DEVMETHOD(bus_add_child,	bus_generic_add_child),
87 	DEVMETHOD(bus_activate_resource,	nexus_activate_resource),
88 	DEVMETHOD(bus_deactivate_resource,	nexus_deactivate_resource),
89 	DEVMETHOD(bus_setup_intr,	nexus_setup_intr),
90 	DEVMETHOD(bus_teardown_intr,	nexus_teardown_intr),
91 #ifdef SMP
92 	DEVMETHOD(bus_bind_intr,	nexus_bind_intr),
93 #endif
94 	DEVMETHOD(bus_config_intr,	nexus_config_intr),
95 	DEVMETHOD(bus_get_bus_tag,	nexus_get_bus_tag),
96 	DEVMETHOD(bus_get_cpus,		nexus_get_cpus),
97 
98 	/* ofw_bus interface */
99 	DEVMETHOD(ofw_bus_map_intr,	nexus_ofw_map_intr),
100 
101 	DEVMETHOD_END
102 };
103 
104 static devclass_t nexus_devclass;
105 
106 DEFINE_CLASS_0(nexus, nexus_driver, nexus_methods, 1);
107 EARLY_DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0,
108     BUS_PASS_BUS);
109 MODULE_VERSION(nexus, 1);
110 
111 static int
112 nexus_probe(device_t dev)
113 {
114 
115 	device_quiet(dev);	/* suppress attach message for neatness */
116 
117 	return (BUS_PROBE_DEFAULT);
118 }
119 
120 static int
121 nexus_attach(device_t dev)
122 {
123 
124 	bus_generic_probe(dev);
125 	bus_generic_attach(dev);
126 
127 	return (0);
128 }
129 
130 static int
131 nexus_setup_intr(device_t bus __unused, device_t child, struct resource *r,
132     int flags, driver_filter_t *filt, driver_intr_t *intr, void *arg,
133     void **cookiep)
134 {
135 	int error, domain;
136 
137 	if (r == NULL)
138 		panic("%s: NULL interrupt resource!", __func__);
139 
140 	if (cookiep != NULL)
141 		*cookiep = NULL;
142 	if ((rman_get_flags(r) & RF_SHAREABLE) == 0)
143 		flags |= INTR_EXCL;
144 
145 	/* We depend here on rman_activate_resource() being idempotent. */
146 	error = rman_activate_resource(r);
147 	if (error)
148 		return (error);
149 
150 	if (bus_get_domain(child, &domain) != 0) {
151 		if(bootverbose)
152 			device_printf(child, "no domain found\n");
153 		domain = 0;
154 	}
155 	error = powerpc_setup_intr(device_get_nameunit(child),
156 	    rman_get_start(r), filt, intr, arg, flags, cookiep, domain);
157 
158 	return (error);
159 }
160 
161 static int
162 nexus_teardown_intr(device_t bus __unused, device_t child __unused,
163     struct resource *r, void *ih)
164 {
165 
166 	if (r == NULL)
167 		return (EINVAL);
168 
169 	return (powerpc_teardown_intr(ih));
170 }
171 
172 static bus_space_tag_t
173 nexus_get_bus_tag(device_t bus __unused, device_t child __unused)
174 {
175 
176 	return(&bs_be_tag);
177 }
178 
179 static int
180 nexus_get_cpus(device_t dev, device_t child, enum cpu_sets op, size_t setsize,
181     cpuset_t *cpuset)
182 {
183 
184 	switch (op) {
185 #ifdef SMP
186 	case INTR_CPUS:
187 		if (setsize != sizeof(cpuset_t))
188 			return (EINVAL);
189 		*cpuset = all_cpus;
190 		return (0);
191 #endif
192 	default:
193 		return (bus_generic_get_cpus(dev, child, op, setsize, cpuset));
194 	}
195 }
196 
197 #ifdef SMP
198 static int
199 nexus_bind_intr(device_t bus __unused, device_t child __unused,
200     struct resource *r, int cpu)
201 {
202 
203 	return (powerpc_bind_intr(rman_get_start(r), cpu));
204 }
205 #endif
206 
207 static int
208 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
209     enum intr_polarity pol)
210 {
211 
212 	return (powerpc_config_intr(irq, trig, pol));
213 }
214 
215 static int
216 nexus_ofw_map_intr(device_t dev, device_t child, phandle_t iparent, int icells,
217     pcell_t *irq)
218 {
219 	u_int intr = MAP_IRQ(iparent, irq[0]);
220 	if (icells > 1)
221 		powerpc_fw_config_intr(intr, irq[1]);
222 	return (intr);
223 }
224 
225 static int
226 nexus_activate_resource(device_t bus __unused, device_t child __unused,
227     int type, int rid __unused, struct resource *r)
228 {
229 
230 	if (type == SYS_RES_MEMORY) {
231 		vm_paddr_t start;
232 		void *p;
233 
234 		start = (vm_paddr_t) rman_get_start(r);
235 		if (bootverbose)
236 			printf("nexus mapdev: start %jx, len %jd\n",
237 			    (uintmax_t)start, rman_get_size(r));
238 
239 		p = pmap_mapdev(start, (vm_size_t) rman_get_size(r));
240 		if (p == NULL)
241 			return (ENOMEM);
242 		rman_set_virtual(r, p);
243 		rman_set_bustag(r, &bs_be_tag);
244 		rman_set_bushandle(r, (u_long)p);
245 	}
246 	return (rman_activate_resource(r));
247 }
248 
249 static int
250 nexus_deactivate_resource(device_t bus __unused, device_t child __unused,
251     int type __unused, int rid __unused, struct resource *r)
252 {
253 
254 	/*
255 	 * If this is a memory resource, unmap it.
256 	 */
257 	if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
258 		bus_size_t psize;
259 
260 		psize = rman_get_size(r);
261 		pmap_unmapdev((vm_offset_t)rman_get_virtual(r), psize);
262 	}
263 
264 	return (rman_deactivate_resource(r));
265 }
266 
267