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