2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009 Neelkanth Natu
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/systm.h>
35 #include <sys/module.h>
37 #include <sys/malloc.h>
40 #include <sys/mutex.h>
42 #include <machine/resource.h>
43 #include <machine/intr_machdep.h>
47 static MALLOC_DEFINE(M_INTMAP, "sb1250 intmap", "Sibyte 1250 Interrupt Mapper");
49 static struct mtx zbbus_intr_mtx;
50 MTX_SYSINIT(zbbus_intr_mtx, &zbbus_intr_mtx, "zbbus_intr_mask/unmask lock",
54 * This array holds the mapping between a MIPS hard interrupt and the
55 * interrupt sources that feed into that it.
57 static uint64_t hardint_to_intsrc_mask[NHARD_IRQS];
60 int intsrc; /* interrupt mapper register number (0 - 63) */
61 int hardint; /* cpu interrupt from 0 to NHARD_IRQS - 1 */
64 * The device that the interrupt belongs to. Note that multiple
65 * devices may share an interrupt. For e.g. PCI_INT_x lines.
67 * The device 'dev' in combination with the 'rid' uniquely
68 * identify this interrupt source.
73 SLIST_ENTRY(sb_intmap) next;
76 static SLIST_HEAD(, sb_intmap) sb_intmap_head;
78 static struct sb_intmap *
79 sb_intmap_lookup(int intrnum, device_t dev, int rid)
81 struct sb_intmap *map;
83 SLIST_FOREACH(map, &sb_intmap_head, next) {
84 if (dev == map->dev && rid == map->rid &&
85 intrnum == map->hardint)
92 * Keep track of which (dev,rid,hardint) tuple is using the interrupt source.
94 * We don't actually unmask the interrupt source until the device calls
95 * a bus_setup_intr() on the resource.
98 sb_intmap_add(int intrnum, device_t dev, int rid, int intsrc)
100 struct sb_intmap *map;
102 KASSERT(intrnum >= 0 && intrnum < NHARD_IRQS,
103 ("intrnum is out of range: %d", intrnum));
105 map = sb_intmap_lookup(intrnum, dev, rid);
107 KASSERT(intsrc == map->intsrc,
108 ("%s%d allocating SYS_RES_IRQ resource with rid %d "
109 "with a different intsrc (%d versus %d)",
110 device_get_name(dev), device_get_unit(dev), rid,
111 intsrc, map->intsrc));
115 map = malloc(sizeof(*map), M_INTMAP, M_WAITOK | M_ZERO);
116 map->intsrc = intsrc;
117 map->hardint = intrnum;
121 SLIST_INSERT_HEAD(&sb_intmap_head, map, next);
125 sb_intmap_activate(int intrnum, device_t dev, int rid)
127 struct sb_intmap *map;
129 KASSERT(intrnum >= 0 && intrnum < NHARD_IRQS,
130 ("intrnum is out of range: %d", intrnum));
132 map = sb_intmap_lookup(intrnum, dev, rid);
135 * Deliver all interrupts to CPU0.
137 mtx_lock_spin(&zbbus_intr_mtx);
138 hardint_to_intsrc_mask[intrnum] |= 1ULL << map->intsrc;
139 sb_enable_intsrc(0, map->intsrc);
140 mtx_unlock_spin(&zbbus_intr_mtx);
143 * In zbbus_setup_intr() we blindly call sb_intmap_activate()
144 * for every interrupt activation that comes our way.
146 * We might end up here if we did not "hijack" the SYS_RES_IRQ
147 * resource in zbbus_alloc_resource().
149 printf("sb_intmap_activate: unable to activate interrupt %d "
150 "for device %s%d rid %d.\n", intrnum,
151 device_get_name(dev), device_get_unit(dev), rid);
156 * Replace the default interrupt mask and unmask routines in intr_machdep.c
157 * with routines that are SMP-friendly. In contrast to the default mask/unmask
158 * routines in intr_machdep.c these routines do not change the SR.int_mask bits.
160 * Instead they use the interrupt mapper to either mask or unmask all
161 * interrupt sources feeding into a particular interrupt line of the processor.
163 * This means that these routines have an identical effect irrespective of
164 * which cpu is executing them. This is important because the ithread may
165 * be scheduled to run on either of the cpus.
168 zbbus_intr_mask(void *arg)
173 irq = (uintptr_t)arg;
175 mtx_lock_spin(&zbbus_intr_mtx);
177 mask = sb_read_intsrc_mask(0);
178 mask |= hardint_to_intsrc_mask[irq];
179 sb_write_intsrc_mask(0, mask);
181 mtx_unlock_spin(&zbbus_intr_mtx);
185 zbbus_intr_unmask(void *arg)
190 irq = (uintptr_t)arg;
192 mtx_lock_spin(&zbbus_intr_mtx);
194 mask = sb_read_intsrc_mask(0);
195 mask &= ~hardint_to_intsrc_mask[irq];
196 sb_write_intsrc_mask(0, mask);
198 mtx_unlock_spin(&zbbus_intr_mtx);
201 struct zbbus_devinfo {
202 struct resource_list resources;
205 static MALLOC_DEFINE(M_ZBBUSDEV, "zbbusdev", "zbbusdev");
208 zbbus_probe(device_t dev)
211 device_set_desc(dev, "Broadcom/Sibyte ZBbus");
212 return (BUS_PROBE_NOWILDCARD);
216 zbbus_attach(device_t dev)
220 device_printf(dev, "attached.\n");
223 cpu_set_hardintr_mask_func(zbbus_intr_mask);
224 cpu_set_hardintr_unmask_func(zbbus_intr_unmask);
226 bus_generic_probe(dev);
227 bus_enumerate_hinted_children(dev);
228 bus_generic_attach(dev);
234 zbbus_hinted_child(device_t bus, const char *dname, int dunit)
240 if (resource_disabled(dname, dunit))
243 child = BUS_ADD_CHILD(bus, 0, dname, dunit);
245 panic("zbbus: could not add child %s unit %d\n", dname, dunit);
249 device_printf(bus, "Adding hinted child %s%d\n", dname, dunit);
252 * Assign any pre-defined resources to the child.
254 if (resource_long_value(dname, dunit, "msize", &msize) == 0 &&
255 resource_long_value(dname, dunit, "maddr", &maddr) == 0) {
257 device_printf(bus, "Assigning memory resource "
258 "0x%0lx/%ld to child %s%d\n",
259 maddr, msize, dname, dunit);
261 err = bus_set_resource(child, SYS_RES_MEMORY, 0, maddr, msize);
263 device_printf(bus, "Unable to set memory resource "
264 "0x%0lx/%ld for child %s%d: %d\n",
265 maddr, msize, dname, dunit, err);
269 if (resource_int_value(dname, dunit, "irq", &irq) == 0) {
271 device_printf(bus, "Assigning irq resource %d to "
272 "child %s%d\n", irq, dname, dunit);
274 err = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
276 device_printf(bus, "Unable to set irq resource %d"
277 "for child %s%d: %d\n",
278 irq, dname, dunit, err);
283 static struct resource *
284 zbbus_alloc_resource(device_t bus, device_t child, int type, int *rid,
285 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
287 struct resource *res;
288 int intrnum, intsrc, isdefault;
289 struct resource_list *rl;
290 struct resource_list_entry *rle;
291 struct zbbus_devinfo *dinfo;
293 isdefault = (RMAN_IS_DEFAULT_RANGE(start, end) && count == 1);
296 * Our direct child is asking for a default resource allocation.
298 if (device_get_parent(child) == bus) {
299 dinfo = device_get_ivars(child);
300 rl = &dinfo->resources;
301 rle = resource_list_find(rl, type, *rid);
304 panic("zbbus_alloc_resource: resource is busy");
307 count = ulmax(count, rle->count);
308 end = ulmax(rle->end, start + count - 1);
313 * Our child is requesting a default
314 * resource allocation but we don't have the
315 * 'type/rid' tuple in the resource list.
317 * We have to fail the resource allocation.
322 * The child is requesting a non-default
323 * resource. We just pass the request up
324 * to our parent. If the resource allocation
325 * succeeds we will create a resource list
326 * entry corresponding to that resource.
336 * nexus doesn't know about the interrupt mapper and only wants to
337 * see the hard irq numbers [0-6]. We translate from the interrupt
338 * source presented to the mapper to the interrupt number presented
341 if ((count == 1) && (type == SYS_RES_IRQ)) {
343 intrnum = sb_route_intsrc(intsrc);
344 start = end = intrnum;
346 intsrc = -1; /* satisfy gcc */
350 res = bus_generic_alloc_resource(bus, child, type, rid,
351 start, end, count, flags);
354 * Keep track of the input into the interrupt mapper that maps
355 * to the resource allocated by 'child' with resource id 'rid'.
357 * If we don't record the mapping here then we won't be able to
358 * locate the interrupt source when bus_setup_intr(child,rid) is
361 if (res != NULL && intrnum != -1)
362 sb_intmap_add(intrnum, child, rman_get_rid(res), intsrc);
365 * If a non-default resource allocation by our child was successful
366 * then keep track of the resource in the resource list associated
369 if (res != NULL && rle == NULL && device_get_parent(child) == bus) {
370 resource_list_add(rl, type, *rid, start, end, count);
371 rle = resource_list_find(rl, type, *rid);
373 panic("zbbus_alloc_resource: cannot find resource");
377 KASSERT(device_get_parent(child) == bus,
378 ("rle should be NULL for passthru device"));
381 rle->start = rman_get_start(rle->res);
382 rle->end = rman_get_end(rle->res);
391 zbbus_setup_intr(device_t dev, device_t child, struct resource *irq, int flags,
392 driver_filter_t *filter, driver_intr_t *intr, void *arg,
397 error = bus_generic_setup_intr(dev, child, irq, flags,
398 filter, intr, arg, cookiep);
400 sb_intmap_activate(rman_get_start(irq), child,
407 zbbus_add_child(device_t bus, u_int order, const char *name, int unit)
410 struct zbbus_devinfo *dinfo;
412 child = device_add_child_ordered(bus, order, name, unit);
414 dinfo = malloc(sizeof(struct zbbus_devinfo), M_ZBBUSDEV,
416 resource_list_init(&dinfo->resources);
417 device_set_ivars(child, dinfo);
423 static struct resource_list *
424 zbbus_get_resource_list(device_t dev, device_t child)
426 struct zbbus_devinfo *dinfo = device_get_ivars(child);
428 return (&dinfo->resources);
431 static device_method_t zbbus_methods[] ={
432 /* Device interface */
433 DEVMETHOD(device_probe, zbbus_probe),
434 DEVMETHOD(device_attach, zbbus_attach),
435 DEVMETHOD(device_detach, bus_generic_detach),
436 DEVMETHOD(device_shutdown, bus_generic_shutdown),
437 DEVMETHOD(device_suspend, bus_generic_suspend),
438 DEVMETHOD(device_resume, bus_generic_resume),
441 DEVMETHOD(bus_alloc_resource, zbbus_alloc_resource),
442 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
443 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
444 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
445 DEVMETHOD(bus_get_resource_list,zbbus_get_resource_list),
446 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
447 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
448 DEVMETHOD(bus_delete_resource, bus_generic_rl_delete_resource),
449 DEVMETHOD(bus_setup_intr, zbbus_setup_intr),
450 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
451 DEVMETHOD(bus_add_child, zbbus_add_child),
452 DEVMETHOD(bus_hinted_child, zbbus_hinted_child),
457 static driver_t zbbus_driver = {
462 static devclass_t zbbus_devclass;
464 DRIVER_MODULE(zbbus, nexus, zbbus_driver, zbbus_devclass, 0, 0);