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