arm32_boot.c revision 1.10 1 /* $NetBSD: arm32_boot.c,v 1.10 2014/11/04 22:37:09 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.10 2014/11/04 22:37:09 matt Exp $");
127
128 #include "opt_ddb.h"
129 #include "opt_kgdb.h"
130 #include "opt_multiprocessor.h"
131
132 #include <sys/param.h>
133 #include <sys/reboot.h>
134 #include <sys/cpu.h>
135 #include <sys/intr.h>
136 #include <sys/atomic.h>
137 #include <sys/device.h>
138
139 #include <uvm/uvm_extern.h>
140
141 #include <arm/locore.h>
142 #include <arm/undefined.h>
143 #include <arm/arm32/machdep.h>
144
145 #include <machine/db_machdep.h>
146 #include <ddb/db_extern.h>
147
148 #include <machine/bootconfig.h>
149
150 #ifdef KGDB
151 #include <sys/kgdb.h>
152 #endif
153
154 vaddr_t
155 initarm_common(vaddr_t kvm_base, vsize_t kvm_size,
156 const struct boot_physmem *bp, size_t nbp)
157 {
158 struct bootmem_info * const bmi = &bootmem_info;
159
160 #ifdef VERBOSE_INIT_ARM
161 printf("nfreeblocks = %u, free_pages = %d (%#x)\n",
162 bmi->bmi_nfreeblocks, bmi->bmi_freepages,
163 bmi->bmi_freepages);
164 #endif
165
166 /*
167 * Moved from cpu_startup() as data_abort_handler() references
168 * this during uvm init.
169 */
170 uvm_lwp_setuarea(&lwp0, kernelstack.pv_va);
171
172 #ifdef VERBOSE_INIT_ARM
173 printf("bootstrap done.\n");
174 #endif
175
176 #ifdef VERBOSE_INIT_ARM
177 printf("vectors");
178 #endif
179 arm32_vector_init(systempage.pv_va, ARM_VEC_ALL);
180 #ifdef VERBOSE_INIT_ARM
181 printf(" %#"PRIxVADDR"\n", vector_page);
182 #endif
183
184 /*
185 * Pages were allocated during the secondary bootstrap for the
186 * stacks for different CPU modes.
187 * We must now set the r13 registers in the different CPU modes to
188 * point to these stacks.
189 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
190 * of the stack memory.
191 */
192 #ifdef VERBOSE_INIT_ARM
193 printf("init subsystems: stacks ");
194 #endif
195 set_stackptr(PSR_FIQ32_MODE,
196 fiqstack.pv_va + FIQ_STACK_SIZE * PAGE_SIZE);
197 set_stackptr(PSR_IRQ32_MODE,
198 irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
199 set_stackptr(PSR_ABT32_MODE,
200 abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
201 set_stackptr(PSR_UND32_MODE,
202 undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
203
204 /*
205 * Well we should set a data abort handler.
206 * Once things get going this will change as we will need a proper
207 * handler.
208 * Until then we will use a handler that just panics but tells us
209 * why.
210 * Initialisation of the vectors will just panic on a data abort.
211 * This just fills in a slightly better one.
212 */
213 #ifdef VERBOSE_INIT_ARM
214 printf("vectors ");
215 #endif
216 data_abort_handler_address = (u_int)data_abort_handler;
217 prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
218 undefined_handler_address = (u_int)undefinedinstruction_bounce;
219
220 /* Initialise the undefined instruction handlers */
221 #ifdef VERBOSE_INIT_ARM
222 printf("undefined ");
223 #endif
224 undefined_init();
225
226 /* Load memory into UVM. */
227 #ifdef VERBOSE_INIT_ARM
228 printf("page ");
229 #endif
230 uvm_setpagesize(); /* initialize PAGE_SIZE-dependent variables */
231
232 #ifdef VERBOSE_INIT_ARM
233 printf("pmap_physload ");
234 #endif
235 KASSERT(bp != NULL || nbp == 0);
236 KASSERT(bp == NULL || nbp != 0);
237
238 for (size_t i = 0; i < bmi->bmi_nfreeblocks; i++) {
239 pv_addr_t * const pv = &bmi->bmi_freeblocks[i];
240 paddr_t start = atop(pv->pv_pa);
241 const paddr_t end = start + atop(pv->pv_size);
242
243 while (start < end) {
244 int vm_freelist = VM_FREELIST_DEFAULT;
245 paddr_t segend = end;
246 /*
247 * This assumes the bp list is sorted in ascending
248 * order.
249 */
250 for (size_t j = 0; j < nbp; j++) {
251 paddr_t bp_start = bp[j].bp_start;
252 paddr_t bp_end = bp_start + bp[j].bp_pages;
253 if (start < bp_start) {
254 if (segend > bp_start) {
255 segend = bp_start;
256 }
257 break;
258 }
259 if (start < bp_end) {
260 if (segend > bp_end) {
261 segend = bp_end;
262 }
263 vm_freelist = bp[j].bp_freelist;
264 break;
265 }
266 }
267
268 uvm_page_physload(start, segend, start, segend,
269 vm_freelist);
270 start = segend;
271 }
272 }
273
274 /* Boot strap pmap telling it where the kernel page table is */
275 #ifdef VERBOSE_INIT_ARM
276 printf("pmap ");
277 #endif
278 pmap_bootstrap(kvm_base, kvm_base + kvm_size);
279
280 #ifdef __HAVE_MEMORY_DISK__
281 md_root_setconf(memory_disk, sizeof memory_disk);
282 #endif
283
284 #ifdef BOOTHOWTO
285 boothowto |= BOOTHOWTO;
286 #endif
287
288 #ifdef KGDB
289 if (boothowto & RB_KDB) {
290 kgdb_debug_init = 1;
291 kgdb_connect(1);
292 }
293 #endif
294
295 #ifdef DDB
296 db_machine_init();
297 ddb_init(0, NULL, NULL);
298
299 if (boothowto & RB_KDB)
300 Debugger();
301 #endif
302
303 #ifdef VERBOSE_INIT_ARM
304 printf("done.\n");
305 #endif
306
307 /* We return the new stack pointer address */
308 return kernelstack.pv_va + USPACE_SVC_STACK_TOP;
309 }
310
311 #ifdef MULTIPROCESSOR
312 /*
313 * When we are called, the MMU and caches are on and we are running on the stack
314 * of the idlelwp for this cpu.
315 */
316 void
317 cpu_hatch(struct cpu_info *ci, cpuid_t cpuid, void (*md_cpu_init)(struct cpu_info *))
318 {
319 KASSERT(cpu_index(ci) == cpuid);
320
321 /*
322 * Raise our IPL to the max
323 */
324 splhigh();
325
326 #ifdef VERBOSE_INIT_ARM
327 printf("%s(%s): ", __func__, ci->ci_data.cpu_name);
328 #endif
329 uint32_t mpidr = armreg_mpidr_read();
330 if (mpidr & MPIDR_MT) {
331 ci->ci_data.cpu_smt_id = mpidr & MPIDR_AFF0;
332 ci->ci_data.cpu_core_id = mpidr & MPIDR_AFF1;
333 ci->ci_data.cpu_package_id = mpidr & MPIDR_AFF2;
334 } else {
335 ci->ci_data.cpu_core_id = mpidr & MPIDR_AFF0;
336 ci->ci_data.cpu_package_id = mpidr & MPIDR_AFF1;
337 }
338
339 /*
340 * Make sure we have the right vector page.
341 */
342 #ifdef VERBOSE_INIT_ARM
343 printf(" vectors");
344 #endif
345 arm32_vector_init(systempage.pv_va, ARM_VEC_ALL);
346
347 /*
348 * Initialize the stack for each mode (we are already running on the
349 * SVC32 stack of the idlelwp).
350 */
351 #ifdef VERBOSE_INIT_ARM
352 printf(" stacks");
353 #endif
354 set_stackptr(PSR_FIQ32_MODE,
355 fiqstack.pv_va + (cpu_index(ci) + 1) * FIQ_STACK_SIZE * PAGE_SIZE);
356 set_stackptr(PSR_IRQ32_MODE,
357 irqstack.pv_va + (cpu_index(ci) + 1) * IRQ_STACK_SIZE * PAGE_SIZE);
358 set_stackptr(PSR_ABT32_MODE,
359 abtstack.pv_va + (cpu_index(ci) + 1) * ABT_STACK_SIZE * PAGE_SIZE);
360 set_stackptr(PSR_UND32_MODE,
361 undstack.pv_va + (cpu_index(ci) + 1) * UND_STACK_SIZE * PAGE_SIZE);
362
363 ci->ci_lastlwp = NULL;
364 ci->ci_pmap_lastuser = NULL;
365 #ifdef ARM_MMU_EXTENDED
366 #ifdef VERBOSE_INIT_ARM
367 printf(" tlb");
368 #endif
369 /*
370 * Attach to the tlb.
371 */
372 ci->ci_pmap_cur = pmap_kernel();
373 ci->ci_pmap_asid_cur = KERNEL_PID;
374 #endif
375
376 #ifdef CPU_CORTEX
377 if (CPU_ID_CORTEX_P(ci->ci_arm_cpuid)) {
378 /*
379 * Start and reset the PMC Cycle Counter.
380 */
381 armreg_pmcr_write(ARM11_PMCCTL_E|ARM11_PMCCTL_P|ARM11_PMCCTL_C);
382 armreg_pmcntenset_write(CORTEX_CNTENS_C);
383 }
384 #endif
385
386 aprint_naive("%s", device_xname(ci->ci_dev));
387 aprint_normal("%s", device_xname(ci->ci_dev));
388 identify_arm_cpu(ci->ci_dev, ci);
389 #ifdef VERBOSE_INIT_ARM
390 printf(" vfp");
391 #endif
392 vfp_attach(ci);
393
394 #ifdef VERBOSE_INIT_ARM
395 printf(" interrupts");
396 #endif
397 /*
398 * Let the interrupts do what they need to on this CPU.
399 */
400 intr_cpu_init(ci);
401
402 #ifdef VERBOSE_INIT_ARM
403 printf(" md(%p)", md_cpu_init);
404 #endif
405 if (md_cpu_init != NULL)
406 (*md_cpu_init)(ci);
407
408 #ifdef VERBOSE_INIT_ARM
409 printf(" done!\n");
410 #endif
411 atomic_and_32(&arm_cpu_mbox, ~(1 << cpuid));
412 membar_producer();
413 __asm __volatile("sev; sev; sev");
414 }
415 #endif /* MULTIPROCESSOR */
416