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 *
41299069Spfg * Machine dependent 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: stable/11/sys/arm/xscale/i8134x/crb_machdep.c 331890 2018-04-02 22:02:49Z gonzo $");
50171626Scognet
51285627Szbb#include "opt_kstack_pages.h"
52285627Szbb
53171626Scognet#define _ARM32_BUS_DMA_PRIVATE
54171626Scognet#include <sys/param.h>
55171626Scognet#include <sys/systm.h>
56171626Scognet#include <sys/sysproto.h>
57171626Scognet#include <sys/signalvar.h>
58171626Scognet#include <sys/imgact.h>
59171626Scognet#include <sys/kernel.h>
60171626Scognet#include <sys/ktr.h>
61171626Scognet#include <sys/linker.h>
62171626Scognet#include <sys/lock.h>
63171626Scognet#include <sys/malloc.h>
64171626Scognet#include <sys/mutex.h>
65171626Scognet#include <sys/pcpu.h>
66171626Scognet#include <sys/proc.h>
67171626Scognet#include <sys/ptrace.h>
68171626Scognet#include <sys/cons.h>
69171626Scognet#include <sys/bio.h>
70171626Scognet#include <sys/bus.h>
71171626Scognet#include <sys/buf.h>
72171626Scognet#include <sys/exec.h>
73171626Scognet#include <sys/kdb.h>
74171626Scognet#include <sys/msgbuf.h>
75298627Sbr#include <sys/devmap.h>
76171626Scognet#include <machine/reg.h>
77171626Scognet#include <machine/cpu.h>
78171626Scognet
79171626Scognet#include <vm/vm.h>
80171626Scognet#include <vm/pmap.h>
81171626Scognet#include <vm/vm_object.h>
82171626Scognet#include <vm/vm_page.h>
83171626Scognet#include <vm/vm_map.h>
84171626Scognet#include <machine/vmparam.h>
85171626Scognet#include <machine/pcb.h>
86171626Scognet#include <machine/undefined.h>
87171626Scognet#include <machine/machdep.h>
88171626Scognet#include <machine/metadata.h>
89171626Scognet#include <machine/armreg.h>
90171626Scognet#include <machine/bus.h>
91261649Sian#include <machine/physmem.h>
92171626Scognet#include <sys/reboot.h>
93171626Scognet
94171626Scognet
95295199Smmel#include <arm/xscale/i8134x/i80321var.h> /* For i80321_calibrate_delay() */
96171626Scognet
97171626Scognet#include <arm/xscale/i8134x/i81342reg.h>
98171626Scognet#include <arm/xscale/i8134x/i81342var.h>
99171626Scognet#include <arm/xscale/i8134x/obiovar.h>
100171626Scognet
101171626Scognet
102171626Scognet#define KERNEL_PT_SYS		0	/* Page table for mapping proc0 zero page */
103171626Scognet#define	KERNEL_PT_IOPXS		1
104171626Scognet#define KERNEL_PT_BEFOREKERN	2
105171626Scognet#define KERNEL_PT_AFKERNEL	3	/* L2 table for mapping after kernel */
106171626Scognet#define	KERNEL_PT_AFKERNEL_NUM	9
107171626Scognet
108171626Scognet/* this should be evenly divisable by PAGE_SIZE / L2_TABLE_SIZE_REAL (or 4) */
109171626Scognet#define NUM_KERNEL_PTS		(KERNEL_PT_AFKERNEL + KERNEL_PT_AFKERNEL_NUM)
110171626Scognet
111171626Scognetstruct pv_addr kernel_pt_table[NUM_KERNEL_PTS];
112171626Scognet
113171626Scognet/* Physical and virtual addresses for some global pages */
114171626Scognet
115171626Scognetstruct pv_addr systempage;
116171626Scognetstruct pv_addr msgbufpv;
117171626Scognetstruct pv_addr irqstack;
118171626Scognetstruct pv_addr undstack;
119171626Scognetstruct pv_addr abtstack;
120171626Scognetstruct pv_addr kernelstack;
121171626Scognet
122171626Scognet/* Static device mappings. */
123298627Sbrstatic const struct devmap_entry iq81342_devmap[] = {
124171626Scognet	    {
125171626Scognet		    IOP34X_VADDR,
126171626Scognet		    IOP34X_HWADDR,
127171626Scognet		    IOP34X_SIZE,
128171626Scognet	    },
129171626Scognet	    {
130171626Scognet		    /*
131171626Scognet		     * Cheat and map a whole section, this will bring
132171626Scognet		     * both PCI-X and PCI-E outbound I/O
133171626Scognet		     */
134298433Spfg		    rounddown2(IOP34X_PCIX_OIOBAR_VADDR, 0x100000),
135298433Spfg		    rounddown2(IOP34X_PCIX_OIOBAR, 0x100000),
136171626Scognet		    0x100000,
137171626Scognet	    },
138172297Scognet	    {
139172297Scognet		    IOP34X_PCE1_VADDR,
140172297Scognet		    IOP34X_PCE1,
141172297Scognet		    IOP34X_PCE1_SIZE,
142172297Scognet	    },
143236987Simp	    {
144171626Scognet		    0,
145171626Scognet		    0,
146171626Scognet		    0,
147171626Scognet	    }
148171626Scognet};
149171626Scognet
150171626Scognet#define SDRAM_START 0x00000000
151171626Scognet
152171626Scognetextern vm_offset_t xscale_cache_clean_addr;
153171626Scognet
154171626Scognetvoid *
155236524Simpinitarm(struct arm_boot_params *abp)
156171626Scognet{
157171626Scognet	struct pv_addr  kernel_l1pt;
158194784Sjeff	struct pv_addr  dpcpu;
159177883Simp	int loop, i;
160171626Scognet	u_int l1pagetable;
161171626Scognet	vm_offset_t freemempos;
162171626Scognet	vm_offset_t freemem_pt;
163171626Scognet	vm_offset_t afterkern;
164171626Scognet	vm_offset_t freemem_after;
165171626Scognet	vm_offset_t lastaddr;
166171626Scognet	uint32_t memsize, memstart;
167171626Scognet
168237040Simp	lastaddr = parse_boot_param(abp);
169261649Sian	arm_physmem_kernaddr = abp->abp_physaddr;
170171626Scognet	set_cpufuncs();
171171626Scognet	pcpu_init(pcpup, 0, sizeof(struct pcpu));
172171626Scognet	PCPU_SET(curthread, &thread0);
173171626Scognet
174220836Spluknet	/* Do basic tuning, hz etc */
175220836Spluknet	init_param1();
176220836Spluknet
177171626Scognet	freemempos = 0x00200000;
178171626Scognet	/* Define a macro to simplify memory allocation */
179171626Scognet#define	valloc_pages(var, np)			\
180171626Scognet	alloc_pages((var).pv_pa, (np));		\
181171626Scognet	(var).pv_va = (var).pv_pa + 0xc0000000;
182171626Scognet
183171626Scognet#define alloc_pages(var, np)			\
184171626Scognet	freemempos -= (np * PAGE_SIZE);		\
185171626Scognet	(var) = freemempos;		\
186171626Scognet	memset((char *)(var), 0, ((np) * PAGE_SIZE));
187171626Scognet
188171626Scognet	while (((freemempos - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) != 0)
189171626Scognet		freemempos -= PAGE_SIZE;
190171626Scognet	valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
191171626Scognet	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
192171626Scognet		if (!(loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL))) {
193171626Scognet			valloc_pages(kernel_pt_table[loop],
194171626Scognet			    L2_TABLE_SIZE / PAGE_SIZE);
195171626Scognet		} else {
196171626Scognet			kernel_pt_table[loop].pv_pa = freemempos +
197171626Scognet			    (loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL)) *
198171626Scognet			    L2_TABLE_SIZE_REAL;
199236987Simp			kernel_pt_table[loop].pv_va =
200171626Scognet			    kernel_pt_table[loop].pv_pa + 0xc0000000;
201171626Scognet		}
202171626Scognet	}
203171626Scognet	freemem_pt = freemempos;
204171626Scognet	freemempos = 0x00100000;
205171626Scognet	/*
206171626Scognet	 * Allocate a page for the system page mapped to V0x00000000
207171626Scognet	 * This page will just contain the system vectors and can be
208171626Scognet	 * shared by all processes.
209171626Scognet	 */
210171626Scognet	valloc_pages(systempage, 1);
211171626Scognet
212194784Sjeff	/* Allocate dynamic per-cpu area. */
213194784Sjeff	valloc_pages(dpcpu, DPCPU_SIZE / PAGE_SIZE);
214194784Sjeff	dpcpu_init((void *)dpcpu.pv_va, 0);
215194784Sjeff
216171626Scognet	/* Allocate stacks for all modes */
217171626Scognet	valloc_pages(irqstack, IRQ_STACK_SIZE);
218171626Scognet	valloc_pages(abtstack, ABT_STACK_SIZE);
219171626Scognet	valloc_pages(undstack, UND_STACK_SIZE);
220286584Skib	valloc_pages(kernelstack, kstack_pages);
221217688Spluknet	valloc_pages(msgbufpv, round_page(msgbufsize) / PAGE_SIZE);
222171626Scognet	/*
223171626Scognet	 * Now we start construction of the L1 page table
224171626Scognet	 * We start by mapping the L2 page tables into the L1.
225171626Scognet	 * This means that we can replace L1 mappings later on if necessary
226171626Scognet	 */
227171626Scognet	l1pagetable = kernel_l1pt.pv_va;
228171626Scognet
229171626Scognet	/* Map the L2 pages tables in the L1 page table */
230298433Spfg	pmap_link_l2pt(l1pagetable, rounddown2(ARM_VECTORS_HIGH, 0x00100000),
231298433Spfg		       &kernel_pt_table[KERNEL_PT_SYS]);
232171626Scognet	pmap_map_chunk(l1pagetable, KERNBASE, SDRAM_START, 0x100000,
233171626Scognet	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
234171626Scognet
235171626Scognet	pmap_map_chunk(l1pagetable, KERNBASE + 0x100000, SDRAM_START + 0x100000,
236171626Scognet	    0x100000, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
237171626Scognet
238171626Scognet	pmap_map_chunk(l1pagetable, KERNBASE + 0x200000, SDRAM_START + 0x200000,
239298433Spfg	   rounddown2(((uint32_t)(lastaddr) - KERNBASE - 0x200000) + L1_S_SIZE, L1_S_SIZE),
240298433Spfg	   VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
241298433Spfg	freemem_after = rounddown2((int)lastaddr + PAGE_SIZE, PAGE_SIZE);
242298433Spfg	afterkern = round_page(rounddown2((vm_offset_t)lastaddr + L1_S_SIZE, L1_S_SIZE));
243171626Scognet	for (i = 0; i < KERNEL_PT_AFKERNEL_NUM; i++) {
244171626Scognet		pmap_link_l2pt(l1pagetable, afterkern + i * 0x00100000,
245171626Scognet		    &kernel_pt_table[KERNEL_PT_AFKERNEL + i]);
246171626Scognet	}
247171626Scognet
248171626Scognet
249171626Scognet	/* Map the vector page. */
250171626Scognet	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
251171626Scognet	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
252298627Sbr	devmap_bootstrap(l1pagetable, iq81342_devmap);
253171626Scognet	/*
254171626Scognet	 * Give the XScale global cache clean code an appropriately
255171626Scognet	 * sized chunk of unmapped VA space starting at 0xff000000
256171626Scognet	 * (our device mappings end before this address).
257171626Scognet	 */
258171626Scognet	xscale_cache_clean_addr = 0xff000000U;
259171626Scognet
260171626Scognet	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
261295213Smmel	cpu_setttb(kernel_l1pt.pv_pa);
262171626Scognet	cpu_tlb_flushID();
263171626Scognet	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
264171626Scognet	/*
265171626Scognet	 * Pages were allocated during the secondary bootstrap for the
266171626Scognet	 * stacks for different CPU modes.
267171626Scognet	 * We must now set the r13 registers in the different CPU modes to
268171626Scognet	 * point to these stacks.
269171626Scognet	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
270171626Scognet	 * of the stack memory.
271171626Scognet	 */
272171626Scognet
273240802Sandrew	set_stackptrs(0);
274171626Scognet
275171626Scognet	/*
276171626Scognet	 * We must now clean the cache again....
277171626Scognet	 * Cleaning may be done by reading new data to displace any
278295213Smmel	 * dirty data in the cache. This will have happened in cpu_setttb()
279171626Scognet	 * but since we are boot strapping the addresses used for the read
280171626Scognet	 * may have just been remapped and thus the cache could be out
281171626Scognet	 * of sync. A re-clean after the switch will cure this.
282185513Sstas	 * After booting there are no gross relocations of the kernel thus
283171626Scognet	 * this problem will not occur after initarm().
284171626Scognet	 */
285171626Scognet	cpu_idcache_wbinv_all();
286280823Sandrew	cpu_setup();
287258412Sian
288171626Scognet	i80321_calibrate_delay();
289331890Sgonzo	i81342_sdram_bounds(arm_base_bs_tag, IOP34X_VADDR, &memstart, &memsize);
290171626Scognet	physmem = memsize / PAGE_SIZE;
291171626Scognet	cninit();
292171626Scognet	/* Set stack for exception handlers */
293171626Scognet
294171626Scognet	undefined_init();
295171626Scognet
296236828Sandrew	init_proc0(kernelstack.pv_va);
297171626Scognet
298171626Scognet	arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
299171626Scognet
300171626Scognet	pmap_curmaxkvaddr = afterkern + PAGE_SIZE;
301261642Sian
302256712Scognet	vm_max_kernel_address = 0xe0000000;
303247046Salc	pmap_bootstrap(pmap_curmaxkvaddr, &kernel_l1pt);
304171626Scognet	msgbufp = (void*)msgbufpv.pv_va;
305217688Spluknet	msgbufinit(msgbufp, msgbufsize);
306171626Scognet	mutex_init();
307261698Sian
308261698Sian	/*
309261698Sian	 * Add the physical ram we have available.
310261698Sian	 *
311261698Sian	 * Exclude the kernel (and all the things we allocated which immediately
312261698Sian	 * follow the kernel) from the VM allocation pool but not from crash
313261698Sian	 * dumps.  virtual_avail is a global variable which tracks the kva we've
314261698Sian	 * "allocated" while setting up pmaps.
315261698Sian	 *
316261698Sian	 * Prepare the list of physical memory available to the vm subsystem.
317261698Sian	 */
318261698Sian	arm_physmem_hardware_region(SDRAM_START, memsize);
319292525Sian	arm_physmem_exclude_region(freemem_pt, abp->abp_physaddr -
320266850Scognet	    freemem_pt, EXFLAG_NOALLOC);
321292525Sian	arm_physmem_exclude_region(freemempos, abp->abp_physaddr - 0x100000 -
322266850Scognet	    freemempos, EXFLAG_NOALLOC);
323261698Sian	arm_physmem_exclude_region(abp->abp_physaddr,
324261698Sian	    virtual_avail - KERNVIRTADDR, EXFLAG_NOALLOC);
325261698Sian	arm_physmem_init_kernel_globals();
326261698Sian
327171626Scognet	init_param2(physmem);
328171626Scognet	kdb_init();
329171626Scognet	return ((void *)(kernelstack.pv_va + USPACE_SVC_STACK_TOP -
330171626Scognet	    sizeof(struct pcb)));
331171626Scognet}
332