aboutsummaryrefslogtreecommitdiff
path: root/module/ppu_v1/src/mod_ppu_v1.c
blob: 511ab626652089d9b0c4307f03df595a09976675 (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
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
/*
 * 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_math.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

/*
 * Inequality that must be true for the usage of the bit mask
 * 'pending_on_core_mask' in 'struct cluster_pd_specific_ctx' to work properly.
 */
static_assert(CORE_PER_CLUSTER_COUNT_MAX < (sizeof(uint32_t) * CHAR_BIT),
              "Maximum number of cores per cluster too large.");

/* Cluster power domain specific context */
struct cluster_pd_specific_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;

    /*
     * Mask indicating which cluster's cores must be powered on when the
     * cluster reaches the ON power mode. The bit 'i' is equal to one if and
     * only if the core defined by the entry 'i' of the 'core_pd_ctx_table'
     * table must be powered on when the cluster reaches the ON power mode.
     */
    uint32_t pending_on_core_mask;

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

    /*
     * Flag indicating whether the cluster power domain has been requested to
     * be in the OFF power state or not.
     */
    bool off_power_state_requested;

    /*
     * Power state corresponding to the OFF power mode. If the cluster power
     * domain has been requested to be in the OFF power state, this is
     * MOD_PD_STATE_OFF, MOD_PD_STATE_SLEEP otherwise.
     */
    unsigned int off_power_mode_power_state;
};

/* Thread context */
struct thread_ctx {
    /* Identifier of the entity bound to the power domain driver API */
    fwk_id_t bound_id;

    /*
     * Flag indicating whether the thread power domain has been requested to be
     * in the OFF power state or not.
     */
    bool off_power_state_requested;

    /*
     * Power state corresponding to the OFF power mode. If the thread has
     * been requested to be in the OFF power state, this is MOD_PD_STATE_OFF,
     * MOD_PD_STATE_SLEEP otherwise.
     */
    unsigned int off_power_mode_power_state;
};

/* Core power domain specific context */
struct core_pd_specific_ctx {
    /* Context of the cluster power domain */
    struct ppu_v1_pd_ctx *cluster_pd_ctx;

    /*
     * Index of the pointer to the core power domain context in the table of
     * pointers to core power domain contexts of its cluster.
     */
    unsigned int core_pd_ctx_idx;

    /*
     * Flag indicating whether the core power domain has been requested to be
     * in the OFF power state or not.
     */
    bool off_power_state_requested;

    /*
     * Power state corresponding to the OFF power mode. If the core power
     * domain has been requested to be in the OFF power state, this is
     * MOD_PD_STATE_OFF, MOD_PD_STATE_SLEEP otherwise.
     */
    unsigned int off_power_mode_power_state;

    /* Number of threads */
    unsigned int thread_count;

    /* Table of thread contexts */
    struct thread_ctx *thread_ctx_table;
};

/* 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 data specific to the type of power domain */
    union {
        struct cluster_pd_specific_ctx *cluster;
        struct core_pd_specific_ctx *core;
    } specific;
};

/* 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;

    /* Minimum power mode for the cluster ON power state */
    unsigned int cluster_on_min_mode;

    /* 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[] = {
    [MOD_PPU_V1_MODE_OFF]         = (uint8_t)MOD_PD_STATE_OFF,
    [MOD_PPU_V1_MODE_OFF_EMU]     = (uint8_t)MOD_PD_STATE_OFF,
    [MOD_PPU_V1_MODE_MEM_RET]     = (uint8_t)MOD_PD_STATE_OFF,
    [MOD_PPU_V1_MODE_MEM_RET_EMU] = (uint8_t)MOD_PD_STATE_OFF,
    [MOD_PPU_V1_MODE_LOGIC_RET]   = (uint8_t)MOD_PD_STATE_ON,
    [MOD_PPU_V1_MODE_FULL_RET]    = (uint8_t)MOD_PD_STATE_ON,
    [MOD_PPU_V1_MODE_MEM_OFF]     = (uint8_t)MOD_PD_STATE_ON,
    [MOD_PPU_V1_MODE_FUNC_RET]    = (uint8_t)MOD_PD_STATE_ON,
    [MOD_PPU_V1_MODE_ON]          = (uint8_t)MOD_PD_STATE_ON,
    [MOD_PPU_V1_MODE_WARM_RST]    = (uint8_t)MODE_UNSUPPORTED,
    [MOD_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 mod_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) ==
        MOD_PPU_V1_MODE_COUNT), "[PPU_V1] ppu_mode_to_power_state size error");

    mode = ppu_v1_get_power_mode(ppu);
    assert(mode < MOD_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, MOD_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, MOD_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, MOD_PPU_V1_MODE_OFF);
    if (status == FWK_SUCCESS)
        status = ppu_v1_set_power_mode(pd_ctx->ppu, MOD_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 threads
 */
static bool cluster_on(struct ppu_v1_pd_ctx *core_pd_ctx);
static void core_on(struct ppu_v1_pd_ctx *pd_ctx);

