1 /* $NetBSD: hpc_machdep.c,v 1.70 2003/09/16 08:18:22 agc Exp $ */
4 * Copyright (c) 1994-1998 Mark Brinicombe.
5 * Copyright (c) 1994 Brini.
8 * This code is derived from software written for Brini by Mark Brinicombe
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
37 * RiscBSD kernel project
41 * Machine dependant functions for kernel setup
43 * This file needs a lot of work.
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
51 #define _ARM32_BUS_DMA_PRIVATE
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/sysproto.h>
55 #include <sys/signalvar.h>
56 #include <sys/imgact.h>
57 #include <sys/kernel.h>
59 #include <sys/linker.h>
61 #include <sys/malloc.h>
62 #include <sys/mutex.h>
65 #include <sys/ptrace.h>
72 #include <sys/msgbuf.h>
73 #include <machine/reg.h>
74 #include <machine/cpu.h>
78 #include <vm/vm_object.h>
79 #include <vm/vm_page.h>
80 #include <vm/vm_map.h>
81 #include <machine/vmparam.h>
82 #include <machine/pcb.h>
83 #include <machine/undefined.h>
84 #include <machine/machdep.h>
85 #include <machine/metadata.h>
86 #include <machine/armreg.h>
87 #include <machine/bus.h>
88 #include <sys/reboot.h>
90 #include <arm/xscale/i80321/i80321reg.h>
91 #include <arm/xscale/i80321/i80321var.h>
92 #include <arm/xscale/i80321/iq80321reg.h>
93 #include <arm/xscale/i80321/obiovar.h>
95 #define KERNEL_PT_SYS 0 /* Page table for mapping proc0 zero page */
96 #define KERNEL_PT_IOPXS 1
97 #define KERNEL_PT_BEFOREKERN 2
98 #define KERNEL_PT_AFKERNEL 3 /* L2 table for mapping after kernel */
99 #define KERNEL_PT_AFKERNEL_NUM 9
101 /* this should be evenly divisable by PAGE_SIZE / L2_TABLE_SIZE_REAL (or 4) */
102 #define NUM_KERNEL_PTS (KERNEL_PT_AFKERNEL + KERNEL_PT_AFKERNEL_NUM)
104 extern u_int data_abort_handler_address;
105 extern u_int prefetch_abort_handler_address;
106 extern u_int undefined_handler_address;
108 struct pv_addr kernel_pt_table[NUM_KERNEL_PTS];
110 /* Physical and virtual addresses for some global pages */
112 vm_paddr_t phys_avail[10];
113 vm_paddr_t dump_avail[4];
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 struct pv_addr minidataclean;
124 /* #define IQ80321_OBIO_BASE 0xfe800000UL */
125 /* #define IQ80321_OBIO_SIZE 0x00100000UL */
127 /* Static device mappings. */
128 static const struct pmap_devmap ep80219_devmap[] = {
130 * Map the on-board devices VA == PA so that we can access them
131 * with the MMU on or off.
137 VM_PROT_READ|VM_PROT_WRITE,
142 VERDE_OUT_XLATE_IO_WIN0_BASE,
143 VERDE_OUT_XLATE_IO_WIN_SIZE,
144 VM_PROT_READ|VM_PROT_WRITE,
151 VM_PROT_READ|VM_PROT_WRITE,
163 extern vm_offset_t xscale_cache_clean_addr;
166 initarm(struct arm_boot_params *abp)
168 struct pv_addr kernel_l1pt;
169 struct pv_addr dpcpu;
172 vm_offset_t freemempos;
173 vm_offset_t freemem_pt;
174 vm_offset_t afterkern;
175 vm_offset_t freemem_after;
176 vm_offset_t lastaddr;
177 uint32_t memsize, memstart;
179 lastaddr = parse_boot_param(abp);
181 pcpu_init(pcpup, 0, sizeof(struct pcpu));
182 PCPU_SET(curthread, &thread0);
184 /* Do basic tuning, hz etc */
187 freemempos = 0xa0200000;
188 /* Define a macro to simplify memory allocation */
189 #define valloc_pages(var, np) \
190 alloc_pages((var).pv_pa, (np)); \
191 (var).pv_va = (var).pv_pa + 0x20000000;
193 #define alloc_pages(var, np) \
194 freemempos -= (np * PAGE_SIZE); \
195 (var) = freemempos; \
196 memset((char *)(var), 0, ((np) * PAGE_SIZE));
198 while (((freemempos - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) != 0)
199 freemempos -= PAGE_SIZE;
200 valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
201 for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
202 if (!(loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL))) {
203 valloc_pages(kernel_pt_table[loop],
204 L2_TABLE_SIZE / PAGE_SIZE);
206 kernel_pt_table[loop].pv_pa = freemempos +
207 (loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL)) *
209 kernel_pt_table[loop].pv_va =
210 kernel_pt_table[loop].pv_pa + 0x20000000;
213 freemem_pt = freemempos;
214 freemempos = 0xa0100000;
216 * Allocate a page for the system page mapped to V0x00000000
217 * This page will just contain the system vectors and can be
218 * shared by all processes.
220 valloc_pages(systempage, 1);
222 /* Allocate dynamic per-cpu area. */
223 valloc_pages(dpcpu, DPCPU_SIZE / PAGE_SIZE);
224 dpcpu_init((void *)dpcpu.pv_va, 0);
226 /* Allocate stacks for all modes */
227 valloc_pages(irqstack, IRQ_STACK_SIZE);
228 valloc_pages(abtstack, ABT_STACK_SIZE);
229 valloc_pages(undstack, UND_STACK_SIZE);
230 valloc_pages(kernelstack, KSTACK_PAGES);
231 alloc_pages(minidataclean.pv_pa, 1);
232 valloc_pages(msgbufpv, round_page(msgbufsize) / PAGE_SIZE);
233 #ifdef ARM_USE_SMALL_ALLOC
234 freemempos -= PAGE_SIZE;
235 freemem_pt = trunc_page(freemem_pt);
236 freemem_after = freemempos - ((freemem_pt - 0xa0100000) /
237 PAGE_SIZE) * sizeof(struct arm_small_page);
238 arm_add_smallalloc_pages((void *)(freemem_after + 0x20000000),
239 (void *)0xc0100000, freemem_pt - 0xa0100000, 1);
240 freemem_after -= ((freemem_after - 0xa0001000) / PAGE_SIZE) *
241 sizeof(struct arm_small_page);
242 arm_add_smallalloc_pages((void *)(freemem_after + 0x20000000),
243 (void *)0xc0001000, trunc_page(freemem_after) - 0xa0001000, 0);
245 freemempos = trunc_page(freemem_after);
246 freemempos -= PAGE_SIZE;
249 * Allocate memory for the l1 and l2 page tables. The scheme to avoid
250 * wasting memory by allocating the l1pt on the first 16k memory was
251 * taken from NetBSD rpc_machdep.c. NKPT should be greater than 12 for
252 * this to work (which is supposed to be the case).
256 * Now we start construction of the L1 page table
257 * We start by mapping the L2 page tables into the L1.
258 * This means that we can replace L1 mappings later on if necessary
260 l1pagetable = kernel_l1pt.pv_va;
262 /* Map the L2 pages tables in the L1 page table */
263 pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00100000 - 1),
264 &kernel_pt_table[KERNEL_PT_SYS]);
265 pmap_link_l2pt(l1pagetable, IQ80321_IOPXS_VBASE,
266 &kernel_pt_table[KERNEL_PT_IOPXS]);
267 pmap_link_l2pt(l1pagetable, KERNBASE,
268 &kernel_pt_table[KERNEL_PT_BEFOREKERN]);
269 pmap_map_chunk(l1pagetable, KERNBASE, IQ80321_SDRAM_START, 0x100000,
270 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
271 pmap_map_chunk(l1pagetable, KERNBASE + 0x100000, IQ80321_SDRAM_START + 0x100000,
272 0x100000, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
273 pmap_map_chunk(l1pagetable, KERNBASE + 0x200000, IQ80321_SDRAM_START + 0x200000,
274 (((uint32_t)(lastaddr) - KERNBASE - 0x200000) + L1_S_SIZE) & ~(L1_S_SIZE - 1),
275 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
276 freemem_after = ((int)lastaddr + PAGE_SIZE) & ~(PAGE_SIZE - 1);
277 afterkern = round_page(((vm_offset_t)lastaddr + L1_S_SIZE) & ~(L1_S_SIZE
279 for (i = 0; i < KERNEL_PT_AFKERNEL_NUM; i++) {
280 pmap_link_l2pt(l1pagetable, afterkern + i * 0x00100000,
281 &kernel_pt_table[KERNEL_PT_AFKERNEL + i]);
283 pmap_map_entry(l1pagetable, afterkern, minidataclean.pv_pa,
284 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
287 #ifdef ARM_USE_SMALL_ALLOC
288 if ((freemem_after + 2 * PAGE_SIZE) <= afterkern) {
289 arm_add_smallalloc_pages((void *)(freemem_after),
290 (void*)(freemem_after + PAGE_SIZE),
291 afterkern - (freemem_after + PAGE_SIZE), 0);
296 /* Map the Mini-Data cache clean area. */
297 xscale_setup_minidata(l1pagetable, afterkern,
298 minidataclean.pv_pa);
300 /* Map the vector page. */
301 pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
302 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
303 pmap_devmap_bootstrap(l1pagetable, ep80219_devmap);
305 * Give the XScale global cache clean code an appropriately
306 * sized chunk of unmapped VA space starting at 0xff000000
307 * (our device mappings end before this address).
309 xscale_cache_clean_addr = 0xff000000U;
311 cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
312 setttb(kernel_l1pt.pv_pa);
314 cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
316 * Pages were allocated during the secondary bootstrap for the
317 * stacks for different CPU modes.
318 * We must now set the r13 registers in the different CPU modes to
319 * point to these stacks.
320 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
321 * of the stack memory.
326 * We must now clean the cache again....
327 * Cleaning may be done by reading new data to displace any
328 * dirty data in the cache. This will have happened in setttb()
329 * but since we are boot strapping the addresses used for the read
330 * may have just been remapped and thus the cache could be out
331 * of sync. A re-clean after the switch will cure this.
332 * After booting there are no gross relocations of the kernel thus
333 * this problem will not occur after initarm().
335 cpu_idcache_wbinv_all();
337 * Fetch the SDRAM start/size from the i80321 SDRAM configration
340 i80321_calibrate_delay();
341 i80321_sdram_bounds(&obio_bs_tag, IQ80321_80321_VBASE + VERDE_MCU_BASE,
342 &memstart, &memsize);
343 physmem = memsize / PAGE_SIZE;
346 /* Set stack for exception handlers */
348 data_abort_handler_address = (u_int)data_abort_handler;
349 prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
350 undefined_handler_address = (u_int)undefinedinstruction_bounce;
353 init_proc0(kernelstack.pv_va);
355 /* Enable MMU, I-cache, D-cache, write buffer. */
357 arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
358 pmap_curmaxkvaddr = afterkern + PAGE_SIZE;
360 * ARM_USE_SMALL_ALLOC uses dump_avail, so it must be filled before
361 * calling pmap_bootstrap.
363 dump_avail[0] = 0xa0000000;
364 dump_avail[1] = 0xa0000000 + memsize;
368 vm_max_kernel_address = 0xd0000000;
369 pmap_bootstrap(pmap_curmaxkvaddr, &kernel_l1pt);
370 msgbufp = (void*)msgbufpv.pv_va;
371 msgbufinit(msgbufp, msgbufsize);
375 #ifdef ARM_USE_SMALL_ALLOC
376 phys_avail[i++] = 0xa0000000;
377 phys_avail[i++] = 0xa0001000; /*
378 *XXX: Gross hack to get our
379 * pages in the vm_page_array
382 phys_avail[i++] = round_page(virtual_avail - KERNBASE + IQ80321_SDRAM_START);
383 phys_avail[i++] = trunc_page(0xa0000000 + memsize - 1);
387 init_param2(physmem);
389 return ((void *)(kernelstack.pv_va + USPACE_SVC_STACK_TOP -
390 sizeof(struct pcb)));
394 machdep_pci_route_interrupt(device_t pcib, device_t dev, int pin)
400 struct i80321_pci_softc *sc = device_get_softc(pcib);
401 bus = pci_get_bus(dev);
402 device = pci_get_slot(dev);
403 func = pci_get_function(dev);
404 busno = bus_space_read_4(sc->sc_st, sc->sc_atu_sh, ATU_PCIXSR);
405 busno = PCIXSR_BUSNO(busno);
412 case 1: /* Ethernet i82555 10/100 */
413 printf("Device %d routed to irq %d\n", device, ICU_INT_XINT(0));
414 return (ICU_INT_XINT(0));
416 printf("Device %d routed to irq %d\n", device, ICU_INT_XINT(1));
417 return (ICU_INT_XINT(1));
420 * The S-ATA chips are behind the bridge, and all of
421 * the S-ATA interrupts are wired together.
423 printf("Device %d routed to irq %d\n", device, ICU_INT_XINT(2));
424 return (ICU_INT_XINT(2));
425 case 4: /* MINI-PIC_INT */
426 printf("Device %d routed to irq %d\n", device, ICU_INT_XINT(3));
427 return( ICU_INT_XINT(3));
430 printf("No mapping for %d/%d/%d/%c\n", bus, device, func, pin);