2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005 Peter Grehan
5 * Copyright (c) 2009 Nathan Whitehorn
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 * Dispatch platform calls to the appropriate platform implementation
36 * through a previously registered kernel object.
39 #include <sys/param.h>
40 #include <sys/kernel.h>
43 #include <sys/mutex.h>
45 #include <sys/systm.h>
47 #include <sys/sysctl.h>
48 #include <sys/types.h>
51 #include <vm/vm_page.h>
53 #include <machine/cpu.h>
54 #include <machine/md_var.h>
55 #include <machine/platform.h>
56 #include <machine/platformvar.h>
57 #include <machine/smp.h>
59 #include "platform_if.h"
61 static platform_def_t *plat_def_impl;
62 static platform_t plat_obj;
63 static struct kobj_ops plat_kernel_kops;
64 static struct platform_kobj plat_kernel_obj;
66 static char plat_name[64] = "";
67 SYSCTL_STRING(_hw, OID_AUTO, platform, CTLFLAG_RD | CTLFLAG_TUN,
68 plat_name, 0, "Platform currently in use");
70 static struct mem_region pregions[PHYS_AVAIL_SZ];
71 static struct mem_region aregions[PHYS_AVAIL_SZ];
72 static int npregions, naregions;
75 * Memory region utilities: determine if two regions overlap,
76 * and merge two overlapping regions into one
79 memr_overlap(struct mem_region *r1, struct mem_region *r2)
81 if ((r1->mr_start + r1->mr_size) < r2->mr_start ||
82 (r2->mr_start + r2->mr_size) < r1->mr_start)
89 memr_merge(struct mem_region *from, struct mem_region *to)
92 end = uqmax(to->mr_start + to->mr_size, from->mr_start + from->mr_size);
93 to->mr_start = uqmin(from->mr_start, to->mr_start);
94 to->mr_size = end - to->mr_start;
98 * Quick sort callout for comparing memory regions.
101 mr_cmp(const void *a, const void *b)
103 const struct mem_region *regiona, *regionb;
107 if (regiona->mr_start < regionb->mr_start)
109 else if (regiona->mr_start > regionb->mr_start)
116 mem_regions(struct mem_region **phys, int *physsz, struct mem_region **avail,
119 int i, j, still_merging;
121 if (npregions == 0) {
122 PLATFORM_MEM_REGIONS(plat_obj, pregions, &npregions,
123 aregions, &naregions);
124 qsort(pregions, npregions, sizeof(*pregions), mr_cmp);
125 qsort(aregions, naregions, sizeof(*aregions), mr_cmp);
127 /* Remove overlapping available regions */
129 still_merging = FALSE;
130 for (i = 0; i < naregions; i++) {
131 if (aregions[i].mr_size == 0)
133 for (j = i+1; j < naregions; j++) {
134 if (aregions[j].mr_size == 0)
136 if (!memr_overlap(&aregions[j],
140 memr_merge(&aregions[j], &aregions[i]);
142 aregions[j].mr_size = 0;
143 still_merging = TRUE;
146 } while (still_merging == TRUE);
148 /* Collapse zero-length available regions */
149 for (i = 0; i < naregions; i++) {
150 if (aregions[i].mr_size == 0) {
151 memcpy(&aregions[i], &aregions[i+1],
152 (naregions - i - 1)*sizeof(*aregions));
162 *availsz = naregions;
166 mem_valid(vm_offset_t addr, int len)
170 if (npregions == 0) {
171 struct mem_region *p, *a;
173 mem_regions(&p, &np, &a, &na);
176 for (i = 0; i < npregions; i++)
177 if ((addr >= pregions[i].mr_start)
178 && (addr + len <= pregions[i].mr_start + pregions[i].mr_size))
185 platform_real_maxaddr(void)
187 return (PLATFORM_REAL_MAXADDR(plat_obj));
193 return (plat_def_impl->name);
197 platform_timebase_freq(struct cpuref *cpu)
199 return (PLATFORM_TIMEBASE_FREQ(plat_obj, cpu));
203 * Put the current CPU, as last step in suspend, to sleep
208 PLATFORM_SLEEP(plat_obj);
212 platform_smp_first_cpu(struct cpuref *cpu)
214 return (PLATFORM_SMP_FIRST_CPU(plat_obj, cpu));
218 platform_smp_next_cpu(struct cpuref *cpu)
220 return (PLATFORM_SMP_NEXT_CPU(plat_obj, cpu));
224 platform_smp_get_bsp(struct cpuref *cpu)
226 return (PLATFORM_SMP_GET_BSP(plat_obj, cpu));
230 platform_smp_start_cpu(struct pcpu *cpu)
232 return (PLATFORM_SMP_START_CPU(plat_obj, cpu));
236 platform_smp_ap_init()
238 PLATFORM_SMP_AP_INIT(plat_obj);
245 return (PLATFORM_SMP_TOPO(plat_obj));
250 * Reset back to firmware.
255 PLATFORM_RESET(plat_obj);
259 cpu_idle_wakeup(int cpu)
261 return (PLATFORM_IDLE_WAKEUP(plat_obj, cpu));
265 platform_cpu_idle(int cpu)
268 PLATFORM_IDLE(plat_obj, cpu);
271 void platform_smp_timebase_sync(u_long tb, int ap)
274 PLATFORM_SMP_TIMEBASE_SYNC(plat_obj, tb, ap);
278 * Platform install routines. Highest priority wins, using the same
279 * algorithm as bus attachment.
281 SET_DECLARE(platform_set, platform_def_t);
284 platform_probe_and_attach()
286 platform_def_t **platpp, *platp;
289 plat_obj = &plat_kernel_obj;
293 * Try to locate the best platform kobj
295 SET_FOREACH(platpp, platform_set) {
299 * Take care of compiling the selected class, and
300 * then statically initialise the MMU object
302 kobj_class_compile_static(platp, &plat_kernel_kops);
303 kobj_init_static((kobj_t)plat_obj, platp);
305 prio = PLATFORM_PROBE(plat_obj);
307 /* Check for errors */
312 * Check if this module was specifically requested through
313 * the loader tunable we provide.
315 if (strcmp(platp->name,plat_name) == 0) {
316 plat_def_impl = platp;
320 /* Otherwise, see if it is better than our current best */
321 if (plat_def_impl == NULL || prio > best_prio) {
323 plat_def_impl = platp;
327 * We can't free the KOBJ, since it is static. Reset the ops
328 * member of this class so that we can come back later.
333 if (plat_def_impl == NULL)
334 panic("No platform module found!");
337 * Recompile to make sure we ended with the
338 * correct one, and then attach.
341 kobj_class_compile_static(plat_def_impl, &plat_kernel_kops);
342 kobj_init_static((kobj_t)plat_obj, plat_def_impl);
344 strlcpy(plat_name,plat_def_impl->name,sizeof(plat_name));
346 PLATFORM_ATTACH(plat_obj);