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