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(count - a2p->skip, msg->out_size - sizeof(p2a));
155 	} else {
156 		count = 0U;
157 	}
158 
159 	p2a.num_protocols = count;
160 
161 	memcpy(msg->out, &p2a, sizeof(p2a));
162 	memcpy(msg->out + sizeof(p2a), list + a2p->skip, count);
163 	msg->out_size_out = sizeof(p2a) + round_up(count, sizeof(uint32_t));
164 }
165 
166 static const scmi_msg_handler_t scmi_base_handler_table[] = {
167 	[SCMI_PROTOCOL_VERSION] = report_version,
168 	[SCMI_PROTOCOL_ATTRIBUTES] = report_attributes,
169 	[SCMI_PROTOCOL_MESSAGE_ATTRIBUTES] = report_message_attributes,
170 	[SCMI_BASE_DISCOVER_VENDOR] = discover_vendor,
171 	[SCMI_BASE_DISCOVER_SUB_VENDOR] = discover_sub_vendor,
172 	[SCMI_BASE_DISCOVER_IMPLEMENTATION_VERSION] =
173 					discover_implementation_version,
174 	[SCMI_BASE_DISCOVER_LIST_PROTOCOLS] = discover_list_protocols,
175 };
176 
message_id_is_supported(unsigned int message_id)177 static bool message_id_is_supported(unsigned int message_id)
178 {
179 	return (message_id < ARRAY_SIZE(scmi_base_handler_table)) &&
180 	       (scmi_base_handler_table[message_id] != NULL);
181 }
182 
scmi_msg_get_base_handler(struct scmi_msg * msg)183 scmi_msg_handler_t scmi_msg_get_base_handler(struct scmi_msg *msg)
184 {
185 	unsigned int message_id = SPECULATION_SAFE_VALUE(msg->message_id);
186 
187 	if (message_id >= ARRAY_SIZE(scmi_base_handler_table)) {
188 		VERBOSE("Base handle not found %u\n", msg->message_id);
189 		return NULL;
190 	}
191 
192 	return scmi_base_handler_table[message_id];
193 }
194