1 // SPDX-License-Identifier: BSD-3-Clause
2 /*
3  * Copyright (c) 2015-2019, Arm Limited and Contributors. All rights reserved.
4  * Copyright (c) 2019-2022, Linaro Limited
5  */
6 #include <assert.h>
7 #include <string.h>
8 
9 #include <drivers/scmi-msg.h>
10 #include <drivers/scmi.h>
11 #include <lib/utils.h>
12 #include <lib/utils_def.h>
13 
14 #include "common.h"
15 
16 static bool message_id_is_supported(unsigned int message_id);
17 
report_version(struct scmi_msg * msg)18 static void report_version(struct scmi_msg *msg)
19 {
20 	struct scmi_protocol_version_p2a return_values = {
21 		.status = SCMI_SUCCESS,
22 		.version = SCMI_PROTOCOL_VERSION_BASE,
23 	};
24 
25 	if (msg->in_size != 0U) {
26 		scmi_status_response(msg, SCMI_PROTOCOL_ERROR);
27 		return;
28 	}
29 
30 	scmi_write_response(msg, &return_values, sizeof(return_values));
31 }
32 
report_attributes(struct scmi_msg * msg)33 static void report_attributes(struct scmi_msg *msg)
34 {
35 	size_t protocol_count = plat_scmi_protocol_count();
36 	struct scmi_protocol_attributes_p2a return_values = {
37 		.status = SCMI_SUCCESS,
38 		/* Null agent count since agent discovery is not supported */
39 		.attributes = SCMI_BASE_PROTOCOL_ATTRIBUTES(protocol_count, 0U),
40 	};
41 
42 	if (msg->in_size != 0U) {
43 		scmi_status_response(msg, SCMI_PROTOCOL_ERROR);
44 		return;
45 	}
46 
47 	scmi_write_response(msg, &return_values, sizeof(return_values));
48 }
49 
report_message_attributes(struct scmi_msg * msg)50 static void report_message_attributes(struct scmi_msg *msg)
51 {
52 	struct scmi_protocol_message_attributes_a2p *in_args = (void *)msg->in;
53 	struct scmi_protocol_message_attributes_p2a return_values = {
54 		.status = SCMI_SUCCESS,
55 		/* For this protocol, attributes shall be zero */
56 		.attributes = 0U,
57 	};
58 
59 	if (msg->in_size != sizeof(*in_args)) {
60 		scmi_status_response(msg, SCMI_PROTOCOL_ERROR);
61 		return;
62 	}
63 
64 	if (!message_id_is_supported(in_args->message_id)) {
65 		scmi_status_response(msg, SCMI_NOT_FOUND);
66 		return;
67 	}
68 
69 	scmi_write_response(msg, &return_values, sizeof(return_values));
70 }
71 
discover_vendor(struct scmi_msg * msg)72 static void discover_vendor(struct scmi_msg *msg)
73 {
74 	const char *name = plat_scmi_vendor_name();
75 	struct scmi_base_discover_vendor_p2a return_values = {
76 		.status = SCMI_SUCCESS,
77 	};
78 
79 	if (msg->in_size != 0U) {
80 		scmi_status_response(msg, SCMI_PROTOCOL_ERROR);
81 		return;
82 	}
83 
84 	COPY_NAME_IDENTIFIER(return_values.vendor_identifier, name);
85 
86 	scmi_write_response(msg, &return_values, sizeof(return_values));
87 }
88 
discover_sub_vendor(struct scmi_msg * msg)89 static void discover_sub_vendor(struct scmi_msg *msg)
90 {
91 	const char *name = plat_scmi_sub_vendor_name();
92 	struct scmi_base_discover_sub_vendor_p2a return_values = {
93 		.status = SCMI_SUCCESS,
94 	};
95 
96 	if (msg->in_size != 0U) {
97 		scmi_status_response(msg, SCMI_PROTOCOL_ERROR);
98 		return;
99 	}
100 
101 	COPY_NAME_IDENTIFIER(return_values.sub_vendor_identifier, name);
102 
103 	scmi_write_response(msg, &return_values, sizeof(return_values));
104 }
105 
discover_implementation_version(struct scmi_msg * msg)106 static void discover_implementation_version(struct scmi_msg *msg)
107 {
108 	struct scmi_protocol_version_p2a return_values = {
109 		.status = SCMI_SUCCESS,
110 		.version = SCMI_IMPL_VERSION,
111 	};
112 
113 	if (msg->in_size != 0U) {
114 		scmi_status_response(msg, SCMI_PROTOCOL_ERROR);
115 		return;
116 	}
117 
118 	scmi_write_response(msg, &return_values, sizeof(return_values));
119 }
120 
count_protocols_in_list(const uint8_t * protocol_list)121 static unsigned int count_protocols_in_list(const uint8_t *protocol_list)
122 {
123 	unsigned int count = 0U;
124 
125 	if (protocol_list != NULL) {
126 		while (protocol_list[count] != 0U) {
127 			count++;
128 		}
129 	}
130 
131 	return count;
132 }
133 
discover_list_protocols(struct scmi_msg * msg)134 static void discover_list_protocols(struct scmi_msg *msg)
135 {
136 	const struct scmi_base_discover_list_protocols_a2p *a2p = NULL;
137 	struct scmi_base_discover_list_protocols_p2a p2a = {
138 		.status = SCMI_SUCCESS,
139 	};
140 	const uint8_t *list = NULL;
141 	unsigned int count = 0U;
142 
143 	if (msg->in_size != sizeof(*a2p)) {
144 		scmi_status_response(msg, SCMI_PROTOCOL_ERROR);
145 		return;
146 	}
147 
148 	a2p = (void *)msg->in;
149 
150 	list = plat_scmi_protocol_list(msg->agent_id);
151 	count = count_protocols_in_list(list);
152 
153 	if (count > a2p->skip) {
154 		count = MIN((uint32_t)(count - a2p->skip),
155 			    (uint32_t)(msg->out_size - sizeof(p2a)));
156 	} else {
157 		count = 0U;
158 	}
159 
160 	p2a.num_protocols = count;
161 
162 	memcpy(msg->out, &p2a, sizeof(p2a));
163 	memcpy(msg->out + sizeof(p2a), list + a2p->skip, count);
164 	msg->out_size_out = sizeof(p2a) + round_up(count, sizeof(uint32_t));
165 }
166 
167 static const scmi_msg_handler_t scmi_base_handler_table[] = {
168 	[SCMI_PROTOCOL_VERSION] = report_version,
169 	[SCMI_PROTOCOL_ATTRIBUTES] = report_attributes,
170 	[SCMI_PROTOCOL_MESSAGE_ATTRIBUTES] = report_message_attributes,
171 	[SCMI_BASE_DISCOVER_VENDOR] = discover_vendor,
172 	[SCMI_BASE_DISCOVER_SUB_VENDOR] = discover_sub_vendor,
173 	[SCMI_BASE_DISCOVER_IMPLEMENTATION_VERSION] =
174 					discover_implementation_version,
175 	[SCMI_BASE_DISCOVER_LIST_PROTOCOLS] = discover_list_protocols,
176 };
177 
message_id_is_supported(unsigned int message_id)178 static bool message_id_is_supported(unsigned int message_id)
179 {
180 	return (message_id < ARRAY_SIZE(scmi_base_handler_table)) &&
181 	       (scmi_base_handler_table[message_id] != NULL);
182 }
183 
scmi_msg_get_base_handler(struct scmi_msg * msg)184 scmi_msg_handler_t scmi_msg_get_base_handler(struct scmi_msg *msg)
185 {
186 	unsigned int message_id = SPECULATION_SAFE_VALUE(msg->message_id);
187 
188 	if (message_id >= ARRAY_SIZE(scmi_base_handler_table)) {
189 		VERBOSE("Base handle not found %u\n", msg->message_id);
190 		return NULL;
191 	}
192 
193 	return scmi_base_handler_table[message_id];
194 }
195