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