powerpc: remove arguments from fault handler functions
[linux-block.git] / arch / powerpc / kernel / head_book3s_32.S
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  *  PowerPC version
4  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
5  *
6  *  Rewritten by Cort Dougan (cort@cs.nmt.edu) for PReP
7  *    Copyright (C) 1996 Cort Dougan <cort@cs.nmt.edu>
8  *  Adapted for Power Macintosh by Paul Mackerras.
9  *  Low-level exception handlers and MMU support
10  *  rewritten by Paul Mackerras.
11  *    Copyright (C) 1996 Paul Mackerras.
12  *  MPC8xx modifications Copyright (C) 1997 Dan Malek (dmalek@jlc.net).
13  *
14  *  This file contains the low-level support and setup for the
15  *  PowerPC platform, including trap and interrupt dispatch.
16  *  (The PPC 8xx embedded CPUs use head_8xx.S instead.)
17  */
18
19 #include <linux/init.h>
20 #include <linux/pgtable.h>
21 #include <asm/reg.h>
22 #include <asm/page.h>
23 #include <asm/mmu.h>
24 #include <asm/cputable.h>
25 #include <asm/cache.h>
26 #include <asm/thread_info.h>
27 #include <asm/ppc_asm.h>
28 #include <asm/asm-offsets.h>
29 #include <asm/ptrace.h>
30 #include <asm/bug.h>
31 #include <asm/kvm_book3s_asm.h>
32 #include <asm/export.h>
33 #include <asm/feature-fixups.h>
34
35 #include "head_32.h"
36
37 #define LOAD_BAT(n, reg, RA, RB)        \
38         /* see the comment for clear_bats() -- Cort */ \
39         li      RA,0;                   \
40         mtspr   SPRN_IBAT##n##U,RA;     \
41         mtspr   SPRN_DBAT##n##U,RA;     \
42         lwz     RA,(n*16)+0(reg);       \
43         lwz     RB,(n*16)+4(reg);       \
44         mtspr   SPRN_IBAT##n##U,RA;     \
45         mtspr   SPRN_IBAT##n##L,RB;     \
46         lwz     RA,(n*16)+8(reg);       \
47         lwz     RB,(n*16)+12(reg);      \
48         mtspr   SPRN_DBAT##n##U,RA;     \
49         mtspr   SPRN_DBAT##n##L,RB
50
51         __HEAD
52         .stabs  "arch/powerpc/kernel/",N_SO,0,0,0f
53         .stabs  "head_book3s_32.S",N_SO,0,0,0f
54 0:
55 _ENTRY(_stext);
56
57 /*
58  * _start is defined this way because the XCOFF loader in the OpenFirmware
59  * on the powermac expects the entry point to be a procedure descriptor.
60  */
61 _ENTRY(_start);
62         /*
63          * These are here for legacy reasons, the kernel used to
64          * need to look like a coff function entry for the pmac
65          * but we're always started by some kind of bootloader now.
66          *  -- Cort
67          */
68         nop     /* used by __secondary_hold on prep (mtx) and chrp smp */
69         nop     /* used by __secondary_hold on prep (mtx) and chrp smp */
70         nop
71
72 /* PMAC
73  * Enter here with the kernel text, data and bss loaded starting at
74  * 0, running with virtual == physical mapping.
75  * r5 points to the prom entry point (the client interface handler
76  * address).  Address translation is turned on, with the prom
77  * managing the hash table.  Interrupts are disabled.  The stack
78  * pointer (r1) points to just below the end of the half-meg region
79  * from 0x380000 - 0x400000, which is mapped in already.
80  *
81  * If we are booted from MacOS via BootX, we enter with the kernel
82  * image loaded somewhere, and the following values in registers:
83  *  r3: 'BooX' (0x426f6f58)
84  *  r4: virtual address of boot_infos_t
85  *  r5: 0
86  *
87  * PREP
88  * This is jumped to on prep systems right after the kernel is relocated
89  * to its proper place in memory by the boot loader.  The expected layout
90  * of the regs is:
91  *   r3: ptr to residual data
92  *   r4: initrd_start or if no initrd then 0
93  *   r5: initrd_end - unused if r4 is 0
94  *   r6: Start of command line string
95  *   r7: End of command line string
96  *
97  * This just gets a minimal mmu environment setup so we can call
98  * start_here() to do the real work.
99  * -- Cort
100  */
101
102         .globl  __start
103 __start:
104 /*
105  * We have to do any OF calls before we map ourselves to KERNELBASE,
106  * because OF may have I/O devices mapped into that area
107  * (particularly on CHRP).
108  */
109         cmpwi   0,r5,0
110         beq     1f
111
112 #ifdef CONFIG_PPC_OF_BOOT_TRAMPOLINE
113         /* find out where we are now */
114         bcl     20,31,$+4
115 0:      mflr    r8                      /* r8 = runtime addr here */
116         addis   r8,r8,(_stext - 0b)@ha
117         addi    r8,r8,(_stext - 0b)@l   /* current runtime base addr */
118         bl      prom_init
119 #endif /* CONFIG_PPC_OF_BOOT_TRAMPOLINE */
120
121         /* We never return. We also hit that trap if trying to boot
122          * from OF while CONFIG_PPC_OF_BOOT_TRAMPOLINE isn't selected */
123         trap
124
125 /*
126  * Check for BootX signature when supporting PowerMac and branch to
127  * appropriate trampoline if it's present
128  */
129 #ifdef CONFIG_PPC_PMAC
130 1:      lis     r31,0x426f
131         ori     r31,r31,0x6f58
132         cmpw    0,r3,r31
133         bne     1f
134         bl      bootx_init
135         trap
136 #endif /* CONFIG_PPC_PMAC */
137
138 1:      mr      r31,r3                  /* save device tree ptr */
139         li      r24,0                   /* cpu # */
140
141 /*
142  * early_init() does the early machine identification and does
143  * the necessary low-level setup and clears the BSS
144  *  -- Cort <cort@fsmlabs.com>
145  */
146         bl      early_init
147
148 /* Switch MMU off, clear BATs and flush TLB. At this point, r3 contains
149  * the physical address we are running at, returned by early_init()
150  */
151         bl      mmu_off
152 __after_mmu_off:
153         bl      clear_bats
154         bl      flush_tlbs
155
156         bl      initial_bats
157         bl      load_segment_registers
158         bl      reloc_offset
159         bl      early_hash_table
160 #if defined(CONFIG_BOOTX_TEXT)
161         bl      setup_disp_bat
162 #endif
163 #ifdef CONFIG_PPC_EARLY_DEBUG_CPM
164         bl      setup_cpm_bat
165 #endif
166 #ifdef CONFIG_PPC_EARLY_DEBUG_USBGECKO
167         bl      setup_usbgecko_bat
168 #endif
169
170 /*
171  * Call setup_cpu for CPU 0 and initialize 6xx Idle
172  */
173         bl      reloc_offset
174         li      r24,0                   /* cpu# */
175         bl      call_setup_cpu          /* Call setup_cpu for this CPU */
176         bl      reloc_offset
177         bl      init_idle_6xx
178
179
180 /*
181  * We need to run with _start at physical address 0.
182  * On CHRP, we are loaded at 0x10000 since OF on CHRP uses
183  * the exception vectors at 0 (and therefore this copy
184  * overwrites OF's exception vectors with our own).
185  * The MMU is off at this point.
186  */
187         bl      reloc_offset
188         mr      r26,r3
189         addis   r4,r3,KERNELBASE@h      /* current address of _start */
190         lis     r5,PHYSICAL_START@h
191         cmplw   0,r4,r5                 /* already running at PHYSICAL_START? */
192         bne     relocate_kernel
193 /*
194  * we now have the 1st 16M of ram mapped with the bats.
195  * prep needs the mmu to be turned on here, but pmac already has it on.
196  * this shouldn't bother the pmac since it just gets turned on again
197  * as we jump to our code at KERNELBASE. -- Cort
198  * Actually no, pmac doesn't have it on any more. BootX enters with MMU
199  * off, and in other cases, we now turn it off before changing BATs above.
200  */
201 turn_on_mmu:
202         mfmsr   r0
203         ori     r0,r0,MSR_DR|MSR_IR|MSR_RI
204         mtspr   SPRN_SRR1,r0
205         lis     r0,start_here@h
206         ori     r0,r0,start_here@l
207         mtspr   SPRN_SRR0,r0
208         rfi                             /* enables MMU */
209
210 /*
211  * We need __secondary_hold as a place to hold the other cpus on
212  * an SMP machine, even when we are running a UP kernel.
213  */
214         . = 0xc0                        /* for prep bootloader */
215         li      r3,1                    /* MTX only has 1 cpu */
216         .globl  __secondary_hold
217 __secondary_hold:
218         /* tell the master we're here */
219         stw     r3,__secondary_hold_acknowledge@l(0)
220 #ifdef CONFIG_SMP
221 100:    lwz     r4,0(0)
222         /* wait until we're told to start */
223         cmpw    0,r4,r3
224         bne     100b
225         /* our cpu # was at addr 0 - go */
226         mr      r24,r3                  /* cpu # */
227         b       __secondary_start
228 #else
229         b       .
230 #endif /* CONFIG_SMP */
231
232         .globl  __secondary_hold_spinloop
233 __secondary_hold_spinloop:
234         .long   0
235         .globl  __secondary_hold_acknowledge
236 __secondary_hold_acknowledge:
237         .long   -1
238
239 /* System reset */
240 /* core99 pmac starts the seconary here by changing the vector, and
241    putting it back to what it was (unknown_exception) when done.  */
242         EXCEPTION(0x100, Reset, unknown_exception, EXC_XFER_STD)
243
244 /* Machine check */
245 /*
246  * On CHRP, this is complicated by the fact that we could get a
247  * machine check inside RTAS, and we have no guarantee that certain
248  * critical registers will have the values we expect.  The set of
249  * registers that might have bad values includes all the GPRs
250  * and all the BATs.  We indicate that we are in RTAS by putting
251  * a non-zero value, the address of the exception frame to use,
252  * in thread.rtas_sp.  The machine check handler checks thread.rtas_sp
253  * and uses its value if it is non-zero.
254  * (Other exception handlers assume that r1 is a valid kernel stack
255  * pointer when we take an exception from supervisor mode.)
256  *      -- paulus.
257  */
258         . = 0x200
259         DO_KVM  0x200
260 MachineCheck:
261         EXCEPTION_PROLOG_0
262 #ifdef CONFIG_PPC_CHRP
263         mfspr   r11, SPRN_SPRG_THREAD
264         lwz     r11, RTAS_SP(r11)
265         cmpwi   cr1, r11, 0
266         bne     cr1, 7f
267 #endif /* CONFIG_PPC_CHRP */
268         EXCEPTION_PROLOG_1 for_rtas=1
269 7:      EXCEPTION_PROLOG_2
270         addi    r3,r1,STACK_FRAME_OVERHEAD
271 #ifdef CONFIG_PPC_CHRP
272 #ifdef CONFIG_VMAP_STACK
273         mfspr   r4, SPRN_SPRG_THREAD
274         tovirt(r4, r4)
275         lwz     r4, RTAS_SP(r4)
276         cmpwi   cr1, r4, 0
277 #endif
278         beq     cr1, machine_check_tramp
279         twi     31, 0, 0
280 #else
281         b       machine_check_tramp
282 #endif
283
284 /* Data access exception. */
285         . = 0x300
286         DO_KVM  0x300
287 DataAccess:
288 #ifdef CONFIG_VMAP_STACK
289 #ifdef CONFIG_PPC_BOOK3S_604
290 BEGIN_MMU_FTR_SECTION
291         mtspr   SPRN_SPRG_SCRATCH2,r10
292         mfspr   r10, SPRN_SPRG_THREAD
293         stw     r11, THR11(r10)
294         mfspr   r10, SPRN_DSISR
295         mfcr    r11
296         andis.  r10, r10, (DSISR_BAD_FAULT_32S | DSISR_DABRMATCH)@h
297         mfspr   r10, SPRN_SPRG_THREAD
298         beq     hash_page_dsi
299 .Lhash_page_dsi_cont:
300         mtcr    r11
301         lwz     r11, THR11(r10)
302         mfspr   r10, SPRN_SPRG_SCRATCH2
303 MMU_FTR_SECTION_ELSE
304         b       1f
305 ALT_MMU_FTR_SECTION_END_IFSET(MMU_FTR_HPTE_TABLE)
306 #endif
307 1:      EXCEPTION_PROLOG_0 handle_dar_dsisr=1
308         EXCEPTION_PROLOG_1
309         b       handle_page_fault_tramp_1
310 #else   /* CONFIG_VMAP_STACK */
311         EXCEPTION_PROLOG handle_dar_dsisr=1
312         get_and_save_dar_dsisr_on_stack r4, r5, r11
313 #ifdef CONFIG_PPC_BOOK3S_604
314 BEGIN_MMU_FTR_SECTION
315         andis.  r0, r5, (DSISR_BAD_FAULT_32S | DSISR_DABRMATCH)@h
316         bne     handle_page_fault_tramp_2       /* if not, try to put a PTE */
317         rlwinm  r3, r5, 32 - 15, 21, 21         /* DSISR_STORE -> _PAGE_RW */
318         bl      hash_page
319         b       handle_page_fault_tramp_1
320 MMU_FTR_SECTION_ELSE
321 #endif
322         b       handle_page_fault_tramp_2
323 #ifdef CONFIG_PPC_BOOK3S_604
324 ALT_MMU_FTR_SECTION_END_IFSET(MMU_FTR_HPTE_TABLE)
325 #endif
326 #endif  /* CONFIG_VMAP_STACK */
327
328 /* Instruction access exception. */
329         . = 0x400
330         DO_KVM  0x400
331 InstructionAccess:
332 #ifdef CONFIG_VMAP_STACK
333         mtspr   SPRN_SPRG_SCRATCH0,r10
334         mtspr   SPRN_SPRG_SCRATCH1,r11
335         mfspr   r10, SPRN_SPRG_THREAD
336         mfspr   r11, SPRN_SRR0
337         stw     r11, SRR0(r10)
338         mfspr   r11, SPRN_SRR1          /* check whether user or kernel */
339         stw     r11, SRR1(r10)
340         mfcr    r10
341 #ifdef CONFIG_PPC_BOOK3S_604
342 BEGIN_MMU_FTR_SECTION
343         andis.  r11, r11, SRR1_ISI_NOPT@h       /* no pte found? */
344         bne     hash_page_isi
345 .Lhash_page_isi_cont:
346         mfspr   r11, SPRN_SRR1          /* check whether user or kernel */
347 END_MMU_FTR_SECTION_IFSET(MMU_FTR_HPTE_TABLE)
348 #endif
349         andi.   r11, r11, MSR_PR
350
351         EXCEPTION_PROLOG_1
352         EXCEPTION_PROLOG_2
353 #else   /* CONFIG_VMAP_STACK */
354         EXCEPTION_PROLOG
355         andis.  r0,r9,SRR1_ISI_NOPT@h   /* no pte found? */
356         beq     1f                      /* if so, try to put a PTE */
357         li      r3,0                    /* into the hash table */
358         mr      r4,r12                  /* SRR0 is fault address */
359 #ifdef CONFIG_PPC_BOOK3S_604
360 BEGIN_MMU_FTR_SECTION
361         bl      hash_page
362 END_MMU_FTR_SECTION_IFSET(MMU_FTR_HPTE_TABLE)
363 #endif
364 #endif  /* CONFIG_VMAP_STACK */
365         andis.  r5,r9,DSISR_SRR1_MATCH_32S@h /* Filter relevant SRR1 bits */
366         stw     r5, _DSISR(r11)
367         stw     r12, _DAR(r11)
368         EXC_XFER_LITE(0x400, handle_page_fault)
369
370 /* External interrupt */
371         EXCEPTION(0x500, HardwareInterrupt, do_IRQ, EXC_XFER_LITE)
372
373 /* Alignment exception */
374         . = 0x600
375         DO_KVM  0x600
376 Alignment:
377         EXCEPTION_PROLOG handle_dar_dsisr=1
378         save_dar_dsisr_on_stack r4, r5, r11
379         addi    r3,r1,STACK_FRAME_OVERHEAD
380         b       alignment_exception_tramp
381
382 /* Program check exception */
383         EXCEPTION(0x700, ProgramCheck, program_check_exception, EXC_XFER_STD)
384
385 /* Floating-point unavailable */
386         . = 0x800
387         DO_KVM  0x800
388 FPUnavailable:
389 #ifdef CONFIG_PPC_FPU
390 BEGIN_FTR_SECTION
391 /*
392  * Certain Freescale cores don't have a FPU and treat fp instructions
393  * as a FP Unavailable exception.  Redirect to illegal/emulation handling.
394  */
395         b       ProgramCheck
396 END_FTR_SECTION_IFSET(CPU_FTR_FPU_UNAVAILABLE)
397         EXCEPTION_PROLOG
398         beq     1f
399         bl      load_up_fpu             /* if from user, just load it up */
400         b       fast_exception_return
401 1:      addi    r3,r1,STACK_FRAME_OVERHEAD
402         EXC_XFER_LITE(0x800, kernel_fp_unavailable_exception)
403 #else
404         b       ProgramCheck
405 #endif
406
407 /* Decrementer */
408         EXCEPTION(0x900, Decrementer, timer_interrupt, EXC_XFER_LITE)
409
410         EXCEPTION(0xa00, Trap_0a, unknown_exception, EXC_XFER_STD)
411         EXCEPTION(0xb00, Trap_0b, unknown_exception, EXC_XFER_STD)
412
413 /* System call */
414         . = 0xc00
415         DO_KVM  0xc00
416 SystemCall:
417         SYSCALL_ENTRY   0xc00
418
419         EXCEPTION(0xd00, SingleStep, single_step_exception, EXC_XFER_STD)
420         EXCEPTION(0xe00, Trap_0e, unknown_exception, EXC_XFER_STD)
421
422 /*
423  * The Altivec unavailable trap is at 0x0f20.  Foo.
424  * We effectively remap it to 0x3000.
425  * We include an altivec unavailable exception vector even if
426  * not configured for Altivec, so that you can't panic a
427  * non-altivec kernel running on a machine with altivec just
428  * by executing an altivec instruction.
429  */
430         . = 0xf00
431         DO_KVM  0xf00
432         b       PerformanceMonitor
433
434         . = 0xf20
435         DO_KVM  0xf20
436         b       AltiVecUnavailable
437
438 /*
439  * Handle TLB miss for instruction on 603/603e.
440  * Note: we get an alternate set of r0 - r3 to use automatically.
441  */
442         . = 0x1000
443 InstructionTLBMiss:
444 /*
445  * r0:  scratch
446  * r1:  linux style pte ( later becomes ppc hardware pte )
447  * r2:  ptr to linux-style pte
448  * r3:  scratch
449  */
450         /* Get PTE (linux-style) and check access */
451         mfspr   r3,SPRN_IMISS
452 #ifdef CONFIG_MODULES
453         lis     r1, TASK_SIZE@h         /* check if kernel address */
454         cmplw   0,r1,r3
455 #endif
456         mfspr   r2, SPRN_SDR1
457         li      r1,_PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_EXEC
458         rlwinm  r2, r2, 28, 0xfffff000
459 #ifdef CONFIG_MODULES
460         bgt-    112f
461         lis     r2, (swapper_pg_dir - PAGE_OFFSET)@ha   /* if kernel address, use */
462         addi    r2, r2, (swapper_pg_dir - PAGE_OFFSET)@l        /* kernel page table */
463 #endif
464 112:    rlwimi  r2,r3,12,20,29          /* insert top 10 bits of address */
465         lwz     r2,0(r2)                /* get pmd entry */
466         rlwinm. r2,r2,0,0,19            /* extract address of pte page */
467         beq-    InstructionAddressInvalid       /* return if no mapping */
468         rlwimi  r2,r3,22,20,29          /* insert next 10 bits of address */
469         lwz     r0,0(r2)                /* get linux-style pte */
470         andc.   r1,r1,r0                /* check access & ~permission */
471         bne-    InstructionAddressInvalid /* return if access not permitted */
472         /* Convert linux-style PTE to low word of PPC-style PTE */
473         rlwimi  r0,r0,32-2,31,31        /* _PAGE_USER -> PP lsb */
474         ori     r1, r1, 0xe06           /* clear out reserved bits */
475         andc    r1, r0, r1              /* PP = user? 1 : 0 */
476 BEGIN_FTR_SECTION
477         rlwinm  r1,r1,0,~_PAGE_COHERENT /* clear M (coherence not required) */
478 END_FTR_SECTION_IFCLR(CPU_FTR_NEED_COHERENT)
479         mtspr   SPRN_RPA,r1
480         tlbli   r3
481         mfspr   r3,SPRN_SRR1            /* Need to restore CR0 */
482         mtcrf   0x80,r3
483         rfi
484 InstructionAddressInvalid:
485         mfspr   r3,SPRN_SRR1
486         rlwinm  r1,r3,9,6,6     /* Get load/store bit */
487
488         addis   r1,r1,0x2000
489         mtspr   SPRN_DSISR,r1   /* (shouldn't be needed) */
490         andi.   r2,r3,0xFFFF    /* Clear upper bits of SRR1 */
491         or      r2,r2,r1
492         mtspr   SPRN_SRR1,r2
493         mfspr   r1,SPRN_IMISS   /* Get failing address */
494         rlwinm. r2,r2,0,31,31   /* Check for little endian access */
495         rlwimi  r2,r2,1,30,30   /* change 1 -> 3 */
496         xor     r1,r1,r2
497         mtspr   SPRN_DAR,r1     /* Set fault address */
498         mfmsr   r0              /* Restore "normal" registers */
499         xoris   r0,r0,MSR_TGPR>>16
500         mtcrf   0x80,r3         /* Restore CR0 */
501         mtmsr   r0
502         b       InstructionAccess
503
504 /*
505  * Handle TLB miss for DATA Load operation on 603/603e
506  */
507         . = 0x1100
508 DataLoadTLBMiss:
509 /*
510  * r0:  scratch
511  * r1:  linux style pte ( later becomes ppc hardware pte )
512  * r2:  ptr to linux-style pte
513  * r3:  scratch
514  */
515         /* Get PTE (linux-style) and check access */
516         mfspr   r3,SPRN_DMISS
517         lis     r1, TASK_SIZE@h         /* check if kernel address */
518         cmplw   0,r1,r3
519         mfspr   r2, SPRN_SDR1
520         li      r1, _PAGE_PRESENT | _PAGE_ACCESSED
521         rlwinm  r2, r2, 28, 0xfffff000
522         bgt-    112f
523         lis     r2, (swapper_pg_dir - PAGE_OFFSET)@ha   /* if kernel address, use */
524         addi    r2, r2, (swapper_pg_dir - PAGE_OFFSET)@l        /* kernel page table */
525 112:    rlwimi  r2,r3,12,20,29          /* insert top 10 bits of address */
526         lwz     r2,0(r2)                /* get pmd entry */
527         rlwinm. r2,r2,0,0,19            /* extract address of pte page */
528         beq-    DataAddressInvalid      /* return if no mapping */
529         rlwimi  r2,r3,22,20,29          /* insert next 10 bits of address */
530         lwz     r0,0(r2)                /* get linux-style pte */
531         andc.   r1,r1,r0                /* check access & ~permission */
532         bne-    DataAddressInvalid      /* return if access not permitted */
533         /*
534          * NOTE! We are assuming this is not an SMP system, otherwise
535          * we would need to update the pte atomically with lwarx/stwcx.
536          */
537         /* Convert linux-style PTE to low word of PPC-style PTE */
538         rlwinm  r1,r0,32-9,30,30        /* _PAGE_RW -> PP msb */
539         rlwimi  r0,r0,32-1,30,30        /* _PAGE_USER -> PP msb */
540         rlwimi  r0,r0,32-1,31,31        /* _PAGE_USER -> PP lsb */
541         ori     r1,r1,0xe04             /* clear out reserved bits */
542         andc    r1,r0,r1                /* PP = user? rw? 1: 3: 0 */
543 BEGIN_FTR_SECTION
544         rlwinm  r1,r1,0,~_PAGE_COHERENT /* clear M (coherence not required) */
545 END_FTR_SECTION_IFCLR(CPU_FTR_NEED_COHERENT)
546         mtspr   SPRN_RPA,r1
547         mfspr   r2,SPRN_SRR1            /* Need to restore CR0 */
548         mtcrf   0x80,r2
549 BEGIN_MMU_FTR_SECTION
550         li      r0,1
551         mfspr   r1,SPRN_SPRG_603_LRU
552         rlwinm  r2,r3,20,27,31          /* Get Address bits 15:19 */
553         slw     r0,r0,r2
554         xor     r1,r0,r1
555         srw     r0,r1,r2
556         mtspr   SPRN_SPRG_603_LRU,r1
557         mfspr   r2,SPRN_SRR1
558         rlwimi  r2,r0,31-14,14,14
559         mtspr   SPRN_SRR1,r2
560 END_MMU_FTR_SECTION_IFSET(MMU_FTR_NEED_DTLB_SW_LRU)
561         tlbld   r3
562         rfi
563 DataAddressInvalid:
564         mfspr   r3,SPRN_SRR1
565         rlwinm  r1,r3,9,6,6     /* Get load/store bit */
566         addis   r1,r1,0x2000
567         mtspr   SPRN_DSISR,r1
568         andi.   r2,r3,0xFFFF    /* Clear upper bits of SRR1 */
569         mtspr   SPRN_SRR1,r2
570         mfspr   r1,SPRN_DMISS   /* Get failing address */
571         rlwinm. r2,r2,0,31,31   /* Check for little endian access */
572         beq     20f             /* Jump if big endian */
573         xori    r1,r1,3
574 20:     mtspr   SPRN_DAR,r1     /* Set fault address */
575         mfmsr   r0              /* Restore "normal" registers */
576         xoris   r0,r0,MSR_TGPR>>16
577         mtcrf   0x80,r3         /* Restore CR0 */
578         mtmsr   r0
579         b       DataAccess
580
581 /*
582  * Handle TLB miss for DATA Store on 603/603e
583  */
584         . = 0x1200
585 DataStoreTLBMiss:
586 /*
587  * r0:  scratch
588  * r1:  linux style pte ( later becomes ppc hardware pte )
589  * r2:  ptr to linux-style pte
590  * r3:  scratch
591  */
592         /* Get PTE (linux-style) and check access */
593         mfspr   r3,SPRN_DMISS
594         lis     r1, TASK_SIZE@h         /* check if kernel address */
595         cmplw   0,r1,r3
596         mfspr   r2, SPRN_SDR1
597         li      r1, _PAGE_RW | _PAGE_DIRTY | _PAGE_PRESENT | _PAGE_ACCESSED
598         rlwinm  r2, r2, 28, 0xfffff000
599         bgt-    112f
600         lis     r2, (swapper_pg_dir - PAGE_OFFSET)@ha   /* if kernel address, use */
601         addi    r2, r2, (swapper_pg_dir - PAGE_OFFSET)@l        /* kernel page table */
602 112:    rlwimi  r2,r3,12,20,29          /* insert top 10 bits of address */
603         lwz     r2,0(r2)                /* get pmd entry */
604         rlwinm. r2,r2,0,0,19            /* extract address of pte page */
605         beq-    DataAddressInvalid      /* return if no mapping */
606         rlwimi  r2,r3,22,20,29          /* insert next 10 bits of address */
607         lwz     r0,0(r2)                /* get linux-style pte */
608         andc.   r1,r1,r0                /* check access & ~permission */
609         bne-    DataAddressInvalid      /* return if access not permitted */
610         /*
611          * NOTE! We are assuming this is not an SMP system, otherwise
612          * we would need to update the pte atomically with lwarx/stwcx.
613          */
614         /* Convert linux-style PTE to low word of PPC-style PTE */
615         rlwimi  r0,r0,32-2,31,31        /* _PAGE_USER -> PP lsb */
616         li      r1,0xe06                /* clear out reserved bits & PP msb */
617         andc    r1,r0,r1                /* PP = user? 1: 0 */
618 BEGIN_FTR_SECTION
619         rlwinm  r1,r1,0,~_PAGE_COHERENT /* clear M (coherence not required) */
620 END_FTR_SECTION_IFCLR(CPU_FTR_NEED_COHERENT)
621         mtspr   SPRN_RPA,r1
622         mfspr   r2,SPRN_SRR1            /* Need to restore CR0 */
623         mtcrf   0x80,r2
624 BEGIN_MMU_FTR_SECTION
625         li      r0,1
626         mfspr   r1,SPRN_SPRG_603_LRU
627         rlwinm  r2,r3,20,27,31          /* Get Address bits 15:19 */
628         slw     r0,r0,r2
629         xor     r1,r0,r1
630         srw     r0,r1,r2
631         mtspr   SPRN_SPRG_603_LRU,r1
632         mfspr   r2,SPRN_SRR1
633         rlwimi  r2,r0,31-14,14,14
634         mtspr   SPRN_SRR1,r2
635 END_MMU_FTR_SECTION_IFSET(MMU_FTR_NEED_DTLB_SW_LRU)
636         tlbld   r3
637         rfi
638
639 #ifndef CONFIG_ALTIVEC
640 #define altivec_assist_exception        unknown_exception
641 #endif
642
643 #ifndef CONFIG_TAU_INT
644 #define TAUException    unknown_exception
645 #endif
646
647         EXCEPTION(0x1300, Trap_13, instruction_breakpoint_exception, EXC_XFER_STD)
648         EXCEPTION(0x1400, SMI, SMIException, EXC_XFER_STD)
649         EXCEPTION(0x1500, Trap_15, unknown_exception, EXC_XFER_STD)
650         EXCEPTION(0x1600, Trap_16, altivec_assist_exception, EXC_XFER_STD)
651         EXCEPTION(0x1700, Trap_17, TAUException, EXC_XFER_STD)
652         EXCEPTION(0x1800, Trap_18, unknown_exception, EXC_XFER_STD)
653         EXCEPTION(0x1900, Trap_19, unknown_exception, EXC_XFER_STD)
654         EXCEPTION(0x1a00, Trap_1a, unknown_exception, EXC_XFER_STD)
655         EXCEPTION(0x1b00, Trap_1b, unknown_exception, EXC_XFER_STD)
656         EXCEPTION(0x1c00, Trap_1c, unknown_exception, EXC_XFER_STD)
657         EXCEPTION(0x1d00, Trap_1d, unknown_exception, EXC_XFER_STD)
658         EXCEPTION(0x1e00, Trap_1e, unknown_exception, EXC_XFER_STD)
659         EXCEPTION(0x1f00, Trap_1f, unknown_exception, EXC_XFER_STD)
660         EXCEPTION(0x2000, RunMode, RunModeException, EXC_XFER_STD)
661         EXCEPTION(0x2100, Trap_21, unknown_exception, EXC_XFER_STD)
662         EXCEPTION(0x2200, Trap_22, unknown_exception, EXC_XFER_STD)
663         EXCEPTION(0x2300, Trap_23, unknown_exception, EXC_XFER_STD)
664         EXCEPTION(0x2400, Trap_24, unknown_exception, EXC_XFER_STD)
665         EXCEPTION(0x2500, Trap_25, unknown_exception, EXC_XFER_STD)
666         EXCEPTION(0x2600, Trap_26, unknown_exception, EXC_XFER_STD)
667         EXCEPTION(0x2700, Trap_27, unknown_exception, EXC_XFER_STD)
668         EXCEPTION(0x2800, Trap_28, unknown_exception, EXC_XFER_STD)
669         EXCEPTION(0x2900, Trap_29, unknown_exception, EXC_XFER_STD)
670         EXCEPTION(0x2a00, Trap_2a, unknown_exception, EXC_XFER_STD)
671         EXCEPTION(0x2b00, Trap_2b, unknown_exception, EXC_XFER_STD)
672         EXCEPTION(0x2c00, Trap_2c, unknown_exception, EXC_XFER_STD)
673         EXCEPTION(0x2d00, Trap_2d, unknown_exception, EXC_XFER_STD)
674         EXCEPTION(0x2e00, Trap_2e, unknown_exception, EXC_XFER_STD)
675         EXCEPTION(0x2f00, Trap_2f, unknown_exception, EXC_XFER_STD)
676
677         . = 0x3000
678
679 machine_check_tramp:
680         EXC_XFER_STD(0x200, machine_check_exception)
681
682 alignment_exception_tramp:
683         EXC_XFER_STD(0x600, alignment_exception)
684
685 handle_page_fault_tramp_1:
686 #ifdef CONFIG_VMAP_STACK
687         EXCEPTION_PROLOG_2 handle_dar_dsisr=1
688 #endif
689         lwz     r5, _DSISR(r11)
690         /* fall through */
691 handle_page_fault_tramp_2:
692         andis.  r0, r5, DSISR_DABRMATCH@h
693         bne-    1f
694         EXC_XFER_LITE(0x300, handle_page_fault)
695 1:      EXC_XFER_STD(0x300, do_break)
696
697 #ifdef CONFIG_VMAP_STACK
698 #ifdef CONFIG_PPC_BOOK3S_604
699 .macro save_regs_thread         thread
700         stw     r0, THR0(\thread)
701         stw     r3, THR3(\thread)
702         stw     r4, THR4(\thread)
703         stw     r5, THR5(\thread)
704         stw     r6, THR6(\thread)
705         stw     r8, THR8(\thread)
706         stw     r9, THR9(\thread)
707         mflr    r0
708         stw     r0, THLR(\thread)
709         mfctr   r0
710         stw     r0, THCTR(\thread)
711 .endm
712
713 .macro restore_regs_thread      thread
714         lwz     r0, THLR(\thread)
715         mtlr    r0
716         lwz     r0, THCTR(\thread)
717         mtctr   r0
718         lwz     r0, THR0(\thread)
719         lwz     r3, THR3(\thread)
720         lwz     r4, THR4(\thread)
721         lwz     r5, THR5(\thread)
722         lwz     r6, THR6(\thread)
723         lwz     r8, THR8(\thread)
724         lwz     r9, THR9(\thread)
725 .endm
726
727 hash_page_dsi:
728         save_regs_thread        r10
729         mfdsisr r3
730         mfdar   r4
731         mfsrr0  r5
732         mfsrr1  r9
733         rlwinm  r3, r3, 32 - 15, _PAGE_RW       /* DSISR_STORE -> _PAGE_RW */
734         bl      hash_page
735         mfspr   r10, SPRN_SPRG_THREAD
736         restore_regs_thread r10
737         b       .Lhash_page_dsi_cont
738
739 hash_page_isi:
740         mr      r11, r10
741         mfspr   r10, SPRN_SPRG_THREAD
742         save_regs_thread        r10
743         li      r3, 0
744         lwz     r4, SRR0(r10)
745         lwz     r9, SRR1(r10)
746         bl      hash_page
747         mfspr   r10, SPRN_SPRG_THREAD
748         restore_regs_thread r10
749         mr      r10, r11
750         b       .Lhash_page_isi_cont
751
752         .globl fast_hash_page_return
753 fast_hash_page_return:
754         andis.  r10, r9, SRR1_ISI_NOPT@h        /* Set on ISI, cleared on DSI */
755         mfspr   r10, SPRN_SPRG_THREAD
756         restore_regs_thread r10
757         bne     1f
758
759         /* DSI */
760         mtcr    r11
761         lwz     r11, THR11(r10)
762         mfspr   r10, SPRN_SPRG_SCRATCH2
763         rfi
764
765 1:      /* ISI */
766         mtcr    r11
767         mfspr   r11, SPRN_SPRG_SCRATCH1
768         mfspr   r10, SPRN_SPRG_SCRATCH0
769         rfi
770 #endif /* CONFIG_PPC_BOOK3S_604 */
771
772 stack_overflow:
773         vmap_stack_overflow_exception
774 #endif
775
776 AltiVecUnavailable:
777         EXCEPTION_PROLOG
778 #ifdef CONFIG_ALTIVEC
779         beq     1f
780         bl      load_up_altivec         /* if from user, just load it up */
781         b       fast_exception_return
782 #endif /* CONFIG_ALTIVEC */
783 1:      addi    r3,r1,STACK_FRAME_OVERHEAD
784         EXC_XFER_LITE(0xf20, altivec_unavailable_exception)
785
786 PerformanceMonitor:
787         EXCEPTION_PROLOG
788         addi    r3,r1,STACK_FRAME_OVERHEAD
789         EXC_XFER_STD(0xf00, performance_monitor_exception)
790
791
792 /*
793  * This code is jumped to from the startup code to copy
794  * the kernel image to physical address PHYSICAL_START.
795  */
796 relocate_kernel:
797         addis   r9,r26,klimit@ha        /* fetch klimit */
798         lwz     r25,klimit@l(r9)
799         addis   r25,r25,-KERNELBASE@h
800         lis     r3,PHYSICAL_START@h     /* Destination base address */
801         li      r6,0                    /* Destination offset */
802         li      r5,0x4000               /* # bytes of memory to copy */
803         bl      copy_and_flush          /* copy the first 0x4000 bytes */
804         addi    r0,r3,4f@l              /* jump to the address of 4f */
805         mtctr   r0                      /* in copy and do the rest. */
806         bctr                            /* jump to the copy */
807 4:      mr      r5,r25
808         bl      copy_and_flush          /* copy the rest */
809         b       turn_on_mmu
810
811 /*
812  * Copy routine used to copy the kernel to start at physical address 0
813  * and flush and invalidate the caches as needed.
814  * r3 = dest addr, r4 = source addr, r5 = copy limit, r6 = start offset
815  * on exit, r3, r4, r5 are unchanged, r6 is updated to be >= r5.
816  */
817 _ENTRY(copy_and_flush)
818         addi    r5,r5,-4
819         addi    r6,r6,-4
820 4:      li      r0,L1_CACHE_BYTES/4
821         mtctr   r0
822 3:      addi    r6,r6,4                 /* copy a cache line */
823         lwzx    r0,r6,r4
824         stwx    r0,r6,r3
825         bdnz    3b
826         dcbst   r6,r3                   /* write it to memory */
827         sync
828         icbi    r6,r3                   /* flush the icache line */
829         cmplw   0,r6,r5
830         blt     4b
831         sync                            /* additional sync needed on g4 */
832         isync
833         addi    r5,r5,4
834         addi    r6,r6,4
835         blr
836
837 #ifdef CONFIG_SMP
838         .globl __secondary_start_mpc86xx
839 __secondary_start_mpc86xx:
840         mfspr   r3, SPRN_PIR
841         stw     r3, __secondary_hold_acknowledge@l(0)
842         mr      r24, r3                 /* cpu # */
843         b       __secondary_start
844
845         .globl  __secondary_start_pmac_0
846 __secondary_start_pmac_0:
847         /* NB the entries for cpus 0, 1, 2 must each occupy 8 bytes. */
848         li      r24,0
849         b       1f
850         li      r24,1
851         b       1f
852         li      r24,2
853         b       1f
854         li      r24,3
855 1:
856         /* on powersurge, we come in here with IR=0 and DR=1, and DBAT 0
857            set to map the 0xf0000000 - 0xffffffff region */
858         mfmsr   r0
859         rlwinm  r0,r0,0,28,26           /* clear DR (0x10) */
860         mtmsr   r0
861         isync
862
863         .globl  __secondary_start
864 __secondary_start:
865         /* Copy some CPU settings from CPU 0 */
866         bl      __restore_cpu_setup
867
868         lis     r3,-KERNELBASE@h
869         mr      r4,r24
870         bl      call_setup_cpu          /* Call setup_cpu for this CPU */
871         lis     r3,-KERNELBASE@h
872         bl      init_idle_6xx
873
874         /* get current's stack and current */
875         lis     r2,secondary_current@ha
876         tophys(r2,r2)
877         lwz     r2,secondary_current@l(r2)
878         tophys(r1,r2)
879         lwz     r1,TASK_STACK(r1)
880
881         /* stack */
882         addi    r1,r1,THREAD_SIZE-STACK_FRAME_OVERHEAD
883         li      r0,0
884         tophys(r3,r1)
885         stw     r0,0(r3)
886
887         /* load up the MMU */
888         bl      load_segment_registers
889         bl      load_up_mmu
890
891         /* ptr to phys current thread */
892         tophys(r4,r2)
893         addi    r4,r4,THREAD    /* phys address of our thread_struct */
894         mtspr   SPRN_SPRG_THREAD,r4
895 BEGIN_MMU_FTR_SECTION
896         lis     r4, (swapper_pg_dir - PAGE_OFFSET)@h
897         ori     r4, r4, (swapper_pg_dir - PAGE_OFFSET)@l
898         rlwinm  r4, r4, 4, 0xffff01ff
899         mtspr   SPRN_SDR1, r4
900 END_MMU_FTR_SECTION_IFCLR(MMU_FTR_HPTE_TABLE)
901
902         /* enable MMU and jump to start_secondary */
903         li      r4,MSR_KERNEL
904         lis     r3,start_secondary@h
905         ori     r3,r3,start_secondary@l
906         mtspr   SPRN_SRR0,r3
907         mtspr   SPRN_SRR1,r4
908         rfi
909 #endif /* CONFIG_SMP */
910
911 #ifdef CONFIG_KVM_BOOK3S_HANDLER
912 #include "../kvm/book3s_rmhandlers.S"
913 #endif
914
915 /*
916  * Load stuff into the MMU.  Intended to be called with
917  * IR=0 and DR=0.
918  */
919 early_hash_table:
920         sync                    /* Force all PTE updates to finish */
921         isync
922         tlbia                   /* Clear all TLB entries */
923         sync                    /* wait for tlbia/tlbie to finish */
924         TLBSYNC                 /* ... on all CPUs */
925         /* Load the SDR1 register (hash table base & size) */
926         lis     r6, early_hash - PAGE_OFFSET@h
927         ori     r6, r6, 3       /* 256kB table */
928         mtspr   SPRN_SDR1, r6
929         blr
930
931 load_up_mmu:
932         sync                    /* Force all PTE updates to finish */
933         isync
934         tlbia                   /* Clear all TLB entries */
935         sync                    /* wait for tlbia/tlbie to finish */
936         TLBSYNC                 /* ... on all CPUs */
937 BEGIN_MMU_FTR_SECTION
938         /* Load the SDR1 register (hash table base & size) */
939         lis     r6,_SDR1@ha
940         tophys(r6,r6)
941         lwz     r6,_SDR1@l(r6)
942         mtspr   SPRN_SDR1,r6
943 END_MMU_FTR_SECTION_IFSET(MMU_FTR_HPTE_TABLE)
944
945 /* Load the BAT registers with the values set up by MMU_init. */
946         lis     r3,BATS@ha
947         addi    r3,r3,BATS@l
948         tophys(r3,r3)
949         LOAD_BAT(0,r3,r4,r5)
950         LOAD_BAT(1,r3,r4,r5)
951         LOAD_BAT(2,r3,r4,r5)
952         LOAD_BAT(3,r3,r4,r5)
953 BEGIN_MMU_FTR_SECTION
954         LOAD_BAT(4,r3,r4,r5)
955         LOAD_BAT(5,r3,r4,r5)
956         LOAD_BAT(6,r3,r4,r5)
957         LOAD_BAT(7,r3,r4,r5)
958 END_MMU_FTR_SECTION_IFSET(MMU_FTR_USE_HIGH_BATS)
959         blr
960
961 _GLOBAL(load_segment_registers)
962         li      r0, NUM_USER_SEGMENTS /* load up user segment register values */
963         mtctr   r0              /* for context 0 */
964         li      r3, 0           /* Kp = 0, Ks = 0, VSID = 0 */
965 #ifdef CONFIG_PPC_KUEP
966         oris    r3, r3, SR_NX@h /* Set Nx */
967 #endif
968 #ifdef CONFIG_PPC_KUAP
969         oris    r3, r3, SR_KS@h /* Set Ks */
970 #endif
971         li      r4, 0
972 3:      mtsrin  r3, r4
973         addi    r3, r3, 0x111   /* increment VSID */
974         addis   r4, r4, 0x1000  /* address of next segment */
975         bdnz    3b
976         li      r0, 16 - NUM_USER_SEGMENTS /* load up kernel segment registers */
977         mtctr   r0                      /* for context 0 */
978         rlwinm  r3, r3, 0, ~SR_NX       /* Nx = 0 */
979         rlwinm  r3, r3, 0, ~SR_KS       /* Ks = 0 */
980         oris    r3, r3, SR_KP@h         /* Kp = 1 */
981 3:      mtsrin  r3, r4
982         addi    r3, r3, 0x111   /* increment VSID */
983         addis   r4, r4, 0x1000  /* address of next segment */
984         bdnz    3b
985         blr
986
987 /*
988  * This is where the main kernel code starts.
989  */
990 start_here:
991         /* ptr to current */
992         lis     r2,init_task@h
993         ori     r2,r2,init_task@l
994         /* Set up for using our exception vectors */
995         /* ptr to phys current thread */
996         tophys(r4,r2)
997         addi    r4,r4,THREAD    /* init task's THREAD */
998         mtspr   SPRN_SPRG_THREAD,r4
999 BEGIN_MMU_FTR_SECTION
1000         lis     r4, (swapper_pg_dir - PAGE_OFFSET)@h
1001         ori     r4, r4, (swapper_pg_dir - PAGE_OFFSET)@l
1002         rlwinm  r4, r4, 4, 0xffff01ff
1003         mtspr   SPRN_SDR1, r4
1004 END_MMU_FTR_SECTION_IFCLR(MMU_FTR_HPTE_TABLE)
1005
1006         /* stack */
1007         lis     r1,init_thread_union@ha
1008         addi    r1,r1,init_thread_union@l
1009         li      r0,0
1010         stwu    r0,THREAD_SIZE-STACK_FRAME_OVERHEAD(r1)
1011 /*
1012  * Do early platform-specific initialization,
1013  * and set up the MMU.
1014  */
1015 #ifdef CONFIG_KASAN
1016         bl      kasan_early_init
1017 #endif
1018         li      r3,0
1019         mr      r4,r31
1020         bl      machine_init
1021         bl      __save_cpu_setup
1022         bl      MMU_init
1023         bl      MMU_init_hw_patch
1024
1025 /*
1026  * Go back to running unmapped so we can load up new values
1027  * for SDR1 (hash table pointer) and the segment registers
1028  * and change to using our exception vectors.
1029  */
1030         lis     r4,2f@h
1031         ori     r4,r4,2f@l
1032         tophys(r4,r4)
1033         li      r3,MSR_KERNEL & ~(MSR_IR|MSR_DR)
1034
1035         .align  4
1036         mtspr   SPRN_SRR0,r4
1037         mtspr   SPRN_SRR1,r3
1038         rfi
1039 /* Load up the kernel context */
1040 2:      bl      load_up_mmu
1041
1042 #ifdef CONFIG_BDI_SWITCH
1043         /* Add helper information for the Abatron bdiGDB debugger.
1044          * We do this here because we know the mmu is disabled, and
1045          * will be enabled for real in just a few instructions.
1046          */
1047         lis     r5, abatron_pteptrs@h
1048         ori     r5, r5, abatron_pteptrs@l
1049         stw     r5, 0xf0(0)     /* This much match your Abatron config */
1050         lis     r6, swapper_pg_dir@h
1051         ori     r6, r6, swapper_pg_dir@l
1052         tophys(r5, r5)
1053         stw     r6, 0(r5)
1054 #endif /* CONFIG_BDI_SWITCH */
1055
1056 /* Now turn on the MMU for real! */
1057         li      r4,MSR_KERNEL
1058         lis     r3,start_kernel@h
1059         ori     r3,r3,start_kernel@l
1060         mtspr   SPRN_SRR0,r3
1061         mtspr   SPRN_SRR1,r4
1062         rfi
1063
1064 /*
1065  * void switch_mmu_context(struct mm_struct *prev, struct mm_struct *next);
1066  *
1067  * Set up the segment registers for a new context.
1068  */
1069 _ENTRY(switch_mmu_context)
1070         lwz     r3,MMCONTEXTID(r4)
1071         cmpwi   cr0,r3,0
1072         blt-    4f
1073         mulli   r3,r3,897       /* multiply context by skew factor */
1074         rlwinm  r3,r3,4,8,27    /* VSID = (context & 0xfffff) << 4 */
1075 #ifdef CONFIG_PPC_KUEP
1076         oris    r3, r3, SR_NX@h /* Set Nx */
1077 #endif
1078 #ifdef CONFIG_PPC_KUAP
1079         oris    r3, r3, SR_KS@h /* Set Ks */
1080 #endif
1081         li      r0,NUM_USER_SEGMENTS
1082         mtctr   r0
1083
1084 #ifdef CONFIG_BDI_SWITCH
1085         /* Context switch the PTE pointer for the Abatron BDI2000.
1086          * The PGDIR is passed as second argument.
1087          */
1088         lwz     r4, MM_PGD(r4)
1089         lis     r5, abatron_pteptrs@ha
1090         stw     r4, abatron_pteptrs@l + 0x4(r5)
1091 #endif
1092 BEGIN_MMU_FTR_SECTION
1093 #ifndef CONFIG_BDI_SWITCH
1094         lwz     r4, MM_PGD(r4)
1095 #endif
1096         tophys(r4, r4)
1097         rlwinm  r4, r4, 4, 0xffff01ff
1098         mtspr   SPRN_SDR1, r4
1099 END_MMU_FTR_SECTION_IFCLR(MMU_FTR_HPTE_TABLE)
1100         li      r4,0
1101         isync
1102 3:
1103         mtsrin  r3,r4
1104         addi    r3,r3,0x111     /* next VSID */
1105         rlwinm  r3,r3,0,8,3     /* clear out any overflow from VSID field */
1106         addis   r4,r4,0x1000    /* address of next segment */
1107         bdnz    3b
1108         sync
1109         isync
1110         blr
1111 4:      trap
1112         EMIT_BUG_ENTRY 4b,__FILE__,__LINE__,0
1113         blr
1114 EXPORT_SYMBOL(switch_mmu_context)
1115
1116 /*
1117  * An undocumented "feature" of 604e requires that the v bit
1118  * be cleared before changing BAT values.
1119  *
1120  * Also, newer IBM firmware does not clear bat3 and 4 so
1121  * this makes sure it's done.
1122  *  -- Cort
1123  */
1124 clear_bats:
1125         li      r10,0
1126
1127         mtspr   SPRN_DBAT0U,r10
1128         mtspr   SPRN_DBAT0L,r10
1129         mtspr   SPRN_DBAT1U,r10
1130         mtspr   SPRN_DBAT1L,r10
1131         mtspr   SPRN_DBAT2U,r10
1132         mtspr   SPRN_DBAT2L,r10
1133         mtspr   SPRN_DBAT3U,r10
1134         mtspr   SPRN_DBAT3L,r10
1135         mtspr   SPRN_IBAT0U,r10
1136         mtspr   SPRN_IBAT0L,r10
1137         mtspr   SPRN_IBAT1U,r10
1138         mtspr   SPRN_IBAT1L,r10
1139         mtspr   SPRN_IBAT2U,r10
1140         mtspr   SPRN_IBAT2L,r10
1141         mtspr   SPRN_IBAT3U,r10
1142         mtspr   SPRN_IBAT3L,r10
1143 BEGIN_MMU_FTR_SECTION
1144         /* Here's a tweak: at this point, CPU setup have
1145          * not been called yet, so HIGH_BAT_EN may not be
1146          * set in HID0 for the 745x processors. However, it
1147          * seems that doesn't affect our ability to actually
1148          * write to these SPRs.
1149          */
1150         mtspr   SPRN_DBAT4U,r10
1151         mtspr   SPRN_DBAT4L,r10
1152         mtspr   SPRN_DBAT5U,r10
1153         mtspr   SPRN_DBAT5L,r10
1154         mtspr   SPRN_DBAT6U,r10
1155         mtspr   SPRN_DBAT6L,r10
1156         mtspr   SPRN_DBAT7U,r10
1157         mtspr   SPRN_DBAT7L,r10
1158         mtspr   SPRN_IBAT4U,r10
1159         mtspr   SPRN_IBAT4L,r10
1160         mtspr   SPRN_IBAT5U,r10
1161         mtspr   SPRN_IBAT5L,r10
1162         mtspr   SPRN_IBAT6U,r10
1163         mtspr   SPRN_IBAT6L,r10
1164         mtspr   SPRN_IBAT7U,r10
1165         mtspr   SPRN_IBAT7L,r10
1166 END_MMU_FTR_SECTION_IFSET(MMU_FTR_USE_HIGH_BATS)
1167         blr
1168
1169 _ENTRY(update_bats)
1170         lis     r4, 1f@h
1171         ori     r4, r4, 1f@l
1172         tophys(r4, r4)
1173         mfmsr   r6
1174         mflr    r7
1175         li      r3, MSR_KERNEL & ~(MSR_IR | MSR_DR)
1176         rlwinm  r0, r6, 0, ~MSR_RI
1177         rlwinm  r0, r0, 0, ~MSR_EE
1178         mtmsr   r0
1179
1180         .align  4
1181         mtspr   SPRN_SRR0, r4
1182         mtspr   SPRN_SRR1, r3
1183         rfi
1184 1:      bl      clear_bats
1185         lis     r3, BATS@ha
1186         addi    r3, r3, BATS@l
1187         tophys(r3, r3)
1188         LOAD_BAT(0, r3, r4, r5)
1189         LOAD_BAT(1, r3, r4, r5)
1190         LOAD_BAT(2, r3, r4, r5)
1191         LOAD_BAT(3, r3, r4, r5)
1192 BEGIN_MMU_FTR_SECTION
1193         LOAD_BAT(4, r3, r4, r5)
1194         LOAD_BAT(5, r3, r4, r5)
1195         LOAD_BAT(6, r3, r4, r5)
1196         LOAD_BAT(7, r3, r4, r5)
1197 END_MMU_FTR_SECTION_IFSET(MMU_FTR_USE_HIGH_BATS)
1198         li      r3, MSR_KERNEL & ~(MSR_IR | MSR_DR | MSR_RI)
1199         mtmsr   r3
1200         mtspr   SPRN_SRR0, r7
1201         mtspr   SPRN_SRR1, r6
1202         rfi
1203
1204 flush_tlbs:
1205         lis     r10, 0x40
1206 1:      addic.  r10, r10, -0x1000
1207         tlbie   r10
1208         bgt     1b
1209         sync
1210         blr
1211
1212 mmu_off:
1213         addi    r4, r3, __after_mmu_off - _start
1214         mfmsr   r3
1215         andi.   r0,r3,MSR_DR|MSR_IR             /* MMU enabled? */
1216         beqlr
1217         andc    r3,r3,r0
1218
1219         .align  4
1220         mtspr   SPRN_SRR0,r4
1221         mtspr   SPRN_SRR1,r3
1222         sync
1223         rfi
1224
1225 /* We use one BAT to map up to 256M of RAM at _PAGE_OFFSET */
1226 initial_bats:
1227         lis     r11,PAGE_OFFSET@h
1228         tophys(r8,r11)
1229 #ifdef CONFIG_SMP
1230         ori     r8,r8,0x12              /* R/W access, M=1 */
1231 #else
1232         ori     r8,r8,2                 /* R/W access */
1233 #endif /* CONFIG_SMP */
1234         ori     r11,r11,BL_256M<<2|0x2  /* set up BAT registers for 604 */
1235
1236         mtspr   SPRN_DBAT0L,r8          /* N.B. 6xx have valid */
1237         mtspr   SPRN_DBAT0U,r11         /* bit in upper BAT register */
1238         mtspr   SPRN_IBAT0L,r8
1239         mtspr   SPRN_IBAT0U,r11
1240         isync
1241         blr
1242
1243 #ifdef CONFIG_BOOTX_TEXT
1244 setup_disp_bat:
1245         /*
1246          * setup the display bat prepared for us in prom.c
1247          */
1248         mflr    r8
1249         bl      reloc_offset
1250         mtlr    r8
1251         addis   r8,r3,disp_BAT@ha
1252         addi    r8,r8,disp_BAT@l
1253         cmpwi   cr0,r8,0
1254         beqlr
1255         lwz     r11,0(r8)
1256         lwz     r8,4(r8)
1257         mtspr   SPRN_DBAT3L,r8
1258         mtspr   SPRN_DBAT3U,r11
1259         blr
1260 #endif /* CONFIG_BOOTX_TEXT */
1261
1262 #ifdef CONFIG_PPC_EARLY_DEBUG_CPM
1263 setup_cpm_bat:
1264         lis     r8, 0xf000
1265         ori     r8, r8, 0x002a
1266         mtspr   SPRN_DBAT1L, r8
1267
1268         lis     r11, 0xf000
1269         ori     r11, r11, (BL_1M << 2) | 2
1270         mtspr   SPRN_DBAT1U, r11
1271
1272         blr
1273 #endif
1274
1275 #ifdef CONFIG_PPC_EARLY_DEBUG_USBGECKO
1276 setup_usbgecko_bat:
1277         /* prepare a BAT for early io */
1278 #if defined(CONFIG_GAMECUBE)
1279         lis     r8, 0x0c00
1280 #elif defined(CONFIG_WII)
1281         lis     r8, 0x0d00
1282 #else
1283 #error Invalid platform for USB Gecko based early debugging.
1284 #endif
1285         /*
1286          * The virtual address used must match the virtual address
1287          * associated to the fixmap entry FIX_EARLY_DEBUG_BASE.
1288          */
1289         lis     r11, 0xfffe     /* top 128K */
1290         ori     r8, r8, 0x002a  /* uncached, guarded ,rw */
1291         ori     r11, r11, 0x2   /* 128K, Vs=1, Vp=0 */
1292         mtspr   SPRN_DBAT1L, r8
1293         mtspr   SPRN_DBAT1U, r11
1294         blr
1295 #endif
1296
1297 #ifdef CONFIG_8260
1298 /* Jump into the system reset for the rom.
1299  * We first disable the MMU, and then jump to the ROM reset address.
1300  *
1301  * r3 is the board info structure, r4 is the location for starting.
1302  * I use this for building a small kernel that can load other kernels,
1303  * rather than trying to write or rely on a rom monitor that can tftp load.
1304  */
1305        .globl  m8260_gorom
1306 m8260_gorom:
1307         mfmsr   r0
1308         rlwinm  r0,r0,0,17,15   /* clear MSR_EE in r0 */
1309         sync
1310         mtmsr   r0
1311         sync
1312         mfspr   r11, SPRN_HID0
1313         lis     r10, 0
1314         ori     r10,r10,HID0_ICE|HID0_DCE
1315         andc    r11, r11, r10
1316         mtspr   SPRN_HID0, r11
1317         isync
1318         li      r5, MSR_ME|MSR_RI
1319         lis     r6,2f@h
1320         addis   r6,r6,-KERNELBASE@h
1321         ori     r6,r6,2f@l
1322         mtspr   SPRN_SRR0,r6
1323         mtspr   SPRN_SRR1,r5
1324         isync
1325         sync
1326         rfi
1327 2:
1328         mtlr    r4
1329         blr
1330 #endif
1331
1332
1333 /*
1334  * We put a few things here that have to be page-aligned.
1335  * This stuff goes at the beginning of the data segment,
1336  * which is page-aligned.
1337  */
1338         .data
1339         .globl  sdata
1340 sdata:
1341         .globl  empty_zero_page
1342 empty_zero_page:
1343         .space  4096
1344 EXPORT_SYMBOL(empty_zero_page)
1345
1346         .globl  swapper_pg_dir
1347 swapper_pg_dir:
1348         .space  PGD_TABLE_SIZE
1349
1350 /* Room for two PTE pointers, usually the kernel and current user pointers
1351  * to their respective root page table.
1352  */
1353 abatron_pteptrs:
1354         .space  8