platform.c revision 262675
1/*-
2 * Copyright (c) 2005 Peter Grehan
3 * Copyright (c) 2009 Nathan Whitehorn
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 *
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
25 * SUCH DAMAGE.
26 *
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: stable/10/sys/powerpc/powerpc/platform.c 262675 2014-03-02 02:35:46Z jhibbits $");
31
32/*
33 * Dispatch platform calls to the appropriate platform implementation
34 * through a previously registered kernel object.
35 */
36
37#include <sys/param.h>
38#include <sys/kernel.h>
39#include <sys/lock.h>
40#include <sys/ktr.h>
41#include <sys/mutex.h>
42#include <sys/systm.h>
43#include <sys/smp.h>
44#include <sys/sysctl.h>
45#include <sys/types.h>
46
47#include <vm/vm.h>
48#include <vm/vm_page.h>
49
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>
55
56#include "platform_if.h"
57
58static platform_def_t	*plat_def_impl;
59static platform_t	plat_obj;
60static struct kobj_ops	plat_kernel_kops;
61static struct platform_kobj	plat_kernel_obj;
62
63static char plat_name[64] = "";
64SYSCTL_STRING(_hw, OID_AUTO, platform, CTLFLAG_RD | CTLFLAG_TUN,
65    plat_name, 0, "Platform currently in use");
66
67static struct mem_region *pregions = NULL;
68static struct mem_region *aregions = NULL;
69static int npregions, naregions;
70
71void
72mem_regions(struct mem_region **phys, int *physsz, struct mem_region **avail,
73    int *availsz)
74{
75	if (pregions == NULL)
76		PLATFORM_MEM_REGIONS(plat_obj, &pregions, &npregions,
77		    &aregions, &naregions);
78
79	*phys = pregions;
80	*avail = aregions;
81	*physsz = npregions;
82	*availsz = naregions;
83}
84
85int
86mem_valid(vm_offset_t addr, int len)
87{
88	int i;
89
90	if (pregions == NULL)
91		PLATFORM_MEM_REGIONS(plat_obj, &pregions, &npregions,
92		    &aregions, &naregions);
93
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))
97			return (0);
98
99	return (EFAULT);
100}
101
102vm_offset_t
103platform_real_maxaddr(void)
104{
105	return (PLATFORM_REAL_MAXADDR(plat_obj));
106}
107
108const char *
109installed_platform()
110{
111	return (plat_def_impl->name);
112}
113
114u_long
115platform_timebase_freq(struct cpuref *cpu)
116{
117	return (PLATFORM_TIMEBASE_FREQ(plat_obj, cpu));
118}
119
120/*
121 * Put the current CPU, as last step in suspend, to sleep
122 */
123void
124platform_sleep()
125{
126        PLATFORM_SLEEP(plat_obj);
127}
128
129int
130platform_smp_first_cpu(struct cpuref *cpu)
131{
132	return (PLATFORM_SMP_FIRST_CPU(plat_obj, cpu));
133}
134
135int
136platform_smp_next_cpu(struct cpuref *cpu)
137{
138	return (PLATFORM_SMP_NEXT_CPU(plat_obj, cpu));
139}
140
141int
142platform_smp_get_bsp(struct cpuref *cpu)
143{
144	return (PLATFORM_SMP_GET_BSP(plat_obj, cpu));
145}
146
147int
148platform_smp_start_cpu(struct pcpu *cpu)
149{
150	return (PLATFORM_SMP_START_CPU(plat_obj, cpu));
151}
152
153void
154platform_smp_ap_init()
155{
156	PLATFORM_SMP_AP_INIT(plat_obj);
157}
158
159#ifdef SMP
160struct cpu_group *
161cpu_topo(void)
162{
163        return (PLATFORM_SMP_TOPO(plat_obj));
164}
165#endif
166
167/*
168 * Reset back to firmware.
169 */
170void
171cpu_reset()
172{
173        PLATFORM_RESET(plat_obj);
174}
175
176/*
177 * Platform install routines. Highest priority wins, using the same
178 * algorithm as bus attachment.
179 */
180SET_DECLARE(platform_set, platform_def_t);
181
182void
183platform_probe_and_attach()
184{
185	platform_def_t	**platpp, *platp;
186	int		prio, best_prio;
187
188	plat_obj = &plat_kernel_obj;
189	best_prio = 0;
190
191	/*
192	 * Try to locate the best platform kobj
193	 */
194	SET_FOREACH(platpp, platform_set) {
195		platp = *platpp;
196
197		/*
198		 * Take care of compiling the selected class, and
199		 * then statically initialise the MMU object
200		 */
201		kobj_class_compile_static(platp, &plat_kernel_kops);
202		kobj_init_static((kobj_t)plat_obj, platp);
203
204		prio = PLATFORM_PROBE(plat_obj);
205
206		/* Check for errors */
207		if (prio > 0)
208			continue;
209
210		/*
211		 * Check if this module was specifically requested through
212		 * the loader tunable we provide.
213		 */
214		if (strcmp(platp->name,plat_name) == 0) {
215			plat_def_impl = platp;
216			break;
217		}
218
219		/* Otherwise, see if it is better than our current best */
220		if (plat_def_impl == NULL || prio > best_prio) {
221			best_prio = prio;
222			plat_def_impl = platp;
223		}
224
225		/*
226		 * We can't free the KOBJ, since it is static. Reset the ops
227		 * member of this class so that we can come back later.
228		 */
229		platp->ops = NULL;
230	}
231
232	if (plat_def_impl == NULL)
233		panic("No platform module found!");
234
235	/*
236	 * Recompile to make sure we ended with the
237	 * correct one, and then attach.
238	 */
239
240	kobj_class_compile_static(plat_def_impl, &plat_kernel_kops);
241	kobj_init_static((kobj_t)plat_obj, plat_def_impl);
242
243	strlcpy(plat_name,plat_def_impl->name,sizeof(plat_name));
244
245	PLATFORM_ATTACH(plat_obj);
246}
247
248