]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - arch/powerpc/kernel/idle_book3s.S
99de9de7efe7a88802886ac8022de8aa33efe1c5
[karo-tx-linux.git] / arch / powerpc / kernel / idle_book3s.S
1 /*
2  *  This file contains idle entry/exit functions for POWER7,
3  *  POWER8 and POWER9 CPUs.
4  *
5  *  This program is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU General Public License
7  *  as published by the Free Software Foundation; either version
8  *  2 of the License, or (at your option) any later version.
9  */
10
11 #include <linux/threads.h>
12 #include <asm/processor.h>
13 #include <asm/page.h>
14 #include <asm/cputable.h>
15 #include <asm/thread_info.h>
16 #include <asm/ppc_asm.h>
17 #include <asm/asm-offsets.h>
18 #include <asm/ppc-opcode.h>
19 #include <asm/hw_irq.h>
20 #include <asm/kvm_book3s_asm.h>
21 #include <asm/opal.h>
22 #include <asm/cpuidle.h>
23 #include <asm/exception-64s.h>
24 #include <asm/book3s/64/mmu-hash.h>
25 #include <asm/mmu.h>
26
27 #undef DEBUG
28
29 /*
30  * Use unused space in the interrupt stack to save and restore
31  * registers for winkle support.
32  */
33 #define _SDR1   GPR3
34 #define _RPR    GPR4
35 #define _SPURR  GPR5
36 #define _PURR   GPR6
37 #define _TSCR   GPR7
38 #define _DSCR   GPR8
39 #define _AMOR   GPR9
40 #define _WORT   GPR10
41 #define _WORC   GPR11
42 #define _PTCR   GPR12
43
44 #define PSSCR_EC_ESL_MASK_SHIFTED          (PSSCR_EC | PSSCR_ESL) >> 16
45
46         .text
47
48 /*
49  * Used by threads before entering deep idle states. Saves SPRs
50  * in interrupt stack frame
51  */
52 save_sprs_to_stack:
53         /*
54          * Note all register i.e per-core, per-subcore or per-thread is saved
55          * here since any thread in the core might wake up first
56          */
57 BEGIN_FTR_SECTION
58         mfspr   r3,SPRN_PTCR
59         std     r3,_PTCR(r1)
60         /*
61          * Note - SDR1 is dropped in Power ISA v3. Hence not restoring
62          * SDR1 here
63          */
64 FTR_SECTION_ELSE
65         mfspr   r3,SPRN_SDR1
66         std     r3,_SDR1(r1)
67 ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_300)
68         mfspr   r3,SPRN_RPR
69         std     r3,_RPR(r1)
70         mfspr   r3,SPRN_SPURR
71         std     r3,_SPURR(r1)
72         mfspr   r3,SPRN_PURR
73         std     r3,_PURR(r1)
74         mfspr   r3,SPRN_TSCR
75         std     r3,_TSCR(r1)
76         mfspr   r3,SPRN_DSCR
77         std     r3,_DSCR(r1)
78         mfspr   r3,SPRN_AMOR
79         std     r3,_AMOR(r1)
80         mfspr   r3,SPRN_WORT
81         std     r3,_WORT(r1)
82         mfspr   r3,SPRN_WORC
83         std     r3,_WORC(r1)
84
85         blr
86
87 /*
88  * Used by threads when the lock bit of core_idle_state is set.
89  * Threads will spin in HMT_LOW until the lock bit is cleared.
90  * r14 - pointer to core_idle_state
91  * r15 - used to load contents of core_idle_state
92  * r9  - used as a temporary variable
93  */
94
95 core_idle_lock_held:
96         HMT_LOW
97 3:      lwz     r15,0(r14)
98         andi.   r15,r15,PNV_CORE_IDLE_LOCK_BIT
99         bne     3b
100         HMT_MEDIUM
101         lwarx   r15,0,r14
102         andi.   r9,r15,PNV_CORE_IDLE_LOCK_BIT
103         bne     core_idle_lock_held
104         blr
105
106 /*
107  * Pass requested state in r3:
108  *      r3 - PNV_THREAD_NAP/SLEEP/WINKLE in POWER8
109  *         - Requested STOP state in POWER9
110  *
111  * To check IRQ_HAPPENED in r4
112  *      0 - don't check
113  *      1 - check
114  *
115  * Address to 'rfid' to in r5
116  */
117 pnv_powersave_common:
118         /* Use r3 to pass state nap/sleep/winkle */
119         /* NAP is a state loss, we create a regs frame on the
120          * stack, fill it up with the state we care about and
121          * stick a pointer to it in PACAR1. We really only
122          * need to save PC, some CR bits and the NV GPRs,
123          * but for now an interrupt frame will do.
124          */
125         mflr    r0
126         std     r0,16(r1)
127         stdu    r1,-INT_FRAME_SIZE(r1)
128         std     r0,_LINK(r1)
129         std     r0,_NIP(r1)
130
131         /* Hard disable interrupts */
132         mfmsr   r9
133         rldicl  r9,r9,48,1
134         rotldi  r9,r9,16
135         mtmsrd  r9,1                    /* hard-disable interrupts */
136
137         /* Check if something happened while soft-disabled */
138         lbz     r0,PACAIRQHAPPENED(r13)
139         andi.   r0,r0,~PACA_IRQ_HARD_DIS@l
140         beq     1f
141         cmpwi   cr0,r4,0
142         beq     1f
143         addi    r1,r1,INT_FRAME_SIZE
144         ld      r0,16(r1)
145         li      r3,0                    /* Return 0 (no nap) */
146         mtlr    r0
147         blr
148
149 1:      /* We mark irqs hard disabled as this is the state we'll
150          * be in when returning and we need to tell arch_local_irq_restore()
151          * about it
152          */
153         li      r0,PACA_IRQ_HARD_DIS
154         stb     r0,PACAIRQHAPPENED(r13)
155
156         /* We haven't lost state ... yet */
157         li      r0,0
158         stb     r0,PACA_NAPSTATELOST(r13)
159
160         /* Continue saving state */
161         SAVE_GPR(2, r1)
162         SAVE_NVGPRS(r1)
163         mfcr    r4
164         std     r4,_CCR(r1)
165         std     r9,_MSR(r1)
166         std     r1,PACAR1(r13)
167
168         /*
169          * Go to real mode to do the nap, as required by the architecture.
170          * Also, we need to be in real mode before setting hwthread_state,
171          * because as soon as we do that, another thread can switch
172          * the MMU context to the guest.
173          */
174         LOAD_REG_IMMEDIATE(r7, MSR_IDLE)
175         li      r6, MSR_RI
176         andc    r6, r9, r6
177         mtmsrd  r6, 1           /* clear RI before setting SRR0/1 */
178         mtspr   SPRN_SRR0, r5
179         mtspr   SPRN_SRR1, r7
180         rfid
181
182         .globl pnv_enter_arch207_idle_mode
183 pnv_enter_arch207_idle_mode:
184 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
185         /* Tell KVM we're entering idle */
186         li      r4,KVM_HWTHREAD_IN_IDLE
187         /******************************************************/
188         /*  N O T E   W E L L    ! ! !    N O T E   W E L L   */
189         /* The following store to HSTATE_HWTHREAD_STATE(r13)  */
190         /* MUST occur in real mode, i.e. with the MMU off,    */
191         /* and the MMU must stay off until we clear this flag */
192         /* and test HSTATE_HWTHREAD_REQ(r13) in               */
193         /* pnv_powersave_wakeup in this file.                 */
194         /* The reason is that another thread can switch the   */
195         /* MMU to a guest context whenever this flag is set   */
196         /* to KVM_HWTHREAD_IN_IDLE, and if the MMU was on,    */
197         /* that would potentially cause this thread to start  */
198         /* executing instructions from guest memory in        */
199         /* hypervisor mode, leading to a host crash or data   */
200         /* corruption, or worse.                              */
201         /******************************************************/
202         stb     r4,HSTATE_HWTHREAD_STATE(r13)
203 #endif
204         stb     r3,PACA_THREAD_IDLE_STATE(r13)
205         cmpwi   cr3,r3,PNV_THREAD_SLEEP
206         bge     cr3,2f
207         IDLE_STATE_ENTER_SEQ_NORET(PPC_NAP)
208         /* No return */
209 2:
210         /* Sleep or winkle */
211         lbz     r7,PACA_THREAD_MASK(r13)
212         ld      r14,PACA_CORE_IDLE_STATE_PTR(r13)
213 lwarx_loop1:
214         lwarx   r15,0,r14
215
216         andi.   r9,r15,PNV_CORE_IDLE_LOCK_BIT
217         bnel    core_idle_lock_held
218
219         andc    r15,r15,r7                      /* Clear thread bit */
220
221         andi.   r15,r15,PNV_CORE_IDLE_THREAD_BITS
222
223 /*
224  * If cr0 = 0, then current thread is the last thread of the core entering
225  * sleep. Last thread needs to execute the hardware bug workaround code if
226  * required by the platform.
227  * Make the workaround call unconditionally here. The below branch call is
228  * patched out when the idle states are discovered if the platform does not
229  * require it.
230  */
231 .global pnv_fastsleep_workaround_at_entry
232 pnv_fastsleep_workaround_at_entry:
233         beq     fastsleep_workaround_at_entry
234
235         stwcx.  r15,0,r14
236         bne-    lwarx_loop1
237         isync
238
239 common_enter: /* common code for all the threads entering sleep or winkle */
240         bgt     cr3,enter_winkle
241         IDLE_STATE_ENTER_SEQ_NORET(PPC_SLEEP)
242
243 fastsleep_workaround_at_entry:
244         ori     r15,r15,PNV_CORE_IDLE_LOCK_BIT
245         stwcx.  r15,0,r14
246         bne-    lwarx_loop1
247         isync
248
249         /* Fast sleep workaround */
250         li      r3,1
251         li      r4,1
252         bl      opal_config_cpu_idle_state
253
254         /* Clear Lock bit */
255         li      r0,0
256         lwsync
257         stw     r0,0(r14)
258         b       common_enter
259
260 enter_winkle:
261         bl      save_sprs_to_stack
262
263         IDLE_STATE_ENTER_SEQ_NORET(PPC_WINKLE)
264
265 /*
266  * r3 - PSSCR value corresponding to the requested stop state.
267  */
268 power_enter_stop:
269 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
270         /* Tell KVM we're entering idle */
271         li      r4,KVM_HWTHREAD_IN_IDLE
272         /* DO THIS IN REAL MODE!  See comment above. */
273         stb     r4,HSTATE_HWTHREAD_STATE(r13)
274 #endif
275 /*
276  * Check if we are executing the lite variant with ESL=EC=0
277  */
278         andis.   r4,r3,PSSCR_EC_ESL_MASK_SHIFTED
279         clrldi   r3,r3,60 /* r3 = Bits[60:63] = Requested Level (RL) */
280         bne      .Lhandle_esl_ec_set
281         IDLE_STATE_ENTER_SEQ(PPC_STOP)
282         li      r3,0  /* Since we didn't lose state, return 0 */
283         b       pnv_wakeup_noloss
284
285 .Lhandle_esl_ec_set:
286 /*
287  * Check if the requested state is a deep idle state.
288  */
289         LOAD_REG_ADDRBASE(r5,pnv_first_deep_stop_state)
290         ld      r4,ADDROFF(pnv_first_deep_stop_state)(r5)
291         cmpd    r3,r4
292         bge     .Lhandle_deep_stop
293         IDLE_STATE_ENTER_SEQ_NORET(PPC_STOP)
294 .Lhandle_deep_stop:
295 /*
296  * Entering deep idle state.
297  * Clear thread bit in PACA_CORE_IDLE_STATE, save SPRs to
298  * stack and enter stop
299  */
300         lbz     r7,PACA_THREAD_MASK(r13)
301         ld      r14,PACA_CORE_IDLE_STATE_PTR(r13)
302
303 lwarx_loop_stop:
304         lwarx   r15,0,r14
305         andi.   r9,r15,PNV_CORE_IDLE_LOCK_BIT
306         bnel    core_idle_lock_held
307         andc    r15,r15,r7                      /* Clear thread bit */
308
309         stwcx.  r15,0,r14
310         bne-    lwarx_loop_stop
311         isync
312
313         bl      save_sprs_to_stack
314
315         IDLE_STATE_ENTER_SEQ_NORET(PPC_STOP)
316
317 _GLOBAL(power7_idle)
318         /* Now check if user or arch enabled NAP mode */
319         LOAD_REG_ADDRBASE(r3,powersave_nap)
320         lwz     r4,ADDROFF(powersave_nap)(r3)
321         cmpwi   0,r4,0
322         beqlr
323         li      r3, 1
324         /* fall through */
325
326 _GLOBAL(power7_nap)
327         mr      r4,r3
328         li      r3,PNV_THREAD_NAP
329         LOAD_REG_ADDR(r5, pnv_enter_arch207_idle_mode)
330         b       pnv_powersave_common
331         /* No return */
332
333 _GLOBAL(power7_sleep)
334         li      r3,PNV_THREAD_SLEEP
335         li      r4,1
336         LOAD_REG_ADDR(r5, pnv_enter_arch207_idle_mode)
337         b       pnv_powersave_common
338         /* No return */
339
340 _GLOBAL(power7_winkle)
341         li      r3,PNV_THREAD_WINKLE
342         li      r4,1
343         LOAD_REG_ADDR(r5, pnv_enter_arch207_idle_mode)
344         b       pnv_powersave_common
345         /* No return */
346
347 #define CHECK_HMI_INTERRUPT                                             \
348         mfspr   r0,SPRN_SRR1;                                           \
349 BEGIN_FTR_SECTION_NESTED(66);                                           \
350         rlwinm  r0,r0,45-31,0xf;  /* extract wake reason field (P8) */  \
351 FTR_SECTION_ELSE_NESTED(66);                                            \
352         rlwinm  r0,r0,45-31,0xe;  /* P7 wake reason field is 3 bits */  \
353 ALT_FTR_SECTION_END_NESTED_IFSET(CPU_FTR_ARCH_207S, 66);                \
354         cmpwi   r0,0xa;                 /* Hypervisor maintenance ? */  \
355         bne     20f;                                                    \
356         /* Invoke opal call to handle hmi */                            \
357         ld      r2,PACATOC(r13);                                        \
358         ld      r1,PACAR1(r13);                                         \
359         std     r3,ORIG_GPR3(r1);       /* Save original r3 */          \
360         li      r3,0;                   /* NULL argument */             \
361         bl      hmi_exception_realmode;                                 \
362         nop;                                                            \
363         ld      r3,ORIG_GPR3(r1);       /* Restore original r3 */       \
364 20:     nop;
365
366 /*
367  * r3 - The PSSCR value corresponding to the stop state.
368  * r4 - The PSSCR mask corrresonding to the stop state.
369  */
370 _GLOBAL(power9_idle_stop)
371         mfspr   r5,SPRN_PSSCR
372         andc    r5,r5,r4
373         or      r3,r3,r5
374         mtspr   SPRN_PSSCR,r3
375         LOAD_REG_ADDR(r5,power_enter_stop)
376         li      r4,1
377         b       pnv_powersave_common
378         /* No return */
379
380 /*
381  * On waking up from stop 0,1,2 with ESL=1 on POWER9 DD1,
382  * HSPRG0 will be set to the HSPRG0 value of one of the
383  * threads in this core. Thus the value we have in r13
384  * may not be this thread's paca pointer.
385  *
386  * Fortunately, the TIR remains invariant. Since this thread's
387  * paca pointer is recorded in all its sibling's paca, we can
388  * correctly recover this thread's paca pointer if we
389  * know the index of this thread in the core.
390  *
391  * This index can be obtained from the TIR.
392  *
393  * i.e, thread's position in the core = TIR.
394  * If this value is i, then this thread's paca is
395  * paca->thread_sibling_pacas[i].
396  */
397 power9_dd1_recover_paca:
398         mfspr   r4, SPRN_TIR
399         /*
400          * Since each entry in thread_sibling_pacas is 8 bytes
401          * we need to left-shift by 3 bits. Thus r4 = i * 8
402          */
403         sldi    r4, r4, 3
404         /* Get &paca->thread_sibling_pacas[0] in r5 */
405         ld      r5, PACA_SIBLING_PACA_PTRS(r13)
406         /* Load paca->thread_sibling_pacas[i] into r13 */
407         ldx     r13, r4, r5
408         SET_PACA(r13)
409         ld      r2, PACATOC(r13)
410         /*
411          * Indicate that we have lost NVGPR state
412          * which needs to be restored from the stack.
413          */
414         li      r3, 1
415         stb     r0,PACA_NAPSTATELOST(r13)
416         blr
417
418 .global pnv_powersave_wakeup
419 pnv_powersave_wakeup:
420 BEGIN_FTR_SECTION
421         GET_PACA(r13) /* Restore HSPRG0 to get the winkle bit in r13 */
422 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
423         bl      pnv_restore_hyp_resource
424
425         li      r0,PNV_THREAD_RUNNING
426         stb     r0,PACA_THREAD_IDLE_STATE(r13)  /* Clear thread state */
427
428 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
429         li      r0,KVM_HWTHREAD_IN_KERNEL
430         stb     r0,HSTATE_HWTHREAD_STATE(r13)
431         /* Order setting hwthread_state vs. testing hwthread_req */
432         sync
433         lbz     r0,HSTATE_HWTHREAD_REQ(r13)
434         cmpwi   r0,0
435         beq     1f
436         b       kvm_start_guest
437 1:
438 #endif
439
440         /* Return SRR1 from power7_nap() */
441         mfspr   r3,SPRN_SRR1
442         blt     cr3,pnv_wakeup_noloss
443         b       pnv_wakeup_loss
444
445 /*
446  * Check whether we have woken up with hypervisor state loss.
447  * If yes, restore hypervisor state and return back to link.
448  *
449  * r13 - Contents of HSPRG0
450  * cr3 - set to gt if waking up with partial/complete hypervisor state loss
451  */
452 pnv_restore_hyp_resource:
453 BEGIN_FTR_SECTION
454 BEGIN_FTR_SECTION_NESTED(70)
455         mflr    r6
456         bl      power9_dd1_recover_paca
457         mtlr    r6
458 FTR_SECTION_ELSE_NESTED(70)
459         ld      r2, PACATOC(r13)
460 ALT_FTR_SECTION_END_NESTED_IFSET(CPU_FTR_POWER9_DD1, 70)
461         /*
462          * POWER ISA 3. Use PSSCR to determine if we
463          * are waking up from deep idle state
464          */
465         LOAD_REG_ADDRBASE(r5,pnv_first_deep_stop_state)
466         ld      r4,ADDROFF(pnv_first_deep_stop_state)(r5)
467
468         mfspr   r5,SPRN_PSSCR
469         /*
470          * 0-3 bits correspond to Power-Saving Level Status
471          * which indicates the idle state we are waking up from
472          */
473         rldicl  r5,r5,4,60
474         cmpd    cr4,r5,r4
475         bge     cr4,pnv_wakeup_tb_loss /* returns to caller */
476
477         blr     /* Waking up without hypervisor state loss. */
478
479 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
480
481         /*
482          * POWER ISA 2.07 or less.
483          * Check if last bit of HSPGR0 is set. This indicates whether we are
484          * waking up from winkle.
485          */
486         clrldi  r5,r13,63
487         clrrdi  r13,r13,1
488
489         /* Now that we are sure r13 is corrected, load TOC */
490         ld      r2,PACATOC(r13);
491         cmpwi   cr4,r5,1
492         mtspr   SPRN_HSPRG0,r13
493
494         lbz     r0,PACA_THREAD_IDLE_STATE(r13)
495         cmpwi   cr2,r0,PNV_THREAD_NAP
496         bgt     cr2,pnv_wakeup_tb_loss  /* Either sleep or Winkle */
497
498         /*
499          * We fall through here if PACA_THREAD_IDLE_STATE shows we are waking
500          * up from nap. At this stage CR3 shouldn't contains 'gt' since that
501          * indicates we are waking with hypervisor state loss from nap.
502          */
503         bgt     cr3,.
504
505         blr     /* Waking up without hypervisor state loss */
506
507 /*
508  * Called if waking up from idle state which can cause either partial or
509  * complete hyp state loss.
510  * In POWER8, called if waking up from fastsleep or winkle
511  * In POWER9, called if waking up from stop state >= pnv_first_deep_stop_state
512  *
513  * r13 - PACA
514  * cr3 - gt if waking up with partial/complete hypervisor state loss
515  * cr4 - gt or eq if waking up from complete hypervisor state loss.
516  */
517 pnv_wakeup_tb_loss:
518         ld      r1,PACAR1(r13)
519         /*
520          * Before entering any idle state, the NVGPRs are saved in the stack
521          * and they are restored before switching to the process context. Hence
522          * until they are restored, they are free to be used.
523          *
524          * Save SRR1 and LR in NVGPRs as they might be clobbered in
525          * opal_call() (called in CHECK_HMI_INTERRUPT). SRR1 is required
526          * to determine the wakeup reason if we branch to kvm_start_guest. LR
527          * is required to return back to reset vector after hypervisor state
528          * restore is complete.
529          */
530         mflr    r17
531         mfspr   r16,SPRN_SRR1
532 BEGIN_FTR_SECTION
533         CHECK_HMI_INTERRUPT
534 END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
535
536         lbz     r7,PACA_THREAD_MASK(r13)
537         ld      r14,PACA_CORE_IDLE_STATE_PTR(r13)
538 lwarx_loop2:
539         lwarx   r15,0,r14
540         andi.   r9,r15,PNV_CORE_IDLE_LOCK_BIT
541         /*
542          * Lock bit is set in one of the 2 cases-
543          * a. In the sleep/winkle enter path, the last thread is executing
544          * fastsleep workaround code.
545          * b. In the wake up path, another thread is executing fastsleep
546          * workaround undo code or resyncing timebase or restoring context
547          * In either case loop until the lock bit is cleared.
548          */
549         bnel    core_idle_lock_held
550
551         cmpwi   cr2,r15,0
552
553         /*
554          * At this stage
555          * cr2 - eq if first thread to wakeup in core
556          * cr3-  gt if waking up with partial/complete hypervisor state loss
557          * cr4 - gt or eq if waking up from complete hypervisor state loss.
558          */
559
560         ori     r15,r15,PNV_CORE_IDLE_LOCK_BIT
561         stwcx.  r15,0,r14
562         bne-    lwarx_loop2
563         isync
564
565 BEGIN_FTR_SECTION
566         lbz     r4,PACA_SUBCORE_SIBLING_MASK(r13)
567         and     r4,r4,r15
568         cmpwi   r4,0    /* Check if first in subcore */
569
570         or      r15,r15,r7              /* Set thread bit */
571         beq     first_thread_in_subcore
572 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
573
574         or      r15,r15,r7              /* Set thread bit */
575         beq     cr2,first_thread_in_core
576
577         /* Not first thread in core or subcore to wake up */
578         b       clear_lock
579
580 first_thread_in_subcore:
581         /*
582          * If waking up from sleep, subcore state is not lost. Hence
583          * skip subcore state restore
584          */
585         blt     cr4,subcore_state_restored
586
587         /* Restore per-subcore state */
588         ld      r4,_SDR1(r1)
589         mtspr   SPRN_SDR1,r4
590
591         ld      r4,_RPR(r1)
592         mtspr   SPRN_RPR,r4
593         ld      r4,_AMOR(r1)
594         mtspr   SPRN_AMOR,r4
595
596 subcore_state_restored:
597         /*
598          * Check if the thread is also the first thread in the core. If not,
599          * skip to clear_lock.
600          */
601         bne     cr2,clear_lock
602
603 first_thread_in_core:
604
605         /*
606          * First thread in the core waking up from any state which can cause
607          * partial or complete hypervisor state loss. It needs to
608          * call the fastsleep workaround code if the platform requires it.
609          * Call it unconditionally here. The below branch instruction will
610          * be patched out if the platform does not have fastsleep or does not
611          * require the workaround. Patching will be performed during the
612          * discovery of idle-states.
613          */
614 .global pnv_fastsleep_workaround_at_exit
615 pnv_fastsleep_workaround_at_exit:
616         b       fastsleep_workaround_at_exit
617
618 timebase_resync:
619         /*
620          * Use cr3 which indicates that we are waking up with atleast partial
621          * hypervisor state loss to determine if TIMEBASE RESYNC is needed.
622          */
623         ble     cr3,clear_lock
624         /* Time base re-sync */
625         bl      opal_resync_timebase;
626         /*
627          * If waking up from sleep, per core state is not lost, skip to
628          * clear_lock.
629          */
630         blt     cr4,clear_lock
631
632         /*
633          * First thread in the core to wake up and its waking up with
634          * complete hypervisor state loss. Restore per core hypervisor
635          * state.
636          */
637 BEGIN_FTR_SECTION
638         ld      r4,_PTCR(r1)
639         mtspr   SPRN_PTCR,r4
640         ld      r4,_RPR(r1)
641         mtspr   SPRN_RPR,r4
642 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
643
644         ld      r4,_TSCR(r1)
645         mtspr   SPRN_TSCR,r4
646         ld      r4,_WORC(r1)
647         mtspr   SPRN_WORC,r4
648
649 clear_lock:
650         andi.   r15,r15,PNV_CORE_IDLE_THREAD_BITS
651         lwsync
652         stw     r15,0(r14)
653
654 common_exit:
655         /*
656          * Common to all threads.
657          *
658          * If waking up from sleep, hypervisor state is not lost. Hence
659          * skip hypervisor state restore.
660          */
661         blt     cr4,hypervisor_state_restored
662
663         /* Waking up from winkle */
664
665 BEGIN_MMU_FTR_SECTION
666         b       no_segments
667 END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
668         /* Restore SLB  from PACA */
669         ld      r8,PACA_SLBSHADOWPTR(r13)
670
671         .rept   SLB_NUM_BOLTED
672         li      r3, SLBSHADOW_SAVEAREA
673         LDX_BE  r5, r8, r3
674         addi    r3, r3, 8
675         LDX_BE  r6, r8, r3
676         andis.  r7,r5,SLB_ESID_V@h
677         beq     1f
678         slbmte  r6,r5
679 1:      addi    r8,r8,16
680         .endr
681 no_segments:
682
683         /* Restore per thread state */
684
685         ld      r4,_SPURR(r1)
686         mtspr   SPRN_SPURR,r4
687         ld      r4,_PURR(r1)
688         mtspr   SPRN_PURR,r4
689         ld      r4,_DSCR(r1)
690         mtspr   SPRN_DSCR,r4
691         ld      r4,_WORT(r1)
692         mtspr   SPRN_WORT,r4
693
694         /* Call cur_cpu_spec->cpu_restore() */
695         LOAD_REG_ADDR(r4, cur_cpu_spec)
696         ld      r4,0(r4)
697         ld      r12,CPU_SPEC_RESTORE(r4)
698 #ifdef PPC64_ELF_ABI_v1
699         ld      r12,0(r12)
700 #endif
701         mtctr   r12
702         bctrl
703
704 hypervisor_state_restored:
705
706         mtspr   SPRN_SRR1,r16
707         mtlr    r17
708         blr             /* return to pnv_powersave_wakeup */
709
710 fastsleep_workaround_at_exit:
711         li      r3,1
712         li      r4,0
713         bl      opal_config_cpu_idle_state
714         b       timebase_resync
715
716 /*
717  * R3 here contains the value that will be returned to the caller
718  * of power7_nap.
719  */
720 .global pnv_wakeup_loss
721 pnv_wakeup_loss:
722         ld      r1,PACAR1(r13)
723 BEGIN_FTR_SECTION
724         CHECK_HMI_INTERRUPT
725 END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
726         REST_NVGPRS(r1)
727         REST_GPR(2, r1)
728         ld      r6,_CCR(r1)
729         ld      r4,_MSR(r1)
730         ld      r5,_NIP(r1)
731         addi    r1,r1,INT_FRAME_SIZE
732         mtcr    r6
733         mtspr   SPRN_SRR1,r4
734         mtspr   SPRN_SRR0,r5
735         rfid
736
737 /*
738  * R3 here contains the value that will be returned to the caller
739  * of power7_nap.
740  */
741 pnv_wakeup_noloss:
742         lbz     r0,PACA_NAPSTATELOST(r13)
743         cmpwi   r0,0
744         bne     pnv_wakeup_loss
745 BEGIN_FTR_SECTION
746         CHECK_HMI_INTERRUPT
747 END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
748         ld      r1,PACAR1(r13)
749         ld      r6,_CCR(r1)
750         ld      r4,_MSR(r1)
751         ld      r5,_NIP(r1)
752         addi    r1,r1,INT_FRAME_SIZE
753         mtcr    r6
754         mtspr   SPRN_SRR1,r4
755         mtspr   SPRN_SRR0,r5
756         rfid