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