base.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * System Control and Management Interface (SCMI) Base Protocol
  4. *
  5. * Copyright (C) 2018 ARM Ltd.
  6. */
  7. #include "common.h"
  8. enum scmi_base_protocol_cmd {
  9. BASE_DISCOVER_VENDOR = 0x3,
  10. BASE_DISCOVER_SUB_VENDOR = 0x4,
  11. BASE_DISCOVER_IMPLEMENT_VERSION = 0x5,
  12. BASE_DISCOVER_LIST_PROTOCOLS = 0x6,
  13. BASE_DISCOVER_AGENT = 0x7,
  14. BASE_NOTIFY_ERRORS = 0x8,
  15. };
  16. struct scmi_msg_resp_base_attributes {
  17. u8 num_protocols;
  18. u8 num_agents;
  19. __le16 reserved;
  20. };
  21. /**
  22. * scmi_base_attributes_get() - gets the implementation details
  23. * that are associated with the base protocol.
  24. *
  25. * @handle: SCMI entity handle
  26. *
  27. * Return: 0 on success, else appropriate SCMI error.
  28. */
  29. static int scmi_base_attributes_get(const struct scmi_handle *handle)
  30. {
  31. int ret;
  32. struct scmi_xfer *t;
  33. struct scmi_msg_resp_base_attributes *attr_info;
  34. struct scmi_revision_info *rev = handle->version;
  35. ret = scmi_xfer_get_init(handle, PROTOCOL_ATTRIBUTES,
  36. SCMI_PROTOCOL_BASE, 0, sizeof(*attr_info), &t);
  37. if (ret)
  38. return ret;
  39. ret = scmi_do_xfer(handle, t);
  40. if (!ret) {
  41. attr_info = t->rx.buf;
  42. rev->num_protocols = attr_info->num_protocols;
  43. rev->num_agents = attr_info->num_agents;
  44. }
  45. scmi_xfer_put(handle, t);
  46. return ret;
  47. }
  48. /**
  49. * scmi_base_vendor_id_get() - gets vendor/subvendor identifier ASCII string.
  50. *
  51. * @handle: SCMI entity handle
  52. * @sub_vendor: specify true if sub-vendor ID is needed
  53. *
  54. * Return: 0 on success, else appropriate SCMI error.
  55. */
  56. static int
  57. scmi_base_vendor_id_get(const struct scmi_handle *handle, bool sub_vendor)
  58. {
  59. u8 cmd;
  60. int ret, size;
  61. char *vendor_id;
  62. struct scmi_xfer *t;
  63. struct scmi_revision_info *rev = handle->version;
  64. if (sub_vendor) {
  65. cmd = BASE_DISCOVER_SUB_VENDOR;
  66. vendor_id = rev->sub_vendor_id;
  67. size = ARRAY_SIZE(rev->sub_vendor_id);
  68. } else {
  69. cmd = BASE_DISCOVER_VENDOR;
  70. vendor_id = rev->vendor_id;
  71. size = ARRAY_SIZE(rev->vendor_id);
  72. }
  73. ret = scmi_xfer_get_init(handle, cmd, SCMI_PROTOCOL_BASE, 0, size, &t);
  74. if (ret)
  75. return ret;
  76. ret = scmi_do_xfer(handle, t);
  77. if (!ret)
  78. memcpy(vendor_id, t->rx.buf, size);
  79. scmi_xfer_put(handle, t);
  80. return ret;
  81. }
  82. /**
  83. * scmi_base_implementation_version_get() - gets a vendor-specific
  84. * implementation 32-bit version. The format of the version number is
  85. * vendor-specific
  86. *
  87. * @handle: SCMI entity handle
  88. *
  89. * Return: 0 on success, else appropriate SCMI error.
  90. */
  91. static int
  92. scmi_base_implementation_version_get(const struct scmi_handle *handle)
  93. {
  94. int ret;
  95. __le32 *impl_ver;
  96. struct scmi_xfer *t;
  97. struct scmi_revision_info *rev = handle->version;
  98. ret = scmi_xfer_get_init(handle, BASE_DISCOVER_IMPLEMENT_VERSION,
  99. SCMI_PROTOCOL_BASE, 0, sizeof(*impl_ver), &t);
  100. if (ret)
  101. return ret;
  102. ret = scmi_do_xfer(handle, t);
  103. if (!ret) {
  104. impl_ver = t->rx.buf;
  105. rev->impl_ver = le32_to_cpu(*impl_ver);
  106. }
  107. scmi_xfer_put(handle, t);
  108. return ret;
  109. }
  110. /**
  111. * scmi_base_implementation_list_get() - gets the list of protocols it is
  112. * OSPM is allowed to access
  113. *
  114. * @handle: SCMI entity handle
  115. * @protocols_imp: pointer to hold the list of protocol identifiers
  116. *
  117. * Return: 0 on success, else appropriate SCMI error.
  118. */
  119. static int scmi_base_implementation_list_get(const struct scmi_handle *handle,
  120. u8 *protocols_imp)
  121. {
  122. u8 *list;
  123. int ret, loop;
  124. struct scmi_xfer *t;
  125. __le32 *num_skip, *num_ret;
  126. u32 tot_num_ret = 0, loop_num_ret;
  127. struct device *dev = handle->dev;
  128. ret = scmi_xfer_get_init(handle, BASE_DISCOVER_LIST_PROTOCOLS,
  129. SCMI_PROTOCOL_BASE, sizeof(*num_skip), 0, &t);
  130. if (ret)
  131. return ret;
  132. num_skip = t->tx.buf;
  133. num_ret = t->rx.buf;
  134. list = t->rx.buf + sizeof(*num_ret);
  135. do {
  136. /* Set the number of protocols to be skipped/already read */
  137. *num_skip = cpu_to_le32(tot_num_ret);
  138. ret = scmi_do_xfer(handle, t);
  139. if (ret)
  140. break;
  141. loop_num_ret = le32_to_cpu(*num_ret);
  142. if (tot_num_ret + loop_num_ret > MAX_PROTOCOLS_IMP) {
  143. dev_err(dev, "No. of Protocol > MAX_PROTOCOLS_IMP");
  144. break;
  145. }
  146. for (loop = 0; loop < loop_num_ret; loop++)
  147. protocols_imp[tot_num_ret + loop] = *(list + loop);
  148. tot_num_ret += loop_num_ret;
  149. } while (loop_num_ret);
  150. scmi_xfer_put(handle, t);
  151. return ret;
  152. }
  153. /**
  154. * scmi_base_discover_agent_get() - discover the name of an agent
  155. *
  156. * @handle: SCMI entity handle
  157. * @id: Agent identifier
  158. * @name: Agent identifier ASCII string
  159. *
  160. * An agent id of 0 is reserved to identify the platform itself.
  161. * Generally operating system is represented as "OSPM"
  162. *
  163. * Return: 0 on success, else appropriate SCMI error.
  164. */
  165. static int scmi_base_discover_agent_get(const struct scmi_handle *handle,
  166. int id, char *name)
  167. {
  168. int ret;
  169. struct scmi_xfer *t;
  170. ret = scmi_xfer_get_init(handle, BASE_DISCOVER_AGENT,
  171. SCMI_PROTOCOL_BASE, sizeof(__le32),
  172. SCMI_MAX_STR_SIZE, &t);
  173. if (ret)
  174. return ret;
  175. *(__le32 *)t->tx.buf = cpu_to_le32(id);
  176. ret = scmi_do_xfer(handle, t);
  177. if (!ret)
  178. strlcpy(name, t->rx.buf, SCMI_MAX_STR_SIZE);
  179. scmi_xfer_put(handle, t);
  180. return ret;
  181. }
  182. int scmi_base_protocol_init(struct scmi_handle *h)
  183. {
  184. int id, ret;
  185. u8 *prot_imp;
  186. u32 version;
  187. char name[SCMI_MAX_STR_SIZE];
  188. const struct scmi_handle *handle = h;
  189. struct device *dev = handle->dev;
  190. struct scmi_revision_info *rev = handle->version;
  191. ret = scmi_version_get(handle, SCMI_PROTOCOL_BASE, &version);
  192. if (ret)
  193. return ret;
  194. prot_imp = devm_kcalloc(dev, MAX_PROTOCOLS_IMP, sizeof(u8), GFP_KERNEL);
  195. if (!prot_imp)
  196. return -ENOMEM;
  197. rev->major_ver = PROTOCOL_REV_MAJOR(version),
  198. rev->minor_ver = PROTOCOL_REV_MINOR(version);
  199. scmi_base_attributes_get(handle);
  200. scmi_base_vendor_id_get(handle, false);
  201. scmi_base_vendor_id_get(handle, true);
  202. scmi_base_implementation_version_get(handle);
  203. scmi_base_implementation_list_get(handle, prot_imp);
  204. scmi_setup_protocol_implemented(handle, prot_imp);
  205. dev_info(dev, "SCMI Protocol v%d.%d '%s:%s' Firmware version 0x%x\n",
  206. rev->major_ver, rev->minor_ver, rev->vendor_id,
  207. rev->sub_vendor_id, rev->impl_ver);
  208. dev_dbg(dev, "Found %d protocol(s) %d agent(s)\n", rev->num_protocols,
  209. rev->num_agents);
  210. for (id = 0; id < rev->num_agents; id++) {
  211. scmi_base_discover_agent_get(handle, id, name);
  212. dev_dbg(dev, "Agent %d: %s\n", id, name);
  213. }
  214. return 0;
  215. }