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