power.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * System Control and Management Interface (SCMI) Power Protocol
  4. *
  5. * Copyright (C) 2018-2022 ARM Ltd.
  6. */
  7. #define pr_fmt(fmt) "SCMI Notifications POWER - " fmt
  8. #include <linux/module.h>
  9. #include <linux/scmi_protocol.h>
  10. #include "protocols.h"
  11. #include "notify.h"
  12. enum scmi_power_protocol_cmd {
  13. POWER_DOMAIN_ATTRIBUTES = 0x3,
  14. POWER_STATE_SET = 0x4,
  15. POWER_STATE_GET = 0x5,
  16. POWER_STATE_NOTIFY = 0x6,
  17. POWER_DOMAIN_NAME_GET = 0x8,
  18. };
  19. struct scmi_msg_resp_power_attributes {
  20. __le16 num_domains;
  21. __le16 reserved;
  22. __le32 stats_addr_low;
  23. __le32 stats_addr_high;
  24. __le32 stats_size;
  25. };
  26. struct scmi_msg_resp_power_domain_attributes {
  27. __le32 flags;
  28. #define SUPPORTS_STATE_SET_NOTIFY(x) ((x) & BIT(31))
  29. #define SUPPORTS_STATE_SET_ASYNC(x) ((x) & BIT(30))
  30. #define SUPPORTS_STATE_SET_SYNC(x) ((x) & BIT(29))
  31. #define SUPPORTS_EXTENDED_NAMES(x) ((x) & BIT(27))
  32. u8 name[SCMI_SHORT_NAME_MAX_SIZE];
  33. };
  34. struct scmi_power_set_state {
  35. __le32 flags;
  36. #define STATE_SET_ASYNC BIT(0)
  37. __le32 domain;
  38. __le32 state;
  39. };
  40. struct scmi_power_state_notify {
  41. __le32 domain;
  42. __le32 notify_enable;
  43. };
  44. struct scmi_power_state_notify_payld {
  45. __le32 agent_id;
  46. __le32 domain_id;
  47. __le32 power_state;
  48. };
  49. struct power_dom_info {
  50. bool state_set_sync;
  51. bool state_set_async;
  52. bool state_set_notify;
  53. char name[SCMI_MAX_STR_SIZE];
  54. };
  55. struct scmi_power_info {
  56. u32 version;
  57. int num_domains;
  58. u64 stats_addr;
  59. u32 stats_size;
  60. struct power_dom_info *dom_info;
  61. };
  62. static int scmi_power_attributes_get(const struct scmi_protocol_handle *ph,
  63. struct scmi_power_info *pi)
  64. {
  65. int ret;
  66. struct scmi_xfer *t;
  67. struct scmi_msg_resp_power_attributes *attr;
  68. ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES,
  69. 0, sizeof(*attr), &t);
  70. if (ret)
  71. return ret;
  72. attr = t->rx.buf;
  73. ret = ph->xops->do_xfer(ph, t);
  74. if (!ret) {
  75. pi->num_domains = le16_to_cpu(attr->num_domains);
  76. pi->stats_addr = le32_to_cpu(attr->stats_addr_low) |
  77. (u64)le32_to_cpu(attr->stats_addr_high) << 32;
  78. pi->stats_size = le32_to_cpu(attr->stats_size);
  79. }
  80. ph->xops->xfer_put(ph, t);
  81. return ret;
  82. }
  83. static int
  84. scmi_power_domain_attributes_get(const struct scmi_protocol_handle *ph,
  85. u32 domain, struct power_dom_info *dom_info,
  86. u32 version)
  87. {
  88. int ret;
  89. u32 flags;
  90. struct scmi_xfer *t;
  91. struct scmi_msg_resp_power_domain_attributes *attr;
  92. ret = ph->xops->xfer_get_init(ph, POWER_DOMAIN_ATTRIBUTES,
  93. sizeof(domain), sizeof(*attr), &t);
  94. if (ret)
  95. return ret;
  96. put_unaligned_le32(domain, t->tx.buf);
  97. attr = t->rx.buf;
  98. ret = ph->xops->do_xfer(ph, t);
  99. if (!ret) {
  100. flags = le32_to_cpu(attr->flags);
  101. dom_info->state_set_notify = SUPPORTS_STATE_SET_NOTIFY(flags);
  102. dom_info->state_set_async = SUPPORTS_STATE_SET_ASYNC(flags);
  103. dom_info->state_set_sync = SUPPORTS_STATE_SET_SYNC(flags);
  104. strscpy(dom_info->name, attr->name, SCMI_SHORT_NAME_MAX_SIZE);
  105. }
  106. ph->xops->xfer_put(ph, t);
  107. /*
  108. * If supported overwrite short name with the extended one;
  109. * on error just carry on and use already provided short name.
  110. */
  111. if (!ret && PROTOCOL_REV_MAJOR(version) >= 0x3 &&
  112. SUPPORTS_EXTENDED_NAMES(flags)) {
  113. ph->hops->extended_name_get(ph, POWER_DOMAIN_NAME_GET,
  114. domain, dom_info->name,
  115. SCMI_MAX_STR_SIZE);
  116. }
  117. return ret;
  118. }
  119. static int scmi_power_state_set(const struct scmi_protocol_handle *ph,
  120. u32 domain, u32 state)
  121. {
  122. int ret;
  123. struct scmi_xfer *t;
  124. struct scmi_power_set_state *st;
  125. ret = ph->xops->xfer_get_init(ph, POWER_STATE_SET, sizeof(*st), 0, &t);
  126. if (ret)
  127. return ret;
  128. st = t->tx.buf;
  129. st->flags = cpu_to_le32(0);
  130. st->domain = cpu_to_le32(domain);
  131. st->state = cpu_to_le32(state);
  132. ret = ph->xops->do_xfer(ph, t);
  133. ph->xops->xfer_put(ph, t);
  134. return ret;
  135. }
  136. static int scmi_power_state_get(const struct scmi_protocol_handle *ph,
  137. u32 domain, u32 *state)
  138. {
  139. int ret;
  140. struct scmi_xfer *t;
  141. ret = ph->xops->xfer_get_init(ph, POWER_STATE_GET, sizeof(u32), sizeof(u32), &t);
  142. if (ret)
  143. return ret;
  144. put_unaligned_le32(domain, t->tx.buf);
  145. ret = ph->xops->do_xfer(ph, t);
  146. if (!ret)
  147. *state = get_unaligned_le32(t->rx.buf);
  148. ph->xops->xfer_put(ph, t);
  149. return ret;
  150. }
  151. static int scmi_power_num_domains_get(const struct scmi_protocol_handle *ph)
  152. {
  153. struct scmi_power_info *pi = ph->get_priv(ph);
  154. return pi->num_domains;
  155. }
  156. static const char *
  157. scmi_power_name_get(const struct scmi_protocol_handle *ph,
  158. u32 domain)
  159. {
  160. struct scmi_power_info *pi = ph->get_priv(ph);
  161. struct power_dom_info *dom = pi->dom_info + domain;
  162. return dom->name;
  163. }
  164. static const struct scmi_power_proto_ops power_proto_ops = {
  165. .num_domains_get = scmi_power_num_domains_get,
  166. .name_get = scmi_power_name_get,
  167. .state_set = scmi_power_state_set,
  168. .state_get = scmi_power_state_get,
  169. };
  170. static int scmi_power_request_notify(const struct scmi_protocol_handle *ph,
  171. u32 domain, bool enable)
  172. {
  173. int ret;
  174. struct scmi_xfer *t;
  175. struct scmi_power_state_notify *notify;
  176. ret = ph->xops->xfer_get_init(ph, POWER_STATE_NOTIFY,
  177. sizeof(*notify), 0, &t);
  178. if (ret)
  179. return ret;
  180. notify = t->tx.buf;
  181. notify->domain = cpu_to_le32(domain);
  182. notify->notify_enable = enable ? cpu_to_le32(BIT(0)) : 0;
  183. ret = ph->xops->do_xfer(ph, t);
  184. ph->xops->xfer_put(ph, t);
  185. return ret;
  186. }
  187. static int scmi_power_set_notify_enabled(const struct scmi_protocol_handle *ph,
  188. u8 evt_id, u32 src_id, bool enable)
  189. {
  190. int ret;
  191. ret = scmi_power_request_notify(ph, src_id, enable);
  192. if (ret)
  193. pr_debug("FAIL_ENABLE - evt[%X] dom[%d] - ret:%d\n",
  194. evt_id, src_id, ret);
  195. return ret;
  196. }
  197. static void *
  198. scmi_power_fill_custom_report(const struct scmi_protocol_handle *ph,
  199. u8 evt_id, ktime_t timestamp,
  200. const void *payld, size_t payld_sz,
  201. void *report, u32 *src_id)
  202. {
  203. const struct scmi_power_state_notify_payld *p = payld;
  204. struct scmi_power_state_changed_report *r = report;
  205. if (evt_id != SCMI_EVENT_POWER_STATE_CHANGED || sizeof(*p) != payld_sz)
  206. return NULL;
  207. r->timestamp = timestamp;
  208. r->agent_id = le32_to_cpu(p->agent_id);
  209. r->domain_id = le32_to_cpu(p->domain_id);
  210. r->power_state = le32_to_cpu(p->power_state);
  211. *src_id = r->domain_id;
  212. return r;
  213. }
  214. static int scmi_power_get_num_sources(const struct scmi_protocol_handle *ph)
  215. {
  216. struct scmi_power_info *pinfo = ph->get_priv(ph);
  217. if (!pinfo)
  218. return -EINVAL;
  219. return pinfo->num_domains;
  220. }
  221. static const struct scmi_event power_events[] = {
  222. {
  223. .id = SCMI_EVENT_POWER_STATE_CHANGED,
  224. .max_payld_sz = sizeof(struct scmi_power_state_notify_payld),
  225. .max_report_sz =
  226. sizeof(struct scmi_power_state_changed_report),
  227. },
  228. };
  229. static const struct scmi_event_ops power_event_ops = {
  230. .get_num_sources = scmi_power_get_num_sources,
  231. .set_notify_enabled = scmi_power_set_notify_enabled,
  232. .fill_custom_report = scmi_power_fill_custom_report,
  233. };
  234. static const struct scmi_protocol_events power_protocol_events = {
  235. .queue_sz = SCMI_PROTO_QUEUE_SZ,
  236. .ops = &power_event_ops,
  237. .evts = power_events,
  238. .num_events = ARRAY_SIZE(power_events),
  239. };
  240. static int scmi_power_protocol_init(const struct scmi_protocol_handle *ph)
  241. {
  242. int domain, ret;
  243. u32 version;
  244. struct scmi_power_info *pinfo;
  245. ret = ph->xops->version_get(ph, &version);
  246. if (ret)
  247. return ret;
  248. dev_dbg(ph->dev, "Power Version %d.%d\n",
  249. PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));
  250. pinfo = devm_kzalloc(ph->dev, sizeof(*pinfo), GFP_KERNEL);
  251. if (!pinfo)
  252. return -ENOMEM;
  253. ret = scmi_power_attributes_get(ph, pinfo);
  254. if (ret)
  255. return ret;
  256. pinfo->dom_info = devm_kcalloc(ph->dev, pinfo->num_domains,
  257. sizeof(*pinfo->dom_info), GFP_KERNEL);
  258. if (!pinfo->dom_info)
  259. return -ENOMEM;
  260. for (domain = 0; domain < pinfo->num_domains; domain++) {
  261. struct power_dom_info *dom = pinfo->dom_info + domain;
  262. scmi_power_domain_attributes_get(ph, domain, dom, version);
  263. }
  264. pinfo->version = version;
  265. return ph->set_priv(ph, pinfo);
  266. }
  267. static const struct scmi_protocol scmi_power = {
  268. .id = SCMI_PROTOCOL_POWER,
  269. .owner = THIS_MODULE,
  270. .instance_init = &scmi_power_protocol_init,
  271. .ops = &power_proto_ops,
  272. .events = &power_protocol_events,
  273. };
  274. DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(power, scmi_power)