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[PHYS_AVAIL_SZ];
68 static struct mem_region aregions[PHYS_AVAIL_SZ];
69 static int npregions, naregions;
72 * Memory region utilities: determine if two regions overlap,
73 * and merge two overlapping regions into one
76 memr_overlap(struct mem_region *r1, struct mem_region *r2)
78 if ((r1->mr_start + r1->mr_size) < r2->mr_start ||
79 (r2->mr_start + r2->mr_size) < r1->mr_start)
86 memr_merge(struct mem_region *from, struct mem_region *to)
89 end = ulmax(to->mr_start + to->mr_size, from->mr_start + from->mr_size);
90 to->mr_start = ulmin(from->mr_start, to->mr_start);
91 to->mr_size = end - to->mr_start;
95 * Quick sort callout for comparing memory regions.
98 mr_cmp(const void *a, const void *b)
100 const struct mem_region *regiona, *regionb;
104 if (regiona->mr_start < regionb->mr_start)
106 else if (regiona->mr_start > regionb->mr_start)
113 mem_regions(struct mem_region **phys, int *physsz, struct mem_region **avail,
116 int i, j, still_merging;
118 if (npregions == 0) {
119 PLATFORM_MEM_REGIONS(plat_obj, &pregions[0], &npregions,
120 aregions, &naregions);
121 qsort(pregions, npregions, sizeof(*pregions), mr_cmp);
122 qsort(aregions, naregions, sizeof(*aregions), mr_cmp);
124 /* Remove overlapping available regions */
126 still_merging = FALSE;
127 for (i = 0; i < naregions; i++) {
128 if (aregions[i].mr_size == 0)
130 for (j = i+1; j < naregions; j++) {
131 if (aregions[j].mr_size == 0)
133 if (!memr_overlap(&aregions[j],
137 memr_merge(&aregions[j], &aregions[i]);
139 aregions[j].mr_size = 0;
140 still_merging = TRUE;
143 } while (still_merging == TRUE);
145 /* Collapse zero-length available regions */
146 for (i = 0; i < naregions; i++) {
147 if (aregions[i].mr_size == 0) {
148 memcpy(&aregions[i], &aregions[i+1],
149 (naregions - i - 1)*sizeof(*aregions));
159 *availsz = naregions;
163 mem_valid(vm_offset_t addr, int len)
167 if (npregions == 0) {
168 struct mem_region *p, *a;
170 mem_regions(&p, &np, &a, &na);
173 for (i = 0; i < npregions; i++)
174 if ((addr >= pregions[i].mr_start)
175 && (addr + len <= pregions[i].mr_start + pregions[i].mr_size))
182 platform_real_maxaddr(void)
184 return (PLATFORM_REAL_MAXADDR(plat_obj));
190 return (plat_def_impl->name);
194 platform_timebase_freq(struct cpuref *cpu)
196 return (PLATFORM_TIMEBASE_FREQ(plat_obj, cpu));
200 * Put the current CPU, as last step in suspend, to sleep
205 PLATFORM_SLEEP(plat_obj);
209 platform_smp_first_cpu(struct cpuref *cpu)
211 return (PLATFORM_SMP_FIRST_CPU(plat_obj, cpu));
215 platform_smp_next_cpu(struct cpuref *cpu)
217 return (PLATFORM_SMP_NEXT_CPU(plat_obj, cpu));
221 platform_smp_get_bsp(struct cpuref *cpu)
223 return (PLATFORM_SMP_GET_BSP(plat_obj, cpu));
227 platform_smp_start_cpu(struct pcpu *cpu)
229 return (PLATFORM_SMP_START_CPU(plat_obj, cpu));
233 platform_smp_ap_init()
235 PLATFORM_SMP_AP_INIT(plat_obj);
242 return (PLATFORM_SMP_TOPO(plat_obj));
247 * Reset back to firmware.
252 PLATFORM_RESET(plat_obj);
256 * Platform install routines. Highest priority wins, using the same
257 * algorithm as bus attachment.
259 SET_DECLARE(platform_set, platform_def_t);
262 platform_probe_and_attach()
264 platform_def_t **platpp, *platp;
267 plat_obj = &plat_kernel_obj;
271 * Try to locate the best platform kobj
273 SET_FOREACH(platpp, platform_set) {
277 * Take care of compiling the selected class, and
278 * then statically initialise the MMU object
280 kobj_class_compile_static(platp, &plat_kernel_kops);
281 kobj_init_static((kobj_t)plat_obj, platp);
283 prio = PLATFORM_PROBE(plat_obj);
285 /* Check for errors */
290 * Check if this module was specifically requested through
291 * the loader tunable we provide.
293 if (strcmp(platp->name,plat_name) == 0) {
294 plat_def_impl = platp;
298 /* Otherwise, see if it is better than our current best */
299 if (plat_def_impl == NULL || prio > best_prio) {
301 plat_def_impl = platp;
305 * We can't free the KOBJ, since it is static. Reset the ops
306 * member of this class so that we can come back later.
311 if (plat_def_impl == NULL)
312 panic("No platform module found!");
315 * Recompile to make sure we ended with the
316 * correct one, and then attach.
319 kobj_class_compile_static(plat_def_impl, &plat_kernel_kops);
320 kobj_init_static((kobj_t)plat_obj, plat_def_impl);
322 strlcpy(plat_name,plat_def_impl->name,sizeof(plat_name));
324 PLATFORM_ATTACH(plat_obj);