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 <sys/param.h>
48 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/module.h>
52 #include <machine/bus.h>
55 #include <machine/vmparam.h>
58 #include <sys/mutex.h>
60 #include <machine/pmap.h>
61 #include <machine/resource.h>
63 static struct rman irq_rman, port_rman, mem_rman;
65 static int mainbus_probe(device_t);
66 static int mainbus_attach(device_t);
67 static int mainbus_print_child(device_t, device_t);
68 static device_t mainbus_add_child(device_t bus, int order, const char *name,
70 static struct resource *mainbus_alloc_resource(device_t, device_t, int, int *,
71 u_long, u_long, u_long, u_int);
72 static int mainbus_activate_resource(device_t, device_t, int, int,
74 static int mainbus_deactivate_resource(device_t, device_t, int, int,
76 static int mainbus_release_resource(device_t, device_t, int, int,
78 static int mainbus_setup_intr(device_t, device_t, struct resource *,
79 int flags, driver_filter_t, void (*)(void *), void *, void **);
80 static int mainbus_teardown_intr(device_t, device_t, struct resource *,
83 static device_method_t mainbus_methods[] = {
84 /* Device interface */
85 DEVMETHOD(device_probe, mainbus_probe),
86 DEVMETHOD(device_attach, mainbus_attach),
87 DEVMETHOD(device_detach, bus_generic_detach),
88 DEVMETHOD(device_shutdown, bus_generic_shutdown),
89 DEVMETHOD(device_suspend, bus_generic_suspend),
90 DEVMETHOD(device_resume, bus_generic_resume),
93 DEVMETHOD(bus_print_child, mainbus_print_child),
94 DEVMETHOD(bus_add_child, mainbus_add_child),
95 DEVMETHOD(bus_read_ivar, bus_generic_read_ivar),
96 DEVMETHOD(bus_write_ivar, bus_generic_write_ivar),
97 DEVMETHOD(bus_alloc_resource, mainbus_alloc_resource),
98 DEVMETHOD(bus_release_resource, mainbus_release_resource),
99 DEVMETHOD(bus_activate_resource, mainbus_activate_resource),
100 DEVMETHOD(bus_deactivate_resource, mainbus_deactivate_resource),
101 DEVMETHOD(bus_setup_intr, mainbus_setup_intr),
102 DEVMETHOD(bus_teardown_intr, mainbus_teardown_intr),
107 static driver_t mainbus_driver = {
112 static devclass_t mainbus_devclass;
114 DRIVER_MODULE(mainbus, root, mainbus_driver, mainbus_devclass, 0, 0);
117 mainbus_probe(device_t dev)
121 device_verbose(dev); /* print attach message */
123 device_quiet(dev); /* suppress attach message for neatness */
126 irq_rman.rm_start = 0;
127 irq_rman.rm_type = RMAN_ARRAY;
128 irq_rman.rm_descr = "Interrupt request lines";
129 irq_rman.rm_end = 15;
130 if (rman_init(&irq_rman) ||
131 rman_manage_region(&irq_rman, irq_rman.rm_start, irq_rman.rm_end))
132 panic("mainbus_probe irq_rman");
135 * IO ports and Memory truely are global at this level,
136 * as are APIC interrupts (however many IO APICS there turn out
137 * to be on large systems..)
139 port_rman.rm_start = 0;
140 port_rman.rm_end = 0xffff;
141 port_rman.rm_type = RMAN_ARRAY;
142 port_rman.rm_descr = "I/O ports";
143 if (rman_init(&port_rman) || rman_manage_region(&port_rman, 0, 0xffff))
144 panic("mainbus_probe port_rman");
146 mem_rman.rm_start = 0;
147 mem_rman.rm_end = ~0u;
148 mem_rman.rm_type = RMAN_ARRAY;
149 mem_rman.rm_descr = "I/O memory addresses";
150 if (rman_init(&mem_rman) || rman_manage_region(&mem_rman, 0, ~0))
151 panic("mainbus_probe mem_rman");
153 return bus_generic_probe(dev);
157 mainbus_attach(device_t dev)
160 * First, deal with the children we know about already
162 bus_generic_attach(dev);
168 mainbus_print_child(device_t bus, device_t child)
172 retval += bus_print_child_header(bus, child);
173 retval += printf(" on motherboard\n");
179 mainbus_add_child(device_t bus, int order, const char *name, int unit)
181 return device_add_child_ordered(bus, order, name, unit);
185 * Allocate a resource on behalf of child. NB: child is usually going to be a
186 * child of one of our descendants, not a direct child of mainbus0.
187 * (Exceptions include npx.)
189 static struct resource *
190 mainbus_alloc_resource(device_t bus, device_t child, int type, int *rid,
191 u_long start, u_long end, u_long count, u_int flags)
195 int needactivate = flags & RF_ACTIVE;
219 rv = rman_reserve_resource(rm, start, end, count, flags, child);
222 printf("mainbus_alloc_resource: no resource is available\n");
226 if (type == SYS_RES_MEMORY) {
227 rman_set_bustag(rv, MIPS_BUS_SPACE_MEM);
229 } else if (type == SYS_RES_IOPORT) {
230 rman_set_bustag(rv, MIPS_BUS_SPACE_IO);
231 /* IBM-PC: the type of bus_space_handle_t is u_int */
232 rman_set_bushandle(rv, rman_get_start(rv));
236 if (bus_activate_resource(child, type, *rid, rv)) {
237 rman_release_resource(rv);
246 mainbus_activate_resource(device_t bus, device_t child, int type, int rid,
250 * If this is a memory resource, map it into the kernel.
255 if (rman_get_bustag(r) == MIPS_BUS_SPACE_MEM) {
258 u_int32_t paddr, psize, poffs;
260 paddr = rman_get_start(r);
261 psize = rman_get_size(r);
263 poffs = paddr - trunc_page(paddr);
264 vaddr = (caddr_t) pmap_mapdev(paddr-poffs, psize+poffs)
267 rman_set_virtual(r, vaddr);
268 /* IBM-PC: the type of bus_space_handle_t is u_int */
270 temp = 0x0000000000000000;
271 temp |= (uint32_t)vaddr;
272 rman_set_bushandle(r, (bus_space_handle_t) temp);
274 rman_set_bushandle(r, (bus_space_handle_t) vaddr);
277 return (rman_activate_resource(r));
281 mainbus_deactivate_resource(device_t bus, device_t child, int type, int rid,
285 * If this is a memory resource, unmap it.
287 if ((rman_get_bustag(r) == MIPS_BUS_SPACE_MEM) && (rman_get_end(r) >=
291 psize = rman_get_size(r);
292 pmap_unmapdev((vm_offset_t)rman_get_virtual(r), psize);
295 return (rman_deactivate_resource(r));
299 mainbus_release_resource(device_t bus, device_t child, int type, int rid,
302 if (rman_get_flags(r) & RF_ACTIVE) {
303 int error = bus_deactivate_resource(child, type, rid, r);
307 return (rman_release_resource(r));
311 * Currently this uses the really grody interface from kern/kern_intr.c
312 * (which really doesn't belong in kern/anything.c). Eventually, all of
313 * the code in kern_intr.c and machdep_intr.c should get moved here, since
314 * this is going to be the official interface.
316 * Set up handler for external interrupt events.
317 * Use CR_INT_<n> to select the proper interrupt
318 * condition to dispatch on.
321 mainbus_setup_intr(device_t bus, device_t child, struct resource *irq,
322 int flags, driver_filter_t filter, void (*ihand)(void *), void *arg,
325 panic("can never mainbus_setup_intr");
329 mainbus_teardown_intr(device_t dev, device_t child, struct resource *r,
332 panic("can never mainbus_teardown_intr");