Kyle Swenson | 8d8f654 | 2021-03-15 11:02:55 -0600 | [diff] [blame^] | 1 | /* |
| 2 | * Generic OPP helper interface for CPU device |
| 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 | */ |
| 13 | |
| 14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 15 | |
| 16 | #include <linux/cpu.h> |
| 17 | #include <linux/cpufreq.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/errno.h> |
| 20 | #include <linux/export.h> |
| 21 | #include <linux/of.h> |
| 22 | #include <linux/slab.h> |
| 23 | |
| 24 | #include "opp.h" |
| 25 | |
| 26 | #ifdef CONFIG_CPU_FREQ |
| 27 | |
| 28 | /** |
| 29 | * dev_pm_opp_init_cpufreq_table() - create a cpufreq table for a device |
| 30 | * @dev: device for which we do this operation |
| 31 | * @table: Cpufreq table returned back to caller |
| 32 | * |
| 33 | * Generate a cpufreq table for a provided device- this assumes that the |
| 34 | * opp list is already initialized and ready for usage. |
| 35 | * |
| 36 | * This function allocates required memory for the cpufreq table. It is |
| 37 | * expected that the caller does the required maintenance such as freeing |
| 38 | * the table as required. |
| 39 | * |
| 40 | * Returns -EINVAL for bad pointers, -ENODEV if the device is not found, -ENOMEM |
| 41 | * if no memory available for the operation (table is not populated), returns 0 |
| 42 | * if successful and table is populated. |
| 43 | * |
| 44 | * WARNING: It is important for the callers to ensure refreshing their copy of |
| 45 | * the table if any of the mentioned functions have been invoked in the interim. |
| 46 | * |
| 47 | * Locking: The internal device_opp and opp structures are RCU protected. |
| 48 | * Since we just use the regular accessor functions to access the internal data |
| 49 | * structures, we use RCU read lock inside this function. As a result, users of |
| 50 | * this function DONOT need to use explicit locks for invoking. |
| 51 | */ |
| 52 | int dev_pm_opp_init_cpufreq_table(struct device *dev, |
| 53 | struct cpufreq_frequency_table **table) |
| 54 | { |
| 55 | struct dev_pm_opp *opp; |
| 56 | struct cpufreq_frequency_table *freq_table = NULL; |
| 57 | int i, max_opps, ret = 0; |
| 58 | unsigned long rate; |
| 59 | |
| 60 | rcu_read_lock(); |
| 61 | |
| 62 | max_opps = dev_pm_opp_get_opp_count(dev); |
| 63 | if (max_opps <= 0) { |
| 64 | ret = max_opps ? max_opps : -ENODATA; |
| 65 | goto out; |
| 66 | } |
| 67 | |
| 68 | freq_table = kcalloc((max_opps + 1), sizeof(*freq_table), GFP_ATOMIC); |
| 69 | if (!freq_table) { |
| 70 | ret = -ENOMEM; |
| 71 | goto out; |
| 72 | } |
| 73 | |
| 74 | for (i = 0, rate = 0; i < max_opps; i++, rate++) { |
| 75 | /* find next rate */ |
| 76 | opp = dev_pm_opp_find_freq_ceil(dev, &rate); |
| 77 | if (IS_ERR(opp)) { |
| 78 | ret = PTR_ERR(opp); |
| 79 | goto out; |
| 80 | } |
| 81 | freq_table[i].driver_data = i; |
| 82 | freq_table[i].frequency = rate / 1000; |
| 83 | |
| 84 | /* Is Boost/turbo opp ? */ |
| 85 | if (dev_pm_opp_is_turbo(opp)) |
| 86 | freq_table[i].flags = CPUFREQ_BOOST_FREQ; |
| 87 | } |
| 88 | |
| 89 | freq_table[i].driver_data = i; |
| 90 | freq_table[i].frequency = CPUFREQ_TABLE_END; |
| 91 | |
| 92 | *table = &freq_table[0]; |
| 93 | |
| 94 | out: |
| 95 | rcu_read_unlock(); |
| 96 | if (ret) |
| 97 | kfree(freq_table); |
| 98 | |
| 99 | return ret; |
| 100 | } |
| 101 | EXPORT_SYMBOL_GPL(dev_pm_opp_init_cpufreq_table); |
| 102 | |
| 103 | /** |
| 104 | * dev_pm_opp_free_cpufreq_table() - free the cpufreq table |
| 105 | * @dev: device for which we do this operation |
| 106 | * @table: table to free |
| 107 | * |
| 108 | * Free up the table allocated by dev_pm_opp_init_cpufreq_table |
| 109 | */ |
| 110 | void dev_pm_opp_free_cpufreq_table(struct device *dev, |
| 111 | struct cpufreq_frequency_table **table) |
| 112 | { |
| 113 | if (!table) |
| 114 | return; |
| 115 | |
| 116 | kfree(*table); |
| 117 | *table = NULL; |
| 118 | } |
| 119 | EXPORT_SYMBOL_GPL(dev_pm_opp_free_cpufreq_table); |
| 120 | #endif /* CONFIG_CPU_FREQ */ |
| 121 | |
| 122 | /* Required only for V1 bindings, as v2 can manage it from DT itself */ |
| 123 | int dev_pm_opp_set_sharing_cpus(struct device *cpu_dev, cpumask_var_t cpumask) |
| 124 | { |
| 125 | struct device_list_opp *list_dev; |
| 126 | struct device_opp *dev_opp; |
| 127 | struct device *dev; |
| 128 | int cpu, ret = 0; |
| 129 | |
| 130 | mutex_lock(&dev_opp_list_lock); |
| 131 | |
| 132 | dev_opp = _find_device_opp(cpu_dev); |
| 133 | if (IS_ERR(dev_opp)) { |
| 134 | ret = -EINVAL; |
| 135 | goto unlock; |
| 136 | } |
| 137 | |
| 138 | for_each_cpu(cpu, cpumask) { |
| 139 | if (cpu == cpu_dev->id) |
| 140 | continue; |
| 141 | |
| 142 | dev = get_cpu_device(cpu); |
| 143 | if (!dev) { |
| 144 | dev_err(cpu_dev, "%s: failed to get cpu%d device\n", |
| 145 | __func__, cpu); |
| 146 | continue; |
| 147 | } |
| 148 | |
| 149 | list_dev = _add_list_dev(dev, dev_opp); |
| 150 | if (!list_dev) { |
| 151 | dev_err(dev, "%s: failed to add list-dev for cpu%d device\n", |
| 152 | __func__, cpu); |
| 153 | continue; |
| 154 | } |
| 155 | } |
| 156 | unlock: |
| 157 | mutex_unlock(&dev_opp_list_lock); |
| 158 | |
| 159 | return ret; |
| 160 | } |
| 161 | EXPORT_SYMBOL_GPL(dev_pm_opp_set_sharing_cpus); |
| 162 | |
| 163 | #ifdef CONFIG_OF |
| 164 | void dev_pm_opp_of_cpumask_remove_table(cpumask_var_t cpumask) |
| 165 | { |
| 166 | struct device *cpu_dev; |
| 167 | int cpu; |
| 168 | |
| 169 | WARN_ON(cpumask_empty(cpumask)); |
| 170 | |
| 171 | for_each_cpu(cpu, cpumask) { |
| 172 | cpu_dev = get_cpu_device(cpu); |
| 173 | if (!cpu_dev) { |
| 174 | pr_err("%s: failed to get cpu%d device\n", __func__, |
| 175 | cpu); |
| 176 | continue; |
| 177 | } |
| 178 | |
| 179 | dev_pm_opp_of_remove_table(cpu_dev); |
| 180 | } |
| 181 | } |
| 182 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_remove_table); |
| 183 | |
| 184 | int dev_pm_opp_of_cpumask_add_table(cpumask_var_t cpumask) |
| 185 | { |
| 186 | struct device *cpu_dev; |
| 187 | int cpu, ret = 0; |
| 188 | |
| 189 | WARN_ON(cpumask_empty(cpumask)); |
| 190 | |
| 191 | for_each_cpu(cpu, cpumask) { |
| 192 | cpu_dev = get_cpu_device(cpu); |
| 193 | if (!cpu_dev) { |
| 194 | pr_err("%s: failed to get cpu%d device\n", __func__, |
| 195 | cpu); |
| 196 | continue; |
| 197 | } |
| 198 | |
| 199 | ret = dev_pm_opp_of_add_table(cpu_dev); |
| 200 | if (ret) { |
| 201 | pr_err("%s: couldn't find opp table for cpu:%d, %d\n", |
| 202 | __func__, cpu, ret); |
| 203 | |
| 204 | /* Free all other OPPs */ |
| 205 | dev_pm_opp_of_cpumask_remove_table(cpumask); |
| 206 | break; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | return ret; |
| 211 | } |
| 212 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_cpumask_add_table); |
| 213 | |
| 214 | /* |
| 215 | * Works only for OPP v2 bindings. |
| 216 | * |
| 217 | * cpumask should be already set to mask of cpu_dev->id. |
| 218 | * Returns -ENOENT if operating-points-v2 bindings aren't supported. |
| 219 | */ |
| 220 | int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev, cpumask_var_t cpumask) |
| 221 | { |
| 222 | struct device_node *np, *tmp_np; |
| 223 | struct device *tcpu_dev; |
| 224 | int cpu, ret = 0; |
| 225 | |
| 226 | /* Get OPP descriptor node */ |
| 227 | np = _of_get_opp_desc_node(cpu_dev); |
| 228 | if (!np) { |
| 229 | dev_dbg(cpu_dev, "%s: Couldn't find cpu_dev node.\n", __func__); |
| 230 | return -ENOENT; |
| 231 | } |
| 232 | |
| 233 | /* OPPs are shared ? */ |
| 234 | if (!of_property_read_bool(np, "opp-shared")) |
| 235 | goto put_cpu_node; |
| 236 | |
| 237 | for_each_possible_cpu(cpu) { |
| 238 | if (cpu == cpu_dev->id) |
| 239 | continue; |
| 240 | |
| 241 | tcpu_dev = get_cpu_device(cpu); |
| 242 | if (!tcpu_dev) { |
| 243 | dev_err(cpu_dev, "%s: failed to get cpu%d device\n", |
| 244 | __func__, cpu); |
| 245 | ret = -ENODEV; |
| 246 | goto put_cpu_node; |
| 247 | } |
| 248 | |
| 249 | /* Get OPP descriptor node */ |
| 250 | tmp_np = _of_get_opp_desc_node(tcpu_dev); |
| 251 | if (!tmp_np) { |
| 252 | dev_err(tcpu_dev, "%s: Couldn't find tcpu_dev node.\n", |
| 253 | __func__); |
| 254 | ret = -ENOENT; |
| 255 | goto put_cpu_node; |
| 256 | } |
| 257 | |
| 258 | /* CPUs are sharing opp node */ |
| 259 | if (np == tmp_np) |
| 260 | cpumask_set_cpu(cpu, cpumask); |
| 261 | |
| 262 | of_node_put(tmp_np); |
| 263 | } |
| 264 | |
| 265 | put_cpu_node: |
| 266 | of_node_put(np); |
| 267 | return ret; |
| 268 | } |
| 269 | EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_sharing_cpus); |
| 270 | #endif |