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