xref: /optee_os/core/drivers/scmi-msg/smt.c (revision d9b0a06db5006c3b79bb98303d63cbfb080c9d4d)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2015-2019, Arm Limited and Contributors. All rights reserved.
4  * Copyright (c) 2019-2020, Linaro Limited
5  */
6 #include <assert.h>
7 #include <drivers/scmi-msg.h>
8 #include <drivers/scmi.h>
9 #include <io.h>
10 #include <kernel/misc.h>
11 #include <stdbool.h>
12 #include <stdint.h>
13 #include <string.h>
14 #include <trace.h>
15 #include <util.h>
16 
17 #include "common.h"
18 
19 #define SCMI_PAYLOAD_U32_MAX	(SCMI_SEC_PAYLOAD_SIZE / sizeof(uint32_t))
20 
21 /**
22  * struct smt_header - SMT formatted header for SMT base shared memory transfer
23  *
24  * @status: Bit flags, see SMT_STATUS_*
25  * @flags: Bit flags, see SMT_FLAG_*
26  * @length: Byte size of message payload (variable) + ::message_header (32bit)
27  * payload: SCMI message payload data
28  */
29 struct smt_header {
30 	uint32_t reserved0;
31 	uint32_t status;
32 	uint64_t reserved1;
33 	uint32_t flags;
34 	uint32_t length; /* message_header + payload */
35 	uint32_t message_header;
36 	uint32_t payload[];
37 };
38 
39 /* Flag set in smt_header::status when SMT does not contain pending message */
40 #define SMT_STATUS_FREE			BIT(0)
41 /* Flag set in smt_header::status when SMT reports an error */
42 #define SMT_STATUS_ERROR		BIT(1)
43 
44 /* Flag set in smt_header::flags when SMT uses interrupts */
45 #define SMT_FLAG_INTR_ENABLED		BIT(1)
46 
47 /* Bit fields packed in smt_header::message_header */
48 #define SMT_MSG_ID_MASK			GENMASK_32(7, 0)
49 #define SMT_HDR_MSG_ID(_hdr)		((_hdr) & SMT_MSG_ID_MASK)
50 
51 #define SMT_MSG_TYPE_MASK		GENMASK_32(9, 8)
52 #define SMT_HDR_TYPE_ID(_hdr)		(((_hdr) & SMT_MSG_TYPE_MASK) >> 8)
53 
54 #define SMT_MSG_PROT_ID_MASK		GENMASK_32(17, 10)
55 #define SMT_HDR_PROT_ID(_hdr)		(((_hdr) & SMT_MSG_PROT_ID_MASK) >> 10)
56 
57 static struct smt_header *channel_to_smt_hdr(struct scmi_msg_channel *chan)
58 {
59 	return (struct smt_header *)io_pa_or_va(&chan->shm_addr,
60 						sizeof(struct smt_header));
61 }
62 
63 /*
64  * Creates a SCMI message instance in secure memory and push it in the SCMI
65  * message drivers. Message structure contains SCMI protocol meta-data and
66  * references to input payload in secure memory and output message buffer
67  * in shared memory.
68  */
69 static void scmi_process_smt(unsigned int channel_id, uint32_t *payload_buf)
70 {
71 	struct scmi_msg_channel *chan = NULL;
72 	struct smt_header *smt_hdr = NULL;
73 	size_t in_payload_size = 0;
74 	uint32_t smt_status = 0;
75 	struct scmi_msg msg = { };
76 	bool error = true;
77 
78 	chan = plat_scmi_get_channel(channel_id);
79 	if (!chan)
80 		return;
81 
82 	smt_hdr = channel_to_smt_hdr(chan);
83 	assert(smt_hdr);
84 
85 	smt_status = READ_ONCE(smt_hdr->status);
86 
87 	if (!scmi_msg_claim_channel(chan)) {
88 		DMSG("SCMI channel %u busy", channel_id);
89 		goto out;
90 	}
91 
92 	in_payload_size = READ_ONCE(smt_hdr->length) -
93 			  sizeof(smt_hdr->message_header);
94 
95 	if (in_payload_size > SCMI_SEC_PAYLOAD_SIZE) {
96 		DMSG("SCMI payload too big %u", in_payload_size);
97 		goto out;
98 	}
99 
100 	if (smt_status & (SMT_STATUS_ERROR | SMT_STATUS_FREE)) {
101 		DMSG("SCMI channel bad status 0x%x",
102 		     smt_hdr->status & (SMT_STATUS_ERROR | SMT_STATUS_FREE));
103 		goto out;
104 	}
105 
106 	/* Fill message */
107 	msg.in = (char *)payload_buf;
108 	msg.in_size = in_payload_size;
109 	msg.out = (char *)smt_hdr->payload;
110 	msg.out_size = chan->shm_size - sizeof(*smt_hdr);
111 
112 	assert(msg.out && msg.out_size >= sizeof(int32_t));
113 
114 	/* Here the payload is copied in secure memory */
115 	memcpy(msg.in, smt_hdr->payload, in_payload_size);
116 
117 	msg.protocol_id = SMT_HDR_PROT_ID(smt_hdr->message_header);
118 	msg.message_id = SMT_HDR_MSG_ID(smt_hdr->message_header);
119 	msg.channel_id = channel_id;
120 
121 	scmi_process_message(&msg);
122 
123 	/* Update message length with the length of the response message */
124 	smt_hdr->length = msg.out_size_out + sizeof(smt_hdr->message_header);
125 
126 	scmi_msg_release_channel(chan);
127 	error = false;
128 
129 out:
130 	if (error) {
131 		DMSG("SCMI error");
132 		smt_hdr->status |= SMT_STATUS_ERROR | SMT_STATUS_FREE;
133 	} else {
134 		smt_hdr->status |= SMT_STATUS_FREE;
135 	}
136 }
137 
138 #ifdef CFG_SCMI_MSG_SMT_FASTCALL_ENTRY
139 /* Provision input message payload buffers for fastcall SMC context entries */
140 static uint32_t fast_smc_payload[CFG_TEE_CORE_NB_CORE][SCMI_PAYLOAD_U32_MAX];
141 
142 void scmi_smt_fastcall_smc_entry(unsigned int channel_id)
143 {
144 	scmi_process_smt(channel_id, fast_smc_payload[get_core_pos()]);
145 }
146 #endif
147 
148 #ifdef CFG_SCMI_MSG_SMT_INTERRUPT_ENTRY
149 /* Provision input message payload buffers for fastcall SMC context entries */
150 static uint32_t interrupt_payload[CFG_TEE_CORE_NB_CORE][SCMI_PAYLOAD_U32_MAX];
151 
152 void scmi_smt_interrupt_entry(unsigned int channel_id)
153 {
154 	scmi_process_smt(channel_id, interrupt_payload[get_core_pos()]);
155 }
156 #endif
157 
158 #ifdef CFG_SCMI_MSG_SMT_THREAD_ENTRY
159 /* Provision input message payload buffers for fastcall SMC context entries */
160 static uint32_t threaded_payload[CFG_NUM_THREADS][SCMI_PAYLOAD_U32_MAX];
161 
162 void scmi_smt_threaded_entry(unsigned int channel_id)
163 {
164 	assert(plat_scmi_get_channel(channel_id)->threaded);
165 
166 	scmi_process_smt(channel_id, threaded_payload[thread_get_id()]);
167 }
168 #endif
169 
170 /* Init a SMT header for a shared memory buffer: state it a free/no-error */
171 void scmi_smt_init_agent_channel(struct scmi_msg_channel *chan)
172 {
173 	static_assert(SCMI_SEC_PAYLOAD_SIZE + sizeof(struct smt_header) <=
174 		      SMT_BUF_SLOT_SIZE &&
175 		      IS_ALIGNED(SCMI_SEC_PAYLOAD_SIZE, sizeof(uint32_t)));
176 
177 	if (chan) {
178 		struct smt_header *smt_header = channel_to_smt_hdr(chan);
179 
180 		if (smt_header) {
181 			memset(smt_header, 0, sizeof(*smt_header));
182 			smt_header->status = SMT_STATUS_FREE;
183 
184 			return;
185 		}
186 	}
187 
188 	panic();
189 }
190 
191 void scmi_smt_set_shared_buffer(struct scmi_msg_channel *channel, void *base)
192 {
193 	paddr_t p_base = 0;
194 
195 	if (base) {
196 		assert(!channel->shm_addr.va && !channel->shm_addr.pa);
197 		p_base = virt_to_phys(base);
198 		assert(p_base);
199 	}
200 
201 	channel->shm_addr.va = (vaddr_t)base;
202 	channel->shm_addr.pa = p_base;
203 }
204