summaryrefslogtreecommitdiffstats
path: root/import/chips/p9/procedures/ppe_closed/cme/stop_cme/p9_cme_stop_entry.c
blob: a41a8edf5b03396d7d937bd366650170f65ec2a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: import/chips/p9/procedures/ppe_closed/cme/stop_cme/p9_cme_stop_entry.c $ */
/*                                                                        */
/* OpenPOWER HCODE Project                                                */
/*                                                                        */
/* COPYRIGHT 2015,2017                                                    */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
/// \file p9_cme_stop_enter_thread.c
/// \brief CME Stop Entey Thread
///
/// Features of this code file:
/// - main() registers an IRQ handler which gets interrupted on a STOP update
///   by a core.
/// - main creates a thread which pends on semaphore to be posted by the IRQ
///   handler.
/// - p9_cme_stop_enter_thread() pends on semaphore from IRQ handler.
/// - On interrupt, stop_handler() does as follows:
///   - masks the IRQ
///   - posts a semaphore
///   - exits.
/// - On semaphore post, p9_cme_stop_enter_thread() does as follows:
///   - clears the IRQ
///   - reads STOP
///   - call p9_cme_stop_entry() sequence
///   - unmasks the IRQ
///   - pends on next semaphore post.
///

#include "p9_cme_stop.h"
#include "p9_cme_stop_enter_marks.h"

extern CmeStopRecord G_cme_stop_record;