static int ppu_v1_thread_set_state(fwk_id_t thread_id, unsigned int state)
{
    int status;
    struct ppu_v1_pd_ctx *pd_ctx;
    struct core_pd_specific_ctx *core_pd_specific_ctx;
    struct ppu_v1_reg *ppu;
    unsigned int thread_idx;
    unsigned int thread_mask;
    struct thread_ctx *thread_ctx;
    enum ppu_v1_op_devactive op_devactive;
    enum ppu_v1_opmode operating_mode;

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

    pd_ctx = ppu_v1_ctx.pd_ctx_table + fwk_id_get_element_idx(thread_id);
    core_pd_specific_ctx = pd_ctx->specific.core;
    ppu = pd_ctx->ppu;
    thread_idx = fwk_id_get_sub_element_idx(thread_id);
    thread_mask = 1 << thread_idx;
    thread_ctx = core_pd_specific_ctx->thread_ctx_table + thread_idx;
    op_devactive = PPU_V1_OP_DEVACTIVE_0 + thread_idx;

    switch (state) {
    case MOD_PD_STATE_OFF:
        thread_ctx->off_power_state_requested = true;
        thread_ctx->off_power_mode_power_state = MOD_PD_STATE_OFF;
        ppu_v1_set_op_active_edge_sensitivity(
                ppu, op_devactive, PPU_V1_EDGE_SENSITIVITY_MASKED);
        ppu_v1_interrupt_mask(ppu, PPU_V1_IMR_STA_POLICY_TRN_IRQ_MASK);

        /*
         * Disable interrupts as the core operating mode is shared between
         * threads and could be modified by the handler of OP_ACTIVE_EDGE_IRQ
         * asserted for another thread.
         */
        fwk_interrupt_global_disable();
        while (ppu_v1_is_op_devactive_high(ppu, op_devactive))
            continue;
        operating_mode = ppu_v1_get_operating_mode(ppu);
        if (operating_mode & thread_mask) {
            ppu_v1_dynamic_disable(ppu);
            ppu_v1_set_operating_mode(ppu,operating_mode & (~(1<<thread_idx)));
        } else {
            operating_mode = ppu_v1_get_operating_mode(ppu);
            if (!(operating_mode & thread_mask)) {
                status =
                    pd_ctx->pd_driver_input_api->report_power_state_transition(
                        thread_ctx->bound_id, MOD_PD_STATE_OFF);
                assert(status == FWK_SUCCESS);
            }
        }
        fwk_interrupt_global_enable();
        break;

    case MOD_PD_STATE_ON:
        thread_ctx->off_power_state_requested = false;
        thread_ctx->off_power_mode_power_state = MOD_PD_STATE_SLEEP;

        ppu_v1_set_op_active_edge_sensitivity(
            ppu, op_devactive, PPU_V1_EDGE_SENSITIVITY_MASKED);

        fwk_interrupt_global_disable();
        operating_mode = ppu_v1_get_operating_mode(ppu);
        ppu_v1_request_operating_mode(ppu, operating_mode | thread_mask);
        fwk_interrupt_global_enable();

        if (cluster_on(pd_ctx))
            core_on(pd_ctx);
        break;

    case MOD_PD_STATE_SLEEP:
        if (thread_ctx->off_power_state_requested) {

            thread_ctx->off_power_state_requested = false;
            thread_ctx->off_power_mode_power_state = MOD_PD_STATE_SLEEP;

            if (ppu_v1_is_op_devactive_high(ppu, op_devactive)) {
                fwk_interrupt_global_disable();
                operating_mode = ppu_v1_get_programmed_operating_mode(ppu);
                ppu_v1_request_operating_mode(
                    ppu, operating_mode | thread_mask);
                fwk_interrupt_global_enable();
            }
        }
        break;

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

    return FWK_SUCCESS;
}

static int ppu_v1_thread_get_state(fwk_id_t thread_id, unsigned int *state)
{
    int status;
    struct ppu_v1_pd_ctx *pd_ctx;
    struct ppu_v1_reg *ppu;
    unsigned int thread_idx;
    unsigned int thread_mask;
    struct thread_ctx *thread_ctx;

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

    pd_ctx = ppu_v1_ctx.pd_ctx_table + fwk_id_get_element_idx(thread_id);
    ppu = pd_ctx->ppu;
    thread_idx = fwk_id_get_sub_element_idx(thread_id);
    thread_mask = 1 << thread_idx;
    thread_ctx = pd_ctx->specific.core->thread_ctx_table + thread_idx;

    if (ppu_v1_get_programmed_operating_mode(ppu) & thread_mask)
        *state = MOD_PD_STATE_ON;
    else
        *state = thread_ctx->off_power_mode_power_state;

    return FWK_SUCCESS;
}

static int ppu_v1_thread_reset(fwk_id_t thread_id)
{
    int status;
    struct ppu_v1_pd_ctx *pd_ctx;
    struct ppu_v1_reg *ppu;
    unsigned int thread_idx;

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

    pd_ctx = ppu_v1_ctx.pd_ctx_table + fwk_id_get_element_idx(thread_id);
    ppu = pd_ctx->ppu;
    thread_idx = fwk_id_get_sub_element_idx(thread_id);

    ppu_v1_request_operating_mode(ppu,
       ppu_v1_get_operating_mode(ppu) & (~(1 << thread_idx)));

/* TODO */

    return FWK_SUCCESS;
}

static void thread_op_active_edge_interrupt_handler(
    struct ppu_v1_pd_ctx *core_pd_ctx,
    unsigned int thread_idx,
    struct thread_ctx *thread_ctx)
{
    int status;
    struct ppu_v1_reg *ppu;
    enum ppu_v1_op_devactive op_devactive;
    enum ppu_v1_opmode operating_mode;

    assert(core_pd_ctx != NULL);
    assert(thread_idx < PPU_V1_OP_DEVACTIVE_INDEPENDENT_COUNT);
    assert(thread_ctx != NULL);

    ppu = core_pd_ctx->ppu;
    op_devactive = PPU_V1_OP_DEVACTIVE_0 + thread_idx;
    if (!ppu_v1_is_op_active_edge_interrupt(ppu, op_devactive))
        return;

    ppu_v1_ack_op_active_edge_interrupt(ppu, op_devactive);
    operating_mode = ppu_v1_get_programmed_operating_mode(ppu);

    if (ppu_v1_get_op_active_edge_sensitivity(ppu, op_devactive) ==
        PPU_V1_EDGE_SENSITIVITY_FALLING_EDGE) {

        operating_mode &= ~(1 << thread_idx);
        ppu_v1_request_operating_mode(ppu, operating_mode);

        status =
            core_pd_ctx->pd_driver_input_api->report_power_state_transition(
                thread_ctx->bound_id, thread_ctx->off_power_mode_power_state);
        assert(status == FWK_SUCCESS);
        (void)status;

        if (thread_ctx->off_power_state_requested) {
            ppu_v1_set_op_active_edge_sensitivity(
                ppu, op_devactive, PPU_V1_EDGE_SENSITIVITY_MASKED);
            return;
        }

        ppu_v1_set_op_active_edge_sensitivity(
            ppu, op_devactive, PPU_V1_EDGE_SENSITIVITY_RISING_EDGE);

        if (!ppu_v1_is_op_devactive_high(ppu, op_devactive))
            return;
    }

    /*
     * The OP_DEVACTIVE signal of the thread is high and the thread has not
     * been put in the OFF power state, activate it.
     */
    ppu_v1_set_op_active_edge_sensitivity(
        ppu, op_devactive, PPU_V1_EDGE_SENSITIVITY_FALLING_EDGE);
    ppu_v1_request_operating_mode(ppu, operating_mode | (1 << thread_idx));

    if (cluster_on(core_pd_ctx))
        core_on(core_pd_ctx);
}

static void thread_report_active_threads(struct ppu_v1_pd_ctx *core_pd_ctx)
{
    int status;
    enum ppu_v1_opmode operating_mode;
    unsigned int thread_count;
    struct thread_ctx *thread_ctx_table, *thread_ctx;
    unsigned int thread_idx;

    assert(core_pd_ctx != NULL);

    operating_mode = ppu_v1_get_operating_mode(core_pd_ctx->ppu);
    thread_count = core_pd_ctx->specific.core->thread_count;
    thread_ctx_table = core_pd_ctx->specific.core->thread_ctx_table;

    for (thread_idx = 0; thread_idx < thread_count; thread_idx++) {
        if (!(operating_mode & (1 << thread_idx)))
            continue;

        thread_ctx = &thread_ctx_table[thread_idx];

        status =
            core_pd_ctx->pd_driver_input_api->report_power_state_transition(
                thread_ctx->bound_id, MOD_PD_STATE_ON);
        assert(status == FWK_SUCCESS);
        (void)status;
    }
}

static void thread_interrupt_handler(struct ppu_v1_pd_ctx *core_pd_ctx)
{
    struct ppu_v1_reg *ppu;
    unsigned int thread_count;
    unsigned int thread_idx;
    struct thread_ctx *thread_ctx_table, *thread_ctx;

    assert(core_pd_ctx != NULL);

    ppu = core_pd_ctx->ppu;
    thread_count = core_pd_ctx->specific.core->thread_count;
    thread_ctx_table = core_pd_ctx->specific.core->thread_ctx_table;

    if (ppu_v1_is_interrupt_pending(ppu, PPU_V1_ISR_OP_ACTIVE_EDGE_IRQ_MASK)) {
        for (thread_idx = 0, thread_ctx = thread_ctx_table;
             thread_idx < thread_count; thread_idx++, thread_ctx++)
            thread_op_active_edge_interrupt_handler(
                core_pd_ctx, thread_idx, thread_ctx);
    }
}

static const struct mod_pd_driver_api thread_driver = {
    .set_state = ppu_v1_thread_set_state,
    .get_state = ppu_v1_thread_get_state,
    .reset = ppu_v1_thread_reset,
};

/*
 * Functions specific to core power domains
 */

static void core_on(struct ppu_v1_pd_ctx *pd_ctx)
{
    struct ppu_v1_reg *ppu = pd_ctx->ppu;

    ppu_v1_additional_interrupt_mask(ppu, PPU_V1_AIMR_DYN_ACCEPT_IRQ_MASK);
    ppu_v1_interrupt_unmask(ppu, PPU_V1_IMR_STA_POLICY_TRN_IRQ_MASK);
    ppu_v1_set_input_edge_sensitivity(
        ppu, MOD_PPU_V1_MODE_ON, PPU_V1_EDGE_SENSITIVITY_MASKED);
    ppu_v1_request_power_mode(ppu, MOD_PPU_V1_MODE_ON);

    /* Just make sure the cluster is not locked in the OFF power mode */
    ppu_v1_off_unlock(pd_ctx->specific.core->cluster_pd_ctx->ppu);
}



static int ppu_v1_core_pd_init(struct ppu_v1_pd_ctx *pd_ctx)
{
    struct core_pd_specific_ctx *core_pd_specific_ctx;
    struct ppu_v1_reg *ppu;
    unsigned int num_opmode;
    enum ppu_v1_opmode operating_mode;
    unsigned int thread_idx;
    struct thread_ctx *thread_ctx;

    core_pd_specific_ctx = pd_ctx->specific.core;
    ppu = pd_ctx->ppu;

    ppu_v1_init(ppu);

    num_opmode = ppu_v1_get_num_opmode(ppu);
    assert(core_pd_specific_ctx->thread_count ==
           (unsigned int)fwk_math_log2(num_opmode));
    (void)num_opmode;

    core_pd_specific_ctx->off_power_state_requested =
        (ppu_v1_get_programmed_power_mode(ppu) == MOD_PPU_V1_MODE_OFF) &&
        (!ppu_v1_is_dynamic_enabled(ppu));
    core_pd_specific_ctx->off_power_mode_power_state =
        core_pd_specific_ctx->off_power_state_requested ?
        MOD_PD_STATE_OFF : MOD_PD_STATE_SLEEP;

    operating_mode = ppu_v1_get_programmed_operating_mode(ppu);
    for (thread_idx = 0;
         thread_idx < core_pd_specific_ctx->thread_count;
         thread_idx++) {
        thread_ctx = &core_pd_specific_ctx->thread_ctx_table[thread_idx];

        thread_ctx->off_power_state_requested =
            (operating_mode & (1 << thread_idx)) == 0;
        thread_ctx->off_power_mode_power_state =
            thread_ctx->off_power_state_requested ?
            MOD_PD_STATE_OFF : MOD_PD_STATE_SLEEP;
    }

    ppu_v1_dynamic_enable(ppu, MOD_PPU_V1_MODE_OFF);
    ppu_v1_interrupt_unmask(ppu, PPU_V1_IMR_LOCKED_IRQ_MASK);

    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 core_pd_specific_ctx *core_pd_specific_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);
    core_pd_specific_ctx = pd_ctx->specific.core;
    ppu = pd_ctx->ppu;

    switch (state) {
    case MOD_PD_STATE_OFF:
        /*
         * Disable the wake-up interrupt and update the context variables used
         * by the LOCKED_IRQ handler for the context to be well defined if the
         * LOCKED_IRQ is asserted while doing the setting.
         */
        fwk_interrupt_global_disable();
        core_pd_specific_ctx->off_power_state_requested = true;
        core_pd_specific_ctx->off_power_mode_power_state = MOD_PD_STATE_OFF;
        ppu_v1_set_input_edge_sensitivity(
            ppu, MOD_PPU_V1_MODE_ON, PPU_V1_EDGE_SENSITIVITY_MASKED);
        fwk_interrupt_global_enable();

        /*
         * Unlock the core to retrigger a locked interrupt in case the core is
         * already locked in the OFF power mode.
         */
        ppu_v1_off_unlock(ppu);
        ppu_v1_dynamic_enable(ppu, MOD_PPU_V1_MODE_OFF);
        break;

    case MOD_PD_STATE_ON:
        core_pd_specific_ctx->off_power_state_requested = false;
        core_pd_specific_ctx->off_power_mode_power_state = MOD_PD_STATE_SLEEP;

        ppu_v1_set_input_edge_sensitivity(
            ppu, MOD_PPU_V1_MODE_ON, PPU_V1_EDGE_SENSITIVITY_MASKED);

        if (cluster_on(pd_ctx))
            core_on(pd_ctx);
        break;

    case MOD_PD_STATE_SLEEP:
        /* OFF to SLEEP power state transition. */
        if (core_pd_specific_ctx->off_power_state_requested) {

            core_pd_specific_ctx->off_power_state_requested = false;
            core_pd_specific_ctx->off_power_mode_power_state =
                MOD_PD_STATE_SLEEP;
        }

        /*
         * Unlock the core to retrigger a locked interrupt in case the core
         * is already locked in the OFF power mode.
         */
        ppu_v1_off_unlock(ppu);
        ppu_v1_dynamic_enable(ppu, MOD_PPU_V1_MODE_OFF);
        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 void core_pd_sta_policy_trn_interrupt_handler(
    struct ppu_v1_pd_ctx *pd_ctx)
{
    int status;
    struct ppu_v1_reg *ppu;

    assert(pd_ctx != NULL);
    ppu = pd_ctx->ppu;

    ppu_v1_ack_interrupt(ppu, PPU_V1_ISR_STA_POLICY_TRN_IRQ);

    /*
     * This interrupt is used only to detect when a core has been manually
     * powered on, its power policy set to the static ON power mode. If the
     * ON power mode has not been reached yet, leave for now, another interrupt
     * will be asserted when the ON power mode is reached.
     */
    if (ppu_v1_get_power_mode(ppu) != MOD_PPU_V1_MODE_ON)
        return;

    ppu_v1_interrupt_mask(ppu, PPU_V1_IMR_STA_POLICY_TRN_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;

    if (pd_ctx->specific.core->thread_count > 0)
        thread_report_active_threads(pd_ctx);

    ppu_v1_dynamic_enable(ppu, MOD_PPU_V1_MODE_OFF);
}

static void core_pd_dyn_accept_interrupt_handler(
    struct ppu_v1_pd_ctx *core_pd_ctx)
{
    int status;
    struct ppu_v1_reg *ppu;

    assert(core_pd_ctx != NULL);
    ppu = core_pd_ctx->ppu;

    ppu_v1_ack_additional_interrupt(ppu, PPU_V1_AISR_DYN_ACCEPT_IRQ);

    /*
     * This interrupt is used only to detect when a core has been powered on
     * dynamically. If the ON power mode has not been reached yet, leave for
     * now, another interrupt will be asserted when the ON power mode is
     * reached.
     */
    if (ppu_v1_get_power_mode(ppu) != MOD_PPU_V1_MODE_ON)
        return;

    ppu_v1_additional_interrupt_mask(ppu, PPU_V1_AIMR_DYN_ACCEPT_IRQ_MASK);

    status = core_pd_ctx->pd_driver_input_api->report_power_state_transition(
        core_pd_ctx->bound_id, MOD_PD_STATE_ON);
    assert(status == FWK_SUCCESS);
    (void)status;
}

static void core_pd_on_rising_edge_interrupt_handler(
    struct ppu_v1_pd_ctx *pd_ctx)
{
    struct ppu_v1_reg *ppu;

    assert(pd_ctx != NULL);
    ppu = pd_ctx->ppu;

    ppu_v1_ack_power_active_edge_interrupt(ppu, MOD_PPU_V1_MODE_ON);
    ppu_v1_set_input_edge_sensitivity(
        ppu, MOD_PPU_V1_MODE_ON, PPU_V1_EDGE_SENSITIVITY_MASKED);

    if (!cluster_on(pd_ctx))
        return;

    ppu_v1_additional_interrupt_unmask(ppu, PPU_V1_AIMR_DYN_ACCEPT_IRQ_MASK);
    ppu_v1_off_unlock(ppu);
}

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

    assert(pd_ctx != NULL);
    ppu = pd_ctx->ppu;

    thread_interrupt_handler(pd_ctx);

    /* Core manually powered on */
    if (ppu_v1_is_interrupt_pending(ppu, PPU_V1_ISR_STA_POLICY_TRN_IRQ))
        core_pd_sta_policy_trn_interrupt_handler(pd_ctx);

    /* Core dynamically powered on */
    if (ppu_v1_is_additional_interrupt_pending(ppu, PPU_V1_AISR_DYN_ACCEPT_IRQ))
        core_pd_dyn_accept_interrupt_handler(pd_ctx);

    /* ON request */
    if (ppu_v1_is_power_active_edge_interrupt(ppu, MOD_PPU_V1_MODE_ON))
        core_pd_on_rising_edge_interrupt_handler(pd_ctx);

    /* Core locked in the MEM_RET or OFF power mode */
    else if (ppu_v1_is_interrupt_pending(ppu, PPU_V1_ISR_LOCKED_IRQ)) {
        ppu_v1_ack_interrupt(ppu, PPU_V1_ISR_LOCKED_IRQ);

        if (ppu_v1_get_power_mode(ppu) == MOD_PPU_V1_MODE_MEM_RET) {
            ppu_v1_off_unlock(ppu);
            return;
        }

        core_pd_specific_ctx = pd_ctx->specific.core;

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

        if (core_pd_specific_ctx->off_power_state_requested)
            return;

        /*
         * 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, power on the core
         * immediately.
         */
        ppu_v1_set_input_edge_sensitivity(
            ppu, MOD_PPU_V1_MODE_ON, PPU_V1_EDGE_SENSITIVITY_RISING_EDGE);
        if (ppu_v1_is_power_devactive_high(ppu, MOD_PPU_V1_MODE_ON))
            core_pd_on_rising_edge_interrupt_handler(pd_ctx);
    }
}

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,
};

/*
 * Functions specific to cluster power domains
 */

static bool cluster_on(struct ppu_v1_pd_ctx *core_pd_ctx)
{
    struct ppu_v1_reg *ppu;
    struct ppu_v1_pd_ctx *cluster_pd_ctx;

    assert(core_pd_ctx != NULL);
    cluster_pd_ctx = core_pd_ctx->specific.core->cluster_pd_ctx;
    ppu = cluster_pd_ctx->ppu;

    /*
     * First, prevent the core's cluster to transit beyond the minimum power
     * mode compatible with the core being powered on.
     */
    ppu_v1_set_input_edge_sensitivity(ppu,
                                      ppu_v1_ctx.cluster_on_min_mode,
                                      PPU_V1_EDGE_SENSITIVITY_FALLING_EDGE);

    if (ppu_v1_ctx.cluster_on_min_mode == MOD_PPU_V1_MODE_OFF) {
        ppu_v1_off_unlock(ppu);
        return true;
    }

    if (ppu_v1_get_programmed_power_mode(ppu) == ppu_v1_ctx.cluster_on_min_mode)
        return true;

    /*
     * The cluster may have transitionned to a power mode lower than the minimum
     * power mode compatible with the core being powered on. The cluster must
     * first be powered on. Once powered on, the core will be powered on in
     * turn.
     */

    fwk_interrupt_global_disable();
    cluster_pd_ctx->specific.cluster->pending_on_core_mask |=
        1 << core_pd_ctx->specific.core->core_pd_ctx_idx;
    fwk_interrupt_global_enable();

    ppu_v1_additional_interrupt_mask(ppu, PPU_V1_AIMR_DYN_ACCEPT_IRQ_MASK);
    ppu_v1_additional_interrupt_unmask(
        ppu, PPU_V1_AIMR_STA_POLICY_PWR_IRQ_MASK);
    ppu_v1_set_input_edge_sensitivity(
        ppu, MOD_PPU_V1_MODE_ON, PPU_V1_EDGE_SENSITIVITY_MASKED);
    ppu_v1_request_power_mode(ppu, MOD_PPU_V1_MODE_ON);

    return false;
}

static int ppu_v1_cluster_pd_init(struct ppu_v1_pd_ctx *pd_ctx)
{
    struct cluster_pd_specific_ctx *cluster_pd_specific_ctx;
    struct ppu_v1_reg *ppu;

    cluster_pd_specific_ctx = pd_ctx->specific.cluster;
    ppu = pd_ctx->ppu;

    ppu_v1_init(ppu);

    cluster_pd_specific_ctx->off_power_state_requested =
        (ppu_v1_get_programmed_power_mode(ppu) == MOD_PPU_V1_MODE_OFF) &&
        (!ppu_v1_is_dynamic_enabled(ppu));
    cluster_pd_specific_ctx->off_power_mode_power_state =
        cluster_pd_specific_ctx->off_power_state_requested ?
        MOD_PD_STATE_OFF : MOD_PD_STATE_SLEEP;

    ppu_v1_lock_off_enable(ppu);
    ppu_v1_dynamic_enable(ppu, MOD_PPU_V1_MODE_OFF);
    ppu_v1_interrupt_unmask(ppu, PPU_V1_IMR_LOCKED_IRQ_MASK);

    /* 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);

    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 cluster_pd_specific_ctx *cluster_pd_specific_ctx;
    struct ppu_v1_reg *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);
    cluster_pd_specific_ctx = pd_ctx->specific.cluster;
    ppu = pd_ctx->ppu;

    switch (state) {
    case MOD_PD_STATE_OFF:
        /*
         * Disable the wake-up interrupt and update the context variables used
         * by the LOCKED_IRQ handler for the context to be well defined if the
         * LOCKED_IRQ is asserted while doing the setting.
         */
        fwk_interrupt_global_disable();
        ppu_v1_set_input_edge_sensitivity(
           ppu, MOD_PPU_V1_MODE_ON, PPU_V1_EDGE_SENSITIVITY_MASKED);
        cluster_pd_specific_ctx->off_power_state_requested = true;
        cluster_pd_specific_ctx->off_power_mode_power_state = MOD_PD_STATE_OFF;
        fwk_interrupt_global_enable();

        /*
         * Unlock the cluster to retrigger a locked interrupt in case the
         * cluster already locked in the OFF power mode.
         */
        ppu_v1_off_unlock(ppu);
        ppu_v1_dynamic_enable(ppu, MOD_PPU_V1_MODE_OFF);
        break;

    case MOD_PD_STATE_ON:
        cluster_pd_specific_ctx->off_power_state_requested = false;
        cluster_pd_specific_ctx->off_power_mode_power_state =
            MOD_PD_STATE_SLEEP;

        ppu_v1_additional_interrupt_unmask(
            ppu, PPU_V1_AIMR_STA_POLICY_PWR_IRQ_MASK);
        ppu_v1_request_power_mode(ppu, MOD_PPU_V1_MODE_ON);
        break;

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

    return FWK_SUCCESS;
}

static void cluster_pd_sta_policy_pwr_interrupt_handler(
    struct ppu_v1_pd_ctx *cluster_pd_ctx)
{
    int status;
    struct cluster_pd_specific_ctx *cluster_pd_specific_ctx;
    struct ppu_v1_reg *ppu;
    unsigned int core_pd_ctx_idx;

    assert(cluster_pd_ctx != NULL);
    cluster_pd_specific_ctx = cluster_pd_ctx->specific.cluster;
    ppu = cluster_pd_ctx->ppu;

    ppu_v1_ack_additional_interrupt(ppu, PPU_V1_AISR_STA_POLICY_PWR_IRQ);

    /*
     * This interrupt is used only to detect when a cluster has been manually
     * powered on, its power policy set to static ON power mode. If the
     * ON power mode has not been reached yet, leave for now, another interrupt
     * will be asserted when the ON power mode is reached.
     */
    if (ppu_v1_get_power_mode(ppu) != MOD_PPU_V1_MODE_ON)
        return;

    ppu_v1_additional_interrupt_mask(ppu, PPU_V1_AIMR_STA_POLICY_PWR_IRQ_MASK);

    status = cluster_pd_ctx->pd_driver_input_api->report_power_state_transition(
        cluster_pd_ctx->bound_id, MOD_PD_STATE_ON);
    assert(status == FWK_SUCCESS);
    (void)status;

    if (cluster_pd_specific_ctx->observer_api != NULL) {
        cluster_pd_specific_ctx->observer_api->post_ppu_on(
            cluster_pd_ctx->config->post_ppu_on_param);
    }

    while (cluster_pd_specific_ctx->pending_on_core_mask) {
        core_pd_ctx_idx = __builtin_ctz(
            cluster_pd_specific_ctx->pending_on_core_mask);
        cluster_pd_specific_ctx->pending_on_core_mask &=
            ~(1 << core_pd_ctx_idx);
        core_on(cluster_pd_specific_ctx->core_pd_ctx_table[core_pd_ctx_idx]);
    }

    ppu_v1_set_input_edge_sensitivity(ppu,
                                      ppu_v1_ctx.cluster_on_min_mode,
                                      PPU_V1_EDGE_SENSITIVITY_FALLING_EDGE);
    ppu_v1_dynamic_enable(ppu, ppu_v1_ctx.cluster_on_min_mode);
}

static void cluster_pd_dyn_accept_interrupt_handler(
    struct ppu_v1_pd_ctx *cluster_pd_ctx)
{
    int status;
    struct ppu_v1_reg *ppu;

    assert(cluster_pd_ctx != NULL);
    ppu = cluster_pd_ctx->ppu;

    ppu_v1_ack_additional_interrupt(ppu, PPU_V1_AISR_DYN_ACCEPT_IRQ);

    /*
     * This interrupt is used only to detect when a cluster has been powered on
     * dynamically. If the ON power mode has not been reached yet, leave for
     * now, another interrupt will be asserted when the ON power mode is
     * reached.
     */
    if (ppu_v1_get_power_mode(ppu) != MOD_PPU_V1_MODE_ON)
        return;

    ppu_v1_additional_interrupt_mask(ppu, PPU_V1_AIMR_DYN_ACCEPT_IRQ_MASK);

    status = cluster_pd_ctx->pd_driver_input_api->report_power_state_transition(
        cluster_pd_ctx->bound_id, MOD_PD_STATE_ON);
    assert(status == FWK_SUCCESS);
    (void)status;

    ppu_v1_set_input_edge_sensitivity(ppu,
                                      ppu_v1_ctx.cluster_on_min_mode,
                                      PPU_V1_EDGE_SENSITIVITY_FALLING_EDGE);
    ppu_v1_dynamic_enable(ppu, ppu_v1_ctx.cluster_on_min_mode);
}

static void cluster_pd_on_rising_edge_interrupt_handler(
    struct ppu_v1_pd_ctx *pd_ctx) {
    struct ppu_v1_reg *ppu;

    assert(pd_ctx != NULL);
    ppu = pd_ctx->ppu;

    ppu_v1_ack_power_active_edge_interrupt(ppu, MOD_PPU_V1_MODE_ON);
    ppu_v1_set_input_edge_sensitivity(
        ppu, MOD_PPU_V1_MODE_ON, PPU_V1_EDGE_SENSITIVITY_MASKED);
    ppu_v1_additional_interrupt_unmask(ppu, PPU_V1_AIMR_DYN_ACCEPT_IRQ_MASK);
    ppu_v1_off_unlock(ppu);
}

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

    assert(pd_ctx != NULL);

    cluster_pd_specific_ctx = pd_ctx->specific.cluster;
    ppu = pd_ctx->ppu;

    /* Cluster manually powered on */
    if (ppu_v1_is_additional_interrupt_pending(ppu,
                                               PPU_V1_AISR_STA_POLICY_PWR_IRQ))
        cluster_pd_sta_policy_pwr_interrupt_handler(pd_ctx);

    /* Cluster dynamically powered on */
    if (ppu_v1_is_additional_interrupt_pending(ppu, PPU_V1_AISR_DYN_ACCEPT_IRQ))
        cluster_pd_dyn_accept_interrupt_handler(pd_ctx);

    /* ON request */
    if (ppu_v1_is_power_active_edge_interrupt(ppu, MOD_PPU_V1_MODE_ON) &&
        (ppu_v1_get_input_edge_sensitivity(ppu, MOD_PPU_V1_MODE_ON) ==
         PPU_V1_EDGE_SENSITIVITY_RISING_EDGE))
        cluster_pd_on_rising_edge_interrupt_handler(pd_ctx);

    /*
     * Cluster can transit to modes deeper than ppu_v1_ctx.cluster_on_min_mode
     */
    else if (ppu_v1_is_power_active_edge_interrupt(ppu,
                 ppu_v1_ctx.cluster_on_min_mode)) {

        ppu_v1_ack_power_active_edge_interrupt(ppu,
                                               ppu_v1_ctx.cluster_on_min_mode);
        ppu_v1_set_input_edge_sensitivity(pd_ctx->ppu,
                                          ppu_v1_ctx.cluster_on_min_mode,
                                          PPU_V1_EDGE_SENSITIVITY_MASKED);
        ppu_v1_dynamic_enable(ppu, MOD_PPU_V1_MODE_OFF);

        /*
         * Enable the cluster 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, power on the cluster
         * immediately.
         */
        ppu_v1_set_input_edge_sensitivity(
            ppu, MOD_PPU_V1_MODE_ON, PPU_V1_EDGE_SENSITIVITY_RISING_EDGE);
        if (ppu_v1_is_power_devactive_high(ppu, MOD_PPU_V1_MODE_ON))
            cluster_pd_on_rising_edge_interrupt_handler(pd_ctx);

    /* Cluster locked in the MEM_RET or OFF power mode */
    } else if (ppu_v1_is_interrupt_pending(ppu, PPU_V1_ISR_LOCKED_IRQ)) {
        ppu_v1_ack_interrupt(ppu, PPU_V1_ISR_LOCKED_IRQ);

        if (ppu_v1_get_power_mode(ppu) == MOD_PPU_V1_MODE_MEM_RET) {
            ppu_v1_off_unlock(ppu);
            return;
        }

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

        if (cluster_pd_specific_ctx->off_power_state_requested)
            return;

        /*
         * 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, power on the cluster
         * immediately.
         */
        ppu_v1_set_input_edge_sensitivity(
            ppu, MOD_PPU_V1_MODE_ON, PPU_V1_EDGE_SENSITIVITY_RISING_EDGE);
        if (ppu_v1_is_power_devactive_high(ppu, MOD_PPU_V1_MODE_ON))
            cluster_pd_on_rising_edge_interrupt_handler(pd_ctx);
    }
}

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, MOD_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 *config)
{
    const struct mod_ppu_v1_config *module_config = config;

    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;
    ppu_v1_ctx.cluster_on_min_mode = module_config->cluster_on_min_mode;

    return FWK_SUCCESS;
}

