1 // SPDX-License-Identifier: BSD-3-Clause 2 /* 3 * Copyright (c) 2015-2025, 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 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 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 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 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 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 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 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 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 unsigned int rounded_count = 0U; 143 int overflow; 144 145 if (msg->in_size != sizeof(*a2p)) { 146 scmi_status_response(msg, SCMI_PROTOCOL_ERROR); 147 return; 148 } 149 150 a2p = (void *)msg->in; 151 152 list = plat_scmi_protocol_list(msg->agent_id); 153 count = count_protocols_in_list(list); 154 155 if (count > a2p->skip) { 156 count = MIN((uint32_t)(count - a2p->skip), 157 (uint32_t)(msg->out_size - sizeof(p2a))); 158 } else { 159 count = 0U; 160 } 161 162 p2a.num_protocols = count; 163 164 memcpy(msg->out, &p2a, sizeof(p2a)); 165 memcpy(msg->out + sizeof(p2a), list + a2p->skip, count); 166 167 overflow = round_up_overflow(count, sizeof(uint32_t), &rounded_count); 168 if (overflow) { 169 ERROR("Overflow rounding up protocol count\n"); 170 panic(); 171 } 172 173 msg->out_size_out = sizeof(p2a) + rounded_count; 174 } 175 176 static const scmi_msg_handler_t scmi_base_handler_table[] = { 177 [SCMI_PROTOCOL_VERSION] = report_version, 178 [SCMI_PROTOCOL_ATTRIBUTES] = report_attributes, 179 [SCMI_PROTOCOL_MESSAGE_ATTRIBUTES] = report_message_attributes, 180 [SCMI_BASE_DISCOVER_VENDOR] = discover_vendor, 181 [SCMI_BASE_DISCOVER_SUB_VENDOR] = discover_sub_vendor, 182 [SCMI_BASE_DISCOVER_IMPLEMENTATION_VERSION] = 183 discover_implementation_version, 184 [SCMI_BASE_DISCOVER_LIST_PROTOCOLS] = discover_list_protocols, 185 }; 186 187 static bool message_id_is_supported(unsigned int message_id) 188 { 189 return (message_id < ARRAY_SIZE(scmi_base_handler_table)) && 190 (scmi_base_handler_table[message_id] != NULL); 191 } 192 193 scmi_msg_handler_t scmi_msg_get_base_handler(struct scmi_msg *msg) 194 { 195 unsigned int message_id = SPECULATION_SAFE_VALUE(msg->message_id); 196 197 if (message_id >= ARRAY_SIZE(scmi_base_handler_table)) { 198 VERBOSE("Base handle not found %u\n", msg->message_id); 199 return NULL; 200 } 201 202 return scmi_base_handler_table[message_id]; 203 } 204