aboutsummaryrefslogtreecommitdiff
path: root/module/smt/src/mod_smt.c
blob: e4e9af15b8a0fe3d5133b6454ac39d602ebab2ca (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
/*
 * Arm SCP/MCP Software
 * Copyright (c) 2015-2019, 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_interrupt.h>
#include <fwk_mm.h>
#include <fwk_module.h>
#include <fwk_module_idx.h>
#include <fwk_notification.h>
#include <fwk_status.h>
#include <mod_log.h>
#include <mod_power_domain.h>
#include <mod_scmi.h>
#include <mod_sideband.h>
#include <mod_smt.h>
#include <mod_mhu2.h>
#include <internal/smt.h>

struct smt_channel_ctx {
    /* Channel identifier */
    fwk_id_t id;

    /* Channel configuration data */
    struct mod_smt_channel_config *config;

    /* Channel read and write cache memory areas */
    struct mod_smt_memory *in, *out;

    /* Message processing in progrees flag */
    volatile bool locked;

    /* Maximum payload size of the channel */
    size_t max_payload_size;

    /* Driver entity identifier */
    fwk_id_t driver_id;

    /* SCMI module service bound to the channel */
    fwk_id_t scmi_service_id;

    /* Driver API */
    struct mod_mhu2_doorbell_api *driver_api;

    /* SCMI service API */
    struct mod_scmi_from_transport_api *scmi_api;

    /* Flag indicating the mailbox is ready */
    bool smt_mailbox_ready;
};

struct smt_ctx {
    /* Log module API */
    struct mod_log_api *log_api;

    /* Table of channel contexts */
    struct smt_channel_ctx *channel_ctx_table;

    /* Sideband module api */
    /* Build has mod sideband */
    struct mod_sideband_api *sideband_api;

    /* Number of channels */
    unsigned int channel_count;
};

static struct smt_ctx smt_ctx;

/*
 * SCMI Transport API
 */
static int smt_get_secure(fwk_id_t channel_id, bool *secure)
{
    int status;
    struct smt_channel_ctx *channel_ctx;

    status = fwk_module_check_call(channel_id);
    if (status != FWK_SUCCESS) {
        assert(false);
        return status;
    }

    if (secure == NULL) {
        assert(false);
        return FWK_E_PARAM;
    }

    channel_ctx =
        &smt_ctx.channel_ctx_table[fwk_id_get_element_idx(channel_id)];

    *secure = channel_ctx->config->policies & MOD_SMT_POLICY_SECURE;

    return FWK_SUCCESS;
}

static int smt_get_max_payload_size(fwk_id_t channel_id, size_t *size)
{
    int status;
    struct smt_channel_ctx *channel_ctx;

    status = fwk_module_check_call(channel_id);
    if (status != FWK_SUCCESS) {
        assert(false);
        return status;
    }

    if (size == NULL) {
        assert(false);
        return FWK_E_PARAM;
    }

    channel_ctx =
        &smt_ctx.channel_ctx_table[fwk_id_get_element_idx(channel_id)];

    *size = channel_ctx->max_payload_size;

    return FWK_SUCCESS;
}

static int smt_get_message_header(fwk_id_t channel_id, uint32_t *header)
{
    int status;
    struct smt_channel_ctx *channel_ctx;

    status = fwk_module_check_call(channel_id);
    if (status != FWK_SUCCESS) {
        assert(false);
        return status;
    }

    if (header == NULL) {
        assert(false);
        return FWK_E_PARAM;
    }

    channel_ctx =
        &smt_ctx.channel_ctx_table[fwk_id_get_element_idx(channel_id)];

    if (!channel_ctx->locked)
        return FWK_E_ACCESS;

    *header = channel_ctx->in->message_header;

    return FWK_SUCCESS;
}

