2 * Copyright (c) 2009 Neelkanth Natu
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/systm.h>
33 #include <sys/module.h>
35 #include <sys/malloc.h>
38 #include <sys/mutex.h>
40 #include <machine/resource.h>
41 #include <machine/intr_machdep.h>
45 static MALLOC_DEFINE(M_INTMAP, "sb1250 intmap", "Sibyte 1250 Interrupt Mapper");
47 static struct mtx zbbus_intr_mtx;
48 MTX_SYSINIT(zbbus_intr_mtx, &zbbus_intr_mtx, "zbbus_intr_mask/unmask lock",
52 * This array holds the mapping between a MIPS hard interrupt and the
53 * interrupt sources that feed into that it.
55 static uint64_t hardint_to_intsrc_mask[NHARD_IRQS];
58 int intsrc; /* interrupt mapper register number (0 - 63) */
59 int hardint; /* cpu interrupt from 0 to NHARD_IRQS - 1 */
62 * The device that the interrupt belongs to. Note that multiple
63 * devices may share an interrupt. For e.g. PCI_INT_x lines.
65 * The device 'dev' in combination with the 'rid' uniquely
66 * identify this interrupt source.
71 SLIST_ENTRY(sb_intmap) next;
74 static SLIST_HEAD(, sb_intmap) sb_intmap_head;
76 static struct sb_intmap *
77 sb_intmap_lookup(int intrnum, device_t dev, int rid)
79 struct sb_intmap *map;
81 SLIST_FOREACH(map, &sb_intmap_head, next) {
82 if (dev == map->dev && rid == map->rid &&
83 intrnum == map->hardint)
90 * Keep track of which (dev,rid,hardint) tuple is using the interrupt source.
92 * We don't actually unmask the interrupt source until the device calls
93 * a bus_setup_intr() on the resource.
96 sb_intmap_add(int intrnum, device_t dev, int rid, int intsrc)
98 struct sb_intmap *map;
100 KASSERT(intrnum >= 0 && intrnum < NHARD_IRQS,
101 ("intrnum is out of range: %d", intrnum));
103 map = sb_intmap_lookup(intrnum, dev, rid);
105 KASSERT(intsrc == map->intsrc,
106 ("%s%d allocating SYS_RES_IRQ resource with rid %d "
107 "with a different intsrc (%d versus %d)",
108 device_get_name(dev), device_get_unit(dev), rid,
109 intsrc, map->intsrc));
113 map = malloc(sizeof(*map), M_INTMAP, M_WAITOK | M_ZERO);
114 map->intsrc = intsrc;
115 map->hardint = intrnum;
119 SLIST_INSERT_HEAD(&sb_intmap_head, map, next);
123 sb_intmap_activate(int intrnum, device_t dev, int rid)
125 struct sb_intmap *map;
127 KASSERT(intrnum >= 0 && intrnum < NHARD_IRQS,
128 ("intrnum is out of range: %d", intrnum));
130 map = sb_intmap_lookup(intrnum, dev, rid);
133 * Deliver all interrupts to CPU0.
135 mtx_lock_spin(&zbbus_intr_mtx);
136 hardint_to_intsrc_mask[intrnum] |= 1ULL << map->intsrc;
137 sb_enable_intsrc(0, map->intsrc);
138 mtx_unlock_spin(&zbbus_intr_mtx);
141 * In zbbus_setup_intr() we blindly call sb_intmap_activate()
142 * for every interrupt activation that comes our way.
144 * We might end up here if we did not "hijack" the SYS_RES_IRQ
145 * resource in zbbus_alloc_resource().
147 printf("sb_intmap_activate: unable to activate interrupt %d "
148 "for device %s%d rid %d.\n", intrnum,
149 device_get_name(dev), device_get_unit(dev), rid);
154 * Replace the default interrupt mask and unmask routines in intr_machdep.c
155 * with routines that are SMP-friendly. In contrast to the default mask/unmask
156 * routines in intr_machdep.c these routines do not change the SR.int_mask bits.
158 * Instead they use the interrupt mapper to either mask or unmask all
159 * interrupt sources feeding into a particular interrupt line of the processor.
161 * This means that these routines have an identical effect irrespective of
162 * which cpu is executing them. This is important because the ithread may
163 * be scheduled to run on either of the cpus.
166 zbbus_intr_mask(void *arg)
171 irq = (uintptr_t)arg;
173 mtx_lock_spin(&zbbus_intr_mtx);
175 mask = sb_read_intsrc_mask(0);
176 mask |= hardint_to_intsrc_mask[irq];
177 sb_write_intsrc_mask(0, mask);
179 mtx_unlock_spin(&zbbus_intr_mtx);
183 zbbus_intr_unmask(void *arg)
188 irq = (uintptr_t)arg;
190 mtx_lock_spin(&zbbus_intr_mtx);
192 mask = sb_read_intsrc_mask(0);
193 mask &= ~hardint_to_intsrc_mask[irq];
194 sb_write_intsrc_mask(0, mask);
196 mtx_unlock_spin(&zbbus_intr_mtx);
199 struct zbbus_devinfo {
200 struct resource_list resources;
203 static MALLOC_DEFINE(M_ZBBUSDEV, "zbbusdev", "zbbusdev");
206 zbbus_probe(device_t dev)
209 device_set_desc(dev, "Broadcom/Sibyte ZBbus");
214 zbbus_attach(device_t dev)
218 device_printf(dev, "attached.\n");
221 cpu_set_hardintr_mask_func(zbbus_intr_mask);
222 cpu_set_hardintr_unmask_func(zbbus_intr_unmask);
224 bus_generic_probe(dev);
225 bus_enumerate_hinted_children(dev);
226 bus_generic_attach(dev);
232 zbbus_hinted_child(device_t bus, const char *dname, int dunit)
238 if (resource_disabled(dname, dunit))
241 child = BUS_ADD_CHILD(bus, 0, dname, dunit);
243 panic("zbbus: could not add child %s unit %d\n", dname, dunit);
247 device_printf(bus, "Adding hinted child %s%d\n", dname, dunit);
250 * Assign any pre-defined resources to the child.
252 if (resource_long_value(dname, dunit, "msize", &msize) == 0 &&
253 resource_long_value(dname, dunit, "maddr", &maddr) == 0) {
255 device_printf(bus, "Assigning memory resource "
256 "0x%0lx/%ld to child %s%d\n",
257 maddr, msize, dname, dunit);
259 err = bus_set_resource(child, SYS_RES_MEMORY, 0, maddr, msize);
261 device_printf(bus, "Unable to set memory resource "
262 "0x%0lx/%ld for child %s%d: %d\n",
263 maddr, msize, dname, dunit, err);
267 if (resource_int_value(dname, dunit, "irq", &irq) == 0) {
269 device_printf(bus, "Assigning irq resource %d to "
270 "child %s%d\n", irq, dname, dunit);
272 err = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
274 device_printf(bus, "Unable to set irq resource %d"
275 "for child %s%d: %d\n",
276 irq, dname, dunit, err);
281 static struct resource *
282 zbbus_alloc_resource(device_t bus, device_t child, int type, int *rid,
283 u_long start, u_long end, u_long count, u_int flags)
285 struct resource *res;
286 int intrnum, intsrc, isdefault;
287 struct resource_list *rl;
288 struct resource_list_entry *rle;
289 struct zbbus_devinfo *dinfo;
291 isdefault = (start == 0UL && end == ~0UL && count == 1);
294 * Our direct child is asking for a default resource allocation.
296 if (device_get_parent(child) == bus) {
297 dinfo = device_get_ivars(child);
298 rl = &dinfo->resources;
299 rle = resource_list_find(rl, type, *rid);
302 panic("zbbus_alloc_resource: resource is busy");
305 count = ulmax(count, rle->count);
306 end = ulmax(rle->end, start + count - 1);
311 * Our child is requesting a default
312 * resource allocation but we don't have the
313 * 'type/rid' tuple in the resource list.
315 * We have to fail the resource allocation.
320 * The child is requesting a non-default
321 * resource. We just pass the request up
322 * to our parent. If the resource allocation
323 * succeeds we will create a resource list
324 * entry corresponding to that resource.
334 * nexus doesn't know about the interrupt mapper and only wants to
335 * see the hard irq numbers [0-6]. We translate from the interrupt
336 * source presented to the mapper to the interrupt number presented
339 if ((count == 1) && (type == SYS_RES_IRQ)) {
341 intrnum = sb_route_intsrc(intsrc);
342 start = end = intrnum;
344 intsrc = -1; /* satisfy gcc */
348 res = bus_generic_alloc_resource(bus, child, type, rid,
349 start, end, count, flags);
352 * Keep track of the input into the interrupt mapper that maps
353 * to the resource allocated by 'child' with resource id 'rid'.
355 * If we don't record the mapping here then we won't be able to
356 * locate the interrupt source when bus_setup_intr(child,rid) is
359 if (res != NULL && intrnum != -1)
360 sb_intmap_add(intrnum, child, rman_get_rid(res), intsrc);
363 * If a non-default resource allocation by our child was successful
364 * then keep track of the resource in the resource list associated
367 if (res != NULL && rle == NULL && device_get_parent(child) == bus) {
368 resource_list_add(rl, type, *rid, start, end, count);
369 rle = resource_list_find(rl, type, *rid);
371 panic("zbbus_alloc_resource: cannot find resource");
375 KASSERT(device_get_parent(child) == bus,
376 ("rle should be NULL for passthru device"));
379 rle->start = rman_get_start(rle->res);
380 rle->end = rman_get_end(rle->res);
389 zbbus_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
390 driver_filter_t *filter, driver_intr_t *intr, void *arg,
395 error = bus_generic_setup_intr(dev, child, irq, flags,
396 filter, intr, arg, cookiep);
398 sb_intmap_activate(rman_get_start(irq), child,
405 zbbus_add_child(device_t bus, u_int order, const char *name, int unit)
408 struct zbbus_devinfo *dinfo;
410 child = device_add_child_ordered(bus, order, name, unit);
412 dinfo = malloc(sizeof(struct zbbus_devinfo), M_ZBBUSDEV,
414 resource_list_init(&dinfo->resources);
415 device_set_ivars(child, dinfo);
421 static struct resource_list *
422 zbbus_get_resource_list(device_t dev, device_t child)
424 struct zbbus_devinfo *dinfo = device_get_ivars(child);
426 return (&dinfo->resources);
429 static device_method_t zbbus_methods[] ={
430 /* Device interface */
431 DEVMETHOD(device_probe, zbbus_probe),
432 DEVMETHOD(device_attach, zbbus_attach),
433 DEVMETHOD(device_detach, bus_generic_detach),
434 DEVMETHOD(device_shutdown, bus_generic_shutdown),
435 DEVMETHOD(device_suspend, bus_generic_suspend),
436 DEVMETHOD(device_resume, bus_generic_resume),
439 DEVMETHOD(bus_alloc_resource, zbbus_alloc_resource),
440 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
441 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
442 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
443 DEVMETHOD(bus_get_resource_list,zbbus_get_resource_list),
444 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
445 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
446 DEVMETHOD(bus_delete_resource, bus_generic_rl_delete_resource),
447 DEVMETHOD(bus_setup_intr, zbbus_setup_intr),
448 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
449 DEVMETHOD(bus_add_child, zbbus_add_child),
450 DEVMETHOD(bus_hinted_child, zbbus_hinted_child),
455 static driver_t zbbus_driver = {
460 static devclass_t zbbus_devclass;
462 DRIVER_MODULE(zbbus, nexus, zbbus_driver, zbbus_devclass, 0, 0);