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