beri_machdep.c revision 259265
1239671Srwatson/*-
2239671Srwatson * Copyright (c) 2006 Wojciech A. Koszek <wkoszek@FreeBSD.org>
3244899Srwatson * Copyright (c) 2012 Robert N. M. Watson
4239671Srwatson * All rights reserved.
5239671Srwatson *
6244899Srwatson * This software was developed by SRI International and the University of
7244899Srwatson * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
8244899Srwatson * ("CTSRD"), as part of the DARPA CRASH research programme.
9244899Srwatson *
10239671Srwatson * Redistribution and use in source and binary forms, with or without
11239671Srwatson * modification, are permitted provided that the following conditions
12239671Srwatson * are met:
13239671Srwatson * 1. Redistributions of source code must retain the above copyright
14239671Srwatson *    notice, this list of conditions and the following disclaimer.
15239671Srwatson * 2. Redistributions in binary form must reproduce the above copyright
16239671Srwatson *    notice, this list of conditions and the following disclaimer in the
17239671Srwatson *    documentation and/or other materials provided with the distribution.
18239671Srwatson *
19239671Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20239671Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21239671Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22239671Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23239671Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24239671Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25239671Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26239671Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27239671Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28239671Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29239671Srwatson * SUCH DAMAGE.
30239671Srwatson */
31239671Srwatson#include <sys/cdefs.h>
32239671Srwatson__FBSDID("$FreeBSD: head/sys/mips/beri/beri_machdep.c 259265 2013-12-12 17:48:33Z bz $");
33239671Srwatson
34239671Srwatson#include "opt_ddb.h"
35244899Srwatson#include "opt_platform.h"
36239671Srwatson
37239671Srwatson#include <sys/param.h>
38239671Srwatson#include <sys/conf.h>
39239671Srwatson#include <sys/kernel.h>
40239671Srwatson#include <sys/systm.h>
41239671Srwatson#include <sys/imgact.h>
42239671Srwatson#include <sys/bio.h>
43239671Srwatson#include <sys/buf.h>
44239671Srwatson#include <sys/bus.h>
45239671Srwatson#include <sys/cpu.h>
46239671Srwatson#include <sys/cons.h>
47239671Srwatson#include <sys/exec.h>
48245330Srwatson#include <sys/linker.h>
49239671Srwatson#include <sys/ucontext.h>
50239671Srwatson#include <sys/proc.h>
51239671Srwatson#include <sys/kdb.h>
52239671Srwatson#include <sys/ptrace.h>
53239671Srwatson#include <sys/reboot.h>
54239671Srwatson#include <sys/signalvar.h>
55239671Srwatson#include <sys/sysent.h>
56239671Srwatson#include <sys/sysproto.h>
57239671Srwatson#include <sys/user.h>
58239671Srwatson
59244942Srwatson#ifdef FDT
60244899Srwatson#include <dev/fdt/fdt_common.h>
61244899Srwatson#include <dev/ofw/openfirm.h>
62244942Srwatson#endif
63244899Srwatson
64239671Srwatson#include <vm/vm.h>
65239671Srwatson#include <vm/vm_object.h>
66239671Srwatson#include <vm/vm_page.h>
67239671Srwatson
68239671Srwatson#include <machine/clock.h>
69239671Srwatson#include <machine/cpu.h>
70239671Srwatson#include <machine/cpuregs.h>
71239671Srwatson#include <machine/hwfunc.h>
72239671Srwatson#include <machine/md_var.h>
73245330Srwatson#include <machine/metadata.h>
74239671Srwatson#include <machine/pmap.h>
75239671Srwatson#include <machine/trap.h>
76239671Srwatson
77239671Srwatsonextern int	*edata;
78239671Srwatsonextern int	*end;
79239671Srwatson
80239671Srwatsonvoid
81239671Srwatsonplatform_cpu_init()
82239671Srwatson{
83239671Srwatson	/* Nothing special */
84239671Srwatson}
85239671Srwatson
86239671Srwatsonstatic void
87239671Srwatsonmips_init(void)
88239671Srwatson{
89239671Srwatson	int i;
90239671Srwatson
91239671Srwatson	for (i = 0; i < 10; i++) {
92239671Srwatson		phys_avail[i] = 0;
93239671Srwatson	}
94239671Srwatson
95239671Srwatson	/* phys_avail regions are in bytes */
96239671Srwatson	phys_avail[0] = MIPS_KSEG0_TO_PHYS(kernel_kseg0_end);
97239671Srwatson	phys_avail[1] = ctob(realmem);
98239671Srwatson
99239671Srwatson	dump_avail[0] = phys_avail[0];
100239671Srwatson	dump_avail[1] = phys_avail[1];
101239671Srwatson
102239671Srwatson	physmem = realmem;
103239671Srwatson
104239671Srwatson	init_param1();
105239671Srwatson	init_param2(physmem);
106239671Srwatson	mips_cpu_init();
107239671Srwatson	pmap_bootstrap();
108239671Srwatson	mips_proc0_init();
109239671Srwatson	mutex_init();
110239671Srwatson	kdb_init();
111239671Srwatson#ifdef KDB
112239671Srwatson	if (boothowto & RB_KDB)
113239671Srwatson		kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
114239671Srwatson#endif
115239671Srwatson}
116239671Srwatson
117239671Srwatson/*
118239671Srwatson * Perform a board-level soft-reset.
119239671Srwatson */
120239671Srwatsonvoid
121239671Srwatsonplatform_reset(void)
122239671Srwatson{
123239671Srwatson
124256745Sbrooks	/* XXX SMP will likely require us to do more. */
125256745Sbrooks	__asm__ __volatile__(
126256745Sbrooks		"mfc0 $k0, $12\n\t"
127256745Sbrooks		"li $k1, 0x00100000\n\t"
128256745Sbrooks		"or $k0, $k0, $k1\n\t"
129256745Sbrooks		"mtc0 $k0, $12\n");
130256745Sbrooks	for( ; ; )
131256745Sbrooks		__asm__ __volatile("wait");
132239671Srwatson}
133239671Srwatson
134239671Srwatsonvoid
135239671Srwatsonplatform_start(__register_t a0, __register_t a1,  __register_t a2,
136239671Srwatson    __register_t a3)
137239671Srwatson{
138239671Srwatson	vm_offset_t kernend;
139239671Srwatson	uint64_t platform_counter_freq;
140239671Srwatson	int argc = a0;
141239671Srwatson	char **argv = (char **)a1;
142239671Srwatson	char **envp = (char **)a2;
143239671Srwatson	unsigned int memsize = a3;
144245330Srwatson#ifdef FDT
145245330Srwatson	vm_offset_t dtbp;
146245330Srwatson	void *kmdp;
147245330Srwatson#endif
148239671Srwatson	int i;
149239671Srwatson
150239671Srwatson	/* clear the BSS and SBSS segments */
151239671Srwatson	kernend = (vm_offset_t)&end;
152239671Srwatson	memset(&edata, 0, kernend - (vm_offset_t)(&edata));
153239671Srwatson
154239671Srwatson	mips_postboot_fixup();
155239671Srwatson
156239671Srwatson	mips_pcpu0_init();
157239671Srwatson
158245329Srwatson#ifdef FDT
159245330Srwatson	/*
160245330Srwatson	 * Find the dtb passed in by the boot loader (currently fictional).
161245330Srwatson	 */
162245330Srwatson	kmdp = preload_search_by_type("elf kernel");
163245330Srwatson	if (kmdp != NULL)
164245330Srwatson		dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
165245330Srwatson	else
166245330Srwatson		dtbp = (vm_offset_t)NULL;
167245330Srwatson
168245330Srwatson#if defined(FDT_DTB_STATIC)
169245330Srwatson	/*
170245330Srwatson	 * In case the device tree blob was not retrieved (from metadata) try
171245330Srwatson	 * to use the statically embedded one.
172245330Srwatson	 */
173245330Srwatson	if (dtbp == (vm_offset_t)NULL)
174245330Srwatson		dtbp = (vm_offset_t)&fdt_static_dtb;
175245330Srwatson#else
176245330Srwatson#error	"Non-static FDT not yet supported on BERI"
177245329Srwatson#endif
178245329Srwatson
179245329Srwatson	if (OF_install(OFW_FDT, 0) == FALSE)
180245329Srwatson		while (1);
181259265Sbz	if (OF_init((void *)dtbp) != 0)
182245329Srwatson		while (1);
183245329Srwatson#endif
184245329Srwatson
185239671Srwatson	/*
186239671Srwatson	 * XXXRW: We have no way to compare wallclock time to cycle rate on
187239671Srwatson	 * BERI, so for now assume we run at the MALTA default (100MHz).
188239671Srwatson	 */
189239671Srwatson	platform_counter_freq = MIPS_DEFAULT_HZ;
190239671Srwatson	mips_timer_early_init(platform_counter_freq);
191239671Srwatson
192239671Srwatson	cninit();
193239671Srwatson	printf("entry: platform_start()\n");
194239671Srwatson
195239671Srwatson	bootverbose = 1;
196239671Srwatson	if (bootverbose) {
197239671Srwatson		printf("cmd line: ");
198239671Srwatson		for (i = 0; i < argc; i++)
199239671Srwatson			printf("%s ", argv[i]);
200239671Srwatson		printf("\n");
201239671Srwatson
202239671Srwatson		printf("envp:\n");
203239671Srwatson		for (i = 0; envp[i]; i += 2)
204239671Srwatson			printf("\t%s = %s\n", envp[i], envp[i+1]);
205239671Srwatson
206239671Srwatson		printf("memsize = %08x\n", memsize);
207239671Srwatson	}
208239671Srwatson
209239671Srwatson	realmem = btoc(memsize);
210239671Srwatson	mips_init();
211239671Srwatson
212239671Srwatson	mips_timer_init_params(platform_counter_freq, 0);
213239671Srwatson}
214