2 * Copyright 1998 Massachusetts Institute of Technology
4 * Permission to use, copy, modify, and distribute this software and
5 * its documentation for any purpose and without fee is hereby
6 * granted, provided that both the above copyright notice and this
7 * permission notice appear in all copies, that both the above
8 * copyright notice and this permission notice appear in all
9 * supporting documentation, and that the name of M.I.T. not be used
10 * in advertising or publicity pertaining to distribution of the
11 * software without specific, written prior permission. M.I.T. makes
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied
16 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS
17 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT
20 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * from: src/sys/i386/i386/nexus.c,v 1.26.2.5 2000/11/16 09:30:57 nyan
30 * JNPR: mainbus.c,v 1.2.4.1 2007/08/16 13:02:11 girish
34 * This code implements a `root mainbus' for Intel Architecture
35 * machines. The function of the root mainbus is to serve as an
36 * attachment point for both processors and buses, and to manage
37 * resources which are common to all of them. In particular,
38 * this code implements the core resource managers for interrupt
39 * requests, DMA requests (which rightfully should be a part of the
40 * ISA code but it's easier to do it here for now), I/O port addresses,
41 * and I/O memory address space.
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include "opt_cputype.h"
49 #include <sys/param.h>
50 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/module.h>
54 #include <machine/bus.h>
57 #include <machine/vmparam.h>
60 #include <sys/mutex.h>
62 #include <machine/pmap.h>
63 #include <machine/resource.h>
65 static struct rman irq_rman, port_rman, mem_rman;
67 static int mainbus_probe(device_t);
68 static int mainbus_attach(device_t);
69 static int mainbus_print_child(device_t, device_t);
70 static device_t mainbus_add_child(device_t bus, u_int order, const char *name,
72 static struct resource *mainbus_alloc_resource(device_t, device_t, int, int *,
73 u_long, u_long, u_long, u_int);
74 static int mainbus_activate_resource(device_t, device_t, int, int,
76 static int mainbus_deactivate_resource(device_t, device_t, int, int,
78 static int mainbus_release_resource(device_t, device_t, int, int,
80 static int mainbus_setup_intr(device_t, device_t, struct resource *,
81 int flags, driver_filter_t, void (*)(void *), void *, void **);
82 static int mainbus_teardown_intr(device_t, device_t, struct resource *,
85 static device_method_t mainbus_methods[] = {
86 /* Device interface */
87 DEVMETHOD(device_probe, mainbus_probe),
88 DEVMETHOD(device_attach, mainbus_attach),
89 DEVMETHOD(device_detach, bus_generic_detach),
90 DEVMETHOD(device_shutdown, bus_generic_shutdown),
91 DEVMETHOD(device_suspend, bus_generic_suspend),
92 DEVMETHOD(device_resume, bus_generic_resume),
95 DEVMETHOD(bus_print_child, mainbus_print_child),
96 DEVMETHOD(bus_add_child, mainbus_add_child),
97 DEVMETHOD(bus_read_ivar, bus_generic_read_ivar),
98 DEVMETHOD(bus_write_ivar, bus_generic_write_ivar),
99 DEVMETHOD(bus_alloc_resource, mainbus_alloc_resource),
100 DEVMETHOD(bus_release_resource, mainbus_release_resource),
101 DEVMETHOD(bus_activate_resource, mainbus_activate_resource),
102 DEVMETHOD(bus_deactivate_resource, mainbus_deactivate_resource),
103 DEVMETHOD(bus_setup_intr, mainbus_setup_intr),
104 DEVMETHOD(bus_teardown_intr, mainbus_teardown_intr),
109 static driver_t mainbus_driver = {
114 static devclass_t mainbus_devclass;
116 DRIVER_MODULE(mainbus, root, mainbus_driver, mainbus_devclass, 0, 0);
119 mainbus_probe(device_t dev)
123 device_verbose(dev); /* print attach message */
125 device_quiet(dev); /* suppress attach message for neatness */
128 irq_rman.rm_start = 0;
129 irq_rman.rm_type = RMAN_ARRAY;
130 irq_rman.rm_descr = "Interrupt request lines";
131 irq_rman.rm_end = 15;
132 if (rman_init(&irq_rman) ||
133 rman_manage_region(&irq_rman, irq_rman.rm_start, irq_rman.rm_end))
134 panic("mainbus_probe irq_rman");
137 * IO ports and Memory truely are global at this level,
138 * as are APIC interrupts (however many IO APICS there turn out
139 * to be on large systems..)
141 port_rman.rm_start = 0;
142 port_rman.rm_end = 0xffff;
143 port_rman.rm_type = RMAN_ARRAY;
144 port_rman.rm_descr = "I/O ports";
145 if (rman_init(&port_rman) || rman_manage_region(&port_rman, 0, 0xffff))
146 panic("mainbus_probe port_rman");
148 mem_rman.rm_start = 0;
149 mem_rman.rm_end = ~0ul;
150 mem_rman.rm_type = RMAN_ARRAY;
151 mem_rman.rm_descr = "I/O memory addresses";
152 if (rman_init(&mem_rman) || rman_manage_region(&mem_rman, 0, ~0))
153 panic("mainbus_probe mem_rman");
155 return bus_generic_probe(dev);
159 mainbus_attach(device_t dev)
162 * First, deal with the children we know about already
164 bus_generic_attach(dev);
170 mainbus_print_child(device_t bus, device_t child)
174 retval += bus_print_child_header(bus, child);
175 retval += printf(" on motherboard\n");
181 mainbus_add_child(device_t bus, u_int order, const char *name, int unit)
183 return device_add_child_ordered(bus, order, name, unit);
187 * Allocate a resource on behalf of child. NB: child is usually going to be a
188 * child of one of our descendants, not a direct child of mainbus0.
189 * (Exceptions include npx.)
191 static struct resource *
192 mainbus_alloc_resource(device_t bus, device_t child, int type, int *rid,
193 u_long start, u_long end, u_long count, u_int flags)
197 int needactivate = flags & RF_ACTIVE;
221 rv = rman_reserve_resource(rm, start, end, count, flags, child);
224 printf("mainbus_alloc_resource: no resource is available\n");
228 if (type == SYS_RES_MEMORY) {
229 rman_set_bustag(rv, MIPS_BUS_SPACE_MEM);
231 } else if (type == SYS_RES_IOPORT) {
232 rman_set_bustag(rv, MIPS_BUS_SPACE_IO);
233 /* IBM-PC: the type of bus_space_handle_t is u_int */
234 rman_set_bushandle(rv, rman_get_start(rv));
238 if (bus_activate_resource(child, type, *rid, rv)) {
239 rman_release_resource(rv);
248 mainbus_activate_resource(device_t bus, device_t child, int type, int rid,
252 * If this is a memory resource, map it into the kernel.
257 if (rman_get_bustag(r) == MIPS_BUS_SPACE_MEM) {
260 u_int32_t paddr, psize, poffs;
262 paddr = rman_get_start(r);
263 psize = rman_get_size(r);
265 poffs = paddr - trunc_page(paddr);
266 vaddr = (caddr_t) pmap_mapdev(paddr-poffs, psize+poffs)
269 rman_set_virtual(r, vaddr);
271 temp = 0x0000000000000000;
272 temp |= (uint32_t)vaddr;
273 rman_set_bushandle(r, (bus_space_handle_t) temp);
275 rman_set_bushandle(r, (bus_space_handle_t) vaddr);
278 return (rman_activate_resource(r));
282 mainbus_deactivate_resource(device_t bus, device_t child, int type, int rid,
286 * If this is a memory resource, unmap it.
288 if ((rman_get_bustag(r) == MIPS_BUS_SPACE_MEM) && (rman_get_end(r) >=
292 psize = rman_get_size(r);
293 pmap_unmapdev((vm_offset_t)rman_get_virtual(r), psize);
296 return (rman_deactivate_resource(r));
300 mainbus_release_resource(device_t bus, device_t child, int type, int rid,
303 if (rman_get_flags(r) & RF_ACTIVE) {
304 int error = bus_deactivate_resource(child, type, rid, r);
308 return (rman_release_resource(r));
312 * Currently this uses the really grody interface from kern/kern_intr.c
313 * (which really doesn't belong in kern/anything.c). Eventually, all of
314 * the code in kern_intr.c and machdep_intr.c should get moved here, since
315 * this is going to be the official interface.
317 * Set up handler for external interrupt events.
318 * Use CR_INT_<n> to select the proper interrupt
319 * condition to dispatch on.
322 mainbus_setup_intr(device_t bus, device_t child, struct resource *irq,
323 int flags, driver_filter_t filter, void (*ihand)(void *), void *arg,
326 panic("can never mainbus_setup_intr");
330 mainbus_teardown_intr(device_t dev, device_t child, struct resource *r,
333 panic("can never mainbus_teardown_intr");