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 /*
36 * This code implements a `root nexus' for Power ISA Architecture
37 * machines. The function of the root nexus is to serve as an
38 * attachment point for both processors and buses, and to manage
39 * resources which are common to all of them. In particular,
40 * this code implements the core resource managers for interrupt
41 * requests and I/O memory address space.
42 */
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/bus.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/module.h>
50 #include <sys/rman.h>
51
52 #include <vm/vm.h>
53 #include <vm/pmap.h>
54
55 #include <machine/bus.h>
56 #include <machine/endian.h>
57 #include <machine/intr_machdep.h>
58 #include <machine/resource.h>
59
60 #include <dev/ofw/ofw_bus.h>
61 #include <dev/ofw/ofw_bus_subr.h>
62 #include <dev/ofw/openfirm.h>
63
64 static struct rman intr_rman;
65 static struct rman mem_rman;
66
67 static device_probe_t nexus_probe;
68 static device_attach_t nexus_attach;
69
70 static bus_get_rman_t nexus_get_rman;
71 static bus_map_resource_t nexus_map_resource;
72 static bus_unmap_resource_t nexus_unmap_resource;
73
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 bus_setup_intr_t nexus_setup_intr;
79 static bus_teardown_intr_t nexus_teardown_intr;
80
81 static bus_get_bus_tag_t nexus_get_bus_tag;
82
83 static ofw_bus_map_intr_t nexus_ofw_map_intr;
84
85 static device_method_t nexus_methods[] = {
86 /* Device interface */
87 DEVMETHOD(device_probe, nexus_probe),
88 DEVMETHOD(device_attach, nexus_attach),
89 DEVMETHOD(device_shutdown, bus_generic_shutdown),
90
91 /* Bus interface */
92 DEVMETHOD(bus_add_child, bus_generic_add_child),
93 DEVMETHOD(bus_adjust_resource, bus_generic_rman_adjust_resource),
94 DEVMETHOD(bus_activate_resource, bus_generic_rman_activate_resource),
95 DEVMETHOD(bus_alloc_resource, bus_generic_rman_alloc_resource),
96 DEVMETHOD(bus_deactivate_resource, bus_generic_rman_deactivate_resource),
97 DEVMETHOD(bus_get_rman, nexus_get_rman),
98 DEVMETHOD(bus_map_resource, nexus_map_resource),
99 DEVMETHOD(bus_release_resource, bus_generic_rman_release_resource),
100 DEVMETHOD(bus_unmap_resource, nexus_unmap_resource),
101 #ifdef SMP
102 DEVMETHOD(bus_bind_intr, nexus_bind_intr),
103 #endif
104 DEVMETHOD(bus_config_intr, nexus_config_intr),
105 DEVMETHOD(bus_setup_intr, nexus_setup_intr),
106 DEVMETHOD(bus_teardown_intr, nexus_teardown_intr),
107 DEVMETHOD(bus_get_bus_tag, nexus_get_bus_tag),
108
109 /* ofw_bus interface */
110 DEVMETHOD(ofw_bus_map_intr, nexus_ofw_map_intr),
111
112 DEVMETHOD_END
113 };
114
115 DEFINE_CLASS_0(nexus, nexus_driver, nexus_methods, 1);
116 EARLY_DRIVER_MODULE(nexus, root, nexus_driver, 0, 0, BUS_PASS_BUS);
117 MODULE_VERSION(nexus, 1);
118
119 static int
nexus_probe(device_t dev)120 nexus_probe(device_t dev)
121 {
122
123 device_quiet(dev); /* suppress attach message for neatness */
124
125 return (BUS_PROBE_DEFAULT);
126 }
127
128 static int
nexus_attach(device_t dev)129 nexus_attach(device_t dev)
130 {
131
132 intr_rman.rm_type = RMAN_ARRAY;
133 intr_rman.rm_descr = "Interrupts";
134 mem_rman.rm_type = RMAN_ARRAY;
135 mem_rman.rm_descr = "I/O memory addresses";
136 if (rman_init(&intr_rman) != 0 || rman_init(&mem_rman) != 0 ||
137 rman_manage_region(&intr_rman, 0, ~0) != 0 ||
138 rman_manage_region(&mem_rman, 0, BUS_SPACE_MAXADDR) != 0)
139 panic("%s: failed to set up rmans.", __func__);
140
141 /* Add ofwbus0. */
142 device_add_child(dev, "ofwbus", 0);
143
144 /* Now, probe children. */
145 bus_identify_children(dev);
146 bus_attach_children(dev);
147
148 return (0);
149 }
150
151 static int
nexus_setup_intr(device_t bus __unused,device_t child,struct resource * r,int flags,driver_filter_t * filt,driver_intr_t * intr,void * arg,void ** cookiep)152 nexus_setup_intr(device_t bus __unused, device_t child, struct resource *r,
153 int flags, driver_filter_t *filt, driver_intr_t *intr, void *arg,
154 void **cookiep)
155 {
156 int error, domain;
157
158 if (r == NULL)
159 panic("%s: NULL interrupt resource!", __func__);
160
161 if (cookiep != NULL)
162 *cookiep = NULL;
163 if ((rman_get_flags(r) & RF_SHAREABLE) == 0)
164 flags |= INTR_EXCL;
165
166 /* We depend here on rman_activate_resource() being idempotent. */
167 error = rman_activate_resource(r);
168 if (error)
169 return (error);
170
171 if (bus_get_domain(child, &domain) != 0) {
172 if(bootverbose)
173 device_printf(child, "no domain found\n");
174 domain = 0;
175 }
176 error = powerpc_setup_intr(device_get_nameunit(child),
177 rman_get_start(r), filt, intr, arg, flags, cookiep, domain);
178
179 return (error);
180 }
181
182 static int
nexus_teardown_intr(device_t bus __unused,device_t child __unused,struct resource * r,void * ih)183 nexus_teardown_intr(device_t bus __unused, device_t child __unused,
184 struct resource *r, void *ih)
185 {
186
187 if (r == NULL)
188 return (EINVAL);
189
190 return (powerpc_teardown_intr(ih));
191 }
192
193 static bus_space_tag_t
nexus_get_bus_tag(device_t bus __unused,device_t child __unused)194 nexus_get_bus_tag(device_t bus __unused, device_t child __unused)
195 {
196
197 #if BYTE_ORDER == LITTLE_ENDIAN
198 return(&bs_le_tag);
199 #else
200 return(&bs_be_tag);
201 #endif
202 }
203
204 #ifdef SMP
205 static int
nexus_bind_intr(device_t bus __unused,device_t child __unused,struct resource * r,int cpu)206 nexus_bind_intr(device_t bus __unused, device_t child __unused,
207 struct resource *r, int cpu)
208 {
209
210 return (powerpc_bind_intr(rman_get_start(r), cpu));
211 }
212 #endif
213
214 static int
nexus_config_intr(device_t dev,int irq,enum intr_trigger trig,enum intr_polarity pol)215 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
216 enum intr_polarity pol)
217 {
218
219 return (powerpc_config_intr(irq, trig, pol));
220 }
221
222 static int
nexus_ofw_map_intr(device_t dev,device_t child,phandle_t iparent,int icells,pcell_t * irq)223 nexus_ofw_map_intr(device_t dev, device_t child, phandle_t iparent, int icells,
224 pcell_t *irq)
225 {
226 u_int intr = MAP_IRQ(iparent, irq[0]);
227 if (icells > 1)
228 powerpc_fw_config_intr(intr, irq[1]);
229 return (intr);
230 }
231
232 static struct rman *
nexus_get_rman(device_t bus,int type,u_int flags)233 nexus_get_rman(device_t bus, int type, u_int flags)
234 {
235 switch (type) {
236 case SYS_RES_IRQ:
237 return (&intr_rman);
238 case SYS_RES_MEMORY:
239 return (&mem_rman);
240 default:
241 return (NULL);
242 }
243 }
244
245 static int
nexus_map_resource(device_t bus,device_t child,struct resource * r,struct resource_map_request * argsp,struct resource_map * map)246 nexus_map_resource(device_t bus, device_t child, struct resource *r,
247 struct resource_map_request *argsp, struct resource_map *map)
248 {
249 struct resource_map_request args;
250 rman_res_t length, start;
251 int error;
252
253 /* Resources must be active to be mapped. */
254 if (!(rman_get_flags(r) & RF_ACTIVE))
255 return (ENXIO);
256
257 /* Mappings are only supported on I/O and memory resources. */
258 switch (rman_get_type(r)) {
259 case SYS_RES_IOPORT:
260 case SYS_RES_MEMORY:
261 break;
262 default:
263 return (EINVAL);
264 }
265
266 resource_init_map_request(&args);
267 error = resource_validate_map_request(r, argsp, &args, &start, &length);
268 if (error)
269 return (error);
270
271 /*
272 * If this is a memory resource, map it into the kernel.
273 */
274 switch (rman_get_type(r)) {
275 case SYS_RES_IOPORT:
276 panic("%s:%d SYS_RES_IOPORT handling not implemented", __func__, __LINE__);
277 /* XXX: untested
278 map->r_bushandle = start;
279 if ((rman_get_flags(r) & RF_LITTLEENDIAN) != 0)
280 map->r_bustag = &bs_le_tag;
281 else
282 map->r_bustag = nexus_get_bus_tag(NULL, NULL);
283 map->r_size = length;
284 map->r_vaddr = NULL;
285 */
286 break;
287 case SYS_RES_MEMORY:
288 map->r_vaddr = pmap_mapdev_attr(start, length, args.memattr);
289 if ((rman_get_flags(r) & RF_LITTLEENDIAN) != 0)
290 map->r_bustag = &bs_le_tag;
291 else
292 map->r_bustag = &bs_be_tag;
293 map->r_size = length;
294 map->r_bushandle = (bus_space_handle_t)map->r_vaddr;
295 break;
296 }
297
298 return (0);
299
300 }
301
302 static int
nexus_unmap_resource(device_t bus,device_t child,struct resource * r,struct resource_map * map)303 nexus_unmap_resource(device_t bus, device_t child, struct resource *r,
304 struct resource_map *map)
305 {
306
307 /*
308 * If this is a memory resource, unmap it.
309 */
310 switch (rman_get_type(r)) {
311 case SYS_RES_MEMORY:
312 pmap_unmapdev(map->r_vaddr, map->r_size);
313 /* FALLTHROUGH */
314 case SYS_RES_IOPORT:
315 break;
316 default:
317 return (EINVAL);
318 }
319
320 return (0);
321
322 }
323