int
p9_cme_stop_entry()
{

    int             rc                  = 0;
    uint8_t         core_aborted        = 0;
    uint8_t         core_catchup        = 0;
    uint8_t         catchup_ongoing     = 0;
    uint8_t         entry_ongoing       = 1;
    uint8_t         target_level;
    uint8_t         deeper_level        = 0;
    uint32_t        deeper_core         = 0;
    uint32_t        core;
    uint32_t        loop;
    uint32_t        pm_states;
    uint64_t        scom_data;
    ppm_sshsrc_t    hist;
    ppm_pig_t       pig;

    //--------------------------------------------------------------------------
    // BEGIN OF STOP ENTRY
    //--------------------------------------------------------------------------

    // First we need to determine which of the two STOP interrupts fired.
    // Iow, which of the two cores, "left-0" or "right-1", updated their
    // STOP PM_STATE. If both have fired by the time we get to this point,
    // CME will do Daul-cast to both cores at the same time in entry flow.
    core = (in32(CME_LCL_EISR) & BITS32(20, 2)) >> SHIFT32(21);

    // override with partial good core mask
    core = core & G_cme_stop_record.cme_flags & CME_MASK_BC;

    PK_TRACE("SE0: Core Select[%d]", core);

    // Return error if None of both fired
    if (!core)
    {
        return CME_STOP_ENTRY_PM_NOT_ACTIVE;
    }

    //===================================
    MARK_TAG(BEGINSCOPE_STOP_ENTRY, core)
    //===================================

    do   // while(0) loop for stop flow control
    {

        //--------------------------------------------------------------------------
        // STOP LEVEL 1 (should be done by hardware)
        //--------------------------------------------------------------------------

        // Read SISR for pm_state_cX
        pm_states = in32_sh(CME_LCL_SISR);
        G_cme_stop_record.req_stop_c0 = (pm_states & BITS32(4, 4)) >> SHIFT32(7);
        G_cme_stop_record.req_stop_c1 = (pm_states & BITS32(8, 4)) >> SHIFT32(11);

        // pm_active AND waken_up   : pm_state = sisr (new state)
        // pm_active AND !waken_up  : pm_state = sisr (only with auto promote)
        // !pm_active AND waken_up  : pm_state = 0    (out dated state)
        // !pm_active AND !waken_up : pm_state = sisr (current state)
        if (~core & G_cme_stop_record.active_core & CME_MASK_C0)
        {
            G_cme_stop_record.req_stop_c0 = 0;
        }

        if (~core & G_cme_stop_record.active_core & CME_MASK_C1)
        {
            G_cme_stop_record.req_stop_c1 = 0;
        }

        G_cme_stop_record.active_core &= ~core;

        PK_TRACE("SE1: Request Stop Levels[%d %d]",
                 G_cme_stop_record.req_stop_c0, G_cme_stop_record.req_stop_c1);

        // Return error if target STOP level == 1(Nap)
        if((core == CME_MASK_C0 && G_cme_stop_record.req_stop_c0 <= STOP_LEVEL_1) ||
           (core == CME_MASK_C1 && G_cme_stop_record.req_stop_c1 <= STOP_LEVEL_1) ||
           (core == CME_MASK_BC && (G_cme_stop_record.req_stop_c0 <= STOP_LEVEL_1 ||
                                    G_cme_stop_record.req_stop_c1 <= STOP_LEVEL_1)))
        {
            return CME_STOP_ENTRY_STOP1_SENT_IRQ;
        }

        //--------------------------------------------------------------------------
        // STOP LEVEL 2
        //--------------------------------------------------------------------------

        PK_TRACE("SE2.a");
        // Disable fired Stop and corresponding Wakeup interrupts
        out32(CME_LCL_EIMR_OR, (CME_MASK_BC << SHIFT32(21)) |
              (core << SHIFT32(13)) |
              (core << SHIFT32(15)) |
              (core << SHIFT32(17)));

        PK_TRACE("SE2.b");
        // Also clear the status of the currently fired STOP interrupt(s)
        out32(CME_LCL_EISR_CLR, core << SHIFT32(21));

        PK_TRACE("SE2.c");
        // Request PCB Mux
        out32(CME_LCL_SICR_OR, core << SHIFT32(11));

        PK_TRACE("SE2.d");
        // Protect PPM Register Write
        CME_PUTSCOM(CPPM_CPMMR_OR, core, BIT64(0));

        // set target_level from pm_state for both cores or just one core
        target_level = (core == CME_MASK_C0) ? G_cme_stop_record.req_stop_c0 :
                       G_cme_stop_record.req_stop_c1;

        // If both cores are going into STOP but targeting different levels,
        if ((core == CME_MASK_BC) &&
            (G_cme_stop_record.req_stop_c0 != G_cme_stop_record.req_stop_c1))
        {
            // set target_level to the lighter level targeted by one core
            // set deeper_level to the deeper level targeted by deeper core
            deeper_level = G_cme_stop_record.req_stop_c0;
            deeper_core  = CME_MASK_C0;

            if (G_cme_stop_record.req_stop_c0 < G_cme_stop_record.req_stop_c1)
            {
                target_level = G_cme_stop_record.req_stop_c0;
                deeper_level = G_cme_stop_record.req_stop_c1;
                deeper_core  = CME_MASK_C1;
            }
        }

        PK_TRACE("SE2.e");
        // Update STOP History: In Transition of Entry
        // Set req_stop_level to target_level of either both or just one core
        CME_STOP_UPDATE_HISTORY(core,
                                STOP_CORE_READY_RUN,
                                STOP_TRANS_ENTRY,
                                target_level,
                                STOP_LEVEL_0,
                                STOP_REQ_ENABLE,
                                STOP_ACT_DISABLE);

        // Set req_stop_level to deeper_level for deeper core
        if (deeper_core)
        {
            CME_STOP_UPDATE_HISTORY(deeper_core,
                                    STOP_CORE_READY_RUN,
                                    STOP_TRANS_ENTRY,
                                    deeper_level,
                                    STOP_LEVEL_0,
                                    STOP_REQ_ENABLE,
                                    STOP_ACT_DISABLE);
        }

        PK_TRACE("SE2: target_lv[%d], deeper_lv[%d], deeper_core[%d]",
                 target_level, deeper_level, deeper_core);

        //=============================
        MARK_TRAP(SE_POLL_PCBMUX_GRANT)
        //=============================

        PK_TRACE("SE2.f");

        // Poll Infinitely for PCB Mux Grant
        // MF: change watchdog timer in pk to ensure forward progress
        while((core & (in32(CME_LCL_SISR) >> SHIFT32(11))) != core);

        PK_TRACE("SE2: PCB Mux Granted");

        //==========================
        MARK_TRAP(SE_STOP_CORE_CLKS)
        //==========================

        PK_TRACE("SE2.g");
        // Acknowledge the STOP Entry to PC with a pulse
        out32(CME_LCL_SICR_OR, core << SHIFT32(1));
        out32(CME_LCL_SICR_CLR, core << SHIFT32(1));

        PK_TRACE("SE2.h");
        // Raise Core-L2 + Core-CC Quiesces
        out32(CME_LCL_SICR_OR, (core << SHIFT32(7)) | (core << SHIFT32(9)));

        PK_TRACE("SE2.i");
        // Waits quiesce done for at least 512 core cycles
        // MF: verify generate FCB otherwise math is wrong.
        PPE_WAIT_CORE_CYCLES(loop, 512)

        PK_TRACE("SE2.j");
        // Raise Core Chiplet Fence
        CME_PUTSCOM(C_CPLT_CTRL0_OR, core, BIT64(2));
        CME_PUTSCOM(CPPM_NC0INDIR_OR, core, BIT64(18));

        PK_TRACE("SE2.k");
        // Set all bits to zero prior stop core clocks
        CME_PUTSCOM(C_SCAN_REGION_TYPE, core, 0);

        PK_TRACE("SE2.l");
        // Stop Core Clocks
        CME_PUTSCOM(C_CLK_REGION, core, 0x9FFC00000000E000);

        PK_TRACE("SE2.m");

        // Poll for core clocks stopped
        do
        {
            CME_GETSCOM(C_CLOCK_STAT_SL, core, CME_SCOM_AND, scom_data);
        }
        while((scom_data & BITS64(4, 10)) != BITS64(4, 10));

        // MF: verify compiler generate single rlwmni
        // MF: delay may be needed for stage latch to propagate thold

        PK_TRACE("SE2: Core Clock Stopped");

        //=========================
        MARK_TRAP(SE_DROP_CLK_SYNC)
        //=========================

        PK_TRACE("SE2.n");
        // Drop clock sync enable before switch to refclk
        CME_PUTSCOM(CPPM_CACCR_CLR, core, BIT64(15));

#if !EPM_P9_TUNING
        PK_TRACE("SE2.o");

        // Poll for clock sync done to drop
        do
        {
            CME_GETSCOM(CPPM_CACSR, core, CME_SCOM_OR, scom_data);
        }
        while(scom_data & BIT64(13));

#endif

        PK_TRACE("SE2.p");
        // Switch glsmux to refclk to save clock grid power
        CME_PUTSCOM(C_PPM_CGCR, core, 0);

        // Assert PCB Fence
        //CME_PUTSCOM(CPPM_NC0INDIR_OR, core, BIT64(25));
        // Assert Vital Fence
        CME_PUTSCOM(C_CPLT_CTRL1_OR,  core, BIT64(3));
        // Assert Regional Fences
        CME_PUTSCOM(C_CPLT_CTRL1_OR, core, 0xFFFF700000000000);

        PK_TRACE("SE2.q");
        // Update Stop History: In Core Stop Level 2
        // Check if STOP level 2 reaches the target for both or one core
        entry_ongoing =
            target_level == STOP_LEVEL_2 ? STOP_TRANS_COMPLETE : STOP_TRANS_ENTRY;
        CME_STOP_UPDATE_HISTORY(core,
                                STOP_CORE_IS_GATED,
                                entry_ongoing,
                                target_level,
                                STOP_LEVEL_2,
                                STOP_REQ_DISABLE,
                                STOP_ACT_ENABLE);

        // If both cores targeting different levels
        // deeper core should have at least deeper stop level than 2
        // but only need to modify deeper core history if another one was done
        if (deeper_core && !entry_ongoing)
        {
            CME_STOP_UPDATE_HISTORY(deeper_core,
                                    STOP_CORE_IS_GATED,
                                    STOP_TRANS_ENTRY,
                                    deeper_level,
                                    STOP_LEVEL_2,
                                    STOP_REQ_DISABLE,
                                    STOP_ACT_DISABLE);
            // from now on, proceed with only deeper core
            core          = deeper_core;
            target_level  = deeper_level;
            deeper_level  = 0;
            deeper_core   = 0;
            entry_ongoing = 1;
        }

        if (core & CME_MASK_C0)
        {
            G_cme_stop_record.act_stop_c0 = STOP_LEVEL_2;
        }

        if (core & CME_MASK_C1)
        {
            G_cme_stop_record.act_stop_c1 = STOP_LEVEL_2;
        }

        PK_TRACE("SE2: Clock Sync Dropped");

        //===========================
        MARK_TAG(SE_STOP2_DONE, core)
        //===========================

        //===========================
        MARK_TRAP(SE_IS0_BEGIN)
        //===========================
#if !SKIP_ABORT
        out32(CME_LCL_EIMR_CLR, /*TODO(core << SHIFT32(13)) |*/
              (core << SHIFT32(15)) |
              (core << SHIFT32(17)));
        sync();
        out32(CME_LCL_EIMR_OR,  (core << SHIFT32(13)) |
              (core << SHIFT32(15)) |
              (core << SHIFT32(17)));
#endif
        //===================
        MARK_TRAP(SE_IS0_END)
        //===================

        core_aborted = 0;

        if ((core & CME_MASK_C0) &&
            G_cme_stop_record.act_stop_c0 == STOP_LEVEL_0 &&
            G_cme_stop_record.req_stop_c0 != STOP_LEVEL_0)
        {
            core_aborted |= CME_MASK_C0;
            core -= CME_MASK_C0;
            deeper_core = 0;

            if (deeper_core == CME_MASK_C1)
            {
                target_level = deeper_level;
            }

        }

        if ((core & CME_MASK_C1) &&
            G_cme_stop_record.act_stop_c1 == STOP_LEVEL_0 &&
            G_cme_stop_record.req_stop_c1 != STOP_LEVEL_0)
        {
            core_aborted |= CME_MASK_C1;
            core -= CME_MASK_C1;
            deeper_core = 0;

            if (deeper_core == CME_MASK_C0)
            {
                target_level = deeper_level;
            }
        }

        if (!core)
        {
            core |= core_aborted;
            entry_ongoing = 0;
            break;
        }

        if (catchup_ongoing)
        {
            core = CME_MASK_BC;
            break;
        }

#if !SKIP_CATCHUP
        core_catchup = (in32(CME_LCL_EISR) & BITS32(20, 2)) >> SHIFT32(21);
#endif

        if (core_catchup > core)
        {
            core = core_catchup - core;
            catchup_ongoing = 1;
        }

    }
    while(catchup_ongoing);

    do
    {

        // If we are done at STOP level 2 or aborted
        if (!entry_ongoing)
        {
            break;
        }

        PK_TRACE("SE2+:core[%d],deeper_core[%d],\
                  target_level[%d],deeper_level[%d]",
                 core, deeper_core, target_level, deeper_level);

        //--------------------------------------------------------------------------
        // STOP LEVEL 3
        //--------------------------------------------------------------------------

        if (target_level == 3)
        {

            //==========================
            MARK_TAG(SE_CORE_VMIN, core)
            //==========================

            PK_TRACE("SE3.a");
            // Enable IVRM if not already

            PK_TRACE("SE3.b");
            // Drop to Vmin

            PK_TRACE("SE3.c");
            // Update Stop History: In Core Stop Level 3
            CME_STOP_UPDATE_HISTORY(core,
                                    STOP_CORE_IS_GATED,
                                    STOP_TRANS_COMPLETE,
                                    target_level,
                                    STOP_LEVEL_3,
                                    STOP_REQ_DISABLE,
                                    STOP_ACT_ENABLE);

            // If both cores targeting different levels
            // deeper core should have at least deeper stop level than 3
            // only need to modify deeper core history if another one was done
            if (deeper_core)
            {
                CME_STOP_UPDATE_HISTORY(deeper_core,
                                        STOP_CORE_IS_GATED,
                                        STOP_TRANS_ENTRY,
                                        deeper_level,
                                        STOP_LEVEL_2,
                                        STOP_REQ_DISABLE,
                                        STOP_ACT_ENABLE);
                // from now on, proceed with only deeper core
                core          = deeper_core;
                target_level  = deeper_level;
                deeper_level  = 0;
                deeper_core   = 0;
                entry_ongoing = 1;
            }
            else
            {
                entry_ongoing = 0;
            }

            if(core & CME_MASK_C0)
            {
                G_cme_stop_record.act_stop_c0 = STOP_LEVEL_3;
            }

            if(core & CME_MASK_C1)
            {
                G_cme_stop_record.act_stop_c1 = STOP_LEVEL_3;
            }

            //===========================
            MARK_TAG(SE_STOP3_DONE, core)
            //===========================

            // If we are done at STOP level 3
            if (!entry_ongoing)
            {
                break;
            }
        }

        //--------------------------------------------------------------------------
        // STOP LEVEL 4
        //--------------------------------------------------------------------------

        //===============================
        MARK_TAG(SE_POWER_OFF_CORE, core)
        //===============================

        // DD: Assert Cores Vital Thold/PCB Fence/Electrical Fence
        PK_TRACE("SE4.a");
        CME_PUTSCOM(CPPM_NC0INDIR_OR, core, BIT64(25));
        CME_PUTSCOM(CPPM_NC0INDIR_OR, core, BIT64(26));
        CME_PUTSCOM(CPPM_NC0INDIR_OR, core, BIT64(16));

#if !STOP_PRIME
#if !EPM_P9_TUNING
        // Make sure we are not forcing PFET for VDD off
        // vdd_pfet_force_state == 00 (Nop)
        PK_TRACE("SE4.b");
        CME_GETSCOM(PPM_PFCS, core, CME_SCOM_AND, scom_data);

        if (scom_data & BITS64(0, 2))
        {
            return CME_STOP_ENTRY_VDD_PFET_NOT_IDLE;
        }

#endif

        // Prepare PFET Controls
        // vdd_pfet_val/sel_override     = 0 (disbaled)
        // vdd_pfet_regulation_finger_en = 0 (controled by FSM)
        PK_TRACE("SE4.c");
        CME_PUTSCOM(PPM_PFCS_CLR, core, BIT64(4) | BIT64(5) | BIT64(8));

        // Power Off Core VDD
        // vdd_pfet_force_state = 01 (Force Voff)
        PK_TRACE("SE4.d");
        CME_PUTSCOM(PPM_PFCS_OR, core, BIT64(1));

        // Poll for power gate sequencer state: 0x8 (FSM Idle)
        PK_TRACE("SE4.e");

        do
        {
            CME_GETSCOM(PPM_PFCS, core, CME_SCOM_AND, scom_data);
        }
        while(!(scom_data & BIT64(42)));

#if !EPM_P9_TUNING
        // Optional: Poll for vdd_pg_sel being: 0x8
        PK_TRACE("SE4.f");

        do
        {
            CME_GETSCOM(PPM_PFCS, core, CME_SCOM_AND, scom_data);
        }
        while(!(scom_data & BIT64(46)));

#endif
        // Turn Off Force Voff
        // vdd_pfet_force_state = 00 (Nop)
        PK_TRACE("SE4.g");
        CME_PUTSCOM(PPM_PFCS_CLR, core, BITS64(0, 2));
#endif

        PK_TRACE("SE4.h");
        // Update Stop History: In Core Stop Level 4
        // Check if STOP level 4 reaches the target for both or one core
        entry_ongoing =
            target_level == STOP_LEVEL_4 ? STOP_TRANS_COMPLETE :
            STOP_TRANS_ENTRY;
        CME_STOP_UPDATE_HISTORY(core,
                                STOP_CORE_IS_GATED,
                                entry_ongoing,
                                target_level,
                                STOP_LEVEL_4,
                                STOP_REQ_DISABLE,
                                STOP_ACT_ENABLE);

        // If both cores targeting different levels
        // deeper core should have at least deeper stop level than 2
        // only need to modify deeper core history if another one was done
        if (deeper_core && !entry_ongoing)
        {
            CME_STOP_UPDATE_HISTORY(deeper_core,
                                    STOP_CORE_IS_GATED,
                                    STOP_TRANS_ENTRY,
                                    deeper_level,
                                    STOP_LEVEL_4,
                                    STOP_REQ_DISABLE,
                                    STOP_ACT_DISABLE);
            // from now on, proceed with only deeper core
            core          = deeper_core;
            target_level  = deeper_level;
            deeper_level  = 0;
            deeper_core   = 0;
            entry_ongoing = 1;
        }

        if (core & CME_MASK_C0)
        {
            G_cme_stop_record.act_stop_c0 = STOP_LEVEL_4;
        }

        if (core & CME_MASK_C1)
        {
            G_cme_stop_record.act_stop_c1 = STOP_LEVEL_4;
        }

        PK_TRACE("SE4: Core Powered Off");

        //===========================
        MARK_TAG(SE_STOP4_DONE, core)
        //===========================

        //===========================
        MARK_TRAP(SE_IS1_BEGIN)
        //===========================
#if !SKIP_ABORT
        out32(CME_LCL_EIMR_CLR, /*TODO(core << SHIFT32(13)) |*/
              (core << SHIFT32(15)) |
              (core << SHIFT32(17)));
        sync();
        out32(CME_LCL_EIMR_OR,  (core << SHIFT32(13)) |
              (core << SHIFT32(15)) |
              (core << SHIFT32(17)));
#endif
        //===================
        MARK_TRAP(SE_IS1_END)
        //===================

        if ((core & CME_MASK_C0) &&
            G_cme_stop_record.act_stop_c0 == STOP_LEVEL_0 &&
            G_cme_stop_record.req_stop_c0 != STOP_LEVEL_0)
        {
            core_aborted |= CME_MASK_C0;
            core -= CME_MASK_C0;
            deeper_core = 0;

            if (deeper_core == CME_MASK_C1)
            {
                target_level = deeper_level;
            }

        }

        if ((core & CME_MASK_C1) &&
            G_cme_stop_record.act_stop_c1 == STOP_LEVEL_0 &&
            G_cme_stop_record.req_stop_c1 != STOP_LEVEL_0)
        {
            core_aborted |= CME_MASK_C1;
            core -= CME_MASK_C1;
            deeper_core = 0;

            if (deeper_core == CME_MASK_C0)
            {
                target_level = deeper_level;
            }
        }

        if (!core)
        {
            core |= core_aborted;
            entry_ongoing = 0;
        }

        // If we are done at STOP level 4 or aborted
        if (!entry_ongoing)
        {
            break;
        }

        PK_TRACE("SE4+:core[%d],deeper_core[%d],\
                  target_level[%d],deeper_level[%d]",
                 core, deeper_core, target_level, deeper_level);

        //--------------------------------------------------------------------------
        // STOP LEVEL 5 (preparsion of STOP LEVEL 8 and above)
        //--------------------------------------------------------------------------

        // block all wake up before purge L2,
        // this is last chance either core can exit
        out32(CME_LCL_EIMR_OR, BITS32(12, 6));

        if ((G_cme_stop_record.req_stop_c0 >= STOP_LEVEL_8) &&
            (G_cme_stop_record.req_stop_c1 >= STOP_LEVEL_8))
        {

            //=========================
            MARK_TAG(SE_PURGE_L2, core)
            //=========================

            // Assert L2+NCU Purges(chtm purge will be done in SGPE),
            // and NCU tlbie quiesce
            PK_TRACE("SE5.1a");
            out32(CME_LCL_SICR_OR, BIT32(18) | BIT32(21) | BIT32(22));

            // todo: poll for tlbie quiesce done?

            // Poll for Purged Done
            PK_TRACE("SE5.1b");
            //===========================
            MARK_TRAP(SE_IS2_BEGIN)

            //===========================
            do
            {
#if !SKIP_L2_PURGE_ABORT

                if(in32(CME_LCL_EINR) & BITS32(12, 6))
                {
                    // abort L2+NCU purges
                    out32(CME_LCL_SICR_OR, BIT32(19) | BIT32(23));
                }

#endif
            }
            while((in32(CME_LCL_EISR) & BITS32(22, 2)) != BITS32(22, 2));

            //===================
            MARK_TRAP(SE_IS2_END)
            //===================

            // Deassert L2+NCU Purges, their possible aborts, NCU tlbie quiesce
            PK_TRACE("SE5.1c");
            out32(CME_LCL_SICR_CLR, BITS32(18, 6));

            PK_TRACE("SE5.1: L2/NCU/CHTM Purged");

            //=========================
            MARK_TRAP(SE_L2_PURGE_DONE)
            //=========================
        }

        //=============================
        MARK_TAG(SE_SGPE_HANDOFF, core)
        //=============================

        // Update Stop History: In Core Stop Level 5
        PK_TRACE("SE5.2a");
        CME_STOP_UPDATE_HISTORY(core,
                                STOP_CORE_IS_GATED,
                                STOP_TRANS_CORE_PORTION,
                                target_level,
                                STOP_LEVEL_5,
                                STOP_REQ_DISABLE,
                                STOP_ACT_ENABLE);

        if (core & CME_MASK_C0)
        {
            G_cme_stop_record.act_stop_c0 = STOP_LEVEL_5;
        }

        if (core & CME_MASK_C1)
        {
            G_cme_stop_record.act_stop_c1 = STOP_LEVEL_5;
        }

        // Send PCB Interrupt per core
        PK_TRACE("SE5.2b");
        pig.fields.req_intr_type = 2; //0b010: STOP State Change

        if (core & CME_MASK_C0)
        {
            pig.fields.req_intr_payload = G_cme_stop_record.req_stop_c0;
            CME_PUTSCOM(PPM_PIG, CME_MASK_C0, pig.value);
        }

        if (core & CME_MASK_C1)
        {
            pig.fields.req_intr_payload = G_cme_stop_record.req_stop_c1;
            CME_PUTSCOM(PPM_PIG, CME_MASK_C1, pig.value);
        }

        // Change PPM Wakeup to STOPGPE
        PK_TRACE("SE5.2c");
        CME_PUTSCOM(CPPM_CPMMR_OR, core, BIT64(13));

        PK_TRACE("SE5.2: Handed off to SGPE");

    }
    while(0);

    //--------------------------------------------------------------------------
    // END OF STOP ENTRY
    //--------------------------------------------------------------------------

    // Release PPM Write Protection
    CME_PUTSCOM(CPPM_CPMMR_CLR, core, BIT64(0));

    // Enable Wakeup Interrupts of this entry core if no handoff to SGPE
    // Otherwise enable Doorbell interrupts of this entry core
    out32(CME_LCL_EIMR_CLR,
          ((~core & G_cme_stop_record.active_core & CME_MASK_BC) << SHIFT32(21)));

    if (!entry_ongoing)
        out32(CME_LCL_EIMR_CLR,
              /*TODO((core & ~core_aborted & CME_MASK_BC) << SHIFT32(13)) |*/
              ((core & ~core_aborted & CME_MASK_BC) << SHIFT32(15)) |
              ((core & ~core_aborted & CME_MASK_BC) << SHIFT32(17)));
    else
    {
        out32_sh(CME_LCL_EIMR_CLR, (core << SHIFT32((41 - 32))));
    }

    //============================
    MARK_TRAP(ENDSCOPE_STOP_ENTRY)
    //============================

    return CME_STOP_SUCCESS;
}
OpenPOWER on IntegriCloud