xref: /netbsd-src/sys/arch/arm/arm32/arm32_boot.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: arm32_boot.c,v 1.5 2013/08/18 06:28:18 matt Exp $	*/
2 
3 /*
4  * Copyright (c) 2002, 2003, 2005  Genetec Corporation.  All rights reserved.
5  * Written by Hiroyuki Bessho for Genetec Corporation.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of Genetec Corporation may not be used to endorse or
16  *    promote products derived from this software without specific prior
17  *    written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GENETEC CORPORATION
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  * Copyright (c) 2001 Wasabi Systems, Inc.
32  * All rights reserved.
33  *
34  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. All advertising materials mentioning features or use of this software
45  *    must display the following acknowledgement:
46  *	This product includes software developed for the NetBSD Project by
47  *	Wasabi Systems, Inc.
48  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
49  *    or promote products derived from this software without specific prior
50  *    written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
54  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
55  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
56  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
57  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
58  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
59  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
60  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
61  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
62  * POSSIBILITY OF SUCH DAMAGE.
63  *
64  * Copyright (c) 1997,1998 Mark Brinicombe.
65  * Copyright (c) 1997,1998 Causality Limited.
66  * All rights reserved.
67  *
68  * Redistribution and use in source and binary forms, with or without
69  * modification, are permitted provided that the following conditions
70  * are met:
71  * 1. Redistributions of source code must retain the above copyright
72  *    notice, this list of conditions and the following disclaimer.
73  * 2. Redistributions in binary form must reproduce the above copyright
74  *    notice, this list of conditions and the following disclaimer in the
75  *    documentation and/or other materials provided with the distribution.
76  * 3. All advertising materials mentioning features or use of this software
77  *    must display the following acknowledgement:
78  *	This product includes software developed by Mark Brinicombe
79  *	for the NetBSD Project.
80  * 4. The name of the company nor the name of the author may be used to
81  *    endorse or promote products derived from this software without specific
82  *    prior written permission.
83  *
84  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
85  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
86  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
87  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
88  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
89  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
90  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
91  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
92  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
93  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
94  * SUCH DAMAGE.
95  *
96  * Copyright (c) 2007 Microsoft
97  * All rights reserved.
98  *
99  * Redistribution and use in source and binary forms, with or without
100  * modification, are permitted provided that the following conditions
101  * are met:
102  * 1. Redistributions of source code must retain the above copyright
103  *    notice, this list of conditions and the following disclaimer.
104  * 2. Redistributions in binary form must reproduce the above copyright
105  *    notice, this list of conditions and the following disclaimer in the
106  *    documentation and/or other materials provided with the distribution.
107  * 3. All advertising materials mentioning features or use of this software
108  *    must display the following acknowledgement:
109  *	This product includes software developed by Microsoft
110  *
111  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
112  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
113  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
114  * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTERS BE LIABLE FOR ANY DIRECT,
115  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
116  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
117  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
118  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
119  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
120  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
121  * SUCH DAMAGE.
122  */
123 
124 #include <sys/cdefs.h>
125 
126 __KERNEL_RCSID(1, "$NetBSD: arm32_boot.c,v 1.5 2013/08/18 06:28:18 matt Exp $");
127 
128 #include "opt_ddb.h"
129 #include "opt_kgdb.h"
130 
131 #include <sys/param.h>
132 #include <sys/reboot.h>
133 #include <sys/cpu.h>
134 #include <sys/intr.h>
135 
136 #include <uvm/uvm_extern.h>
137 
138 #include <arm/locore.h>
139 #include <arm/undefined.h>
140 #include <arm/arm32/machdep.h>
141 
142 #include <machine/db_machdep.h>
143 #include <ddb/db_extern.h>
144 
145 #include <machine/bootconfig.h>
146 
147 #ifdef KGDB
148 #include <sys/kgdb.h>
149 #endif
150 
151 vaddr_t
152 initarm_common(vaddr_t kvm_base, vsize_t kvm_size,
153 	const struct boot_physmem *bp, size_t nbp)
154 {
155 	struct bootmem_info * const bmi = &bootmem_info;
156 
157 #ifdef VERBOSE_INIT_ARM
158 	printf("nfreeblocks = %u, free_pages = %d (%#x)\n",
159 	    bmi->bmi_nfreeblocks, bmi->bmi_freepages,
160 	    bmi->bmi_freepages);
161 #endif
162 
163 	/*
164 	 * Moved from cpu_startup() as data_abort_handler() references
165 	 * this during uvm init.
166 	 */
167 	uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
168 
169 #ifdef VERBOSE_INIT_ARM
170 	printf("bootstrap done.\n");
171 #endif
172 
173 #ifdef VERBOSE_INIT_ARM
174 	printf("vectors");
175 #endif
176 	arm32_vector_init(systempage.pv_va, ARM_VEC_ALL);
177 #ifdef VERBOSE_INIT_ARM
178 	printf(" %#"PRIxVADDR"\n", vector_page);
179 #endif
180 
181 	/*
182 	 * Pages were allocated during the secondary bootstrap for the
183 	 * stacks for different CPU modes.
184 	 * We must now set the r13 registers in the different CPU modes to
185 	 * point to these stacks.
186 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
187 	 * of the stack memory.
188 	 */
189 #ifdef VERBOSE_INIT_ARM
190 	printf("init subsystems: stacks ");
191 #endif
192 	set_stackptr(PSR_FIQ32_MODE,
193 	    fiqstack.pv_va + FIQ_STACK_SIZE * PAGE_SIZE);
194 	set_stackptr(PSR_IRQ32_MODE,
195 	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
196 	set_stackptr(PSR_ABT32_MODE,
197 	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
198 	set_stackptr(PSR_UND32_MODE,
199 	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
200 
201 	/*
202 	 * Well we should set a data abort handler.
203 	 * Once things get going this will change as we will need a proper
204 	 * handler.
205 	 * Until then we will use a handler that just panics but tells us
206 	 * why.
207 	 * Initialisation of the vectors will just panic on a data abort.
208 	 * This just fills in a slightly better one.
209 	 */
210 #ifdef VERBOSE_INIT_ARM
211 	printf("vectors ");
212 #endif
213 	data_abort_handler_address = (u_int)data_abort_handler;
214 	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
215 	undefined_handler_address = (u_int)undefinedinstruction_bounce;
216 
217 	/* Initialise the undefined instruction handlers */
218 #ifdef VERBOSE_INIT_ARM
219 	printf("undefined ");
220 #endif
221 	undefined_init();
222 
223 	/* Load memory into UVM. */
224 #ifdef VERBOSE_INIT_ARM
225 	printf("page ");
226 #endif
227 	uvm_setpagesize();	/* initialize PAGE_SIZE-dependent variables */
228 
229 #ifdef VERBOSE_INIT_ARM
230 	printf("pmap_physload ");
231 #endif
232 	KASSERT(bp != NULL || nbp == 0);
233 	KASSERT(bp == NULL || nbp != 0);
234 
235 	for (size_t i = 0; i < bmi->bmi_nfreeblocks; i++) {
236 		pv_addr_t * const pv = &bmi->bmi_freeblocks[i];
237 		paddr_t start = atop(pv->pv_pa);
238 		const paddr_t end = start + atop(pv->pv_size);
239 
240 		while (start < end) {
241 			int vm_freelist = VM_FREELIST_DEFAULT;
242 			paddr_t segend = end;
243 			/*
244 			 * This assumes the bp list is sorted in ascending
245 			 * order.
246 			 */
247 			for (size_t j = 0; j < nbp; j++) {
248 				paddr_t bp_start = bp[j].bp_start;
249 				paddr_t bp_end = bp_start + bp[j].bp_pages;
250 				if (start < bp_start) {
251 					if (segend > bp_start) {
252 						segend = bp_start;
253 					}
254 					break;
255 				}
256 				if (start < bp_end) {
257 					if (segend > bp_end) {
258 						segend = bp_end;
259 					}
260 					vm_freelist = bp[j].bp_freelist;
261 					break;
262 				}
263 			}
264 
265 			uvm_page_physload(start, segend, start, segend,
266 			    vm_freelist);
267 			start = segend;
268 		}
269 	}
270 
271 	/* Boot strap pmap telling it where the kernel page table is */
272 #ifdef VERBOSE_INIT_ARM
273 	printf("pmap ");
274 #endif
275 	pmap_bootstrap(kvm_base, kvm_base + kvm_size);
276 
277 #ifdef __HAVE_MEMORY_DISK__
278 	md_root_setconf(memory_disk, sizeof memory_disk);
279 #endif
280 
281 #ifdef BOOTHOWTO
282 	boothowto |= BOOTHOWTO;
283 #endif
284 
285 #ifdef KGDB
286 	if (boothowto & RB_KDB) {
287 		kgdb_debug_init = 1;
288 		kgdb_connect(1);
289 	}
290 #endif
291 
292 #ifdef DDB
293 	db_machine_init();
294 	ddb_init(0, NULL, NULL);
295 
296 	if (boothowto & RB_KDB)
297 		Debugger();
298 #endif
299 
300 #ifdef VERBOSE_INIT_ARM
301 	printf("done.\n");
302 #endif
303 
304 	/* We return the new stack pointer address */
305 	return kernelstack.pv_va + USPACE_SVC_STACK_TOP;
306 }
307 
308 #ifdef MULTIPROCESSOR
309 /*
310  * When we are called, the MMU and caches are on and we are running on the stack
311  * of the idlelwp for this cpu.
312  */
313 void
314 cpu_hatch(struct cpu_info *ci, cpuid_t cpuid, void (*md_cpu_init)(struct cpu_info *))
315 {
316 	KASSERT(cpu_index(ci) == cpuid);
317 
318 	/*
319 	 * Raise our IPL to the max
320 	 */
321 	splhigh();
322 
323 	printf("%s(%s): ", __func__, ci->ci_data.cpu_name);
324 
325 	/*
326 	 * Make sure we have the right vector page.
327 	 */
328 	printf(" vectors");
329 	arm32_vector_init(systempage.pv_va, ARM_VEC_ALL);
330 
331 	/*
332 	 * Initialize the stack for each mode (we are already running on the SVC32
333 	 * stack of the idlelwp).
334 	 */
335 	printf(" stacks");
336 	set_stackptr(PSR_FIQ32_MODE,
337 	    fiqstack.pv_va + cpu_index(ci) * FIQ_STACK_SIZE * PAGE_SIZE);
338 	set_stackptr(PSR_IRQ32_MODE,
339 	    irqstack.pv_va + cpu_index(ci) * IRQ_STACK_SIZE * PAGE_SIZE);
340 	set_stackptr(PSR_ABT32_MODE,
341 	    abtstack.pv_va + cpu_index(ci) * ABT_STACK_SIZE * PAGE_SIZE);
342 	set_stackptr(PSR_UND32_MODE,
343 	    undstack.pv_va + cpu_index(ci) * UND_STACK_SIZE * PAGE_SIZE);
344 
345 #if 0
346 	/*
347 	 * Now that we are going to apart of the kernel,
348 	 * take out the kernel lock.
349 	 */
350 	printf(" kernel_lock");
351 	KERNEL_LOCK(1, NULL);
352 #endif
353 #ifdef CPU_CORTEX
354 	if (CPU_ID_CORTEX_P(ci->ci_arm_cpuid)) {
355 		/*
356 		 * Start and reset the PMC Cycle Counter.
357 		 */
358 		armreg_pmcr_write(ARM11_PMCCTL_E|ARM11_PMCCTL_P|ARM11_PMCCTL_C);
359 		armreg_pmcntenset_write(CORTEX_CNTENS_C);
360 	}
361 #endif
362 
363 	printf(" interrupts");
364 	/*
365 	 * Let the interrupts do what they need to on this CPU.
366 	 */
367 	intr_cpu_init(ci);
368 
369 	printf(" md(%p)", md_cpu_init);
370 	if (md_cpu_init != NULL)
371 		(*md_cpu_init)(ci);
372 
373 #if 0
374 	/*
375 	 * Tell the MI code we are alive!
376 	 */
377 	printf(" mi_cpu");
378 	mi_cpu_running(ci);
379 #endif
380 
381 	printf(" done!\n");
382 }
383 #endif /* MULTIPROCESSOR */
384