]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - sys/ia64/ia64/nexus.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.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
54 #include <vm/vm.h>
55 #include <vm/pmap.h>
56
57 #include <machine/efi.h>
58 #include <machine/intr.h>
59 #include <machine/nexusvar.h>
60 #include <machine/pmap.h>
61 #include <machine/resource.h>
62 #include <machine/sapicvar.h>
63 #include <machine/vmparam.h>
64
65 #include <contrib/dev/acpica/include/acpi.h>
66
67 #include <dev/acpica/acpivar.h>
68
69 #include <isa/isareg.h>
70 #include <sys/rtprio.h>
71
72 #include "clock_if.h"
73
74 static MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device");
75 struct nexus_device {
76         struct resource_list    nx_resources;
77         int                     nx_pcibus;
78 };
79
80 #define DEVTONX(dev)    ((struct nexus_device *)device_get_ivars(dev))
81
82 static struct rman irq_rman, drq_rman, port_rman, mem_rman;
83
84 static  int nexus_probe(device_t);
85 static  int nexus_attach(device_t);
86 static  int nexus_print_child(device_t, device_t);
87 static device_t nexus_add_child(device_t bus, int order, const char *name,
88                                 int unit);
89 static  struct resource *nexus_alloc_resource(device_t, device_t, int, int *,
90                                               u_long, u_long, u_long, u_int);
91 static  int nexus_read_ivar(device_t, device_t, int, uintptr_t *);
92 static  int nexus_write_ivar(device_t, device_t, int, uintptr_t);
93 static  int nexus_activate_resource(device_t, device_t, int, int,
94                                     struct resource *);
95 static  int nexus_deactivate_resource(device_t, device_t, int, int,
96                                       struct resource *);
97 static  int nexus_release_resource(device_t, device_t, int, int,
98                                    struct resource *);
99 static  int nexus_setup_intr(device_t, device_t, struct resource *, int flags,
100                              driver_filter_t filter, void (*)(void *), void *, 
101                              void **);
102 static  int nexus_teardown_intr(device_t, device_t, struct resource *,
103                                 void *);
104 static struct resource_list *nexus_get_reslist(device_t dev, device_t child);
105 static  int nexus_set_resource(device_t, device_t, int, int, u_long, u_long);
106 static  int nexus_get_resource(device_t, device_t, int, int, u_long *,
107                                u_long *);
108 static void nexus_delete_resource(device_t, device_t, int, int);
109 static  int nexus_config_intr(device_t, int, enum intr_trigger,
110                               enum intr_polarity);
111
112 static int nexus_gettime(device_t, struct timespec *);
113 static int nexus_settime(device_t, struct timespec *);
114
115 static device_method_t nexus_methods[] = {
116         /* Device interface */
117         DEVMETHOD(device_probe,         nexus_probe),
118         DEVMETHOD(device_attach,        nexus_attach),
119         DEVMETHOD(device_detach,        bus_generic_detach),
120         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
121         DEVMETHOD(device_suspend,       bus_generic_suspend),
122         DEVMETHOD(device_resume,        bus_generic_resume),
123
124         /* Bus interface */
125         DEVMETHOD(bus_print_child,      nexus_print_child),
126         DEVMETHOD(bus_add_child,        nexus_add_child),
127         DEVMETHOD(bus_read_ivar,        nexus_read_ivar),
128         DEVMETHOD(bus_write_ivar,       nexus_write_ivar),
129         DEVMETHOD(bus_alloc_resource,   nexus_alloc_resource),
130         DEVMETHOD(bus_release_resource, nexus_release_resource),
131         DEVMETHOD(bus_activate_resource, nexus_activate_resource),
132         DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource),
133         DEVMETHOD(bus_setup_intr,       nexus_setup_intr),
134         DEVMETHOD(bus_teardown_intr,    nexus_teardown_intr),
135         DEVMETHOD(bus_get_resource_list, nexus_get_reslist),
136         DEVMETHOD(bus_set_resource,     nexus_set_resource),
137         DEVMETHOD(bus_get_resource,     nexus_get_resource),
138         DEVMETHOD(bus_delete_resource,  nexus_delete_resource),
139         DEVMETHOD(bus_config_intr,      nexus_config_intr),
140
141         /* Clock interface */
142         DEVMETHOD(clock_gettime,        nexus_gettime),
143         DEVMETHOD(clock_settime,        nexus_settime),
144
145         { 0, 0 }
146 };
147
148 static driver_t nexus_driver = {
149         "nexus",
150         nexus_methods,
151         1,                      /* no softc */
152 };
153 static devclass_t nexus_devclass;
154
155 DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0);
156
157 static int
158 nexus_probe(device_t dev)
159 {
160
161         device_quiet(dev);      /* suppress attach message for neatness */
162
163         /* 
164          * XXX working notes:
165          *
166          * - IRQ resource creation should be moved to the PIC/APIC driver.
167          * - DRQ resource creation should be moved to the DMAC driver.
168          * - The above should be sorted to probe earlier than any child busses.
169          *
170          * - Leave I/O and memory creation here, as child probes may need them.
171          *   (especially eg. ACPI)
172          */
173
174         /*
175          * IRQ's are on the mainboard on old systems, but on the ISA part
176          * of PCI->ISA bridges.  There would be multiple sets of IRQs on
177          * multi-ISA-bus systems.  PCI interrupts are routed to the ISA
178          * component, so in a way, PCI can be a partial child of an ISA bus(!).
179          * APIC interrupts are global though.
180          *
181          * XXX We depend on the AT PIC driver correctly claiming IRQ 2
182          *     to prevent its reuse elsewhere in the !APIC_IO case.
183          */
184         irq_rman.rm_start = 0;
185         irq_rman.rm_type = RMAN_ARRAY;
186         irq_rman.rm_descr = "Interrupt request lines";
187         irq_rman.rm_end = 255;
188         if (rman_init(&irq_rman)
189             || rman_manage_region(&irq_rman,
190                                   irq_rman.rm_start, irq_rman.rm_end))
191                 panic("nexus_probe irq_rman");
192
193         /*
194          * ISA DMA on PCI systems is implemented in the ISA part of each
195          * PCI->ISA bridge and the channels can be duplicated if there are
196          * multiple bridges.  (eg: laptops with docking stations)
197          */
198         drq_rman.rm_start = 0;
199         drq_rman.rm_end = 7;
200         drq_rman.rm_type = RMAN_ARRAY;
201         drq_rman.rm_descr = "DMA request lines";
202         /* XXX drq 0 not available on some machines */
203         if (rman_init(&drq_rman)
204             || rman_manage_region(&drq_rman,
205                                   drq_rman.rm_start, drq_rman.rm_end))
206                 panic("nexus_probe drq_rman");
207
208         /*
209          * However, IO ports and Memory truely are global at this level,
210          * as are APIC interrupts (however many IO APICS there turn out
211          * to be on large systems..)
212          */
213         port_rman.rm_start = 0;
214         port_rman.rm_end = 0xffff;
215         port_rman.rm_type = RMAN_ARRAY;
216         port_rman.rm_descr = "I/O ports";
217         if (rman_init(&port_rman)
218             || rman_manage_region(&port_rman, 0, 0xffff))
219                 panic("nexus_probe port_rman");
220
221         mem_rman.rm_start = 0;
222         mem_rman.rm_end = ~0u;
223         mem_rman.rm_type = RMAN_ARRAY;
224         mem_rman.rm_descr = "I/O memory addresses";
225         if (rman_init(&mem_rman)
226             || rman_manage_region(&mem_rman, 0, ~0))
227                 panic("nexus_probe mem_rman");
228
229         return bus_generic_probe(dev);
230 }
231
232 static int
233 nexus_attach(device_t dev)
234 {
235
236         /*
237          * Mask the legacy PICs - we will use the I/O SAPIC for interrupt.
238          */
239         outb(IO_ICU1+1, 0xff);
240         outb(IO_ICU2+1, 0xff);
241
242         if (acpi_identify() == 0)
243                 BUS_ADD_CHILD(dev, 10, "acpi", 0);
244         clock_register(dev, 1000);
245         bus_generic_attach(dev);
246         return 0;
247 }
248
249 static int
250 nexus_print_child(device_t bus, device_t child)
251 {
252         struct nexus_device *ndev = DEVTONX(child);
253         struct resource_list *rl = &ndev->nx_resources;
254         int retval = 0;
255
256         retval += bus_print_child_header(bus, child);
257         retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
258         retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
259         retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
260         if (ndev->nx_pcibus != -1)
261                 retval += printf(" pcibus %d", ndev->nx_pcibus);
262         if (device_get_flags(child))
263                 retval += printf(" flags %#x", device_get_flags(child));
264         retval += printf(" on motherboard\n");  /* XXX "motherboard", ick */
265
266         return (retval);
267 }
268
269 static device_t
270 nexus_add_child(device_t bus, int order, const char *name, int unit)
271 {
272         device_t                child;
273         struct nexus_device     *ndev;
274
275         ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO);
276         if (!ndev)
277                 return(0);
278         resource_list_init(&ndev->nx_resources);
279         ndev->nx_pcibus = -1;
280
281         child = device_add_child_ordered(bus, order, name, unit); 
282
283         /* should we free this in nexus_child_detached? */
284         device_set_ivars(child, ndev);
285
286         return(child);
287 }
288
289 static int
290 nexus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
291 {
292         struct  nexus_device *ndev = DEVTONX(child);
293
294         switch (which) {
295         case NEXUS_IVAR_PCIBUS:
296                 *result = ndev->nx_pcibus;
297                 break;
298         default:
299                 return ENOENT;
300         }
301         return 0;
302 }
303         
304
305 static int
306 nexus_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
307 {
308         struct  nexus_device *ndev = DEVTONX(child);
309
310         switch (which) {
311         case NEXUS_IVAR_PCIBUS:
312                 ndev->nx_pcibus = value;
313                 break;
314         default:
315                 return ENOENT;
316         }
317         return 0;
318 }
319
320
321 /*
322  * Allocate a resource on behalf of child.  NB: child is usually going to be a
323  * child of one of our descendants, not a direct child of nexus0.
324  * (Exceptions include npx.)
325  */
326 static struct resource *
327 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
328                      u_long start, u_long end, u_long count, u_int flags)
329 {
330         struct nexus_device *ndev = DEVTONX(child);
331         struct  resource *rv;
332         struct resource_list_entry *rle;
333         struct  rman *rm;
334         int needactivate = flags & RF_ACTIVE;
335
336         /*
337          * If this is an allocation of the "default" range for a given RID, and
338          * we know what the resources for this device are (ie. they aren't maintained
339          * by a child bus), then work out the start/end values.
340          */
341         if ((start == 0UL) && (end == ~0UL) && (count == 1)) {
342                 if (ndev == NULL)
343                         return(NULL);
344                 rle = resource_list_find(&ndev->nx_resources, type, *rid);
345                 if (rle == NULL)
346                         return(NULL);
347                 start = rle->start;
348                 end = rle->end;
349                 count = rle->count;
350         }
351
352         flags &= ~RF_ACTIVE;
353
354         switch (type) {
355         case SYS_RES_IRQ:
356                 rm = &irq_rman;
357                 break;
358
359         case SYS_RES_DRQ:
360                 rm = &drq_rman;
361                 break;
362
363         case SYS_RES_IOPORT:
364                 rm = &port_rman;
365                 break;
366
367         case SYS_RES_MEMORY:
368                 rm = &mem_rman;
369                 break;
370
371         default:
372                 return 0;
373         }
374
375         rv = rman_reserve_resource(rm, start, end, count, flags, child);
376         if (rv == 0)
377                 return 0;
378         rman_set_rid(rv, *rid);
379
380         if (needactivate) {
381                 if (bus_activate_resource(child, type, *rid, rv)) {
382                         rman_release_resource(rv);
383                         return 0;
384                 }
385         }
386         
387         return rv;
388 }
389
390 static int
391 nexus_activate_resource(device_t bus, device_t child, int type, int rid,
392                         struct resource *r)
393 {
394         vm_paddr_t paddr, psize;
395         void *vaddr;
396
397         /*
398          * If this is a memory resource, map it into the kernel.
399          */
400         switch (type) {
401         case SYS_RES_IOPORT:
402                 rman_set_bustag(r, IA64_BUS_SPACE_IO);
403                 rman_set_bushandle(r, rman_get_start(r));
404                 break;
405         case SYS_RES_MEMORY:
406                 paddr = rman_get_start(r);
407                 psize = rman_get_size(r);
408                 vaddr = pmap_mapdev(paddr, psize);
409                 rman_set_virtual(r, vaddr);
410                 rman_set_bustag(r, IA64_BUS_SPACE_MEM);
411                 rman_set_bushandle(r, (bus_space_handle_t) paddr);
412                 break;
413         }
414         return (rman_activate_resource(r));
415 }
416
417 static int
418 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
419                           struct resource *r)
420 {
421                 
422         return (rman_deactivate_resource(r));
423 }
424
425 static int
426 nexus_release_resource(device_t bus, device_t child, int type, int rid,
427                        struct resource *r)
428 {
429         if (rman_get_flags(r) & RF_ACTIVE) {
430                 int error = bus_deactivate_resource(child, type, rid, r);
431                 if (error)
432                         return error;
433         }
434         return (rman_release_resource(r));
435 }
436
437 /*
438  * Currently this uses the really grody interface from kern/kern_intr.c
439  * (which really doesn't belong in kern/anything.c).  Eventually, all of
440  * the code in kern_intr.c and machdep_intr.c should get moved here, since
441  * this is going to be the official interface.
442  */
443 static int
444 nexus_setup_intr(device_t bus, device_t child, struct resource *irq,
445                  int flags, driver_filter_t filter, void (*ihand)(void *), 
446                  void *arg, void **cookiep)
447 {
448         driver_t        *driver;
449         int             error;
450
451         /* somebody tried to setup an irq that failed to allocate! */
452         if (irq == NULL)
453                 panic("nexus_setup_intr: NULL irq resource!");
454
455         *cookiep = 0;
456         if ((rman_get_flags(irq) & RF_SHAREABLE) == 0)
457                 flags |= INTR_EXCL;
458
459         driver = device_get_driver(child);
460
461         /*
462          * We depend here on rman_activate_resource() being idempotent.
463          */
464         error = rman_activate_resource(irq);
465         if (error)
466                 return (error);
467
468         error = ia64_setup_intr(device_get_nameunit(child),
469             rman_get_start(irq), filter, ihand, arg, flags, cookiep);
470
471         return (error);
472 }
473
474 static int
475 nexus_teardown_intr(device_t dev, device_t child, struct resource *ires,
476     void *cookie)
477 {
478
479         return (ia64_teardown_intr(cookie));
480 }
481
482 static struct resource_list *
483 nexus_get_reslist(device_t dev, device_t child)
484 {
485         struct nexus_device *ndev = DEVTONX(child);
486
487         return (&ndev->nx_resources);
488 }
489
490 static int
491 nexus_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count)
492 {
493         struct nexus_device     *ndev = DEVTONX(child);
494         struct resource_list    *rl = &ndev->nx_resources;
495
496         /* XXX this should return a success/failure indicator */
497         resource_list_add(rl, type, rid, start, start + count - 1, count);
498         return(0);
499 }
500
501 static int
502 nexus_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
503 {
504         struct nexus_device     *ndev = DEVTONX(child);
505         struct resource_list    *rl = &ndev->nx_resources;
506         struct resource_list_entry *rle;
507
508         rle = resource_list_find(rl, type, rid);
509         device_printf(child, "type %d  rid %d  startp %p  countp %p - got %p\n",
510                       type, rid, startp, countp, rle);
511         if (!rle)
512                 return(ENOENT);
513         if (startp)
514                 *startp = rle->start;
515         if (countp)
516                 *countp = rle->count;
517         return(0);
518 }
519
520 static void
521 nexus_delete_resource(device_t dev, device_t child, int type, int rid)
522 {
523         struct nexus_device     *ndev = DEVTONX(child);
524         struct resource_list    *rl = &ndev->nx_resources;
525
526         resource_list_delete(rl, type, rid);
527 }
528
529 static int
530 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
531     enum intr_polarity pol)
532 {
533
534         return (sapic_config_intr(irq, trig, pol));
535 }
536
537 static int
538 nexus_gettime(device_t dev, struct timespec *ts)
539 {
540         struct clocktime ct;
541         struct efi_tm tm;
542
543         efi_get_time(&tm);
544
545         /*
546          * This code was written in 2005, so logically EFI cannot return
547          * a year smaller than that. Assume the EFI clock is out of whack
548          * in that case and reset the EFI clock.
549          */
550         if (tm.tm_year < 2005)
551                 return (EINVAL);
552
553         ct.nsec = tm.tm_nsec;
554         ct.sec = tm.tm_sec;
555         ct.min = tm.tm_min;
556         ct.hour = tm.tm_hour;
557         ct.day = tm.tm_mday;
558         ct.mon = tm.tm_mon;
559         ct.year = tm.tm_year;
560         ct.dow = -1;
561         return (clock_ct_to_ts(&ct, ts));
562 }
563
564 static int
565 nexus_settime(device_t dev, struct timespec *ts)
566 {
567         struct clocktime ct;
568         struct efi_tm tm;
569
570         efi_get_time(&tm);
571
572         clock_ts_to_ct(ts, &ct);
573         tm.tm_nsec = ts->tv_nsec;
574         tm.tm_sec = ct.sec;
575         tm.tm_min = ct.min;
576         tm.tm_hour = ct.hour;
577         tm.tm_year = ct.year;
578         tm.tm_mon = ct.mon;
579         tm.tm_mday = ct.day;
580         return (efi_set_time(&tm));
581 }
582