Merge branch 'vendor/LIBARCHIVE'
[dragonfly.git] / sys / platform / pc32 / i386 / 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: src/sys/i386/i386/nexus.c,v 1.26.2.10 2003/02/22 13:16:45 imp Exp $
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 <sys/rman.h>
50 #include <sys/interrupt.h>
51 #include <sys/machintr.h>
52
53 #include <machine/vmparam.h>
54 #include <vm/vm.h>
55 #include <vm/pmap.h>
56 #include <machine/pmap.h>
57
58 #include <machine/nexusvar.h>
59 #include <machine/smp.h>
60 #include <machine/intr_machdep.h>
61 #include <machine_base/apic/ioapic.h>
62 #include <machine_base/apic/lapic.h>
63
64 #include <bus/pci/pcivar.h>
65 #include <bus/pci/pcireg.h>
66 #include <bus/pci/pcibus.h>
67 #include <bus/pci/pci_cfgreg.h>
68 #include <bus/pci/pcib_private.h>
69
70 #include "pcib_if.h"
71
72 static MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device");
73 struct nexus_device {
74         struct resource_list    nx_resources;
75         int                     nx_pcibus;
76 };
77
78 #define DEVTONX(dev)    ((struct nexus_device *)device_get_ivars(dev))
79
80 static struct rman irq_rman[MAXCPU], drq_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_all_resources(device_t dev);
85 static  int nexus_print_child(device_t, device_t);
86 static device_t nexus_add_child(device_t bus, device_t parent, int order,
87                                 const char *name, int unit);
88 static  struct resource *nexus_alloc_resource(device_t, device_t, int, int *,
89     u_long, u_long, u_long, u_int, int);
90 static  int nexus_read_ivar(device_t, device_t, int, uintptr_t *);
91 static  int nexus_write_ivar(device_t, device_t, int, uintptr_t);
92 static  int nexus_activate_resource(device_t, device_t, int, int,
93                                     struct resource *);
94 static  int nexus_deactivate_resource(device_t, device_t, int, int,
95                                       struct resource *);
96 static  int nexus_release_resource(device_t, device_t, int, int,
97                                    struct resource *);
98 static  int nexus_config_intr(device_t, device_t, int, enum intr_trigger,
99                               enum intr_polarity);
100 static  int nexus_setup_intr(device_t, device_t, struct resource *, int flags,
101                              void (*)(void *), void *, 
102                              void **, lwkt_serialize_t);
103 static  int nexus_teardown_intr(device_t, device_t, struct resource *,
104                                 void *);
105 static  int nexus_set_resource(device_t, device_t, int, int, u_long, u_long,
106                                int);
107 static  int nexus_get_resource(device_t, device_t, int, int, u_long *, u_long *);
108 static void nexus_delete_resource(device_t, device_t, int, int);
109
110 static  int nexus_alloc_msi(device_t, device_t, int, int, int *, int);
111 static  int nexus_release_msi(device_t, device_t, int, int *, int);
112 static  int nexus_map_msi(device_t, device_t, int, uint64_t *, uint32_t *, int);
113
114 /*
115  * The device_identify method will cause nexus to automatically associate
116  * and attach to the root bus.
117  */
118 static device_method_t nexus_methods[] = {
119         /* Device interface */
120         DEVMETHOD(device_identify,      bus_generic_identify),
121         DEVMETHOD(device_probe,         nexus_probe),
122         DEVMETHOD(device_attach,        nexus_attach),
123         DEVMETHOD(device_detach,        bus_generic_detach),
124         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
125         DEVMETHOD(device_suspend,       bus_generic_suspend),
126         DEVMETHOD(device_resume,        bus_generic_resume),
127
128         /* Bus interface */
129         DEVMETHOD(bus_print_child,      nexus_print_child),
130         DEVMETHOD(bus_add_child,        nexus_add_child),
131         DEVMETHOD(bus_read_ivar,        nexus_read_ivar),
132         DEVMETHOD(bus_write_ivar,       nexus_write_ivar),
133         DEVMETHOD(bus_alloc_resource,   nexus_alloc_resource),
134         DEVMETHOD(bus_release_resource, nexus_release_resource),
135         DEVMETHOD(bus_activate_resource, nexus_activate_resource),
136         DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource),
137         DEVMETHOD(bus_config_intr,      nexus_config_intr),
138         DEVMETHOD(bus_setup_intr,       nexus_setup_intr),
139         DEVMETHOD(bus_teardown_intr,    nexus_teardown_intr),
140         DEVMETHOD(bus_set_resource,     nexus_set_resource),
141         DEVMETHOD(bus_get_resource,     nexus_get_resource),
142         DEVMETHOD(bus_delete_resource,  nexus_delete_resource),
143
144         DEVMETHOD(pcib_alloc_msi,       nexus_alloc_msi),
145         DEVMETHOD(pcib_release_msi,     nexus_release_msi),
146         DEVMETHOD(pcib_map_msi,         nexus_map_msi),
147
148         { 0, 0 }
149 };
150
151 static driver_t nexus_driver = {
152         "nexus",
153         nexus_methods,
154         1,                      /* no softc */
155 };
156 static devclass_t nexus_devclass;
157
158 DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, NULL, NULL);
159
160 static int
161 nexus_probe(device_t dev)
162 {
163         int cpuid;
164
165         device_quiet(dev);      /* suppress attach message for neatness */
166
167         for (cpuid = 0; cpuid < ncpus; ++cpuid) {
168                 struct rman *rm = &irq_rman[cpuid];
169
170                 rm->rm_start = 0;
171                 rm->rm_end = IDT_HWI_VECTORS - 1;
172                 rm->rm_type = RMAN_ARRAY;
173                 rm->rm_descr = "Interrupt request lines";
174
175                 if (rman_init(rm, cpuid))
176                         panic("nexus_probe rman_init");
177                 MachIntrABI.rman_setup(rm);
178         }
179
180         /*
181          * ISA DMA on PCI systems is implemented in the ISA part of each
182          * PCI->ISA bridge and the channels can be duplicated if there are
183          * multiple bridges.  (eg: laptops with docking stations)
184          */
185         drq_rman.rm_start = 0;
186         drq_rman.rm_end = 7;
187         drq_rman.rm_type = RMAN_ARRAY;
188         drq_rman.rm_descr = "DMA request lines";
189         /* XXX drq 0 not available on some machines */
190         if (rman_init(&drq_rman, -1)
191             || rman_manage_region(&drq_rman,
192                                   drq_rman.rm_start, drq_rman.rm_end))
193                 panic("nexus_probe drq_rman");
194
195         /*
196          * However, IO ports and Memory truely are global at this level,
197          * as are APIC interrupts (however many IO APICS there turn out
198          * to be on large systems..)
199          */
200         port_rman.rm_start = 0;
201         port_rman.rm_end = 0xffff;
202         port_rman.rm_type = RMAN_ARRAY;
203         port_rman.rm_descr = "I/O ports";
204         if (rman_init(&port_rman, -1)
205             || rman_manage_region(&port_rman, 0, 0xffff))
206                 panic("nexus_probe port_rman");
207
208         mem_rman.rm_start = 0;
209         mem_rman.rm_end = ~0u;
210         mem_rman.rm_type = RMAN_ARRAY;
211         mem_rman.rm_descr = "I/O memory addresses";
212         if (rman_init(&mem_rman, -1)
213             || rman_manage_region(&mem_rman, 0, ~0))
214                 panic("nexus_probe mem_rman");
215
216         return bus_generic_probe(dev);
217 }
218
219 static int
220 nexus_attach(device_t dev)
221 {
222         device_t        child;
223
224         /*
225          * First, let our child driver's identify any child devices that
226          * they can find.  Once that is done attach any devices that we
227          * found.
228          */
229 #if 0 /* FUTURE */
230         bus_generic_probe(dev);
231 #endif
232         bus_generic_attach(dev);
233
234         /*
235          * And if we didn't see ISA on a pci bridge, create a
236          * connection point now so it shows up "on motherboard".
237          */
238         if (!devclass_get_device(devclass_find("isa"), 0)) {
239                 child = BUS_ADD_CHILD(dev, dev, 0, "isa", 0);
240                 if (child == NULL)
241                         panic("nexus_attach isa");
242                 device_probe_and_attach(child);
243         }
244
245         return 0;
246 }
247
248 static int
249 nexus_print_all_resources(device_t dev)
250 {
251         struct  nexus_device *ndev = DEVTONX(dev);
252         struct resource_list *rl = &ndev->nx_resources;
253         int retval = 0;
254
255         if (SLIST_FIRST(rl) || ndev->nx_pcibus != -1)
256                 retval += kprintf(" at");
257         
258         retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
259         retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
260         retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
261
262         return retval;
263 }
264
265 static int
266 nexus_print_child(device_t bus, device_t child)
267 {
268         struct  nexus_device *ndev = DEVTONX(child);
269         int retval = 0;
270
271         retval += bus_print_child_header(bus, child);
272         retval += nexus_print_all_resources(child);
273         if (ndev->nx_pcibus != -1)
274                 retval += kprintf(" pcibus %d", ndev->nx_pcibus);
275         retval += kprintf(" on motherboard\n");
276
277         return (retval);
278 }
279
280 static device_t
281 nexus_add_child(device_t bus, device_t parent, int order,
282                 const char *name, int unit)
283 {
284         device_t                child;
285         struct nexus_device     *ndev;
286
287         ndev = kmalloc(sizeof(struct nexus_device), M_NEXUSDEV, M_INTWAIT|M_ZERO);
288         if (!ndev)
289                 return(0);
290         resource_list_init(&ndev->nx_resources);
291         ndev->nx_pcibus = -1;
292
293         child = device_add_child_ordered(parent, order, name, unit); 
294
295         /* should we free this in nexus_child_detached? */
296         device_set_ivars(child, ndev);
297
298         return(child);
299 }
300
301 static int
302 nexus_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
303 {
304         struct nexus_device *ndev = DEVTONX(child);
305         
306         switch (which) {
307         case NEXUS_IVAR_PCIBUS:
308                 *result = ndev->nx_pcibus;
309                 break;
310         default:
311                 return ENOENT;
312         }
313         return 0;
314 }
315
316 static int
317 nexus_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
318 {
319         struct nexus_device *ndev = DEVTONX(child);
320         
321         switch (which) {
322         case NEXUS_IVAR_PCIBUS:
323                 ndev->nx_pcibus = value;
324                 break;
325         default:
326                 return ENOENT;
327         }
328         return 0;
329 }
330
331 /*
332  * Allocate a resource on behalf of child.  NB: child is usually going to be a
333  * child of one of our descendants, not a direct child of nexus0.
334  * (Exceptions include npx.)
335  */
336 static struct resource *
337 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
338     u_long start, u_long end, u_long count, u_int flags, int cpuid)
339 {
340         struct nexus_device *ndev = DEVTONX(child);
341         struct  resource *rv;
342         struct resource_list_entry *rle;
343         struct  rman *rm;
344         int needactivate = flags & RF_ACTIVE;
345
346         /*
347          * If this is an allocation of the "default" range for a given RID, and
348          * we know what the resources for this device are (ie. they aren't maintained
349          * by a child bus), then work out the start/end values.
350          */
351         if ((start == 0UL) && (end == ~0UL) && (count == 1)) {
352                 if (ndev == NULL)
353                         return(NULL);
354                 rle = resource_list_find(&ndev->nx_resources, type, *rid);
355                 if (rle == NULL)
356                         return(NULL);
357                 start = rle->start;
358                 end = rle->end;
359                 count = rle->count;
360                 cpuid = rle->cpuid;
361         }
362
363         flags &= ~RF_ACTIVE;
364
365         switch (type) {
366         case SYS_RES_IRQ:
367                 KASSERT(cpuid >= 0 && cpuid < ncpus,
368                     ("nexus invalid cpuid %d:\n", cpuid));
369                 rm = &irq_rman[cpuid];
370                 break;
371
372         case SYS_RES_DRQ:
373                 rm = &drq_rman;
374                 break;
375
376         case SYS_RES_IOPORT:
377                 rm = &port_rman;
378                 break;
379
380         case SYS_RES_MEMORY:
381                 rm = &mem_rman;
382                 break;
383
384         default:
385                 return 0;
386         }
387
388         rv = rman_reserve_resource(rm, start, end, count, flags, child);
389         if (rv == NULL)
390                 return 0;
391         rman_set_rid(rv, *rid);
392
393         if (type == SYS_RES_MEMORY) {
394                 rman_set_bustag(rv, I386_BUS_SPACE_MEM);
395         } else if (type == SYS_RES_IOPORT) {
396                 rman_set_bustag(rv, I386_BUS_SPACE_IO);
397                 rman_set_bushandle(rv, rv->r_start);
398         }
399
400         if (needactivate) {
401                 if (bus_activate_resource(child, type, *rid, rv)) {
402                         rman_release_resource(rv);
403                         return 0;
404                 }
405         }
406         
407         return rv;
408 }
409
410 static int
411 nexus_activate_resource(device_t bus, device_t child, int type, int rid,
412                         struct resource *r)
413 {
414         /*
415          * If this is a memory resource, map it into the kernel.
416          */
417         if (rman_get_bustag(r) == I386_BUS_SPACE_MEM) {
418                 caddr_t vaddr = 0;
419
420                 if (rman_get_end(r) < 1024 * 1024) {
421                         /*
422                          * The first 1Mb is mapped at KERNBASE.
423                          */
424                         vaddr = (caddr_t)(uintptr_t)(KERNBASE + rman_get_start(r));
425                 } else {
426                         u_int32_t paddr;
427                         u_int32_t psize;
428                         u_int32_t poffs;
429
430                         paddr = rman_get_start(r);
431                         psize = rman_get_size(r);
432
433                         poffs = paddr - trunc_page(paddr);
434                         vaddr = (caddr_t) pmap_mapdev(paddr-poffs, psize+poffs) + poffs;
435                 }
436                 rman_set_virtual(r, vaddr);
437                 /* IBM-PC: the type of bus_space_handle_t is u_int */
438                 rman_set_bushandle(r, (bus_space_handle_t) vaddr);
439         }
440         return (rman_activate_resource(r));
441 }
442
443 static int
444 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
445                           struct resource *r)
446 {
447         /*
448          * If this is a memory resource, unmap it.
449          */
450         if ((rman_get_bustag(r) == I386_BUS_SPACE_MEM) &&
451             (rman_get_end(r) >= 1024 * 1024)) {
452                 u_int32_t psize;
453
454                 psize = rman_get_size(r);
455                 pmap_unmapdev((vm_offset_t)rman_get_virtual(r), psize);
456         }
457                 
458         return (rman_deactivate_resource(r));
459 }
460
461 static int
462 nexus_release_resource(device_t bus, device_t child, int type, int rid,
463                        struct resource *r)
464 {
465         if (rman_get_flags(r) & RF_ACTIVE) {
466                 int error = bus_deactivate_resource(child, type, rid, r);
467                 if (error)
468                         return error;
469         }
470         return (rman_release_resource(r));
471 }
472
473 static int
474 nexus_config_intr(device_t bus, device_t chile, int irq,
475     enum intr_trigger trig, enum intr_polarity pola)
476 {
477         machintr_legacy_intr_config(irq, trig, pola);
478         return 0;
479 }
480
481 /*
482  * Currently this uses the really grody interface from kern/kern_intr.c
483  * (which really doesn't belong in kern/anything.c).  Eventually, all of
484  * the code in kern_intr.c and machdep_intr.c should get moved here, since
485  * this is going to be the official interface.
486  */
487 static int
488 nexus_setup_intr(device_t bus, device_t child, struct resource *irq,
489                  int flags, void (*ihand)(void *), void *arg,
490                  void **cookiep, lwkt_serialize_t serializer)
491 {
492         int     error, icflags;
493
494         /* somebody tried to setup an irq that failed to allocate! */
495         if (irq == NULL)
496                 panic("nexus_setup_intr: NULL irq resource!");
497
498         *cookiep = NULL;
499         icflags = flags;
500         if ((irq->r_flags & RF_SHAREABLE) == 0)
501                 icflags |= INTR_EXCL;
502
503         /*
504          * We depend here on rman_activate_resource() being idempotent.
505          */
506         error = rman_activate_resource(irq);
507         if (error)
508                 return (error);
509
510         /*
511          * XXX cast the interrupt handler function to an inthand2_t.  The
512          * difference is that an additional frame argument is passed which
513          * we do not currently want to expose the BUS subsystem to.
514          */
515         *cookiep = register_int(irq->r_start, (inthand2_t *)ihand, arg,
516                                 device_get_nameunit(child), serializer,
517                                 icflags, rman_get_cpuid(irq));
518         if (*cookiep == NULL)
519                 error = EINVAL;
520         return (error);
521 }
522
523 static int
524 nexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
525 {
526         if (ih) {
527                 unregister_int(ih, rman_get_cpuid(r));
528                 return (0);
529         }
530         return(-1);
531 }
532
533 static int
534 nexus_set_resource(device_t dev, device_t child, int type, int rid,
535     u_long start, u_long count, int cpuid)
536 {
537         struct nexus_device     *ndev = DEVTONX(child);
538         struct resource_list    *rl = &ndev->nx_resources;
539
540         /* XXX this should return a success/failure indicator */
541         resource_list_add(rl, type, rid, start, start + count - 1, count,
542             cpuid);
543         return(0);
544 }
545
546 static int
547 nexus_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
548 {
549         struct nexus_device     *ndev = DEVTONX(child);
550         struct resource_list    *rl = &ndev->nx_resources;
551         struct resource_list_entry *rle;
552
553         rle = resource_list_find(rl, type, rid);
554         device_printf(child, "type %d  rid %d  startp %p  countp %p - got %p\n",
555                       type, rid, startp, countp, rle);
556         if (!rle)
557                 return(ENOENT);
558         if (startp)
559                 *startp = rle->start;
560         if (countp)
561                 *countp = rle->count;
562         return(0);
563 }
564
565 static void
566 nexus_delete_resource(device_t dev, device_t child, int type, int rid)
567 {
568         struct nexus_device     *ndev = DEVTONX(child);
569         struct resource_list    *rl = &ndev->nx_resources;
570
571         resource_list_delete(rl, type, rid);
572 }
573
574 static int
575 nexus_alloc_msi(device_t dev, device_t child, int count, int maxcount,
576     int *irqs, int cpuid)
577 {
578         if (!lapic_enable)
579                 return ENODEV;
580
581         return MachIntrABI.msi_alloc(irqs, count, cpuid);
582 }
583
584 static int
585 nexus_release_msi(device_t dev, device_t child, int count, int *irqs, int cpuid)
586 {
587         KKASSERT(lapic_enable);
588         MachIntrABI.msi_release(irqs, count, cpuid);
589         return 0;
590 }
591
592 static int
593 nexus_map_msi(device_t dev, device_t child, int irq, uint64_t *addr,
594     uint32_t *data, int cpuid)
595 {
596         KKASSERT(lapic_enable);
597         MachIntrABI.msi_map(irq, addr, data, cpuid);
598         return 0;
599 }