1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*
3  * Copyright (c) 2015-2019, Arm Limited and Contributors. All rights reserved.
4  * Copyright (c) 2019-2020, 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 
14 #include "base.h"
15 #include "clock.h"
16 #include "power_domain.h"
17 #include "reset_domain.h"
18 
19 #define SCMI_VERSION			0x20000U
20 #define SCMI_IMPL_VERSION		0U
21 
22 #define SCMI_PLAYLOAD_MAX		92U
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 		strlcpy((_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  * @agent_id: SCMI agent 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 agent_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  * scmi_msg_get_base_handler - Return a handler for a base message
95  * @msg - message to process
96  * Return a function handler for the message or NULL
97  */
98 scmi_msg_handler_t scmi_msg_get_base_handler(struct scmi_msg *msg);
99 
100 /*
101  * scmi_msg_get_clock_handler - Return a handler for a clock message
102  * @msg - message to process
103  * Return a function handler for the message or NULL
104  */
105 scmi_msg_handler_t scmi_msg_get_clock_handler(struct scmi_msg *msg);
106 
107 /*
108  * scmi_msg_get_rstd_handler - Return a handler for a reset domain message
109  * @msg - message to process
110  * Return a function handler for the message or NULL
111  */
112 scmi_msg_handler_t scmi_msg_get_rstd_handler(struct scmi_msg *msg);
113 
114 /*
115  * scmi_msg_get_pd_handler - Return a handler for a power domain message
116  * @msg - message to process
117  * Return a function handler for the message or NULL
118  */
119 scmi_msg_handler_t scmi_msg_get_pd_handler(struct scmi_msg *msg);
120 
121 /*
122  * Process Read, process and write response for input SCMI message
123  *
124  * @msg: SCMI message context
125  */
126 void scmi_process_message(struct scmi_msg *msg);
127 
128 /*
129  * Write SCMI response payload to output message shared memory
130  *
131  * @msg: SCMI message context
132  * @payload: Output message payload
133  * @size: Byte size of output message payload
134  */
135 void scmi_write_response(struct scmi_msg *msg, void *payload, size_t size);
136 
137 /*
138  * Write status only SCMI response payload to output message shared memory
139  *
140  * @msg: SCMI message context
141  * @status: SCMI status value returned to caller
142  */
143 void scmi_status_response(struct scmi_msg *msg, int32_t status);
144 #endif /* SCMI_MSG_COMMON_H */
145