aboutsummaryrefslogtreecommitdiff
path: root/module/cmn600/src/mod_cmn600.c
blob: 03eccc9a7c1c04ad114299d3c18b9ef06ac6ce40 (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
/*
 * Arm SCP/MCP Software
 * Copyright (c) 2017-2020, Arm Limited and Contributors. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <stdbool.h>
#include <string.h>
#include <fwk_assert.h>
#include <fwk_macros.h>
#include <fwk_math.h>
#include <fwk_mm.h>
#include <fwk_module.h>
#include <fwk_module_idx.h>
#include <fwk_notification.h>
#include <fwk_status.h>
#include <mod_clock.h>
#include <mod_cmn600.h>
#include <mod_log.h>
#include <mod_power_domain.h>
#include <cmn600.h>
#include <internal/cmn600_ccix.h>
#include <internal/cmn600_ctx.h>
#include <mod_ppu_v1.h>

#define MOD_NAME "[CMN600] "

struct cmn600_ctx *ctx;

static void process_node_hnf(struct cmn600_hnf_reg *hnf)
{
    unsigned int logical_id;
    unsigned int group;
    unsigned int bit_pos;
    unsigned int region_idx;
    unsigned int region_sub_count = 0;
    const struct mod_cmn600_memory_region_map *region;
    const struct mod_cmn600_config *config = ctx->config;
    uint64_t base_offset;

    logical_id = get_node_logical_id(hnf);

    assert(logical_id < config->snf_count);

    group = logical_id / CMN600_HNF_CACHE_GROUP_ENTRIES_PER_GROUP;
    bit_pos = CMN600_HNF_CACHE_GROUP_ENTRY_BITS_WIDTH *
              (logical_id % CMN600_HNF_CACHE_GROUP_ENTRIES_PER_GROUP);

    ctx->hnf_cache_group[group] += ((uint64_t)get_node_id(hnf)) << bit_pos;

    /* Set target node */
    hnf->SAM_CONTROL = config->snf_table[logical_id];

    if (ctx->chip_id != 0) {
        base_offset = ((uint64_t)(ctx->config->chip_addr_space *
                                  ctx->chip_id));
    } else
        base_offset = 0;

    /*
     * Map sub-regions to this HN-F node
     */
    for (region_idx = 0; region_idx < config->mmap_count; region_idx++) {
        region = &config->mmap_table[region_idx];

        /* Skip non sub-regions */
        if (region->type != MOD_CMN600_REGION_TYPE_SYSCACHE_SUB)
            continue;

        /* Configure sub-region entry */
        hnf->SAM_MEMREGION[region_sub_count] = region->node_id |
            (sam_encode_region_size(region->size) <<
                CMN600_HNF_SAM_MEMREGION_SIZE_POS) |
            (((region->base + base_offset) / SAM_GRANULARITY) <<
              CMN600_HNF_SAM_MEMREGION_BASE_POS) |
            CMN600_HNF_SAM_MEMREGION_VALID;

        region_sub_count++;
    }

    /* Configure the system cache RAM PPU */
    hnf->PPU_PWPR = CMN600_PPU_PWPR_POLICY_ON |
                    CMN600_PPU_PWPR_OPMODE_FAM |
                    CMN600_PPU_PWPR_DYN_EN;
}

/*
 * Scan the CMN600 to find out:
 * - Number of external RN-SAM nodes
 * - Number of internal RN-SAM nodes
 * - Number of HN-F nodes (cache)
 */
