2 * Copyright (c) 1994-1998 Mark Brinicombe.
3 * Copyright (c) 1994 Brini.
6 * This code is derived from software written for Brini by Mark Brinicombe
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Brini.
19 * 4. The name of the company nor the name of the author may be used to
20 * endorse or promote products derived from this software without specific
21 * prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * from: FreeBSD: //depot/projects/arm/src/sys/arm/at91/kb920x_machdep.c, rev 45
38 #include "opt_msgbuf.h"
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #define _ARM32_BUS_DMA_PRIVATE
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/sysproto.h>
48 #include <sys/signalvar.h>
49 #include <sys/imgact.h>
50 #include <sys/kernel.h>
52 #include <sys/linker.h>
54 #include <sys/malloc.h>
55 #include <sys/mutex.h>
58 #include <sys/ptrace.h>
65 #include <sys/msgbuf.h>
66 #include <machine/reg.h>
67 #include <machine/cpu.h>
71 #include <vm/vm_object.h>
72 #include <vm/vm_page.h>
73 #include <vm/vm_pager.h>
74 #include <vm/vm_map.h>
75 #include <vm/vnode_pager.h>
76 #include <machine/pte.h>
77 #include <machine/pmap.h>
78 #include <machine/vmparam.h>
79 #include <machine/pcb.h>
80 #include <machine/undefined.h>
81 #include <machine/machdep.h>
82 #include <machine/metadata.h>
83 #include <machine/armreg.h>
84 #include <machine/bus.h>
85 #include <sys/reboot.h>
86 #include <machine/bootinfo.h>
88 #include <arm/mv/mvvar.h> /* XXX eventually this should be eliminated */
91 #define debugf(fmt, args...) printf(fmt, ##args)
93 #define debugf(fmt, args...)
97 * This is the number of L2 page tables required for covering max
98 * (hypothetical) memsize of 4GB and all kernel mappings (vectors, msgbuf,
99 * stacks etc.), uprounded to be divisible by 4.
101 #define KERNEL_PT_MAX 78
103 /* Define various stack sizes in pages */
104 #define IRQ_STACK_SIZE 1
105 #define ABT_STACK_SIZE 1
106 #define UND_STACK_SIZE 1
108 /* Maximum number of memory regions */
109 #define MEM_REGIONS 8
111 extern unsigned char kernbase[];
112 extern unsigned char _etext[];
113 extern unsigned char _edata[];
114 extern unsigned char __bss_start[];
115 extern unsigned char _end[];
117 extern u_int data_abort_handler_address;
118 extern u_int prefetch_abort_handler_address;
119 extern u_int undefined_handler_address;
121 extern const struct pmap_devmap *pmap_devmap_bootstrap_table;
122 extern vm_offset_t pmap_bootstrap_lastaddr;
124 struct pv_addr kernel_pt_table[KERNEL_PT_MAX];
129 struct pcpu *pcpup = &__pcpu;
131 /* Physical and virtual addresses for some global pages */
133 vm_paddr_t phys_avail[10];
134 vm_paddr_t dump_avail[4];
135 vm_offset_t physical_pages;
137 struct pv_addr systempage;
138 struct pv_addr msgbufpv;
139 struct pv_addr irqstack;
140 struct pv_addr undstack;
141 struct pv_addr abtstack;
142 struct pv_addr kernelstack;
144 static struct trapframe proc0_tf;
147 vm_offset_t mr_start;
151 static struct mem_region availmem_regions[MEM_REGIONS];
152 static int availmem_regions_sz;
154 struct bootinfo *bootinfo;
156 static void print_kenv(void);
157 static void print_kernel_section_addr(void);
158 static void print_bootinfo(void);
160 static void physmap_init(int);
182 debugf("loader passed (static) kenv:\n");
183 if (kern_envp == NULL) {
184 debugf(" no env, null ptr\n");
187 debugf(" kern_envp = 0x%08x\n", (uint32_t)kern_envp);
190 for (cp = kern_envp; cp != NULL; cp = kenv_next(cp))
191 debugf(" %x %s\n", (uint32_t)cp, cp);
197 struct bi_mem_region *mr;
198 struct bi_eth_addr *eth;
201 debugf("bootinfo:\n");
202 if (bootinfo == NULL) {
203 debugf(" no bootinfo, null ptr\n");
207 debugf(" version = 0x%08x\n", bootinfo->bi_version);
208 debugf(" ccsrbar = 0x%08x\n", bootinfo->bi_bar_base);
209 debugf(" cpu_clk = 0x%08x\n", bootinfo->bi_cpu_clk);
210 debugf(" bus_clk = 0x%08x\n", bootinfo->bi_bus_clk);
212 debugf(" mem regions:\n");
213 mr = (struct bi_mem_region *)bootinfo->bi_data;
214 for (i = 0; i < bootinfo->bi_mem_reg_no; i++, mr++)
215 debugf(" #%d, base = 0x%08x, size = 0x%08x\n", i,
216 mr->mem_base, mr->mem_size);
218 debugf(" eth addresses:\n");
219 eth = (struct bi_eth_addr *)mr;
220 for (i = 0; i < bootinfo->bi_eth_addr_no; i++, eth++) {
221 debugf(" #%d, addr = ", i);
222 for (j = 0; j < 6; j++)
223 debugf("%02x ", eth->mac_addr[j]);
229 print_kernel_section_addr(void)
232 debugf("kernel image addresses:\n");
233 debugf(" kernbase = 0x%08x\n", (uint32_t)kernbase);
234 debugf(" _etext (sdata) = 0x%08x\n", (uint32_t)_etext);
235 debugf(" _edata = 0x%08x\n", (uint32_t)_edata);
236 debugf(" __bss_start = 0x%08x\n", (uint32_t)__bss_start);
237 debugf(" _end = 0x%08x\n", (uint32_t)_end);
240 struct bi_mem_region *
244 return ((struct bi_mem_region *)bootinfo->bi_data);
248 physmap_init(int hardcoded)
251 vm_offset_t phys_kernelend, kernload;
253 struct mem_region *mp, *mp1;
255 phys_kernelend = KERNPHYSADDR + (virtual_avail - KERNVIRTADDR);
256 kernload = KERNPHYSADDR;
259 * Use hardcoded physical addresses if we don't use memory regions
264 phys_avail[1] = kernload;
266 phys_avail[2] = phys_kernelend;
267 phys_avail[3] = PHYSMEM_SIZE;
275 * Remove kernel physical address range from avail
276 * regions list. Page align all regions.
277 * Non-page aligned memory isn't very interesting to us.
278 * Also, sort the entries for ascending addresses.
281 cnt = availmem_regions_sz;
282 debugf("processing avail regions:\n");
283 for (mp = availmem_regions; mp->mr_size; mp++) {
285 e = mp->mr_start + mp->mr_size;
286 debugf(" %08x-%08x -> ", s, e);
287 /* Check whether this region holds all of the kernel. */
288 if (s < kernload && e > phys_kernelend) {
289 availmem_regions[cnt].mr_start = phys_kernelend;
290 availmem_regions[cnt++].mr_size = e - phys_kernelend;
293 /* Look whether this regions starts within the kernel. */
294 if (s >= kernload && s < phys_kernelend) {
295 if (e <= phys_kernelend)
299 /* Now look whether this region ends within the kernel. */
300 if (e > kernload && e <= phys_kernelend) {
306 /* Now page align the start and size of the region. */
312 debugf("%08x-%08x = %x\n", s, e, sz);
314 /* Check whether some memory is left here. */
317 printf("skipping\n");
319 (cnt - (mp - availmem_regions)) * sizeof(*mp));
325 /* Do an insertion sort. */
326 for (mp1 = availmem_regions; mp1 < mp; mp1++)
327 if (s < mp1->mr_start)
330 bcopy(mp1, mp1 + 1, (char *)mp - (char *)mp1);
338 availmem_regions_sz = cnt;
340 /* Fill in phys_avail table, based on availmem_regions */
341 debugf("fill in phys_avail:\n");
342 for (i = 0, j = 0; i < availmem_regions_sz; i++, j += 2) {
344 debugf(" region: 0x%08x - 0x%08x (0x%08x)\n",
345 availmem_regions[i].mr_start,
346 availmem_regions[i].mr_start + availmem_regions[i].mr_size,
347 availmem_regions[i].mr_size);
349 phys_avail[j] = availmem_regions[i].mr_start;
350 phys_avail[j + 1] = availmem_regions[i].mr_start +
351 availmem_regions[i].mr_size;
354 phys_avail[j + 1] = 0;
358 initarm(void *mdp, void *unused __unused)
360 struct pv_addr kernel_l1pt;
361 struct pv_addr dpcpu;
362 vm_offset_t freemempos, l2_start, lastaddr;
363 uint32_t memsize, l2size;
364 struct bi_mem_region *mr;
376 * Mask metadata pointer: it is supposed to be on page boundary. If
377 * the first argument (mdp) doesn't point to a valid address the
378 * bootloader must have passed us something else than the metadata
379 * ptr... In this case we want to fall back to some built-in settings.
381 mdp = (void *)((uint32_t)mdp & ~PAGE_MASK);
383 /* Parse metadata and fetch parameters */
385 preload_metadata = mdp;
386 kmdp = preload_search_by_type("elf kernel");
388 bootinfo = (struct bootinfo *)preload_search_info(kmdp,
389 MODINFO_METADATA|MODINFOMD_BOOTINFO);
391 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
392 kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
393 lastaddr = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
396 /* Initialize memory regions table */
398 for (i = 0; i < bootinfo->bi_mem_reg_no; i++, mr++) {
399 if (i == MEM_REGIONS)
401 availmem_regions[i].mr_start = mr->mem_base;
402 availmem_regions[i].mr_size = mr->mem_size;
403 memsize += mr->mem_size;
405 availmem_regions_sz = i;
407 /* Fall back to hardcoded boothowto flags and metadata. */
408 boothowto = RB_VERBOSE | RB_SINGLE;
409 lastaddr = fake_preload_metadata();
412 * Assume a single memory region of size specified in board
413 * configuration file.
415 memsize = PHYSMEM_SIZE;
419 * If memsize is invalid, we can neither proceed nor panic (too
420 * early for console output).
425 /* Platform-specific initialisation */
426 if (platform_pmap_init() != 0)
429 pcpu_init(pcpup, 0, sizeof(struct pcpu));
430 PCPU_SET(curthread, &thread0);
432 /* Calculate number of L2 tables needed for mapping vm_page_array */
433 l2size = (memsize / PAGE_SIZE) * sizeof(struct vm_page);
434 l2size = (l2size >> L1_S_SHIFT) + 1;
437 * Add one table for end of kernel map, one for stacks, msgbuf and
438 * L1 and L2 tables map and one for vectors map.
442 /* Make it divisible by 4 */
443 l2size = (l2size + 3) & ~3;
445 #define KERNEL_TEXT_BASE (KERNBASE)
446 freemempos = (lastaddr + PAGE_MASK) & ~PAGE_MASK;
448 /* Define a macro to simplify memory allocation */
449 #define valloc_pages(var, np) \
450 alloc_pages((var).pv_va, (np)); \
451 (var).pv_pa = (var).pv_va + (KERNPHYSADDR - KERNVIRTADDR);
453 #define alloc_pages(var, np) \
454 (var) = freemempos; \
455 freemempos += (np * PAGE_SIZE); \
456 memset((char *)(var), 0, ((np) * PAGE_SIZE));
458 while (((freemempos - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) != 0)
459 freemempos += PAGE_SIZE;
460 valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
462 for (i = 0; i < l2size; ++i) {
463 if (!(i % (PAGE_SIZE / L2_TABLE_SIZE_REAL))) {
464 valloc_pages(kernel_pt_table[i],
465 L2_TABLE_SIZE / PAGE_SIZE);
468 kernel_pt_table[i].pv_va = kernel_pt_table[j].pv_va +
469 L2_TABLE_SIZE_REAL * (i - j);
470 kernel_pt_table[i].pv_pa =
471 kernel_pt_table[i].pv_va - KERNVIRTADDR +
477 * Allocate a page for the system page mapped to 0x00000000
478 * or 0xffff0000. This page will just contain the system vectors
479 * and can be shared by all processes.
481 valloc_pages(systempage, 1);
483 /* Allocate dynamic per-cpu area. */
484 valloc_pages(dpcpu, DPCPU_SIZE / PAGE_SIZE);
485 dpcpu_init((void *)dpcpu.pv_va, 0);
487 /* Allocate stacks for all modes */
488 valloc_pages(irqstack, IRQ_STACK_SIZE);
489 valloc_pages(abtstack, ABT_STACK_SIZE);
490 valloc_pages(undstack, UND_STACK_SIZE);
491 valloc_pages(kernelstack, KSTACK_PAGES);
492 valloc_pages(msgbufpv, round_page(MSGBUF_SIZE) / PAGE_SIZE);
495 * Now we start construction of the L1 page table
496 * We start by mapping the L2 page tables into the L1.
497 * This means that we can replace L1 mappings later on if necessary
499 l1pagetable = kernel_l1pt.pv_va;
502 * Try to map as much as possible of kernel text and data using
503 * 1MB section mapping and for the rest of initial kernel address
504 * space use L2 coarse tables.
506 * Link L2 tables for mapping remainder of kernel (modulo 1MB)
507 * and kernel structures
509 l2_start = lastaddr & ~(L1_S_OFFSET);
510 for (i = 0 ; i < l2size - 1; i++)
511 pmap_link_l2pt(l1pagetable, l2_start + i * L1_S_SIZE,
512 &kernel_pt_table[i]);
514 pmap_curmaxkvaddr = l2_start + (l2size - 1) * L1_S_SIZE;
516 /* Map kernel code and data */
517 pmap_map_chunk(l1pagetable, KERNVIRTADDR, KERNPHYSADDR,
518 (((uint32_t)(lastaddr) - KERNVIRTADDR) + PAGE_MASK) & ~PAGE_MASK,
519 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
522 /* Map L1 directory and allocated L2 page tables */
523 pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
524 L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
526 pmap_map_chunk(l1pagetable, kernel_pt_table[0].pv_va,
527 kernel_pt_table[0].pv_pa,
528 L2_TABLE_SIZE_REAL * l2size,
529 VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
531 /* Map allocated DPCPU, stacks and msgbuf */
532 pmap_map_chunk(l1pagetable, dpcpu.pv_va, dpcpu.pv_pa,
533 freemempos - dpcpu.pv_va,
534 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
536 /* Link and map the vector page */
537 pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH,
538 &kernel_pt_table[l2size - 1]);
539 pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
540 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
542 pmap_devmap_bootstrap(l1pagetable, pmap_devmap_bootstrap_table);
543 cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2)) |
545 setttb(kernel_l1pt.pv_pa);
547 cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2));
549 physmem = memsize / PAGE_SIZE;
551 debugf("initarm: console initialized\n");
552 debugf(" arg1 mdp = 0x%08x\n", (uint32_t)mdp);
553 debugf(" boothowto = 0x%08x\n", boothowto);
555 print_kernel_section_addr();
564 * Re-initialise decode windows
566 if (soc_decode_win() != 0)
567 printf("WARNING: could not re-initialise decode windows! "
568 "Running with existing settings...\n");
570 * Pages were allocated during the secondary bootstrap for the
571 * stacks for different CPU modes.
572 * We must now set the r13 registers in the different CPU modes to
573 * point to these stacks.
574 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
575 * of the stack memory.
577 cpu_control(CPU_CONTROL_MMU_ENABLE, CPU_CONTROL_MMU_ENABLE);
578 set_stackptr(PSR_IRQ32_MODE,
579 irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
580 set_stackptr(PSR_ABT32_MODE,
581 abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
582 set_stackptr(PSR_UND32_MODE,
583 undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
586 * We must now clean the cache again....
587 * Cleaning may be done by reading new data to displace any
588 * dirty data in the cache. This will have happened in setttb()
589 * but since we are boot strapping the addresses used for the read
590 * may have just been remapped and thus the cache could be out
591 * of sync. A re-clean after the switch will cure this.
592 * After booting there are no gross relocations of the kernel thus
593 * this problem will not occur after initarm().
595 cpu_idcache_wbinv_all();
597 /* Set stack for exception handlers */
598 data_abort_handler_address = (u_int)data_abort_handler;
599 prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
600 undefined_handler_address = (u_int)undefinedinstruction_bounce;
603 proc_linkup0(&proc0, &thread0);
604 thread0.td_kstack = kernelstack.pv_va;
605 thread0.td_kstack_pages = KSTACK_PAGES;
606 thread0.td_pcb = (struct pcb *)
607 (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
608 thread0.td_pcb->pcb_flags = 0;
609 thread0.td_frame = &proc0_tf;
610 pcpup->pc_curpcb = thread0.td_pcb;
612 arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
615 dump_avail[1] = memsize;
619 pmap_bootstrap(freemempos, pmap_bootstrap_lastaddr, &kernel_l1pt);
620 msgbufp = (void *)msgbufpv.pv_va;
621 msgbufinit(msgbufp, MSGBUF_SIZE);
625 * Prepare map of physical memory regions available to vm subsystem.
626 * If metadata pointer doesn't point to a valid address, use hardcoded
629 physmap_init((mdp != NULL) ? 0 : 1);
631 /* Do basic tuning, hz etc */
633 init_param2(physmem);
635 return ((void *)(kernelstack.pv_va + USPACE_SVC_STACK_TOP -
636 sizeof(struct pcb)));
639 struct arm32_dma_range *
640 bus_dma_get_range(void)
647 bus_dma_get_range_nb(void)