]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/powerpc/powerpc/nexus.c
ofwbus: remove handling of resources from ofwbus
[FreeBSD/FreeBSD.git] / sys / powerpc / powerpc / nexus.c
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 struct rman intr_rman;
67 static struct rman mem_rman;
68
69 static device_probe_t nexus_probe;
70 static device_attach_t nexus_attach;
71 static bus_setup_intr_t nexus_setup_intr;
72 static bus_teardown_intr_t nexus_teardown_intr;
73 static bus_alloc_resource_t nexus_alloc_resource;
74 static bus_activate_resource_t nexus_activate_resource;
75 static bus_deactivate_resource_t nexus_deactivate_resource;
76 static bus_adjust_resource_t nexus_adjust_resource;
77 static bus_release_resource_t nexus_release_resource;
78 static  int nexus_map_resource(device_t bus, device_t child, int type,
79                                struct resource *r,
80                                struct resource_map_request *argsp,
81                                struct resource_map *map);
82 static  int nexus_unmap_resource(device_t bus, device_t child, int type,
83                                struct resource *r, struct resource_map *map);
84
85 static bus_space_tag_t nexus_get_bus_tag(device_t, device_t);
86 static int nexus_get_cpus(device_t, device_t, enum cpu_sets, size_t,
87     cpuset_t *);
88 #ifdef SMP
89 static bus_bind_intr_t nexus_bind_intr;
90 #endif
91 static bus_config_intr_t nexus_config_intr;
92 static ofw_bus_map_intr_t nexus_ofw_map_intr;
93
94 static device_method_t nexus_methods[] = {
95         /* Device interface */
96         DEVMETHOD(device_probe,         nexus_probe),
97         DEVMETHOD(device_attach,        nexus_attach),
98
99         /* Bus interface */
100         DEVMETHOD(bus_add_child,        bus_generic_add_child),
101         DEVMETHOD(bus_alloc_resource,   nexus_alloc_resource),
102         DEVMETHOD(bus_activate_resource,        nexus_activate_resource),
103         DEVMETHOD(bus_deactivate_resource,      nexus_deactivate_resource),
104         DEVMETHOD(bus_adjust_resource,  nexus_adjust_resource),
105         DEVMETHOD(bus_release_resource, nexus_release_resource),
106         DEVMETHOD(bus_map_resource,     nexus_map_resource),
107         DEVMETHOD(bus_unmap_resource,   nexus_unmap_resource),
108         DEVMETHOD(bus_setup_intr,       nexus_setup_intr),
109         DEVMETHOD(bus_teardown_intr,    nexus_teardown_intr),
110 #ifdef SMP
111         DEVMETHOD(bus_bind_intr,        nexus_bind_intr),
112 #endif
113         DEVMETHOD(bus_config_intr,      nexus_config_intr),
114         DEVMETHOD(bus_get_bus_tag,      nexus_get_bus_tag),
115         DEVMETHOD(bus_get_cpus,         nexus_get_cpus),
116
117         /* ofw_bus interface */
118         DEVMETHOD(ofw_bus_map_intr,     nexus_ofw_map_intr),
119
120         DEVMETHOD_END
121 };
122
123 DEFINE_CLASS_0(nexus, nexus_driver, nexus_methods, 1);
124 EARLY_DRIVER_MODULE(nexus, root, nexus_driver, 0, 0, BUS_PASS_BUS);
125 MODULE_VERSION(nexus, 1);
126
127 static int
128 nexus_probe(device_t dev)
129 {
130         
131         device_quiet(dev);      /* suppress attach message for neatness */
132
133         return (BUS_PROBE_DEFAULT);
134 }
135
136 static int
137 nexus_attach(device_t dev)
138 {
139
140         intr_rman.rm_type = RMAN_ARRAY;
141         intr_rman.rm_descr = "Interrupts";
142         mem_rman.rm_type = RMAN_ARRAY;
143         mem_rman.rm_descr = "I/O memory addresses";
144         if (rman_init(&intr_rman) != 0 || rman_init(&mem_rman) != 0 ||
145             rman_manage_region(&intr_rman, 0, ~0) != 0 ||
146             rman_manage_region(&mem_rman, 0, BUS_SPACE_MAXADDR) != 0)
147                 panic("%s: failed to set up rmans.", __func__);
148
149         bus_generic_probe(dev);
150         bus_generic_attach(dev);
151
152         return (0);
153 }
154
155 static int
156 nexus_setup_intr(device_t bus __unused, device_t child, struct resource *r,
157     int flags, driver_filter_t *filt, driver_intr_t *intr, void *arg,
158     void **cookiep)
159 {
160         int error, domain;
161
162         if (r == NULL)
163                 panic("%s: NULL interrupt resource!", __func__);
164
165         if (cookiep != NULL)
166                 *cookiep = NULL;
167         if ((rman_get_flags(r) & RF_SHAREABLE) == 0)
168                 flags |= INTR_EXCL;
169
170         /* We depend here on rman_activate_resource() being idempotent. */
171         error = rman_activate_resource(r);
172         if (error)
173                 return (error);
174
175         if (bus_get_domain(child, &domain) != 0) {
176                 if(bootverbose)
177                         device_printf(child, "no domain found\n");
178                 domain = 0;
179         }
180         error = powerpc_setup_intr(device_get_nameunit(child),
181             rman_get_start(r), filt, intr, arg, flags, cookiep, domain);
182
183         return (error);
184 }
185
186 static int
187 nexus_teardown_intr(device_t bus __unused, device_t child __unused,
188     struct resource *r, void *ih)
189 {
190         
191         if (r == NULL)
192                 return (EINVAL);
193
194         return (powerpc_teardown_intr(ih));
195 }
196
197 static bus_space_tag_t
198 nexus_get_bus_tag(device_t bus __unused, device_t child __unused)
199 {
200
201 #if BYTE_ORDER == LITTLE_ENDIAN
202         return(&bs_le_tag);
203 #else
204         return(&bs_be_tag);
205 #endif
206 }
207
208 static int
209 nexus_get_cpus(device_t dev, device_t child, enum cpu_sets op, size_t setsize,
210     cpuset_t *cpuset)
211 {
212
213         switch (op) {
214 #ifdef SMP
215         case INTR_CPUS:
216                 if (setsize != sizeof(cpuset_t))
217                         return (EINVAL);
218                 *cpuset = all_cpus;
219                 return (0);
220 #endif
221         default:
222                 return (bus_generic_get_cpus(dev, child, op, setsize, cpuset));
223         }
224 }
225
226 #ifdef SMP
227 static int
228 nexus_bind_intr(device_t bus __unused, device_t child __unused,
229     struct resource *r, int cpu)
230 {
231
232         return (powerpc_bind_intr(rman_get_start(r), cpu));
233 }
234 #endif
235
236 static int
237 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
238     enum intr_polarity pol)
239 {
240
241         return (powerpc_config_intr(irq, trig, pol));
242
243
244 static int
245 nexus_ofw_map_intr(device_t dev, device_t child, phandle_t iparent, int icells,
246     pcell_t *irq)
247 {
248         u_int intr = MAP_IRQ(iparent, irq[0]);
249         if (icells > 1)
250                 powerpc_fw_config_intr(intr, irq[1]);
251         return (intr);
252 }
253
254 /*
255  * Allocate a resource on behalf of child.  NB: child is usually going to be a
256  * child of one of our descendants, not a direct child of nexus0.
257  */
258 static struct resource *
259 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
260     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
261 {
262         struct rman *rm;
263         struct resource *rv;
264
265         switch (type) {
266         case SYS_RES_IRQ:
267                 rm = &intr_rman;
268                 break;
269         case SYS_RES_MEMORY:
270                 rm = &mem_rman;
271                 break;
272         default:
273                 return (NULL);
274         }
275
276         rv = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE,
277             child);
278         if (rv == NULL)
279                 return (NULL);
280         rman_set_rid(rv, *rid);
281
282         if ((flags & RF_ACTIVE) != 0) {
283                 if (bus_activate_resource(child, type, *rid, rv) != 0) {
284                         rman_release_resource(rv);
285                         return (NULL);
286                 }
287         }
288
289         return (rv);
290 }
291
292 static int
293 nexus_activate_resource(device_t bus __unused, device_t child __unused,
294     int type, int rid __unused, struct resource *r)
295 {
296
297         if (type == SYS_RES_MEMORY) {
298                 vm_paddr_t start;
299                 void *p;
300
301                 start = (vm_paddr_t) rman_get_start(r);
302                 if (bootverbose)
303                         printf("nexus mapdev: start %jx, len %jd\n",
304                             (uintmax_t)start, rman_get_size(r));
305
306                 p = pmap_mapdev(start, (vm_size_t) rman_get_size(r));
307                 if (p == NULL)
308                         return (ENOMEM);
309                 rman_set_virtual(r, p);
310                 rman_set_bustag(r, &bs_be_tag);
311                 rman_set_bushandle(r, (u_long)p);
312         }
313         return (rman_activate_resource(r));
314 }
315
316 static int
317 nexus_deactivate_resource(device_t bus __unused, device_t child __unused,
318     int type __unused, int rid __unused, struct resource *r)
319 {
320
321         /*
322          * If this is a memory resource, unmap it.
323          */
324         if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
325                 bus_size_t psize;
326
327                 psize = rman_get_size(r);
328                 pmap_unmapdev(rman_get_virtual(r), psize);
329         }
330
331         return (rman_deactivate_resource(r));
332 }
333
334 static int
335 nexus_adjust_resource(device_t bus, device_t child __unused, int type,
336     struct resource *r, rman_res_t start, rman_res_t end)
337 {
338         struct rman *rm;
339
340         switch (type) {
341         case SYS_RES_IRQ:
342                 rm = &intr_rman;
343                 break;
344         case SYS_RES_MEMORY:
345                 rm = &mem_rman;
346                 break;
347         default:
348                 return (EINVAL);
349         }
350         if (rm == NULL)
351                 return (ENXIO);
352         if (rman_is_region_manager(r, rm) == 0)
353                 return (EINVAL);
354         return (rman_adjust_resource(r, start, end));
355 }
356
357 static int
358 nexus_release_resource(device_t bus, device_t child, int type,
359     int rid, struct resource *r)
360 {
361         int error;
362
363         if ((rman_get_flags(r) & RF_ACTIVE) != 0) {
364                 error = bus_deactivate_resource(child, type, rid, r);
365                 if (error)
366                         return (error);
367         }
368         return (rman_release_resource(r));
369 }
370
371 static int
372 nexus_map_resource(device_t bus, device_t child, int type, struct resource *r,
373     struct resource_map_request *argsp, struct resource_map *map)
374 {
375
376         struct resource_map_request args;
377         rman_res_t end, length, start;
378
379         /* Resources must be active to be mapped. */
380         if (!(rman_get_flags(r) & RF_ACTIVE))
381                 return (ENXIO);
382
383         /* Mappings are only supported on I/O and memory resources. */
384         switch (type) {
385         case SYS_RES_IOPORT:
386         case SYS_RES_MEMORY:
387                 break;
388         default:
389                 return (EINVAL);
390         }
391
392         resource_init_map_request(&args);
393         if (argsp != NULL)
394                 bcopy(argsp, &args, imin(argsp->size, args.size));
395
396         start = rman_get_start(r) + args.offset;
397         if (args.length == 0)
398                 length = rman_get_size(r);
399         else
400                 length = args.length;
401
402         end = start + length - 1;
403         if (start > rman_get_end(r) || start < rman_get_start(r))
404                 return (EINVAL);
405
406         if (end > rman_get_end(r) || end < start)
407                 return (EINVAL);
408
409         /*
410          * If this is a memory resource, map it into the kernel.
411          */
412         switch (type) {
413         case SYS_RES_IOPORT:
414                 panic("%s:%d SYS_RES_IOPORT handling not implemented", __func__, __LINE__);
415                 /*   XXX: untested
416                 map->r_bushandle = start;
417                 map->r_bustag = nexus_get_bus_tag(NULL, NULL);
418                 map->r_size = length;
419                 map->r_vaddr = NULL;
420                 */
421                 break;
422         case SYS_RES_MEMORY:
423                 map->r_vaddr = pmap_mapdev_attr(start, length, args.memattr);
424                 map->r_bustag = nexus_get_bus_tag(NULL, NULL);
425                 map->r_size = length;
426                 map->r_bushandle = (bus_space_handle_t)map->r_vaddr;
427                 break;
428         }
429
430         return (0);
431
432 }
433
434 static int
435 nexus_unmap_resource(device_t bus, device_t child, int type, struct resource *r,
436     struct resource_map *map)
437 {
438
439         /*
440          * If this is a memory resource, unmap it.
441          */
442         switch (type) {
443         case SYS_RES_MEMORY:
444                 pmap_unmapdev(map->r_vaddr, map->r_size);
445                 /* FALLTHROUGH */
446         case SYS_RES_IOPORT:
447                 break;
448         default:
449                 return (EINVAL);
450         }
451
452         return (0);
453
454 }