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