1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * System Control and Management Interface (SCMI) Performance Protocol 4 * 5 * Copyright (C) 2018-2023 ARM Ltd. 6 */ 7 8 #define pr_fmt(fmt) "SCMI Notifications PERF - " fmt 9 10 #include <linux/bits.h> 11 #include <linux/hashtable.h> 12 #include <linux/io.h> 13 #include <linux/log2.h> 14 #include <linux/module.h> 15 #include <linux/of.h> 16 #include <linux/platform_device.h> 17 #include <linux/pm_opp.h> 18 #include <linux/scmi_protocol.h> 19 #include <linux/sort.h> 20 #include <linux/xarray.h> 21 22 #include <trace/events/scmi.h> 23 24 #include "protocols.h" 25 #include "notify.h" 26 27 #define MAX_OPPS 16 28 29 enum scmi_performance_protocol_cmd { 30 PERF_DOMAIN_ATTRIBUTES = 0x3, 31 PERF_DESCRIBE_LEVELS = 0x4, 32 PERF_LIMITS_SET = 0x5, 33 PERF_LIMITS_GET = 0x6, 34 PERF_LEVEL_SET = 0x7, 35 PERF_LEVEL_GET = 0x8, 36 PERF_NOTIFY_LIMITS = 0x9, 37 PERF_NOTIFY_LEVEL = 0xa, 38 PERF_DESCRIBE_FASTCHANNEL = 0xb, 39 PERF_DOMAIN_NAME_GET = 0xc, 40 }; 41 42 enum { 43 PERF_FC_LEVEL, 44 PERF_FC_LIMIT, 45 PERF_FC_MAX, 46 }; 47 48 struct scmi_opp { 49 u32 perf; 50 u32 power; 51 u32 trans_latency_us; 52 u32 indicative_freq; 53 u32 level_index; 54 struct hlist_node hash; 55 }; 56 57 struct scmi_msg_resp_perf_attributes { 58 __le16 num_domains; 59 __le16 flags; 60 #define POWER_SCALE_IN_MILLIWATT(x) ((x) & BIT(0)) 61 #define POWER_SCALE_IN_MICROWATT(x) ((x) & BIT(1)) 62 __le32 stats_addr_low; 63 __le32 stats_addr_high; 64 __le32 stats_size; 65 }; 66 67 struct scmi_msg_resp_perf_domain_attributes { 68 __le32 flags; 69 #define SUPPORTS_SET_LIMITS(x) ((x) & BIT(31)) 70 #define SUPPORTS_SET_PERF_LVL(x) ((x) & BIT(30)) 71 #define SUPPORTS_PERF_LIMIT_NOTIFY(x) ((x) & BIT(29)) 72 #define SUPPORTS_PERF_LEVEL_NOTIFY(x) ((x) & BIT(28)) 73 #define SUPPORTS_PERF_FASTCHANNELS(x) ((x) & BIT(27)) 74 #define SUPPORTS_EXTENDED_NAMES(x) ((x) & BIT(26)) 75 #define SUPPORTS_LEVEL_INDEXING(x) ((x) & BIT(25)) 76 __le32 rate_limit_us; 77 __le32 sustained_freq_khz; 78 __le32 sustained_perf_level; 79 u8 name[SCMI_SHORT_NAME_MAX_SIZE]; 80 }; 81 82 struct scmi_msg_perf_describe_levels { 83 __le32 domain; 84 __le32 level_index; 85 }; 86 87 struct scmi_perf_set_limits { 88 __le32 domain; 89 __le32 max_level; 90 __le32 min_level; 91 }; 92 93 struct scmi_perf_get_limits { 94 __le32 max_level; 95 __le32 min_level; 96 }; 97 98 struct scmi_perf_set_level { 99 __le32 domain; 100 __le32 level; 101 }; 102 103 struct scmi_perf_notify_level_or_limits { 104 __le32 domain; 105 __le32 notify_enable; 106 }; 107 108 struct scmi_perf_limits_notify_payld { 109 __le32 agent_id; 110 __le32 domain_id; 111 __le32 range_max; 112 __le32 range_min; 113 }; 114 115 struct scmi_perf_level_notify_payld { 116 __le32 agent_id; 117 __le32 domain_id; 118 __le32 performance_level; 119 }; 120 121 struct scmi_msg_resp_perf_describe_levels { 122 __le16 num_returned; 123 __le16 num_remaining; 124 struct { 125 __le32 perf_val; 126 __le32 power; 127 __le16 transition_latency_us; 128 __le16 reserved; 129 } opp[]; 130 }; 131 132 struct scmi_msg_resp_perf_describe_levels_v4 { 133 __le16 num_returned; 134 __le16 num_remaining; 135 struct { 136 __le32 perf_val; 137 __le32 power; 138 __le16 transition_latency_us; 139 __le16 reserved; 140 __le32 indicative_freq; 141 __le32 level_index; 142 } opp[]; 143 }; 144 145 struct perf_dom_info { 146 u32 id; 147 bool set_limits; 148 bool set_perf; 149 bool perf_limit_notify; 150 bool perf_level_notify; 151 bool perf_fastchannels; 152 bool level_indexing_mode; 153 u32 opp_count; 154 u32 sustained_freq_khz; 155 u32 sustained_perf_level; 156 u32 mult_factor; 157 char name[SCMI_MAX_STR_SIZE]; 158 struct scmi_opp opp[MAX_OPPS]; 159 struct scmi_fc_info *fc_info; 160 struct xarray opps_by_idx; 161 struct xarray opps_by_lvl; 162 DECLARE_HASHTABLE(opps_by_freq, ilog2(MAX_OPPS)); 163 }; 164 165 #define LOOKUP_BY_FREQ(__htp, __freq) \ 166 ({ \ 167 /* u32 cast is needed to pick right hash func */ \ 168 u32 f_ = (u32)(__freq); \ 169 struct scmi_opp *_opp; \ 170 \ 171 hash_for_each_possible((__htp), _opp, hash, f_) \ 172 if (_opp->indicative_freq == f_) \ 173 break; \ 174 _opp; \ 175 }) 176 177 struct scmi_perf_info { 178 u32 version; 179 u16 num_domains; 180 enum scmi_power_scale power_scale; 181 u64 stats_addr; 182 u32 stats_size; 183 struct perf_dom_info *dom_info; 184 }; 185 186 static enum scmi_performance_protocol_cmd evt_2_cmd[] = { 187 PERF_NOTIFY_LIMITS, 188 PERF_NOTIFY_LEVEL, 189 }; 190 191 static int scmi_perf_attributes_get(const struct scmi_protocol_handle *ph, 192 struct scmi_perf_info *pi) 193 { 194 int ret; 195 struct scmi_xfer *t; 196 struct scmi_msg_resp_perf_attributes *attr; 197 198 ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES, 0, 199 sizeof(*attr), &t); 200 if (ret) 201 return ret; 202 203 attr = t->rx.buf; 204 205 ret = ph->xops->do_xfer(ph, t); 206 if (!ret) { 207 u16 flags = le16_to_cpu(attr->flags); 208 209 pi->num_domains = le16_to_cpu(attr->num_domains); 210 211 if (POWER_SCALE_IN_MILLIWATT(flags)) 212 pi->power_scale = SCMI_POWER_MILLIWATTS; 213 if (PROTOCOL_REV_MAJOR(pi->version) >= 0x3) 214 if (POWER_SCALE_IN_MICROWATT(flags)) 215 pi->power_scale = SCMI_POWER_MICROWATTS; 216 217 pi->stats_addr = le32_to_cpu(attr->stats_addr_low) | 218 (u64)le32_to_cpu(attr->stats_addr_high) << 32; 219 pi->stats_size = le32_to_cpu(attr->stats_size); 220 } 221 222 ph->xops->xfer_put(ph, t); 223 return ret; 224 } 225 226 static void scmi_perf_xa_destroy(void *data) 227 { 228 int domain; 229 struct scmi_perf_info *pinfo = data; 230 231 for (domain = 0; domain < pinfo->num_domains; domain++) { 232 xa_destroy(&((pinfo->dom_info + domain)->opps_by_idx)); 233 xa_destroy(&((pinfo->dom_info + domain)->opps_by_lvl)); 234 } 235 } 236 237 static int 238 scmi_perf_domain_attributes_get(const struct scmi_protocol_handle *ph, 239 struct perf_dom_info *dom_info, 240 u32 version) 241 { 242 int ret; 243 u32 flags; 244 struct scmi_xfer *t; 245 struct scmi_msg_resp_perf_domain_attributes *attr; 246 247 ret = ph->xops->xfer_get_init(ph, PERF_DOMAIN_ATTRIBUTES, 248 sizeof(dom_info->id), sizeof(*attr), &t); 249 if (ret) 250 return ret; 251 252 put_unaligned_le32(dom_info->id, t->tx.buf); 253 attr = t->rx.buf; 254 255 ret = ph->xops->do_xfer(ph, t); 256 if (!ret) { 257 flags = le32_to_cpu(attr->flags); 258 259 dom_info->set_limits = SUPPORTS_SET_LIMITS(flags); 260 dom_info->set_perf = SUPPORTS_SET_PERF_LVL(flags); 261 dom_info->perf_limit_notify = SUPPORTS_PERF_LIMIT_NOTIFY(flags); 262 dom_info->perf_level_notify = SUPPORTS_PERF_LEVEL_NOTIFY(flags); 263 dom_info->perf_fastchannels = SUPPORTS_PERF_FASTCHANNELS(flags); 264 if (PROTOCOL_REV_MAJOR(version) >= 0x4) 265 dom_info->level_indexing_mode = 266 SUPPORTS_LEVEL_INDEXING(flags); 267 dom_info->sustained_freq_khz = 268 le32_to_cpu(attr->sustained_freq_khz); 269 dom_info->sustained_perf_level = 270 le32_to_cpu(attr->sustained_perf_level); 271 if (!dom_info->sustained_freq_khz || 272 !dom_info->sustained_perf_level) 273 /* CPUFreq converts to kHz, hence default 1000 */ 274 dom_info->mult_factor = 1000; 275 else 276 dom_info->mult_factor = 277 (dom_info->sustained_freq_khz * 1000) / 278 dom_info->sustained_perf_level; 279 strscpy(dom_info->name, attr->name, SCMI_SHORT_NAME_MAX_SIZE); 280 } 281 282 ph->xops->xfer_put(ph, t); 283 284 /* 285 * If supported overwrite short name with the extended one; 286 * on error just carry on and use already provided short name. 287 */ 288 if (!ret && PROTOCOL_REV_MAJOR(version) >= 0x3 && 289 SUPPORTS_EXTENDED_NAMES(flags)) 290 ph->hops->extended_name_get(ph, PERF_DOMAIN_NAME_GET, 291 dom_info->id, dom_info->name, 292 SCMI_MAX_STR_SIZE); 293 294 if (dom_info->level_indexing_mode) { 295 xa_init(&dom_info->opps_by_idx); 296 xa_init(&dom_info->opps_by_lvl); 297 hash_init(dom_info->opps_by_freq); 298 } 299 300 return ret; 301 } 302 303 static int opp_cmp_func(const void *opp1, const void *opp2) 304 { 305 const struct scmi_opp *t1 = opp1, *t2 = opp2; 306 307 return t1->perf - t2->perf; 308 } 309 310 struct scmi_perf_ipriv { 311 u32 version; 312 struct perf_dom_info *perf_dom; 313 }; 314 315 static void iter_perf_levels_prepare_message(void *message, 316 unsigned int desc_index, 317 const void *priv) 318 { 319 struct scmi_msg_perf_describe_levels *msg = message; 320 const struct scmi_perf_ipriv *p = priv; 321 322 msg->domain = cpu_to_le32(p->perf_dom->id); 323 /* Set the number of OPPs to be skipped/already read */ 324 msg->level_index = cpu_to_le32(desc_index); 325 } 326 327 static int iter_perf_levels_update_state(struct scmi_iterator_state *st, 328 const void *response, void *priv) 329 { 330 const struct scmi_msg_resp_perf_describe_levels *r = response; 331 332 st->num_returned = le16_to_cpu(r->num_returned); 333 st->num_remaining = le16_to_cpu(r->num_remaining); 334 335 return 0; 336 } 337 338 static inline void 339 process_response_opp(struct scmi_opp *opp, unsigned int loop_idx, 340 const struct scmi_msg_resp_perf_describe_levels *r) 341 { 342 opp->perf = le32_to_cpu(r->opp[loop_idx].perf_val); 343 opp->power = le32_to_cpu(r->opp[loop_idx].power); 344 opp->trans_latency_us = 345 le16_to_cpu(r->opp[loop_idx].transition_latency_us); 346 } 347 348 static inline void 349 process_response_opp_v4(struct perf_dom_info *dom, struct scmi_opp *opp, 350 unsigned int loop_idx, 351 const struct scmi_msg_resp_perf_describe_levels_v4 *r) 352 { 353 opp->perf = le32_to_cpu(r->opp[loop_idx].perf_val); 354 opp->power = le32_to_cpu(r->opp[loop_idx].power); 355 opp->trans_latency_us = 356 le16_to_cpu(r->opp[loop_idx].transition_latency_us); 357 358 /* Note that PERF v4 reports always five 32-bit words */ 359 opp->indicative_freq = le32_to_cpu(r->opp[loop_idx].indicative_freq); 360 if (dom->level_indexing_mode) { 361 opp->level_index = le32_to_cpu(r->opp[loop_idx].level_index); 362 363 xa_store(&dom->opps_by_idx, opp->level_index, opp, GFP_KERNEL); 364 xa_store(&dom->opps_by_lvl, opp->perf, opp, GFP_KERNEL); 365 hash_add(dom->opps_by_freq, &opp->hash, opp->indicative_freq); 366 } 367 } 368 369 static int 370 iter_perf_levels_process_response(const struct scmi_protocol_handle *ph, 371 const void *response, 372 struct scmi_iterator_state *st, void *priv) 373 { 374 struct scmi_opp *opp; 375 struct scmi_perf_ipriv *p = priv; 376 377 opp = &p->perf_dom->opp[st->desc_index + st->loop_idx]; 378 if (PROTOCOL_REV_MAJOR(p->version) <= 0x3) 379 process_response_opp(opp, st->loop_idx, response); 380 else 381 process_response_opp_v4(p->perf_dom, opp, st->loop_idx, 382 response); 383 p->perf_dom->opp_count++; 384 385 dev_dbg(ph->dev, "Level %d Power %d Latency %dus Ifreq %d Index %d\n", 386 opp->perf, opp->power, opp->trans_latency_us, 387 opp->indicative_freq, opp->level_index); 388 389 return 0; 390 } 391 392 static int 393 scmi_perf_describe_levels_get(const struct scmi_protocol_handle *ph, 394 struct perf_dom_info *perf_dom, u32 version) 395 { 396 int ret; 397 void *iter; 398 struct scmi_iterator_ops ops = { 399 .prepare_message = iter_perf_levels_prepare_message, 400 .update_state = iter_perf_levels_update_state, 401 .process_response = iter_perf_levels_process_response, 402 }; 403 struct scmi_perf_ipriv ppriv = { 404 .version = version, 405 .perf_dom = perf_dom, 406 }; 407 408 iter = ph->hops->iter_response_init(ph, &ops, MAX_OPPS, 409 PERF_DESCRIBE_LEVELS, 410 sizeof(struct scmi_msg_perf_describe_levels), 411 &ppriv); 412 if (IS_ERR(iter)) 413 return PTR_ERR(iter); 414 415 ret = ph->hops->iter_response_run(iter); 416 if (ret) 417 return ret; 418 419 if (perf_dom->opp_count) 420 sort(perf_dom->opp, perf_dom->opp_count, 421 sizeof(struct scmi_opp), opp_cmp_func, NULL); 422 423 return ret; 424 } 425 426 static int scmi_perf_msg_limits_set(const struct scmi_protocol_handle *ph, 427 u32 domain, u32 max_perf, u32 min_perf) 428 { 429 int ret; 430 struct scmi_xfer *t; 431 struct scmi_perf_set_limits *limits; 432 433 ret = ph->xops->xfer_get_init(ph, PERF_LIMITS_SET, 434 sizeof(*limits), 0, &t); 435 if (ret) 436 return ret; 437 438 limits = t->tx.buf; 439 limits->domain = cpu_to_le32(domain); 440 limits->max_level = cpu_to_le32(max_perf); 441 limits->min_level = cpu_to_le32(min_perf); 442 443 ret = ph->xops->do_xfer(ph, t); 444 445 ph->xops->xfer_put(ph, t); 446 return ret; 447 } 448 449 static inline struct perf_dom_info * 450 scmi_perf_domain_lookup(const struct scmi_protocol_handle *ph, u32 domain) 451 { 452 struct scmi_perf_info *pi = ph->get_priv(ph); 453 454 if (domain >= pi->num_domains) 455 return ERR_PTR(-EINVAL); 456 457 return pi->dom_info + domain; 458 } 459 460 static int __scmi_perf_limits_set(const struct scmi_protocol_handle *ph, 461 struct perf_dom_info *dom, u32 max_perf, 462 u32 min_perf) 463 { 464 if (dom->fc_info && dom->fc_info[PERF_FC_LIMIT].set_addr) { 465 struct scmi_fc_info *fci = &dom->fc_info[PERF_FC_LIMIT]; 466 467 trace_scmi_fc_call(SCMI_PROTOCOL_PERF, PERF_LIMITS_SET, 468 dom->id, min_perf, max_perf); 469 iowrite32(max_perf, fci->set_addr); 470 iowrite32(min_perf, fci->set_addr + 4); 471 ph->hops->fastchannel_db_ring(fci->set_db); 472 return 0; 473 } 474 475 return scmi_perf_msg_limits_set(ph, dom->id, max_perf, min_perf); 476 } 477 478 static int scmi_perf_limits_set(const struct scmi_protocol_handle *ph, 479 u32 domain, u32 max_perf, u32 min_perf) 480 { 481 struct scmi_perf_info *pi = ph->get_priv(ph); 482 struct perf_dom_info *dom; 483 484 dom = scmi_perf_domain_lookup(ph, domain); 485 if (IS_ERR(dom)) 486 return PTR_ERR(dom); 487 488 if (PROTOCOL_REV_MAJOR(pi->version) >= 0x3 && !max_perf && !min_perf) 489 return -EINVAL; 490 491 if (dom->level_indexing_mode) { 492 struct scmi_opp *opp; 493 494 if (min_perf) { 495 opp = xa_load(&dom->opps_by_lvl, min_perf); 496 if (!opp) 497 return -EIO; 498 499 min_perf = opp->level_index; 500 } 501 502 if (max_perf) { 503 opp = xa_load(&dom->opps_by_lvl, max_perf); 504 if (!opp) 505 return -EIO; 506 507 max_perf = opp->level_index; 508 } 509 } 510 511 return __scmi_perf_limits_set(ph, dom, max_perf, min_perf); 512 } 513 514 static int scmi_perf_msg_limits_get(const struct scmi_protocol_handle *ph, 515 u32 domain, u32 *max_perf, u32 *min_perf) 516 { 517 int ret; 518 struct scmi_xfer *t; 519 struct scmi_perf_get_limits *limits; 520 521 ret = ph->xops->xfer_get_init(ph, PERF_LIMITS_GET, 522 sizeof(__le32), 0, &t); 523 if (ret) 524 return ret; 525 526 put_unaligned_le32(domain, t->tx.buf); 527 528 ret = ph->xops->do_xfer(ph, t); 529 if (!ret) { 530 limits = t->rx.buf; 531 532 *max_perf = le32_to_cpu(limits->max_level); 533 *min_perf = le32_to_cpu(limits->min_level); 534 } 535 536 ph->xops->xfer_put(ph, t); 537 return ret; 538 } 539 540 static int __scmi_perf_limits_get(const struct scmi_protocol_handle *ph, 541 struct perf_dom_info *dom, u32 *max_perf, 542 u32 *min_perf) 543 { 544 if (dom->fc_info && dom->fc_info[PERF_FC_LIMIT].get_addr) { 545 struct scmi_fc_info *fci = &dom->fc_info[PERF_FC_LIMIT]; 546 547 *max_perf = ioread32(fci->get_addr); 548 *min_perf = ioread32(fci->get_addr + 4); 549 trace_scmi_fc_call(SCMI_PROTOCOL_PERF, PERF_LIMITS_GET, 550 dom->id, *min_perf, *max_perf); 551 return 0; 552 } 553 554 return scmi_perf_msg_limits_get(ph, dom->id, max_perf, min_perf); 555 } 556 557 static int scmi_perf_limits_get(const struct scmi_protocol_handle *ph, 558 u32 domain, u32 *max_perf, u32 *min_perf) 559 { 560 int ret; 561 struct perf_dom_info *dom; 562 563 dom = scmi_perf_domain_lookup(ph, domain); 564 if (IS_ERR(dom)) 565 return PTR_ERR(dom); 566 567 ret = __scmi_perf_limits_get(ph, dom, max_perf, min_perf); 568 if (ret) 569 return ret; 570 571 if (dom->level_indexing_mode) { 572 struct scmi_opp *opp; 573 574 opp = xa_load(&dom->opps_by_idx, *min_perf); 575 if (!opp) 576 return -EIO; 577 578 *min_perf = opp->perf; 579 580 opp = xa_load(&dom->opps_by_idx, *max_perf); 581 if (!opp) 582 return -EIO; 583 584 *max_perf = opp->perf; 585 } 586 587 return 0; 588 } 589 590 static int scmi_perf_msg_level_set(const struct scmi_protocol_handle *ph, 591 u32 domain, u32 level, bool poll) 592 { 593 int ret; 594 struct scmi_xfer *t; 595 struct scmi_perf_set_level *lvl; 596 597 ret = ph->xops->xfer_get_init(ph, PERF_LEVEL_SET, sizeof(*lvl), 0, &t); 598 if (ret) 599 return ret; 600 601 t->hdr.poll_completion = poll; 602 lvl = t->tx.buf; 603 lvl->domain = cpu_to_le32(domain); 604 lvl->level = cpu_to_le32(level); 605 606 ret = ph->xops->do_xfer(ph, t); 607 608 ph->xops->xfer_put(ph, t); 609 return ret; 610 } 611 612 static int __scmi_perf_level_set(const struct scmi_protocol_handle *ph, 613 struct perf_dom_info *dom, u32 level, 614 bool poll) 615 { 616 if (dom->fc_info && dom->fc_info[PERF_FC_LEVEL].set_addr) { 617 struct scmi_fc_info *fci = &dom->fc_info[PERF_FC_LEVEL]; 618 619 trace_scmi_fc_call(SCMI_PROTOCOL_PERF, PERF_LEVEL_SET, 620 dom->id, level, 0); 621 iowrite32(level, fci->set_addr); 622 ph->hops->fastchannel_db_ring(fci->set_db); 623 return 0; 624 } 625 626 return scmi_perf_msg_level_set(ph, dom->id, level, poll); 627 } 628 629 static int scmi_perf_level_set(const struct scmi_protocol_handle *ph, 630 u32 domain, u32 level, bool poll) 631 { 632 struct perf_dom_info *dom; 633 634 dom = scmi_perf_domain_lookup(ph, domain); 635 if (IS_ERR(dom)) 636 return PTR_ERR(dom); 637 638 if (dom->level_indexing_mode) { 639 struct scmi_opp *opp; 640 641 opp = xa_load(&dom->opps_by_lvl, level); 642 if (!opp) 643 return -EIO; 644 645 level = opp->level_index; 646 } 647 648 return __scmi_perf_level_set(ph, dom, level, poll); 649 } 650 651 static int scmi_perf_msg_level_get(const struct scmi_protocol_handle *ph, 652 u32 domain, u32 *level, bool poll) 653 { 654 int ret; 655 struct scmi_xfer *t; 656 657 ret = ph->xops->xfer_get_init(ph, PERF_LEVEL_GET, 658 sizeof(u32), sizeof(u32), &t); 659 if (ret) 660 return ret; 661 662 t->hdr.poll_completion = poll; 663 put_unaligned_le32(domain, t->tx.buf); 664 665 ret = ph->xops->do_xfer(ph, t); 666 if (!ret) 667 *level = get_unaligned_le32(t->rx.buf); 668 669 ph->xops->xfer_put(ph, t); 670 return ret; 671 } 672 673 static int __scmi_perf_level_get(const struct scmi_protocol_handle *ph, 674 struct perf_dom_info *dom, u32 *level, 675 bool poll) 676 { 677 if (dom->fc_info && dom->fc_info[PERF_FC_LEVEL].get_addr) { 678 *level = ioread32(dom->fc_info[PERF_FC_LEVEL].get_addr); 679 trace_scmi_fc_call(SCMI_PROTOCOL_PERF, PERF_LEVEL_GET, 680 dom->id, *level, 0); 681 return 0; 682 } 683 684 return scmi_perf_msg_level_get(ph, dom->id, level, poll); 685 } 686 687 static int scmi_perf_level_get(const struct scmi_protocol_handle *ph, 688 u32 domain, u32 *level, bool poll) 689 { 690 int ret; 691 struct perf_dom_info *dom; 692 693 dom = scmi_perf_domain_lookup(ph, domain); 694 if (IS_ERR(dom)) 695 return PTR_ERR(dom); 696 697 ret = __scmi_perf_level_get(ph, dom, level, poll); 698 if (ret) 699 return ret; 700 701 if (dom->level_indexing_mode) { 702 struct scmi_opp *opp; 703 704 opp = xa_load(&dom->opps_by_idx, *level); 705 if (!opp) 706 return -EIO; 707 708 *level = opp->perf; 709 } 710 711 return 0; 712 } 713 714 static int scmi_perf_level_limits_notify(const struct scmi_protocol_handle *ph, 715 u32 domain, int message_id, 716 bool enable) 717 { 718 int ret; 719 struct scmi_xfer *t; 720 struct scmi_perf_notify_level_or_limits *notify; 721 722 ret = ph->xops->xfer_get_init(ph, message_id, sizeof(*notify), 0, &t); 723 if (ret) 724 return ret; 725 726 notify = t->tx.buf; 727 notify->domain = cpu_to_le32(domain); 728 notify->notify_enable = enable ? cpu_to_le32(BIT(0)) : 0; 729 730 ret = ph->xops->do_xfer(ph, t); 731 732 ph->xops->xfer_put(ph, t); 733 return ret; 734 } 735 736 static void scmi_perf_domain_init_fc(const struct scmi_protocol_handle *ph, 737 u32 domain, struct scmi_fc_info **p_fc) 738 { 739 struct scmi_fc_info *fc; 740 741 fc = devm_kcalloc(ph->dev, PERF_FC_MAX, sizeof(*fc), GFP_KERNEL); 742 if (!fc) 743 return; 744 745 ph->hops->fastchannel_init(ph, PERF_DESCRIBE_FASTCHANNEL, 746 PERF_LEVEL_SET, 4, domain, 747 &fc[PERF_FC_LEVEL].set_addr, 748 &fc[PERF_FC_LEVEL].set_db); 749 750 ph->hops->fastchannel_init(ph, PERF_DESCRIBE_FASTCHANNEL, 751 PERF_LEVEL_GET, 4, domain, 752 &fc[PERF_FC_LEVEL].get_addr, NULL); 753 754 ph->hops->fastchannel_init(ph, PERF_DESCRIBE_FASTCHANNEL, 755 PERF_LIMITS_SET, 8, domain, 756 &fc[PERF_FC_LIMIT].set_addr, 757 &fc[PERF_FC_LIMIT].set_db); 758 759 ph->hops->fastchannel_init(ph, PERF_DESCRIBE_FASTCHANNEL, 760 PERF_LIMITS_GET, 8, domain, 761 &fc[PERF_FC_LIMIT].get_addr, NULL); 762 763 *p_fc = fc; 764 } 765 766 /* Device specific ops */ 767 static int scmi_dev_domain_id(struct device *dev) 768 { 769 struct of_phandle_args clkspec; 770 771 if (of_parse_phandle_with_args(dev->of_node, "clocks", "#clock-cells", 772 0, &clkspec)) 773 return -EINVAL; 774 775 return clkspec.args[0]; 776 } 777 778 static int scmi_dvfs_device_opps_add(const struct scmi_protocol_handle *ph, 779 struct device *dev) 780 { 781 int idx, ret, domain; 782 unsigned long freq; 783 struct scmi_opp *opp; 784 struct perf_dom_info *dom; 785 786 domain = scmi_dev_domain_id(dev); 787 if (domain < 0) 788 return -EINVAL; 789 790 dom = scmi_perf_domain_lookup(ph, domain); 791 if (IS_ERR(dom)) 792 return PTR_ERR(dom); 793 794 for (opp = dom->opp, idx = 0; idx < dom->opp_count; idx++, opp++) { 795 if (!dom->level_indexing_mode) 796 freq = opp->perf * dom->mult_factor; 797 else 798 freq = opp->indicative_freq * 1000; 799 800 ret = dev_pm_opp_add(dev, freq, 0); 801 if (ret) { 802 dev_warn(dev, "failed to add opp %luHz\n", freq); 803 804 while (idx-- > 0) { 805 if (!dom->level_indexing_mode) 806 freq = (--opp)->perf * dom->mult_factor; 807 else 808 freq = (--opp)->indicative_freq * 1000; 809 dev_pm_opp_remove(dev, freq); 810 } 811 return ret; 812 } 813 814 dev_dbg(dev, "[%d][%s]:: Registered OPP[%d] %lu\n", 815 domain, dom->name, idx, freq); 816 } 817 return 0; 818 } 819 820 static int 821 scmi_dvfs_transition_latency_get(const struct scmi_protocol_handle *ph, 822 struct device *dev) 823 { 824 int domain; 825 struct perf_dom_info *dom; 826 827 domain = scmi_dev_domain_id(dev); 828 if (domain < 0) 829 return -EINVAL; 830 831 dom = scmi_perf_domain_lookup(ph, domain); 832 if (IS_ERR(dom)) 833 return PTR_ERR(dom); 834 835 /* uS to nS */ 836 return dom->opp[dom->opp_count - 1].trans_latency_us * 1000; 837 } 838 839 static int scmi_dvfs_freq_set(const struct scmi_protocol_handle *ph, u32 domain, 840 unsigned long freq, bool poll) 841 { 842 unsigned int level; 843 struct perf_dom_info *dom; 844 845 dom = scmi_perf_domain_lookup(ph, domain); 846 if (IS_ERR(dom)) 847 return PTR_ERR(dom); 848 849 if (!dom->level_indexing_mode) { 850 level = freq / dom->mult_factor; 851 } else { 852 struct scmi_opp *opp; 853 854 opp = LOOKUP_BY_FREQ(dom->opps_by_freq, freq / 1000); 855 if (!opp) 856 return -EIO; 857 858 level = opp->level_index; 859 } 860 861 return __scmi_perf_level_set(ph, dom, level, poll); 862 } 863 864 static int scmi_dvfs_freq_get(const struct scmi_protocol_handle *ph, u32 domain, 865 unsigned long *freq, bool poll) 866 { 867 int ret; 868 u32 level; 869 struct perf_dom_info *dom; 870 871 dom = scmi_perf_domain_lookup(ph, domain); 872 if (IS_ERR(dom)) 873 return PTR_ERR(dom); 874 875 ret = __scmi_perf_level_get(ph, dom, &level, poll); 876 if (ret) 877 return ret; 878 879 if (!dom->level_indexing_mode) { 880 *freq = level * dom->mult_factor; 881 } else { 882 struct scmi_opp *opp; 883 884 opp = xa_load(&dom->opps_by_idx, level); 885 if (!opp) 886 return -EIO; 887 888 *freq = opp->indicative_freq * 1000; 889 } 890 891 return ret; 892 } 893 894 static int scmi_dvfs_est_power_get(const struct scmi_protocol_handle *ph, 895 u32 domain, unsigned long *freq, 896 unsigned long *power) 897 { 898 struct perf_dom_info *dom; 899 unsigned long opp_freq; 900 int idx, ret = -EINVAL; 901 struct scmi_opp *opp; 902 903 dom = scmi_perf_domain_lookup(ph, domain); 904 if (IS_ERR(dom)) 905 return PTR_ERR(dom); 906 907 for (opp = dom->opp, idx = 0; idx < dom->opp_count; idx++, opp++) { 908 if (!dom->level_indexing_mode) 909 opp_freq = opp->perf * dom->mult_factor; 910 else 911 opp_freq = opp->indicative_freq * 1000; 912 913 if (opp_freq < *freq) 914 continue; 915 916 *freq = opp_freq; 917 *power = opp->power; 918 ret = 0; 919 break; 920 } 921 922 return ret; 923 } 924 925 static bool scmi_fast_switch_possible(const struct scmi_protocol_handle *ph, 926 struct device *dev) 927 { 928 int domain; 929 struct perf_dom_info *dom; 930 931 domain = scmi_dev_domain_id(dev); 932 if (domain < 0) 933 return false; 934 935 dom = scmi_perf_domain_lookup(ph, domain); 936 if (IS_ERR(dom)) 937 return false; 938 939 return dom->fc_info && dom->fc_info[PERF_FC_LEVEL].set_addr; 940 } 941 942 static enum scmi_power_scale 943 scmi_power_scale_get(const struct scmi_protocol_handle *ph) 944 { 945 struct scmi_perf_info *pi = ph->get_priv(ph); 946 947 return pi->power_scale; 948 } 949 950 static const struct scmi_perf_proto_ops perf_proto_ops = { 951 .limits_set = scmi_perf_limits_set, 952 .limits_get = scmi_perf_limits_get, 953 .level_set = scmi_perf_level_set, 954 .level_get = scmi_perf_level_get, 955 .device_domain_id = scmi_dev_domain_id, 956 .transition_latency_get = scmi_dvfs_transition_latency_get, 957 .device_opps_add = scmi_dvfs_device_opps_add, 958 .freq_set = scmi_dvfs_freq_set, 959 .freq_get = scmi_dvfs_freq_get, 960 .est_power_get = scmi_dvfs_est_power_get, 961 .fast_switch_possible = scmi_fast_switch_possible, 962 .power_scale_get = scmi_power_scale_get, 963 }; 964 965 static int scmi_perf_set_notify_enabled(const struct scmi_protocol_handle *ph, 966 u8 evt_id, u32 src_id, bool enable) 967 { 968 int ret, cmd_id; 969 970 if (evt_id >= ARRAY_SIZE(evt_2_cmd)) 971 return -EINVAL; 972 973 cmd_id = evt_2_cmd[evt_id]; 974 ret = scmi_perf_level_limits_notify(ph, src_id, cmd_id, enable); 975 if (ret) 976 pr_debug("FAIL_ENABLED - evt[%X] dom[%d] - ret:%d\n", 977 evt_id, src_id, ret); 978 979 return ret; 980 } 981 982 static void *scmi_perf_fill_custom_report(const struct scmi_protocol_handle *ph, 983 u8 evt_id, ktime_t timestamp, 984 const void *payld, size_t payld_sz, 985 void *report, u32 *src_id) 986 { 987 void *rep = NULL; 988 989 switch (evt_id) { 990 case SCMI_EVENT_PERFORMANCE_LIMITS_CHANGED: 991 { 992 const struct scmi_perf_limits_notify_payld *p = payld; 993 struct scmi_perf_limits_report *r = report; 994 995 if (sizeof(*p) != payld_sz) 996 break; 997 998 r->timestamp = timestamp; 999 r->agent_id = le32_to_cpu(p->agent_id); 1000 r->domain_id = le32_to_cpu(p->domain_id); 1001 r->range_max = le32_to_cpu(p->range_max); 1002 r->range_min = le32_to_cpu(p->range_min); 1003 *src_id = r->domain_id; 1004 rep = r; 1005 break; 1006 } 1007 case SCMI_EVENT_PERFORMANCE_LEVEL_CHANGED: 1008 { 1009 const struct scmi_perf_level_notify_payld *p = payld; 1010 struct scmi_perf_level_report *r = report; 1011 1012 if (sizeof(*p) != payld_sz) 1013 break; 1014 1015 r->timestamp = timestamp; 1016 r->agent_id = le32_to_cpu(p->agent_id); 1017 r->domain_id = le32_to_cpu(p->domain_id); 1018 r->performance_level = le32_to_cpu(p->performance_level); 1019 *src_id = r->domain_id; 1020 rep = r; 1021 break; 1022 } 1023 default: 1024 break; 1025 } 1026 1027 return rep; 1028 } 1029 1030 static int scmi_perf_get_num_sources(const struct scmi_protocol_handle *ph) 1031 { 1032 struct scmi_perf_info *pi = ph->get_priv(ph); 1033 1034 if (!pi) 1035 return -EINVAL; 1036 1037 return pi->num_domains; 1038 } 1039 1040 static const struct scmi_event perf_events[] = { 1041 { 1042 .id = SCMI_EVENT_PERFORMANCE_LIMITS_CHANGED, 1043 .max_payld_sz = sizeof(struct scmi_perf_limits_notify_payld), 1044 .max_report_sz = sizeof(struct scmi_perf_limits_report), 1045 }, 1046 { 1047 .id = SCMI_EVENT_PERFORMANCE_LEVEL_CHANGED, 1048 .max_payld_sz = sizeof(struct scmi_perf_level_notify_payld), 1049 .max_report_sz = sizeof(struct scmi_perf_level_report), 1050 }, 1051 }; 1052 1053 static const struct scmi_event_ops perf_event_ops = { 1054 .get_num_sources = scmi_perf_get_num_sources, 1055 .set_notify_enabled = scmi_perf_set_notify_enabled, 1056 .fill_custom_report = scmi_perf_fill_custom_report, 1057 }; 1058 1059 static const struct scmi_protocol_events perf_protocol_events = { 1060 .queue_sz = SCMI_PROTO_QUEUE_SZ, 1061 .ops = &perf_event_ops, 1062 .evts = perf_events, 1063 .num_events = ARRAY_SIZE(perf_events), 1064 }; 1065 1066 static int scmi_perf_protocol_init(const struct scmi_protocol_handle *ph) 1067 { 1068 int domain, ret; 1069 u32 version; 1070 struct scmi_perf_info *pinfo; 1071 1072 ret = ph->xops->version_get(ph, &version); 1073 if (ret) 1074 return ret; 1075 1076 dev_dbg(ph->dev, "Performance Version %d.%d\n", 1077 PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version)); 1078 1079 pinfo = devm_kzalloc(ph->dev, sizeof(*pinfo), GFP_KERNEL); 1080 if (!pinfo) 1081 return -ENOMEM; 1082 1083 pinfo->version = version; 1084 1085 ret = scmi_perf_attributes_get(ph, pinfo); 1086 if (ret) 1087 return ret; 1088 1089 pinfo->dom_info = devm_kcalloc(ph->dev, pinfo->num_domains, 1090 sizeof(*pinfo->dom_info), GFP_KERNEL); 1091 if (!pinfo->dom_info) 1092 return -ENOMEM; 1093 1094 for (domain = 0; domain < pinfo->num_domains; domain++) { 1095 struct perf_dom_info *dom = pinfo->dom_info + domain; 1096 1097 dom->id = domain; 1098 scmi_perf_domain_attributes_get(ph, dom, version); 1099 scmi_perf_describe_levels_get(ph, dom, version); 1100 1101 if (dom->perf_fastchannels) 1102 scmi_perf_domain_init_fc(ph, dom->id, &dom->fc_info); 1103 } 1104 1105 ret = devm_add_action_or_reset(ph->dev, scmi_perf_xa_destroy, pinfo); 1106 if (ret) 1107 return ret; 1108 1109 return ph->set_priv(ph, pinfo); 1110 } 1111 1112 static const struct scmi_protocol scmi_perf = { 1113 .id = SCMI_PROTOCOL_PERF, 1114 .owner = THIS_MODULE, 1115 .instance_init = &scmi_perf_protocol_init, 1116 .ops = &perf_proto_ops, 1117 .events = &perf_protocol_events, 1118 }; 1119 1120 DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(perf, scmi_perf) 1121