436caa9d6eec8c6f8e516acec2be1253c9f33b5f
[linux-2.6-microblaze.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 #include <asm/asm-compat.h>
27
28 #undef DEBUG
29
30 /*
31  * Use unused space in the interrupt stack to save and restore
32  * registers for winkle support.
33  */
34 #define _MMCR0  GPR0
35 #define _SDR1   GPR3
36 #define _PTCR   GPR3
37 #define _RPR    GPR4
38 #define _SPURR  GPR5
39 #define _PURR   GPR6
40 #define _TSCR   GPR7
41 #define _DSCR   GPR8
42 #define _AMOR   GPR9
43 #define _WORT   GPR10
44 #define _WORC   GPR11
45 #define _LPCR   GPR12
46
47 #define PSSCR_EC_ESL_MASK_SHIFTED          (PSSCR_EC | PSSCR_ESL) >> 16
48
49         .text
50
51 /*
52  * Used by threads before entering deep idle states. Saves SPRs
53  * in interrupt stack frame
54  */
55 save_sprs_to_stack:
56         /*
57          * Note all register i.e per-core, per-subcore or per-thread is saved
58          * here since any thread in the core might wake up first
59          */
60 BEGIN_FTR_SECTION
61         /*
62          * Note - SDR1 is dropped in Power ISA v3. Hence not restoring
63          * SDR1 here
64          */
65         mfspr   r3,SPRN_PTCR
66         std     r3,_PTCR(r1)
67         mfspr   r3,SPRN_LPCR
68         std     r3,_LPCR(r1)
69 FTR_SECTION_ELSE
70         mfspr   r3,SPRN_SDR1
71         std     r3,_SDR1(r1)
72 ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_300)
73         mfspr   r3,SPRN_RPR
74         std     r3,_RPR(r1)
75         mfspr   r3,SPRN_SPURR
76         std     r3,_SPURR(r1)
77         mfspr   r3,SPRN_PURR
78         std     r3,_PURR(r1)
79         mfspr   r3,SPRN_TSCR
80         std     r3,_TSCR(r1)
81         mfspr   r3,SPRN_DSCR
82         std     r3,_DSCR(r1)
83         mfspr   r3,SPRN_AMOR
84         std     r3,_AMOR(r1)
85         mfspr   r3,SPRN_WORT
86         std     r3,_WORT(r1)
87         mfspr   r3,SPRN_WORC
88         std     r3,_WORC(r1)
89 /*
90  * On POWER9, there are idle states such as stop4, invoked via cpuidle,
91  * that lose hypervisor resources. In such cases, we need to save
92  * additional SPRs before entering those idle states so that they can
93  * be restored to their older values on wakeup from the idle state.
94  *
95  * On POWER8, the only such deep idle state is winkle which is used
96  * only in the context of CPU-Hotplug, where these additional SPRs are
97  * reinitiazed to a sane value. Hence there is no need to save/restore
98  * these SPRs.
99  */
100 BEGIN_FTR_SECTION
101         blr
102 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
103
104 power9_save_additional_sprs:
105         mfspr   r3, SPRN_PID
106         mfspr   r4, SPRN_LDBAR
107         std     r3, STOP_PID(r13)
108         std     r4, STOP_LDBAR(r13)
109
110         mfspr   r3, SPRN_FSCR
111         mfspr   r4, SPRN_HFSCR
112         std     r3, STOP_FSCR(r13)
113         std     r4, STOP_HFSCR(r13)
114
115         mfspr   r3, SPRN_MMCRA
116         mfspr   r4, SPRN_MMCR0
117         std     r3, STOP_MMCRA(r13)
118         std     r4, _MMCR0(r1)
119
120         mfspr   r3, SPRN_MMCR1
121         mfspr   r4, SPRN_MMCR2
122         std     r3, STOP_MMCR1(r13)
123         std     r4, STOP_MMCR2(r13)
124         blr
125
126 power9_restore_additional_sprs:
127         ld      r3,_LPCR(r1)
128         ld      r4, STOP_PID(r13)
129         mtspr   SPRN_LPCR,r3
130         mtspr   SPRN_PID, r4
131
132         ld      r3, STOP_LDBAR(r13)
133         ld      r4, STOP_FSCR(r13)
134         mtspr   SPRN_LDBAR, r3
135         mtspr   SPRN_FSCR, r4
136
137         ld      r3, STOP_HFSCR(r13)
138         ld      r4, STOP_MMCRA(r13)
139         mtspr   SPRN_HFSCR, r3
140         mtspr   SPRN_MMCRA, r4
141
142         ld      r3, _MMCR0(r1)
143         ld      r4, STOP_MMCR1(r13)
144         mtspr   SPRN_MMCR0, r3
145         mtspr   SPRN_MMCR1, r4
146
147         ld      r3, STOP_MMCR2(r13)
148         mtspr   SPRN_MMCR2, r3
149         blr
150
151 /*
152  * Used by threads when the lock bit of core_idle_state is set.
153  * Threads will spin in HMT_LOW until the lock bit is cleared.
154  * r14 - pointer to core_idle_state
155  * r15 - used to load contents of core_idle_state
156  * r9  - used as a temporary variable
157  */
158
159 core_idle_lock_held:
160         HMT_LOW
161 3:      lwz     r15,0(r14)
162         andis.  r15,r15,PNV_CORE_IDLE_LOCK_BIT@h
163         bne     3b
164         HMT_MEDIUM
165         lwarx   r15,0,r14
166         andis.  r9,r15,PNV_CORE_IDLE_LOCK_BIT@h
167         bne-    core_idle_lock_held
168         blr
169
170 /*
171  * Pass requested state in r3:
172  *      r3 - PNV_THREAD_NAP/SLEEP/WINKLE in POWER8
173  *         - Requested PSSCR value in POWER9
174  *
175  * Address of idle handler to branch to in realmode in r4
176  */
177 pnv_powersave_common:
178         /* Use r3 to pass state nap/sleep/winkle */
179         /* NAP is a state loss, we create a regs frame on the
180          * stack, fill it up with the state we care about and
181          * stick a pointer to it in PACAR1. We really only
182          * need to save PC, some CR bits and the NV GPRs,
183          * but for now an interrupt frame will do.
184          */
185         mtctr   r4
186
187         mflr    r0
188         std     r0,16(r1)
189         stdu    r1,-INT_FRAME_SIZE(r1)
190         std     r0,_LINK(r1)
191         std     r0,_NIP(r1)
192
193         /* We haven't lost state ... yet */
194         li      r0,0
195         stb     r0,PACA_NAPSTATELOST(r13)
196
197         /* Continue saving state */
198         SAVE_GPR(2, r1)
199         SAVE_NVGPRS(r1)
200         mfcr    r5
201         std     r5,_CCR(r1)
202         std     r1,PACAR1(r13)
203
204 BEGIN_FTR_SECTION
205         /*
206          * POWER9 does not require real mode to stop, and presently does not
207          * set hwthread_state for KVM (threads don't share MMU context), so
208          * we can remain in virtual mode for this.
209          */
210         bctr
211 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
212         /*
213          * POWER8
214          * Go to real mode to do the nap, as required by the architecture.
215          * Also, we need to be in real mode before setting hwthread_state,
216          * because as soon as we do that, another thread can switch
217          * the MMU context to the guest.
218          */
219         LOAD_REG_IMMEDIATE(r7, MSR_IDLE)
220         mtmsrd  r7,0
221         bctr
222
223 /*
224  * This is the sequence required to execute idle instructions, as
225  * specified in ISA v2.07 (and earlier). MSR[IR] and MSR[DR] must be 0.
226  */
227 #define IDLE_STATE_ENTER_SEQ_NORET(IDLE_INST)                   \
228         /* Magic NAP/SLEEP/WINKLE mode enter sequence */        \
229         std     r0,0(r1);                                       \
230         ptesync;                                                \
231         ld      r0,0(r1);                                       \
232 236:    cmpd    cr0,r0,r0;                                      \
233         bne     236b;                                           \
234         IDLE_INST;
235
236
237         .globl pnv_enter_arch207_idle_mode
238 pnv_enter_arch207_idle_mode:
239 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
240         /* Tell KVM we're entering idle */
241         li      r4,KVM_HWTHREAD_IN_IDLE
242         /******************************************************/
243         /*  N O T E   W E L L    ! ! !    N O T E   W E L L   */
244         /* The following store to HSTATE_HWTHREAD_STATE(r13)  */
245         /* MUST occur in real mode, i.e. with the MMU off,    */
246         /* and the MMU must stay off until we clear this flag */
247         /* and test HSTATE_HWTHREAD_REQ(r13) in               */
248         /* pnv_powersave_wakeup in this file.                 */
249         /* The reason is that another thread can switch the   */
250         /* MMU to a guest context whenever this flag is set   */
251         /* to KVM_HWTHREAD_IN_IDLE, and if the MMU was on,    */
252         /* that would potentially cause this thread to start  */
253         /* executing instructions from guest memory in        */
254         /* hypervisor mode, leading to a host crash or data   */
255         /* corruption, or worse.                              */
256         /******************************************************/
257         stb     r4,HSTATE_HWTHREAD_STATE(r13)
258 #endif
259         stb     r3,PACA_THREAD_IDLE_STATE(r13)
260         cmpwi   cr3,r3,PNV_THREAD_SLEEP
261         bge     cr3,2f
262         IDLE_STATE_ENTER_SEQ_NORET(PPC_NAP)
263         /* No return */
264 2:
265         /* Sleep or winkle */
266         lbz     r7,PACA_THREAD_MASK(r13)
267         ld      r14,PACA_CORE_IDLE_STATE_PTR(r13)
268         li      r5,0
269         beq     cr3,3f
270         lis     r5,PNV_CORE_IDLE_WINKLE_COUNT@h
271 3:
272 lwarx_loop1:
273         lwarx   r15,0,r14
274
275         andis.  r9,r15,PNV_CORE_IDLE_LOCK_BIT@h
276         bnel-   core_idle_lock_held
277
278         add     r15,r15,r5                      /* Add if winkle */
279         andc    r15,r15,r7                      /* Clear thread bit */
280
281         andi.   r9,r15,PNV_CORE_IDLE_THREAD_BITS
282
283 /*
284  * If cr0 = 0, then current thread is the last thread of the core entering
285  * sleep. Last thread needs to execute the hardware bug workaround code if
286  * required by the platform.
287  * Make the workaround call unconditionally here. The below branch call is
288  * patched out when the idle states are discovered if the platform does not
289  * require it.
290  */
291 .global pnv_fastsleep_workaround_at_entry
292 pnv_fastsleep_workaround_at_entry:
293         beq     fastsleep_workaround_at_entry
294
295         stwcx.  r15,0,r14
296         bne-    lwarx_loop1
297         isync
298
299 common_enter: /* common code for all the threads entering sleep or winkle */
300         bgt     cr3,enter_winkle
301         IDLE_STATE_ENTER_SEQ_NORET(PPC_SLEEP)
302
303 fastsleep_workaround_at_entry:
304         oris    r15,r15,PNV_CORE_IDLE_LOCK_BIT@h
305         stwcx.  r15,0,r14
306         bne-    lwarx_loop1
307         isync
308
309         /* Fast sleep workaround */
310         li      r3,1
311         li      r4,1
312         bl      opal_config_cpu_idle_state
313
314         /* Unlock */
315         xoris   r15,r15,PNV_CORE_IDLE_LOCK_BIT@h
316         lwsync
317         stw     r15,0(r14)
318         b       common_enter
319
320 enter_winkle:
321         bl      save_sprs_to_stack
322
323         IDLE_STATE_ENTER_SEQ_NORET(PPC_WINKLE)
324
325 /*
326  * r3 - PSSCR value corresponding to the requested stop state.
327  */
328 power_enter_stop:
329 /*
330  * Check if we are executing the lite variant with ESL=EC=0
331  */
332         andis.   r4,r3,PSSCR_EC_ESL_MASK_SHIFTED
333         clrldi   r3,r3,60 /* r3 = Bits[60:63] = Requested Level (RL) */
334         bne      .Lhandle_esl_ec_set
335         PPC_STOP
336         li      r3,0  /* Since we didn't lose state, return 0 */
337         std     r3, PACA_REQ_PSSCR(r13)
338
339         /*
340          * pnv_wakeup_noloss() expects r12 to contain the SRR1 value so
341          * it can determine if the wakeup reason is an HMI in
342          * CHECK_HMI_INTERRUPT.
343          *
344          * However, when we wakeup with ESL=0, SRR1 will not contain the wakeup
345          * reason, so there is no point setting r12 to SRR1.
346          *
347          * Further, we clear r12 here, so that we don't accidentally enter the
348          * HMI in pnv_wakeup_noloss() if the value of r12[42:45] == WAKE_HMI.
349          */
350         li      r12, 0
351         b       pnv_wakeup_noloss
352
353 .Lhandle_esl_ec_set:
354 BEGIN_FTR_SECTION
355         /*
356          * POWER9 DD2.0 or earlier can incorrectly set PMAO when waking up after
357          * a state-loss idle. Saving and restoring MMCR0 over idle is a
358          * workaround.
359          */
360         mfspr   r4,SPRN_MMCR0
361         std     r4,_MMCR0(r1)
362 END_FTR_SECTION_IFCLR(CPU_FTR_POWER9_DD2_1)
363
364 /*
365  * Check if the requested state is a deep idle state.
366  */
367         LOAD_REG_ADDRBASE(r5,pnv_first_deep_stop_state)
368         ld      r4,ADDROFF(pnv_first_deep_stop_state)(r5)
369         cmpd    r3,r4
370         bge     .Lhandle_deep_stop
371         PPC_STOP        /* Does not return (system reset interrupt) */
372
373 .Lhandle_deep_stop:
374 /*
375  * Entering deep idle state.
376  * Clear thread bit in PACA_CORE_IDLE_STATE, save SPRs to
377  * stack and enter stop
378  */
379         lbz     r7,PACA_THREAD_MASK(r13)
380         ld      r14,PACA_CORE_IDLE_STATE_PTR(r13)
381
382 lwarx_loop_stop:
383         lwarx   r15,0,r14
384         andis.  r9,r15,PNV_CORE_IDLE_LOCK_BIT@h
385         bnel-   core_idle_lock_held
386         andc    r15,r15,r7                      /* Clear thread bit */
387
388         stwcx.  r15,0,r14
389         bne-    lwarx_loop_stop
390         isync
391
392         bl      save_sprs_to_stack
393
394         PPC_STOP        /* Does not return (system reset interrupt) */
395
396 /*
397  * Entered with MSR[EE]=0 and no soft-masked interrupts pending.
398  * r3 contains desired idle state (PNV_THREAD_NAP/SLEEP/WINKLE).
399  */
400 _GLOBAL(power7_idle_insn)
401         /* Now check if user or arch enabled NAP mode */
402         LOAD_REG_ADDR(r4, pnv_enter_arch207_idle_mode)
403         b       pnv_powersave_common
404
405 #define CHECK_HMI_INTERRUPT                                             \
406 BEGIN_FTR_SECTION_NESTED(66);                                           \
407         rlwinm  r0,r12,45-31,0xf;  /* extract wake reason field (P8) */ \
408 FTR_SECTION_ELSE_NESTED(66);                                            \
409         rlwinm  r0,r12,45-31,0xe;  /* P7 wake reason field is 3 bits */ \
410 ALT_FTR_SECTION_END_NESTED_IFSET(CPU_FTR_ARCH_207S, 66);                \
411         cmpwi   r0,0xa;                 /* Hypervisor maintenance ? */  \
412         bne+    20f;                                                    \
413         /* Invoke opal call to handle hmi */                            \
414         ld      r2,PACATOC(r13);                                        \
415         ld      r1,PACAR1(r13);                                         \
416         std     r3,ORIG_GPR3(r1);       /* Save original r3 */          \
417         li      r3,0;                   /* NULL argument */             \
418         bl      hmi_exception_realmode;                                 \
419         nop;                                                            \
420         ld      r3,ORIG_GPR3(r1);       /* Restore original r3 */       \
421 20:     nop;
422
423 /*
424  * Entered with MSR[EE]=0 and no soft-masked interrupts pending.
425  * r3 contains desired PSSCR register value.
426  *
427  * Offline (CPU unplug) case also must notify KVM that the CPU is
428  * idle.
429  */
430 _GLOBAL(power9_offline_stop)
431 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
432         /*
433          * Tell KVM we're entering idle.
434          * This does not have to be done in real mode because the P9 MMU
435          * is independent per-thread. Some steppings share radix/hash mode
436          * between threads, but in that case KVM has a barrier sync in real
437          * mode before and after switching between radix and hash.
438          */
439         li      r4,KVM_HWTHREAD_IN_IDLE
440         stb     r4,HSTATE_HWTHREAD_STATE(r13)
441 #endif
442         /* fall through */
443
444 _GLOBAL(power9_idle_stop)
445         std     r3, PACA_REQ_PSSCR(r13)
446 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
447 BEGIN_FTR_SECTION
448         sync
449         lwz     r5, PACA_DONT_STOP(r13)
450         cmpwi   r5, 0
451         bne     1f
452 END_FTR_SECTION_IFSET(CPU_FTR_P9_TM_XER_SO_BUG)
453 #endif
454         mtspr   SPRN_PSSCR,r3
455         LOAD_REG_ADDR(r4,power_enter_stop)
456         b       pnv_powersave_common
457         /* No return */
458 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
459 1:
460         /*
461          * We get here when TM / thread reconfiguration bug workaround
462          * code wants to get the CPU into SMT4 mode, and therefore
463          * we are being asked not to stop.
464          */
465         li      r3, 0
466         std     r3, PACA_REQ_PSSCR(r13)
467         blr             /* return 0 for wakeup cause / SRR1 value */
468 #endif
469
470 /*
471  * Called from machine check handler for powersave wakeups.
472  * Low level machine check processing has already been done. Now just
473  * go through the wake up path to get everything in order.
474  *
475  * r3 - The original SRR1 value.
476  * Original SRR[01] have been clobbered.
477  * MSR_RI is clear.
478  */
479 .global pnv_powersave_wakeup_mce
480 pnv_powersave_wakeup_mce:
481         /* Set cr3 for pnv_powersave_wakeup */
482         rlwinm  r11,r3,47-31,30,31
483         cmpwi   cr3,r11,2
484
485         /*
486          * Now put the original SRR1 with SRR1_WAKEMCE_RESVD as the wake
487          * reason into r12, which allows reuse of the system reset wakeup
488          * code without being mistaken for another type of wakeup.
489          */
490         oris    r12,r3,SRR1_WAKEMCE_RESVD@h
491
492         b       pnv_powersave_wakeup
493
494 /*
495  * Called from reset vector for powersave wakeups.
496  * cr3 - set to gt if waking up with partial/complete hypervisor state loss
497  * r12 - SRR1
498  */
499 .global pnv_powersave_wakeup
500 pnv_powersave_wakeup:
501         ld      r2, PACATOC(r13)
502
503 BEGIN_FTR_SECTION
504         bl      pnv_restore_hyp_resource_arch300
505 FTR_SECTION_ELSE
506         bl      pnv_restore_hyp_resource_arch207
507 ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_300)
508
509         li      r0,PNV_THREAD_RUNNING
510         stb     r0,PACA_THREAD_IDLE_STATE(r13)  /* Clear thread state */
511
512         mr      r3,r12
513
514 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
515         lbz     r0,HSTATE_HWTHREAD_STATE(r13)
516         cmpwi   r0,KVM_HWTHREAD_IN_KERNEL
517         beq     0f
518         li      r0,KVM_HWTHREAD_IN_KERNEL
519         stb     r0,HSTATE_HWTHREAD_STATE(r13)
520         /* Order setting hwthread_state vs. testing hwthread_req */
521         sync
522 0:      lbz     r0,HSTATE_HWTHREAD_REQ(r13)
523         cmpwi   r0,0
524         beq     1f
525         b       kvm_start_guest
526 1:
527 #endif
528
529         /* Return SRR1 from power7_nap() */
530         blt     cr3,pnv_wakeup_noloss
531         b       pnv_wakeup_loss
532
533 /*
534  * Check whether we have woken up with hypervisor state loss.
535  * If yes, restore hypervisor state and return back to link.
536  *
537  * cr3 - set to gt if waking up with partial/complete hypervisor state loss
538  */
539 pnv_restore_hyp_resource_arch300:
540         /*
541          * Workaround for POWER9, if we lost resources, the ERAT
542          * might have been mixed up and needs flushing. We also need
543          * to reload MMCR0 (see comment above). We also need to set
544          * then clear bit 60 in MMCRA to ensure the PMU starts running.
545          */
546         blt     cr3,1f
547 BEGIN_FTR_SECTION
548         PPC_INVALIDATE_ERAT
549         ld      r1,PACAR1(r13)
550         ld      r4,_MMCR0(r1)
551         mtspr   SPRN_MMCR0,r4
552 END_FTR_SECTION_IFCLR(CPU_FTR_POWER9_DD2_1)
553         mfspr   r4,SPRN_MMCRA
554         ori     r4,r4,(1 << (63-60))
555         mtspr   SPRN_MMCRA,r4
556         xori    r4,r4,(1 << (63-60))
557         mtspr   SPRN_MMCRA,r4
558 1:
559         /*
560          * POWER ISA 3. Use PSSCR to determine if we
561          * are waking up from deep idle state
562          */
563         LOAD_REG_ADDRBASE(r5,pnv_first_deep_stop_state)
564         ld      r4,ADDROFF(pnv_first_deep_stop_state)(r5)
565
566         /*
567          * 0-3 bits correspond to Power-Saving Level Status
568          * which indicates the idle state we are waking up from
569          */
570         mfspr   r5, SPRN_PSSCR
571         rldicl  r5,r5,4,60
572         li      r0, 0           /* clear requested_psscr to say we're awake */
573         std     r0, PACA_REQ_PSSCR(r13)
574         cmpd    cr4,r5,r4
575         bge     cr4,pnv_wakeup_tb_loss /* returns to caller */
576
577         blr     /* Waking up without hypervisor state loss. */
578
579 /* Same calling convention as arch300 */
580 pnv_restore_hyp_resource_arch207:
581         /*
582          * POWER ISA 2.07 or less.
583          * Check if we slept with sleep or winkle.
584          */
585         lbz     r4,PACA_THREAD_IDLE_STATE(r13)
586         cmpwi   cr2,r4,PNV_THREAD_NAP
587         bgt     cr2,pnv_wakeup_tb_loss  /* Either sleep or Winkle */
588
589         /*
590          * We fall through here if PACA_THREAD_IDLE_STATE shows we are waking
591          * up from nap. At this stage CR3 shouldn't contains 'gt' since that
592          * indicates we are waking with hypervisor state loss from nap.
593          */
594         bgt     cr3,.
595
596         blr     /* Waking up without hypervisor state loss */
597
598 /*
599  * Called if waking up from idle state which can cause either partial or
600  * complete hyp state loss.
601  * In POWER8, called if waking up from fastsleep or winkle
602  * In POWER9, called if waking up from stop state >= pnv_first_deep_stop_state
603  *
604  * r13 - PACA
605  * cr3 - gt if waking up with partial/complete hypervisor state loss
606  *
607  * If ISA300:
608  * cr4 - gt or eq if waking up from complete hypervisor state loss.
609  *
610  * If ISA207:
611  * r4 - PACA_THREAD_IDLE_STATE
612  */
613 pnv_wakeup_tb_loss:
614         ld      r1,PACAR1(r13)
615         /*
616          * Before entering any idle state, the NVGPRs are saved in the stack.
617          * If there was a state loss, or PACA_NAPSTATELOST was set, then the
618          * NVGPRs are restored. If we are here, it is likely that state is lost,
619          * but not guaranteed -- neither ISA207 nor ISA300 tests to reach
620          * here are the same as the test to restore NVGPRS:
621          * PACA_THREAD_IDLE_STATE test for ISA207, PSSCR test for ISA300,
622          * and SRR1 test for restoring NVGPRs.
623          *
624          * We are about to clobber NVGPRs now, so set NAPSTATELOST to
625          * guarantee they will always be restored. This might be tightened
626          * with careful reading of specs (particularly for ISA300) but this
627          * is already a slow wakeup path and it's simpler to be safe.
628          */
629         li      r0,1
630         stb     r0,PACA_NAPSTATELOST(r13)
631
632         /*
633          *
634          * Save SRR1 and LR in NVGPRs as they might be clobbered in
635          * opal_call() (called in CHECK_HMI_INTERRUPT). SRR1 is required
636          * to determine the wakeup reason if we branch to kvm_start_guest. LR
637          * is required to return back to reset vector after hypervisor state
638          * restore is complete.
639          */
640         mr      r19,r12
641         mr      r18,r4
642         mflr    r17
643 BEGIN_FTR_SECTION
644         CHECK_HMI_INTERRUPT
645 END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
646
647         ld      r14,PACA_CORE_IDLE_STATE_PTR(r13)
648         lbz     r7,PACA_THREAD_MASK(r13)
649
650         /*
651          * Take the core lock to synchronize against other threads.
652          *
653          * Lock bit is set in one of the 2 cases-
654          * a. In the sleep/winkle enter path, the last thread is executing
655          * fastsleep workaround code.
656          * b. In the wake up path, another thread is executing fastsleep
657          * workaround undo code or resyncing timebase or restoring context
658          * In either case loop until the lock bit is cleared.
659          */
660 1:
661         lwarx   r15,0,r14
662         andis.  r9,r15,PNV_CORE_IDLE_LOCK_BIT@h
663         bnel-   core_idle_lock_held
664         oris    r15,r15,PNV_CORE_IDLE_LOCK_BIT@h
665         stwcx.  r15,0,r14
666         bne-    1b
667         isync
668
669         andi.   r9,r15,PNV_CORE_IDLE_THREAD_BITS
670         cmpwi   cr2,r9,0
671
672         /*
673          * At this stage
674          * cr2 - eq if first thread to wakeup in core
675          * cr3-  gt if waking up with partial/complete hypervisor state loss
676          * ISA300:
677          * cr4 - gt or eq if waking up from complete hypervisor state loss.
678          */
679
680 BEGIN_FTR_SECTION
681         /*
682          * Were we in winkle?
683          * If yes, check if all threads were in winkle, decrement our
684          * winkle count, set all thread winkle bits if all were in winkle.
685          * Check if our thread has a winkle bit set, and set cr4 accordingly
686          * (to match ISA300, above). Pseudo-code for core idle state
687          * transitions for ISA207 is as follows (everything happens atomically
688          * due to store conditional and/or lock bit):
689          *
690          * nap_idle() { }
691          * nap_wake() { }
692          *
693          * sleep_idle()
694          * {
695          *      core_idle_state &= ~thread_in_core
696          * }
697          *
698          * sleep_wake()
699          * {
700          *     bool first_in_core, first_in_subcore;
701          *
702          *     first_in_core = (core_idle_state & IDLE_THREAD_BITS) == 0;
703          *     first_in_subcore = (core_idle_state & SUBCORE_SIBLING_MASK) == 0;
704          *
705          *     core_idle_state |= thread_in_core;
706          * }
707          *
708          * winkle_idle()
709          * {
710          *      core_idle_state &= ~thread_in_core;
711          *      core_idle_state += 1 << WINKLE_COUNT_SHIFT;
712          * }
713          *
714          * winkle_wake()
715          * {
716          *     bool first_in_core, first_in_subcore, winkle_state_lost;
717          *
718          *     first_in_core = (core_idle_state & IDLE_THREAD_BITS) == 0;
719          *     first_in_subcore = (core_idle_state & SUBCORE_SIBLING_MASK) == 0;
720          *
721          *     core_idle_state |= thread_in_core;
722          *
723          *     if ((core_idle_state & WINKLE_MASK) == (8 << WINKLE_COUNT_SIHFT))
724          *         core_idle_state |= THREAD_WINKLE_BITS;
725          *     core_idle_state -= 1 << WINKLE_COUNT_SHIFT;
726          *
727          *     winkle_state_lost = core_idle_state &
728          *                              (thread_in_core << WINKLE_THREAD_SHIFT);
729          *     core_idle_state &= ~(thread_in_core << WINKLE_THREAD_SHIFT);
730          * }
731          *
732          */
733         cmpwi   r18,PNV_THREAD_WINKLE
734         bne     2f
735         andis.  r9,r15,PNV_CORE_IDLE_WINKLE_COUNT_ALL_BIT@h
736         subis   r15,r15,PNV_CORE_IDLE_WINKLE_COUNT@h
737         beq     2f
738         ori     r15,r15,PNV_CORE_IDLE_THREAD_WINKLE_BITS /* all were winkle */
739 2:
740         /* Shift thread bit to winkle mask, then test if this thread is set,
741          * and remove it from the winkle bits */
742         slwi    r8,r7,8
743         and     r8,r8,r15
744         andc    r15,r15,r8
745         cmpwi   cr4,r8,1 /* cr4 will be gt if our bit is set, lt if not */
746
747         lbz     r4,PACA_SUBCORE_SIBLING_MASK(r13)
748         and     r4,r4,r15
749         cmpwi   r4,0    /* Check if first in subcore */
750
751         or      r15,r15,r7              /* Set thread bit */
752         beq     first_thread_in_subcore
753 END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300)
754
755         or      r15,r15,r7              /* Set thread bit */
756         beq     cr2,first_thread_in_core
757
758         /* Not first thread in core or subcore to wake up */
759         b       clear_lock
760
761 first_thread_in_subcore:
762         /*
763          * If waking up from sleep, subcore state is not lost. Hence
764          * skip subcore state restore
765          */
766         blt     cr4,subcore_state_restored
767
768         /* Restore per-subcore state */
769         ld      r4,_SDR1(r1)
770         mtspr   SPRN_SDR1,r4
771
772         ld      r4,_RPR(r1)
773         mtspr   SPRN_RPR,r4
774         ld      r4,_AMOR(r1)
775         mtspr   SPRN_AMOR,r4
776
777 subcore_state_restored:
778         /*
779          * Check if the thread is also the first thread in the core. If not,
780          * skip to clear_lock.
781          */
782         bne     cr2,clear_lock
783
784 first_thread_in_core:
785
786         /*
787          * First thread in the core waking up from any state which can cause
788          * partial or complete hypervisor state loss. It needs to
789          * call the fastsleep workaround code if the platform requires it.
790          * Call it unconditionally here. The below branch instruction will
791          * be patched out if the platform does not have fastsleep or does not
792          * require the workaround. Patching will be performed during the
793          * discovery of idle-states.
794          */
795 .global pnv_fastsleep_workaround_at_exit
796 pnv_fastsleep_workaround_at_exit:
797         b       fastsleep_workaround_at_exit
798
799 timebase_resync:
800         /*
801          * Use cr3 which indicates that we are waking up with atleast partial
802          * hypervisor state loss to determine if TIMEBASE RESYNC is needed.
803          */
804         ble     cr3,.Ltb_resynced
805         /* Time base re-sync */
806         bl      opal_resync_timebase;
807         /*
808          * If waking up from sleep (POWER8), per core state
809          * is not lost, skip to clear_lock.
810          */
811 .Ltb_resynced:
812         blt     cr4,clear_lock
813
814         /*
815          * First thread in the core to wake up and its waking up with
816          * complete hypervisor state loss. Restore per core hypervisor
817          * state.
818          */
819 BEGIN_FTR_SECTION
820         ld      r4,_PTCR(r1)
821         mtspr   SPRN_PTCR,r4
822         ld      r4,_RPR(r1)
823         mtspr   SPRN_RPR,r4
824         ld      r4,_AMOR(r1)
825         mtspr   SPRN_AMOR,r4
826 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
827
828         ld      r4,_TSCR(r1)
829         mtspr   SPRN_TSCR,r4
830         ld      r4,_WORC(r1)
831         mtspr   SPRN_WORC,r4
832
833 clear_lock:
834         xoris   r15,r15,PNV_CORE_IDLE_LOCK_BIT@h
835         lwsync
836         stw     r15,0(r14)
837
838 common_exit:
839         /*
840          * Common to all threads.
841          *
842          * If waking up from sleep, hypervisor state is not lost. Hence
843          * skip hypervisor state restore.
844          */
845         blt     cr4,hypervisor_state_restored
846
847         /* Waking up from winkle */
848
849 BEGIN_MMU_FTR_SECTION
850         b       no_segments
851 END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_RADIX)
852         /* Restore SLB  from PACA */
853         ld      r8,PACA_SLBSHADOWPTR(r13)
854
855         .rept   SLB_NUM_BOLTED
856         li      r3, SLBSHADOW_SAVEAREA
857         LDX_BE  r5, r8, r3
858         addi    r3, r3, 8
859         LDX_BE  r6, r8, r3
860         andis.  r7,r5,SLB_ESID_V@h
861         beq     1f
862         slbmte  r6,r5
863 1:      addi    r8,r8,16
864         .endr
865 no_segments:
866
867         /* Restore per thread state */
868
869         ld      r4,_SPURR(r1)
870         mtspr   SPRN_SPURR,r4
871         ld      r4,_PURR(r1)
872         mtspr   SPRN_PURR,r4
873         ld      r4,_DSCR(r1)
874         mtspr   SPRN_DSCR,r4
875         ld      r4,_WORT(r1)
876         mtspr   SPRN_WORT,r4
877
878         /* Call cur_cpu_spec->cpu_restore() */
879         LOAD_REG_ADDR(r4, cur_cpu_spec)
880         ld      r4,0(r4)
881         ld      r12,CPU_SPEC_RESTORE(r4)
882 #ifdef PPC64_ELF_ABI_v1
883         ld      r12,0(r12)
884 #endif
885         mtctr   r12
886         bctrl
887
888 /*
889  * On POWER9, we can come here on wakeup from a cpuidle stop state.
890  * Hence restore the additional SPRs to the saved value.
891  *
892  * On POWER8, we come here only on winkle. Since winkle is used
893  * only in the case of CPU-Hotplug, we don't need to restore
894  * the additional SPRs.
895  */
896 BEGIN_FTR_SECTION
897         bl      power9_restore_additional_sprs
898 END_FTR_SECTION_IFSET(CPU_FTR_ARCH_300)
899 hypervisor_state_restored:
900
901         mr      r12,r19
902         mtlr    r17
903         blr             /* return to pnv_powersave_wakeup */
904
905 fastsleep_workaround_at_exit:
906         li      r3,1
907         li      r4,0
908         bl      opal_config_cpu_idle_state
909         b       timebase_resync
910
911 /*
912  * R3 here contains the value that will be returned to the caller
913  * of power7_nap.
914  * R12 contains SRR1 for CHECK_HMI_INTERRUPT.
915  */
916 .global pnv_wakeup_loss
917 pnv_wakeup_loss:
918         ld      r1,PACAR1(r13)
919 BEGIN_FTR_SECTION
920         CHECK_HMI_INTERRUPT
921 END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
922         REST_NVGPRS(r1)
923         REST_GPR(2, r1)
924         ld      r4,PACAKMSR(r13)
925         ld      r5,_LINK(r1)
926         ld      r6,_CCR(r1)
927         addi    r1,r1,INT_FRAME_SIZE
928         mtlr    r5
929         mtcr    r6
930         mtmsrd  r4
931         blr
932
933 /*
934  * R3 here contains the value that will be returned to the caller
935  * of power7_nap.
936  * R12 contains SRR1 for CHECK_HMI_INTERRUPT.
937  */
938 pnv_wakeup_noloss:
939         lbz     r0,PACA_NAPSTATELOST(r13)
940         cmpwi   r0,0
941         bne     pnv_wakeup_loss
942         ld      r1,PACAR1(r13)
943 BEGIN_FTR_SECTION
944         CHECK_HMI_INTERRUPT
945 END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
946         ld      r4,PACAKMSR(r13)
947         ld      r5,_NIP(r1)
948         ld      r6,_CCR(r1)
949         addi    r1,r1,INT_FRAME_SIZE
950         mtlr    r5
951         mtcr    r6
952         mtmsrd  r4
953         blr