9f40b3e7710064da3a317240ffa162a7fa484f40
[safe/jmp/linux-2.6] / arch / powerpc / kernel / head_fsl_booke.S
1 /*
2  * Kernel execution entry point code.
3  *
4  *    Copyright (c) 1995-1996 Gary Thomas <gdt@linuxppc.org>
5  *      Initial PowerPC version.
6  *    Copyright (c) 1996 Cort Dougan <cort@cs.nmt.edu>
7  *      Rewritten for PReP
8  *    Copyright (c) 1996 Paul Mackerras <paulus@cs.anu.edu.au>
9  *      Low-level exception handers, MMU support, and rewrite.
10  *    Copyright (c) 1997 Dan Malek <dmalek@jlc.net>
11  *      PowerPC 8xx modifications.
12  *    Copyright (c) 1998-1999 TiVo, Inc.
13  *      PowerPC 403GCX modifications.
14  *    Copyright (c) 1999 Grant Erickson <grant@lcse.umn.edu>
15  *      PowerPC 403GCX/405GP modifications.
16  *    Copyright 2000 MontaVista Software Inc.
17  *      PPC405 modifications
18  *      PowerPC 403GCX/405GP modifications.
19  *      Author: MontaVista Software, Inc.
20  *              frank_rowand@mvista.com or source@mvista.com
21  *              debbie_chu@mvista.com
22  *    Copyright 2002-2004 MontaVista Software, Inc.
23  *      PowerPC 44x support, Matt Porter <mporter@kernel.crashing.org>
24  *    Copyright 2004 Freescale Semiconductor, Inc
25  *      PowerPC e500 modifications, Kumar Gala <galak@kernel.crashing.org>
26  *
27  * This program is free software; you can redistribute  it and/or modify it
28  * under  the terms of  the GNU General  Public License as published by the
29  * Free Software Foundation;  either version 2 of the  License, or (at your
30  * option) any later version.
31  */
32
33 #include <linux/threads.h>
34 #include <asm/processor.h>
35 #include <asm/page.h>
36 #include <asm/mmu.h>
37 #include <asm/pgtable.h>
38 #include <asm/cputable.h>
39 #include <asm/thread_info.h>
40 #include <asm/ppc_asm.h>
41 #include <asm/asm-offsets.h>
42 #include "head_booke.h"
43
44 /* As with the other PowerPC ports, it is expected that when code
45  * execution begins here, the following registers contain valid, yet
46  * optional, information:
47  *
48  *   r3 - Board info structure pointer (DRAM, frequency, MAC address, etc.)
49  *   r4 - Starting address of the init RAM disk
50  *   r5 - Ending address of the init RAM disk
51  *   r6 - Start of kernel command line string (e.g. "mem=128")
52  *   r7 - End of kernel command line string
53  *
54  */
55         .section        .text.head, "ax"
56 _ENTRY(_stext);
57 _ENTRY(_start);
58         /*
59          * Reserve a word at a fixed location to store the address
60          * of abatron_pteptrs
61          */
62         nop
63 /*
64  * Save parameters we are passed
65  */
66         mr      r31,r3
67         mr      r30,r4
68         mr      r29,r5
69         mr      r28,r6
70         mr      r27,r7
71         li      r25,0           /* phys kernel start (low) */
72         li      r24,0           /* CPU number */
73         li      r23,0           /* phys kernel start (high) */
74
75 /* We try to not make any assumptions about how the boot loader
76  * setup or used the TLBs.  We invalidate all mappings from the
77  * boot loader and load a single entry in TLB1[0] to map the
78  * first 64M of kernel memory.  Any boot info passed from the
79  * bootloader needs to live in this first 64M.
80  *
81  * Requirement on bootloader:
82  *  - The page we're executing in needs to reside in TLB1 and
83  *    have IPROT=1.  If not an invalidate broadcast could
84  *    evict the entry we're currently executing in.
85  *
86  *  r3 = Index of TLB1 were executing in
87  *  r4 = Current MSR[IS]
88  *  r5 = Index of TLB1 temp mapping
89  *
90  * Later in mapin_ram we will correctly map lowmem, and resize TLB1[0]
91  * if needed
92  */
93
94 /* 1. Find the index of the entry we're executing in */
95         bl      invstr                          /* Find our address */
96 invstr: mflr    r6                              /* Make it accessible */
97         mfmsr   r7
98         rlwinm  r4,r7,27,31,31                  /* extract MSR[IS] */
99         mfspr   r7, SPRN_PID0
100         slwi    r7,r7,16
101         or      r7,r7,r4
102         mtspr   SPRN_MAS6,r7
103         tlbsx   0,r6                            /* search MSR[IS], SPID=PID0 */
104 #ifndef CONFIG_E200
105         mfspr   r7,SPRN_MAS1
106         andis.  r7,r7,MAS1_VALID@h
107         bne     match_TLB
108         mfspr   r7,SPRN_PID1
109         slwi    r7,r7,16
110         or      r7,r7,r4
111         mtspr   SPRN_MAS6,r7
112         tlbsx   0,r6                            /* search MSR[IS], SPID=PID1 */
113         mfspr   r7,SPRN_MAS1
114         andis.  r7,r7,MAS1_VALID@h
115         bne     match_TLB
116         mfspr   r7, SPRN_PID2
117         slwi    r7,r7,16
118         or      r7,r7,r4
119         mtspr   SPRN_MAS6,r7
120         tlbsx   0,r6                            /* Fall through, we had to match */
121 #endif
122 match_TLB:
123         mfspr   r7,SPRN_MAS0
124         rlwinm  r3,r7,16,20,31                  /* Extract MAS0(Entry) */
125
126         mfspr   r7,SPRN_MAS1                    /* Insure IPROT set */
127         oris    r7,r7,MAS1_IPROT@h
128         mtspr   SPRN_MAS1,r7
129         tlbwe
130
131 /* 2. Invalidate all entries except the entry we're executing in */
132         mfspr   r9,SPRN_TLB1CFG
133         andi.   r9,r9,0xfff
134         li      r6,0                            /* Set Entry counter to 0 */
135 1:      lis     r7,0x1000                       /* Set MAS0(TLBSEL) = 1 */
136         rlwimi  r7,r6,16,4,15                   /* Setup MAS0 = TLBSEL | ESEL(r6) */
137         mtspr   SPRN_MAS0,r7
138         tlbre
139         mfspr   r7,SPRN_MAS1
140         rlwinm  r7,r7,0,2,31                    /* Clear MAS1 Valid and IPROT */
141         cmpw    r3,r6
142         beq     skpinv                          /* Dont update the current execution TLB */
143         mtspr   SPRN_MAS1,r7
144         tlbwe
145         isync
146 skpinv: addi    r6,r6,1                         /* Increment */
147         cmpw    r6,r9                           /* Are we done? */
148         bne     1b                              /* If not, repeat */
149
150         /* Invalidate TLB0 */
151         li      r6,0x04
152         tlbivax 0,r6
153 #ifdef CONFIG_SMP
154         tlbsync
155 #endif
156         /* Invalidate TLB1 */
157         li      r6,0x0c
158         tlbivax 0,r6
159 #ifdef CONFIG_SMP
160         tlbsync
161 #endif
162         msync
163
164 /* 3. Setup a temp mapping and jump to it */
165         andi.   r5, r3, 0x1     /* Find an entry not used and is non-zero */
166         addi    r5, r5, 0x1
167         lis     r7,0x1000       /* Set MAS0(TLBSEL) = 1 */
168         rlwimi  r7,r3,16,4,15   /* Setup MAS0 = TLBSEL | ESEL(r3) */
169         mtspr   SPRN_MAS0,r7
170         tlbre
171
172         /* grab and fixup the RPN */
173         mfspr   r6,SPRN_MAS1    /* extract MAS1[SIZE] */
174         rlwinm  r6,r6,25,27,30
175         li      r8,-1
176         addi    r6,r6,10
177         slw     r6,r8,r6        /* convert to mask */
178
179         bl      1f              /* Find our address */
180 1:      mflr    r7
181
182         mfspr   r8,SPRN_MAS3
183 #ifdef CONFIG_PHYS_64BIT
184         mfspr   r23,SPRN_MAS7
185 #endif
186         and     r8,r6,r8
187         subfic  r9,r6,-4096
188         and     r9,r9,r7
189
190         or      r25,r8,r9
191         ori     r8,r25,(MAS3_SX|MAS3_SW|MAS3_SR)
192
193         /* Just modify the entry ID and EPN for the temp mapping */
194         lis     r7,0x1000       /* Set MAS0(TLBSEL) = 1 */
195         rlwimi  r7,r5,16,4,15   /* Setup MAS0 = TLBSEL | ESEL(r5) */
196         mtspr   SPRN_MAS0,r7
197         xori    r6,r4,1         /* Setup TMP mapping in the other Address space */
198         slwi    r6,r6,12
199         oris    r6,r6,(MAS1_VALID|MAS1_IPROT)@h
200         ori     r6,r6,(MAS1_TSIZE(BOOKE_PAGESZ_4K))@l
201         mtspr   SPRN_MAS1,r6
202         mfspr   r6,SPRN_MAS2
203         li      r7,0            /* temp EPN = 0 */
204         rlwimi  r7,r6,0,20,31
205         mtspr   SPRN_MAS2,r7
206         mtspr   SPRN_MAS3,r8
207         tlbwe
208
209         xori    r6,r4,1
210         slwi    r6,r6,5         /* setup new context with other address space */
211         bl      1f              /* Find our address */
212 1:      mflr    r9
213         rlwimi  r7,r9,0,20,31
214         addi    r7,r7,24
215         mtspr   SPRN_SRR0,r7
216         mtspr   SPRN_SRR1,r6
217         rfi
218
219 /* 4. Clear out PIDs & Search info */
220         li      r6,0
221         mtspr   SPRN_PID0,r6
222 #ifndef CONFIG_E200
223         mtspr   SPRN_PID1,r6
224         mtspr   SPRN_PID2,r6
225 #endif
226         mtspr   SPRN_MAS6,r6
227
228 /* 5. Invalidate mapping we started in */
229         lis     r7,0x1000       /* Set MAS0(TLBSEL) = 1 */
230         rlwimi  r7,r3,16,4,15   /* Setup MAS0 = TLBSEL | ESEL(r3) */
231         mtspr   SPRN_MAS0,r7
232         tlbre
233         mfspr   r6,SPRN_MAS1
234         rlwinm  r6,r6,0,2,0     /* clear IPROT */
235         mtspr   SPRN_MAS1,r6
236         tlbwe
237         /* Invalidate TLB1 */
238         li      r9,0x0c
239         tlbivax 0,r9
240 #ifdef CONFIG_SMP
241         tlbsync
242 #endif
243         msync
244
245 /* 6. Setup KERNELBASE mapping in TLB1[0] */
246         lis     r6,0x1000               /* Set MAS0(TLBSEL) = TLB1(1), ESEL = 0 */
247         mtspr   SPRN_MAS0,r6
248         lis     r6,(MAS1_VALID|MAS1_IPROT)@h
249         ori     r6,r6,(MAS1_TSIZE(BOOKE_PAGESZ_64M))@l
250         mtspr   SPRN_MAS1,r6
251         li      r7,0
252         lis     r6,PAGE_OFFSET@h
253         ori     r6,r6,PAGE_OFFSET@l
254         rlwimi  r6,r7,0,20,31
255         mtspr   SPRN_MAS2,r6
256         mtspr   SPRN_MAS3,r8
257         tlbwe
258
259 /* 7. Jump to KERNELBASE mapping */
260         lis     r6,KERNELBASE@h
261         ori     r6,r6,KERNELBASE@l
262         rlwimi  r6,r7,0,20,31
263         lis     r7,MSR_KERNEL@h
264         ori     r7,r7,MSR_KERNEL@l
265         bl      1f                      /* Find our address */
266 1:      mflr    r9
267         rlwimi  r6,r9,0,20,31
268         addi    r6,r6,24
269         mtspr   SPRN_SRR0,r6
270         mtspr   SPRN_SRR1,r7
271         rfi                             /* start execution out of TLB1[0] entry */
272
273 /* 8. Clear out the temp mapping */
274         lis     r7,0x1000       /* Set MAS0(TLBSEL) = 1 */
275         rlwimi  r7,r5,16,4,15   /* Setup MAS0 = TLBSEL | ESEL(r5) */
276         mtspr   SPRN_MAS0,r7
277         tlbre
278         mfspr   r8,SPRN_MAS1
279         rlwinm  r8,r8,0,2,0     /* clear IPROT */
280         mtspr   SPRN_MAS1,r8
281         tlbwe
282         /* Invalidate TLB1 */
283         li      r9,0x0c
284         tlbivax 0,r9
285 #ifdef CONFIG_SMP
286         tlbsync
287 #endif
288         msync
289
290         /* Establish the interrupt vector offsets */
291         SET_IVOR(0,  CriticalInput);
292         SET_IVOR(1,  MachineCheck);
293         SET_IVOR(2,  DataStorage);
294         SET_IVOR(3,  InstructionStorage);
295         SET_IVOR(4,  ExternalInput);
296         SET_IVOR(5,  Alignment);
297         SET_IVOR(6,  Program);
298         SET_IVOR(7,  FloatingPointUnavailable);
299         SET_IVOR(8,  SystemCall);
300         SET_IVOR(9,  AuxillaryProcessorUnavailable);
301         SET_IVOR(10, Decrementer);
302         SET_IVOR(11, FixedIntervalTimer);
303         SET_IVOR(12, WatchdogTimer);
304         SET_IVOR(13, DataTLBError);
305         SET_IVOR(14, InstructionTLBError);
306         SET_IVOR(15, Debug);
307         SET_IVOR(32, SPEUnavailable);
308         SET_IVOR(33, SPEFloatingPointData);
309         SET_IVOR(34, SPEFloatingPointRound);
310 #ifndef CONFIG_E200
311         SET_IVOR(35, PerformanceMonitor);
312 #endif
313
314         /* Establish the interrupt vector base */
315         lis     r4,interrupt_base@h     /* IVPR only uses the high 16-bits */
316         mtspr   SPRN_IVPR,r4
317
318         /* Setup the defaults for TLB entries */
319         li      r2,(MAS4_TSIZED(BOOKE_PAGESZ_4K))@l
320 #ifdef CONFIG_E200
321         oris    r2,r2,MAS4_TLBSELD(1)@h
322 #endif
323         mtspr   SPRN_MAS4, r2
324
325 #if 0
326         /* Enable DOZE */
327         mfspr   r2,SPRN_HID0
328         oris    r2,r2,HID0_DOZE@h
329         mtspr   SPRN_HID0, r2
330 #endif
331 #ifdef CONFIG_E200
332         /* enable dedicated debug exception handling resources (Debug APU) */
333         mfspr   r2,SPRN_HID0
334         ori     r2,r2,HID0_DAPUEN@l
335         mtspr   SPRN_HID0,r2
336 #endif
337
338 #if !defined(CONFIG_BDI_SWITCH)
339         /*
340          * The Abatron BDI JTAG debugger does not tolerate others
341          * mucking with the debug registers.
342          */
343         lis     r2,DBCR0_IDM@h
344         mtspr   SPRN_DBCR0,r2
345         isync
346         /* clear any residual debug events */
347         li      r2,-1
348         mtspr   SPRN_DBSR,r2
349 #endif
350
351         /*
352          * This is where the main kernel code starts.
353          */
354
355         /* ptr to current */
356         lis     r2,init_task@h
357         ori     r2,r2,init_task@l
358
359         /* ptr to current thread */
360         addi    r4,r2,THREAD    /* init task's THREAD */
361         mtspr   SPRN_SPRG3,r4
362
363         /* stack */
364         lis     r1,init_thread_union@h
365         ori     r1,r1,init_thread_union@l
366         li      r0,0
367         stwu    r0,THREAD_SIZE-STACK_FRAME_OVERHEAD(r1)
368
369         bl      early_init
370
371         mfspr   r3,SPRN_TLB1CFG
372         andi.   r3,r3,0xfff
373         lis     r4,num_tlbcam_entries@ha
374         stw     r3,num_tlbcam_entries@l(r4)
375 /*
376  * Decide what sort of machine this is and initialize the MMU.
377  */
378         mr      r3,r31
379         mr      r4,r30
380         mr      r5,r29
381         mr      r6,r28
382         mr      r7,r27
383         bl      machine_init
384         bl      MMU_init
385
386         /* Setup PTE pointers for the Abatron bdiGDB */
387         lis     r6, swapper_pg_dir@h
388         ori     r6, r6, swapper_pg_dir@l
389         lis     r5, abatron_pteptrs@h
390         ori     r5, r5, abatron_pteptrs@l
391         lis     r4, KERNELBASE@h
392         ori     r4, r4, KERNELBASE@l
393         stw     r5, 0(r4)       /* Save abatron_pteptrs at a fixed location */
394         stw     r6, 0(r5)
395
396         /* Let's move on */
397         lis     r4,start_kernel@h
398         ori     r4,r4,start_kernel@l
399         lis     r3,MSR_KERNEL@h
400         ori     r3,r3,MSR_KERNEL@l
401         mtspr   SPRN_SRR0,r4
402         mtspr   SPRN_SRR1,r3
403         rfi                     /* change context and jump to start_kernel */
404
405 /* Macros to hide the PTE size differences
406  *
407  * FIND_PTE -- walks the page tables given EA & pgdir pointer
408  *   r10 -- EA of fault
409  *   r11 -- PGDIR pointer
410  *   r12 -- free
411  *   label 2: is the bailout case
412  *
413  * if we find the pte (fall through):
414  *   r11 is low pte word
415  *   r12 is pointer to the pte
416  */
417 #ifdef CONFIG_PTE_64BIT
418 #define PTE_FLAGS_OFFSET        4
419 #define FIND_PTE        \
420         rlwinm  r12, r10, 13, 19, 29;   /* Compute pgdir/pmd offset */  \
421         lwzx    r11, r12, r11;          /* Get pgd/pmd entry */         \
422         rlwinm. r12, r11, 0, 0, 20;     /* Extract pt base address */   \
423         beq     2f;                     /* Bail if no table */          \
424         rlwimi  r12, r10, 23, 20, 28;   /* Compute pte address */       \
425         lwz     r11, 4(r12);            /* Get pte entry */
426 #else
427 #define PTE_FLAGS_OFFSET        0
428 #define FIND_PTE        \
429         rlwimi  r11, r10, 12, 20, 29;   /* Create L1 (pgdir/pmd) address */     \
430         lwz     r11, 0(r11);            /* Get L1 entry */                      \
431         rlwinm. r12, r11, 0, 0, 19;     /* Extract L2 (pte) base address */     \
432         beq     2f;                     /* Bail if no table */                  \
433         rlwimi  r12, r10, 22, 20, 29;   /* Compute PTE address */               \
434         lwz     r11, 0(r12);            /* Get Linux PTE */
435 #endif
436
437 /*
438  * Interrupt vector entry code
439  *
440  * The Book E MMUs are always on so we don't need to handle
441  * interrupts in real mode as with previous PPC processors. In
442  * this case we handle interrupts in the kernel virtual address
443  * space.
444  *
445  * Interrupt vectors are dynamically placed relative to the
446  * interrupt prefix as determined by the address of interrupt_base.
447  * The interrupt vectors offsets are programmed using the labels
448  * for each interrupt vector entry.
449  *
450  * Interrupt vectors must be aligned on a 16 byte boundary.
451  * We align on a 32 byte cache line boundary for good measure.
452  */
453
454 interrupt_base:
455         /* Critical Input Interrupt */
456         CRITICAL_EXCEPTION(0x0100, CriticalInput, unknown_exception)
457
458         /* Machine Check Interrupt */
459 #ifdef CONFIG_E200
460         /* no RFMCI, MCSRRs on E200 */
461         CRITICAL_EXCEPTION(0x0200, MachineCheck, machine_check_exception)
462 #else
463         MCHECK_EXCEPTION(0x0200, MachineCheck, machine_check_exception)
464 #endif
465
466         /* Data Storage Interrupt */
467         START_EXCEPTION(DataStorage)
468         mtspr   SPRN_SPRG0, r10         /* Save some working registers */
469         mtspr   SPRN_SPRG1, r11
470         mtspr   SPRN_SPRG4W, r12
471         mtspr   SPRN_SPRG5W, r13
472         mfcr    r11
473         mtspr   SPRN_SPRG7W, r11
474
475         /*
476          * Check if it was a store fault, if not then bail
477          * because a user tried to access a kernel or
478          * read-protected page.  Otherwise, get the
479          * offending address and handle it.
480          */
481         mfspr   r10, SPRN_ESR
482         andis.  r10, r10, ESR_ST@h
483         beq     2f
484
485         mfspr   r10, SPRN_DEAR          /* Get faulting address */
486
487         /* If we are faulting a kernel address, we have to use the
488          * kernel page tables.
489          */
490         lis     r11, PAGE_OFFSET@h
491         cmplw   0, r10, r11
492         bge     2f
493
494         /* Get the PGD for the current thread */
495 3:
496         mfspr   r11,SPRN_SPRG3
497         lwz     r11,PGDIR(r11)
498 4:
499         FIND_PTE
500
501         /* Are _PAGE_USER & _PAGE_RW set & _PAGE_HWWRITE not? */
502         andi.   r13, r11, _PAGE_RW|_PAGE_USER|_PAGE_HWWRITE
503         cmpwi   0, r13, _PAGE_RW|_PAGE_USER
504         bne     2f                      /* Bail if not */
505
506         /* Update 'changed'. */
507         ori     r11, r11, _PAGE_DIRTY|_PAGE_ACCESSED|_PAGE_HWWRITE
508         stw     r11, PTE_FLAGS_OFFSET(r12) /* Update Linux page table */
509
510         /* MAS2 not updated as the entry does exist in the tlb, this
511            fault taken to detect state transition (eg: COW -> DIRTY)
512          */
513         andi.   r11, r11, _PAGE_HWEXEC
514         rlwimi  r11, r11, 31, 27, 27    /* SX <- _PAGE_HWEXEC */
515         ori     r11, r11, (MAS3_UW|MAS3_SW|MAS3_UR|MAS3_SR)@l /* set static perms */
516
517         /* update search PID in MAS6, AS = 0 */
518         mfspr   r12, SPRN_PID0
519         slwi    r12, r12, 16
520         mtspr   SPRN_MAS6, r12
521
522         /* find the TLB index that caused the fault.  It has to be here. */
523         tlbsx   0, r10
524
525         /* only update the perm bits, assume the RPN is fine */
526         mfspr   r12, SPRN_MAS3
527         rlwimi  r12, r11, 0, 20, 31
528         mtspr   SPRN_MAS3,r12
529         tlbwe
530
531         /* Done...restore registers and get out of here.  */
532         mfspr   r11, SPRN_SPRG7R
533         mtcr    r11
534         mfspr   r13, SPRN_SPRG5R
535         mfspr   r12, SPRN_SPRG4R
536         mfspr   r11, SPRN_SPRG1
537         mfspr   r10, SPRN_SPRG0
538         rfi                     /* Force context change */
539
540 2:
541         /*
542          * The bailout.  Restore registers to pre-exception conditions
543          * and call the heavyweights to help us out.
544          */
545         mfspr   r11, SPRN_SPRG7R
546         mtcr    r11
547         mfspr   r13, SPRN_SPRG5R
548         mfspr   r12, SPRN_SPRG4R
549         mfspr   r11, SPRN_SPRG1
550         mfspr   r10, SPRN_SPRG0
551         b       data_access
552
553         /* Instruction Storage Interrupt */
554         INSTRUCTION_STORAGE_EXCEPTION
555
556         /* External Input Interrupt */
557         EXCEPTION(0x0500, ExternalInput, do_IRQ, EXC_XFER_LITE)
558
559         /* Alignment Interrupt */
560         ALIGNMENT_EXCEPTION
561
562         /* Program Interrupt */
563         PROGRAM_EXCEPTION
564
565         /* Floating Point Unavailable Interrupt */
566 #ifdef CONFIG_PPC_FPU
567         FP_UNAVAILABLE_EXCEPTION
568 #else
569 #ifdef CONFIG_E200
570         /* E200 treats 'normal' floating point instructions as FP Unavail exception */
571         EXCEPTION(0x0800, FloatingPointUnavailable, program_check_exception, EXC_XFER_EE)
572 #else
573         EXCEPTION(0x0800, FloatingPointUnavailable, unknown_exception, EXC_XFER_EE)
574 #endif
575 #endif
576
577         /* System Call Interrupt */
578         START_EXCEPTION(SystemCall)
579         NORMAL_EXCEPTION_PROLOG
580         EXC_XFER_EE_LITE(0x0c00, DoSyscall)
581
582         /* Auxillary Processor Unavailable Interrupt */
583         EXCEPTION(0x2900, AuxillaryProcessorUnavailable, unknown_exception, EXC_XFER_EE)
584
585         /* Decrementer Interrupt */
586         DECREMENTER_EXCEPTION
587
588         /* Fixed Internal Timer Interrupt */
589         /* TODO: Add FIT support */
590         EXCEPTION(0x3100, FixedIntervalTimer, unknown_exception, EXC_XFER_EE)
591
592         /* Watchdog Timer Interrupt */
593 #ifdef CONFIG_BOOKE_WDT
594         CRITICAL_EXCEPTION(0x3200, WatchdogTimer, WatchdogException)
595 #else
596         CRITICAL_EXCEPTION(0x3200, WatchdogTimer, unknown_exception)
597 #endif
598
599         /* Data TLB Error Interrupt */
600         START_EXCEPTION(DataTLBError)
601         mtspr   SPRN_SPRG0, r10         /* Save some working registers */
602         mtspr   SPRN_SPRG1, r11
603         mtspr   SPRN_SPRG4W, r12
604         mtspr   SPRN_SPRG5W, r13
605         mfcr    r11
606         mtspr   SPRN_SPRG7W, r11
607         mfspr   r10, SPRN_DEAR          /* Get faulting address */
608
609         /* If we are faulting a kernel address, we have to use the
610          * kernel page tables.
611          */
612         lis     r11, PAGE_OFFSET@h
613         cmplw   5, r10, r11
614         blt     5, 3f
615         lis     r11, swapper_pg_dir@h
616         ori     r11, r11, swapper_pg_dir@l
617
618         mfspr   r12,SPRN_MAS1           /* Set TID to 0 */
619         rlwinm  r12,r12,0,16,1
620         mtspr   SPRN_MAS1,r12
621
622         b       4f
623
624         /* Get the PGD for the current thread */
625 3:
626         mfspr   r11,SPRN_SPRG3
627         lwz     r11,PGDIR(r11)
628
629 4:
630         FIND_PTE
631         andi.   r13, r11, _PAGE_PRESENT /* Is the page present? */
632         beq     2f                      /* Bail if not present */
633
634 #ifdef CONFIG_PTE_64BIT
635         lwz     r13, 0(r12)
636 #endif
637         ori     r11, r11, _PAGE_ACCESSED
638         stw     r11, PTE_FLAGS_OFFSET(r12)
639
640          /* Jump to common tlb load */
641         b       finish_tlb_load
642 2:
643         /* The bailout.  Restore registers to pre-exception conditions
644          * and call the heavyweights to help us out.
645          */
646         mfspr   r11, SPRN_SPRG7R
647         mtcr    r11
648         mfspr   r13, SPRN_SPRG5R
649         mfspr   r12, SPRN_SPRG4R
650         mfspr   r11, SPRN_SPRG1
651         mfspr   r10, SPRN_SPRG0
652         b       data_access
653
654         /* Instruction TLB Error Interrupt */
655         /*
656          * Nearly the same as above, except we get our
657          * information from different registers and bailout
658          * to a different point.
659          */
660         START_EXCEPTION(InstructionTLBError)
661         mtspr   SPRN_SPRG0, r10         /* Save some working registers */
662         mtspr   SPRN_SPRG1, r11
663         mtspr   SPRN_SPRG4W, r12
664         mtspr   SPRN_SPRG5W, r13
665         mfcr    r11
666         mtspr   SPRN_SPRG7W, r11
667         mfspr   r10, SPRN_SRR0          /* Get faulting address */
668
669         /* If we are faulting a kernel address, we have to use the
670          * kernel page tables.
671          */
672         lis     r11, PAGE_OFFSET@h
673         cmplw   5, r10, r11
674         blt     5, 3f
675         lis     r11, swapper_pg_dir@h
676         ori     r11, r11, swapper_pg_dir@l
677
678         mfspr   r12,SPRN_MAS1           /* Set TID to 0 */
679         rlwinm  r12,r12,0,16,1
680         mtspr   SPRN_MAS1,r12
681
682         b       4f
683
684         /* Get the PGD for the current thread */
685 3:
686         mfspr   r11,SPRN_SPRG3
687         lwz     r11,PGDIR(r11)
688
689 4:
690         FIND_PTE
691         andi.   r13, r11, _PAGE_PRESENT /* Is the page present? */
692         beq     2f                      /* Bail if not present */
693
694 #ifdef CONFIG_PTE_64BIT
695         lwz     r13, 0(r12)
696 #endif
697         ori     r11, r11, _PAGE_ACCESSED
698         stw     r11, PTE_FLAGS_OFFSET(r12)
699
700         /* Jump to common TLB load point */
701         b       finish_tlb_load
702
703 2:
704         /* The bailout.  Restore registers to pre-exception conditions
705          * and call the heavyweights to help us out.
706          */
707         mfspr   r11, SPRN_SPRG7R
708         mtcr    r11
709         mfspr   r13, SPRN_SPRG5R
710         mfspr   r12, SPRN_SPRG4R
711         mfspr   r11, SPRN_SPRG1
712         mfspr   r10, SPRN_SPRG0
713         b       InstructionStorage
714
715 #ifdef CONFIG_SPE
716         /* SPE Unavailable */
717         START_EXCEPTION(SPEUnavailable)
718         NORMAL_EXCEPTION_PROLOG
719         bne     load_up_spe
720         addi    r3,r1,STACK_FRAME_OVERHEAD
721         EXC_XFER_EE_LITE(0x2010, KernelSPE)
722 #else
723         EXCEPTION(0x2020, SPEUnavailable, unknown_exception, EXC_XFER_EE)
724 #endif /* CONFIG_SPE */
725
726         /* SPE Floating Point Data */
727 #ifdef CONFIG_SPE
728         EXCEPTION(0x2030, SPEFloatingPointData, SPEFloatingPointException, EXC_XFER_EE);
729 #else
730         EXCEPTION(0x2040, SPEFloatingPointData, unknown_exception, EXC_XFER_EE)
731 #endif /* CONFIG_SPE */
732
733         /* SPE Floating Point Round */
734         EXCEPTION(0x2050, SPEFloatingPointRound, unknown_exception, EXC_XFER_EE)
735
736         /* Performance Monitor */
737         EXCEPTION(0x2060, PerformanceMonitor, performance_monitor_exception, EXC_XFER_STD)
738
739
740         /* Debug Interrupt */
741         DEBUG_EXCEPTION
742
743 /*
744  * Local functions
745  */
746
747         /*
748          * Data TLB exceptions will bail out to this point
749          * if they can't resolve the lightweight TLB fault.
750          */
751 data_access:
752         NORMAL_EXCEPTION_PROLOG
753         mfspr   r5,SPRN_ESR             /* Grab the ESR, save it, pass arg3 */
754         stw     r5,_ESR(r11)
755         mfspr   r4,SPRN_DEAR            /* Grab the DEAR, save it, pass arg2 */
756         andis.  r10,r5,(ESR_ILK|ESR_DLK)@h
757         bne     1f
758         EXC_XFER_EE_LITE(0x0300, handle_page_fault)
759 1:
760         addi    r3,r1,STACK_FRAME_OVERHEAD
761         EXC_XFER_EE_LITE(0x0300, CacheLockingException)
762
763 /*
764
765  * Both the instruction and data TLB miss get to this
766  * point to load the TLB.
767  *      r10 - EA of fault
768  *      r11 - TLB (info from Linux PTE)
769  *      r12, r13 - available to use
770  *      CR5 - results of addr >= PAGE_OFFSET
771  *      MAS0, MAS1 - loaded with proper value when we get here
772  *      MAS2, MAS3 - will need additional info from Linux PTE
773  *      Upon exit, we reload everything and RFI.
774  */
775 finish_tlb_load:
776         /*
777          * We set execute, because we don't have the granularity to
778          * properly set this at the page level (Linux problem).
779          * Many of these bits are software only.  Bits we don't set
780          * here we (properly should) assume have the appropriate value.
781          */
782
783         mfspr   r12, SPRN_MAS2
784 #ifdef CONFIG_PTE_64BIT
785         rlwimi  r12, r11, 26, 24, 31    /* extract ...WIMGE from pte */
786 #else
787         rlwimi  r12, r11, 26, 27, 31    /* extract WIMGE from pte */
788 #endif
789         mtspr   SPRN_MAS2, r12
790
791         bge     5, 1f
792
793         /* is user addr */
794         andi.   r12, r11, (_PAGE_USER | _PAGE_HWWRITE | _PAGE_HWEXEC)
795         andi.   r10, r11, _PAGE_USER    /* Test for _PAGE_USER */
796         srwi    r10, r12, 1
797         or      r12, r12, r10   /* Copy user perms into supervisor */
798         iseleq  r12, 0, r12
799         b       2f
800
801         /* is kernel addr */
802 1:      rlwinm  r12, r11, 31, 29, 29    /* Extract _PAGE_HWWRITE into SW */
803         ori     r12, r12, (MAS3_SX | MAS3_SR)
804
805 #ifdef CONFIG_PTE_64BIT
806 2:      rlwimi  r12, r13, 24, 0, 7      /* grab RPN[32:39] */
807         rlwimi  r12, r11, 24, 8, 19     /* grab RPN[40:51] */
808         mtspr   SPRN_MAS3, r12
809 BEGIN_FTR_SECTION
810         srwi    r10, r13, 8             /* grab RPN[8:31] */
811         mtspr   SPRN_MAS7, r10
812 END_FTR_SECTION_IFSET(CPU_FTR_BIG_PHYS)
813 #else
814 2:      rlwimi  r11, r12, 0, 20, 31     /* Extract RPN from PTE and merge with perms */
815         mtspr   SPRN_MAS3, r11
816 #endif
817 #ifdef CONFIG_E200
818         /* Round robin TLB1 entries assignment */
819         mfspr   r12, SPRN_MAS0
820
821         /* Extract TLB1CFG(NENTRY) */
822         mfspr   r11, SPRN_TLB1CFG
823         andi.   r11, r11, 0xfff
824
825         /* Extract MAS0(NV) */
826         andi.   r13, r12, 0xfff
827         addi    r13, r13, 1
828         cmpw    0, r13, r11
829         addi    r12, r12, 1
830
831         /* check if we need to wrap */
832         blt     7f
833
834         /* wrap back to first free tlbcam entry */
835         lis     r13, tlbcam_index@ha
836         lwz     r13, tlbcam_index@l(r13)
837         rlwimi  r12, r13, 0, 20, 31
838 7:
839         mtspr   SPRN_MAS0,r12
840 #endif /* CONFIG_E200 */
841
842         tlbwe
843
844         /* Done...restore registers and get out of here.  */
845         mfspr   r11, SPRN_SPRG7R
846         mtcr    r11
847         mfspr   r13, SPRN_SPRG5R
848         mfspr   r12, SPRN_SPRG4R
849         mfspr   r11, SPRN_SPRG1
850         mfspr   r10, SPRN_SPRG0
851         rfi                                     /* Force context change */
852
853 #ifdef CONFIG_SPE
854 /* Note that the SPE support is closely modeled after the AltiVec
855  * support.  Changes to one are likely to be applicable to the
856  * other!  */
857 load_up_spe:
858 /*
859  * Disable SPE for the task which had SPE previously,
860  * and save its SPE registers in its thread_struct.
861  * Enables SPE for use in the kernel on return.
862  * On SMP we know the SPE units are free, since we give it up every
863  * switch.  -- Kumar
864  */
865         mfmsr   r5
866         oris    r5,r5,MSR_SPE@h
867         mtmsr   r5                      /* enable use of SPE now */
868         isync
869 /*
870  * For SMP, we don't do lazy SPE switching because it just gets too
871  * horrendously complex, especially when a task switches from one CPU
872  * to another.  Instead we call giveup_spe in switch_to.
873  */
874 #ifndef CONFIG_SMP
875         lis     r3,last_task_used_spe@ha
876         lwz     r4,last_task_used_spe@l(r3)
877         cmpi    0,r4,0
878         beq     1f
879         addi    r4,r4,THREAD    /* want THREAD of last_task_used_spe */
880         SAVE_32EVRS(0,r10,r4)
881         evxor   evr10, evr10, evr10     /* clear out evr10 */
882         evmwumiaa evr10, evr10, evr10   /* evr10 <- ACC = 0 * 0 + ACC */
883         li      r5,THREAD_ACC
884         evstddx evr10, r4, r5           /* save off accumulator */
885         lwz     r5,PT_REGS(r4)
886         lwz     r4,_MSR-STACK_FRAME_OVERHEAD(r5)
887         lis     r10,MSR_SPE@h
888         andc    r4,r4,r10       /* disable SPE for previous task */
889         stw     r4,_MSR-STACK_FRAME_OVERHEAD(r5)
890 1:
891 #endif /* !CONFIG_SMP */
892         /* enable use of SPE after return */
893         oris    r9,r9,MSR_SPE@h
894         mfspr   r5,SPRN_SPRG3           /* current task's THREAD (phys) */
895         li      r4,1
896         li      r10,THREAD_ACC
897         stw     r4,THREAD_USED_SPE(r5)
898         evlddx  evr4,r10,r5
899         evmra   evr4,evr4
900         REST_32EVRS(0,r10,r5)
901 #ifndef CONFIG_SMP
902         subi    r4,r5,THREAD
903         stw     r4,last_task_used_spe@l(r3)
904 #endif /* !CONFIG_SMP */
905         /* restore registers and return */
906 2:      REST_4GPRS(3, r11)
907         lwz     r10,_CCR(r11)
908         REST_GPR(1, r11)
909         mtcr    r10
910         lwz     r10,_LINK(r11)
911         mtlr    r10
912         REST_GPR(10, r11)
913         mtspr   SPRN_SRR1,r9
914         mtspr   SPRN_SRR0,r12
915         REST_GPR(9, r11)
916         REST_GPR(12, r11)
917         lwz     r11,GPR11(r11)
918         rfi
919
920 /*
921  * SPE unavailable trap from kernel - print a message, but let
922  * the task use SPE in the kernel until it returns to user mode.
923  */
924 KernelSPE:
925         lwz     r3,_MSR(r1)
926         oris    r3,r3,MSR_SPE@h
927         stw     r3,_MSR(r1)     /* enable use of SPE after return */
928         lis     r3,87f@h
929         ori     r3,r3,87f@l
930         mr      r4,r2           /* current */
931         lwz     r5,_NIP(r1)
932         bl      printk
933         b       ret_from_except
934 87:     .string "SPE used in kernel  (task=%p, pc=%x)  \n"
935         .align  4,0
936
937 #endif /* CONFIG_SPE */
938
939 /*
940  * Global functions
941  */
942
943 /*
944  * extern void loadcam_entry(unsigned int index)
945  *
946  * Load TLBCAM[index] entry in to the L2 CAM MMU
947  */
948 _GLOBAL(loadcam_entry)
949         lis     r4,TLBCAM@ha
950         addi    r4,r4,TLBCAM@l
951         mulli   r5,r3,20
952         add     r3,r5,r4
953         lwz     r4,0(r3)
954         mtspr   SPRN_MAS0,r4
955         lwz     r4,4(r3)
956         mtspr   SPRN_MAS1,r4
957         lwz     r4,8(r3)
958         mtspr   SPRN_MAS2,r4
959         lwz     r4,12(r3)
960         mtspr   SPRN_MAS3,r4
961         tlbwe
962         isync
963         blr
964
965 /*
966  * extern void giveup_altivec(struct task_struct *prev)
967  *
968  * The e500 core does not have an AltiVec unit.
969  */
970 _GLOBAL(giveup_altivec)
971         blr
972
973 #ifdef CONFIG_SPE
974 /*
975  * extern void giveup_spe(struct task_struct *prev)
976  *
977  */
978 _GLOBAL(giveup_spe)
979         mfmsr   r5
980         oris    r5,r5,MSR_SPE@h
981         mtmsr   r5                      /* enable use of SPE now */
982         isync
983         cmpi    0,r3,0
984         beqlr-                          /* if no previous owner, done */
985         addi    r3,r3,THREAD            /* want THREAD of task */
986         lwz     r5,PT_REGS(r3)
987         cmpi    0,r5,0
988         SAVE_32EVRS(0, r4, r3)
989         evxor   evr6, evr6, evr6        /* clear out evr6 */
990         evmwumiaa evr6, evr6, evr6      /* evr6 <- ACC = 0 * 0 + ACC */
991         li      r4,THREAD_ACC
992         evstddx evr6, r4, r3            /* save off accumulator */
993         mfspr   r6,SPRN_SPEFSCR
994         stw     r6,THREAD_SPEFSCR(r3)   /* save spefscr register value */
995         beq     1f
996         lwz     r4,_MSR-STACK_FRAME_OVERHEAD(r5)
997         lis     r3,MSR_SPE@h
998         andc    r4,r4,r3                /* disable SPE for previous task */
999         stw     r4,_MSR-STACK_FRAME_OVERHEAD(r5)
1000 1:
1001 #ifndef CONFIG_SMP
1002         li      r5,0
1003         lis     r4,last_task_used_spe@ha
1004         stw     r5,last_task_used_spe@l(r4)
1005 #endif /* !CONFIG_SMP */
1006         blr
1007 #endif /* CONFIG_SPE */
1008
1009 /*
1010  * extern void giveup_fpu(struct task_struct *prev)
1011  *
1012  * Not all FSL Book-E cores have an FPU
1013  */
1014 #ifndef CONFIG_PPC_FPU
1015 _GLOBAL(giveup_fpu)
1016         blr
1017 #endif
1018
1019 /*
1020  * extern void abort(void)
1021  *
1022  * At present, this routine just applies a system reset.
1023  */
1024 _GLOBAL(abort)
1025         li      r13,0
1026         mtspr   SPRN_DBCR0,r13          /* disable all debug events */
1027         isync
1028         mfmsr   r13
1029         ori     r13,r13,MSR_DE@l        /* Enable Debug Events */
1030         mtmsr   r13
1031         isync
1032         mfspr   r13,SPRN_DBCR0
1033         lis     r13,(DBCR0_IDM|DBCR0_RST_CHIP)@h
1034         mtspr   SPRN_DBCR0,r13
1035         isync
1036
1037 _GLOBAL(set_context)
1038
1039 #ifdef CONFIG_BDI_SWITCH
1040         /* Context switch the PTE pointer for the Abatron BDI2000.
1041          * The PGDIR is the second parameter.
1042          */
1043         lis     r5, abatron_pteptrs@h
1044         ori     r5, r5, abatron_pteptrs@l
1045         stw     r4, 0x4(r5)
1046 #endif
1047         mtspr   SPRN_PID,r3
1048         isync                   /* Force context change */
1049         blr
1050
1051 /*
1052  * We put a few things here that have to be page-aligned. This stuff
1053  * goes at the beginning of the data segment, which is page-aligned.
1054  */
1055         .data
1056         .align  12
1057         .globl  sdata
1058 sdata:
1059         .globl  empty_zero_page
1060 empty_zero_page:
1061         .space  4096
1062         .globl  swapper_pg_dir
1063 swapper_pg_dir:
1064         .space  PGD_TABLE_SIZE
1065
1066 /* Reserved 4k for the critical exception stack & 4k for the machine
1067  * check stack per CPU for kernel mode exceptions */
1068         .section .bss
1069         .align 12
1070 exception_stack_bottom:
1071         .space  BOOKE_EXCEPTION_STACK_SIZE * NR_CPUS
1072         .globl  exception_stack_top
1073 exception_stack_top:
1074
1075 /*
1076  * Room for two PTE pointers, usually the kernel and current user pointers
1077  * to their respective root page table.
1078  */
1079 abatron_pteptrs:
1080         .space  8