static int ppu_v1_pd_init(fwk_id_t pd_id, unsigned int sub_element_count,
                          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);
    }

    switch (config->pd_type) {
    case MOD_PD_TYPE_CORE:
        pd_ctx->specific.core =
            fwk_mm_calloc(1, sizeof(struct core_pd_specific_ctx));
        if (pd_ctx->specific.core == NULL)
            return FWK_E_NOMEM;
        pd_ctx->specific.core->thread_count = sub_element_count;

        if (sub_element_count != 0) {
            pd_ctx->specific.core->thread_ctx_table =
                fwk_mm_calloc(sub_element_count, sizeof(struct thread_ctx));
            if (pd_ctx->specific.core->thread_ctx_table == NULL)
                return FWK_E_NOMEM;
        }
        break;

    case MOD_PD_TYPE_CLUSTER:
        if (sub_element_count != 0)
            return FWK_E_DATA;
        pd_ctx->specific.cluster =
            fwk_mm_calloc(1, sizeof(struct cluster_pd_specific_ctx));
        if (pd_ctx->specific.cluster == NULL)
            return FWK_E_NOMEM;
        break;

    default:
        if (sub_element_count != 0)
            return FWK_E_DATA;
        break;
    }

    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, MOD_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 core_pd_specific_ctx *core_pd_specific_ctx;
    struct cluster_pd_specific_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];
        core_pd_specific_ctx = pd_ctx->specific.core;
        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->specific.cluster;

       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;

       core_pd_specific_ctx->cluster_pd_ctx = cluster_pd_ctx;
       core_pd_specific_ctx->core_pd_ctx_idx =
           cluster_pd_specific_ctx->core_count;

       cluster_pd_specific_ctx->core_count++;
    }

    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->specific.cluster->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;
    unsigned int thread_idx;

    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) &&
        !fwk_module_is_valid_sub_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)
            break;

        if (fwk_module_is_valid_sub_element_id(target_id)) {
            thread_idx = fwk_id_get_sub_element_idx(target_id);
            *api = &thread_driver;
            pd_ctx->specific.core->thread_ctx_table[thread_idx].bound_id =
                source_id;
        } else {
            *api = &core_pd_driver;
            pd_ctx->bound_id = source_id;
        }
        return FWK_SUCCESS;

    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,
};