aboutsummaryrefslogtreecommitdiff
path: root/module/ppu_v1/src/mod_ppu_v1.c
blob: 116c22a5d400482ffa90e278d99f55a24bae3fea (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
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
/*
 * Arm SCP/MCP Software
 * Copyright (c) 2015-2019, Arm Limited and Contributors. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *
 * Description:
 *     Power State Management PPU v1 driver.
 */

#include <stdbool.h>
#include <fwk_assert.h>
#include <fwk_id.h>
#include <fwk_interrupt.h>
#include <fwk_macros.h>
#include <fwk_mm.h>
#include <fwk_module.h>
#include <fwk_module_idx.h>
#include <fwk_notification.h>
#include <mod_log.h>
#include <mod_power_domain.h>
#include <mod_ppu_v1.h>
#include <ppu_v1.h>
#if BUILD_HAS_MOD_SYSTEM_POWER
#include <mod_system_power.h>
#endif

#define CORE_PER_CLUSTER_COUNT_MAX 8

/* Power domain context */
struct ppu_v1_pd_ctx {
    /* Power domain configuration data */
    const struct mod_ppu_v1_pd_config *config;

    /* PPU registers */
    struct ppu_v1_reg *ppu;

    /* Identifier of the entity bound to the power domain driver API */
    fwk_id_t bound_id;

    /* Power module driver input API */
    struct mod_pd_driver_input_api *pd_driver_input_api;

    /* Context of the parent power domain (used only for core power domains) */
    struct ppu_v1_pd_ctx *parent_pd_ctx;

    /* Pointer to the power state observer API */
    const struct mod_ppu_v1_power_state_observer_api *observer_api;

    /* Context data specific to the type of power domain */
    void *data;
};

/* Cluster power domain specific context */
struct ppu_v1_cluster_pd_ctx {
    /*
     * Table of pointers to the contexts of the cores being part of the
     * cluster.
     */
    struct ppu_v1_pd_ctx *core_pd_ctx_table[CORE_PER_CLUSTER_COUNT_MAX];

    /* Number of cores */
    unsigned int core_count;
};

/* Module context */
struct ppu_v1_ctx {
    /* Table of the power domain contexts */
    struct ppu_v1_pd_ctx *pd_ctx_table;

    /* Number of power domains */
    size_t pd_ctx_table_size;

    /* Log API */
    struct mod_log_api *log_api;
};

/*
 * Internal variables
 */

static struct ppu_v1_ctx ppu_v1_ctx;

#define MODE_UNSUPPORTED        ~0U
static const uint8_t ppu_mode_to_power_state[] = {
    [PPU_V1_MODE_OFF]         = (uint8_t)MOD_PD_STATE_OFF,
    [PPU_V1_MODE_OFF_EMU]     = (uint8_t)MOD_PD_STATE_OFF,
    [PPU_V1_MODE_MEM_RET]     = (uint8_t)MOD_PD_STATE_OFF,
    [PPU_V1_MODE_MEM_RET_EMU] = (uint8_t)MOD_PD_STATE_OFF,
    [PPU_V1_MODE_LOGIC_RET]   = (uint8_t)MOD_PD_STATE_ON,
    [PPU_V1_MODE_FULL_RET]    = (uint8_t)MOD_PD_STATE_ON,
    [PPU_V1_MODE_MEM_OFF]     = (uint8_t)MOD_PD_STATE_ON,
    [PPU_V1_MODE_FUNC_RET]    = (uint8_t)MOD_PD_STATE_ON,
    [PPU_V1_MODE_ON]          = (uint8_t)MOD_PD_STATE_ON,
    [PPU_V1_MODE_WARM_RST]    = (uint8_t)MODE_UNSUPPORTED,
    [PPU_V1_MODE_DBG_RECOV]   = (uint8_t)MODE_UNSUPPORTED
};

/*
 * Functions not specific to any type of power domain
 */

static int get_state(struct ppu_v1_reg *ppu, unsigned int *state)
{
    enum ppu_v1_mode mode;

    /* Ensure ppu_to_pd_state_v1 has an entry for each PPU state */
    static_assert((FWK_ARRAY_SIZE(ppu_mode_to_power_state) ==
        PPU_V1_MODE_COUNT), "[PPU_V1] ppu_mode_to_power_state size error");

    mode = ppu_v1_get_power_mode(ppu);
    assert(mode < PPU_V1_MODE_COUNT);

    *state = ppu_mode_to_power_state[mode];

    if ((*state == MOD_PD_STATE_OFF) && (ppu_v1_is_dynamic_enabled(ppu)))
        *state = MOD_PD_STATE_SLEEP;

    if (*state == MODE_UNSUPPORTED) {
        ppu_v1_ctx.log_api->log(MOD_LOG_GROUP_ERROR,
                                "[PPU_V1] Unexpected PPU mode (%i).\n", mode);
        return FWK_E_DEVICE;
    }

    return FWK_SUCCESS;
}

static int ppu_v1_pd_set_state(fwk_id_t pd_id, unsigned int state)
{
    int status;
    struct ppu_v1_pd_ctx *pd_ctx;

    status = fwk_module_check_call(pd_id);
    if (status != FWK_SUCCESS)
        return status;

    pd_ctx = ppu_v1_ctx.pd_ctx_table + fwk_id_get_element_idx(pd_id);

    switch (state) {
    case MOD_PD_STATE_ON:
        ppu_v1_set_power_mode(pd_ctx->ppu, PPU_V1_MODE_ON);
        status = pd_ctx->pd_driver_input_api->report_power_state_transition(
            pd_ctx->bound_id, MOD_PD_STATE_ON);
        assert(status == FWK_SUCCESS);
        break;

    case MOD_PD_STATE_OFF:
        ppu_v1_set_power_mode(pd_ctx->ppu, PPU_V1_MODE_OFF);
        status = pd_ctx->pd_driver_input_api->report_power_state_transition(
            pd_ctx->bound_id, MOD_PD_STATE_OFF);
        assert(status == FWK_SUCCESS);
        break;

    default:
        ppu_v1_ctx.log_api->log(MOD_LOG_GROUP_ERROR,
            "[PD] Requested power state (%i) is not supported.\n", state);
        return FWK_E_PARAM;
    }

    return FWK_SUCCESS;
}

static int ppu_v1_pd_get_state(fwk_id_t pd_id, unsigned int *state)
{
    int status;
    struct ppu_v1_pd_ctx *pd_ctx;

    status = fwk_module_check_call(pd_id);
    if (status != FWK_SUCCESS)
        return status;

    pd_ctx = ppu_v1_ctx.pd_ctx_table + fwk_id_get_element_idx(pd_id);

    return get_state(pd_ctx->ppu, state);
}

static int ppu_v1_pd_reset(fwk_id_t pd_id)
{
    int status;
    struct ppu_v1_pd_ctx *pd_ctx;

    status = fwk_module_check_call(pd_id);
    if (status != FWK_SUCCESS)
        return status;

    pd_ctx = ppu_v1_ctx.pd_ctx_table + fwk_id_get_element_idx(pd_id);

    /* Model does not support warm reset at the moment. Using OFF instead. */
    status = ppu_v1_set_power_mode(pd_ctx->ppu, PPU_V1_MODE_OFF);
    if (status == FWK_SUCCESS)
        status = ppu_v1_set_power_mode(pd_ctx->ppu, PPU_V1_MODE_ON);

    return status;
}

static const struct mod_pd_driver_api pd_driver = {
    .set_state = ppu_v1_pd_set_state,
    .get_state = ppu_v1_pd_get_state,
    .reset = ppu_v1_pd_reset,
};

/*
 * Functions specific to core power domains
 */
static int ppu_v1_core_pd_init(struct ppu_v1_pd_ctx *pd_ctx)
{
    int status;
    struct ppu_v1_reg *ppu = pd_ctx->ppu;
    unsigned int state;

    ppu_v1_init(ppu);

    status = get_state(ppu, &state);
    if (status != FWK_SUCCESS)
        return status;

    if (state == MOD_PD_STATE_ON) {
        ppu_v1_interrupt_unmask(ppu, PPU_V1_IMR_DYN_POLICY_MIN_IRQ_MASK);
        ppu_v1_dynamic_enable(ppu, PPU_V1_MODE_OFF);
    }

    return FWK_SUCCESS;
}

static int ppu_v1_core_pd_set_state(fwk_id_t core_pd_id, unsigned int state)
{
    int status;
    struct ppu_v1_pd_ctx *pd_ctx;
    struct ppu_v1_reg *ppu;

    status = fwk_module_check_call(core_pd_id);
    if (status != FWK_SUCCESS)
        return status;

    pd_ctx = ppu_v1_ctx.pd_ctx_table + fwk_id_get_element_idx(core_pd_id);
    ppu = pd_ctx->ppu;

    switch (state) {
    case MOD_PD_STATE_OFF:
        ppu_v1_set_input_edge_sensitivity(ppu,
                                          PPU_V1_MODE_ON,
                                          PPU_V1_EDGE_SENSITIVITY_MASKED);
        ppu_v1_interrupt_mask(ppu, PPU_V1_IMR_DYN_POLICY_MIN_IRQ_MASK);
        ppu_v1_set_power_mode(ppu, PPU_V1_MODE_OFF);
        ppu_v1_lock_off_disable(ppu);
        ppu_v1_off_unlock(ppu);
        status = pd_ctx->pd_driver_input_api->report_power_state_transition(
            pd_ctx->bound_id, MOD_PD_STATE_OFF);
        assert(status == FWK_SUCCESS);
        break;

    case MOD_PD_STATE_ON:
        ppu_v1_interrupt_unmask(ppu, PPU_V1_IMR_DYN_POLICY_MIN_IRQ_MASK);
        ppu_v1_set_input_edge_sensitivity(ppu,
                                          PPU_V1_MODE_ON,
                                          PPU_V1_EDGE_SENSITIVITY_MASKED);
        ppu_v1_set_power_mode(ppu, PPU_V1_MODE_ON);
        ppu_v1_dynamic_enable(ppu, PPU_V1_MODE_OFF);
        status = pd_ctx->pd_driver_input_api->report_power_state_transition(
            pd_ctx->bound_id, MOD_PD_STATE_ON);
        assert(status == FWK_SUCCESS);
        break;

    case MOD_PD_STATE_SLEEP:
        /*
         * If the dynamic transitions have been enabled then the core is
         * already in the SLEEP power state or will transit to the SLEEP power
         * state if the appropriate processing is done on AP side. Thus nothing
         * to do in that case. If the dynamic transitions are not enabled then
         * this is an OFF to SLEEP transition.
         */
        if (!ppu_v1_is_dynamic_enabled(ppu)) {
            ppu_v1_dynamic_enable(ppu, PPU_V1_MODE_OFF);
            ppu_v1_set_input_edge_sensitivity(ppu,
                                              PPU_V1_MODE_ON,
                                              PPU_V1_EDGE_SENSITIVITY_MASKED);
        }
        status = pd_ctx->pd_driver_input_api->report_power_state_transition(
            pd_ctx->bound_id, MOD_PD_STATE_SLEEP);
        assert(status == FWK_SUCCESS);
        break;

    default:
        ppu_v1_ctx.log_api->log(MOD_LOG_GROUP_ERROR,
            "[PPU_V1] Requested CPU power state (%i) is not supported!\n",
            state);
        return FWK_E_PARAM;
    }

    return FWK_SUCCESS;
}

static int ppu_v1_core_pd_reset(fwk_id_t core_pd_id)
{
    int status;

    status = ppu_v1_core_pd_set_state(core_pd_id, MOD_PD_STATE_OFF);
    if (status == FWK_SUCCESS)
        status = ppu_v1_core_pd_set_state(core_pd_id, MOD_PD_STATE_ON);

    return status;
}

static int ppu_v1_core_pd_prepare_for_system_suspend(fwk_id_t core_pd_id)
{
    int status;
    struct ppu_v1_pd_ctx *pd_ctx;
    struct ppu_v1_reg *ppu;

    status = fwk_module_check_call(core_pd_id);
    if (status != FWK_SUCCESS)
        return status;

    pd_ctx = ppu_v1_ctx.pd_ctx_table + fwk_id_get_element_idx(core_pd_id);
    ppu = pd_ctx->ppu;

    ppu_v1_set_input_edge_sensitivity(ppu,
                                      PPU_V1_MODE_ON,
                                      PPU_V1_EDGE_SENSITIVITY_MASKED);
    ppu_v1_request_power_mode(ppu, PPU_V1_MODE_OFF);

    return FWK_SUCCESS;
}

static void core_pd_ppu_interrupt_handler(struct ppu_v1_pd_ctx *pd_ctx)
{
    int status;
    struct ppu_v1_reg *ppu;

    ppu = pd_ctx->ppu;

    /* ON request interrupt */
    if (ppu_v1_is_power_active_edge_interrupt(ppu, PPU_V1_MODE_ON)) {
        ppu_v1_ack_power_active_edge_interrupt(ppu, PPU_V1_MODE_ON);
        ppu_v1_set_input_edge_sensitivity(ppu,
                                          PPU_V1_MODE_ON,
                                          PPU_V1_EDGE_SENSITIVITY_MASKED);
        ppu_v1_interrupt_unmask(ppu, PPU_V1_IMR_DYN_POLICY_MIN_IRQ_MASK);

        status = pd_ctx->pd_driver_input_api->report_power_state_transition(
            pd_ctx->bound_id, MOD_PD_STATE_ON);
        assert(status == FWK_SUCCESS);
        (void)status;
    /* Minimum policy reached interrupt */
    } else if (ppu_v1_is_dyn_policy_min_interrupt(ppu)) {
        ppu_v1_ack_interrupt(ppu, PPU_V1_ISR_DYN_POLICY_MIN_IRQ);
        ppu_v1_interrupt_mask(ppu, PPU_V1_IMR_DYN_POLICY_MIN_IRQ_MASK);

        status = pd_ctx->pd_driver_input_api->report_power_state_transition(
            pd_ctx->bound_id, MOD_PD_STATE_SLEEP);
        assert(status == FWK_SUCCESS);
        (void)status;

        /*
         * Enable the core PACTIVE ON signal rising edge interrupt then check if
         * the PACTIVE ON signal is high. If it is high, we may have missed the
         * transition from low to high. In that case, just disable the interrupt
         * and acknowledge it in case it is pending. There is no need to send an
         * update request as one has already been queued.
         */
        ppu_v1_set_input_edge_sensitivity(ppu,
                                          PPU_V1_MODE_ON,
                                          PPU_V1_EDGE_SENSITIVITY_RISING_EDGE);
        if (ppu_v1_is_power_devactive_high(ppu, PPU_V1_MODE_ON)) {
            ppu_v1_set_input_edge_sensitivity(ppu,
                                              PPU_V1_MODE_ON,
                                              PPU_V1_EDGE_SENSITIVITY_MASKED);
            ppu_v1_ack_power_active_edge_interrupt(ppu, PPU_V1_MODE_ON);
            ppu_v1_interrupt_unmask(ppu, PPU_V1_IMR_DYN_POLICY_MIN_IRQ_MASK);
        }
    }
}

static const struct mod_pd_driver_api core_pd_driver = {
    .set_state = ppu_v1_core_pd_set_state,
    .get_state = ppu_v1_pd_get_state,
    .reset = ppu_v1_core_pd_reset,
    .prepare_core_for_system_suspend = ppu_v1_core_pd_prepare_for_system_suspend
};

/*
 * Functions specific to cluster power domains
 */

static void unlock_all_cores(struct ppu_v1_pd_ctx *pd_ctx)
{
    struct ppu_v1_cluster_pd_ctx *cluster_pd_ctx;
    struct ppu_v1_reg *cpu_ppu;
    unsigned int core_idx;

    assert(pd_ctx != NULL);

    cluster_pd_ctx = pd_ctx->data;

    for (core_idx = 0; core_idx < cluster_pd_ctx->core_count; ++core_idx) {
        cpu_ppu = cluster_pd_ctx->core_pd_ctx_table[core_idx]->ppu;
        ppu_v1_lock_off_disable(cpu_ppu);
        ppu_v1_off_unlock(cpu_ppu);
    }
}

static bool lock_all_dynamic_cores(struct ppu_v1_pd_ctx *pd_ctx)
{
    struct ppu_v1_cluster_pd_ctx *cluster_pd_ctx;
    struct ppu_v1_reg *cpu_ppu;
    unsigned int core_idx;

    assert(pd_ctx != NULL);

    cluster_pd_ctx = pd_ctx->data;

    for (core_idx = 0; core_idx < cluster_pd_ctx->core_count; ++core_idx) {
        cpu_ppu = cluster_pd_ctx->core_pd_ctx_table[core_idx]->ppu;

        if (!ppu_v1_is_dynamic_enabled(cpu_ppu))
            continue;

        ppu_v1_lock_off_enable(cpu_ppu);
        while ((!ppu_v1_is_locked(cpu_ppu)) &&
               (!ppu_v1_is_power_devactive_high(cpu_ppu, PPU_V1_MODE_ON)))
            continue;

        if (ppu_v1_is_power_devactive_high(cpu_ppu, PPU_V1_MODE_ON))
            return false;
    }

    return true;
}

static bool cluster_off(struct ppu_v1_pd_ctx *pd_ctx)
{
    struct ppu_v1_reg *ppu;
    bool lock_successful;

    assert(pd_ctx != NULL);

    ppu = pd_ctx->ppu;

    ppu_v1_set_input_edge_sensitivity(ppu,
                                      PPU_V1_MODE_ON,
                                      PPU_V1_EDGE_SENSITIVITY_MASKED);

    lock_successful = lock_all_dynamic_cores(pd_ctx);
    if (!lock_successful) {
        unlock_all_cores(pd_ctx);
        return false;
    }

    ppu_v1_set_power_mode(ppu, PPU_V1_MODE_OFF);
    return true;
}

static void cluster_on(struct ppu_v1_pd_ctx *pd_ctx)
{
    int status;
    struct ppu_v1_reg *ppu;

    assert(pd_ctx != NULL);

    ppu = pd_ctx->ppu;

    ppu_v1_set_input_edge_sensitivity(ppu,
                                      PPU_V1_MODE_ON,
                                      PPU_V1_EDGE_SENSITIVITY_MASKED);

    ppu_v1_set_power_mode(ppu, PPU_V1_MODE_ON);
    status = pd_ctx->pd_driver_input_api->report_power_state_transition(
        pd_ctx->bound_id, MOD_PD_STATE_ON);
    assert(status == FWK_SUCCESS);
    (void)status;

    if (pd_ctx->observer_api != NULL)
        pd_ctx->observer_api->post_ppu_on(pd_ctx->config->post_ppu_on_param);

    unlock_all_cores(pd_ctx);
}

static int ppu_v1_cluster_pd_init(struct ppu_v1_pd_ctx *pd_ctx)
{
    int status;
    struct ppu_v1_reg *ppu = pd_ctx->ppu;
    unsigned int state;

    ppu_v1_init(ppu);

    status = get_state(ppu, &state);
    if (status != FWK_SUCCESS)
        return status;

    /* For clusters with operating mode support, enable the dynamic support */
    if (ppu_v1_get_num_opmode(ppu) > 1)
        ppu_v1_opmode_dynamic_enable(ppu, PPU_V1_OPMODE_00);

    if (state == MOD_PD_STATE_ON) {
        ppu_v1_set_input_edge_sensitivity(ppu,
                                          PPU_V1_MODE_ON,
                                          PPU_V1_EDGE_SENSITIVITY_FALLING_EDGE);
    }

    return FWK_SUCCESS;
}

static int ppu_v1_cluster_pd_set_state(fwk_id_t cluster_pd_id,
                                       unsigned int state)
{
    int status;
    struct ppu_v1_pd_ctx *pd_ctx;
    struct ppu_v1_reg *ppu;
    (void)ppu;

    status = fwk_module_check_call(cluster_pd_id);
    if (status != FWK_SUCCESS)
        return status;

    pd_ctx = ppu_v1_ctx.pd_ctx_table + fwk_id_get_element_idx(cluster_pd_id);
    ppu = pd_ctx->ppu;

    switch (state) {
    case MOD_PD_STATE_ON:
        cluster_on(pd_ctx);
        #ifdef BUILD_HAS_MULTITHREADING
        ppu_v1_set_input_edge_sensitivity(ppu,
                                          PPU_V1_MODE_ON,
                                          PPU_V1_EDGE_SENSITIVITY_FALLING_EDGE);
        #endif
        return FWK_SUCCESS;

    case MOD_PD_STATE_OFF:
        if (!cluster_off(pd_ctx)) {
            /* Cluster failed to transition to off */
            #ifdef BUILD_HAS_MULTITHREADING
            ppu_v1_set_input_edge_sensitivity(ppu,
                PPU_V1_MODE_ON, PPU_V1_EDGE_SENSITIVITY_FALLING_EDGE);
            #endif
            return FWK_E_STATE;
        }
        status = pd_ctx->pd_driver_input_api->report_power_state_transition(
            pd_ctx->bound_id, MOD_PD_STATE_OFF);
        assert(status == FWK_SUCCESS);
        return FWK_SUCCESS;

    default:
        ppu_v1_ctx.log_api->log(MOD_LOG_GROUP_ERROR,
            "[PPU_V1] Requested CPU power state (%i) is not supported!\n",
            state);
        return FWK_E_PARAM;
    }
}

static void cluster_pd_ppu_interrupt_handler(struct ppu_v1_pd_ctx *pd_ctx)
{
    int status;
    struct ppu_v1_reg *ppu;
    enum ppu_v1_mode current_mode;

    assert(pd_ctx != NULL);

    ppu = pd_ctx->ppu;

    if (!ppu_v1_is_power_active_edge_interrupt(ppu, PPU_V1_MODE_ON))
        return; /* Spurious interrupt */

    ppu_v1_ack_power_active_edge_interrupt(ppu, PPU_V1_MODE_ON);
    current_mode = ppu_v1_get_power_mode(ppu);

    switch (current_mode) {
    case PPU_V1_MODE_OFF:
        /* Cluster has to be powered on */
        cluster_on(pd_ctx);
        ppu_v1_set_input_edge_sensitivity(ppu,
                                          PPU_V1_MODE_ON,
                                          PPU_V1_EDGE_SENSITIVITY_FALLING_EDGE);
        return;

    case PPU_V1_MODE_ON:
        /*
         * It may be possible to turn off the cluster, check all PACTIVE lines
         * to make sure it is not just requesting a low power mode.
         */
        while (current_mode > 0) {
            if (ppu_v1_is_power_devactive_high(ppu, current_mode--))
                return;
        }

        /* All PACTIVE lines are low, so the cluster can be turned off */
        if (cluster_off(pd_ctx)) {
            /* Cluster successfuly transitioned to off */
            ppu_v1_set_input_edge_sensitivity(ppu,
                PPU_V1_MODE_ON, PPU_V1_EDGE_SENSITIVITY_RISING_EDGE);
            status = pd_ctx->pd_driver_input_api->report_power_state_transition(
                pd_ctx->bound_id, MOD_PD_STATE_SLEEP);
            assert(status == FWK_SUCCESS);
            (void)status;
        } else {
            /* Cluster did not transition to off */
            ppu_v1_set_input_edge_sensitivity(ppu,
                PPU_V1_MODE_ON, PPU_V1_EDGE_SENSITIVITY_FALLING_EDGE);
        }
        return;

    default:
        /* Cluster is in an invalid power mode */
        assert(false);
        return;
    }
}

static const struct mod_pd_driver_api cluster_pd_driver = {
    .set_state = ppu_v1_cluster_pd_set_state,
    .get_state = ppu_v1_pd_get_state,
    .reset = ppu_v1_pd_reset,
};

static void ppu_interrupt_handler(uintptr_t pd_ctx_param)
{
    struct ppu_v1_pd_ctx *pd_ctx = (struct ppu_v1_pd_ctx *)pd_ctx_param;

    assert(pd_ctx != NULL);

    if (pd_ctx->config->pd_type == MOD_PD_TYPE_CORE)
        core_pd_ppu_interrupt_handler(pd_ctx);
    else
        cluster_pd_ppu_interrupt_handler(pd_ctx);
}

static void ppu_isr_api_interrupt_handler(fwk_id_t pd_id)
{
    int status;
    struct ppu_v1_pd_ctx *pd_ctx;

    status = fwk_module_check_call(pd_id);
    if (status != FWK_SUCCESS)
        return;

    if (!fwk_id_is_type(pd_id, FWK_ID_TYPE_ELEMENT))
        return;

    pd_ctx = ppu_v1_ctx.pd_ctx_table + fwk_id_get_element_idx(pd_id);
    ppu_interrupt_handler((uintptr_t)pd_ctx);
}

static const struct ppu_v1_isr_api isr_api = {
    .ppu_interrupt_handler = ppu_isr_api_interrupt_handler,
};

static int ppu_power_mode_on(fwk_id_t pd_id)
{
    int status;
    struct ppu_v1_pd_ctx *pd_ctx;

    status = fwk_module_check_call(pd_id);
    if (status != FWK_SUCCESS)
        return status;

    if (!fwk_id_is_type(pd_id, FWK_ID_TYPE_ELEMENT))
        return FWK_E_PARAM;

    pd_ctx = ppu_v1_ctx.pd_ctx_table + fwk_id_get_element_idx(pd_id);

    return ppu_v1_set_power_mode(pd_ctx->ppu, PPU_V1_MODE_ON);
}

static const struct ppu_v1_boot_api boot_api = {
    .power_mode_on = ppu_power_mode_on,
};

/*
 * Framework handlers
 */

static int ppu_v1_mod_init(fwk_id_t module_id, unsigned int pd_count,
                           const void *unused)
{
    ppu_v1_ctx.pd_ctx_table = fwk_mm_calloc(pd_count,
                                            sizeof(struct ppu_v1_pd_ctx));
    if (ppu_v1_ctx.pd_ctx_table == NULL)
        return FWK_E_NOMEM;

    ppu_v1_ctx.pd_ctx_table_size = pd_count;

    return FWK_SUCCESS;
}

static int ppu_v1_pd_init(fwk_id_t pd_id, unsigned int unused, const void *data)
{
    const struct mod_ppu_v1_pd_config *config = data;
    struct ppu_v1_pd_ctx *pd_ctx;

    if (config->pd_type >= MOD_PD_TYPE_COUNT)
        return FWK_E_DATA;

    pd_ctx = ppu_v1_ctx.pd_ctx_table + fwk_id_get_element_idx(pd_id);
    pd_ctx->config = config;
    pd_ctx->ppu = (struct ppu_v1_reg *)(config->ppu.reg_base);
    pd_ctx->bound_id = FWK_ID_NONE;

    if (config->ppu.irq != FWK_INTERRUPT_NONE) {
        fwk_interrupt_set_isr_param(config->ppu.irq,
                                    ppu_interrupt_handler,
                                    (uintptr_t)pd_ctx);
    }

    if (config->pd_type == MOD_PD_TYPE_CLUSTER) {
        pd_ctx->data = fwk_mm_calloc(1, sizeof(struct ppu_v1_cluster_pd_ctx));
        if (pd_ctx->data == NULL)
            return FWK_E_NOMEM;
    }

    if (config->default_power_on) {
        switch (config->pd_type) {
        case MOD_PD_TYPE_DEVICE:
            /* Fall through */
        case MOD_PD_TYPE_DEVICE_DEBUG:
            /* Fall through */
        case MOD_PD_TYPE_SYSTEM:
            ppu_v1_init(pd_ctx->ppu);
            return ppu_v1_set_power_mode(pd_ctx->ppu, PPU_V1_MODE_ON);

        default:
            assert(false);
            return FWK_E_SUPPORT;
        }
    }

    return FWK_SUCCESS;
}

static int ppu_v1_post_init(fwk_id_t module_id)
{
    unsigned int pd_idx;
    struct ppu_v1_pd_ctx *pd_ctx, *cluster_pd_ctx;
    const struct mod_ppu_v1_pd_config *config;
    fwk_id_t cluster_id;
    struct ppu_v1_cluster_pd_ctx *cluster_pd_specific_ctx;

    for (pd_idx = 0; pd_idx < ppu_v1_ctx.pd_ctx_table_size; pd_idx++) {
        pd_ctx = &ppu_v1_ctx.pd_ctx_table[pd_idx];
        config = pd_ctx->config;
        if (config->pd_type != MOD_PD_TYPE_CORE)
            continue;

        cluster_id = config->cluster_id;

        if ((!fwk_module_is_valid_element_id(cluster_id)) ||
            (fwk_id_get_module_idx(cluster_id) != FWK_MODULE_IDX_PPU_V1))
            return FWK_E_PARAM;

        cluster_pd_ctx = &ppu_v1_ctx.pd_ctx_table[
            fwk_id_get_element_idx(cluster_id)];
        cluster_pd_specific_ctx = cluster_pd_ctx->data;

       if (cluster_pd_specific_ctx->core_count >= CORE_PER_CLUSTER_COUNT_MAX)
           return FWK_E_NOMEM;

       cluster_pd_specific_ctx->core_pd_ctx_table[
           cluster_pd_specific_ctx->core_count++] = pd_ctx;
       pd_ctx->parent_pd_ctx = cluster_pd_ctx;
    }

    return FWK_SUCCESS;
}

static int ppu_v1_bind(fwk_id_t id, unsigned int round)
{
    int status = FWK_SUCCESS;
    struct ppu_v1_pd_ctx *pd_ctx;

    /* Nothing to do during the first round of calls where the power module
       will bind to the power domains of this module. */
    if (round == 0)
        return FWK_SUCCESS;

    /* In the case of the module, bind to the log component */
    if (fwk_id_is_type(id, FWK_ID_TYPE_MODULE)) {
        status = fwk_module_bind(FWK_ID_MODULE(FWK_MODULE_IDX_LOG),
                                 FWK_ID_API(FWK_MODULE_IDX_LOG, 0),
                                 &ppu_v1_ctx.log_api);
        return status;
    }

    pd_ctx = ppu_v1_ctx.pd_ctx_table + fwk_id_get_element_idx(id);

    if (!fwk_id_is_equal(pd_ctx->config->observer_id, FWK_ID_NONE)) {
        if (pd_ctx->config->pd_type != MOD_PD_TYPE_CLUSTER) {
            /* State observation only supported for clusters */
            assert(false);
            return FWK_E_SUPPORT;
        }

        status = fwk_module_bind(pd_ctx->config->observer_id,
                                 pd_ctx->config->observer_api,
                                 &pd_ctx->observer_api);
        if (status != FWK_SUCCESS)
            return status;
    }

    if (fwk_id_is_equal(pd_ctx->bound_id, FWK_ID_NONE))
        return FWK_SUCCESS;

    switch (fwk_id_get_module_idx(pd_ctx->bound_id)) {
    #if BUILD_HAS_MOD_POWER_DOMAIN
    case FWK_MODULE_IDX_POWER_DOMAIN:
        return fwk_module_bind(pd_ctx->bound_id,
                               mod_pd_api_id_driver_input,
                               &pd_ctx->pd_driver_input_api);
        break;
    #endif

    #if BUILD_HAS_MOD_SYSTEM_POWER
    case FWK_MODULE_IDX_SYSTEM_POWER:
        return fwk_module_bind(pd_ctx->bound_id,
                               mod_system_power_api_id_pd_driver_input,
                               &pd_ctx->pd_driver_input_api);
        break;
    #endif

    default:
        assert(false);
        return FWK_E_SUPPORT;
    }
}

static int ppu_v1_process_bind_request(fwk_id_t source_id,
                                       fwk_id_t target_id, fwk_id_t api_id,
                                       const void **api)
{
    struct ppu_v1_pd_ctx *pd_ctx;
    unsigned int api_idx;
    bool is_power_domain_module = false;
    bool is_system_power_module = false;

    api_idx = fwk_id_get_api_idx(api_id);

    if (api_idx == MOD_PPU_V1_API_IDX_ISR) {
        if (!fwk_id_is_type(target_id, FWK_ID_TYPE_MODULE))
            return FWK_E_SUPPORT;

        *api = &isr_api;
        return FWK_SUCCESS;
    }

    if (api_idx == MOD_PPU_V1_API_IDX_BOOT) {
        *api = &boot_api;
        return FWK_SUCCESS;
    }

    if (api_idx != MOD_PPU_V1_API_IDX_POWER_DOMAIN_DRIVER)
        return FWK_E_SUPPORT;

    if (!fwk_module_is_valid_element_id(target_id))
        return FWK_E_PARAM;

    pd_ctx = ppu_v1_ctx.pd_ctx_table + fwk_id_get_element_idx(target_id);

    /* Allow multiple binding only for device power domain for now */
    if ((pd_ctx->config->pd_type != MOD_PD_TYPE_DEVICE) &&
        (!fwk_id_is_equal(pd_ctx->bound_id, FWK_ID_NONE))) {
        assert(false);
        return FWK_E_ACCESS;
    }

    #if BUILD_HAS_MOD_POWER_DOMAIN
    is_power_domain_module = (fwk_id_get_module_idx(source_id) ==
        FWK_MODULE_IDX_POWER_DOMAIN);
    #endif
    #if BUILD_HAS_MOD_SYSTEM_POWER
    is_system_power_module = (fwk_id_get_module_idx(source_id) ==
        FWK_MODULE_IDX_SYSTEM_POWER);
    #endif

    switch (pd_ctx->config->pd_type) {
    case MOD_PD_TYPE_CORE:
        if (is_power_domain_module) {
            *api = &core_pd_driver;
            pd_ctx->bound_id = source_id;
            return FWK_SUCCESS;
        }
        break;

    case MOD_PD_TYPE_CLUSTER:
        if (is_power_domain_module) {
            *api = &cluster_pd_driver;
            pd_ctx->bound_id = source_id;
            return FWK_SUCCESS;
        }
        break;

    case MOD_PD_TYPE_SYSTEM:
        if (is_power_domain_module || is_system_power_module) {
            *api = &pd_driver;
            pd_ctx->bound_id = source_id;
            return FWK_SUCCESS;
        }
        break;

    default:
        if (is_power_domain_module)
            pd_ctx->bound_id = source_id;
        *api = &pd_driver;
        return FWK_SUCCESS;
    }

    pd_ctx->bound_id = FWK_ID_NONE;
    return FWK_E_ACCESS;
}

static int ppu_v1_start(fwk_id_t id)
{
    int status;
    struct ppu_v1_pd_ctx *pd_ctx;
    const struct mod_ppu_v1_config *module_config;

    if (!fwk_id_is_type(id, FWK_ID_TYPE_ELEMENT))
        return FWK_SUCCESS;

    pd_ctx = ppu_v1_ctx.pd_ctx_table + fwk_id_get_element_idx(id);
    module_config = fwk_module_get_data(fwk_id_build_module_id(id));
    assert(module_config != NULL);

    /* Register for power domain transition notifications */
    status = fwk_notification_subscribe(
        module_config->pd_notification_id,
        module_config->pd_source_id,
        id);
    if (status != FWK_SUCCESS)
        return status;

    switch (pd_ctx->config->pd_type) {
    case MOD_PD_TYPE_CORE:
    case MOD_PD_TYPE_CLUSTER:
        fwk_interrupt_clear_pending(pd_ctx->config->ppu.irq);
        fwk_interrupt_enable(pd_ctx->config->ppu.irq);
        break;
    default:
        /* Nothing to be done for other types */
        break;
    }

    return FWK_SUCCESS;
}

static int ppu_v1_process_notification(
    const struct fwk_event *event,
    struct fwk_event *resp_event)
{
    const struct mod_ppu_v1_config *module_config;
    struct ppu_v1_pd_ctx *pd_ctx;
    struct mod_pd_power_state_transition_notification_params *params;

    assert(fwk_id_is_type(event->target_id, FWK_ID_TYPE_ELEMENT));
    module_config =
        fwk_module_get_data(fwk_id_build_module_id(event->target_id));
    assert(
        fwk_id_is_equal(
            event->id,
            module_config->pd_notification_id));
    (void)module_config;

    params = (struct mod_pd_power_state_transition_notification_params *)
        event->params;

    if (params->state != MOD_PD_STATE_ON)
        return FWK_SUCCESS;

    pd_ctx = ppu_v1_ctx.pd_ctx_table + fwk_id_get_element_idx(event->target_id);

    switch (pd_ctx->config->pd_type) {
    case MOD_PD_TYPE_CORE:
        return ppu_v1_core_pd_init(pd_ctx);

    case MOD_PD_TYPE_CLUSTER:
        return ppu_v1_cluster_pd_init(pd_ctx);

    default:
        ppu_v1_init(pd_ctx->ppu);
        return FWK_SUCCESS;
    }
}

const struct fwk_module module_ppu_v1 = {
    .name = "PPU_V1",
    .type = FWK_MODULE_TYPE_DRIVER,
    .api_count = MOD_PPU_V1_API_IDX_COUNT,
    .init = ppu_v1_mod_init,
    .element_init = ppu_v1_pd_init,
    .post_init = ppu_v1_post_init,
    .bind = ppu_v1_bind,
    .start = ppu_v1_start,
    .process_bind_request = ppu_v1_process_bind_request,
    .process_notification = ppu_v1_process_notification,
};