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