platform.c revision 330897
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2005 Peter Grehan
5 * Copyright (c) 2009 Nathan Whitehorn
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
16 *
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
27 * SUCH DAMAGE.
28 *
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: stable/11/sys/powerpc/powerpc/platform.c 330897 2018-03-14 03:19:51Z eadler $");
33
34/*
35 * Dispatch platform calls to the appropriate platform implementation
36 * through a previously registered kernel object.
37 */
38
39#include <sys/param.h>
40#include <sys/kernel.h>
41#include <sys/lock.h>
42#include <sys/ktr.h>
43#include <sys/mutex.h>
44#include <sys/proc.h>
45#include <sys/systm.h>
46#include <sys/smp.h>
47#include <sys/sysctl.h>
48#include <sys/types.h>
49
50#include <vm/vm.h>
51#include <vm/vm_page.h>
52
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>
58
59#include "platform_if.h"
60
61static platform_def_t	*plat_def_impl;
62static platform_t	plat_obj;
63static struct kobj_ops	plat_kernel_kops;
64static struct platform_kobj	plat_kernel_obj;
65
66static char plat_name[64] = "";
67SYSCTL_STRING(_hw, OID_AUTO, platform, CTLFLAG_RD | CTLFLAG_TUN,
68    plat_name, 0, "Platform currently in use");
69
70static struct mem_region pregions[PHYS_AVAIL_SZ];
71static struct mem_region aregions[PHYS_AVAIL_SZ];
72static int npregions, naregions;
73
74/*
75 * Memory region utilities: determine if two regions overlap,
76 * and merge two overlapping regions into one
77 */
78static int
79memr_overlap(struct mem_region *r1, struct mem_region *r2)
80{
81	if ((r1->mr_start + r1->mr_size) < r2->mr_start ||
82	    (r2->mr_start + r2->mr_size) < r1->mr_start)
83		return (FALSE);
84
85	return (TRUE);
86}
87
88static void
89memr_merge(struct mem_region *from, struct mem_region *to)
90{
91	vm_offset_t end;
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;
95}
96
97/*
98 * Quick sort callout for comparing memory regions.
99 */
100static int
101mr_cmp(const void *a, const void *b)
102{
103	const struct mem_region *regiona, *regionb;
104
105	regiona = a;
106	regionb = b;
107	if (regiona->mr_start < regionb->mr_start)
108		return (-1);
109	else if (regiona->mr_start > regionb->mr_start)
110		return (1);
111	else
112		return (0);
113}
114
115void
116mem_regions(struct mem_region **phys, int *physsz, struct mem_region **avail,
117    int *availsz)
118{
119	int i, j, still_merging;
120
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);
126
127		/* Remove overlapping available regions */
128		do {
129			still_merging = FALSE;
130			for (i = 0; i < naregions; i++) {
131				if (aregions[i].mr_size == 0)
132					continue;
133				for (j = i+1; j < naregions; j++) {
134					if (aregions[j].mr_size == 0)
135						continue;
136					if (!memr_overlap(&aregions[j],
137					    &aregions[i]))
138						continue;
139
140					memr_merge(&aregions[j], &aregions[i]);
141					/* mark inactive */
142					aregions[j].mr_size = 0;
143					still_merging = TRUE;
144				}
145			}
146		} while (still_merging == TRUE);
147
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));
153				naregions--;
154				i--;
155			}
156		}
157	}
158
159	*phys = pregions;
160	*avail = aregions;
161	*physsz = npregions;
162	*availsz = naregions;
163}
164
165int
166mem_valid(vm_offset_t addr, int len)
167{
168	int i;
169
170	if (npregions == 0) {
171		struct mem_region *p, *a;
172		int na, np;
173		mem_regions(&p, &np, &a, &na);
174	}
175
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))
179			return (0);
180
181	return (EFAULT);
182}
183
184vm_offset_t
185platform_real_maxaddr(void)
186{
187	return (PLATFORM_REAL_MAXADDR(plat_obj));
188}
189
190const char *
191installed_platform()
192{
193	return (plat_def_impl->name);
194}
195
196u_long
197platform_timebase_freq(struct cpuref *cpu)
198{
199	return (PLATFORM_TIMEBASE_FREQ(plat_obj, cpu));
200}
201
202/*
203 * Put the current CPU, as last step in suspend, to sleep
204 */
205void
206platform_sleep()
207{
208        PLATFORM_SLEEP(plat_obj);
209}
210
211int
212platform_smp_first_cpu(struct cpuref *cpu)
213{
214	return (PLATFORM_SMP_FIRST_CPU(plat_obj, cpu));
215}
216
217int
218platform_smp_next_cpu(struct cpuref *cpu)
219{
220	return (PLATFORM_SMP_NEXT_CPU(plat_obj, cpu));
221}
222
223int
224platform_smp_get_bsp(struct cpuref *cpu)
225{
226	return (PLATFORM_SMP_GET_BSP(plat_obj, cpu));
227}
228
229int
230platform_smp_start_cpu(struct pcpu *cpu)
231{
232	return (PLATFORM_SMP_START_CPU(plat_obj, cpu));
233}
234
235void
236platform_smp_ap_init()
237{
238	PLATFORM_SMP_AP_INIT(plat_obj);
239}
240
241#ifdef SMP
242struct cpu_group *
243cpu_topo(void)
244{
245        return (PLATFORM_SMP_TOPO(plat_obj));
246}
247#endif
248
249/*
250 * Reset back to firmware.
251 */
252void
253cpu_reset()
254{
255        PLATFORM_RESET(plat_obj);
256}
257
258int
259cpu_idle_wakeup(int cpu)
260{
261	return (PLATFORM_IDLE_WAKEUP(plat_obj, cpu));
262}
263
264void
265platform_cpu_idle(int cpu)
266{
267
268	PLATFORM_IDLE(plat_obj, cpu);
269}
270
271/*
272 * Platform install routines. Highest priority wins, using the same
273 * algorithm as bus attachment.
274 */
275SET_DECLARE(platform_set, platform_def_t);
276
277void
278platform_probe_and_attach()
279{
280	platform_def_t	**platpp, *platp;
281	int		prio, best_prio;
282
283	plat_obj = &plat_kernel_obj;
284	best_prio = 0;
285
286	/*
287	 * Try to locate the best platform kobj
288	 */
289	SET_FOREACH(platpp, platform_set) {
290		platp = *platpp;
291
292		/*
293		 * Take care of compiling the selected class, and
294		 * then statically initialise the MMU object
295		 */
296		kobj_class_compile_static(platp, &plat_kernel_kops);
297		kobj_init_static((kobj_t)plat_obj, platp);
298
299		prio = PLATFORM_PROBE(plat_obj);
300
301		/* Check for errors */
302		if (prio > 0)
303			continue;
304
305		/*
306		 * Check if this module was specifically requested through
307		 * the loader tunable we provide.
308		 */
309		if (strcmp(platp->name,plat_name) == 0) {
310			plat_def_impl = platp;
311			break;
312		}
313
314		/* Otherwise, see if it is better than our current best */
315		if (plat_def_impl == NULL || prio > best_prio) {
316			best_prio = prio;
317			plat_def_impl = platp;
318		}
319
320		/*
321		 * We can't free the KOBJ, since it is static. Reset the ops
322		 * member of this class so that we can come back later.
323		 */
324		platp->ops = NULL;
325	}
326
327	if (plat_def_impl == NULL)
328		panic("No platform module found!");
329
330	/*
331	 * Recompile to make sure we ended with the
332	 * correct one, and then attach.
333	 */
334
335	kobj_class_compile_static(plat_def_impl, &plat_kernel_kops);
336	kobj_init_static((kobj_t)plat_obj, plat_def_impl);
337
338	strlcpy(plat_name,plat_def_impl->name,sizeof(plat_name));
339
340	PLATFORM_ATTACH(plat_obj);
341}
342
343