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 "opt_msgbuf.h"
51 #include <sys/cdefs.h>
52 __FBSDID("$FreeBSD$");
54 #define _ARM32_BUS_DMA_PRIVATE
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/sysproto.h>
58 #include <sys/signalvar.h>
59 #include <sys/imgact.h>
60 #include <sys/kernel.h>
62 #include <sys/linker.h>
64 #include <sys/malloc.h>
65 #include <sys/mutex.h>
68 #include <sys/ptrace.h>
75 #include <sys/msgbuf.h>
76 #include <machine/reg.h>
77 #include <machine/cpu.h>
81 #include <vm/vm_object.h>
82 #include <vm/vm_page.h>
83 #include <vm/vm_pager.h>
84 #include <vm/vm_map.h>
85 #include <vm/vnode_pager.h>
86 #include <machine/pmap.h>
87 #include <machine/vmparam.h>
88 #include <machine/pcb.h>
89 #include <machine/undefined.h>
90 #include <machine/machdep.h>
91 #include <machine/metadata.h>
92 #include <machine/armreg.h>
93 #include <machine/bus.h>
94 #include <sys/reboot.h>
96 #include <arm/xscale/pxa/pxareg.h>
97 #include <arm/xscale/pxa/pxavar.h>
99 #define KERNEL_PT_SYS 0 /* Page table for mapping proc0 zero page */
100 #define KERNEL_PT_IOPXS 1
101 #define KERNEL_PT_BEFOREKERN 2
102 #define KERNEL_PT_AFKERNEL 3 /* L2 table for mapping after kernel */
103 #define KERNEL_PT_AFKERNEL_NUM 9
105 /* this should be evenly divisable by PAGE_SIZE / L2_TABLE_SIZE_REAL (or 4) */
106 #define NUM_KERNEL_PTS (KERNEL_PT_AFKERNEL + KERNEL_PT_AFKERNEL_NUM)
108 /* Define various stack sizes in pages */
109 #define IRQ_STACK_SIZE 1
110 #define ABT_STACK_SIZE 1
111 #define UND_STACK_SIZE 1
113 extern u_int data_abort_handler_address;
114 extern u_int prefetch_abort_handler_address;
115 extern u_int undefined_handler_address;
117 struct pv_addr kernel_pt_table[NUM_KERNEL_PTS];
124 struct pcpu *pcpup = &__pcpu;
126 /* Physical and virtual addresses for some global pages */
128 vm_paddr_t phys_avail[PXA2X0_SDRAM_BANKS * 2 + 4];
129 vm_paddr_t dump_avail[PXA2X0_SDRAM_BANKS * 2 + 4];
130 vm_offset_t physical_pages;
132 struct pv_addr systempage;
133 struct pv_addr msgbufpv;
134 struct pv_addr irqstack;
135 struct pv_addr undstack;
136 struct pv_addr abtstack;
137 struct pv_addr kernelstack;
138 struct pv_addr minidataclean;
140 static struct trapframe proc0_tf;
142 static void pxa_probe_sdram(bus_space_tag_t, bus_space_handle_t,
143 uint32_t *, uint32_t *);
145 /* Static device mappings. */
146 static const struct pmap_devmap pxa_devmap[] = {
148 * Map the on-board devices up into the KVA region so we don't muck
152 PXA2X0_PERIPH_START + PXA2X0_PERIPH_OFFSET,
154 PXA250_PERIPH_END - PXA2X0_PERIPH_START,
155 VM_PROT_READ|VM_PROT_WRITE,
161 #define SDRAM_START 0xa0000000
163 extern vm_offset_t xscale_cache_clean_addr;
166 initarm(void *arg, void *arg2)
168 struct pv_addr kernel_l1pt;
171 vm_offset_t freemempos;
172 vm_offset_t freemem_pt;
173 vm_offset_t afterkern;
174 vm_offset_t freemem_after;
175 vm_offset_t lastaddr;
177 uint32_t memsize[PXA2X0_SDRAM_BANKS], memstart[PXA2X0_SDRAM_BANKS];
181 lastaddr = fake_preload_metadata();
182 pcpu_init(pcpup, 0, sizeof(struct pcpu));
183 PCPU_SET(curthread, &thread0);
185 freemempos = 0xa0200000;
186 /* Define a macro to simplify memory allocation */
187 #define valloc_pages(var, np) \
188 alloc_pages((var).pv_pa, (np)); \
189 (var).pv_va = (var).pv_pa + 0x20000000;
191 #define alloc_pages(var, np) \
192 freemempos -= (np * PAGE_SIZE); \
193 (var) = freemempos; \
194 memset((char *)(var), 0, ((np) * PAGE_SIZE));
196 while (((freemempos - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) != 0)
197 freemempos -= PAGE_SIZE;
198 valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
199 for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
200 if (!(loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL))) {
201 valloc_pages(kernel_pt_table[loop],
202 L2_TABLE_SIZE / PAGE_SIZE);
204 kernel_pt_table[loop].pv_pa = freemempos +
205 (loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL)) *
207 kernel_pt_table[loop].pv_va =
208 kernel_pt_table[loop].pv_pa + 0x20000000;
212 freemem_pt = freemempos;
213 freemempos = 0xa0100000;
215 * Allocate a page for the system page mapped to V0x00000000
216 * This page will just contain the system vectors and can be
217 * shared by all processes.
219 valloc_pages(systempage, 1);
221 /* Allocate stacks for all modes */
222 valloc_pages(irqstack, IRQ_STACK_SIZE);
223 valloc_pages(abtstack, ABT_STACK_SIZE);
224 valloc_pages(undstack, UND_STACK_SIZE);
225 valloc_pages(kernelstack, KSTACK_PAGES);
226 alloc_pages(minidataclean.pv_pa, 1);
227 valloc_pages(msgbufpv, round_page(MSGBUF_SIZE) / PAGE_SIZE);
228 #ifdef ARM_USE_SMALL_ALLOC
229 freemempos -= PAGE_SIZE;
230 freemem_pt = trunc_page(freemem_pt);
231 freemem_after = freemempos - ((freemem_pt - 0xa0100000) /
232 PAGE_SIZE) * sizeof(struct arm_small_page);
233 arm_add_smallalloc_pages((void *)(freemem_after + 0x20000000)
234 , (void *)0xc0100000, freemem_pt - 0xa0100000, 1);
235 freemem_after -= ((freemem_after - 0xa0001000) / PAGE_SIZE) *
236 sizeof(struct arm_small_page);
237 arm_add_smallalloc_pages((void *)(freemem_after + 0x20000000)
238 , (void *)0xc0001000, trunc_page(freemem_after) - 0xa0001000, 0);
239 freemempos = trunc_page(freemem_after);
240 freemempos -= PAGE_SIZE;
243 * Allocate memory for the l1 and l2 page tables. The scheme to avoid
244 * wasting memory by allocating the l1pt on the first 16k memory was
245 * taken from NetBSD rpc_machdep.c. NKPT should be greater than 12 for
246 * this to work (which is supposed to be the case).
250 * Now we start construction of the L1 page table
251 * We start by mapping the L2 page tables into the L1.
252 * This means that we can replace L1 mappings later on if necessary
254 l1pagetable = kernel_l1pt.pv_va;
256 /* Map the L2 pages tables in the L1 page table */
257 pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00100000 - 1),
258 &kernel_pt_table[KERNEL_PT_SYS]);
259 #if 0 /* XXXBJR: What is this? Don't know if there's an analogue. */
260 pmap_link_l2pt(l1pagetable, IQ80321_IOPXS_VBASE,
261 &kernel_pt_table[KERNEL_PT_IOPXS]);
263 pmap_link_l2pt(l1pagetable, KERNBASE,
264 &kernel_pt_table[KERNEL_PT_BEFOREKERN]);
265 pmap_map_chunk(l1pagetable, KERNBASE, SDRAM_START, 0x100000,
266 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
267 pmap_map_chunk(l1pagetable, KERNBASE + 0x100000, SDRAM_START + 0x100000,
268 0x100000, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
269 pmap_map_chunk(l1pagetable, KERNBASE + 0x200000, SDRAM_START + 0x200000,
270 (((uint32_t)(lastaddr) - KERNBASE - 0x200000) + L1_S_SIZE) & ~(L1_S_SIZE - 1),
271 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
272 freemem_after = ((int)lastaddr + PAGE_SIZE) & ~(PAGE_SIZE - 1);
273 afterkern = round_page(((vm_offset_t)lastaddr + L1_S_SIZE) &
275 for (i = 0; i < KERNEL_PT_AFKERNEL_NUM; i++) {
276 pmap_link_l2pt(l1pagetable, afterkern + i * 0x00100000,
277 &kernel_pt_table[KERNEL_PT_AFKERNEL + i]);
279 pmap_map_entry(l1pagetable, afterkern, minidataclean.pv_pa,
280 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
282 #ifdef ARM_USE_SMALL_ALLOC
283 if ((freemem_after + 2 * PAGE_SIZE) <= afterkern) {
284 arm_add_smallalloc_pages((void *)(freemem_after),
285 (void*)(freemem_after + PAGE_SIZE),
286 afterkern - (freemem_after + PAGE_SIZE), 0);
290 /* Map the Mini-Data cache clean area. */
291 xscale_setup_minidata(l1pagetable, afterkern,
292 minidataclean.pv_pa);
294 /* Map the vector page. */
295 pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
296 VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
297 pmap_devmap_bootstrap(l1pagetable, pxa_devmap);
300 * Give the XScale global cache clean code an appropriately
301 * sized chunk of unmapped VA space starting at 0xff000000
302 * (our device mappings end before this address).
304 xscale_cache_clean_addr = 0xff000000U;
306 cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
307 setttb(kernel_l1pt.pv_pa);
309 cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
312 * Pages were allocated during the secondary bootstrap for the
313 * stacks for different CPU modes.
314 * We must now set the r13 registers in the different CPU modes to
315 * point to these stacks.
316 * Since the ARM stacks use STMFD etc. we must set r13 to the top end
317 * of the stack memory.
319 set_stackptr(PSR_IRQ32_MODE,
320 irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
321 set_stackptr(PSR_ABT32_MODE,
322 abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
323 set_stackptr(PSR_UND32_MODE,
324 undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
327 * We must now clean the cache again....
328 * Cleaning may be done by reading new data to displace any
329 * dirty data in the cache. This will have happened in setttb()
330 * but since we are boot strapping the addresses used for the read
331 * may have just been remapped and thus the cache could be out
332 * of sync. A re-clean after the switch will cure this.
333 * After booting there are no gross reloations of the kernel thus
334 * this problem will not occur after initarm().
336 cpu_idcache_wbinv_all();
339 * Sort out bus_space for on-board devices.
344 * Fetch the SDRAM start/size from the PXA2X0 SDRAM configration
347 pxa_probe_sdram(obio_tag, PXA2X0_MEMCTL_BASE, memstart, memsize);
350 for (i = 0; i < PXA2X0_SDRAM_BANKS; i++) {
351 physmem += memsize[i] / PAGE_SIZE;
354 /* Fire up consoles. */
357 /* Set stack for exception handlers */
358 data_abort_handler_address = (u_int)data_abort_handler;
359 prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
360 undefined_handler_address = (u_int)undefinedinstruction_bounce;
363 proc_linkup(&proc0, &thread0);
364 thread0.td_kstack = kernelstack.pv_va;
365 thread0.td_pcb = (struct pcb *)
366 (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
367 thread0.td_pcb->pcb_flags = 0;
368 thread0.td_frame = &proc0_tf;
369 pcpup->pc_curpcb = thread0.td_pcb;
371 /* Enable MMU, I-cache, D-cache, write buffer. */
372 arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
374 pmap_curmaxkvaddr = afterkern + PAGE_SIZE;
376 * ARM USE_SMALL_ALLOC uses dump_avail, so it must be filled before
377 * calling pmap_bootstrap.
380 for (j = 0; j < PXA2X0_SDRAM_BANKS; j++) {
381 if (memsize[j] > 0) {
382 dump_avail[i++] = round_page(memstart[j]);
384 trunc_page(memstart[j] + memsize[j]);
389 pmap_bootstrap(pmap_curmaxkvaddr, 0xd0000000, &kernel_l1pt);
390 msgbufp = (void*)msgbufpv.pv_va;
391 msgbufinit(msgbufp, MSGBUF_SIZE);
395 #ifdef ARM_USE_SMALL_ALLOC
396 phys_avail[i++] = 0xa0000000;
397 phys_avail[i++] = 0xa0001000; /*
398 *XXX: Gross hack to get our
399 * pages in the vm_page_array
402 for (j = 0; j < PXA2X0_SDRAM_BANKS; j++) {
403 if (memsize[j] > 0) {
404 phys_avail[i] = round_page(memstart[j]);
405 dump_avail[i++] = round_page(memstart[j]);
407 trunc_page(memstart[j] + memsize[j]);
409 trunc_page(memstart[j] + memsize[j]);
417 #ifdef ARM_USE_SMALL_ALLOC
418 phys_avail[2] = round_page(virtual_avail - KERNBASE + phys_avail[2]);
420 phys_avail[0] = round_page(virtual_avail - KERNBASE + phys_avail[0]);
423 /* Do basic tuning, hz etc */
425 init_param2(physmem);
427 return ((void *)(kernelstack.pv_va + USPACE_SVC_STACK_TOP -
428 sizeof(struct pcb)));
432 pxa_probe_sdram(bus_space_tag_t bst, bus_space_handle_t bsh,
433 uint32_t *memstart, uint32_t *memsize)
435 uint32_t mdcnfg, dwid, dcac, drac, dnb;
438 mdcnfg = bus_space_read_4(bst, bsh, MEMCTL_MDCNFG);
441 * Scan all 4 SDRAM banks
443 for (i = 0; i < PXA2X0_SDRAM_BANKS; i++) {
450 if ((i == 0 && (mdcnfg & MDCNFG_DE0) == 0) ||
451 (i == 1 && (mdcnfg & MDCNFG_DE1) == 0))
453 dwid = mdcnfg >> MDCNFD_DWID01_SHIFT;
454 dcac = mdcnfg >> MDCNFD_DCAC01_SHIFT;
455 drac = mdcnfg >> MDCNFD_DRAC01_SHIFT;
456 dnb = mdcnfg >> MDCNFD_DNB01_SHIFT;
461 if ((i == 2 && (mdcnfg & MDCNFG_DE2) == 0) ||
462 (i == 3 && (mdcnfg & MDCNFG_DE3) == 0))
464 dwid = mdcnfg >> MDCNFD_DWID23_SHIFT;
465 dcac = mdcnfg >> MDCNFD_DCAC23_SHIFT;
466 drac = mdcnfg >> MDCNFD_DRAC23_SHIFT;
467 dnb = mdcnfg >> MDCNFD_DNB23_SHIFT;
470 panic("pxa_probe_sdram: impossible");
473 dwid = 2 << (1 - (dwid & MDCNFD_DWID_MASK)); /* 16/32 width */
474 dcac = 1 << ((dcac & MDCNFD_DCAC_MASK) + 8); /* 8-11 columns */
475 drac = 1 << ((drac & MDCNFD_DRAC_MASK) + 11); /* 11-13 rows */
476 dnb = 2 << (dnb & MDCNFD_DNB_MASK); /* # of banks */
478 memsize[i] = dwid * dcac * drac * dnb;
479 memstart[i] = PXA2X0_SDRAM0_START +
480 (i * PXA2X0_SDRAM_BANK_SIZE);
484 #define TIMER_FREQUENCY 3686400
485 #define UNIMPLEMENTED panic("%s: unimplemented", __func__)
487 /* XXXBJR: Belongs with DELAY in a timer.c of some sort. */
489 cpu_startprofclock(void)
495 cpu_stopprofclock(void)
500 static struct arm32_dma_range pxa_range = {
506 struct arm32_dma_range *
507 bus_dma_get_range(void)
514 bus_dma_get_range_nb(void)