8a7238dd2f4b7df93eccfdc4ac6b0b4b1a69b2d1
[kvmfornfv.git] / kernel / arch / powerpc / kernel / misc_64.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  * Adapted for iSeries by Mike Corrigan (mikejc@us.ibm.com)
8  * PPC64 updates by Dave Engebretsen (engebret@us.ibm.com)
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version
13  * 2 of the License, or (at your option) any later version.
14  *
15  */
16
17 #include <linux/sys.h>
18 #include <asm/unistd.h>
19 #include <asm/errno.h>
20 #include <asm/processor.h>
21 #include <asm/page.h>
22 #include <asm/cache.h>
23 #include <asm/ppc_asm.h>
24 #include <asm/asm-offsets.h>
25 #include <asm/cputable.h>
26 #include <asm/thread_info.h>
27 #include <asm/kexec.h>
28 #include <asm/ptrace.h>
29
30         .text
31
32 #ifndef CONFIG_PREEMPT_RT_FULL
33 _GLOBAL(call_do_softirq)
34         mflr    r0
35         std     r0,16(r1)
36         stdu    r1,THREAD_SIZE-STACK_FRAME_OVERHEAD(r3)
37         mr      r1,r3
38         bl      __do_softirq
39         ld      r1,0(r1)
40         ld      r0,16(r1)
41         mtlr    r0
42         blr
43 #endif
44
45 _GLOBAL(call_do_irq)
46         mflr    r0
47         std     r0,16(r1)
48         stdu    r1,THREAD_SIZE-STACK_FRAME_OVERHEAD(r4)
49         mr      r1,r4
50         bl      __do_irq
51         ld      r1,0(r1)
52         ld      r0,16(r1)
53         mtlr    r0
54         blr
55
56         .section        ".toc","aw"
57 PPC64_CACHES:
58         .tc             ppc64_caches[TC],ppc64_caches
59         .section        ".text"
60
61 /*
62  * Write any modified data cache blocks out to memory
63  * and invalidate the corresponding instruction cache blocks.
64  *
65  * flush_icache_range(unsigned long start, unsigned long stop)
66  *
67  *   flush all bytes from start through stop-1 inclusive
68  */
69
70 _KPROBE(flush_icache_range)
71 BEGIN_FTR_SECTION
72         PURGE_PREFETCHED_INS
73         blr
74 END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE)
75 /*
76  * Flush the data cache to memory 
77  * 
78  * Different systems have different cache line sizes
79  * and in some cases i-cache and d-cache line sizes differ from
80  * each other.
81  */
82         ld      r10,PPC64_CACHES@toc(r2)
83         lwz     r7,DCACHEL1LINESIZE(r10)/* Get cache line size */
84         addi    r5,r7,-1
85         andc    r6,r3,r5                /* round low to line bdy */
86         subf    r8,r6,r4                /* compute length */
87         add     r8,r8,r5                /* ensure we get enough */
88         lwz     r9,DCACHEL1LOGLINESIZE(r10)     /* Get log-2 of cache line size */
89         srw.    r8,r8,r9                /* compute line count */
90         beqlr                           /* nothing to do? */
91         mtctr   r8
92 1:      dcbst   0,r6
93         add     r6,r6,r7
94         bdnz    1b
95         sync
96
97 /* Now invalidate the instruction cache */
98         
99         lwz     r7,ICACHEL1LINESIZE(r10)        /* Get Icache line size */
100         addi    r5,r7,-1
101         andc    r6,r3,r5                /* round low to line bdy */
102         subf    r8,r6,r4                /* compute length */
103         add     r8,r8,r5
104         lwz     r9,ICACHEL1LOGLINESIZE(r10)     /* Get log-2 of Icache line size */
105         srw.    r8,r8,r9                /* compute line count */
106         beqlr                           /* nothing to do? */
107         mtctr   r8
108 2:      icbi    0,r6
109         add     r6,r6,r7
110         bdnz    2b
111         isync
112         blr
113         .previous .text
114 /*
115  * Like above, but only do the D-cache.
116  *
117  * flush_dcache_range(unsigned long start, unsigned long stop)
118  *
119  *    flush all bytes from start to stop-1 inclusive
120  */
121 _GLOBAL(flush_dcache_range)
122
123 /*
124  * Flush the data cache to memory 
125  * 
126  * Different systems have different cache line sizes
127  */
128         ld      r10,PPC64_CACHES@toc(r2)
129         lwz     r7,DCACHEL1LINESIZE(r10)        /* Get dcache line size */
130         addi    r5,r7,-1
131         andc    r6,r3,r5                /* round low to line bdy */
132         subf    r8,r6,r4                /* compute length */
133         add     r8,r8,r5                /* ensure we get enough */
134         lwz     r9,DCACHEL1LOGLINESIZE(r10)     /* Get log-2 of dcache line size */
135         srw.    r8,r8,r9                /* compute line count */
136         beqlr                           /* nothing to do? */
137         mtctr   r8
138 0:      dcbst   0,r6
139         add     r6,r6,r7
140         bdnz    0b
141         sync
142         blr
143
144 /*
145  * Like above, but works on non-mapped physical addresses.
146  * Use only for non-LPAR setups ! It also assumes real mode
147  * is cacheable. Used for flushing out the DART before using
148  * it as uncacheable memory 
149  *
150  * flush_dcache_phys_range(unsigned long start, unsigned long stop)
151  *
152  *    flush all bytes from start to stop-1 inclusive
153  */
154 _GLOBAL(flush_dcache_phys_range)
155         ld      r10,PPC64_CACHES@toc(r2)
156         lwz     r7,DCACHEL1LINESIZE(r10)        /* Get dcache line size */
157         addi    r5,r7,-1
158         andc    r6,r3,r5                /* round low to line bdy */
159         subf    r8,r6,r4                /* compute length */
160         add     r8,r8,r5                /* ensure we get enough */
161         lwz     r9,DCACHEL1LOGLINESIZE(r10)     /* Get log-2 of dcache line size */
162         srw.    r8,r8,r9                /* compute line count */
163         beqlr                           /* nothing to do? */
164         mfmsr   r5                      /* Disable MMU Data Relocation */
165         ori     r0,r5,MSR_DR
166         xori    r0,r0,MSR_DR
167         sync
168         mtmsr   r0
169         sync
170         isync
171         mtctr   r8
172 0:      dcbst   0,r6
173         add     r6,r6,r7
174         bdnz    0b
175         sync
176         isync
177         mtmsr   r5                      /* Re-enable MMU Data Relocation */
178         sync
179         isync
180         blr
181
182 _GLOBAL(flush_inval_dcache_range)
183         ld      r10,PPC64_CACHES@toc(r2)
184         lwz     r7,DCACHEL1LINESIZE(r10)        /* Get dcache line size */
185         addi    r5,r7,-1
186         andc    r6,r3,r5                /* round low to line bdy */
187         subf    r8,r6,r4                /* compute length */
188         add     r8,r8,r5                /* ensure we get enough */
189         lwz     r9,DCACHEL1LOGLINESIZE(r10)/* Get log-2 of dcache line size */
190         srw.    r8,r8,r9                /* compute line count */
191         beqlr                           /* nothing to do? */
192         sync
193         isync
194         mtctr   r8
195 0:      dcbf    0,r6
196         add     r6,r6,r7
197         bdnz    0b
198         sync
199         isync
200         blr
201
202
203 /*
204  * Flush a particular page from the data cache to RAM.
205  * Note: this is necessary because the instruction cache does *not*
206  * snoop from the data cache.
207  *
208  *      void __flush_dcache_icache(void *page)
209  */
210 _GLOBAL(__flush_dcache_icache)
211 /*
212  * Flush the data cache to memory 
213  * 
214  * Different systems have different cache line sizes
215  */
216
217 BEGIN_FTR_SECTION
218         PURGE_PREFETCHED_INS
219         blr
220 END_FTR_SECTION_IFSET(CPU_FTR_COHERENT_ICACHE)
221
222 /* Flush the dcache */
223         ld      r7,PPC64_CACHES@toc(r2)
224         clrrdi  r3,r3,PAGE_SHIFT                    /* Page align */
225         lwz     r4,DCACHEL1LINESPERPAGE(r7)     /* Get # dcache lines per page */
226         lwz     r5,DCACHEL1LINESIZE(r7)         /* Get dcache line size */
227         mr      r6,r3
228         mtctr   r4
229 0:      dcbst   0,r6
230         add     r6,r6,r5
231         bdnz    0b
232         sync
233
234 /* Now invalidate the icache */ 
235
236         lwz     r4,ICACHEL1LINESPERPAGE(r7)     /* Get # icache lines per page */
237         lwz     r5,ICACHEL1LINESIZE(r7)         /* Get icache line size */
238         mtctr   r4
239 1:      icbi    0,r3
240         add     r3,r3,r5
241         bdnz    1b
242         isync
243         blr
244
245 _GLOBAL(__bswapdi2)
246         srdi    r8,r3,32
247         rlwinm  r7,r3,8,0xffffffff
248         rlwimi  r7,r3,24,0,7
249         rlwinm  r9,r8,8,0xffffffff
250         rlwimi  r7,r3,24,16,23
251         rlwimi  r9,r8,24,0,7
252         rlwimi  r9,r8,24,16,23
253         sldi    r7,r7,32
254         or      r3,r7,r9
255         blr
256
257
258 #ifdef CONFIG_PPC_EARLY_DEBUG_BOOTX
259 _GLOBAL(rmci_on)
260         sync
261         isync
262         li      r3,0x100
263         rldicl  r3,r3,32,0
264         mfspr   r5,SPRN_HID4
265         or      r5,r5,r3
266         sync
267         mtspr   SPRN_HID4,r5
268         isync
269         slbia
270         isync
271         sync
272         blr
273
274 _GLOBAL(rmci_off)
275         sync
276         isync
277         li      r3,0x100
278         rldicl  r3,r3,32,0
279         mfspr   r5,SPRN_HID4
280         andc    r5,r5,r3
281         sync
282         mtspr   SPRN_HID4,r5
283         isync
284         slbia
285         isync
286         sync
287         blr
288 #endif /* CONFIG_PPC_EARLY_DEBUG_BOOTX */
289
290 #if defined(CONFIG_PPC_PMAC) || defined(CONFIG_PPC_MAPLE)
291
292 /*
293  * Do an IO access in real mode
294  */
295 _GLOBAL(real_readb)
296         mfmsr   r7
297         ori     r0,r7,MSR_DR
298         xori    r0,r0,MSR_DR
299         sync
300         mtmsrd  r0
301         sync
302         isync
303         mfspr   r6,SPRN_HID4
304         rldicl  r5,r6,32,0
305         ori     r5,r5,0x100
306         rldicl  r5,r5,32,0
307         sync
308         mtspr   SPRN_HID4,r5
309         isync
310         slbia
311         isync
312         lbz     r3,0(r3)
313         sync
314         mtspr   SPRN_HID4,r6
315         isync
316         slbia
317         isync
318         mtmsrd  r7
319         sync
320         isync
321         blr
322
323         /*
324  * Do an IO access in real mode
325  */
326 _GLOBAL(real_writeb)
327         mfmsr   r7
328         ori     r0,r7,MSR_DR
329         xori    r0,r0,MSR_DR
330         sync
331         mtmsrd  r0
332         sync
333         isync
334         mfspr   r6,SPRN_HID4
335         rldicl  r5,r6,32,0
336         ori     r5,r5,0x100
337         rldicl  r5,r5,32,0
338         sync
339         mtspr   SPRN_HID4,r5
340         isync
341         slbia
342         isync
343         stb     r3,0(r4)
344         sync
345         mtspr   SPRN_HID4,r6
346         isync
347         slbia
348         isync
349         mtmsrd  r7
350         sync
351         isync
352         blr
353 #endif /* defined(CONFIG_PPC_PMAC) || defined(CONFIG_PPC_MAPLE) */
354
355 #ifdef CONFIG_PPC_PASEMI
356
357 _GLOBAL(real_205_readb)
358         mfmsr   r7
359         ori     r0,r7,MSR_DR
360         xori    r0,r0,MSR_DR
361         sync
362         mtmsrd  r0
363         sync
364         isync
365         LBZCIX(R3,R0,R3)
366         isync
367         mtmsrd  r7
368         sync
369         isync
370         blr
371
372 _GLOBAL(real_205_writeb)
373         mfmsr   r7
374         ori     r0,r7,MSR_DR
375         xori    r0,r0,MSR_DR
376         sync
377         mtmsrd  r0
378         sync
379         isync
380         STBCIX(R3,R0,R4)
381         isync
382         mtmsrd  r7
383         sync
384         isync
385         blr
386
387 #endif /* CONFIG_PPC_PASEMI */
388
389
390 #if defined(CONFIG_CPU_FREQ_PMAC64) || defined(CONFIG_CPU_FREQ_MAPLE)
391 /*
392  * SCOM access functions for 970 (FX only for now)
393  *
394  * unsigned long scom970_read(unsigned int address);
395  * void scom970_write(unsigned int address, unsigned long value);
396  *
397  * The address passed in is the 24 bits register address. This code
398  * is 970 specific and will not check the status bits, so you should
399  * know what you are doing.
400  */
401 _GLOBAL(scom970_read)
402         /* interrupts off */
403         mfmsr   r4
404         ori     r0,r4,MSR_EE
405         xori    r0,r0,MSR_EE
406         mtmsrd  r0,1
407
408         /* rotate 24 bits SCOM address 8 bits left and mask out it's low 8 bits
409          * (including parity). On current CPUs they must be 0'd,
410          * and finally or in RW bit
411          */
412         rlwinm  r3,r3,8,0,15
413         ori     r3,r3,0x8000
414
415         /* do the actual scom read */
416         sync
417         mtspr   SPRN_SCOMC,r3
418         isync
419         mfspr   r3,SPRN_SCOMD
420         isync
421         mfspr   r0,SPRN_SCOMC
422         isync
423
424         /* XXX: fixup result on some buggy 970's (ouch ! we lost a bit, bah
425          * that's the best we can do). Not implemented yet as we don't use
426          * the scom on any of the bogus CPUs yet, but may have to be done
427          * ultimately
428          */
429
430         /* restore interrupts */
431         mtmsrd  r4,1
432         blr
433
434
435 _GLOBAL(scom970_write)
436         /* interrupts off */
437         mfmsr   r5
438         ori     r0,r5,MSR_EE
439         xori    r0,r0,MSR_EE
440         mtmsrd  r0,1
441
442         /* rotate 24 bits SCOM address 8 bits left and mask out it's low 8 bits
443          * (including parity). On current CPUs they must be 0'd.
444          */
445
446         rlwinm  r3,r3,8,0,15
447
448         sync
449         mtspr   SPRN_SCOMD,r4      /* write data */
450         isync
451         mtspr   SPRN_SCOMC,r3      /* write command */
452         isync
453         mfspr   3,SPRN_SCOMC
454         isync
455
456         /* restore interrupts */
457         mtmsrd  r5,1
458         blr
459 #endif /* CONFIG_CPU_FREQ_PMAC64 || CONFIG_CPU_FREQ_MAPLE */
460
461 /* kexec_wait(phys_cpu)
462  *
463  * wait for the flag to change, indicating this kernel is going away but
464  * the slave code for the next one is at addresses 0 to 100.
465  *
466  * This is used by all slaves, even those that did not find a matching
467  * paca in the secondary startup code.
468  *
469  * Physical (hardware) cpu id should be in r3.
470  */
471 _GLOBAL(kexec_wait)
472         bl      1f
473 1:      mflr    r5
474         addi    r5,r5,kexec_flag-1b
475
476 99:     HMT_LOW
477 #ifdef CONFIG_KEXEC             /* use no memory without kexec */
478         lwz     r4,0(r5)
479         cmpwi   0,r4,0
480         bnea    0x60
481 #endif
482         b       99b
483
484 /* this can be in text because we won't change it until we are
485  * running in real anyways
486  */
487 kexec_flag:
488         .long   0
489
490
491 #ifdef CONFIG_KEXEC
492
493 /* kexec_smp_wait(void)
494  *
495  * call with interrupts off
496  * note: this is a terminal routine, it does not save lr
497  *
498  * get phys id from paca
499  * switch to real mode
500  * mark the paca as no longer used
501  * join other cpus in kexec_wait(phys_id)
502  */
503 _GLOBAL(kexec_smp_wait)
504         lhz     r3,PACAHWCPUID(r13)
505         bl      real_mode
506
507         li      r4,KEXEC_STATE_REAL_MODE
508         stb     r4,PACAKEXECSTATE(r13)
509         SYNC
510
511         b       kexec_wait
512
513 /*
514  * switch to real mode (turn mmu off)
515  * we use the early kernel trick that the hardware ignores bits
516  * 0 and 1 (big endian) of the effective address in real mode
517  *
518  * don't overwrite r3 here, it is live for kexec_wait above.
519  */
520 real_mode:      /* assume normal blr return */
521 1:      li      r9,MSR_RI
522         li      r10,MSR_DR|MSR_IR
523         mflr    r11             /* return address to SRR0 */
524         mfmsr   r12
525         andc    r9,r12,r9
526         andc    r10,r12,r10
527
528         mtmsrd  r9,1
529         mtspr   SPRN_SRR1,r10
530         mtspr   SPRN_SRR0,r11
531         rfid
532
533
534 /*
535  * kexec_sequence(newstack, start, image, control, clear_all())
536  *
537  * does the grungy work with stack switching and real mode switches
538  * also does simple calls to other code
539  */
540
541 _GLOBAL(kexec_sequence)
542         mflr    r0
543         std     r0,16(r1)
544
545         /* switch stacks to newstack -- &kexec_stack.stack */
546         stdu    r1,THREAD_SIZE-STACK_FRAME_OVERHEAD(r3)
547         mr      r1,r3
548
549         li      r0,0
550         std     r0,16(r1)
551
552         /* save regs for local vars on new stack.
553          * yes, we won't go back, but ...
554          */
555         std     r31,-8(r1)
556         std     r30,-16(r1)
557         std     r29,-24(r1)
558         std     r28,-32(r1)
559         std     r27,-40(r1)
560         std     r26,-48(r1)
561         std     r25,-56(r1)
562
563         stdu    r1,-STACK_FRAME_OVERHEAD-64(r1)
564
565         /* save args into preserved regs */
566         mr      r31,r3                  /* newstack (both) */
567         mr      r30,r4                  /* start (real) */
568         mr      r29,r5                  /* image (virt) */
569         mr      r28,r6                  /* control, unused */
570         mr      r27,r7                  /* clear_all() fn desc */
571         mr      r26,r8                  /* spare */
572         lhz     r25,PACAHWCPUID(r13)    /* get our phys cpu from paca */
573
574         /* disable interrupts, we are overwriting kernel data next */
575         mfmsr   r3
576         rlwinm  r3,r3,0,17,15
577         mtmsrd  r3,1
578
579         /* copy dest pages, flush whole dest image */
580         mr      r3,r29
581         bl      kexec_copy_flush        /* (image) */
582
583         /* turn off mmu */
584         bl      real_mode
585
586         /* copy  0x100 bytes starting at start to 0 */
587         li      r3,0
588         mr      r4,r30          /* start, aka phys mem offset */
589         li      r5,0x100
590         li      r6,0
591         bl      copy_and_flush  /* (dest, src, copy limit, start offset) */
592 1:      /* assume normal blr return */
593
594         /* release other cpus to the new kernel secondary start at 0x60 */
595         mflr    r5
596         li      r6,1
597         stw     r6,kexec_flag-1b(5)
598
599         /* clear out hardware hash page table and tlb */
600 #if !defined(_CALL_ELF) || _CALL_ELF != 2
601         ld      r12,0(r27)              /* deref function descriptor */
602 #else
603         mr      r12,r27
604 #endif
605         mtctr   r12
606         bctrl                           /* ppc_md.hpte_clear_all(void); */
607
608 /*
609  *   kexec image calling is:
610  *      the first 0x100 bytes of the entry point are copied to 0
611  *
612  *      all slaves branch to slave = 0x60 (absolute)
613  *              slave(phys_cpu_id);
614  *
615  *      master goes to start = entry point
616  *              start(phys_cpu_id, start, 0);
617  *
618  *
619  *   a wrapper is needed to call existing kernels, here is an approximate
620  *   description of one method:
621  *
622  * v2: (2.6.10)
623  *   start will be near the boot_block (maybe 0x100 bytes before it?)
624  *   it will have a 0x60, which will b to boot_block, where it will wait
625  *   and 0 will store phys into struct boot-block and load r3 from there,
626  *   copy kernel 0-0x100 and tell slaves to back down to 0x60 again
627  *
628  * v1: (2.6.9)
629  *    boot block will have all cpus scanning device tree to see if they
630  *    are the boot cpu ?????
631  *    other device tree differences (prop sizes, va vs pa, etc)...
632  */
633         mr      r3,r25  # my phys cpu
634         mr      r4,r30  # start, aka phys mem offset
635         mtlr    4
636         li      r5,0
637         blr     /* image->start(physid, image->start, 0); */
638 #endif /* CONFIG_KEXEC */
639
640 #ifdef CONFIG_MODULES
641 #if defined(_CALL_ELF) && _CALL_ELF == 2
642
643 #ifdef CONFIG_MODVERSIONS
644 .weak __crc_TOC.
645 .section "___kcrctab+TOC.","a"
646 .globl __kcrctab_TOC.
647 __kcrctab_TOC.:
648         .llong  __crc_TOC.
649 #endif
650
651 /*
652  * Export a fake .TOC. since both modpost and depmod will complain otherwise.
653  * Both modpost and depmod strip the leading . so we do the same here.
654  */
655 .section "__ksymtab_strings","a"
656 __kstrtab_TOC.:
657         .asciz "TOC."
658
659 .section "___ksymtab+TOC.","a"
660 /* This symbol name is important: it's used by modpost to find exported syms */
661 .globl __ksymtab_TOC.
662 __ksymtab_TOC.:
663         .llong 0 /* .value */
664         .llong __kstrtab_TOC.
665 #endif /* ELFv2 */
666 #endif /* MODULES */