mv_machdep.c revision 236524
1262569Simp/*-
2262569Simp * Copyright (c) 1994-1998 Mark Brinicombe.
3262569Simp * Copyright (c) 1994 Brini.
4262569Simp * All rights reserved.
5262569Simp *
6262569Simp * This code is derived from software written for Brini by Mark Brinicombe
7262569Simp *
8262569Simp * Redistribution and use in source and binary forms, with or without
9262569Simp * modification, are permitted provided that the following conditions
10262569Simp * are met:
11262569Simp * 1. Redistributions of source code must retain the above copyright
12262569Simp *    notice, this list of conditions and the following disclaimer.
13262569Simp * 2. Redistributions in binary form must reproduce the above copyright
14262569Simp *    notice, this list of conditions and the following disclaimer in the
15262569Simp *    documentation and/or other materials provided with the distribution.
16270864Simp * 3. All advertising materials mentioning features or use of this software
17262569Simp *    must display the following acknowledgement:
18262569Simp *      This product includes software developed by Brini.
19262569Simp * 4. The name of the company nor the name of the author may be used to
20262569Simp *    endorse or promote products derived from this software without specific
21262569Simp *    prior written permission.
22262569Simp *
23262569Simp * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
24262569Simp * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25262569Simp * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26262569Simp * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27262569Simp * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28262569Simp * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29295436Sandrew * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30262569Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31262569Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32262569Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33262569Simp * SUCH DAMAGE.
34262569Simp *
35262569Simp * from: FreeBSD: //depot/projects/arm/src/sys/arm/at91/kb920x_machdep.c, rev 45
36262569Simp */
37262569Simp
38262569Simp#include "opt_ddb.h"
39262569Simp#include "opt_platform.h"
40262569Simp
41262569Simp#include <sys/cdefs.h>
42262569Simp__FBSDID("$FreeBSD: head/sys/arm/mv/mv_machdep.c 236524 2012-06-03 18:34:32Z imp $");
43262569Simp
44262569Simp#define _ARM32_BUS_DMA_PRIVATE
45262569Simp#include <sys/param.h>
46262569Simp#include <sys/systm.h>
47262569Simp#include <sys/sysproto.h>
48262569Simp#include <sys/signalvar.h>
49262569Simp#include <sys/imgact.h>
50262569Simp#include <sys/kernel.h>
51262569Simp#include <sys/ktr.h>
52262569Simp#include <sys/linker.h>
53262569Simp#include <sys/lock.h>
54262569Simp#include <sys/malloc.h>
55262569Simp#include <sys/mutex.h>
56262569Simp#include <sys/pcpu.h>
57262569Simp#include <sys/proc.h>
58262569Simp#include <sys/ptrace.h>
59262569Simp#include <sys/cons.h>
60262569Simp#include <sys/bio.h>
61262569Simp#include <sys/bus.h>
62262569Simp#include <sys/buf.h>
63270864Simp#include <sys/exec.h>
64270864Simp#include <sys/kdb.h>
65270864Simp#include <sys/msgbuf.h>
66270864Simp#include <machine/reg.h>
67270864Simp#include <machine/cpu.h>
68270864Simp#include <machine/fdt.h>
69270864Simp
70270864Simp#include <dev/fdt/fdt_common.h>
71270864Simp#include <dev/ofw/openfirm.h>
72270864Simp
73270864Simp#include <vm/vm.h>
74270864Simp#include <vm/pmap.h>
75262569Simp#include <vm/vm_object.h>
76262569Simp#include <vm/vm_page.h>
77262569Simp#include <vm/vm_pager.h>
78295436Sandrew#include <vm/vm_map.h>
79262569Simp#include <machine/pte.h>
80262569Simp#include <machine/pmap.h>
81262569Simp#include <machine/vmparam.h>
82279385Simp#include <machine/pcb.h>
83279385Simp#include <machine/undefined.h>
84279385Simp#include <machine/machdep.h>
85279385Simp#include <machine/metadata.h>
86279385Simp#include <machine/armreg.h>
87262569Simp#include <machine/bus.h>
88262569Simp#include <sys/reboot.h>
89262569Simp
90262569Simp#include <arm/mv/mvreg.h>	/* XXX */
91262569Simp#include <arm/mv/mvvar.h>	/* XXX eventually this should be eliminated */
92262569Simp#include <arm/mv/mvwin.h>
93262569Simp
94262569Simp#ifdef  DEBUG
95262569Simp#define debugf(fmt, args...) printf(fmt, ##args)
96262569Simp#else
97262569Simp#define debugf(fmt, args...)
98262569Simp#endif
99262569Simp
100262569Simp/*
101262569Simp * This is the number of L2 page tables required for covering max
102262569Simp * (hypothetical) memsize of 4GB and all kernel mappings (vectors, msgbuf,
103262569Simp * stacks etc.), uprounded to be divisible by 4.
104262569Simp */
105262569Simp#define KERNEL_PT_MAX	78
106262569Simp
107262569Simp/* Define various stack sizes in pages */
108262569Simp#define IRQ_STACK_SIZE	1
109262569Simp#define ABT_STACK_SIZE	1
110262569Simp#define UND_STACK_SIZE	1
111262569Simp
112262569Simpextern unsigned char kernbase[];
113262569Simpextern unsigned char _etext[];
114262569Simpextern unsigned char _edata[];
115262569Simpextern unsigned char __bss_start[];
116262569Simpextern unsigned char _end[];
117262569Simp
118262569Simp#ifdef DDB
119262569Simpextern vm_offset_t ksym_start, ksym_end;
120262569Simp#endif
121262569Simp
122262569Simpextern u_int data_abort_handler_address;
123262569Simpextern u_int prefetch_abort_handler_address;
124262569Simpextern u_int undefined_handler_address;
125262569Simp
126262569Simpextern vm_offset_t pmap_bootstrap_lastaddr;
127262569Simpextern int *end;
128262569Simp
129262569Simpstruct pv_addr kernel_pt_table[KERNEL_PT_MAX];
130262569Simpstruct pcpu __pcpu;
131262569Simpstruct pcpu *pcpup = &__pcpu;
132262569Simp
133262569Simp/* Physical and virtual addresses for some global pages */
134270864Simp
135270864Simpvm_paddr_t phys_avail[10];
136270864Simpvm_paddr_t dump_avail[4];
137262569Simpvm_offset_t physical_pages;
138262569Simpvm_offset_t pmap_bootstrap_lastaddr;
139262569Simp
140262569Simpconst struct pmap_devmap *pmap_devmap_bootstrap_table;
141262569Simpstruct pv_addr systempage;
142262569Simpstruct pv_addr msgbufpv;
143262569Simpstruct pv_addr irqstack;
144262569Simpstruct pv_addr undstack;
145262569Simpstruct pv_addr abtstack;
146262569Simpstruct pv_addr kernelstack;
147262569Simp
148295436Sandrewstatic struct trapframe proc0_tf;
149295436Sandrew
150262569Simpstatic struct mem_region availmem_regions[FDT_MEM_REGIONS];
151262569Simpstatic int availmem_regions_sz;
152262569Simp
153262569Simpstatic void print_kenv(void);
154262569Simpstatic void print_kernel_section_addr(void);
155262569Simp
156262569Simpstatic void physmap_init(void);
157262569Simpstatic int platform_devmap_init(void);
158262569Simpstatic int platform_mpp_init(void);
159262569Simp
160262569Simpstatic char *
161262569Simpkenv_next(char *cp)
162262569Simp{
163262569Simp
164262569Simp	if (cp != NULL) {
165262569Simp		while (*cp != 0)
166262569Simp			cp++;
167262569Simp		cp++;
168262569Simp		if (*cp == 0)
169262569Simp			cp = NULL;
170262569Simp	}
171262569Simp	return (cp);
172262569Simp}
173262569Simp
174262569Simpstatic void
175262569Simpprint_kenv(void)
176262569Simp{
177262569Simp	int len;
178262569Simp	char *cp;
179262569Simp
180262569Simp	debugf("loader passed (static) kenv:\n");
181262569Simp	if (kern_envp == NULL) {
182262569Simp		debugf(" no env, null ptr\n");
183262569Simp		return;
184262569Simp	}
185262569Simp	debugf(" kern_envp = 0x%08x\n", (uint32_t)kern_envp);
186279385Simp
187279385Simp	len = 0;
188279385Simp	for (cp = kern_envp; cp != NULL; cp = kenv_next(cp))
189262569Simp		debugf(" %x %s\n", (uint32_t)cp, cp);
190262569Simp}
191262569Simp
192262569Simpstatic void
193262569Simpprint_kernel_section_addr(void)
194262569Simp{
195262569Simp
196262569Simp	debugf("kernel image addresses:\n");
197262569Simp	debugf(" kernbase       = 0x%08x\n", (uint32_t)kernbase);
198262569Simp	debugf(" _etext (sdata) = 0x%08x\n", (uint32_t)_etext);
199262569Simp	debugf(" _edata         = 0x%08x\n", (uint32_t)_edata);
200279385Simp	debugf(" __bss_start    = 0x%08x\n", (uint32_t)__bss_start);
201279385Simp	debugf(" _end           = 0x%08x\n", (uint32_t)_end);
202279385Simp}
203262569Simp
204262569Simpstatic void
205262569Simpphysmap_init(void)
206262569Simp{
207262569Simp	int i, j, cnt;
208262569Simp	vm_offset_t phys_kernelend, kernload;
209262569Simp	uint32_t s, e, sz;
210295436Sandrew	struct mem_region *mp, *mp1;
211295436Sandrew
212295436Sandrew	phys_kernelend = KERNPHYSADDR + (virtual_avail - KERNVIRTADDR);
213295436Sandrew	kernload = KERNPHYSADDR;
214295436Sandrew
215295436Sandrew	/*
216295436Sandrew	 * Remove kernel physical address range from avail
217295436Sandrew	 * regions list. Page align all regions.
218295436Sandrew	 * Non-page aligned memory isn't very interesting to us.
219295436Sandrew	 * Also, sort the entries for ascending addresses.
220295436Sandrew	 */
221262569Simp	sz = 0;
222262569Simp	cnt = availmem_regions_sz;
223262569Simp	debugf("processing avail regions:\n");
224262569Simp	for (mp = availmem_regions; mp->mr_size; mp++) {
225262569Simp		s = mp->mr_start;
226262569Simp		e = mp->mr_start + mp->mr_size;
227262569Simp		debugf(" %08x-%08x -> ", s, e);
228262569Simp		/* Check whether this region holds all of the kernel. */
229262569Simp		if (s < kernload && e > phys_kernelend) {
230262569Simp			availmem_regions[cnt].mr_start = phys_kernelend;
231262569Simp			availmem_regions[cnt++].mr_size = e - phys_kernelend;
232262569Simp			e = kernload;
233262569Simp		}
234279385Simp		/* Look whether this regions starts within the kernel. */
235279385Simp		if (s >= kernload && s < phys_kernelend) {
236279385Simp			if (e <= phys_kernelend)
237279385Simp				goto empty;
238262569Simp			s = phys_kernelend;
239279385Simp		}
240279385Simp		/* Now look whether this region ends within the kernel. */
241279385Simp		if (e > kernload && e <= phys_kernelend) {
242279385Simp			if (s >= kernload) {
243262569Simp				goto empty;
244262569Simp			}
245279385Simp			e = kernload;
246279385Simp		}
247279385Simp		/* Now page align the start and size of the region. */
248279385Simp		s = round_page(s);
249279385Simp		e = trunc_page(e);
250262569Simp		if (e < s)
251262569Simp			e = s;
252262569Simp		sz = e - s;
253262569Simp		debugf("%08x-%08x = %x\n", s, e, sz);
254262569Simp
255262569Simp		/* Check whether some memory is left here. */
256262569Simp		if (sz == 0) {
257262569Simp		empty:
258262569Simp			printf("skipping\n");
259262569Simp			bcopy(mp + 1, mp,
260262569Simp			    (cnt - (mp - availmem_regions)) * sizeof(*mp));
261262569Simp			cnt--;
262262569Simp			mp--;
263262569Simp			continue;
264262569Simp		}
265262569Simp
266262569Simp		/* Do an insertion sort. */
267262569Simp		for (mp1 = availmem_regions; mp1 < mp; mp1++)
268262569Simp			if (s < mp1->mr_start)
269262569Simp				break;
270262569Simp		if (mp1 < mp) {
271262569Simp			bcopy(mp1, mp1 + 1, (char *)mp - (char *)mp1);
272262569Simp			mp1->mr_start = s;
273262569Simp			mp1->mr_size = sz;
274262569Simp		} else {
275262569Simp			mp->mr_start = s;
276262569Simp			mp->mr_size = sz;
277262569Simp		}
278262569Simp	}
279262569Simp	availmem_regions_sz = cnt;
280262569Simp
281262569Simp	/* Fill in phys_avail table, based on availmem_regions */
282262569Simp	debugf("fill in phys_avail:\n");
283262569Simp	for (i = 0, j = 0; i < availmem_regions_sz; i++, j += 2) {
284262569Simp
285270864Simp		debugf(" region: 0x%08x - 0x%08x (0x%08x)\n",
286270864Simp		    availmem_regions[i].mr_start,
287270864Simp		    availmem_regions[i].mr_start + availmem_regions[i].mr_size,
288262569Simp		    availmem_regions[i].mr_size);
289262569Simp
290262569Simp		/*
291262569Simp		 * We should not map the page at PA 0x0000000, the VM can't
292262569Simp		 * handle it, as pmap_extract() == 0 means failure.
293262569Simp		 */
294262569Simp		if (availmem_regions[i].mr_start > 0 ||
295262569Simp		    availmem_regions[i].mr_size > PAGE_SIZE) {
296270864Simp			phys_avail[j] = availmem_regions[i].mr_start;
297270864Simp			if (phys_avail[j] == 0)
298270864Simp				phys_avail[j] += PAGE_SIZE;
299262569Simp			phys_avail[j + 1] = availmem_regions[i].mr_start +
300262569Simp			    availmem_regions[i].mr_size;
301262569Simp		} else
302262569Simp			j -= 2;
303262569Simp	}
304262569Simp	phys_avail[j] = 0;
305262569Simp	phys_avail[j + 1] = 0;
306262569Simp}
307262569Simp
308262569Simpvoid *
309262569Simpinitarm(struct arm_boot_params *abp)
310262569Simp{
311262569Simp	struct pv_addr kernel_l1pt;
312262569Simp	struct pv_addr dpcpu;
313262569Simp	vm_offset_t dtbp, freemempos, l2_start, lastaddr;
314262569Simp	uint32_t memsize, l2size;
315262569Simp	void *kmdp;
316262569Simp	void *mdp;
317262569Simp	u_int l1pagetable;
318262569Simp	int i = 0, j = 0, err_devmap = 0;
319262569Simp
320262569Simp	mdp = (void *)abp->abp_r0;
321262569Simp	kmdp = NULL;
322270864Simp	lastaddr = 0;
323262569Simp	memsize = 0;
324262569Simp	dtbp = (vm_offset_t)NULL;
325295436Sandrew
326262569Simp	set_cpufuncs();
327262569Simp
328262569Simp	/*
329262569Simp	 * Mask metadata pointer: it is supposed to be on page boundary. If
330270864Simp	 * the first argument (mdp) doesn't point to a valid address the
331262569Simp	 * bootloader must have passed us something else than the metadata
332262569Simp	 * ptr... In this case we want to fall back to some built-in settings.
333262569Simp	 */
334262569Simp	mdp = (void *)((uint32_t)mdp & ~PAGE_MASK);
335262569Simp
336270864Simp	/* Parse metadata and fetch parameters */
337262569Simp	if (mdp != NULL) {
338262569Simp		preload_metadata = mdp;
339262569Simp		kmdp = preload_search_by_type("elf kernel");
340262569Simp		if (kmdp != NULL) {
341262569Simp			boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
342270864Simp			kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
343262569Simp			dtbp = MD_FETCH(kmdp, MODINFOMD_DTBP, vm_offset_t);
344262569Simp			lastaddr = MD_FETCH(kmdp, MODINFOMD_KERNEND,
345262569Simp			    vm_offset_t);
346262569Simp#ifdef DDB
347262569Simp			ksym_start = MD_FETCH(kmdp, MODINFOMD_SSYM, uintptr_t);
348270864Simp			ksym_end = MD_FETCH(kmdp, MODINFOMD_ESYM, uintptr_t);
349262569Simp#endif
350262569Simp		}
351262569Simp
352262569Simp		preload_addr_relocate = KERNVIRTADDR - KERNPHYSADDR;
353262569Simp	} else {
354262569Simp		/* Fall back to hardcoded metadata. */
355262569Simp		lastaddr = fake_preload_metadata();
356262569Simp	}
357262569Simp
358262569Simp#if defined(FDT_DTB_STATIC)
359262569Simp	/*
360262569Simp	 * In case the device tree blob was not retrieved (from metadata) try
361262569Simp	 * to use the statically embedded one.
362262569Simp	 */
363262569Simp	if (dtbp == (vm_offset_t)NULL)
364262569Simp		dtbp = (vm_offset_t)&fdt_static_dtb;
365262569Simp#endif
366262569Simp
367262569Simp	if (OF_install(OFW_FDT, 0) == FALSE)
368262569Simp		while (1);
369262569Simp
370262569Simp	if (OF_init((void *)dtbp) != 0)
371262569Simp		while (1);
372262569Simp
373279385Simp	/* Grab physical memory regions information from device tree. */
374279385Simp	if (fdt_get_mem_regions(availmem_regions, &availmem_regions_sz,
375279385Simp	    &memsize) != 0)
376262569Simp		while(1);
377262569Simp
378262569Simp	if (fdt_immr_addr(MV_BASE) != 0)
379262569Simp		while (1);
380262569Simp
381262569Simp	/* Platform-specific initialisation */
382262569Simp	pmap_bootstrap_lastaddr = fdt_immr_va - ARM_NOCACHE_KVA_SIZE;
383262569Simp
384262569Simp	pcpu_init(pcpup, 0, sizeof(struct pcpu));
385262569Simp	PCPU_SET(curthread, &thread0);
386262569Simp
387279385Simp	/* Calculate number of L2 tables needed for mapping vm_page_array */
388279385Simp	l2size = (memsize / PAGE_SIZE) * sizeof(struct vm_page);
389279385Simp	l2size = (l2size >> L1_S_SHIFT) + 1;
390262569Simp
391262569Simp	/*
392262569Simp	 * Add one table for end of kernel map, one for stacks, msgbuf and
393262569Simp	 * L1 and L2 tables map and one for vectors map.
394262569Simp	 */
395262569Simp	l2size += 3;
396262569Simp
397262569Simp	/* Make it divisible by 4 */
398262569Simp	l2size = (l2size + 3) & ~3;
399262569Simp
400262569Simp#define KERNEL_TEXT_BASE (KERNBASE)
401262569Simp	freemempos = (lastaddr + PAGE_MASK) & ~PAGE_MASK;
402262569Simp
403262569Simp	/* Define a macro to simplify memory allocation */
404262569Simp#define valloc_pages(var, np)                   \
405262569Simp	alloc_pages((var).pv_va, (np));         \
406262569Simp	(var).pv_pa = (var).pv_va + (KERNPHYSADDR - KERNVIRTADDR);
407262569Simp
408262569Simp#define alloc_pages(var, np)			\
409262569Simp	(var) = freemempos;		\
410262569Simp	freemempos += (np * PAGE_SIZE);		\
411262569Simp	memset((char *)(var), 0, ((np) * PAGE_SIZE));
412262569Simp
413262569Simp	while (((freemempos - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) != 0)
414262569Simp		freemempos += PAGE_SIZE;
415262569Simp	valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
416262569Simp
417262569Simp	for (i = 0; i < l2size; ++i) {
418262569Simp		if (!(i % (PAGE_SIZE / L2_TABLE_SIZE_REAL))) {
419262569Simp			valloc_pages(kernel_pt_table[i],
420262569Simp			    L2_TABLE_SIZE / PAGE_SIZE);
421262569Simp			j = i;
422262569Simp		} else {
423262569Simp			kernel_pt_table[i].pv_va = kernel_pt_table[j].pv_va +
424262569Simp			    L2_TABLE_SIZE_REAL * (i - j);
425262569Simp			kernel_pt_table[i].pv_pa =
426262569Simp			    kernel_pt_table[i].pv_va - KERNVIRTADDR +
427262569Simp			    KERNPHYSADDR;
428262569Simp
429262569Simp		}
430262569Simp	}
431262569Simp	/*
432262569Simp	 * Allocate a page for the system page mapped to 0x00000000
433262569Simp	 * or 0xffff0000. This page will just contain the system vectors
434262569Simp	 * and can be shared by all processes.
435262569Simp	 */
436262569Simp	valloc_pages(systempage, 1);
437262569Simp
438262569Simp	/* Allocate dynamic per-cpu area. */
439262569Simp	valloc_pages(dpcpu, DPCPU_SIZE / PAGE_SIZE);
440262569Simp	dpcpu_init((void *)dpcpu.pv_va, 0);
441262569Simp
442262569Simp	/* Allocate stacks for all modes */
443262569Simp	valloc_pages(irqstack, IRQ_STACK_SIZE);
444262569Simp	valloc_pages(abtstack, ABT_STACK_SIZE);
445262569Simp	valloc_pages(undstack, UND_STACK_SIZE);
446262569Simp	valloc_pages(kernelstack, KSTACK_PAGES);
447262569Simp
448279385Simp	init_param1();
449262569Simp
450279385Simp	valloc_pages(msgbufpv, round_page(msgbufsize) / PAGE_SIZE);
451279385Simp
452262569Simp	/*
453262569Simp	 * Now we start construction of the L1 page table
454262569Simp	 * We start by mapping the L2 page tables into the L1.
455295436Sandrew	 * This means that we can replace L1 mappings later on if necessary
456262569Simp	 */
457262569Simp	l1pagetable = kernel_l1pt.pv_va;
458279385Simp
459279385Simp	/*
460279385Simp	 * Try to map as much as possible of kernel text and data using
461262569Simp	 * 1MB section mapping and for the rest of initial kernel address
462262569Simp	 * space use L2 coarse tables.
463262569Simp	 *
464262569Simp	 * Link L2 tables for mapping remainder of kernel (modulo 1MB)
465262569Simp	 * and kernel structures
466262569Simp	 */
467262569Simp	l2_start = lastaddr & ~(L1_S_OFFSET);
468262569Simp	for (i = 0 ; i < l2size - 1; i++)
469262569Simp		pmap_link_l2pt(l1pagetable, l2_start + i * L1_S_SIZE,
470262569Simp		    &kernel_pt_table[i]);
471262569Simp
472262569Simp	pmap_curmaxkvaddr = l2_start + (l2size - 1) * L1_S_SIZE;
473262569Simp
474262569Simp	/* Map kernel code and data */
475262569Simp	pmap_map_chunk(l1pagetable, KERNVIRTADDR, KERNPHYSADDR,
476262569Simp	   (((uint32_t)(lastaddr) - KERNVIRTADDR) + PAGE_MASK) & ~PAGE_MASK,
477262569Simp	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
478262569Simp
479279385Simp
480262569Simp	/* Map L1 directory and allocated L2 page tables */
481262569Simp	pmap_map_chunk(l1pagetable, kernel_l1pt.pv_va, kernel_l1pt.pv_pa,
482262569Simp	    L1_TABLE_SIZE, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
483262569Simp
484262569Simp	pmap_map_chunk(l1pagetable, kernel_pt_table[0].pv_va,
485262569Simp	    kernel_pt_table[0].pv_pa,
486262569Simp	    L2_TABLE_SIZE_REAL * l2size,
487262569Simp	    VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
488262569Simp
489262569Simp	/* Map allocated DPCPU, stacks and msgbuf */
490262569Simp	pmap_map_chunk(l1pagetable, dpcpu.pv_va, dpcpu.pv_pa,
491262569Simp	    freemempos - dpcpu.pv_va,
492262569Simp	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
493262569Simp
494262569Simp	/* Link and map the vector page */
495262569Simp	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH,
496262569Simp	    &kernel_pt_table[l2size - 1]);
497262569Simp	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
498262569Simp	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
499262569Simp
500262569Simp	/* Map pmap_devmap[] entries */
501262569Simp	err_devmap = platform_devmap_init();
502262569Simp	pmap_devmap_bootstrap(l1pagetable, pmap_devmap_bootstrap_table);
503262569Simp
504262569Simp	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2)) |
505262569Simp	    DOMAIN_CLIENT);
506262569Simp	setttb(kernel_l1pt.pv_pa);
507262569Simp	cpu_tlb_flushID();
508262569Simp	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL * 2));
509262569Simp
510262569Simp	/*
511262569Simp	 * Only after the SOC registers block is mapped we can perform device
512262569Simp	 * tree fixups, as they may attempt to read parameters from hardware.
513262569Simp	 */
514262569Simp	OF_interpret("perform-fixup", 0);
515262569Simp
516262569Simp	/*
517262569Simp	 * Re-initialise MPP. It is important to call this prior to using
518262569Simp	 * console as the physical connection can be routed via MPP.
519262569Simp	 */
520262569Simp	if (platform_mpp_init() != 0)
521262569Simp		while (1);
522262569Simp
523262569Simp	cninit();
524262569Simp
525262569Simp	physmem = memsize / PAGE_SIZE;
526262569Simp
527262569Simp	debugf("initarm: console initialized\n");
528262569Simp	debugf(" arg1 mdp = 0x%08x\n", (uint32_t)mdp);
529262569Simp	debugf(" boothowto = 0x%08x\n", boothowto);
530262569Simp	printf(" dtbp = 0x%08x\n", (uint32_t)dtbp);
531262569Simp	print_kernel_section_addr();
532262569Simp	print_kenv();
533262569Simp
534262569Simp	if (err_devmap != 0)
535262569Simp		printf("WARNING: could not fully configure devmap, error=%d\n",
536262569Simp                    err_devmap);
537262569Simp
538262569Simp	/*
539262569Simp	 * Re-initialise decode windows
540262569Simp	 */
541262569Simp	if (soc_decode_win() != 0)
542262569Simp		printf("WARNING: could not re-initialise decode windows! "
543262569Simp		    "Running with existing settings...\n");
544262569Simp
545262569Simp	/*
546262569Simp	 * Pages were allocated during the secondary bootstrap for the
547262569Simp	 * stacks for different CPU modes.
548262569Simp	 * We must now set the r13 registers in the different CPU modes to
549262569Simp	 * point to these stacks.
550262569Simp	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
551262569Simp	 * of the stack memory.
552262569Simp	 */
553262569Simp	cpu_control(CPU_CONTROL_MMU_ENABLE, CPU_CONTROL_MMU_ENABLE);
554262569Simp	set_stackptr(PSR_IRQ32_MODE,
555262569Simp	    irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
556262569Simp	set_stackptr(PSR_ABT32_MODE,
557262569Simp	    abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
558262569Simp	set_stackptr(PSR_UND32_MODE,
559262569Simp	    undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
560262569Simp
561262569Simp	/*
562262569Simp	 * We must now clean the cache again....
563262569Simp	 * Cleaning may be done by reading new data to displace any
564262569Simp	 * dirty data in the cache. This will have happened in setttb()
565262569Simp	 * but since we are boot strapping the addresses used for the read
566262569Simp	 * may have just been remapped and thus the cache could be out
567262569Simp	 * of sync. A re-clean after the switch will cure this.
568262569Simp	 * After booting there are no gross relocations of the kernel thus
569262569Simp	 * this problem will not occur after initarm().
570262569Simp	 */
571262569Simp	cpu_idcache_wbinv_all();
572262569Simp
573262569Simp	/* Set stack for exception handlers */
574262569Simp	data_abort_handler_address = (u_int)data_abort_handler;
575262569Simp	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
576262569Simp	undefined_handler_address = (u_int)undefinedinstruction_bounce;
577262569Simp	undefined_init();
578262569Simp
579279385Simp	proc_linkup0(&proc0, &thread0);
580262569Simp	thread0.td_kstack = kernelstack.pv_va;
581262569Simp	thread0.td_kstack_pages = KSTACK_PAGES;
582262569Simp	thread0.td_pcb = (struct pcb *)
583262569Simp	    (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
584262569Simp	thread0.td_pcb->pcb_flags = 0;
585262569Simp	thread0.td_frame = &proc0_tf;
586262569Simp	pcpup->pc_curpcb = thread0.td_pcb;
587262569Simp
588262569Simp	arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
589262569Simp
590262569Simp	dump_avail[0] = 0;
591279385Simp	dump_avail[1] = memsize;
592279385Simp	dump_avail[2] = 0;
593279385Simp	dump_avail[3] = 0;
594279385Simp
595279385Simp	pmap_bootstrap(freemempos, pmap_bootstrap_lastaddr, &kernel_l1pt);
596279385Simp	msgbufp = (void *)msgbufpv.pv_va;
597262569Simp	msgbufinit(msgbufp, msgbufsize);
598262569Simp	mutex_init();
599279385Simp
600279385Simp	/*
601262569Simp	 * Prepare map of physical memory regions available to vm subsystem.
602279385Simp	 */
603279385Simp	physmap_init();
604262569Simp
605262569Simp	/* Do basic tuning, hz etc */
606262569Simp	init_param2(physmem);
607262569Simp	kdb_init();
608262569Simp	return ((void *)(kernelstack.pv_va + USPACE_SVC_STACK_TOP -
609262569Simp	    sizeof(struct pcb)));
610262569Simp}
611262569Simp
612262569Simp#define MPP_PIN_MAX		50
613262569Simp#define MPP_PIN_CELLS		2
614262569Simp#define MPP_PINS_PER_REG	8
615262569Simp#define MPP_SEL(pin,func)	(((func) & 0xf) <<		\
616262569Simp    (((pin) % MPP_PINS_PER_REG) * 4))
617262569Simp
618262569Simpstatic int
619262569Simpplatform_mpp_init(void)
620262569Simp{
621262569Simp	pcell_t pinmap[MPP_PIN_MAX * MPP_PIN_CELLS];
622262569Simp	int mpp[MPP_PIN_MAX];
623262569Simp	uint32_t ctrl_val, ctrl_offset;
624262569Simp	pcell_t reg[4];
625262569Simp	u_long start, size;
626262569Simp	phandle_t node;
627262569Simp	pcell_t pin_cells, *pinmap_ptr, pin_count;
628262569Simp	ssize_t len;
629262569Simp	int par_addr_cells, par_size_cells;
630262569Simp	int tuple_size, tuples, rv, pins, i, j;
631262569Simp	int mpp_pin, mpp_function;
632262569Simp
633262569Simp	/*
634262569Simp	 * Try to access the MPP node directly i.e. through /aliases/mpp.
635262569Simp	 */
636262569Simp	if ((node = OF_finddevice("mpp")) != -1)
637262569Simp		if (fdt_is_compatible(node, "mrvl,mpp"))
638262569Simp			goto moveon;
639262569Simp	/*
640262569Simp	 * Find the node the long way.
641262569Simp	 */
642262569Simp	if ((node = OF_finddevice("/")) == -1)
643262569Simp		return (ENXIO);
644262569Simp
645262569Simp	if ((node = fdt_find_compatible(node, "simple-bus", 0)) == 0)
646262569Simp		return (ENXIO);
647262569Simp
648262569Simp	if ((node = fdt_find_compatible(node, "mrvl,mpp", 0)) == 0)
649262569Simp		return (ENXIO);
650262569Simpmoveon:
651262569Simp	/*
652270864Simp	 * Process 'reg' prop.
653270864Simp	 */
654270864Simp	if ((rv = fdt_addrsize_cells(OF_parent(node), &par_addr_cells,
655270864Simp	    &par_size_cells)) != 0)
656270864Simp		return(ENXIO);
657270864Simp
658270864Simp	tuple_size = sizeof(pcell_t) * (par_addr_cells + par_size_cells);
659270864Simp	len = OF_getprop(node, "reg", reg, sizeof(reg));
660270864Simp	tuples = len / tuple_size;
661270864Simp	if (tuple_size <= 0)
662270864Simp		return (EINVAL);
663270864Simp
664270864Simp	/*
665270864Simp	 * Get address/size. XXX we assume only the first 'reg' tuple is used.
666270864Simp	 */
667270864Simp	rv = fdt_data_to_res(reg, par_addr_cells, par_size_cells,
668270864Simp	    &start, &size);
669270864Simp	if (rv != 0)
670270864Simp		return (rv);
671270864Simp	start += fdt_immr_va;
672270864Simp
673270864Simp	/*
674270864Simp	 * Process 'pin-count' and 'pin-map' props.
675270864Simp	 */
676270864Simp	if (OF_getprop(node, "pin-count", &pin_count, sizeof(pin_count)) <= 0)
677270864Simp		return (ENXIO);
678270864Simp	pin_count = fdt32_to_cpu(pin_count);
679270864Simp	if (pin_count > MPP_PIN_MAX)
680270864Simp		return (ERANGE);
681270864Simp
682270864Simp	if (OF_getprop(node, "#pin-cells", &pin_cells, sizeof(pin_cells)) <= 0)
683270864Simp		pin_cells = MPP_PIN_CELLS;
684270864Simp	pin_cells = fdt32_to_cpu(pin_cells);
685270864Simp	if (pin_cells > MPP_PIN_CELLS)
686270864Simp		return (ERANGE);
687270864Simp	tuple_size = sizeof(pcell_t) * pin_cells;
688270864Simp
689270864Simp	bzero(pinmap, sizeof(pinmap));
690270864Simp	len = OF_getprop(node, "pin-map", pinmap, sizeof(pinmap));
691270864Simp	if (len <= 0)
692270864Simp		return (ERANGE);
693270864Simp	if (len % tuple_size)
694270864Simp		return (ERANGE);
695270864Simp	pins = len / tuple_size;
696270864Simp	if (pins > pin_count)
697270864Simp		return (ERANGE);
698270864Simp	/*
699270864Simp	 * Fill out a "mpp[pin] => function" table. All pins unspecified in
700270864Simp	 * the 'pin-map' property are defaulted to 0 function i.e. GPIO.
701270864Simp	 */
702270864Simp	bzero(mpp, sizeof(mpp));
703270864Simp	pinmap_ptr = pinmap;
704270864Simp	for (i = 0; i < pins; i++) {
705270864Simp		mpp_pin = fdt32_to_cpu(*pinmap_ptr);
706270864Simp		mpp_function = fdt32_to_cpu(*(pinmap_ptr + 1));
707270864Simp		mpp[mpp_pin] = mpp_function;
708270864Simp		pinmap_ptr += pin_cells;
709270864Simp	}
710270864Simp
711270864Simp	/*
712270864Simp	 * Prepare and program MPP control register values.
713270864Simp	 */
714270864Simp	ctrl_offset = 0;
715270864Simp	for (i = 0; i < pin_count;) {
716270864Simp		ctrl_val = 0;
717270864Simp
718270864Simp		for (j = 0; j < MPP_PINS_PER_REG; j++) {
719270864Simp			if (i + j == pin_count - 1)
720270864Simp				break;
721270864Simp			ctrl_val |= MPP_SEL(i + j, mpp[i + j]);
722270864Simp		}
723270864Simp		i += MPP_PINS_PER_REG;
724270864Simp		bus_space_write_4(fdtbus_bs_tag, start, ctrl_offset,
725270864Simp		    ctrl_val);
726270864Simp
727270864Simp#if defined(SOC_MV_ORION)
728270864Simp		/*
729270864Simp		 * Third MPP reg on Orion SoC is placed
730262569Simp		 * non-linearly (with different offset).
731262569Simp		 */
732262569Simp		if (i ==  (2 * MPP_PINS_PER_REG))
733262569Simp			ctrl_offset = 0x50;
734262569Simp		else
735262569Simp#endif
736262569Simp			ctrl_offset += 4;
737262569Simp	}
738262569Simp
739262569Simp	return (0);
740262569Simp}
741262569Simp
742262569Simp#define FDT_DEVMAP_MAX	(MV_WIN_CPU_MAX + 1)
743262569Simpstatic struct pmap_devmap fdt_devmap[FDT_DEVMAP_MAX] = {
744262569Simp	{ 0, 0, 0, 0, 0, }
745262569Simp};
746262569Simp
747262569Simp/*
748262569Simp * XXX: When device entry in devmap has pd_size smaller than section size,
749262569Simp * system will freeze during initialization
750262569Simp */
751262569Simp
752262569Simp/*
753262569Simp * Construct pmap_devmap[] with DT-derived config data.
754262569Simp */
755262569Simp
756262569Simpstatic int
757262569Simpplatform_devmap_init(void)
758262569Simp{
759262569Simp	phandle_t root, child;
760262569Simp	pcell_t bank_count;
761262569Simp	u_long base, size;
762262569Simp	int i, num_mapped;
763262569Simp
764262569Simp	i = 0;
765262569Simp	pmap_devmap_bootstrap_table = &fdt_devmap[0];
766262569Simp
767262569Simp	/*
768262569Simp	 * IMMR range.
769262569Simp	 */
770262569Simp	fdt_devmap[i].pd_va = fdt_immr_va;
771262569Simp	fdt_devmap[i].pd_pa = fdt_immr_pa;
772262569Simp	fdt_devmap[i].pd_size = fdt_immr_size;
773262569Simp	fdt_devmap[i].pd_prot = VM_PROT_READ | VM_PROT_WRITE;
774262569Simp	fdt_devmap[i].pd_cache = PTE_NOCACHE;
775262569Simp	i++;
776262569Simp
777262569Simp	/*
778262569Simp	 * PCI range(s) and localbus.
779262569Simp	 */
780295436Sandrew	if ((root = OF_finddevice("/")) == -1)
781295436Sandrew		return (ENXIO);
782295436Sandrew
783295436Sandrew	for (child = OF_child(root); child != 0; child = OF_peer(child)) {
784295436Sandrew		if (fdt_is_type(child, "pci")) {
785295436Sandrew			/*
786295436Sandrew			 * Check space: each PCI node will consume 2 devmap
787295436Sandrew			 * entries.
788295436Sandrew			 */
789295436Sandrew			if (i + 1 >= FDT_DEVMAP_MAX) {
790295436Sandrew				return (ENOMEM);
791295436Sandrew			}
792295436Sandrew
793295436Sandrew			/*
794295436Sandrew			 * XXX this should account for PCI and multiple ranges
795295436Sandrew			 * of a given kind.
796262569Simp			 */
797262569Simp			if (fdt_pci_devmap(child, &fdt_devmap[i],
798262569Simp			    MV_PCIE_IO_BASE, MV_PCIE_MEM_BASE) != 0)
799262569Simp				return (ENXIO);
800262569Simp			i += 2;
801262569Simp		}
802262569Simp
803262569Simp		if (fdt_is_compatible(child, "mrvl,lbc")) {
804262569Simp			/* Check available space */
805262569Simp			if (OF_getprop(child, "bank-count", (void *)&bank_count,
806262569Simp			    sizeof(bank_count)) <= 0)
807262569Simp				/* If no property, use default value */
808262569Simp				bank_count = 1;
809262569Simp			else
810262569Simp				bank_count = fdt32_to_cpu(bank_count);
811262569Simp
812262569Simp			if ((i + bank_count) >= FDT_DEVMAP_MAX)
813262569Simp				return (ENOMEM);
814262569Simp
815262569Simp			/* Add all localbus ranges to device map */
816262569Simp			num_mapped = 0;
817262569Simp
818262569Simp			if (fdt_localbus_devmap(child, &fdt_devmap[i],
819262569Simp			    (int)bank_count, &num_mapped) != 0)
820262569Simp				return (ENXIO);
821262569Simp
822262569Simp			i += num_mapped;
823262569Simp		}
824262569Simp	}
825262569Simp
826262569Simp	/*
827262569Simp	 * CESA SRAM range.
828262569Simp	 */
829262569Simp	if ((child = OF_finddevice("sram")) != -1)
830262569Simp		if (fdt_is_compatible(child, "mrvl,cesa-sram"))
831262569Simp			goto moveon;
832262569Simp
833262569Simp	if ((child = fdt_find_compatible(root, "mrvl,cesa-sram", 0)) == 0)
834262569Simp		/* No CESA SRAM node. */
835262569Simp		return (0);
836262569Simpmoveon:
837262569Simp	if (i >= FDT_DEVMAP_MAX)
838262569Simp		return (ENOMEM);
839262569Simp
840262569Simp	if (fdt_regsize(child, &base, &size) != 0)
841262569Simp		return (EINVAL);
842262569Simp
843262569Simp	fdt_devmap[i].pd_va = MV_CESA_SRAM_BASE; /* XXX */
844262569Simp	fdt_devmap[i].pd_pa = base;
845262569Simp	fdt_devmap[i].pd_size = size;
846262569Simp	fdt_devmap[i].pd_prot = VM_PROT_READ | VM_PROT_WRITE;
847262569Simp	fdt_devmap[i].pd_cache = PTE_NOCACHE;
848262569Simp
849262569Simp	return (0);
850262569Simp}
851262569Simp
852262569Simpstruct arm32_dma_range *
853262569Simpbus_dma_get_range(void)
854262569Simp{
855262569Simp
856262569Simp	return (NULL);
857262569Simp}
858262569Simp
859262569Simpint
860262569Simpbus_dma_get_range_nb(void)
861262569Simp{
862262569Simp
863262569Simp	return (0);
864262569Simp}
865262569Simp