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