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