base.c 4.9 KB

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