c671a9359f9c5ff266e0524c8288528a8f5224dc
[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         /* Debug Interrupt */
672         DEBUG_EXCEPTION
673
674 /*
675  * Local functions
676  */
677         /*
678          * Data TLB exceptions will bail out to this point
679          * if they can't resolve the lightweight TLB fault.
680          */
681 data_access:
682         NORMAL_EXCEPTION_PROLOG
683         mfspr   r5,SPRN_ESR             /* Grab the ESR, save it, pass arg3 */
684         stw     r5,_ESR(r11)
685         mfspr   r4,SPRN_DEAR            /* Grab the DEAR, save it, pass arg2 */
686         andis.  r10,r5,(ESR_ILK|ESR_DLK)@h
687         bne     1f
688         EXC_XFER_EE_LITE(0x0300, handle_page_fault)
689 1:
690         addi    r3,r1,STACK_FRAME_OVERHEAD
691         EXC_XFER_EE_LITE(0x0300, CacheLockingException)
692
693 /*
694
695  * Both the instruction and data TLB miss get to this
696  * point to load the TLB.
697  *      r10 - EA of fault
698  *      r11 - TLB (info from Linux PTE)
699  *      r12, r13 - available to use
700  *      CR5 - results of addr < TASK_SIZE
701  *      MAS0, MAS1 - loaded with proper value when we get here
702  *      MAS2, MAS3 - will need additional info from Linux PTE
703  *      Upon exit, we reload everything and RFI.
704  */
705 finish_tlb_load:
706         /*
707          * We set execute, because we don't have the granularity to
708          * properly set this at the page level (Linux problem).
709          * Many of these bits are software only.  Bits we don't set
710          * here we (properly should) assume have the appropriate value.
711          */
712
713         mfspr   r12, SPRN_MAS2
714         rlwimi  r12, r11, 26, 27, 31    /* extract WIMGE from pte */
715         mtspr   SPRN_MAS2, r12
716
717         bge     5, 1f
718
719         /* addr > TASK_SIZE */
720         li      r10, (MAS3_UX | MAS3_UW | MAS3_UR)
721         andi.   r13, r11, (_PAGE_USER | _PAGE_HWWRITE | _PAGE_HWEXEC)
722         andi.   r12, r11, _PAGE_USER    /* Test for _PAGE_USER */
723         iseleq  r12, 0, r10
724         and     r10, r12, r13
725         srwi    r12, r10, 1
726         or      r12, r12, r10   /* Copy user perms into supervisor */
727         b       2f
728
729         /* addr <= TASK_SIZE */
730 1:      rlwinm  r12, r11, 31, 29, 29    /* Extract _PAGE_HWWRITE into SW */
731         ori     r12, r12, (MAS3_SX | MAS3_SR)
732
733 2:      rlwimi  r11, r12, 0, 20, 31     /* Extract RPN from PTE and merge with perms */
734         mtspr   SPRN_MAS3, r11
735         tlbwe
736
737         /* Done...restore registers and get out of here.  */
738         mfspr   r11, SPRG7R
739         mtcr    r11
740         mfspr   r13, SPRG5R
741         mfspr   r12, SPRG4R
742         mfspr   r11, SPRG1
743         mfspr   r10, SPRG0
744         rfi                                     /* Force context change */
745
746 #ifdef CONFIG_SPE
747 /* Note that the SPE support is closely modeled after the AltiVec
748  * support.  Changes to one are likely to be applicable to the
749  * other!  */
750 load_up_spe:
751 /*
752  * Disable SPE for the task which had SPE previously,
753  * and save its SPE registers in its thread_struct.
754  * Enables SPE for use in the kernel on return.
755  * On SMP we know the SPE units are free, since we give it up every
756  * switch.  -- Kumar
757  */
758         mfmsr   r5
759         oris    r5,r5,MSR_SPE@h
760         mtmsr   r5                      /* enable use of SPE now */
761         isync
762 /*
763  * For SMP, we don't do lazy SPE switching because it just gets too
764  * horrendously complex, especially when a task switches from one CPU
765  * to another.  Instead we call giveup_spe in switch_to.
766  */
767 #ifndef CONFIG_SMP
768         lis     r3,last_task_used_spe@ha
769         lwz     r4,last_task_used_spe@l(r3)
770         cmpi    0,r4,0
771         beq     1f
772         addi    r4,r4,THREAD    /* want THREAD of last_task_used_spe */
773         SAVE_32EVR(0,r10,r4)
774         evxor   evr10, evr10, evr10     /* clear out evr10 */
775         evmwumiaa evr10, evr10, evr10   /* evr10 <- ACC = 0 * 0 + ACC */
776         li      r5,THREAD_ACC
777         evstddx evr10, r4, r5           /* save off accumulator */
778         lwz     r5,PT_REGS(r4)
779         lwz     r4,_MSR-STACK_FRAME_OVERHEAD(r5)
780         lis     r10,MSR_SPE@h
781         andc    r4,r4,r10       /* disable SPE for previous task */
782         stw     r4,_MSR-STACK_FRAME_OVERHEAD(r5)
783 1:
784 #endif /* CONFIG_SMP */
785         /* enable use of SPE after return */
786         oris    r9,r9,MSR_SPE@h
787         mfspr   r5,SPRG3                /* current task's THREAD (phys) */
788         li      r4,1
789         li      r10,THREAD_ACC
790         stw     r4,THREAD_USED_SPE(r5)
791         evlddx  evr4,r10,r5
792         evmra   evr4,evr4
793         REST_32EVR(0,r10,r5)
794 #ifndef CONFIG_SMP
795         subi    r4,r5,THREAD
796         stw     r4,last_task_used_spe@l(r3)
797 #endif /* CONFIG_SMP */
798         /* restore registers and return */
799 2:      REST_4GPRS(3, r11)
800         lwz     r10,_CCR(r11)
801         REST_GPR(1, r11)
802         mtcr    r10
803         lwz     r10,_LINK(r11)
804         mtlr    r10
805         REST_GPR(10, r11)
806         mtspr   SRR1,r9
807         mtspr   SRR0,r12
808         REST_GPR(9, r11)
809         REST_GPR(12, r11)
810         lwz     r11,GPR11(r11)
811         SYNC
812         rfi
813
814
815
816 /*
817  * SPE unavailable trap from kernel - print a message, but let
818  * the task use SPE in the kernel until it returns to user mode.
819  */
820 KernelSPE:
821         lwz     r3,_MSR(r1)
822         oris    r3,r3,MSR_SPE@h
823         stw     r3,_MSR(r1)     /* enable use of SPE after return */
824         lis     r3,87f@h
825         ori     r3,r3,87f@l
826         mr      r4,r2           /* current */
827         lwz     r5,_NIP(r1)
828         bl      printk
829         b       ret_from_except
830 87:     .string "SPE used in kernel  (task=%p, pc=%x)  \n"
831         .align  4,0
832
833 #endif /* CONFIG_SPE */
834
835 /*
836  * Global functions
837  */
838
839 /*
840  * extern void loadcam_entry(unsigned int index)
841  *
842  * Load TLBCAM[index] entry in to the L2 CAM MMU
843  */
844 _GLOBAL(loadcam_entry)
845         lis     r4,TLBCAM@ha
846         addi    r4,r4,TLBCAM@l
847         mulli   r5,r3,20
848         add     r3,r5,r4
849         lwz     r4,0(r3)
850         mtspr   SPRN_MAS0,r4
851         lwz     r4,4(r3)
852         mtspr   SPRN_MAS1,r4
853         lwz     r4,8(r3)
854         mtspr   SPRN_MAS2,r4
855         lwz     r4,12(r3)
856         mtspr   SPRN_MAS3,r4
857         tlbwe
858         isync
859         blr
860
861 /*
862  * extern void giveup_altivec(struct task_struct *prev)
863  *
864  * The e500 core does not have an AltiVec unit.
865  */
866 _GLOBAL(giveup_altivec)
867         blr
868
869 #ifdef CONFIG_SPE
870 /*
871  * extern void giveup_spe(struct task_struct *prev)
872  *
873  */
874 _GLOBAL(giveup_spe)
875         mfmsr   r5
876         oris    r5,r5,MSR_SPE@h
877         SYNC
878         mtmsr   r5                      /* enable use of SPE now */
879         isync
880         cmpi    0,r3,0
881         beqlr-                          /* if no previous owner, done */
882         addi    r3,r3,THREAD            /* want THREAD of task */
883         lwz     r5,PT_REGS(r3)
884         cmpi    0,r5,0
885         SAVE_32EVR(0, r4, r3)
886         evxor   evr6, evr6, evr6        /* clear out evr6 */
887         evmwumiaa evr6, evr6, evr6      /* evr6 <- ACC = 0 * 0 + ACC */
888         li      r4,THREAD_ACC
889         evstddx evr6, r4, r3            /* save off accumulator */
890         mfspr   r6,SPRN_SPEFSCR
891         stw     r6,THREAD_SPEFSCR(r3)   /* save spefscr register value */
892         beq     1f
893         lwz     r4,_MSR-STACK_FRAME_OVERHEAD(r5)
894         lis     r3,MSR_SPE@h
895         andc    r4,r4,r3                /* disable SPE for previous task */
896         stw     r4,_MSR-STACK_FRAME_OVERHEAD(r5)
897 1:
898 #ifndef CONFIG_SMP
899         li      r5,0
900         lis     r4,last_task_used_spe@ha
901         stw     r5,last_task_used_spe@l(r4)
902 #endif /* CONFIG_SMP */
903         blr
904 #endif /* CONFIG_SPE */
905
906 /*
907  * extern void giveup_fpu(struct task_struct *prev)
908  *
909  * The e500 core does not have an FPU.
910  */
911 _GLOBAL(giveup_fpu)
912         blr
913
914 /*
915  * extern void abort(void)
916  *
917  * At present, this routine just applies a system reset.
918  */
919 _GLOBAL(abort)
920         li      r13,0
921         mtspr   SPRN_DBCR0,r13          /* disable all debug events */
922         mfmsr   r13
923         ori     r13,r13,MSR_DE@l        /* Enable Debug Events */
924         mtmsr   r13
925         mfspr   r13,SPRN_DBCR0
926         lis     r13,(DBCR0_IDM|DBCR0_RST_CHIP)@h
927         mtspr   SPRN_DBCR0,r13
928
929 _GLOBAL(set_context)
930
931 #ifdef CONFIG_BDI_SWITCH
932         /* Context switch the PTE pointer for the Abatron BDI2000.
933          * The PGDIR is the second parameter.
934          */
935         lis     r5, abatron_pteptrs@h
936         ori     r5, r5, abatron_pteptrs@l
937         stw     r4, 0x4(r5)
938 #endif
939         mtspr   SPRN_PID,r3
940         isync                   /* Force context change */
941         blr
942
943 /*
944  * We put a few things here that have to be page-aligned. This stuff
945  * goes at the beginning of the data segment, which is page-aligned.
946  */
947         .data
948 _GLOBAL(sdata)
949 _GLOBAL(empty_zero_page)
950         .space  4096
951 _GLOBAL(swapper_pg_dir)
952         .space  4096
953
954         .section .bss
955 /* Stack for handling critical exceptions from kernel mode */
956 critical_stack_bottom:
957         .space 4096
958 critical_stack_top:
959         .previous
960
961 /* Stack for handling machine check exceptions from kernel mode */
962 mcheck_stack_bottom:
963         .space 4096
964 mcheck_stack_top:
965         .previous
966
967 /*
968  * This area is used for temporarily saving registers during the
969  * critical and machine check exception prologs. It must always
970  * follow the page aligned allocations, so it starts on a page
971  * boundary, ensuring that all crit_save areas are in a single
972  * page.
973  */
974
975 /* crit_save */
976 _GLOBAL(crit_save)
977         .space  4
978 _GLOBAL(crit_r10)
979         .space  4
980 _GLOBAL(crit_r11)
981         .space  4
982 _GLOBAL(crit_sprg0)
983         .space  4
984 _GLOBAL(crit_sprg1)
985         .space  4
986 _GLOBAL(crit_sprg4)
987         .space  4
988 _GLOBAL(crit_sprg5)
989         .space  4
990 _GLOBAL(crit_sprg7)
991         .space  4
992 _GLOBAL(crit_pid)
993         .space  4
994 _GLOBAL(crit_srr0)
995         .space  4
996 _GLOBAL(crit_srr1)
997         .space  4
998
999 /* mcheck_save */
1000 _GLOBAL(mcheck_save)
1001         .space  4
1002 _GLOBAL(mcheck_r10)
1003         .space  4
1004 _GLOBAL(mcheck_r11)
1005         .space  4
1006 _GLOBAL(mcheck_sprg0)
1007         .space  4
1008 _GLOBAL(mcheck_sprg1)
1009         .space  4
1010 _GLOBAL(mcheck_sprg4)
1011         .space  4
1012 _GLOBAL(mcheck_sprg5)
1013         .space  4
1014 _GLOBAL(mcheck_sprg7)
1015         .space  4
1016 _GLOBAL(mcheck_pid)
1017         .space  4
1018 _GLOBAL(mcheck_srr0)
1019         .space  4
1020 _GLOBAL(mcheck_srr1)
1021         .space  4
1022 _GLOBAL(mcheck_csrr0)
1023         .space  4
1024 _GLOBAL(mcheck_csrr1)
1025         .space  4
1026
1027 /*
1028  * This space gets a copy of optional info passed to us by the bootstrap
1029  * which is used to pass parameters into the kernel like root=/dev/sda1, etc.
1030  */
1031 _GLOBAL(cmd_line)
1032         .space  512
1033
1034 /*
1035  * Room for two PTE pointers, usually the kernel and current user pointers
1036  * to their respective root page table.
1037  */
1038 abatron_pteptrs:
1039         .space  8
1040
1041