drm/rockchip: vop2: Support 32x8 superblock afbc
[drm/drm-misc.git] / kernel / power / energy_model.c
blobd07faf42eace6f4f9baff2729868d330e8941ad1
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Energy Model of devices
5 * Copyright (c) 2018-2021, Arm ltd.
6 * Written by: Quentin Perret, Arm ltd.
7 * Improvements provided by: Lukasz Luba, Arm ltd.
8 */
10 #define pr_fmt(fmt) "energy_model: " fmt
12 #include <linux/cpu.h>
13 #include <linux/cpufreq.h>
14 #include <linux/cpumask.h>
15 #include <linux/debugfs.h>
16 #include <linux/energy_model.h>
17 #include <linux/sched/topology.h>
18 #include <linux/slab.h>
21 * Mutex serializing the registrations of performance domains and letting
22 * callbacks defined by drivers sleep.
24 static DEFINE_MUTEX(em_pd_mutex);
26 static void em_cpufreq_update_efficiencies(struct device *dev,
27 struct em_perf_state *table);
28 static void em_check_capacity_update(void);
29 static void em_update_workfn(struct work_struct *work);
30 static DECLARE_DELAYED_WORK(em_update_work, em_update_workfn);
32 static bool _is_cpu_device(struct device *dev)
34 return (dev->bus == &cpu_subsys);
37 #ifdef CONFIG_DEBUG_FS
38 static struct dentry *rootdir;
40 struct em_dbg_info {
41 struct em_perf_domain *pd;
42 int ps_id;
45 #define DEFINE_EM_DBG_SHOW(name, fname) \
46 static int em_debug_##fname##_show(struct seq_file *s, void *unused) \
47 { \
48 struct em_dbg_info *em_dbg = s->private; \
49 struct em_perf_state *table; \
50 unsigned long val; \
52 rcu_read_lock(); \
53 table = em_perf_state_from_pd(em_dbg->pd); \
54 val = table[em_dbg->ps_id].name; \
55 rcu_read_unlock(); \
57 seq_printf(s, "%lu\n", val); \
58 return 0; \
59 } \
60 DEFINE_SHOW_ATTRIBUTE(em_debug_##fname)
62 DEFINE_EM_DBG_SHOW(frequency, frequency);
63 DEFINE_EM_DBG_SHOW(power, power);
64 DEFINE_EM_DBG_SHOW(cost, cost);
65 DEFINE_EM_DBG_SHOW(performance, performance);
66 DEFINE_EM_DBG_SHOW(flags, inefficiency);
68 static void em_debug_create_ps(struct em_perf_domain *em_pd,
69 struct em_dbg_info *em_dbg, int i,
70 struct dentry *pd)
72 struct em_perf_state *table;
73 unsigned long freq;
74 struct dentry *d;
75 char name[24];
77 em_dbg[i].pd = em_pd;
78 em_dbg[i].ps_id = i;
80 rcu_read_lock();
81 table = em_perf_state_from_pd(em_pd);
82 freq = table[i].frequency;
83 rcu_read_unlock();
85 snprintf(name, sizeof(name), "ps:%lu", freq);
87 /* Create per-ps directory */
88 d = debugfs_create_dir(name, pd);
89 debugfs_create_file("frequency", 0444, d, &em_dbg[i],
90 &em_debug_frequency_fops);
91 debugfs_create_file("power", 0444, d, &em_dbg[i],
92 &em_debug_power_fops);
93 debugfs_create_file("cost", 0444, d, &em_dbg[i],
94 &em_debug_cost_fops);
95 debugfs_create_file("performance", 0444, d, &em_dbg[i],
96 &em_debug_performance_fops);
97 debugfs_create_file("inefficient", 0444, d, &em_dbg[i],
98 &em_debug_inefficiency_fops);
101 static int em_debug_cpus_show(struct seq_file *s, void *unused)
103 seq_printf(s, "%*pbl\n", cpumask_pr_args(to_cpumask(s->private)));
105 return 0;
107 DEFINE_SHOW_ATTRIBUTE(em_debug_cpus);
109 static int em_debug_flags_show(struct seq_file *s, void *unused)
111 struct em_perf_domain *pd = s->private;
113 seq_printf(s, "%#lx\n", pd->flags);
115 return 0;
117 DEFINE_SHOW_ATTRIBUTE(em_debug_flags);
119 static void em_debug_create_pd(struct device *dev)
121 struct em_dbg_info *em_dbg;
122 struct dentry *d;
123 int i;
125 /* Create the directory of the performance domain */
126 d = debugfs_create_dir(dev_name(dev), rootdir);
128 if (_is_cpu_device(dev))
129 debugfs_create_file("cpus", 0444, d, dev->em_pd->cpus,
130 &em_debug_cpus_fops);
132 debugfs_create_file("flags", 0444, d, dev->em_pd,
133 &em_debug_flags_fops);
135 em_dbg = devm_kcalloc(dev, dev->em_pd->nr_perf_states,
136 sizeof(*em_dbg), GFP_KERNEL);
137 if (!em_dbg)
138 return;
140 /* Create a sub-directory for each performance state */
141 for (i = 0; i < dev->em_pd->nr_perf_states; i++)
142 em_debug_create_ps(dev->em_pd, em_dbg, i, d);
146 static void em_debug_remove_pd(struct device *dev)
148 debugfs_lookup_and_remove(dev_name(dev), rootdir);
151 static int __init em_debug_init(void)
153 /* Create /sys/kernel/debug/energy_model directory */
154 rootdir = debugfs_create_dir("energy_model", NULL);
156 return 0;
158 fs_initcall(em_debug_init);
159 #else /* CONFIG_DEBUG_FS */
160 static void em_debug_create_pd(struct device *dev) {}
161 static void em_debug_remove_pd(struct device *dev) {}
162 #endif
164 static void em_destroy_table_rcu(struct rcu_head *rp)
166 struct em_perf_table __rcu *table;
168 table = container_of(rp, struct em_perf_table, rcu);
169 kfree(table);
172 static void em_release_table_kref(struct kref *kref)
174 struct em_perf_table __rcu *table;
176 /* It was the last owner of this table so we can free */
177 table = container_of(kref, struct em_perf_table, kref);
179 call_rcu(&table->rcu, em_destroy_table_rcu);
183 * em_table_free() - Handles safe free of the EM table when needed
184 * @table : EM table which is going to be freed
186 * No return values.
188 void em_table_free(struct em_perf_table __rcu *table)
190 kref_put(&table->kref, em_release_table_kref);
194 * em_table_alloc() - Allocate a new EM table
195 * @pd : EM performance domain for which this must be done
197 * Allocate a new EM table and initialize its kref to indicate that it
198 * has a user.
199 * Returns allocated table or NULL.
201 struct em_perf_table __rcu *em_table_alloc(struct em_perf_domain *pd)
203 struct em_perf_table __rcu *table;
204 int table_size;
206 table_size = sizeof(struct em_perf_state) * pd->nr_perf_states;
208 table = kzalloc(sizeof(*table) + table_size, GFP_KERNEL);
209 if (!table)
210 return NULL;
212 kref_init(&table->kref);
214 return table;
217 static void em_init_performance(struct device *dev, struct em_perf_domain *pd,
218 struct em_perf_state *table, int nr_states)
220 u64 fmax, max_cap;
221 int i, cpu;
223 /* This is needed only for CPUs and EAS skip other devices */
224 if (!_is_cpu_device(dev))
225 return;
227 cpu = cpumask_first(em_span_cpus(pd));
230 * Calculate the performance value for each frequency with
231 * linear relationship. The final CPU capacity might not be ready at
232 * boot time, but the EM will be updated a bit later with correct one.
234 fmax = (u64) table[nr_states - 1].frequency;
235 max_cap = (u64) arch_scale_cpu_capacity(cpu);
236 for (i = 0; i < nr_states; i++)
237 table[i].performance = div64_u64(max_cap * table[i].frequency,
238 fmax);
241 static int em_compute_costs(struct device *dev, struct em_perf_state *table,
242 struct em_data_callback *cb, int nr_states,
243 unsigned long flags)
245 unsigned long prev_cost = ULONG_MAX;
246 int i, ret;
248 /* Compute the cost of each performance state. */
249 for (i = nr_states - 1; i >= 0; i--) {
250 unsigned long power_res, cost;
252 if ((flags & EM_PERF_DOMAIN_ARTIFICIAL) && cb->get_cost) {
253 ret = cb->get_cost(dev, table[i].frequency, &cost);
254 if (ret || !cost || cost > EM_MAX_POWER) {
255 dev_err(dev, "EM: invalid cost %lu %d\n",
256 cost, ret);
257 return -EINVAL;
259 } else {
260 /* increase resolution of 'cost' precision */
261 power_res = table[i].power * 10;
262 cost = power_res / table[i].performance;
265 table[i].cost = cost;
267 if (table[i].cost >= prev_cost) {
268 table[i].flags = EM_PERF_STATE_INEFFICIENT;
269 dev_dbg(dev, "EM: OPP:%lu is inefficient\n",
270 table[i].frequency);
271 } else {
272 prev_cost = table[i].cost;
276 return 0;
280 * em_dev_compute_costs() - Calculate cost values for new runtime EM table
281 * @dev : Device for which the EM table is to be updated
282 * @table : The new EM table that is going to get the costs calculated
283 * @nr_states : Number of performance states
285 * Calculate the em_perf_state::cost values for new runtime EM table. The
286 * values are used for EAS during task placement. It also calculates and sets
287 * the efficiency flag for each performance state. When the function finish
288 * successfully the EM table is ready to be updated and used by EAS.
290 * Return 0 on success or a proper error in case of failure.
292 int em_dev_compute_costs(struct device *dev, struct em_perf_state *table,
293 int nr_states)
295 return em_compute_costs(dev, table, NULL, nr_states, 0);
299 * em_dev_update_perf_domain() - Update runtime EM table for a device
300 * @dev : Device for which the EM is to be updated
301 * @new_table : The new EM table that is going to be used from now
303 * Update EM runtime modifiable table for the @dev using the provided @table.
305 * This function uses a mutex to serialize writers, so it must not be called
306 * from a non-sleeping context.
308 * Return 0 on success or an error code on failure.
310 int em_dev_update_perf_domain(struct device *dev,
311 struct em_perf_table __rcu *new_table)
313 struct em_perf_table __rcu *old_table;
314 struct em_perf_domain *pd;
316 if (!dev)
317 return -EINVAL;
319 /* Serialize update/unregister or concurrent updates */
320 mutex_lock(&em_pd_mutex);
322 if (!dev->em_pd) {
323 mutex_unlock(&em_pd_mutex);
324 return -EINVAL;
326 pd = dev->em_pd;
328 kref_get(&new_table->kref);
330 old_table = pd->em_table;
331 rcu_assign_pointer(pd->em_table, new_table);
333 em_cpufreq_update_efficiencies(dev, new_table->state);
335 em_table_free(old_table);
337 mutex_unlock(&em_pd_mutex);
338 return 0;
340 EXPORT_SYMBOL_GPL(em_dev_update_perf_domain);
342 static int em_create_perf_table(struct device *dev, struct em_perf_domain *pd,
343 struct em_perf_state *table,
344 struct em_data_callback *cb,
345 unsigned long flags)
347 unsigned long power, freq, prev_freq = 0;
348 int nr_states = pd->nr_perf_states;
349 int i, ret;
351 /* Build the list of performance states for this performance domain */
352 for (i = 0, freq = 0; i < nr_states; i++, freq++) {
354 * active_power() is a driver callback which ceils 'freq' to
355 * lowest performance state of 'dev' above 'freq' and updates
356 * 'power' and 'freq' accordingly.
358 ret = cb->active_power(dev, &power, &freq);
359 if (ret) {
360 dev_err(dev, "EM: invalid perf. state: %d\n",
361 ret);
362 return -EINVAL;
366 * We expect the driver callback to increase the frequency for
367 * higher performance states.
369 if (freq <= prev_freq) {
370 dev_err(dev, "EM: non-increasing freq: %lu\n",
371 freq);
372 return -EINVAL;
376 * The power returned by active_state() is expected to be
377 * positive and be in range.
379 if (!power || power > EM_MAX_POWER) {
380 dev_err(dev, "EM: invalid power: %lu\n",
381 power);
382 return -EINVAL;
385 table[i].power = power;
386 table[i].frequency = prev_freq = freq;
389 em_init_performance(dev, pd, table, nr_states);
391 ret = em_compute_costs(dev, table, cb, nr_states, flags);
392 if (ret)
393 return -EINVAL;
395 return 0;
398 static int em_create_pd(struct device *dev, int nr_states,
399 struct em_data_callback *cb, cpumask_t *cpus,
400 unsigned long flags)
402 struct em_perf_table __rcu *em_table;
403 struct em_perf_domain *pd;
404 struct device *cpu_dev;
405 int cpu, ret, num_cpus;
407 if (_is_cpu_device(dev)) {
408 num_cpus = cpumask_weight(cpus);
410 /* Prevent max possible energy calculation to not overflow */
411 if (num_cpus > EM_MAX_NUM_CPUS) {
412 dev_err(dev, "EM: too many CPUs, overflow possible\n");
413 return -EINVAL;
416 pd = kzalloc(sizeof(*pd) + cpumask_size(), GFP_KERNEL);
417 if (!pd)
418 return -ENOMEM;
420 cpumask_copy(em_span_cpus(pd), cpus);
421 } else {
422 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
423 if (!pd)
424 return -ENOMEM;
427 pd->nr_perf_states = nr_states;
429 em_table = em_table_alloc(pd);
430 if (!em_table)
431 goto free_pd;
433 ret = em_create_perf_table(dev, pd, em_table->state, cb, flags);
434 if (ret)
435 goto free_pd_table;
437 rcu_assign_pointer(pd->em_table, em_table);
439 if (_is_cpu_device(dev))
440 for_each_cpu(cpu, cpus) {
441 cpu_dev = get_cpu_device(cpu);
442 cpu_dev->em_pd = pd;
445 dev->em_pd = pd;
447 return 0;
449 free_pd_table:
450 kfree(em_table);
451 free_pd:
452 kfree(pd);
453 return -EINVAL;
456 static void
457 em_cpufreq_update_efficiencies(struct device *dev, struct em_perf_state *table)
459 struct em_perf_domain *pd = dev->em_pd;
460 struct cpufreq_policy *policy;
461 int found = 0;
462 int i, cpu;
464 if (!_is_cpu_device(dev))
465 return;
467 /* Try to get a CPU which is active and in this PD */
468 cpu = cpumask_first_and(em_span_cpus(pd), cpu_active_mask);
469 if (cpu >= nr_cpu_ids) {
470 dev_warn(dev, "EM: No online CPU for CPUFreq policy\n");
471 return;
474 policy = cpufreq_cpu_get(cpu);
475 if (!policy) {
476 dev_warn(dev, "EM: Access to CPUFreq policy failed\n");
477 return;
480 for (i = 0; i < pd->nr_perf_states; i++) {
481 if (!(table[i].flags & EM_PERF_STATE_INEFFICIENT))
482 continue;
484 if (!cpufreq_table_set_inefficient(policy, table[i].frequency))
485 found++;
488 cpufreq_cpu_put(policy);
490 if (!found)
491 return;
494 * Efficiencies have been installed in CPUFreq, inefficient frequencies
495 * will be skipped. The EM can do the same.
497 pd->flags |= EM_PERF_DOMAIN_SKIP_INEFFICIENCIES;
501 * em_pd_get() - Return the performance domain for a device
502 * @dev : Device to find the performance domain for
504 * Returns the performance domain to which @dev belongs, or NULL if it doesn't
505 * exist.
507 struct em_perf_domain *em_pd_get(struct device *dev)
509 if (IS_ERR_OR_NULL(dev))
510 return NULL;
512 return dev->em_pd;
514 EXPORT_SYMBOL_GPL(em_pd_get);
517 * em_cpu_get() - Return the performance domain for a CPU
518 * @cpu : CPU to find the performance domain for
520 * Returns the performance domain to which @cpu belongs, or NULL if it doesn't
521 * exist.
523 struct em_perf_domain *em_cpu_get(int cpu)
525 struct device *cpu_dev;
527 cpu_dev = get_cpu_device(cpu);
528 if (!cpu_dev)
529 return NULL;
531 return em_pd_get(cpu_dev);
533 EXPORT_SYMBOL_GPL(em_cpu_get);
536 * em_dev_register_perf_domain() - Register the Energy Model (EM) for a device
537 * @dev : Device for which the EM is to register
538 * @nr_states : Number of performance states to register
539 * @cb : Callback functions providing the data of the Energy Model
540 * @cpus : Pointer to cpumask_t, which in case of a CPU device is
541 * obligatory. It can be taken from i.e. 'policy->cpus'. For other
542 * type of devices this should be set to NULL.
543 * @microwatts : Flag indicating that the power values are in micro-Watts or
544 * in some other scale. It must be set properly.
546 * Create Energy Model tables for a performance domain using the callbacks
547 * defined in cb.
549 * The @microwatts is important to set with correct value. Some kernel
550 * sub-systems might rely on this flag and check if all devices in the EM are
551 * using the same scale.
553 * If multiple clients register the same performance domain, all but the first
554 * registration will be ignored.
556 * Return 0 on success
558 int em_dev_register_perf_domain(struct device *dev, unsigned int nr_states,
559 struct em_data_callback *cb, cpumask_t *cpus,
560 bool microwatts)
562 unsigned long cap, prev_cap = 0;
563 unsigned long flags = 0;
564 int cpu, ret;
566 if (!dev || !nr_states || !cb)
567 return -EINVAL;
570 * Use a mutex to serialize the registration of performance domains and
571 * let the driver-defined callback functions sleep.
573 mutex_lock(&em_pd_mutex);
575 if (dev->em_pd) {
576 ret = -EEXIST;
577 goto unlock;
580 if (_is_cpu_device(dev)) {
581 if (!cpus) {
582 dev_err(dev, "EM: invalid CPU mask\n");
583 ret = -EINVAL;
584 goto unlock;
587 for_each_cpu(cpu, cpus) {
588 if (em_cpu_get(cpu)) {
589 dev_err(dev, "EM: exists for CPU%d\n", cpu);
590 ret = -EEXIST;
591 goto unlock;
594 * All CPUs of a domain must have the same
595 * micro-architecture since they all share the same
596 * table.
598 cap = arch_scale_cpu_capacity(cpu);
599 if (prev_cap && prev_cap != cap) {
600 dev_err(dev, "EM: CPUs of %*pbl must have the same capacity\n",
601 cpumask_pr_args(cpus));
603 ret = -EINVAL;
604 goto unlock;
606 prev_cap = cap;
610 if (microwatts)
611 flags |= EM_PERF_DOMAIN_MICROWATTS;
612 else if (cb->get_cost)
613 flags |= EM_PERF_DOMAIN_ARTIFICIAL;
616 * EM only supports uW (exception is artificial EM).
617 * Therefore, check and force the drivers to provide
618 * power in uW.
620 if (!microwatts && !(flags & EM_PERF_DOMAIN_ARTIFICIAL)) {
621 dev_err(dev, "EM: only supports uW power values\n");
622 ret = -EINVAL;
623 goto unlock;
626 ret = em_create_pd(dev, nr_states, cb, cpus, flags);
627 if (ret)
628 goto unlock;
630 dev->em_pd->flags |= flags;
631 dev->em_pd->min_perf_state = 0;
632 dev->em_pd->max_perf_state = nr_states - 1;
634 em_cpufreq_update_efficiencies(dev, dev->em_pd->em_table->state);
636 em_debug_create_pd(dev);
637 dev_info(dev, "EM: created perf domain\n");
639 unlock:
640 mutex_unlock(&em_pd_mutex);
642 if (_is_cpu_device(dev))
643 em_check_capacity_update();
645 return ret;
647 EXPORT_SYMBOL_GPL(em_dev_register_perf_domain);
650 * em_dev_unregister_perf_domain() - Unregister Energy Model (EM) for a device
651 * @dev : Device for which the EM is registered
653 * Unregister the EM for the specified @dev (but not a CPU device).
655 void em_dev_unregister_perf_domain(struct device *dev)
657 if (IS_ERR_OR_NULL(dev) || !dev->em_pd)
658 return;
660 if (_is_cpu_device(dev))
661 return;
664 * The mutex separates all register/unregister requests and protects
665 * from potential clean-up/setup issues in the debugfs directories.
666 * The debugfs directory name is the same as device's name.
668 mutex_lock(&em_pd_mutex);
669 em_debug_remove_pd(dev);
671 em_table_free(dev->em_pd->em_table);
673 kfree(dev->em_pd);
674 dev->em_pd = NULL;
675 mutex_unlock(&em_pd_mutex);
677 EXPORT_SYMBOL_GPL(em_dev_unregister_perf_domain);
679 static struct em_perf_table __rcu *em_table_dup(struct em_perf_domain *pd)
681 struct em_perf_table __rcu *em_table;
682 struct em_perf_state *ps, *new_ps;
683 int ps_size;
685 em_table = em_table_alloc(pd);
686 if (!em_table)
687 return NULL;
689 new_ps = em_table->state;
691 rcu_read_lock();
692 ps = em_perf_state_from_pd(pd);
693 /* Initialize data based on old table */
694 ps_size = sizeof(struct em_perf_state) * pd->nr_perf_states;
695 memcpy(new_ps, ps, ps_size);
697 rcu_read_unlock();
699 return em_table;
702 static int em_recalc_and_update(struct device *dev, struct em_perf_domain *pd,
703 struct em_perf_table __rcu *em_table)
705 int ret;
707 ret = em_compute_costs(dev, em_table->state, NULL, pd->nr_perf_states,
708 pd->flags);
709 if (ret)
710 goto free_em_table;
712 ret = em_dev_update_perf_domain(dev, em_table);
713 if (ret)
714 goto free_em_table;
717 * This is one-time-update, so give up the ownership in this updater.
718 * The EM framework has incremented the usage counter and from now
719 * will keep the reference (then free the memory when needed).
721 free_em_table:
722 em_table_free(em_table);
723 return ret;
727 * Adjustment of CPU performance values after boot, when all CPUs capacites
728 * are correctly calculated.
730 static void em_adjust_new_capacity(struct device *dev,
731 struct em_perf_domain *pd,
732 u64 max_cap)
734 struct em_perf_table __rcu *em_table;
736 em_table = em_table_dup(pd);
737 if (!em_table) {
738 dev_warn(dev, "EM: allocation failed\n");
739 return;
742 em_init_performance(dev, pd, em_table->state, pd->nr_perf_states);
744 em_recalc_and_update(dev, pd, em_table);
747 static void em_check_capacity_update(void)
749 cpumask_var_t cpu_done_mask;
750 struct em_perf_state *table;
751 struct em_perf_domain *pd;
752 unsigned long cpu_capacity;
753 int cpu;
755 if (!zalloc_cpumask_var(&cpu_done_mask, GFP_KERNEL)) {
756 pr_warn("no free memory\n");
757 return;
760 /* Check if CPUs capacity has changed than update EM */
761 for_each_possible_cpu(cpu) {
762 struct cpufreq_policy *policy;
763 unsigned long em_max_perf;
764 struct device *dev;
766 if (cpumask_test_cpu(cpu, cpu_done_mask))
767 continue;
769 policy = cpufreq_cpu_get(cpu);
770 if (!policy) {
771 pr_debug("Accessing cpu%d policy failed\n", cpu);
772 schedule_delayed_work(&em_update_work,
773 msecs_to_jiffies(1000));
774 break;
776 cpufreq_cpu_put(policy);
778 pd = em_cpu_get(cpu);
779 if (!pd || em_is_artificial(pd))
780 continue;
782 cpumask_or(cpu_done_mask, cpu_done_mask,
783 em_span_cpus(pd));
785 cpu_capacity = arch_scale_cpu_capacity(cpu);
787 rcu_read_lock();
788 table = em_perf_state_from_pd(pd);
789 em_max_perf = table[pd->nr_perf_states - 1].performance;
790 rcu_read_unlock();
793 * Check if the CPU capacity has been adjusted during boot
794 * and trigger the update for new performance values.
796 if (em_max_perf == cpu_capacity)
797 continue;
799 pr_debug("updating cpu%d cpu_cap=%lu old capacity=%lu\n",
800 cpu, cpu_capacity, em_max_perf);
802 dev = get_cpu_device(cpu);
803 em_adjust_new_capacity(dev, pd, cpu_capacity);
806 free_cpumask_var(cpu_done_mask);
809 static void em_update_workfn(struct work_struct *work)
811 em_check_capacity_update();
815 * em_dev_update_chip_binning() - Update Energy Model after the new voltage
816 * information is present in the OPPs.
817 * @dev : Device for which the Energy Model has to be updated.
819 * This function allows to update easily the EM with new values available in
820 * the OPP framework and DT. It can be used after the chip has been properly
821 * verified by device drivers and the voltages adjusted for the 'chip binning'.
823 int em_dev_update_chip_binning(struct device *dev)
825 struct em_perf_table __rcu *em_table;
826 struct em_perf_domain *pd;
827 int i, ret;
829 if (IS_ERR_OR_NULL(dev))
830 return -EINVAL;
832 pd = em_pd_get(dev);
833 if (!pd) {
834 dev_warn(dev, "Couldn't find Energy Model\n");
835 return -EINVAL;
838 em_table = em_table_dup(pd);
839 if (!em_table) {
840 dev_warn(dev, "EM: allocation failed\n");
841 return -ENOMEM;
844 /* Update power values which might change due to new voltage in OPPs */
845 for (i = 0; i < pd->nr_perf_states; i++) {
846 unsigned long freq = em_table->state[i].frequency;
847 unsigned long power;
849 ret = dev_pm_opp_calc_power(dev, &power, &freq);
850 if (ret) {
851 em_table_free(em_table);
852 return ret;
855 em_table->state[i].power = power;
858 return em_recalc_and_update(dev, pd, em_table);
860 EXPORT_SYMBOL_GPL(em_dev_update_chip_binning);
864 * em_update_performance_limits() - Update Energy Model with performance
865 * limits information.
866 * @pd : Performance Domain with EM that has to be updated.
867 * @freq_min_khz : New minimum allowed frequency for this device.
868 * @freq_max_khz : New maximum allowed frequency for this device.
870 * This function allows to update the EM with information about available
871 * performance levels. It takes the minimum and maximum frequency in kHz
872 * and does internal translation to performance levels.
873 * Returns 0 on success or -EINVAL when failed.
875 int em_update_performance_limits(struct em_perf_domain *pd,
876 unsigned long freq_min_khz, unsigned long freq_max_khz)
878 struct em_perf_state *table;
879 int min_ps = -1;
880 int max_ps = -1;
881 int i;
883 if (!pd)
884 return -EINVAL;
886 rcu_read_lock();
887 table = em_perf_state_from_pd(pd);
889 for (i = 0; i < pd->nr_perf_states; i++) {
890 if (freq_min_khz == table[i].frequency)
891 min_ps = i;
892 if (freq_max_khz == table[i].frequency)
893 max_ps = i;
895 rcu_read_unlock();
897 /* Only update when both are found and sane */
898 if (min_ps < 0 || max_ps < 0 || max_ps < min_ps)
899 return -EINVAL;
902 /* Guard simultaneous updates and make them atomic */
903 mutex_lock(&em_pd_mutex);
904 pd->min_perf_state = min_ps;
905 pd->max_perf_state = max_ps;
906 mutex_unlock(&em_pd_mutex);
908 return 0;
910 EXPORT_SYMBOL_GPL(em_update_performance_limits);