Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge OPP material for v4.11 to satisfy dependencies.

+599 -985
+16 -36
Documentation/power/opp.txt
··· 79 79 specific framework which uses the OPP library. Similar care needs to be taken 80 80 care to refresh the cpufreq table in cases of these operations. 81 81 82 - WARNING on OPP List locking mechanism: 83 - ------------------------------------------------- 84 - OPP library uses RCU for exclusivity. RCU allows the query functions to operate 85 - in multiple contexts and this synchronization mechanism is optimal for a read 86 - intensive operations on data structure as the OPP library caters to. 87 - 88 - To ensure that the data retrieved are sane, the users such as SoC framework 89 - should ensure that the section of code operating on OPP queries are locked 90 - using RCU read locks. The opp_find_freq_{exact,ceil,floor}, 91 - opp_get_{voltage, freq, opp_count} fall into this category. 92 - 93 - opp_{add,enable,disable} are updaters which use mutex and implement it's own 94 - RCU locking mechanisms. These functions should *NOT* be called under RCU locks 95 - and other contexts that prevent blocking functions in RCU or mutex operations 96 - from working. 97 - 98 82 2. Initial OPP List Registration 99 83 ================================ 100 84 The SoC implementation calls dev_pm_opp_add function iteratively to add OPPs per ··· 121 137 found, else returns error. These errors are expected to be handled by standard 122 138 error checks such as IS_ERR() and appropriate actions taken by the caller. 123 139 140 + Callers of these functions shall call dev_pm_opp_put() after they have used the 141 + OPP. Otherwise the memory for the OPP will never get freed and result in 142 + memleak. 143 + 124 144 dev_pm_opp_find_freq_exact - Search for an OPP based on an *exact* frequency and 125 145 availability. This function is especially useful to enable an OPP which 126 146 is not available by default. 127 147 Example: In a case when SoC framework detects a situation where a 128 148 higher frequency could be made available, it can use this function to 129 149 find the OPP prior to call the dev_pm_opp_enable to actually make it available. 130 - rcu_read_lock(); 131 150 opp = dev_pm_opp_find_freq_exact(dev, 1000000000, false); 132 - rcu_read_unlock(); 151 + dev_pm_opp_put(opp); 133 152 /* dont operate on the pointer.. just do a sanity check.. */ 134 153 if (IS_ERR(opp)) { 135 154 pr_err("frequency not disabled!\n"); ··· 150 163 frequency. 151 164 Example: To find the highest opp for a device: 152 165 freq = ULONG_MAX; 153 - rcu_read_lock(); 154 - dev_pm_opp_find_freq_floor(dev, &freq); 155 - rcu_read_unlock(); 166 + opp = dev_pm_opp_find_freq_floor(dev, &freq); 167 + dev_pm_opp_put(opp); 156 168 157 169 dev_pm_opp_find_freq_ceil - Search for an available OPP which is *at least* the 158 170 provided frequency. This function is useful while searching for a ··· 159 173 frequency. 160 174 Example 1: To find the lowest opp for a device: 161 175 freq = 0; 162 - rcu_read_lock(); 163 - dev_pm_opp_find_freq_ceil(dev, &freq); 164 - rcu_read_unlock(); 176 + opp = dev_pm_opp_find_freq_ceil(dev, &freq); 177 + dev_pm_opp_put(opp); 165 178 Example 2: A simplified implementation of a SoC cpufreq_driver->target: 166 179 soc_cpufreq_target(..) 167 180 { 168 181 /* Do stuff like policy checks etc. */ 169 182 /* Find the best frequency match for the req */ 170 - rcu_read_lock(); 171 183 opp = dev_pm_opp_find_freq_ceil(dev, &freq); 172 - rcu_read_unlock(); 184 + dev_pm_opp_put(opp); 173 185 if (!IS_ERR(opp)) 174 186 soc_switch_to_freq_voltage(freq); 175 187 else ··· 192 208 implementation might choose to do something as follows: 193 209 if (cur_temp < temp_low_thresh) { 194 210 /* Enable 1GHz if it was disabled */ 195 - rcu_read_lock(); 196 211 opp = dev_pm_opp_find_freq_exact(dev, 1000000000, false); 197 - rcu_read_unlock(); 212 + dev_pm_opp_put(opp); 198 213 /* just error check */ 199 214 if (!IS_ERR(opp)) 200 215 ret = dev_pm_opp_enable(dev, 1000000000); ··· 207 224 choose to do something as follows: 208 225 if (cur_temp > temp_high_thresh) { 209 226 /* Disable 1GHz if it was enabled */ 210 - rcu_read_lock(); 211 227 opp = dev_pm_opp_find_freq_exact(dev, 1000000000, true); 212 - rcu_read_unlock(); 228 + dev_pm_opp_put(opp); 213 229 /* just error check */ 214 230 if (!IS_ERR(opp)) 215 231 ret = dev_pm_opp_disable(dev, 1000000000); ··· 231 249 soc_switch_to_freq_voltage(freq) 232 250 { 233 251 /* do things */ 234 - rcu_read_lock(); 235 252 opp = dev_pm_opp_find_freq_ceil(dev, &freq); 236 253 v = dev_pm_opp_get_voltage(opp); 237 - rcu_read_unlock(); 254 + dev_pm_opp_put(opp); 238 255 if (v) 239 256 regulator_set_voltage(.., v); 240 257 /* do other things */ ··· 247 266 { 248 267 /* do things.. */ 249 268 max_freq = ULONG_MAX; 250 - rcu_read_lock(); 251 269 max_opp = dev_pm_opp_find_freq_floor(dev,&max_freq); 252 270 requested_opp = dev_pm_opp_find_freq_ceil(dev,&freq); 253 271 if (!IS_ERR(max_opp) && !IS_ERR(requested_opp)) 254 272 r = soc_test_validity(max_opp, requested_opp); 255 - rcu_read_unlock(); 273 + dev_pm_opp_put(max_opp); 274 + dev_pm_opp_put(requested_opp); 256 275 /* do other things */ 257 276 } 258 277 soc_test_validity(..) ··· 270 289 soc_notify_coproc_available_frequencies() 271 290 { 272 291 /* Do things */ 273 - rcu_read_lock(); 274 292 num_available = dev_pm_opp_get_opp_count(dev); 275 293 speeds = kzalloc(sizeof(u32) * num_available, GFP_KERNEL); 276 294 /* populate the table in increasing order */ ··· 278 298 speeds[i] = freq; 279 299 freq++; 280 300 i++; 301 + dev_pm_opp_put(opp); 281 302 } 282 - rcu_read_unlock(); 283 303 284 304 soc_notify_coproc(AVAILABLE_FREQs, speeds, num_available); 285 305 /* Do other things */
+2 -3
arch/arm/mach-omap2/pm.c
··· 130 130 freq = clk_get_rate(clk); 131 131 clk_put(clk); 132 132 133 - rcu_read_lock(); 134 133 opp = dev_pm_opp_find_freq_ceil(dev, &freq); 135 134 if (IS_ERR(opp)) { 136 - rcu_read_unlock(); 137 135 pr_err("%s: unable to find boot up OPP for vdd_%s\n", 138 136 __func__, vdd_name); 139 137 goto exit; 140 138 } 141 139 142 140 bootup_volt = dev_pm_opp_get_voltage(opp); 143 - rcu_read_unlock(); 141 + dev_pm_opp_put(opp); 142 + 144 143 if (!bootup_volt) { 145 144 pr_err("%s: unable to find voltage corresponding to the bootup OPP for vdd_%s\n", 146 145 __func__, vdd_name);
+389 -632
drivers/base/power/opp/core.c
··· 32 32 /* Lock to allow exclusive modification to the device and opp lists */ 33 33 DEFINE_MUTEX(opp_table_lock); 34 34 35 - #define opp_rcu_lockdep_assert() \ 36 - do { \ 37 - RCU_LOCKDEP_WARN(!rcu_read_lock_held() && \ 38 - !lockdep_is_held(&opp_table_lock), \ 39 - "Missing rcu_read_lock() or " \ 40 - "opp_table_lock protection"); \ 41 - } while (0) 35 + static void dev_pm_opp_get(struct dev_pm_opp *opp); 42 36 43 37 static struct opp_device *_find_opp_dev(const struct device *dev, 44 38 struct opp_table *opp_table) ··· 46 52 return NULL; 47 53 } 48 54 55 + static struct opp_table *_find_opp_table_unlocked(struct device *dev) 56 + { 57 + struct opp_table *opp_table; 58 + 59 + list_for_each_entry(opp_table, &opp_tables, node) { 60 + if (_find_opp_dev(dev, opp_table)) { 61 + _get_opp_table_kref(opp_table); 62 + 63 + return opp_table; 64 + } 65 + } 66 + 67 + return ERR_PTR(-ENODEV); 68 + } 69 + 49 70 /** 50 71 * _find_opp_table() - find opp_table struct using device pointer 51 72 * @dev: device pointer used to lookup OPP table 52 73 * 53 - * Search OPP table for one containing matching device. Does a RCU reader 54 - * operation to grab the pointer needed. 74 + * Search OPP table for one containing matching device. 55 75 * 56 76 * Return: pointer to 'struct opp_table' if found, otherwise -ENODEV or 57 77 * -EINVAL based on type of error. 58 78 * 59 - * Locking: For readers, this function must be called under rcu_read_lock(). 60 - * opp_table is a RCU protected pointer, which means that opp_table is valid 61 - * as long as we are under RCU lock. 62 - * 63 - * For Writers, this function must be called with opp_table_lock held. 79 + * The callers must call dev_pm_opp_put_opp_table() after the table is used. 64 80 */ 65 81 struct opp_table *_find_opp_table(struct device *dev) 66 82 { 67 83 struct opp_table *opp_table; 68 - 69 - opp_rcu_lockdep_assert(); 70 84 71 85 if (IS_ERR_OR_NULL(dev)) { 72 86 pr_err("%s: Invalid parameters\n", __func__); 73 87 return ERR_PTR(-EINVAL); 74 88 } 75 89 76 - list_for_each_entry_rcu(opp_table, &opp_tables, node) 77 - if (_find_opp_dev(dev, opp_table)) 78 - return opp_table; 90 + mutex_lock(&opp_table_lock); 91 + opp_table = _find_opp_table_unlocked(dev); 92 + mutex_unlock(&opp_table_lock); 79 93 80 - return ERR_PTR(-ENODEV); 94 + return opp_table; 81 95 } 82 96 83 97 /** ··· 96 94 * return 0 97 95 * 98 96 * This is useful only for devices with single power supply. 99 - * 100 - * Locking: This function must be called under rcu_read_lock(). opp is a rcu 101 - * protected pointer. This means that opp which could have been fetched by 102 - * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are 103 - * under RCU lock. The pointer returned by the opp_find_freq family must be 104 - * used in the same section as the usage of this function with the pointer 105 - * prior to unlocking with rcu_read_unlock() to maintain the integrity of the 106 - * pointer. 107 97 */ 108 98 unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp) 109 99 { 110 - struct dev_pm_opp *tmp_opp; 111 - unsigned long v = 0; 112 - 113 - opp_rcu_lockdep_assert(); 114 - 115 - tmp_opp = rcu_dereference(opp); 116 - if (IS_ERR_OR_NULL(tmp_opp)) 100 + if (IS_ERR_OR_NULL(opp)) { 117 101 pr_err("%s: Invalid parameters\n", __func__); 118 - else 119 - v = tmp_opp->supplies[0].u_volt; 102 + return 0; 103 + } 120 104 121 - return v; 105 + return opp->supplies[0].u_volt; 122 106 } 123 107 EXPORT_SYMBOL_GPL(dev_pm_opp_get_voltage); 124 108 ··· 114 126 * 115 127 * Return: frequency in hertz corresponding to the opp, else 116 128 * return 0 117 - * 118 - * Locking: This function must be called under rcu_read_lock(). opp is a rcu 119 - * protected pointer. This means that opp which could have been fetched by 120 - * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are 121 - * under RCU lock. The pointer returned by the opp_find_freq family must be 122 - * used in the same section as the usage of this function with the pointer 123 - * prior to unlocking with rcu_read_unlock() to maintain the integrity of the 124 - * pointer. 125 129 */ 126 130 unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp) 127 131 { 128 - struct dev_pm_opp *tmp_opp; 129 - unsigned long f = 0; 130 - 131 - opp_rcu_lockdep_assert(); 132 - 133 - tmp_opp = rcu_dereference(opp); 134 - if (IS_ERR_OR_NULL(tmp_opp) || !tmp_opp->available) 132 + if (IS_ERR_OR_NULL(opp) || !opp->available) { 135 133 pr_err("%s: Invalid parameters\n", __func__); 136 - else 137 - f = tmp_opp->rate; 134 + return 0; 135 + } 138 136 139 - return f; 137 + return opp->rate; 140 138 } 141 139 EXPORT_SYMBOL_GPL(dev_pm_opp_get_freq); 142 140 ··· 135 161 * quickly. Running on them for longer times may overheat the chip. 136 162 * 137 163 * Return: true if opp is turbo opp, else false. 138 - * 139 - * Locking: This function must be called under rcu_read_lock(). opp is a rcu 140 - * protected pointer. This means that opp which could have been fetched by 141 - * opp_find_freq_{exact,ceil,floor} functions is valid as long as we are 142 - * under RCU lock. The pointer returned by the opp_find_freq family must be 143 - * used in the same section as the usage of this function with the pointer 144 - * prior to unlocking with rcu_read_unlock() to maintain the integrity of the 145 - * pointer. 146 164 */ 147 165 bool dev_pm_opp_is_turbo(struct dev_pm_opp *opp) 148 166 { 149 - struct dev_pm_opp *tmp_opp; 150 - 151 - opp_rcu_lockdep_assert(); 152 - 153 - tmp_opp = rcu_dereference(opp); 154 - if (IS_ERR_OR_NULL(tmp_opp) || !tmp_opp->available) { 167 + if (IS_ERR_OR_NULL(opp) || !opp->available) { 155 168 pr_err("%s: Invalid parameters\n", __func__); 156 169 return false; 157 170 } 158 171 159 - return tmp_opp->turbo; 172 + return opp->turbo; 160 173 } 161 174 EXPORT_SYMBOL_GPL(dev_pm_opp_is_turbo); 162 175 ··· 152 191 * @dev: device for which we do this operation 153 192 * 154 193 * Return: This function returns the max clock latency in nanoseconds. 155 - * 156 - * Locking: This function takes rcu_read_lock(). 157 194 */ 158 195 unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev) 159 196 { 160 197 struct opp_table *opp_table; 161 198 unsigned long clock_latency_ns; 162 199 163 - rcu_read_lock(); 164 - 165 200 opp_table = _find_opp_table(dev); 166 201 if (IS_ERR(opp_table)) 167 - clock_latency_ns = 0; 168 - else 169 - clock_latency_ns = opp_table->clock_latency_ns_max; 202 + return 0; 170 203 171 - rcu_read_unlock(); 204 + clock_latency_ns = opp_table->clock_latency_ns_max; 205 + 206 + dev_pm_opp_put_opp_table(opp_table); 207 + 172 208 return clock_latency_ns; 173 209 } 174 210 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_clock_latency); 175 - 176 - static int _get_regulator_count(struct device *dev) 177 - { 178 - struct opp_table *opp_table; 179 - int count; 180 - 181 - rcu_read_lock(); 182 - 183 - opp_table = _find_opp_table(dev); 184 - if (!IS_ERR(opp_table)) 185 - count = opp_table->regulator_count; 186 - else 187 - count = 0; 188 - 189 - rcu_read_unlock(); 190 - 191 - return count; 192 - } 193 211 194 212 /** 195 213 * dev_pm_opp_get_max_volt_latency() - Get max voltage latency in nanoseconds 196 214 * @dev: device for which we do this operation 197 215 * 198 216 * Return: This function returns the max voltage latency in nanoseconds. 199 - * 200 - * Locking: This function takes rcu_read_lock(). 201 217 */ 202 218 unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev) 203 219 { ··· 188 250 unsigned long max; 189 251 } *uV; 190 252 191 - count = _get_regulator_count(dev); 253 + opp_table = _find_opp_table(dev); 254 + if (IS_ERR(opp_table)) 255 + return 0; 256 + 257 + count = opp_table->regulator_count; 192 258 193 259 /* Regulator may not be required for the device */ 194 260 if (!count) 195 - return 0; 261 + goto put_opp_table; 196 262 197 263 regulators = kmalloc_array(count, sizeof(*regulators), GFP_KERNEL); 198 264 if (!regulators) 199 - return 0; 265 + goto put_opp_table; 200 266 201 267 uV = kmalloc_array(count, sizeof(*uV), GFP_KERNEL); 202 268 if (!uV) 203 269 goto free_regulators; 204 270 205 - rcu_read_lock(); 206 - 207 - opp_table = _find_opp_table(dev); 208 - if (IS_ERR(opp_table)) { 209 - rcu_read_unlock(); 210 - goto free_uV; 211 - } 212 - 213 271 memcpy(regulators, opp_table->regulators, count * sizeof(*regulators)); 272 + 273 + mutex_lock(&opp_table->lock); 214 274 215 275 for (i = 0; i < count; i++) { 216 276 uV[i].min = ~0; 217 277 uV[i].max = 0; 218 278 219 - list_for_each_entry_rcu(opp, &opp_table->opp_list, node) { 279 + list_for_each_entry(opp, &opp_table->opp_list, node) { 220 280 if (!opp->available) 221 281 continue; 222 282 ··· 225 289 } 226 290 } 227 291 228 - rcu_read_unlock(); 292 + mutex_unlock(&opp_table->lock); 229 293 230 294 /* 231 295 * The caller needs to ensure that opp_table (and hence the regulator) ··· 237 301 latency_ns += ret * 1000; 238 302 } 239 303 240 - free_uV: 241 304 kfree(uV); 242 305 free_regulators: 243 306 kfree(regulators); 307 + put_opp_table: 308 + dev_pm_opp_put_opp_table(opp_table); 244 309 245 310 return latency_ns; 246 311 } ··· 254 317 * 255 318 * Return: This function returns the max transition latency, in nanoseconds, to 256 319 * switch from one OPP to other. 257 - * 258 - * Locking: This function takes rcu_read_lock(). 259 320 */ 260 321 unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev) 261 322 { ··· 263 328 EXPORT_SYMBOL_GPL(dev_pm_opp_get_max_transition_latency); 264 329 265 330 /** 266 - * dev_pm_opp_get_suspend_opp() - Get suspend opp 331 + * dev_pm_opp_get_suspend_opp_freq() - Get frequency of suspend opp in Hz 267 332 * @dev: device for which we do this operation 268 333 * 269 - * Return: This function returns pointer to the suspend opp if it is 270 - * defined and available, otherwise it returns NULL. 271 - * 272 - * Locking: This function must be called under rcu_read_lock(). opp is a rcu 273 - * protected pointer. The reason for the same is that the opp pointer which is 274 - * returned will remain valid for use with opp_get_{voltage, freq} only while 275 - * under the locked area. The pointer returned must be used prior to unlocking 276 - * with rcu_read_unlock() to maintain the integrity of the pointer. 334 + * Return: This function returns the frequency of the OPP marked as suspend_opp 335 + * if one is available, else returns 0; 277 336 */ 278 - struct dev_pm_opp *dev_pm_opp_get_suspend_opp(struct device *dev) 337 + unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev) 279 338 { 280 339 struct opp_table *opp_table; 281 - 282 - opp_rcu_lockdep_assert(); 340 + unsigned long freq = 0; 283 341 284 342 opp_table = _find_opp_table(dev); 285 - if (IS_ERR(opp_table) || !opp_table->suspend_opp || 286 - !opp_table->suspend_opp->available) 287 - return NULL; 343 + if (IS_ERR(opp_table)) 344 + return 0; 288 345 289 - return opp_table->suspend_opp; 346 + if (opp_table->suspend_opp && opp_table->suspend_opp->available) 347 + freq = dev_pm_opp_get_freq(opp_table->suspend_opp); 348 + 349 + dev_pm_opp_put_opp_table(opp_table); 350 + 351 + return freq; 290 352 } 291 - EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp); 353 + EXPORT_SYMBOL_GPL(dev_pm_opp_get_suspend_opp_freq); 292 354 293 355 /** 294 356 * dev_pm_opp_get_opp_count() - Get number of opps available in the opp table ··· 293 361 * 294 362 * Return: This function returns the number of available opps if there are any, 295 363 * else returns 0 if none or the corresponding error value. 296 - * 297 - * Locking: This function takes rcu_read_lock(). 298 364 */ 299 365 int dev_pm_opp_get_opp_count(struct device *dev) 300 366 { ··· 300 370 struct dev_pm_opp *temp_opp; 301 371 int count = 0; 302 372 303 - rcu_read_lock(); 304 - 305 373 opp_table = _find_opp_table(dev); 306 374 if (IS_ERR(opp_table)) { 307 375 count = PTR_ERR(opp_table); 308 376 dev_err(dev, "%s: OPP table not found (%d)\n", 309 377 __func__, count); 310 - goto out_unlock; 378 + return count; 311 379 } 312 380 313 - list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) { 381 + mutex_lock(&opp_table->lock); 382 + 383 + list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 314 384 if (temp_opp->available) 315 385 count++; 316 386 } 317 387 318 - out_unlock: 319 - rcu_read_unlock(); 388 + mutex_unlock(&opp_table->lock); 389 + dev_pm_opp_put_opp_table(opp_table); 390 + 320 391 return count; 321 392 } 322 393 EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_count); ··· 342 411 * This provides a mechanism to enable an opp which is not available currently 343 412 * or the opposite as well. 344 413 * 345 - * Locking: This function must be called under rcu_read_lock(). opp is a rcu 346 - * protected pointer. The reason for the same is that the opp pointer which is 347 - * returned will remain valid for use with opp_get_{voltage, freq} only while 348 - * under the locked area. The pointer returned must be used prior to unlocking 349 - * with rcu_read_unlock() to maintain the integrity of the pointer. 414 + * The callers are required to call dev_pm_opp_put() for the returned OPP after 415 + * use. 350 416 */ 351 417 struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev, 352 418 unsigned long freq, ··· 351 423 { 352 424 struct opp_table *opp_table; 353 425 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 354 - 355 - opp_rcu_lockdep_assert(); 356 426 357 427 opp_table = _find_opp_table(dev); 358 428 if (IS_ERR(opp_table)) { ··· 360 434 return ERR_PTR(r); 361 435 } 362 436 363 - list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) { 437 + mutex_lock(&opp_table->lock); 438 + 439 + list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 364 440 if (temp_opp->available == available && 365 441 temp_opp->rate == freq) { 366 442 opp = temp_opp; 443 + 444 + /* Increment the reference count of OPP */ 445 + dev_pm_opp_get(opp); 367 446 break; 368 447 } 369 448 } 449 + 450 + mutex_unlock(&opp_table->lock); 451 + dev_pm_opp_put_opp_table(opp_table); 370 452 371 453 return opp; 372 454 } ··· 385 451 { 386 452 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 387 453 388 - list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) { 454 + mutex_lock(&opp_table->lock); 455 + 456 + list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 389 457 if (temp_opp->available && temp_opp->rate >= *freq) { 390 458 opp = temp_opp; 391 459 *freq = opp->rate; 460 + 461 + /* Increment the reference count of OPP */ 462 + dev_pm_opp_get(opp); 392 463 break; 393 464 } 394 465 } 466 + 467 + mutex_unlock(&opp_table->lock); 395 468 396 469 return opp; 397 470 } ··· 418 477 * ERANGE: no match found for search 419 478 * ENODEV: if device not found in list of registered devices 420 479 * 421 - * Locking: This function must be called under rcu_read_lock(). opp is a rcu 422 - * protected pointer. The reason for the same is that the opp pointer which is 423 - * returned will remain valid for use with opp_get_{voltage, freq} only while 424 - * under the locked area. The pointer returned must be used prior to unlocking 425 - * with rcu_read_unlock() to maintain the integrity of the pointer. 480 + * The callers are required to call dev_pm_opp_put() for the returned OPP after 481 + * use. 426 482 */ 427 483 struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev, 428 484 unsigned long *freq) 429 485 { 430 486 struct opp_table *opp_table; 431 - 432 - opp_rcu_lockdep_assert(); 487 + struct dev_pm_opp *opp; 433 488 434 489 if (!dev || !freq) { 435 490 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq); ··· 436 499 if (IS_ERR(opp_table)) 437 500 return ERR_CAST(opp_table); 438 501 439 - return _find_freq_ceil(opp_table, freq); 502 + opp = _find_freq_ceil(opp_table, freq); 503 + 504 + dev_pm_opp_put_opp_table(opp_table); 505 + 506 + return opp; 440 507 } 441 508 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_ceil); 442 509 ··· 459 518 * ERANGE: no match found for search 460 519 * ENODEV: if device not found in list of registered devices 461 520 * 462 - * Locking: This function must be called under rcu_read_lock(). opp is a rcu 463 - * protected pointer. The reason for the same is that the opp pointer which is 464 - * returned will remain valid for use with opp_get_{voltage, freq} only while 465 - * under the locked area. The pointer returned must be used prior to unlocking 466 - * with rcu_read_unlock() to maintain the integrity of the pointer. 521 + * The callers are required to call dev_pm_opp_put() for the returned OPP after 522 + * use. 467 523 */ 468 524 struct dev_pm_opp *dev_pm_opp_find_freq_floor(struct device *dev, 469 525 unsigned long *freq) 470 526 { 471 527 struct opp_table *opp_table; 472 528 struct dev_pm_opp *temp_opp, *opp = ERR_PTR(-ERANGE); 473 - 474 - opp_rcu_lockdep_assert(); 475 529 476 530 if (!dev || !freq) { 477 531 dev_err(dev, "%s: Invalid argument freq=%p\n", __func__, freq); ··· 477 541 if (IS_ERR(opp_table)) 478 542 return ERR_CAST(opp_table); 479 543 480 - list_for_each_entry_rcu(temp_opp, &opp_table->opp_list, node) { 544 + mutex_lock(&opp_table->lock); 545 + 546 + list_for_each_entry(temp_opp, &opp_table->opp_list, node) { 481 547 if (temp_opp->available) { 482 548 /* go to the next node, before choosing prev */ 483 549 if (temp_opp->rate > *freq) ··· 488 550 opp = temp_opp; 489 551 } 490 552 } 553 + 554 + /* Increment the reference count of OPP */ 555 + if (!IS_ERR(opp)) 556 + dev_pm_opp_get(opp); 557 + mutex_unlock(&opp_table->lock); 558 + dev_pm_opp_put_opp_table(opp_table); 559 + 491 560 if (!IS_ERR(opp)) 492 561 *freq = opp->rate; 493 562 494 563 return opp; 495 564 } 496 565 EXPORT_SYMBOL_GPL(dev_pm_opp_find_freq_floor); 497 - 498 - /* 499 - * The caller needs to ensure that opp_table (and hence the clk) isn't freed, 500 - * while clk returned here is used. 501 - */ 502 - static struct clk *_get_opp_clk(struct device *dev) 503 - { 504 - struct opp_table *opp_table; 505 - struct clk *clk; 506 - 507 - rcu_read_lock(); 508 - 509 - opp_table = _find_opp_table(dev); 510 - if (IS_ERR(opp_table)) { 511 - dev_err(dev, "%s: device opp doesn't exist\n", __func__); 512 - clk = ERR_CAST(opp_table); 513 - goto unlock; 514 - } 515 - 516 - clk = opp_table->clk; 517 - if (IS_ERR(clk)) 518 - dev_err(dev, "%s: No clock available for the device\n", 519 - __func__); 520 - 521 - unlock: 522 - rcu_read_unlock(); 523 - return clk; 524 - } 525 566 526 567 static int _set_opp_voltage(struct device *dev, struct regulator *reg, 527 568 struct dev_pm_opp_supply *supply) ··· 597 680 * 598 681 * This configures the power-supplies and clock source to the levels specified 599 682 * by the OPP corresponding to the target_freq. 600 - * 601 - * Locking: This function takes rcu_read_lock(). 602 683 */ 603 684 int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq) 604 685 { ··· 615 700 return -EINVAL; 616 701 } 617 702 618 - clk = _get_opp_clk(dev); 619 - if (IS_ERR(clk)) 620 - return PTR_ERR(clk); 703 + opp_table = _find_opp_table(dev); 704 + if (IS_ERR(opp_table)) { 705 + dev_err(dev, "%s: device opp doesn't exist\n", __func__); 706 + return PTR_ERR(opp_table); 707 + } 708 + 709 + clk = opp_table->clk; 710 + if (IS_ERR(clk)) { 711 + dev_err(dev, "%s: No clock available for the device\n", 712 + __func__); 713 + ret = PTR_ERR(clk); 714 + goto put_opp_table; 715 + } 621 716 622 717 freq = clk_round_rate(clk, target_freq); 623 718 if ((long)freq <= 0) ··· 639 714 if (old_freq == freq) { 640 715 dev_dbg(dev, "%s: old/new frequencies (%lu Hz) are same, nothing to do\n", 641 716 __func__, freq); 642 - return 0; 643 - } 644 - 645 - rcu_read_lock(); 646 - 647 - opp_table = _find_opp_table(dev); 648 - if (IS_ERR(opp_table)) { 649 - dev_err(dev, "%s: device opp doesn't exist\n", __func__); 650 - rcu_read_unlock(); 651 - return PTR_ERR(opp_table); 717 + ret = 0; 718 + goto put_opp_table; 652 719 } 653 720 654 721 old_opp = _find_freq_ceil(opp_table, &old_freq); ··· 654 737 ret = PTR_ERR(opp); 655 738 dev_err(dev, "%s: failed to find OPP for freq %lu (%d)\n", 656 739 __func__, freq, ret); 657 - rcu_read_unlock(); 658 - return ret; 740 + goto put_old_opp; 659 741 } 660 742 661 743 dev_dbg(dev, "%s: switching OPP: %lu Hz --> %lu Hz\n", __func__, ··· 664 748 665 749 /* Only frequency scaling */ 666 750 if (!regulators) { 667 - rcu_read_unlock(); 668 - return _generic_set_opp_clk_only(dev, clk, old_freq, freq); 751 + ret = _generic_set_opp_clk_only(dev, clk, old_freq, freq); 752 + goto put_opps; 669 753 } 670 754 671 755 if (opp_table->set_opp) ··· 689 773 data->new_opp.rate = freq; 690 774 memcpy(data->new_opp.supplies, opp->supplies, size); 691 775 692 - rcu_read_unlock(); 776 + ret = set_opp(data); 693 777 694 - return set_opp(data); 778 + put_opps: 779 + dev_pm_opp_put(opp); 780 + put_old_opp: 781 + if (!IS_ERR(old_opp)) 782 + dev_pm_opp_put(old_opp); 783 + put_opp_table: 784 + dev_pm_opp_put_opp_table(opp_table); 785 + return ret; 695 786 } 696 787 EXPORT_SYMBOL_GPL(dev_pm_opp_set_rate); 697 788 698 789 /* OPP-dev Helpers */ 699 - static void _kfree_opp_dev_rcu(struct rcu_head *head) 700 - { 701 - struct opp_device *opp_dev; 702 - 703 - opp_dev = container_of(head, struct opp_device, rcu_head); 704 - kfree_rcu(opp_dev, rcu_head); 705 - } 706 - 707 790 static void _remove_opp_dev(struct opp_device *opp_dev, 708 791 struct opp_table *opp_table) 709 792 { 710 793 opp_debug_unregister(opp_dev, opp_table); 711 794 list_del(&opp_dev->node); 712 - call_srcu(&opp_table->srcu_head.srcu, &opp_dev->rcu_head, 713 - _kfree_opp_dev_rcu); 795 + kfree(opp_dev); 714 796 } 715 797 716 798 struct opp_device *_add_opp_dev(const struct device *dev, ··· 723 809 724 810 /* Initialize opp-dev */ 725 811 opp_dev->dev = dev; 726 - list_add_rcu(&opp_dev->node, &opp_table->dev_list); 812 + list_add(&opp_dev->node, &opp_table->dev_list); 727 813 728 814 /* Create debugfs entries for the opp_table */ 729 815 ret = opp_debug_register(opp_dev, opp_table); ··· 734 820 return opp_dev; 735 821 } 736 822 737 - /** 738 - * _add_opp_table() - Find OPP table or allocate a new one 739 - * @dev: device for which we do this operation 740 - * 741 - * It tries to find an existing table first, if it couldn't find one, it 742 - * allocates a new OPP table and returns that. 743 - * 744 - * Return: valid opp_table pointer if success, else NULL. 745 - */ 746 - static struct opp_table *_add_opp_table(struct device *dev) 823 + static struct opp_table *_allocate_opp_table(struct device *dev) 747 824 { 748 825 struct opp_table *opp_table; 749 826 struct opp_device *opp_dev; 750 827 int ret; 751 - 752 - /* Check for existing table for 'dev' first */ 753 - opp_table = _find_opp_table(dev); 754 - if (!IS_ERR(opp_table)) 755 - return opp_table; 756 828 757 829 /* 758 830 * Allocate a new OPP table. In the infrequent case where a new ··· 767 867 ret); 768 868 } 769 869 770 - srcu_init_notifier_head(&opp_table->srcu_head); 870 + BLOCKING_INIT_NOTIFIER_HEAD(&opp_table->head); 771 871 INIT_LIST_HEAD(&opp_table->opp_list); 872 + mutex_init(&opp_table->lock); 873 + kref_init(&opp_table->kref); 772 874 773 875 /* Secure the device table modification */ 774 - list_add_rcu(&opp_table->node, &opp_tables); 876 + list_add(&opp_table->node, &opp_tables); 775 877 return opp_table; 776 878 } 777 879 778 - /** 779 - * _kfree_device_rcu() - Free opp_table RCU handler 780 - * @head: RCU head 781 - */ 782 - static void _kfree_device_rcu(struct rcu_head *head) 880 + void _get_opp_table_kref(struct opp_table *opp_table) 783 881 { 784 - struct opp_table *opp_table = container_of(head, struct opp_table, 785 - rcu_head); 786 - 787 - kfree_rcu(opp_table, rcu_head); 882 + kref_get(&opp_table->kref); 788 883 } 789 884 790 - /** 791 - * _remove_opp_table() - Removes a OPP table 792 - * @opp_table: OPP table to be removed. 793 - * 794 - * Removes/frees OPP table if it doesn't contain any OPPs. 795 - */ 796 - static void _remove_opp_table(struct opp_table *opp_table) 885 + struct opp_table *dev_pm_opp_get_opp_table(struct device *dev) 797 886 { 887 + struct opp_table *opp_table; 888 + 889 + /* Hold our table modification lock here */ 890 + mutex_lock(&opp_table_lock); 891 + 892 + opp_table = _find_opp_table_unlocked(dev); 893 + if (!IS_ERR(opp_table)) 894 + goto unlock; 895 + 896 + opp_table = _allocate_opp_table(dev); 897 + 898 + unlock: 899 + mutex_unlock(&opp_table_lock); 900 + 901 + return opp_table; 902 + } 903 + EXPORT_SYMBOL_GPL(dev_pm_opp_get_opp_table); 904 + 905 + static void _opp_table_kref_release(struct kref *kref) 906 + { 907 + struct opp_table *opp_table = container_of(kref, struct opp_table, kref); 798 908 struct opp_device *opp_dev; 799 - 800 - if (!list_empty(&opp_table->opp_list)) 801 - return; 802 - 803 - if (opp_table->supported_hw) 804 - return; 805 - 806 - if (opp_table->prop_name) 807 - return; 808 - 809 - if (opp_table->regulators) 810 - return; 811 - 812 - if (opp_table->set_opp) 813 - return; 814 909 815 910 /* Release clk */ 816 911 if (!IS_ERR(opp_table->clk)) ··· 819 924 /* dev_list must be empty now */ 820 925 WARN_ON(!list_empty(&opp_table->dev_list)); 821 926 822 - list_del_rcu(&opp_table->node); 823 - call_srcu(&opp_table->srcu_head.srcu, &opp_table->rcu_head, 824 - _kfree_device_rcu); 927 + mutex_destroy(&opp_table->lock); 928 + list_del(&opp_table->node); 929 + kfree(opp_table); 930 + 931 + mutex_unlock(&opp_table_lock); 825 932 } 826 933 827 - /** 828 - * _kfree_opp_rcu() - Free OPP RCU handler 829 - * @head: RCU head 830 - */ 831 - static void _kfree_opp_rcu(struct rcu_head *head) 934 + void dev_pm_opp_put_opp_table(struct opp_table *opp_table) 832 935 { 833 - struct dev_pm_opp *opp = container_of(head, struct dev_pm_opp, rcu_head); 936 + kref_put_mutex(&opp_table->kref, _opp_table_kref_release, 937 + &opp_table_lock); 938 + } 939 + EXPORT_SYMBOL_GPL(dev_pm_opp_put_opp_table); 834 940 835 - kfree_rcu(opp, rcu_head); 941 + void _opp_free(struct dev_pm_opp *opp) 942 + { 943 + kfree(opp); 836 944 } 837 945 838 - /** 839 - * _opp_remove() - Remove an OPP from a table definition 840 - * @opp_table: points back to the opp_table struct this opp belongs to 841 - * @opp: pointer to the OPP to remove 842 - * @notify: OPP_EVENT_REMOVE notification should be sent or not 843 - * 844 - * This function removes an opp definition from the opp table. 845 - * 846 - * Locking: The internal opp_table and opp structures are RCU protected. 847 - * It is assumed that the caller holds required mutex for an RCU updater 848 - * strategy. 849 - */ 850 - void _opp_remove(struct opp_table *opp_table, struct dev_pm_opp *opp, 851 - bool notify) 946 + static void _opp_kref_release(struct kref *kref) 852 947 { 948 + struct dev_pm_opp *opp = container_of(kref, struct dev_pm_opp, kref); 949 + struct opp_table *opp_table = opp->opp_table; 950 + 853 951 /* 854 952 * Notify the changes in the availability of the operable 855 953 * frequency/voltage list. 856 954 */ 857 - if (notify) 858 - srcu_notifier_call_chain(&opp_table->srcu_head, 859 - OPP_EVENT_REMOVE, opp); 955 + blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_REMOVE, opp); 860 956 opp_debug_remove_one(opp); 861 - list_del_rcu(&opp->node); 862 - call_srcu(&opp_table->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu); 957 + list_del(&opp->node); 958 + kfree(opp); 863 959 864 - _remove_opp_table(opp_table); 960 + mutex_unlock(&opp_table->lock); 961 + dev_pm_opp_put_opp_table(opp_table); 865 962 } 963 + 964 + static void dev_pm_opp_get(struct dev_pm_opp *opp) 965 + { 966 + kref_get(&opp->kref); 967 + } 968 + 969 + void dev_pm_opp_put(struct dev_pm_opp *opp) 970 + { 971 + kref_put_mutex(&opp->kref, _opp_kref_release, &opp->opp_table->lock); 972 + } 973 + EXPORT_SYMBOL_GPL(dev_pm_opp_put); 866 974 867 975 /** 868 976 * dev_pm_opp_remove() - Remove an OPP from OPP table ··· 873 975 * @freq: OPP to remove with matching 'freq' 874 976 * 875 977 * This function removes an opp from the opp table. 876 - * 877 - * Locking: The internal opp_table and opp structures are RCU protected. 878 - * Hence this function internally uses RCU updater strategy with mutex locks 879 - * to keep the integrity of the internal data structures. Callers should ensure 880 - * that this function is *NOT* called under RCU protection or in contexts where 881 - * mutex cannot be locked. 882 978 */ 883 979 void dev_pm_opp_remove(struct device *dev, unsigned long freq) 884 980 { ··· 880 988 struct opp_table *opp_table; 881 989 bool found = false; 882 990 883 - /* Hold our table modification lock here */ 884 - mutex_lock(&opp_table_lock); 885 - 886 991 opp_table = _find_opp_table(dev); 887 992 if (IS_ERR(opp_table)) 888 - goto unlock; 993 + return; 994 + 995 + mutex_lock(&opp_table->lock); 889 996 890 997 list_for_each_entry(opp, &opp_table->opp_list, node) { 891 998 if (opp->rate == freq) { ··· 893 1002 } 894 1003 } 895 1004 896 - if (!found) { 1005 + mutex_unlock(&opp_table->lock); 1006 + 1007 + if (found) { 1008 + dev_pm_opp_put(opp); 1009 + } else { 897 1010 dev_warn(dev, "%s: Couldn't find OPP with freq: %lu\n", 898 1011 __func__, freq); 899 - goto unlock; 900 1012 } 901 1013 902 - _opp_remove(opp_table, opp, true); 903 - unlock: 904 - mutex_unlock(&opp_table_lock); 1014 + dev_pm_opp_put_opp_table(opp_table); 905 1015 } 906 1016 EXPORT_SYMBOL_GPL(dev_pm_opp_remove); 907 1017 908 - struct dev_pm_opp *_allocate_opp(struct device *dev, 909 - struct opp_table **opp_table) 1018 + struct dev_pm_opp *_opp_allocate(struct opp_table *table) 910 1019 { 911 1020 struct dev_pm_opp *opp; 912 1021 int count, supply_size; 913 - struct opp_table *table; 914 - 915 - table = _add_opp_table(dev); 916 - if (!table) 917 - return NULL; 918 1022 919 1023 /* Allocate space for at least one supply */ 920 1024 count = table->regulator_count ? table->regulator_count : 1; ··· 917 1031 918 1032 /* allocate new OPP node and supplies structures */ 919 1033 opp = kzalloc(sizeof(*opp) + supply_size, GFP_KERNEL); 920 - if (!opp) { 921 - kfree(table); 1034 + if (!opp) 922 1035 return NULL; 923 - } 924 1036 925 1037 /* Put the supplies at the end of the OPP structure as an empty array */ 926 1038 opp->supplies = (struct dev_pm_opp_supply *)(opp + 1); 927 1039 INIT_LIST_HEAD(&opp->node); 928 - 929 - *opp_table = table; 930 1040 931 1041 return opp; 932 1042 } ··· 949 1067 return true; 950 1068 } 951 1069 1070 + /* 1071 + * Returns: 1072 + * 0: On success. And appropriate error message for duplicate OPPs. 1073 + * -EBUSY: For OPP with same freq/volt and is available. The callers of 1074 + * _opp_add() must return 0 if they receive -EBUSY from it. This is to make 1075 + * sure we don't print error messages unnecessarily if different parts of 1076 + * kernel try to initialize the OPP table. 1077 + * -EEXIST: For OPP with same freq but different volt or is unavailable. This 1078 + * should be considered an error by the callers of _opp_add(). 1079 + */ 952 1080 int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, 953 1081 struct opp_table *opp_table) 954 1082 { 955 1083 struct dev_pm_opp *opp; 956 - struct list_head *head = &opp_table->opp_list; 1084 + struct list_head *head; 957 1085 int ret; 958 1086 959 1087 /* ··· 974 1082 * loop, don't replace it with head otherwise it will become an infinite 975 1083 * loop. 976 1084 */ 977 - list_for_each_entry_rcu(opp, &opp_table->opp_list, node) { 1085 + mutex_lock(&opp_table->lock); 1086 + head = &opp_table->opp_list; 1087 + 1088 + list_for_each_entry(opp, &opp_table->opp_list, node) { 978 1089 if (new_opp->rate > opp->rate) { 979 1090 head = &opp->node; 980 1091 continue; ··· 993 1098 new_opp->supplies[0].u_volt, new_opp->available); 994 1099 995 1100 /* Should we compare voltages for all regulators here ? */ 996 - return opp->available && 997 - new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? 0 : -EEXIST; 1101 + ret = opp->available && 1102 + new_opp->supplies[0].u_volt == opp->supplies[0].u_volt ? -EBUSY : -EEXIST; 1103 + 1104 + mutex_unlock(&opp_table->lock); 1105 + return ret; 998 1106 } 999 1107 1108 + list_add(&new_opp->node, head); 1109 + mutex_unlock(&opp_table->lock); 1110 + 1000 1111 new_opp->opp_table = opp_table; 1001 - list_add_rcu(&new_opp->node, head); 1112 + kref_init(&new_opp->kref); 1113 + 1114 + /* Get a reference to the OPP table */ 1115 + _get_opp_table_kref(opp_table); 1002 1116 1003 1117 ret = opp_debug_create_one(new_opp, opp_table); 1004 1118 if (ret) ··· 1025 1121 1026 1122 /** 1027 1123 * _opp_add_v1() - Allocate a OPP based on v1 bindings. 1124 + * @opp_table: OPP table 1028 1125 * @dev: device for which we do this operation 1029 1126 * @freq: Frequency in Hz for this OPP 1030 1127 * @u_volt: Voltage in uVolts for this OPP ··· 1038 1133 * NOTE: "dynamic" parameter impacts OPPs added by the dev_pm_opp_of_add_table 1039 1134 * and freed by dev_pm_opp_of_remove_table. 1040 1135 * 1041 - * Locking: The internal opp_table and opp structures are RCU protected. 1042 - * Hence this function internally uses RCU updater strategy with mutex locks 1043 - * to keep the integrity of the internal data structures. Callers should ensure 1044 - * that this function is *NOT* called under RCU protection or in contexts where 1045 - * mutex cannot be locked. 1046 - * 1047 1136 * Return: 1048 1137 * 0 On success OR 1049 1138 * Duplicate OPPs (both freq and volt are same) and opp->available ··· 1045 1146 * Duplicate OPPs (both freq and volt are same) and !opp->available 1046 1147 * -ENOMEM Memory allocation failure 1047 1148 */ 1048 - int _opp_add_v1(struct device *dev, unsigned long freq, long u_volt, 1049 - bool dynamic) 1149 + int _opp_add_v1(struct opp_table *opp_table, struct device *dev, 1150 + unsigned long freq, long u_volt, bool dynamic) 1050 1151 { 1051 - struct opp_table *opp_table; 1052 1152 struct dev_pm_opp *new_opp; 1053 1153 unsigned long tol; 1054 1154 int ret; 1055 1155 1056 - /* Hold our table modification lock here */ 1057 - mutex_lock(&opp_table_lock); 1058 - 1059 - new_opp = _allocate_opp(dev, &opp_table); 1060 - if (!new_opp) { 1061 - ret = -ENOMEM; 1062 - goto unlock; 1063 - } 1156 + new_opp = _opp_allocate(opp_table); 1157 + if (!new_opp) 1158 + return -ENOMEM; 1064 1159 1065 1160 /* populate the opp table */ 1066 1161 new_opp->rate = freq; ··· 1066 1173 new_opp->dynamic = dynamic; 1067 1174 1068 1175 ret = _opp_add(dev, new_opp, opp_table); 1069 - if (ret) 1176 + if (ret) { 1177 + /* Don't return error for duplicate OPPs */ 1178 + if (ret == -EBUSY) 1179 + ret = 0; 1070 1180 goto free_opp; 1071 - 1072 - mutex_unlock(&opp_table_lock); 1181 + } 1073 1182 1074 1183 /* 1075 1184 * Notify the changes in the availability of the operable 1076 1185 * frequency/voltage list. 1077 1186 */ 1078 - srcu_notifier_call_chain(&opp_table->srcu_head, OPP_EVENT_ADD, new_opp); 1187 + blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp); 1079 1188 return 0; 1080 1189 1081 1190 free_opp: 1082 - _opp_remove(opp_table, new_opp, false); 1083 - unlock: 1084 - mutex_unlock(&opp_table_lock); 1191 + _opp_free(new_opp); 1192 + 1085 1193 return ret; 1086 1194 } 1087 1195 ··· 1096 1202 * specify the hierarchy of versions it supports. OPP layer will then enable 1097 1203 * OPPs, which are available for those versions, based on its 'opp-supported-hw' 1098 1204 * property. 1099 - * 1100 - * Locking: The internal opp_table and opp structures are RCU protected. 1101 - * Hence this function internally uses RCU updater strategy with mutex locks 1102 - * to keep the integrity of the internal data structures. Callers should ensure 1103 - * that this function is *NOT* called under RCU protection or in contexts where 1104 - * mutex cannot be locked. 1105 1205 */ 1106 - int dev_pm_opp_set_supported_hw(struct device *dev, const u32 *versions, 1107 - unsigned int count) 1206 + struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev, 1207 + const u32 *versions, unsigned int count) 1108 1208 { 1109 1209 struct opp_table *opp_table; 1110 - int ret = 0; 1210 + int ret; 1111 1211 1112 - /* Hold our table modification lock here */ 1113 - mutex_lock(&opp_table_lock); 1114 - 1115 - opp_table = _add_opp_table(dev); 1116 - if (!opp_table) { 1117 - ret = -ENOMEM; 1118 - goto unlock; 1119 - } 1212 + opp_table = dev_pm_opp_get_opp_table(dev); 1213 + if (!opp_table) 1214 + return ERR_PTR(-ENOMEM); 1120 1215 1121 1216 /* Make sure there are no concurrent readers while updating opp_table */ 1122 1217 WARN_ON(!list_empty(&opp_table->opp_list)); ··· 1126 1243 } 1127 1244 1128 1245 opp_table->supported_hw_count = count; 1129 - mutex_unlock(&opp_table_lock); 1130 - return 0; 1246 + 1247 + return opp_table; 1131 1248 1132 1249 err: 1133 - _remove_opp_table(opp_table); 1134 - unlock: 1135 - mutex_unlock(&opp_table_lock); 1250 + dev_pm_opp_put_opp_table(opp_table); 1136 1251 1137 - return ret; 1252 + return ERR_PTR(ret); 1138 1253 } 1139 1254 EXPORT_SYMBOL_GPL(dev_pm_opp_set_supported_hw); 1140 1255 1141 1256 /** 1142 1257 * dev_pm_opp_put_supported_hw() - Releases resources blocked for supported hw 1143 - * @dev: Device for which supported-hw has to be put. 1258 + * @opp_table: OPP table returned by dev_pm_opp_set_supported_hw(). 1144 1259 * 1145 1260 * This is required only for the V2 bindings, and is called for a matching 1146 1261 * dev_pm_opp_set_supported_hw(). Until this is called, the opp_table structure 1147 1262 * will not be freed. 1148 - * 1149 - * Locking: The internal opp_table and opp structures are RCU protected. 1150 - * Hence this function internally uses RCU updater strategy with mutex locks 1151 - * to keep the integrity of the internal data structures. Callers should ensure 1152 - * that this function is *NOT* called under RCU protection or in contexts where 1153 - * mutex cannot be locked. 1154 1263 */ 1155 - void dev_pm_opp_put_supported_hw(struct device *dev) 1264 + void dev_pm_opp_put_supported_hw(struct opp_table *opp_table) 1156 1265 { 1157 - struct opp_table *opp_table; 1158 - 1159 - /* Hold our table modification lock here */ 1160 - mutex_lock(&opp_table_lock); 1161 - 1162 - /* Check for existing table for 'dev' first */ 1163 - opp_table = _find_opp_table(dev); 1164 - if (IS_ERR(opp_table)) { 1165 - dev_err(dev, "Failed to find opp_table: %ld\n", 1166 - PTR_ERR(opp_table)); 1167 - goto unlock; 1168 - } 1169 - 1170 1266 /* Make sure there are no concurrent readers while updating opp_table */ 1171 1267 WARN_ON(!list_empty(&opp_table->opp_list)); 1172 1268 1173 1269 if (!opp_table->supported_hw) { 1174 - dev_err(dev, "%s: Doesn't have supported hardware list\n", 1175 - __func__); 1176 - goto unlock; 1270 + pr_err("%s: Doesn't have supported hardware list\n", 1271 + __func__); 1272 + return; 1177 1273 } 1178 1274 1179 1275 kfree(opp_table->supported_hw); 1180 1276 opp_table->supported_hw = NULL; 1181 1277 opp_table->supported_hw_count = 0; 1182 1278 1183 - /* Try freeing opp_table if this was the last blocking resource */ 1184 - _remove_opp_table(opp_table); 1185 - 1186 - unlock: 1187 - mutex_unlock(&opp_table_lock); 1279 + dev_pm_opp_put_opp_table(opp_table); 1188 1280 } 1189 1281 EXPORT_SYMBOL_GPL(dev_pm_opp_put_supported_hw); 1190 1282 ··· 1172 1314 * specify the extn to be used for certain property names. The properties to 1173 1315 * which the extension will apply are opp-microvolt and opp-microamp. OPP core 1174 1316 * should postfix the property name with -<name> while looking for them. 1175 - * 1176 - * Locking: The internal opp_table and opp structures are RCU protected. 1177 - * Hence this function internally uses RCU updater strategy with mutex locks 1178 - * to keep the integrity of the internal data structures. Callers should ensure 1179 - * that this function is *NOT* called under RCU protection or in contexts where 1180 - * mutex cannot be locked. 1181 1317 */ 1182 - int dev_pm_opp_set_prop_name(struct device *dev, const char *name) 1318 + struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name) 1183 1319 { 1184 1320 struct opp_table *opp_table; 1185 - int ret = 0; 1321 + int ret; 1186 1322 1187 - /* Hold our table modification lock here */ 1188 - mutex_lock(&opp_table_lock); 1189 - 1190 - opp_table = _add_opp_table(dev); 1191 - if (!opp_table) { 1192 - ret = -ENOMEM; 1193 - goto unlock; 1194 - } 1323 + opp_table = dev_pm_opp_get_opp_table(dev); 1324 + if (!opp_table) 1325 + return ERR_PTR(-ENOMEM); 1195 1326 1196 1327 /* Make sure there are no concurrent readers while updating opp_table */ 1197 1328 WARN_ON(!list_empty(&opp_table->opp_list)); ··· 1199 1352 goto err; 1200 1353 } 1201 1354 1202 - mutex_unlock(&opp_table_lock); 1203 - return 0; 1355 + return opp_table; 1204 1356 1205 1357 err: 1206 - _remove_opp_table(opp_table); 1207 - unlock: 1208 - mutex_unlock(&opp_table_lock); 1358 + dev_pm_opp_put_opp_table(opp_table); 1209 1359 1210 - return ret; 1360 + return ERR_PTR(ret); 1211 1361 } 1212 1362 EXPORT_SYMBOL_GPL(dev_pm_opp_set_prop_name); 1213 1363 1214 1364 /** 1215 1365 * dev_pm_opp_put_prop_name() - Releases resources blocked for prop-name 1216 - * @dev: Device for which the prop-name has to be put. 1366 + * @opp_table: OPP table returned by dev_pm_opp_set_prop_name(). 1217 1367 * 1218 1368 * This is required only for the V2 bindings, and is called for a matching 1219 1369 * dev_pm_opp_set_prop_name(). Until this is called, the opp_table structure 1220 1370 * will not be freed. 1221 - * 1222 - * Locking: The internal opp_table and opp structures are RCU protected. 1223 - * Hence this function internally uses RCU updater strategy with mutex locks 1224 - * to keep the integrity of the internal data structures. Callers should ensure 1225 - * that this function is *NOT* called under RCU protection or in contexts where 1226 - * mutex cannot be locked. 1227 1371 */ 1228 - void dev_pm_opp_put_prop_name(struct device *dev) 1372 + void dev_pm_opp_put_prop_name(struct opp_table *opp_table) 1229 1373 { 1230 - struct opp_table *opp_table; 1231 - 1232 - /* Hold our table modification lock here */ 1233 - mutex_lock(&opp_table_lock); 1234 - 1235 - /* Check for existing table for 'dev' first */ 1236 - opp_table = _find_opp_table(dev); 1237 - if (IS_ERR(opp_table)) { 1238 - dev_err(dev, "Failed to find opp_table: %ld\n", 1239 - PTR_ERR(opp_table)); 1240 - goto unlock; 1241 - } 1242 - 1243 1374 /* Make sure there are no concurrent readers while updating opp_table */ 1244 1375 WARN_ON(!list_empty(&opp_table->opp_list)); 1245 1376 1246 1377 if (!opp_table->prop_name) { 1247 - dev_err(dev, "%s: Doesn't have a prop-name\n", __func__); 1248 - goto unlock; 1378 + pr_err("%s: Doesn't have a prop-name\n", __func__); 1379 + return; 1249 1380 } 1250 1381 1251 1382 kfree(opp_table->prop_name); 1252 1383 opp_table->prop_name = NULL; 1253 1384 1254 - /* Try freeing opp_table if this was the last blocking resource */ 1255 - _remove_opp_table(opp_table); 1256 - 1257 - unlock: 1258 - mutex_unlock(&opp_table_lock); 1385 + dev_pm_opp_put_opp_table(opp_table); 1259 1386 } 1260 1387 EXPORT_SYMBOL_GPL(dev_pm_opp_put_prop_name); 1261 1388 ··· 1276 1455 * well. 1277 1456 * 1278 1457 * This must be called before any OPPs are initialized for the device. 1279 - * 1280 - * Locking: The internal opp_table and opp structures are RCU protected. 1281 - * Hence this function internally uses RCU updater strategy with mutex locks 1282 - * to keep the integrity of the internal data structures. Callers should ensure 1283 - * that this function is *NOT* called under RCU protection or in contexts where 1284 - * mutex cannot be locked. 1285 1458 */ 1286 1459 struct opp_table *dev_pm_opp_set_regulators(struct device *dev, 1287 1460 const char * const names[], ··· 1285 1470 struct regulator *reg; 1286 1471 int ret, i; 1287 1472 1288 - mutex_lock(&opp_table_lock); 1289 - 1290 - opp_table = _add_opp_table(dev); 1291 - if (!opp_table) { 1292 - ret = -ENOMEM; 1293 - goto unlock; 1294 - } 1473 + opp_table = dev_pm_opp_get_opp_table(dev); 1474 + if (!opp_table) 1475 + return ERR_PTR(-ENOMEM); 1295 1476 1296 1477 /* This should be called before OPPs are initialized */ 1297 1478 if (WARN_ON(!list_empty(&opp_table->opp_list))) { ··· 1329 1518 if (ret) 1330 1519 goto free_regulators; 1331 1520 1332 - mutex_unlock(&opp_table_lock); 1333 1521 return opp_table; 1334 1522 1335 1523 free_regulators: ··· 1339 1529 opp_table->regulators = NULL; 1340 1530 opp_table->regulator_count = 0; 1341 1531 err: 1342 - _remove_opp_table(opp_table); 1343 - unlock: 1344 - mutex_unlock(&opp_table_lock); 1532 + dev_pm_opp_put_opp_table(opp_table); 1345 1533 1346 1534 return ERR_PTR(ret); 1347 1535 } ··· 1348 1540 /** 1349 1541 * dev_pm_opp_put_regulators() - Releases resources blocked for regulator 1350 1542 * @opp_table: OPP table returned from dev_pm_opp_set_regulators(). 1351 - * 1352 - * Locking: The internal opp_table and opp structures are RCU protected. 1353 - * Hence this function internally uses RCU updater strategy with mutex locks 1354 - * to keep the integrity of the internal data structures. Callers should ensure 1355 - * that this function is *NOT* called under RCU protection or in contexts where 1356 - * mutex cannot be locked. 1357 1543 */ 1358 1544 void dev_pm_opp_put_regulators(struct opp_table *opp_table) 1359 1545 { 1360 1546 int i; 1361 1547 1362 - mutex_lock(&opp_table_lock); 1363 - 1364 1548 if (!opp_table->regulators) { 1365 1549 pr_err("%s: Doesn't have regulators set\n", __func__); 1366 - goto unlock; 1550 + return; 1367 1551 } 1368 1552 1369 1553 /* Make sure there are no concurrent readers while updating opp_table */ ··· 1370 1570 opp_table->regulators = NULL; 1371 1571 opp_table->regulator_count = 0; 1372 1572 1373 - /* Try freeing opp_table if this was the last blocking resource */ 1374 - _remove_opp_table(opp_table); 1375 - 1376 - unlock: 1377 - mutex_unlock(&opp_table_lock); 1573 + dev_pm_opp_put_opp_table(opp_table); 1378 1574 } 1379 1575 EXPORT_SYMBOL_GPL(dev_pm_opp_put_regulators); 1380 1576 ··· 1383 1587 * regulators per device), instead of the generic OPP set rate helper. 1384 1588 * 1385 1589 * This must be called before any OPPs are initialized for the device. 1386 - * 1387 - * Locking: The internal opp_table and opp structures are RCU protected. 1388 - * Hence this function internally uses RCU updater strategy with mutex locks 1389 - * to keep the integrity of the internal data structures. Callers should ensure 1390 - * that this function is *NOT* called under RCU protection or in contexts where 1391 - * mutex cannot be locked. 1392 1590 */ 1393 - int dev_pm_opp_register_set_opp_helper(struct device *dev, 1591 + struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev, 1394 1592 int (*set_opp)(struct dev_pm_set_opp_data *data)) 1395 1593 { 1396 1594 struct opp_table *opp_table; 1397 1595 int ret; 1398 1596 1399 1597 if (!set_opp) 1400 - return -EINVAL; 1598 + return ERR_PTR(-EINVAL); 1401 1599 1402 - mutex_lock(&opp_table_lock); 1403 - 1404 - opp_table = _add_opp_table(dev); 1405 - if (!opp_table) { 1406 - ret = -ENOMEM; 1407 - goto unlock; 1408 - } 1600 + opp_table = dev_pm_opp_get_opp_table(dev); 1601 + if (!opp_table) 1602 + return ERR_PTR(-ENOMEM); 1409 1603 1410 1604 /* This should be called before OPPs are initialized */ 1411 1605 if (WARN_ON(!list_empty(&opp_table->opp_list))) { ··· 1411 1625 1412 1626 opp_table->set_opp = set_opp; 1413 1627 1414 - mutex_unlock(&opp_table_lock); 1415 - return 0; 1628 + return opp_table; 1416 1629 1417 1630 err: 1418 - _remove_opp_table(opp_table); 1419 - unlock: 1420 - mutex_unlock(&opp_table_lock); 1631 + dev_pm_opp_put_opp_table(opp_table); 1421 1632 1422 - return ret; 1633 + return ERR_PTR(ret); 1423 1634 } 1424 1635 EXPORT_SYMBOL_GPL(dev_pm_opp_register_set_opp_helper); 1425 1636 1426 1637 /** 1427 1638 * dev_pm_opp_register_put_opp_helper() - Releases resources blocked for 1428 1639 * set_opp helper 1429 - * @dev: Device for which custom set_opp helper has to be cleared. 1640 + * @opp_table: OPP table returned from dev_pm_opp_register_set_opp_helper(). 1430 1641 * 1431 - * Locking: The internal opp_table and opp structures are RCU protected. 1432 - * Hence this function internally uses RCU updater strategy with mutex locks 1433 - * to keep the integrity of the internal data structures. Callers should ensure 1434 - * that this function is *NOT* called under RCU protection or in contexts where 1435 - * mutex cannot be locked. 1642 + * Release resources blocked for platform specific set_opp helper. 1436 1643 */ 1437 - void dev_pm_opp_register_put_opp_helper(struct device *dev) 1644 + void dev_pm_opp_register_put_opp_helper(struct opp_table *opp_table) 1438 1645 { 1439 - struct opp_table *opp_table; 1440 - 1441 - mutex_lock(&opp_table_lock); 1442 - 1443 - /* Check for existing table for 'dev' first */ 1444 - opp_table = _find_opp_table(dev); 1445 - if (IS_ERR(opp_table)) { 1446 - dev_err(dev, "Failed to find opp_table: %ld\n", 1447 - PTR_ERR(opp_table)); 1448 - goto unlock; 1449 - } 1450 - 1451 1646 if (!opp_table->set_opp) { 1452 - dev_err(dev, "%s: Doesn't have custom set_opp helper set\n", 1453 - __func__); 1454 - goto unlock; 1647 + pr_err("%s: Doesn't have custom set_opp helper set\n", 1648 + __func__); 1649 + return; 1455 1650 } 1456 1651 1457 1652 /* Make sure there are no concurrent readers while updating opp_table */ ··· 1440 1673 1441 1674 opp_table->set_opp = NULL; 1442 1675 1443 - /* Try freeing opp_table if this was the last blocking resource */ 1444 - _remove_opp_table(opp_table); 1445 - 1446 - unlock: 1447 - mutex_unlock(&opp_table_lock); 1676 + dev_pm_opp_put_opp_table(opp_table); 1448 1677 } 1449 1678 EXPORT_SYMBOL_GPL(dev_pm_opp_register_put_opp_helper); 1450 1679 ··· 1454 1691 * The opp is made available by default and it can be controlled using 1455 1692 * dev_pm_opp_enable/disable functions. 1456 1693 * 1457 - * Locking: The internal opp_table and opp structures are RCU protected. 1458 - * Hence this function internally uses RCU updater strategy with mutex locks 1459 - * to keep the integrity of the internal data structures. Callers should ensure 1460 - * that this function is *NOT* called under RCU protection or in contexts where 1461 - * mutex cannot be locked. 1462 - * 1463 1694 * Return: 1464 1695 * 0 On success OR 1465 1696 * Duplicate OPPs (both freq and volt are same) and opp->available ··· 1463 1706 */ 1464 1707 int dev_pm_opp_add(struct device *dev, unsigned long freq, unsigned long u_volt) 1465 1708 { 1466 - return _opp_add_v1(dev, freq, u_volt, true); 1709 + struct opp_table *opp_table; 1710 + int ret; 1711 + 1712 + opp_table = dev_pm_opp_get_opp_table(dev); 1713 + if (!opp_table) 1714 + return -ENOMEM; 1715 + 1716 + ret = _opp_add_v1(opp_table, dev, freq, u_volt, true); 1717 + 1718 + dev_pm_opp_put_opp_table(opp_table); 1719 + return ret; 1467 1720 } 1468 1721 EXPORT_SYMBOL_GPL(dev_pm_opp_add); 1469 1722 ··· 1483 1716 * @freq: OPP frequency to modify availability 1484 1717 * @availability_req: availability status requested for this opp 1485 1718 * 1486 - * Set the availability of an OPP with an RCU operation, opp_{enable,disable} 1487 - * share a common logic which is isolated here. 1719 + * Set the availability of an OPP, opp_{enable,disable} share a common logic 1720 + * which is isolated here. 1488 1721 * 1489 1722 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 1490 1723 * copy operation, returns 0 if no modification was done OR modification was 1491 1724 * successful. 1492 - * 1493 - * Locking: The internal opp_table and opp structures are RCU protected. 1494 - * Hence this function internally uses RCU updater strategy with mutex locks to 1495 - * keep the integrity of the internal data structures. Callers should ensure 1496 - * that this function is *NOT* called under RCU protection or in contexts where 1497 - * mutex locking or synchronize_rcu() blocking calls cannot be used. 1498 1725 */ 1499 1726 static int _opp_set_availability(struct device *dev, unsigned long freq, 1500 1727 bool availability_req) 1501 1728 { 1502 1729 struct opp_table *opp_table; 1503 - struct dev_pm_opp *new_opp, *tmp_opp, *opp = ERR_PTR(-ENODEV); 1730 + struct dev_pm_opp *tmp_opp, *opp = ERR_PTR(-ENODEV); 1504 1731 int r = 0; 1505 - 1506 - /* keep the node allocated */ 1507 - new_opp = kmalloc(sizeof(*new_opp), GFP_KERNEL); 1508 - if (!new_opp) 1509 - return -ENOMEM; 1510 - 1511 - mutex_lock(&opp_table_lock); 1512 1732 1513 1733 /* Find the opp_table */ 1514 1734 opp_table = _find_opp_table(dev); 1515 1735 if (IS_ERR(opp_table)) { 1516 1736 r = PTR_ERR(opp_table); 1517 1737 dev_warn(dev, "%s: Device OPP not found (%d)\n", __func__, r); 1518 - goto unlock; 1738 + return r; 1519 1739 } 1740 + 1741 + mutex_lock(&opp_table->lock); 1520 1742 1521 1743 /* Do we have the frequency? */ 1522 1744 list_for_each_entry(tmp_opp, &opp_table->opp_list, node) { ··· 1514 1758 break; 1515 1759 } 1516 1760 } 1761 + 1517 1762 if (IS_ERR(opp)) { 1518 1763 r = PTR_ERR(opp); 1519 1764 goto unlock; ··· 1523 1766 /* Is update really needed? */ 1524 1767 if (opp->available == availability_req) 1525 1768 goto unlock; 1526 - /* copy the old data over */ 1527 - *new_opp = *opp; 1528 1769 1529 - /* plug in new node */ 1530 - new_opp->available = availability_req; 1531 - 1532 - list_replace_rcu(&opp->node, &new_opp->node); 1533 - mutex_unlock(&opp_table_lock); 1534 - call_srcu(&opp_table->srcu_head.srcu, &opp->rcu_head, _kfree_opp_rcu); 1770 + opp->available = availability_req; 1535 1771 1536 1772 /* Notify the change of the OPP availability */ 1537 1773 if (availability_req) 1538 - srcu_notifier_call_chain(&opp_table->srcu_head, 1539 - OPP_EVENT_ENABLE, new_opp); 1774 + blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ENABLE, 1775 + opp); 1540 1776 else 1541 - srcu_notifier_call_chain(&opp_table->srcu_head, 1542 - OPP_EVENT_DISABLE, new_opp); 1543 - 1544 - return 0; 1777 + blocking_notifier_call_chain(&opp_table->head, 1778 + OPP_EVENT_DISABLE, opp); 1545 1779 1546 1780 unlock: 1547 - mutex_unlock(&opp_table_lock); 1548 - kfree(new_opp); 1781 + mutex_unlock(&opp_table->lock); 1782 + dev_pm_opp_put_opp_table(opp_table); 1549 1783 return r; 1550 1784 } 1551 1785 ··· 1548 1800 * Enables a provided opp. If the operation is valid, this returns 0, else the 1549 1801 * corresponding error value. It is meant to be used for users an OPP available 1550 1802 * after being temporarily made unavailable with dev_pm_opp_disable. 1551 - * 1552 - * Locking: The internal opp_table and opp structures are RCU protected. 1553 - * Hence this function indirectly uses RCU and mutex locks to keep the 1554 - * integrity of the internal data structures. Callers should ensure that 1555 - * this function is *NOT* called under RCU protection or in contexts where 1556 - * mutex locking or synchronize_rcu() blocking calls cannot be used. 1557 1803 * 1558 1804 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 1559 1805 * copy operation, returns 0 if no modification was done OR modification was ··· 1569 1827 * control by users to make this OPP not available until the circumstances are 1570 1828 * right to make it available again (with a call to dev_pm_opp_enable). 1571 1829 * 1572 - * Locking: The internal opp_table and opp structures are RCU protected. 1573 - * Hence this function indirectly uses RCU and mutex locks to keep the 1574 - * integrity of the internal data structures. Callers should ensure that 1575 - * this function is *NOT* called under RCU protection or in contexts where 1576 - * mutex locking or synchronize_rcu() blocking calls cannot be used. 1577 - * 1578 1830 * Return: -EINVAL for bad pointers, -ENOMEM if no memory available for the 1579 1831 * copy operation, returns 0 if no modification was done OR modification was 1580 1832 * successful. ··· 1580 1844 EXPORT_SYMBOL_GPL(dev_pm_opp_disable); 1581 1845 1582 1846 /** 1583 - * dev_pm_opp_get_notifier() - find notifier_head of the device with opp 1584 - * @dev: device pointer used to lookup OPP table. 1847 + * dev_pm_opp_register_notifier() - Register OPP notifier for the device 1848 + * @dev: Device for which notifier needs to be registered 1849 + * @nb: Notifier block to be registered 1585 1850 * 1586 - * Return: pointer to notifier head if found, otherwise -ENODEV or 1587 - * -EINVAL based on type of error casted as pointer. value must be checked 1588 - * with IS_ERR to determine valid pointer or error result. 1589 - * 1590 - * Locking: This function must be called under rcu_read_lock(). opp_table is a 1591 - * RCU protected pointer. The reason for the same is that the opp pointer which 1592 - * is returned will remain valid for use with opp_get_{voltage, freq} only while 1593 - * under the locked area. The pointer returned must be used prior to unlocking 1594 - * with rcu_read_unlock() to maintain the integrity of the pointer. 1851 + * Return: 0 on success or a negative error value. 1595 1852 */ 1596 - struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev) 1853 + int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb) 1597 1854 { 1598 - struct opp_table *opp_table = _find_opp_table(dev); 1855 + struct opp_table *opp_table; 1856 + int ret; 1599 1857 1858 + opp_table = _find_opp_table(dev); 1600 1859 if (IS_ERR(opp_table)) 1601 - return ERR_CAST(opp_table); /* matching type */ 1860 + return PTR_ERR(opp_table); 1602 1861 1603 - return &opp_table->srcu_head; 1862 + ret = blocking_notifier_chain_register(&opp_table->head, nb); 1863 + 1864 + dev_pm_opp_put_opp_table(opp_table); 1865 + 1866 + return ret; 1604 1867 } 1605 - EXPORT_SYMBOL_GPL(dev_pm_opp_get_notifier); 1868 + EXPORT_SYMBOL(dev_pm_opp_register_notifier); 1869 + 1870 + /** 1871 + * dev_pm_opp_unregister_notifier() - Unregister OPP notifier for the device 1872 + * @dev: Device for which notifier needs to be unregistered 1873 + * @nb: Notifier block to be unregistered 1874 + * 1875 + * Return: 0 on success or a negative error value. 1876 + */ 1877 + int dev_pm_opp_unregister_notifier(struct device *dev, 1878 + struct notifier_block *nb) 1879 + { 1880 + struct opp_table *opp_table; 1881 + int ret; 1882 + 1883 + opp_table = _find_opp_table(dev); 1884 + if (IS_ERR(opp_table)) 1885 + return PTR_ERR(opp_table); 1886 + 1887 + ret = blocking_notifier_chain_unregister(&opp_table->head, nb); 1888 + 1889 + dev_pm_opp_put_opp_table(opp_table); 1890 + 1891 + return ret; 1892 + } 1893 + EXPORT_SYMBOL(dev_pm_opp_unregister_notifier); 1606 1894 1607 1895 /* 1608 1896 * Free OPPs either created using static entries present in DT or even the 1609 1897 * dynamically added entries based on remove_all param. 1610 1898 */ 1611 - void _dev_pm_opp_remove_table(struct device *dev, bool remove_all) 1899 + void _dev_pm_opp_remove_table(struct opp_table *opp_table, struct device *dev, 1900 + bool remove_all) 1612 1901 { 1613 - struct opp_table *opp_table; 1614 1902 struct dev_pm_opp *opp, *tmp; 1615 1903 1616 - /* Hold our table modification lock here */ 1617 - mutex_lock(&opp_table_lock); 1904 + /* Find if opp_table manages a single device */ 1905 + if (list_is_singular(&opp_table->dev_list)) { 1906 + /* Free static OPPs */ 1907 + list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) { 1908 + if (remove_all || !opp->dynamic) 1909 + dev_pm_opp_put(opp); 1910 + } 1911 + } else { 1912 + _remove_opp_dev(_find_opp_dev(dev, opp_table), opp_table); 1913 + } 1914 + } 1915 + 1916 + void _dev_pm_opp_find_and_remove_table(struct device *dev, bool remove_all) 1917 + { 1918 + struct opp_table *opp_table; 1618 1919 1619 1920 /* Check for existing table for 'dev' */ 1620 1921 opp_table = _find_opp_table(dev); ··· 1663 1890 IS_ERR_OR_NULL(dev) ? 1664 1891 "Invalid device" : dev_name(dev), 1665 1892 error); 1666 - goto unlock; 1893 + return; 1667 1894 } 1668 1895 1669 - /* Find if opp_table manages a single device */ 1670 - if (list_is_singular(&opp_table->dev_list)) { 1671 - /* Free static OPPs */ 1672 - list_for_each_entry_safe(opp, tmp, &opp_table->opp_list, node) { 1673 - if (remove_all || !opp->dynamic) 1674 - _opp_remove(opp_table, opp, true); 1675 - } 1676 - } else { 1677 - _remove_opp_dev(_find_opp_dev(dev, opp_table), opp_table); 1678 - } 1896 + _dev_pm_opp_remove_table(opp_table, dev, remove_all); 1679 1897 1680 - unlock: 1681 - mutex_unlock(&opp_table_lock); 1898 + dev_pm_opp_put_opp_table(opp_table); 1682 1899 } 1683 1900 1684 1901 /** ··· 1677 1914 * 1678 1915 * Free both OPPs created using static entries present in DT and the 1679 1916 * dynamically added entries. 1680 - * 1681 - * Locking: The internal opp_table and opp structures are RCU protected. 1682 - * Hence this function indirectly uses RCU updater strategy with mutex locks 1683 - * to keep the integrity of the internal data structures. Callers should ensure 1684 - * that this function is *NOT* called under RCU protection or in contexts where 1685 - * mutex cannot be locked. 1686 1917 */ 1687 1918 void dev_pm_opp_remove_table(struct device *dev) 1688 1919 { 1689 - _dev_pm_opp_remove_table(dev, true); 1920 + _dev_pm_opp_find_and_remove_table(dev, true); 1690 1921 } 1691 1922 EXPORT_SYMBOL_GPL(dev_pm_opp_remove_table);
+15 -51
drivers/base/power/opp/cpu.c
··· 42 42 * 43 43 * WARNING: It is important for the callers to ensure refreshing their copy of 44 44 * the table if any of the mentioned functions have been invoked in the interim. 45 - * 46 - * Locking: The internal opp_table and opp structures are RCU protected. 47 - * Since we just use the regular accessor functions to access the internal data 48 - * structures, we use RCU read lock inside this function. As a result, users of 49 - * this function DONOT need to use explicit locks for invoking. 50 45 */ 51 46 int dev_pm_opp_init_cpufreq_table(struct device *dev, 52 47 struct cpufreq_frequency_table **table) ··· 51 56 int i, max_opps, ret = 0; 52 57 unsigned long rate; 53 58 54 - rcu_read_lock(); 55 - 56 59 max_opps = dev_pm_opp_get_opp_count(dev); 57 - if (max_opps <= 0) { 58 - ret = max_opps ? max_opps : -ENODATA; 59 - goto out; 60 - } 60 + if (max_opps <= 0) 61 + return max_opps ? max_opps : -ENODATA; 61 62 62 63 freq_table = kcalloc((max_opps + 1), sizeof(*freq_table), GFP_ATOMIC); 63 - if (!freq_table) { 64 - ret = -ENOMEM; 65 - goto out; 66 - } 64 + if (!freq_table) 65 + return -ENOMEM; 67 66 68 67 for (i = 0, rate = 0; i < max_opps; i++, rate++) { 69 68 /* find next rate */ ··· 72 83 /* Is Boost/turbo opp ? */ 73 84 if (dev_pm_opp_is_turbo(opp)) 74 85 freq_table[i].flags = CPUFREQ_BOOST_FREQ; 86 + 87 + dev_pm_opp_put(opp); 75 88 } 76 89 77 90 freq_table[i].driver_data = i; ··· 82 91 *table = &freq_table[0]; 83 92 84 93 out: 85 - rcu_read_unlock(); 86 94 if (ret) 87 95 kfree(freq_table); 88 96 ··· 137 147 * This removes the OPP tables for CPUs present in the @cpumask. 138 148 * This should be used to remove all the OPPs entries associated with 139 149 * the cpus in @cpumask. 140 - * 141 - * Locking: The internal opp_table and opp structures are RCU protected. 142 - * Hence this function internally uses RCU updater strategy with mutex locks 143 - * to keep the integrity of the internal data structures. Callers should ensure 144 - * that this function is *NOT* called under RCU protection or in contexts where 145 - * mutex cannot be locked. 146 150 */ 147 151 void dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask) 148 152 { ··· 153 169 * @cpumask. 154 170 * 155 171 * Returns -ENODEV if OPP table isn't already present. 156 - * 157 - * Locking: The internal opp_table and opp structures are RCU protected. 158 - * Hence this function internally uses RCU updater strategy with mutex locks 159 - * to keep the integrity of the internal data structures. Callers should ensure 160 - * that this function is *NOT* called under RCU protection or in contexts where 161 - * mutex cannot be locked. 162 172 */ 163 173 int dev_pm_opp_set_sharing_cpus(struct device *cpu_dev, 164 174 const struct cpumask *cpumask) ··· 162 184 struct device *dev; 163 185 int cpu, ret = 0; 164 186 165 - mutex_lock(&opp_table_lock); 166 - 167 187 opp_table = _find_opp_table(cpu_dev); 168 - if (IS_ERR(opp_table)) { 169 - ret = PTR_ERR(opp_table); 170 - goto unlock; 171 - } 188 + if (IS_ERR(opp_table)) 189 + return PTR_ERR(opp_table); 172 190 173 191 for_each_cpu(cpu, cpumask) { 174 192 if (cpu == cpu_dev->id) ··· 187 213 /* Mark opp-table as multiple CPUs are sharing it now */ 188 214 opp_table->shared_opp = OPP_TABLE_ACCESS_SHARED; 189 215 } 190 - unlock: 191 - mutex_unlock(&opp_table_lock); 216 + 217 + dev_pm_opp_put_opp_table(opp_table); 192 218 193 219 return ret; 194 220 } ··· 203 229 * 204 230 * Returns -ENODEV if OPP table isn't already present and -EINVAL if the OPP 205 231 * table's status is access-unknown. 206 - * 207 - * Locking: The internal opp_table and opp structures are RCU protected. 208 - * Hence this function internally uses RCU updater strategy with mutex locks 209 - * to keep the integrity of the internal data structures. Callers should ensure 210 - * that this function is *NOT* called under RCU protection or in contexts where 211 - * mutex cannot be locked. 212 232 */ 213 233 int dev_pm_opp_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask) 214 234 { ··· 210 242 struct opp_table *opp_table; 211 243 int ret = 0; 212 244 213 - mutex_lock(&opp_table_lock); 214 - 215 245 opp_table = _find_opp_table(cpu_dev); 216 - if (IS_ERR(opp_table)) { 217 - ret = PTR_ERR(opp_table); 218 - goto unlock; 219 - } 246 + if (IS_ERR(opp_table)) 247 + return PTR_ERR(opp_table); 220 248 221 249 if (opp_table->shared_opp == OPP_TABLE_ACCESS_UNKNOWN) { 222 250 ret = -EINVAL; 223 - goto unlock; 251 + goto put_opp_table; 224 252 } 225 253 226 254 cpumask_clear(cpumask); ··· 228 264 cpumask_set_cpu(cpu_dev->id, cpumask); 229 265 } 230 266 231 - unlock: 232 - mutex_unlock(&opp_table_lock); 267 + put_opp_table: 268 + dev_pm_opp_put_opp_table(opp_table); 233 269 234 270 return ret; 235 271 }
+60 -94
drivers/base/power/opp/of.c
··· 24 24 25 25 static struct opp_table *_managed_opp(const struct device_node *np) 26 26 { 27 - struct opp_table *opp_table; 27 + struct opp_table *opp_table, *managed_table = NULL; 28 28 29 - list_for_each_entry_rcu(opp_table, &opp_tables, node) { 29 + mutex_lock(&opp_table_lock); 30 + 31 + list_for_each_entry(opp_table, &opp_tables, node) { 30 32 if (opp_table->np == np) { 31 33 /* 32 34 * Multiple devices can point to the same OPP table and ··· 37 35 * But the OPPs will be considered as shared only if the 38 36 * OPP table contains a "opp-shared" property. 39 37 */ 40 - if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) 41 - return opp_table; 38 + if (opp_table->shared_opp == OPP_TABLE_ACCESS_SHARED) { 39 + _get_opp_table_kref(opp_table); 40 + managed_table = opp_table; 41 + } 42 42 43 - return NULL; 43 + break; 44 44 } 45 45 } 46 46 47 - return NULL; 47 + mutex_unlock(&opp_table_lock); 48 + 49 + return managed_table; 48 50 } 49 51 50 52 void _of_init_opp_table(struct opp_table *opp_table, struct device *dev) ··· 235 229 * @dev: device pointer used to lookup OPP table. 236 230 * 237 231 * Free OPPs created using static entries present in DT. 238 - * 239 - * Locking: The internal opp_table and opp structures are RCU protected. 240 - * Hence this function indirectly uses RCU updater strategy with mutex locks 241 - * to keep the integrity of the internal data structures. Callers should ensure 242 - * that this function is *NOT* called under RCU protection or in contexts where 243 - * mutex cannot be locked. 244 232 */ 245 233 void dev_pm_opp_of_remove_table(struct device *dev) 246 234 { 247 - _dev_pm_opp_remove_table(dev, false); 235 + _dev_pm_opp_find_and_remove_table(dev, false); 248 236 } 249 237 EXPORT_SYMBOL_GPL(dev_pm_opp_of_remove_table); 250 238 251 239 /* Returns opp descriptor node for a device, caller must do of_node_put() */ 252 - static struct device_node *_of_get_opp_desc_node(struct device *dev) 240 + struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev) 253 241 { 254 242 /* 255 - * TODO: Support for multiple OPP tables. 256 - * 257 243 * There should be only ONE phandle present in "operating-points-v2" 258 244 * property. 259 245 */ 260 246 261 247 return of_parse_phandle(dev->of_node, "operating-points-v2", 0); 262 248 } 249 + EXPORT_SYMBOL_GPL(dev_pm_opp_of_get_opp_desc_node); 263 250 264 251 /** 265 252 * _opp_add_static_v2() - Allocate static OPPs (As per 'v2' DT bindings) 253 + * @opp_table: OPP table 266 254 * @dev: device for which we do this operation 267 255 * @np: device node 268 256 * 269 257 * This function adds an opp definition to the opp table and returns status. The 270 258 * opp can be controlled using dev_pm_opp_enable/disable functions and may be 271 259 * removed by dev_pm_opp_remove. 272 - * 273 - * Locking: The internal opp_table and opp structures are RCU protected. 274 - * Hence this function internally uses RCU updater strategy with mutex locks 275 - * to keep the integrity of the internal data structures. Callers should ensure 276 - * that this function is *NOT* called under RCU protection or in contexts where 277 - * mutex cannot be locked. 278 260 * 279 261 * Return: 280 262 * 0 On success OR ··· 272 278 * -ENOMEM Memory allocation failure 273 279 * -EINVAL Failed parsing the OPP node 274 280 */ 275 - static int _opp_add_static_v2(struct device *dev, struct device_node *np) 281 + static int _opp_add_static_v2(struct opp_table *opp_table, struct device *dev, 282 + struct device_node *np) 276 283 { 277 - struct opp_table *opp_table; 278 284 struct dev_pm_opp *new_opp; 279 285 u64 rate; 280 286 u32 val; 281 287 int ret; 282 288 283 - /* Hold our table modification lock here */ 284 - mutex_lock(&opp_table_lock); 285 - 286 - new_opp = _allocate_opp(dev, &opp_table); 287 - if (!new_opp) { 288 - ret = -ENOMEM; 289 - goto unlock; 290 - } 289 + new_opp = _opp_allocate(opp_table); 290 + if (!new_opp) 291 + return -ENOMEM; 291 292 292 293 ret = of_property_read_u64(np, "opp-hz", &rate); 293 294 if (ret < 0) { ··· 316 327 goto free_opp; 317 328 318 329 ret = _opp_add(dev, new_opp, opp_table); 319 - if (ret) 330 + if (ret) { 331 + /* Don't return error for duplicate OPPs */ 332 + if (ret == -EBUSY) 333 + ret = 0; 320 334 goto free_opp; 335 + } 321 336 322 337 /* OPP to select on device suspend */ 323 338 if (of_property_read_bool(np, "opp-suspend")) { ··· 338 345 if (new_opp->clock_latency_ns > opp_table->clock_latency_ns_max) 339 346 opp_table->clock_latency_ns_max = new_opp->clock_latency_ns; 340 347 341 - mutex_unlock(&opp_table_lock); 342 - 343 348 pr_debug("%s: turbo:%d rate:%lu uv:%lu uvmin:%lu uvmax:%lu latency:%lu\n", 344 349 __func__, new_opp->turbo, new_opp->rate, 345 350 new_opp->supplies[0].u_volt, new_opp->supplies[0].u_volt_min, ··· 347 356 * Notify the changes in the availability of the operable 348 357 * frequency/voltage list. 349 358 */ 350 - srcu_notifier_call_chain(&opp_table->srcu_head, OPP_EVENT_ADD, new_opp); 359 + blocking_notifier_call_chain(&opp_table->head, OPP_EVENT_ADD, new_opp); 351 360 return 0; 352 361 353 362 free_opp: 354 - _opp_remove(opp_table, new_opp, false); 355 - unlock: 356 - mutex_unlock(&opp_table_lock); 363 + _opp_free(new_opp); 364 + 357 365 return ret; 358 366 } 359 367 ··· 363 373 struct opp_table *opp_table; 364 374 int ret = 0, count = 0; 365 375 366 - mutex_lock(&opp_table_lock); 367 - 368 376 opp_table = _managed_opp(opp_np); 369 377 if (opp_table) { 370 378 /* OPPs are already managed */ 371 379 if (!_add_opp_dev(dev, opp_table)) 372 380 ret = -ENOMEM; 373 - mutex_unlock(&opp_table_lock); 374 - return ret; 381 + goto put_opp_table; 375 382 } 376 - mutex_unlock(&opp_table_lock); 383 + 384 + opp_table = dev_pm_opp_get_opp_table(dev); 385 + if (!opp_table) 386 + return -ENOMEM; 377 387 378 388 /* We have opp-table node now, iterate over it and add OPPs */ 379 389 for_each_available_child_of_node(opp_np, np) { 380 390 count++; 381 391 382 - ret = _opp_add_static_v2(dev, np); 392 + ret = _opp_add_static_v2(opp_table, dev, np); 383 393 if (ret) { 384 394 dev_err(dev, "%s: Failed to add OPP, %d\n", __func__, 385 395 ret); 386 - goto free_table; 396 + _dev_pm_opp_remove_table(opp_table, dev, false); 397 + goto put_opp_table; 387 398 } 388 399 } 389 400 390 401 /* There should be one of more OPP defined */ 391 - if (WARN_ON(!count)) 392 - return -ENOENT; 393 - 394 - mutex_lock(&opp_table_lock); 395 - 396 - opp_table = _find_opp_table(dev); 397 - if (WARN_ON(IS_ERR(opp_table))) { 398 - ret = PTR_ERR(opp_table); 399 - mutex_unlock(&opp_table_lock); 400 - goto free_table; 402 + if (WARN_ON(!count)) { 403 + ret = -ENOENT; 404 + goto put_opp_table; 401 405 } 402 406 403 407 opp_table->np = opp_np; ··· 400 416 else 401 417 opp_table->shared_opp = OPP_TABLE_ACCESS_EXCLUSIVE; 402 418 403 - mutex_unlock(&opp_table_lock); 404 - 405 - return 0; 406 - 407 - free_table: 408 - dev_pm_opp_of_remove_table(dev); 419 + put_opp_table: 420 + dev_pm_opp_put_opp_table(opp_table); 409 421 410 422 return ret; 411 423 } ··· 409 429 /* Initializes OPP tables based on old-deprecated bindings */ 410 430 static int _of_add_opp_table_v1(struct device *dev) 411 431 { 432 + struct opp_table *opp_table; 412 433 const struct property *prop; 413 434 const __be32 *val; 414 - int nr; 435 + int nr, ret = 0; 415 436 416 437 prop = of_find_property(dev->of_node, "operating-points", NULL); 417 438 if (!prop) ··· 430 449 return -EINVAL; 431 450 } 432 451 452 + opp_table = dev_pm_opp_get_opp_table(dev); 453 + if (!opp_table) 454 + return -ENOMEM; 455 + 433 456 val = prop->value; 434 457 while (nr) { 435 458 unsigned long freq = be32_to_cpup(val++) * 1000; 436 459 unsigned long volt = be32_to_cpup(val++); 437 460 438 - if (_opp_add_v1(dev, freq, volt, false)) 439 - dev_warn(dev, "%s: Failed to add OPP %ld\n", 440 - __func__, freq); 461 + ret = _opp_add_v1(opp_table, dev, freq, volt, false); 462 + if (ret) { 463 + dev_err(dev, "%s: Failed to add OPP %ld (%d)\n", 464 + __func__, freq, ret); 465 + _dev_pm_opp_remove_table(opp_table, dev, false); 466 + break; 467 + } 441 468 nr -= 2; 442 469 } 443 470 444 - return 0; 471 + dev_pm_opp_put_opp_table(opp_table); 472 + return ret; 445 473 } 446 474 447 475 /** ··· 458 468 * @dev: device pointer used to lookup OPP table. 459 469 * 460 470 * Register the initial OPP table with the OPP library for given device. 461 - * 462 - * Locking: The internal opp_table and opp structures are RCU protected. 463 - * Hence this function indirectly uses RCU updater strategy with mutex locks 464 - * to keep the integrity of the internal data structures. Callers should ensure 465 - * that this function is *NOT* called under RCU protection or in contexts where 466 - * mutex cannot be locked. 467 471 * 468 472 * Return: 469 473 * 0 On success OR ··· 479 495 * OPPs have two version of bindings now. The older one is deprecated, 480 496 * try for the new binding first. 481 497 */ 482 - opp_np = _of_get_opp_desc_node(dev); 498 + opp_np = dev_pm_opp_of_get_opp_desc_node(dev); 483 499 if (!opp_np) { 484 500 /* 485 501 * Try old-deprecated bindings for backward compatibility with ··· 503 519 * 504 520 * This removes the OPP tables for CPUs present in the @cpumask. 505 521 * This should be used only to remove static entries created from DT. 506 - * 507 - * Locking: The internal opp_table and opp structures are RCU protected. 508 - * Hence this function internally uses RCU updater strategy with mutex locks 509 - * to keep the integrity of the internal data structures. Callers should ensure 510 - * that this function is *NOT* called under RCU protection or in contexts where 511 - * mutex cannot be locked. 512 522 */ 513 523 void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask) 514 524 { ··· 515 537 * @cpumask: cpumask for which OPP table needs to be added. 516 538 * 517 539 * This adds the OPP tables for CPUs present in the @cpumask. 518 - * 519 - * Locking: The internal opp_table and opp structures are RCU protected. 520 - * Hence this function internally uses RCU updater strategy with mutex locks 521 - * to keep the integrity of the internal data structures. Callers should ensure 522 - * that this function is *NOT* called under RCU protection or in contexts where 523 - * mutex cannot be locked. 524 540 */ 525 541 int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask) 526 542 { ··· 562 590 * This updates the @cpumask with CPUs that are sharing OPPs with @cpu_dev. 563 591 * 564 592 * Returns -ENOENT if operating-points-v2 isn't present for @cpu_dev. 565 - * 566 - * Locking: The internal opp_table and opp structures are RCU protected. 567 - * Hence this function internally uses RCU updater strategy with mutex locks 568 - * to keep the integrity of the internal data structures. Callers should ensure 569 - * that this function is *NOT* called under RCU protection or in contexts where 570 - * mutex cannot be locked. 571 593 */ 572 594 int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev, 573 595 struct cpumask *cpumask) ··· 571 605 int cpu, ret = 0; 572 606 573 607 /* Get OPP descriptor node */ 574 - np = _of_get_opp_desc_node(cpu_dev); 608 + np = dev_pm_opp_of_get_opp_desc_node(cpu_dev); 575 609 if (!np) { 576 610 dev_dbg(cpu_dev, "%s: Couldn't find opp node.\n", __func__); 577 611 return -ENOENT; ··· 596 630 } 597 631 598 632 /* Get OPP descriptor node */ 599 - tmp_np = _of_get_opp_desc_node(tcpu_dev); 633 + tmp_np = dev_pm_opp_of_get_opp_desc_node(tcpu_dev); 600 634 if (!tmp_np) { 601 635 dev_err(tcpu_dev, "%s: Couldn't find opp node.\n", 602 636 __func__);
+17 -23
drivers/base/power/opp/opp.h
··· 16 16 17 17 #include <linux/device.h> 18 18 #include <linux/kernel.h> 19 + #include <linux/kref.h> 19 20 #include <linux/list.h> 20 21 #include <linux/limits.h> 21 22 #include <linux/pm_opp.h> 22 - #include <linux/rculist.h> 23 - #include <linux/rcupdate.h> 23 + #include <linux/notifier.h> 24 24 25 25 struct clk; 26 26 struct regulator; ··· 51 51 * @node: opp table node. The nodes are maintained throughout the lifetime 52 52 * of boot. It is expected only an optimal set of OPPs are 53 53 * added to the library by the SoC framework. 54 - * RCU usage: opp table is traversed with RCU locks. node 55 - * modification is possible realtime, hence the modifications 56 - * are protected by the opp_table_lock for integrity. 57 54 * IMPORTANT: the opp nodes should be maintained in increasing 58 55 * order. 56 + * @kref: for reference count of the OPP. 59 57 * @available: true/false - marks if this OPP as available or not 60 58 * @dynamic: not-created from static DT entries. 61 59 * @turbo: true if turbo (boost) OPP ··· 63 65 * @clock_latency_ns: Latency (in nanoseconds) of switching to this OPP's 64 66 * frequency from any other OPP's frequency. 65 67 * @opp_table: points back to the opp_table struct this opp belongs to 66 - * @rcu_head: RCU callback head used for deferred freeing 67 68 * @np: OPP's device node. 68 69 * @dentry: debugfs dentry pointer (per opp) 69 70 * ··· 70 73 */ 71 74 struct dev_pm_opp { 72 75 struct list_head node; 76 + struct kref kref; 73 77 74 78 bool available; 75 79 bool dynamic; ··· 83 85 unsigned long clock_latency_ns; 84 86 85 87 struct opp_table *opp_table; 86 - struct rcu_head rcu_head; 87 88 88 89 struct device_node *np; 89 90 ··· 95 98 * struct opp_device - devices managed by 'struct opp_table' 96 99 * @node: list node 97 100 * @dev: device to which the struct object belongs 98 - * @rcu_head: RCU callback head used for deferred freeing 99 101 * @dentry: debugfs dentry pointer (per device) 100 102 * 101 103 * This is an internal data structure maintaining the devices that are managed ··· 103 107 struct opp_device { 104 108 struct list_head node; 105 109 const struct device *dev; 106 - struct rcu_head rcu_head; 107 110 108 111 #ifdef CONFIG_DEBUG_FS 109 112 struct dentry *dentry; ··· 120 125 * @node: table node - contains the devices with OPPs that 121 126 * have been registered. Nodes once added are not modified in this 122 127 * table. 123 - * RCU usage: nodes are not modified in the table of opp_table, 124 - * however addition is possible and is secured by opp_table_lock 125 - * @srcu_head: notifier head to notify the OPP availability changes. 126 - * @rcu_head: RCU callback head used for deferred freeing 128 + * @head: notifier head to notify the OPP availability changes. 127 129 * @dev_list: list of devices that share these OPPs 128 130 * @opp_list: table of opps 131 + * @kref: for reference count of the table. 132 + * @lock: mutex protecting the opp_list. 129 133 * @np: struct device_node pointer for opp's DT node. 130 134 * @clock_latency_ns_max: Max clock latency in nanoseconds. 131 135 * @shared_opp: OPP is shared between multiple devices. ··· 145 151 * This is an internal data structure maintaining the link to opps attached to 146 152 * a device. This structure is not meant to be shared to users as it is 147 153 * meant for book keeping and private to OPP library. 148 - * 149 - * Because the opp structures can be used from both rcu and srcu readers, we 150 - * need to wait for the grace period of both of them before freeing any 151 - * resources. And so we have used kfree_rcu() from within call_srcu() handlers. 152 154 */ 153 155 struct opp_table { 154 156 struct list_head node; 155 157 156 - struct srcu_notifier_head srcu_head; 157 - struct rcu_head rcu_head; 158 + struct blocking_notifier_head head; 158 159 struct list_head dev_list; 159 160 struct list_head opp_list; 161 + struct kref kref; 162 + struct mutex lock; 160 163 161 164 struct device_node *np; 162 165 unsigned long clock_latency_ns_max; ··· 181 190 }; 182 191 183 192 /* Routines internal to opp core */ 193 + void _get_opp_table_kref(struct opp_table *opp_table); 184 194 struct opp_table *_find_opp_table(struct device *dev); 185 195 struct opp_device *_add_opp_dev(const struct device *dev, struct opp_table *opp_table); 186 - void _dev_pm_opp_remove_table(struct device *dev, bool remove_all); 187 - struct dev_pm_opp *_allocate_opp(struct device *dev, struct opp_table **opp_table); 196 + void _dev_pm_opp_remove_table(struct opp_table *opp_table, struct device *dev, bool remove_all); 197 + void _dev_pm_opp_find_and_remove_table(struct device *dev, bool remove_all); 198 + struct dev_pm_opp *_opp_allocate(struct opp_table *opp_table); 199 + void _opp_free(struct dev_pm_opp *opp); 188 200 int _opp_add(struct device *dev, struct dev_pm_opp *new_opp, struct opp_table *opp_table); 189 - void _opp_remove(struct opp_table *opp_table, struct dev_pm_opp *opp, bool notify); 190 - int _opp_add_v1(struct device *dev, unsigned long freq, long u_volt, bool dynamic); 201 + int _opp_add_v1(struct opp_table *opp_table, struct device *dev, unsigned long freq, long u_volt, bool dynamic); 191 202 void _dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask, bool of); 203 + struct opp_table *_add_opp_table(struct device *dev); 192 204 193 205 #ifdef CONFIG_OF 194 206 void _of_init_opp_table(struct opp_table *opp_table, struct device *dev);
+6 -11
drivers/clk/tegra/clk-dfll.c
··· 633 633 struct dev_pm_opp *opp; 634 634 int i, uv; 635 635 636 - rcu_read_lock(); 637 - 638 636 opp = dev_pm_opp_find_freq_ceil(td->soc->dev, &rate); 639 - if (IS_ERR(opp)) { 640 - rcu_read_unlock(); 637 + if (IS_ERR(opp)) 641 638 return PTR_ERR(opp); 642 - } 643 - uv = dev_pm_opp_get_voltage(opp); 644 639 645 - rcu_read_unlock(); 640 + uv = dev_pm_opp_get_voltage(opp); 641 + dev_pm_opp_put(opp); 646 642 647 643 for (i = 0; i < td->i2c_lut_size; i++) { 648 644 if (regulator_list_voltage(td->vdd_reg, td->i2c_lut[i]) == uv) ··· 1436 1440 struct dev_pm_opp *opp; 1437 1441 int lut; 1438 1442 1439 - rcu_read_lock(); 1440 - 1441 1443 rate = ULONG_MAX; 1442 1444 opp = dev_pm_opp_find_freq_floor(td->soc->dev, &rate); 1443 1445 if (IS_ERR(opp)) { ··· 1443 1449 goto out; 1444 1450 } 1445 1451 v_max = dev_pm_opp_get_voltage(opp); 1452 + dev_pm_opp_put(opp); 1446 1453 1447 1454 v = td->soc->cvb->min_millivolts * 1000; 1448 1455 lut = find_vdd_map_entry_exact(td, v); ··· 1459 1464 1460 1465 if (v_opp <= td->soc->cvb->min_millivolts * 1000) 1461 1466 td->dvco_rate_min = dev_pm_opp_get_freq(opp); 1467 + 1468 + dev_pm_opp_put(opp); 1462 1469 1463 1470 for (;;) { 1464 1471 v += max(1, (v_max - v) / (MAX_DFLL_VOLTAGES - j)); ··· 1493 1496 ret = 0; 1494 1497 1495 1498 out: 1496 - rcu_read_unlock(); 1497 - 1498 1499 return ret; 1499 1500 } 1500 1501
+1 -6
drivers/cpufreq/cpufreq-dt.c
··· 148 148 struct private_data *priv; 149 149 struct device *cpu_dev; 150 150 struct clk *cpu_clk; 151 - struct dev_pm_opp *suspend_opp; 152 151 unsigned int transition_latency; 153 152 bool fallback = false; 154 153 const char *name; ··· 251 252 policy->driver_data = priv; 252 253 policy->clk = cpu_clk; 253 254 254 - rcu_read_lock(); 255 - suspend_opp = dev_pm_opp_get_suspend_opp(cpu_dev); 256 - if (suspend_opp) 257 - policy->suspend_freq = dev_pm_opp_get_freq(suspend_opp) / 1000; 258 - rcu_read_unlock(); 255 + policy->suspend_freq = dev_pm_opp_get_suspend_opp_freq(cpu_dev) / 1000; 259 256 260 257 ret = cpufreq_table_validate_and_show(policy, freq_table); 261 258 if (ret) {
+2 -3
drivers/cpufreq/exynos5440-cpufreq.c
··· 118 118 unsigned int tmp, clk_div, ema_div, freq, volt_id; 119 119 struct dev_pm_opp *opp; 120 120 121 - rcu_read_lock(); 122 121 cpufreq_for_each_entry(pos, freq_tbl) { 123 122 opp = dev_pm_opp_find_freq_exact(dvfs_info->dev, 124 123 pos->frequency * 1000, true); 125 124 if (IS_ERR(opp)) { 126 - rcu_read_unlock(); 127 125 dev_err(dvfs_info->dev, 128 126 "failed to find valid OPP for %u KHZ\n", 129 127 pos->frequency); ··· 138 140 139 141 /* Calculate EMA */ 140 142 volt_id = dev_pm_opp_get_voltage(opp); 143 + 141 144 volt_id = (MAX_VOLTAGE - volt_id) / VOLTAGE_STEP; 142 145 if (volt_id < PMIC_HIGH_VOLT) { 143 146 ema_div = (CPUEMA_HIGH << P0_7_CPUEMA_SHIFT) | ··· 156 157 157 158 __raw_writel(tmp, dvfs_info->base + XMU_PMU_P0_7 + 4 * 158 159 (pos - freq_tbl)); 160 + dev_pm_opp_put(opp); 159 161 } 160 162 161 - rcu_read_unlock(); 162 163 return 0; 163 164 } 164 165
+5 -5
drivers/cpufreq/imx6q-cpufreq.c
··· 53 53 freq_hz = new_freq * 1000; 54 54 old_freq = clk_get_rate(arm_clk) / 1000; 55 55 56 - rcu_read_lock(); 57 56 opp = dev_pm_opp_find_freq_ceil(cpu_dev, &freq_hz); 58 57 if (IS_ERR(opp)) { 59 - rcu_read_unlock(); 60 58 dev_err(cpu_dev, "failed to find OPP for %ld\n", freq_hz); 61 59 return PTR_ERR(opp); 62 60 } 63 61 64 62 volt = dev_pm_opp_get_voltage(opp); 65 - rcu_read_unlock(); 63 + dev_pm_opp_put(opp); 64 + 66 65 volt_old = regulator_get_voltage(arm_reg); 67 66 68 67 dev_dbg(cpu_dev, "%u MHz, %ld mV --> %u MHz, %ld mV\n", ··· 320 321 * freq_table initialised from OPP is therefore sorted in the 321 322 * same order. 322 323 */ 323 - rcu_read_lock(); 324 324 opp = dev_pm_opp_find_freq_exact(cpu_dev, 325 325 freq_table[0].frequency * 1000, true); 326 326 min_volt = dev_pm_opp_get_voltage(opp); 327 + dev_pm_opp_put(opp); 327 328 opp = dev_pm_opp_find_freq_exact(cpu_dev, 328 329 freq_table[--num].frequency * 1000, true); 329 330 max_volt = dev_pm_opp_get_voltage(opp); 330 - rcu_read_unlock(); 331 + dev_pm_opp_put(opp); 332 + 331 333 ret = regulator_set_voltage_time(arm_reg, min_volt, max_volt); 332 334 if (ret > 0) 333 335 transition_latency += ret * 1000;
+2 -6
drivers/cpufreq/mt8173-cpufreq.c
··· 232 232 233 233 freq_hz = freq_table[index].frequency * 1000; 234 234 235 - rcu_read_lock(); 236 235 opp = dev_pm_opp_find_freq_ceil(cpu_dev, &freq_hz); 237 236 if (IS_ERR(opp)) { 238 - rcu_read_unlock(); 239 237 pr_err("cpu%d: failed to find OPP for %ld\n", 240 238 policy->cpu, freq_hz); 241 239 return PTR_ERR(opp); 242 240 } 243 241 vproc = dev_pm_opp_get_voltage(opp); 244 - rcu_read_unlock(); 242 + dev_pm_opp_put(opp); 245 243 246 244 /* 247 245 * If the new voltage or the intermediate voltage is higher than the ··· 409 411 410 412 /* Search a safe voltage for intermediate frequency. */ 411 413 rate = clk_get_rate(inter_clk); 412 - rcu_read_lock(); 413 414 opp = dev_pm_opp_find_freq_ceil(cpu_dev, &rate); 414 415 if (IS_ERR(opp)) { 415 - rcu_read_unlock(); 416 416 pr_err("failed to get intermediate opp for cpu%d\n", cpu); 417 417 ret = PTR_ERR(opp); 418 418 goto out_free_opp_table; 419 419 } 420 420 info->intermediate_voltage = dev_pm_opp_get_voltage(opp); 421 - rcu_read_unlock(); 421 + dev_pm_opp_put(opp); 422 422 423 423 info->cpu_dev = cpu_dev; 424 424 info->proc_reg = proc_reg;
+1 -3
drivers/cpufreq/omap-cpufreq.c
··· 63 63 freq = ret; 64 64 65 65 if (mpu_reg) { 66 - rcu_read_lock(); 67 66 opp = dev_pm_opp_find_freq_ceil(mpu_dev, &freq); 68 67 if (IS_ERR(opp)) { 69 - rcu_read_unlock(); 70 68 dev_err(mpu_dev, "%s: unable to find MPU OPP for %d\n", 71 69 __func__, new_freq); 72 70 return -EINVAL; 73 71 } 74 72 volt = dev_pm_opp_get_voltage(opp); 75 - rcu_read_unlock(); 73 + dev_pm_opp_put(opp); 76 74 tol = volt * OPP_TOLERANCE / 100; 77 75 volt_old = regulator_get_voltage(mpu_reg); 78 76 }
+7 -6
drivers/cpufreq/sti-cpufreq.c
··· 160 160 int pcode, substrate, major, minor; 161 161 int ret; 162 162 char name[MAX_PCODE_NAME_LEN]; 163 + struct opp_table *opp_table; 163 164 164 165 reg_fields = sti_cpufreq_match(); 165 166 if (!reg_fields) { ··· 212 211 213 212 snprintf(name, MAX_PCODE_NAME_LEN, "pcode%d", pcode); 214 213 215 - ret = dev_pm_opp_set_prop_name(dev, name); 216 - if (ret) { 214 + opp_table = dev_pm_opp_set_prop_name(dev, name); 215 + if (IS_ERR(opp_table)) { 217 216 dev_err(dev, "Failed to set prop name\n"); 218 - return ret; 217 + return PTR_ERR(opp_table); 219 218 } 220 219 221 220 version[0] = BIT(major); 222 221 version[1] = BIT(minor); 223 222 version[2] = BIT(substrate); 224 223 225 - ret = dev_pm_opp_set_supported_hw(dev, version, VERSION_ELEMENTS); 226 - if (ret) { 224 + opp_table = dev_pm_opp_set_supported_hw(dev, version, VERSION_ELEMENTS); 225 + if (IS_ERR(opp_table)) { 227 226 dev_err(dev, "Failed to set supported hardware\n"); 228 - return ret; 227 + return PTR_ERR(opp_table); 229 228 } 230 229 231 230 dev_dbg(dev, "pcode: %d major: %d minor: %d substrate: %d\n",
+6 -34
drivers/devfreq/devfreq.c
··· 111 111 return; 112 112 } 113 113 114 - rcu_read_lock(); 115 114 for (i = 0, freq = 0; i < profile->max_state; i++, freq++) { 116 115 opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &freq); 117 116 if (IS_ERR(opp)) { 118 117 devm_kfree(devfreq->dev.parent, profile->freq_table); 119 118 profile->max_state = 0; 120 - rcu_read_unlock(); 121 119 return; 122 120 } 121 + dev_pm_opp_put(opp); 123 122 profile->freq_table[i] = freq; 124 123 } 125 - rcu_read_unlock(); 126 124 } 127 125 128 126 /** ··· 1110 1112 ssize_t count = 0; 1111 1113 unsigned long freq = 0; 1112 1114 1113 - rcu_read_lock(); 1114 1115 do { 1115 1116 opp = dev_pm_opp_find_freq_ceil(dev, &freq); 1116 1117 if (IS_ERR(opp)) 1117 1118 break; 1118 1119 1120 + dev_pm_opp_put(opp); 1119 1121 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2), 1120 1122 "%lu ", freq); 1121 1123 freq++; 1122 1124 } while (1); 1123 - rcu_read_unlock(); 1124 1125 1125 1126 /* Truncate the trailing space */ 1126 1127 if (count) ··· 1221 1224 * @freq: The frequency given to target function 1222 1225 * @flags: Flags handed from devfreq framework. 1223 1226 * 1224 - * Locking: This function must be called under rcu_read_lock(). opp is a rcu 1225 - * protected pointer. The reason for the same is that the opp pointer which is 1226 - * returned will remain valid for use with opp_get_{voltage, freq} only while 1227 - * under the locked area. The pointer returned must be used prior to unlocking 1228 - * with rcu_read_unlock() to maintain the integrity of the pointer. 1227 + * The callers are required to call dev_pm_opp_put() for the returned OPP after 1228 + * use. 1229 1229 */ 1230 1230 struct dev_pm_opp *devfreq_recommended_opp(struct device *dev, 1231 1231 unsigned long *freq, ··· 1259 1265 */ 1260 1266 int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq) 1261 1267 { 1262 - struct srcu_notifier_head *nh; 1263 - int ret = 0; 1264 - 1265 - rcu_read_lock(); 1266 - nh = dev_pm_opp_get_notifier(dev); 1267 - if (IS_ERR(nh)) 1268 - ret = PTR_ERR(nh); 1269 - rcu_read_unlock(); 1270 - if (!ret) 1271 - ret = srcu_notifier_chain_register(nh, &devfreq->nb); 1272 - 1273 - return ret; 1268 + return dev_pm_opp_register_notifier(dev, &devfreq->nb); 1274 1269 } 1275 1270 EXPORT_SYMBOL(devfreq_register_opp_notifier); 1276 1271 ··· 1275 1292 */ 1276 1293 int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq) 1277 1294 { 1278 - struct srcu_notifier_head *nh; 1279 - int ret = 0; 1280 - 1281 - rcu_read_lock(); 1282 - nh = dev_pm_opp_get_notifier(dev); 1283 - if (IS_ERR(nh)) 1284 - ret = PTR_ERR(nh); 1285 - rcu_read_unlock(); 1286 - if (!ret) 1287 - ret = srcu_notifier_chain_unregister(nh, &devfreq->nb); 1288 - 1289 - return ret; 1295 + return dev_pm_opp_unregister_notifier(dev, &devfreq->nb); 1290 1296 } 1291 1297 EXPORT_SYMBOL(devfreq_unregister_opp_notifier); 1292 1298
+5 -9
drivers/devfreq/exynos-bus.c
··· 103 103 int ret = 0; 104 104 105 105 /* Get new opp-bus instance according to new bus clock */ 106 - rcu_read_lock(); 107 106 new_opp = devfreq_recommended_opp(dev, freq, flags); 108 107 if (IS_ERR(new_opp)) { 109 108 dev_err(dev, "failed to get recommended opp instance\n"); 110 - rcu_read_unlock(); 111 109 return PTR_ERR(new_opp); 112 110 } 113 111 114 112 new_freq = dev_pm_opp_get_freq(new_opp); 115 113 new_volt = dev_pm_opp_get_voltage(new_opp); 114 + dev_pm_opp_put(new_opp); 115 + 116 116 old_freq = bus->curr_freq; 117 - rcu_read_unlock(); 118 117 119 118 if (old_freq == new_freq) 120 119 return 0; ··· 213 214 int ret = 0; 214 215 215 216 /* Get new opp-bus instance according to new bus clock */ 216 - rcu_read_lock(); 217 217 new_opp = devfreq_recommended_opp(dev, freq, flags); 218 218 if (IS_ERR(new_opp)) { 219 219 dev_err(dev, "failed to get recommended opp instance\n"); 220 - rcu_read_unlock(); 221 220 return PTR_ERR(new_opp); 222 221 } 223 222 224 223 new_freq = dev_pm_opp_get_freq(new_opp); 224 + dev_pm_opp_put(new_opp); 225 + 225 226 old_freq = bus->curr_freq; 226 - rcu_read_unlock(); 227 227 228 228 if (old_freq == new_freq) 229 229 return 0; ··· 356 358 357 359 rate = clk_get_rate(bus->clk); 358 360 359 - rcu_read_lock(); 360 361 opp = devfreq_recommended_opp(dev, &rate, 0); 361 362 if (IS_ERR(opp)) { 362 363 dev_err(dev, "failed to find dev_pm_opp\n"); 363 - rcu_read_unlock(); 364 364 ret = PTR_ERR(opp); 365 365 goto err_opp; 366 366 } 367 367 bus->curr_freq = dev_pm_opp_get_freq(opp); 368 - rcu_read_unlock(); 368 + dev_pm_opp_put(opp); 369 369 370 370 return 0; 371 371
+2 -2
drivers/devfreq/governor_passive.c
··· 59 59 * list of parent device. Because in this case, *freq is temporary 60 60 * value which is decided by ondemand governor. 61 61 */ 62 - rcu_read_lock(); 63 62 opp = devfreq_recommended_opp(parent_devfreq->dev.parent, freq, 0); 64 - rcu_read_unlock(); 65 63 if (IS_ERR(opp)) { 66 64 ret = PTR_ERR(opp); 67 65 goto out; 68 66 } 67 + 68 + dev_pm_opp_put(opp); 69 69 70 70 /* 71 71 * Get the OPP table's index of decided freqeuncy by governor
+5 -11
drivers/devfreq/rk3399_dmc.c
··· 91 91 unsigned long target_volt, target_rate; 92 92 int err; 93 93 94 - rcu_read_lock(); 95 94 opp = devfreq_recommended_opp(dev, freq, flags); 96 - if (IS_ERR(opp)) { 97 - rcu_read_unlock(); 95 + if (IS_ERR(opp)) 98 96 return PTR_ERR(opp); 99 - } 100 97 101 98 target_rate = dev_pm_opp_get_freq(opp); 102 99 target_volt = dev_pm_opp_get_voltage(opp); 103 - 104 - rcu_read_unlock(); 100 + dev_pm_opp_put(opp); 105 101 106 102 if (dmcfreq->rate == target_rate) 107 103 return 0; ··· 418 422 419 423 data->rate = clk_get_rate(data->dmc_clk); 420 424 421 - rcu_read_lock(); 422 425 opp = devfreq_recommended_opp(dev, &data->rate, 0); 423 - if (IS_ERR(opp)) { 424 - rcu_read_unlock(); 426 + if (IS_ERR(opp)) 425 427 return PTR_ERR(opp); 426 - } 428 + 427 429 data->rate = dev_pm_opp_get_freq(opp); 428 430 data->volt = dev_pm_opp_get_voltage(opp); 429 - rcu_read_unlock(); 431 + dev_pm_opp_put(opp); 430 432 431 433 rk3399_devfreq_dmc_profile.initial_freq = data->rate; 432 434
+1 -3
drivers/devfreq/tegra-devfreq.c
··· 487 487 struct dev_pm_opp *opp; 488 488 unsigned long rate = *freq * KHZ; 489 489 490 - rcu_read_lock(); 491 490 opp = devfreq_recommended_opp(dev, &rate, flags); 492 491 if (IS_ERR(opp)) { 493 - rcu_read_unlock(); 494 492 dev_err(dev, "Failed to find opp for %lu KHz\n", *freq); 495 493 return PTR_ERR(opp); 496 494 } 497 495 rate = dev_pm_opp_get_freq(opp); 498 - rcu_read_unlock(); 496 + dev_pm_opp_put(opp); 499 497 500 498 clk_set_min_rate(tegra->emc_clock, rate); 501 499 clk_set_rate(tegra->emc_clock, 0);
+2 -9
drivers/thermal/cpu_cooling.c
··· 297 297 if (!power_table) 298 298 return -ENOMEM; 299 299 300 - rcu_read_lock(); 301 - 302 300 for (freq = 0, i = 0; 303 301 opp = dev_pm_opp_find_freq_ceil(dev, &freq), !IS_ERR(opp); 304 302 freq++, i++) { ··· 304 306 u64 power; 305 307 306 308 if (i >= num_opps) { 307 - rcu_read_unlock(); 308 309 ret = -EAGAIN; 309 310 goto free_power_table; 310 311 } 311 312 312 313 freq_mhz = freq / 1000000; 313 314 voltage_mv = dev_pm_opp_get_voltage(opp) / 1000; 315 + dev_pm_opp_put(opp); 314 316 315 317 /* 316 318 * Do the multiplication with MHz and millivolt so as ··· 325 327 /* power is stored in mW */ 326 328 power_table[i].power = power; 327 329 } 328 - 329 - rcu_read_unlock(); 330 330 331 331 if (i != num_opps) { 332 332 ret = PTR_ERR(opp); ··· 429 433 return 0; 430 434 } 431 435 432 - rcu_read_lock(); 433 - 434 436 opp = dev_pm_opp_find_freq_exact(cpufreq_device->cpu_dev, freq_hz, 435 437 true); 436 438 voltage = dev_pm_opp_get_voltage(opp); 437 - 438 - rcu_read_unlock(); 439 + dev_pm_opp_put(opp); 439 440 440 441 if (voltage == 0) { 441 442 dev_warn_ratelimited(cpufreq_device->cpu_dev,
+4 -11
drivers/thermal/devfreq_cooling.c
··· 113 113 unsigned int freq = dfc->freq_table[i]; 114 114 bool want_enable = i >= cdev_state ? true : false; 115 115 116 - rcu_read_lock(); 117 116 opp = dev_pm_opp_find_freq_exact(dev, freq, !want_enable); 118 - rcu_read_unlock(); 119 117 120 118 if (PTR_ERR(opp) == -ERANGE) 121 119 continue; 122 120 else if (IS_ERR(opp)) 123 121 return PTR_ERR(opp); 122 + 123 + dev_pm_opp_put(opp); 124 124 125 125 if (want_enable) 126 126 ret = dev_pm_opp_enable(dev, freq); ··· 221 221 if (!dfc->power_ops->get_static_power) 222 222 return 0; 223 223 224 - rcu_read_lock(); 225 - 226 224 opp = dev_pm_opp_find_freq_exact(dev, freq, true); 227 225 if (IS_ERR(opp) && (PTR_ERR(opp) == -ERANGE)) 228 226 opp = dev_pm_opp_find_freq_exact(dev, freq, false); 229 227 230 228 voltage = dev_pm_opp_get_voltage(opp) / 1000; /* mV */ 231 - 232 - rcu_read_unlock(); 229 + dev_pm_opp_put(opp); 233 230 234 231 if (voltage == 0) { 235 232 dev_warn_ratelimited(dev, ··· 409 412 unsigned long power_dyn, voltage; 410 413 struct dev_pm_opp *opp; 411 414 412 - rcu_read_lock(); 413 - 414 415 opp = dev_pm_opp_find_freq_floor(dev, &freq); 415 416 if (IS_ERR(opp)) { 416 - rcu_read_unlock(); 417 417 ret = PTR_ERR(opp); 418 418 goto free_tables; 419 419 } 420 420 421 421 voltage = dev_pm_opp_get_voltage(opp) / 1000; /* mV */ 422 - 423 - rcu_read_unlock(); 422 + dev_pm_opp_put(opp); 424 423 425 424 if (dfc->power_ops) { 426 425 power_dyn = get_dynamic_power(dfc, freq, voltage);
+51 -27
include/linux/pm_opp.h
··· 78 78 79 79 #if defined(CONFIG_PM_OPP) 80 80 81 + struct opp_table *dev_pm_opp_get_opp_table(struct device *dev); 82 + void dev_pm_opp_put_opp_table(struct opp_table *opp_table); 83 + 81 84 unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp); 82 85 83 86 unsigned long dev_pm_opp_get_freq(struct dev_pm_opp *opp); ··· 91 88 unsigned long dev_pm_opp_get_max_clock_latency(struct device *dev); 92 89 unsigned long dev_pm_opp_get_max_volt_latency(struct device *dev); 93 90 unsigned long dev_pm_opp_get_max_transition_latency(struct device *dev); 94 - struct dev_pm_opp *dev_pm_opp_get_suspend_opp(struct device *dev); 91 + unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev); 95 92 96 93 struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev, 97 94 unsigned long freq, ··· 102 99 103 100 struct dev_pm_opp *dev_pm_opp_find_freq_ceil(struct device *dev, 104 101 unsigned long *freq); 102 + void dev_pm_opp_put(struct dev_pm_opp *opp); 105 103 106 104 int dev_pm_opp_add(struct device *dev, unsigned long freq, 107 105 unsigned long u_volt); ··· 112 108 113 109 int dev_pm_opp_disable(struct device *dev, unsigned long freq); 114 110 115 - struct srcu_notifier_head *dev_pm_opp_get_notifier(struct device *dev); 116 - int dev_pm_opp_set_supported_hw(struct device *dev, const u32 *versions, 117 - unsigned int count); 118 - void dev_pm_opp_put_supported_hw(struct device *dev); 119 - int dev_pm_opp_set_prop_name(struct device *dev, const char *name); 120 - void dev_pm_opp_put_prop_name(struct device *dev); 111 + int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb); 112 + int dev_pm_opp_unregister_notifier(struct device *dev, struct notifier_block *nb); 113 + 114 + struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev, const u32 *versions, unsigned int count); 115 + void dev_pm_opp_put_supported_hw(struct opp_table *opp_table); 116 + struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name); 117 + void dev_pm_opp_put_prop_name(struct opp_table *opp_table); 121 118 struct opp_table *dev_pm_opp_set_regulators(struct device *dev, const char * const names[], unsigned int count); 122 119 void dev_pm_opp_put_regulators(struct opp_table *opp_table); 123 - int dev_pm_opp_register_set_opp_helper(struct device *dev, int (*set_opp)(struct dev_pm_set_opp_data *data)); 124 - void dev_pm_opp_register_put_opp_helper(struct device *dev); 120 + struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev, int (*set_opp)(struct dev_pm_set_opp_data *data)); 121 + void dev_pm_opp_register_put_opp_helper(struct opp_table *opp_table); 125 122 int dev_pm_opp_set_rate(struct device *dev, unsigned long target_freq); 126 123 int dev_pm_opp_set_sharing_cpus(struct device *cpu_dev, const struct cpumask *cpumask); 127 124 int dev_pm_opp_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask); 128 125 void dev_pm_opp_remove_table(struct device *dev); 129 126 void dev_pm_opp_cpumask_remove_table(const struct cpumask *cpumask); 130 127 #else 128 + static inline struct opp_table *dev_pm_opp_get_opp_table(struct device *dev) 129 + { 130 + return ERR_PTR(-ENOTSUPP); 131 + } 132 + 133 + static inline void dev_pm_opp_put_opp_table(struct opp_table *opp_table) {} 134 + 131 135 static inline unsigned long dev_pm_opp_get_voltage(struct dev_pm_opp *opp) 132 136 { 133 137 return 0; ··· 171 159 return 0; 172 160 } 173 161 174 - static inline struct dev_pm_opp *dev_pm_opp_get_suspend_opp(struct device *dev) 162 + static inline unsigned long dev_pm_opp_get_suspend_opp_freq(struct device *dev) 175 163 { 176 - return NULL; 164 + return 0; 177 165 } 178 166 179 167 static inline struct dev_pm_opp *dev_pm_opp_find_freq_exact(struct device *dev, ··· 193 181 { 194 182 return ERR_PTR(-ENOTSUPP); 195 183 } 184 + 185 + static inline void dev_pm_opp_put(struct dev_pm_opp *opp) {} 196 186 197 187 static inline int dev_pm_opp_add(struct device *dev, unsigned long freq, 198 188 unsigned long u_volt) ··· 216 202 return 0; 217 203 } 218 204 219 - static inline struct srcu_notifier_head *dev_pm_opp_get_notifier( 220 - struct device *dev) 205 + static inline int dev_pm_opp_register_notifier(struct device *dev, struct notifier_block *nb) 206 + { 207 + return -ENOTSUPP; 208 + } 209 + 210 + static inline int dev_pm_opp_unregister_notifier(struct device *dev, struct notifier_block *nb) 211 + { 212 + return -ENOTSUPP; 213 + } 214 + 215 + static inline struct opp_table *dev_pm_opp_set_supported_hw(struct device *dev, 216 + const u32 *versions, 217 + unsigned int count) 221 218 { 222 219 return ERR_PTR(-ENOTSUPP); 223 220 } 224 221 225 - static inline int dev_pm_opp_set_supported_hw(struct device *dev, 226 - const u32 *versions, 227 - unsigned int count) 228 - { 229 - return -ENOTSUPP; 230 - } 222 + static inline void dev_pm_opp_put_supported_hw(struct opp_table *opp_table) {} 231 223 232 - static inline void dev_pm_opp_put_supported_hw(struct device *dev) {} 233 - 234 - static inline int dev_pm_opp_register_set_opp_helper(struct device *dev, 224 + static inline struct opp_table *dev_pm_opp_register_set_opp_helper(struct device *dev, 235 225 int (*set_opp)(struct dev_pm_set_opp_data *data)) 236 226 { 237 - return -ENOTSUPP; 227 + return ERR_PTR(-ENOTSUPP); 238 228 } 239 229 240 - static inline void dev_pm_opp_register_put_opp_helper(struct device *dev) {} 230 + static inline void dev_pm_opp_register_put_opp_helper(struct opp_table *opp_table) {} 241 231 242 - static inline int dev_pm_opp_set_prop_name(struct device *dev, const char *name) 232 + static inline struct opp_table *dev_pm_opp_set_prop_name(struct device *dev, const char *name) 243 233 { 244 - return -ENOTSUPP; 234 + return ERR_PTR(-ENOTSUPP); 245 235 } 246 236 247 - static inline void dev_pm_opp_put_prop_name(struct device *dev) {} 237 + static inline void dev_pm_opp_put_prop_name(struct opp_table *opp_table) {} 248 238 249 239 static inline struct opp_table *dev_pm_opp_set_regulators(struct device *dev, const char * const names[], unsigned int count) 250 240 { ··· 288 270 int dev_pm_opp_of_cpumask_add_table(const struct cpumask *cpumask); 289 271 void dev_pm_opp_of_cpumask_remove_table(const struct cpumask *cpumask); 290 272 int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask); 273 + struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev); 291 274 #else 292 275 static inline int dev_pm_opp_of_add_table(struct device *dev) 293 276 { ··· 311 292 static inline int dev_pm_opp_of_get_sharing_cpus(struct device *cpu_dev, struct cpumask *cpumask) 312 293 { 313 294 return -ENOTSUPP; 295 + } 296 + 297 + static inline struct device_node *dev_pm_opp_of_get_opp_desc_node(struct device *dev) 298 + { 299 + return NULL; 314 300 } 315 301 #endif 316 302