Lines Matching +full:switching +full:- +full:freq

1 // SPDX-License-Identifier: GPL-2.0-only
6 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
9 * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
11 * Feb 2006 - Jacob Shin <jacob.shin@amd.com>
12 * Fix handling for CPU hotplug -- affected CPUs
56 * The "cpufreq driver" - the arch- or hardware-dependent low
75 return cpufreq_driver->target_index || cpufreq_driver->target; in has_target()
110 return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY); in have_governor_per_policy()
119 return &policy->kobj; in get_governor_parent_kobj()
143 idle_time = cur_wall_time - busy_time; in get_cpu_idle_time_jiffy()
154 if (idle_time == -1ULL) in get_cpu_idle_time()
166 * - validate & show freq table passed
167 * - set policies transition latency
168 * - policy->cpus with all possible CPUs
174 policy->freq_table = table; in cpufreq_generic_init()
175 policy->cpuinfo.transition_latency = transition_latency; in cpufreq_generic_init()
181 cpumask_setall(policy->cpus); in cpufreq_generic_init()
189 return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL; in cpufreq_cpu_get_raw()
197 if (!policy || IS_ERR(policy->clk)) { in cpufreq_generic_get()
203 return clk_get_rate(policy->clk) / 1000; in cpufreq_generic_get()
208 * cpufreq_cpu_get - Return policy for a CPU and mark it as busy.
233 kobject_get(&policy->kobj); in cpufreq_cpu_get()
243 * cpufreq_cpu_put - Decrement kobject usage counter for cpufreq policy.
248 kobject_put(&policy->kobj); in cpufreq_cpu_put()
253 * cpufreq_cpu_release - Unlock a policy and decrement its usage counter.
261 lockdep_assert_held(&policy->rwsem); in cpufreq_cpu_release()
263 up_write(&policy->rwsem); in cpufreq_cpu_release()
269 * cpufreq_cpu_acquire - Find policy for a CPU, mark it as busy and lock it.
287 down_write(&policy->rwsem); in cpufreq_cpu_acquire()
302 * adjust_jiffies - adjust the system "loops_per_jiffy"
307 * per-CPU loops_per_jiffy value wherever possible.
315 if (ci->flags & CPUFREQ_CONST_LOOPS) in adjust_jiffies()
320 l_p_j_ref_freq = ci->old; in adjust_jiffies()
321 pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n", in adjust_jiffies()
324 if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) { in adjust_jiffies()
326 ci->new); in adjust_jiffies()
328 loops_per_jiffy, ci->new); in adjust_jiffies()
334 * cpufreq_notify_transition - Notify frequency transition and adjust_jiffies.
335 * @policy: cpufreq policy to enable fast frequency switching for.
354 freqs->policy = policy; in cpufreq_notify_transition()
355 freqs->flags = cpufreq_driver->flags; in cpufreq_notify_transition()
357 state, freqs->new); in cpufreq_notify_transition()
366 if (policy->cur && policy->cur != freqs->old) { in cpufreq_notify_transition()
368 freqs->old, policy->cur); in cpufreq_notify_transition()
369 freqs->old = policy->cur; in cpufreq_notify_transition()
380 pr_debug("FREQ: %u - CPUs: %*pbl\n", freqs->new, in cpufreq_notify_transition()
381 cpumask_pr_args(policy->cpus)); in cpufreq_notify_transition()
383 for_each_cpu(cpu, policy->cpus) in cpufreq_notify_transition()
384 trace_cpu_frequency(freqs->new, cpu); in cpufreq_notify_transition()
389 cpufreq_stats_record_transition(policy, freqs->new); in cpufreq_notify_transition()
390 policy->cur = freqs->new; in cpufreq_notify_transition()
402 swap(freqs->old, freqs->new); in cpufreq_notify_post_transition()
412 * Catch double invocations of _begin() which lead to self-deadlock. in cpufreq_freq_transition_begin()
416 * where these checks can emit false-positive warnings in these in cpufreq_freq_transition_begin()
419 WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION) in cpufreq_freq_transition_begin()
420 && current == policy->transition_task); in cpufreq_freq_transition_begin()
423 wait_event(policy->transition_wait, !policy->transition_ongoing); in cpufreq_freq_transition_begin()
425 spin_lock(&policy->transition_lock); in cpufreq_freq_transition_begin()
427 if (unlikely(policy->transition_ongoing)) { in cpufreq_freq_transition_begin()
428 spin_unlock(&policy->transition_lock); in cpufreq_freq_transition_begin()
432 policy->transition_ongoing = true; in cpufreq_freq_transition_begin()
433 policy->transition_task = current; in cpufreq_freq_transition_begin()
435 spin_unlock(&policy->transition_lock); in cpufreq_freq_transition_begin()
444 if (WARN_ON(!policy->transition_ongoing)) in cpufreq_freq_transition_end()
449 arch_set_freq_scale(policy->related_cpus, in cpufreq_freq_transition_end()
450 policy->cur, in cpufreq_freq_transition_end()
451 policy->cpuinfo.max_freq); in cpufreq_freq_transition_end()
453 policy->transition_ongoing = false; in cpufreq_freq_transition_end()
454 policy->transition_task = NULL; in cpufreq_freq_transition_end()
456 wake_up(&policy->transition_wait); in cpufreq_freq_transition_end()
461 * Fast frequency switching status count. Positive means "enabled", negative
475 for (nb = cpufreq_transition_notifier_list.head; nb; nb = nb->next) in cpufreq_list_transition_notifiers()
476 pr_info("%pS\n", nb->notifier_call); in cpufreq_list_transition_notifiers()
482 * cpufreq_enable_fast_switch - Enable fast frequency switching for policy.
483 * @policy: cpufreq policy to enable fast frequency switching for.
485 * Try to enable fast frequency switching for @policy.
488 * at this point, as fast frequency switching is quite fundamentally at odds
494 lockdep_assert_held(&policy->rwsem); in cpufreq_enable_fast_switch()
496 if (!policy->fast_switch_possible) in cpufreq_enable_fast_switch()
502 policy->fast_switch_enabled = true; in cpufreq_enable_fast_switch()
504 pr_warn("CPU%u: Fast frequency switching not enabled\n", in cpufreq_enable_fast_switch()
505 policy->cpu); in cpufreq_enable_fast_switch()
513 * cpufreq_disable_fast_switch - Disable fast frequency switching for policy.
514 * @policy: cpufreq policy to disable fast frequency switching for.
519 if (policy->fast_switch_enabled) { in cpufreq_disable_fast_switch()
520 policy->fast_switch_enabled = false; in cpufreq_disable_fast_switch()
522 cpufreq_fast_switch_count--; in cpufreq_disable_fast_switch()
529 * cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported
536 * Return: Lowest driver-supported frequency greater than or equal to the
542 target_freq = clamp_val(target_freq, policy->min, policy->max); in cpufreq_driver_resolve_freq()
543 policy->cached_target_freq = target_freq; in cpufreq_driver_resolve_freq()
545 if (cpufreq_driver->target_index) { in cpufreq_driver_resolve_freq()
550 policy->cached_resolved_idx = idx; in cpufreq_driver_resolve_freq()
551 return policy->freq_table[idx].frequency; in cpufreq_driver_resolve_freq()
554 if (cpufreq_driver->resolve_freq) in cpufreq_driver_resolve_freq()
555 return cpufreq_driver->resolve_freq(policy, target_freq); in cpufreq_driver_resolve_freq()
565 if (policy->transition_delay_us) in cpufreq_policy_transition_delay_us()
566 return policy->transition_delay_us; in cpufreq_policy_transition_delay_us()
568 latency = policy->cpuinfo.transition_latency / NSEC_PER_USEC; in cpufreq_policy_transition_delay_us()
593 return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled); in show_boost()
603 return -EINVAL; in store_boost()
608 return -EINVAL; in store_boost()
623 if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN)) in find_governor()
638 if (!try_module_get(t->owner)) in get_governor()
659 * cpufreq_parse_governor - parse a governor string only for has_target()
677 * cpufreq_per_cpu_attr_read() / show_##file_name() -
680 * Write out information from cpufreq_driver->policy[cpu]; object must be
688 return sprintf(buf, "%u\n", policy->object); \
705 unsigned int freq; in show_scaling_cur_freq() local
707 freq = arch_freq_get_on_cpu(policy->cpu); in show_scaling_cur_freq()
708 if (freq) in show_scaling_cur_freq()
709 ret = sprintf(buf, "%u\n", freq); in show_scaling_cur_freq()
710 else if (cpufreq_driver->setpolicy && cpufreq_driver->get) in show_scaling_cur_freq()
711 ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu)); in show_scaling_cur_freq()
713 ret = sprintf(buf, "%u\n", policy->cur); in show_scaling_cur_freq()
718 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
729 return -EINVAL; \
731 ret = freq_qos_update_request(policy->object##_freq_req, val);\
739 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
753 * show_scaling_governor - show the current policy for the specified CPU
757 if (policy->policy == CPUFREQ_POLICY_POWERSAVE) in show_scaling_governor()
759 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) in show_scaling_governor()
761 else if (policy->governor) in show_scaling_governor()
763 policy->governor->name); in show_scaling_governor()
764 return -EINVAL; in show_scaling_governor()
768 * store_scaling_governor - store policy for the specified CPU
778 return -EINVAL; in store_scaling_governor()
780 if (cpufreq_driver->setpolicy) { in store_scaling_governor()
785 return -EINVAL; in store_scaling_governor()
793 return -EINVAL; in store_scaling_governor()
798 module_put(new_gov->owner); in store_scaling_governor()
805 * show_scaling_driver - show the cpufreq driver currently loaded
809 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name); in show_scaling_driver()
813 * show_scaling_available_governors - show the available CPUfreq governors
829 - (CPUFREQ_NAME_LEN + 2))) in show_scaling_available_governors()
831 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name); in show_scaling_available_governors()
846 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " "); in cpufreq_show_cpus()
847 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu); in cpufreq_show_cpus()
848 if (i >= (PAGE_SIZE - 5)) in cpufreq_show_cpus()
857 * show_related_cpus - show the CPUs affected by each transition even if
862 return cpufreq_show_cpus(policy->related_cpus, buf); in show_related_cpus()
866 * show_affected_cpus - show the CPUs affected by each transition
870 return cpufreq_show_cpus(policy->cpus, buf); in show_affected_cpus()
876 unsigned int freq = 0; in store_scaling_setspeed() local
879 if (!policy->governor || !policy->governor->store_setspeed) in store_scaling_setspeed()
880 return -EINVAL; in store_scaling_setspeed()
882 ret = sscanf(buf, "%u", &freq); in store_scaling_setspeed()
884 return -EINVAL; in store_scaling_setspeed()
886 policy->governor->store_setspeed(policy, freq); in store_scaling_setspeed()
893 if (!policy->governor || !policy->governor->show_setspeed) in show_scaling_setspeed()
896 return policy->governor->show_setspeed(policy, buf); in show_scaling_setspeed()
900 * show_bios_limit - show the current cpufreq HW/BIOS limitation
906 ret = cpufreq_driver->bios_limit(policy->cpu, &limit); in show_bios_limit()
909 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq); in show_bios_limit()
951 if (!fattr->show) in show()
952 return -EIO; in show()
954 down_read(&policy->rwsem); in show()
955 ret = fattr->show(policy, buf); in show()
956 up_read(&policy->rwsem); in show()
966 ssize_t ret = -EINVAL; in store()
968 if (!fattr->store) in store()
969 return -EIO; in store()
976 return -EBUSY; in store()
978 if (cpu_online(policy->cpu)) { in store()
979 down_write(&policy->rwsem); in store()
980 ret = fattr->store(policy, buf, count); in store()
981 up_write(&policy->rwsem); in store()
993 complete(&policy->kobj_unregister); in cpufreq_sysfs_release()
1014 if (cpumask_test_and_set_cpu(cpu, policy->real_cpus)) in add_cpu_dev_symlink()
1018 if (sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq")) in add_cpu_dev_symlink()
1026 sysfs_remove_link(&dev->kobj, "cpufreq"); in remove_cpu_dev_symlink()
1035 drv_attr = cpufreq_driver->attr; in cpufreq_add_dev_interface()
1037 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr)); in cpufreq_add_dev_interface()
1042 if (cpufreq_driver->get) { in cpufreq_add_dev_interface()
1043 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr); in cpufreq_add_dev_interface()
1048 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr); in cpufreq_add_dev_interface()
1052 if (cpufreq_driver->bios_limit) { in cpufreq_add_dev_interface()
1053 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr); in cpufreq_add_dev_interface()
1069 gov = get_governor(policy->last_governor); in cpufreq_init_policy()
1072 gov->name, policy->cpu); in cpufreq_init_policy()
1079 __module_get(gov->owner); in cpufreq_init_policy()
1085 if (policy->last_policy) { in cpufreq_init_policy()
1086 pol = policy->last_policy; in cpufreq_init_policy()
1095 pol = policy->policy; in cpufreq_init_policy()
1099 return -ENODATA; in cpufreq_init_policy()
1104 module_put(gov->owner); in cpufreq_init_policy()
1114 if (cpumask_test_cpu(cpu, policy->cpus)) in cpufreq_add_policy_cpu()
1117 down_write(&policy->rwsem); in cpufreq_add_policy_cpu()
1121 cpumask_set_cpu(cpu, policy->cpus); in cpufreq_add_policy_cpu()
1128 up_write(&policy->rwsem); in cpufreq_add_policy_cpu()
1135 pr_debug("updating policy for CPU %u\n", policy->cpu); in refresh_frequency_limits()
1137 cpufreq_set_policy(policy, policy->governor, policy->policy); in refresh_frequency_limits()
1147 pr_debug("handle_update for cpu %u called\n", policy->cpu); in handle_update()
1148 down_write(&policy->rwsem); in handle_update()
1150 up_write(&policy->rwsem); in handle_update()
1153 static int cpufreq_notifier_min(struct notifier_block *nb, unsigned long freq, in cpufreq_notifier_min() argument
1158 schedule_work(&policy->update); in cpufreq_notifier_min()
1162 static int cpufreq_notifier_max(struct notifier_block *nb, unsigned long freq, in cpufreq_notifier_max() argument
1167 schedule_work(&policy->update); in cpufreq_notifier_max()
1176 down_write(&policy->rwsem); in cpufreq_policy_put_kobj()
1178 kobj = &policy->kobj; in cpufreq_policy_put_kobj()
1179 cmp = &policy->kobj_unregister; in cpufreq_policy_put_kobj()
1180 up_write(&policy->rwsem); in cpufreq_policy_put_kobj()
1206 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL)) in cpufreq_policy_alloc()
1209 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL)) in cpufreq_policy_alloc()
1212 if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL)) in cpufreq_policy_alloc()
1215 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq, in cpufreq_policy_alloc()
1218 dev_err(dev, "%s: failed to init policy->kobj: %d\n", __func__, ret); in cpufreq_policy_alloc()
1224 kobject_put(&policy->kobj); in cpufreq_policy_alloc()
1228 freq_constraints_init(&policy->constraints); in cpufreq_policy_alloc()
1230 policy->nb_min.notifier_call = cpufreq_notifier_min; in cpufreq_policy_alloc()
1231 policy->nb_max.notifier_call = cpufreq_notifier_max; in cpufreq_policy_alloc()
1233 ret = freq_qos_add_notifier(&policy->constraints, FREQ_QOS_MIN, in cpufreq_policy_alloc()
1234 &policy->nb_min); in cpufreq_policy_alloc()
1237 ret, cpumask_pr_args(policy->cpus)); in cpufreq_policy_alloc()
1241 ret = freq_qos_add_notifier(&policy->constraints, FREQ_QOS_MAX, in cpufreq_policy_alloc()
1242 &policy->nb_max); in cpufreq_policy_alloc()
1245 ret, cpumask_pr_args(policy->cpus)); in cpufreq_policy_alloc()
1249 INIT_LIST_HEAD(&policy->policy_list); in cpufreq_policy_alloc()
1250 init_rwsem(&policy->rwsem); in cpufreq_policy_alloc()
1251 spin_lock_init(&policy->transition_lock); in cpufreq_policy_alloc()
1252 init_waitqueue_head(&policy->transition_wait); in cpufreq_policy_alloc()
1253 init_completion(&policy->kobj_unregister); in cpufreq_policy_alloc()
1254 INIT_WORK(&policy->update, handle_update); in cpufreq_policy_alloc()
1256 policy->cpu = cpu; in cpufreq_policy_alloc()
1260 freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MIN, in cpufreq_policy_alloc()
1261 &policy->nb_min); in cpufreq_policy_alloc()
1265 free_cpumask_var(policy->real_cpus); in cpufreq_policy_alloc()
1267 free_cpumask_var(policy->related_cpus); in cpufreq_policy_alloc()
1269 free_cpumask_var(policy->cpus); in cpufreq_policy_alloc()
1283 list_del(&policy->policy_list); in cpufreq_policy_free()
1285 for_each_cpu(cpu, policy->related_cpus) in cpufreq_policy_free()
1289 freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MAX, in cpufreq_policy_free()
1290 &policy->nb_max); in cpufreq_policy_free()
1291 freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MIN, in cpufreq_policy_free()
1292 &policy->nb_min); in cpufreq_policy_free()
1294 /* Cancel any pending policy->update work before freeing the policy. */ in cpufreq_policy_free()
1295 cancel_work_sync(&policy->update); in cpufreq_policy_free()
1297 if (policy->max_freq_req) { in cpufreq_policy_free()
1304 freq_qos_remove_request(policy->max_freq_req); in cpufreq_policy_free()
1307 freq_qos_remove_request(policy->min_freq_req); in cpufreq_policy_free()
1308 kfree(policy->min_freq_req); in cpufreq_policy_free()
1311 free_cpumask_var(policy->real_cpus); in cpufreq_policy_free()
1312 free_cpumask_var(policy->related_cpus); in cpufreq_policy_free()
1313 free_cpumask_var(policy->cpus); in cpufreq_policy_free()
1330 WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus)); in cpufreq_online()
1336 down_write(&policy->rwsem); in cpufreq_online()
1337 policy->cpu = cpu; in cpufreq_online()
1338 policy->governor = NULL; in cpufreq_online()
1339 up_write(&policy->rwsem); in cpufreq_online()
1344 return -ENOMEM; in cpufreq_online()
1347 if (!new_policy && cpufreq_driver->online) { in cpufreq_online()
1348 ret = cpufreq_driver->online(policy); in cpufreq_online()
1355 /* Recover policy->cpus using related_cpus */ in cpufreq_online()
1356 cpumask_copy(policy->cpus, policy->related_cpus); in cpufreq_online()
1358 cpumask_copy(policy->cpus, cpumask_of(cpu)); in cpufreq_online()
1362 * to accept all calls to ->verify and ->setpolicy for this CPU. in cpufreq_online()
1364 ret = cpufreq_driver->init(policy); in cpufreq_online()
1375 /* related_cpus should at least include policy->cpus. */ in cpufreq_online()
1376 cpumask_copy(policy->related_cpus, policy->cpus); in cpufreq_online()
1379 down_write(&policy->rwsem); in cpufreq_online()
1384 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask); in cpufreq_online()
1387 for_each_cpu(j, policy->related_cpus) { in cpufreq_online()
1392 policy->min_freq_req = kzalloc(2 * sizeof(*policy->min_freq_req), in cpufreq_online()
1394 if (!policy->min_freq_req) in cpufreq_online()
1397 ret = freq_qos_add_request(&policy->constraints, in cpufreq_online()
1398 policy->min_freq_req, FREQ_QOS_MIN, in cpufreq_online()
1399 policy->min); in cpufreq_online()
1405 kfree(policy->min_freq_req); in cpufreq_online()
1406 policy->min_freq_req = NULL; in cpufreq_online()
1415 policy->max_freq_req = policy->min_freq_req + 1; in cpufreq_online()
1417 ret = freq_qos_add_request(&policy->constraints, in cpufreq_online()
1418 policy->max_freq_req, FREQ_QOS_MAX, in cpufreq_online()
1419 policy->max); in cpufreq_online()
1421 policy->max_freq_req = NULL; in cpufreq_online()
1429 if (cpufreq_driver->get && has_target()) { in cpufreq_online()
1430 policy->cur = cpufreq_driver->get(policy->cpu); in cpufreq_online()
1431 if (!policy->cur) { in cpufreq_online()
1432 pr_err("%s: ->get() failed\n", __func__); in cpufreq_online()
1442 * freq-table. This also makes cpufreq stats inconsistent as in cpufreq_online()
1443 * cpufreq-stats would fail to register because current frequency of CPU in cpufreq_online()
1444 * isn't found in freq-table. in cpufreq_online()
1447 * for the next freq which is >= policy->cur ('cur' must be set by now, in cpufreq_online()
1448 * otherwise we will end up setting freq to lowest of the table as 'cur' in cpufreq_online()
1451 * We are passing target-freq as "policy->cur - 1" otherwise in cpufreq_online()
1452 * __cpufreq_driver_target() would simply fail, as policy->cur will be in cpufreq_online()
1453 * equal to target-freq. in cpufreq_online()
1455 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK) in cpufreq_online()
1457 unsigned int old_freq = policy->cur; in cpufreq_online()
1461 if (ret == -EINVAL) { in cpufreq_online()
1462 ret = __cpufreq_driver_target(policy, old_freq - 1, in cpufreq_online()
1472 __func__, policy->cpu, old_freq, policy->cur); in cpufreq_online()
1484 list_add(&policy->policy_list, &cpufreq_policy_list); in cpufreq_online()
1495 up_write(&policy->rwsem); in cpufreq_online()
1497 kobject_uevent(&policy->kobj, KOBJ_ADD); in cpufreq_online()
1500 if (cpufreq_driver->ready) in cpufreq_online()
1501 cpufreq_driver->ready(policy); in cpufreq_online()
1504 policy->cdev = of_cpufreq_cooling_register(policy); in cpufreq_online()
1511 for_each_cpu(j, policy->real_cpus) in cpufreq_online()
1514 up_write(&policy->rwsem); in cpufreq_online()
1517 if (cpufreq_driver->exit) in cpufreq_online()
1518 cpufreq_driver->exit(policy); in cpufreq_online()
1526 * cpufreq_add_dev - the cpufreq interface for a CPU device.
1533 unsigned cpu = dev->id; in cpufreq_add_dev()
1565 down_write(&policy->rwsem); in cpufreq_offline()
1569 cpumask_clear_cpu(cpu, policy->cpus); in cpufreq_offline()
1573 strncpy(policy->last_governor, policy->governor->name, in cpufreq_offline()
1576 policy->last_policy = policy->policy; in cpufreq_offline()
1577 } else if (cpu == policy->cpu) { in cpufreq_offline()
1579 policy->cpu = cpumask_any(policy->cpus); in cpufreq_offline()
1594 cpufreq_cooling_unregister(policy->cdev); in cpufreq_offline()
1595 policy->cdev = NULL; in cpufreq_offline()
1598 if (cpufreq_driver->stop_cpu) in cpufreq_offline()
1599 cpufreq_driver->stop_cpu(policy); in cpufreq_offline()
1605 * Perform the ->offline() during light-weight tear-down, as in cpufreq_offline()
1608 if (cpufreq_driver->offline) { in cpufreq_offline()
1609 cpufreq_driver->offline(policy); in cpufreq_offline()
1610 } else if (cpufreq_driver->exit) { in cpufreq_offline()
1611 cpufreq_driver->exit(policy); in cpufreq_offline()
1612 policy->freq_table = NULL; in cpufreq_offline()
1616 up_write(&policy->rwsem); in cpufreq_offline()
1621 * cpufreq_remove_dev - remove a CPU device
1627 unsigned int cpu = dev->id; in cpufreq_remove_dev()
1636 cpumask_clear_cpu(cpu, policy->real_cpus); in cpufreq_remove_dev()
1639 if (cpumask_empty(policy->real_cpus)) { in cpufreq_remove_dev()
1640 /* We did light-weight exit earlier, do full tear down now */ in cpufreq_remove_dev()
1641 if (cpufreq_driver->offline) in cpufreq_remove_dev()
1642 cpufreq_driver->exit(policy); in cpufreq_remove_dev()
1649 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1663 policy->cur, new_freq); in cpufreq_out_of_sync()
1665 freqs.old = policy->cur; in cpufreq_out_of_sync()
1676 new_freq = cpufreq_driver->get(policy->cpu); in cpufreq_verify_current_freq()
1681 * If fast frequency switching is used with the given policy, the check in cpufreq_verify_current_freq()
1682 * against policy->cur is pointless, so skip it in that case. in cpufreq_verify_current_freq()
1684 if (policy->fast_switch_enabled || !has_target()) in cpufreq_verify_current_freq()
1687 if (policy->cur != new_freq) { in cpufreq_verify_current_freq()
1690 schedule_work(&policy->update); in cpufreq_verify_current_freq()
1697 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
1700 * This is the last known freq, without actually getting it from the driver.
1711 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) { in cpufreq_quick_get()
1712 ret_freq = cpufreq_driver->get(cpu); in cpufreq_quick_get()
1721 ret_freq = policy->cur; in cpufreq_quick_get()
1730 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1741 ret_freq = policy->max; in cpufreq_quick_get_max()
1750 * cpufreq_get_hw_max_freq - get the max hardware frequency of the CPU
1761 ret_freq = policy->cpuinfo.max_freq; in cpufreq_get_hw_max_freq()
1778 * cpufreq_get - get the current CPU frequency (in kHz)
1789 down_read(&policy->rwsem); in cpufreq_get()
1790 if (cpufreq_driver->get) in cpufreq_get()
1792 up_read(&policy->rwsem); in cpufreq_get()
1816 if (!policy->suspend_freq) { in cpufreq_generic_suspend()
1821 pr_debug("%s: Setting suspend-freq: %u\n", __func__, in cpufreq_generic_suspend()
1822 policy->suspend_freq); in cpufreq_generic_suspend()
1824 ret = __cpufreq_driver_target(policy, policy->suspend_freq, in cpufreq_generic_suspend()
1827 pr_err("%s: unable to set suspend-freq: %u. err: %d\n", in cpufreq_generic_suspend()
1828 __func__, policy->suspend_freq, ret); in cpufreq_generic_suspend()
1835 * cpufreq_suspend() - Suspend CPUFreq governors
1849 if (!has_target() && !cpufreq_driver->suspend) in cpufreq_suspend()
1856 down_write(&policy->rwsem); in cpufreq_suspend()
1858 up_write(&policy->rwsem); in cpufreq_suspend()
1861 if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy)) in cpufreq_suspend()
1863 cpufreq_driver->name); in cpufreq_suspend()
1871 * cpufreq_resume() - Resume CPUFreq governors
1889 if (!has_target() && !cpufreq_driver->resume) in cpufreq_resume()
1895 if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) { in cpufreq_resume()
1899 down_write(&policy->rwsem); in cpufreq_resume()
1901 up_write(&policy->rwsem); in cpufreq_resume()
1911 * cpufreq_driver_test_flags - Test cpufreq driver's flags against given ones.
1919 return !!(cpufreq_driver->flags & flags); in cpufreq_driver_test_flags()
1923 * cpufreq_get_current_driver - return current driver's name
1931 return cpufreq_driver->name; in cpufreq_get_current_driver()
1938 * cpufreq_get_driver_data - return current driver data
1946 return cpufreq_driver->driver_data; in cpufreq_get_driver_data()
1957 * cpufreq_register_notifier - register a driver with cpufreq
1974 return -EINVAL; in cpufreq_register_notifier()
1982 return -EBUSY; in cpufreq_register_notifier()
1987 cpufreq_fast_switch_count--; in cpufreq_register_notifier()
1996 ret = -EINVAL; in cpufreq_register_notifier()
2004 * cpufreq_unregister_notifier - unregister a driver with cpufreq
2018 return -EINVAL; in cpufreq_unregister_notifier()
2036 ret = -EINVAL; in cpufreq_unregister_notifier()
2049 * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch.
2055 * The driver's ->fast_switch() callback invoked by this function must be
2056 * suitable for being called from within RCU-sched read-side critical sections
2060 * This function must not be called if policy->fast_switch_enabled is unset.
2064 * parallel with either ->target() or ->target_index() for the same policy.
2068 * If 0 is returned by the driver's ->fast_switch() callback to indicate an
2074 unsigned int freq; in cpufreq_driver_fast_switch() local
2077 target_freq = clamp_val(target_freq, policy->min, policy->max); in cpufreq_driver_fast_switch()
2078 freq = cpufreq_driver->fast_switch(policy, target_freq); in cpufreq_driver_fast_switch()
2080 if (!freq) in cpufreq_driver_fast_switch()
2083 policy->cur = freq; in cpufreq_driver_fast_switch()
2084 arch_set_freq_scale(policy->related_cpus, freq, in cpufreq_driver_fast_switch()
2085 policy->cpuinfo.max_freq); in cpufreq_driver_fast_switch()
2086 cpufreq_stats_record_transition(policy, freq); in cpufreq_driver_fast_switch()
2089 for_each_cpu(cpu, policy->cpus) in cpufreq_driver_fast_switch()
2090 trace_cpu_frequency(freq, cpu); in cpufreq_driver_fast_switch()
2093 return freq; in cpufreq_driver_fast_switch()
2097 /* Must set freqs->new to intermediate frequency */
2103 freqs->new = cpufreq_driver->get_intermediate(policy, index); in __target_intermediate()
2105 /* We don't need to switch to intermediate freq */ in __target_intermediate()
2106 if (!freqs->new) in __target_intermediate()
2109 pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n", in __target_intermediate()
2110 __func__, policy->cpu, freqs->old, freqs->new); in __target_intermediate()
2113 ret = cpufreq_driver->target_intermediate(policy, index); in __target_intermediate()
2125 struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0}; in __target_index()
2127 unsigned int newfreq = policy->freq_table[index].frequency; in __target_index()
2128 int retval = -EINVAL; in __target_index()
2131 if (newfreq == policy->cur) in __target_index()
2134 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION); in __target_index()
2136 /* Handle switching to intermediate frequency */ in __target_index()
2137 if (cpufreq_driver->get_intermediate) { in __target_index()
2143 /* Set old freq to intermediate */ in __target_index()
2149 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n", in __target_index()
2150 __func__, policy->cpu, freqs.old, freqs.new); in __target_index()
2155 retval = cpufreq_driver->target_index(policy, index); in __target_index()
2164 * Failed after setting to intermediate freq? Driver should have in __target_index()
2167 * case we haven't switched to intermediate freq at all. in __target_index()
2171 freqs.new = policy->restore_freq; in __target_index()
2188 return -ENODEV; in __cpufreq_driver_target()
2191 target_freq = clamp_val(target_freq, policy->min, policy->max); in __cpufreq_driver_target()
2194 policy->cpu, target_freq, relation, old_target_freq); in __cpufreq_driver_target()
2199 * exactly same freq is called again and so we can save on few function in __cpufreq_driver_target()
2202 if (target_freq == policy->cur && in __cpufreq_driver_target()
2203 !(cpufreq_driver->flags & CPUFREQ_NEED_UPDATE_LIMITS)) in __cpufreq_driver_target()
2207 policy->restore_freq = policy->cur; in __cpufreq_driver_target()
2209 if (cpufreq_driver->target) in __cpufreq_driver_target()
2210 return cpufreq_driver->target(policy, target_freq, relation); in __cpufreq_driver_target()
2212 if (!cpufreq_driver->target_index) in __cpufreq_driver_target()
2213 return -EINVAL; in __cpufreq_driver_target()
2227 down_write(&policy->rwsem); in cpufreq_driver_target()
2231 up_write(&policy->rwsem); in cpufreq_driver_target()
2253 if (!policy->governor) in cpufreq_init_governor()
2254 return -EINVAL; in cpufreq_init_governor()
2256 /* Platform doesn't want dynamic frequency switching ? */ in cpufreq_init_governor()
2257 if (policy->governor->flags & CPUFREQ_GOV_DYNAMIC_SWITCHING && in cpufreq_init_governor()
2258 cpufreq_driver->flags & CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING) { in cpufreq_init_governor()
2262 pr_warn("Can't use %s governor as dynamic switching is disallowed. Fallback to %s governor\n", in cpufreq_init_governor()
2263 policy->governor->name, gov->name); in cpufreq_init_governor()
2264 policy->governor = gov; in cpufreq_init_governor()
2266 return -EINVAL; in cpufreq_init_governor()
2270 if (!try_module_get(policy->governor->owner)) in cpufreq_init_governor()
2271 return -EINVAL; in cpufreq_init_governor()
2273 pr_debug("%s: for CPU %u\n", __func__, policy->cpu); in cpufreq_init_governor()
2275 if (policy->governor->init) { in cpufreq_init_governor()
2276 ret = policy->governor->init(policy); in cpufreq_init_governor()
2278 module_put(policy->governor->owner); in cpufreq_init_governor()
2283 policy->strict_target = !!(policy->governor->flags & CPUFREQ_GOV_STRICT_TARGET); in cpufreq_init_governor()
2290 if (cpufreq_suspended || !policy->governor) in cpufreq_exit_governor()
2293 pr_debug("%s: for CPU %u\n", __func__, policy->cpu); in cpufreq_exit_governor()
2295 if (policy->governor->exit) in cpufreq_exit_governor()
2296 policy->governor->exit(policy); in cpufreq_exit_governor()
2298 module_put(policy->governor->owner); in cpufreq_exit_governor()
2308 if (!policy->governor) in cpufreq_start_governor()
2309 return -EINVAL; in cpufreq_start_governor()
2311 pr_debug("%s: for CPU %u\n", __func__, policy->cpu); in cpufreq_start_governor()
2313 if (cpufreq_driver->get) in cpufreq_start_governor()
2316 if (policy->governor->start) { in cpufreq_start_governor()
2317 ret = policy->governor->start(policy); in cpufreq_start_governor()
2322 if (policy->governor->limits) in cpufreq_start_governor()
2323 policy->governor->limits(policy); in cpufreq_start_governor()
2330 if (cpufreq_suspended || !policy->governor) in cpufreq_stop_governor()
2333 pr_debug("%s: for CPU %u\n", __func__, policy->cpu); in cpufreq_stop_governor()
2335 if (policy->governor->stop) in cpufreq_stop_governor()
2336 policy->governor->stop(policy); in cpufreq_stop_governor()
2341 if (cpufreq_suspended || !policy->governor) in cpufreq_governor_limits()
2344 pr_debug("%s: for CPU %u\n", __func__, policy->cpu); in cpufreq_governor_limits()
2346 if (policy->governor->limits) in cpufreq_governor_limits()
2347 policy->governor->limits(policy); in cpufreq_governor_limits()
2355 return -EINVAL; in cpufreq_register_governor()
2358 return -ENODEV; in cpufreq_register_governor()
2362 err = -EBUSY; in cpufreq_register_governor()
2363 if (!find_governor(governor->name)) { in cpufreq_register_governor()
2365 list_add(&governor->governor_list, &cpufreq_governor_list); in cpufreq_register_governor()
2387 if (!strcmp(policy->last_governor, governor->name)) { in cpufreq_unregister_governor()
2388 policy->governor = NULL; in cpufreq_unregister_governor()
2389 strcpy(policy->last_governor, "\0"); in cpufreq_unregister_governor()
2395 list_del(&governor->governor_list); in cpufreq_unregister_governor()
2406 * cpufreq_get_policy - get the current cpufreq_policy
2417 return -EINVAL; in cpufreq_get_policy()
2421 return -EINVAL; in cpufreq_get_policy()
2431 * cpufreq_set_policy - Modify cpufreq policy parameters.
2434 * @new_pol: Policy value (for drivers with built-in governors).
2436 * Invoke the cpufreq driver's ->verify() callback to sanity-check the frequency
2438 * values and either invoke the driver's ->setpolicy() callback (if present) or
2440 * ->limits() callback (if @new_gov points to the same object as the one in
2453 memcpy(&new_data.cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo)); in cpufreq_set_policy()
2454 new_data.freq_table = policy->freq_table; in cpufreq_set_policy()
2455 new_data.cpu = policy->cpu; in cpufreq_set_policy()
2460 new_data.min = freq_qos_read_value(&policy->constraints, FREQ_QOS_MIN); in cpufreq_set_policy()
2461 new_data.max = freq_qos_read_value(&policy->constraints, FREQ_QOS_MAX); in cpufreq_set_policy()
2463 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", in cpufreq_set_policy()
2470 ret = cpufreq_driver->verify(&new_data); in cpufreq_set_policy()
2474 policy->min = new_data.min; in cpufreq_set_policy()
2475 policy->max = new_data.max; in cpufreq_set_policy()
2478 policy->cached_target_freq = UINT_MAX; in cpufreq_set_policy()
2480 pr_debug("new min and max freqs are %u - %u kHz\n", in cpufreq_set_policy()
2481 policy->min, policy->max); in cpufreq_set_policy()
2483 if (cpufreq_driver->setpolicy) { in cpufreq_set_policy()
2484 policy->policy = new_pol; in cpufreq_set_policy()
2486 return cpufreq_driver->setpolicy(policy); in cpufreq_set_policy()
2489 if (new_gov == policy->governor) { in cpufreq_set_policy()
2498 old_gov = policy->governor; in cpufreq_set_policy()
2506 policy->governor = new_gov; in cpufreq_set_policy()
2518 /* new governor failed, so re-start old one */ in cpufreq_set_policy()
2519 pr_debug("starting governor %s failed\n", policy->governor->name); in cpufreq_set_policy()
2521 policy->governor = old_gov; in cpufreq_set_policy()
2523 policy->governor = NULL; in cpufreq_set_policy()
2532 * cpufreq_update_policy - Re-evaluate an existing cpufreq policy.
2533 * @cpu: CPU to re-evaluate the policy for.
2536 * cpufreq_set_policy() to re-apply the min and max limits, which triggers the
2537 * evaluation of policy notifiers and the cpufreq driver's ->verify() callback
2548 * BIOS might change freq behind our back in cpufreq_update_policy()
2549 * -> ask driver for current freq and notify governors about a change in cpufreq_update_policy()
2551 if (cpufreq_driver->get && has_target() && in cpufreq_update_policy()
2563 * cpufreq_update_limits - Update policy limits for a given CPU.
2566 * Invoke the driver's ->update_limits callback if present or call
2571 if (cpufreq_driver->update_limits) in cpufreq_update_limits()
2572 cpufreq_driver->update_limits(cpu); in cpufreq_update_limits()
2585 if (!policy->freq_table) in cpufreq_boost_set_sw()
2586 return -ENXIO; in cpufreq_boost_set_sw()
2588 ret = cpufreq_frequency_table_cpuinfo(policy, policy->freq_table); in cpufreq_boost_set_sw()
2594 ret = freq_qos_update_request(policy->max_freq_req, policy->max); in cpufreq_boost_set_sw()
2607 if (cpufreq_driver->boost_enabled == state) in cpufreq_boost_trigger_state()
2611 cpufreq_driver->boost_enabled = state; in cpufreq_boost_trigger_state()
2616 ret = cpufreq_driver->set_boost(policy, state); in cpufreq_boost_trigger_state()
2628 cpufreq_driver->boost_enabled = !state; in cpufreq_boost_trigger_state()
2639 return cpufreq_driver->set_boost; in cpufreq_boost_supported()
2663 return -EINVAL; in cpufreq_enable_boost_support()
2668 cpufreq_driver->set_boost = cpufreq_boost_set_sw; in cpufreq_enable_boost_support()
2677 return cpufreq_driver->boost_enabled; in cpufreq_boost_enabled()
2701 * cpufreq_register_driver - register a CPU Frequency driver
2706 * returns zero on success, -EEXIST when another driver got here first
2716 return -ENODEV; in cpufreq_register_driver()
2723 return -EPROBE_DEFER; in cpufreq_register_driver()
2725 if (!driver_data || !driver_data->verify || !driver_data->init || in cpufreq_register_driver()
2726 !(driver_data->setpolicy || driver_data->target_index || in cpufreq_register_driver()
2727 driver_data->target) || in cpufreq_register_driver()
2728 (driver_data->setpolicy && (driver_data->target_index || in cpufreq_register_driver()
2729 driver_data->target)) || in cpufreq_register_driver()
2730 (!driver_data->get_intermediate != !driver_data->target_intermediate) || in cpufreq_register_driver()
2731 (!driver_data->online != !driver_data->offline)) in cpufreq_register_driver()
2732 return -EINVAL; in cpufreq_register_driver()
2734 pr_debug("trying to register driver %s\n", driver_data->name); in cpufreq_register_driver()
2742 ret = -EEXIST; in cpufreq_register_driver()
2752 if (!cpufreq_driver->setpolicy) { in cpufreq_register_driver()
2757 if (driver_data->setpolicy) in cpufreq_register_driver()
2758 driver_data->flags |= CPUFREQ_CONST_LOOPS; in cpufreq_register_driver()
2770 if (!(cpufreq_driver->flags & CPUFREQ_STICKY) && in cpufreq_register_driver()
2772 /* if all ->init() calls failed, unregister */ in cpufreq_register_driver()
2773 ret = -ENODEV; in cpufreq_register_driver()
2775 driver_data->name); in cpufreq_register_driver()
2788 pr_debug("driver %s up and running\n", driver_data->name); in cpufreq_register_driver()
2806 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2810 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2818 return -EINVAL; in cpufreq_unregister_driver()
2820 pr_debug("unregistering driver %s\n", driver->name); in cpufreq_unregister_driver()
2845 return -ENODEV; in cpufreq_core_init()
2847 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj); in cpufreq_core_init()
2851 strncpy(default_governor, gov->name, CPUFREQ_NAME_LEN); in cpufreq_core_init()