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