1ae8c8068SEtienne Carriere // SPDX-License-Identifier: BSD-3-Clause
2ae8c8068SEtienne Carriere /*
3ae8c8068SEtienne Carriere * Copyright (c) 2015-2019, Arm Limited and Contributors. All rights reserved.
45c34a982SEtienne Carriere * Copyright (c) 2019-2022, Linaro Limited
5ae8c8068SEtienne Carriere */
65c34a982SEtienne Carriere #include <assert.h>
7ae8c8068SEtienne Carriere #include <drivers/scmi-msg.h>
8ae8c8068SEtienne Carriere #include <drivers/scmi.h>
94a382700SEtienne Carriere #include <kernel/misc.h>
105c34a982SEtienne Carriere #include <kernel/spinlock.h>
114a382700SEtienne Carriere #include <kernel/thread.h>
125c34a982SEtienne Carriere #include <string.h>
13ae8c8068SEtienne Carriere #include <trace.h>
14ae8c8068SEtienne Carriere
1560c96f68SEtienne Carriere #include "base.h"
1660c96f68SEtienne Carriere #include "clock.h"
17ae8c8068SEtienne Carriere #include "common.h"
18*f1cec17aSPascal Paillet #include "perf_domain.h"
1960c96f68SEtienne Carriere #include "reset_domain.h"
2060c96f68SEtienne Carriere #include "voltage_domain.h"
21ae8c8068SEtienne Carriere
224a382700SEtienne Carriere /* Provision input message payload buffers for each supported entry channel */
234a382700SEtienne Carriere #define SCMI_PAYLOAD_U32_MAX (SCMI_SEC_PAYLOAD_SIZE / sizeof(uint32_t))
244a382700SEtienne Carriere
254a382700SEtienne Carriere static uint32_t threaded_payload[CFG_NUM_THREADS][SCMI_PAYLOAD_U32_MAX]
264a382700SEtienne Carriere __maybe_unused;
274a382700SEtienne Carriere
284a382700SEtienne Carriere static uint32_t interrupt_payload[CFG_TEE_CORE_NB_CORE][SCMI_PAYLOAD_U32_MAX]
294a382700SEtienne Carriere __maybe_unused;
304a382700SEtienne Carriere
314a382700SEtienne Carriere static uint32_t fastcall_payload[CFG_TEE_CORE_NB_CORE][SCMI_PAYLOAD_U32_MAX]
324a382700SEtienne Carriere __maybe_unused;
334a382700SEtienne Carriere
345c34a982SEtienne Carriere /* SMP protection on channel->busy field */
355c34a982SEtienne Carriere static unsigned int smt_channels_lock;
365c34a982SEtienne Carriere
375c34a982SEtienne Carriere /* If channel is not busy, set busy and return true, otherwise return false */
scmi_msg_claim_channel(struct scmi_msg_channel * channel)385c34a982SEtienne Carriere bool scmi_msg_claim_channel(struct scmi_msg_channel *channel)
395c34a982SEtienne Carriere {
405c34a982SEtienne Carriere uint32_t exceptions = cpu_spin_lock_xsave(&smt_channels_lock);
415c34a982SEtienne Carriere bool channel_is_busy = channel->busy;
425c34a982SEtienne Carriere
435c34a982SEtienne Carriere if (!channel_is_busy)
445c34a982SEtienne Carriere channel->busy = true;
455c34a982SEtienne Carriere
465c34a982SEtienne Carriere cpu_spin_unlock_xrestore(&smt_channels_lock, exceptions);
475c34a982SEtienne Carriere
485c34a982SEtienne Carriere return !channel_is_busy;
495c34a982SEtienne Carriere }
505c34a982SEtienne Carriere
scmi_msg_release_channel(struct scmi_msg_channel * channel)515c34a982SEtienne Carriere void scmi_msg_release_channel(struct scmi_msg_channel *channel)
525c34a982SEtienne Carriere {
535c34a982SEtienne Carriere channel->busy = false;
545c34a982SEtienne Carriere }
555c34a982SEtienne Carriere
scmi_status_response(struct scmi_msg * msg,int32_t status)56ae8c8068SEtienne Carriere void scmi_status_response(struct scmi_msg *msg, int32_t status)
57ae8c8068SEtienne Carriere {
58ae8c8068SEtienne Carriere assert(msg->out && msg->out_size >= sizeof(int32_t));
59ae8c8068SEtienne Carriere
60ae8c8068SEtienne Carriere memcpy(msg->out, &status, sizeof(int32_t));
61ae8c8068SEtienne Carriere msg->out_size_out = sizeof(int32_t);
62ae8c8068SEtienne Carriere }
63ae8c8068SEtienne Carriere
scmi_write_response(struct scmi_msg * msg,void * payload,size_t size)64ae8c8068SEtienne Carriere void scmi_write_response(struct scmi_msg *msg, void *payload, size_t size)
65ae8c8068SEtienne Carriere {
667b49ff33SEtienne Carriere if (msg->out_size < size) {
677b49ff33SEtienne Carriere DMSG("SCMI resp. payload %zu > %zu bytes", size, msg->out_size);
687b49ff33SEtienne Carriere scmi_status_response(msg, SCMI_PROTOCOL_ERROR);
697b49ff33SEtienne Carriere } else {
70ae8c8068SEtienne Carriere memcpy(msg->out, payload, size);
71ae8c8068SEtienne Carriere msg->out_size_out = size;
72ae8c8068SEtienne Carriere }
737b49ff33SEtienne Carriere }
74ae8c8068SEtienne Carriere
scmi_process_message(struct scmi_msg * msg)75ae8c8068SEtienne Carriere void scmi_process_message(struct scmi_msg *msg)
76ae8c8068SEtienne Carriere {
77ae8c8068SEtienne Carriere scmi_msg_handler_t handler = NULL;
78ae8c8068SEtienne Carriere
79ae8c8068SEtienne Carriere switch (msg->protocol_id) {
80ae8c8068SEtienne Carriere case SCMI_PROTOCOL_ID_BASE:
81ae8c8068SEtienne Carriere handler = scmi_msg_get_base_handler(msg);
82ae8c8068SEtienne Carriere break;
83a7a9e3baSEtienne Carriere case SCMI_PROTOCOL_ID_CLOCK:
84a7a9e3baSEtienne Carriere handler = scmi_msg_get_clock_handler(msg);
85a7a9e3baSEtienne Carriere break;
8656a1f10eSEtienne Carriere case SCMI_PROTOCOL_ID_RESET_DOMAIN:
8756a1f10eSEtienne Carriere handler = scmi_msg_get_rd_handler(msg);
8856a1f10eSEtienne Carriere break;
89006d89b8SEtienne Carriere case SCMI_PROTOCOL_ID_VOLTAGE_DOMAIN:
90006d89b8SEtienne Carriere handler = scmi_msg_get_voltd_handler(msg);
91006d89b8SEtienne Carriere break;
92*f1cec17aSPascal Paillet case SCMI_PROTOCOL_ID_PERF:
93*f1cec17aSPascal Paillet handler = scmi_msg_get_perf_handler(msg);
94*f1cec17aSPascal Paillet break;
95ae8c8068SEtienne Carriere default:
96ae8c8068SEtienne Carriere break;
97ae8c8068SEtienne Carriere }
98ae8c8068SEtienne Carriere
99ae8c8068SEtienne Carriere if (handler) {
100ae8c8068SEtienne Carriere handler(msg);
101ae8c8068SEtienne Carriere return;
102ae8c8068SEtienne Carriere }
103ae8c8068SEtienne Carriere
104659a1f88SEtienne Carriere DMSG("Channel %u Protocol %#x Message %#x: not supported",
105659a1f88SEtienne Carriere msg->channel_id, msg->protocol_id, msg->message_id);
106ae8c8068SEtienne Carriere
107ae8c8068SEtienne Carriere scmi_status_response(msg, SCMI_NOT_SUPPORTED);
108ae8c8068SEtienne Carriere }
1094a382700SEtienne Carriere
1104a382700SEtienne Carriere #ifdef CFG_SCMI_MSG_SMT_FASTCALL_ENTRY
scmi_smt_fastcall_smc_entry(unsigned int channel_id)1114a382700SEtienne Carriere void scmi_smt_fastcall_smc_entry(unsigned int channel_id)
1124a382700SEtienne Carriere {
1134a382700SEtienne Carriere assert(!plat_scmi_get_channel(channel_id)->threaded);
1144a382700SEtienne Carriere
1154a382700SEtienne Carriere scmi_entry_smt(channel_id, fastcall_payload[get_core_pos()]);
1164a382700SEtienne Carriere }
1174a382700SEtienne Carriere #endif
1184a382700SEtienne Carriere
1194a382700SEtienne Carriere #ifdef CFG_SCMI_MSG_SMT_INTERRUPT_ENTRY
scmi_smt_interrupt_entry(unsigned int channel_id)1204a382700SEtienne Carriere void scmi_smt_interrupt_entry(unsigned int channel_id)
1214a382700SEtienne Carriere {
1224a382700SEtienne Carriere assert(!plat_scmi_get_channel(channel_id)->threaded);
1234a382700SEtienne Carriere
1244a382700SEtienne Carriere scmi_entry_smt(channel_id, interrupt_payload[get_core_pos()]);
1254a382700SEtienne Carriere }
1264a382700SEtienne Carriere #endif
1274a382700SEtienne Carriere
1284a382700SEtienne Carriere #ifdef CFG_SCMI_MSG_SMT_THREAD_ENTRY
scmi_smt_threaded_entry(unsigned int channel_id)1294a382700SEtienne Carriere void scmi_smt_threaded_entry(unsigned int channel_id)
1304a382700SEtienne Carriere {
1314a382700SEtienne Carriere assert(plat_scmi_get_channel(channel_id)->threaded);
1324a382700SEtienne Carriere
1334a382700SEtienne Carriere scmi_entry_smt(channel_id, threaded_payload[thread_get_id()]);
1344a382700SEtienne Carriere }
1354a382700SEtienne Carriere #endif
136916cc52aSEtienne Carriere
137916cc52aSEtienne Carriere #ifdef CFG_SCMI_MSG_SHM_MSG
scmi_msg_threaded_entry(unsigned int channel_id,void * in_buf,size_t in_size,void * out_buf,size_t * out_size)138916cc52aSEtienne Carriere TEE_Result scmi_msg_threaded_entry(unsigned int channel_id,
139916cc52aSEtienne Carriere void *in_buf, size_t in_size,
140916cc52aSEtienne Carriere void *out_buf, size_t *out_size)
141916cc52aSEtienne Carriere {
142916cc52aSEtienne Carriere assert(plat_scmi_get_channel(channel_id)->threaded);
143916cc52aSEtienne Carriere
144916cc52aSEtienne Carriere return scmi_entry_msg(channel_id, in_buf, in_size, out_buf, out_size,
145916cc52aSEtienne Carriere threaded_payload[thread_get_id()]);
146916cc52aSEtienne Carriere }
147916cc52aSEtienne Carriere #endif
148