iq31244_machdep.c revision 266175
1123626Snjl/*	$NetBSD: hpc_machdep.c,v 1.70 2003/09/16 08:18:22 agc Exp $	*/
2123626Snjl
3123626Snjl/*-
4123626Snjl * Copyright (c) 1994-1998 Mark Brinicombe.
5123626Snjl * Copyright (c) 1994 Brini.
6123626Snjl * All rights reserved.
7123626Snjl *
8123626Snjl * This code is derived from software written for Brini by Mark Brinicombe
9123626Snjl *
10123626Snjl * Redistribution and use in source and binary forms, with or without
11123626Snjl * modification, are permitted provided that the following conditions
12168283Sdes * are met:
13136224Smtm * 1. Redistributions of source code must retain the above copyright
14123626Snjl *    notice, this list of conditions and the following disclaimer.
15123626Snjl * 2. Redistributions in binary form must reproduce the above copyright
16123626Snjl *    notice, this list of conditions and the following disclaimer in the
17123626Snjl *    documentation and/or other materials provided with the distribution.
18174464Sdougb * 3. All advertising materials mentioning features or use of this software
19123626Snjl *    must display the following acknowledgement:
20123626Snjl *      This product includes software developed by Brini.
21123626Snjl * 4. The name of the company nor the name of the author may be used to
22123626Snjl *    endorse or promote products derived from this software without specific
23123626Snjl *    prior written permission.
24123626Snjl *
25123626Snjl * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
26123626Snjl * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27123626Snjl * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28123626Snjl * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29123626Snjl * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30238416Skevlo * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31123626Snjl * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32123626Snjl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33123626Snjl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34123626Snjl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35123626Snjl * SUCH DAMAGE.
36123626Snjl *
37123626Snjl * RiscBSD kernel project
38123626Snjl *
39123626Snjl * machdep.c
40123626Snjl *
41123626Snjl * Machine dependant functions for kernel setup
42123626Snjl *
43123626Snjl * This file needs a lot of work.
44123626Snjl *
45142572Snjl * Created      : 17/09/94
46142572Snjl */
47142572Snjl
48123626Snjl#include <sys/cdefs.h>
49123626Snjl__FBSDID("$FreeBSD: stable/10/sys/arm/xscale/i80321/iq31244_machdep.c 266175 2014-05-15 19:09:31Z ian $");
50123626Snjl
51123626Snjl#define _ARM32_BUS_DMA_PRIVATE
52123626Snjl#include <sys/param.h>
53179965Smtm#include <sys/systm.h>
54179965Smtm#include <sys/sysproto.h>
55179965Smtm#include <sys/signalvar.h>
56179965Smtm#include <sys/imgact.h>
57179965Smtm#include <sys/kernel.h>
58123626Snjl#include <sys/ktr.h>
59123626Snjl#include <sys/linker.h>
60123626Snjl#include <sys/lock.h>
61123626Snjl#include <sys/malloc.h>
62123626Snjl#include <sys/mutex.h>
63123626Snjl#include <sys/pcpu.h>
64123626Snjl#include <sys/proc.h>
65123626Snjl#include <sys/ptrace.h>
66123626Snjl#include <sys/cons.h>
67123626Snjl#include <sys/bio.h>
68123626Snjl#include <sys/bus.h>
69123626Snjl#include <sys/buf.h>
70123626Snjl#include <sys/exec.h>
71123626Snjl#include <sys/kdb.h>
72123626Snjl#include <sys/msgbuf.h>
73123626Snjl#include <machine/reg.h>
74123626Snjl#include <machine/cpu.h>
75123626Snjl
76123626Snjl#include <vm/vm.h>
77123626Snjl#include <vm/pmap.h>
78123626Snjl#include <vm/vm_object.h>
79123626Snjl#include <vm/vm_page.h>
80123626Snjl#include <vm/vm_map.h>
81123626Snjl#include <machine/devmap.h>
82123626Snjl#include <machine/vmparam.h>
83129021Snjl#include <machine/pcb.h>
84240343Savg#include <machine/undefined.h>
85123626Snjl#include <machine/machdep.h>
86123626Snjl#include <machine/metadata.h>
87123626Snjl#include <machine/armreg.h>
88141417Snjl#include <machine/bus.h>
89142523Snjl#include <sys/reboot.h>
90142523Snjl
91142523Snjl#include <arm/xscale/i80321/i80321reg.h>
92238009Ssbruno#include <arm/xscale/i80321/i80321var.h>
93141417Snjl#include <arm/xscale/i80321/iq80321reg.h>
94123626Snjl#include <arm/xscale/i80321/obiovar.h>
95123626Snjl
96123626Snjl#define	KERNEL_PT_SYS		0	/* Page table for mapping proc0 zero page */
97#define	KERNEL_PT_IOPXS		1
98#define	KERNEL_PT_BEFOREKERN	2
99#define	KERNEL_PT_AFKERNEL	3	/* L2 table for mapping after kernel */
100#define	KERNEL_PT_AFKERNEL_NUM	9
101
102/* this should be evenly divisable by PAGE_SIZE / L2_TABLE_SIZE_REAL (or 4) */
103#define NUM_KERNEL_PTS		(KERNEL_PT_AFKERNEL + KERNEL_PT_AFKERNEL_NUM)
104
105extern u_int data_abort_handler_address;
106extern u_int prefetch_abort_handler_address;
107extern u_int undefined_handler_address;
108
109struct pv_addr kernel_pt_table[NUM_KERNEL_PTS];
110
111/* Physical and virtual addresses for some global pages */
112
113vm_paddr_t phys_avail[10];
114vm_paddr_t dump_avail[4];
115
116struct pv_addr systempage;
117struct pv_addr msgbufpv;
118struct pv_addr irqstack;
119struct pv_addr undstack;
120struct pv_addr abtstack;
121struct pv_addr kernelstack;
122struct pv_addr minidataclean;
123
124#define IQ80321_OBIO_BASE 0xfe800000UL
125#define IQ80321_OBIO_SIZE 0x00100000UL
126/* Static device mappings. */
127static const struct arm_devmap_entry iq80321_devmap[] = {
128	/*
129	 * Map the on-board devices VA == PA so that we can access them
130	 * with the MMU on or off.
131	 */
132	    {
133		    IQ80321_OBIO_BASE,
134		    IQ80321_OBIO_BASE,
135		    IQ80321_OBIO_SIZE,
136		    VM_PROT_READ|VM_PROT_WRITE,
137		    PTE_NOCACHE,
138	    },
139	    {
140	    	    IQ80321_IOW_VBASE,
141		    VERDE_OUT_XLATE_IO_WIN0_BASE,
142		    VERDE_OUT_XLATE_IO_WIN_SIZE,
143		    VM_PROT_READ|VM_PROT_WRITE,
144		    PTE_NOCACHE,
145	    },
146
147	    {
148		    IQ80321_80321_VBASE,
149		    VERDE_PMMR_BASE,
150		    VERDE_PMMR_SIZE,
151		    VM_PROT_READ|VM_PROT_WRITE,
152		    PTE_NOCACHE,
153	    },
154	    {
155		    0,
156		    0,
157		    0,
158		    0,
159		    0,
160	    }
161};
162
163#define SDRAM_START 0xa0000000
164
165extern vm_offset_t xscale_cache_clean_addr;
166
167void *
168initarm(struct arm_boot_params *abp)
169{
170	struct pv_addr  kernel_l1pt;
171	struct pv_addr  dpcpu;
172	int loop, i;
173	u_int l1pagetable;
174	vm_offset_t freemempos;
175	vm_offset_t freemem_pt;
176	vm_offset_t afterkern;
177	vm_offset_t freemem_after;
178	vm_offset_t lastaddr;
179	uint32_t memsize, memstart;
180
181	lastaddr = parse_boot_param(abp);
182	set_cpufuncs();
183	pcpu_init(pcpup, 0, sizeof(struct pcpu));
184	PCPU_SET(curthread, &thread0);
185
186	/* Do basic tuning, hz etc */
187	init_param1();
188
189	freemempos = 0xa0200000;
190	/* Define a macro to simplify memory allocation */
191#define	valloc_pages(var, np)			\
192	alloc_pages((var).pv_pa, (np));		\
193	(var).pv_va = (var).pv_pa + 0x20000000;
194
195#define alloc_pages(var, np)			\
196	freemempos -= (np * PAGE_SIZE);		\
197	(var) = freemempos;		\
198	memset((char *)(var), 0, ((np) * PAGE_SIZE));
199
200	while (((freemempos - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) != 0)
201		freemempos -= PAGE_SIZE;
202	valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
203	for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
204		if (!(loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL))) {
205			valloc_pages(kernel_pt_table[loop],
206			    L2_TABLE_SIZE / PAGE_SIZE);
207		} else {
208			kernel_pt_table[loop].pv_pa = freemempos +
209			    (loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL)) *
210			    L2_TABLE_SIZE_REAL;
211			kernel_pt_table[loop].pv_va =
212			    kernel_pt_table[loop].pv_pa + 0x20000000;
213		}
214	}
215	freemem_pt = freemempos;
216	freemempos = 0xa0100000;
217	/*
218	 * Allocate a page for the system page mapped to V0x00000000
219	 * This page will just contain the system vectors and can be
220	 * shared by all processes.
221	 */
222	valloc_pages(systempage, 1);
223
224	/* Allocate dynamic per-cpu area. */
225	valloc_pages(dpcpu, DPCPU_SIZE / PAGE_SIZE);
226	dpcpu_init((void *)dpcpu.pv_va, 0);
227
228	/* Allocate stacks for all modes */
229	valloc_pages(irqstack, IRQ_STACK_SIZE);
230	valloc_pages(abtstack, ABT_STACK_SIZE);
231	valloc_pages(undstack, UND_STACK_SIZE);
232	valloc_pages(kernelstack, KSTACK_PAGES);
233	alloc_pages(minidataclean.pv_pa, 1);
234	valloc_pages(msgbufpv, round_page(msgbufsize) / PAGE_SIZE);
235	/*
236	 * Allocate memory for the l1 and l2 page tables. The scheme to avoid
237	 * wasting memory by allocating the l1pt on the first 16k memory was
238	 * taken from NetBSD rpc_machdep.c. NKPT should be greater than 12 for
239	 * this to work (which is supposed to be the case).
240	 */
241
242	/*
243	 * Now we start construction of the L1 page table
244	 * We start by mapping the L2 page tables into the L1.
245	 * This means that we can replace L1 mappings later on if necessary
246	 */
247	l1pagetable = kernel_l1pt.pv_va;
248
249	/* Map the L2 pages tables in the L1 page table */
250	pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00100000 - 1),
251	    &kernel_pt_table[KERNEL_PT_SYS]);
252	pmap_link_l2pt(l1pagetable, IQ80321_IOPXS_VBASE,
253	    &kernel_pt_table[KERNEL_PT_IOPXS]);
254	pmap_link_l2pt(l1pagetable, KERNBASE,
255	    &kernel_pt_table[KERNEL_PT_BEFOREKERN]);
256	pmap_map_chunk(l1pagetable, KERNBASE, SDRAM_START, 0x100000,
257	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
258	pmap_map_chunk(l1pagetable, KERNBASE + 0x100000, SDRAM_START + 0x100000,
259	    0x100000, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
260	pmap_map_chunk(l1pagetable, KERNBASE + 0x200000, SDRAM_START + 0x200000,
261	    (((uint32_t)(lastaddr) - KERNBASE - 0x200000) + L1_S_SIZE) & ~(L1_S_SIZE - 1),
262	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
263	freemem_after = ((int)lastaddr + PAGE_SIZE) & ~(PAGE_SIZE - 1);
264	afterkern = round_page(((vm_offset_t)lastaddr + L1_S_SIZE) & ~(L1_S_SIZE
265	    - 1));
266	for (i = 0; i < KERNEL_PT_AFKERNEL_NUM; i++) {
267		pmap_link_l2pt(l1pagetable, afterkern + i * 0x00100000,
268		    &kernel_pt_table[KERNEL_PT_AFKERNEL + i]);
269	}
270	pmap_map_entry(l1pagetable, afterkern, minidataclean.pv_pa,
271	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
272
273
274	/* Map the Mini-Data cache clean area. */
275	xscale_setup_minidata(l1pagetable, afterkern,
276	    minidataclean.pv_pa);
277
278	/* Map the vector page. */
279	pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
280	    VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
281	arm_devmap_bootstrap(l1pagetable, iq80321_devmap);
282	/*
283	 * Give the XScale global cache clean code an appropriately
284	 * sized chunk of unmapped VA space starting at 0xff000000
285	 * (our device mappings end before this address).
286	 */
287	xscale_cache_clean_addr = 0xff000000U;
288
289	cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
290	setttb(kernel_l1pt.pv_pa);
291	cpu_tlb_flushID();
292	cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
293	/*
294	 * Pages were allocated during the secondary bootstrap for the
295	 * stacks for different CPU modes.
296	 * We must now set the r13 registers in the different CPU modes to
297	 * point to these stacks.
298	 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
299	 * of the stack memory.
300	 */
301	set_stackptrs(0);
302
303	/*
304	 * We must now clean the cache again....
305	 * Cleaning may be done by reading new data to displace any
306	 * dirty data in the cache. This will have happened in setttb()
307	 * but since we are boot strapping the addresses used for the read
308	 * may have just been remapped and thus the cache could be out
309	 * of sync. A re-clean after the switch will cure this.
310	 * After booting there are no gross relocations of the kernel thus
311	 * this problem will not occur after initarm().
312	 */
313	cpu_idcache_wbinv_all();
314	cpu_setup("");
315
316	/*
317	 * Fetch the SDRAM start/size from the i80321 SDRAM configration
318	 * registers.
319	 */
320	i80321_calibrate_delay();
321	i80321_sdram_bounds(&obio_bs_tag, IQ80321_80321_VBASE + VERDE_MCU_BASE,
322	    &memstart, &memsize);
323	physmem = memsize / PAGE_SIZE;
324	cninit();
325
326	/* Set stack for exception handlers */
327
328	data_abort_handler_address = (u_int)data_abort_handler;
329	prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
330	undefined_handler_address = (u_int)undefinedinstruction_bounce;
331	undefined_init();
332
333	init_proc0(kernelstack.pv_va);
334
335	/* Enable MMU, I-cache, D-cache, write buffer. */
336
337	arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
338	pmap_curmaxkvaddr = afterkern + PAGE_SIZE;
339	dump_avail[0] = 0xa0000000;
340	dump_avail[1] = 0xa0000000 + memsize;
341	dump_avail[2] = 0;
342	dump_avail[3] = 0;
343
344	vm_max_kernel_address = 0xd0000000;
345	pmap_bootstrap(pmap_curmaxkvaddr, &kernel_l1pt);
346	msgbufp = (void*)msgbufpv.pv_va;
347	msgbufinit(msgbufp, msgbufsize);
348	mutex_init();
349
350	i = 0;
351	phys_avail[i++] = round_page(virtual_avail - KERNBASE + SDRAM_START);
352	phys_avail[i++] = trunc_page(0xa0000000 + memsize - 1);
353	phys_avail[i++] = 0;
354	phys_avail[i] = 0;
355
356	init_param2(physmem);
357	kdb_init();
358	return ((void *)(kernelstack.pv_va + USPACE_SVC_STACK_TOP -
359	    sizeof(struct pcb)));
360}
361
362extern int
363machdep_pci_route_interrupt(device_t pcib, device_t dev, int pin)
364{
365	int bus;
366	int device;
367	int func;
368	uint32_t busno;
369	struct i80321_pci_softc *sc = device_get_softc(pcib);
370	bus = pci_get_bus(dev);
371	device = pci_get_slot(dev);
372	func = pci_get_function(dev);
373	busno = bus_space_read_4(sc->sc_st, sc->sc_atu_sh, ATU_PCIXSR);
374	busno = PCIXSR_BUSNO(busno);
375	if (busno == 0xff)
376		busno = 0;
377	if (bus != busno)
378		goto no_mapping;
379	switch (device) {
380		/* IQ31244 PCI */
381	case 1: /* PCIX-PCIX bridge */
382		/*
383		 * The S-ATA chips are behind the bridge, and all of
384		 * the S-ATA interrupts are wired together.
385		 */
386		return (ICU_INT_XINT(2));
387	case 2: /* PCI slot */
388		/* All pins are wired together. */
389		return (ICU_INT_XINT(3));
390	case 3: /* i82546 dual Gig-E */
391		if (pin == 1 || pin == 2)
392			return (ICU_INT_XINT(0));
393		goto no_mapping;
394		/* IQ80321 PCI */
395	case 4: /* i82544 Gig-E */
396	case 8: /*
397		 * Apparently you can set the device for the ethernet adapter
398		 * to 8 with a jumper, so handle that as well
399		 */
400		if (pin == 1)
401			return (ICU_INT_XINT(0));
402		goto no_mapping;
403	case 6: /* S-PCI-X slot */
404		if (pin == 1)
405			return (ICU_INT_XINT(2));
406		if (pin == 2)
407			return (ICU_INT_XINT(3));
408		goto no_mapping;
409	default:
410no_mapping:
411		printf("No mapping for %d/%d/%d/%c\n", bus, device, func, pin);
412
413	}
414	return (0);
415
416}
417