Nishanth Menon | a0dd7b7 | 2014-05-05 08:33:50 -0500 | [diff] [blame] | 1 | /* |
Viresh Kumar | 33692dc | 2015-09-04 13:47:25 +0530 | [diff] [blame] | 2 | * Generic OPP helper interface for CPU device |
Nishanth Menon | a0dd7b7 | 2014-05-05 08:33:50 -0500 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2009-2014 Texas Instruments Incorporated. |
| 5 | * Nishanth Menon |
| 6 | * Romit Dasgupta |
| 7 | * Kevin Hilman |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
Viresh Kumar | d6d2a52 | 2015-10-17 09:45:18 +0530 | [diff] [blame] | 13 | |
| 14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 15 | |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 16 | #include <linux/cpu.h> |
Nishanth Menon | a0dd7b7 | 2014-05-05 08:33:50 -0500 | [diff] [blame] | 17 | #include <linux/cpufreq.h> |
Nishanth Menon | a0dd7b7 | 2014-05-05 08:33:50 -0500 | [diff] [blame] | 18 | #include <linux/err.h> |
| 19 | #include <linux/errno.h> |
| 20 | #include <linux/export.h> |
Nishanth Menon | a0dd7b7 | 2014-05-05 08:33:50 -0500 | [diff] [blame] | 21 | #include <linux/slab.h> |
| 22 | |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 23 | #include "opp.h" |
| 24 | |
Viresh Kumar | 33692dc | 2015-09-04 13:47:25 +0530 | [diff] [blame] | 25 | #ifdef CONFIG_CPU_FREQ |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 26 | |
Nishanth Menon | a0dd7b7 | 2014-05-05 08:33:50 -0500 | [diff] [blame] | 27 | /** |
| 28 | * dev_pm_opp_init_cpufreq_table() - create a cpufreq table for a device |
| 29 | * @dev: device for which we do this operation |
| 30 | * @table: Cpufreq table returned back to caller |
| 31 | * |
| 32 | * Generate a cpufreq table for a provided device- this assumes that the |
Viresh Kumar | 2c2709d | 2016-02-16 14:17:53 +0530 | [diff] [blame] | 33 | * opp table is already initialized and ready for usage. |
Nishanth Menon | a0dd7b7 | 2014-05-05 08:33:50 -0500 | [diff] [blame] | 34 | * |
| 35 | * This function allocates required memory for the cpufreq table. It is |
| 36 | * expected that the caller does the required maintenance such as freeing |
| 37 | * the table as required. |
| 38 | * |
| 39 | * Returns -EINVAL for bad pointers, -ENODEV if the device is not found, -ENOMEM |
| 40 | * if no memory available for the operation (table is not populated), returns 0 |
| 41 | * if successful and table is populated. |
| 42 | * |
| 43 | * WARNING: It is important for the callers to ensure refreshing their copy of |
| 44 | * the table if any of the mentioned functions have been invoked in the interim. |
Nishanth Menon | a0dd7b7 | 2014-05-05 08:33:50 -0500 | [diff] [blame] | 45 | */ |
| 46 | int dev_pm_opp_init_cpufreq_table(struct device *dev, |
| 47 | struct cpufreq_frequency_table **table) |
| 48 | { |
| 49 | struct dev_pm_opp *opp; |
| 50 | struct cpufreq_frequency_table *freq_table = NULL; |
| 51 | int i, max_opps, ret = 0; |
| 52 | unsigned long rate; |
| 53 | |
Nishanth Menon | a0dd7b7 | 2014-05-05 08:33:50 -0500 | [diff] [blame] | 54 | max_opps = dev_pm_opp_get_opp_count(dev); |
Viresh Kumar | 8a31d9d9 | 2017-01-23 10:11:47 +0530 | [diff] [blame] | 55 | if (max_opps <= 0) |
| 56 | return max_opps ? max_opps : -ENODATA; |
Nishanth Menon | a0dd7b7 | 2014-05-05 08:33:50 -0500 | [diff] [blame] | 57 | |
Jia-Ju Bai | 4a823c0 | 2018-01-26 16:48:49 +0800 | [diff] [blame] | 58 | freq_table = kcalloc((max_opps + 1), sizeof(*freq_table), GFP_KERNEL); |
Viresh Kumar | 8a31d9d9 | 2017-01-23 10:11:47 +0530 | [diff] [blame] | 59 | if (!freq_table) |
| 60 | return -ENOMEM; |
Nishanth Menon | a0dd7b7 | 2014-05-05 08:33:50 -0500 | [diff] [blame] | 61 | |
| 62 | for (i = 0, rate = 0; i < max_opps; i++, rate++) { |
| 63 | /* find next rate */ |
| 64 | opp = dev_pm_opp_find_freq_ceil(dev, &rate); |
| 65 | if (IS_ERR(opp)) { |
| 66 | ret = PTR_ERR(opp); |
| 67 | goto out; |
| 68 | } |
| 69 | freq_table[i].driver_data = i; |
| 70 | freq_table[i].frequency = rate / 1000; |
Bartlomiej Zolnierkiewicz | 79eea44 | 2015-07-29 16:23:08 +0530 | [diff] [blame] | 71 | |
| 72 | /* Is Boost/turbo opp ? */ |
| 73 | if (dev_pm_opp_is_turbo(opp)) |
| 74 | freq_table[i].flags = CPUFREQ_BOOST_FREQ; |
Viresh Kumar | 8a31d9d9 | 2017-01-23 10:11:47 +0530 | [diff] [blame] | 75 | |
| 76 | dev_pm_opp_put(opp); |
Nishanth Menon | a0dd7b7 | 2014-05-05 08:33:50 -0500 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | freq_table[i].driver_data = i; |
| 80 | freq_table[i].frequency = CPUFREQ_TABLE_END; |
| 81 | |
| 82 | *table = &freq_table[0]; |
| 83 | |
| 84 | out: |
Nishanth Menon | a0dd7b7 | 2014-05-05 08:33:50 -0500 | [diff] [blame] | 85 | if (ret) |
| 86 | kfree(freq_table); |
| 87 | |
| 88 | return ret; |
| 89 | } |
| 90 | EXPORT_SYMBOL_GPL(dev_pm_opp_init_cpufreq_table); |
| 91 | |
| 92 | /** |
| 93 | * dev_pm_opp_free_cpufreq_table() - free the cpufreq table |
| 94 | * @dev: device for which we do this operation |
| 95 | * @table: table to free |
| 96 | * |
| 97 | * Free up the table allocated by dev_pm_opp_init_cpufreq_table |
| 98 | */ |
| 99 | void dev_pm_opp_free_cpufreq_table(struct device *dev, |
| 100 | struct cpufreq_frequency_table **table) |
| 101 | { |
| 102 | if (!table) |
| 103 | return; |
| 104 | |
| 105 | kfree(*table); |
| 106 | *table = NULL; |
| 107 | } |
| 108 | EXPORT_SYMBOL_GPL(dev_pm_opp_free_cpufreq_table); |
Viresh Kumar | 33692dc | 2015-09-04 13:47:25 +0530 | [diff] [blame] | 109 | #endif /* CONFIG_CPU_FREQ */ |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 110 | |
Viresh Kumar | f47b72a | 2016-05-05 16:20:33 +0530 | [diff] [blame] | 111 | void _dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask, bool of) |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 112 | { |
| 113 | struct device *cpu_dev; |
| 114 | int cpu; |
| 115 | |
| 116 | WARN_ON(cpumask_empty(cpumask)); |
| 117 | |
| 118 | for_each_cpu(cpu, cpumask) { |
| 119 | cpu_dev = get_cpu_device(cpu); |
| 120 | if (!cpu_dev) { |
| 121 | pr_err("%s: failed to get cpu%d device\n", __func__, |
| 122 | cpu); |
| 123 | continue; |
| 124 | } |
| 125 | |
Sudeep Holla | 411466c | 2016-05-03 15:05:04 +0100 | [diff] [blame] | 126 | if (of) |
| 127 | dev_pm_opp_of_remove_table(cpu_dev); |
| 128 | else |
| 129 | dev_pm_opp_remove_table(cpu_dev); |
Viresh Kumar | f59d3ee | 2015-09-04 13:47:26 +0530 | [diff] [blame] | 130 | } |
| 131 | } |
Sudeep Holla | 411466c | 2016-05-03 15:05:04 +0100 | [diff] [blame] | 132 | |
| 133 | /** |
| 134 | * dev_pm_opp_cpumask_remove_table() - Removes OPP table for @cpumask |
| 135 | * @cpumask: cpumask for which OPP table needs to be removed |
| 136 | * |
| 137 | * This removes the OPP tables for CPUs present in the @cpumask. |
| 138 | * This should be used to remove all the OPPs entries associated with |
| 139 | * the cpus in @cpumask. |
Sudeep Holla | 411466c | 2016-05-03 15:05:04 +0100 | [diff] [blame] | 140 | */ |
| 141 | void dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask) |
| 142 | { |
| 143 | _dev_pm_opp_cpumask_remove_table(cpumask, false); |
| 144 | } |
| 145 | EXPORT_SYMBOL_GPL(dev_pm_opp_cpumask_remove_table); |
| 146 | |
Viresh Kumar | 2c93104 | 2016-04-21 14:28:56 +0530 | [diff] [blame] | 147 | /** |
| 148 | * dev_pm_opp_set_sharing_cpus() - Mark OPP table as shared by few CPUs |
| 149 | * @cpu_dev: CPU device for which we do this operation |
| 150 | * @cpumask: cpumask of the CPUs which share the OPP table with @cpu_dev |
| 151 | * |
| 152 | * This marks OPP table of the @cpu_dev as shared by the CPUs present in |
| 153 | * @cpumask. |
| 154 | * |
| 155 | * Returns -ENODEV if OPP table isn't already present. |
Viresh Kumar | 2c93104 | 2016-04-21 14:28:56 +0530 | [diff] [blame] | 156 | */ |
Viresh Kumar | dde370b | 2016-04-27 08:52:22 +0530 | [diff] [blame] | 157 | int dev_pm_opp_set_sharing_cpus(struct device *cpu_dev, |
Arnd Bergmann | ddbb74b | 2016-04-30 13:33:29 +0200 | [diff] [blame] | 158 | const struct cpumask *cpumask) |
Viresh Kumar | 2c93104 | 2016-04-21 14:28:56 +0530 | [diff] [blame] | 159 | { |
| 160 | struct opp_device *opp_dev; |
| 161 | struct opp_table *opp_table; |
| 162 | struct device *dev; |
| 163 | int cpu, ret = 0; |
| 164 | |
Viresh Kumar | 2c93104 | 2016-04-21 14:28:56 +0530 | [diff] [blame] | 165 | opp_table = _find_opp_table(cpu_dev); |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 166 | if (IS_ERR(opp_table)) |
| 167 | return PTR_ERR(opp_table); |
Viresh Kumar | 2c93104 | 2016-04-21 14:28:56 +0530 | [diff] [blame] | 168 | |
| 169 | for_each_cpu(cpu, cpumask) { |
| 170 | if (cpu == cpu_dev->id) |
| 171 | continue; |
| 172 | |
| 173 | dev = get_cpu_device(cpu); |
| 174 | if (!dev) { |
| 175 | dev_err(cpu_dev, "%s: failed to get cpu%d device\n", |
| 176 | __func__, cpu); |
| 177 | continue; |
| 178 | } |
| 179 | |
| 180 | opp_dev = _add_opp_dev(dev, opp_table); |
| 181 | if (!opp_dev) { |
| 182 | dev_err(dev, "%s: failed to add opp-dev for cpu%d device\n", |
| 183 | __func__, cpu); |
| 184 | continue; |
| 185 | } |
Viresh Kumar | 46e7a4e | 2016-04-21 14:28:57 +0530 | [diff] [blame] | 186 | |
| 187 | /* Mark opp-table as multiple CPUs are sharing it now */ |
Viresh Kumar | 79ee2e8 | 2016-06-16 19:03:11 +0530 | [diff] [blame] | 188 | opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED; |
Viresh Kumar | 2c93104 | 2016-04-21 14:28:56 +0530 | [diff] [blame] | 189 | } |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 190 | |
| 191 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | 2c93104 | 2016-04-21 14:28:56 +0530 | [diff] [blame] | 192 | |
| 193 | return ret; |
| 194 | } |
| 195 | EXPORT_SYMBOL_GPL(dev_pm_opp_set_sharing_cpus); |
Viresh Kumar | 6f707da | 2016-04-27 08:52:23 +0530 | [diff] [blame] | 196 | |
| 197 | /** |
| 198 | * dev_pm_opp_get_sharing_cpus() - Get cpumask of CPUs sharing OPPs with @cpu_dev |
| 199 | * @cpu_dev: CPU device for which we do this operation |
| 200 | * @cpumask: cpumask to update with information of sharing CPUs |
| 201 | * |
| 202 | * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev. |
| 203 | * |
Viresh Kumar | 79ee2e8 | 2016-06-16 19:03:11 +0530 | [diff] [blame] | 204 | * Returns -ENODEV if OPP table isn't already present and -EINVAL if the OPP |
| 205 | * table's status is access-unknown. |
Viresh Kumar | 6f707da | 2016-04-27 08:52:23 +0530 | [diff] [blame] | 206 | */ |
Arnd Bergmann | ddbb74b | 2016-04-30 13:33:29 +0200 | [diff] [blame] | 207 | int dev_pm_opp_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask) |
Viresh Kumar | 6f707da | 2016-04-27 08:52:23 +0530 | [diff] [blame] | 208 | { |
| 209 | struct opp_device *opp_dev; |
| 210 | struct opp_table *opp_table; |
| 211 | int ret = 0; |
| 212 | |
Viresh Kumar | 6f707da | 2016-04-27 08:52:23 +0530 | [diff] [blame] | 213 | opp_table = _find_opp_table(cpu_dev); |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 214 | if (IS_ERR(opp_table)) |
| 215 | return PTR_ERR(opp_table); |
Viresh Kumar | 6f707da | 2016-04-27 08:52:23 +0530 | [diff] [blame] | 216 | |
Viresh Kumar | 79ee2e8 | 2016-06-16 19:03:11 +0530 | [diff] [blame] | 217 | if (opp_table->shared_opp == OPP_TABLE_ACCESS_UNKNOWN) { |
| 218 | ret = -EINVAL; |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 219 | goto put_opp_table; |
Viresh Kumar | 79ee2e8 | 2016-06-16 19:03:11 +0530 | [diff] [blame] | 220 | } |
| 221 | |
Viresh Kumar | 6f707da | 2016-04-27 08:52:23 +0530 | [diff] [blame] | 222 | cpumask_clear(cpumask); |
| 223 | |
Viresh Kumar | 79ee2e8 | 2016-06-16 19:03:11 +0530 | [diff] [blame] | 224 | if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) { |
Viresh Kumar | 6f707da | 2016-04-27 08:52:23 +0530 | [diff] [blame] | 225 | list_for_each_entry(opp_dev, &opp_table->dev_list, node) |
| 226 | cpumask_set_cpu(opp_dev->dev->id, cpumask); |
| 227 | } else { |
| 228 | cpumask_set_cpu(cpu_dev->id, cpumask); |
| 229 | } |
| 230 | |
Viresh Kumar | 5b650b3 | 2017-01-23 10:11:48 +0530 | [diff] [blame] | 231 | put_opp_table: |
| 232 | dev_pm_opp_put_opp_table(opp_table); |
Viresh Kumar | 6f707da | 2016-04-27 08:52:23 +0530 | [diff] [blame] | 233 | |
| 234 | return ret; |
| 235 | } |
| 236 | EXPORT_SYMBOL_GPL(dev_pm_opp_get_sharing_cpus); |