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