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 * RiscBSD kernel project
39 * Machine dependant functions for kernel setup
41 * This file needs a lot of work.
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
51 #define _ARM32_BUS_DMA_PRIVATE
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/sysproto.h>
55 #include <sys/signalvar.h>
56 #include <sys/imgact.h>
57 #include <sys/kernel.h>
59 #include <sys/linker.h>
61 #include <sys/malloc.h>
62 #include <sys/mutex.h>
65 #include <sys/ptrace.h>
72 #include <sys/msgbuf.h>
73 #include <machine/physmem.h>
74 #include <machine/reg.h>
75 #include <machine/cpu.h>
79 #include <vm/vm_object.h>
80 #include <vm/vm_page.h>
81 #include <vm/vm_map.h>
82 #include <machine/devmap.h>
83 #include <machine/vmparam.h>
84 #include <machine/pcb.h>
85 #include <machine/undefined.h>
86 #include <machine/machdep.h>
87 #include <machine/metadata.h>
88 #include <machine/armreg.h>
89 #include <machine/bus.h>
90 #include <sys/reboot.h>
92 #include <arm/samsung/s3c2xx0/s3c24x0var.h>
93 #include <arm/samsung/s3c2xx0/s3c2410reg.h>
94 #include <arm/samsung/s3c2xx0/s3c2xx0board.h>
96 /* Page table for mapping proc0 zero page */
97 #define KERNEL_PT_SYS 0
98 #define KERNEL_PT_KERN 1
99 #define KERNEL_PT_KERN_NUM 44
100 /* L2 table for mapping after kernel */
101 #define KERNEL_PT_AFKERNEL KERNEL_PT_KERN + KERNEL_PT_KERN_NUM
102 #define KERNEL_PT_AFKERNEL_NUM 5
104 /* this should be evenly divisable by PAGE_SIZE / L2_TABLE_SIZE_REAL (or 4) */
105 #define NUM_KERNEL_PTS (KERNEL_PT_AFKERNEL + KERNEL_PT_AFKERNEL_NUM)
107 extern int s3c2410_pclk;
109 struct pv_addr kernel_pt_table[NUM_KERNEL_PTS];
111 /* Physical and virtual addresses for some global pages */
113 struct pv_addr systempage;
114 struct pv_addr msgbufpv;
115 struct pv_addr irqstack;
116 struct pv_addr undstack;
117 struct pv_addr abtstack;
118 struct pv_addr kernelstack;
120 #define _A(a) ((a) & ~L1_S_OFFSET)
121 #define _S(s) (((s) + L1_S_SIZE - 1) & ~(L1_S_SIZE-1))
123 /* Static device mappings. */
124 static const struct arm_devmap_entry s3c24x0_devmap[] = {
126 * Map the devices we need early on.
129 _A(S3C24X0_CLKMAN_BASE),
130 _A(S3C24X0_CLKMAN_PA_BASE),
131 _S(S3C24X0_CLKMAN_SIZE),
132 VM_PROT_READ|VM_PROT_WRITE,
136 _A(S3C24X0_GPIO_BASE),
137 _A(S3C24X0_GPIO_PA_BASE),
138 _S(S3C2410_GPIO_SIZE),
139 VM_PROT_READ|VM_PROT_WRITE,
143 _A(S3C24X0_INTCTL_BASE),
144 _A(S3C24X0_INTCTL_PA_BASE),
145 _S(S3C24X0_INTCTL_SIZE),
146 VM_PROT_READ|VM_PROT_WRITE,
150 _A(S3C24X0_TIMER_BASE),
151 _A(S3C24X0_TIMER_PA_BASE),
152 _S(S3C24X0_TIMER_SIZE),
153 VM_PROT_READ|VM_PROT_WRITE,
157 _A(S3C24X0_UART0_BASE),
158 _A(S3C24X0_UART0_PA_BASE),
159 _S(S3C24X0_UART_PA_BASE(3) - S3C24X0_UART0_PA_BASE),
160 VM_PROT_READ|VM_PROT_WRITE,
164 _A(S3C24X0_WDT_BASE),
165 _A(S3C24X0_WDT_PA_BASE),
166 _S(S3C24X0_WDT_SIZE),
167 VM_PROT_READ|VM_PROT_WRITE,
182 #define ioreg_read32(a) (*(volatile uint32_t *)(a))
183 #define ioreg_write32(a,v) (*(volatile uint32_t *)(a)=(v))
185 struct arm32_dma_range s3c24x0_range = {
191 struct arm32_dma_range *
192 bus_dma_get_range(void)
195 if (s3c24x0_range.dr_len == 0) {
196 s3c24x0_range.dr_sysbase = dump_avail[0];
197 s3c24x0_range.dr_busbase = dump_avail[0];
198 s3c24x0_range.dr_len = dump_avail[1] - dump_avail[0];
200 return (&s3c24x0_range);
204 bus_dma_get_range_nb(void)
210 initarm(struct arm_boot_params *abp)
212 struct pv_addr kernel_l1pt;
215 vm_offset_t freemempos;
216 vm_offset_t afterkern;
217 vm_offset_t lastaddr;
222 boothowto = 0; /* Likely not needed */
223 lastaddr = parse_boot_param(abp);
224 arm_physmem_kernaddr = abp->abp_physaddr;
227 cpufuncs.cf_sleep = s3c24x0_sleep;
231 /* Do basic tuning, hz etc */
234 #define KERNEL_TEXT_BASE (KERNBASE)
235 freemempos = (lastaddr + PAGE_MASK) & ~PAGE_MASK;
236 /* Define a macro to simplify memory allocation */
237 #define valloc_pages(var, np) \
238 alloc_pages((var).pv_va, (np)); \
239 (var).pv_pa = (var).pv_va + (abp->abp_physaddr - KERNVIRTADDR);
241 #define alloc_pages(var, np) \
242 (var) = freemempos; \
243 freemempos += (np * PAGE_SIZE); \
244 memset((char *)(var), 0, ((np) * PAGE_SIZE));
246 while (((freemempos - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) != 0)
247 freemempos += PAGE_SIZE;
248 valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
249 for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
250 if (!(loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL))) {
251 valloc_pages(kernel_pt_table[loop],
252 L2_TABLE_SIZE / PAGE_SIZE);
254 kernel_pt_table[loop].pv_va = freemempos -
255 (loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL)) *
257 kernel_pt_table[loop].pv_pa =
258 kernel_pt_table[loop].pv_va - KERNVIRTADDR +
263 * Allocate a page for the system page mapped to V0x00000000
264 * This page will just contain the system vectors and can be
265 * shared by all processes.
267 valloc_pages(systempage, 1);
269 /* Allocate stacks for all modes */
270 valloc_pages(irqstack, IRQ_STACK_SIZE);
271 valloc_pages(abtstack, ABT_STACK_SIZE);
272 valloc_pages(undstack, UND_STACK_SIZE);
273 valloc_pages(kernelstack, KSTACK_PAGES);
274 valloc_pages(msgbufpv, round_page(msgbufsize) / PAGE_SIZE);
276 * Now we start construction of the L1 page table
277 * We start by mapping the L2 page tables into the L1.
278 * This means that we can replace L1 mappings later on if necessary
280 l1pagetable = kernel_l1pt.pv_va;
282 /* Map the L2 pages tables in the L1 page table */
283 pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH,
284 &kernel_pt_table[KERNEL_PT_SYS]);
285 for (i = 0; i < KERNEL_PT_KERN_NUM; i++)
286 pmap_link_l2pt(l1pagetable, KERNBASE + i * L1_S_SIZE,
287 &kernel_pt_table[KERNEL_PT_KERN + i]);
288 pmap_map_chunk(l1pagetable, KERNBASE, PHYSADDR,
289 (((uint32_t)(lastaddr) - KERNBASE) + PAGE_SIZE) & ~(PAGE_SIZE - 1),
290 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
291 afterkern = round_page((lastaddr + L1_S_SIZE) & ~(L1_S_SIZE
293 for (i = 0; i < KERNEL_PT_AFKERNEL_NUM; i++) {
294 pmap_link_l2pt(l1pagetable, afterkern + i * L1_S_SIZE,
295 &kernel_pt_table[KERNEL_PT_AFKERNEL + i]);
298 /* Map the vector page. */
299 pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
300 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
301 /* Map the stack pages */
302 pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
303 IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
304 pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
305 ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
306 pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
307 UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
308 pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
309 KSTACK_PAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
311 pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
312 L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
313 pmap_map_chunk(l1pagetable, msgbufpv.pv_va, msgbufpv.pv_pa,
314 msgbufsize, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
317 for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
318 pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
319 kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
320 VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
323 arm_devmap_bootstrap(l1pagetable, s3c24x0_devmap);
325 cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
326 setttb(kernel_l1pt.pv_pa);
328 cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
331 * Pages were allocated during the secondary bootstrap for the
332 * stacks for different CPU modes.
333 * We must now set the r13 registers in the different CPU modes to
334 * point to these stacks.
335 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
336 * of the stack memory.
339 cpu_control(CPU_CONTROL_MMU_ENABLE, CPU_CONTROL_MMU_ENABLE);
343 * We must now clean the cache again....
344 * Cleaning may be done by reading new data to displace any
345 * dirty data in the cache. This will have happened in setttb()
346 * but since we are boot strapping the addresses used for the read
347 * may have just been remapped and thus the cache could be out
348 * of sync. A re-clean after the switch will cure this.
349 * After booting there are no gross relocations of the kernel thus
350 * this problem will not occur after initarm().
352 cpu_idcache_wbinv_all();
355 /* Disable all peripheral interrupts */
356 ioreg_write32(S3C24X0_INTCTL_BASE + INTCTL_INTMSK, ~0);
357 memsize = board_init();
358 /* Find pclk for uart */
359 switch(ioreg_read32(S3C24X0_GPIO_BASE + GPIO_GSTATUS1) >> 16) {
361 s3c2410_clock_freq2(S3C24X0_CLKMAN_BASE, NULL, NULL,
365 s3c2440_clock_freq2(S3C24X0_CLKMAN_BASE, NULL, NULL,
373 init_proc0(kernelstack.pv_va);
375 arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
377 pmap_curmaxkvaddr = afterkern + 0x100000 * (KERNEL_PT_KERN_NUM - 1);
378 vm_max_kernel_address = KERNVIRTADDR + 3 * memsize;
379 pmap_bootstrap(freemempos, &kernel_l1pt);
380 msgbufp = (void*)msgbufpv.pv_va;
381 msgbufinit(msgbufp, msgbufsize);
385 * Add the physical ram we have available.
387 * Exclude the kernel, and all the things we allocated which immediately
388 * follow the kernel, from the VM allocation pool but not from crash
389 * dumps. virtual_avail is a global variable which tracks the kva we've
390 * "allocated" while setting up pmaps.
392 * Prepare the list of physical memory available to the vm subsystem.
394 arm_physmem_hardware_region(PHYSADDR, memsize);
395 arm_physmem_exclude_region(abp->abp_physaddr,
396 virtual_avail - KERNVIRTADDR, EXFLAG_NOALLOC);
397 arm_physmem_init_kernel_globals();
399 init_param2(physmem);
402 return ((void *)(kernelstack.pv_va + USPACE_SVC_STACK_TOP -
403 sizeof(struct pcb)));