Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Energy Model of devices
0004  *
0005  * Copyright (c) 2018-2021, Arm ltd.
0006  * Written by: Quentin Perret, Arm ltd.
0007  * Improvements provided by: Lukasz Luba, Arm ltd.
0008  */
0009 
0010 #define pr_fmt(fmt) "energy_model: " fmt
0011 
0012 #include <linux/cpu.h>
0013 #include <linux/cpufreq.h>
0014 #include <linux/cpumask.h>
0015 #include <linux/debugfs.h>
0016 #include <linux/energy_model.h>
0017 #include <linux/sched/topology.h>
0018 #include <linux/slab.h>
0019 
0020 /*
0021  * Mutex serializing the registrations of performance domains and letting
0022  * callbacks defined by drivers sleep.
0023  */
0024 static DEFINE_MUTEX(em_pd_mutex);
0025 
0026 static bool _is_cpu_device(struct device *dev)
0027 {
0028     return (dev->bus == &cpu_subsys);
0029 }
0030 
0031 #ifdef CONFIG_DEBUG_FS
0032 static struct dentry *rootdir;
0033 
0034 static void em_debug_create_ps(struct em_perf_state *ps, struct dentry *pd)
0035 {
0036     struct dentry *d;
0037     char name[24];
0038 
0039     snprintf(name, sizeof(name), "ps:%lu", ps->frequency);
0040 
0041     /* Create per-ps directory */
0042     d = debugfs_create_dir(name, pd);
0043     debugfs_create_ulong("frequency", 0444, d, &ps->frequency);
0044     debugfs_create_ulong("power", 0444, d, &ps->power);
0045     debugfs_create_ulong("cost", 0444, d, &ps->cost);
0046     debugfs_create_ulong("inefficient", 0444, d, &ps->flags);
0047 }
0048 
0049 static int em_debug_cpus_show(struct seq_file *s, void *unused)
0050 {
0051     seq_printf(s, "%*pbl\n", cpumask_pr_args(to_cpumask(s->private)));
0052 
0053     return 0;
0054 }
0055 DEFINE_SHOW_ATTRIBUTE(em_debug_cpus);
0056 
0057 static int em_debug_flags_show(struct seq_file *s, void *unused)
0058 {
0059     struct em_perf_domain *pd = s->private;
0060 
0061     seq_printf(s, "%#lx\n", pd->flags);
0062 
0063     return 0;
0064 }
0065 DEFINE_SHOW_ATTRIBUTE(em_debug_flags);
0066 
0067 static void em_debug_create_pd(struct device *dev)
0068 {
0069     struct dentry *d;
0070     int i;
0071 
0072     /* Create the directory of the performance domain */
0073     d = debugfs_create_dir(dev_name(dev), rootdir);
0074 
0075     if (_is_cpu_device(dev))
0076         debugfs_create_file("cpus", 0444, d, dev->em_pd->cpus,
0077                     &em_debug_cpus_fops);
0078 
0079     debugfs_create_file("flags", 0444, d, dev->em_pd,
0080                 &em_debug_flags_fops);
0081 
0082     /* Create a sub-directory for each performance state */
0083     for (i = 0; i < dev->em_pd->nr_perf_states; i++)
0084         em_debug_create_ps(&dev->em_pd->table[i], d);
0085 
0086 }
0087 
0088 static void em_debug_remove_pd(struct device *dev)
0089 {
0090     struct dentry *debug_dir;
0091 
0092     debug_dir = debugfs_lookup(dev_name(dev), rootdir);
0093     debugfs_remove_recursive(debug_dir);
0094 }
0095 
0096 static int __init em_debug_init(void)
0097 {
0098     /* Create /sys/kernel/debug/energy_model directory */
0099     rootdir = debugfs_create_dir("energy_model", NULL);
0100 
0101     return 0;
0102 }
0103 fs_initcall(em_debug_init);
0104 #else /* CONFIG_DEBUG_FS */
0105 static void em_debug_create_pd(struct device *dev) {}
0106 static void em_debug_remove_pd(struct device *dev) {}
0107 #endif
0108 
0109 static int em_create_perf_table(struct device *dev, struct em_perf_domain *pd,
0110                 int nr_states, struct em_data_callback *cb,
0111                 unsigned long flags)
0112 {
0113     unsigned long power, freq, prev_freq = 0, prev_cost = ULONG_MAX;
0114     struct em_perf_state *table;
0115     int i, ret;
0116     u64 fmax;
0117 
0118     table = kcalloc(nr_states, sizeof(*table), GFP_KERNEL);
0119     if (!table)
0120         return -ENOMEM;
0121 
0122     /* Build the list of performance states for this performance domain */
0123     for (i = 0, freq = 0; i < nr_states; i++, freq++) {
0124         /*
0125          * active_power() is a driver callback which ceils 'freq' to
0126          * lowest performance state of 'dev' above 'freq' and updates
0127          * 'power' and 'freq' accordingly.
0128          */
0129         ret = cb->active_power(dev, &power, &freq);
0130         if (ret) {
0131             dev_err(dev, "EM: invalid perf. state: %d\n",
0132                 ret);
0133             goto free_ps_table;
0134         }
0135 
0136         /*
0137          * We expect the driver callback to increase the frequency for
0138          * higher performance states.
0139          */
0140         if (freq <= prev_freq) {
0141             dev_err(dev, "EM: non-increasing freq: %lu\n",
0142                 freq);
0143             goto free_ps_table;
0144         }
0145 
0146         /*
0147          * The power returned by active_state() is expected to be
0148          * positive and be in range.
0149          */
0150         if (!power || power > EM_MAX_POWER) {
0151             dev_err(dev, "EM: invalid power: %lu\n",
0152                 power);
0153             goto free_ps_table;
0154         }
0155 
0156         table[i].power = power;
0157         table[i].frequency = prev_freq = freq;
0158     }
0159 
0160     /* Compute the cost of each performance state. */
0161     fmax = (u64) table[nr_states - 1].frequency;
0162     for (i = nr_states - 1; i >= 0; i--) {
0163         unsigned long power_res, cost;
0164 
0165         if (flags & EM_PERF_DOMAIN_ARTIFICIAL) {
0166             ret = cb->get_cost(dev, table[i].frequency, &cost);
0167             if (ret || !cost || cost > EM_MAX_POWER) {
0168                 dev_err(dev, "EM: invalid cost %lu %d\n",
0169                     cost, ret);
0170                 goto free_ps_table;
0171             }
0172         } else {
0173             power_res = table[i].power;
0174             cost = div64_u64(fmax * power_res, table[i].frequency);
0175         }
0176 
0177         table[i].cost = cost;
0178 
0179         if (table[i].cost >= prev_cost) {
0180             table[i].flags = EM_PERF_STATE_INEFFICIENT;
0181             dev_dbg(dev, "EM: OPP:%lu is inefficient\n",
0182                 table[i].frequency);
0183         } else {
0184             prev_cost = table[i].cost;
0185         }
0186     }
0187 
0188     pd->table = table;
0189     pd->nr_perf_states = nr_states;
0190 
0191     return 0;
0192 
0193 free_ps_table:
0194     kfree(table);
0195     return -EINVAL;
0196 }
0197 
0198 static int em_create_pd(struct device *dev, int nr_states,
0199             struct em_data_callback *cb, cpumask_t *cpus,
0200             unsigned long flags)
0201 {
0202     struct em_perf_domain *pd;
0203     struct device *cpu_dev;
0204     int cpu, ret, num_cpus;
0205 
0206     if (_is_cpu_device(dev)) {
0207         num_cpus = cpumask_weight(cpus);
0208 
0209         /* Prevent max possible energy calculation to not overflow */
0210         if (num_cpus > EM_MAX_NUM_CPUS) {
0211             dev_err(dev, "EM: too many CPUs, overflow possible\n");
0212             return -EINVAL;
0213         }
0214 
0215         pd = kzalloc(sizeof(*pd) + cpumask_size(), GFP_KERNEL);
0216         if (!pd)
0217             return -ENOMEM;
0218 
0219         cpumask_copy(em_span_cpus(pd), cpus);
0220     } else {
0221         pd = kzalloc(sizeof(*pd), GFP_KERNEL);
0222         if (!pd)
0223             return -ENOMEM;
0224     }
0225 
0226     ret = em_create_perf_table(dev, pd, nr_states, cb, flags);
0227     if (ret) {
0228         kfree(pd);
0229         return ret;
0230     }
0231 
0232     if (_is_cpu_device(dev))
0233         for_each_cpu(cpu, cpus) {
0234             cpu_dev = get_cpu_device(cpu);
0235             cpu_dev->em_pd = pd;
0236         }
0237 
0238     dev->em_pd = pd;
0239 
0240     return 0;
0241 }
0242 
0243 static void em_cpufreq_update_efficiencies(struct device *dev)
0244 {
0245     struct em_perf_domain *pd = dev->em_pd;
0246     struct em_perf_state *table;
0247     struct cpufreq_policy *policy;
0248     int found = 0;
0249     int i;
0250 
0251     if (!_is_cpu_device(dev) || !pd)
0252         return;
0253 
0254     policy = cpufreq_cpu_get(cpumask_first(em_span_cpus(pd)));
0255     if (!policy) {
0256         dev_warn(dev, "EM: Access to CPUFreq policy failed");
0257         return;
0258     }
0259 
0260     table = pd->table;
0261 
0262     for (i = 0; i < pd->nr_perf_states; i++) {
0263         if (!(table[i].flags & EM_PERF_STATE_INEFFICIENT))
0264             continue;
0265 
0266         if (!cpufreq_table_set_inefficient(policy, table[i].frequency))
0267             found++;
0268     }
0269 
0270     cpufreq_cpu_put(policy);
0271 
0272     if (!found)
0273         return;
0274 
0275     /*
0276      * Efficiencies have been installed in CPUFreq, inefficient frequencies
0277      * will be skipped. The EM can do the same.
0278      */
0279     pd->flags |= EM_PERF_DOMAIN_SKIP_INEFFICIENCIES;
0280 }
0281 
0282 /**
0283  * em_pd_get() - Return the performance domain for a device
0284  * @dev : Device to find the performance domain for
0285  *
0286  * Returns the performance domain to which @dev belongs, or NULL if it doesn't
0287  * exist.
0288  */
0289 struct em_perf_domain *em_pd_get(struct device *dev)
0290 {
0291     if (IS_ERR_OR_NULL(dev))
0292         return NULL;
0293 
0294     return dev->em_pd;
0295 }
0296 EXPORT_SYMBOL_GPL(em_pd_get);
0297 
0298 /**
0299  * em_cpu_get() - Return the performance domain for a CPU
0300  * @cpu : CPU to find the performance domain for
0301  *
0302  * Returns the performance domain to which @cpu belongs, or NULL if it doesn't
0303  * exist.
0304  */
0305 struct em_perf_domain *em_cpu_get(int cpu)
0306 {
0307     struct device *cpu_dev;
0308 
0309     cpu_dev = get_cpu_device(cpu);
0310     if (!cpu_dev)
0311         return NULL;
0312 
0313     return em_pd_get(cpu_dev);
0314 }
0315 EXPORT_SYMBOL_GPL(em_cpu_get);
0316 
0317 /**
0318  * em_dev_register_perf_domain() - Register the Energy Model (EM) for a device
0319  * @dev     : Device for which the EM is to register
0320  * @nr_states   : Number of performance states to register
0321  * @cb      : Callback functions providing the data of the Energy Model
0322  * @cpus    : Pointer to cpumask_t, which in case of a CPU device is
0323  *      obligatory. It can be taken from i.e. 'policy->cpus'. For other
0324  *      type of devices this should be set to NULL.
0325  * @microwatts  : Flag indicating that the power values are in micro-Watts or
0326  *      in some other scale. It must be set properly.
0327  *
0328  * Create Energy Model tables for a performance domain using the callbacks
0329  * defined in cb.
0330  *
0331  * The @microwatts is important to set with correct value. Some kernel
0332  * sub-systems might rely on this flag and check if all devices in the EM are
0333  * using the same scale.
0334  *
0335  * If multiple clients register the same performance domain, all but the first
0336  * registration will be ignored.
0337  *
0338  * Return 0 on success
0339  */
0340 int em_dev_register_perf_domain(struct device *dev, unsigned int nr_states,
0341                 struct em_data_callback *cb, cpumask_t *cpus,
0342                 bool microwatts)
0343 {
0344     unsigned long cap, prev_cap = 0;
0345     unsigned long flags = 0;
0346     int cpu, ret;
0347 
0348     if (!dev || !nr_states || !cb)
0349         return -EINVAL;
0350 
0351     /*
0352      * Use a mutex to serialize the registration of performance domains and
0353      * let the driver-defined callback functions sleep.
0354      */
0355     mutex_lock(&em_pd_mutex);
0356 
0357     if (dev->em_pd) {
0358         ret = -EEXIST;
0359         goto unlock;
0360     }
0361 
0362     if (_is_cpu_device(dev)) {
0363         if (!cpus) {
0364             dev_err(dev, "EM: invalid CPU mask\n");
0365             ret = -EINVAL;
0366             goto unlock;
0367         }
0368 
0369         for_each_cpu(cpu, cpus) {
0370             if (em_cpu_get(cpu)) {
0371                 dev_err(dev, "EM: exists for CPU%d\n", cpu);
0372                 ret = -EEXIST;
0373                 goto unlock;
0374             }
0375             /*
0376              * All CPUs of a domain must have the same
0377              * micro-architecture since they all share the same
0378              * table.
0379              */
0380             cap = arch_scale_cpu_capacity(cpu);
0381             if (prev_cap && prev_cap != cap) {
0382                 dev_err(dev, "EM: CPUs of %*pbl must have the same capacity\n",
0383                     cpumask_pr_args(cpus));
0384 
0385                 ret = -EINVAL;
0386                 goto unlock;
0387             }
0388             prev_cap = cap;
0389         }
0390     }
0391 
0392     if (microwatts)
0393         flags |= EM_PERF_DOMAIN_MICROWATTS;
0394     else if (cb->get_cost)
0395         flags |= EM_PERF_DOMAIN_ARTIFICIAL;
0396 
0397     ret = em_create_pd(dev, nr_states, cb, cpus, flags);
0398     if (ret)
0399         goto unlock;
0400 
0401     dev->em_pd->flags |= flags;
0402 
0403     em_cpufreq_update_efficiencies(dev);
0404 
0405     em_debug_create_pd(dev);
0406     dev_info(dev, "EM: created perf domain\n");
0407 
0408 unlock:
0409     mutex_unlock(&em_pd_mutex);
0410     return ret;
0411 }
0412 EXPORT_SYMBOL_GPL(em_dev_register_perf_domain);
0413 
0414 /**
0415  * em_dev_unregister_perf_domain() - Unregister Energy Model (EM) for a device
0416  * @dev     : Device for which the EM is registered
0417  *
0418  * Unregister the EM for the specified @dev (but not a CPU device).
0419  */
0420 void em_dev_unregister_perf_domain(struct device *dev)
0421 {
0422     if (IS_ERR_OR_NULL(dev) || !dev->em_pd)
0423         return;
0424 
0425     if (_is_cpu_device(dev))
0426         return;
0427 
0428     /*
0429      * The mutex separates all register/unregister requests and protects
0430      * from potential clean-up/setup issues in the debugfs directories.
0431      * The debugfs directory name is the same as device's name.
0432      */
0433     mutex_lock(&em_pd_mutex);
0434     em_debug_remove_pd(dev);
0435 
0436     kfree(dev->em_pd->table);
0437     kfree(dev->em_pd);
0438     dev->em_pd = NULL;
0439     mutex_unlock(&em_pd_mutex);
0440 }
0441 EXPORT_SYMBOL_GPL(em_dev_unregister_perf_domain);