clock.c 27 KB

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