]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/mips/mips/mainbus.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / mips / mips / mainbus.c
1 /*-
2  * Copyright 1998 Massachusetts Institute of Technology
3  *
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
14  * warranty.
15  * 
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
27  * SUCH DAMAGE.
28  *
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
31  */
32
33 /*
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.
42  */
43
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/bus.h>
50 #include <sys/kernel.h>
51 #include <sys/module.h>
52 #include <machine/bus.h>
53 #include <sys/rman.h>
54
55 #include <machine/vmparam.h>
56 #include <vm/vm.h>
57 #include <sys/lock.h>
58 #include <sys/mutex.h>
59 #include <vm/pmap.h>
60 #include <machine/pmap.h>
61 #include <machine/resource.h>
62
63 static struct rman irq_rman, port_rman, mem_rman;
64
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,
69             int unit);
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,
73             struct resource *);
74 static  int mainbus_deactivate_resource(device_t, device_t, int, int,
75             struct resource *);
76 static  int mainbus_release_resource(device_t, device_t, int, int,
77             struct resource *);
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 *,
81             void *);
82
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),
91
92         /* Bus interface */
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),
103
104         { 0, 0 }
105 };
106
107 static driver_t mainbus_driver = {
108         "mainbus",
109         mainbus_methods,
110         1,                      /* no softc */
111 };
112 static devclass_t mainbus_devclass;
113
114 DRIVER_MODULE(mainbus, root, mainbus_driver, mainbus_devclass, 0, 0);
115
116 static int
117 mainbus_probe(device_t dev)
118 {
119
120 #ifdef  DEBUG_BRINGUP
121         device_verbose(dev);    /* print attach message */
122 #else
123         device_quiet(dev);      /* suppress attach message for neatness */
124 #endif
125
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");
133
134         /*
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..)
138          */
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");
145
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");
152
153         return bus_generic_probe(dev);
154 }
155
156 static int
157 mainbus_attach(device_t dev)
158 {
159         /*
160          * First, deal with the children we know about already
161          */
162         bus_generic_attach(dev);
163
164         return 0;
165 }
166
167 static int
168 mainbus_print_child(device_t bus, device_t child)
169 {
170         int retval = 0;
171
172         retval += bus_print_child_header(bus, child);
173         retval += printf(" on motherboard\n");
174
175         return (retval);
176 }
177
178 static device_t
179 mainbus_add_child(device_t bus, int order, const char *name, int unit)
180 {
181         return device_add_child_ordered(bus, order, name, unit);
182 }
183
184 /*
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.)
188  */
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)
192 {
193         struct  resource *rv;
194         struct  rman *rm;
195         int needactivate = flags & RF_ACTIVE;
196
197         flags &= ~RF_ACTIVE;
198
199         switch (type) {
200         case SYS_RES_IRQ:
201                 rm = &irq_rman;
202                 break;
203
204         case SYS_RES_DRQ:
205                 return 0;
206
207         case SYS_RES_IOPORT:
208                 rm = &port_rman;
209                 break;
210
211         case SYS_RES_MEMORY:
212                 rm = &mem_rman;
213                 break;
214
215         default:
216                 return 0;
217         }
218
219         rv = rman_reserve_resource(rm, start, end, count, flags, child);
220
221         if (rv == 0) {
222                 printf("mainbus_alloc_resource: no resource is available\n");
223                 return 0;
224         }
225
226         if (type == SYS_RES_MEMORY) {
227                 rman_set_bustag(rv, MIPS_BUS_SPACE_MEM);
228
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));
233         }
234
235         if (needactivate) {
236                 if (bus_activate_resource(child, type, *rid, rv)) {
237                         rman_release_resource(rv);
238                         return 0;
239                 }
240         }
241
242         return rv;
243 }
244
245 static int
246 mainbus_activate_resource(device_t bus, device_t child, int type, int rid,
247                         struct resource *r)
248 {
249         /*
250          * If this is a memory resource, map it into the kernel.
251          */
252 #ifdef TARGET_OCTEON
253          uint64_t temp;
254 #endif  
255         if (rman_get_bustag(r) == MIPS_BUS_SPACE_MEM) {
256                 caddr_t vaddr = 0;
257                 {
258                         u_int32_t paddr, psize, poffs;
259
260                         paddr = rman_get_start(r);
261                         psize = rman_get_size(r);
262
263                         poffs = paddr - trunc_page(paddr);
264                         vaddr = (caddr_t) pmap_mapdev(paddr-poffs, psize+poffs)
265                             + poffs;
266                 }
267                 rman_set_virtual(r, vaddr);
268                 /* IBM-PC: the type of bus_space_handle_t is u_int */
269 #ifdef TARGET_OCTEON
270                 temp = 0x0000000000000000;
271                 temp |= (uint32_t)vaddr;
272                 rman_set_bushandle(r, (bus_space_handle_t) temp);
273 #else           
274                 rman_set_bushandle(r, (bus_space_handle_t) vaddr);
275 #endif          
276         }
277         return (rman_activate_resource(r));
278 }
279
280 static int
281 mainbus_deactivate_resource(device_t bus, device_t child, int type, int rid,
282     struct resource *r)
283 {
284         /*
285          * If this is a memory resource, unmap it.
286          */
287         if ((rman_get_bustag(r) == MIPS_BUS_SPACE_MEM) && (rman_get_end(r) >=
288             1024 * 1024)) {
289                 u_int32_t psize;
290
291                 psize = rman_get_size(r);
292                 pmap_unmapdev((vm_offset_t)rman_get_virtual(r), psize);
293         }
294
295         return (rman_deactivate_resource(r));
296 }
297
298 static int
299 mainbus_release_resource(device_t bus, device_t child, int type, int rid,
300                        struct resource *r)
301 {
302         if (rman_get_flags(r) & RF_ACTIVE) {
303                 int error = bus_deactivate_resource(child, type, rid, r);
304                 if (error)
305                         return error;
306         }
307         return (rman_release_resource(r));
308 }
309
310 /*
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.
315  *
316  *  Set up handler for external interrupt events.
317  *  Use CR_INT_<n> to select the proper interrupt
318  *  condition to dispatch on.
319  */
320 static int
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,
323     void **cookiep)
324 {
325         panic("can never mainbus_setup_intr");
326 }
327
328 static int
329 mainbus_teardown_intr(device_t dev, device_t child, struct resource *r,
330     void *ih)
331 {
332         panic("can never mainbus_teardown_intr");
333 }