static int cmn600_discovery(void)
{
    unsigned int xp_count;
    unsigned int xp_idx;
    unsigned int node_count;
    unsigned int node_idx;
    struct cmn600_mxp_reg *xp;
    struct node_header *node;
    const struct mod_cmn600_config *config = ctx->config;

    ctx->log_api->log(MOD_LOG_GROUP_DEBUG, MOD_NAME "Starting discovery...\n");

    assert(get_node_type(ctx->root) == NODE_TYPE_CFG);

    /* Traverse cross points (XP) */
    xp_count = get_node_child_count(ctx->root);
    for (xp_idx = 0; xp_idx < xp_count; xp_idx++) {

        xp = get_child_node(config->base, ctx->root, xp_idx);
        assert(get_node_type(xp) == NODE_TYPE_XP);

        ctx->log_api->log(MOD_LOG_GROUP_DEBUG, MOD_NAME "\n");
        ctx->log_api->log(MOD_LOG_GROUP_DEBUG,
            MOD_NAME "XP (%d, %d) ID:%d, LID:%d\n",
            get_node_pos_x(xp),
            get_node_pos_y(xp),
            get_node_id(xp),
            get_node_logical_id(xp));

        /* Traverse nodes */
        node_count = get_node_child_count(xp);
        for (node_idx = 0; node_idx < node_count; node_idx++) {

            node = get_child_node(config->base, xp, node_idx);

            /* External nodes */
            if (is_child_external(xp, node_idx)) {
                ctx->external_rnsam_count++;

            if (get_child_node_id(xp, node_idx) == config->cxgla_node_id)
                ctx->cxla_reg = (void *)node;

            ctx->log_api->log(MOD_LOG_GROUP_DEBUG,
                    MOD_NAME "  Found external node ID:%d\n",
                    get_child_node_id(xp, node_idx));

            /* Internal nodes */
            } else {
                switch (get_node_type(node)) {
                case NODE_TYPE_HN_F:
                    if (ctx->hnf_count >= MAX_HNF_COUNT) {
                        ctx->log_api->log(MOD_LOG_GROUP_DEBUG,
                                MOD_NAME "  hnf count %d >= max limit (%d)\n",
                                ctx->hnf_count, MAX_HNF_COUNT);
                        return FWK_E_DATA;
                    }
                    ctx->hnf_offset[ctx->hnf_count++] = (uint32_t)node;
                    break;

                case NODE_TYPE_RN_SAM:
                    ctx->internal_rnsam_count++;
                    break;

                case NODE_TYPE_RN_D:
                    if (ctx->rnd_count >= MAX_RND_COUNT) {
                        ctx->log_api->log(MOD_LOG_GROUP_DEBUG,
                                MOD_NAME "  rnd count %d >= max limit (%d)\n",
                                ctx->rnd_count, MAX_RND_COUNT);
                        return FWK_E_DATA;
                    }
                    ctx->rnd_ldid[ctx->rnd_count++] = get_node_logical_id(node);
                    break;

                case NODE_TYPE_RN_I:
                    if (ctx->rni_count >= MAX_RNI_COUNT) {
                        ctx->log_api->log(MOD_LOG_GROUP_DEBUG,
                                MOD_NAME "  rni count %d >= max limit (%d)\n",
                                ctx->rni_count, MAX_RNI_COUNT);
                        return FWK_E_DATA;
                    }
                    ctx->rni_ldid[ctx->rni_count++] = get_node_logical_id(node);
                    break;

                case NODE_TYPE_CXRA:
                    ctx->cxg_ra_reg = (struct cmn600_cxg_ra_reg *)node;
                    break;

                case NODE_TYPE_CXHA:
                    ctx->cxg_ha_reg = (struct cmn600_cxg_ha_reg *)node;
                    ctx->ccix_host_info.host_ha_count++;
                    break;

                default:
                    /* Nothing to be done for other node types */
                    break;
                }

                ctx->log_api->log(MOD_LOG_GROUP_DEBUG,
                    MOD_NAME "  %s ID:%d, LID:%d\n",
                    get_node_type_name(get_node_type(node)),
                    get_node_id(node),
                    get_node_logical_id(node));
            }
        }
    }

    ctx->log_api->log(MOD_LOG_GROUP_DEBUG,
        MOD_NAME "Total internal RN-SAM nodes: %d\n"
        MOD_NAME "Total external RN-SAM nodes: %d\n"
        MOD_NAME "Total HN-F nodes: %d\n"
        MOD_NAME "Total RN-D nodes: %d\n"
        MOD_NAME "Total RN-I nodes: %d\n",
        ctx->internal_rnsam_count,
        ctx->external_rnsam_count,
        ctx->hnf_count,
        ctx->rnd_count,
        ctx->rni_count);

    if (ctx->cxla_reg) {
        ctx->log_api->log(MOD_LOG_GROUP_DEBUG,
            MOD_NAME "CCIX CXLA node at: 0x%08x\n",
            (uint32_t)ctx->cxla_reg);
    }
    if (ctx->cxg_ra_reg) {
        ctx->log_api->log(MOD_LOG_GROUP_DEBUG,
            MOD_NAME "CCIX CXRA node at: 0x%08x\n",
            (uint32_t)ctx->cxg_ra_reg);
    }
    if (ctx->cxg_ha_reg) {
        ctx->log_api->log(MOD_LOG_GROUP_DEBUG,
            MOD_NAME "CCIX CXHA node at: 0x%08x\n",
            (uint32_t)ctx->cxg_ha_reg);
    }
    return FWK_SUCCESS;
}

