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