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