static int smt_get_payload(fwk_id_t channel_id,
                           const void **payload,
                           size_t *size)
{
    int status;
    struct smt_channel_ctx *channel_ctx;

    status = fwk_module_check_call(channel_id);
    if (status != FWK_SUCCESS) {
        assert(false);
        return status;
    }

    if (payload == NULL) {
        assert(false);
        return FWK_E_PARAM;
    }

    channel_ctx =
        &smt_ctx.channel_ctx_table[fwk_id_get_element_idx(channel_id)];

    if (!channel_ctx->locked)
        return FWK_E_ACCESS;

    *payload = channel_ctx->in->payload;

    if (size != NULL) {
        *size = channel_ctx->in->length -
            sizeof(channel_ctx->in->message_header);
    }

    return FWK_SUCCESS;
}

static int smt_write_payload(fwk_id_t channel_id,
                             size_t offset,
                             const void *payload,
                             size_t size)
{
    int status;
    struct smt_channel_ctx *channel_ctx;

    status = fwk_module_check_call(channel_id);
    if (status != FWK_SUCCESS) {
        assert(false);
        return status;
    }

    channel_ctx =
        &smt_ctx.channel_ctx_table[fwk_id_get_element_idx(channel_id)];

    if ((payload == NULL)                         ||
        (offset  > channel_ctx->max_payload_size) ||
        (size > channel_ctx->max_payload_size)    ||
        ((offset + size) > channel_ctx->max_payload_size)) {

        assert(false);
        return FWK_E_PARAM;
    }

    if (!channel_ctx->locked)
        return FWK_E_ACCESS;

    memcpy(((uint8_t*)channel_ctx->out->payload) + offset, payload, size);

    return FWK_SUCCESS;
}

static int smt_respond(fwk_id_t channel_id, const void *payload, size_t size)
{
    struct smt_channel_ctx *channel_ctx;
    struct mod_smt_memory *memory;
    int status;
    (void)status;

    status = fwk_module_check_call(channel_id);
    if (status != FWK_SUCCESS) {
        assert(false);
        return status;
    }

    channel_ctx =
        &smt_ctx.channel_ctx_table[fwk_id_get_element_idx(channel_id)];
    memory = ((struct mod_smt_memory*)channel_ctx->config->mailbox_address);

    /* Copy the header from the write buffer */
    *memory = *channel_ctx->out;

    /* Copy the payload from either the write buffer or the payload parameter */
    memcpy(memory->payload,
           (payload == NULL ? channel_ctx->out->payload : payload),
           size);

    /*
     * NOTE: Disable interrupts for a brief period to ensure interrupts are not
     * erroneously accepted in between unlocking the context, and setting
     * the mailbox free bit. The agent should not interrupt during this
     * period anyway, but this guard is included to protect against a
     * misbehaving agent.
     */
    fwk_interrupt_global_disable();

    channel_ctx->locked = false;

    memory->length = sizeof(memory->message_header) + size;
    memory->status |= MOD_SMT_MAILBOX_STATUS_FREE_MASK;

    fwk_interrupt_global_enable();

    if (memory->flags & MOD_SMT_MAILBOX_FLAGS_IENABLED_MASK)
        channel_ctx->driver_api->ring_doorbell(channel_ctx->driver_id);

    return FWK_SUCCESS;
}

/*
 * API used in case of multiple SCMI Platforms that manages SCMI requests.
 * It uses a service module called sideband to communicate with other
 * SCMI Platforms.
 *
 */
static int smt_forward(fwk_id_t id , int remote_domain)
{

    int idx = fwk_id_get_element_idx(id);
    int status;
    struct client client;
    struct smt_channel_ctx *channel_ctx;

    client.instance = remote_domain;
    client.agent = FWK_ID_MODULE(FWK_MODULE_IDX_SMT);

    /*
     * Converts a element identifer from SCMI module
     * to a element identifier of SMT Module.
     * This conversion is on the basis that both SMT and SCMI
     * modules use the same enumeration to define the different SCMI
     * services. For EX:- PSCI and OSPM
     *
     */
    fwk_id_t remote_id = FWK_ID_ELEMENT(FWK_MODULE_IDX_SMT,idx);

    channel_ctx =
        &smt_ctx.channel_ctx_table[fwk_id_get_element_idx(remote_id)];

    channel_ctx->locked = false;

    status = smt_ctx.sideband_api->send_msg(&remote_id, sizeof(remote_id), client, NO_ACKNOWLEDGEMENT);

    return status;
}