static void cmn600_configure(void)
{
    unsigned int xp_count;
    unsigned int xp_idx;
    unsigned int node_count;
    unsigned int node_idx;
    unsigned int xrnsam_entry;
    unsigned int irnsam_entry;
    struct cmn600_mxp_reg *xp;
    void *node;
    const struct mod_cmn600_config *config = ctx->config;

    assert(get_node_type(ctx->root) == NODE_TYPE_CFG);

    xrnsam_entry = 0;
    irnsam_entry = 0;

    /* Traverse cross points (XP) */
    xp_count = get_node_child_count(ctx->root);
    for (xp_idx = 0; xp_idx < xp_count; xp_idx++) {
        xp = get_child_node(config->base, ctx->root, xp_idx);
        assert(get_node_type(xp) == NODE_TYPE_XP);

        /* Traverse nodes */
        node_count = get_node_child_count(xp);
        for (node_idx = 0; node_idx < node_count; node_idx++) {
            node = get_child_node(config->base, xp, node_idx);

            if (is_child_external(xp, node_idx)) {
                unsigned int node_id = get_child_node_id(xp, node_idx);

                fwk_assert(xrnsam_entry < ctx->external_rnsam_count);

                ctx->external_rnsam_table[xrnsam_entry].node_id = node_id;
                ctx->external_rnsam_table[xrnsam_entry].node = node;

                xrnsam_entry++;
            } else {
                enum node_type node_type = get_node_type(node);

                if (node_type == NODE_TYPE_RN_SAM) {
                    fwk_assert(irnsam_entry < ctx->internal_rnsam_count);

                    ctx->internal_rnsam_table[irnsam_entry] = node;

                    irnsam_entry++;
                } else if (node_type == NODE_TYPE_HN_F)
                    process_node_hnf(node);
            }
        }
    }
}

static const char * const mmap_type_name[] = {
    [MOD_CMN600_MEMORY_REGION_TYPE_IO] = "I/O",
    [MOD_CMN600_MEMORY_REGION_TYPE_SYSCACHE] = "System Cache",
    [MOD_CMN600_REGION_TYPE_SYSCACHE_SUB] = "Sub-System Cache",
    [MOD_CMN600_REGION_TYPE_CCIX] = "CCIX",
    [MOD_CMN600_REGION_TYPE_SYSCACHE_NONHASH] = "System Cache Non-hash",
};

