2 * Copyright (c) 2005 Peter Grehan
3 * Copyright (c) 2009 Nathan Whitehorn
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 * Dispatch platform calls to the appropriate platform implementation
34 * through a previously registered kernel object.
37 #include <sys/param.h>
38 #include <sys/kernel.h>
41 #include <sys/mutex.h>
42 #include <sys/systm.h>
44 #include <sys/sysctl.h>
45 #include <sys/types.h>
48 #include <vm/vm_page.h>
50 #include <machine/cpu.h>
51 #include <machine/md_var.h>
52 #include <machine/platform.h>
53 #include <machine/platformvar.h>
54 #include <machine/smp.h>
56 #include "platform_if.h"
58 static platform_def_t *plat_def_impl;
59 static platform_t plat_obj;
60 static struct kobj_ops plat_kernel_kops;
61 static struct platform_kobj plat_kernel_obj;
63 static char plat_name[64] = "";
64 SYSCTL_STRING(_hw, OID_AUTO, platform, CTLFLAG_RD | CTLFLAG_TUN,
65 plat_name, 0, "Platform currently in use");
67 static struct mem_region *pregions = NULL;
68 static struct mem_region *aregions = NULL;
69 static int npregions, naregions;
72 mem_regions(struct mem_region **phys, int *physsz, struct mem_region **avail,
76 PLATFORM_MEM_REGIONS(plat_obj, &pregions, &npregions,
77 &aregions, &naregions);
86 mem_valid(vm_offset_t addr, int len)
91 PLATFORM_MEM_REGIONS(plat_obj, &pregions, &npregions,
92 &aregions, &naregions);
94 for (i = 0; i < npregions; i++)
95 if ((addr >= pregions[i].mr_start)
96 && (addr + len <= pregions[i].mr_start + pregions[i].mr_size))
103 platform_real_maxaddr(void)
105 return (PLATFORM_REAL_MAXADDR(plat_obj));
111 return (plat_def_impl->name);
115 platform_timebase_freq(struct cpuref *cpu)
117 return (PLATFORM_TIMEBASE_FREQ(plat_obj, cpu));
121 platform_smp_first_cpu(struct cpuref *cpu)
123 return (PLATFORM_SMP_FIRST_CPU(plat_obj, cpu));
127 platform_smp_next_cpu(struct cpuref *cpu)
129 return (PLATFORM_SMP_NEXT_CPU(plat_obj, cpu));
133 platform_smp_get_bsp(struct cpuref *cpu)
135 return (PLATFORM_SMP_GET_BSP(plat_obj, cpu));
139 platform_smp_start_cpu(struct pcpu *cpu)
141 return (PLATFORM_SMP_START_CPU(plat_obj, cpu));
145 platform_smp_ap_init()
147 PLATFORM_SMP_AP_INIT(plat_obj);
154 return (PLATFORM_SMP_TOPO(plat_obj));
159 * Reset back to firmware.
164 PLATFORM_RESET(plat_obj);
168 * Platform install routines. Highest priority wins, using the same
169 * algorithm as bus attachment.
171 SET_DECLARE(platform_set, platform_def_t);
174 platform_probe_and_attach()
176 platform_def_t **platpp, *platp;
179 plat_obj = &plat_kernel_obj;
183 * Try to locate the best platform kobj
185 SET_FOREACH(platpp, platform_set) {
189 * Take care of compiling the selected class, and
190 * then statically initialise the MMU object
192 kobj_class_compile_static(platp, &plat_kernel_kops);
193 kobj_init_static((kobj_t)plat_obj, platp);
195 prio = PLATFORM_PROBE(plat_obj);
197 /* Check for errors */
202 * Check if this module was specifically requested through
203 * the loader tunable we provide.
205 if (strcmp(platp->name,plat_name) == 0) {
206 plat_def_impl = platp;
210 /* Otherwise, see if it is better than our current best */
211 if (plat_def_impl == NULL || prio > best_prio) {
213 plat_def_impl = platp;
217 * We can't free the KOBJ, since it is static. Reset the ops
218 * member of this class so that we can come back later.
223 if (plat_def_impl == NULL)
224 panic("No platform module found!");
227 * Recompile to make sure we ended with the
228 * correct one, and then attach.
231 kobj_class_compile_static(plat_def_impl, &plat_kernel_kops);
232 kobj_init_static((kobj_t)plat_obj, plat_def_impl);
234 strlcpy(plat_name,plat_def_impl->name,sizeof(plat_name));
236 PLATFORM_ATTACH(plat_obj);