arm32_kvminit.c revision 1.26 1 /* $NetBSD: arm32_kvminit.c,v 1.26 2014/04/01 05:37:35 skrll 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 __KERNEL_RCSID(0, "$NetBSD: arm32_kvminit.c,v 1.26 2014/04/01 05:37:35 skrll Exp $");
126
127 #include <sys/param.h>
128 #include <sys/device.h>
129 #include <sys/kernel.h>
130 #include <sys/reboot.h>
131 #include <sys/bus.h>
132
133 #include <dev/cons.h>
134
135 #include <uvm/uvm_extern.h>
136
137 #include <arm/locore.h>
138 #include <arm/db_machdep.h>
139 #include <arm/undefined.h>
140 #include <arm/bootconfig.h>
141 #include <arm/arm32/machdep.h>
142
143 #include "ksyms.h"
144
145 struct bootmem_info bootmem_info;
146
147 paddr_t msgbufphys;
148 paddr_t physical_start;
149 paddr_t physical_end;
150
151 extern char etext[];
152 extern char __data_start[], _edata[];
153 extern char __bss_start[], __bss_end__[];
154 extern char _end[];
155
156 /* Page tables for mapping kernel VM */
157 #define KERNEL_L2PT_VMDATA_NUM 8 /* start with 32MB of KVM */
158
159 /*
160 * Macros to translate between physical and virtual for a subset of the
161 * kernel address space. *Not* for general use.
162 */
163 #define KERN_VTOPHYS(bmi, va) \
164 ((paddr_t)((vaddr_t)(va) - KERNEL_BASE + (bmi)->bmi_start))
165 #define KERN_PHYSTOV(bmi, pa) \
166 ((vaddr_t)((paddr_t)(pa) - (bmi)->bmi_start + KERNEL_BASE))
167
168 void
169 arm32_bootmem_init(paddr_t memstart, psize_t memsize, vsize_t kernelstart)
170 {
171 struct bootmem_info * const bmi = &bootmem_info;
172 pv_addr_t *pv = bmi->bmi_freeblocks;
173
174 #ifdef VERBOSE_INIT_ARM
175 printf("%s: memstart=%#lx, memsize=%#lx, kernelstart=%#lx\n",
176 __func__, memstart, memsize, kernelstart);
177 #endif
178
179 physical_start = bmi->bmi_start = memstart;
180 physical_end = bmi->bmi_end = memstart + memsize;
181 physmem = memsize / PAGE_SIZE;
182
183 /*
184 * Let's record where the kernel lives.
185 */
186 bmi->bmi_kernelstart = kernelstart;
187 bmi->bmi_kernelend = KERN_VTOPHYS(bmi, round_page((vaddr_t)_end));
188
189 #ifdef VERBOSE_INIT_ARM
190 printf("%s: kernelend=%#lx\n", __func__, bmi->bmi_kernelend);
191 #endif
192
193 /*
194 * Now the rest of the free memory must be after the kernel.
195 */
196 pv->pv_pa = bmi->bmi_kernelend;
197 pv->pv_va = KERN_PHYSTOV(bmi, pv->pv_pa);
198 pv->pv_size = bmi->bmi_end - bmi->bmi_kernelend;
199 bmi->bmi_freepages += pv->pv_size / PAGE_SIZE;
200 #ifdef VERBOSE_INIT_ARM
201 printf("%s: adding %lu free pages: [%#lx..%#lx] (VA %#lx)\n",
202 __func__, pv->pv_size / PAGE_SIZE, pv->pv_pa,
203 pv->pv_pa + pv->pv_size - 1, pv->pv_va);
204 #endif
205 pv++;
206
207 /*
208 * Add a free block for any memory before the kernel.
209 */
210 if (bmi->bmi_start < bmi->bmi_kernelstart) {
211 pv->pv_pa = bmi->bmi_start;
212 pv->pv_va = KERNEL_BASE;
213 pv->pv_size = bmi->bmi_kernelstart - bmi->bmi_start;
214 bmi->bmi_freepages += pv->pv_size / PAGE_SIZE;
215 #ifdef VERBOSE_INIT_ARM
216 printf("%s: adding %lu free pages: [%#lx..%#lx] (VA %#lx)\n",
217 __func__, pv->pv_size / PAGE_SIZE, pv->pv_pa,
218 pv->pv_pa + pv->pv_size - 1, pv->pv_va);
219 #endif
220 pv++;
221 }
222
223 bmi->bmi_nfreeblocks = pv - bmi->bmi_freeblocks;
224
225 SLIST_INIT(&bmi->bmi_freechunks);
226 SLIST_INIT(&bmi->bmi_chunks);
227 }
228
229 static bool
230 concat_pvaddr(pv_addr_t *acc_pv, pv_addr_t *pv)
231 {
232 if (acc_pv->pv_pa + acc_pv->pv_size == pv->pv_pa
233 && acc_pv->pv_va + acc_pv->pv_size == pv->pv_va
234 && acc_pv->pv_prot == pv->pv_prot
235 && acc_pv->pv_cache == pv->pv_cache) {
236 #ifdef VERBOSE_INIT_ARMX
237 printf("%s: appending pv %p (%#lx..%#lx) to %#lx..%#lx\n",
238 __func__, pv, pv->pv_pa, pv->pv_pa + pv->pv_size + 1,
239 acc_pv->pv_pa, acc_pv->pv_pa + acc_pv->pv_size + 1);
240 #endif
241 acc_pv->pv_size += pv->pv_size;
242 return true;
243 }
244
245 return false;
246 }
247
248 static void
249 add_pages(struct bootmem_info *bmi, pv_addr_t *pv)
250 {
251 pv_addr_t **pvp = &SLIST_FIRST(&bmi->bmi_chunks);
252 while ((*pvp) != NULL && (*pvp)->pv_va <= pv->pv_va) {
253 pv_addr_t * const pv0 = (*pvp);
254 KASSERT(SLIST_NEXT(pv0, pv_list) == NULL || pv0->pv_pa < SLIST_NEXT(pv0, pv_list)->pv_pa);
255 if (concat_pvaddr(pv0, pv)) {
256 #ifdef VERBOSE_INIT_ARM
257 printf("%s: %s pv %p (%#lx..%#lx) to %#lx..%#lx\n",
258 __func__, "appending", pv,
259 pv->pv_pa, pv->pv_pa + pv->pv_size - 1,
260 pv0->pv_pa, pv0->pv_pa + pv0->pv_size - pv->pv_size - 1);
261 #endif
262 pv = SLIST_NEXT(pv0, pv_list);
263 if (pv != NULL && concat_pvaddr(pv0, pv)) {
264 #ifdef VERBOSE_INIT_ARM
265 printf("%s: %s pv %p (%#lx..%#lx) to %#lx..%#lx\n",
266 __func__, "merging", pv,
267 pv->pv_pa, pv->pv_pa + pv->pv_size - 1,
268 pv0->pv_pa,
269 pv0->pv_pa + pv0->pv_size - pv->pv_size - 1);
270 #endif
271 SLIST_REMOVE_AFTER(pv0, pv_list);
272 SLIST_INSERT_HEAD(&bmi->bmi_freechunks, pv, pv_list);
273 }
274 return;
275 }
276 KASSERT(pv->pv_va != (*pvp)->pv_va);
277 pvp = &SLIST_NEXT(*pvp, pv_list);
278 }
279 KASSERT((*pvp) == NULL || pv->pv_va < (*pvp)->pv_va);
280 pv_addr_t * const new_pv = SLIST_FIRST(&bmi->bmi_freechunks);
281 KASSERT(new_pv != NULL);
282 SLIST_REMOVE_HEAD(&bmi->bmi_freechunks, pv_list);
283 *new_pv = *pv;
284 SLIST_NEXT(new_pv, pv_list) = *pvp;
285 (*pvp) = new_pv;
286 #ifdef VERBOSE_INIT_ARM
287 printf("%s: adding pv %p (pa %#lx, va %#lx, %lu pages) ",
288 __func__, new_pv, new_pv->pv_pa, new_pv->pv_va,
289 new_pv->pv_size / PAGE_SIZE);
290 if (SLIST_NEXT(new_pv, pv_list))
291 printf("before pa %#lx\n", SLIST_NEXT(new_pv, pv_list)->pv_pa);
292 else
293 printf("at tail\n");
294 #endif
295 }
296
297 static void
298 valloc_pages(struct bootmem_info *bmi, pv_addr_t *pv, size_t npages,
299 int prot, int cache, bool zero_p)
300 {
301 size_t nbytes = npages * PAGE_SIZE;
302 pv_addr_t *free_pv = bmi->bmi_freeblocks;
303 size_t free_idx = 0;
304 static bool l1pt_found;
305
306 KASSERT(npages > 0);
307
308 /*
309 * If we haven't allocated the kernel L1 page table and we are aligned
310 * at a L1 table boundary, alloc the memory for it.
311 */
312 if (!l1pt_found
313 && (free_pv->pv_pa & (L1_TABLE_SIZE - 1)) == 0
314 && free_pv->pv_size >= L1_TABLE_SIZE) {
315 l1pt_found = true;
316 valloc_pages(bmi, &kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE,
317 VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE, true);
318 add_pages(bmi, &kernel_l1pt);
319 }
320
321 while (nbytes > free_pv->pv_size) {
322 free_pv++;
323 free_idx++;
324 if (free_idx == bmi->bmi_nfreeblocks) {
325 panic("%s: could not allocate %zu bytes",
326 __func__, nbytes);
327 }
328 }
329
330 /*
331 * As we allocate the memory, make sure that we don't walk over
332 * our current first level translation table.
333 */
334 KASSERT((armreg_ttbr_read() & ~(L1_TABLE_SIZE - 1)) != free_pv->pv_pa);
335
336 pv->pv_pa = free_pv->pv_pa;
337 pv->pv_va = free_pv->pv_va;
338 pv->pv_size = nbytes;
339 pv->pv_prot = prot;
340 pv->pv_cache = cache;
341
342 /*
343 * If PTE_PAGETABLE uses the same cache modes as PTE_CACHE
344 * just use PTE_CACHE.
345 */
346 if (cache == PTE_PAGETABLE
347 && pte_l1_s_cache_mode == pte_l1_s_cache_mode_pt
348 && pte_l2_l_cache_mode == pte_l2_l_cache_mode_pt
349 && pte_l2_s_cache_mode == pte_l2_s_cache_mode_pt)
350 pv->pv_cache = PTE_CACHE;
351
352 free_pv->pv_pa += nbytes;
353 free_pv->pv_va += nbytes;
354 free_pv->pv_size -= nbytes;
355 if (free_pv->pv_size == 0) {
356 --bmi->bmi_nfreeblocks;
357 for (; free_idx < bmi->bmi_nfreeblocks; free_idx++) {
358 free_pv[0] = free_pv[1];
359 }
360 }
361
362 bmi->bmi_freepages -= npages;
363
364 if (zero_p)
365 memset((void *)pv->pv_va, 0, nbytes);
366 }
367
368 void
369 arm32_kernel_vm_init(vaddr_t kernel_vm_base, vaddr_t vectors, vaddr_t iovbase,
370 const struct pmap_devmap *devmap, bool mapallmem_p)
371 {
372 struct bootmem_info * const bmi = &bootmem_info;
373 #ifdef MULTIPROCESSOR
374 const size_t cpu_num = arm_cpu_max;
375 #else
376 const size_t cpu_num = 1;
377 #endif
378 #ifdef ARM_HAS_VBAR
379 const bool map_vectors_p = false;
380 #elif defined(CPU_ARMV7) || defined(CPU_ARM11)
381 const bool map_vectors_p = vectors == ARM_VECTORS_HIGH
382 || (armreg_pfr1_read() & ARM_PFR1_SEC_MASK) == 0;
383 #else
384 const bool map_vectors_p = true;
385 #endif
386
387 #ifdef __HAVE_MM_MD_DIRECT_MAPPED_PHYS
388 KASSERT(mapallmem_p);
389 #endif
390
391 /*
392 * Calculate the number of L2 pages needed for mapping the
393 * kernel + data + stuff. Assume 2 L2 pages for kernel, 1 for vectors,
394 * and 1 for IO
395 */
396 size_t kernel_size = bmi->bmi_kernelend;
397 kernel_size -= (bmi->bmi_kernelstart & -L2_S_SEGSIZE);
398 kernel_size += L1_TABLE_SIZE_REAL;
399 kernel_size += PAGE_SIZE * KERNEL_L2PT_VMDATA_NUM;
400 if (map_vectors_p) {
401 kernel_size += PAGE_SIZE; /* L2PT for VECTORS */
402 }
403 if (iovbase) {
404 kernel_size += PAGE_SIZE; /* L2PT for IO */
405 }
406 kernel_size +=
407 cpu_num * (ABT_STACK_SIZE + FIQ_STACK_SIZE + IRQ_STACK_SIZE
408 + UND_STACK_SIZE + UPAGES) * PAGE_SIZE;
409 kernel_size += round_page(MSGBUFSIZE);
410 kernel_size += 0x10000; /* slop */
411 if (!mapallmem_p) {
412 kernel_size += PAGE_SIZE
413 * ((kernel_size + L2_S_SEGSIZE - 1) / L2_S_SEGSIZE);
414 }
415 kernel_size = round_page(kernel_size);
416
417 /*
418 * Now we know how many L2 pages it will take. If we've mapped
419 * all of memory, then it won't take any.
420 */
421 const size_t KERNEL_L2PT_KERNEL_NUM = mapallmem_p
422 ? 0 : round_page(kernel_size + L2_S_SEGSIZE - 1) / L2_S_SEGSIZE;
423
424 #ifdef VERBOSE_INIT_ARM
425 printf("%s: %zu L2 pages are needed to map %#zx kernel bytes\n",
426 __func__, KERNEL_L2PT_KERNEL_NUM, kernel_size);
427 #endif
428
429 KASSERT(KERNEL_L2PT_KERNEL_NUM + KERNEL_L2PT_VMDATA_NUM < __arraycount(bmi->bmi_l2pts));
430 pv_addr_t * const kernel_l2pt = bmi->bmi_l2pts;
431 pv_addr_t * const vmdata_l2pt = kernel_l2pt + KERNEL_L2PT_KERNEL_NUM;
432 pv_addr_t msgbuf;
433 pv_addr_t text;
434 pv_addr_t data;
435 pv_addr_t chunks[KERNEL_L2PT_KERNEL_NUM+KERNEL_L2PT_VMDATA_NUM+11];
436 #if ARM_MMU_XSCALE == 1
437 pv_addr_t minidataclean;
438 #endif
439
440 /*
441 * We need to allocate some fixed page tables to get the kernel going.
442 *
443 * We are going to allocate our bootstrap pages from the beginning of
444 * the free space that we just calculated. We allocate one page
445 * directory and a number of page tables and store the physical
446 * addresses in the bmi_l2pts array in bootmem_info.
447 *
448 * The kernel page directory must be on a 16K boundary. The page
449 * tables must be on 4K boundaries. What we do is allocate the
450 * page directory on the first 16K boundary that we encounter, and
451 * the page tables on 4K boundaries otherwise. Since we allocate
452 * at least 3 L2 page tables, we are guaranteed to encounter at
453 * least one 16K aligned region.
454 */
455
456 #ifdef VERBOSE_INIT_ARM
457 printf("%s: allocating page tables for", __func__);
458 #endif
459 for (size_t i = 0; i < __arraycount(chunks); i++) {
460 SLIST_INSERT_HEAD(&bmi->bmi_freechunks, &chunks[i], pv_list);
461 }
462
463 kernel_l1pt.pv_pa = 0;
464 kernel_l1pt.pv_va = 0;
465
466 /*
467 * Allocate the L2 pages, but if we get to a page that is aligned for
468 * an L1 page table, we will allocate the pages for it first and then
469 * allocate the L2 page.
470 */
471
472 if (map_vectors_p) {
473 /*
474 * First allocate L2 page for the vectors.
475 */
476 #ifdef VERBOSE_INIT_ARM
477 printf(" vector");
478 #endif
479 valloc_pages(bmi, &bmi->bmi_vector_l2pt, 1,
480 VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE, true);
481 add_pages(bmi, &bmi->bmi_vector_l2pt);
482 }
483
484 /*
485 * Now allocate L2 pages for the kernel
486 */
487 #ifdef VERBOSE_INIT_ARM
488 printf(" kernel");
489 #endif
490 KASSERT(mapallmem_p || KERNEL_L2PT_KERNEL_NUM > 0);
491 KASSERT(!mapallmem_p || KERNEL_L2PT_KERNEL_NUM == 0);
492 for (size_t idx = 0; idx < KERNEL_L2PT_KERNEL_NUM; ++idx) {
493 valloc_pages(bmi, &kernel_l2pt[idx], 1,
494 VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE, true);
495 add_pages(bmi, &kernel_l2pt[idx]);
496 }
497
498 /*
499 * Now allocate L2 pages for the initial kernel VA space.
500 */
501 #ifdef VERBOSE_INIT_ARM
502 printf(" vm");
503 #endif
504 for (size_t idx = 0; idx < KERNEL_L2PT_VMDATA_NUM; ++idx) {
505 valloc_pages(bmi, &vmdata_l2pt[idx], 1,
506 VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE, true);
507 add_pages(bmi, &vmdata_l2pt[idx]);
508 }
509
510 /*
511 * If someone wanted a L2 page for I/O, allocate it now.
512 */
513 if (iovbase) {
514 #ifdef VERBOSE_INIT_ARM
515 printf(" io");
516 #endif
517 valloc_pages(bmi, &bmi->bmi_io_l2pt, 1,
518 VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE, true);
519 add_pages(bmi, &bmi->bmi_io_l2pt);
520 }
521
522 #ifdef VERBOSE_INIT_ARM
523 printf("%s: allocating stacks\n", __func__);
524 #endif
525
526 /* Allocate stacks for all modes and CPUs */
527 valloc_pages(bmi, &abtstack, ABT_STACK_SIZE * cpu_num,
528 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE, true);
529 add_pages(bmi, &abtstack);
530 valloc_pages(bmi, &fiqstack, FIQ_STACK_SIZE * cpu_num,
531 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE, true);
532 add_pages(bmi, &fiqstack);
533 valloc_pages(bmi, &irqstack, IRQ_STACK_SIZE * cpu_num,
534 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE, true);
535 add_pages(bmi, &irqstack);
536 valloc_pages(bmi, &undstack, UND_STACK_SIZE * cpu_num,
537 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE, true);
538 add_pages(bmi, &undstack);
539 valloc_pages(bmi, &idlestack, UPAGES * cpu_num, /* SVC32 */
540 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE, true);
541 add_pages(bmi, &idlestack);
542 valloc_pages(bmi, &kernelstack, UPAGES, /* SVC32 */
543 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE, true);
544 add_pages(bmi, &kernelstack);
545
546 /* Allocate the message buffer from the end of memory. */
547 const size_t msgbuf_pgs = round_page(MSGBUFSIZE) / PAGE_SIZE;
548 valloc_pages(bmi, &msgbuf, msgbuf_pgs,
549 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE, false);
550 add_pages(bmi, &msgbuf);
551 msgbufphys = msgbuf.pv_pa;
552
553 if (map_vectors_p) {
554 /*
555 * Allocate a page for the system vector page.
556 * This page will just contain the system vectors and can be
557 * shared by all processes.
558 */
559 valloc_pages(bmi, &systempage, 1, VM_PROT_READ|VM_PROT_WRITE,
560 PTE_CACHE, true);
561 }
562 systempage.pv_va = vectors;
563
564 /*
565 * If the caller needed a few extra pages for some reason, allocate
566 * them now.
567 */
568 #if ARM_MMU_XSCALE == 1
569 #if (ARM_NMMUS > 1)
570 if (xscale_use_minidata)
571 #endif
572 valloc_pages(bmi, extrapv, nextrapages,
573 VM_PROT_READ|VM_PROT_WRITE, 0, true);
574 #endif
575
576 /*
577 * Ok we have allocated physical pages for the primary kernel
578 * page tables and stacks. Let's just confirm that.
579 */
580 if (kernel_l1pt.pv_va == 0
581 && (!kernel_l1pt.pv_pa || (kernel_l1pt.pv_pa & (L1_TABLE_SIZE - 1)) != 0))
582 panic("%s: Failed to allocate or align the kernel "
583 "page directory", __func__);
584
585
586 #ifdef VERBOSE_INIT_ARM
587 printf("Creating L1 page table at 0x%08lx\n", kernel_l1pt.pv_pa);
588 #endif
589
590 /*
591 * Now we start construction of the L1 page table
592 * We start by mapping the L2 page tables into the L1.
593 * This means that we can replace L1 mappings later on if necessary
594 */
595 vaddr_t l1pt_va = kernel_l1pt.pv_va;
596 paddr_t l1pt_pa = kernel_l1pt.pv_pa;
597
598 if (map_vectors_p) {
599 /* Map the L2 pages tables in the L1 page table */
600 pmap_link_l2pt(l1pt_va, systempage.pv_va & -L2_S_SEGSIZE,
601 &bmi->bmi_vector_l2pt);
602 #ifdef VERBOSE_INIT_ARM
603 printf("%s: adding L2 pt (VA %#lx, PA %#lx) "
604 "for VA %#lx\n (vectors)",
605 __func__, bmi->bmi_vector_l2pt.pv_va,
606 bmi->bmi_vector_l2pt.pv_pa, systempage.pv_va);
607 #endif
608 }
609
610 const vaddr_t kernel_base =
611 KERN_PHYSTOV(bmi, bmi->bmi_kernelstart & -L2_S_SEGSIZE);
612 for (size_t idx = 0; idx < KERNEL_L2PT_KERNEL_NUM; idx++) {
613 pmap_link_l2pt(l1pt_va, kernel_base + idx * L2_S_SEGSIZE,
614 &kernel_l2pt[idx]);
615 #ifdef VERBOSE_INIT_ARM
616 printf("%s: adding L2 pt (VA %#lx, PA %#lx) for VA %#lx (kernel)\n",
617 __func__, kernel_l2pt[idx].pv_va,
618 kernel_l2pt[idx].pv_pa, kernel_base + idx * L2_S_SEGSIZE);
619 #endif
620 }
621
622 for (size_t idx = 0; idx < KERNEL_L2PT_VMDATA_NUM; idx++) {
623 pmap_link_l2pt(l1pt_va, kernel_vm_base + idx * L2_S_SEGSIZE,
624 &vmdata_l2pt[idx]);
625 #ifdef VERBOSE_INIT_ARM
626 printf("%s: adding L2 pt (VA %#lx, PA %#lx) for VA %#lx (vm)\n",
627 __func__, vmdata_l2pt[idx].pv_va, vmdata_l2pt[idx].pv_pa,
628 kernel_vm_base + idx * L2_S_SEGSIZE);
629 #endif
630 }
631 if (iovbase) {
632 pmap_link_l2pt(l1pt_va, iovbase & -L2_S_SEGSIZE, &bmi->bmi_io_l2pt);
633 #ifdef VERBOSE_INIT_ARM
634 printf("%s: adding L2 pt (VA %#lx, PA %#lx) for VA %#lx (io)\n",
635 __func__, bmi->bmi_io_l2pt.pv_va, bmi->bmi_io_l2pt.pv_pa,
636 iovbase & -L2_S_SEGSIZE);
637 #endif
638 }
639
640 /* update the top of the kernel VM */
641 pmap_curmaxkvaddr =
642 kernel_vm_base + (KERNEL_L2PT_VMDATA_NUM * L2_S_SEGSIZE);
643
644 #ifdef VERBOSE_INIT_ARM
645 printf("Mapping kernel\n");
646 #endif
647
648 extern char etext[], _end[];
649 size_t totalsize = bmi->bmi_kernelend - bmi->bmi_kernelstart;
650 size_t textsize = KERN_VTOPHYS(bmi, (uintptr_t)etext) - bmi->bmi_kernelstart;
651
652 textsize = (textsize + PGOFSET) & ~PGOFSET;
653
654 /* start at offset of kernel in RAM */
655
656 text.pv_pa = bmi->bmi_kernelstart;
657 text.pv_va = KERN_PHYSTOV(bmi, bmi->bmi_kernelstart);
658 text.pv_size = textsize;
659 text.pv_prot = VM_PROT_READ|VM_PROT_WRITE; /* XXX VM_PROT_EXECUTE */
660 text.pv_cache = PTE_CACHE;
661
662 #ifdef VERBOSE_INIT_ARM
663 printf("%s: adding chunk for kernel text %#lx..%#lx (VA %#lx)\n",
664 __func__, text.pv_pa, text.pv_pa + text.pv_size - 1, text.pv_va);
665 #endif
666
667 add_pages(bmi, &text);
668
669 data.pv_pa = text.pv_pa + textsize;
670 data.pv_va = text.pv_va + textsize;
671 data.pv_size = totalsize - textsize;
672 data.pv_prot = VM_PROT_READ|VM_PROT_WRITE;
673 data.pv_cache = PTE_CACHE;
674
675 #ifdef VERBOSE_INIT_ARM
676 printf("%s: adding chunk for kernel data/bss %#lx..%#lx (VA %#lx)\n",
677 __func__, data.pv_pa, data.pv_pa + data.pv_size - 1, data.pv_va);
678 #endif
679
680 add_pages(bmi, &data);
681
682 #ifdef VERBOSE_INIT_ARM
683 printf("Listing Chunks\n");
684
685 pv_addr_t *lpv;
686 SLIST_FOREACH(lpv, &bmi->bmi_chunks, pv_list) {
687 printf("%s: pv %p: chunk VA %#lx..%#lx "
688 "(PA %#lx, prot %d, cache %d)\n",
689 __func__, lpv, lpv->pv_va, lpv->pv_va + lpv->pv_size - 1,
690 lpv->pv_pa, lpv->pv_prot, lpv->pv_cache);
691 }
692 printf("\nMapping Chunks\n");
693 #endif
694
695 pv_addr_t cur_pv;
696 pv_addr_t *pv = SLIST_FIRST(&bmi->bmi_chunks);
697 if (!mapallmem_p || pv->pv_pa == bmi->bmi_start) {
698 cur_pv = *pv;
699 pv = SLIST_NEXT(pv, pv_list);
700 } else {
701 cur_pv.pv_va = KERNEL_BASE;
702 cur_pv.pv_pa = bmi->bmi_start;
703 cur_pv.pv_size = pv->pv_pa - bmi->bmi_start;
704 cur_pv.pv_prot = VM_PROT_READ | VM_PROT_WRITE;
705 cur_pv.pv_cache = PTE_CACHE;
706 }
707 while (pv != NULL) {
708 if (mapallmem_p) {
709 if (concat_pvaddr(&cur_pv, pv)) {
710 pv = SLIST_NEXT(pv, pv_list);
711 continue;
712 }
713 if (cur_pv.pv_pa + cur_pv.pv_size < pv->pv_pa) {
714 /*
715 * See if we can extend the current pv to emcompass the
716 * hole, and if so do it and retry the concatenation.
717 */
718 if (cur_pv.pv_prot == (VM_PROT_READ|VM_PROT_WRITE)
719 && cur_pv.pv_cache == PTE_CACHE) {
720 cur_pv.pv_size = pv->pv_pa - cur_pv.pv_va;
721 continue;
722 }
723
724 /*
725 * We couldn't so emit the current chunk and then
726 */
727 #ifdef VERBOSE_INIT_ARM
728 printf("%s: mapping chunk VA %#lx..%#lx "
729 "(PA %#lx, prot %d, cache %d)\n",
730 __func__,
731 cur_pv.pv_va, cur_pv.pv_va + cur_pv.pv_size - 1,
732 cur_pv.pv_pa, cur_pv.pv_prot, cur_pv.pv_cache);
733 #endif
734 pmap_map_chunk(l1pt_va, cur_pv.pv_va, cur_pv.pv_pa,
735 cur_pv.pv_size, cur_pv.pv_prot, cur_pv.pv_cache);
736
737 /*
738 * set the current chunk to the hole and try again.
739 */
740 cur_pv.pv_pa += cur_pv.pv_size;
741 cur_pv.pv_va += cur_pv.pv_size;
742 cur_pv.pv_size = pv->pv_pa - cur_pv.pv_va;
743 cur_pv.pv_prot = VM_PROT_READ | VM_PROT_WRITE;
744 cur_pv.pv_cache = PTE_CACHE;
745 continue;
746 }
747 }
748
749 /*
750 * The new pv didn't concatenate so emit the current one
751 * and use the new pv as the current pv.
752 */
753 #ifdef VERBOSE_INIT_ARM
754 printf("%s: mapping chunk VA %#lx..%#lx "
755 "(PA %#lx, prot %d, cache %d)\n",
756 __func__, cur_pv.pv_va, cur_pv.pv_va + cur_pv.pv_size - 1,
757 cur_pv.pv_pa, cur_pv.pv_prot, cur_pv.pv_cache);
758 #endif
759 pmap_map_chunk(l1pt_va, cur_pv.pv_va, cur_pv.pv_pa,
760 cur_pv.pv_size, cur_pv.pv_prot, cur_pv.pv_cache);
761 cur_pv = *pv;
762 pv = SLIST_NEXT(pv, pv_list);
763 }
764
765 /*
766 * If we are mapping all of memory, let's map the rest of memory.
767 */
768 if (mapallmem_p && cur_pv.pv_pa + cur_pv.pv_size < bmi->bmi_end) {
769 if (cur_pv.pv_prot == (VM_PROT_READ | VM_PROT_WRITE)
770 && cur_pv.pv_cache == PTE_CACHE) {
771 cur_pv.pv_size = bmi->bmi_end - cur_pv.pv_pa;
772 } else {
773 #ifdef VERBOSE_INIT_ARM
774 printf("%s: mapping chunk VA %#lx..%#lx "
775 "(PA %#lx, prot %d, cache %d)\n",
776 __func__, cur_pv.pv_va, cur_pv.pv_va + cur_pv.pv_size - 1,
777 cur_pv.pv_pa, cur_pv.pv_prot, cur_pv.pv_cache);
778 #endif
779 pmap_map_chunk(l1pt_va, cur_pv.pv_va, cur_pv.pv_pa,
780 cur_pv.pv_size, cur_pv.pv_prot, cur_pv.pv_cache);
781 cur_pv.pv_pa += cur_pv.pv_size;
782 cur_pv.pv_va += cur_pv.pv_size;
783 cur_pv.pv_size = bmi->bmi_end - cur_pv.pv_pa;
784 cur_pv.pv_prot = VM_PROT_READ | VM_PROT_WRITE;
785 cur_pv.pv_cache = PTE_CACHE;
786 }
787 }
788
789 /*
790 * Now we map the final chunk.
791 */
792 #ifdef VERBOSE_INIT_ARM
793 printf("%s: mapping last chunk VA %#lx..%#lx (PA %#lx, prot %d, cache %d)\n",
794 __func__, cur_pv.pv_va, cur_pv.pv_va + cur_pv.pv_size - 1,
795 cur_pv.pv_pa, cur_pv.pv_prot, cur_pv.pv_cache);
796 #endif
797 pmap_map_chunk(l1pt_va, cur_pv.pv_va, cur_pv.pv_pa,
798 cur_pv.pv_size, cur_pv.pv_prot, cur_pv.pv_cache);
799
800 /*
801 * Now we map the stuff that isn't directly after the kernel
802 */
803
804 if (map_vectors_p) {
805 /* Map the vector page. */
806 pmap_map_entry(l1pt_va, systempage.pv_va, systempage.pv_pa,
807 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
808 }
809
810 /* Map the Mini-Data cache clean area. */
811 #if ARM_MMU_XSCALE == 1
812 #if (ARM_NMMUS > 1)
813 if (xscale_use_minidata)
814 #endif
815 xscale_setup_minidata(l1_va, minidataclean.pv_va,
816 minidataclean.pv_pa);
817 #endif
818
819 /*
820 * Map integrated peripherals at same address in first level page
821 * table so that we can continue to use console.
822 */
823 if (devmap)
824 pmap_devmap_bootstrap(l1pt_va, devmap);
825
826 #ifdef VERBOSE_INIT_ARM
827 /* Tell the user about where all the bits and pieces live. */
828 printf("%22s Physical Virtual Num\n", " ");
829 printf("%22s Starting Ending Starting Ending Pages\n", " ");
830
831 static const char mem_fmt[] =
832 "%20s: 0x%08lx 0x%08lx 0x%08lx 0x%08lx %u\n";
833 static const char mem_fmt_nov[] =
834 "%20s: 0x%08lx 0x%08lx %zu\n";
835
836 printf(mem_fmt, "SDRAM", bmi->bmi_start, bmi->bmi_end - 1,
837 KERN_PHYSTOV(bmi, bmi->bmi_start), KERN_PHYSTOV(bmi, bmi->bmi_end - 1),
838 physmem);
839 printf(mem_fmt, "text section",
840 text.pv_pa, text.pv_pa + text.pv_size - 1,
841 text.pv_va, text.pv_va + text.pv_size - 1,
842 (int)(text.pv_size / PAGE_SIZE));
843 printf(mem_fmt, "data section",
844 KERN_VTOPHYS(bmi, __data_start), KERN_VTOPHYS(bmi, _edata),
845 (vaddr_t)__data_start, (vaddr_t)_edata,
846 (int)((round_page((vaddr_t)_edata)
847 - trunc_page((vaddr_t)__data_start)) / PAGE_SIZE));
848 printf(mem_fmt, "bss section",
849 KERN_VTOPHYS(bmi, __bss_start), KERN_VTOPHYS(bmi, __bss_end__),
850 (vaddr_t)__bss_start, (vaddr_t)__bss_end__,
851 (int)((round_page((vaddr_t)__bss_end__)
852 - trunc_page((vaddr_t)__bss_start)) / PAGE_SIZE));
853 printf(mem_fmt, "L1 page directory",
854 kernel_l1pt.pv_pa, kernel_l1pt.pv_pa + L1_TABLE_SIZE - 1,
855 kernel_l1pt.pv_va, kernel_l1pt.pv_va + L1_TABLE_SIZE - 1,
856 L1_TABLE_SIZE / PAGE_SIZE);
857 printf(mem_fmt, "ABT stack (CPU 0)",
858 abtstack.pv_pa, abtstack.pv_pa + (ABT_STACK_SIZE * PAGE_SIZE) - 1,
859 abtstack.pv_va, abtstack.pv_va + (ABT_STACK_SIZE * PAGE_SIZE) - 1,
860 ABT_STACK_SIZE);
861 printf(mem_fmt, "FIQ stack (CPU 0)",
862 fiqstack.pv_pa, fiqstack.pv_pa + (FIQ_STACK_SIZE * PAGE_SIZE) - 1,
863 fiqstack.pv_va, fiqstack.pv_va + (FIQ_STACK_SIZE * PAGE_SIZE) - 1,
864 FIQ_STACK_SIZE);
865 printf(mem_fmt, "IRQ stack (CPU 0)",
866 irqstack.pv_pa, irqstack.pv_pa + (IRQ_STACK_SIZE * PAGE_SIZE) - 1,
867 irqstack.pv_va, irqstack.pv_va + (IRQ_STACK_SIZE * PAGE_SIZE) - 1,
868 IRQ_STACK_SIZE);
869 printf(mem_fmt, "UND stack (CPU 0)",
870 undstack.pv_pa, undstack.pv_pa + (UND_STACK_SIZE * PAGE_SIZE) - 1,
871 undstack.pv_va, undstack.pv_va + (UND_STACK_SIZE * PAGE_SIZE) - 1,
872 UND_STACK_SIZE);
873 printf(mem_fmt, "IDLE stack (CPU 0)",
874 idlestack.pv_pa, idlestack.pv_pa + (UPAGES * PAGE_SIZE) - 1,
875 idlestack.pv_va, idlestack.pv_va + (UPAGES * PAGE_SIZE) - 1,
876 UPAGES);
877 printf(mem_fmt, "SVC stack",
878 kernelstack.pv_pa, kernelstack.pv_pa + (UPAGES * PAGE_SIZE) - 1,
879 kernelstack.pv_va, kernelstack.pv_va + (UPAGES * PAGE_SIZE) - 1,
880 UPAGES);
881 printf(mem_fmt, "Message Buffer",
882 msgbuf.pv_pa, msgbuf.pv_pa + (msgbuf_pgs * PAGE_SIZE) - 1,
883 msgbuf.pv_va, msgbuf.pv_va + (msgbuf_pgs * PAGE_SIZE) - 1,
884 (int)msgbuf_pgs);
885 if (map_vectors_p) {
886 printf(mem_fmt, "Exception Vectors",
887 systempage.pv_pa, systempage.pv_pa + PAGE_SIZE - 1,
888 systempage.pv_va, systempage.pv_va + PAGE_SIZE - 1,
889 1);
890 }
891 for (size_t i = 0; i < bmi->bmi_nfreeblocks; i++) {
892 pv = &bmi->bmi_freeblocks[i];
893
894 printf(mem_fmt_nov, "Free Memory",
895 pv->pv_pa, pv->pv_pa + pv->pv_size - 1,
896 pv->pv_size / PAGE_SIZE);
897 }
898 #endif
899 /*
900 * Now we have the real page tables in place so we can switch to them.
901 * Once this is done we will be running with the REAL kernel page
902 * tables.
903 */
904
905 #if defined(VERBOSE_INIT_ARM)
906 printf("TTBR0=%#x", armreg_ttbr_read());
907 #ifdef _ARM_ARCH_6
908 printf(" TTBR1=%#x TTBCR=%#x CONTEXTIDR=%#x",
909 armreg_ttbr1_read(), armreg_ttbcr_read(),
910 armreg_contextidr_read());
911 #endif
912 printf("\n");
913 #endif
914
915 /* Switch tables */
916 #ifdef VERBOSE_INIT_ARM
917 printf("switching to new L1 page table @%#lx...", l1pt_pa);
918 #endif
919
920 #ifdef ARM_MMU_EXTENDED
921 cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2))
922 | (DOMAIN_CLIENT << (PMAP_DOMAIN_USER*2)));
923 #else
924 cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
925 #endif
926 cpu_idcache_wbinv_all();
927 #ifdef VERBOSE_INIT_ARM
928 printf(" ttb");
929 #endif
930 #ifdef ARM_MMU_EXTENDED
931 /*
932 * TTBCR should have been initialized by the MD start code.
933 */
934 KASSERT((armreg_contextidr_read() & 0xff) == 0);
935 KASSERT(armreg_ttbcr_read() == __SHIFTIN(1, TTBCR_S_N));
936 /*
937 * Disable lookups via TTBR0 until there is an activated pmap.
938 */
939 armreg_ttbcr_write(armreg_ttbcr_read() | TTBCR_S_PD0);
940 cpu_setttb(l1pt_pa, KERNEL_PID);
941 arm_isb();
942 #else
943 cpu_setttb(l1pt_pa, true);
944 cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
945 #endif
946 cpu_tlb_flushID();
947
948 #ifdef VERBOSE_INIT_ARM
949 #ifdef ARM_MMU_EXTENDED
950 printf(" (TTBCR=%#x TTBR0=%#x TTBR1=%#x)",
951 armreg_ttbcr_read(), armreg_ttbr_read(), armreg_ttbr1_read());
952 #else
953 printf(" (TTBR0=%#x)", armreg_ttbr_read());
954 #endif
955 #endif
956
957 #ifdef MULTIPROCESSOR
958 /*
959 * Kick the secondaries to load the TTB. After which they'll go
960 * back to sleep to wait for the final kick so they will hatch.
961 */
962 #ifdef VERBOSE_INIT_ARM
963 printf(" hatchlings");
964 #endif
965 cpu_boot_secondary_processors();
966 #endif
967
968 #ifdef VERBOSE_INIT_ARM
969 printf(" OK\n");
970 #endif
971 }
972