static const struct mod_scmi_to_transport_api smt_mod_scmi_to_transport_api = {
    .get_secure = smt_get_secure,
    .get_max_payload_size = smt_get_max_payload_size,
    .get_message_header = smt_get_message_header,
    .get_payload = smt_get_payload,
    .write_payload = smt_write_payload,
    .respond = smt_respond,
    .forward = smt_forward,
};

/*
 * Driver handler API
 */
static int smt_slave_handler(struct smt_channel_ctx *channel_ctx)
{
    struct mod_smt_memory *memory, *in, *out;
    size_t payload_size;
    int status;

    /* Check if we are already processing */
    if (channel_ctx->locked)
        return FWK_E_STATE;

    /* Check if we are already processing */
    memory = ((struct mod_smt_memory*)channel_ctx->config->mailbox_address);
    in = channel_ctx->in;
    out = channel_ctx->out;

    /* Check we have ownership of the mailbox */
    if (memory->status & MOD_SMT_MAILBOX_STATUS_FREE_MASK) {
        smt_ctx.log_api->log(
            MOD_LOG_GROUP_ERROR,
            "[SMT] Mailbox ownership error on channel %u\n",
            fwk_id_get_element_idx(channel_ctx->id));

        return FWK_E_STATE;
    }

    /* Commit to sending a response */
    channel_ctx->locked = true;

    /* Mirror mailbox contents in read and write buffers (Payload not copied) */
    *in  = *memory;
    *out = *memory;

    /* Ensure error bit is not set */
    out->status &= ~MOD_SMT_MAILBOX_STATUS_ERROR_MASK;

    /*
     * Verify:
     * 1. The length is at least as large as the message header
     * 2. The length, minus the size of the message header, is less than or
     *         equal to the maximum payload size
     *
     * Note: the payload size is permitted to be of size zero.
     */
    if ((in->length < sizeof(in->message_header)) ||
        ((in->length - sizeof(in->message_header))
         > channel_ctx->max_payload_size)) {

        out->status |= MOD_SMT_MAILBOX_STATUS_ERROR_MASK;
        return smt_respond(channel_ctx->id, &(int32_t){SCMI_PROTOCOL_ERROR},
                           sizeof(int32_t));
    }

    /* Copy payload from shared memory to read buffer */
    payload_size = in->length - sizeof(in->message_header);
    memcpy(in->payload, memory->payload, payload_size);

    /* Let SCMI handle the message */
    status =
        channel_ctx->scmi_api->signal_message(channel_ctx->scmi_service_id);

    if (status != FWK_SUCCESS)
        return FWK_E_HANDLER;

    return FWK_SUCCESS;
}

static int smt_signal_message(fwk_id_t channel_id)
{
    int status;
    struct smt_channel_ctx *channel_ctx;

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

    channel_ctx =
        &smt_ctx.channel_ctx_table[fwk_id_get_element_idx(channel_id)];

    if (!channel_ctx->smt_mailbox_ready) {
        /* Discard any message in the mailbox when not ready */
        smt_ctx.log_api->log(MOD_LOG_GROUP_ERROR, "[SMT] Message not valid\n");

        return FWK_SUCCESS;
    }

    switch (channel_ctx->config->type) {
    case MOD_SMT_CHANNEL_TYPE_MASTER:
        /* Not supported yet */
        assert(false);
        break;
    case MOD_SMT_CHANNEL_TYPE_SLAVE:
        return smt_slave_handler(channel_ctx);
        break;
    default:
        /* Invalid config */
        assert(false);
        break;
    }

    return FWK_SUCCESS;
}

static const struct mod_smt_driver_input_api driver_input_api = {
    .signal_message = smt_signal_message,
};

