powerpc: Don't use CURRENT_THREAD_INFO to find the stack
[sfrench/cifs-2.6.git] / arch / powerpc / kernel / misc_32.S
1 /*
2  * This file contains miscellaneous low-level functions.
3  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
4  *
5  * Largely rewritten by Cort Dougan (cort@cs.nmt.edu)
6  * and Paul Mackerras.
7  *
8  * kexec bits:
9  * Copyright (C) 2002-2003 Eric Biederman  <ebiederm@xmission.com>
10  * GameCube/ppc32 port Copyright (C) 2004 Albert Herranz
11  * PPC44x port. Copyright (C) 2011,  IBM Corporation
12  *              Author: Suzuki Poulose <suzuki@in.ibm.com>
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version
17  * 2 of the License, or (at your option) any later version.
18  *
19  */
20
21 #include <linux/sys.h>
22 #include <asm/unistd.h>
23 #include <asm/errno.h>
24 #include <asm/reg.h>
25 #include <asm/page.h>
26 #include <asm/cache.h>
27 #include <asm/cputable.h>
28 #include <asm/mmu.h>
29 #include <asm/ppc_asm.h>
30 #include <asm/thread_info.h>
31 #include <asm/asm-offsets.h>
32 #include <asm/processor.h>
33 #include <asm/kexec.h>
34 #include <asm/bug.h>
35 #include <asm/ptrace.h>
36 #include <asm/export.h>
37 #include <asm/feature-fixups.h>
38
39         .text
40
41 /*
42  * We store the saved ksp_limit in the unused part
43  * of the STACK_FRAME_OVERHEAD
44  */
45 _GLOBAL(call_do_softirq)
46         mflr    r0
47         stw     r0,4(r1)
48         lwz     r10,THREAD+KSP_LIMIT(r2)
49         addi    r11,r3,THREAD_INFO_GAP
50         stwu    r1,THREAD_SIZE-STACK_FRAME_OVERHEAD(r3)
51         mr      r1,r3
52         stw     r10,8(r1)
53         stw     r11,THREAD+KSP_LIMIT(r2)
54         bl      __do_softirq
55         lwz     r10,8(r1)
56         lwz     r1,0(r1)
57         lwz     r0,4(r1)
58         stw     r10,THREAD+KSP_LIMIT(r2)
59         mtlr    r0
60         blr
61
62 /*
63  * void call_do_irq(struct pt_regs *regs, void *sp);
64  */
65 _GLOBAL(call_do_irq)
66         mflr    r0
67         stw     r0,4(r1)
68         lwz     r10,THREAD+KSP_LIMIT(r2)
69         addi    r11,r4,THREAD_INFO_GAP
70         stwu    r1,THREAD_SIZE-STACK_FRAME_OVERHEAD(r4)
71         mr      r1,r4
72         stw     r10,8(r1)
73         stw     r11,THREAD+KSP_LIMIT(r2)
74         bl      __do_irq
75         lwz     r10,8(r1)
76         lwz     r1,0(r1)
77         lwz     r0,4(r1)
78         stw     r10,THREAD+KSP_LIMIT(r2)
79         mtlr    r0
80         blr
81
82 /*
83  * This returns the high 64 bits of the product of two 64-bit numbers.
84  */
85 _GLOBAL(mulhdu)
86         cmpwi   r6,0
87         cmpwi   cr1,r3,0
88         mr      r10,r4
89         mulhwu  r4,r4,r5
90         beq     1f
91         mulhwu  r0,r10,r6
92         mullw   r7,r10,r5
93         addc    r7,r0,r7
94         addze   r4,r4
95 1:      beqlr   cr1             /* all done if high part of A is 0 */
96         mullw   r9,r3,r5
97         mulhwu  r10,r3,r5
98         beq     2f
99         mullw   r0,r3,r6
100         mulhwu  r8,r3,r6
101         addc    r7,r0,r7
102         adde    r4,r4,r8
103         addze   r10,r10
104 2:      addc    r4,r4,r9
105         addze   r3,r10
106         blr
107
108 /*
109  * reloc_got2 runs through the .got2 section adding an offset
110  * to each entry.
111  */
112 _GLOBAL(reloc_got2)
113         mflr    r11
114         lis     r7,__got2_start@ha
115         addi    r7,r7,__got2_start@l
116         lis     r8,__got2_end@ha
117         addi    r8,r8,__got2_end@l
118         subf    r8,r7,r8
119         srwi.   r8,r8,2
120         beqlr
121         mtctr   r8
122         bl      1f
123 1:      mflr    r0
124         lis     r4,1b@ha
125         addi    r4,r4,1b@l
126         subf    r0,r4,r0
127         add     r7,r0,r7
128 2:      lwz     r0,0(r7)
129         add     r0,r0,r3
130         stw     r0,0(r7)
131         addi    r7,r7,4
132         bdnz    2b
133         mtlr    r11
134         blr
135
136 /*
137  * call_setup_cpu - call the setup_cpu function for this cpu
138  * r3 = data offset, r24 = cpu number
139  *
140  * Setup function is called with:
141  *   r3 = data offset
142  *   r4 = ptr to CPU spec (relocated)
143  */
144 _GLOBAL(call_setup_cpu)
145         addis   r4,r3,cur_cpu_spec@ha
146         addi    r4,r4,cur_cpu_spec@l
147         lwz     r4,0(r4)
148         add     r4,r4,r3
149         lwz     r5,CPU_SPEC_SETUP(r4)
150         cmpwi   0,r5,0
151         add     r5,r5,r3
152         beqlr
153         mtctr   r5
154         bctr
155
156 #if defined(CONFIG_CPU_FREQ_PMAC) && defined(CONFIG_PPC_BOOK3S_32)
157
158 /* This gets called by via-pmu.c to switch the PLL selection
159  * on 750fx CPU. This function should really be moved to some
160  * other place (as most of the cpufreq code in via-pmu
161  */
162 _GLOBAL(low_choose_750fx_pll)
163         /* Clear MSR:EE */
164         mfmsr   r7
165         rlwinm  r0,r7,0,17,15
166         mtmsr   r0
167
168         /* If switching to PLL1, disable HID0:BTIC */
169         cmplwi  cr0,r3,0
170         beq     1f
171         mfspr   r5,SPRN_HID0
172         rlwinm  r5,r5,0,27,25
173         sync
174         mtspr   SPRN_HID0,r5
175         isync
176         sync
177
178 1:
179         /* Calc new HID1 value */
180         mfspr   r4,SPRN_HID1    /* Build a HID1:PS bit from parameter */
181         rlwinm  r5,r3,16,15,15  /* Clear out HID1:PS from value read */
182         rlwinm  r4,r4,0,16,14   /* Could have I used rlwimi here ? */
183         or      r4,r4,r5
184         mtspr   SPRN_HID1,r4
185
186 #ifdef CONFIG_SMP
187         /* Store new HID1 image */
188         CURRENT_THREAD_INFO(r6, r1)
189         lwz     r6,TI_CPU(r6)
190         slwi    r6,r6,2
191 #else
192         li      r6, 0
193 #endif
194         addis   r6,r6,nap_save_hid1@ha
195         stw     r4,nap_save_hid1@l(r6)
196
197         /* If switching to PLL0, enable HID0:BTIC */
198         cmplwi  cr0,r3,0
199         bne     1f
200         mfspr   r5,SPRN_HID0
201         ori     r5,r5,HID0_BTIC
202         sync
203         mtspr   SPRN_HID0,r5
204         isync
205         sync
206
207 1:
208         /* Return */
209         mtmsr   r7
210         blr
211
212 _GLOBAL(low_choose_7447a_dfs)
213         /* Clear MSR:EE */
214         mfmsr   r7
215         rlwinm  r0,r7,0,17,15
216         mtmsr   r0
217         
218         /* Calc new HID1 value */
219         mfspr   r4,SPRN_HID1
220         insrwi  r4,r3,1,9       /* insert parameter into bit 9 */
221         sync
222         mtspr   SPRN_HID1,r4
223         sync
224         isync
225
226         /* Return */
227         mtmsr   r7
228         blr
229
230 #endif /* CONFIG_CPU_FREQ_PMAC && CONFIG_PPC_BOOK3S_32 */
231
232 /*
233  * complement mask on the msr then "or" some values on.
234  *     _nmask_and_or_msr(nmask, value_to_or)
235  */
236 _GLOBAL(_nmask_and_or_msr)
237         mfmsr   r0              /* Get current msr */
238         andc    r0,r0,r3        /* And off the bits set in r3 (first parm) */
239         or      r0,r0,r4        /* Or on the bits in r4 (second parm) */
240         SYNC                    /* Some chip revs have problems here... */
241         mtmsr   r0              /* Update machine state */
242         isync
243         blr                     /* Done */
244
245 #ifdef CONFIG_40x
246
247 /*
248  * Do an IO access in real mode
249  */
250 _GLOBAL(real_readb)
251         mfmsr   r7
252         rlwinm  r0,r7,0,~MSR_DR
253         sync
254         mtmsr   r0
255         sync
256         isync
257         lbz     r3,0(r3)
258         sync
259         mtmsr   r7
260         sync
261         isync
262         blr
263
264         /*
265  * Do an IO access in real mode
266  */
267 _GLOBAL(real_writeb)
268         mfmsr   r7
269         rlwinm  r0,r7,0,~MSR_DR
270         sync
271         mtmsr   r0
272         sync
273         isync
274         stb     r3,0(r4)
275         sync
276         mtmsr   r7
277         sync
278         isync
279         blr
280
281 #endif /* CONFIG_40x */
282
283
284 /*
285  * Flush instruction cache.
286  * This is a no-op on the 601.
287  */
288 #ifndef CONFIG_PPC_8xx
289 _GLOBAL(flush_instruction_cache)
290 #if defined(CONFIG_4xx)
291 #ifdef CONFIG_403GCX
292         li      r3, 512
293         mtctr   r3
294         lis     r4, KERNELBASE@h
295 1:      iccci   0, r4
296         addi    r4, r4, 16
297         bdnz    1b
298 #else
299         lis     r3, KERNELBASE@h
300         iccci   0,r3
301 #endif
302 #elif defined(CONFIG_FSL_BOOKE)
303 BEGIN_FTR_SECTION
304         mfspr   r3,SPRN_L1CSR0
305         ori     r3,r3,L1CSR0_CFI|L1CSR0_CLFC
306         /* msync; isync recommended here */
307         mtspr   SPRN_L1CSR0,r3
308         isync
309         blr
310 END_FTR_SECTION_IFSET(CPU_FTR_UNIFIED_ID_CACHE)
311         mfspr   r3,SPRN_L1CSR1
312         ori     r3,r3,L1CSR1_ICFI|L1CSR1_ICLFR
313         mtspr   SPRN_L1CSR1,r3
314 #else
315         mfspr   r3,SPRN_PVR
316         rlwinm  r3,r3,16,16,31
317         cmpwi   0,r3,1
318         beqlr                   /* for 601, do nothing */
319         /* 603/604 processor - use invalidate-all bit in HID0 */
320         mfspr   r3,SPRN_HID0
321         ori     r3,r3,HID0_ICFI
322         mtspr   SPRN_HID0,r3
323 #endif /* CONFIG_4xx */
324         isync
325         blr
326 EXPORT_SYMBOL(flush_instruction_cache)
327 #endif /* CONFIG_PPC_8xx */
328
329 /*
330  * Write any modified data cache blocks out to memory
331  * and invalidate the corresponding instruction cache blocks.
332  * This is a no-op on the 601.
333  *
334  * flush_icache_range(unsigned long start, unsigned long stop)
335  */
336 _GLOBAL(flush_icache_range)
337 BEGIN_FTR_SECTION
338         PURGE_PREFETCHED_INS
339         blr                             /* for 601, do nothing */
340 END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE)
341         rlwinm  r3,r3,0,0,31 - L1_CACHE_SHIFT
342         subf    r4,r3,r4
343         addi    r4,r4,L1_CACHE_BYTES - 1
344         srwi.   r4,r4,L1_CACHE_SHIFT
345         beqlr
346         mtctr   r4
347         mr      r6,r3
348 1:      dcbst   0,r3
349         addi    r3,r3,L1_CACHE_BYTES
350         bdnz    1b
351         sync                            /* wait for dcbst's to get to ram */
352 #ifndef CONFIG_44x
353         mtctr   r4
354 2:      icbi    0,r6
355         addi    r6,r6,L1_CACHE_BYTES
356         bdnz    2b
357 #else
358         /* Flash invalidate on 44x because we are passed kmapped addresses and
359            this doesn't work for userspace pages due to the virtually tagged
360            icache.  Sigh. */
361         iccci   0, r0
362 #endif
363         sync                            /* additional sync needed on g4 */
364         isync
365         blr
366 _ASM_NOKPROBE_SYMBOL(flush_icache_range)
367 EXPORT_SYMBOL(flush_icache_range)
368
369 /*
370  * Flush a particular page from the data cache to RAM.
371  * Note: this is necessary because the instruction cache does *not*
372  * snoop from the data cache.
373  * This is a no-op on the 601 which has a unified cache.
374  *
375  *      void __flush_dcache_icache(void *page)
376  */
377 _GLOBAL(__flush_dcache_icache)
378 BEGIN_FTR_SECTION
379         PURGE_PREFETCHED_INS
380         blr
381 END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE)
382         rlwinm  r3,r3,0,0,31-PAGE_SHIFT         /* Get page base address */
383         li      r4,PAGE_SIZE/L1_CACHE_BYTES     /* Number of lines in a page */
384         mtctr   r4
385         mr      r6,r3
386 0:      dcbst   0,r3                            /* Write line to ram */
387         addi    r3,r3,L1_CACHE_BYTES
388         bdnz    0b
389         sync
390 #ifdef CONFIG_44x
391         /* We don't flush the icache on 44x. Those have a virtual icache
392          * and we don't have access to the virtual address here (it's
393          * not the page vaddr but where it's mapped in user space). The
394          * flushing of the icache on these is handled elsewhere, when
395          * a change in the address space occurs, before returning to
396          * user space
397          */
398 BEGIN_MMU_FTR_SECTION
399         blr
400 END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_44x)
401 #endif /* CONFIG_44x */
402         mtctr   r4
403 1:      icbi    0,r6
404         addi    r6,r6,L1_CACHE_BYTES
405         bdnz    1b
406         sync
407         isync
408         blr
409
410 #ifndef CONFIG_BOOKE
411 /*
412  * Flush a particular page from the data cache to RAM, identified
413  * by its physical address.  We turn off the MMU so we can just use
414  * the physical address (this may be a highmem page without a kernel
415  * mapping).
416  *
417  *      void __flush_dcache_icache_phys(unsigned long physaddr)
418  */
419 _GLOBAL(__flush_dcache_icache_phys)
420 BEGIN_FTR_SECTION
421         PURGE_PREFETCHED_INS
422         blr                                     /* for 601, do nothing */
423 END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE)
424         mfmsr   r10
425         rlwinm  r0,r10,0,28,26                  /* clear DR */
426         mtmsr   r0
427         isync
428         rlwinm  r3,r3,0,0,31-PAGE_SHIFT         /* Get page base address */
429         li      r4,PAGE_SIZE/L1_CACHE_BYTES     /* Number of lines in a page */
430         mtctr   r4
431         mr      r6,r3
432 0:      dcbst   0,r3                            /* Write line to ram */
433         addi    r3,r3,L1_CACHE_BYTES
434         bdnz    0b
435         sync
436         mtctr   r4
437 1:      icbi    0,r6
438         addi    r6,r6,L1_CACHE_BYTES
439         bdnz    1b
440         sync
441         mtmsr   r10                             /* restore DR */
442         isync
443         blr
444 #endif /* CONFIG_BOOKE */
445
446 /*
447  * Copy a whole page.  We use the dcbz instruction on the destination
448  * to reduce memory traffic (it eliminates the unnecessary reads of
449  * the destination into cache).  This requires that the destination
450  * is cacheable.
451  */
452 #define COPY_16_BYTES           \
453         lwz     r6,4(r4);       \
454         lwz     r7,8(r4);       \
455         lwz     r8,12(r4);      \
456         lwzu    r9,16(r4);      \
457         stw     r6,4(r3);       \
458         stw     r7,8(r3);       \
459         stw     r8,12(r3);      \
460         stwu    r9,16(r3)
461
462 _GLOBAL(copy_page)
463         addi    r3,r3,-4
464         addi    r4,r4,-4
465
466         li      r5,4
467
468 #if MAX_COPY_PREFETCH > 1
469         li      r0,MAX_COPY_PREFETCH
470         li      r11,4
471         mtctr   r0
472 11:     dcbt    r11,r4
473         addi    r11,r11,L1_CACHE_BYTES
474         bdnz    11b
475 #else /* MAX_COPY_PREFETCH == 1 */
476         dcbt    r5,r4
477         li      r11,L1_CACHE_BYTES+4
478 #endif /* MAX_COPY_PREFETCH */
479         li      r0,PAGE_SIZE/L1_CACHE_BYTES - MAX_COPY_PREFETCH
480         crclr   4*cr0+eq
481 2:
482         mtctr   r0
483 1:
484         dcbt    r11,r4
485         dcbz    r5,r3
486         COPY_16_BYTES
487 #if L1_CACHE_BYTES >= 32
488         COPY_16_BYTES
489 #if L1_CACHE_BYTES >= 64
490         COPY_16_BYTES
491         COPY_16_BYTES
492 #if L1_CACHE_BYTES >= 128
493         COPY_16_BYTES
494         COPY_16_BYTES
495         COPY_16_BYTES
496         COPY_16_BYTES
497 #endif
498 #endif
499 #endif
500         bdnz    1b
501         beqlr
502         crnot   4*cr0+eq,4*cr0+eq
503         li      r0,MAX_COPY_PREFETCH
504         li      r11,4
505         b       2b
506 EXPORT_SYMBOL(copy_page)
507
508 /*
509  * Extended precision shifts.
510  *
511  * Updated to be valid for shift counts from 0 to 63 inclusive.
512  * -- Gabriel
513  *
514  * R3/R4 has 64 bit value
515  * R5    has shift count
516  * result in R3/R4
517  *
518  *  ashrdi3: arithmetic right shift (sign propagation)  
519  *  lshrdi3: logical right shift
520  *  ashldi3: left shift
521  */
522 _GLOBAL(__ashrdi3)
523         subfic  r6,r5,32
524         srw     r4,r4,r5        # LSW = count > 31 ? 0 : LSW >> count
525         addi    r7,r5,32        # could be xori, or addi with -32
526         slw     r6,r3,r6        # t1 = count > 31 ? 0 : MSW << (32-count)
527         rlwinm  r8,r7,0,32      # t3 = (count < 32) ? 32 : 0
528         sraw    r7,r3,r7        # t2 = MSW >> (count-32)
529         or      r4,r4,r6        # LSW |= t1
530         slw     r7,r7,r8        # t2 = (count < 32) ? 0 : t2
531         sraw    r3,r3,r5        # MSW = MSW >> count
532         or      r4,r4,r7        # LSW |= t2
533         blr
534 EXPORT_SYMBOL(__ashrdi3)
535
536 _GLOBAL(__ashldi3)
537         subfic  r6,r5,32
538         slw     r3,r3,r5        # MSW = count > 31 ? 0 : MSW << count
539         addi    r7,r5,32        # could be xori, or addi with -32
540         srw     r6,r4,r6        # t1 = count > 31 ? 0 : LSW >> (32-count)
541         slw     r7,r4,r7        # t2 = count < 32 ? 0 : LSW << (count-32)
542         or      r3,r3,r6        # MSW |= t1
543         slw     r4,r4,r5        # LSW = LSW << count
544         or      r3,r3,r7        # MSW |= t2
545         blr
546 EXPORT_SYMBOL(__ashldi3)
547
548 _GLOBAL(__lshrdi3)
549         subfic  r6,r5,32
550         srw     r4,r4,r5        # LSW = count > 31 ? 0 : LSW >> count
551         addi    r7,r5,32        # could be xori, or addi with -32
552         slw     r6,r3,r6        # t1 = count > 31 ? 0 : MSW << (32-count)
553         srw     r7,r3,r7        # t2 = count < 32 ? 0 : MSW >> (count-32)
554         or      r4,r4,r6        # LSW |= t1
555         srw     r3,r3,r5        # MSW = MSW >> count
556         or      r4,r4,r7        # LSW |= t2
557         blr
558 EXPORT_SYMBOL(__lshrdi3)
559
560 /*
561  * 64-bit comparison: __cmpdi2(s64 a, s64 b)
562  * Returns 0 if a < b, 1 if a == b, 2 if a > b.
563  */
564 _GLOBAL(__cmpdi2)
565         cmpw    r3,r5
566         li      r3,1
567         bne     1f
568         cmplw   r4,r6
569         beqlr
570 1:      li      r3,0
571         bltlr
572         li      r3,2
573         blr
574 EXPORT_SYMBOL(__cmpdi2)
575 /*
576  * 64-bit comparison: __ucmpdi2(u64 a, u64 b)
577  * Returns 0 if a < b, 1 if a == b, 2 if a > b.
578  */
579 _GLOBAL(__ucmpdi2)
580         cmplw   r3,r5
581         li      r3,1
582         bne     1f
583         cmplw   r4,r6
584         beqlr
585 1:      li      r3,0
586         bltlr
587         li      r3,2
588         blr
589 EXPORT_SYMBOL(__ucmpdi2)
590
591 _GLOBAL(__bswapdi2)
592         rotlwi  r9,r4,8
593         rotlwi  r10,r3,8
594         rlwimi  r9,r4,24,0,7
595         rlwimi  r10,r3,24,0,7
596         rlwimi  r9,r4,24,16,23
597         rlwimi  r10,r3,24,16,23
598         mr      r3,r9
599         mr      r4,r10
600         blr
601 EXPORT_SYMBOL(__bswapdi2)
602
603 #ifdef CONFIG_SMP
604 _GLOBAL(start_secondary_resume)
605         /* Reset stack */
606         rlwinm  r1, r1, 0, 0, 31 - THREAD_SHIFT
607         addi    r1,r1,THREAD_SIZE-STACK_FRAME_OVERHEAD
608         li      r3,0
609         stw     r3,0(r1)                /* Zero the stack frame pointer */
610         bl      start_secondary
611         b       .
612 #endif /* CONFIG_SMP */
613         
614 /*
615  * This routine is just here to keep GCC happy - sigh...
616  */
617 _GLOBAL(__main)
618         blr
619
620 #ifdef CONFIG_KEXEC_CORE
621         /*
622          * Must be relocatable PIC code callable as a C function.
623          */
624         .globl relocate_new_kernel
625 relocate_new_kernel:
626         /* r3 = page_list   */
627         /* r4 = reboot_code_buffer */
628         /* r5 = start_address      */
629
630 #ifdef CONFIG_FSL_BOOKE
631
632         mr      r29, r3
633         mr      r30, r4
634         mr      r31, r5
635
636 #define ENTRY_MAPPING_KEXEC_SETUP
637 #include "fsl_booke_entry_mapping.S"
638 #undef ENTRY_MAPPING_KEXEC_SETUP
639
640         mr      r3, r29
641         mr      r4, r30
642         mr      r5, r31
643
644         li      r0, 0
645 #elif defined(CONFIG_44x)
646
647         /* Save our parameters */
648         mr      r29, r3
649         mr      r30, r4
650         mr      r31, r5
651
652 #ifdef CONFIG_PPC_47x
653         /* Check for 47x cores */
654         mfspr   r3,SPRN_PVR
655         srwi    r3,r3,16
656         cmplwi  cr0,r3,PVR_476FPE@h
657         beq     setup_map_47x
658         cmplwi  cr0,r3,PVR_476@h
659         beq     setup_map_47x
660         cmplwi  cr0,r3,PVR_476_ISS@h
661         beq     setup_map_47x
662 #endif /* CONFIG_PPC_47x */
663         
664 /*
665  * Code for setting up 1:1 mapping for PPC440x for KEXEC
666  *
667  * We cannot switch off the MMU on PPC44x.
668  * So we:
669  * 1) Invalidate all the mappings except the one we are running from.
670  * 2) Create a tmp mapping for our code in the other address space(TS) and
671  *    jump to it. Invalidate the entry we started in.
672  * 3) Create a 1:1 mapping for 0-2GiB in chunks of 256M in original TS.
673  * 4) Jump to the 1:1 mapping in original TS.
674  * 5) Invalidate the tmp mapping.
675  *
676  * - Based on the kexec support code for FSL BookE
677  *
678  */
679
680         /* 
681          * Load the PID with kernel PID (0).
682          * Also load our MSR_IS and TID to MMUCR for TLB search.
683          */
684         li      r3, 0
685         mtspr   SPRN_PID, r3
686         mfmsr   r4
687         andi.   r4,r4,MSR_IS@l
688         beq     wmmucr
689         oris    r3,r3,PPC44x_MMUCR_STS@h
690 wmmucr:
691         mtspr   SPRN_MMUCR,r3
692         sync
693
694         /*
695          * Invalidate all the TLB entries except the current entry
696          * where we are running from
697          */
698         bl      0f                              /* Find our address */
699 0:      mflr    r5                              /* Make it accessible */
700         tlbsx   r23,0,r5                        /* Find entry we are in */
701         li      r4,0                            /* Start at TLB entry 0 */
702         li      r3,0                            /* Set PAGEID inval value */
703 1:      cmpw    r23,r4                          /* Is this our entry? */
704         beq     skip                            /* If so, skip the inval */
705         tlbwe   r3,r4,PPC44x_TLB_PAGEID         /* If not, inval the entry */
706 skip:
707         addi    r4,r4,1                         /* Increment */
708         cmpwi   r4,64                           /* Are we done? */
709         bne     1b                              /* If not, repeat */
710         isync
711
712         /* Create a temp mapping and jump to it */
713         andi.   r6, r23, 1              /* Find the index to use */
714         addi    r24, r6, 1              /* r24 will contain 1 or 2 */
715
716         mfmsr   r9                      /* get the MSR */
717         rlwinm  r5, r9, 27, 31, 31      /* Extract the MSR[IS] */
718         xori    r7, r5, 1               /* Use the other address space */
719
720         /* Read the current mapping entries */
721         tlbre   r3, r23, PPC44x_TLB_PAGEID
722         tlbre   r4, r23, PPC44x_TLB_XLAT
723         tlbre   r5, r23, PPC44x_TLB_ATTRIB
724
725         /* Save our current XLAT entry */
726         mr      r25, r4
727
728         /* Extract the TLB PageSize */
729         li      r10, 1                  /* r10 will hold PageSize */
730         rlwinm  r11, r3, 0, 24, 27      /* bits 24-27 */
731
732         /* XXX: As of now we use 256M, 4K pages */
733         cmpwi   r11, PPC44x_TLB_256M
734         bne     tlb_4k
735         rotlwi  r10, r10, 28            /* r10 = 256M */
736         b       write_out
737 tlb_4k:
738         cmpwi   r11, PPC44x_TLB_4K
739         bne     default
740         rotlwi  r10, r10, 12            /* r10 = 4K */
741         b       write_out
742 default:
743         rotlwi  r10, r10, 10            /* r10 = 1K */
744
745 write_out:
746         /*
747          * Write out the tmp 1:1 mapping for this code in other address space
748          * Fixup  EPN = RPN , TS=other address space
749          */
750         insrwi  r3, r7, 1, 23           /* Bit 23 is TS for PAGEID field */
751
752         /* Write out the tmp mapping entries */
753         tlbwe   r3, r24, PPC44x_TLB_PAGEID
754         tlbwe   r4, r24, PPC44x_TLB_XLAT
755         tlbwe   r5, r24, PPC44x_TLB_ATTRIB
756
757         subi    r11, r10, 1             /* PageOffset Mask = PageSize - 1 */
758         not     r10, r11                /* Mask for PageNum */
759
760         /* Switch to other address space in MSR */
761         insrwi  r9, r7, 1, 26           /* Set MSR[IS] = r7 */
762
763         bl      1f
764 1:      mflr    r8
765         addi    r8, r8, (2f-1b)         /* Find the target offset */
766
767         /* Jump to the tmp mapping */
768         mtspr   SPRN_SRR0, r8
769         mtspr   SPRN_SRR1, r9
770         rfi
771
772 2:
773         /* Invalidate the entry we were executing from */
774         li      r3, 0
775         tlbwe   r3, r23, PPC44x_TLB_PAGEID
776
777         /* attribute fields. rwx for SUPERVISOR mode */
778         li      r5, 0
779         ori     r5, r5, (PPC44x_TLB_SW | PPC44x_TLB_SR | PPC44x_TLB_SX | PPC44x_TLB_G)
780
781         /* Create 1:1 mapping in 256M pages */
782         xori    r7, r7, 1                       /* Revert back to Original TS */
783
784         li      r8, 0                           /* PageNumber */
785         li      r6, 3                           /* TLB Index, start at 3  */
786
787 next_tlb:
788         rotlwi  r3, r8, 28                      /* Create EPN (bits 0-3) */
789         mr      r4, r3                          /* RPN = EPN  */
790         ori     r3, r3, (PPC44x_TLB_VALID | PPC44x_TLB_256M) /* SIZE = 256M, Valid */
791         insrwi  r3, r7, 1, 23                   /* Set TS from r7 */
792
793         tlbwe   r3, r6, PPC44x_TLB_PAGEID       /* PageID field : EPN, V, SIZE */
794         tlbwe   r4, r6, PPC44x_TLB_XLAT         /* Address translation : RPN   */
795         tlbwe   r5, r6, PPC44x_TLB_ATTRIB       /* Attributes */
796
797         addi    r8, r8, 1                       /* Increment PN */
798         addi    r6, r6, 1                       /* Increment TLB Index */
799         cmpwi   r8, 8                           /* Are we done ? */
800         bne     next_tlb
801         isync
802
803         /* Jump to the new mapping 1:1 */
804         li      r9,0
805         insrwi  r9, r7, 1, 26                   /* Set MSR[IS] = r7 */
806
807         bl      1f
808 1:      mflr    r8
809         and     r8, r8, r11                     /* Get our offset within page */
810         addi    r8, r8, (2f-1b)
811
812         and     r5, r25, r10                    /* Get our target PageNum */
813         or      r8, r8, r5                      /* Target jump address */
814
815         mtspr   SPRN_SRR0, r8
816         mtspr   SPRN_SRR1, r9
817         rfi
818 2:
819         /* Invalidate the tmp entry we used */
820         li      r3, 0
821         tlbwe   r3, r24, PPC44x_TLB_PAGEID
822         sync
823         b       ppc44x_map_done
824
825 #ifdef CONFIG_PPC_47x
826
827         /* 1:1 mapping for 47x */
828
829 setup_map_47x:
830
831         /*
832          * Load the kernel pid (0) to PID and also to MMUCR[TID].
833          * Also set the MSR IS->MMUCR STS
834          */
835         li      r3, 0
836         mtspr   SPRN_PID, r3                    /* Set PID */
837         mfmsr   r4                              /* Get MSR */
838         andi.   r4, r4, MSR_IS@l                /* TS=1? */
839         beq     1f                              /* If not, leave STS=0 */
840         oris    r3, r3, PPC47x_MMUCR_STS@h      /* Set STS=1 */
841 1:      mtspr   SPRN_MMUCR, r3                  /* Put MMUCR */
842         sync
843
844         /* Find the entry we are running from */
845         bl      2f
846 2:      mflr    r23
847         tlbsx   r23, 0, r23
848         tlbre   r24, r23, 0                     /* TLB Word 0 */
849         tlbre   r25, r23, 1                     /* TLB Word 1 */
850         tlbre   r26, r23, 2                     /* TLB Word 2 */
851
852
853         /*
854          * Invalidates all the tlb entries by writing to 256 RPNs(r4)
855          * of 4k page size in all  4 ways (0-3 in r3).
856          * This would invalidate the entire UTLB including the one we are
857          * running from. However the shadow TLB entries would help us 
858          * to continue the execution, until we flush them (rfi/isync).
859          */
860         addis   r3, 0, 0x8000                   /* specify the way */
861         addi    r4, 0, 0                        /* TLB Word0 = (EPN=0, VALID = 0) */
862         addi    r5, 0, 0
863         b       clear_utlb_entry
864
865         /* Align the loop to speed things up. from head_44x.S */
866         .align  6
867
868 clear_utlb_entry:
869
870         tlbwe   r4, r3, 0
871         tlbwe   r5, r3, 1
872         tlbwe   r5, r3, 2
873         addis   r3, r3, 0x2000                  /* Increment the way */
874         cmpwi   r3, 0
875         bne     clear_utlb_entry
876         addis   r3, 0, 0x8000
877         addis   r4, r4, 0x100                   /* Increment the EPN */
878         cmpwi   r4, 0
879         bne     clear_utlb_entry
880
881         /* Create the entries in the other address space */
882         mfmsr   r5
883         rlwinm  r7, r5, 27, 31, 31              /* Get the TS (Bit 26) from MSR */
884         xori    r7, r7, 1                       /* r7 = !TS */
885
886         insrwi  r24, r7, 1, 21                  /* Change the TS in the saved TLB word 0 */
887
888         /* 
889          * write out the TLB entries for the tmp mapping
890          * Use way '0' so that we could easily invalidate it later.
891          */
892         lis     r3, 0x8000                      /* Way '0' */ 
893
894         tlbwe   r24, r3, 0
895         tlbwe   r25, r3, 1
896         tlbwe   r26, r3, 2
897
898         /* Update the msr to the new TS */
899         insrwi  r5, r7, 1, 26
900
901         bl      1f
902 1:      mflr    r6
903         addi    r6, r6, (2f-1b)
904
905         mtspr   SPRN_SRR0, r6
906         mtspr   SPRN_SRR1, r5
907         rfi
908
909         /* 
910          * Now we are in the tmp address space.
911          * Create a 1:1 mapping for 0-2GiB in the original TS.
912          */
913 2:
914         li      r3, 0
915         li      r4, 0                           /* TLB Word 0 */
916         li      r5, 0                           /* TLB Word 1 */
917         li      r6, 0
918         ori     r6, r6, PPC47x_TLB2_S_RWX       /* TLB word 2 */
919
920         li      r8, 0                           /* PageIndex */
921
922         xori    r7, r7, 1                       /* revert back to original TS */
923
924 write_utlb:
925         rotlwi  r5, r8, 28                      /* RPN = PageIndex * 256M */
926                                                 /* ERPN = 0 as we don't use memory above 2G */
927
928         mr      r4, r5                          /* EPN = RPN */
929         ori     r4, r4, (PPC47x_TLB0_VALID | PPC47x_TLB0_256M)
930         insrwi  r4, r7, 1, 21                   /* Insert the TS to Word 0 */
931
932         tlbwe   r4, r3, 0                       /* Write out the entries */
933         tlbwe   r5, r3, 1
934         tlbwe   r6, r3, 2
935         addi    r8, r8, 1
936         cmpwi   r8, 8                           /* Have we completed ? */
937         bne     write_utlb
938
939         /* make sure we complete the TLB write up */
940         isync
941
942         /* 
943          * Prepare to jump to the 1:1 mapping.
944          * 1) Extract page size of the tmp mapping
945          *    DSIZ = TLB_Word0[22:27]
946          * 2) Calculate the physical address of the address
947          *    to jump to.
948          */
949         rlwinm  r10, r24, 0, 22, 27
950
951         cmpwi   r10, PPC47x_TLB0_4K
952         bne     0f
953         li      r10, 0x1000                     /* r10 = 4k */
954         bl      1f
955
956 0:
957         /* Defaults to 256M */
958         lis     r10, 0x1000
959         
960         bl      1f
961 1:      mflr    r4
962         addi    r4, r4, (2f-1b)                 /* virtual address  of 2f */
963
964         subi    r11, r10, 1                     /* offsetmask = Pagesize - 1 */
965         not     r10, r11                        /* Pagemask = ~(offsetmask) */
966
967         and     r5, r25, r10                    /* Physical page */
968         and     r6, r4, r11                     /* offset within the current page */
969
970         or      r5, r5, r6                      /* Physical address for 2f */
971
972         /* Switch the TS in MSR to the original one */
973         mfmsr   r8
974         insrwi  r8, r7, 1, 26
975
976         mtspr   SPRN_SRR1, r8
977         mtspr   SPRN_SRR0, r5
978         rfi
979
980 2:
981         /* Invalidate the tmp mapping */
982         lis     r3, 0x8000                      /* Way '0' */
983
984         clrrwi  r24, r24, 12                    /* Clear the valid bit */
985         tlbwe   r24, r3, 0
986         tlbwe   r25, r3, 1
987         tlbwe   r26, r3, 2
988
989         /* Make sure we complete the TLB write and flush the shadow TLB */
990         isync
991
992 #endif
993
994 ppc44x_map_done:
995
996
997         /* Restore the parameters */
998         mr      r3, r29
999         mr      r4, r30
1000         mr      r5, r31
1001
1002         li      r0, 0
1003 #else
1004         li      r0, 0
1005
1006         /*
1007          * Set Machine Status Register to a known status,
1008          * switch the MMU off and jump to 1: in a single step.
1009          */
1010
1011         mr      r8, r0
1012         ori     r8, r8, MSR_RI|MSR_ME
1013         mtspr   SPRN_SRR1, r8
1014         addi    r8, r4, 1f - relocate_new_kernel
1015         mtspr   SPRN_SRR0, r8
1016         sync
1017         rfi
1018
1019 1:
1020 #endif
1021         /* from this point address translation is turned off */
1022         /* and interrupts are disabled */
1023
1024         /* set a new stack at the bottom of our page... */
1025         /* (not really needed now) */
1026         addi    r1, r4, KEXEC_CONTROL_PAGE_SIZE - 8 /* for LR Save+Back Chain */
1027         stw     r0, 0(r1)
1028
1029         /* Do the copies */
1030         li      r6, 0 /* checksum */
1031         mr      r0, r3
1032         b       1f
1033
1034 0:      /* top, read another word for the indirection page */
1035         lwzu    r0, 4(r3)
1036
1037 1:
1038         /* is it a destination page? (r8) */
1039         rlwinm. r7, r0, 0, 31, 31 /* IND_DESTINATION (1<<0) */
1040         beq     2f
1041
1042         rlwinm  r8, r0, 0, 0, 19 /* clear kexec flags, page align */
1043         b       0b
1044
1045 2:      /* is it an indirection page? (r3) */
1046         rlwinm. r7, r0, 0, 30, 30 /* IND_INDIRECTION (1<<1) */
1047         beq     2f
1048
1049         rlwinm  r3, r0, 0, 0, 19 /* clear kexec flags, page align */
1050         subi    r3, r3, 4
1051         b       0b
1052
1053 2:      /* are we done? */
1054         rlwinm. r7, r0, 0, 29, 29 /* IND_DONE (1<<2) */
1055         beq     2f
1056         b       3f
1057
1058 2:      /* is it a source page? (r9) */
1059         rlwinm. r7, r0, 0, 28, 28 /* IND_SOURCE (1<<3) */
1060         beq     0b
1061
1062         rlwinm  r9, r0, 0, 0, 19 /* clear kexec flags, page align */
1063
1064         li      r7, PAGE_SIZE / 4
1065         mtctr   r7
1066         subi    r9, r9, 4
1067         subi    r8, r8, 4
1068 9:
1069         lwzu    r0, 4(r9)  /* do the copy */
1070         xor     r6, r6, r0
1071         stwu    r0, 4(r8)
1072         dcbst   0, r8
1073         sync
1074         icbi    0, r8
1075         bdnz    9b
1076
1077         addi    r9, r9, 4
1078         addi    r8, r8, 4
1079         b       0b
1080
1081 3:
1082
1083         /* To be certain of avoiding problems with self-modifying code
1084          * execute a serializing instruction here.
1085          */
1086         isync
1087         sync
1088
1089         mfspr   r3, SPRN_PIR /* current core we are running on */
1090         mr      r4, r5 /* load physical address of chunk called */
1091
1092         /* jump to the entry point, usually the setup routine */
1093         mtlr    r5
1094         blrl
1095
1096 1:      b       1b
1097
1098 relocate_new_kernel_end:
1099
1100         .globl relocate_new_kernel_size
1101 relocate_new_kernel_size:
1102         .long relocate_new_kernel_end - relocate_new_kernel
1103 #endif