| 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * System Control and Power Interface (SCMI) based CPUFreq Interface driver |
| 4 | * |
| 5 | * Copyright (C) 2018-2021 ARM Ltd. |
| 6 | * Sudeep Holla <sudeep.holla@arm.com> |
| 7 | */ |
| 8 | |
| 9 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 10 | |
| 11 | #include <linux/clk-provider.h> |
| 12 | #include <linux/cpu.h> |
| 13 | #include <linux/cpufreq.h> |
| 14 | #include <linux/cpumask.h> |
| 15 | #include <linux/energy_model.h> |
| 16 | #include <linux/export.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/of.h> |
| 19 | #include <linux/pm_opp.h> |
| 20 | #include <linux/pm_qos.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/scmi_protocol.h> |
| 23 | #include <linux/types.h> |
| 24 | #include <linux/units.h> |
| 25 | |
| 26 | struct scmi_data { |
| 27 | int domain_id; |
| 28 | int nr_opp; |
| 29 | struct device *cpu_dev; |
| 30 | cpumask_var_t opp_shared_cpus; |
| 31 | struct notifier_block limit_notify_nb; |
| 32 | struct freq_qos_request limits_freq_req; |
| 33 | }; |
| 34 | |
| 35 | static struct scmi_protocol_handle *ph; |
| 36 | static const struct scmi_perf_proto_ops *perf_ops; |
| 37 | static struct cpufreq_driver scmi_cpufreq_driver; |
| 38 | |
| 39 | static unsigned int scmi_cpufreq_get_rate(unsigned int cpu) |
| 40 | { |
| 41 | struct cpufreq_policy *policy; |
| 42 | struct scmi_data *priv; |
| 43 | unsigned long rate; |
| 44 | int ret; |
| 45 | |
| 46 | policy = cpufreq_cpu_get_raw(cpu); |
| 47 | if (unlikely(!policy)) |
| 48 | return 0; |
| 49 | |
| 50 | priv = policy->driver_data; |
| 51 | |
| 52 | ret = perf_ops->freq_get(ph, priv->domain_id, &rate, false); |
| 53 | if (ret) |
| 54 | return 0; |
| 55 | return rate / 1000; |
| 56 | } |
| 57 | |
| 58 | /* |
| 59 | * perf_ops->freq_set is not a synchronous, the actual OPP change will |
| 60 | * happen asynchronously and can get notified if the events are |
| 61 | * subscribed for by the SCMI firmware |
| 62 | */ |
| 63 | static int |
| 64 | scmi_cpufreq_set_target(struct cpufreq_policy *policy, unsigned int index) |
| 65 | { |
| 66 | struct scmi_data *priv = policy->driver_data; |
| 67 | u64 freq = policy->freq_table[index].frequency; |
| 68 | |
| 69 | return perf_ops->freq_set(ph, priv->domain_id, freq * 1000, false); |
| 70 | } |
| 71 | |
| 72 | static unsigned int scmi_cpufreq_fast_switch(struct cpufreq_policy *policy, |
| 73 | unsigned int target_freq) |
| 74 | { |
| 75 | struct scmi_data *priv = policy->driver_data; |
| 76 | unsigned long freq = target_freq; |
| 77 | |
| 78 | if (!perf_ops->freq_set(ph, priv->domain_id, freq * 1000, true)) |
| 79 | return target_freq; |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | static int scmi_cpu_domain_id(struct device *cpu_dev) |
| 85 | { |
| 86 | struct device_node *np = cpu_dev->of_node; |
| 87 | struct of_phandle_args domain_id; |
| 88 | int index; |
| 89 | |
| 90 | if (of_parse_phandle_with_args(np, list_name: "clocks" , cells_name: "#clock-cells" , index: 0, |
| 91 | out_args: &domain_id)) { |
| 92 | /* Find the corresponding index for power-domain "perf". */ |
| 93 | index = of_property_match_string(np, propname: "power-domain-names" , |
| 94 | string: "perf" ); |
| 95 | if (index < 0) |
| 96 | return -EINVAL; |
| 97 | |
| 98 | if (of_parse_phandle_with_args(np, list_name: "power-domains" , |
| 99 | cells_name: "#power-domain-cells" , index, |
| 100 | out_args: &domain_id)) |
| 101 | return -EINVAL; |
| 102 | } |
| 103 | |
| 104 | return domain_id.args[0]; |
| 105 | } |
| 106 | |
| 107 | static int |
| 108 | scmi_get_sharing_cpus(struct device *cpu_dev, int domain, |
| 109 | struct cpumask *cpumask) |
| 110 | { |
| 111 | int cpu, tdomain; |
| 112 | struct device *tcpu_dev; |
| 113 | |
| 114 | for_each_present_cpu(cpu) { |
| 115 | if (cpu == cpu_dev->id) |
| 116 | continue; |
| 117 | |
| 118 | tcpu_dev = get_cpu_device(cpu); |
| 119 | if (!tcpu_dev) |
| 120 | continue; |
| 121 | |
| 122 | tdomain = scmi_cpu_domain_id(cpu_dev: tcpu_dev); |
| 123 | if (tdomain == domain) |
| 124 | cpumask_set_cpu(cpu, dstp: cpumask); |
| 125 | } |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | static int __maybe_unused |
| 131 | scmi_get_cpu_power(struct device *cpu_dev, unsigned long *power, |
| 132 | unsigned long *KHz) |
| 133 | { |
| 134 | enum scmi_power_scale power_scale = perf_ops->power_scale_get(ph); |
| 135 | unsigned long Hz; |
| 136 | int ret, domain; |
| 137 | |
| 138 | domain = scmi_cpu_domain_id(cpu_dev); |
| 139 | if (domain < 0) |
| 140 | return domain; |
| 141 | |
| 142 | /* Get the power cost of the performance domain. */ |
| 143 | Hz = *KHz * 1000; |
| 144 | ret = perf_ops->est_power_get(ph, domain, &Hz, power); |
| 145 | if (ret) |
| 146 | return ret; |
| 147 | |
| 148 | /* Convert the power to uW if it is mW (ignore bogoW) */ |
| 149 | if (power_scale == SCMI_POWER_MILLIWATTS) |
| 150 | *power *= MICROWATT_PER_MILLIWATT; |
| 151 | |
| 152 | /* The EM framework specifies the frequency in KHz. */ |
| 153 | *KHz = Hz / 1000; |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | static int |
| 159 | scmi_get_rate_limit(u32 domain, bool has_fast_switch) |
| 160 | { |
| 161 | int ret, rate_limit; |
| 162 | |
| 163 | if (has_fast_switch) { |
| 164 | /* |
| 165 | * Fast channels are used whenever available, |
| 166 | * so use their rate_limit value if populated. |
| 167 | */ |
| 168 | ret = perf_ops->fast_switch_rate_limit(ph, domain, |
| 169 | &rate_limit); |
| 170 | if (!ret && rate_limit) |
| 171 | return rate_limit; |
| 172 | } |
| 173 | |
| 174 | ret = perf_ops->rate_limit_get(ph, domain, &rate_limit); |
| 175 | if (ret) |
| 176 | return 0; |
| 177 | |
| 178 | return rate_limit; |
| 179 | } |
| 180 | |
| 181 | static int scmi_limit_notify_cb(struct notifier_block *nb, unsigned long event, void *data) |
| 182 | { |
| 183 | struct scmi_data *priv = container_of(nb, struct scmi_data, limit_notify_nb); |
| 184 | struct scmi_perf_limits_report *limit_notify = data; |
| 185 | unsigned int limit_freq_khz; |
| 186 | int ret; |
| 187 | |
| 188 | limit_freq_khz = limit_notify->range_max_freq / HZ_PER_KHZ; |
| 189 | |
| 190 | ret = freq_qos_update_request(req: &priv->limits_freq_req, new_value: limit_freq_khz); |
| 191 | if (ret < 0) |
| 192 | pr_warn("failed to update freq constraint: %d\n" , ret); |
| 193 | |
| 194 | return NOTIFY_OK; |
| 195 | } |
| 196 | |
| 197 | static int scmi_cpufreq_init(struct cpufreq_policy *policy) |
| 198 | { |
| 199 | int ret, nr_opp, domain; |
| 200 | unsigned int latency; |
| 201 | struct device *cpu_dev; |
| 202 | struct scmi_data *priv; |
| 203 | struct cpufreq_frequency_table *freq_table; |
| 204 | struct scmi_device *sdev = cpufreq_get_driver_data(); |
| 205 | |
| 206 | cpu_dev = get_cpu_device(cpu: policy->cpu); |
| 207 | if (!cpu_dev) { |
| 208 | pr_err("failed to get cpu%d device\n" , policy->cpu); |
| 209 | return -ENODEV; |
| 210 | } |
| 211 | |
| 212 | domain = scmi_cpu_domain_id(cpu_dev); |
| 213 | if (domain < 0) |
| 214 | return domain; |
| 215 | |
| 216 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 217 | if (!priv) |
| 218 | return -ENOMEM; |
| 219 | |
| 220 | if (!zalloc_cpumask_var(mask: &priv->opp_shared_cpus, GFP_KERNEL)) { |
| 221 | ret = -ENOMEM; |
| 222 | goto out_free_priv; |
| 223 | } |
| 224 | |
| 225 | /* Obtain CPUs that share SCMI performance controls */ |
| 226 | ret = scmi_get_sharing_cpus(cpu_dev, domain, cpumask: policy->cpus); |
| 227 | if (ret) { |
| 228 | dev_warn(cpu_dev, "failed to get sharing cpumask\n" ); |
| 229 | goto out_free_cpumask; |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | * Obtain CPUs that share performance levels. |
| 234 | * The OPP 'sharing cpus' info may come from DT through an empty opp |
| 235 | * table and opp-shared. |
| 236 | */ |
| 237 | ret = dev_pm_opp_of_get_sharing_cpus(cpu_dev, cpumask: priv->opp_shared_cpus); |
| 238 | if (ret || cpumask_empty(srcp: priv->opp_shared_cpus)) { |
| 239 | /* |
| 240 | * Either opp-table is not set or no opp-shared was found. |
| 241 | * Use the CPU mask from SCMI to designate CPUs sharing an OPP |
| 242 | * table. |
| 243 | */ |
| 244 | cpumask_copy(dstp: priv->opp_shared_cpus, srcp: policy->cpus); |
| 245 | } |
| 246 | |
| 247 | /* |
| 248 | * A previous CPU may have marked OPPs as shared for a few CPUs, based on |
| 249 | * what OPP core provided. If the current CPU is part of those few, then |
| 250 | * there is no need to add OPPs again. |
| 251 | */ |
| 252 | nr_opp = dev_pm_opp_get_opp_count(dev: cpu_dev); |
| 253 | if (nr_opp <= 0) { |
| 254 | ret = perf_ops->device_opps_add(ph, cpu_dev, domain); |
| 255 | if (ret) { |
| 256 | dev_warn(cpu_dev, "failed to add opps to the device\n" ); |
| 257 | goto out_free_cpumask; |
| 258 | } |
| 259 | |
| 260 | nr_opp = dev_pm_opp_get_opp_count(dev: cpu_dev); |
| 261 | if (nr_opp <= 0) { |
| 262 | dev_err(cpu_dev, "%s: No OPPs for this device: %d\n" , |
| 263 | __func__, nr_opp); |
| 264 | |
| 265 | ret = -ENODEV; |
| 266 | goto out_free_opp; |
| 267 | } |
| 268 | |
| 269 | ret = dev_pm_opp_set_sharing_cpus(cpu_dev, cpumask: priv->opp_shared_cpus); |
| 270 | if (ret) { |
| 271 | dev_err(cpu_dev, "%s: failed to mark OPPs as shared: %d\n" , |
| 272 | __func__, ret); |
| 273 | |
| 274 | goto out_free_opp; |
| 275 | } |
| 276 | |
| 277 | priv->nr_opp = nr_opp; |
| 278 | } |
| 279 | |
| 280 | ret = dev_pm_opp_init_cpufreq_table(dev: cpu_dev, table: &freq_table); |
| 281 | if (ret) { |
| 282 | dev_err(cpu_dev, "failed to init cpufreq table: %d\n" , ret); |
| 283 | goto out_free_opp; |
| 284 | } |
| 285 | |
| 286 | priv->cpu_dev = cpu_dev; |
| 287 | priv->domain_id = domain; |
| 288 | |
| 289 | policy->driver_data = priv; |
| 290 | policy->freq_table = freq_table; |
| 291 | |
| 292 | /* SCMI allows DVFS request for any domain from any CPU */ |
| 293 | policy->dvfs_possible_from_any_cpu = true; |
| 294 | |
| 295 | latency = perf_ops->transition_latency_get(ph, domain); |
| 296 | if (!latency) |
| 297 | latency = CPUFREQ_DEFAULT_TRANSITION_LATENCY_NS; |
| 298 | |
| 299 | policy->cpuinfo.transition_latency = latency; |
| 300 | |
| 301 | policy->fast_switch_possible = |
| 302 | perf_ops->fast_switch_possible(ph, domain); |
| 303 | |
| 304 | policy->transition_delay_us = |
| 305 | scmi_get_rate_limit(domain, has_fast_switch: policy->fast_switch_possible); |
| 306 | |
| 307 | ret = freq_qos_add_request(qos: &policy->constraints, req: &priv->limits_freq_req, type: FREQ_QOS_MAX, |
| 308 | FREQ_QOS_MAX_DEFAULT_VALUE); |
| 309 | if (ret < 0) { |
| 310 | dev_err(cpu_dev, "failed to add qos limits request: %d\n" , ret); |
| 311 | goto out_free_table; |
| 312 | } |
| 313 | |
| 314 | priv->limit_notify_nb.notifier_call = scmi_limit_notify_cb; |
| 315 | ret = sdev->handle->notify_ops->event_notifier_register(sdev->handle, SCMI_PROTOCOL_PERF, |
| 316 | SCMI_EVENT_PERFORMANCE_LIMITS_CHANGED, |
| 317 | &priv->domain_id, |
| 318 | &priv->limit_notify_nb); |
| 319 | if (ret) |
| 320 | dev_warn(&sdev->dev, |
| 321 | "failed to register for limits change notifier for domain %d\n" , |
| 322 | priv->domain_id); |
| 323 | |
| 324 | return 0; |
| 325 | |
| 326 | out_free_table: |
| 327 | dev_pm_opp_free_cpufreq_table(dev: cpu_dev, table: &freq_table); |
| 328 | out_free_opp: |
| 329 | dev_pm_opp_remove_all_dynamic(dev: cpu_dev); |
| 330 | |
| 331 | out_free_cpumask: |
| 332 | free_cpumask_var(mask: priv->opp_shared_cpus); |
| 333 | |
| 334 | out_free_priv: |
| 335 | kfree(objp: priv); |
| 336 | |
| 337 | return ret; |
| 338 | } |
| 339 | |
| 340 | static void scmi_cpufreq_exit(struct cpufreq_policy *policy) |
| 341 | { |
| 342 | struct scmi_data *priv = policy->driver_data; |
| 343 | struct scmi_device *sdev = cpufreq_get_driver_data(); |
| 344 | |
| 345 | sdev->handle->notify_ops->event_notifier_unregister(sdev->handle, SCMI_PROTOCOL_PERF, |
| 346 | SCMI_EVENT_PERFORMANCE_LIMITS_CHANGED, |
| 347 | &priv->domain_id, |
| 348 | &priv->limit_notify_nb); |
| 349 | freq_qos_remove_request(req: &priv->limits_freq_req); |
| 350 | dev_pm_opp_free_cpufreq_table(dev: priv->cpu_dev, table: &policy->freq_table); |
| 351 | dev_pm_opp_remove_all_dynamic(dev: priv->cpu_dev); |
| 352 | free_cpumask_var(mask: priv->opp_shared_cpus); |
| 353 | kfree(objp: priv); |
| 354 | } |
| 355 | |
| 356 | static void scmi_cpufreq_register_em(struct cpufreq_policy *policy) |
| 357 | { |
| 358 | struct em_data_callback em_cb = EM_DATA_CB(scmi_get_cpu_power); |
| 359 | enum scmi_power_scale power_scale = perf_ops->power_scale_get(ph); |
| 360 | struct scmi_data *priv = policy->driver_data; |
| 361 | bool em_power_scale = false; |
| 362 | |
| 363 | /* |
| 364 | * This callback will be called for each policy, but we don't need to |
| 365 | * register with EM every time. Despite not being part of the same |
| 366 | * policy, some CPUs may still share their perf-domains, and a CPU from |
| 367 | * another policy may already have registered with EM on behalf of CPUs |
| 368 | * of this policy. |
| 369 | */ |
| 370 | if (!priv->nr_opp) |
| 371 | return; |
| 372 | |
| 373 | if (power_scale == SCMI_POWER_MILLIWATTS |
| 374 | || power_scale == SCMI_POWER_MICROWATTS) |
| 375 | em_power_scale = true; |
| 376 | |
| 377 | em_dev_register_perf_domain(dev: get_cpu_device(cpu: policy->cpu), nr_states: priv->nr_opp, |
| 378 | cb: &em_cb, cpus: priv->opp_shared_cpus, |
| 379 | microwatts: em_power_scale); |
| 380 | } |
| 381 | |
| 382 | static struct cpufreq_driver scmi_cpufreq_driver = { |
| 383 | .name = "scmi" , |
| 384 | .flags = CPUFREQ_HAVE_GOVERNOR_PER_POLICY | |
| 385 | CPUFREQ_NEED_INITIAL_FREQ_CHECK | |
| 386 | CPUFREQ_IS_COOLING_DEV, |
| 387 | .verify = cpufreq_generic_frequency_table_verify, |
| 388 | .target_index = scmi_cpufreq_set_target, |
| 389 | .fast_switch = scmi_cpufreq_fast_switch, |
| 390 | .get = scmi_cpufreq_get_rate, |
| 391 | .init = scmi_cpufreq_init, |
| 392 | .exit = scmi_cpufreq_exit, |
| 393 | .register_em = scmi_cpufreq_register_em, |
| 394 | .set_boost = cpufreq_boost_set_sw, |
| 395 | }; |
| 396 | |
| 397 | static bool scmi_dev_used_by_cpus(struct device *scmi_dev) |
| 398 | { |
| 399 | struct device_node *scmi_np = dev_of_node(dev: scmi_dev); |
| 400 | struct device_node *cpu_np, *np; |
| 401 | struct device *cpu_dev; |
| 402 | int cpu, idx; |
| 403 | |
| 404 | if (!scmi_np) |
| 405 | return false; |
| 406 | |
| 407 | for_each_possible_cpu(cpu) { |
| 408 | cpu_dev = get_cpu_device(cpu); |
| 409 | if (!cpu_dev) |
| 410 | continue; |
| 411 | |
| 412 | cpu_np = dev_of_node(dev: cpu_dev); |
| 413 | |
| 414 | np = of_parse_phandle(np: cpu_np, phandle_name: "clocks" , index: 0); |
| 415 | of_node_put(node: np); |
| 416 | |
| 417 | if (np == scmi_np) |
| 418 | return true; |
| 419 | |
| 420 | idx = of_property_match_string(np: cpu_np, propname: "power-domain-names" , string: "perf" ); |
| 421 | np = of_parse_phandle(np: cpu_np, phandle_name: "power-domains" , index: idx); |
| 422 | of_node_put(node: np); |
| 423 | |
| 424 | if (np == scmi_np) |
| 425 | return true; |
| 426 | } |
| 427 | |
| 428 | /* |
| 429 | * Older Broadcom STB chips had a "clocks" property for CPU node(s) |
| 430 | * that did not match the SCMI performance protocol node, if we got |
| 431 | * there, it means we had such an older Device Tree, therefore return |
| 432 | * true to preserve backwards compatibility. |
| 433 | */ |
| 434 | if (of_machine_is_compatible(compat: "brcm,brcmstb" )) |
| 435 | return true; |
| 436 | |
| 437 | return false; |
| 438 | } |
| 439 | |
| 440 | static int scmi_cpufreq_probe(struct scmi_device *sdev) |
| 441 | { |
| 442 | int ret; |
| 443 | struct device *dev = &sdev->dev; |
| 444 | const struct scmi_handle *handle; |
| 445 | |
| 446 | handle = sdev->handle; |
| 447 | |
| 448 | if (!handle || !scmi_dev_used_by_cpus(scmi_dev: dev)) |
| 449 | return -ENODEV; |
| 450 | |
| 451 | scmi_cpufreq_driver.driver_data = sdev; |
| 452 | |
| 453 | perf_ops = handle->devm_protocol_get(sdev, SCMI_PROTOCOL_PERF, &ph); |
| 454 | if (IS_ERR(ptr: perf_ops)) |
| 455 | return PTR_ERR(ptr: perf_ops); |
| 456 | |
| 457 | #ifdef CONFIG_COMMON_CLK |
| 458 | /* dummy clock provider as needed by OPP if clocks property is used */ |
| 459 | if (of_property_present(np: dev->of_node, propname: "#clock-cells" )) { |
| 460 | ret = devm_of_clk_add_hw_provider(dev, get: of_clk_hw_simple_get, NULL); |
| 461 | if (ret) |
| 462 | return dev_err_probe(dev, err: ret, fmt: "%s: registering clock provider failed\n" , __func__); |
| 463 | } |
| 464 | #endif |
| 465 | |
| 466 | ret = cpufreq_register_driver(driver_data: &scmi_cpufreq_driver); |
| 467 | if (ret) { |
| 468 | dev_err(dev, "%s: registering cpufreq failed, err: %d\n" , |
| 469 | __func__, ret); |
| 470 | } |
| 471 | |
| 472 | return ret; |
| 473 | } |
| 474 | |
| 475 | static void scmi_cpufreq_remove(struct scmi_device *sdev) |
| 476 | { |
| 477 | cpufreq_unregister_driver(driver_data: &scmi_cpufreq_driver); |
| 478 | } |
| 479 | |
| 480 | static const struct scmi_device_id scmi_id_table[] = { |
| 481 | { SCMI_PROTOCOL_PERF, "cpufreq" }, |
| 482 | { }, |
| 483 | }; |
| 484 | MODULE_DEVICE_TABLE(scmi, scmi_id_table); |
| 485 | |
| 486 | static struct scmi_driver scmi_cpufreq_drv = { |
| 487 | .name = "scmi-cpufreq" , |
| 488 | .probe = scmi_cpufreq_probe, |
| 489 | .remove = scmi_cpufreq_remove, |
| 490 | .id_table = scmi_id_table, |
| 491 | }; |
| 492 | module_scmi_driver(scmi_cpufreq_drv); |
| 493 | |
| 494 | MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>" ); |
| 495 | MODULE_DESCRIPTION("ARM SCMI CPUFreq interface driver" ); |
| 496 | MODULE_LICENSE("GPL v2" ); |
| 497 | |