board_bwct.c revision 157029
1155324Simp/*-
2155324Simp * Copyright (c) 1994-1998 Mark Brinicombe.
3155324Simp * Copyright (c) 1994 Brini.
4155324Simp * All rights reserved.
5155324Simp *
6155324Simp * This code is derived from software written for Brini by Mark Brinicombe
7155324Simp *
8155324Simp * Redistribution and use in source and binary forms, with or without
9155324Simp * modification, are permitted provided that the following conditions
10155324Simp * are met:
11155324Simp * 1. Redistributions of source code must retain the above copyright
12155324Simp *    notice, this list of conditions and the following disclaimer.
13155324Simp * 2. Redistributions in binary form must reproduce the above copyright
14155324Simp *    notice, this list of conditions and the following disclaimer in the
15155324Simp *    documentation and/or other materials provided with the distribution.
16155324Simp * 3. All advertising materials mentioning features or use of this software
17155324Simp *    must display the following acknowledgement:
18155324Simp *      This product includes software developed by Brini.
19155324Simp * 4. The name of the company nor the name of the author may be used to
20155324Simp *    endorse or promote products derived from this software without specific
21155324Simp *    prior written permission.
22155324Simp *
23155324Simp * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
24155324Simp * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25155324Simp * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26155324Simp * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27155324Simp * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28155324Simp * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29155324Simp * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30155324Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31155324Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32155324Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33155324Simp * SUCH DAMAGE.
34155324Simp *
35155324Simp * RiscBSD kernel project
36155324Simp *
37155324Simp * machdep.c
38155324Simp *
39155324Simp * Machine dependant functions for kernel setup
40155324Simp *
41155324Simp * This file needs a lot of work.
42155324Simp *
43155324Simp * Created      : 17/09/94
44155324Simp */
45155324Simp
46155324Simp#include "opt_msgbuf.h"
47155324Simp#include "opt_ddb.h"
48155324Simp
49155324Simp#include <sys/cdefs.h>
50155324Simp__FBSDID("$FreeBSD: head/sys/arm/at91/kb920x_machdep.c 157029 2006-03-22 22:31:31Z cognet $");
51155324Simp
52155324Simp#define _ARM32_BUS_DMA_PRIVATE
53155324Simp#include <sys/param.h>
54155324Simp#include <sys/systm.h>
55155324Simp#include <sys/sysproto.h>
56155324Simp#include <sys/signalvar.h>
57155324Simp#include <sys/imgact.h>
58155324Simp#include <sys/kernel.h>
59155324Simp#include <sys/ktr.h>
60155324Simp#include <sys/linker.h>
61155324Simp#include <sys/lock.h>
62155324Simp#include <sys/malloc.h>
63155324Simp#include <sys/mutex.h>
64155324Simp#include <sys/pcpu.h>
65155324Simp#include <sys/proc.h>
66155324Simp#include <sys/ptrace.h>
67155324Simp#include <sys/cons.h>
68155324Simp#include <sys/bio.h>
69155324Simp#include <sys/bus.h>
70155324Simp#include <sys/buf.h>
71155324Simp#include <sys/exec.h>
72155324Simp#include <sys/kdb.h>
73155324Simp#include <sys/msgbuf.h>
74155324Simp#include <machine/reg.h>
75155324Simp#include <machine/cpu.h>
76155324Simp
77155324Simp#include <vm/vm.h>
78155324Simp#include <vm/pmap.h>
79155324Simp#include <vm/vm.h>
80155324Simp#include <vm/vm_object.h>
81155324Simp#include <vm/vm_page.h>
82155324Simp#include <vm/vm_pager.h>
83155324Simp#include <vm/vm_map.h>
84155324Simp#include <vm/vnode_pager.h>
85155324Simp#include <machine/pmap.h>
86155324Simp#include <machine/vmparam.h>
87155324Simp#include <machine/pcb.h>
88155324Simp#include <machine/undefined.h>
89155324Simp#include <machine/machdep.h>
90155324Simp#include <machine/metadata.h>
91155324Simp#include <machine/armreg.h>
92155324Simp#include <machine/bus.h>
93155324Simp#include <sys/reboot.h>
94155324Simp
95155324Simp#include <arm/at91/at91rm92reg.h>
96155324Simp
97155324Simp#define KERNEL_PT_SYS		0	/* Page table for mapping proc0 zero page */
98155324Simp#define KERNEL_PT_KERN		1
99155324Simp#define KERNEL_PT_KERN_NUM	8
100155324Simp#define KERNEL_PT_AFKERNEL	KERNEL_PT_KERN + KERNEL_PT_KERN_NUM	/* L2 table for mapping after kernel */
101155324Simp#define	KERNEL_PT_AFKERNEL_NUM	5
102155324Simp
103155324Simp/* this should be evenly divisable by PAGE_SIZE / L2_TABLE_SIZE_REAL (or 4) */
104155324Simp#define NUM_KERNEL_PTS		(KERNEL_PT_AFKERNEL + KERNEL_PT_AFKERNEL_NUM)
105155324Simp
106155324Simp/* Define various stack sizes in pages */
107155324Simp#define IRQ_STACK_SIZE	1
108155324Simp#define ABT_STACK_SIZE	1
109155324Simp#define UND_STACK_SIZE	1
110155324Simp
111155324Simpextern u_int data_abort_handler_address;
112155324Simpextern u_int prefetch_abort_handler_address;
113155324Simpextern u_int undefined_handler_address;
114155324Simp
115155324Simpstruct pv_addr kernel_pt_table[NUM_KERNEL_PTS];
116155324Simp
117155324Simpextern void *_end;
118155324Simp
119155324Simpextern vm_offset_t sa1_cache_clean_addr;
120155324Simp
121155324Simpextern int *end;
122155324Simp
123155324Simpstruct pcpu __pcpu;
124155324Simpstruct pcpu *pcpup = &__pcpu;
125155324Simp
126155324Simp/* Physical and virtual addresses for some global pages */
127155324Simp
128155324Simpvm_paddr_t phys_avail[10];
129155324Simpvm_paddr_t dump_avail[4];
130155324Simpvm_offset_t physical_pages;
131155324Simpvm_offset_t clean_sva, clean_eva;
132155324Simp
133155324Simpstruct pv_addr systempage;
134155324Simpstruct pv_addr msgbufpv;
135155324Simpstruct pv_addr irqstack;
136155324Simpstruct pv_addr undstack;
137155324Simpstruct pv_addr abtstack;
138155324Simpstruct pv_addr kernelstack;
139155324Simpstruct pv_addr minidataclean;
140155324Simp
141155324Simpstatic struct trapframe proc0_tf;
142155324Simp
143155324Simp/* Static device mappings. */
144155324Simpstatic const struct pmap_devmap kb920x_devmap[] = {
145155324Simp	/*
146155324Simp	 * Map the on-board devices VA == PA so that we can access them
147155324Simp	 * with the MMU on or off.
148155324Simp	 */
149156832Simp	{
150156832Simp		/*
151156832Simp		 * This at least maps the interrupt controller, the UART
152156832Simp		 * and the timer. Other devices should use newbus to
153156832Simp		 * map their memory anyway.
154156832Simp		 */
155156832Simp		0xfff00000,
156156832Simp		0xfff00000,
157156832Simp		0x100000,
158156832Simp		VM_PROT_READ|VM_PROT_WRITE,
159156832Simp		PTE_NOCACHE,
160156832Simp	},
161156832Simp	{
162156832Simp		/*
163156832Simp		 * Add the ohci controller, and anything else that might be
164156832Simp		 * on this chip select for a VA/PA mapping.
165156832Simp		 */
166156832Simp		AT91RM92_OHCI_BASE,
167156832Simp		AT91RM92_OHCI_BASE,
168156832Simp		AT91RM92_OHCI_SIZE,
169156832Simp		VM_PROT_READ|VM_PROT_WRITE,
170156832Simp		PTE_NOCACHE,
171156832Simp	},
172156832Simp	{
173156832Simp		0,
174156832Simp		0,
175156832Simp		0,
176156832Simp		0,
177156832Simp		0,
178156832Simp	}
179155324Simp};
180155324Simp
181155324Simp#define SDRAM_START 0xa0000000
182155324Simp
183155324Simp#ifdef DDB
184155324Simpextern vm_offset_t ksym_start, ksym_end;
185155324Simp#endif
186155324Simp
187155324Simpvoid *
188155324Simpinitarm(void *arg, void *arg2)
189155324Simp{
190155324Simp	struct pv_addr  kernel_l1pt;
191155324Simp	int loop;
192155324Simp	u_int l1pagetable;
193155324Simp	vm_offset_t freemempos;
194155324Simp	vm_offset_t afterkern;
195155324Simp	int i = 0;
196155324Simp	uint32_t fake_preload[35];
197155324Simp	uint32_t memsize = 32 * 1024 * 1024;
198157024Scognet	vm_offset_t lastaddr;
199157024Scognet#ifdef DDB
200157024Scognet	vm_offset_t zstart = 0, zend = 0;
201157024Scognet#endif
202155324Simp
203155324Simp	i = 0;
204155324Simp
205155324Simp	set_cpufuncs();
206155324Simp
207155324Simp	fake_preload[i++] = MODINFO_NAME;
208155324Simp	fake_preload[i++] = strlen("elf kernel") + 1;
209155324Simp	strcpy((char*)&fake_preload[i++], "elf kernel");
210155324Simp	i += 2;
211155324Simp	fake_preload[i++] = MODINFO_TYPE;
212155324Simp	fake_preload[i++] = strlen("elf kernel") + 1;
213155324Simp	strcpy((char*)&fake_preload[i++], "elf kernel");
214155324Simp	i += 2;
215155324Simp	fake_preload[i++] = MODINFO_ADDR;
216155324Simp	fake_preload[i++] = sizeof(vm_offset_t);
217155324Simp	fake_preload[i++] = KERNBASE;
218155324Simp	fake_preload[i++] = MODINFO_SIZE;
219155324Simp	fake_preload[i++] = sizeof(uint32_t);
220155324Simp	fake_preload[i++] = (uint32_t)&end - KERNBASE;
221157024Scognet#ifdef DDB
222157024Scognet	if (*(uint32_t *)KERNVIRTADDR == MAGIC_TRAMP_NUMBER) {
223157024Scognet		fake_preload[i++] = MODINFO_METADATA|MODINFOMD_SSYM;
224157024Scognet		fake_preload[i++] = sizeof(vm_offset_t);
225157024Scognet		fake_preload[i++] = *(uint32_t *)(KERNVIRTADDR + 4);
226157024Scognet		fake_preload[i++] = MODINFO_METADATA|MODINFOMD_ESYM;
227157024Scognet		fake_preload[i++] = sizeof(vm_offset_t);
228157024Scognet		fake_preload[i++] = *(uint32_t *)(KERNVIRTADDR + 8);
229157024Scognet		lastaddr = *(uint32_t *)(KERNVIRTADDR + 8);
230157024Scognet		zend = lastaddr;
231157024Scognet		zstart = *(uint32_t *)(KERNVIRTADDR + 4);
232157024Scognet		ksym_start = zstart;
233157024Scognet		ksym_end = zend;
234157024Scognet	} else
235157024Scognet#endif
236157024Scognet		lastaddr = (vm_offset_t)&end;
237157024Scognet
238155324Simp	fake_preload[i++] = 0;
239155324Simp	fake_preload[i] = 0;
240155324Simp	preload_metadata = (void *)fake_preload;
241155324Simp
242155324Simp
243155324Simp	pcpu_init(pcpup, 0, sizeof(struct pcpu));
244155324Simp	PCPU_SET(curthread, &thread0);
245155324Simp
246155324Simp#define KERNEL_TEXT_BASE (KERNBASE)
247157024Scognet	freemempos = (lastaddr + PAGE_MASK) & ~PAGE_MASK;
248155324Simp	/* Define a macro to simplify memory allocation */
249155324Simp#define valloc_pages(var, np)                   \
250155324Simp	alloc_pages((var).pv_va, (np));         \
251155324Simp	(var).pv_pa = (var).pv_va + (KERNPHYSADDR - KERNVIRTADDR);
252155324Simp
253155324Simp#define alloc_pages(var, np)			\
254155324Simp	(var) = freemempos;		\
255155324Simp	freemempos += (np * PAGE_SIZE);		\
256155324Simp	memset((char *)(var), 0, ((np) * PAGE_SIZE));
257155324Simp
258155324Simp	while (((freemempos - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) != 0)
259155324Simp		freemempos += PAGE_SIZE;
260155324Simp	valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
261155324Simp	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
262155324Simp		if (!(loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL))) {
263155324Simp			valloc_pages(kernel_pt_table[loop],
264155324Simp			    L2_TABLE_SIZE / PAGE_SIZE);
265155324Simp		} else {
266155324Simp			kernel_pt_table[loop].pv_va = freemempos -
267155324Simp			    (loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL)) *
268155324Simp			    L2_TABLE_SIZE_REAL;
269155324Simp			kernel_pt_table[loop].pv_pa =
270155324Simp			    kernel_pt_table[loop].pv_va - KERNVIRTADDR +
271155324Simp			    KERNPHYSADDR;
272155324Simp		}
273155324Simp		i++;
274155324Simp	}
275155324Simp	/*
276155324Simp	 * Allocate a page for the system page mapped to V0x00000000
277155324Simp	 * This page will just contain the system vectors and can be
278155324Simp	 * shared by all processes.
279155324Simp	 */
280155324Simp	valloc_pages(systempage, 1);
281155324Simp
282155324Simp	/* Allocate stacks for all modes */
283155324Simp	valloc_pages(irqstack, IRQ_STACK_SIZE);
284155324Simp	valloc_pages(abtstack, ABT_STACK_SIZE);
285155324Simp	valloc_pages(undstack, UND_STACK_SIZE);
286155324Simp	valloc_pages(kernelstack, KSTACK_PAGES);
287155324Simp	alloc_pages(minidataclean.pv_pa, 1);
288155324Simp	valloc_pages(msgbufpv, round_page(MSGBUF_SIZE) / PAGE_SIZE);
289155324Simp	/*
290155324Simp	 * Now we start construction of the L1 page table
291155324Simp	 * We start by mapping the L2 page tables into the L1.
292155324Simp	 * This means that we can replace L1 mappings later on if necessary
293155324Simp	 */
294155324Simp	l1pagetable = kernel_l1pt.pv_va;
295155324Simp
296155324Simp	/* Map the L2 pages tables in the L1 page table */
297155324Simp	pmap_link_l2pt(l1pagetable, ARM_VECTORS_LOW,
298155324Simp	    &kernel_pt_table[KERNEL_PT_SYS]);
299155324Simp	for (i = 0; i < KERNEL_PT_KERN_NUM; i++)
300155324Simp		pmap_link_l2pt(l1pagetable, KERNBASE + i * 0x100000,
301155324Simp		    &kernel_pt_table[KERNEL_PT_KERN + i]);
302155324Simp	pmap_map_chunk(l1pagetable, KERNBASE, KERNPHYSADDR,
303157024Scognet	   (((uint32_t)(lastaddr) - KERNBASE) + PAGE_SIZE) & ~(PAGE_SIZE - 1),
304155324Simp	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
305157024Scognet	afterkern = round_page((lastaddr + L1_S_SIZE) & ~(L1_S_SIZE
306155324Simp	    - 1));
307155324Simp	for (i = 0; i < KERNEL_PT_AFKERNEL_NUM; i++) {
308155324Simp		pmap_link_l2pt(l1pagetable, afterkern + i * 0x00100000,
309155324Simp		    &kernel_pt_table[KERNEL_PT_AFKERNEL + i]);
310155324Simp	}
311155324Simp	pmap_map_entry(l1pagetable, afterkern, minidataclean.pv_pa,
312155324Simp	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
313155324Simp
314155324Simp
315155324Simp	/* Map the vector page. */
316155324Simp	pmap_map_entry(l1pagetable, ARM_VECTORS_LOW, systempage.pv_pa,
317155324Simp	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
318155324Simp	/* Map the stack pages */
319155324Simp	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
320155324Simp	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
321155324Simp	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
322155324Simp	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
323155324Simp	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
324155324Simp	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
325155324Simp	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
326155324Simp	    KSTACK_PAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
327155324Simp
328155324Simp	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
329155324Simp	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
330155324Simp	pmap_map_chunk(l1pagetable, msgbufpv.pv_va, msgbufpv.pv_pa,
331155324Simp	    MSGBUF_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
332155324Simp
333155324Simp
334155324Simp	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
335155324Simp		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
336155324Simp		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
337155324Simp		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
338155324Simp	}
339155324Simp
340155324Simp	pmap_devmap_bootstrap(l1pagetable, kb920x_devmap);
341155324Simp	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
342155324Simp	setttb(kernel_l1pt.pv_pa);
343155324Simp	cpu_tlb_flushID();
344155324Simp	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
345157029Scognet	cninit();
346155324Simp
347155324Simp	/*
348155324Simp	 * Pages were allocated during the secondary bootstrap for the
349155324Simp	 * stacks for different CPU modes.
350155324Simp	 * We must now set the r13 registers in the different CPU modes to
351155324Simp	 * point to these stacks.
352155324Simp	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
353155324Simp	 * of the stack memory.
354155324Simp	 */
355155324Simp
356155324Simp	cpu_control(CPU_CONTROL_MMU_ENABLE, CPU_CONTROL_MMU_ENABLE);
357155324Simp	set_stackptr(PSR_IRQ32_MODE,
358155324Simp	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
359155324Simp	set_stackptr(PSR_ABT32_MODE,
360155324Simp	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
361155324Simp	set_stackptr(PSR_UND32_MODE,
362155324Simp	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
363155324Simp
364155324Simp
365155324Simp
366155324Simp	/*
367155324Simp	 * We must now clean the cache again....
368155324Simp	 * Cleaning may be done by reading new data to displace any
369155324Simp	 * dirty data in the cache. This will have happened in setttb()
370155324Simp	 * but since we are boot strapping the addresses used for the read
371155324Simp	 * may have just been remapped and thus the cache could be out
372155324Simp	 * of sync. A re-clean after the switch will cure this.
373155324Simp	 * After booting there are no gross reloations of the kernel thus
374155324Simp	 * this problem will not occur after initarm().
375155324Simp	 */
376155324Simp	cpu_idcache_wbinv_all();
377155324Simp
378155324Simp	/* Set stack for exception handlers */
379155324Simp
380155324Simp	data_abort_handler_address = (u_int)data_abort_handler;
381155324Simp	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
382155324Simp	undefined_handler_address = (u_int)undefinedinstruction_bounce;
383155324Simp	undefined_init();
384155324Simp
385155324Simp	proc_linkup(&proc0, &ksegrp0, &thread0);
386155324Simp	thread0.td_kstack = kernelstack.pv_va;
387155324Simp	thread0.td_pcb = (struct pcb *)
388155324Simp		(thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
389155324Simp	thread0.td_pcb->pcb_flags = 0;
390155324Simp	thread0.td_frame = &proc0_tf;
391155324Simp	pcpup->pc_curpcb = thread0.td_pcb;
392155324Simp
393155324Simp	arm_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
394155324Simp
395155324Simp	pmap_curmaxkvaddr = afterkern + 0x100000 * (KERNEL_PT_KERN_NUM - 1);
396155324Simp	pmap_bootstrap(freemempos,
397155324Simp	    KERNVIRTADDR + 3 * memsize,
398155324Simp	    &kernel_l1pt);
399155324Simp	msgbufp = (void*)msgbufpv.pv_va;
400155324Simp	msgbufinit(msgbufp, MSGBUF_SIZE);
401155324Simp	mutex_init();
402155324Simp
403155324Simp	i = 0;
404155324Simp	dump_avail[0] = KERNPHYSADDR;
405155324Simp	dump_avail[1] = KERNPHYSADDR + memsize;
406155324Simp	dump_avail[2] = 0;
407155324Simp	dump_avail[3] = 0;
408155324Simp
409155324Simp	phys_avail[0] = freemempos - KERNVIRTADDR + KERNPHYSADDR;
410155324Simp	phys_avail[1] = KERNPHYSADDR + memsize;
411155324Simp	phys_avail[2] = 0;
412155324Simp	phys_avail[3] = 0;
413155324Simp	/* Do basic tuning, hz etc */
414155324Simp	init_param1();
415155324Simp	init_param2(memsize / PAGE_SIZE);
416155324Simp	avail_end = KERNPHYSADDR + memsize - 1;
417155324Simp	kdb_init();
418155324Simp	return ((void *)(kernelstack.pv_va + USPACE_SVC_STACK_TOP -
419155324Simp	    sizeof(struct pcb)));
420155324Simp}
421