int smt_callback(void *data, size_t size)
{

    fwk_id_t element_id = *(fwk_id_t *)data;

    return smt_signal_message(element_id);
}

int doorbell_callback(fwk_id_t source_id)
{
    unsigned int idx;
    struct smt_channel_ctx *channel_ctx;

    for (idx = 0; idx < smt_ctx.channel_count; idx++) {
        channel_ctx = &smt_ctx.channel_ctx_table[idx];
        if (fwk_id_is_equal(channel_ctx->config->driver_id, source_id)) {
            return smt_signal_message(channel_ctx->id);
        }
    }
    return FWK_E_PARAM;
}

/*
 * Framework API
 */
static int smt_init(fwk_id_t module_id, unsigned int element_count,
                    const void *data)
{
    smt_ctx.channel_ctx_table = fwk_mm_calloc(element_count,
        sizeof(smt_ctx.channel_ctx_table[0]));

    if (smt_ctx.channel_ctx_table == NULL) {
        assert(false);
        return FWK_E_NOMEM;
    }

    smt_ctx.channel_count = element_count;

    return FWK_SUCCESS;
}

static int smt_channel_init(fwk_id_t channel_id, unsigned int unused,
                            const void *data)
{
    struct smt_channel_ctx *channel_ctx;

    channel_ctx =
        &smt_ctx.channel_ctx_table[fwk_id_get_element_idx(channel_id)];
    channel_ctx->config = (struct mod_smt_channel_config*)data;

    /* Validate channel config */
    if ((channel_ctx->config->type >= MOD_SMT_CHANNEL_TYPE_COUNT) ||
        (channel_ctx->config->mailbox_address == 0) ||
        (channel_ctx->config->mailbox_size == 0)) {
        assert(false);
        return FWK_E_DATA;
    }

    channel_ctx->id = channel_id;
    channel_ctx->in = fwk_mm_alloc(1, channel_ctx->config->mailbox_size);
    channel_ctx->out = fwk_mm_alloc(1, channel_ctx->config->mailbox_size);

    if ((channel_ctx->in == NULL) || (channel_ctx->out == NULL))
        return FWK_E_NOMEM;

    channel_ctx->max_payload_size = channel_ctx->config->mailbox_size -
        sizeof(struct mod_smt_memory);

    /* Check memory allocations */
    if ((channel_ctx->in == NULL) || (channel_ctx->out == NULL)) {
        assert(false);
        return FWK_E_NOMEM;
    }

    return FWK_SUCCESS;
}

static int smt_bind(fwk_id_t id, unsigned int round)
{
    int status;
    struct smt_channel_ctx *channel_ctx;

    if (round == 0) {
        if (fwk_id_is_type(id, FWK_ID_TYPE_MODULE)) {

            status = fwk_module_bind(FWK_ID_MODULE(FWK_MODULE_IDX_SIDEBAND), FWK_ID_API(FWK_MODULE_IDX_SIDEBAND,MOD_SIDEBAND_DEFAULT_API), &smt_ctx.sideband_api);
            if (status != FWK_SUCCESS)
                return status;
            /*
             * The default API in Sideband module requires
             * a call back to be registered.
             *
             */
            status = smt_ctx.sideband_api->register_callback(id, &smt_callback);

            return fwk_module_bind(fwk_module_id_log,
                                   FWK_ID_API(FWK_MODULE_IDX_LOG, 0),
                                   &smt_ctx.log_api);
        }

        channel_ctx = &smt_ctx.channel_ctx_table[fwk_id_get_element_idx(id)];
        status = fwk_module_bind(channel_ctx->config->driver_id,
                                 channel_ctx->config->driver_api_id,
                                 &channel_ctx->driver_api);

        if (status != FWK_SUCCESS)
            return status;

        channel_ctx->driver_id = channel_ctx->config->driver_id;
        status = channel_ctx->driver_api->register_doorbell_callback(channel_ctx->driver_id, &doorbell_callback);
        return status;
    }

    if ((round == 1) && fwk_id_is_type(id, FWK_ID_TYPE_ELEMENT)) {
        channel_ctx = &smt_ctx.channel_ctx_table[fwk_id_get_element_idx(id)];
        status = fwk_module_bind(channel_ctx->scmi_service_id,
            FWK_ID_API(FWK_MODULE_IDX_SCMI, MOD_SCMI_API_IDX_TRANSPORT),
            &channel_ctx->scmi_api);
        if (status != FWK_SUCCESS)
            return status;
    }

    return FWK_SUCCESS;
}

