beri_machdep.c revision 330897
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2006 Wojciech A. Koszek <wkoszek@FreeBSD.org>
5 * Copyright (c) 2012-2014 Robert N. M. Watson
6 * All rights reserved.
7 *
8 * This software was developed by SRI International and the University of
9 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
10 * ("CTSRD"), as part of the DARPA CRASH research programme.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: stable/11/sys/mips/beri/beri_machdep.c 330897 2018-03-14 03:19:51Z eadler $");
35
36#include "opt_ddb.h"
37#include "opt_platform.h"
38
39#include <sys/param.h>
40#include <sys/conf.h>
41#include <sys/kernel.h>
42#include <sys/systm.h>
43#include <sys/imgact.h>
44#include <sys/bio.h>
45#include <sys/buf.h>
46#include <sys/bus.h>
47#include <sys/cpu.h>
48#include <sys/cons.h>
49#include <sys/exec.h>
50#include <sys/linker.h>
51#include <sys/ucontext.h>
52#include <sys/proc.h>
53#include <sys/kdb.h>
54#include <sys/ptrace.h>
55#include <sys/reboot.h>
56#include <sys/signalvar.h>
57#include <sys/sysent.h>
58#include <sys/sysproto.h>
59#include <sys/user.h>
60
61#ifdef FDT
62#include <dev/fdt/fdt_common.h>
63#include <dev/ofw/openfirm.h>
64#endif
65
66#include <vm/vm.h>
67#include <vm/vm_object.h>
68#include <vm/vm_page.h>
69
70#include <machine/bootinfo.h>
71#include <machine/clock.h>
72#include <machine/cpu.h>
73#include <machine/cpuregs.h>
74#include <machine/hwfunc.h>
75#include <machine/md_var.h>
76#include <machine/metadata.h>
77#include <machine/pmap.h>
78#include <machine/trap.h>
79
80extern int	*edata;
81extern int	*end;
82
83void
84platform_cpu_init()
85{
86	/* Nothing special */
87}
88
89static void
90mips_init(void)
91{
92	int i;
93#ifdef FDT
94	struct mem_region mr[FDT_MEM_REGIONS];
95	uint64_t val;
96	int mr_cnt;
97	int j;
98#endif
99
100	for (i = 0; i < 10; i++) {
101		phys_avail[i] = 0;
102	}
103
104	/* phys_avail regions are in bytes */
105	phys_avail[0] = MIPS_KSEG0_TO_PHYS(kernel_kseg0_end);
106	phys_avail[1] = ctob(realmem);
107
108	dump_avail[0] = phys_avail[0];
109	dump_avail[1] = phys_avail[1];
110
111	physmem = realmem;
112
113#ifdef FDT
114	if (fdt_get_mem_regions(mr, &mr_cnt, &val) == 0) {
115
116		physmem = btoc(val);
117
118		KASSERT((phys_avail[0] >= mr[0].mr_start) && \
119			(phys_avail[0] < (mr[0].mr_start + mr[0].mr_size)),
120			("First region is not within FDT memory range"));
121
122		/* Limit size of the first region */
123		phys_avail[1] = (mr[0].mr_start + MIN(mr[0].mr_size, ctob(realmem)));
124		dump_avail[1] = phys_avail[1];
125
126		/* Add the rest of regions */
127		for (i = 1, j = 2; i < mr_cnt; i++, j+=2) {
128			phys_avail[j] = mr[i].mr_start;
129			phys_avail[j+1] = (mr[i].mr_start + mr[i].mr_size);
130			dump_avail[j] = phys_avail[j];
131			dump_avail[j+1] = phys_avail[j+1];
132		}
133	}
134#endif
135
136	init_param1();
137	init_param2(physmem);
138	mips_cpu_init();
139	pmap_bootstrap();
140	mips_proc0_init();
141	mutex_init();
142	kdb_init();
143#ifdef KDB
144	if (boothowto & RB_KDB)
145		kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
146#endif
147}
148
149/*
150 * Perform a board-level soft-reset.
151 */
152void
153platform_reset(void)
154{
155
156	/* XXX SMP will likely require us to do more. */
157	__asm__ __volatile__(
158		"mfc0 $k0, $12\n\t"
159		"li $k1, 0x00100000\n\t"
160		"or $k0, $k0, $k1\n\t"
161		"mtc0 $k0, $12\n");
162	for( ; ; )
163		__asm__ __volatile("wait");
164}
165
166#ifdef FDT
167/* Parse cmd line args as env - copied from xlp_machdep. */
168/* XXX-BZ this should really be centrally provided for all (boot) code. */
169static void
170_parse_bootargs(char *cmdline)
171{
172	char *n, *v;
173
174	while ((v = strsep(&cmdline, " \n")) != NULL) {
175		if (*v == '\0')
176			continue;
177		if (*v == '-') {
178			while (*v != '\0') {
179				v++;
180				switch (*v) {
181				case 'a': boothowto |= RB_ASKNAME; break;
182				/* Someone should simulate that ;-) */
183				case 'C': boothowto |= RB_CDROM; break;
184				case 'd': boothowto |= RB_KDB; break;
185				case 'D': boothowto |= RB_MULTIPLE; break;
186				case 'm': boothowto |= RB_MUTE; break;
187				case 'g': boothowto |= RB_GDB; break;
188				case 'h': boothowto |= RB_SERIAL; break;
189				case 'p': boothowto |= RB_PAUSE; break;
190				case 'r': boothowto |= RB_DFLTROOT; break;
191				case 's': boothowto |= RB_SINGLE; break;
192				case 'v': boothowto |= RB_VERBOSE; break;
193				}
194			}
195		} else {
196			n = strsep(&v, "=");
197			if (v == NULL)
198				kern_setenv(n, "1");
199			else
200				kern_setenv(n, v);
201		}
202	}
203}
204#endif
205
206void
207platform_start(__register_t a0, __register_t a1,  __register_t a2,
208    __register_t a3)
209{
210	struct bootinfo *bootinfop;
211	vm_offset_t kernend;
212	uint64_t platform_counter_freq;
213	int argc = a0;
214	char **argv = (char **)a1;
215	char **envp = (char **)a2;
216	long memsize;
217#ifdef FDT
218	char buf[2048];		/* early stack supposedly big enough */
219	vm_offset_t dtbp;
220	phandle_t chosen;
221	void *kmdp;
222#endif
223	int i;
224
225	/* clear the BSS and SBSS segments */
226	kernend = (vm_offset_t)&end;
227	memset(&edata, 0, kernend - (vm_offset_t)(&edata));
228
229	mips_postboot_fixup();
230
231	mips_pcpu0_init();
232
233	/*
234	 * Over time, we've changed out boot-time binary interface for the
235	 * kernel.  Miniboot simply provides a 'memsize' in a3, whereas the
236	 * FreeBSD boot loader provides a 'bootinfo *' in a3.  While slightly
237	 * grody, we support both here by detecting 'pointer-like' values in
238	 * a3 and assuming physical memory can never be that back.
239	 *
240	 * XXXRW: Pull more values than memsize out of bootinfop -- e.g.,
241	 * module information.
242	 */
243	if (a3 >= 0x9800000000000000ULL) {
244		bootinfop = (void *)a3;
245		memsize = bootinfop->bi_memsize;
246		preload_metadata = (caddr_t)bootinfop->bi_modulep;
247	} else {
248		bootinfop = NULL;
249		memsize = a3;
250	}
251
252#ifdef FDT
253	/*
254	 * Find the dtb passed in by the boot loader (currently fictional).
255	 */
256	kmdp = preload_search_by_type("elf kernel");
257	dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
258
259#if defined(FDT_DTB_STATIC)
260	/*
261	 * In case the device tree blob was not retrieved (from metadata) try
262	 * to use the statically embedded one.
263	 */
264	if (dtbp == (vm_offset_t)NULL)
265		dtbp = (vm_offset_t)&fdt_static_dtb;
266#else
267#error	"Non-static FDT not yet supported on BERI"
268#endif
269
270	if (OF_install(OFW_FDT, 0) == FALSE)
271		while (1);
272	if (OF_init((void *)dtbp) != 0)
273		while (1);
274
275	/*
276	 * Configure more boot-time parameters passed in by loader.
277	 */
278	boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
279	init_static_kenv(MD_FETCH(kmdp, MODINFOMD_ENVP, char *), 0);
280
281	/*
282	 * Get bootargs from FDT if specified.
283	 */
284	chosen = OF_finddevice("/chosen");
285	if (OF_getprop(chosen, "bootargs", buf, sizeof(buf)) != -1)
286		_parse_bootargs(buf);
287#endif
288
289	/*
290	 * XXXRW: We have no way to compare wallclock time to cycle rate on
291	 * BERI, so for now assume we run at the MALTA default (100MHz).
292	 */
293	platform_counter_freq = MIPS_DEFAULT_HZ;
294	mips_timer_early_init(platform_counter_freq);
295
296	cninit();
297	printf("entry: platform_start()\n");
298
299	bootverbose = 1;
300	if (bootverbose) {
301		printf("cmd line: ");
302		for (i = 0; i < argc; i++)
303			printf("%s ", argv[i]);
304		printf("\n");
305
306		printf("envp:\n");
307		for (i = 0; envp[i]; i += 2)
308			printf("\t%s = %s\n", envp[i], envp[i+1]);
309
310		if (bootinfop != NULL)
311			printf("bootinfo found at %p\n", bootinfop);
312
313		printf("memsize = %p\n", (void *)memsize);
314	}
315
316	realmem = btoc(memsize);
317	mips_init();
318
319	mips_timer_init_params(platform_counter_freq, 0);
320}
321