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