1 // SPDX-License-Identifier: GPL-2.0
3 * System Control and Management Interface (SCMI) Clock Protocol
5 * Copyright (C) 2018-2022 ARM Ltd.
8 #include <linux/module.h>
9 #include <linux/limits.h>
10 #include <linux/sort.h>
12 #include "protocols.h"
15 enum scmi_clock_protocol_cmd {
16 CLOCK_ATTRIBUTES = 0x3,
17 CLOCK_DESCRIBE_RATES = 0x4,
20 CLOCK_CONFIG_SET = 0x7,
22 CLOCK_RATE_NOTIFY = 0x9,
23 CLOCK_RATE_CHANGE_REQUESTED_NOTIFY = 0xA,
26 struct scmi_msg_resp_clock_protocol_attributes {
32 struct scmi_msg_resp_clock_attributes {
34 #define CLOCK_ENABLE BIT(0)
35 #define SUPPORTS_RATE_CHANGED_NOTIF(x) ((x) & BIT(31))
36 #define SUPPORTS_RATE_CHANGE_REQUESTED_NOTIF(x) ((x) & BIT(30))
37 #define SUPPORTS_EXTENDED_NAMES(x) ((x) & BIT(29))
38 u8 name[SCMI_SHORT_NAME_MAX_SIZE];
39 __le32 clock_enable_latency;
42 struct scmi_clock_set_config {
47 struct scmi_msg_clock_describe_rates {
52 struct scmi_msg_resp_clock_describe_rates {
53 __le32 num_rates_flags;
54 #define NUM_RETURNED(x) ((x) & 0xfff)
55 #define RATE_DISCRETE(x) !((x) & BIT(12))
56 #define NUM_REMAINING(x) ((x) >> 16)
61 #define RATE_TO_U64(X) \
64 le32_to_cpu((x).value_low) | (u64)le32_to_cpu((x).value_high) << 32; \
68 struct scmi_clock_set_rate {
70 #define CLOCK_SET_ASYNC BIT(0)
71 #define CLOCK_SET_IGNORE_RESP BIT(1)
72 #define CLOCK_SET_ROUND_UP BIT(2)
73 #define CLOCK_SET_ROUND_AUTO BIT(3)
79 struct scmi_msg_resp_set_rate_complete {
85 struct scmi_msg_clock_rate_notify {
90 struct scmi_clock_rate_notify_payld {
101 atomic_t cur_async_req;
102 struct scmi_clock_info *clk;
105 static enum scmi_clock_protocol_cmd evt_2_cmd[] = {
107 CLOCK_RATE_CHANGE_REQUESTED_NOTIFY,
111 scmi_clock_protocol_attributes_get(const struct scmi_protocol_handle *ph,
112 struct clock_info *ci)
116 struct scmi_msg_resp_clock_protocol_attributes *attr;
118 ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES,
119 0, sizeof(*attr), &t);
125 ret = ph->xops->do_xfer(ph, t);
127 ci->num_clocks = le16_to_cpu(attr->num_clocks);
128 ci->max_async_req = attr->max_async_req;
131 ph->xops->xfer_put(ph, t);
135 static int scmi_clock_attributes_get(const struct scmi_protocol_handle *ph,
136 u32 clk_id, struct scmi_clock_info *clk,
142 struct scmi_msg_resp_clock_attributes *attr;
144 ret = ph->xops->xfer_get_init(ph, CLOCK_ATTRIBUTES,
145 sizeof(clk_id), sizeof(*attr), &t);
149 put_unaligned_le32(clk_id, t->tx.buf);
152 ret = ph->xops->do_xfer(ph, t);
155 attributes = le32_to_cpu(attr->attributes);
156 strscpy(clk->name, attr->name, SCMI_SHORT_NAME_MAX_SIZE);
157 /* clock_enable_latency field is present only since SCMI v3.1 */
158 if (PROTOCOL_REV_MAJOR(version) >= 0x2)
159 latency = le32_to_cpu(attr->clock_enable_latency);
160 clk->enable_latency = latency ? : U32_MAX;
163 ph->xops->xfer_put(ph, t);
166 * If supported overwrite short name with the extended one;
167 * on error just carry on and use already provided short name.
169 if (!ret && PROTOCOL_REV_MAJOR(version) >= 0x2) {
170 if (SUPPORTS_EXTENDED_NAMES(attributes))
171 ph->hops->extended_name_get(ph, CLOCK_NAME_GET, clk_id,
175 if (SUPPORTS_RATE_CHANGED_NOTIF(attributes))
176 clk->rate_changed_notifications = true;
177 if (SUPPORTS_RATE_CHANGE_REQUESTED_NOTIF(attributes))
178 clk->rate_change_requested_notifications = true;
184 static int rate_cmp_func(const void *_r1, const void *_r2)
186 const u64 *r1 = _r1, *r2 = _r2;
196 struct scmi_clk_ipriv {
199 struct scmi_clock_info *clk;
202 static void iter_clk_describe_prepare_message(void *message,
203 const unsigned int desc_index,
206 struct scmi_msg_clock_describe_rates *msg = message;
207 const struct scmi_clk_ipriv *p = priv;
209 msg->id = cpu_to_le32(p->clk_id);
210 /* Set the number of rates to be skipped/already read */
211 msg->rate_index = cpu_to_le32(desc_index);
215 iter_clk_describe_update_state(struct scmi_iterator_state *st,
216 const void *response, void *priv)
219 struct scmi_clk_ipriv *p = priv;
220 const struct scmi_msg_resp_clock_describe_rates *r = response;
222 flags = le32_to_cpu(r->num_rates_flags);
223 st->num_remaining = NUM_REMAINING(flags);
224 st->num_returned = NUM_RETURNED(flags);
225 p->clk->rate_discrete = RATE_DISCRETE(flags);
227 /* Warn about out of spec replies ... */
228 if (!p->clk->rate_discrete &&
229 (st->num_returned != 3 || st->num_remaining != 0)) {
231 "Out-of-spec CLOCK_DESCRIBE_RATES reply for %s - returned:%d remaining:%d rx_len:%zd\n",
232 p->clk->name, st->num_returned, st->num_remaining,
236 * A known quirk: a triplet is returned but num_returned != 3
237 * Check for a safe payload size and fix.
239 if (st->num_returned != 3 && st->num_remaining == 0 &&
240 st->rx_len == sizeof(*r) + sizeof(__le32) * 2 * 3) {
241 st->num_returned = 3;
242 st->num_remaining = 0;
245 "Cannot fix out-of-spec reply !\n");
254 iter_clk_describe_process_response(const struct scmi_protocol_handle *ph,
255 const void *response,
256 struct scmi_iterator_state *st, void *priv)
259 struct scmi_clk_ipriv *p = priv;
260 const struct scmi_msg_resp_clock_describe_rates *r = response;
262 if (!p->clk->rate_discrete) {
263 switch (st->desc_index + st->loop_idx) {
265 p->clk->range.min_rate = RATE_TO_U64(r->rate[0]);
268 p->clk->range.max_rate = RATE_TO_U64(r->rate[1]);
271 p->clk->range.step_size = RATE_TO_U64(r->rate[2]);
278 u64 *rate = &p->clk->list.rates[st->desc_index + st->loop_idx];
280 *rate = RATE_TO_U64(r->rate[st->loop_idx]);
281 p->clk->list.num_rates++;
288 scmi_clock_describe_rates_get(const struct scmi_protocol_handle *ph, u32 clk_id,
289 struct scmi_clock_info *clk)
293 struct scmi_iterator_ops ops = {
294 .prepare_message = iter_clk_describe_prepare_message,
295 .update_state = iter_clk_describe_update_state,
296 .process_response = iter_clk_describe_process_response,
298 struct scmi_clk_ipriv cpriv = {
304 iter = ph->hops->iter_response_init(ph, &ops, SCMI_MAX_NUM_RATES,
305 CLOCK_DESCRIBE_RATES,
306 sizeof(struct scmi_msg_clock_describe_rates),
309 return PTR_ERR(iter);
311 ret = ph->hops->iter_response_run(iter);
315 if (!clk->rate_discrete) {
316 dev_dbg(ph->dev, "Min %llu Max %llu Step %llu Hz\n",
317 clk->range.min_rate, clk->range.max_rate,
318 clk->range.step_size);
319 } else if (clk->list.num_rates) {
320 sort(clk->list.rates, clk->list.num_rates,
321 sizeof(clk->list.rates[0]), rate_cmp_func, NULL);
328 scmi_clock_rate_get(const struct scmi_protocol_handle *ph,
329 u32 clk_id, u64 *value)
334 ret = ph->xops->xfer_get_init(ph, CLOCK_RATE_GET,
335 sizeof(__le32), sizeof(u64), &t);
339 put_unaligned_le32(clk_id, t->tx.buf);
341 ret = ph->xops->do_xfer(ph, t);
343 *value = get_unaligned_le64(t->rx.buf);
345 ph->xops->xfer_put(ph, t);
349 static int scmi_clock_rate_set(const struct scmi_protocol_handle *ph,
350 u32 clk_id, u64 rate)
355 struct scmi_clock_set_rate *cfg;
356 struct clock_info *ci = ph->get_priv(ph);
358 ret = ph->xops->xfer_get_init(ph, CLOCK_RATE_SET, sizeof(*cfg), 0, &t);
362 if (ci->max_async_req &&
363 atomic_inc_return(&ci->cur_async_req) < ci->max_async_req)
364 flags |= CLOCK_SET_ASYNC;
367 cfg->flags = cpu_to_le32(flags);
368 cfg->id = cpu_to_le32(clk_id);
369 cfg->value_low = cpu_to_le32(rate & 0xffffffff);
370 cfg->value_high = cpu_to_le32(rate >> 32);
372 if (flags & CLOCK_SET_ASYNC) {
373 ret = ph->xops->do_xfer_with_response(ph, t);
375 struct scmi_msg_resp_set_rate_complete *resp;
378 if (le32_to_cpu(resp->id) == clk_id)
380 "Clk ID %d set async to %llu\n", clk_id,
381 get_unaligned_le64(&resp->rate_low));
386 ret = ph->xops->do_xfer(ph, t);
389 if (ci->max_async_req)
390 atomic_dec(&ci->cur_async_req);
392 ph->xops->xfer_put(ph, t);
397 scmi_clock_config_set(const struct scmi_protocol_handle *ph, u32 clk_id,
398 u32 config, bool atomic)
402 struct scmi_clock_set_config *cfg;
404 ret = ph->xops->xfer_get_init(ph, CLOCK_CONFIG_SET,
405 sizeof(*cfg), 0, &t);
409 t->hdr.poll_completion = atomic;
412 cfg->id = cpu_to_le32(clk_id);
413 cfg->attributes = cpu_to_le32(config);
415 ret = ph->xops->do_xfer(ph, t);
417 ph->xops->xfer_put(ph, t);
421 static int scmi_clock_enable(const struct scmi_protocol_handle *ph, u32 clk_id)
423 return scmi_clock_config_set(ph, clk_id, CLOCK_ENABLE, false);
426 static int scmi_clock_disable(const struct scmi_protocol_handle *ph, u32 clk_id)
428 return scmi_clock_config_set(ph, clk_id, 0, false);
431 static int scmi_clock_enable_atomic(const struct scmi_protocol_handle *ph,
434 return scmi_clock_config_set(ph, clk_id, CLOCK_ENABLE, true);
437 static int scmi_clock_disable_atomic(const struct scmi_protocol_handle *ph,
440 return scmi_clock_config_set(ph, clk_id, 0, true);
443 static int scmi_clock_count_get(const struct scmi_protocol_handle *ph)
445 struct clock_info *ci = ph->get_priv(ph);
447 return ci->num_clocks;
450 static const struct scmi_clock_info *
451 scmi_clock_info_get(const struct scmi_protocol_handle *ph, u32 clk_id)
453 struct clock_info *ci = ph->get_priv(ph);
454 struct scmi_clock_info *clk = ci->clk + clk_id;
462 static const struct scmi_clk_proto_ops clk_proto_ops = {
463 .count_get = scmi_clock_count_get,
464 .info_get = scmi_clock_info_get,
465 .rate_get = scmi_clock_rate_get,
466 .rate_set = scmi_clock_rate_set,
467 .enable = scmi_clock_enable,
468 .disable = scmi_clock_disable,
469 .enable_atomic = scmi_clock_enable_atomic,
470 .disable_atomic = scmi_clock_disable_atomic,
473 static int scmi_clk_rate_notify(const struct scmi_protocol_handle *ph,
474 u32 clk_id, int message_id, bool enable)
478 struct scmi_msg_clock_rate_notify *notify;
480 ret = ph->xops->xfer_get_init(ph, message_id, sizeof(*notify), 0, &t);
485 notify->clk_id = cpu_to_le32(clk_id);
486 notify->notify_enable = enable ? cpu_to_le32(BIT(0)) : 0;
488 ret = ph->xops->do_xfer(ph, t);
490 ph->xops->xfer_put(ph, t);
494 static int scmi_clk_set_notify_enabled(const struct scmi_protocol_handle *ph,
495 u8 evt_id, u32 src_id, bool enable)
499 if (evt_id >= ARRAY_SIZE(evt_2_cmd))
502 cmd_id = evt_2_cmd[evt_id];
503 ret = scmi_clk_rate_notify(ph, src_id, cmd_id, enable);
505 pr_debug("FAIL_ENABLED - evt[%X] dom[%d] - ret:%d\n",
506 evt_id, src_id, ret);
511 static void *scmi_clk_fill_custom_report(const struct scmi_protocol_handle *ph,
512 u8 evt_id, ktime_t timestamp,
513 const void *payld, size_t payld_sz,
514 void *report, u32 *src_id)
516 const struct scmi_clock_rate_notify_payld *p = payld;
517 struct scmi_clock_rate_notif_report *r = report;
519 if (sizeof(*p) != payld_sz ||
520 (evt_id != SCMI_EVENT_CLOCK_RATE_CHANGED &&
521 evt_id != SCMI_EVENT_CLOCK_RATE_CHANGE_REQUESTED))
524 r->timestamp = timestamp;
525 r->agent_id = le32_to_cpu(p->agent_id);
526 r->clock_id = le32_to_cpu(p->clock_id);
527 r->rate = get_unaligned_le64(&p->rate_low);
528 *src_id = r->clock_id;
533 static int scmi_clk_get_num_sources(const struct scmi_protocol_handle *ph)
535 struct clock_info *ci = ph->get_priv(ph);
540 return ci->num_clocks;
543 static const struct scmi_event clk_events[] = {
545 .id = SCMI_EVENT_CLOCK_RATE_CHANGED,
546 .max_payld_sz = sizeof(struct scmi_clock_rate_notify_payld),
547 .max_report_sz = sizeof(struct scmi_clock_rate_notif_report),
550 .id = SCMI_EVENT_CLOCK_RATE_CHANGE_REQUESTED,
551 .max_payld_sz = sizeof(struct scmi_clock_rate_notify_payld),
552 .max_report_sz = sizeof(struct scmi_clock_rate_notif_report),
556 static const struct scmi_event_ops clk_event_ops = {
557 .get_num_sources = scmi_clk_get_num_sources,
558 .set_notify_enabled = scmi_clk_set_notify_enabled,
559 .fill_custom_report = scmi_clk_fill_custom_report,
562 static const struct scmi_protocol_events clk_protocol_events = {
563 .queue_sz = SCMI_PROTO_QUEUE_SZ,
564 .ops = &clk_event_ops,
566 .num_events = ARRAY_SIZE(clk_events),
569 static int scmi_clock_protocol_init(const struct scmi_protocol_handle *ph)
573 struct clock_info *cinfo;
575 ret = ph->xops->version_get(ph, &version);
579 dev_dbg(ph->dev, "Clock Version %d.%d\n",
580 PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));
582 cinfo = devm_kzalloc(ph->dev, sizeof(*cinfo), GFP_KERNEL);
586 ret = scmi_clock_protocol_attributes_get(ph, cinfo);
590 cinfo->clk = devm_kcalloc(ph->dev, cinfo->num_clocks,
591 sizeof(*cinfo->clk), GFP_KERNEL);
595 for (clkid = 0; clkid < cinfo->num_clocks; clkid++) {
596 struct scmi_clock_info *clk = cinfo->clk + clkid;
598 ret = scmi_clock_attributes_get(ph, clkid, clk, version);
600 scmi_clock_describe_rates_get(ph, clkid, clk);
603 cinfo->version = version;
604 return ph->set_priv(ph, cinfo);
607 static const struct scmi_protocol scmi_clock = {
608 .id = SCMI_PROTOCOL_CLOCK,
609 .owner = THIS_MODULE,
610 .instance_init = &scmi_clock_protocol_init,
611 .ops = &clk_proto_ops,
612 .events = &clk_protocol_events,
615 DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(clock, scmi_clock)