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 8 #include <linux/math64.h> 9 #include <linux/module.h> 10 #include <linux/limits.h> 11 #include <linux/sort.h> 12 13 #include "protocols.h" 14 #include "notify.h" 15 #include "quirks.h" 16 17 /* Updated only after ALL the mandatory features for that version are merged */ 18 #define SCMI_PROTOCOL_SUPPORTED_VERSION 0x30000 19 20 enum scmi_clock_protocol_cmd { 21 CLOCK_ATTRIBUTES = 0x3, 22 CLOCK_DESCRIBE_RATES = 0x4, 23 CLOCK_RATE_SET = 0x5, 24 CLOCK_RATE_GET = 0x6, 25 CLOCK_CONFIG_SET = 0x7, 26 CLOCK_NAME_GET = 0x8, 27 CLOCK_RATE_NOTIFY = 0x9, 28 CLOCK_RATE_CHANGE_REQUESTED_NOTIFY = 0xA, 29 CLOCK_CONFIG_GET = 0xB, 30 CLOCK_POSSIBLE_PARENTS_GET = 0xC, 31 CLOCK_PARENT_SET = 0xD, 32 CLOCK_PARENT_GET = 0xE, 33 CLOCK_GET_PERMISSIONS = 0xF, 34 }; 35 36 #define CLOCK_STATE_CONTROL_ALLOWED BIT(31) 37 #define CLOCK_PARENT_CONTROL_ALLOWED BIT(30) 38 #define CLOCK_RATE_CONTROL_ALLOWED BIT(29) 39 40 enum clk_state { 41 CLK_STATE_DISABLE, 42 CLK_STATE_ENABLE, 43 CLK_STATE_RESERVED, 44 CLK_STATE_UNCHANGED, 45 }; 46 47 struct scmi_msg_resp_clock_protocol_attributes { 48 __le16 num_clocks; 49 u8 max_async_req; 50 u8 reserved; 51 }; 52 53 struct scmi_msg_resp_clock_attributes { 54 __le32 attributes; 55 #define SUPPORTS_RATE_CHANGED_NOTIF(x) ((x) & BIT(31)) 56 #define SUPPORTS_RATE_CHANGE_REQUESTED_NOTIF(x) ((x) & BIT(30)) 57 #define SUPPORTS_EXTENDED_NAMES(x) ((x) & BIT(29)) 58 #define SUPPORTS_PARENT_CLOCK(x) ((x) & BIT(28)) 59 #define SUPPORTS_EXTENDED_CONFIG(x) ((x) & BIT(27)) 60 #define SUPPORTS_GET_PERMISSIONS(x) ((x) & BIT(1)) 61 u8 name[SCMI_SHORT_NAME_MAX_SIZE]; 62 __le32 clock_enable_latency; 63 }; 64 65 struct scmi_msg_clock_possible_parents { 66 __le32 id; 67 __le32 skip_parents; 68 }; 69 70 struct scmi_msg_resp_clock_possible_parents { 71 __le32 num_parent_flags; 72 #define NUM_PARENTS_RETURNED(x) ((x) & 0xff) 73 #define NUM_PARENTS_REMAINING(x) ((x) >> 24) 74 __le32 possible_parents[]; 75 }; 76 77 struct scmi_msg_clock_set_parent { 78 __le32 id; 79 __le32 parent_id; 80 }; 81 82 struct scmi_msg_clock_config_set { 83 __le32 id; 84 __le32 attributes; 85 }; 86 87 /* Valid only from SCMI clock v2.1 */ 88 struct scmi_msg_clock_config_set_v2 { 89 __le32 id; 90 __le32 attributes; 91 #define NULL_OEM_TYPE 0 92 #define REGMASK_OEM_TYPE_SET GENMASK(23, 16) 93 #define REGMASK_CLK_STATE GENMASK(1, 0) 94 __le32 oem_config_val; 95 }; 96 97 struct scmi_msg_clock_config_get { 98 __le32 id; 99 __le32 flags; 100 #define REGMASK_OEM_TYPE_GET GENMASK(7, 0) 101 }; 102 103 struct scmi_msg_resp_clock_config_get { 104 __le32 attributes; 105 __le32 config; 106 #define IS_CLK_ENABLED(x) le32_get_bits((x), BIT(0)) 107 __le32 oem_config_val; 108 }; 109 110 struct scmi_msg_clock_describe_rates { 111 __le32 id; 112 __le32 rate_index; 113 }; 114 115 struct scmi_msg_resp_clock_describe_rates { 116 __le32 num_rates_flags; 117 #define NUM_RETURNED(x) ((x) & 0xfff) 118 #define RATE_DISCRETE(x) !((x) & BIT(12)) 119 #define NUM_REMAINING(x) ((x) >> 16) 120 struct { 121 __le32 value_low; 122 __le32 value_high; 123 } rate[]; 124 #define RATE_TO_U64(X) \ 125 ({ \ 126 typeof(X) x = (X); \ 127 le32_to_cpu((x).value_low) | (u64)le32_to_cpu((x).value_high) << 32; \ 128 }) 129 }; 130 131 struct scmi_clock_set_rate { 132 __le32 flags; 133 #define CLOCK_SET_ASYNC BIT(0) 134 #define CLOCK_SET_IGNORE_RESP BIT(1) 135 #define CLOCK_SET_ROUND_UP BIT(2) 136 #define CLOCK_SET_ROUND_AUTO BIT(3) 137 __le32 id; 138 __le32 value_low; 139 __le32 value_high; 140 }; 141 142 struct scmi_msg_resp_set_rate_complete { 143 __le32 id; 144 __le32 rate_low; 145 __le32 rate_high; 146 }; 147 148 struct scmi_msg_clock_rate_notify { 149 __le32 clk_id; 150 __le32 notify_enable; 151 }; 152 153 struct scmi_clock_rate_notify_payld { 154 __le32 agent_id; 155 __le32 clock_id; 156 __le32 rate_low; 157 __le32 rate_high; 158 }; 159 160 struct scmi_clock_desc { 161 u32 id; 162 unsigned int tot_rates; 163 struct scmi_clock_rates r; 164 #define RATE_MIN 0 165 #define RATE_MAX 1 166 #define RATE_STEP 2 167 struct scmi_clock_info info; 168 }; 169 170 #define to_desc(p) (container_of(p, struct scmi_clock_desc, info)) 171 172 struct clock_info { 173 int num_clocks; 174 int max_async_req; 175 bool notify_rate_changed_cmd; 176 bool notify_rate_change_requested_cmd; 177 atomic_t cur_async_req; 178 struct scmi_clock_desc *clkds; 179 #define CLOCK_INFO(c, i) (&(((c)->clkds + (i))->info)) 180 int (*clock_config_set)(const struct scmi_protocol_handle *ph, 181 u32 clk_id, enum clk_state state, 182 enum scmi_clock_oem_config oem_type, 183 u32 oem_val, bool atomic); 184 int (*clock_config_get)(const struct scmi_protocol_handle *ph, 185 u32 clk_id, enum scmi_clock_oem_config oem_type, 186 u32 *attributes, bool *enabled, u32 *oem_val, 187 bool atomic); 188 }; 189 190 static enum scmi_clock_protocol_cmd evt_2_cmd[] = { 191 CLOCK_RATE_NOTIFY, 192 CLOCK_RATE_CHANGE_REQUESTED_NOTIFY, 193 }; 194 195 static inline struct scmi_clock_info * 196 scmi_clock_domain_lookup(struct clock_info *ci, u32 clk_id) 197 { 198 if (clk_id >= ci->num_clocks) 199 return ERR_PTR(-EINVAL); 200 201 return CLOCK_INFO(ci, clk_id); 202 } 203 204 static int 205 scmi_clock_protocol_attributes_get(const struct scmi_protocol_handle *ph, 206 struct clock_info *ci) 207 { 208 int ret; 209 struct scmi_xfer *t; 210 struct scmi_msg_resp_clock_protocol_attributes *attr; 211 212 ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES, 213 0, sizeof(*attr), &t); 214 if (ret) 215 return ret; 216 217 attr = t->rx.buf; 218 219 ret = ph->xops->do_xfer(ph, t); 220 if (!ret) { 221 ci->num_clocks = le16_to_cpu(attr->num_clocks); 222 ci->max_async_req = attr->max_async_req; 223 } 224 225 ph->xops->xfer_put(ph, t); 226 227 if (!ret) { 228 if (!ph->hops->protocol_msg_check(ph, CLOCK_RATE_NOTIFY, NULL)) 229 ci->notify_rate_changed_cmd = true; 230 231 if (!ph->hops->protocol_msg_check(ph, 232 CLOCK_RATE_CHANGE_REQUESTED_NOTIFY, 233 NULL)) 234 ci->notify_rate_change_requested_cmd = true; 235 } 236 237 return ret; 238 } 239 240 struct scmi_clk_ipriv { 241 struct device *dev; 242 struct scmi_clock_desc *clkd; 243 }; 244 245 static void iter_clk_possible_parents_prepare_message(void *message, unsigned int desc_index, 246 const void *priv) 247 { 248 struct scmi_msg_clock_possible_parents *msg = message; 249 const struct scmi_clk_ipriv *p = priv; 250 251 msg->id = cpu_to_le32(p->clkd->id); 252 /* Set the number of OPPs to be skipped/already read */ 253 msg->skip_parents = cpu_to_le32(desc_index); 254 } 255 256 static int iter_clk_possible_parents_update_state(struct scmi_iterator_state *st, 257 const void *response, void *priv) 258 { 259 const struct scmi_msg_resp_clock_possible_parents *r = response; 260 struct scmi_clk_ipriv *p = priv; 261 u32 flags; 262 263 flags = le32_to_cpu(r->num_parent_flags); 264 st->num_returned = NUM_PARENTS_RETURNED(flags); 265 st->num_remaining = NUM_PARENTS_REMAINING(flags); 266 267 /* 268 * num parents is not declared previously anywhere so we 269 * assume it's returned+remaining on first call. 270 */ 271 if (!st->max_resources) { 272 int num_parents = st->num_returned + st->num_remaining; 273 274 p->clkd->info.parents = devm_kcalloc(p->dev, num_parents, 275 sizeof(*p->clkd->info.parents), 276 GFP_KERNEL); 277 if (!p->clkd->info.parents) 278 return -ENOMEM; 279 280 /* max_resources is used by the iterators to control bounds */ 281 st->max_resources = st->num_returned + st->num_remaining; 282 } 283 284 return 0; 285 } 286 287 static int iter_clk_possible_parents_process_response(const struct scmi_protocol_handle *ph, 288 const void *response, 289 struct scmi_iterator_state *st, 290 void *priv) 291 { 292 const struct scmi_msg_resp_clock_possible_parents *r = response; 293 struct scmi_clk_ipriv *p = priv; 294 295 p->clkd->info.parents[st->desc_index + st->loop_idx] = 296 le32_to_cpu(r->possible_parents[st->loop_idx]); 297 298 /* Count only effectively discovered parents */ 299 p->clkd->info.num_parents++; 300 301 return 0; 302 } 303 304 static int scmi_clock_possible_parents(const struct scmi_protocol_handle *ph, 305 u32 clk_id, struct clock_info *cinfo) 306 { 307 struct scmi_iterator_ops ops = { 308 .prepare_message = iter_clk_possible_parents_prepare_message, 309 .update_state = iter_clk_possible_parents_update_state, 310 .process_response = iter_clk_possible_parents_process_response, 311 }; 312 struct scmi_clock_desc *clkd = &cinfo->clkds[clk_id]; 313 struct scmi_clk_ipriv ppriv = { 314 .clkd = clkd, 315 .dev = ph->dev, 316 }; 317 void *iter; 318 319 iter = ph->hops->iter_response_init(ph, &ops, 0, 320 CLOCK_POSSIBLE_PARENTS_GET, 321 sizeof(struct scmi_msg_clock_possible_parents), 322 &ppriv); 323 if (IS_ERR(iter)) 324 return PTR_ERR(iter); 325 326 return ph->hops->iter_response_run(iter); 327 } 328 329 static int 330 scmi_clock_get_permissions(const struct scmi_protocol_handle *ph, u32 clk_id, 331 struct scmi_clock_info *clk) 332 { 333 struct scmi_xfer *t; 334 u32 perm; 335 int ret; 336 337 ret = ph->xops->xfer_get_init(ph, CLOCK_GET_PERMISSIONS, 338 sizeof(clk_id), sizeof(perm), &t); 339 if (ret) 340 return ret; 341 342 put_unaligned_le32(clk_id, t->tx.buf); 343 344 ret = ph->xops->do_xfer(ph, t); 345 if (!ret) { 346 perm = get_unaligned_le32(t->rx.buf); 347 348 clk->state_ctrl_forbidden = !(perm & CLOCK_STATE_CONTROL_ALLOWED); 349 clk->rate_ctrl_forbidden = !(perm & CLOCK_RATE_CONTROL_ALLOWED); 350 clk->parent_ctrl_forbidden = !(perm & CLOCK_PARENT_CONTROL_ALLOWED); 351 } 352 353 ph->xops->xfer_put(ph, t); 354 355 return ret; 356 } 357 358 static int scmi_clock_attributes_get(const struct scmi_protocol_handle *ph, 359 u32 clk_id, struct clock_info *cinfo) 360 { 361 int ret; 362 u32 attributes; 363 struct scmi_xfer *t; 364 struct scmi_msg_resp_clock_attributes *attr; 365 struct scmi_clock_info *clk = CLOCK_INFO(cinfo, clk_id); 366 367 ret = ph->xops->xfer_get_init(ph, CLOCK_ATTRIBUTES, 368 sizeof(clk_id), sizeof(*attr), &t); 369 if (ret) 370 return ret; 371 372 put_unaligned_le32(clk_id, t->tx.buf); 373 attr = t->rx.buf; 374 375 ret = ph->xops->do_xfer(ph, t); 376 if (!ret) { 377 u32 latency = 0; 378 379 attributes = le32_to_cpu(attr->attributes); 380 strscpy(clk->name, attr->name, SCMI_SHORT_NAME_MAX_SIZE); 381 /* clock_enable_latency field is present only since SCMI v3.1 */ 382 if (PROTOCOL_REV_MAJOR(ph->version) >= 0x2) 383 latency = le32_to_cpu(attr->clock_enable_latency); 384 clk->enable_latency = latency ? : U32_MAX; 385 } 386 387 ph->xops->xfer_put(ph, t); 388 389 /* 390 * If supported overwrite short name with the extended one; 391 * on error just carry on and use already provided short name. 392 */ 393 if (!ret && PROTOCOL_REV_MAJOR(ph->version) >= 0x2) { 394 if (SUPPORTS_EXTENDED_NAMES(attributes)) 395 ph->hops->extended_name_get(ph, CLOCK_NAME_GET, clk_id, 396 NULL, clk->name, 397 SCMI_MAX_STR_SIZE); 398 399 if (cinfo->notify_rate_changed_cmd && 400 SUPPORTS_RATE_CHANGED_NOTIF(attributes)) 401 clk->rate_changed_notifications = true; 402 if (cinfo->notify_rate_change_requested_cmd && 403 SUPPORTS_RATE_CHANGE_REQUESTED_NOTIF(attributes)) 404 clk->rate_change_requested_notifications = true; 405 if (PROTOCOL_REV_MAJOR(ph->version) >= 0x3) { 406 if (SUPPORTS_PARENT_CLOCK(attributes)) 407 scmi_clock_possible_parents(ph, clk_id, cinfo); 408 if (SUPPORTS_GET_PERMISSIONS(attributes)) 409 scmi_clock_get_permissions(ph, clk_id, clk); 410 if (SUPPORTS_EXTENDED_CONFIG(attributes)) 411 clk->extended_config = true; 412 } 413 } 414 415 return ret; 416 } 417 418 static int rate_cmp_func(const void *_r1, const void *_r2) 419 { 420 const u64 *r1 = _r1, *r2 = _r2; 421 422 if (*r1 < *r2) 423 return -1; 424 else if (*r1 == *r2) 425 return 0; 426 else 427 return 1; 428 } 429 430 static void iter_clk_describe_prepare_message(void *message, 431 const unsigned int desc_index, 432 const void *priv) 433 { 434 struct scmi_msg_clock_describe_rates *msg = message; 435 const struct scmi_clk_ipriv *p = priv; 436 437 msg->id = cpu_to_le32(p->clkd->id); 438 /* Set the number of rates to be skipped/already read */ 439 msg->rate_index = cpu_to_le32(desc_index); 440 } 441 442 #define QUIRK_OUT_OF_SPEC_TRIPLET \ 443 ({ \ 444 /* \ 445 * A known quirk: a triplet is returned but num_returned != 3 \ 446 * Check for a safe payload size and fix. \ 447 */ \ 448 if (st->num_returned != 3 && st->num_remaining == 0 && \ 449 st->rx_len == sizeof(*r) + sizeof(__le32) * 2 * 3) { \ 450 st->num_returned = 3; \ 451 st->num_remaining = 0; \ 452 } else { \ 453 dev_err(p->dev, \ 454 "Cannot fix out-of-spec reply !\n"); \ 455 return -EPROTO; \ 456 } \ 457 }) 458 459 static int 460 iter_clk_describe_update_state(struct scmi_iterator_state *st, 461 const void *response, void *priv) 462 { 463 u32 flags; 464 struct scmi_clk_ipriv *p = priv; 465 const struct scmi_msg_resp_clock_describe_rates *r = response; 466 467 flags = le32_to_cpu(r->num_rates_flags); 468 st->num_remaining = NUM_REMAINING(flags); 469 st->num_returned = NUM_RETURNED(flags); 470 p->clkd->r.rate_discrete = RATE_DISCRETE(flags); 471 472 /* Warn about out of spec replies ... */ 473 if (!p->clkd->r.rate_discrete && 474 (st->num_returned != 3 || st->num_remaining != 0)) { 475 dev_warn(p->dev, 476 "Out-of-spec CLOCK_DESCRIBE_RATES reply for %s - returned:%d remaining:%d rx_len:%zd\n", 477 p->clkd->info.name, st->num_returned, st->num_remaining, 478 st->rx_len); 479 480 SCMI_QUIRK(clock_rates_triplet_out_of_spec, 481 QUIRK_OUT_OF_SPEC_TRIPLET); 482 } 483 484 if (!st->max_resources) { 485 unsigned int tot_rates = st->num_returned + st->num_remaining; 486 487 p->clkd->r.rates = devm_kcalloc(p->dev, tot_rates, 488 sizeof(*p->clkd->r.rates), GFP_KERNEL); 489 if (!p->clkd->r.rates) 490 return -ENOMEM; 491 492 /* max_resources is used by the iterators to control bounds */ 493 p->clkd->tot_rates = tot_rates; 494 st->max_resources = tot_rates; 495 } 496 497 return 0; 498 } 499 500 static int 501 iter_clk_describe_process_response(const struct scmi_protocol_handle *ph, 502 const void *response, 503 struct scmi_iterator_state *st, void *priv) 504 { 505 struct scmi_clk_ipriv *p = priv; 506 const struct scmi_msg_resp_clock_describe_rates *r = response; 507 508 p->clkd->r.rates[p->clkd->r.num_rates] = RATE_TO_U64(r->rate[st->loop_idx]); 509 510 /* Count only effectively discovered rates */ 511 p->clkd->r.num_rates++; 512 513 return 0; 514 } 515 516 static int 517 scmi_clock_describe_rates_get_full(const struct scmi_protocol_handle *ph, 518 struct scmi_clock_desc *clkd) 519 { 520 int ret; 521 void *iter; 522 struct scmi_iterator_ops ops = { 523 .prepare_message = iter_clk_describe_prepare_message, 524 .update_state = iter_clk_describe_update_state, 525 .process_response = iter_clk_describe_process_response, 526 }; 527 struct scmi_clk_ipriv cpriv = { 528 .clkd = clkd, 529 .dev = ph->dev, 530 }; 531 532 /* 533 * Using tot_rates as max_resources parameter here so as to trigger 534 * the dynamic allocation only when strictly needed: when trying a 535 * full enumeration after a lazy one tot_rates will be non-zero. 536 */ 537 iter = ph->hops->iter_response_init(ph, &ops, clkd->tot_rates, 538 CLOCK_DESCRIBE_RATES, 539 sizeof(struct scmi_msg_clock_describe_rates), 540 &cpriv); 541 if (IS_ERR(iter)) 542 return PTR_ERR(iter); 543 544 ret = ph->hops->iter_response_run(iter); 545 if (ret) 546 return ret; 547 548 /* empty set ? */ 549 if (!clkd->r.num_rates) 550 return 0; 551 552 if (clkd->r.rate_discrete && PROTOCOL_REV_MAJOR(ph->version) == 0x1) 553 sort(clkd->r.rates, clkd->r.num_rates, 554 sizeof(clkd->r.rates[0]), rate_cmp_func, NULL); 555 556 return 0; 557 } 558 559 static int 560 scmi_clock_describe_rates_get_lazy(const struct scmi_protocol_handle *ph, 561 struct scmi_clock_desc *clkd) 562 { 563 struct scmi_iterator_ops ops = { 564 .prepare_message = iter_clk_describe_prepare_message, 565 .update_state = iter_clk_describe_update_state, 566 .process_response = iter_clk_describe_process_response, 567 }; 568 struct scmi_clk_ipriv cpriv = { 569 .clkd = clkd, 570 .dev = ph->dev, 571 }; 572 unsigned int first, last; 573 void *iter; 574 int ret; 575 576 iter = ph->hops->iter_response_init(ph, &ops, 0, CLOCK_DESCRIBE_RATES, 577 sizeof(struct scmi_msg_clock_describe_rates), 578 &cpriv); 579 if (IS_ERR(iter)) 580 return PTR_ERR(iter); 581 582 /* Try to grab a triplet, so that in case is NON-discrete we are done */ 583 first = 0; 584 last = 2; 585 ret = ph->hops->iter_response_run_bound(iter, &first, &last); 586 if (ret) 587 goto out; 588 589 /* 590 * If discrete and we don't already have it, grab the last value, which 591 * should be the max 592 */ 593 if (clkd->r.rate_discrete && clkd->tot_rates > clkd->r.num_rates) { 594 first = clkd->tot_rates - 1; 595 last = clkd->tot_rates - 1; 596 ret = ph->hops->iter_response_run_bound(iter, &first, &last); 597 } 598 599 out: 600 ph->hops->iter_response_bound_cleanup(iter); 601 602 return ret; 603 } 604 605 static int 606 scmi_clock_describe_rates_get(const struct scmi_protocol_handle *ph, 607 u32 clk_id, struct clock_info *cinfo) 608 { 609 struct scmi_clock_desc *clkd = &cinfo->clkds[clk_id]; 610 int ret; 611 612 /* 613 * Since only after SCMI Clock v1.0 the returned rates are guaranteed to 614 * be discovered in ascending order, lazy enumeration cannot be use for 615 * SCMI Clock v1.0 protocol. 616 */ 617 if (PROTOCOL_REV_MAJOR(ph->version) > 0x1) 618 ret = scmi_clock_describe_rates_get_lazy(ph, clkd); 619 else 620 ret = scmi_clock_describe_rates_get_full(ph, clkd); 621 622 if (ret) 623 return ret; 624 625 clkd->info.min_rate = clkd->r.rates[RATE_MIN]; 626 if (!clkd->r.rate_discrete) { 627 clkd->info.max_rate = clkd->r.rates[RATE_MAX]; 628 dev_dbg(ph->dev, "Min %llu Max %llu Step %llu Hz\n", 629 clkd->r.rates[RATE_MIN], clkd->r.rates[RATE_MAX], 630 clkd->r.rates[RATE_STEP]); 631 } else { 632 clkd->info.max_rate = clkd->r.rates[clkd->r.num_rates - 1]; 633 dev_dbg(ph->dev, "Clock:%s Num_Rates:%u -> Min %llu Max %llu\n", 634 clkd->info.name, clkd->tot_rates, 635 clkd->info.min_rate, clkd->info.max_rate); 636 } 637 638 return 0; 639 } 640 641 static int 642 scmi_clock_rate_get(const struct scmi_protocol_handle *ph, 643 u32 clk_id, u64 *value) 644 { 645 int ret; 646 struct scmi_xfer *t; 647 648 ret = ph->xops->xfer_get_init(ph, CLOCK_RATE_GET, 649 sizeof(__le32), sizeof(u64), &t); 650 if (ret) 651 return ret; 652 653 put_unaligned_le32(clk_id, t->tx.buf); 654 655 ret = ph->xops->do_xfer(ph, t); 656 if (!ret) 657 *value = get_unaligned_le64(t->rx.buf); 658 659 ph->xops->xfer_put(ph, t); 660 return ret; 661 } 662 663 static int scmi_clock_rate_set(const struct scmi_protocol_handle *ph, 664 u32 clk_id, u64 rate) 665 { 666 int ret; 667 u32 flags = 0; 668 struct scmi_xfer *t; 669 struct scmi_clock_set_rate *cfg; 670 struct clock_info *ci = ph->get_priv(ph); 671 struct scmi_clock_info *clk; 672 673 clk = scmi_clock_domain_lookup(ci, clk_id); 674 if (IS_ERR(clk)) 675 return PTR_ERR(clk); 676 677 if (clk->rate_ctrl_forbidden) 678 return -EACCES; 679 680 ret = ph->xops->xfer_get_init(ph, CLOCK_RATE_SET, sizeof(*cfg), 0, &t); 681 if (ret) 682 return ret; 683 684 if (ci->max_async_req && 685 atomic_inc_return(&ci->cur_async_req) < ci->max_async_req) 686 flags |= CLOCK_SET_ASYNC; 687 688 cfg = t->tx.buf; 689 cfg->flags = cpu_to_le32(flags); 690 cfg->id = cpu_to_le32(clk_id); 691 cfg->value_low = cpu_to_le32(rate & 0xffffffff); 692 cfg->value_high = cpu_to_le32(rate >> 32); 693 694 if (flags & CLOCK_SET_ASYNC) { 695 ret = ph->xops->do_xfer_with_response(ph, t); 696 if (!ret) { 697 struct scmi_msg_resp_set_rate_complete *resp; 698 699 resp = t->rx.buf; 700 if (le32_to_cpu(resp->id) == clk_id) 701 dev_dbg(ph->dev, 702 "Clk ID %d set async to %llu\n", clk_id, 703 get_unaligned_le64(&resp->rate_low)); 704 else 705 ret = -EPROTO; 706 } 707 } else { 708 ret = ph->xops->do_xfer(ph, t); 709 } 710 711 if (ci->max_async_req) 712 atomic_dec(&ci->cur_async_req); 713 714 ph->xops->xfer_put(ph, t); 715 return ret; 716 } 717 718 static int scmi_clock_determine_rate(const struct scmi_protocol_handle *ph, 719 u32 clk_id, unsigned long *rate) 720 { 721 u64 fmin, fmax, ftmp; 722 struct scmi_clock_info *clk; 723 struct scmi_clock_desc *clkd; 724 struct clock_info *ci = ph->get_priv(ph); 725 726 if (!rate) 727 return -EINVAL; 728 729 clk = scmi_clock_domain_lookup(ci, clk_id); 730 if (IS_ERR(clk)) 731 return PTR_ERR(clk); 732 733 clkd = to_desc(clk); 734 735 /* 736 * If we can't figure out what rate it will be, so just return the 737 * rate back to the caller. 738 */ 739 if (clkd->r.rate_discrete) 740 return 0; 741 742 fmin = clk->min_rate; 743 fmax = clk->max_rate; 744 if (*rate <= fmin) { 745 *rate = fmin; 746 return 0; 747 } else if (*rate >= fmax) { 748 *rate = fmax; 749 return 0; 750 } 751 752 ftmp = *rate - fmin; 753 ftmp += clkd->r.rates[RATE_STEP] - 1; /* to round up */ 754 ftmp = div64_ul(ftmp, clkd->r.rates[RATE_STEP]); 755 756 *rate = ftmp * clkd->r.rates[RATE_STEP] + fmin; 757 758 return 0; 759 } 760 761 static const struct scmi_clock_rates * 762 scmi_clock_all_rates_get(const struct scmi_protocol_handle *ph, u32 clk_id) 763 { 764 struct clock_info *ci = ph->get_priv(ph); 765 struct scmi_clock_desc *clkd; 766 struct scmi_clock_info *clk; 767 768 clk = scmi_clock_domain_lookup(ci, clk_id); 769 if (IS_ERR(clk) || !clk->name[0]) 770 return NULL; 771 772 clkd = to_desc(clk); 773 /* Needs full enumeration ? */ 774 if (clkd->r.rate_discrete && clkd->tot_rates != clkd->r.num_rates) { 775 int ret; 776 777 /* rates[] is already allocated BUT we need to re-enumerate */ 778 clkd->r.num_rates = 0; 779 ret = scmi_clock_describe_rates_get_full(ph, clkd); 780 if (ret) 781 return NULL; 782 } 783 784 return &clkd->r; 785 } 786 787 static int 788 scmi_clock_config_set(const struct scmi_protocol_handle *ph, u32 clk_id, 789 enum clk_state state, 790 enum scmi_clock_oem_config __unused0, u32 __unused1, 791 bool atomic) 792 { 793 int ret; 794 struct scmi_xfer *t; 795 struct scmi_msg_clock_config_set *cfg; 796 797 if (state >= CLK_STATE_RESERVED) 798 return -EINVAL; 799 800 ret = ph->xops->xfer_get_init(ph, CLOCK_CONFIG_SET, 801 sizeof(*cfg), 0, &t); 802 if (ret) 803 return ret; 804 805 t->hdr.poll_completion = atomic; 806 807 cfg = t->tx.buf; 808 cfg->id = cpu_to_le32(clk_id); 809 cfg->attributes = cpu_to_le32(state); 810 811 ret = ph->xops->do_xfer(ph, t); 812 813 ph->xops->xfer_put(ph, t); 814 return ret; 815 } 816 817 static int 818 scmi_clock_set_parent(const struct scmi_protocol_handle *ph, u32 clk_id, 819 u32 parent_id) 820 { 821 int ret; 822 struct scmi_xfer *t; 823 struct scmi_msg_clock_set_parent *cfg; 824 struct clock_info *ci = ph->get_priv(ph); 825 struct scmi_clock_info *clk; 826 827 clk = scmi_clock_domain_lookup(ci, clk_id); 828 if (IS_ERR(clk)) 829 return PTR_ERR(clk); 830 831 if (parent_id >= clk->num_parents) 832 return -EINVAL; 833 834 if (clk->parent_ctrl_forbidden) 835 return -EACCES; 836 837 ret = ph->xops->xfer_get_init(ph, CLOCK_PARENT_SET, 838 sizeof(*cfg), 0, &t); 839 if (ret) 840 return ret; 841 842 t->hdr.poll_completion = false; 843 844 cfg = t->tx.buf; 845 cfg->id = cpu_to_le32(clk_id); 846 cfg->parent_id = cpu_to_le32(clk->parents[parent_id]); 847 848 ret = ph->xops->do_xfer(ph, t); 849 850 ph->xops->xfer_put(ph, t); 851 852 return ret; 853 } 854 855 static int 856 scmi_clock_get_parent(const struct scmi_protocol_handle *ph, u32 clk_id, 857 u32 *parent_id) 858 { 859 int ret; 860 struct scmi_xfer *t; 861 862 ret = ph->xops->xfer_get_init(ph, CLOCK_PARENT_GET, 863 sizeof(__le32), sizeof(u32), &t); 864 if (ret) 865 return ret; 866 867 put_unaligned_le32(clk_id, t->tx.buf); 868 869 ret = ph->xops->do_xfer(ph, t); 870 if (!ret) 871 *parent_id = get_unaligned_le32(t->rx.buf); 872 873 ph->xops->xfer_put(ph, t); 874 return ret; 875 } 876 877 /* For SCMI clock v3.0 and onwards */ 878 static int 879 scmi_clock_config_set_v2(const struct scmi_protocol_handle *ph, u32 clk_id, 880 enum clk_state state, 881 enum scmi_clock_oem_config oem_type, u32 oem_val, 882 bool atomic) 883 { 884 int ret; 885 u32 attrs; 886 struct scmi_xfer *t; 887 struct scmi_msg_clock_config_set_v2 *cfg; 888 889 if (state == CLK_STATE_RESERVED || 890 (!oem_type && state == CLK_STATE_UNCHANGED)) 891 return -EINVAL; 892 893 ret = ph->xops->xfer_get_init(ph, CLOCK_CONFIG_SET, 894 sizeof(*cfg), 0, &t); 895 if (ret) 896 return ret; 897 898 t->hdr.poll_completion = atomic; 899 900 attrs = FIELD_PREP(REGMASK_OEM_TYPE_SET, oem_type) | 901 FIELD_PREP(REGMASK_CLK_STATE, state); 902 903 cfg = t->tx.buf; 904 cfg->id = cpu_to_le32(clk_id); 905 cfg->attributes = cpu_to_le32(attrs); 906 /* Clear in any case */ 907 cfg->oem_config_val = cpu_to_le32(0); 908 if (oem_type) 909 cfg->oem_config_val = cpu_to_le32(oem_val); 910 911 ret = ph->xops->do_xfer(ph, t); 912 913 ph->xops->xfer_put(ph, t); 914 return ret; 915 } 916 917 static int scmi_clock_enable(const struct scmi_protocol_handle *ph, u32 clk_id, 918 bool atomic) 919 { 920 struct clock_info *ci = ph->get_priv(ph); 921 struct scmi_clock_info *clk; 922 923 clk = scmi_clock_domain_lookup(ci, clk_id); 924 if (IS_ERR(clk)) 925 return PTR_ERR(clk); 926 927 if (clk->state_ctrl_forbidden) 928 return -EACCES; 929 930 return ci->clock_config_set(ph, clk_id, CLK_STATE_ENABLE, 931 NULL_OEM_TYPE, 0, atomic); 932 } 933 934 static int scmi_clock_disable(const struct scmi_protocol_handle *ph, u32 clk_id, 935 bool atomic) 936 { 937 struct clock_info *ci = ph->get_priv(ph); 938 struct scmi_clock_info *clk; 939 940 clk = scmi_clock_domain_lookup(ci, clk_id); 941 if (IS_ERR(clk)) 942 return PTR_ERR(clk); 943 944 if (clk->state_ctrl_forbidden) 945 return -EACCES; 946 947 return ci->clock_config_set(ph, clk_id, CLK_STATE_DISABLE, 948 NULL_OEM_TYPE, 0, atomic); 949 } 950 951 /* For SCMI clock v3.0 and onwards */ 952 static int 953 scmi_clock_config_get_v2(const struct scmi_protocol_handle *ph, u32 clk_id, 954 enum scmi_clock_oem_config oem_type, u32 *attributes, 955 bool *enabled, u32 *oem_val, bool atomic) 956 { 957 int ret; 958 u32 flags; 959 struct scmi_xfer *t; 960 struct scmi_msg_clock_config_get *cfg; 961 962 ret = ph->xops->xfer_get_init(ph, CLOCK_CONFIG_GET, 963 sizeof(*cfg), 0, &t); 964 if (ret) 965 return ret; 966 967 t->hdr.poll_completion = atomic; 968 969 flags = FIELD_PREP(REGMASK_OEM_TYPE_GET, oem_type); 970 971 cfg = t->tx.buf; 972 cfg->id = cpu_to_le32(clk_id); 973 cfg->flags = cpu_to_le32(flags); 974 975 ret = ph->xops->do_xfer(ph, t); 976 if (!ret) { 977 struct scmi_msg_resp_clock_config_get *resp = t->rx.buf; 978 979 if (attributes) 980 *attributes = le32_to_cpu(resp->attributes); 981 982 if (enabled) 983 *enabled = IS_CLK_ENABLED(resp->config); 984 985 if (oem_val && oem_type) 986 *oem_val = le32_to_cpu(resp->oem_config_val); 987 } 988 989 ph->xops->xfer_put(ph, t); 990 991 return ret; 992 } 993 994 static int 995 scmi_clock_config_get(const struct scmi_protocol_handle *ph, u32 clk_id, 996 enum scmi_clock_oem_config oem_type, u32 *attributes, 997 bool *enabled, u32 *oem_val, bool atomic) 998 { 999 int ret; 1000 struct scmi_xfer *t; 1001 struct scmi_msg_resp_clock_attributes *resp; 1002 1003 if (!enabled) 1004 return -EINVAL; 1005 1006 ret = ph->xops->xfer_get_init(ph, CLOCK_ATTRIBUTES, 1007 sizeof(clk_id), sizeof(*resp), &t); 1008 if (ret) 1009 return ret; 1010 1011 t->hdr.poll_completion = atomic; 1012 put_unaligned_le32(clk_id, t->tx.buf); 1013 resp = t->rx.buf; 1014 1015 ret = ph->xops->do_xfer(ph, t); 1016 if (!ret) 1017 *enabled = IS_CLK_ENABLED(resp->attributes); 1018 1019 ph->xops->xfer_put(ph, t); 1020 1021 return ret; 1022 } 1023 1024 static int scmi_clock_state_get(const struct scmi_protocol_handle *ph, 1025 u32 clk_id, bool *enabled, bool atomic) 1026 { 1027 struct clock_info *ci = ph->get_priv(ph); 1028 1029 return ci->clock_config_get(ph, clk_id, NULL_OEM_TYPE, NULL, 1030 enabled, NULL, atomic); 1031 } 1032 1033 static int scmi_clock_config_oem_set(const struct scmi_protocol_handle *ph, 1034 u32 clk_id, 1035 enum scmi_clock_oem_config oem_type, 1036 u32 oem_val, bool atomic) 1037 { 1038 struct clock_info *ci = ph->get_priv(ph); 1039 struct scmi_clock_info *clk; 1040 1041 clk = scmi_clock_domain_lookup(ci, clk_id); 1042 if (IS_ERR(clk)) 1043 return PTR_ERR(clk); 1044 1045 if (!clk->extended_config) 1046 return -EOPNOTSUPP; 1047 1048 return ci->clock_config_set(ph, clk_id, CLK_STATE_UNCHANGED, 1049 oem_type, oem_val, atomic); 1050 } 1051 1052 static int scmi_clock_config_oem_get(const struct scmi_protocol_handle *ph, 1053 u32 clk_id, 1054 enum scmi_clock_oem_config oem_type, 1055 u32 *oem_val, u32 *attributes, bool atomic) 1056 { 1057 struct clock_info *ci = ph->get_priv(ph); 1058 struct scmi_clock_info *clk; 1059 1060 clk = scmi_clock_domain_lookup(ci, clk_id); 1061 if (IS_ERR(clk)) 1062 return PTR_ERR(clk); 1063 1064 if (!clk->extended_config) 1065 return -EOPNOTSUPP; 1066 1067 return ci->clock_config_get(ph, clk_id, oem_type, attributes, 1068 NULL, oem_val, atomic); 1069 } 1070 1071 static int scmi_clock_count_get(const struct scmi_protocol_handle *ph) 1072 { 1073 struct clock_info *ci = ph->get_priv(ph); 1074 1075 return ci->num_clocks; 1076 } 1077 1078 static const struct scmi_clock_info * 1079 scmi_clock_info_get(const struct scmi_protocol_handle *ph, u32 clk_id) 1080 { 1081 struct scmi_clock_info *clk; 1082 struct clock_info *ci = ph->get_priv(ph); 1083 1084 clk = scmi_clock_domain_lookup(ci, clk_id); 1085 if (IS_ERR(clk)) 1086 return NULL; 1087 1088 if (!clk->name[0]) 1089 return NULL; 1090 1091 return clk; 1092 } 1093 1094 static const struct scmi_clk_proto_ops clk_proto_ops = { 1095 .count_get = scmi_clock_count_get, 1096 .info_get = scmi_clock_info_get, 1097 .rate_get = scmi_clock_rate_get, 1098 .rate_set = scmi_clock_rate_set, 1099 .determine_rate = scmi_clock_determine_rate, 1100 .all_rates_get = scmi_clock_all_rates_get, 1101 .enable = scmi_clock_enable, 1102 .disable = scmi_clock_disable, 1103 .state_get = scmi_clock_state_get, 1104 .config_oem_get = scmi_clock_config_oem_get, 1105 .config_oem_set = scmi_clock_config_oem_set, 1106 .parent_set = scmi_clock_set_parent, 1107 .parent_get = scmi_clock_get_parent, 1108 }; 1109 1110 static bool scmi_clk_notify_supported(const struct scmi_protocol_handle *ph, 1111 u8 evt_id, u32 src_id) 1112 { 1113 bool supported; 1114 struct scmi_clock_info *clk; 1115 struct clock_info *ci = ph->get_priv(ph); 1116 1117 if (evt_id >= ARRAY_SIZE(evt_2_cmd)) 1118 return false; 1119 1120 clk = scmi_clock_domain_lookup(ci, src_id); 1121 if (IS_ERR(clk)) 1122 return false; 1123 1124 if (evt_id == SCMI_EVENT_CLOCK_RATE_CHANGED) 1125 supported = clk->rate_changed_notifications; 1126 else 1127 supported = clk->rate_change_requested_notifications; 1128 1129 return supported; 1130 } 1131 1132 static int scmi_clk_rate_notify(const struct scmi_protocol_handle *ph, 1133 u32 clk_id, int message_id, bool enable) 1134 { 1135 int ret; 1136 struct scmi_xfer *t; 1137 struct scmi_msg_clock_rate_notify *notify; 1138 1139 ret = ph->xops->xfer_get_init(ph, message_id, sizeof(*notify), 0, &t); 1140 if (ret) 1141 return ret; 1142 1143 notify = t->tx.buf; 1144 notify->clk_id = cpu_to_le32(clk_id); 1145 notify->notify_enable = enable ? cpu_to_le32(BIT(0)) : 0; 1146 1147 ret = ph->xops->do_xfer(ph, t); 1148 1149 ph->xops->xfer_put(ph, t); 1150 return ret; 1151 } 1152 1153 static int scmi_clk_set_notify_enabled(const struct scmi_protocol_handle *ph, 1154 u8 evt_id, u32 src_id, bool enable) 1155 { 1156 int ret, cmd_id; 1157 1158 if (evt_id >= ARRAY_SIZE(evt_2_cmd)) 1159 return -EINVAL; 1160 1161 cmd_id = evt_2_cmd[evt_id]; 1162 ret = scmi_clk_rate_notify(ph, src_id, cmd_id, enable); 1163 if (ret) 1164 pr_debug("FAIL_ENABLED - evt[%X] dom[%d] - ret:%d\n", 1165 evt_id, src_id, ret); 1166 1167 return ret; 1168 } 1169 1170 static void *scmi_clk_fill_custom_report(const struct scmi_protocol_handle *ph, 1171 u8 evt_id, ktime_t timestamp, 1172 const void *payld, size_t payld_sz, 1173 void *report, u32 *src_id) 1174 { 1175 const struct scmi_clock_rate_notify_payld *p = payld; 1176 struct scmi_clock_rate_notif_report *r = report; 1177 1178 if (sizeof(*p) != payld_sz || 1179 (evt_id != SCMI_EVENT_CLOCK_RATE_CHANGED && 1180 evt_id != SCMI_EVENT_CLOCK_RATE_CHANGE_REQUESTED)) 1181 return NULL; 1182 1183 r->timestamp = timestamp; 1184 r->agent_id = le32_to_cpu(p->agent_id); 1185 r->clock_id = le32_to_cpu(p->clock_id); 1186 r->rate = get_unaligned_le64(&p->rate_low); 1187 *src_id = r->clock_id; 1188 1189 return r; 1190 } 1191 1192 static int scmi_clk_get_num_sources(const struct scmi_protocol_handle *ph) 1193 { 1194 struct clock_info *ci = ph->get_priv(ph); 1195 1196 if (!ci) 1197 return -EINVAL; 1198 1199 return ci->num_clocks; 1200 } 1201 1202 static const struct scmi_event clk_events[] = { 1203 { 1204 .id = SCMI_EVENT_CLOCK_RATE_CHANGED, 1205 .max_payld_sz = sizeof(struct scmi_clock_rate_notify_payld), 1206 .max_report_sz = sizeof(struct scmi_clock_rate_notif_report), 1207 }, 1208 { 1209 .id = SCMI_EVENT_CLOCK_RATE_CHANGE_REQUESTED, 1210 .max_payld_sz = sizeof(struct scmi_clock_rate_notify_payld), 1211 .max_report_sz = sizeof(struct scmi_clock_rate_notif_report), 1212 }, 1213 }; 1214 1215 static const struct scmi_event_ops clk_event_ops = { 1216 .is_notify_supported = scmi_clk_notify_supported, 1217 .get_num_sources = scmi_clk_get_num_sources, 1218 .set_notify_enabled = scmi_clk_set_notify_enabled, 1219 .fill_custom_report = scmi_clk_fill_custom_report, 1220 }; 1221 1222 static const struct scmi_protocol_events clk_protocol_events = { 1223 .queue_sz = SCMI_PROTO_QUEUE_SZ, 1224 .ops = &clk_event_ops, 1225 .evts = clk_events, 1226 .num_events = ARRAY_SIZE(clk_events), 1227 }; 1228 1229 static int scmi_clock_protocol_init(const struct scmi_protocol_handle *ph) 1230 { 1231 int clkid, ret; 1232 struct clock_info *cinfo; 1233 1234 dev_dbg(ph->dev, "Clock Version %d.%d\n", 1235 PROTOCOL_REV_MAJOR(ph->version), PROTOCOL_REV_MINOR(ph->version)); 1236 1237 cinfo = devm_kzalloc(ph->dev, sizeof(*cinfo), GFP_KERNEL); 1238 if (!cinfo) 1239 return -ENOMEM; 1240 1241 ret = scmi_clock_protocol_attributes_get(ph, cinfo); 1242 if (ret) 1243 return ret; 1244 1245 cinfo->clkds = devm_kcalloc(ph->dev, cinfo->num_clocks, 1246 sizeof(*cinfo->clkds), GFP_KERNEL); 1247 if (!cinfo->clkds) 1248 return -ENOMEM; 1249 1250 for (clkid = 0; clkid < cinfo->num_clocks; clkid++) { 1251 cinfo->clkds[clkid].id = clkid; 1252 ret = scmi_clock_attributes_get(ph, clkid, cinfo); 1253 if (!ret) 1254 scmi_clock_describe_rates_get(ph, clkid, cinfo); 1255 } 1256 1257 if (PROTOCOL_REV_MAJOR(ph->version) >= 0x3) { 1258 cinfo->clock_config_set = scmi_clock_config_set_v2; 1259 cinfo->clock_config_get = scmi_clock_config_get_v2; 1260 } else { 1261 cinfo->clock_config_set = scmi_clock_config_set; 1262 cinfo->clock_config_get = scmi_clock_config_get; 1263 } 1264 1265 return ph->set_priv(ph, cinfo); 1266 } 1267 1268 static const struct scmi_protocol scmi_clock = { 1269 .id = SCMI_PROTOCOL_CLOCK, 1270 .owner = THIS_MODULE, 1271 .instance_init = &scmi_clock_protocol_init, 1272 .ops = &clk_proto_ops, 1273 .events = &clk_protocol_events, 1274 .supported_version = SCMI_PROTOCOL_SUPPORTED_VERSION, 1275 }; 1276 1277 DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(clock, scmi_clock) 1278