patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / arch / ppc / kernel / misc.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  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version
11  * 2 of the License, or (at your option) any later version.
12  *
13  */
14
15 #include <linux/config.h>
16 #include <linux/sys.h>
17 #include <asm/unistd.h>
18 #include <asm/errno.h>
19 #include <asm/processor.h>
20 #include <asm/page.h>
21 #include <asm/cache.h>
22 #include <asm/cputable.h>
23 #include <asm/mmu.h>
24 #include <asm/ppc_asm.h>
25 #include <asm/thread_info.h>
26 #include <asm/offsets.h>
27
28         .text
29
30         .align  5
31 _GLOBAL(__delay)
32         cmpwi   0,r3,0
33         mtctr   r3
34         beqlr
35 1:      bdnz    1b
36         blr
37
38 /*
39  * Returns (address we're running at) - (address we were linked at)
40  * for use before the text and data are mapped to KERNELBASE.
41  */
42 _GLOBAL(reloc_offset)
43         mflr    r0
44         bl      1f
45 1:      mflr    r3
46         lis     r4,1b@ha
47         addi    r4,r4,1b@l
48         subf    r3,r4,r3
49         mtlr    r0
50         blr
51
52 /*
53  * add_reloc_offset(x) returns x + reloc_offset().
54  */
55 _GLOBAL(add_reloc_offset)
56         mflr    r0
57         bl      1f
58 1:      mflr    r5
59         lis     r4,1b@ha
60         addi    r4,r4,1b@l
61         subf    r5,r4,r5
62         add     r3,r3,r5
63         mtlr    r0
64         blr
65
66 /*
67  * sub_reloc_offset(x) returns x - reloc_offset().
68  */
69 _GLOBAL(sub_reloc_offset)
70         mflr    r0
71         bl      1f
72 1:      mflr    r5
73         lis     r4,1b@ha
74         addi    r4,r4,1b@l
75         subf    r5,r4,r5
76         subf    r3,r5,r3
77         mtlr    r0
78         blr
79
80 /*
81  * reloc_got2 runs through the .got2 section adding an offset
82  * to each entry.
83  */
84 _GLOBAL(reloc_got2)
85         mflr    r11
86         lis     r7,__got2_start@ha
87         addi    r7,r7,__got2_start@l
88         lis     r8,__got2_end@ha
89         addi    r8,r8,__got2_end@l
90         subf    r8,r7,r8
91         srwi.   r8,r8,2
92         beqlr
93         mtctr   r8
94         bl      1f
95 1:      mflr    r0
96         lis     r4,1b@ha
97         addi    r4,r4,1b@l
98         subf    r0,r4,r0
99         add     r7,r0,r7
100 2:      lwz     r0,0(r7)
101         add     r0,r0,r3
102         stw     r0,0(r7)
103         addi    r7,r7,4
104         bdnz    2b
105         mtlr    r11
106         blr
107
108 /*
109  * identify_cpu,
110  * called with r3 = data offset and r4 = CPU number
111  * doesn't change r3
112  */
113 _GLOBAL(identify_cpu)
114         addis   r8,r3,cpu_specs@ha
115         addi    r8,r8,cpu_specs@l
116         mfpvr   r7
117 1:
118         lwz     r5,CPU_SPEC_PVR_MASK(r8)
119         and     r5,r5,r7
120         lwz     r6,CPU_SPEC_PVR_VALUE(r8)
121         cmplw   0,r6,r5
122         beq     1f
123         addi    r8,r8,CPU_SPEC_ENTRY_SIZE
124         b       1b
125 1:
126         addis   r6,r3,cur_cpu_spec@ha
127         addi    r6,r6,cur_cpu_spec@l
128         slwi    r4,r4,2
129         sub     r8,r8,r3
130         stwx    r8,r4,r6
131         blr
132
133 /*
134  * do_cpu_ftr_fixups - goes through the list of CPU feature fixups
135  * and writes nop's over sections of code that don't apply for this cpu.
136  * r3 = data offset (not changed)
137  */
138 _GLOBAL(do_cpu_ftr_fixups)
139         /* Get CPU 0 features */
140         addis   r6,r3,cur_cpu_spec@ha
141         addi    r6,r6,cur_cpu_spec@l
142         lwz     r4,0(r6)
143         add     r4,r4,r3
144         lwz     r4,CPU_SPEC_FEATURES(r4)
145
146         /* Get the fixup table */
147         addis   r6,r3,__start___ftr_fixup@ha
148         addi    r6,r6,__start___ftr_fixup@l
149         addis   r7,r3,__stop___ftr_fixup@ha
150         addi    r7,r7,__stop___ftr_fixup@l
151
152         /* Do the fixup */
153 1:      cmplw   0,r6,r7
154         bgelr
155         addi    r6,r6,16
156         lwz     r8,-16(r6)      /* mask */
157         and     r8,r8,r4
158         lwz     r9,-12(r6)      /* value */
159         cmplw   0,r8,r9
160         beq     1b
161         lwz     r8,-8(r6)       /* section begin */
162         lwz     r9,-4(r6)       /* section end */
163         subf.   r9,r8,r9
164         beq     1b
165         /* write nops over the section of code */
166         /* todo: if large section, add a branch at the start of it */
167         srwi    r9,r9,2
168         mtctr   r9
169         add     r8,r8,r3
170         lis     r0,0x60000000@h /* nop */
171 3:      stw     r0,0(r8)
172         andi.   r10,r4,CPU_FTR_SPLIT_ID_CACHE@l
173         beq     2f
174         dcbst   0,r8            /* suboptimal, but simpler */
175         sync
176         icbi    0,r8
177 2:      addi    r8,r8,4
178         bdnz    3b
179         sync                    /* additional sync needed on g4 */
180         isync
181         b       1b
182
183 /*
184  * call_setup_cpu - call the setup_cpu function for this cpu
185  * r3 = data offset, r24 = cpu number
186  *
187  * Setup function is called with:
188  *   r3 = data offset
189  *   r4 = CPU number
190  *   r5 = ptr to CPU spec (relocated)
191  */
192 _GLOBAL(call_setup_cpu)
193         addis   r5,r3,cur_cpu_spec@ha
194         addi    r5,r5,cur_cpu_spec@l
195         slwi    r4,r24,2
196         lwzx    r5,r4,r5
197         add     r5,r5,r3
198         lwz     r6,CPU_SPEC_SETUP(r5)
199         add     r6,r6,r3
200         mtctr   r6
201         mr      r4,r24
202         bctr
203
204 #if defined(CONFIG_CPU_FREQ_PMAC) && defined(CONFIG_6xx)
205
206 /* This gets called by via-pmu.c to switch the PLL selection
207  * on 750fx CPU. This function should really be moved to some
208  * other place (as most of the cpufreq code in via-pmu
209  */
210 _GLOBAL(low_choose_750fx_pll)
211         /* Clear MSR:EE */
212         mfmsr   r7
213         rlwinm  r0,r7,0,17,15
214         mtmsr   r0
215
216         /* If switching to PLL1, disable HID0:BTIC */
217         cmpli   cr0,r3,0
218         beq     1f
219         mfspr   r5,HID0
220         rlwinm  r5,r5,0,27,25
221         sync
222         mtspr   HID0,r5
223         isync
224         sync
225
226 1:
227         /* Calc new HID1 value */
228         mfspr   r4,SPRN_HID1    /* Build a HID1:PS bit from parameter */
229         rlwinm  r5,r3,16,15,15  /* Clear out HID1:PS from value read */
230         rlwinm  r4,r4,0,16,14   /* Could have I used rlwimi here ? */
231         or      r4,r4,r5
232         mtspr   SPRN_HID1,r4
233
234         /* Store new HID1 image */
235         rlwinm  r6,r1,0,0,18
236         lwz     r6,TI_CPU(r6)
237         slwi    r6,r6,2
238         addis   r6,r6,nap_save_hid1@ha
239         stw     r4,nap_save_hid1@l(r6)
240
241         /* If switching to PLL0, enable HID0:BTIC */
242         cmpli   cr0,r3,0
243         bne     1f
244         mfspr   r5,HID0
245         ori     r5,r5,HID0_BTIC
246         sync
247         mtspr   HID0,r5
248         isync
249         sync
250
251 1:
252         /* Return */
253         mtmsr   r7
254         blr
255
256 #endif /* CONFIG_CPU_FREQ_PMAC && CONFIG_6xx */
257
258 /* void local_save_flags_ptr(unsigned long *flags) */
259 _GLOBAL(local_save_flags_ptr)
260         mfmsr   r4
261         stw     r4,0(r3)
262         blr
263         /*
264          * Need these nops here for taking over save/restore to
265          * handle lost intrs
266          * -- Cort
267          */
268         nop
269         nop
270         nop
271         nop
272         nop
273         nop
274         nop
275         nop
276         nop
277         nop
278         nop
279         nop
280         nop
281         nop
282         nop
283         nop
284         nop
285 _GLOBAL(local_save_flags_ptr_end)
286
287 /* void local_irq_restore(unsigned long flags) */
288 _GLOBAL(local_irq_restore)
289 /*
290  * Just set/clear the MSR_EE bit through restore/flags but do not
291  * change anything else.  This is needed by the RT system and makes
292  * sense anyway.
293  *    -- Cort
294  */
295         mfmsr   r4
296         /* Copy all except the MSR_EE bit from r4 (current MSR value)
297            to r3.  This is the sort of thing the rlwimi instruction is
298            designed for.  -- paulus. */
299         rlwimi  r3,r4,0,17,15
300          /* Check if things are setup the way we want _already_. */
301         cmpw    0,r3,r4
302         beqlr
303 1:      SYNC
304         mtmsr   r3
305         SYNC
306         blr
307         nop
308         nop
309         nop
310         nop
311         nop
312         nop
313         nop
314         nop
315         nop
316         nop
317         nop
318         nop
319         nop
320         nop
321         nop
322         nop
323         nop
324         nop
325         nop
326 _GLOBAL(local_irq_restore_end)
327
328 _GLOBAL(local_irq_disable)
329         mfmsr   r0              /* Get current interrupt state */
330         rlwinm  r3,r0,16+1,32-1,31      /* Extract old value of 'EE' */
331         rlwinm  r0,r0,0,17,15   /* clear MSR_EE in r0 */
332         SYNC                    /* Some chip revs have problems here... */
333         mtmsr   r0              /* Update machine state */
334         blr                     /* Done */
335         /*
336          * Need these nops here for taking over save/restore to
337          * handle lost intrs
338          * -- Cort
339          */
340         nop
341         nop
342         nop
343         nop
344         nop
345         nop
346         nop
347         nop
348         nop
349         nop
350         nop
351         nop
352         nop
353         nop
354         nop
355 _GLOBAL(local_irq_disable_end)
356
357 _GLOBAL(local_irq_enable)
358         mfmsr   r3              /* Get current state */
359         ori     r3,r3,MSR_EE    /* Turn on 'EE' bit */
360         SYNC                    /* Some chip revs have problems here... */
361         mtmsr   r3              /* Update machine state */
362         blr
363         /*
364          * Need these nops here for taking over save/restore to
365          * handle lost intrs
366          * -- Cort
367          */
368         nop
369         nop
370         nop
371         nop
372         nop
373         nop
374         nop
375         nop
376         nop
377         nop
378         nop
379         nop
380         nop
381         nop
382         nop
383         nop
384 _GLOBAL(local_irq_enable_end)
385
386 /*
387  * complement mask on the msr then "or" some values on.
388  *     _nmask_and_or_msr(nmask, value_to_or)
389  */
390 _GLOBAL(_nmask_and_or_msr)
391         mfmsr   r0              /* Get current msr */
392         andc    r0,r0,r3        /* And off the bits set in r3 (first parm) */
393         or      r0,r0,r4        /* Or on the bits in r4 (second parm) */
394         SYNC                    /* Some chip revs have problems here... */
395         mtmsr   r0              /* Update machine state */
396         isync
397         blr                     /* Done */
398
399
400 /*
401  * Flush MMU TLB
402  */
403 _GLOBAL(_tlbia)
404 #if defined(CONFIG_40x)
405         sync                    /* Flush to memory before changing mapping */
406         tlbia
407         isync                   /* Flush shadow TLB */
408 #elif defined(CONFIG_44x)
409         li      r3,0
410         sync
411
412         /* Load high watermark */
413         lis     r4,tlb_44x_hwater@ha
414         lwz     r5,tlb_44x_hwater@l(r4)
415
416 1:      tlbwe   r3,r3,PPC44x_TLB_PAGEID
417         addi    r3,r3,1
418         cmpw    0,r3,r5
419         ble     1b
420
421         isync
422 #else /* !(CONFIG_40x || CONFIG_44x) */
423 #if defined(CONFIG_SMP)
424         rlwinm  r8,r1,0,0,18
425         lwz     r8,TI_CPU(r8)
426         oris    r8,r8,10
427         mfmsr   r10
428         SYNC
429         rlwinm  r0,r10,0,17,15          /* clear bit 16 (MSR_EE) */
430         rlwinm  r0,r0,0,28,26           /* clear DR */
431         mtmsr   r0
432         SYNC_601
433         isync
434         lis     r9,mmu_hash_lock@h
435         ori     r9,r9,mmu_hash_lock@l
436         tophys(r9,r9)
437 10:     lwarx   r7,0,r9
438         cmpi    0,r7,0
439         bne-    10b
440         stwcx.  r8,0,r9
441         bne-    10b
442         sync
443         tlbia
444         sync
445         TLBSYNC
446         li      r0,0
447         stw     r0,0(r9)                /* clear mmu_hash_lock */
448         mtmsr   r10
449         SYNC_601
450         isync
451 #else /* CONFIG_SMP */
452         sync
453         tlbia
454         sync
455 #endif /* CONFIG_SMP */
456 #endif /* ! defined(CONFIG_40x) */
457         blr
458
459 /*
460  * Flush MMU TLB for a particular address
461  */
462 _GLOBAL(_tlbie)
463 #if defined(CONFIG_40x)
464         tlbsx.  r3, 0, r3
465         bne     10f
466         sync
467         /* There are only 64 TLB entries, so r3 < 64, which means bit 25 is clear.
468          * Since 25 is the V bit in the TLB_TAG, loading this value will invalidate
469          * the TLB entry. */
470         tlbwe   r3, r3, TLB_TAG
471         isync
472 10:
473 #elif defined(CONFIG_44x)
474         mfspr   r4,SPRN_MMUCR
475         mfspr   r5,SPRN_PID                     /* Get PID */
476         rlwimi  r4,r5,0,24,31                   /* Set TID */
477         mtspr   SPRN_MMUCR,r4
478
479         tlbsx.  r3, 0, r3
480         bne     10f
481         sync
482         /* There are only 64 TLB entries, so r3 < 64,
483          * which means bit 22, is clear.  Since 22 is
484          * the V bit in the TLB_PAGEID, loading this
485          * value will invalidate the TLB entry.
486          */
487         tlbwe   r3, r3, PPC44x_TLB_PAGEID
488         isync
489 10:
490 #else /* !(CONFIG_40x || CONFIG_44x) */
491 #if defined(CONFIG_SMP)
492         rlwinm  r8,r1,0,0,18
493         lwz     r8,TI_CPU(r8)
494         oris    r8,r8,11
495         mfmsr   r10
496         SYNC
497         rlwinm  r0,r10,0,17,15          /* clear bit 16 (MSR_EE) */
498         rlwinm  r0,r0,0,28,26           /* clear DR */
499         mtmsr   r0
500         SYNC_601
501         isync
502         lis     r9,mmu_hash_lock@h
503         ori     r9,r9,mmu_hash_lock@l
504         tophys(r9,r9)
505 10:     lwarx   r7,0,r9
506         cmpi    0,r7,0
507         bne-    10b
508         stwcx.  r8,0,r9
509         bne-    10b
510         eieio
511         tlbie   r3
512         sync
513         TLBSYNC
514         li      r0,0
515         stw     r0,0(r9)                /* clear mmu_hash_lock */
516         mtmsr   r10
517         SYNC_601
518         isync
519 #else /* CONFIG_SMP */
520         tlbie   r3
521         sync
522 #endif /* CONFIG_SMP */
523 #endif /* ! CONFIG_40x */
524         blr
525
526 /*
527  * Flush instruction cache.
528  * This is a no-op on the 601.
529  */
530 _GLOBAL(flush_instruction_cache)
531 #if defined(CONFIG_8xx)
532         isync
533         lis     r5, IDC_INVALL@h
534         mtspr   IC_CST, r5
535 #elif defined(CONFIG_4xx)
536 #ifdef CONFIG_403GCX
537         li      r3, 512
538         mtctr   r3
539         lis     r4, KERNELBASE@h
540 1:      iccci   0, r4
541         addi    r4, r4, 16
542         bdnz    1b
543 #else
544         lis     r3, KERNELBASE@h
545         iccci   0,r3
546 #endif
547 #else
548         mfspr   r3,PVR
549         rlwinm  r3,r3,16,16,31
550         cmpi    0,r3,1
551         beqlr                   /* for 601, do nothing */
552         /* 603/604 processor - use invalidate-all bit in HID0 */
553         mfspr   r3,HID0
554         ori     r3,r3,HID0_ICFI
555         mtspr   HID0,r3
556 #endif /* CONFIG_8xx/4xx */
557         isync
558         blr
559
560 /*
561  * Write any modified data cache blocks out to memory
562  * and invalidate the corresponding instruction cache blocks.
563  * This is a no-op on the 601.
564  *
565  * flush_icache_range(unsigned long start, unsigned long stop)
566  */
567 _GLOBAL(flush_icache_range)
568         mfspr   r5,PVR
569         rlwinm  r5,r5,16,16,31
570         cmpi    0,r5,1
571         beqlr                           /* for 601, do nothing */
572         li      r5,L1_CACHE_LINE_SIZE-1
573         andc    r3,r3,r5
574         subf    r4,r3,r4
575         add     r4,r4,r5
576         srwi.   r4,r4,LG_L1_CACHE_LINE_SIZE
577         beqlr
578         mtctr   r4
579         mr      r6,r3
580 1:      dcbst   0,r3
581         addi    r3,r3,L1_CACHE_LINE_SIZE
582         bdnz    1b
583         sync                            /* wait for dcbst's to get to ram */
584         mtctr   r4
585 2:      icbi    0,r6
586         addi    r6,r6,L1_CACHE_LINE_SIZE
587         bdnz    2b
588         sync                            /* additional sync needed on g4 */
589         isync
590         blr
591 /*
592  * Write any modified data cache blocks out to memory.
593  * Does not invalidate the corresponding cache lines (especially for
594  * any corresponding instruction cache).
595  *
596  * clean_dcache_range(unsigned long start, unsigned long stop)
597  */
598 _GLOBAL(clean_dcache_range)
599         li      r5,L1_CACHE_LINE_SIZE-1
600         andc    r3,r3,r5
601         subf    r4,r3,r4
602         add     r4,r4,r5
603         srwi.   r4,r4,LG_L1_CACHE_LINE_SIZE
604         beqlr
605         mtctr   r4
606
607 1:      dcbst   0,r3
608         addi    r3,r3,L1_CACHE_LINE_SIZE
609         bdnz    1b
610         sync                            /* wait for dcbst's to get to ram */
611         blr
612
613 /*
614  * Write any modified data cache blocks out to memory and invalidate them.
615  * Does not invalidate the corresponding instruction cache blocks.
616  *
617  * flush_dcache_range(unsigned long start, unsigned long stop)
618  */
619 _GLOBAL(flush_dcache_range)
620         li      r5,L1_CACHE_LINE_SIZE-1
621         andc    r3,r3,r5
622         subf    r4,r3,r4
623         add     r4,r4,r5
624         srwi.   r4,r4,LG_L1_CACHE_LINE_SIZE
625         beqlr
626         mtctr   r4
627
628 1:      dcbf    0,r3
629         addi    r3,r3,L1_CACHE_LINE_SIZE
630         bdnz    1b
631         sync                            /* wait for dcbst's to get to ram */
632         blr
633
634 /*
635  * Like above, but invalidate the D-cache.  This is used by the 8xx
636  * to invalidate the cache so the PPC core doesn't get stale data
637  * from the CPM (no cache snooping here :-).
638  *
639  * invalidate_dcache_range(unsigned long start, unsigned long stop)
640  */
641 _GLOBAL(invalidate_dcache_range)
642         li      r5,L1_CACHE_LINE_SIZE-1
643         andc    r3,r3,r5
644         subf    r4,r3,r4
645         add     r4,r4,r5
646         srwi.   r4,r4,LG_L1_CACHE_LINE_SIZE
647         beqlr
648         mtctr   r4
649
650 1:      dcbi    0,r3
651         addi    r3,r3,L1_CACHE_LINE_SIZE
652         bdnz    1b
653         sync                            /* wait for dcbi's to get to ram */
654         blr
655
656 #ifdef CONFIG_NOT_COHERENT_CACHE
657 /*
658  * 40x cores have 8K or 16K dcache and 32 byte line size.
659  * 44x has a 32K dcache and 32 byte line size.
660  * 8xx has 1, 2, 4, 8K variants.
661  * For now, cover the worst case of the 44x.
662  * Must be called with external interrupts disabled.
663  */
664 #define CACHE_NWAYS     64
665 #define CACHE_NLINES    16
666
667 _GLOBAL(flush_dcache_all)
668         li      r4, (2 * CACHE_NWAYS * CACHE_NLINES)
669         mtctr   r4
670         lis     r5, KERNELBASE@h
671 1:      lwz     r3, 0(r5)               /* Load one word from every line */
672         addi    r5, r5, L1_CACHE_LINE_SIZE
673         bdnz    1b
674         blr
675 #endif /* CONFIG_NOT_COHERENT_CACHE */
676
677 /*
678  * Flush a particular page from the data cache to RAM.
679  * Note: this is necessary because the instruction cache does *not*
680  * snoop from the data cache.
681  * This is a no-op on the 601 which has a unified cache.
682  *
683  *      void __flush_dcache_icache(void *page)
684  */
685 _GLOBAL(__flush_dcache_icache)
686         mfspr   r5,PVR
687         rlwinm  r5,r5,16,16,31
688         cmpi    0,r5,1
689         beqlr                                   /* for 601, do nothing */
690         rlwinm  r3,r3,0,0,19                    /* Get page base address */
691         li      r4,4096/L1_CACHE_LINE_SIZE      /* Number of lines in a page */
692         mtctr   r4
693         mr      r6,r3
694 0:      dcbst   0,r3                            /* Write line to ram */
695         addi    r3,r3,L1_CACHE_LINE_SIZE
696         bdnz    0b
697         sync
698         mtctr   r4
699 1:      icbi    0,r6
700         addi    r6,r6,L1_CACHE_LINE_SIZE
701         bdnz    1b
702         sync
703         isync
704         blr
705
706 /*
707  * Flush a particular page from the data cache to RAM, identified
708  * by its physical address.  We turn off the MMU so we can just use
709  * the physical address (this may be a highmem page without a kernel
710  * mapping).
711  *
712  *      void __flush_dcache_icache_phys(unsigned long physaddr)
713  */
714 _GLOBAL(__flush_dcache_icache_phys)
715         mfspr   r5,PVR
716         rlwinm  r5,r5,16,16,31
717         cmpi    0,r5,1
718         beqlr                                   /* for 601, do nothing */
719         mfmsr   r10
720         rlwinm  r0,r10,0,28,26                  /* clear DR */
721         mtmsr   r0
722         isync
723         rlwinm  r3,r3,0,0,19                    /* Get page base address */
724         li      r4,4096/L1_CACHE_LINE_SIZE      /* Number of lines in a page */
725         mtctr   r4
726         mr      r6,r3
727 0:      dcbst   0,r3                            /* Write line to ram */
728         addi    r3,r3,L1_CACHE_LINE_SIZE
729         bdnz    0b
730         sync
731         mtctr   r4
732 1:      icbi    0,r6
733         addi    r6,r6,L1_CACHE_LINE_SIZE
734         bdnz    1b
735         sync
736         mtmsr   r10                             /* restore DR */
737         isync
738         blr
739
740 /*
741  * Clear pages using the dcbz instruction, which doesn't cause any
742  * memory traffic (except to write out any cache lines which get
743  * displaced).  This only works on cacheable memory.
744  *
745  * void clear_pages(void *page, int order) ;
746  */
747 _GLOBAL(clear_pages)
748         li      r0,4096/L1_CACHE_LINE_SIZE
749         slw     r0,r0,r4
750         mtctr   r0
751 #ifdef CONFIG_8xx
752         li      r4, 0
753 1:      stw     r4, 0(r3)
754         stw     r4, 4(r3)
755         stw     r4, 8(r3)
756         stw     r4, 12(r3)
757 #else
758 1:      dcbz    0,r3
759 #endif
760         addi    r3,r3,L1_CACHE_LINE_SIZE
761         bdnz    1b
762         blr
763
764 /*
765  * Copy a whole page.  We use the dcbz instruction on the destination
766  * to reduce memory traffic (it eliminates the unnecessary reads of
767  * the destination into cache).  This requires that the destination
768  * is cacheable.
769  */
770 #define COPY_16_BYTES           \
771         lwz     r6,4(r4);       \
772         lwz     r7,8(r4);       \
773         lwz     r8,12(r4);      \
774         lwzu    r9,16(r4);      \
775         stw     r6,4(r3);       \
776         stw     r7,8(r3);       \
777         stw     r8,12(r3);      \
778         stwu    r9,16(r3)
779
780 _GLOBAL(copy_page)
781         addi    r3,r3,-4
782         addi    r4,r4,-4
783
784 #ifdef CONFIG_8xx
785         /* don't use prefetch on 8xx */
786         li      r0,4096/L1_CACHE_LINE_SIZE
787         mtctr   r0
788 1:      COPY_16_BYTES
789         bdnz    1b
790         blr
791
792 #else   /* not 8xx, we can prefetch */
793         li      r5,4
794
795 #if MAX_COPY_PREFETCH > 1
796         li      r0,MAX_COPY_PREFETCH
797         li      r11,4
798         mtctr   r0
799 11:     dcbt    r11,r4
800         addi    r11,r11,L1_CACHE_LINE_SIZE
801         bdnz    11b
802 #else /* MAX_COPY_PREFETCH == 1 */
803         dcbt    r5,r4
804         li      r11,L1_CACHE_LINE_SIZE+4
805 #endif /* MAX_COPY_PREFETCH */
806         li      r0,4096/L1_CACHE_LINE_SIZE - MAX_COPY_PREFETCH
807         crclr   4*cr0+eq
808 2:
809         mtctr   r0
810 1:
811         dcbt    r11,r4
812         dcbz    r5,r3
813         COPY_16_BYTES
814 #if L1_CACHE_LINE_SIZE >= 32
815         COPY_16_BYTES
816 #if L1_CACHE_LINE_SIZE >= 64
817         COPY_16_BYTES
818         COPY_16_BYTES
819 #if L1_CACHE_LINE_SIZE >= 128
820         COPY_16_BYTES
821         COPY_16_BYTES
822         COPY_16_BYTES
823         COPY_16_BYTES
824 #endif
825 #endif
826 #endif
827         bdnz    1b
828         beqlr
829         crnot   4*cr0+eq,4*cr0+eq
830         li      r0,MAX_COPY_PREFETCH
831         li      r11,4
832         b       2b
833 #endif  /* CONFIG_8xx */
834
835 /*
836  * void atomic_clear_mask(atomic_t mask, atomic_t *addr)
837  * void atomic_set_mask(atomic_t mask, atomic_t *addr);
838  */
839 _GLOBAL(atomic_clear_mask)
840 10:     lwarx   r5,0,r4
841         andc    r5,r5,r3
842         PPC405_ERR77(0,r4)
843         stwcx.  r5,0,r4
844         bne-    10b
845         blr
846 _GLOBAL(atomic_set_mask)
847 10:     lwarx   r5,0,r4
848         or      r5,r5,r3
849         PPC405_ERR77(0,r4)
850         stwcx.  r5,0,r4
851         bne-    10b
852         blr
853
854 /*
855  * I/O string operations
856  *
857  * insb(port, buf, len)
858  * outsb(port, buf, len)
859  * insw(port, buf, len)
860  * outsw(port, buf, len)
861  * insl(port, buf, len)
862  * outsl(port, buf, len)
863  * insw_ns(port, buf, len)
864  * outsw_ns(port, buf, len)
865  * insl_ns(port, buf, len)
866  * outsl_ns(port, buf, len)
867  *
868  * The *_ns versions don't do byte-swapping.
869  */
870 _GLOBAL(_insb)
871         cmpwi   0,r5,0
872         mtctr   r5
873         subi    r4,r4,1
874         blelr-
875 00:     lbz     r5,0(r3)
876         eieio
877         stbu    r5,1(r4)
878         bdnz    00b
879         blr
880
881 _GLOBAL(_outsb)
882         cmpwi   0,r5,0
883         mtctr   r5
884         subi    r4,r4,1
885         blelr-
886 00:     lbzu    r5,1(r4)
887         stb     r5,0(r3)
888         eieio
889         bdnz    00b
890         blr
891
892 _GLOBAL(_insw)
893         cmpwi   0,r5,0
894         mtctr   r5
895         subi    r4,r4,2
896         blelr-
897 00:     lhbrx   r5,0,r3
898         eieio
899         sthu    r5,2(r4)
900         bdnz    00b
901         blr
902
903 _GLOBAL(_outsw)
904         cmpwi   0,r5,0
905         mtctr   r5
906         subi    r4,r4,2
907         blelr-
908 00:     lhzu    r5,2(r4)
909         eieio
910         sthbrx  r5,0,r3
911         bdnz    00b
912         blr
913
914 _GLOBAL(_insl)
915         cmpwi   0,r5,0
916         mtctr   r5
917         subi    r4,r4,4
918         blelr-
919 00:     lwbrx   r5,0,r3
920         eieio
921         stwu    r5,4(r4)
922         bdnz    00b
923         blr
924
925 _GLOBAL(_outsl)
926         cmpwi   0,r5,0
927         mtctr   r5
928         subi    r4,r4,4
929         blelr-
930 00:     lwzu    r5,4(r4)
931         stwbrx  r5,0,r3
932         eieio
933         bdnz    00b
934         blr
935
936 _GLOBAL(__ide_mm_insw)
937 _GLOBAL(_insw_ns)
938         cmpwi   0,r5,0
939         mtctr   r5
940         subi    r4,r4,2
941         blelr-
942 00:     lhz     r5,0(r3)
943         eieio
944         sthu    r5,2(r4)
945         bdnz    00b
946         blr
947
948 _GLOBAL(__ide_mm_outsw)
949 _GLOBAL(_outsw_ns)
950         cmpwi   0,r5,0
951         mtctr   r5
952         subi    r4,r4,2
953         blelr-
954 00:     lhzu    r5,2(r4)
955         sth     r5,0(r3)
956         eieio
957         bdnz    00b
958         blr
959
960 _GLOBAL(__ide_mm_insl)
961 _GLOBAL(_insl_ns)
962         cmpwi   0,r5,0
963         mtctr   r5
964         subi    r4,r4,4
965         blelr-
966 00:     lwz     r5,0(r3)
967         eieio
968         stwu    r5,4(r4)
969         bdnz    00b
970         blr
971
972 _GLOBAL(__ide_mm_outsl)
973 _GLOBAL(_outsl_ns)
974         cmpwi   0,r5,0
975         mtctr   r5
976         subi    r4,r4,4
977         blelr-
978 00:     lwzu    r5,4(r4)
979         stw     r5,0(r3)
980         eieio
981         bdnz    00b
982         blr
983
984 /*
985  * Extended precision shifts.
986  *
987  * Updated to be valid for shift counts from 0 to 63 inclusive.
988  * -- Gabriel
989  *
990  * R3/R4 has 64 bit value
991  * R5    has shift count
992  * result in R3/R4
993  *
994  *  ashrdi3: arithmetic right shift (sign propagation)  
995  *  lshrdi3: logical right shift
996  *  ashldi3: left shift
997  */
998 _GLOBAL(__ashrdi3)
999         subfic  r6,r5,32
1000         srw     r4,r4,r5        # LSW = count > 31 ? 0 : LSW >> count
1001         addi    r7,r5,32        # could be xori, or addi with -32
1002         slw     r6,r3,r6        # t1 = count > 31 ? 0 : MSW << (32-count)
1003         rlwinm  r8,r7,0,32      # t3 = (count < 32) ? 32 : 0
1004         sraw    r7,r3,r7        # t2 = MSW >> (count-32)
1005         or      r4,r4,r6        # LSW |= t1
1006         slw     r7,r7,r8        # t2 = (count < 32) ? 0 : t2
1007         sraw    r3,r3,r5        # MSW = MSW >> count
1008         or      r4,r4,r7        # LSW |= t2
1009         blr
1010
1011 _GLOBAL(__ashldi3)
1012         subfic  r6,r5,32
1013         slw     r3,r3,r5        # MSW = count > 31 ? 0 : MSW << count
1014         addi    r7,r5,32        # could be xori, or addi with -32
1015         srw     r6,r4,r6        # t1 = count > 31 ? 0 : LSW >> (32-count)
1016         slw     r7,r4,r7        # t2 = count < 32 ? 0 : LSW << (count-32)
1017         or      r3,r3,r6        # MSW |= t1
1018         slw     r4,r4,r5        # LSW = LSW << count
1019         or      r3,r3,r7        # MSW |= t2
1020         blr
1021
1022 _GLOBAL(__lshrdi3)
1023         subfic  r6,r5,32
1024         srw     r4,r4,r5        # LSW = count > 31 ? 0 : LSW >> count
1025         addi    r7,r5,32        # could be xori, or addi with -32
1026         slw     r6,r3,r6        # t1 = count > 31 ? 0 : MSW << (32-count)
1027         srw     r7,r3,r7        # t2 = count < 32 ? 0 : MSW >> (count-32)
1028         or      r4,r4,r6        # LSW |= t1
1029         srw     r3,r3,r5        # MSW = MSW >> count
1030         or      r4,r4,r7        # LSW |= t2
1031         blr
1032
1033 _GLOBAL(abs)
1034         srawi   r4,r3,31
1035         xor     r3,r3,r4
1036         sub     r3,r3,r4
1037         blr
1038
1039 _GLOBAL(_get_SP)
1040         mr      r3,r1           /* Close enough */
1041         blr
1042
1043 /*
1044  * These are used in the alignment trap handler when emulating
1045  * single-precision loads and stores.
1046  * We restore and save the fpscr so the task gets the same result
1047  * and exceptions as if the cpu had performed the load or store.
1048  */
1049
1050 #if defined(CONFIG_4xx)
1051 _GLOBAL(cvt_fd)
1052         lfs     0,0(r3)
1053         stfd    0,0(r4)
1054         blr
1055
1056 _GLOBAL(cvt_df)
1057         lfd     0,0(r3)
1058         stfs    0,0(r4)
1059         blr
1060 #else
1061 _GLOBAL(cvt_fd)
1062         lfd     0,-4(r5)        /* load up fpscr value */
1063         mtfsf   0xff,0
1064         lfs     0,0(r3)
1065         stfd    0,0(r4)
1066         mffs    0               /* save new fpscr value */
1067         stfd    0,-4(r5)
1068         blr
1069
1070 _GLOBAL(cvt_df)
1071         lfd     0,-4(r5)        /* load up fpscr value */
1072         mtfsf   0xff,0
1073         lfd     0,0(r3)
1074         stfs    0,0(r4)
1075         mffs    0               /* save new fpscr value */
1076         stfd    0,-4(r5)
1077         blr
1078 #endif
1079
1080 /*
1081  * Create a kernel thread
1082  *   kernel_thread(fn, arg, flags)
1083  */
1084 _GLOBAL(kernel_thread)
1085         stwu    r1,-16(r1)
1086         stw     r30,8(r1)
1087         stw     r31,12(r1)
1088         mr      r30,r3          /* function */
1089         mr      r31,r4          /* argument */
1090         ori     r3,r5,CLONE_VM  /* flags */
1091         oris    r3,r3,CLONE_UNTRACED>>16
1092         li      r4,0            /* new sp (unused) */
1093         li      r0,__NR_clone
1094         sc
1095         cmpi    0,r3,0          /* parent or child? */
1096         bne     1f              /* return if parent */
1097         li      r0,0            /* make top-level stack frame */
1098         stwu    r0,-16(r1)
1099         mtlr    r30             /* fn addr in lr */
1100         mr      r3,r31          /* load arg and call fn */
1101         blrl
1102         li      r0,__NR_exit    /* exit if function returns */
1103         li      r3,0
1104         sc
1105 1:      lwz     r30,8(r1)
1106         lwz     r31,12(r1)
1107         addi    r1,r1,16
1108         blr
1109
1110 /*
1111  * This routine is just here to keep GCC happy - sigh...
1112  */
1113 _GLOBAL(__main)
1114         blr
1115
1116 #define SYSCALL(name) \
1117 _GLOBAL(name) \
1118         li      r0,__NR_##name; \
1119         sc; \
1120         bnslr; \
1121         lis     r4,errno@ha; \
1122         stw     r3,errno@l(r4); \
1123         li      r3,-1; \
1124         blr
1125
1126 SYSCALL(execve)
1127
1128 /* Why isn't this a) automatic, b) written in 'C'? */
1129         .data
1130         .align 4
1131 _GLOBAL(sys_call_table)
1132         .long sys_restart_syscall /* 0 */
1133         .long sys_exit
1134         .long ppc_fork
1135         .long sys_read
1136         .long sys_write
1137         .long sys_open          /* 5 */
1138         .long sys_close
1139         .long sys_waitpid
1140         .long sys_creat
1141         .long sys_link
1142         .long sys_unlink        /* 10 */
1143         .long sys_execve
1144         .long sys_chdir
1145         .long sys_time
1146         .long sys_mknod
1147         .long sys_chmod         /* 15 */
1148         .long sys_lchown
1149         .long sys_ni_syscall                    /* old break syscall holder */
1150         .long sys_stat
1151         .long sys_lseek
1152         .long sys_getpid        /* 20 */
1153         .long sys_mount
1154         .long sys_oldumount
1155         .long sys_setuid
1156         .long sys_getuid
1157         .long sys_stime         /* 25 */
1158         .long sys_ptrace
1159         .long sys_alarm
1160         .long sys_fstat
1161         .long sys_pause
1162         .long sys_utime         /* 30 */
1163         .long sys_ni_syscall                    /* old stty syscall holder */
1164         .long sys_ni_syscall                    /* old gtty syscall holder */
1165         .long sys_access
1166         .long sys_nice
1167         .long sys_ni_syscall    /* 35 */        /* old ftime syscall holder */
1168         .long sys_sync
1169         .long sys_kill
1170         .long sys_rename
1171         .long sys_mkdir
1172         .long sys_rmdir         /* 40 */
1173         .long sys_dup
1174         .long sys_pipe
1175         .long sys_times
1176         .long sys_ni_syscall                    /* old prof syscall holder */
1177         .long sys_brk           /* 45 */
1178         .long sys_setgid
1179         .long sys_getgid
1180         .long sys_signal
1181         .long sys_geteuid
1182         .long sys_getegid       /* 50 */
1183         .long sys_acct
1184         .long sys_umount                        /* recycled never used phys() */
1185         .long sys_ni_syscall                    /* old lock syscall holder */
1186         .long sys_ioctl
1187         .long sys_fcntl         /* 55 */
1188         .long sys_ni_syscall                    /* old mpx syscall holder */
1189         .long sys_setpgid
1190         .long sys_ni_syscall                    /* old ulimit syscall holder */
1191         .long sys_olduname
1192         .long sys_umask         /* 60 */
1193         .long sys_chroot
1194         .long sys_ustat
1195         .long sys_dup2
1196         .long sys_getppid
1197         .long sys_getpgrp       /* 65 */
1198         .long sys_setsid
1199         .long sys_sigaction
1200         .long sys_sgetmask
1201         .long sys_ssetmask
1202         .long sys_setreuid      /* 70 */
1203         .long sys_setregid
1204         .long ppc_sigsuspend
1205         .long sys_sigpending
1206         .long sys_sethostname
1207         .long sys_setrlimit     /* 75 */
1208         .long sys_old_getrlimit
1209         .long sys_getrusage
1210         .long sys_gettimeofday
1211         .long sys_settimeofday
1212         .long sys_getgroups     /* 80 */
1213         .long sys_setgroups
1214         .long ppc_select
1215         .long sys_symlink
1216         .long sys_lstat
1217         .long sys_readlink      /* 85 */
1218         .long sys_uselib
1219         .long sys_swapon
1220         .long sys_reboot
1221         .long old_readdir
1222         .long sys_mmap          /* 90 */
1223         .long sys_munmap
1224         .long sys_truncate
1225         .long sys_ftruncate
1226         .long sys_fchmod
1227         .long sys_fchown        /* 95 */
1228         .long sys_getpriority
1229         .long sys_setpriority
1230         .long sys_ni_syscall                    /* old profil syscall holder */
1231         .long sys_statfs
1232         .long sys_fstatfs       /* 100 */
1233         .long sys_ni_syscall
1234         .long sys_socketcall
1235         .long sys_syslog
1236         .long sys_setitimer
1237         .long sys_getitimer     /* 105 */
1238         .long sys_newstat
1239         .long sys_newlstat
1240         .long sys_newfstat
1241         .long sys_uname
1242         .long sys_ni_syscall    /* 110 */
1243         .long sys_vhangup
1244         .long sys_ni_syscall    /* old 'idle' syscall */
1245         .long sys_ni_syscall
1246         .long sys_wait4
1247         .long sys_swapoff       /* 115 */
1248         .long sys_sysinfo
1249         .long sys_ipc
1250         .long sys_fsync
1251         .long sys_sigreturn
1252         .long ppc_clone         /* 120 */
1253         .long sys_setdomainname
1254         .long sys_newuname
1255         .long sys_ni_syscall
1256         .long sys_adjtimex
1257         .long sys_mprotect      /* 125 */
1258         .long sys_sigprocmask
1259         .long sys_ni_syscall    /* old sys_create_module */
1260         .long sys_init_module
1261         .long sys_delete_module
1262         .long sys_ni_syscall    /* old sys_get_kernel_syms */   /* 130 */
1263         .long sys_quotactl
1264         .long sys_getpgid
1265         .long sys_fchdir
1266         .long sys_bdflush
1267         .long sys_sysfs         /* 135 */
1268         .long sys_personality
1269         .long sys_ni_syscall    /* for afs_syscall */
1270         .long sys_setfsuid
1271         .long sys_setfsgid
1272         .long sys_llseek        /* 140 */
1273         .long sys_getdents
1274         .long ppc_select
1275         .long sys_flock
1276         .long sys_msync
1277         .long sys_readv         /* 145 */
1278         .long sys_writev
1279         .long sys_getsid
1280         .long sys_fdatasync
1281         .long sys_sysctl
1282         .long sys_mlock         /* 150 */
1283         .long sys_munlock
1284         .long sys_mlockall
1285         .long sys_munlockall
1286         .long sys_sched_setparam
1287         .long sys_sched_getparam        /* 155 */
1288         .long sys_sched_setscheduler
1289         .long sys_sched_getscheduler
1290         .long sys_sched_yield
1291         .long sys_sched_get_priority_max
1292         .long sys_sched_get_priority_min  /* 160 */
1293         .long sys_sched_rr_get_interval
1294         .long sys_nanosleep
1295         .long sys_mremap
1296         .long sys_setresuid
1297         .long sys_getresuid     /* 165 */
1298         .long sys_ni_syscall            /* old sys_query_module */
1299         .long sys_poll
1300         .long sys_nfsservctl
1301         .long sys_setresgid
1302         .long sys_getresgid     /* 170 */
1303         .long sys_prctl
1304         .long sys_rt_sigreturn
1305         .long sys_rt_sigaction
1306         .long sys_rt_sigprocmask
1307         .long sys_rt_sigpending /* 175 */
1308         .long sys_rt_sigtimedwait
1309         .long sys_rt_sigqueueinfo
1310         .long ppc_rt_sigsuspend
1311         .long sys_pread64
1312         .long sys_pwrite64      /* 180 */
1313         .long sys_chown
1314         .long sys_getcwd
1315         .long sys_capget
1316         .long sys_capset
1317         .long sys_sigaltstack   /* 185 */
1318         .long sys_sendfile
1319         .long sys_ni_syscall            /* streams1 */
1320         .long sys_ni_syscall            /* streams2 */
1321         .long ppc_vfork
1322         .long sys_getrlimit     /* 190 */
1323         .long sys_readahead
1324         .long sys_mmap2
1325         .long sys_truncate64
1326         .long sys_ftruncate64
1327         .long sys_stat64        /* 195 */
1328         .long sys_lstat64
1329         .long sys_fstat64
1330         .long sys_pciconfig_read
1331         .long sys_pciconfig_write
1332         .long sys_pciconfig_iobase      /* 200 */
1333         .long sys_ni_syscall            /* 201 - reserved - MacOnLinux - new */
1334         .long sys_getdents64
1335         .long sys_pivot_root
1336         .long sys_fcntl64
1337         .long sys_madvise       /* 205 */
1338         .long sys_mincore
1339         .long sys_gettid
1340         .long sys_tkill
1341         .long sys_setxattr
1342         .long sys_lsetxattr     /* 210 */
1343         .long sys_fsetxattr
1344         .long sys_getxattr
1345         .long sys_lgetxattr
1346         .long sys_fgetxattr
1347         .long sys_listxattr     /* 215 */
1348         .long sys_llistxattr
1349         .long sys_flistxattr
1350         .long sys_removexattr
1351         .long sys_lremovexattr
1352         .long sys_fremovexattr  /* 220 */
1353         .long sys_futex
1354         .long sys_sched_setaffinity
1355         .long sys_sched_getaffinity
1356         .long sys_ni_syscall
1357         .long sys_ni_syscall    /* 225 - reserved for Tux */
1358         .long sys_sendfile64
1359         .long sys_io_setup
1360         .long sys_io_destroy
1361         .long sys_io_getevents
1362         .long sys_io_submit     /* 230 */
1363         .long sys_io_cancel
1364         .long sys_set_tid_address
1365         .long sys_fadvise64
1366         .long sys_exit_group
1367         .long sys_lookup_dcookie /* 235 */
1368         .long sys_epoll_create
1369         .long sys_epoll_ctl
1370         .long sys_epoll_wait
1371         .long sys_remap_file_pages
1372         .long sys_timer_create  /* 240 */
1373         .long sys_timer_settime
1374         .long sys_timer_gettime
1375         .long sys_timer_getoverrun
1376         .long sys_timer_delete
1377         .long sys_clock_settime /* 245 */
1378         .long sys_clock_gettime
1379         .long sys_clock_getres
1380         .long sys_clock_nanosleep
1381         .long ppc_swapcontext
1382         .long sys_tgkill        /* 250 */
1383         .long sys_utimes
1384         .long sys_statfs64
1385         .long sys_fstatfs64
1386         .long ppc_fadvise64_64
1387         .long sys_ni_syscall            /* 255 - rtas (used on ppc64) */
1388         .long sys_ni_syscall            /* 256 reserved for sys_debug_setcontext */
1389         .long sys_vserver
1390         .long sys_ni_syscall            /* 258 reserved for new sys_remap_file_pages */
1391         .long sys_ni_syscall            /* 259 reserved for new sys_mbind */
1392         .long sys_ni_syscall            /* 260 reserved for new sys_get_mempolicy */
1393         .long sys_ni_syscall            /* 261 reserved for new sys_set_mempolicy */
1394         .long sys_mq_open
1395         .long sys_mq_unlink
1396         .long sys_mq_timedsend
1397         .long sys_mq_timedreceive       /* 265 */
1398         .long sys_mq_notify
1399         .long sys_mq_getsetattr
1400         .long sys_ni_syscall            /* 268 reserved for sys_kexec_load */