1 // SPDX-License-Identifier: BSD-3-Clause
2 /*
3 * Copyright (c) 2015-2019, Arm Limited and Contributors. All rights reserved.
4 * Copyright (c) 2019-2022, Linaro Limited
5 */
6 #include <assert.h>
7 #include <drivers/scmi-msg.h>
8 #include <drivers/scmi.h>
9 #include <kernel/misc.h>
10 #include <kernel/spinlock.h>
11 #include <kernel/thread.h>
12 #include <string.h>
13 #include <trace.h>
14
15 #include "base.h"
16 #include "clock.h"
17 #include "common.h"
18 #include "reset_domain.h"
19 #include "voltage_domain.h"
20
21 /* Provision input message payload buffers for each supported entry channel */
22 #define SCMI_PAYLOAD_U32_MAX (SCMI_SEC_PAYLOAD_SIZE / sizeof(uint32_t))
23
24 static uint32_t threaded_payload[CFG_NUM_THREADS][SCMI_PAYLOAD_U32_MAX]
25 __maybe_unused;
26
27 static uint32_t interrupt_payload[CFG_TEE_CORE_NB_CORE][SCMI_PAYLOAD_U32_MAX]
28 __maybe_unused;
29
30 static uint32_t fastcall_payload[CFG_TEE_CORE_NB_CORE][SCMI_PAYLOAD_U32_MAX]
31 __maybe_unused;
32
33 /* SMP protection on channel->busy field */
34 static unsigned int smt_channels_lock;
35
36 /* If channel is not busy, set busy and return true, otherwise return false */
scmi_msg_claim_channel(struct scmi_msg_channel * channel)37 bool scmi_msg_claim_channel(struct scmi_msg_channel *channel)
38 {
39 uint32_t exceptions = cpu_spin_lock_xsave(&smt_channels_lock);
40 bool channel_is_busy = channel->busy;
41
42 if (!channel_is_busy)
43 channel->busy = true;
44
45 cpu_spin_unlock_xrestore(&smt_channels_lock, exceptions);
46
47 return !channel_is_busy;
48 }
49
scmi_msg_release_channel(struct scmi_msg_channel * channel)50 void scmi_msg_release_channel(struct scmi_msg_channel *channel)
51 {
52 channel->busy = false;
53 }
54
scmi_status_response(struct scmi_msg * msg,int32_t status)55 void scmi_status_response(struct scmi_msg *msg, int32_t status)
56 {
57 assert(msg->out && msg->out_size >= sizeof(int32_t));
58
59 memcpy(msg->out, &status, sizeof(int32_t));
60 msg->out_size_out = sizeof(int32_t);
61 }
62
scmi_write_response(struct scmi_msg * msg,void * payload,size_t size)63 void scmi_write_response(struct scmi_msg *msg, void *payload, size_t size)
64 {
65 if (msg->out_size < size) {
66 DMSG("SCMI resp. payload %zu > %zu bytes", size, msg->out_size);
67 scmi_status_response(msg, SCMI_PROTOCOL_ERROR);
68 } else {
69 memcpy(msg->out, payload, size);
70 msg->out_size_out = size;
71 }
72 }
73
scmi_process_message(struct scmi_msg * msg)74 void scmi_process_message(struct scmi_msg *msg)
75 {
76 scmi_msg_handler_t handler = NULL;
77
78 switch (msg->protocol_id) {
79 case SCMI_PROTOCOL_ID_BASE:
80 handler = scmi_msg_get_base_handler(msg);
81 break;
82 case SCMI_PROTOCOL_ID_CLOCK:
83 handler = scmi_msg_get_clock_handler(msg);
84 break;
85 case SCMI_PROTOCOL_ID_RESET_DOMAIN:
86 handler = scmi_msg_get_rd_handler(msg);
87 break;
88 case SCMI_PROTOCOL_ID_VOLTAGE_DOMAIN:
89 handler = scmi_msg_get_voltd_handler(msg);
90 break;
91 default:
92 break;
93 }
94
95 if (handler) {
96 handler(msg);
97 return;
98 }
99
100 DMSG("Channel %u Protocol %#x Message %#x: not supported",
101 msg->channel_id, msg->protocol_id, msg->message_id);
102
103 scmi_status_response(msg, SCMI_NOT_SUPPORTED);
104 }
105
106 #ifdef CFG_SCMI_MSG_SMT_FASTCALL_ENTRY
scmi_smt_fastcall_smc_entry(unsigned int channel_id)107 void scmi_smt_fastcall_smc_entry(unsigned int channel_id)
108 {
109 assert(!plat_scmi_get_channel(channel_id)->threaded);
110
111 scmi_entry_smt(channel_id, fastcall_payload[get_core_pos()]);
112 }
113 #endif
114
115 #ifdef CFG_SCMI_MSG_SMT_INTERRUPT_ENTRY
scmi_smt_interrupt_entry(unsigned int channel_id)116 void scmi_smt_interrupt_entry(unsigned int channel_id)
117 {
118 assert(!plat_scmi_get_channel(channel_id)->threaded);
119
120 scmi_entry_smt(channel_id, interrupt_payload[get_core_pos()]);
121 }
122 #endif
123
124 #ifdef CFG_SCMI_MSG_SMT_THREAD_ENTRY
scmi_smt_threaded_entry(unsigned int channel_id)125 void scmi_smt_threaded_entry(unsigned int channel_id)
126 {
127 assert(plat_scmi_get_channel(channel_id)->threaded);
128
129 scmi_entry_smt(channel_id, threaded_payload[thread_get_id()]);
130 }
131 #endif
132
133 #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)134 TEE_Result scmi_msg_threaded_entry(unsigned int channel_id,
135 void *in_buf, size_t in_size,
136 void *out_buf, size_t *out_size)
137 {
138 assert(plat_scmi_get_channel(channel_id)->threaded);
139
140 return scmi_entry_msg(channel_id, in_buf, in_size, out_buf, out_size,
141 threaded_payload[thread_get_id()]);
142 }
143 #endif
144