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