blob: 2d7203451fa95f3e77494527df34c0dfd376f80a [file] [log] [blame]
Etienne Carriere13b353c2019-11-28 09:13:34 +01001// 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#include <assert.h>
7#include <string.h>
8
Peng Fan8053e072021-01-20 11:04:08 +08009#include <drivers/scmi-msg.h>
10#include <drivers/scmi.h>
Etienne Carriere13b353c2019-11-28 09:13:34 +010011#include <lib/utils.h>
12#include <lib/utils_def.h>
13
14#include "common.h"
15
16static bool message_id_is_supported(unsigned int message_id);
17
18static 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
33static 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
50static 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
72static 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
89static 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
106static 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
121static 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
134#define MAX_PROTOCOL_IN_LIST 8U
135
136static void discover_list_protocols(struct scmi_msg *msg)
137{
138 const struct scmi_base_discover_list_protocols_a2p *a2p = NULL;
139 struct scmi_base_discover_list_protocols_p2a p2a = {
140 .status = SCMI_SUCCESS,
141 };
142 uint8_t outargs[sizeof(p2a) + MAX_PROTOCOL_IN_LIST] = { 0U };
143 const uint8_t *list = NULL;
144 unsigned int count = 0U;
145
146 if (msg->in_size != sizeof(*a2p)) {
147 scmi_status_response(msg, SCMI_PROTOCOL_ERROR);
148 return;
149 }
150
151 assert(msg->out_size > sizeof(outargs));
152
153 a2p = (void *)msg->in;
154
155 list = plat_scmi_protocol_list(msg->agent_id);
156 count = count_protocols_in_list(list);
157 if (count > a2p->skip) {
158 count = MIN(count - a2p->skip, MAX_PROTOCOL_IN_LIST);
159 } else {
160 count = 0U;
161 }
162
163 p2a.num_protocols = count;
164
165 memcpy(outargs, &p2a, sizeof(p2a));
166 memcpy(outargs + sizeof(p2a), list + a2p->skip, count);
167
168 scmi_write_response(msg, outargs, sizeof(outargs));
169}
170
171static const scmi_msg_handler_t scmi_base_handler_table[] = {
172 [SCMI_PROTOCOL_VERSION] = report_version,
173 [SCMI_PROTOCOL_ATTRIBUTES] = report_attributes,
174 [SCMI_PROTOCOL_MESSAGE_ATTRIBUTES] = report_message_attributes,
175 [SCMI_BASE_DISCOVER_VENDOR] = discover_vendor,
176 [SCMI_BASE_DISCOVER_SUB_VENDOR] = discover_sub_vendor,
177 [SCMI_BASE_DISCOVER_IMPLEMENTATION_VERSION] =
178 discover_implementation_version,
179 [SCMI_BASE_DISCOVER_LIST_PROTOCOLS] = discover_list_protocols,
180};
181
182static bool message_id_is_supported(unsigned int message_id)
183{
184 return (message_id < ARRAY_SIZE(scmi_base_handler_table)) &&
185 (scmi_base_handler_table[message_id] != NULL);
186}
187
188scmi_msg_handler_t scmi_msg_get_base_handler(struct scmi_msg *msg)
189{
190 unsigned int message_id = SPECULATION_SAFE_VALUE(msg->message_id);
191
192 if (message_id >= ARRAY_SIZE(scmi_base_handler_table)) {
193 VERBOSE("Base handle not found %u\n", msg->message_id);
194 return NULL;
195 }
196
197 return scmi_base_handler_table[message_id];
198}