xref: /optee_os/core/drivers/scmi-msg/common.h (revision 916cc52ae46d43e1ad1ce87f4618b3c104eb15ee)
1ae8c8068SEtienne Carriere /* SPDX-License-Identifier: BSD-3-Clause */
2ae8c8068SEtienne Carriere /*
3ae8c8068SEtienne Carriere  * Copyright (c) 2015-2019, Arm Limited and Contributors. All rights reserved.
4ae8c8068SEtienne Carriere  * Copyright (c) 2019, Linaro Limited
5ae8c8068SEtienne Carriere  */
6ae8c8068SEtienne Carriere #ifndef SCMI_MSG_COMMON_H
7ae8c8068SEtienne Carriere #define SCMI_MSG_COMMON_H
8ae8c8068SEtienne Carriere 
9ae8c8068SEtienne Carriere #include <assert.h>
10ae8c8068SEtienne Carriere #include <stdbool.h>
11ae8c8068SEtienne Carriere #include <stdint.h>
12ae8c8068SEtienne Carriere #include <string.h>
13ae8c8068SEtienne Carriere #include <types_ext.h>
14ae8c8068SEtienne Carriere 
15c007fb39SEtienne Carriere #define SCMI_VERSION			0x30000
16ae8c8068SEtienne Carriere #define SCMI_IMPL_VERSION		0
17ae8c8068SEtienne Carriere 
18d9b0a06dSEtienne Carriere /*
19d9b0a06dSEtienne Carriere  * Secure copy of input payload: we expect small messages, at most the legacy
20d9b0a06dSEtienne Carriere  * SMT messages that are 128 bytes (Linux kernel) including SMT header.
21d9b0a06dSEtienne Carriere  */
22d9b0a06dSEtienne Carriere #define SCMI_SEC_PAYLOAD_SIZE		92
23ae8c8068SEtienne Carriere 
24ae8c8068SEtienne Carriere /*
25ae8c8068SEtienne Carriere  * Copy name identifier in target buffer following the SCMI specification
26ae8c8068SEtienne Carriere  * that state name identifier shall be a null terminated string.
27ae8c8068SEtienne Carriere  */
28ae8c8068SEtienne Carriere #define COPY_NAME_IDENTIFIER(_dst_array, _name)				\
29ae8c8068SEtienne Carriere 	do {								\
30ae8c8068SEtienne Carriere 		assert(strlen(_name) < sizeof(_dst_array));		\
31ae8c8068SEtienne Carriere 		strncpy((_dst_array), (_name), sizeof(_dst_array));	\
32ae8c8068SEtienne Carriere 	} while (0)
33ae8c8068SEtienne Carriere 
34ae8c8068SEtienne Carriere /* Common command identifiers shared by all procotols */
35ae8c8068SEtienne Carriere enum scmi_common_message_id {
36ae8c8068SEtienne Carriere 	SCMI_PROTOCOL_VERSION = 0x000,
37ae8c8068SEtienne Carriere 	SCMI_PROTOCOL_ATTRIBUTES = 0x001,
38ae8c8068SEtienne Carriere 	SCMI_PROTOCOL_MESSAGE_ATTRIBUTES = 0x002
39ae8c8068SEtienne Carriere };
40ae8c8068SEtienne Carriere 
41ae8c8068SEtienne Carriere /* Common platform-to-agent (p2a) PROTOCOL_VERSION structure */
42ae8c8068SEtienne Carriere struct scmi_protocol_version_p2a {
43ae8c8068SEtienne Carriere 	int32_t status;
44ae8c8068SEtienne Carriere 	uint32_t version;
45ae8c8068SEtienne Carriere };
46ae8c8068SEtienne Carriere 
47ae8c8068SEtienne Carriere /* Generic platform-to-agent (p2a) PROTOCOL_ATTRIBUTES structure */
48ae8c8068SEtienne Carriere struct scmi_protocol_attributes_p2a {
49ae8c8068SEtienne Carriere 	int32_t status;
50ae8c8068SEtienne Carriere 	uint32_t attributes;
51ae8c8068SEtienne Carriere };
52ae8c8068SEtienne Carriere 
53ae8c8068SEtienne Carriere /* Generic agent-to-platform (a2p) PROTOCOL_MESSAGE_ATTRIBUTES structure */
54ae8c8068SEtienne Carriere struct scmi_protocol_message_attributes_a2p {
55ae8c8068SEtienne Carriere 	uint32_t message_id;
56ae8c8068SEtienne Carriere };
57ae8c8068SEtienne Carriere 
58ae8c8068SEtienne Carriere /* Generic platform-to-agent (p2a) PROTOCOL_MESSAGE_ATTRIBUTES structure */
59ae8c8068SEtienne Carriere struct scmi_protocol_message_attributes_p2a {
60ae8c8068SEtienne Carriere 	int32_t status;
61ae8c8068SEtienne Carriere 	uint32_t attributes;
62ae8c8068SEtienne Carriere };
63ae8c8068SEtienne Carriere 
64ae8c8068SEtienne Carriere /*
65ae8c8068SEtienne Carriere  * struct scmi_msg - SCMI message context
66ae8c8068SEtienne Carriere  *
67659a1f88SEtienne Carriere  * @channel_id: SCMI channel ID, safely set from secure world
68ae8c8068SEtienne Carriere  * @protocol_id: SCMI protocol ID for the related message, set by caller agent
69ae8c8068SEtienne Carriere  * @message_id: SCMI message ID for the related message, set by caller agent
70ae8c8068SEtienne Carriere  * @in: Address of the incoming message payload copied in secure memory
71ae8c8068SEtienne Carriere  * @in_size: Byte length of the incoming message payload, set by caller agent
72ae8c8068SEtienne Carriere  * @out: Address of of the output message payload message in non-secure memory
73ae8c8068SEtienne Carriere  * @out_size: Byte length of the provisionned output buffer
74ae8c8068SEtienne Carriere  * @out_size_out: Byte length of the output message payload
75ae8c8068SEtienne Carriere  */
76ae8c8068SEtienne Carriere struct scmi_msg {
77659a1f88SEtienne Carriere 	unsigned int channel_id;
78ae8c8068SEtienne Carriere 	unsigned int protocol_id;
79ae8c8068SEtienne Carriere 	unsigned int message_id;
80ae8c8068SEtienne Carriere 	char *in;
81ae8c8068SEtienne Carriere 	size_t in_size;
82ae8c8068SEtienne Carriere 	char *out;
83ae8c8068SEtienne Carriere 	size_t out_size;
84ae8c8068SEtienne Carriere 	size_t out_size_out;
85ae8c8068SEtienne Carriere };
86ae8c8068SEtienne Carriere 
87ae8c8068SEtienne Carriere /*
88ae8c8068SEtienne Carriere  * Type scmi_msg_handler_t is used by procotol drivers to safely find
89ae8c8068SEtienne Carriere  * the handler function for the incoming message ID.
90ae8c8068SEtienne Carriere  */
91ae8c8068SEtienne Carriere typedef void (*scmi_msg_handler_t)(struct scmi_msg *msg);
92ae8c8068SEtienne Carriere 
93ae8c8068SEtienne Carriere /*
94ae8c8068SEtienne Carriere  * Process Read, process and write response for input SCMI message
95ae8c8068SEtienne Carriere  *
96ae8c8068SEtienne Carriere  * @msg: SCMI message context
97ae8c8068SEtienne Carriere  */
98ae8c8068SEtienne Carriere void scmi_process_message(struct scmi_msg *msg);
99ae8c8068SEtienne Carriere 
100ae8c8068SEtienne Carriere /*
101ae8c8068SEtienne Carriere  * Write SCMI response payload to output message shared memory
102ae8c8068SEtienne Carriere  *
103ae8c8068SEtienne Carriere  * @msg: SCMI message context
104ae8c8068SEtienne Carriere  * @payload: Output message payload
105ae8c8068SEtienne Carriere  * @size: Byte size of output message payload
106ae8c8068SEtienne Carriere  */
107ae8c8068SEtienne Carriere void scmi_write_response(struct scmi_msg *msg, void *payload, size_t size);
108ae8c8068SEtienne Carriere 
109ae8c8068SEtienne Carriere /*
110ae8c8068SEtienne Carriere  * Write status only SCMI response payload to output message shared memory
111ae8c8068SEtienne Carriere  *
112ae8c8068SEtienne Carriere  * @msg: SCMI message context
113ae8c8068SEtienne Carriere  * @status: SCMI status value returned to caller
114ae8c8068SEtienne Carriere  */
115ae8c8068SEtienne Carriere void scmi_status_response(struct scmi_msg *msg, int32_t status);
1165c34a982SEtienne Carriere 
1175c34a982SEtienne Carriere /*
1185c34a982SEtienne Carriere  * Claim access to channel
1195c34a982SEtienne Carriere  * @channel: SCMI channel reference
1205c34a982SEtienne Carriere  * Return true upon success or false if the channel is already busy
1215c34a982SEtienne Carriere  */
1225c34a982SEtienne Carriere bool scmi_msg_claim_channel(struct scmi_msg_channel *channel);
1235c34a982SEtienne Carriere 
1245c34a982SEtienne Carriere /*
1255c34a982SEtienne Carriere  * Release access channel
1265c34a982SEtienne Carriere  * @channel: SCMI channel reference
1275c34a982SEtienne Carriere  */
1285c34a982SEtienne Carriere void scmi_msg_release_channel(struct scmi_msg_channel *channel);
1294a382700SEtienne Carriere 
1304a382700SEtienne Carriere /*
1314a382700SEtienne Carriere  * Entry for processing a channel using SMT shared memory protocol
1324a382700SEtienne Carriere  * @channel_id: SCMI channel identifier provided by client
1334a382700SEtienne Carriere  * @payload_buf: Secure buffer where to copy input message
1344a382700SEtienne Carriere  */
1354a382700SEtienne Carriere void scmi_entry_smt(unsigned int channel_id, uint32_t *payload_buf);
136*916cc52aSEtienne Carriere 
137*916cc52aSEtienne Carriere /*
138*916cc52aSEtienne Carriere  * Entry for processing a channel using SMT shared memory protocol
139*916cc52aSEtienne Carriere  *
140*916cc52aSEtienne Carriere  * @channel_id: SCMI channel identifier provided by client
141*916cc52aSEtienne Carriere  * @in_buf: Shared buffer storing input SCMI message
142*916cc52aSEtienne Carriere  * @in_size: Byte size of @in_buf, including MSG header and message payload
143*916cc52aSEtienne Carriere  * @out_buf: Shared buffer storing input SCMI message
144*916cc52aSEtienne Carriere  * @out_size: [in] @out_buf max byte size
145*916cc52aSEtienne Carriere  *            [out] @out_buf output byte size (MSG header and message payload)
146*916cc52aSEtienne Carriere  * @sec_buf: Secure buffer where to copy input message
147*916cc52aSEtienne Carriere  */
148*916cc52aSEtienne Carriere TEE_Result scmi_entry_msg(unsigned int channel_id, void *in_buf, size_t in_size,
149*916cc52aSEtienne Carriere 			  void *out_buf, size_t *out_size, uint32_t *sec_buf);
150ae8c8068SEtienne Carriere #endif /* SCMI_MSG_COMMON_H */
151