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