xref: /NextBSD/sys/arm/xscale/i8134x/crb_machdep.c (revision 4557fabb34e865d7f40be64b39c9e34fa41dbb60)
1 /*	$NetBSD: hpc_machdep.c,v 1.70 2003/09/16 08:18:22 agc Exp $	*/
2 
3 /*-
4  * Copyright (c) 1994-1998 Mark Brinicombe.
5  * Copyright (c) 1994 Brini.
6  * All rights reserved.
7  *
8  * This code is derived from software written for Brini by Mark Brinicombe
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by Brini.
21  * 4. The name of the company nor the name of the author may be used to
22  *    endorse or promote products derived from this software without specific
23  *    prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
26  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28  * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  * RiscBSD kernel project
38  *
39  * machdep.c
40  *
41  * Machine dependant functions for kernel setup
42  *
43  * This file needs a lot of work.
44  *
45  * Created      : 17/09/94
46  */
47 
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
50 
51 #include "opt_kstack_pages.h"
52 
53 #define _ARM32_BUS_DMA_PRIVATE
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/sysproto.h>
57 #include <sys/signalvar.h>
58 #include <sys/imgact.h>
59 #include <sys/kernel.h>
60 #include <sys/ktr.h>
61 #include <sys/linker.h>
62 #include <sys/lock.h>
63 #include <sys/malloc.h>
64 #include <sys/mutex.h>
65 #include <sys/pcpu.h>
66 #include <sys/proc.h>
67 #include <sys/ptrace.h>
68 #include <sys/cons.h>
69 #include <sys/bio.h>
70 #include <sys/bus.h>
71 #include <sys/buf.h>
72 #include <sys/exec.h>
73 #include <sys/kdb.h>
74 #include <sys/msgbuf.h>
75 #include <machine/reg.h>
76 #include <machine/cpu.h>
77 
78 #include <vm/vm.h>
79 #include <vm/pmap.h>
80 #include <vm/vm_object.h>
81 #include <vm/vm_page.h>
82 #include <vm/vm_map.h>
83 #include <machine/devmap.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 <machine/physmem.h>
92 #include <sys/reboot.h>
93 
94 
95 #include <arm/xscale/i80321/i80321var.h> /* For i80321_calibrate_delay() */
96 
97 #include <arm/xscale/i8134x/i81342reg.h>
98 #include <arm/xscale/i8134x/i81342var.h>
99 #include <arm/xscale/i8134x/obiovar.h>
100 
101 
102 #define KERNEL_PT_SYS		0	/* Page table for mapping proc0 zero page */
103 #define	KERNEL_PT_IOPXS		1
104 #define KERNEL_PT_BEFOREKERN	2
105 #define KERNEL_PT_AFKERNEL	3	/* L2 table for mapping after kernel */
106 #define	KERNEL_PT_AFKERNEL_NUM	9
107 
108 /* this should be evenly divisable by PAGE_SIZE / L2_TABLE_SIZE_REAL (or 4) */
109 #define NUM_KERNEL_PTS		(KERNEL_PT_AFKERNEL + KERNEL_PT_AFKERNEL_NUM)
110 
111 struct pv_addr kernel_pt_table[NUM_KERNEL_PTS];
112 
113 /* Physical and virtual addresses for some global pages */
114 
115 struct pv_addr systempage;
116 struct pv_addr msgbufpv;
117 struct pv_addr irqstack;
118 struct pv_addr undstack;
119 struct pv_addr abtstack;
120 struct pv_addr kernelstack;
121 
122 /* Static device mappings. */
123 static const struct arm_devmap_entry iq81342_devmap[] = {
124 	    {
125 		    IOP34X_VADDR,
126 		    IOP34X_HWADDR,
127 		    IOP34X_SIZE,
128 		    VM_PROT_READ|VM_PROT_WRITE,
129 		    PTE_DEVICE,
130 	    },
131 	    {
132 		    /*
133 		     * Cheat and map a whole section, this will bring
134 		     * both PCI-X and PCI-E outbound I/O
135 		     */
136 		    IOP34X_PCIX_OIOBAR_VADDR &~ (0x100000 - 1),
137 		    IOP34X_PCIX_OIOBAR &~ (0x100000 - 1),
138 		    0x100000,
139 		    VM_PROT_READ|VM_PROT_WRITE,
140 		    PTE_DEVICE,
141 	    },
142 	    {
143 		    IOP34X_PCE1_VADDR,
144 		    IOP34X_PCE1,
145 		    IOP34X_PCE1_SIZE,
146 		    VM_PROT_READ|VM_PROT_WRITE,
147 		    PTE_DEVICE,
148 	    },
149 	    {
150 		    0,
151 		    0,
152 		    0,
153 		    0,
154 		    0,
155 	    }
156 };
157 
158 #define SDRAM_START 0x00000000
159 
160 extern vm_offset_t xscale_cache_clean_addr;
161 
162 void *
initarm(struct arm_boot_params * abp)163 initarm(struct arm_boot_params *abp)
164 {
165 	struct pv_addr  kernel_l1pt;
166 	struct pv_addr  dpcpu;
167 	int loop, i;
168 	u_int l1pagetable;
169 	vm_offset_t freemempos;
170 	vm_offset_t freemem_pt;
171 	vm_offset_t afterkern;
172 	vm_offset_t freemem_after;
173 	vm_offset_t lastaddr;
174 	uint32_t memsize, memstart;
175 
176 	lastaddr = parse_boot_param(abp);
177 	arm_physmem_kernaddr = abp->abp_physaddr;
178 	set_cpufuncs();
179 	pcpu_init(pcpup, 0, sizeof(struct pcpu));
180 	PCPU_SET(curthread, &thread0);
181 
182 	/* Do basic tuning, hz etc */
183 	init_param1();
184 
185 	freemempos = 0x00200000;
186 	/* Define a macro to simplify memory allocation */
187 #define	valloc_pages(var, np)			\
188 	alloc_pages((var).pv_pa, (np));		\
189 	(var).pv_va = (var).pv_pa + 0xc0000000;
190 
191 #define alloc_pages(var, np)			\
192 	freemempos -= (np * PAGE_SIZE);		\
193 	(var) = freemempos;		\
194 	memset((char *)(var), 0, ((np) * PAGE_SIZE));
195 
196 	while (((freemempos - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) != 0)
197 		freemempos -= PAGE_SIZE;
198 	valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
199 	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
200 		if (!(loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL))) {
201 			valloc_pages(kernel_pt_table[loop],
202 			    L2_TABLE_SIZE / PAGE_SIZE);
203 		} else {
204 			kernel_pt_table[loop].pv_pa = freemempos +
205 			    (loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL)) *
206 			    L2_TABLE_SIZE_REAL;
207 			kernel_pt_table[loop].pv_va =
208 			    kernel_pt_table[loop].pv_pa + 0xc0000000;
209 		}
210 	}
211 	freemem_pt = freemempos;
212 	freemempos = 0x00100000;
213 	/*
214 	 * Allocate a page for the system page mapped to V0x00000000
215 	 * This page will just contain the system vectors and can be
216 	 * shared by all processes.
217 	 */
218 	valloc_pages(systempage, 1);
219 
220 	/* Allocate dynamic per-cpu area. */
221 	valloc_pages(dpcpu, DPCPU_SIZE / PAGE_SIZE);
222 	dpcpu_init((void *)dpcpu.pv_va, 0);
223 
224 	/* Allocate stacks for all modes */
225 	valloc_pages(irqstack, IRQ_STACK_SIZE);
226 	valloc_pages(abtstack, ABT_STACK_SIZE);
227 	valloc_pages(undstack, UND_STACK_SIZE);
228 	valloc_pages(kernelstack, kstack_pages);
229 	valloc_pages(msgbufpv, round_page(msgbufsize) / PAGE_SIZE);
230 	/*
231 	 * Now we start construction of the L1 page table
232 	 * We start by mapping the L2 page tables into the L1.
233 	 * This means that we can replace L1 mappings later on if necessary
234 	 */
235 	l1pagetable = kernel_l1pt.pv_va;
236 
237 	/* Map the L2 pages tables in the L1 page table */
238 	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00100000 - 1),
239 	    &kernel_pt_table[KERNEL_PT_SYS]);
240 	pmap_map_chunk(l1pagetable, KERNBASE, SDRAM_START, 0x100000,
241 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
242 
243 	pmap_map_chunk(l1pagetable, KERNBASE + 0x100000, SDRAM_START + 0x100000,
244 	    0x100000, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
245 
246 	pmap_map_chunk(l1pagetable, KERNBASE + 0x200000, SDRAM_START + 0x200000,
247 	   (((uint32_t)(lastaddr) - KERNBASE - 0x200000) + L1_S_SIZE) & ~(L1_S_SIZE - 1),
248 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
249 	freemem_after = ((int)lastaddr + PAGE_SIZE) & ~(PAGE_SIZE - 1);
250 	afterkern = round_page(((vm_offset_t)lastaddr + L1_S_SIZE) & ~(L1_S_SIZE
251 	    - 1));
252 	for (i = 0; i < KERNEL_PT_AFKERNEL_NUM; i++) {
253 		pmap_link_l2pt(l1pagetable, afterkern + i * 0x00100000,
254 		    &kernel_pt_table[KERNEL_PT_AFKERNEL + i]);
255 	}
256 
257 
258 	/* Map the vector page. */
259 	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
260 	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
261 	arm_devmap_bootstrap(l1pagetable, iq81342_devmap);
262 	/*
263 	 * Give the XScale global cache clean code an appropriately
264 	 * sized chunk of unmapped VA space starting at 0xff000000
265 	 * (our device mappings end before this address).
266 	 */
267 	xscale_cache_clean_addr = 0xff000000U;
268 
269 	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
270 	setttb(kernel_l1pt.pv_pa);
271 	cpu_tlb_flushID();
272 	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
273 	/*
274 	 * Pages were allocated during the secondary bootstrap for the
275 	 * stacks for different CPU modes.
276 	 * We must now set the r13 registers in the different CPU modes to
277 	 * point to these stacks.
278 	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
279 	 * of the stack memory.
280 	 */
281 
282 	set_stackptrs(0);
283 
284 	/*
285 	 * We must now clean the cache again....
286 	 * Cleaning may be done by reading new data to displace any
287 	 * dirty data in the cache. This will have happened in setttb()
288 	 * but since we are boot strapping the addresses used for the read
289 	 * may have just been remapped and thus the cache could be out
290 	 * of sync. A re-clean after the switch will cure this.
291 	 * After booting there are no gross relocations of the kernel thus
292 	 * this problem will not occur after initarm().
293 	 */
294 	cpu_idcache_wbinv_all();
295 	cpu_setup();
296 
297 	i80321_calibrate_delay();
298 	i81342_sdram_bounds(obio_bs_tag, IOP34X_VADDR, &memstart, &memsize);
299 	physmem = memsize / PAGE_SIZE;
300 	cninit();
301 	/* Set stack for exception handlers */
302 
303 	undefined_init();
304 
305 	init_proc0(kernelstack.pv_va);
306 
307 	arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
308 
309 	pmap_curmaxkvaddr = afterkern + PAGE_SIZE;
310 
311 	vm_max_kernel_address = 0xe0000000;
312 	pmap_bootstrap(pmap_curmaxkvaddr, &kernel_l1pt);
313 	msgbufp = (void*)msgbufpv.pv_va;
314 	msgbufinit(msgbufp, msgbufsize);
315 	mutex_init();
316 
317 	/*
318 	 * Add the physical ram we have available.
319 	 *
320 	 * Exclude the kernel (and all the things we allocated which immediately
321 	 * follow the kernel) from the VM allocation pool but not from crash
322 	 * dumps.  virtual_avail is a global variable which tracks the kva we've
323 	 * "allocated" while setting up pmaps.
324 	 *
325 	 * Prepare the list of physical memory available to the vm subsystem.
326 	 */
327 	arm_physmem_hardware_region(SDRAM_START, memsize);
328 	arm_physmem_exclude_region(freemem_pt, abp->abp_physaddr -
329 	    freemem_pt, EXFLAG_NOALLOC);
330 	arm_physmem_exclude_region(freemempos, abp->abp_physaddr - 0x100000 -
331 	    freemempos, EXFLAG_NOALLOC);
332 	arm_physmem_exclude_region(abp->abp_physaddr,
333 	    virtual_avail - KERNVIRTADDR, EXFLAG_NOALLOC);
334 	arm_physmem_init_kernel_globals();
335 
336 	init_param2(physmem);
337 	kdb_init();
338 	return ((void *)(kernelstack.pv_va + USPACE_SVC_STACK_TOP -
339 	    sizeof(struct pcb)));
340 }
341