crb_machdep.c revision 236828
1145524Sdarrenr/*	$NetBSD: hpc_machdep.c,v 1.70 2003/09/16 08:18:22 agc Exp $	*/
2145524Sdarrenr
3298107Sgjb/*-
4145524Sdarrenr * Copyright (c) 1994-1998 Mark Brinicombe.
5145630Sdarrenr * Copyright (c) 1994 Brini.
6145524Sdarrenr * All rights reserved.
7255332Scy *
8255332Scy * This code is derived from software written for Brini by Mark Brinicombe
9255332Scy *
10255332Scy * Redistribution and use in source and binary forms, with or without
11255332Scy * modification, are permitted provided that the following conditions
12255332Scy * are met:
13255332Scy * 1. Redistributions of source code must retain the above copyright
14255332Scy *    notice, this list of conditions and the following disclaimer.
15255332Scy * 2. Redistributions in binary form must reproduce the above copyright
16255332Scy *    notice, this list of conditions and the following disclaimer in the
17170268Sdarrenr *    documentation and/or other materials provided with the distribution.
18255332Scy * 3. All advertising materials mentioning features or use of this software
19255332Scy *    must display the following acknowledgement:
20255332Scy *      This product includes software developed by Brini.
21255332Scy * 4. The name of the company nor the name of the author may be used to
22255332Scy *    endorse or promote products derived from this software without specific
23255332Scy *    prior written permission.
24170268Sdarrenr *
25255332Scy * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
26170268Sdarrenr * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27255332Scy * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28255332Scy * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29255332Scy * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30255332Scy * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31255332Scy * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32255332Scy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33255332Scy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34170268Sdarrenr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35255332Scy * SUCH DAMAGE.
36255332Scy *
37255332Scy * RiscBSD kernel project
38255332Scy *
39255332Scy * machdep.c
40255332Scy *
41255332Scy * Machine dependant functions for kernel setup
42170268Sdarrenr *
43255332Scy * This file needs a lot of work.
44170268Sdarrenr *
45170268Sdarrenr * Created      : 17/09/94
46145524Sdarrenr */
47291735Sbdrewery
48291735Sbdrewery#include <sys/cdefs.h>
49145524Sdarrenr__FBSDID("$FreeBSD: head/sys/arm/xscale/i8134x/crb_machdep.c 236828 2012-06-10 01:13:04Z andrew $");
50
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>
58#include <sys/ktr.h>
59#include <sys/linker.h>
60#include <sys/lock.h>
61#include <sys/malloc.h>
62#include <sys/mutex.h>
63#include <sys/pcpu.h>
64#include <sys/proc.h>
65#include <sys/ptrace.h>
66#include <sys/cons.h>
67#include <sys/bio.h>
68#include <sys/bus.h>
69#include <sys/buf.h>
70#include <sys/exec.h>
71#include <sys/kdb.h>
72#include <sys/msgbuf.h>
73#include <machine/reg.h>
74#include <machine/cpu.h>
75
76#include <vm/vm.h>
77#include <vm/pmap.h>
78#include <vm/vm_object.h>
79#include <vm/vm_page.h>
80#include <vm/vm_pager.h>
81#include <vm/vm_map.h>
82#include <vm/vnode_pager.h>
83#include <machine/pmap.h>
84#include <machine/vmparam.h>
85#include <machine/pcb.h>
86#include <machine/undefined.h>
87#include <machine/machdep.h>
88#include <machine/metadata.h>
89#include <machine/armreg.h>
90#include <machine/bus.h>
91#include <sys/reboot.h>
92
93
94#include <arm/xscale/i80321/i80321var.h> /* For i80321_calibrate_delay() */
95
96#include <arm/xscale/i8134x/i81342reg.h>
97#include <arm/xscale/i8134x/i81342var.h>
98#include <arm/xscale/i8134x/obiovar.h>
99
100
101#define KERNEL_PT_SYS		0	/* Page table for mapping proc0 zero page */
102#define	KERNEL_PT_IOPXS		1
103#define KERNEL_PT_BEFOREKERN	2
104#define KERNEL_PT_AFKERNEL	3	/* L2 table for mapping after kernel */
105#define	KERNEL_PT_AFKERNEL_NUM	9
106
107/* this should be evenly divisable by PAGE_SIZE / L2_TABLE_SIZE_REAL (or 4) */
108#define NUM_KERNEL_PTS		(KERNEL_PT_AFKERNEL + KERNEL_PT_AFKERNEL_NUM)
109
110/* Define various stack sizes in pages */
111#define IRQ_STACK_SIZE	1
112#define ABT_STACK_SIZE	1
113#define UND_STACK_SIZE	1
114
115extern u_int data_abort_handler_address;
116extern u_int prefetch_abort_handler_address;
117extern u_int undefined_handler_address;
118
119struct pv_addr kernel_pt_table[NUM_KERNEL_PTS];
120extern int *end;
121
122struct pcpu __pcpu;
123struct pcpu *pcpup = &__pcpu;
124
125/* Physical and virtual addresses for some global pages */
126
127vm_paddr_t phys_avail[10];
128vm_paddr_t dump_avail[4];
129vm_offset_t physical_pages;
130
131struct pv_addr systempage;
132struct pv_addr msgbufpv;
133struct pv_addr irqstack;
134struct pv_addr undstack;
135struct pv_addr abtstack;
136struct pv_addr kernelstack;
137
138/* Static device mappings. */
139static const struct pmap_devmap iq81342_devmap[] = {
140	    {
141		    IOP34X_VADDR,
142		    IOP34X_HWADDR,
143		    IOP34X_SIZE,
144		    VM_PROT_READ|VM_PROT_WRITE,
145		    PTE_NOCACHE,
146	    },
147	    {
148		    /*
149		     * Cheat and map a whole section, this will bring
150		     * both PCI-X and PCI-E outbound I/O
151		     */
152		    IOP34X_PCIX_OIOBAR_VADDR &~ (0x100000 - 1),
153		    IOP34X_PCIX_OIOBAR &~ (0x100000 - 1),
154		    0x100000,
155		    VM_PROT_READ|VM_PROT_WRITE,
156		    PTE_NOCACHE,
157	    },
158	    {
159		    IOP34X_PCE1_VADDR,
160		    IOP34X_PCE1,
161		    IOP34X_PCE1_SIZE,
162		    VM_PROT_READ|VM_PROT_WRITE,
163		    PTE_NOCACHE,
164	    },
165	    {
166		    0,
167		    0,
168		    0,
169		    0,
170		    0,
171	    }
172};
173
174#define SDRAM_START 0x00000000
175
176extern vm_offset_t xscale_cache_clean_addr;
177
178void *
179initarm(struct arm_boot_params *abp)
180{
181	struct pv_addr  kernel_l1pt;
182	struct pv_addr  dpcpu;
183	int loop, i;
184	u_int l1pagetable;
185	vm_offset_t freemempos;
186	vm_offset_t freemem_pt;
187	vm_offset_t afterkern;
188	vm_offset_t freemem_after;
189	vm_offset_t lastaddr;
190	uint32_t memsize, memstart;
191
192	set_cpufuncs();
193	lastaddr = fake_preload_metadata();
194	pcpu_init(pcpup, 0, sizeof(struct pcpu));
195	PCPU_SET(curthread, &thread0);
196
197	/* Do basic tuning, hz etc */
198	init_param1();
199
200	freemempos = 0x00200000;
201	/* Define a macro to simplify memory allocation */
202#define	valloc_pages(var, np)			\
203	alloc_pages((var).pv_pa, (np));		\
204	(var).pv_va = (var).pv_pa + 0xc0000000;
205
206#define alloc_pages(var, np)			\
207	freemempos -= (np * PAGE_SIZE);		\
208	(var) = freemempos;		\
209	memset((char *)(var), 0, ((np) * PAGE_SIZE));
210
211	while (((freemempos - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) != 0)
212		freemempos -= PAGE_SIZE;
213	valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
214	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
215		if (!(loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL))) {
216			valloc_pages(kernel_pt_table[loop],
217			    L2_TABLE_SIZE / PAGE_SIZE);
218		} else {
219			kernel_pt_table[loop].pv_pa = freemempos +
220			    (loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL)) *
221			    L2_TABLE_SIZE_REAL;
222			kernel_pt_table[loop].pv_va =
223			    kernel_pt_table[loop].pv_pa + 0xc0000000;
224		}
225	}
226	freemem_pt = freemempos;
227	freemempos = 0x00100000;
228	/*
229	 * Allocate a page for the system page mapped to V0x00000000
230	 * This page will just contain the system vectors and can be
231	 * shared by all processes.
232	 */
233	valloc_pages(systempage, 1);
234
235	/* Allocate dynamic per-cpu area. */
236	valloc_pages(dpcpu, DPCPU_SIZE / PAGE_SIZE);
237	dpcpu_init((void *)dpcpu.pv_va, 0);
238
239	/* Allocate stacks for all modes */
240	valloc_pages(irqstack, IRQ_STACK_SIZE);
241	valloc_pages(abtstack, ABT_STACK_SIZE);
242	valloc_pages(undstack, UND_STACK_SIZE);
243	valloc_pages(kernelstack, KSTACK_PAGES);
244	valloc_pages(msgbufpv, round_page(msgbufsize) / PAGE_SIZE);
245#ifdef ARM_USE_SMALL_ALLOC
246	freemempos -= PAGE_SIZE;
247	freemem_pt = trunc_page(freemem_pt);
248	freemem_after = freemempos - ((freemem_pt - 0x00100000) /
249	    PAGE_SIZE) * sizeof(struct arm_small_page);
250	arm_add_smallalloc_pages((void *)(freemem_after + 0xc0000000)
251	    , (void *)0xc0100000, freemem_pt - 0x00100000, 1);
252	freemem_after -= ((freemem_after - 0x00001000) / PAGE_SIZE) *
253	    sizeof(struct arm_small_page);
254#if 0
255	arm_add_smallalloc_pages((void *)(freemem_after + 0xc0000000)
256	, (void *)0xc0001000, trunc_page(freemem_after) - 0x00001000, 0);
257#endif
258	freemempos = trunc_page(freemem_after);
259	freemempos -= PAGE_SIZE;
260#endif
261	/*
262	 * Now we start construction of the L1 page table
263	 * We start by mapping the L2 page tables into the L1.
264	 * This means that we can replace L1 mappings later on if necessary
265	 */
266	l1pagetable = kernel_l1pt.pv_va;
267
268	/* Map the L2 pages tables in the L1 page table */
269	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00100000 - 1),
270	    &kernel_pt_table[KERNEL_PT_SYS]);
271	pmap_map_chunk(l1pagetable, KERNBASE, SDRAM_START, 0x100000,
272	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
273
274	pmap_map_chunk(l1pagetable, KERNBASE + 0x100000, SDRAM_START + 0x100000,
275	    0x100000, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
276
277	pmap_map_chunk(l1pagetable, KERNBASE + 0x200000, SDRAM_START + 0x200000,
278	   (((uint32_t)(lastaddr) - KERNBASE - 0x200000) + L1_S_SIZE) & ~(L1_S_SIZE - 1),
279	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
280	freemem_after = ((int)lastaddr + PAGE_SIZE) & ~(PAGE_SIZE - 1);
281	afterkern = round_page(((vm_offset_t)lastaddr + L1_S_SIZE) & ~(L1_S_SIZE
282	    - 1));
283	for (i = 0; i < KERNEL_PT_AFKERNEL_NUM; i++) {
284		pmap_link_l2pt(l1pagetable, afterkern + i * 0x00100000,
285		    &kernel_pt_table[KERNEL_PT_AFKERNEL + i]);
286	}
287
288
289#ifdef ARM_USE_SMALL_ALLOC
290	if ((freemem_after + 2 * PAGE_SIZE) <= afterkern) {
291		arm_add_smallalloc_pages((void *)(freemem_after),
292		    (void*)(freemem_after + PAGE_SIZE),
293		    afterkern - (freemem_after + PAGE_SIZE), 0);
294
295	}
296#endif
297
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	pmap_devmap_bootstrap(l1pagetable, iq81342_devmap);
302	/*
303	 * Give the XScale global cache clean code an appropriately
304	 * sized chunk of unmapped VA space starting at 0xff000000
305	 * (our device mappings end before this address).
306	 */
307	xscale_cache_clean_addr = 0xff000000U;
308
309	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
310	setttb(kernel_l1pt.pv_pa);
311	cpu_tlb_flushID();
312	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
313	/*
314	 * Pages were allocated during the secondary bootstrap for the
315	 * stacks for different CPU modes.
316	 * We must now set the r13 registers in the different CPU modes to
317	 * point to these stacks.
318	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
319	 * of the stack memory.
320	 */
321
322
323	set_stackptr(PSR_IRQ32_MODE,
324	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
325	set_stackptr(PSR_ABT32_MODE,
326	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
327	set_stackptr(PSR_UND32_MODE,
328	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
329
330
331
332	/*
333	 * We must now clean the cache again....
334	 * Cleaning may be done by reading new data to displace any
335	 * dirty data in the cache. This will have happened in setttb()
336	 * but since we are boot strapping the addresses used for the read
337	 * may have just been remapped and thus the cache could be out
338	 * of sync. A re-clean after the switch will cure this.
339	 * After booting there are no gross relocations of the kernel thus
340	 * this problem will not occur after initarm().
341	 */
342	cpu_idcache_wbinv_all();
343	i80321_calibrate_delay();
344	i81342_sdram_bounds(&obio_bs_tag, IOP34X_VADDR, &memstart, &memsize);
345	physmem = memsize / PAGE_SIZE;
346	cninit();
347	/* Set stack for exception handlers */
348
349	data_abort_handler_address = (u_int)data_abort_handler;
350	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
351	undefined_handler_address = (u_int)undefinedinstruction_bounce;
352	undefined_init();
353
354	init_proc0(kernelstack.pv_va);
355
356	arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
357
358	pmap_curmaxkvaddr = afterkern + PAGE_SIZE;
359	/*
360	 * ARM_USE_SMALL_ALLOC uses dump_avail, so it must be filled before
361	 * calling pmap_bootstrap.
362	 */
363	dump_avail[0] = 0x00000000;
364	dump_avail[1] = 0x00000000 + memsize;
365	dump_avail[2] = 0;
366	dump_avail[3] = 0;
367
368	pmap_bootstrap(pmap_curmaxkvaddr,
369	    0xd0000000, &kernel_l1pt);
370	msgbufp = (void*)msgbufpv.pv_va;
371	msgbufinit(msgbufp, msgbufsize);
372	mutex_init();
373
374	i = 0;
375#ifdef ARM_USE_SMALL_ALLOC
376	phys_avail[i++] = 0x00001000;
377	phys_avail[i++] = 0x00002000; 	/*
378					 *XXX: Gross hack to get our
379					 * pages in the vm_page_array
380					 . */
381#endif
382	phys_avail[i++] = round_page(virtual_avail - KERNBASE + SDRAM_START);
383	phys_avail[i++] = trunc_page(0x00000000 + memsize - 1);
384	phys_avail[i++] = 0;
385	phys_avail[i] = 0;
386
387	init_param2(physmem);
388	kdb_init();
389	return ((void *)(kernelstack.pv_va + USPACE_SVC_STACK_TOP -
390	    sizeof(struct pcb)));
391}
392