]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/ia64/ia64/nexus.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / ia64 / ia64 / nexus.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  * $FreeBSD$
30  */
31
32 /*
33  * This code implements a `root nexus' for Intel Architecture
34  * machines.  The function of the root nexus is to serve as an
35  * attachment point for both processors and buses, and to manage
36  * resources which are common to all of them.  In particular,
37  * this code implements the core resource managers for interrupt
38  * requests, DMA requests (which rightfully should be a part of the
39  * ISA code but it's easier to do it here for now), I/O port addresses,
40  * and I/O memory address space.
41  */
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/bus.h>
46 #include <sys/clock.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/module.h>
50 #include <machine/bus.h>
51 #include <sys/rman.h>
52 #include <sys/interrupt.h>
53 #include <sys/pcpu.h>
54
55 #include <vm/vm.h>
56 #include <vm/pmap.h>
57
58 #include <machine/efi.h>
59 #include <machine/intr.h>
60 #include <machine/pmap.h>
61 #include <machine/resource.h>
62 #include <machine/vmparam.h>
63
64 #include <contrib/dev/acpica/include/acpi.h>
65
66 #include <dev/acpica/acpivar.h>
67
68 #include <isa/isareg.h>
69 #include <sys/rtprio.h>
70
71 #include "clock_if.h"
72
73 static MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device");
74 struct nexus_device {
75         struct resource_list    nx_resources;
76 };
77
78 #define DEVTONX(dev)    ((struct nexus_device *)device_get_ivars(dev))
79
80 static struct rman irq_rman, port_rman, mem_rman;
81
82 static  int nexus_probe(device_t);
83 static  int nexus_attach(device_t);
84 static  int nexus_print_child(device_t, device_t);
85 static device_t nexus_add_child(device_t bus, int order, const char *name,
86                                 int unit);
87 static  struct resource *nexus_alloc_resource(device_t, device_t, int, int *,
88                                               u_long, u_long, u_long, u_int);
89 static  int nexus_activate_resource(device_t, device_t, int, int,
90                                     struct resource *);
91 static  int nexus_deactivate_resource(device_t, device_t, int, int,
92                                       struct resource *);
93 static  int nexus_release_resource(device_t, device_t, int, int,
94                                    struct resource *);
95 static  int nexus_setup_intr(device_t, device_t, struct resource *, int flags,
96                              driver_filter_t filter, void (*)(void *), void *, 
97                              void **);
98 static  int nexus_teardown_intr(device_t, device_t, struct resource *,
99                                 void *);
100 static struct resource_list *nexus_get_reslist(device_t dev, device_t child);
101 static  int nexus_set_resource(device_t, device_t, int, int, u_long, u_long);
102 static  int nexus_get_resource(device_t, device_t, int, int, u_long *,
103                                u_long *);
104 static void nexus_delete_resource(device_t, device_t, int, int);
105 static int nexus_bind_intr(device_t, device_t, struct resource *, int);
106 static  int nexus_config_intr(device_t, int, enum intr_trigger,
107                               enum intr_polarity);
108
109 static int nexus_gettime(device_t, struct timespec *);
110 static int nexus_settime(device_t, struct timespec *);
111
112 static device_method_t nexus_methods[] = {
113         /* Device interface */
114         DEVMETHOD(device_probe,         nexus_probe),
115         DEVMETHOD(device_attach,        nexus_attach),
116         DEVMETHOD(device_detach,        bus_generic_detach),
117         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
118         DEVMETHOD(device_suspend,       bus_generic_suspend),
119         DEVMETHOD(device_resume,        bus_generic_resume),
120
121         /* Bus interface */
122         DEVMETHOD(bus_print_child,      nexus_print_child),
123         DEVMETHOD(bus_add_child,        nexus_add_child),
124         DEVMETHOD(bus_alloc_resource,   nexus_alloc_resource),
125         DEVMETHOD(bus_release_resource, nexus_release_resource),
126         DEVMETHOD(bus_activate_resource, nexus_activate_resource),
127         DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource),
128         DEVMETHOD(bus_setup_intr,       nexus_setup_intr),
129         DEVMETHOD(bus_teardown_intr,    nexus_teardown_intr),
130         DEVMETHOD(bus_get_resource_list, nexus_get_reslist),
131         DEVMETHOD(bus_set_resource,     nexus_set_resource),
132         DEVMETHOD(bus_get_resource,     nexus_get_resource),
133         DEVMETHOD(bus_delete_resource,  nexus_delete_resource),
134         DEVMETHOD(bus_bind_intr,        nexus_bind_intr),
135         DEVMETHOD(bus_config_intr,      nexus_config_intr),
136
137         /* Clock interface */
138         DEVMETHOD(clock_gettime,        nexus_gettime),
139         DEVMETHOD(clock_settime,        nexus_settime),
140
141         { 0, 0 }
142 };
143
144 static driver_t nexus_driver = {
145         "nexus",
146         nexus_methods,
147         1,                      /* no softc */
148 };
149 static devclass_t nexus_devclass;
150
151 DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0);
152
153 static int
154 nexus_probe(device_t dev)
155 {
156
157         device_quiet(dev);      /* suppress attach message for neatness */
158
159         irq_rman.rm_type = RMAN_ARRAY;
160         irq_rman.rm_descr = "Interrupt request lines";
161         irq_rman.rm_start = 0;
162         irq_rman.rm_end = IA64_NXIVS - 1;
163         if (rman_init(&irq_rman)
164             || rman_manage_region(&irq_rman,
165                                   irq_rman.rm_start, irq_rman.rm_end))
166                 panic("nexus_probe irq_rman");
167
168         port_rman.rm_start = 0;
169         port_rman.rm_end = 0xffff;
170         port_rman.rm_type = RMAN_ARRAY;
171         port_rman.rm_descr = "I/O ports";
172         if (rman_init(&port_rman)
173             || rman_manage_region(&port_rman, 0, 0xffff))
174                 panic("nexus_probe port_rman");
175
176         mem_rman.rm_start = 0;
177         mem_rman.rm_end = ~0u;
178         mem_rman.rm_type = RMAN_ARRAY;
179         mem_rman.rm_descr = "I/O memory addresses";
180         if (rman_init(&mem_rman)
181             || rman_manage_region(&mem_rman, 0, ~0))
182                 panic("nexus_probe mem_rman");
183
184         return bus_generic_probe(dev);
185 }
186
187 static int
188 nexus_attach(device_t dev)
189 {
190
191         /*
192          * Mask the legacy PICs - we will use the I/O SAPIC for interrupt.
193          */
194         outb(IO_ICU1+1, 0xff);
195         outb(IO_ICU2+1, 0xff);
196
197         if (acpi_identify() == 0)
198                 BUS_ADD_CHILD(dev, 10, "acpi", 0);
199         clock_register(dev, 1000);
200         bus_generic_attach(dev);
201         return 0;
202 }
203
204 static int
205 nexus_print_child(device_t bus, device_t child)
206 {
207         struct nexus_device *ndev = DEVTONX(child);
208         struct resource_list *rl = &ndev->nx_resources;
209         int retval = 0;
210
211         retval += bus_print_child_header(bus, child);
212         retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
213         retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
214         retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
215         if (device_get_flags(child))
216                 retval += printf(" flags %#x", device_get_flags(child));
217         retval += printf(" on motherboard\n");  /* XXX "motherboard", ick */
218
219         return (retval);
220 }
221
222 static device_t
223 nexus_add_child(device_t bus, int order, const char *name, int unit)
224 {
225         device_t                child;
226         struct nexus_device     *ndev;
227
228         ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO);
229         if (!ndev)
230                 return(0);
231         resource_list_init(&ndev->nx_resources);
232
233         child = device_add_child_ordered(bus, order, name, unit); 
234
235         /* should we free this in nexus_child_detached? */
236         device_set_ivars(child, ndev);
237
238         return(child);
239 }
240
241
242 /*
243  * Allocate a resource on behalf of child.  NB: child is usually going to be a
244  * child of one of our descendants, not a direct child of nexus0.
245  * (Exceptions include npx.)
246  */
247 static struct resource *
248 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
249                      u_long start, u_long end, u_long count, u_int flags)
250 {
251         struct nexus_device *ndev = DEVTONX(child);
252         struct  resource *rv;
253         struct resource_list_entry *rle;
254         struct  rman *rm;
255         int needactivate = flags & RF_ACTIVE;
256
257         /*
258          * If this is an allocation of the "default" range for a given RID, and
259          * we know what the resources for this device are (ie. they aren't maintained
260          * by a child bus), then work out the start/end values.
261          */
262         if ((start == 0UL) && (end == ~0UL) && (count == 1)) {
263                 if (ndev == NULL)
264                         return(NULL);
265                 rle = resource_list_find(&ndev->nx_resources, type, *rid);
266                 if (rle == NULL)
267                         return(NULL);
268                 start = rle->start;
269                 end = rle->end;
270                 count = rle->count;
271         }
272
273         flags &= ~RF_ACTIVE;
274
275         switch (type) {
276         case SYS_RES_IRQ:
277                 rm = &irq_rman;
278                 break;
279
280         case SYS_RES_IOPORT:
281                 rm = &port_rman;
282                 break;
283
284         case SYS_RES_MEMORY:
285                 rm = &mem_rman;
286                 break;
287
288         default:
289                 return 0;
290         }
291
292         rv = rman_reserve_resource(rm, start, end, count, flags, child);
293         if (rv == 0)
294                 return 0;
295         rman_set_rid(rv, *rid);
296
297         if (needactivate) {
298                 if (bus_activate_resource(child, type, *rid, rv)) {
299                         rman_release_resource(rv);
300                         return 0;
301                 }
302         }
303         
304         return rv;
305 }
306
307 static int
308 nexus_activate_resource(device_t bus, device_t child, int type, int rid,
309     struct resource *r)
310 {
311         vm_paddr_t paddr;
312         void *vaddr;
313
314         paddr = rman_get_start(r);
315
316         switch (type) {
317         case SYS_RES_IOPORT:
318                 rman_set_bustag(r, IA64_BUS_SPACE_IO);
319                 rman_set_bushandle(r, paddr);
320                 break;
321         case SYS_RES_MEMORY:
322                 vaddr = pmap_mapdev(paddr, rman_get_size(r));
323                 rman_set_bustag(r, IA64_BUS_SPACE_MEM);
324                 rman_set_bushandle(r, (bus_space_handle_t) vaddr);
325                 rman_set_virtual(r, vaddr);
326                 break;
327         }
328         return (rman_activate_resource(r));
329 }
330
331 static int
332 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
333                           struct resource *r)
334 {
335                 
336         return (rman_deactivate_resource(r));
337 }
338
339 static int
340 nexus_release_resource(device_t bus, device_t child, int type, int rid,
341                        struct resource *r)
342 {
343         if (rman_get_flags(r) & RF_ACTIVE) {
344                 int error = bus_deactivate_resource(child, type, rid, r);
345                 if (error)
346                         return error;
347         }
348         return (rman_release_resource(r));
349 }
350
351 /*
352  * Currently this uses the really grody interface from kern/kern_intr.c
353  * (which really doesn't belong in kern/anything.c).  Eventually, all of
354  * the code in kern_intr.c and machdep_intr.c should get moved here, since
355  * this is going to be the official interface.
356  */
357 static int
358 nexus_setup_intr(device_t bus, device_t child, struct resource *irq,
359                  int flags, driver_filter_t filter, void (*ihand)(void *), 
360                  void *arg, void **cookiep)
361 {
362         driver_t        *driver;
363         int             error;
364
365         /* somebody tried to setup an irq that failed to allocate! */
366         if (irq == NULL)
367                 panic("nexus_setup_intr: NULL irq resource!");
368
369         *cookiep = 0;
370         if ((rman_get_flags(irq) & RF_SHAREABLE) == 0)
371                 flags |= INTR_EXCL;
372
373         driver = device_get_driver(child);
374
375         /*
376          * We depend here on rman_activate_resource() being idempotent.
377          */
378         error = rman_activate_resource(irq);
379         if (error)
380                 return (error);
381
382         error = ia64_setup_intr(device_get_nameunit(child),
383             rman_get_start(irq), filter, ihand, arg, flags, cookiep);
384
385         return (error);
386 }
387
388 static int
389 nexus_teardown_intr(device_t dev, device_t child, struct resource *ires,
390     void *cookie)
391 {
392
393         return (ia64_teardown_intr(cookie));
394 }
395
396 static struct resource_list *
397 nexus_get_reslist(device_t dev, device_t child)
398 {
399         struct nexus_device *ndev = DEVTONX(child);
400
401         return (&ndev->nx_resources);
402 }
403
404 static int
405 nexus_set_resource(device_t dev, device_t child, int type, int rid,
406     u_long start, u_long count)
407 {
408         struct nexus_device     *ndev = DEVTONX(child);
409         struct resource_list    *rl = &ndev->nx_resources;
410
411         if (type == SYS_RES_IOPORT && start > (0x10000 - count)) {
412                 /*
413                  * Work around a firmware bug in the HP rx2660, where in ACPI
414                  * an I/O port is really a memory mapped I/O address. The bug
415                  * is in the GAS that describes the address and in particular
416                  * the SpaceId field. The field should not say the address is
417                  * an I/O port when it is in fact an I/O memory address.
418                  */
419                 if (bootverbose)
420                         printf("%s: invalid port range (%#lx-%#lx); "
421                             "assuming I/O memory range.\n", __func__, start,
422                             start + count - 1);
423                 type = SYS_RES_MEMORY;
424         }
425
426         /* XXX this should return a success/failure indicator */
427         resource_list_add(rl, type, rid, start, start + count - 1, count);
428         return(0);
429 }
430
431 static int
432 nexus_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
433 {
434         struct nexus_device     *ndev = DEVTONX(child);
435         struct resource_list    *rl = &ndev->nx_resources;
436         struct resource_list_entry *rle;
437
438         rle = resource_list_find(rl, type, rid);
439         device_printf(child, "type %d  rid %d  startp %p  countp %p - got %p\n",
440                       type, rid, startp, countp, rle);
441         if (!rle)
442                 return(ENOENT);
443         if (startp)
444                 *startp = rle->start;
445         if (countp)
446                 *countp = rle->count;
447         return(0);
448 }
449
450 static void
451 nexus_delete_resource(device_t dev, device_t child, int type, int rid)
452 {
453         struct nexus_device     *ndev = DEVTONX(child);
454         struct resource_list    *rl = &ndev->nx_resources;
455
456         resource_list_delete(rl, type, rid);
457 }
458
459 static int
460 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
461     enum intr_polarity pol)
462 {
463
464         return (sapic_config_intr(irq, trig, pol));
465 }
466
467 static int
468 nexus_bind_intr(device_t dev, device_t child, struct resource *irq, int cpu)
469 {
470         struct pcpu *pc;
471
472         pc = cpuid_to_pcpu[cpu];
473         if (pc == NULL)
474                 return (EINVAL);
475         return (sapic_bind_intr(rman_get_start(irq), pc));
476 }
477
478 static int
479 nexus_gettime(device_t dev, struct timespec *ts)
480 {
481         struct clocktime ct;
482         struct efi_tm tm;
483
484         efi_get_time(&tm);
485
486         /*
487          * This code was written in 2005, so logically EFI cannot return
488          * a year smaller than that. Assume the EFI clock is out of whack
489          * in that case and reset the EFI clock.
490          */
491         if (tm.tm_year < 2005)
492                 return (EINVAL);
493
494         ct.nsec = tm.tm_nsec;
495         ct.sec = tm.tm_sec;
496         ct.min = tm.tm_min;
497         ct.hour = tm.tm_hour;
498         ct.day = tm.tm_mday;
499         ct.mon = tm.tm_mon;
500         ct.year = tm.tm_year;
501         ct.dow = -1;
502         return (clock_ct_to_ts(&ct, ts));
503 }
504
505 static int
506 nexus_settime(device_t dev, struct timespec *ts)
507 {
508         struct clocktime ct;
509         struct efi_tm tm;
510
511         efi_get_time(&tm);
512
513         clock_ts_to_ct(ts, &ct);
514         tm.tm_nsec = ts->tv_nsec;
515         tm.tm_sec = ct.sec;
516         tm.tm_min = ct.min;
517         tm.tm_hour = ct.hour;
518         tm.tm_year = ct.year;
519         tm.tm_mon = ct.mon;
520         tm.tm_mday = ct.day;
521         return (efi_set_time(&tm));
522 }