static int smt_process_bind_request(fwk_id_t source_id,
                                    fwk_id_t target_id,
                                    fwk_id_t api_id,
                                    const void **api)
{
    struct smt_channel_ctx *channel_ctx;

    /* Only bind to a channel (not the whole module) */
    if (!fwk_id_is_type(target_id, FWK_ID_TYPE_ELEMENT)) {
        /* Tried to bind to something other than a specific channel */
        assert(false);
        return FWK_E_PARAM;
    }

    channel_ctx =
        &smt_ctx.channel_ctx_table[fwk_id_get_element_idx(target_id)];

    switch (fwk_id_get_api_idx(api_id)) {

    case MOD_SMT_API_IDX_SCMI_TRANSPORT:
        /* SCMI transport API */
        *api = &smt_mod_scmi_to_transport_api;
        channel_ctx->scmi_service_id = source_id;
        break;

    default:
        /* Invalid API */
        assert(false);
        return FWK_E_PARAM;
    }

    return FWK_SUCCESS;
}

static int smt_start(fwk_id_t id)
{
    struct smt_channel_ctx *ctx;

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

    ctx = &smt_ctx.channel_ctx_table[fwk_id_get_element_idx(id)];

    /* Register for power domain state transition notifications */
    return fwk_notification_subscribe(
        mod_pd_notification_id_power_state_transition,
        ctx->config->pd_source_id,
        id);
}

static int smt_process_notification(
    const struct fwk_event *event,
    struct fwk_event *resp_event)
{
    struct mod_pd_power_state_transition_notification_params *params;
    struct smt_channel_ctx *channel_ctx;
    unsigned int notifications_sent;
    int status;

    assert(fwk_id_is_equal(event->id,
        mod_pd_notification_id_power_state_transition));
    assert(fwk_id_is_type(event->target_id, FWK_ID_TYPE_ELEMENT));

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

    channel_ctx =
        &smt_ctx.channel_ctx_table[fwk_id_get_element_idx(event->target_id)];

    if (params->state != MOD_PD_STATE_ON) {
        if (params->state == MOD_PD_STATE_OFF)
            channel_ctx->smt_mailbox_ready = false;

        return FWK_SUCCESS;
    }

    if (channel_ctx->config->policies & MOD_SMT_POLICY_INIT_MAILBOX) {
        /* Initialize mailbox */
        *((struct mod_smt_memory *)0xA4000DC0) =
            (struct mod_smt_memory) {
            .status = (1 << MOD_SMT_MAILBOX_STATUS_FREE_POS)
        };

        /* Notify that this mailbox is initialized */
        struct fwk_event smt_channels_initialized_notification = {
            .id = mod_smt_notification_id_initialized,
        };

        channel_ctx->smt_mailbox_ready = true;

        status = fwk_notification_notify(&smt_channels_initialized_notification,
            &notifications_sent);
        if (status != FWK_SUCCESS)
            return status;
    }

    return FWK_SUCCESS;
}

const struct fwk_module module_smt = {
    .name = "smt",
    .type = FWK_MODULE_TYPE_SERVICE,
    .api_count = MOD_SMT_API_IDX_COUNT,
    .notification_count = MOD_SMT_NOTIFICATION_IDX_COUNT,
    .init = smt_init,
    .element_init = smt_channel_init,
    .bind = smt_bind,
    .start = smt_start,
    .process_bind_request = smt_process_bind_request,
    .process_notification = smt_process_notification,
};