]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/x86/x86/nexus.c
Set bootverbose directly in mi_startup() rather than via a SYSINIT. This
[FreeBSD/FreeBSD.git] / sys / x86 / x86 / 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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 /*
34  * This code implements a `root nexus' for Intel Architecture
35  * machines.  The function of the root nexus 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 #ifdef __amd64__
45 #define DEV_APIC
46 #else
47 #include "opt_apic.h"
48 #endif
49 #include "opt_isa.h"
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/bus.h>
54 #include <sys/kernel.h>
55 #ifdef __amd64__
56 #include <sys/linker.h>
57 #endif
58 #include <sys/malloc.h>
59 #include <sys/module.h>
60 #include <machine/bus.h>
61 #include <machine/intr_machdep.h>
62 #include <sys/rman.h>
63 #include <sys/interrupt.h>
64
65 #include <machine/vmparam.h>
66 #include <vm/vm.h>
67 #include <vm/pmap.h>
68 #include <machine/pmap.h>
69
70 #ifdef __amd64__
71 #include <machine/metadata.h>
72 #include <machine/pc/bios.h>
73 #endif
74 #include <machine/nexusvar.h>
75 #include <machine/resource.h>
76
77 #ifdef DEV_APIC
78 #include "pcib_if.h"
79 #endif
80
81 #ifdef DEV_ISA
82 #include <isa/isavar.h>
83 #ifdef PC98
84 #include <pc98/cbus/cbus.h>
85 #else
86 #include <x86/isa/isa.h>
87 #endif
88 #endif
89 #include <sys/rtprio.h>
90
91 #ifdef __amd64__
92 #define RMAN_BUS_SPACE_IO       AMD64_BUS_SPACE_IO
93 #define RMAN_BUS_SPACE_MEM      AMD64_BUS_SPACE_MEM
94 #else
95 #define RMAN_BUS_SPACE_IO       I386_BUS_SPACE_IO
96 #define RMAN_BUS_SPACE_MEM      I386_BUS_SPACE_MEM
97 #endif
98
99 static MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device");
100
101 #define DEVTONX(dev)    ((struct nexus_device *)device_get_ivars(dev))
102
103 struct rman irq_rman, drq_rman, port_rman, mem_rman;
104
105 static  int nexus_probe(device_t);
106 static  int nexus_attach(device_t);
107 static  int nexus_print_all_resources(device_t dev);
108 static  int nexus_print_child(device_t, device_t);
109 static device_t nexus_add_child(device_t bus, u_int order, const char *name,
110                                 int unit);
111 static  struct resource *nexus_alloc_resource(device_t, device_t, int, int *,
112                                               u_long, u_long, u_long, u_int);
113 #ifdef SMP
114 static  int nexus_bind_intr(device_t, device_t, struct resource *, int);
115 #endif
116 static  int nexus_config_intr(device_t, int, enum intr_trigger,
117                               enum intr_polarity);
118 static  int nexus_describe_intr(device_t dev, device_t child,
119                                 struct resource *irq, void *cookie,
120                                 const char *descr);
121 static  int nexus_activate_resource(device_t, device_t, int, int,
122                                     struct resource *);
123 static  int nexus_deactivate_resource(device_t, device_t, int, int,
124                                       struct resource *);
125 static  int nexus_release_resource(device_t, device_t, int, int,
126                                    struct resource *);
127 static  int nexus_setup_intr(device_t, device_t, struct resource *, int flags,
128                              driver_filter_t filter, void (*)(void *), void *,
129                               void **);
130 static  int nexus_teardown_intr(device_t, device_t, struct resource *,
131                                 void *);
132 static struct resource_list *nexus_get_reslist(device_t dev, device_t child);
133 static  int nexus_set_resource(device_t, device_t, int, int, u_long, u_long);
134 static  int nexus_get_resource(device_t, device_t, int, int, u_long *, u_long *);
135 static void nexus_delete_resource(device_t, device_t, int, int);
136 #ifdef DEV_APIC
137 static  int nexus_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs);
138 static  int nexus_release_msi(device_t pcib, device_t dev, int count, int *irqs);
139 static  int nexus_alloc_msix(device_t pcib, device_t dev, int *irq);
140 static  int nexus_release_msix(device_t pcib, device_t dev, int irq);
141 static  int nexus_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr, uint32_t *data);
142 #endif
143
144 static device_method_t nexus_methods[] = {
145         /* Device interface */
146         DEVMETHOD(device_probe,         nexus_probe),
147         DEVMETHOD(device_attach,        nexus_attach),
148         DEVMETHOD(device_detach,        bus_generic_detach),
149         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
150         DEVMETHOD(device_suspend,       bus_generic_suspend),
151         DEVMETHOD(device_resume,        bus_generic_resume),
152
153         /* Bus interface */
154         DEVMETHOD(bus_print_child,      nexus_print_child),
155         DEVMETHOD(bus_add_child,        nexus_add_child),
156         DEVMETHOD(bus_alloc_resource,   nexus_alloc_resource),
157         DEVMETHOD(bus_release_resource, nexus_release_resource),
158         DEVMETHOD(bus_activate_resource, nexus_activate_resource),
159         DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource),
160         DEVMETHOD(bus_setup_intr,       nexus_setup_intr),
161         DEVMETHOD(bus_teardown_intr,    nexus_teardown_intr),
162 #ifdef SMP
163         DEVMETHOD(bus_bind_intr,        nexus_bind_intr),
164 #endif
165         DEVMETHOD(bus_config_intr,      nexus_config_intr),
166         DEVMETHOD(bus_describe_intr,    nexus_describe_intr),
167         DEVMETHOD(bus_get_resource_list, nexus_get_reslist),
168         DEVMETHOD(bus_set_resource,     nexus_set_resource),
169         DEVMETHOD(bus_get_resource,     nexus_get_resource),
170         DEVMETHOD(bus_delete_resource,  nexus_delete_resource),
171
172         /* pcib interface */
173 #ifdef DEV_APIC
174         DEVMETHOD(pcib_alloc_msi,       nexus_alloc_msi),
175         DEVMETHOD(pcib_release_msi,     nexus_release_msi),
176         DEVMETHOD(pcib_alloc_msix,      nexus_alloc_msix),
177         DEVMETHOD(pcib_release_msix,    nexus_release_msix),
178         DEVMETHOD(pcib_map_msi,         nexus_map_msi),
179 #endif
180
181         { 0, 0 }
182 };
183
184 DEFINE_CLASS_0(nexus, nexus_driver, nexus_methods, 1);
185 static devclass_t nexus_devclass;
186
187 DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0);
188
189 static int
190 nexus_probe(device_t dev)
191 {
192
193         device_quiet(dev);      /* suppress attach message for neatness */
194         return (BUS_PROBE_GENERIC);
195 }
196
197 void
198 nexus_init_resources(void)
199 {
200         int irq;
201
202         /*
203          * XXX working notes:
204          *
205          * - IRQ resource creation should be moved to the PIC/APIC driver.
206          * - DRQ resource creation should be moved to the DMAC driver.
207          * - The above should be sorted to probe earlier than any child busses.
208          *
209          * - Leave I/O and memory creation here, as child probes may need them.
210          *   (especially eg. ACPI)
211          */
212
213         /*
214          * IRQ's are on the mainboard on old systems, but on the ISA part
215          * of PCI->ISA bridges.  There would be multiple sets of IRQs on
216          * multi-ISA-bus systems.  PCI interrupts are routed to the ISA
217          * component, so in a way, PCI can be a partial child of an ISA bus(!).
218          * APIC interrupts are global though.
219          */
220         irq_rman.rm_start = 0;
221         irq_rman.rm_type = RMAN_ARRAY;
222         irq_rman.rm_descr = "Interrupt request lines";
223         irq_rman.rm_end = NUM_IO_INTS - 1;
224         if (rman_init(&irq_rman))
225                 panic("nexus_init_resources irq_rman");
226
227         /*
228          * We search for regions of existing IRQs and add those to the IRQ
229          * resource manager.
230          */
231         for (irq = 0; irq < NUM_IO_INTS; irq++)
232                 if (intr_lookup_source(irq) != NULL)
233                         if (rman_manage_region(&irq_rman, irq, irq) != 0)
234                                 panic("nexus_init_resources irq_rman add");
235
236         /*
237          * ISA DMA on PCI systems is implemented in the ISA part of each
238          * PCI->ISA bridge and the channels can be duplicated if there are
239          * multiple bridges.  (eg: laptops with docking stations)
240          */
241         drq_rman.rm_start = 0;
242 #ifdef PC98
243         drq_rman.rm_end = 3;
244 #else
245         drq_rman.rm_end = 7;
246 #endif
247         drq_rman.rm_type = RMAN_ARRAY;
248         drq_rman.rm_descr = "DMA request lines";
249         /* XXX drq 0 not available on some machines */
250         if (rman_init(&drq_rman)
251             || rman_manage_region(&drq_rman,
252                                   drq_rman.rm_start, drq_rman.rm_end))
253                 panic("nexus_init_resources drq_rman");
254
255         /*
256          * However, IO ports and Memory truely are global at this level,
257          * as are APIC interrupts (however many IO APICS there turn out
258          * to be on large systems..)
259          */
260         port_rman.rm_start = 0;
261         port_rman.rm_end = 0xffff;
262         port_rman.rm_type = RMAN_ARRAY;
263         port_rman.rm_descr = "I/O ports";
264         if (rman_init(&port_rman)
265             || rman_manage_region(&port_rman, 0, 0xffff))
266                 panic("nexus_init_resources port_rman");
267
268         mem_rman.rm_start = 0;
269         mem_rman.rm_end = ~0u;
270         mem_rman.rm_type = RMAN_ARRAY;
271         mem_rman.rm_descr = "I/O memory addresses";
272         if (rman_init(&mem_rman)
273             || rman_manage_region(&mem_rman, 0, ~0))
274                 panic("nexus_init_resources mem_rman");
275 }
276
277 static int
278 nexus_attach(device_t dev)
279 {
280
281         nexus_init_resources();
282         bus_generic_probe(dev);
283
284         /*
285          * Explicitly add the legacy0 device here.  Other platform
286          * types (such as ACPI), use their own nexus(4) subclass
287          * driver to override this routine and add their own root bus.
288          */
289         if (BUS_ADD_CHILD(dev, 10, "legacy", 0) == NULL)
290                 panic("legacy: could not attach");
291         bus_generic_attach(dev);
292         return 0;
293 }
294
295 static int
296 nexus_print_all_resources(device_t dev)
297 {
298         struct  nexus_device *ndev = DEVTONX(dev);
299         struct resource_list *rl = &ndev->nx_resources;
300         int retval = 0;
301
302         if (STAILQ_FIRST(rl))
303                 retval += printf(" at");
304
305         retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx");
306         retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx");
307         retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld");
308
309         return retval;
310 }
311
312 static int
313 nexus_print_child(device_t bus, device_t child)
314 {
315         int retval = 0;
316
317         retval += bus_print_child_header(bus, child);
318         retval += nexus_print_all_resources(child);
319         if (device_get_flags(child))
320                 retval += printf(" flags %#x", device_get_flags(child));
321         retval += printf(" on motherboard\n");  /* XXX "motherboard", ick */
322
323         return (retval);
324 }
325
326 static device_t
327 nexus_add_child(device_t bus, u_int order, const char *name, int unit)
328 {
329         device_t                child;
330         struct nexus_device     *ndev;
331
332         ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO);
333         if (!ndev)
334                 return(0);
335         resource_list_init(&ndev->nx_resources);
336
337         child = device_add_child_ordered(bus, order, name, unit);
338
339         /* should we free this in nexus_child_detached? */
340         device_set_ivars(child, ndev);
341
342         return(child);
343 }
344
345 /*
346  * Allocate a resource on behalf of child.  NB: child is usually going to be a
347  * child of one of our descendants, not a direct child of nexus0.
348  * (Exceptions include npx.)
349  */
350 static struct resource *
351 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid,
352                      u_long start, u_long end, u_long count, u_int flags)
353 {
354         struct nexus_device *ndev = DEVTONX(child);
355         struct  resource *rv;
356         struct resource_list_entry *rle;
357         struct  rman *rm;
358         int needactivate = flags & RF_ACTIVE;
359
360         /*
361          * If this is an allocation of the "default" range for a given RID, and
362          * we know what the resources for this device are (ie. they aren't maintained
363          * by a child bus), then work out the start/end values.
364          */
365         if ((start == 0UL) && (end == ~0UL) && (count == 1)) {
366                 if (ndev == NULL)
367                         return(NULL);
368                 rle = resource_list_find(&ndev->nx_resources, type, *rid);
369                 if (rle == NULL)
370                         return(NULL);
371                 start = rle->start;
372                 end = rle->end;
373                 count = rle->count;
374         }
375
376         flags &= ~RF_ACTIVE;
377
378         switch (type) {
379         case SYS_RES_IRQ:
380                 rm = &irq_rman;
381                 break;
382
383         case SYS_RES_DRQ:
384                 rm = &drq_rman;
385                 break;
386
387         case SYS_RES_IOPORT:
388                 rm = &port_rman;
389                 break;
390
391         case SYS_RES_MEMORY:
392                 rm = &mem_rman;
393                 break;
394
395         default:
396                 return 0;
397         }
398
399         rv = rman_reserve_resource(rm, start, end, count, flags, child);
400         if (rv == 0)
401                 return 0;
402         rman_set_rid(rv, *rid);
403
404         if (needactivate) {
405                 if (bus_activate_resource(child, type, *rid, rv)) {
406                         rman_release_resource(rv);
407                         return 0;
408                 }
409         }
410
411         return rv;
412 }
413
414 static int
415 nexus_activate_resource(device_t bus, device_t child, int type, int rid,
416                         struct resource *r)
417 {
418 #ifdef PC98
419         bus_space_handle_t bh;
420         int error;
421 #endif
422         void *vaddr;
423
424         /*
425          * If this is a memory resource, map it into the kernel.
426          */
427         switch (type) {
428         case SYS_RES_IOPORT:
429 #ifdef PC98
430                 error = i386_bus_space_handle_alloc(I386_BUS_SPACE_IO,
431                     rman_get_start(r), rman_get_size(r), &bh);
432                 if (error)
433                         return (error);
434                 rman_set_bushandle(r, bh);
435 #else
436                 rman_set_bushandle(r, rman_get_start(r));
437 #endif
438                 rman_set_bustag(r, RMAN_BUS_SPACE_IO);
439                 break;
440         case SYS_RES_MEMORY:
441 #ifdef PC98
442                 error = i386_bus_space_handle_alloc(I386_BUS_SPACE_MEM,
443                     rman_get_start(r), rman_get_size(r), &bh);
444                 if (error)
445                         return (error);
446 #endif
447                 vaddr = pmap_mapdev(rman_get_start(r), rman_get_size(r));
448                 rman_set_virtual(r, vaddr);
449                 rman_set_bustag(r, RMAN_BUS_SPACE_MEM);
450 #ifdef PC98
451                 /* PC-98: the type of bus_space_handle_t is the structure. */
452                 bh->bsh_base = (bus_addr_t) vaddr;
453                 rman_set_bushandle(r, bh);
454 #else
455                 /* IBM-PC: the type of bus_space_handle_t is u_int */
456                 rman_set_bushandle(r, (bus_space_handle_t) vaddr);
457 #endif
458         }
459         return (rman_activate_resource(r));
460 }
461
462 static int
463 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid,
464                           struct resource *r)
465 {
466
467         /*
468          * If this is a memory resource, unmap it.
469          */
470         if (type == SYS_RES_MEMORY) {
471                 pmap_unmapdev((vm_offset_t)rman_get_virtual(r),
472                     rman_get_size(r));
473         }
474 #ifdef PC98
475         if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
476                 bus_space_handle_t bh;
477
478                 bh = rman_get_bushandle(r);
479                 i386_bus_space_handle_free(rman_get_bustag(r), bh, bh->bsh_sz);
480         }
481 #endif
482         return (rman_deactivate_resource(r));
483 }
484
485 static int
486 nexus_release_resource(device_t bus, device_t child, int type, int rid,
487                        struct resource *r)
488 {
489         if (rman_get_flags(r) & RF_ACTIVE) {
490                 int error = bus_deactivate_resource(child, type, rid, r);
491                 if (error)
492                         return error;
493         }
494         return (rman_release_resource(r));
495 }
496
497 /*
498  * Currently this uses the really grody interface from kern/kern_intr.c
499  * (which really doesn't belong in kern/anything.c).  Eventually, all of
500  * the code in kern_intr.c and machdep_intr.c should get moved here, since
501  * this is going to be the official interface.
502  */
503 static int
504 nexus_setup_intr(device_t bus, device_t child, struct resource *irq,
505                  int flags, driver_filter_t filter, void (*ihand)(void *),
506                  void *arg, void **cookiep)
507 {
508         int             error;
509
510         /* somebody tried to setup an irq that failed to allocate! */
511         if (irq == NULL)
512                 panic("nexus_setup_intr: NULL irq resource!");
513
514         *cookiep = 0;
515         if ((rman_get_flags(irq) & RF_SHAREABLE) == 0)
516                 flags |= INTR_EXCL;
517
518         /*
519          * We depend here on rman_activate_resource() being idempotent.
520          */
521         error = rman_activate_resource(irq);
522         if (error)
523                 return (error);
524
525         error = intr_add_handler(device_get_nameunit(child),
526             rman_get_start(irq), filter, ihand, arg, flags, cookiep);
527
528         return (error);
529 }
530
531 static int
532 nexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih)
533 {
534         return (intr_remove_handler(ih));
535 }
536
537 #ifdef SMP
538 static int
539 nexus_bind_intr(device_t dev, device_t child, struct resource *irq, int cpu)
540 {
541         return (intr_bind(rman_get_start(irq), cpu));
542 }
543 #endif
544
545 static int
546 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig,
547     enum intr_polarity pol)
548 {
549         return (intr_config_intr(irq, trig, pol));
550 }
551
552 static int
553 nexus_describe_intr(device_t dev, device_t child, struct resource *irq,
554     void *cookie, const char *descr)
555 {
556
557         return (intr_describe(rman_get_start(irq), cookie, descr));
558 }
559
560 static struct resource_list *
561 nexus_get_reslist(device_t dev, device_t child)
562 {
563         struct nexus_device *ndev = DEVTONX(child);
564
565         return (&ndev->nx_resources);
566 }
567
568 static int
569 nexus_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count)
570 {
571         struct nexus_device     *ndev = DEVTONX(child);
572         struct resource_list    *rl = &ndev->nx_resources;
573
574         /* XXX this should return a success/failure indicator */
575         resource_list_add(rl, type, rid, start, start + count - 1, count);
576         return(0);
577 }
578
579 static int
580 nexus_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp)
581 {
582         struct nexus_device     *ndev = DEVTONX(child);
583         struct resource_list    *rl = &ndev->nx_resources;
584         struct resource_list_entry *rle;
585
586         rle = resource_list_find(rl, type, rid);
587         if (!rle)
588                 return(ENOENT);
589         if (startp)
590                 *startp = rle->start;
591         if (countp)
592                 *countp = rle->count;
593         return(0);
594 }
595
596 static void
597 nexus_delete_resource(device_t dev, device_t child, int type, int rid)
598 {
599         struct nexus_device     *ndev = DEVTONX(child);
600         struct resource_list    *rl = &ndev->nx_resources;
601
602         resource_list_delete(rl, type, rid);
603 }
604
605 /* Called from the MSI code to add new IRQs to the IRQ rman. */
606 void
607 nexus_add_irq(u_long irq)
608 {
609
610         if (rman_manage_region(&irq_rman, irq, irq) != 0)
611                 panic("%s: failed", __func__);
612 }
613
614 #ifdef DEV_APIC
615 static int
616 nexus_alloc_msix(device_t pcib, device_t dev, int *irq)
617 {
618
619         return (msix_alloc(dev, irq));
620 }
621
622 static int
623 nexus_release_msix(device_t pcib, device_t dev, int irq)
624 {
625
626         return (msix_release(irq));
627 }
628
629 static int
630 nexus_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs)
631 {
632
633         return (msi_alloc(dev, count, maxcount, irqs));
634 }
635
636 static int
637 nexus_release_msi(device_t pcib, device_t dev, int count, int *irqs)
638 {
639
640         return (msi_release(irqs, count));
641 }
642
643 static int
644 nexus_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr, uint32_t *data)
645 {
646
647         return (msi_map(irq, addr, data));
648 }
649 #endif
650
651 /* Placeholder for system RAM. */
652 static void
653 ram_identify(driver_t *driver, device_t parent)
654 {
655
656         if (resource_disabled("ram", 0))
657                 return; 
658         if (BUS_ADD_CHILD(parent, 0, "ram", 0) == NULL)
659                 panic("ram_identify");
660 }
661
662 static int
663 ram_probe(device_t dev)
664 {
665
666         device_quiet(dev);
667         device_set_desc(dev, "System RAM");
668         return (0);
669 }
670
671 #ifdef __amd64__
672 static int
673 ram_attach(device_t dev)
674 {
675         struct bios_smap *smapbase, *smap, *smapend;
676         struct resource *res;
677         caddr_t kmdp;
678         uint32_t smapsize;
679         int error, rid;
680
681         /* Retrieve the system memory map from the loader. */
682         kmdp = preload_search_by_type("elf kernel");
683         if (kmdp == NULL)
684                 kmdp = preload_search_by_type("elf64 kernel");  
685         smapbase = (struct bios_smap *)preload_search_info(kmdp,
686             MODINFO_METADATA | MODINFOMD_SMAP);
687         smapsize = *((u_int32_t *)smapbase - 1);
688         smapend = (struct bios_smap *)((uintptr_t)smapbase + smapsize);
689
690         rid = 0;
691         for (smap = smapbase; smap < smapend; smap++) {
692                 if (smap->type != SMAP_TYPE_MEMORY || smap->length == 0)
693                         continue;
694                 error = bus_set_resource(dev, SYS_RES_MEMORY, rid, smap->base,
695                     smap->length);
696                 if (error)
697                         panic("ram_attach: resource %d failed set with %d", rid,
698                             error);
699                 res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 0);
700                 if (res == NULL)
701                         panic("ram_attach: resource %d failed to attach", rid);
702                 rid++;
703         }
704         return (0);
705 }
706 #else
707 static int
708 ram_attach(device_t dev)
709 {
710         struct resource *res;
711         vm_paddr_t *p;
712         int error, i, rid;
713
714         /*
715          * We use the dump_avail[] array rather than phys_avail[] for
716          * the memory map as phys_avail[] contains holes for kernel
717          * memory, page 0, the message buffer, and the dcons buffer.
718          * We test the end address in the loop instead of the start
719          * since the start address for the first segment is 0.
720          *
721          * XXX: It would be preferable to use the SMAP if it exists
722          * instead since if the SMAP is very fragmented we may not
723          * include some memory regions in dump_avail[] and phys_avail[].
724          */
725         for (i = 0, p = dump_avail; p[1] != 0; i++, p += 2) {
726                 rid = i;
727 #ifdef PAE
728                 /*
729                  * Resources use long's to track resources, so we can't
730                  * include memory regions above 4GB.
731                  */
732                 if (p[0] >= ~0ul)
733                         break;
734 #endif
735                 error = bus_set_resource(dev, SYS_RES_MEMORY, rid, p[0],
736                     p[1] - p[0]);
737                 if (error)
738                         panic("ram_attach: resource %d failed set with %d", i,
739                             error);
740                 res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 0);
741                 if (res == NULL)
742                         panic("ram_attach: resource %d failed to attach", i);
743         }
744         return (0);
745 }
746 #endif
747
748 static device_method_t ram_methods[] = {
749         /* Device interface */
750         DEVMETHOD(device_identify,      ram_identify),
751         DEVMETHOD(device_probe,         ram_probe),
752         DEVMETHOD(device_attach,        ram_attach),
753         { 0, 0 }
754 };
755
756 static driver_t ram_driver = {
757         "ram",
758         ram_methods,
759         1,              /* no softc */
760 };
761
762 static devclass_t ram_devclass;
763
764 DRIVER_MODULE(ram, nexus, ram_driver, ram_devclass, 0, 0);
765
766 #ifdef DEV_ISA
767 /*
768  * Placeholder which claims PnP 'devices' which describe system
769  * resources.
770  */
771 static struct isa_pnp_id sysresource_ids[] = {
772         { 0x010cd041 /* PNP0c01 */, "System Memory" },
773         { 0x020cd041 /* PNP0c02 */, "System Resource" },
774         { 0 }
775 };
776
777 static int
778 sysresource_probe(device_t dev)
779 {
780         int     result;
781
782         if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, sysresource_ids)) <= 0) {
783                 device_quiet(dev);
784         }
785         return(result);
786 }
787
788 static int
789 sysresource_attach(device_t dev)
790 {
791         return(0);
792 }
793
794 static device_method_t sysresource_methods[] = {
795         /* Device interface */
796         DEVMETHOD(device_probe,         sysresource_probe),
797         DEVMETHOD(device_attach,        sysresource_attach),
798         DEVMETHOD(device_detach,        bus_generic_detach),
799         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
800         DEVMETHOD(device_suspend,       bus_generic_suspend),
801         DEVMETHOD(device_resume,        bus_generic_resume),
802         { 0, 0 }
803 };
804
805 static driver_t sysresource_driver = {
806         "sysresource",
807         sysresource_methods,
808         1,              /* no softc */
809 };
810
811 static devclass_t sysresource_devclass;
812
813 DRIVER_MODULE(sysresource, isa, sysresource_driver, sysresource_devclass, 0, 0);
814 #endif /* DEV_ISA */