base.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * System Control and Management Interface (SCMI) Base Protocol
  4. *
  5. * Copyright (C) 2018-2021 ARM Ltd.
  6. */
  7. #define pr_fmt(fmt) "SCMI Notifications BASE - " fmt
  8. #include <linux/module.h>
  9. #include <linux/scmi_protocol.h>
  10. #include "common.h"
  11. #include "notify.h"
  12. /* Updated only after ALL the mandatory features for that version are merged */
  13. #define SCMI_PROTOCOL_SUPPORTED_VERSION 0x20001
  14. #define SCMI_BASE_NUM_SOURCES 1
  15. #define SCMI_BASE_MAX_CMD_ERR_COUNT 1024
  16. enum scmi_base_protocol_cmd {
  17. BASE_DISCOVER_VENDOR = 0x3,
  18. BASE_DISCOVER_SUB_VENDOR = 0x4,
  19. BASE_DISCOVER_IMPLEMENT_VERSION = 0x5,
  20. BASE_DISCOVER_LIST_PROTOCOLS = 0x6,
  21. BASE_DISCOVER_AGENT = 0x7,
  22. BASE_NOTIFY_ERRORS = 0x8,
  23. BASE_SET_DEVICE_PERMISSIONS = 0x9,
  24. BASE_SET_PROTOCOL_PERMISSIONS = 0xa,
  25. BASE_RESET_AGENT_CONFIGURATION = 0xb,
  26. };
  27. struct scmi_msg_resp_base_attributes {
  28. u8 num_protocols;
  29. u8 num_agents;
  30. __le16 reserved;
  31. };
  32. struct scmi_msg_resp_base_discover_agent {
  33. __le32 agent_id;
  34. u8 name[SCMI_SHORT_NAME_MAX_SIZE];
  35. };
  36. struct scmi_msg_base_error_notify {
  37. __le32 event_control;
  38. #define BASE_TP_NOTIFY_ALL BIT(0)
  39. };
  40. struct scmi_base_error_notify_payld {
  41. __le32 agent_id;
  42. __le32 error_status;
  43. #define IS_FATAL_ERROR(x) ((x) & BIT(31))
  44. #define ERROR_CMD_COUNT(x) FIELD_GET(GENMASK(9, 0), (x))
  45. __le64 msg_reports[SCMI_BASE_MAX_CMD_ERR_COUNT];
  46. };
  47. /**
  48. * scmi_base_attributes_get() - gets the implementation details
  49. * that are associated with the base protocol.
  50. *
  51. * @ph: SCMI protocol handle
  52. *
  53. * Return: 0 on success, else appropriate SCMI error.
  54. */
  55. static int scmi_base_attributes_get(const struct scmi_protocol_handle *ph)
  56. {
  57. int ret;
  58. struct scmi_xfer *t;
  59. struct scmi_msg_resp_base_attributes *attr_info;
  60. struct scmi_revision_info *rev = ph->get_priv(ph);
  61. ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES,
  62. 0, sizeof(*attr_info), &t);
  63. if (ret)
  64. return ret;
  65. ret = ph->xops->do_xfer(ph, t);
  66. if (!ret) {
  67. attr_info = t->rx.buf;
  68. rev->num_protocols = attr_info->num_protocols;
  69. rev->num_agents = attr_info->num_agents;
  70. }
  71. ph->xops->xfer_put(ph, t);
  72. return ret;
  73. }
  74. /**
  75. * scmi_base_vendor_id_get() - gets vendor/subvendor identifier ASCII string.
  76. *
  77. * @ph: SCMI protocol handle
  78. * @sub_vendor: specify true if sub-vendor ID is needed
  79. *
  80. * Return: 0 on success, else appropriate SCMI error.
  81. */
  82. static int
  83. scmi_base_vendor_id_get(const struct scmi_protocol_handle *ph, bool sub_vendor)
  84. {
  85. u8 cmd;
  86. int ret, size;
  87. char *vendor_id;
  88. struct scmi_xfer *t;
  89. struct scmi_revision_info *rev = ph->get_priv(ph);
  90. if (sub_vendor) {
  91. cmd = BASE_DISCOVER_SUB_VENDOR;
  92. vendor_id = rev->sub_vendor_id;
  93. size = ARRAY_SIZE(rev->sub_vendor_id);
  94. } else {
  95. cmd = BASE_DISCOVER_VENDOR;
  96. vendor_id = rev->vendor_id;
  97. size = ARRAY_SIZE(rev->vendor_id);
  98. }
  99. ret = ph->xops->xfer_get_init(ph, cmd, 0, size, &t);
  100. if (ret)
  101. return ret;
  102. ret = ph->xops->do_xfer(ph, t);
  103. if (!ret)
  104. strscpy(vendor_id, t->rx.buf, size);
  105. ph->xops->xfer_put(ph, t);
  106. return ret;
  107. }
  108. /**
  109. * scmi_base_implementation_version_get() - gets a vendor-specific
  110. * implementation 32-bit version. The format of the version number is
  111. * vendor-specific
  112. *
  113. * @ph: SCMI protocol handle
  114. *
  115. * Return: 0 on success, else appropriate SCMI error.
  116. */
  117. static int
  118. scmi_base_implementation_version_get(const struct scmi_protocol_handle *ph)
  119. {
  120. int ret;
  121. __le32 *impl_ver;
  122. struct scmi_xfer *t;
  123. struct scmi_revision_info *rev = ph->get_priv(ph);
  124. ret = ph->xops->xfer_get_init(ph, BASE_DISCOVER_IMPLEMENT_VERSION,
  125. 0, sizeof(*impl_ver), &t);
  126. if (ret)
  127. return ret;
  128. ret = ph->xops->do_xfer(ph, t);
  129. if (!ret) {
  130. impl_ver = t->rx.buf;
  131. rev->impl_ver = le32_to_cpu(*impl_ver);
  132. }
  133. ph->xops->xfer_put(ph, t);
  134. return ret;
  135. }
  136. /**
  137. * scmi_base_implementation_list_get() - gets the list of protocols it is
  138. * OSPM is allowed to access
  139. *
  140. * @ph: SCMI protocol handle
  141. * @protocols_imp: pointer to hold the list of protocol identifiers
  142. *
  143. * Return: 0 on success, else appropriate SCMI error.
  144. */
  145. static int
  146. scmi_base_implementation_list_get(const struct scmi_protocol_handle *ph,
  147. u8 *protocols_imp)
  148. {
  149. u8 *list;
  150. int ret, loop;
  151. struct scmi_xfer *t;
  152. __le32 *num_skip, *num_ret;
  153. u32 tot_num_ret = 0, loop_num_ret;
  154. struct device *dev = ph->dev;
  155. struct scmi_revision_info *rev = ph->get_priv(ph);
  156. ret = ph->xops->xfer_get_init(ph, BASE_DISCOVER_LIST_PROTOCOLS,
  157. sizeof(*num_skip), 0, &t);
  158. if (ret)
  159. return ret;
  160. num_skip = t->tx.buf;
  161. num_ret = t->rx.buf;
  162. list = t->rx.buf + sizeof(*num_ret);
  163. do {
  164. size_t real_list_sz;
  165. u32 calc_list_sz;
  166. /* Set the number of protocols to be skipped/already read */
  167. *num_skip = cpu_to_le32(tot_num_ret);
  168. ret = ph->xops->do_xfer(ph, t);
  169. if (ret)
  170. break;
  171. loop_num_ret = le32_to_cpu(*num_ret);
  172. if (!loop_num_ret)
  173. break;
  174. if (loop_num_ret > rev->num_protocols - tot_num_ret) {
  175. dev_err(dev,
  176. "No. Returned protocols > Total protocols.\n");
  177. break;
  178. }
  179. if (t->rx.len < (sizeof(u32) * 2)) {
  180. dev_err(dev, "Truncated reply - rx.len:%zd\n",
  181. t->rx.len);
  182. ret = -EPROTO;
  183. break;
  184. }
  185. real_list_sz = t->rx.len - sizeof(u32);
  186. calc_list_sz = (1 + (loop_num_ret - 1) / sizeof(u32)) *
  187. sizeof(u32);
  188. if (calc_list_sz != real_list_sz) {
  189. dev_warn(dev,
  190. "Malformed reply - real_sz:%zd calc_sz:%u (loop_num_ret:%d)\n",
  191. real_list_sz, calc_list_sz, loop_num_ret);
  192. /*
  193. * Bail out if the expected list size is bigger than the
  194. * total payload size of the received reply.
  195. */
  196. if (calc_list_sz > real_list_sz) {
  197. ret = -EPROTO;
  198. break;
  199. }
  200. }
  201. for (loop = 0; loop < loop_num_ret; loop++)
  202. protocols_imp[tot_num_ret + loop] = *(list + loop);
  203. tot_num_ret += loop_num_ret;
  204. ph->xops->reset_rx_to_maxsz(ph, t);
  205. } while (tot_num_ret < rev->num_protocols);
  206. ph->xops->xfer_put(ph, t);
  207. return ret;
  208. }
  209. /**
  210. * scmi_base_discover_agent_get() - discover the name of an agent
  211. *
  212. * @ph: SCMI protocol handle
  213. * @id: Agent identifier
  214. * @name: Agent identifier ASCII string
  215. *
  216. * An agent id of 0 is reserved to identify the platform itself.
  217. * Generally operating system is represented as "OSPM"
  218. *
  219. * Return: 0 on success, else appropriate SCMI error.
  220. */
  221. static int scmi_base_discover_agent_get(const struct scmi_protocol_handle *ph,
  222. int id, char *name)
  223. {
  224. int ret;
  225. struct scmi_msg_resp_base_discover_agent *agent_info;
  226. struct scmi_xfer *t;
  227. ret = ph->xops->xfer_get_init(ph, BASE_DISCOVER_AGENT,
  228. sizeof(__le32), sizeof(*agent_info), &t);
  229. if (ret)
  230. return ret;
  231. put_unaligned_le32(id, t->tx.buf);
  232. ret = ph->xops->do_xfer(ph, t);
  233. if (!ret) {
  234. agent_info = t->rx.buf;
  235. strscpy(name, agent_info->name, SCMI_SHORT_NAME_MAX_SIZE);
  236. }
  237. ph->xops->xfer_put(ph, t);
  238. return ret;
  239. }
  240. static int scmi_base_error_notify(const struct scmi_protocol_handle *ph,
  241. bool enable)
  242. {
  243. int ret;
  244. u32 evt_cntl = enable ? BASE_TP_NOTIFY_ALL : 0;
  245. struct scmi_xfer *t;
  246. struct scmi_msg_base_error_notify *cfg;
  247. ret = ph->xops->xfer_get_init(ph, BASE_NOTIFY_ERRORS,
  248. sizeof(*cfg), 0, &t);
  249. if (ret)
  250. return ret;
  251. cfg = t->tx.buf;
  252. cfg->event_control = cpu_to_le32(evt_cntl);
  253. ret = ph->xops->do_xfer(ph, t);
  254. ph->xops->xfer_put(ph, t);
  255. return ret;
  256. }
  257. static int scmi_base_set_notify_enabled(const struct scmi_protocol_handle *ph,
  258. u8 evt_id, u32 src_id, bool enable)
  259. {
  260. int ret;
  261. ret = scmi_base_error_notify(ph, enable);
  262. if (ret)
  263. pr_debug("FAIL_ENABLED - evt[%X] ret:%d\n", evt_id, ret);
  264. return ret;
  265. }
  266. static void *scmi_base_fill_custom_report(const struct scmi_protocol_handle *ph,
  267. u8 evt_id, ktime_t timestamp,
  268. const void *payld, size_t payld_sz,
  269. void *report, u32 *src_id)
  270. {
  271. int i;
  272. const struct scmi_base_error_notify_payld *p = payld;
  273. struct scmi_base_error_report *r = report;
  274. /*
  275. * BaseError notification payload is variable in size but
  276. * up to a maximum length determined by the struct ponted by p.
  277. * Instead payld_sz is the effective length of this notification
  278. * payload so cannot be greater of the maximum allowed size as
  279. * pointed by p.
  280. */
  281. if (evt_id != SCMI_EVENT_BASE_ERROR_EVENT || sizeof(*p) < payld_sz)
  282. return NULL;
  283. r->timestamp = timestamp;
  284. r->agent_id = le32_to_cpu(p->agent_id);
  285. r->fatal = IS_FATAL_ERROR(le32_to_cpu(p->error_status));
  286. r->cmd_count = ERROR_CMD_COUNT(le32_to_cpu(p->error_status));
  287. for (i = 0; i < r->cmd_count; i++)
  288. r->reports[i] = le64_to_cpu(p->msg_reports[i]);
  289. *src_id = 0;
  290. return r;
  291. }
  292. static const struct scmi_event base_events[] = {
  293. {
  294. .id = SCMI_EVENT_BASE_ERROR_EVENT,
  295. .max_payld_sz = sizeof(struct scmi_base_error_notify_payld),
  296. .max_report_sz = sizeof(struct scmi_base_error_report) +
  297. SCMI_BASE_MAX_CMD_ERR_COUNT * sizeof(u64),
  298. },
  299. };
  300. static const struct scmi_event_ops base_event_ops = {
  301. .set_notify_enabled = scmi_base_set_notify_enabled,
  302. .fill_custom_report = scmi_base_fill_custom_report,
  303. };
  304. static const struct scmi_protocol_events base_protocol_events = {
  305. .queue_sz = 4 * SCMI_PROTO_QUEUE_SZ,
  306. .ops = &base_event_ops,
  307. .evts = base_events,
  308. .num_events = ARRAY_SIZE(base_events),
  309. .num_sources = SCMI_BASE_NUM_SOURCES,
  310. };
  311. static int scmi_base_protocol_init(const struct scmi_protocol_handle *ph)
  312. {
  313. int id, ret;
  314. u8 *prot_imp;
  315. u32 version;
  316. char name[SCMI_SHORT_NAME_MAX_SIZE];
  317. struct device *dev = ph->dev;
  318. struct scmi_revision_info *rev = scmi_revision_area_get(ph);
  319. ret = ph->xops->version_get(ph, &version);
  320. if (ret)
  321. return ret;
  322. rev->major_ver = PROTOCOL_REV_MAJOR(version);
  323. rev->minor_ver = PROTOCOL_REV_MINOR(version);
  324. ph->set_priv(ph, rev, version);
  325. ret = scmi_base_attributes_get(ph);
  326. if (ret)
  327. return ret;
  328. prot_imp = devm_kcalloc(dev, rev->num_protocols, sizeof(u8),
  329. GFP_KERNEL);
  330. if (!prot_imp)
  331. return -ENOMEM;
  332. scmi_base_vendor_id_get(ph, false);
  333. scmi_base_vendor_id_get(ph, true);
  334. scmi_base_implementation_version_get(ph);
  335. scmi_base_implementation_list_get(ph, prot_imp);
  336. scmi_setup_protocol_implemented(ph, prot_imp);
  337. dev_info(dev, "SCMI Protocol v%d.%d '%s:%s' Firmware version 0x%x\n",
  338. rev->major_ver, rev->minor_ver, rev->vendor_id,
  339. rev->sub_vendor_id, rev->impl_ver);
  340. dev_dbg(dev, "Found %d protocol(s) %d agent(s)\n", rev->num_protocols,
  341. rev->num_agents);
  342. for (id = 0; id < rev->num_agents; id++) {
  343. scmi_base_discover_agent_get(ph, id, name);
  344. dev_dbg(dev, "Agent %d: %s\n", id, name);
  345. }
  346. return 0;
  347. }
  348. static const struct scmi_protocol scmi_base = {
  349. .id = SCMI_PROTOCOL_BASE,
  350. .owner = NULL,
  351. .instance_init = &scmi_base_protocol_init,
  352. .ops = NULL,
  353. .events = &base_protocol_events,
  354. .supported_version = SCMI_PROTOCOL_SUPPORTED_VERSION,
  355. };
  356. DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(base, scmi_base)