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