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