clock.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * System Control and Management Interface (SCMI) Clock Protocol
  4. *
  5. * Copyright (C) 2018-2022 ARM Ltd.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/limits.h>
  9. #include <linux/sort.h>
  10. #include "protocols.h"
  11. #include "notify.h"
  12. /* Updated only after ALL the mandatory features for that version are merged */
  13. #define SCMI_PROTOCOL_SUPPORTED_VERSION 0x30000
  14. enum scmi_clock_protocol_cmd {
  15. CLOCK_ATTRIBUTES = 0x3,
  16. CLOCK_DESCRIBE_RATES = 0x4,
  17. CLOCK_RATE_SET = 0x5,
  18. CLOCK_RATE_GET = 0x6,
  19. CLOCK_CONFIG_SET = 0x7,
  20. CLOCK_NAME_GET = 0x8,
  21. CLOCK_RATE_NOTIFY = 0x9,
  22. CLOCK_RATE_CHANGE_REQUESTED_NOTIFY = 0xA,
  23. CLOCK_CONFIG_GET = 0xB,
  24. CLOCK_POSSIBLE_PARENTS_GET = 0xC,
  25. CLOCK_PARENT_SET = 0xD,
  26. CLOCK_PARENT_GET = 0xE,
  27. CLOCK_GET_PERMISSIONS = 0xF,
  28. };
  29. #define CLOCK_STATE_CONTROL_ALLOWED BIT(31)
  30. #define CLOCK_PARENT_CONTROL_ALLOWED BIT(30)
  31. #define CLOCK_RATE_CONTROL_ALLOWED BIT(29)
  32. enum clk_state {
  33. CLK_STATE_DISABLE,
  34. CLK_STATE_ENABLE,
  35. CLK_STATE_RESERVED,
  36. CLK_STATE_UNCHANGED,
  37. };
  38. struct scmi_msg_resp_clock_protocol_attributes {
  39. __le16 num_clocks;
  40. u8 max_async_req;
  41. u8 reserved;
  42. };
  43. struct scmi_msg_resp_clock_attributes {
  44. __le32 attributes;
  45. #define SUPPORTS_RATE_CHANGED_NOTIF(x) ((x) & BIT(31))
  46. #define SUPPORTS_RATE_CHANGE_REQUESTED_NOTIF(x) ((x) & BIT(30))
  47. #define SUPPORTS_EXTENDED_NAMES(x) ((x) & BIT(29))
  48. #define SUPPORTS_PARENT_CLOCK(x) ((x) & BIT(28))
  49. #define SUPPORTS_EXTENDED_CONFIG(x) ((x) & BIT(27))
  50. #define SUPPORTS_GET_PERMISSIONS(x) ((x) & BIT(1))
  51. u8 name[SCMI_SHORT_NAME_MAX_SIZE];
  52. __le32 clock_enable_latency;
  53. };
  54. struct scmi_msg_clock_possible_parents {
  55. __le32 id;
  56. __le32 skip_parents;
  57. };
  58. struct scmi_msg_resp_clock_possible_parents {
  59. __le32 num_parent_flags;
  60. #define NUM_PARENTS_RETURNED(x) ((x) & 0xff)
  61. #define NUM_PARENTS_REMAINING(x) ((x) >> 24)
  62. __le32 possible_parents[];
  63. };
  64. struct scmi_msg_clock_set_parent {
  65. __le32 id;
  66. __le32 parent_id;
  67. };
  68. struct scmi_msg_clock_config_set {
  69. __le32 id;
  70. __le32 attributes;
  71. };
  72. /* Valid only from SCMI clock v2.1 */
  73. struct scmi_msg_clock_config_set_v2 {
  74. __le32 id;
  75. __le32 attributes;
  76. #define NULL_OEM_TYPE 0
  77. #define REGMASK_OEM_TYPE_SET GENMASK(23, 16)
  78. #define REGMASK_CLK_STATE GENMASK(1, 0)
  79. __le32 oem_config_val;
  80. };
  81. struct scmi_msg_clock_config_get {
  82. __le32 id;
  83. __le32 flags;
  84. #define REGMASK_OEM_TYPE_GET GENMASK(7, 0)
  85. };
  86. struct scmi_msg_resp_clock_config_get {
  87. __le32 attributes;
  88. __le32 config;
  89. #define IS_CLK_ENABLED(x) le32_get_bits((x), BIT(0))
  90. __le32 oem_config_val;
  91. };
  92. struct scmi_msg_clock_describe_rates {
  93. __le32 id;
  94. __le32 rate_index;
  95. };
  96. struct scmi_msg_resp_clock_describe_rates {
  97. __le32 num_rates_flags;
  98. #define NUM_RETURNED(x) ((x) & 0xfff)
  99. #define RATE_DISCRETE(x) !((x) & BIT(12))
  100. #define NUM_REMAINING(x) ((x) >> 16)
  101. struct {
  102. __le32 value_low;
  103. __le32 value_high;
  104. } rate[];
  105. #define RATE_TO_U64(X) \
  106. ({ \
  107. typeof(X) x = (X); \
  108. le32_to_cpu((x).value_low) | (u64)le32_to_cpu((x).value_high) << 32; \
  109. })
  110. };
  111. struct scmi_clock_set_rate {
  112. __le32 flags;
  113. #define CLOCK_SET_ASYNC BIT(0)
  114. #define CLOCK_SET_IGNORE_RESP BIT(1)
  115. #define CLOCK_SET_ROUND_UP BIT(2)
  116. #define CLOCK_SET_ROUND_AUTO BIT(3)
  117. __le32 id;
  118. __le32 value_low;
  119. __le32 value_high;
  120. };
  121. struct scmi_msg_resp_set_rate_complete {
  122. __le32 id;
  123. __le32 rate_low;
  124. __le32 rate_high;
  125. };
  126. struct scmi_msg_clock_rate_notify {
  127. __le32 clk_id;
  128. __le32 notify_enable;
  129. };
  130. struct scmi_clock_rate_notify_payld {
  131. __le32 agent_id;
  132. __le32 clock_id;
  133. __le32 rate_low;
  134. __le32 rate_high;
  135. };
  136. struct clock_info {
  137. u32 version;
  138. int num_clocks;
  139. int max_async_req;
  140. bool notify_rate_changed_cmd;
  141. bool notify_rate_change_requested_cmd;
  142. atomic_t cur_async_req;
  143. struct scmi_clock_info *clk;
  144. int (*clock_config_set)(const struct scmi_protocol_handle *ph,
  145. u32 clk_id, enum clk_state state,
  146. enum scmi_clock_oem_config oem_type,
  147. u32 oem_val, bool atomic);
  148. int (*clock_config_get)(const struct scmi_protocol_handle *ph,
  149. u32 clk_id, enum scmi_clock_oem_config oem_type,
  150. u32 *attributes, bool *enabled, u32 *oem_val,
  151. bool atomic);
  152. };
  153. static enum scmi_clock_protocol_cmd evt_2_cmd[] = {
  154. CLOCK_RATE_NOTIFY,
  155. CLOCK_RATE_CHANGE_REQUESTED_NOTIFY,
  156. };
  157. static inline struct scmi_clock_info *
  158. scmi_clock_domain_lookup(struct clock_info *ci, u32 clk_id)
  159. {
  160. if (clk_id >= ci->num_clocks)
  161. return ERR_PTR(-EINVAL);
  162. return ci->clk + clk_id;
  163. }
  164. static int
  165. scmi_clock_protocol_attributes_get(const struct scmi_protocol_handle *ph,
  166. struct clock_info *ci)
  167. {
  168. int ret;
  169. struct scmi_xfer *t;
  170. struct scmi_msg_resp_clock_protocol_attributes *attr;
  171. ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES,
  172. 0, sizeof(*attr), &t);
  173. if (ret)
  174. return ret;
  175. attr = t->rx.buf;
  176. ret = ph->xops->do_xfer(ph, t);
  177. if (!ret) {
  178. ci->num_clocks = le16_to_cpu(attr->num_clocks);
  179. ci->max_async_req = attr->max_async_req;
  180. }
  181. ph->xops->xfer_put(ph, t);
  182. if (!ret) {
  183. if (!ph->hops->protocol_msg_check(ph, CLOCK_RATE_NOTIFY, NULL))
  184. ci->notify_rate_changed_cmd = true;
  185. if (!ph->hops->protocol_msg_check(ph,
  186. CLOCK_RATE_CHANGE_REQUESTED_NOTIFY,
  187. NULL))
  188. ci->notify_rate_change_requested_cmd = true;
  189. }
  190. return ret;
  191. }
  192. struct scmi_clk_ipriv {
  193. struct device *dev;
  194. u32 clk_id;
  195. struct scmi_clock_info *clk;
  196. };
  197. static void iter_clk_possible_parents_prepare_message(void *message, unsigned int desc_index,
  198. const void *priv)
  199. {
  200. struct scmi_msg_clock_possible_parents *msg = message;
  201. const struct scmi_clk_ipriv *p = priv;
  202. msg->id = cpu_to_le32(p->clk_id);
  203. /* Set the number of OPPs to be skipped/already read */
  204. msg->skip_parents = cpu_to_le32(desc_index);
  205. }
  206. static int iter_clk_possible_parents_update_state(struct scmi_iterator_state *st,
  207. const void *response, void *priv)
  208. {
  209. const struct scmi_msg_resp_clock_possible_parents *r = response;
  210. struct scmi_clk_ipriv *p = priv;
  211. struct device *dev = ((struct scmi_clk_ipriv *)p)->dev;
  212. u32 flags;
  213. flags = le32_to_cpu(r->num_parent_flags);
  214. st->num_returned = NUM_PARENTS_RETURNED(flags);
  215. st->num_remaining = NUM_PARENTS_REMAINING(flags);
  216. /*
  217. * num parents is not declared previously anywhere so we
  218. * assume it's returned+remaining on first call.
  219. */
  220. if (!st->max_resources) {
  221. p->clk->num_parents = st->num_returned + st->num_remaining;
  222. p->clk->parents = devm_kcalloc(dev, p->clk->num_parents,
  223. sizeof(*p->clk->parents),
  224. GFP_KERNEL);
  225. if (!p->clk->parents) {
  226. p->clk->num_parents = 0;
  227. return -ENOMEM;
  228. }
  229. st->max_resources = st->num_returned + st->num_remaining;
  230. }
  231. return 0;
  232. }
  233. static int iter_clk_possible_parents_process_response(const struct scmi_protocol_handle *ph,
  234. const void *response,
  235. struct scmi_iterator_state *st,
  236. void *priv)
  237. {
  238. const struct scmi_msg_resp_clock_possible_parents *r = response;
  239. struct scmi_clk_ipriv *p = priv;
  240. u32 *parent = &p->clk->parents[st->desc_index + st->loop_idx];
  241. *parent = le32_to_cpu(r->possible_parents[st->loop_idx]);
  242. return 0;
  243. }
  244. static int scmi_clock_possible_parents(const struct scmi_protocol_handle *ph, u32 clk_id,
  245. struct scmi_clock_info *clk)
  246. {
  247. struct scmi_iterator_ops ops = {
  248. .prepare_message = iter_clk_possible_parents_prepare_message,
  249. .update_state = iter_clk_possible_parents_update_state,
  250. .process_response = iter_clk_possible_parents_process_response,
  251. };
  252. struct scmi_clk_ipriv ppriv = {
  253. .clk_id = clk_id,
  254. .clk = clk,
  255. .dev = ph->dev,
  256. };
  257. void *iter;
  258. int ret;
  259. iter = ph->hops->iter_response_init(ph, &ops, 0,
  260. CLOCK_POSSIBLE_PARENTS_GET,
  261. sizeof(struct scmi_msg_clock_possible_parents),
  262. &ppriv);
  263. if (IS_ERR(iter))
  264. return PTR_ERR(iter);
  265. ret = ph->hops->iter_response_run(iter);
  266. return ret;
  267. }
  268. static int
  269. scmi_clock_get_permissions(const struct scmi_protocol_handle *ph, u32 clk_id,
  270. struct scmi_clock_info *clk)
  271. {
  272. struct scmi_xfer *t;
  273. u32 perm;
  274. int ret;
  275. ret = ph->xops->xfer_get_init(ph, CLOCK_GET_PERMISSIONS,
  276. sizeof(clk_id), sizeof(perm), &t);
  277. if (ret)
  278. return ret;
  279. put_unaligned_le32(clk_id, t->tx.buf);
  280. ret = ph->xops->do_xfer(ph, t);
  281. if (!ret) {
  282. perm = get_unaligned_le32(t->rx.buf);
  283. clk->state_ctrl_forbidden = !(perm & CLOCK_STATE_CONTROL_ALLOWED);
  284. clk->rate_ctrl_forbidden = !(perm & CLOCK_RATE_CONTROL_ALLOWED);
  285. clk->parent_ctrl_forbidden = !(perm & CLOCK_PARENT_CONTROL_ALLOWED);
  286. }
  287. ph->xops->xfer_put(ph, t);
  288. return ret;
  289. }
  290. static int scmi_clock_attributes_get(const struct scmi_protocol_handle *ph,
  291. u32 clk_id, struct clock_info *cinfo,
  292. u32 version)
  293. {
  294. int ret;
  295. u32 attributes;
  296. struct scmi_xfer *t;
  297. struct scmi_msg_resp_clock_attributes *attr;
  298. struct scmi_clock_info *clk = cinfo->clk + clk_id;
  299. ret = ph->xops->xfer_get_init(ph, CLOCK_ATTRIBUTES,
  300. sizeof(clk_id), sizeof(*attr), &t);
  301. if (ret)
  302. return ret;
  303. put_unaligned_le32(clk_id, t->tx.buf);
  304. attr = t->rx.buf;
  305. ret = ph->xops->do_xfer(ph, t);
  306. if (!ret) {
  307. u32 latency = 0;
  308. attributes = le32_to_cpu(attr->attributes);
  309. strscpy(clk->name, attr->name, SCMI_SHORT_NAME_MAX_SIZE);
  310. /* clock_enable_latency field is present only since SCMI v3.1 */
  311. if (PROTOCOL_REV_MAJOR(version) >= 0x2)
  312. latency = le32_to_cpu(attr->clock_enable_latency);
  313. clk->enable_latency = latency ? : U32_MAX;
  314. }
  315. ph->xops->xfer_put(ph, t);
  316. /*
  317. * If supported overwrite short name with the extended one;
  318. * on error just carry on and use already provided short name.
  319. */
  320. if (!ret && PROTOCOL_REV_MAJOR(version) >= 0x2) {
  321. if (SUPPORTS_EXTENDED_NAMES(attributes))
  322. ph->hops->extended_name_get(ph, CLOCK_NAME_GET, clk_id,
  323. NULL, clk->name,
  324. SCMI_MAX_STR_SIZE);
  325. if (cinfo->notify_rate_changed_cmd &&
  326. SUPPORTS_RATE_CHANGED_NOTIF(attributes))
  327. clk->rate_changed_notifications = true;
  328. if (cinfo->notify_rate_change_requested_cmd &&
  329. SUPPORTS_RATE_CHANGE_REQUESTED_NOTIF(attributes))
  330. clk->rate_change_requested_notifications = true;
  331. if (PROTOCOL_REV_MAJOR(version) >= 0x3) {
  332. if (SUPPORTS_PARENT_CLOCK(attributes))
  333. scmi_clock_possible_parents(ph, clk_id, clk);
  334. if (SUPPORTS_GET_PERMISSIONS(attributes))
  335. scmi_clock_get_permissions(ph, clk_id, clk);
  336. if (SUPPORTS_EXTENDED_CONFIG(attributes))
  337. clk->extended_config = true;
  338. }
  339. }
  340. return ret;
  341. }
  342. static int rate_cmp_func(const void *_r1, const void *_r2)
  343. {
  344. const u64 *r1 = _r1, *r2 = _r2;
  345. if (*r1 < *r2)
  346. return -1;
  347. else if (*r1 == *r2)
  348. return 0;
  349. else
  350. return 1;
  351. }
  352. static void iter_clk_describe_prepare_message(void *message,
  353. const unsigned int desc_index,
  354. const void *priv)
  355. {
  356. struct scmi_msg_clock_describe_rates *msg = message;
  357. const struct scmi_clk_ipriv *p = priv;
  358. msg->id = cpu_to_le32(p->clk_id);
  359. /* Set the number of rates to be skipped/already read */
  360. msg->rate_index = cpu_to_le32(desc_index);
  361. }
  362. static int
  363. iter_clk_describe_update_state(struct scmi_iterator_state *st,
  364. const void *response, void *priv)
  365. {
  366. u32 flags;
  367. struct scmi_clk_ipriv *p = priv;
  368. const struct scmi_msg_resp_clock_describe_rates *r = response;
  369. flags = le32_to_cpu(r->num_rates_flags);
  370. st->num_remaining = NUM_REMAINING(flags);
  371. st->num_returned = NUM_RETURNED(flags);
  372. p->clk->rate_discrete = RATE_DISCRETE(flags);
  373. /* Warn about out of spec replies ... */
  374. if (!p->clk->rate_discrete &&
  375. (st->num_returned != 3 || st->num_remaining != 0)) {
  376. dev_warn(p->dev,
  377. "Out-of-spec CLOCK_DESCRIBE_RATES reply for %s - returned:%d remaining:%d rx_len:%zd\n",
  378. p->clk->name, st->num_returned, st->num_remaining,
  379. st->rx_len);
  380. /*
  381. * A known quirk: a triplet is returned but num_returned != 3
  382. * Check for a safe payload size and fix.
  383. */
  384. if (st->num_returned != 3 && st->num_remaining == 0 &&
  385. st->rx_len == sizeof(*r) + sizeof(__le32) * 2 * 3) {
  386. st->num_returned = 3;
  387. st->num_remaining = 0;
  388. } else {
  389. dev_err(p->dev,
  390. "Cannot fix out-of-spec reply !\n");
  391. return -EPROTO;
  392. }
  393. }
  394. return 0;
  395. }
  396. static int
  397. iter_clk_describe_process_response(const struct scmi_protocol_handle *ph,
  398. const void *response,
  399. struct scmi_iterator_state *st, void *priv)
  400. {
  401. int ret = 0;
  402. struct scmi_clk_ipriv *p = priv;
  403. const struct scmi_msg_resp_clock_describe_rates *r = response;
  404. if (!p->clk->rate_discrete) {
  405. switch (st->desc_index + st->loop_idx) {
  406. case 0:
  407. p->clk->range.min_rate = RATE_TO_U64(r->rate[0]);
  408. break;
  409. case 1:
  410. p->clk->range.max_rate = RATE_TO_U64(r->rate[1]);
  411. break;
  412. case 2:
  413. p->clk->range.step_size = RATE_TO_U64(r->rate[2]);
  414. break;
  415. default:
  416. ret = -EINVAL;
  417. break;
  418. }
  419. } else {
  420. u64 *rate = &p->clk->list.rates[st->desc_index + st->loop_idx];
  421. *rate = RATE_TO_U64(r->rate[st->loop_idx]);
  422. p->clk->list.num_rates++;
  423. }
  424. return ret;
  425. }
  426. static int
  427. scmi_clock_describe_rates_get(const struct scmi_protocol_handle *ph, u32 clk_id,
  428. struct scmi_clock_info *clk)
  429. {
  430. int ret;
  431. void *iter;
  432. struct scmi_iterator_ops ops = {
  433. .prepare_message = iter_clk_describe_prepare_message,
  434. .update_state = iter_clk_describe_update_state,
  435. .process_response = iter_clk_describe_process_response,
  436. };
  437. struct scmi_clk_ipriv cpriv = {
  438. .clk_id = clk_id,
  439. .clk = clk,
  440. .dev = ph->dev,
  441. };
  442. iter = ph->hops->iter_response_init(ph, &ops, SCMI_MAX_NUM_RATES,
  443. CLOCK_DESCRIBE_RATES,
  444. sizeof(struct scmi_msg_clock_describe_rates),
  445. &cpriv);
  446. if (IS_ERR(iter))
  447. return PTR_ERR(iter);
  448. ret = ph->hops->iter_response_run(iter);
  449. if (ret)
  450. return ret;
  451. if (!clk->rate_discrete) {
  452. dev_dbg(ph->dev, "Min %llu Max %llu Step %llu Hz\n",
  453. clk->range.min_rate, clk->range.max_rate,
  454. clk->range.step_size);
  455. } else if (clk->list.num_rates) {
  456. sort(clk->list.rates, clk->list.num_rates,
  457. sizeof(clk->list.rates[0]), rate_cmp_func, NULL);
  458. }
  459. return ret;
  460. }
  461. static int
  462. scmi_clock_rate_get(const struct scmi_protocol_handle *ph,
  463. u32 clk_id, u64 *value)
  464. {
  465. int ret;
  466. struct scmi_xfer *t;
  467. ret = ph->xops->xfer_get_init(ph, CLOCK_RATE_GET,
  468. sizeof(__le32), sizeof(u64), &t);
  469. if (ret)
  470. return ret;
  471. put_unaligned_le32(clk_id, t->tx.buf);
  472. ret = ph->xops->do_xfer(ph, t);
  473. if (!ret)
  474. *value = get_unaligned_le64(t->rx.buf);
  475. ph->xops->xfer_put(ph, t);
  476. return ret;
  477. }
  478. static int scmi_clock_rate_set(const struct scmi_protocol_handle *ph,
  479. u32 clk_id, u64 rate)
  480. {
  481. int ret;
  482. u32 flags = 0;
  483. struct scmi_xfer *t;
  484. struct scmi_clock_set_rate *cfg;
  485. struct clock_info *ci = ph->get_priv(ph);
  486. struct scmi_clock_info *clk;
  487. clk = scmi_clock_domain_lookup(ci, clk_id);
  488. if (IS_ERR(clk))
  489. return PTR_ERR(clk);
  490. if (clk->rate_ctrl_forbidden)
  491. return -EACCES;
  492. ret = ph->xops->xfer_get_init(ph, CLOCK_RATE_SET, sizeof(*cfg), 0, &t);
  493. if (ret)
  494. return ret;
  495. if (ci->max_async_req &&
  496. atomic_inc_return(&ci->cur_async_req) < ci->max_async_req)
  497. flags |= CLOCK_SET_ASYNC;
  498. cfg = t->tx.buf;
  499. cfg->flags = cpu_to_le32(flags);
  500. cfg->id = cpu_to_le32(clk_id);
  501. cfg->value_low = cpu_to_le32(rate & 0xffffffff);
  502. cfg->value_high = cpu_to_le32(rate >> 32);
  503. if (flags & CLOCK_SET_ASYNC) {
  504. ret = ph->xops->do_xfer_with_response(ph, t);
  505. if (!ret) {
  506. struct scmi_msg_resp_set_rate_complete *resp;
  507. resp = t->rx.buf;
  508. if (le32_to_cpu(resp->id) == clk_id)
  509. dev_dbg(ph->dev,
  510. "Clk ID %d set async to %llu\n", clk_id,
  511. get_unaligned_le64(&resp->rate_low));
  512. else
  513. ret = -EPROTO;
  514. }
  515. } else {
  516. ret = ph->xops->do_xfer(ph, t);
  517. }
  518. if (ci->max_async_req)
  519. atomic_dec(&ci->cur_async_req);
  520. ph->xops->xfer_put(ph, t);
  521. return ret;
  522. }
  523. static int
  524. scmi_clock_config_set(const struct scmi_protocol_handle *ph, u32 clk_id,
  525. enum clk_state state,
  526. enum scmi_clock_oem_config __unused0, u32 __unused1,
  527. bool atomic)
  528. {
  529. int ret;
  530. struct scmi_xfer *t;
  531. struct scmi_msg_clock_config_set *cfg;
  532. if (state >= CLK_STATE_RESERVED)
  533. return -EINVAL;
  534. ret = ph->xops->xfer_get_init(ph, CLOCK_CONFIG_SET,
  535. sizeof(*cfg), 0, &t);
  536. if (ret)
  537. return ret;
  538. t->hdr.poll_completion = atomic;
  539. cfg = t->tx.buf;
  540. cfg->id = cpu_to_le32(clk_id);
  541. cfg->attributes = cpu_to_le32(state);
  542. ret = ph->xops->do_xfer(ph, t);
  543. ph->xops->xfer_put(ph, t);
  544. return ret;
  545. }
  546. static int
  547. scmi_clock_set_parent(const struct scmi_protocol_handle *ph, u32 clk_id,
  548. u32 parent_id)
  549. {
  550. int ret;
  551. struct scmi_xfer *t;
  552. struct scmi_msg_clock_set_parent *cfg;
  553. struct clock_info *ci = ph->get_priv(ph);
  554. struct scmi_clock_info *clk;
  555. clk = scmi_clock_domain_lookup(ci, clk_id);
  556. if (IS_ERR(clk))
  557. return PTR_ERR(clk);
  558. if (parent_id >= clk->num_parents)
  559. return -EINVAL;
  560. if (clk->parent_ctrl_forbidden)
  561. return -EACCES;
  562. ret = ph->xops->xfer_get_init(ph, CLOCK_PARENT_SET,
  563. sizeof(*cfg), 0, &t);
  564. if (ret)
  565. return ret;
  566. t->hdr.poll_completion = false;
  567. cfg = t->tx.buf;
  568. cfg->id = cpu_to_le32(clk_id);
  569. cfg->parent_id = cpu_to_le32(clk->parents[parent_id]);
  570. ret = ph->xops->do_xfer(ph, t);
  571. ph->xops->xfer_put(ph, t);
  572. return ret;
  573. }
  574. static int
  575. scmi_clock_get_parent(const struct scmi_protocol_handle *ph, u32 clk_id,
  576. u32 *parent_id)
  577. {
  578. int ret;
  579. struct scmi_xfer *t;
  580. ret = ph->xops->xfer_get_init(ph, CLOCK_PARENT_GET,
  581. sizeof(__le32), sizeof(u32), &t);
  582. if (ret)
  583. return ret;
  584. put_unaligned_le32(clk_id, t->tx.buf);
  585. ret = ph->xops->do_xfer(ph, t);
  586. if (!ret)
  587. *parent_id = get_unaligned_le32(t->rx.buf);
  588. ph->xops->xfer_put(ph, t);
  589. return ret;
  590. }
  591. /* For SCMI clock v3.0 and onwards */
  592. static int
  593. scmi_clock_config_set_v2(const struct scmi_protocol_handle *ph, u32 clk_id,
  594. enum clk_state state,
  595. enum scmi_clock_oem_config oem_type, u32 oem_val,
  596. bool atomic)
  597. {
  598. int ret;
  599. u32 attrs;
  600. struct scmi_xfer *t;
  601. struct scmi_msg_clock_config_set_v2 *cfg;
  602. if (state == CLK_STATE_RESERVED ||
  603. (!oem_type && state == CLK_STATE_UNCHANGED))
  604. return -EINVAL;
  605. ret = ph->xops->xfer_get_init(ph, CLOCK_CONFIG_SET,
  606. sizeof(*cfg), 0, &t);
  607. if (ret)
  608. return ret;
  609. t->hdr.poll_completion = atomic;
  610. attrs = FIELD_PREP(REGMASK_OEM_TYPE_SET, oem_type) |
  611. FIELD_PREP(REGMASK_CLK_STATE, state);
  612. cfg = t->tx.buf;
  613. cfg->id = cpu_to_le32(clk_id);
  614. cfg->attributes = cpu_to_le32(attrs);
  615. /* Clear in any case */
  616. cfg->oem_config_val = cpu_to_le32(0);
  617. if (oem_type)
  618. cfg->oem_config_val = cpu_to_le32(oem_val);
  619. ret = ph->xops->do_xfer(ph, t);
  620. ph->xops->xfer_put(ph, t);
  621. return ret;
  622. }
  623. static int scmi_clock_enable(const struct scmi_protocol_handle *ph, u32 clk_id,
  624. bool atomic)
  625. {
  626. struct clock_info *ci = ph->get_priv(ph);
  627. struct scmi_clock_info *clk;
  628. clk = scmi_clock_domain_lookup(ci, clk_id);
  629. if (IS_ERR(clk))
  630. return PTR_ERR(clk);
  631. if (clk->state_ctrl_forbidden)
  632. return -EACCES;
  633. return ci->clock_config_set(ph, clk_id, CLK_STATE_ENABLE,
  634. NULL_OEM_TYPE, 0, atomic);
  635. }
  636. static int scmi_clock_disable(const struct scmi_protocol_handle *ph, u32 clk_id,
  637. bool atomic)
  638. {
  639. struct clock_info *ci = ph->get_priv(ph);
  640. struct scmi_clock_info *clk;
  641. clk = scmi_clock_domain_lookup(ci, clk_id);
  642. if (IS_ERR(clk))
  643. return PTR_ERR(clk);
  644. if (clk->state_ctrl_forbidden)
  645. return -EACCES;
  646. return ci->clock_config_set(ph, clk_id, CLK_STATE_DISABLE,
  647. NULL_OEM_TYPE, 0, atomic);
  648. }
  649. /* For SCMI clock v3.0 and onwards */
  650. static int
  651. scmi_clock_config_get_v2(const struct scmi_protocol_handle *ph, u32 clk_id,
  652. enum scmi_clock_oem_config oem_type, u32 *attributes,
  653. bool *enabled, u32 *oem_val, bool atomic)
  654. {
  655. int ret;
  656. u32 flags;
  657. struct scmi_xfer *t;
  658. struct scmi_msg_clock_config_get *cfg;
  659. ret = ph->xops->xfer_get_init(ph, CLOCK_CONFIG_GET,
  660. sizeof(*cfg), 0, &t);
  661. if (ret)
  662. return ret;
  663. t->hdr.poll_completion = atomic;
  664. flags = FIELD_PREP(REGMASK_OEM_TYPE_GET, oem_type);
  665. cfg = t->tx.buf;
  666. cfg->id = cpu_to_le32(clk_id);
  667. cfg->flags = cpu_to_le32(flags);
  668. ret = ph->xops->do_xfer(ph, t);
  669. if (!ret) {
  670. struct scmi_msg_resp_clock_config_get *resp = t->rx.buf;
  671. if (attributes)
  672. *attributes = le32_to_cpu(resp->attributes);
  673. if (enabled)
  674. *enabled = IS_CLK_ENABLED(resp->config);
  675. if (oem_val && oem_type)
  676. *oem_val = le32_to_cpu(resp->oem_config_val);
  677. }
  678. ph->xops->xfer_put(ph, t);
  679. return ret;
  680. }
  681. static int
  682. scmi_clock_config_get(const struct scmi_protocol_handle *ph, u32 clk_id,
  683. enum scmi_clock_oem_config oem_type, u32 *attributes,
  684. bool *enabled, u32 *oem_val, bool atomic)
  685. {
  686. int ret;
  687. struct scmi_xfer *t;
  688. struct scmi_msg_resp_clock_attributes *resp;
  689. if (!enabled)
  690. return -EINVAL;
  691. ret = ph->xops->xfer_get_init(ph, CLOCK_ATTRIBUTES,
  692. sizeof(clk_id), sizeof(*resp), &t);
  693. if (ret)
  694. return ret;
  695. t->hdr.poll_completion = atomic;
  696. put_unaligned_le32(clk_id, t->tx.buf);
  697. resp = t->rx.buf;
  698. ret = ph->xops->do_xfer(ph, t);
  699. if (!ret)
  700. *enabled = IS_CLK_ENABLED(resp->attributes);
  701. ph->xops->xfer_put(ph, t);
  702. return ret;
  703. }
  704. static int scmi_clock_state_get(const struct scmi_protocol_handle *ph,
  705. u32 clk_id, bool *enabled, bool atomic)
  706. {
  707. struct clock_info *ci = ph->get_priv(ph);
  708. return ci->clock_config_get(ph, clk_id, NULL_OEM_TYPE, NULL,
  709. enabled, NULL, atomic);
  710. }
  711. static int scmi_clock_config_oem_set(const struct scmi_protocol_handle *ph,
  712. u32 clk_id,
  713. enum scmi_clock_oem_config oem_type,
  714. u32 oem_val, bool atomic)
  715. {
  716. struct clock_info *ci = ph->get_priv(ph);
  717. struct scmi_clock_info *clk;
  718. clk = scmi_clock_domain_lookup(ci, clk_id);
  719. if (IS_ERR(clk))
  720. return PTR_ERR(clk);
  721. if (!clk->extended_config)
  722. return -EOPNOTSUPP;
  723. return ci->clock_config_set(ph, clk_id, CLK_STATE_UNCHANGED,
  724. oem_type, oem_val, atomic);
  725. }
  726. static int scmi_clock_config_oem_get(const struct scmi_protocol_handle *ph,
  727. u32 clk_id,
  728. enum scmi_clock_oem_config oem_type,
  729. u32 *oem_val, u32 *attributes, bool atomic)
  730. {
  731. struct clock_info *ci = ph->get_priv(ph);
  732. struct scmi_clock_info *clk;
  733. clk = scmi_clock_domain_lookup(ci, clk_id);
  734. if (IS_ERR(clk))
  735. return PTR_ERR(clk);
  736. if (!clk->extended_config)
  737. return -EOPNOTSUPP;
  738. return ci->clock_config_get(ph, clk_id, oem_type, attributes,
  739. NULL, oem_val, atomic);
  740. }
  741. static int scmi_clock_count_get(const struct scmi_protocol_handle *ph)
  742. {
  743. struct clock_info *ci = ph->get_priv(ph);
  744. return ci->num_clocks;
  745. }
  746. static const struct scmi_clock_info *
  747. scmi_clock_info_get(const struct scmi_protocol_handle *ph, u32 clk_id)
  748. {
  749. struct scmi_clock_info *clk;
  750. struct clock_info *ci = ph->get_priv(ph);
  751. clk = scmi_clock_domain_lookup(ci, clk_id);
  752. if (IS_ERR(clk))
  753. return NULL;
  754. if (!clk->name[0])
  755. return NULL;
  756. return clk;
  757. }
  758. static const struct scmi_clk_proto_ops clk_proto_ops = {
  759. .count_get = scmi_clock_count_get,
  760. .info_get = scmi_clock_info_get,
  761. .rate_get = scmi_clock_rate_get,
  762. .rate_set = scmi_clock_rate_set,
  763. .enable = scmi_clock_enable,
  764. .disable = scmi_clock_disable,
  765. .state_get = scmi_clock_state_get,
  766. .config_oem_get = scmi_clock_config_oem_get,
  767. .config_oem_set = scmi_clock_config_oem_set,
  768. .parent_set = scmi_clock_set_parent,
  769. .parent_get = scmi_clock_get_parent,
  770. };
  771. static bool scmi_clk_notify_supported(const struct scmi_protocol_handle *ph,
  772. u8 evt_id, u32 src_id)
  773. {
  774. bool supported;
  775. struct scmi_clock_info *clk;
  776. struct clock_info *ci = ph->get_priv(ph);
  777. if (evt_id >= ARRAY_SIZE(evt_2_cmd))
  778. return false;
  779. clk = scmi_clock_domain_lookup(ci, src_id);
  780. if (IS_ERR(clk))
  781. return false;
  782. if (evt_id == SCMI_EVENT_CLOCK_RATE_CHANGED)
  783. supported = clk->rate_changed_notifications;
  784. else
  785. supported = clk->rate_change_requested_notifications;
  786. return supported;
  787. }
  788. static int scmi_clk_rate_notify(const struct scmi_protocol_handle *ph,
  789. u32 clk_id, int message_id, bool enable)
  790. {
  791. int ret;
  792. struct scmi_xfer *t;
  793. struct scmi_msg_clock_rate_notify *notify;
  794. ret = ph->xops->xfer_get_init(ph, message_id, sizeof(*notify), 0, &t);
  795. if (ret)
  796. return ret;
  797. notify = t->tx.buf;
  798. notify->clk_id = cpu_to_le32(clk_id);
  799. notify->notify_enable = enable ? cpu_to_le32(BIT(0)) : 0;
  800. ret = ph->xops->do_xfer(ph, t);
  801. ph->xops->xfer_put(ph, t);
  802. return ret;
  803. }
  804. static int scmi_clk_set_notify_enabled(const struct scmi_protocol_handle *ph,
  805. u8 evt_id, u32 src_id, bool enable)
  806. {
  807. int ret, cmd_id;
  808. if (evt_id >= ARRAY_SIZE(evt_2_cmd))
  809. return -EINVAL;
  810. cmd_id = evt_2_cmd[evt_id];
  811. ret = scmi_clk_rate_notify(ph, src_id, cmd_id, enable);
  812. if (ret)
  813. pr_debug("FAIL_ENABLED - evt[%X] dom[%d] - ret:%d\n",
  814. evt_id, src_id, ret);
  815. return ret;
  816. }
  817. static void *scmi_clk_fill_custom_report(const struct scmi_protocol_handle *ph,
  818. u8 evt_id, ktime_t timestamp,
  819. const void *payld, size_t payld_sz,
  820. void *report, u32 *src_id)
  821. {
  822. const struct scmi_clock_rate_notify_payld *p = payld;
  823. struct scmi_clock_rate_notif_report *r = report;
  824. if (sizeof(*p) != payld_sz ||
  825. (evt_id != SCMI_EVENT_CLOCK_RATE_CHANGED &&
  826. evt_id != SCMI_EVENT_CLOCK_RATE_CHANGE_REQUESTED))
  827. return NULL;
  828. r->timestamp = timestamp;
  829. r->agent_id = le32_to_cpu(p->agent_id);
  830. r->clock_id = le32_to_cpu(p->clock_id);
  831. r->rate = get_unaligned_le64(&p->rate_low);
  832. *src_id = r->clock_id;
  833. return r;
  834. }
  835. static int scmi_clk_get_num_sources(const struct scmi_protocol_handle *ph)
  836. {
  837. struct clock_info *ci = ph->get_priv(ph);
  838. if (!ci)
  839. return -EINVAL;
  840. return ci->num_clocks;
  841. }
  842. static const struct scmi_event clk_events[] = {
  843. {
  844. .id = SCMI_EVENT_CLOCK_RATE_CHANGED,
  845. .max_payld_sz = sizeof(struct scmi_clock_rate_notify_payld),
  846. .max_report_sz = sizeof(struct scmi_clock_rate_notif_report),
  847. },
  848. {
  849. .id = SCMI_EVENT_CLOCK_RATE_CHANGE_REQUESTED,
  850. .max_payld_sz = sizeof(struct scmi_clock_rate_notify_payld),
  851. .max_report_sz = sizeof(struct scmi_clock_rate_notif_report),
  852. },
  853. };
  854. static const struct scmi_event_ops clk_event_ops = {
  855. .is_notify_supported = scmi_clk_notify_supported,
  856. .get_num_sources = scmi_clk_get_num_sources,
  857. .set_notify_enabled = scmi_clk_set_notify_enabled,
  858. .fill_custom_report = scmi_clk_fill_custom_report,
  859. };
  860. static const struct scmi_protocol_events clk_protocol_events = {
  861. .queue_sz = SCMI_PROTO_QUEUE_SZ,
  862. .ops = &clk_event_ops,
  863. .evts = clk_events,
  864. .num_events = ARRAY_SIZE(clk_events),
  865. };
  866. static int scmi_clock_protocol_init(const struct scmi_protocol_handle *ph)
  867. {
  868. u32 version;
  869. int clkid, ret;
  870. struct clock_info *cinfo;
  871. ret = ph->xops->version_get(ph, &version);
  872. if (ret)
  873. return ret;
  874. dev_dbg(ph->dev, "Clock Version %d.%d\n",
  875. PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));
  876. cinfo = devm_kzalloc(ph->dev, sizeof(*cinfo), GFP_KERNEL);
  877. if (!cinfo)
  878. return -ENOMEM;
  879. ret = scmi_clock_protocol_attributes_get(ph, cinfo);
  880. if (ret)
  881. return ret;
  882. cinfo->clk = devm_kcalloc(ph->dev, cinfo->num_clocks,
  883. sizeof(*cinfo->clk), GFP_KERNEL);
  884. if (!cinfo->clk)
  885. return -ENOMEM;
  886. for (clkid = 0; clkid < cinfo->num_clocks; clkid++) {
  887. struct scmi_clock_info *clk = cinfo->clk + clkid;
  888. ret = scmi_clock_attributes_get(ph, clkid, cinfo, version);
  889. if (!ret)
  890. scmi_clock_describe_rates_get(ph, clkid, clk);
  891. }
  892. if (PROTOCOL_REV_MAJOR(version) >= 0x3) {
  893. cinfo->clock_config_set = scmi_clock_config_set_v2;
  894. cinfo->clock_config_get = scmi_clock_config_get_v2;
  895. } else {
  896. cinfo->clock_config_set = scmi_clock_config_set;
  897. cinfo->clock_config_get = scmi_clock_config_get;
  898. }
  899. cinfo->version = version;
  900. return ph->set_priv(ph, cinfo, version);
  901. }
  902. static const struct scmi_protocol scmi_clock = {
  903. .id = SCMI_PROTOCOL_CLOCK,
  904. .owner = THIS_MODULE,
  905. .instance_init = &scmi_clock_protocol_init,
  906. .ops = &clk_proto_ops,
  907. .events = &clk_protocol_events,
  908. .supported_version = SCMI_PROTOCOL_SUPPORTED_VERSION,
  909. };
  910. DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(clock, scmi_clock)