int cmn600_setup_sam(struct cmn600_rnsam_reg *rnsam)
{
    unsigned int region_idx;
    unsigned int region_io_count = 0;
    unsigned int region_sys_count = 1;
    const struct mod_cmn600_memory_region_map *region;
    const struct mod_cmn600_config *config = ctx->config;
    unsigned int bit_pos;
    unsigned int group;
    unsigned int group_count;
    enum sam_node_type sam_node_type;
    uint64_t base;

    ctx->log_api->log(MOD_LOG_GROUP_DEBUG,
        MOD_NAME "Configuring SAM for node %d\n",
        get_node_id(rnsam));

    for (region_idx = 0; region_idx < config->mmap_count; region_idx++) {
        region = &config->mmap_table[region_idx];

        if (ctx->chip_id != 0) {
            if (region->type == MOD_CMN600_REGION_TYPE_CCIX)
                base = 0;
            else if (region->type == MOD_CMN600_MEMORY_REGION_TYPE_SYSCACHE)
                base = region->base;
            else {
                base = ((uint64_t)(ctx->config->chip_addr_space *
                                   ctx->chip_id) + region->base);
            }
        } else
            base = region->base;

        ctx->log_api->log(MOD_LOG_GROUP_DEBUG,
                          MOD_NAME "  [0x%lx - 0x%lx] %s\n",
                          base,
                          base + region->size - 1,
                          mmap_type_name[region->type]);

        group = region_io_count / CMN600_RNSAM_REGION_ENTRIES_PER_GROUP;
        bit_pos = (region_io_count % CMN600_RNSAM_REGION_ENTRIES_PER_GROUP) *
                  CMN600_RNSAM_REGION_ENTRY_BITS_WIDTH;

        switch (region->type) {
        case MOD_CMN600_MEMORY_REGION_TYPE_IO:
        case MOD_CMN600_REGION_TYPE_CCIX:
            /*
             * Configure memory region
             */
            sam_node_type =
                (region->type == MOD_CMN600_MEMORY_REGION_TYPE_IO) ?
                    SAM_NODE_TYPE_HN_I : SAM_NODE_TYPE_CXRA;
            configure_region(&rnsam->NON_HASH_MEM_REGION[group],
                             bit_pos,
                             base,
                             region->size,
                             sam_node_type);
            /*
             * Configure target node
             */
            group = region_io_count /
                CMN600_RNSAM_NON_HASH_TGT_NODEID_ENTRIES_PER_GROUP;
            bit_pos = CMN600_RNSAM_NON_HASH_TGT_NODEID_ENTRY_BITS_WIDTH *
                      (region_io_count %
                       CMN600_RNSAM_NON_HASH_TGT_NODEID_ENTRIES_PER_GROUP);

            rnsam->NON_HASH_TGT_NODEID[group] &=
                ~(CMN600_RNSAM_NON_HASH_TGT_NODEID_ENTRY_MASK << bit_pos);
            rnsam->NON_HASH_TGT_NODEID[group] |= (region->node_id &
                CMN600_RNSAM_NON_HASH_TGT_NODEID_ENTRY_MASK) << bit_pos;

            region_io_count++;
            break;

        case MOD_CMN600_MEMORY_REGION_TYPE_SYSCACHE:
            /*
             * Configure memory region
             */
            configure_region(&rnsam->SYS_CACHE_GRP_REGION[group],
                             bit_pos,
                             region->base,
                             region->size,
                             SAM_NODE_TYPE_HN_F);
           break;

        case MOD_CMN600_REGION_TYPE_SYSCACHE_NONHASH:
            group = region_sys_count / CMN600_RNSAM_REGION_ENTRIES_PER_GROUP;
            bit_pos = (region_sys_count %
                       CMN600_RNSAM_REGION_ENTRIES_PER_GROUP) *
                      CMN600_RNSAM_REGION_ENTRY_BITS_WIDTH;
            /*
             * Configure memory region
             */
            configure_region(&rnsam->SYS_CACHE_GRP_REGION[group],
                             bit_pos,
                             region->base,
                             region->size,
                             SAM_NODE_TYPE_HN_I);

            rnsam->SYS_CACHE_GRP_REGION[group] |= (UINT64_C(0x2) << bit_pos);
            bit_pos = CMN600_RNSAM_NON_HASH_TGT_NODEID_ENTRY_BITS_WIDTH *
                  ((region_sys_count - 1) %
                  CMN600_RNSAM_NON_HASH_TGT_NODEID_ENTRIES_PER_GROUP);
            rnsam->SYS_CACHE_GRP_NOHASH_NODEID &=
                ~(CMN600_RNSAM_NON_HASH_TGT_NODEID_ENTRY_MASK << bit_pos);
            rnsam->SYS_CACHE_GRP_NOHASH_NODEID |= (region->node_id &
                CMN600_RNSAM_NON_HASH_TGT_NODEID_ENTRY_MASK) << bit_pos;

            region_sys_count++;
            break;

        case MOD_CMN600_REGION_TYPE_SYSCACHE_SUB:
            /* Do nothing. System cache sub-regions are handled by HN-Fs */
            break;

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

    group_count = ctx->hnf_count / CMN600_HNF_CACHE_GROUP_ENTRIES_PER_GROUP;
    for (group = 0; group < group_count; group++)
        rnsam->SYS_CACHE_GRP_HN_NODEID[group] = ctx->hnf_cache_group[group];

    /* Program the number of HNFs */
    rnsam->SYS_CACHE_GRP_HN_COUNT = ctx->hnf_count;

    /* Enable RNSAM */
    rnsam->STATUS = CMN600_RNSAM_STATUS_UNSTALL;
    __sync_synchronize();

    return FWK_SUCCESS;
}

static int cmn600_setup(void)
{
    unsigned int rnsam_idx, i, ccix_mmap_idx;
    int status = FWK_SUCCESS;

    if (!ctx->initialized) {
        status = cmn600_discovery();
        if (status != FWK_SUCCESS)
            return status;
        /*
         * Allocate resources based on the discovery
         */

        /* Pointers for the internal RN-SAM nodes */
        if (ctx->internal_rnsam_count != 0) {
            ctx->internal_rnsam_table = fwk_mm_calloc(
                ctx->internal_rnsam_count, sizeof(*ctx->internal_rnsam_table));
            if (ctx->internal_rnsam_table == NULL)
                return FWK_E_NOMEM;
        }

        /* Tuples for the external RN-RAM nodes (including their node IDs) */
        if (ctx->external_rnsam_count != 0) {
            ctx->external_rnsam_table = fwk_mm_calloc(
                ctx->external_rnsam_count, sizeof(*ctx->external_rnsam_table));
            if (ctx->external_rnsam_table == NULL)
                return FWK_E_NOMEM;
        }

        /* Cache groups */
        if (ctx->hnf_count != 0) {
            /*
             * Allocate enough group descriptors to accommodate all expected
             * HN-F nodes in the system.
             */
            ctx->hnf_cache_group = fwk_mm_calloc(
                ctx->hnf_count / CMN600_HNF_CACHE_GROUP_ENTRIES_PER_GROUP,
                sizeof(*ctx->hnf_cache_group));
            if (ctx->hnf_cache_group == NULL)
                return FWK_E_NOMEM;
        }
    }

    cmn600_configure();

    /* Setup internal RN-SAM nodes */
    for (rnsam_idx = 0; rnsam_idx < ctx->internal_rnsam_count; rnsam_idx++)
        cmn600_setup_sam(ctx->internal_rnsam_table[rnsam_idx]);

    /* Capture CCIX Host Topology */
    for (i = 0; i < ctx->config->mmap_count; i++) {
        if (ctx->config->mmap_table[i].type == MOD_CMN600_REGION_TYPE_CCIX) {
            ccix_mmap_idx = ctx->ccix_host_info.ccix_host_mmap_count;
            if (ccix_mmap_idx >= MAX_HA_MMAP_ENTRIES)
                return FWK_E_DATA;

            ctx->ccix_host_info.ccix_host_mmap[ccix_mmap_idx].base =
                ctx->config->mmap_table[i].base;
            ctx->ccix_host_info.ccix_host_mmap[ccix_mmap_idx].size =
                ctx->config->mmap_table[i].size;
            ctx->ccix_host_info.ccix_host_mmap_count++;
        }
    }

    ctx->log_api->log(MOD_LOG_GROUP_DEBUG, MOD_NAME "Done\n");

    ctx->initialized = true;

    return FWK_SUCCESS;
}

static int cmn600_setup_rnsam(unsigned int node_id)
{
    unsigned int node_idx;

    for (node_idx = 0; node_idx < ctx->external_rnsam_count; node_idx++) {
        if (ctx->external_rnsam_table[node_idx].node_id == node_id) {
            cmn600_setup_sam(ctx->external_rnsam_table[node_idx].node);
            return FWK_SUCCESS;
        }
    }

    return FWK_E_PARAM;
}

/*
 * PPUv1 State Observer API
 */

static void post_ppu_on(void *data)
{
    assert(data != NULL);
    cmn600_setup_rnsam(*(unsigned int *)data);
}

static const struct mod_ppu_v1_power_state_observer_api cmn600_observer_api = {
    .post_ppu_on = post_ppu_on,
};

/*
 * CCIX configuration APIs invoked by SCMI
 */

static int cmn600_ccix_config_get(
    struct mod_cmn600_ccix_host_node_config *config)
{
    if (ctx->internal_rnsam_count == 0)
        return FWK_E_DATA;

    ctx->ccix_host_info.host_ra_count =
        ctx->internal_rnsam_count + ctx->external_rnsam_count - 1;
    ctx->ccix_host_info.host_sa_count = ctx->config->sa_count;

    memcpy((void *)config, (void *)&ctx->ccix_host_info,
        sizeof(struct mod_cmn600_ccix_host_node_config));
    return FWK_SUCCESS;
}


static int cmn600_ccix_config_set(
    struct mod_cmn600_ccix_remote_node_config *config)
{
    unsigned int i;
    int status;


    status = ccix_setup(ctx, config);
    if (status != FWK_SUCCESS)
        return status;

    for (i = 0; i < ctx->config->mmap_count; i++) {
        if (ctx->config->mmap_table[i].type == MOD_CMN600_REGION_TYPE_CCIX)
            cmn600_setup_rnsam(ctx->config->mmap_table[i].node_id);
    }
    return FWK_SUCCESS;
}

static int cmn600_ccix_exchange_protocol_credit(uint8_t link_id)
{
    return ccix_exchange_protocol_credit(ctx, link_id);
}

static int cmn600_ccix_enter_system_coherency(uint8_t link_id)
{
    return ccix_enter_system_coherency(ctx, link_id);
}

static int cmn600_ccix_enter_dvm_domain(uint8_t link_id)
{
    return ccix_enter_dvm_domain(ctx, link_id);
}

static const struct mod_cmn600_ccix_config_api cmn600_ccix_config_api = {
    .get_config = cmn600_ccix_config_get,
    .set_config = cmn600_ccix_config_set,
    .exchange_protocol_credit = cmn600_ccix_exchange_protocol_credit,
    .enter_system_coherency = cmn600_ccix_enter_system_coherency,
    .enter_dvm_domain = cmn600_ccix_enter_dvm_domain,
};


/*
 * Framework handlers
 */

static int cmn600_init(fwk_id_t module_id, unsigned int element_count,
    const void *data)
{
    const struct mod_cmn600_config *config = data;

    /* No elements support */
    if (element_count > 0)
        return FWK_E_DATA;

    /* Allocate space for the context */
    ctx = fwk_mm_calloc(1, sizeof(*ctx));
    if (ctx == NULL)
        return FWK_E_NOMEM;

    if (config->base == 0)
        return FWK_E_DATA;

    if ((config->mesh_size_x == 0) || (config->mesh_size_x > CMN600_MESH_X_MAX))
        return FWK_E_DATA;

    if ((config->mesh_size_y == 0) || (config->mesh_size_y > CMN600_MESH_Y_MAX))
        return FWK_E_DATA;

    if (config->snf_count > CMN600_HNF_CACHE_GROUP_ENTRIES_MAX)
        return FWK_E_DATA;

    ctx->root = get_root_node(config->base, config->hnd_node_id,
        config->mesh_size_x, config->mesh_size_y);

    ctx->config = config;

    return FWK_SUCCESS;
}

static int cmn600_bind(fwk_id_t id, unsigned int round)
{
    int status;

    /* Use second round only (round numbering is zero-indexed) */
    if (round == 1) {

        /* Bind to the log component */
        status = fwk_module_bind(FWK_ID_MODULE(FWK_MODULE_IDX_LOG),
                                 FWK_ID_API(FWK_MODULE_IDX_LOG, 0),
                                 &ctx->log_api);

        if (status != FWK_SUCCESS)
            return FWK_E_PANIC;

        /* Bind to the timer component */
        status = fwk_module_bind(FWK_ID_ELEMENT(FWK_MODULE_IDX_TIMER, 0),
                                 FWK_ID_API(FWK_MODULE_IDX_TIMER,
                                            MOD_TIMER_API_IDX_TIMER),
                                 &ctx->timer_api);
        if (status != FWK_SUCCESS)
            return FWK_E_PANIC;

        /* Bind to the chip information API in platform if provided */
        if (!fwk_id_is_equal(ctx->config->chipinfo_mod_id, FWK_ID_NONE)) {
            status = fwk_module_bind(ctx->config->chipinfo_mod_id,
                                     ctx->config->chipinfo_api_id,
                                     &ctx->chipinfo_api);
            if (status != FWK_SUCCESS)
                return FWK_E_PANIC;
        }
    }

    return FWK_SUCCESS;
}

static int cmn600_process_bind_request(fwk_id_t requester_id,
    fwk_id_t target_id, fwk_id_t api_id, const void **api)
{
    switch (fwk_id_get_api_idx(api_id)) {
    case MOD_CMN600_API_IDX_PPU_OBSERVER:
        *api = &cmn600_observer_api;
        break;

    case MOD_CMN600_API_IDX_CCIX_CONFIG:
        *api = &cmn600_ccix_config_api;
        break;
    }

    return FWK_SUCCESS;
}

int cmn600_start(fwk_id_t id)
{
    uint8_t chip_id = 0;
    bool mc_mode = false;
    int status;

    if (fwk_id_is_equal(ctx->config->clock_id, FWK_ID_NONE)) {
        cmn600_setup();
        return FWK_SUCCESS;
    }

    if (!fwk_id_is_equal(ctx->config->chipinfo_mod_id, FWK_ID_NONE)) {
        status = ctx->chipinfo_api->get_chipinfo(&chip_id, &mc_mode);
        if (status != FWK_SUCCESS)
            return status;
    }
    ctx->chip_id = chip_id;

    ctx->log_api->log(MOD_LOG_GROUP_DEBUG,
        MOD_NAME "Multichip mode: %d Chip ID: %d\n", mc_mode, chip_id);

    /* Register the module for clock state notifications */
    return fwk_notification_subscribe(
        mod_clock_notification_id_state_changed,
        ctx->config->clock_id,
        id);
}

static int cmn600_process_notification(
    const struct fwk_event *event,
    struct fwk_event *resp_event)
{
    struct clock_notification_params *params;

    assert(fwk_id_is_equal(event->id, mod_clock_notification_id_state_changed));
    assert(fwk_id_is_type(event->target_id, FWK_ID_TYPE_MODULE));

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

    if (params->new_state == MOD_CLOCK_STATE_RUNNING)
        cmn600_setup();

    return FWK_SUCCESS;
}

const struct fwk_module module_cmn600 = {
    .name = "CMN600",
    .type = FWK_MODULE_TYPE_DRIVER,
    .api_count = MOD_CMN600_API_COUNT,
    .init = cmn600_init,
    .bind = cmn600_bind,
    .start = cmn600_start,
    .process_bind_request = cmn600_process_bind_request,
    .process_notification = cmn600_process_notification,
};