]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/arm/xscale/pxa/pxa_machdep.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / arm / xscale / pxa / pxa_machdep.c
1 /*      $NetBSD: hpc_machdep.c,v 1.70 2003/09/16 08:18:22 agc Exp $     */
2
3 /*-
4  * Copyright (c) 1994-1998 Mark Brinicombe.
5  * Copyright (c) 1994 Brini.
6  * All rights reserved.
7  *
8  * This code is derived from software written for Brini by Mark Brinicombe
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
24  *
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
35  * SUCH DAMAGE.
36  *
37  * RiscBSD kernel project
38  *
39  * machdep.c
40  *
41  * Machine dependant functions for kernel setup
42  *
43  * This file needs a lot of work. 
44  *
45  * Created      : 17/09/94
46  */
47
48 #include "opt_msgbuf.h"
49 #include "opt_ddb.h"
50
51 #include <sys/cdefs.h>
52 __FBSDID("$FreeBSD$");
53
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>
61 #include <sys/ktr.h>
62 #include <sys/linker.h>
63 #include <sys/lock.h>
64 #include <sys/malloc.h>
65 #include <sys/mutex.h>
66 #include <sys/pcpu.h>
67 #include <sys/proc.h>
68 #include <sys/ptrace.h>
69 #include <sys/cons.h>
70 #include <sys/bio.h>
71 #include <sys/bus.h>
72 #include <sys/buf.h>
73 #include <sys/exec.h>
74 #include <sys/kdb.h>
75 #include <sys/msgbuf.h>
76 #include <machine/reg.h>
77 #include <machine/cpu.h>
78
79 #include <vm/vm.h>
80 #include <vm/pmap.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>
95
96 #include <arm/xscale/pxa/pxareg.h>
97 #include <arm/xscale/pxa/pxavar.h>
98
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
104
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)
107
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
112
113 extern u_int data_abort_handler_address;
114 extern u_int prefetch_abort_handler_address;
115 extern u_int undefined_handler_address;
116
117 struct pv_addr kernel_pt_table[NUM_KERNEL_PTS];
118
119 extern void *_end;
120
121 extern int *end;
122
123 struct pcpu __pcpu;
124 struct pcpu *pcpup = &__pcpu;
125
126 /* Physical and virtual addresses for some global pages */
127
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;
131
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;
139
140 static struct trapframe proc0_tf;
141
142 static void     pxa_probe_sdram(bus_space_tag_t, bus_space_handle_t,
143                     uint32_t *, uint32_t *);
144
145 /* Static device mappings. */
146 static const struct pmap_devmap pxa_devmap[] = {
147         /* 
148          * Map the on-board devices up into the KVA region so we don't muck
149          * up user-space.
150          */
151         {
152                 PXA2X0_PERIPH_START + PXA2X0_PERIPH_OFFSET,
153                 PXA2X0_PERIPH_START,
154                 PXA250_PERIPH_END - PXA2X0_PERIPH_START,
155                 VM_PROT_READ|VM_PROT_WRITE,
156                 PTE_NOCACHE,
157         },
158         { 0, 0, 0, 0, 0, }
159 };
160
161 #define SDRAM_START 0xa0000000
162
163 extern vm_offset_t xscale_cache_clean_addr;
164
165 void *
166 initarm(void *arg, void *arg2)
167 {
168         struct pv_addr  kernel_l1pt;
169         struct pv_addr  dpcpu;
170         int loop;
171         u_int l1pagetable;
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         int i, j;
178         uint32_t memsize[PXA2X0_SDRAM_BANKS], memstart[PXA2X0_SDRAM_BANKS];
179
180         set_cpufuncs();
181
182         lastaddr = fake_preload_metadata();
183         pcpu_init(pcpup, 0, sizeof(struct pcpu));
184         PCPU_SET(curthread, &thread0);
185
186         freemempos = 0xa0200000;
187         /* Define a macro to simplify memory allocation */
188 #define valloc_pages(var, np)                   \
189         alloc_pages((var).pv_pa, (np));         \
190         (var).pv_va = (var).pv_pa + 0x20000000;
191
192 #define alloc_pages(var, np)                    \
193         freemempos -= (np * PAGE_SIZE);         \
194         (var) = freemempos;             \
195         memset((char *)(var), 0, ((np) * PAGE_SIZE));
196
197         while (((freemempos - L1_TABLE_SIZE) & (L1_TABLE_SIZE - 1)) != 0)
198                 freemempos -= PAGE_SIZE;
199         valloc_pages(kernel_l1pt, L1_TABLE_SIZE / PAGE_SIZE);
200         for (loop = 0; loop < NUM_KERNEL_PTS; ++loop) {
201                 if (!(loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL))) {
202                         valloc_pages(kernel_pt_table[loop],
203                             L2_TABLE_SIZE / PAGE_SIZE);
204                 } else {
205                         kernel_pt_table[loop].pv_pa = freemempos +
206                             (loop % (PAGE_SIZE / L2_TABLE_SIZE_REAL)) *
207                             L2_TABLE_SIZE_REAL;
208                         kernel_pt_table[loop].pv_va = 
209                             kernel_pt_table[loop].pv_pa + 0x20000000;
210                 }
211                 i++;
212         }
213         freemem_pt = freemempos;
214         freemempos = 0xa0100000;
215         /*
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.
219          */
220         valloc_pages(systempage, 1);
221
222         /* Allocate dynamic per-cpu area. */
223         valloc_pages(dpcpu, DPCPU_SIZE / PAGE_SIZE);
224         dpcpu_init((void *)dpcpu.pv_va, 0);
225
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(MSGBUF_SIZE) / 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);
244         freemempos = trunc_page(freemem_after);
245         freemempos -= PAGE_SIZE;
246 #endif
247         /*
248          * Allocate memory for the l1 and l2 page tables. The scheme to avoid
249          * wasting memory by allocating the l1pt on the first 16k memory was
250          * taken from NetBSD rpc_machdep.c. NKPT should be greater than 12 for
251          * this to work (which is supposed to be the case).
252          */
253
254         /*
255          * Now we start construction of the L1 page table
256          * We start by mapping the L2 page tables into the L1.
257          * This means that we can replace L1 mappings later on if necessary
258          */
259         l1pagetable = kernel_l1pt.pv_va;
260
261         /* Map the L2 pages tables in the L1 page table */
262         pmap_link_l2pt(l1pagetable, ARM_VECTORS_HIGH & ~(0x00100000 - 1),
263             &kernel_pt_table[KERNEL_PT_SYS]);
264 #if 0 /* XXXBJR: What is this?  Don't know if there's an analogue. */
265         pmap_link_l2pt(l1pagetable, IQ80321_IOPXS_VBASE,
266                         &kernel_pt_table[KERNEL_PT_IOPXS]);
267 #endif
268         pmap_link_l2pt(l1pagetable, KERNBASE,
269             &kernel_pt_table[KERNEL_PT_BEFOREKERN]);
270         pmap_map_chunk(l1pagetable, KERNBASE, SDRAM_START, 0x100000,
271             VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
272         pmap_map_chunk(l1pagetable, KERNBASE + 0x100000, SDRAM_START + 0x100000,
273             0x100000, VM_PROT_READ|VM_PROT_WRITE, PTE_PAGETABLE);
274         pmap_map_chunk(l1pagetable, KERNBASE + 0x200000, SDRAM_START + 0x200000,
275            (((uint32_t)(lastaddr) - KERNBASE - 0x200000) + L1_S_SIZE) & ~(L1_S_SIZE - 1),
276             VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
277         freemem_after = ((int)lastaddr + PAGE_SIZE) & ~(PAGE_SIZE - 1);
278         afterkern = round_page(((vm_offset_t)lastaddr + L1_S_SIZE) &
279             ~(L1_S_SIZE - 1));
280         for (i = 0; i < KERNEL_PT_AFKERNEL_NUM; i++) {
281                 pmap_link_l2pt(l1pagetable, afterkern + i * 0x00100000,
282                     &kernel_pt_table[KERNEL_PT_AFKERNEL + i]);
283         }
284         pmap_map_entry(l1pagetable, afterkern, minidataclean.pv_pa, 
285             VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
286
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);
292         }
293 #endif
294
295         /* Map the Mini-Data cache clean area. */
296         xscale_setup_minidata(l1pagetable, afterkern,
297             minidataclean.pv_pa);
298
299         /* Map the vector page. */
300         pmap_map_entry(l1pagetable, ARM_VECTORS_HIGH, systempage.pv_pa,
301             VM_PROT_READ|VM_PROT_WRITE, PTE_CACHE);
302         pmap_devmap_bootstrap(l1pagetable, pxa_devmap);
303
304         /*
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).
308          */
309         xscale_cache_clean_addr = 0xff000000U;
310
311         cpu_domains((DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2)) | DOMAIN_CLIENT);
312         setttb(kernel_l1pt.pv_pa);
313         cpu_tlb_flushID();
314         cpu_domains(DOMAIN_CLIENT << (PMAP_DOMAIN_KERNEL*2));
315
316         /*
317          * Pages were allocated during the secondary bootstrap for the
318          * stacks for different CPU modes.
319          * We must now set the r13 registers in the different CPU modes to
320          * point to these stacks.
321          * Since the ARM stacks use STMFD etc. we must set r13 to the top end
322          * of the stack memory.
323          */
324         set_stackptr(PSR_IRQ32_MODE,
325             irqstack.pv_va + IRQ_STACK_SIZE * PAGE_SIZE);
326         set_stackptr(PSR_ABT32_MODE,
327             abtstack.pv_va + ABT_STACK_SIZE * PAGE_SIZE);
328         set_stackptr(PSR_UND32_MODE,
329             undstack.pv_va + UND_STACK_SIZE * PAGE_SIZE);
330
331         /*
332          * We must now clean the cache again....
333          * Cleaning may be done by reading new data to displace any
334          * dirty data in the cache. This will have happened in setttb()
335          * but since we are boot strapping the addresses used for the read
336          * may have just been remapped and thus the cache could be out
337          * of sync. A re-clean after the switch will cure this.
338          * After booting there are no gross relocations of the kernel thus
339          * this problem will not occur after initarm().
340          */
341         cpu_idcache_wbinv_all();
342
343         /*
344          * Sort out bus_space for on-board devices.
345          */
346         pxa_obio_tag_init();
347
348         /*
349          * Fetch the SDRAM start/size from the PXA2X0 SDRAM configration
350          * registers.
351          */
352         pxa_probe_sdram(obio_tag, PXA2X0_MEMCTL_BASE, memstart, memsize);
353
354         physmem = 0;
355         for (i = 0; i < PXA2X0_SDRAM_BANKS; i++) {
356                 physmem += memsize[i] / PAGE_SIZE;
357         }
358
359         /* Fire up consoles. */
360         cninit();
361
362         /* Set stack for exception handlers */
363         data_abort_handler_address = (u_int)data_abort_handler;
364         prefetch_abort_handler_address = (u_int)prefetch_abort_handler;
365         undefined_handler_address = (u_int)undefinedinstruction_bounce;
366         undefined_init();
367
368         proc_linkup(&proc0, &thread0);
369         thread0.td_kstack = kernelstack.pv_va;
370         thread0.td_pcb = (struct pcb *)
371                 (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
372         thread0.td_pcb->pcb_flags = 0;
373         thread0.td_frame = &proc0_tf;
374         pcpup->pc_curpcb = thread0.td_pcb;
375
376         /* Enable MMU, I-cache, D-cache, write buffer. */
377         arm_vector_init(ARM_VECTORS_HIGH, ARM_VEC_ALL);
378
379         pmap_curmaxkvaddr = afterkern + PAGE_SIZE;
380         /*
381          * ARM USE_SMALL_ALLOC uses dump_avail, so it must be filled before
382          * calling pmap_bootstrap.
383          */
384         i = 0;
385         for (j = 0; j < PXA2X0_SDRAM_BANKS; j++) {
386                 if (memsize[j] > 0) {
387                         dump_avail[i++] = round_page(memstart[j]);
388                         dump_avail[i++] =
389                             trunc_page(memstart[j] + memsize[j]);
390                 }
391         }
392         dump_avail[i] = 0;
393         dump_avail[i] = 0;
394         pmap_bootstrap(pmap_curmaxkvaddr, 0xd0000000, &kernel_l1pt);
395         msgbufp = (void*)msgbufpv.pv_va;
396         msgbufinit(msgbufp, MSGBUF_SIZE);
397         mutex_init();
398
399         i = 0;
400 #ifdef ARM_USE_SMALL_ALLOC
401         phys_avail[i++] = 0xa0000000;
402         phys_avail[i++] = 0xa0001000;   /*
403                                          *XXX: Gross hack to get our
404                                          * pages in the vm_page_array
405                                          . */
406 #endif
407         for (j = 0; j < PXA2X0_SDRAM_BANKS; j++) {
408                 if (memsize[j] > 0) {
409                         phys_avail[i] = round_page(memstart[j]);
410                         dump_avail[i++] = round_page(memstart[j]);
411                         phys_avail[i] =
412                             trunc_page(memstart[j] + memsize[j]);
413                         dump_avail[i++] =
414                             trunc_page(memstart[j] + memsize[j]);
415                 }
416         }
417
418         dump_avail[i] = 0;
419         phys_avail[i++] = 0;
420         dump_avail[i] = 0;
421         phys_avail[i] = 0;
422 #ifdef ARM_USE_SMALL_ALLOC
423         phys_avail[2] = round_page(virtual_avail - KERNBASE + phys_avail[2]);
424 #else
425         phys_avail[0] = round_page(virtual_avail - KERNBASE + phys_avail[0]);
426 #endif
427
428         /* Do basic tuning, hz etc */
429         init_param1();
430         init_param2(physmem);
431         kdb_init();
432         return ((void *)(kernelstack.pv_va + USPACE_SVC_STACK_TOP -
433             sizeof(struct pcb)));
434 }
435
436 static void
437 pxa_probe_sdram(bus_space_tag_t bst, bus_space_handle_t bsh,
438     uint32_t *memstart, uint32_t *memsize)
439 {
440         uint32_t        mdcnfg, dwid, dcac, drac, dnb;
441         int             i;
442
443         mdcnfg = bus_space_read_4(bst, bsh, MEMCTL_MDCNFG);
444
445         /*
446          * Scan all 4 SDRAM banks
447          */
448         for (i = 0; i < PXA2X0_SDRAM_BANKS; i++) {
449                 memstart[i] = 0;
450                 memsize[i] = 0;
451
452                 switch (i) {
453                 case 0:
454                 case 1:
455                         if ((i == 0 && (mdcnfg & MDCNFG_DE0) == 0) ||
456                             (i == 1 && (mdcnfg & MDCNFG_DE1) == 0))
457                                 continue;
458                         dwid = mdcnfg >> MDCNFD_DWID01_SHIFT;
459                         dcac = mdcnfg >> MDCNFD_DCAC01_SHIFT;
460                         drac = mdcnfg >> MDCNFD_DRAC01_SHIFT;
461                         dnb = mdcnfg >> MDCNFD_DNB01_SHIFT;
462                         break;
463
464                 case 2:
465                 case 3:
466                         if ((i == 2 && (mdcnfg & MDCNFG_DE2) == 0) ||
467                             (i == 3 && (mdcnfg & MDCNFG_DE3) == 0))
468                                 continue;
469                         dwid = mdcnfg >> MDCNFD_DWID23_SHIFT;
470                         dcac = mdcnfg >> MDCNFD_DCAC23_SHIFT;
471                         drac = mdcnfg >> MDCNFD_DRAC23_SHIFT;
472                         dnb = mdcnfg >> MDCNFD_DNB23_SHIFT;
473                         break;
474                 default:
475                         panic("pxa_probe_sdram: impossible");
476                 }
477
478                 dwid = 2 << (1 - (dwid & MDCNFD_DWID_MASK));  /* 16/32 width */
479                 dcac = 1 << ((dcac & MDCNFD_DCAC_MASK) + 8);  /* 8-11 columns */
480                 drac = 1 << ((drac & MDCNFD_DRAC_MASK) + 11); /* 11-13 rows */
481                 dnb = 2 << (dnb & MDCNFD_DNB_MASK);           /* # of banks */
482
483                 memsize[i] = dwid * dcac * drac * dnb;
484                 memstart[i] = PXA2X0_SDRAM0_START +
485                     (i * PXA2X0_SDRAM_BANK_SIZE);
486         }
487 }
488
489 #define TIMER_FREQUENCY 3686400
490 #define UNIMPLEMENTED   panic("%s: unimplemented", __func__)
491
492 /* XXXBJR: Belongs with DELAY in a timer.c of some sort. */
493 void
494 cpu_startprofclock(void)
495 {
496         UNIMPLEMENTED;
497 }
498
499 void
500 cpu_stopprofclock(void)
501 {
502         UNIMPLEMENTED;
503 }
504
505 static struct arm32_dma_range pxa_range = {
506         .dr_sysbase = 0,
507         .dr_busbase = 0,
508         .dr_len = ~0u,
509 };
510
511 struct arm32_dma_range *
512 bus_dma_get_range(void)
513 {
514
515         return (&pxa_range);
516 }
517
518 int
519 bus_dma_get_range_nb(void)
520 {
521
522         return (1);
523 }