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