clock.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. // SPDX-License-Identifier: BSD-3-Clause
  2. /*
  3. * Copyright (c) 2015-2020, Arm Limited and Contributors. All rights reserved.
  4. * Copyright (c) 2019-2020, Linaro Limited
  5. */
  6. #include <cdefs.h>
  7. #include <string.h>
  8. #include <drivers/scmi-msg.h>
  9. #include <drivers/scmi.h>
  10. #include <lib/utils_def.h>
  11. #include "common.h"
  12. #pragma weak plat_scmi_clock_count
  13. #pragma weak plat_scmi_clock_get_name
  14. #pragma weak plat_scmi_clock_rates_array
  15. #pragma weak plat_scmi_clock_rates_by_step
  16. #pragma weak plat_scmi_clock_get_rate
  17. #pragma weak plat_scmi_clock_set_rate
  18. #pragma weak plat_scmi_clock_get_state
  19. #pragma weak plat_scmi_clock_set_state
  20. static bool message_id_is_supported(unsigned int message_id);
  21. size_t plat_scmi_clock_count(unsigned int agent_id __unused)
  22. {
  23. return 0U;
  24. }
  25. const char *plat_scmi_clock_get_name(unsigned int agent_id __unused,
  26. unsigned int scmi_id __unused)
  27. {
  28. return NULL;
  29. }
  30. int32_t plat_scmi_clock_rates_array(unsigned int agent_id __unused,
  31. unsigned int scmi_id __unused,
  32. unsigned long *rates __unused,
  33. size_t *nb_elts __unused)
  34. {
  35. return SCMI_NOT_SUPPORTED;
  36. }
  37. int32_t plat_scmi_clock_rates_by_step(unsigned int agent_id __unused,
  38. unsigned int scmi_id __unused,
  39. unsigned long *steps __unused)
  40. {
  41. return SCMI_NOT_SUPPORTED;
  42. }
  43. unsigned long plat_scmi_clock_get_rate(unsigned int agent_id __unused,
  44. unsigned int scmi_id __unused)
  45. {
  46. return 0U;
  47. }
  48. int32_t plat_scmi_clock_set_rate(unsigned int agent_id __unused,
  49. unsigned int scmi_id __unused,
  50. unsigned long rate __unused)
  51. {
  52. return SCMI_NOT_SUPPORTED;
  53. }
  54. int32_t plat_scmi_clock_get_state(unsigned int agent_id __unused,
  55. unsigned int scmi_id __unused)
  56. {
  57. return SCMI_NOT_SUPPORTED;
  58. }
  59. int32_t plat_scmi_clock_set_state(unsigned int agent_id __unused,
  60. unsigned int scmi_id __unused,
  61. bool enable_not_disable __unused)
  62. {
  63. return SCMI_NOT_SUPPORTED;
  64. }
  65. static void report_version(struct scmi_msg *msg)
  66. {
  67. struct scmi_protocol_version_p2a return_values = {
  68. .status = SCMI_SUCCESS,
  69. .version = SCMI_PROTOCOL_VERSION_CLOCK,
  70. };
  71. if (msg->in_size != 0) {
  72. scmi_status_response(msg, SCMI_PROTOCOL_ERROR);
  73. return;
  74. }
  75. scmi_write_response(msg, &return_values, sizeof(return_values));
  76. }
  77. static void report_attributes(struct scmi_msg *msg)
  78. {
  79. size_t agent_count = plat_scmi_clock_count(msg->agent_id);
  80. struct scmi_protocol_attributes_p2a return_values = {
  81. .status = SCMI_SUCCESS,
  82. .attributes = SCMI_CLOCK_PROTOCOL_ATTRIBUTES(1U, agent_count),
  83. };
  84. if (msg->in_size != 0) {
  85. scmi_status_response(msg, SCMI_PROTOCOL_ERROR);
  86. return;
  87. }
  88. scmi_write_response(msg, &return_values, sizeof(return_values));
  89. }
  90. static void report_message_attributes(struct scmi_msg *msg)
  91. {
  92. struct scmi_protocol_message_attributes_a2p *in_args = (void *)msg->in;
  93. struct scmi_protocol_message_attributes_p2a return_values = {
  94. .status = SCMI_SUCCESS,
  95. /* For this protocol, attributes shall be zero */
  96. .attributes = 0U,
  97. };
  98. if (msg->in_size != sizeof(*in_args)) {
  99. scmi_status_response(msg, SCMI_PROTOCOL_ERROR);
  100. return;
  101. }
  102. if (!message_id_is_supported(in_args->message_id)) {
  103. scmi_status_response(msg, SCMI_NOT_FOUND);
  104. return;
  105. }
  106. scmi_write_response(msg, &return_values, sizeof(return_values));
  107. }
  108. static void scmi_clock_attributes(struct scmi_msg *msg)
  109. {
  110. const struct scmi_clock_attributes_a2p *in_args = (void *)msg->in;
  111. struct scmi_clock_attributes_p2a return_values = {
  112. .status = SCMI_SUCCESS,
  113. };
  114. const char *name = NULL;
  115. unsigned int clock_id = 0U;
  116. if (msg->in_size != sizeof(*in_args)) {
  117. scmi_status_response(msg, SCMI_PROTOCOL_ERROR);
  118. return;
  119. }
  120. clock_id = SPECULATION_SAFE_VALUE(in_args->clock_id);
  121. if (clock_id >= plat_scmi_clock_count(msg->agent_id)) {
  122. scmi_status_response(msg, SCMI_INVALID_PARAMETERS);
  123. return;
  124. }
  125. name = plat_scmi_clock_get_name(msg->agent_id, clock_id);
  126. if (name == NULL) {
  127. scmi_status_response(msg, SCMI_NOT_FOUND);
  128. return;
  129. }
  130. COPY_NAME_IDENTIFIER(return_values.clock_name, name);
  131. return_values.attributes = plat_scmi_clock_get_state(msg->agent_id,
  132. clock_id);
  133. scmi_write_response(msg, &return_values, sizeof(return_values));
  134. }
  135. static void scmi_clock_rate_get(struct scmi_msg *msg)
  136. {
  137. const struct scmi_clock_rate_get_a2p *in_args = (void *)msg->in;
  138. unsigned long rate = 0U;
  139. struct scmi_clock_rate_get_p2a return_values = {
  140. .status = SCMI_SUCCESS,
  141. };
  142. unsigned int clock_id = 0U;
  143. if (msg->in_size != sizeof(*in_args)) {
  144. scmi_status_response(msg, SCMI_PROTOCOL_ERROR);
  145. return;
  146. }
  147. clock_id = SPECULATION_SAFE_VALUE(in_args->clock_id);
  148. if (clock_id >= plat_scmi_clock_count(msg->agent_id)) {
  149. scmi_status_response(msg, SCMI_INVALID_PARAMETERS);
  150. return;
  151. }
  152. rate = plat_scmi_clock_get_rate(msg->agent_id, clock_id);
  153. return_values.rate[0] = (uint32_t)rate;
  154. return_values.rate[1] = (uint32_t)((uint64_t)rate >> 32);
  155. scmi_write_response(msg, &return_values, sizeof(return_values));
  156. }
  157. static void scmi_clock_rate_set(struct scmi_msg *msg)
  158. {
  159. const struct scmi_clock_rate_set_a2p *in_args = (void *)msg->in;
  160. unsigned long rate = 0U;
  161. int32_t status = 0;
  162. unsigned int clock_id = 0U;
  163. if (msg->in_size != sizeof(*in_args)) {
  164. scmi_status_response(msg, SCMI_PROTOCOL_ERROR);
  165. return;
  166. }
  167. clock_id = SPECULATION_SAFE_VALUE(in_args->clock_id);
  168. if (clock_id >= plat_scmi_clock_count(msg->agent_id)) {
  169. scmi_status_response(msg, SCMI_INVALID_PARAMETERS);
  170. return;
  171. }
  172. rate = (unsigned long)(((uint64_t)in_args->rate[1] << 32) |
  173. in_args->rate[0]);
  174. status = plat_scmi_clock_set_rate(msg->agent_id, clock_id, rate);
  175. scmi_status_response(msg, status);
  176. }
  177. static void scmi_clock_config_set(struct scmi_msg *msg)
  178. {
  179. const struct scmi_clock_config_set_a2p *in_args = (void *)msg->in;
  180. int32_t status = SCMI_GENERIC_ERROR;
  181. bool enable = false;
  182. unsigned int clock_id = 0U;
  183. if (msg->in_size != sizeof(*in_args)) {
  184. scmi_status_response(msg, SCMI_PROTOCOL_ERROR);
  185. return;
  186. }
  187. clock_id = SPECULATION_SAFE_VALUE(in_args->clock_id);
  188. if (clock_id >= plat_scmi_clock_count(msg->agent_id)) {
  189. scmi_status_response(msg, SCMI_INVALID_PARAMETERS);
  190. return;
  191. }
  192. enable = in_args->attributes & SCMI_CLOCK_CONFIG_SET_ENABLE_MASK;
  193. status = plat_scmi_clock_set_state(msg->agent_id, clock_id, enable);
  194. scmi_status_response(msg, status);
  195. }
  196. #define RATES_ARRAY_SIZE_MAX (SCMI_PLAYLOAD_MAX - \
  197. sizeof(struct scmi_clock_describe_rates_p2a))
  198. #define SCMI_RATES_BY_ARRAY(_nb_rates, _rem_rates) \
  199. SCMI_CLOCK_DESCRIBE_RATES_NUM_RATES_FLAGS((_nb_rates), \
  200. SCMI_CLOCK_RATE_FORMAT_LIST, \
  201. (_rem_rates))
  202. #define SCMI_RATES_BY_STEP \
  203. SCMI_CLOCK_DESCRIBE_RATES_NUM_RATES_FLAGS(3U, \
  204. SCMI_CLOCK_RATE_FORMAT_RANGE, \
  205. 0U)
  206. #define RATE_DESC_SIZE sizeof(struct scmi_clock_rate)
  207. static void write_rate_desc_array_in_buffer(char *dest, unsigned long *rates,
  208. size_t nb_elt)
  209. {
  210. uint32_t *out = (uint32_t *)(uintptr_t)dest;
  211. size_t n;
  212. ASSERT_SYM_PTR_ALIGN(out);
  213. for (n = 0U; n < nb_elt; n++) {
  214. out[2 * n] = (uint32_t)rates[n];
  215. out[2 * n + 1] = (uint32_t)((uint64_t)rates[n] >> 32);
  216. }
  217. }
  218. static void scmi_clock_describe_rates(struct scmi_msg *msg)
  219. {
  220. const struct scmi_clock_describe_rates_a2p *in_args = (void *)msg->in;
  221. struct scmi_clock_describe_rates_p2a p2a = {
  222. .status = SCMI_SUCCESS,
  223. };
  224. size_t nb_rates;
  225. int32_t status;
  226. unsigned int clock_id;
  227. if (msg->in_size != sizeof(*in_args)) {
  228. scmi_status_response(msg, SCMI_PROTOCOL_ERROR);
  229. return;
  230. }
  231. clock_id = SPECULATION_SAFE_VALUE(in_args->clock_id);
  232. if (clock_id >= plat_scmi_clock_count(msg->agent_id)) {
  233. scmi_status_response(msg, SCMI_INVALID_PARAMETERS);
  234. return;
  235. }
  236. /* Platform may support array rate description */
  237. status = plat_scmi_clock_rates_array(msg->agent_id, clock_id, NULL,
  238. &nb_rates);
  239. if (status == SCMI_SUCCESS) {
  240. /* Currently 12 cells mex, so it's affordable for the stack */
  241. unsigned long plat_rates[RATES_ARRAY_SIZE_MAX / RATE_DESC_SIZE];
  242. size_t max_nb = RATES_ARRAY_SIZE_MAX / RATE_DESC_SIZE;
  243. size_t ret_nb = MIN(nb_rates - in_args->rate_index, max_nb);
  244. size_t rem_nb = nb_rates - in_args->rate_index - ret_nb;
  245. status = plat_scmi_clock_rates_array(msg->agent_id, clock_id,
  246. plat_rates, &ret_nb);
  247. if (status == SCMI_SUCCESS) {
  248. write_rate_desc_array_in_buffer(msg->out + sizeof(p2a),
  249. plat_rates, ret_nb);
  250. p2a.num_rates_flags = SCMI_RATES_BY_ARRAY(ret_nb,
  251. rem_nb);
  252. p2a.status = SCMI_SUCCESS;
  253. memcpy(msg->out, &p2a, sizeof(p2a));
  254. msg->out_size_out = sizeof(p2a) +
  255. ret_nb * RATE_DESC_SIZE;
  256. }
  257. } else if (status == SCMI_NOT_SUPPORTED) {
  258. unsigned long triplet[3] = { 0U, 0U, 0U };
  259. /* Platform may support min§max/step triplet description */
  260. status = plat_scmi_clock_rates_by_step(msg->agent_id, clock_id,
  261. triplet);
  262. if (status == SCMI_SUCCESS) {
  263. write_rate_desc_array_in_buffer(msg->out + sizeof(p2a),
  264. triplet, 3U);
  265. p2a.num_rates_flags = SCMI_RATES_BY_STEP;
  266. p2a.status = SCMI_SUCCESS;
  267. memcpy(msg->out, &p2a, sizeof(p2a));
  268. msg->out_size_out = sizeof(p2a) + (3U * RATE_DESC_SIZE);
  269. }
  270. } else {
  271. /* Fallthrough generic exit sequence below with error status */
  272. }
  273. if (status != SCMI_SUCCESS) {
  274. scmi_status_response(msg, status);
  275. } else {
  276. /*
  277. * Message payload is already written to msg->out, and
  278. * msg->out_size_out updated.
  279. */
  280. }
  281. }
  282. static const scmi_msg_handler_t scmi_clock_handler_table[] = {
  283. [SCMI_PROTOCOL_VERSION] = report_version,
  284. [SCMI_PROTOCOL_ATTRIBUTES] = report_attributes,
  285. [SCMI_PROTOCOL_MESSAGE_ATTRIBUTES] = report_message_attributes,
  286. [SCMI_CLOCK_ATTRIBUTES] = scmi_clock_attributes,
  287. [SCMI_CLOCK_DESCRIBE_RATES] = scmi_clock_describe_rates,
  288. [SCMI_CLOCK_RATE_SET] = scmi_clock_rate_set,
  289. [SCMI_CLOCK_RATE_GET] = scmi_clock_rate_get,
  290. [SCMI_CLOCK_CONFIG_SET] = scmi_clock_config_set,
  291. };
  292. static bool message_id_is_supported(unsigned int message_id)
  293. {
  294. return (message_id < ARRAY_SIZE(scmi_clock_handler_table)) &&
  295. (scmi_clock_handler_table[message_id] != NULL);
  296. }
  297. scmi_msg_handler_t scmi_msg_get_clock_handler(struct scmi_msg *msg)
  298. {
  299. const size_t array_size = ARRAY_SIZE(scmi_clock_handler_table);
  300. unsigned int message_id = SPECULATION_SAFE_VALUE(msg->message_id);
  301. if (message_id >= array_size) {
  302. VERBOSE("Clock handle not found %u", msg->message_id);
  303. return NULL;
  304. }
  305. return scmi_clock_handler_table[message_id];
  306. }