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