crb_machdep.c revision 266850
1171626Scognet/*	$NetBSD: hpc_machdep.c,v 1.70 2003/09/16 08:18:22 agc Exp $	*/
2171626Scognet
3171626Scognet/*-
4171626Scognet * Copyright (c) 1994-1998 Mark Brinicombe.
5171626Scognet * Copyright (c) 1994 Brini.
6171626Scognet * All rights reserved.
7171626Scognet *
8171626Scognet * This code is derived from software written for Brini by Mark Brinicombe
9171626Scognet *
10171626Scognet * Redistribution and use in source and binary forms, with or without
11171626Scognet * modification, are permitted provided that the following conditions
12171626Scognet * are met:
13171626Scognet * 1. Redistributions of source code must retain the above copyright
14171626Scognet *    notice, this list of conditions and the following disclaimer.
15171626Scognet * 2. Redistributions in binary form must reproduce the above copyright
16171626Scognet *    notice, this list of conditions and the following disclaimer in the
17171626Scognet *    documentation and/or other materials provided with the distribution.
18171626Scognet * 3. All advertising materials mentioning features or use of this software
19171626Scognet *    must display the following acknowledgement:
20171626Scognet *      This product includes software developed by Brini.
21171626Scognet * 4. The name of the company nor the name of the author may be used to
22171626Scognet *    endorse or promote products derived from this software without specific
23171626Scognet *    prior written permission.
24171626Scognet *
25171626Scognet * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
26171626Scognet * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27171626Scognet * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28171626Scognet * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29171626Scognet * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30171626Scognet * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31171626Scognet * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32171626Scognet * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33171626Scognet * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34171626Scognet * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35171626Scognet * SUCH DAMAGE.
36171626Scognet *
37171626Scognet * RiscBSD kernel project
38171626Scognet *
39171626Scognet * machdep.c
40171626Scognet *
41171626Scognet * Machine dependant functions for kernel setup
42171626Scognet *
43236987Simp * This file needs a lot of work.
44171626Scognet *
45171626Scognet * Created      : 17/09/94
46171626Scognet */
47171626Scognet
48171626Scognet#include <sys/cdefs.h>
49171626Scognet__FBSDID("$FreeBSD: head/sys/arm/xscale/i8134x/crb_machdep.c 266850 2014-05-29 16:56:39Z cognet $");
50171626Scognet
51171626Scognet#define _ARM32_BUS_DMA_PRIVATE
52171626Scognet#include <sys/param.h>
53171626Scognet#include <sys/systm.h>
54171626Scognet#include <sys/sysproto.h>
55171626Scognet#include <sys/signalvar.h>
56171626Scognet#include <sys/imgact.h>
57171626Scognet#include <sys/kernel.h>
58171626Scognet#include <sys/ktr.h>
59171626Scognet#include <sys/linker.h>
60171626Scognet#include <sys/lock.h>
61171626Scognet#include <sys/malloc.h>
62171626Scognet#include <sys/mutex.h>
63171626Scognet#include <sys/pcpu.h>
64171626Scognet#include <sys/proc.h>
65171626Scognet#include <sys/ptrace.h>
66171626Scognet#include <sys/cons.h>
67171626Scognet#include <sys/bio.h>
68171626Scognet#include <sys/bus.h>
69171626Scognet#include <sys/buf.h>
70171626Scognet#include <sys/exec.h>
71171626Scognet#include <sys/kdb.h>
72171626Scognet#include <sys/msgbuf.h>
73171626Scognet#include <machine/reg.h>
74171626Scognet#include <machine/cpu.h>
75171626Scognet
76171626Scognet#include <vm/vm.h>
77171626Scognet#include <vm/pmap.h>
78171626Scognet#include <vm/vm_object.h>
79171626Scognet#include <vm/vm_page.h>
80171626Scognet#include <vm/vm_map.h>
81257660Sian#include <machine/devmap.h>
82171626Scognet#include <machine/vmparam.h>
83171626Scognet#include <machine/pcb.h>
84171626Scognet#include <machine/undefined.h>
85171626Scognet#include <machine/machdep.h>
86171626Scognet#include <machine/metadata.h>
87171626Scognet#include <machine/armreg.h>
88171626Scognet#include <machine/bus.h>
89261649Sian#include <machine/physmem.h>
90171626Scognet#include <sys/reboot.h>
91171626Scognet
92171626Scognet
93171626Scognet#include <arm/xscale/i80321/i80321var.h> /* For i80321_calibrate_delay() */
94171626Scognet
95171626Scognet#include <arm/xscale/i8134x/i81342reg.h>
96171626Scognet#include <arm/xscale/i8134x/i81342var.h>
97171626Scognet#include <arm/xscale/i8134x/obiovar.h>
98171626Scognet
99171626Scognet
100171626Scognet#define KERNEL_PT_SYS		0	/* Page table for mapping proc0 zero page */
101171626Scognet#define	KERNEL_PT_IOPXS		1
102171626Scognet#define KERNEL_PT_BEFOREKERN	2
103171626Scognet#define KERNEL_PT_AFKERNEL	3	/* L2 table for mapping after kernel */
104171626Scognet#define	KERNEL_PT_AFKERNEL_NUM	9
105171626Scognet
106171626Scognet/* this should be evenly divisable by PAGE_SIZE / L2_TABLE_SIZE_REAL (or 4) */
107171626Scognet#define NUM_KERNEL_PTS		(KERNEL_PT_AFKERNEL + KERNEL_PT_AFKERNEL_NUM)
108171626Scognet
109171626Scognetstruct pv_addr kernel_pt_table[NUM_KERNEL_PTS];
110171626Scognet
111171626Scognet/* Physical and virtual addresses for some global pages */
112171626Scognet
113171626Scognetstruct pv_addr systempage;
114171626Scognetstruct pv_addr msgbufpv;
115171626Scognetstruct pv_addr irqstack;
116171626Scognetstruct pv_addr undstack;
117171626Scognetstruct pv_addr abtstack;
118171626Scognetstruct pv_addr kernelstack;
119171626Scognet
120171626Scognet/* Static device mappings. */
121257660Sianstatic const struct arm_devmap_entry iq81342_devmap[] = {
122171626Scognet	    {
123171626Scognet		    IOP34X_VADDR,
124171626Scognet		    IOP34X_HWADDR,
125171626Scognet		    IOP34X_SIZE,
126171626Scognet		    VM_PROT_READ|VM_PROT_WRITE,
127265852Sian		    PTE_DEVICE,
128171626Scognet	    },
129171626Scognet	    {
130171626Scognet		    /*
131171626Scognet		     * Cheat and map a whole section, this will bring
132171626Scognet		     * both PCI-X and PCI-E outbound I/O
133171626Scognet		     */
134171626Scognet		    IOP34X_PCIX_OIOBAR_VADDR &~ (0x100000 - 1),
135171626Scognet		    IOP34X_PCIX_OIOBAR &~ (0x100000 - 1),
136171626Scognet		    0x100000,
137171626Scognet		    VM_PROT_READ|VM_PROT_WRITE,
138265852Sian		    PTE_DEVICE,
139171626Scognet	    },
140172297Scognet	    {
141172297Scognet		    IOP34X_PCE1_VADDR,
142172297Scognet		    IOP34X_PCE1,
143172297Scognet		    IOP34X_PCE1_SIZE,
144172297Scognet		    VM_PROT_READ|VM_PROT_WRITE,
145265852Sian		    PTE_DEVICE,
146172297Scognet	    },
147236987Simp	    {
148171626Scognet		    0,
149171626Scognet		    0,
150171626Scognet		    0,
151171626Scognet		    0,
152171626Scognet		    0,
153171626Scognet	    }
154171626Scognet};
155171626Scognet
156171626Scognet#define SDRAM_START 0x00000000
157171626Scognet
158171626Scognetextern vm_offset_t xscale_cache_clean_addr;
159171626Scognet
160171626Scognetvoid *
161236524Simpinitarm(struct arm_boot_params *abp)
162171626Scognet{
163171626Scognet	struct pv_addr  kernel_l1pt;
164194784Sjeff	struct pv_addr  dpcpu;
165177883Simp	int loop, i;
166171626Scognet	u_int l1pagetable;
167171626Scognet	vm_offset_t freemempos;
168171626Scognet	vm_offset_t freemem_pt;
169171626Scognet	vm_offset_t afterkern;
170171626Scognet	vm_offset_t freemem_after;
171171626Scognet	vm_offset_t lastaddr;
172171626Scognet	uint32_t memsize, memstart;
173171626Scognet
174237040Simp	lastaddr = parse_boot_param(abp);
175261649Sian	arm_physmem_kernaddr = abp->abp_physaddr;
176171626Scognet	set_cpufuncs();
177171626Scognet	pcpu_init(pcpup, 0, sizeof(struct pcpu));
178171626Scognet	PCPU_SET(curthread, &thread0);
179171626Scognet
180220836Spluknet	/* Do basic tuning, hz etc */
181220836Spluknet	init_param1();
182220836Spluknet
183171626Scognet	freemempos = 0x00200000;
184171626Scognet	/* Define a macro to simplify memory allocation */
185171626Scognet#define	valloc_pages(var, np)			\
186171626Scognet	alloc_pages((var).pv_pa, (np));		\
187171626Scognet	(var).pv_va = (var).pv_pa + 0xc0000000;
188171626Scognet
189171626Scognet#define alloc_pages(var, np)			\
190171626Scognet	freemempos -= (np * PAGE_SIZE);		\
191171626Scognet	(var) = freemempos;		\
192171626Scognet	memset((char *)(var), 0, ((np) * PAGE_SIZE));
193171626Scognet
194171626Scognet	while (((freemempos - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) != 0)
195171626Scognet		freemempos -= PAGE_SIZE;
196171626Scognet	valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
197171626Scognet	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
198171626Scognet		if (!(loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL))) {
199171626Scognet			valloc_pages(kernel_pt_table[loop],
200171626Scognet			    L2_TABLE_SIZE / PAGE_SIZE);
201171626Scognet		} else {
202171626Scognet			kernel_pt_table[loop].pv_pa = freemempos +
203171626Scognet			    (loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL)) *
204171626Scognet			    L2_TABLE_SIZE_REAL;
205236987Simp			kernel_pt_table[loop].pv_va =
206171626Scognet			    kernel_pt_table[loop].pv_pa + 0xc0000000;
207171626Scognet		}
208171626Scognet	}
209171626Scognet	freemem_pt = freemempos;
210171626Scognet	freemempos = 0x00100000;
211171626Scognet	/*
212171626Scognet	 * Allocate a page for the system page mapped to V0x00000000
213171626Scognet	 * This page will just contain the system vectors and can be
214171626Scognet	 * shared by all processes.
215171626Scognet	 */
216171626Scognet	valloc_pages(systempage, 1);
217171626Scognet
218194784Sjeff	/* Allocate dynamic per-cpu area. */
219194784Sjeff	valloc_pages(dpcpu, DPCPU_SIZE / PAGE_SIZE);
220194784Sjeff	dpcpu_init((void *)dpcpu.pv_va, 0);
221194784Sjeff
222171626Scognet	/* Allocate stacks for all modes */
223171626Scognet	valloc_pages(irqstack, IRQ_STACK_SIZE);
224171626Scognet	valloc_pages(abtstack, ABT_STACK_SIZE);
225171626Scognet	valloc_pages(undstack, UND_STACK_SIZE);
226171626Scognet	valloc_pages(kernelstack, KSTACK_PAGES);
227217688Spluknet	valloc_pages(msgbufpv, round_page(msgbufsize) / PAGE_SIZE);
228171626Scognet	/*
229171626Scognet	 * Now we start construction of the L1 page table
230171626Scognet	 * We start by mapping the L2 page tables into the L1.
231171626Scognet	 * This means that we can replace L1 mappings later on if necessary
232171626Scognet	 */
233171626Scognet	l1pagetable = kernel_l1pt.pv_va;
234171626Scognet
235171626Scognet	/* Map the L2 pages tables in the L1 page table */
236171626Scognet	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00100000 - 1),
237171626Scognet	    &kernel_pt_table[KERNEL_PT_SYS]);
238171626Scognet	pmap_map_chunk(l1pagetable, KERNBASE, SDRAM_START, 0x100000,
239171626Scognet	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
240171626Scognet
241171626Scognet	pmap_map_chunk(l1pagetable, KERNBASE + 0x100000, SDRAM_START + 0x100000,
242171626Scognet	    0x100000, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
243171626Scognet
244171626Scognet	pmap_map_chunk(l1pagetable, KERNBASE + 0x200000, SDRAM_START + 0x200000,
245171626Scognet	   (((uint32_t)(lastaddr) - KERNBASE - 0x200000) + L1_S_SIZE) & ~(L1_S_SIZE - 1),
246171626Scognet	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
247171626Scognet	freemem_after = ((int)lastaddr + PAGE_SIZE) & ~(PAGE_SIZE - 1);
248236987Simp	afterkern = round_page(((vm_offset_t)lastaddr + L1_S_SIZE) & ~(L1_S_SIZE
249171626Scognet	    - 1));
250171626Scognet	for (i = 0; i < KERNEL_PT_AFKERNEL_NUM; i++) {
251171626Scognet		pmap_link_l2pt(l1pagetable, afterkern + i * 0x00100000,
252171626Scognet		    &kernel_pt_table[KERNEL_PT_AFKERNEL + i]);
253171626Scognet	}
254171626Scognet
255171626Scognet
256171626Scognet	/* Map the vector page. */
257171626Scognet	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
258171626Scognet	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
259257660Sian	arm_devmap_bootstrap(l1pagetable, iq81342_devmap);
260171626Scognet	/*
261171626Scognet	 * Give the XScale global cache clean code an appropriately
262171626Scognet	 * sized chunk of unmapped VA space starting at 0xff000000
263171626Scognet	 * (our device mappings end before this address).
264171626Scognet	 */
265171626Scognet	xscale_cache_clean_addr = 0xff000000U;
266171626Scognet
267171626Scognet	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
268171626Scognet	setttb(kernel_l1pt.pv_pa);
269171626Scognet	cpu_tlb_flushID();
270171626Scognet	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
271171626Scognet	/*
272171626Scognet	 * Pages were allocated during the secondary bootstrap for the
273171626Scognet	 * stacks for different CPU modes.
274171626Scognet	 * We must now set the r13 registers in the different CPU modes to
275171626Scognet	 * point to these stacks.
276171626Scognet	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
277171626Scognet	 * of the stack memory.
278171626Scognet	 */
279171626Scognet
280240802Sandrew	set_stackptrs(0);
281171626Scognet
282171626Scognet	/*
283171626Scognet	 * We must now clean the cache again....
284171626Scognet	 * Cleaning may be done by reading new data to displace any
285171626Scognet	 * dirty data in the cache. This will have happened in setttb()
286171626Scognet	 * but since we are boot strapping the addresses used for the read
287171626Scognet	 * may have just been remapped and thus the cache could be out
288171626Scognet	 * of sync. A re-clean after the switch will cure this.
289185513Sstas	 * After booting there are no gross relocations of the kernel thus
290171626Scognet	 * this problem will not occur after initarm().
291171626Scognet	 */
292171626Scognet	cpu_idcache_wbinv_all();
293258412Sian	cpu_setup("");
294258412Sian
295171626Scognet	i80321_calibrate_delay();
296171626Scognet	i81342_sdram_bounds(&obio_bs_tag, IOP34X_VADDR, &memstart, &memsize);
297171626Scognet	physmem = memsize / PAGE_SIZE;
298171626Scognet	cninit();
299171626Scognet	/* Set stack for exception handlers */
300171626Scognet
301171626Scognet	undefined_init();
302171626Scognet
303236828Sandrew	init_proc0(kernelstack.pv_va);
304171626Scognet
305171626Scognet	arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
306171626Scognet
307171626Scognet	pmap_curmaxkvaddr = afterkern + PAGE_SIZE;
308261642Sian
309256712Scognet	vm_max_kernel_address = 0xe0000000;
310247046Salc	pmap_bootstrap(pmap_curmaxkvaddr, &kernel_l1pt);
311171626Scognet	msgbufp = (void*)msgbufpv.pv_va;
312217688Spluknet	msgbufinit(msgbufp, msgbufsize);
313171626Scognet	mutex_init();
314261698Sian
315261698Sian	/*
316261698Sian	 * Add the physical ram we have available.
317261698Sian	 *
318261698Sian	 * Exclude the kernel (and all the things we allocated which immediately
319261698Sian	 * follow the kernel) from the VM allocation pool but not from crash
320261698Sian	 * dumps.  virtual_avail is a global variable which tracks the kva we've
321261698Sian	 * "allocated" while setting up pmaps.
322261698Sian	 *
323261698Sian	 * Prepare the list of physical memory available to the vm subsystem.
324261698Sian	 */
325261698Sian	arm_physmem_hardware_region(SDRAM_START, memsize);
326266850Scognet	arm_physmem_exclude_region(freemem_pt, KERNPHYSADDR -
327266850Scognet	    freemem_pt, EXFLAG_NOALLOC);
328266850Scognet	arm_physmem_exclude_region(freemempos, KERNPHYSADDR - 0x100000 -
329266850Scognet	    freemempos, EXFLAG_NOALLOC);
330261698Sian	arm_physmem_exclude_region(abp->abp_physaddr,
331261698Sian	    virtual_avail - KERNVIRTADDR, EXFLAG_NOALLOC);
332261698Sian	arm_physmem_init_kernel_globals();
333261698Sian
334171626Scognet	init_param2(physmem);
335171626Scognet	kdb_init();
336171626Scognet	return ((void *)(kernelstack.pv_va + USPACE_SVC_STACK_TOP -
337171626Scognet	    sizeof(struct pcb)));
338171626Scognet}
339