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_param.h>
52 #include <vm/vm_page.h>
53 #include <vm/vm_phys.h>
55 #include <machine/cpu.h>
56 #include <machine/md_var.h>
57 #include <machine/ofw_machdep.h>
58 #include <machine/platform.h>
59 #include <machine/platformvar.h>
60 #include <machine/smp.h>
61 #include <machine/vmparam.h>
63 #include "platform_if.h"
65 static platform_def_t *plat_def_impl;
66 static platform_t plat_obj;
67 static struct kobj_ops plat_kernel_kops;
68 static struct platform_kobj plat_kernel_obj;
70 static char plat_name[64] = "";
71 SYSCTL_STRING(_hw, OID_AUTO, platform, CTLFLAG_RD | CTLFLAG_TUN,
72 plat_name, 0, "Platform currently in use");
74 static struct mem_affinity mem_info[VM_PHYSSEG_MAX + 1];
75 static int vm_locality_table[MAXMEMDOM * MAXMEMDOM];
76 static struct mem_region pregions[PHYS_AVAIL_SZ];
77 static struct numa_mem_region numa_pregions[PHYS_AVAIL_SZ];
78 static struct mem_region aregions[PHYS_AVAIL_SZ];
79 static int nnumapregions, npregions, naregions;
82 * Memory region utilities: determine if two regions overlap,
83 * and merge two overlapping regions into one
86 memr_overlap(struct mem_region *r1, struct mem_region *r2)
88 if ((r1->mr_start + r1->mr_size) < r2->mr_start ||
89 (r2->mr_start + r2->mr_size) < r1->mr_start)
96 memr_merge(struct mem_region *from, struct mem_region *to)
99 end = uqmax(to->mr_start + to->mr_size, from->mr_start + from->mr_size);
100 to->mr_start = uqmin(from->mr_start, to->mr_start);
101 to->mr_size = end - to->mr_start;
105 * Quick sort callout for comparing memory regions.
108 mr_cmp(const void *a, const void *b)
110 const struct mem_region *regiona, *regionb;
114 if (regiona->mr_start < regionb->mr_start)
116 else if (regiona->mr_start > regionb->mr_start)
123 numa_mem_regions(struct numa_mem_region **phys, int *physsz)
125 struct mem_affinity *mi;
126 int i, j, maxdom, ndomain, offset;
129 PLATFORM_NUMA_MEM_REGIONS(plat_obj, numa_pregions, &nnumapregions);
132 *physsz = nnumapregions;
134 *phys = numa_pregions;
135 if (physsz == NULL || phys == NULL) {
136 printf("unset value\n");
140 for (i = 0; i < nnumapregions; i++)
141 if (numa_pregions[i].mr_domain > maxdom)
142 maxdom = numa_pregions[i].mr_domain;
145 for (i = 0; i < nnumapregions; i++, mi++) {
146 mi->start = numa_pregions[i].mr_start;
147 mi->end = numa_pregions[i].mr_start + numa_pregions[i].mr_size;
148 mi->domain = numa_pregions[i].mr_domain;
151 vm_locality_table[offset] = 10;
152 ndomain = maxdom + 1;
154 for (i = 0; i < ndomain; i++) {
155 for (j = 0; j < ndomain; j++) {
157 * Not sure what these values should actually be
160 vm_locality_table[offset] = 10;
162 vm_locality_table[offset] = 21;
167 vm_phys_register_domains(ndomain, mem_info, vm_locality_table);
171 mem_regions(struct mem_region **phys, int *physsz, struct mem_region **avail,
174 int i, j, still_merging;
176 if (npregions == 0) {
177 PLATFORM_MEM_REGIONS(plat_obj, pregions, &npregions,
178 aregions, &naregions);
179 qsort(pregions, npregions, sizeof(*pregions), mr_cmp);
180 qsort(aregions, naregions, sizeof(*aregions), mr_cmp);
182 /* Remove overlapping available regions */
184 still_merging = FALSE;
185 for (i = 0; i < naregions; i++) {
186 if (aregions[i].mr_size == 0)
188 for (j = i+1; j < naregions; j++) {
189 if (aregions[j].mr_size == 0)
191 if (!memr_overlap(&aregions[j],
195 memr_merge(&aregions[j], &aregions[i]);
197 aregions[j].mr_size = 0;
198 still_merging = TRUE;
201 } while (still_merging == TRUE);
203 /* Collapse zero-length available regions */
204 for (i = 0; i < naregions; i++) {
205 if (aregions[i].mr_size == 0) {
206 memcpy(&aregions[i], &aregions[i+1],
207 (naregions - i - 1)*sizeof(*aregions));
221 *availsz = naregions;
225 mem_valid(vm_offset_t addr, int len)
229 if (npregions == 0) {
230 struct mem_region *p, *a;
232 mem_regions(&p, &np, &a, &na);
235 for (i = 0; i < npregions; i++)
236 if ((addr >= pregions[i].mr_start)
237 && (addr + len <= pregions[i].mr_start + pregions[i].mr_size))
244 platform_real_maxaddr(void)
246 return (PLATFORM_REAL_MAXADDR(plat_obj));
252 return (plat_def_impl->name);
256 platform_timebase_freq(struct cpuref *cpu)
258 return (PLATFORM_TIMEBASE_FREQ(plat_obj, cpu));
262 * Put the current CPU, as last step in suspend, to sleep
267 PLATFORM_SLEEP(plat_obj);
271 platform_smp_first_cpu(struct cpuref *cpu)
273 return (PLATFORM_SMP_FIRST_CPU(plat_obj, cpu));
277 platform_smp_next_cpu(struct cpuref *cpu)
279 return (PLATFORM_SMP_NEXT_CPU(plat_obj, cpu));
283 platform_smp_get_bsp(struct cpuref *cpu)
285 return (PLATFORM_SMP_GET_BSP(plat_obj, cpu));
289 platform_smp_start_cpu(struct pcpu *cpu)
291 return (PLATFORM_SMP_START_CPU(plat_obj, cpu));
295 platform_smp_ap_init()
297 PLATFORM_SMP_AP_INIT(plat_obj);
301 platform_smp_probe_threads(void)
303 PLATFORM_SMP_PROBE_THREADS(plat_obj);
310 return (PLATFORM_SMP_TOPO(plat_obj));
315 platform_node_numa_domain(phandle_t node)
317 return (PLATFORM_NODE_NUMA_DOMAIN(plat_obj, node));
321 * Reset back to firmware.
326 PLATFORM_RESET(plat_obj);
329 void platform_smp_timebase_sync(u_long tb, int ap)
332 PLATFORM_SMP_TIMEBASE_SYNC(plat_obj, tb, ap);
336 * Platform install routines. Highest priority wins, using the same
337 * algorithm as bus attachment.
339 SET_DECLARE(platform_set, platform_def_t);
342 platform_probe_and_attach()
344 platform_def_t **platpp, *platp;
347 plat_obj = &plat_kernel_obj;
351 * Try to locate the best platform kobj
353 SET_FOREACH(platpp, platform_set) {
357 * Take care of compiling the selected class, and
358 * then statically initialise the MMU object
360 kobj_class_compile_static(platp, &plat_kernel_kops);
361 kobj_init_static((kobj_t)plat_obj, platp);
363 prio = PLATFORM_PROBE(plat_obj);
365 /* Check for errors */
370 * Check if this module was specifically requested through
371 * the loader tunable we provide.
373 if (strcmp(platp->name,plat_name) == 0) {
374 plat_def_impl = platp;
378 /* Otherwise, see if it is better than our current best */
379 if (plat_def_impl == NULL || prio > best_prio) {
381 plat_def_impl = platp;
385 * We can't free the KOBJ, since it is static. Reset the ops
386 * member of this class so that we can come back later.
391 if (plat_def_impl == NULL)
392 panic("No platform module found!");
395 * Recompile to make sure we ended with the
396 * correct one, and then attach.
399 kobj_class_compile_static(plat_def_impl, &plat_kernel_kops);
400 kobj_init_static((kobj_t)plat_obj, plat_def_impl);
402 strlcpy(plat_name,plat_def_impl->name,sizeof(plat_name));
404 PLATFORM_ATTACH(plat_obj);