board_bwct.c revision 157029
1/*-
2 * Copyright (c) 1994-1998 Mark Brinicombe.
3 * Copyright (c) 1994 Brini.
4 * All rights reserved.
5 *
6 * This code is derived from software written for Brini by Mark Brinicombe
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
22 *
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
33 * SUCH DAMAGE.
34 *
35 * RiscBSD kernel project
36 *
37 * machdep.c
38 *
39 * Machine dependant functions for kernel setup
40 *
41 * This file needs a lot of work.
42 *
43 * Created      : 17/09/94
44 */
45
46#include "opt_msgbuf.h"
47#include "opt_ddb.h"
48
49#include <sys/cdefs.h>
50__FBSDID("$FreeBSD: head/sys/arm/at91/kb920x_machdep.c 157029 2006-03-22 22:31:31Z cognet $");
51
52#define _ARM32_BUS_DMA_PRIVATE
53#include <sys/param.h>
54#include <sys/systm.h>
55#include <sys/sysproto.h>
56#include <sys/signalvar.h>
57#include <sys/imgact.h>
58#include <sys/kernel.h>
59#include <sys/ktr.h>
60#include <sys/linker.h>
61#include <sys/lock.h>
62#include <sys/malloc.h>
63#include <sys/mutex.h>
64#include <sys/pcpu.h>
65#include <sys/proc.h>
66#include <sys/ptrace.h>
67#include <sys/cons.h>
68#include <sys/bio.h>
69#include <sys/bus.h>
70#include <sys/buf.h>
71#include <sys/exec.h>
72#include <sys/kdb.h>
73#include <sys/msgbuf.h>
74#include <machine/reg.h>
75#include <machine/cpu.h>
76
77#include <vm/vm.h>
78#include <vm/pmap.h>
79#include <vm/vm.h>
80#include <vm/vm_object.h>
81#include <vm/vm_page.h>
82#include <vm/vm_pager.h>
83#include <vm/vm_map.h>
84#include <vm/vnode_pager.h>
85#include <machine/pmap.h>
86#include <machine/vmparam.h>
87#include <machine/pcb.h>
88#include <machine/undefined.h>
89#include <machine/machdep.h>
90#include <machine/metadata.h>
91#include <machine/armreg.h>
92#include <machine/bus.h>
93#include <sys/reboot.h>
94
95#include <arm/at91/at91rm92reg.h>
96
97#define KERNEL_PT_SYS		0	/* Page table for mapping proc0 zero page */
98#define KERNEL_PT_KERN		1
99#define KERNEL_PT_KERN_NUM	8
100#define KERNEL_PT_AFKERNEL	KERNEL_PT_KERN + KERNEL_PT_KERN_NUM	/* L2 table for mapping after kernel */
101#define	KERNEL_PT_AFKERNEL_NUM	5
102
103/* this should be evenly divisable by PAGE_SIZE / L2_TABLE_SIZE_REAL (or 4) */
104#define NUM_KERNEL_PTS		(KERNEL_PT_AFKERNEL + KERNEL_PT_AFKERNEL_NUM)
105
106/* Define various stack sizes in pages */
107#define IRQ_STACK_SIZE	1
108#define ABT_STACK_SIZE	1
109#define UND_STACK_SIZE	1
110
111extern u_int data_abort_handler_address;
112extern u_int prefetch_abort_handler_address;
113extern u_int undefined_handler_address;
114
115struct pv_addr kernel_pt_table[NUM_KERNEL_PTS];
116
117extern void *_end;
118
119extern vm_offset_t sa1_cache_clean_addr;
120
121extern int *end;
122
123struct pcpu __pcpu;
124struct pcpu *pcpup = &__pcpu;
125
126/* Physical and virtual addresses for some global pages */
127
128vm_paddr_t phys_avail[10];
129vm_paddr_t dump_avail[4];
130vm_offset_t physical_pages;
131vm_offset_t clean_sva, clean_eva;
132
133struct pv_addr systempage;
134struct pv_addr msgbufpv;
135struct pv_addr irqstack;
136struct pv_addr undstack;
137struct pv_addr abtstack;
138struct pv_addr kernelstack;
139struct pv_addr minidataclean;
140
141static struct trapframe proc0_tf;
142
143/* Static device mappings. */
144static const struct pmap_devmap kb920x_devmap[] = {
145	/*
146	 * Map the on-board devices VA == PA so that we can access them
147	 * with the MMU on or off.
148	 */
149	{
150		/*
151		 * This at least maps the interrupt controller, the UART
152		 * and the timer. Other devices should use newbus to
153		 * map their memory anyway.
154		 */
155		0xfff00000,
156		0xfff00000,
157		0x100000,
158		VM_PROT_READ|VM_PROT_WRITE,
159		PTE_NOCACHE,
160	},
161	{
162		/*
163		 * Add the ohci controller, and anything else that might be
164		 * on this chip select for a VA/PA mapping.
165		 */
166		AT91RM92_OHCI_BASE,
167		AT91RM92_OHCI_BASE,
168		AT91RM92_OHCI_SIZE,
169		VM_PROT_READ|VM_PROT_WRITE,
170		PTE_NOCACHE,
171	},
172	{
173		0,
174		0,
175		0,
176		0,
177		0,
178	}
179};
180
181#define SDRAM_START 0xa0000000
182
183#ifdef DDB
184extern vm_offset_t ksym_start, ksym_end;
185#endif
186
187void *
188initarm(void *arg, void *arg2)
189{
190	struct pv_addr  kernel_l1pt;
191	int loop;
192	u_int l1pagetable;
193	vm_offset_t freemempos;
194	vm_offset_t afterkern;
195	int i = 0;
196	uint32_t fake_preload[35];
197	uint32_t memsize = 32 * 1024 * 1024;
198	vm_offset_t lastaddr;
199#ifdef DDB
200	vm_offset_t zstart = 0, zend = 0;
201#endif
202
203	i = 0;
204
205	set_cpufuncs();
206
207	fake_preload[i++] = MODINFO_NAME;
208	fake_preload[i++] = strlen("elf kernel") + 1;
209	strcpy((char*)&fake_preload[i++], "elf kernel");
210	i += 2;
211	fake_preload[i++] = MODINFO_TYPE;
212	fake_preload[i++] = strlen("elf kernel") + 1;
213	strcpy((char*)&fake_preload[i++], "elf kernel");
214	i += 2;
215	fake_preload[i++] = MODINFO_ADDR;
216	fake_preload[i++] = sizeof(vm_offset_t);
217	fake_preload[i++] = KERNBASE;
218	fake_preload[i++] = MODINFO_SIZE;
219	fake_preload[i++] = sizeof(uint32_t);
220	fake_preload[i++] = (uint32_t)&end - KERNBASE;
221#ifdef DDB
222	if (*(uint32_t *)KERNVIRTADDR == MAGIC_TRAMP_NUMBER) {
223		fake_preload[i++] = MODINFO_METADATA|MODINFOMD_SSYM;
224		fake_preload[i++] = sizeof(vm_offset_t);
225		fake_preload[i++] = *(uint32_t *)(KERNVIRTADDR + 4);
226		fake_preload[i++] = MODINFO_METADATA|MODINFOMD_ESYM;
227		fake_preload[i++] = sizeof(vm_offset_t);
228		fake_preload[i++] = *(uint32_t *)(KERNVIRTADDR + 8);
229		lastaddr = *(uint32_t *)(KERNVIRTADDR + 8);
230		zend = lastaddr;
231		zstart = *(uint32_t *)(KERNVIRTADDR + 4);
232		ksym_start = zstart;
233		ksym_end = zend;
234	} else
235#endif
236		lastaddr = (vm_offset_t)&end;
237
238	fake_preload[i++] = 0;
239	fake_preload[i] = 0;
240	preload_metadata = (void *)fake_preload;
241
242
243	pcpu_init(pcpup, 0, sizeof(struct pcpu));
244	PCPU_SET(curthread, &thread0);
245
246#define KERNEL_TEXT_BASE (KERNBASE)
247	freemempos = (lastaddr + PAGE_MASK) & ~PAGE_MASK;
248	/* Define a macro to simplify memory allocation */
249#define valloc_pages(var, np)                   \
250	alloc_pages((var).pv_va, (np));         \
251	(var).pv_pa = (var).pv_va + (KERNPHYSADDR - KERNVIRTADDR);
252
253#define alloc_pages(var, np)			\
254	(var) = freemempos;		\
255	freemempos += (np * PAGE_SIZE);		\
256	memset((char *)(var), 0, ((np) * PAGE_SIZE));
257
258	while (((freemempos - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) != 0)
259		freemempos += PAGE_SIZE;
260	valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
261	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
262		if (!(loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL))) {
263			valloc_pages(kernel_pt_table[loop],
264			    L2_TABLE_SIZE / PAGE_SIZE);
265		} else {
266			kernel_pt_table[loop].pv_va = freemempos -
267			    (loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL)) *
268			    L2_TABLE_SIZE_REAL;
269			kernel_pt_table[loop].pv_pa =
270			    kernel_pt_table[loop].pv_va - KERNVIRTADDR +
271			    KERNPHYSADDR;
272		}
273		i++;
274	}
275	/*
276	 * Allocate a page for the system page mapped to V0x00000000
277	 * This page will just contain the system vectors and can be
278	 * shared by all processes.
279	 */
280	valloc_pages(systempage, 1);
281
282	/* Allocate stacks for all modes */
283	valloc_pages(irqstack, IRQ_STACK_SIZE);
284	valloc_pages(abtstack, ABT_STACK_SIZE);
285	valloc_pages(undstack, UND_STACK_SIZE);
286	valloc_pages(kernelstack, KSTACK_PAGES);
287	alloc_pages(minidataclean.pv_pa, 1);
288	valloc_pages(msgbufpv, round_page(MSGBUF_SIZE) / PAGE_SIZE);
289	/*
290	 * Now we start construction of the L1 page table
291	 * We start by mapping the L2 page tables into the L1.
292	 * This means that we can replace L1 mappings later on if necessary
293	 */
294	l1pagetable = kernel_l1pt.pv_va;
295
296	/* Map the L2 pages tables in the L1 page table */
297	pmap_link_l2pt(l1pagetable, ARM_VECTORS_LOW,
298	    &kernel_pt_table[KERNEL_PT_SYS]);
299	for (i = 0; i < KERNEL_PT_KERN_NUM; i++)
300		pmap_link_l2pt(l1pagetable, KERNBASE + i * 0x100000,
301		    &kernel_pt_table[KERNEL_PT_KERN + i]);
302	pmap_map_chunk(l1pagetable, KERNBASE, KERNPHYSADDR,
303	   (((uint32_t)(lastaddr) - KERNBASE) + PAGE_SIZE) & ~(PAGE_SIZE - 1),
304	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
305	afterkern = round_page((lastaddr + L1_S_SIZE) & ~(L1_S_SIZE
306	    - 1));
307	for (i = 0; i < KERNEL_PT_AFKERNEL_NUM; i++) {
308		pmap_link_l2pt(l1pagetable, afterkern + i * 0x00100000,
309		    &kernel_pt_table[KERNEL_PT_AFKERNEL + i]);
310	}
311	pmap_map_entry(l1pagetable, afterkern, minidataclean.pv_pa,
312	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
313
314
315	/* Map the vector page. */
316	pmap_map_entry(l1pagetable, ARM_VECTORS_LOW, systempage.pv_pa,
317	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
318	/* Map the stack pages */
319	pmap_map_chunk(l1pagetable, irqstack.pv_va, irqstack.pv_pa,
320	    IRQ_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
321	pmap_map_chunk(l1pagetable, abtstack.pv_va, abtstack.pv_pa,
322	    ABT_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
323	pmap_map_chunk(l1pagetable, undstack.pv_va, undstack.pv_pa,
324	    UND_STACK_SIZE * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
325	pmap_map_chunk(l1pagetable, kernelstack.pv_va, kernelstack.pv_pa,
326	    KSTACK_PAGES * PAGE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
327
328	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
329	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
330	pmap_map_chunk(l1pagetable, msgbufpv.pv_va, msgbufpv.pv_pa,
331	    MSGBUF_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
332
333
334	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
335		pmap_map_chunk(l1pagetable, kernel_pt_table[loop].pv_va,
336		    kernel_pt_table[loop].pv_pa, L2_TABLE_SIZE,
337		    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
338	}
339
340	pmap_devmap_bootstrap(l1pagetable, kb920x_devmap);
341	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
342	setttb(kernel_l1pt.pv_pa);
343	cpu_tlb_flushID();
344	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
345	cninit();
346
347	/*
348	 * Pages were allocated during the secondary bootstrap for the
349	 * stacks for different CPU modes.
350	 * We must now set the r13 registers in the different CPU modes to
351	 * point to these stacks.
352	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
353	 * of the stack memory.
354	 */
355
356	cpu_control(CPU_CONTROL_MMU_ENABLE, CPU_CONTROL_MMU_ENABLE);
357	set_stackptr(PSR_IRQ32_MODE,
358	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
359	set_stackptr(PSR_ABT32_MODE,
360	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
361	set_stackptr(PSR_UND32_MODE,
362	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
363
364
365
366	/*
367	 * We must now clean the cache again....
368	 * Cleaning may be done by reading new data to displace any
369	 * dirty data in the cache. This will have happened in setttb()
370	 * but since we are boot strapping the addresses used for the read
371	 * may have just been remapped and thus the cache could be out
372	 * of sync. A re-clean after the switch will cure this.
373	 * After booting there are no gross reloations of the kernel thus
374	 * this problem will not occur after initarm().
375	 */
376	cpu_idcache_wbinv_all();
377
378	/* Set stack for exception handlers */
379
380	data_abort_handler_address = (u_int)data_abort_handler;
381	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
382	undefined_handler_address = (u_int)undefinedinstruction_bounce;
383	undefined_init();
384
385	proc_linkup(&proc0, &ksegrp0, &thread0);
386	thread0.td_kstack = kernelstack.pv_va;
387	thread0.td_pcb = (struct pcb *)
388		(thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
389	thread0.td_pcb->pcb_flags = 0;
390	thread0.td_frame = &proc0_tf;
391	pcpup->pc_curpcb = thread0.td_pcb;
392
393	arm_vector_init(ARM_VECTORS_LOW, ARM_VEC_ALL);
394
395	pmap_curmaxkvaddr = afterkern + 0x100000 * (KERNEL_PT_KERN_NUM - 1);
396	pmap_bootstrap(freemempos,
397	    KERNVIRTADDR + 3 * memsize,
398	    &kernel_l1pt);
399	msgbufp = (void*)msgbufpv.pv_va;
400	msgbufinit(msgbufp, MSGBUF_SIZE);
401	mutex_init();
402
403	i = 0;
404	dump_avail[0] = KERNPHYSADDR;
405	dump_avail[1] = KERNPHYSADDR + memsize;
406	dump_avail[2] = 0;
407	dump_avail[3] = 0;
408
409	phys_avail[0] = freemempos - KERNVIRTADDR + KERNPHYSADDR;
410	phys_avail[1] = KERNPHYSADDR + memsize;
411	phys_avail[2] = 0;
412	phys_avail[3] = 0;
413	/* Do basic tuning, hz etc */
414	init_param1();
415	init_param2(memsize / PAGE_SIZE);
416	avail_end = KERNPHYSADDR + memsize - 1;
417	kdb_init();
418	return ((void *)(kernelstack.pv_va + USPACE_SVC_STACK_TOP -
419	    sizeof(struct pcb)));
420}
421