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/endian.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/bus.h>
42 #include <sys/kdb.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/module.h>
46 #include <sys/pcpu.h>
47 #include <sys/rman.h>
48 #include <sys/smp.h>
49
50 #include <vm/vm.h>
51 #include <vm/pmap.h>
52
53 #include <dev/ofw/ofw_bus.h>
54 #include <dev/ofw/ofw_bus_subr.h>
55 #include <dev/ofw/openfirm.h>
56
57 #include <machine/bus.h>
58 #include <machine/intr_machdep.h>
59 #include <machine/resource.h>
60
61 /*
62 * The nexus handles root-level resource allocation requests and interrupt
63 * mapping. All direct subdevices of nexus are attached by DEVICE_IDENTIFY().
64 */
65
66 static device_probe_t nexus_probe;
67 static device_attach_t nexus_attach;
68 static bus_setup_intr_t nexus_setup_intr;
69 static bus_teardown_intr_t nexus_teardown_intr;
70 static bus_activate_resource_t nexus_activate_resource;
71 static bus_deactivate_resource_t nexus_deactivate_resource;
72 static int nexus_map_resource(device_t bus, device_t child, int type,
73 struct resource *r,
74 struct resource_map_request *argsp,
75 struct resource_map *map);
76 static int nexus_unmap_resource(device_t bus, device_t child, int type,
77 struct resource *r, struct resource_map *map);
78
79 static bus_space_tag_t nexus_get_bus_tag(device_t, device_t);
80 static int nexus_get_cpus(device_t, device_t, enum cpu_sets, size_t,
81 cpuset_t *);
82 #ifdef SMP
83 static bus_bind_intr_t nexus_bind_intr;
84 #endif
85 static bus_config_intr_t nexus_config_intr;
86 static ofw_bus_map_intr_t nexus_ofw_map_intr;
87
88 static device_method_t nexus_methods[] = {
89 /* Device interface */
90 DEVMETHOD(device_probe, nexus_probe),
91 DEVMETHOD(device_attach, nexus_attach),
92
93 /* Bus interface */
94 DEVMETHOD(bus_add_child, bus_generic_add_child),
95 DEVMETHOD(bus_activate_resource, nexus_activate_resource),
96 DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource),
97 DEVMETHOD(bus_map_resource, nexus_map_resource),
98 DEVMETHOD(bus_unmap_resource, nexus_unmap_resource),
99 DEVMETHOD(bus_setup_intr, nexus_setup_intr),
100 DEVMETHOD(bus_teardown_intr, nexus_teardown_intr),
101 #ifdef SMP
102 DEVMETHOD(bus_bind_intr, nexus_bind_intr),
103 #endif
104 DEVMETHOD(bus_config_intr, nexus_config_intr),
105 DEVMETHOD(bus_get_bus_tag, nexus_get_bus_tag),
106 DEVMETHOD(bus_get_cpus, nexus_get_cpus),
107
108 /* ofw_bus interface */
109 DEVMETHOD(ofw_bus_map_intr, nexus_ofw_map_intr),
110
111 DEVMETHOD_END
112 };
113
114 static devclass_t nexus_devclass;
115
116 DEFINE_CLASS_0(nexus, nexus_driver, nexus_methods, 1);
117 EARLY_DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0,
118 BUS_PASS_BUS);
119 MODULE_VERSION(nexus, 1);
120
121 static int
122 nexus_probe(device_t dev)
123 {
124
125 device_quiet(dev); /* suppress attach message for neatness */
126
127 return (BUS_PROBE_DEFAULT);
128 }
129
130 static int
131 nexus_attach(device_t dev)
132 {
133
134 bus_generic_probe(dev);
135 bus_generic_attach(dev);
136
137 return (0);
138 }
139
140 static int
141 nexus_setup_intr(device_t bus __unused, device_t child, struct resource *r,
142 int flags, driver_filter_t *filt, driver_intr_t *intr, void *arg,
143 void **cookiep)
144 {
145 int error, domain;
146
147 if (r == NULL)
148 panic("%s: NULL interrupt resource!", __func__);
149
150 if (cookiep != NULL)
151 *cookiep = NULL;
152 if ((rman_get_flags(r) & RF_SHAREABLE) == 0)
153 flags |= INTR_EXCL;
154
155 /* We depend here on rman_activate_resource() being idempotent. */
156 error = rman_activate_resource(r);
157 if (error)
158 return (error);
159
160 if (bus_get_domain(child, &domain) != 0) {
161 if(bootverbose)
162 device_printf(child, "no domain found\n");
163 domain = 0;
164 }
165 error = powerpc_setup_intr(device_get_nameunit(child),
166 rman_get_start(r), filt, intr, arg, flags, cookiep, domain);
167
168 return (error);
169 }
170
171 static int
172 nexus_teardown_intr(device_t bus __unused, device_t child __unused,
173 struct resource *r, void *ih)
174 {
175
176 if (r == NULL)
177 return (EINVAL);
178
179 return (powerpc_teardown_intr(ih));
180 }
181
182 static bus_space_tag_t
183 nexus_get_bus_tag(device_t bus __unused, device_t child __unused)
184 {
185
186 #if BYTE_ORDER == LITTLE_ENDIAN
187 return(&bs_le_tag);
188 #else
189 return(&bs_be_tag);
190 #endif
191 }
192
193 static int
194 nexus_get_cpus(device_t dev, device_t child, enum cpu_sets op, size_t setsize,
195 cpuset_t *cpuset)
196 {
197
198 switch (op) {
199 #ifdef SMP
200 case INTR_CPUS:
201 if (setsize != sizeof(cpuset_t))
202 return (EINVAL);
203 *cpuset = all_cpus;
204 return (0);
205 #endif
206 default:
207 return (bus_generic_get_cpus(dev, child, op, setsize, cpuset));
208 }
209 }
210
211 #ifdef SMP
212 static int
213 nexus_bind_intr(device_t bus __unused, device_t child __unused,
214 struct resource *r, int cpu)
215 {
216
217 return (powerpc_bind_intr(rman_get_start(r), cpu));
218 }
219 #endif
220
221 static int
222 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
223 enum intr_polarity pol)
224 {
225
226 return (powerpc_config_intr(irq, trig, pol));
227 }
228
229 static int
230 nexus_ofw_map_intr(device_t dev, device_t child, phandle_t iparent, int icells,
231 pcell_t *irq)
232 {
233 u_int intr = MAP_IRQ(iparent, irq[0]);
234 if (icells > 1)
235 powerpc_fw_config_intr(intr, irq[1]);
236 return (intr);
237 }
238
239 static int
240 nexus_activate_resource(device_t bus __unused, device_t child __unused,
241 int type, int rid __unused, struct resource *r)
242 {
243
244 if (type == SYS_RES_MEMORY) {
245 vm_paddr_t start;
246 void *p;
247
248 start = (vm_paddr_t) rman_get_start(r);
249 if (bootverbose)
250 printf("nexus mapdev: start %jx, len %jd\n",
251 (uintmax_t)start, rman_get_size(r));
252
253 p = pmap_mapdev(start, (vm_size_t) rman_get_size(r));
254 if (p == NULL)
255 return (ENOMEM);
256 rman_set_virtual(r, p);
257 rman_set_bustag(r, &bs_be_tag);
258 rman_set_bushandle(r, (u_long)p);
259 }
260 return (rman_activate_resource(r));
261 }
262
263 static int
264 nexus_deactivate_resource(device_t bus __unused, device_t child __unused,
265 int type __unused, int rid __unused, struct resource *r)
266 {
267
268 /*
269 * If this is a memory resource, unmap it.
270 */
271 if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
272 bus_size_t psize;
273
274 psize = rman_get_size(r);
275 pmap_unmapdev((vm_offset_t)rman_get_virtual(r), psize);
276 }
277
278 return (rman_deactivate_resource(r));
279 }
280
281
282 static int
283 nexus_map_resource(device_t bus, device_t child, int type, struct resource *r,
284 struct resource_map_request *argsp, struct resource_map *map)
285 {
286
287 struct resource_map_request args;
288 rman_res_t end, length, start;
289
290 /* Resources must be active to be mapped. */
291 if (!(rman_get_flags(r) & RF_ACTIVE))
292 return (ENXIO);
293
294 /* Mappings are only supported on I/O and memory resources. */
295 switch (type) {
296 case SYS_RES_IOPORT:
297 case SYS_RES_MEMORY:
298 break;
299 default:
300 return (EINVAL);
301 }
302
303 resource_init_map_request(&args);
304 if (argsp != NULL)
305 bcopy(argsp, &args, imin(argsp->size, args.size));
306
307 start = rman_get_start(r) + args.offset;
308 if (args.length == 0)
309 length = rman_get_size(r);
310 else
311 length = args.length;
312
313 end = start + length - 1;
314 if (start > rman_get_end(r) || start < rman_get_start(r))
315 return (EINVAL);
316
317 if (end > rman_get_end(r) || end < start)
318 return (EINVAL);
319
320 /*
321 * If this is a memory resource, map it into the kernel.
322 */
323 switch (type) {
324 case SYS_RES_IOPORT:
325 panic("%s:%d SYS_RES_IOPORT handling not implemented", __func__, __LINE__);
326 /* XXX: untested
327 map->r_bushandle = start;
328 map->r_bustag = nexus_get_bus_tag(NULL, NULL);
329 map->r_size = length;
330 map->r_vaddr = NULL;
331 */
332 break;
333 case SYS_RES_MEMORY:
334 map->r_vaddr = pmap_mapdev_attr(start, length, args.memattr);
335 map->r_bustag = nexus_get_bus_tag(NULL, NULL);
336 map->r_size = length;
337 map->r_bushandle = (bus_space_handle_t)map->r_vaddr;
338 break;
339 }
340
341 return (0);
342
343 }
344
345 static int
346 nexus_unmap_resource(device_t bus, device_t child, int type, struct resource *r,
347 struct resource_map *map)
348 {
349
350 /*
351 * If this is a memory resource, unmap it.
352 */
353 switch (type) {
354 case SYS_RES_MEMORY:
355 pmap_unmapdev((vm_offset_t)map->r_vaddr, map->r_size);
356 /* FALLTHROUGH */
357 case SYS_RES_IOPORT:
358 break;
359 default:
360 return (EINVAL);
361 }
362
363 return (0);
364
365 }
Cache object: a0536f95797c0b024db64386185e409b
|