diff options
Diffstat (limited to 'drivers/devfreq/devfreq.c')
-rw-r--r-- | drivers/devfreq/devfreq.c | 921 |
1 files changed, 642 insertions, 279 deletions
diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c index b146d76f04c..53766f39aad 100644 --- a/drivers/devfreq/devfreq.c +++ b/drivers/devfreq/devfreq.c | |||
@@ -27,21 +27,17 @@ | |||
27 | #include <linux/hrtimer.h> | 27 | #include <linux/hrtimer.h> |
28 | #include "governor.h" | 28 | #include "governor.h" |
29 | 29 | ||
30 | struct class *devfreq_class; | 30 | static struct class *devfreq_class; |
31 | 31 | ||
32 | /* | 32 | /* |
33 | * devfreq_work periodically monitors every registered device. | 33 | * devfreq core provides delayed work based load monitoring helper |
34 | * The minimum polling interval is one jiffy. The polling interval is | 34 | * functions. Governors can use these or can implement their own |
35 | * determined by the minimum polling period among all polling devfreq | 35 | * monitoring mechanism. |
36 | * devices. The resolution of polling interval is one jiffy. | ||
37 | */ | 36 | */ |
38 | static bool polling; | ||
39 | static struct workqueue_struct *devfreq_wq; | 37 | static struct workqueue_struct *devfreq_wq; |
40 | static struct delayed_work devfreq_work; | ||
41 | |||
42 | /* wait removing if this is to be removed */ | ||
43 | static struct devfreq *wait_remove_device; | ||
44 | 38 | ||
39 | /* The list of all device-devfreq governors */ | ||
40 | static LIST_HEAD(devfreq_governor_list); | ||
45 | /* The list of all device-devfreq */ | 41 | /* The list of all device-devfreq */ |
46 | static LIST_HEAD(devfreq_list); | 42 | static LIST_HEAD(devfreq_list); |
47 | static DEFINE_MUTEX(devfreq_list_lock); | 43 | static DEFINE_MUTEX(devfreq_list_lock); |
@@ -73,6 +69,79 @@ static struct devfreq *find_device_devfreq(struct device *dev) | |||
73 | } | 69 | } |
74 | 70 | ||
75 | /** | 71 | /** |
72 | * devfreq_get_freq_level() - Lookup freq_table for the frequency | ||
73 | * @devfreq: the devfreq instance | ||
74 | * @freq: the target frequency | ||
75 | */ | ||
76 | static int devfreq_get_freq_level(struct devfreq *devfreq, unsigned long freq) | ||
77 | { | ||
78 | int lev; | ||
79 | |||
80 | for (lev = 0; lev < devfreq->profile->max_state; lev++) | ||
81 | if (freq == devfreq->profile->freq_table[lev]) | ||
82 | return lev; | ||
83 | |||
84 | return -EINVAL; | ||
85 | } | ||
86 | |||
87 | /** | ||
88 | * devfreq_update_status() - Update statistics of devfreq behavior | ||
89 | * @devfreq: the devfreq instance | ||
90 | * @freq: the update target frequency | ||
91 | */ | ||
92 | static int devfreq_update_status(struct devfreq *devfreq, unsigned long freq) | ||
93 | { | ||
94 | int lev, prev_lev; | ||
95 | unsigned long cur_time; | ||
96 | |||
97 | lev = devfreq_get_freq_level(devfreq, freq); | ||
98 | if (lev < 0) | ||
99 | return lev; | ||
100 | |||
101 | cur_time = jiffies; | ||
102 | devfreq->time_in_state[lev] += | ||
103 | cur_time - devfreq->last_stat_updated; | ||
104 | if (freq != devfreq->previous_freq) { | ||
105 | prev_lev = devfreq_get_freq_level(devfreq, | ||
106 | devfreq->previous_freq); | ||
107 | devfreq->trans_table[(prev_lev * | ||
108 | devfreq->profile->max_state) + lev]++; | ||
109 | devfreq->total_trans++; | ||
110 | } | ||
111 | devfreq->last_stat_updated = cur_time; | ||
112 | |||
113 | return 0; | ||
114 | } | ||
115 | |||
116 | /** | ||
117 | * find_devfreq_governor() - find devfreq governor from name | ||
118 | * @name: name of the governor | ||
119 | * | ||
120 | * Search the list of devfreq governors and return the matched | ||
121 | * governor's pointer. devfreq_list_lock should be held by the caller. | ||
122 | */ | ||
123 | static struct devfreq_governor *find_devfreq_governor(const char *name) | ||
124 | { | ||
125 | struct devfreq_governor *tmp_governor; | ||
126 | |||
127 | if (unlikely(IS_ERR_OR_NULL(name))) { | ||
128 | pr_err("DEVFREQ: %s: Invalid parameters\n", __func__); | ||
129 | return ERR_PTR(-EINVAL); | ||
130 | } | ||
131 | WARN(!mutex_is_locked(&devfreq_list_lock), | ||
132 | "devfreq_list_lock must be locked."); | ||
133 | |||
134 | list_for_each_entry(tmp_governor, &devfreq_governor_list, node) { | ||
135 | if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN)) | ||
136 | return tmp_governor; | ||
137 | } | ||
138 | |||
139 | return ERR_PTR(-ENODEV); | ||
140 | } | ||
141 | |||
142 | /* Load monitoring helper functions for governors use */ | ||
143 | |||
144 | /** | ||
76 | * update_devfreq() - Reevaluate the device and configure frequency. | 145 | * update_devfreq() - Reevaluate the device and configure frequency. |
77 | * @devfreq: the devfreq instance. | 146 | * @devfreq: the devfreq instance. |
78 | * | 147 | * |
@@ -90,6 +159,9 @@ int update_devfreq(struct devfreq *devfreq) | |||
90 | return -EINVAL; | 159 | return -EINVAL; |
91 | } | 160 | } |
92 | 161 | ||
162 | if (!devfreq->governor) | ||
163 | return -EINVAL; | ||
164 | |||
93 | /* Reevaluate the proper frequency */ | 165 | /* Reevaluate the proper frequency */ |
94 | err = devfreq->governor->get_target_freq(devfreq, &freq); | 166 | err = devfreq->governor->get_target_freq(devfreq, &freq); |
95 | if (err) | 167 | if (err) |
@@ -116,16 +188,173 @@ int update_devfreq(struct devfreq *devfreq) | |||
116 | if (err) | 188 | if (err) |
117 | return err; | 189 | return err; |
118 | 190 | ||
191 | if (devfreq->profile->freq_table) | ||
192 | if (devfreq_update_status(devfreq, freq)) | ||
193 | dev_err(&devfreq->dev, | ||
194 | "Couldn't update frequency transition information.\n"); | ||
195 | |||
119 | devfreq->previous_freq = freq; | 196 | devfreq->previous_freq = freq; |
120 | return err; | 197 | return err; |
121 | } | 198 | } |
199 | EXPORT_SYMBOL(update_devfreq); | ||
200 | |||
201 | /** | ||
202 | * devfreq_monitor() - Periodically poll devfreq objects. | ||
203 | * @work: the work struct used to run devfreq_monitor periodically. | ||
204 | * | ||
205 | */ | ||
206 | static void devfreq_monitor(struct work_struct *work) | ||
207 | { | ||
208 | int err; | ||
209 | struct devfreq *devfreq = container_of(work, | ||
210 | struct devfreq, work.work); | ||
211 | |||
212 | mutex_lock(&devfreq->lock); | ||
213 | err = update_devfreq(devfreq); | ||
214 | if (err) | ||
215 | dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err); | ||
216 | |||
217 | queue_delayed_work(devfreq_wq, &devfreq->work, | ||
218 | msecs_to_jiffies(devfreq->profile->polling_ms)); | ||
219 | mutex_unlock(&devfreq->lock); | ||
220 | } | ||
221 | |||
222 | /** | ||
223 | * devfreq_monitor_start() - Start load monitoring of devfreq instance | ||
224 | * @devfreq: the devfreq instance. | ||
225 | * | ||
226 | * Helper function for starting devfreq device load monitoing. By | ||
227 | * default delayed work based monitoring is supported. Function | ||
228 | * to be called from governor in response to DEVFREQ_GOV_START | ||
229 | * event when device is added to devfreq framework. | ||
230 | */ | ||
231 | void devfreq_monitor_start(struct devfreq *devfreq) | ||
232 | { | ||
233 | INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor); | ||
234 | if (devfreq->profile->polling_ms) | ||
235 | queue_delayed_work(devfreq_wq, &devfreq->work, | ||
236 | msecs_to_jiffies(devfreq->profile->polling_ms)); | ||
237 | } | ||
238 | EXPORT_SYMBOL(devfreq_monitor_start); | ||
239 | |||
240 | /** | ||
241 | * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance | ||
242 | * @devfreq: the devfreq instance. | ||
243 | * | ||
244 | * Helper function to stop devfreq device load monitoing. Function | ||
245 | * to be called from governor in response to DEVFREQ_GOV_STOP | ||
246 | * event when device is removed from devfreq framework. | ||
247 | */ | ||
248 | void devfreq_monitor_stop(struct devfreq *devfreq) | ||
249 | { | ||
250 | cancel_delayed_work_sync(&devfreq->work); | ||
251 | } | ||
252 | EXPORT_SYMBOL(devfreq_monitor_stop); | ||
253 | |||
254 | /** | ||
255 | * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance | ||
256 | * @devfreq: the devfreq instance. | ||
257 | * | ||
258 | * Helper function to suspend devfreq device load monitoing. Function | ||
259 | * to be called from governor in response to DEVFREQ_GOV_SUSPEND | ||
260 | * event or when polling interval is set to zero. | ||
261 | * | ||
262 | * Note: Though this function is same as devfreq_monitor_stop(), | ||
263 | * intentionally kept separate to provide hooks for collecting | ||
264 | * transition statistics. | ||
265 | */ | ||
266 | void devfreq_monitor_suspend(struct devfreq *devfreq) | ||
267 | { | ||
268 | mutex_lock(&devfreq->lock); | ||
269 | if (devfreq->stop_polling) { | ||
270 | mutex_unlock(&devfreq->lock); | ||
271 | return; | ||
272 | } | ||
273 | |||
274 | devfreq->stop_polling = true; | ||
275 | mutex_unlock(&devfreq->lock); | ||
276 | cancel_delayed_work_sync(&devfreq->work); | ||
277 | } | ||
278 | EXPORT_SYMBOL(devfreq_monitor_suspend); | ||
279 | |||
280 | /** | ||
281 | * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance | ||
282 | * @devfreq: the devfreq instance. | ||
283 | * | ||
284 | * Helper function to resume devfreq device load monitoing. Function | ||
285 | * to be called from governor in response to DEVFREQ_GOV_RESUME | ||
286 | * event or when polling interval is set to non-zero. | ||
287 | */ | ||
288 | void devfreq_monitor_resume(struct devfreq *devfreq) | ||
289 | { | ||
290 | mutex_lock(&devfreq->lock); | ||
291 | if (!devfreq->stop_polling) | ||
292 | goto out; | ||
293 | |||
294 | if (!delayed_work_pending(&devfreq->work) && | ||
295 | devfreq->profile->polling_ms) | ||
296 | queue_delayed_work(devfreq_wq, &devfreq->work, | ||
297 | msecs_to_jiffies(devfreq->profile->polling_ms)); | ||
298 | devfreq->stop_polling = false; | ||
299 | |||
300 | out: | ||
301 | mutex_unlock(&devfreq->lock); | ||
302 | } | ||
303 | EXPORT_SYMBOL(devfreq_monitor_resume); | ||
304 | |||
305 | /** | ||
306 | * devfreq_interval_update() - Update device devfreq monitoring interval | ||
307 | * @devfreq: the devfreq instance. | ||
308 | * @delay: new polling interval to be set. | ||
309 | * | ||
310 | * Helper function to set new load monitoring polling interval. Function | ||
311 | * to be called from governor in response to DEVFREQ_GOV_INTERVAL event. | ||
312 | */ | ||
313 | void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay) | ||
314 | { | ||
315 | unsigned int cur_delay = devfreq->profile->polling_ms; | ||
316 | unsigned int new_delay = *delay; | ||
317 | |||
318 | mutex_lock(&devfreq->lock); | ||
319 | devfreq->profile->polling_ms = new_delay; | ||
320 | |||
321 | if (devfreq->stop_polling) | ||
322 | goto out; | ||
323 | |||
324 | /* if new delay is zero, stop polling */ | ||
325 | if (!new_delay) { | ||
326 | mutex_unlock(&devfreq->lock); | ||
327 | cancel_delayed_work_sync(&devfreq->work); | ||
328 | return; | ||
329 | } | ||
330 | |||
331 | /* if current delay is zero, start polling with new delay */ | ||
332 | if (!cur_delay) { | ||
333 | queue_delayed_work(devfreq_wq, &devfreq->work, | ||
334 | msecs_to_jiffies(devfreq->profile->polling_ms)); | ||
335 | goto out; | ||
336 | } | ||
337 | |||
338 | /* if current delay is greater than new delay, restart polling */ | ||
339 | if (cur_delay > new_delay) { | ||
340 | mutex_unlock(&devfreq->lock); | ||
341 | cancel_delayed_work_sync(&devfreq->work); | ||
342 | mutex_lock(&devfreq->lock); | ||
343 | if (!devfreq->stop_polling) | ||
344 | queue_delayed_work(devfreq_wq, &devfreq->work, | ||
345 | msecs_to_jiffies(devfreq->profile->polling_ms)); | ||
346 | } | ||
347 | out: | ||
348 | mutex_unlock(&devfreq->lock); | ||
349 | } | ||
350 | EXPORT_SYMBOL(devfreq_interval_update); | ||
122 | 351 | ||
123 | /** | 352 | /** |
124 | * devfreq_notifier_call() - Notify that the device frequency requirements | 353 | * devfreq_notifier_call() - Notify that the device frequency requirements |
125 | * has been changed out of devfreq framework. | 354 | * has been changed out of devfreq framework. |
126 | * @nb the notifier_block (supposed to be devfreq->nb) | 355 | * @nb: the notifier_block (supposed to be devfreq->nb) |
127 | * @type not used | 356 | * @type: not used |
128 | * @devp not used | 357 | * @devp: not used |
129 | * | 358 | * |
130 | * Called by a notifier that uses devfreq->nb. | 359 | * Called by a notifier that uses devfreq->nb. |
131 | */ | 360 | */ |
@@ -143,59 +372,34 @@ static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type, | |||
143 | } | 372 | } |
144 | 373 | ||
145 | /** | 374 | /** |
146 | * _remove_devfreq() - Remove devfreq from the device. | 375 | * _remove_devfreq() - Remove devfreq from the list and release its resources. |
147 | * @devfreq: the devfreq struct | 376 | * @devfreq: the devfreq struct |
148 | * @skip: skip calling device_unregister(). | 377 | * @skip: skip calling device_unregister(). |
149 | * | ||
150 | * Note that the caller should lock devfreq->lock before calling | ||
151 | * this. _remove_devfreq() will unlock it and free devfreq | ||
152 | * internally. devfreq_list_lock should be locked by the caller | ||
153 | * as well (not relased at return) | ||
154 | * | ||
155 | * Lock usage: | ||
156 | * devfreq->lock: locked before call. | ||
157 | * unlocked at return (and freed) | ||
158 | * devfreq_list_lock: locked before call. | ||
159 | * kept locked at return. | ||
160 | * if devfreq is centrally polled. | ||
161 | * | ||
162 | * Freed memory: | ||
163 | * devfreq | ||
164 | */ | 378 | */ |
165 | static void _remove_devfreq(struct devfreq *devfreq, bool skip) | 379 | static void _remove_devfreq(struct devfreq *devfreq, bool skip) |
166 | { | 380 | { |
167 | if (!mutex_is_locked(&devfreq->lock)) { | 381 | mutex_lock(&devfreq_list_lock); |
168 | WARN(true, "devfreq->lock must be locked by the caller.\n"); | 382 | if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) { |
169 | return; | 383 | mutex_unlock(&devfreq_list_lock); |
170 | } | 384 | dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n"); |
171 | if (!devfreq->governor->no_central_polling && | ||
172 | !mutex_is_locked(&devfreq_list_lock)) { | ||
173 | WARN(true, "devfreq_list_lock must be locked by the caller.\n"); | ||
174 | return; | 385 | return; |
175 | } | 386 | } |
387 | list_del(&devfreq->node); | ||
388 | mutex_unlock(&devfreq_list_lock); | ||
176 | 389 | ||
177 | if (devfreq->being_removed) | 390 | if (devfreq->governor) |
178 | return; | 391 | devfreq->governor->event_handler(devfreq, |
179 | 392 | DEVFREQ_GOV_STOP, NULL); | |
180 | devfreq->being_removed = true; | ||
181 | 393 | ||
182 | if (devfreq->profile->exit) | 394 | if (devfreq->profile->exit) |
183 | devfreq->profile->exit(devfreq->dev.parent); | 395 | devfreq->profile->exit(devfreq->dev.parent); |
184 | 396 | ||
185 | if (devfreq->governor->exit) | ||
186 | devfreq->governor->exit(devfreq); | ||
187 | |||
188 | if (!skip && get_device(&devfreq->dev)) { | 397 | if (!skip && get_device(&devfreq->dev)) { |
189 | device_unregister(&devfreq->dev); | 398 | device_unregister(&devfreq->dev); |
190 | put_device(&devfreq->dev); | 399 | put_device(&devfreq->dev); |
191 | } | 400 | } |
192 | 401 | ||
193 | if (!devfreq->governor->no_central_polling) | ||
194 | list_del(&devfreq->node); | ||
195 | |||
196 | mutex_unlock(&devfreq->lock); | ||
197 | mutex_destroy(&devfreq->lock); | 402 | mutex_destroy(&devfreq->lock); |
198 | |||
199 | kfree(devfreq); | 403 | kfree(devfreq); |
200 | } | 404 | } |
201 | 405 | ||
@@ -210,163 +414,39 @@ static void _remove_devfreq(struct devfreq *devfreq, bool skip) | |||
210 | static void devfreq_dev_release(struct device *dev) | 414 | static void devfreq_dev_release(struct device *dev) |
211 | { | 415 | { |
212 | struct devfreq *devfreq = to_devfreq(dev); | 416 | struct devfreq *devfreq = to_devfreq(dev); |
213 | bool central_polling = !devfreq->governor->no_central_polling; | ||
214 | |||
215 | /* | ||
216 | * If devfreq_dev_release() was called by device_unregister() of | ||
217 | * _remove_devfreq(), we cannot mutex_lock(&devfreq->lock) and | ||
218 | * being_removed is already set. This also partially checks the case | ||
219 | * where devfreq_dev_release() is called from a thread other than | ||
220 | * the one called _remove_devfreq(); however, this case is | ||
221 | * dealt completely with another following being_removed check. | ||
222 | * | ||
223 | * Because being_removed is never being | ||
224 | * unset, we do not need to worry about race conditions on | ||
225 | * being_removed. | ||
226 | */ | ||
227 | if (devfreq->being_removed) | ||
228 | return; | ||
229 | |||
230 | if (central_polling) | ||
231 | mutex_lock(&devfreq_list_lock); | ||
232 | |||
233 | mutex_lock(&devfreq->lock); | ||
234 | 417 | ||
235 | /* | ||
236 | * Check being_removed flag again for the case where | ||
237 | * devfreq_dev_release() was called in a thread other than the one | ||
238 | * possibly called _remove_devfreq(). | ||
239 | */ | ||
240 | if (devfreq->being_removed) { | ||
241 | mutex_unlock(&devfreq->lock); | ||
242 | goto out; | ||
243 | } | ||
244 | |||
245 | /* devfreq->lock is unlocked and removed in _removed_devfreq() */ | ||
246 | _remove_devfreq(devfreq, true); | 418 | _remove_devfreq(devfreq, true); |
247 | |||
248 | out: | ||
249 | if (central_polling) | ||
250 | mutex_unlock(&devfreq_list_lock); | ||
251 | } | ||
252 | |||
253 | /** | ||
254 | * devfreq_monitor() - Periodically poll devfreq objects. | ||
255 | * @work: the work struct used to run devfreq_monitor periodically. | ||
256 | * | ||
257 | */ | ||
258 | static void devfreq_monitor(struct work_struct *work) | ||
259 | { | ||
260 | static unsigned long last_polled_at; | ||
261 | struct devfreq *devfreq, *tmp; | ||
262 | int error; | ||
263 | unsigned long jiffies_passed; | ||
264 | unsigned long next_jiffies = ULONG_MAX, now = jiffies; | ||
265 | struct device *dev; | ||
266 | |||
267 | /* Initially last_polled_at = 0, polling every device at bootup */ | ||
268 | jiffies_passed = now - last_polled_at; | ||
269 | last_polled_at = now; | ||
270 | if (jiffies_passed == 0) | ||
271 | jiffies_passed = 1; | ||
272 | |||
273 | mutex_lock(&devfreq_list_lock); | ||
274 | list_for_each_entry_safe(devfreq, tmp, &devfreq_list, node) { | ||
275 | mutex_lock(&devfreq->lock); | ||
276 | dev = devfreq->dev.parent; | ||
277 | |||
278 | /* Do not remove tmp for a while */ | ||
279 | wait_remove_device = tmp; | ||
280 | |||
281 | if (devfreq->governor->no_central_polling || | ||
282 | devfreq->next_polling == 0) { | ||
283 | mutex_unlock(&devfreq->lock); | ||
284 | continue; | ||
285 | } | ||
286 | mutex_unlock(&devfreq_list_lock); | ||
287 | |||
288 | /* | ||
289 | * Reduce more next_polling if devfreq_wq took an extra | ||
290 | * delay. (i.e., CPU has been idled.) | ||
291 | */ | ||
292 | if (devfreq->next_polling <= jiffies_passed) { | ||
293 | error = update_devfreq(devfreq); | ||
294 | |||
295 | /* Remove a devfreq with an error. */ | ||
296 | if (error && error != -EAGAIN) { | ||
297 | |||
298 | dev_err(dev, "Due to update_devfreq error(%d), devfreq(%s) is removed from the device\n", | ||
299 | error, devfreq->governor->name); | ||
300 | |||
301 | /* | ||
302 | * Unlock devfreq before locking the list | ||
303 | * in order to avoid deadlock with | ||
304 | * find_device_devfreq or others | ||
305 | */ | ||
306 | mutex_unlock(&devfreq->lock); | ||
307 | mutex_lock(&devfreq_list_lock); | ||
308 | /* Check if devfreq is already removed */ | ||
309 | if (IS_ERR(find_device_devfreq(dev))) | ||
310 | continue; | ||
311 | mutex_lock(&devfreq->lock); | ||
312 | /* This unlocks devfreq->lock and free it */ | ||
313 | _remove_devfreq(devfreq, false); | ||
314 | continue; | ||
315 | } | ||
316 | devfreq->next_polling = devfreq->polling_jiffies; | ||
317 | } else { | ||
318 | devfreq->next_polling -= jiffies_passed; | ||
319 | } | ||
320 | |||
321 | if (devfreq->next_polling) | ||
322 | next_jiffies = (next_jiffies > devfreq->next_polling) ? | ||
323 | devfreq->next_polling : next_jiffies; | ||
324 | |||
325 | mutex_unlock(&devfreq->lock); | ||
326 | mutex_lock(&devfreq_list_lock); | ||
327 | } | ||
328 | wait_remove_device = NULL; | ||
329 | mutex_unlock(&devfreq_list_lock); | ||
330 | |||
331 | if (next_jiffies > 0 && next_jiffies < ULONG_MAX) { | ||
332 | polling = true; | ||
333 | queue_delayed_work(devfreq_wq, &devfreq_work, next_jiffies); | ||
334 | } else { | ||
335 | polling = false; | ||
336 | } | ||
337 | } | 419 | } |
338 | 420 | ||
339 | /** | 421 | /** |
340 | * devfreq_add_device() - Add devfreq feature to the device | 422 | * devfreq_add_device() - Add devfreq feature to the device |
341 | * @dev: the device to add devfreq feature. | 423 | * @dev: the device to add devfreq feature. |
342 | * @profile: device-specific profile to run devfreq. | 424 | * @profile: device-specific profile to run devfreq. |
343 | * @governor: the policy to choose frequency. | 425 | * @governor_name: name of the policy to choose frequency. |
344 | * @data: private data for the governor. The devfreq framework does not | 426 | * @data: private data for the governor. The devfreq framework does not |
345 | * touch this value. | 427 | * touch this value. |
346 | */ | 428 | */ |
347 | struct devfreq *devfreq_add_device(struct device *dev, | 429 | struct devfreq *devfreq_add_device(struct device *dev, |
348 | struct devfreq_dev_profile *profile, | 430 | struct devfreq_dev_profile *profile, |
349 | const struct devfreq_governor *governor, | 431 | const char *governor_name, |
350 | void *data) | 432 | void *data) |
351 | { | 433 | { |
352 | struct devfreq *devfreq; | 434 | struct devfreq *devfreq; |
435 | struct devfreq_governor *governor; | ||
353 | int err = 0; | 436 | int err = 0; |
354 | 437 | ||
355 | if (!dev || !profile || !governor) { | 438 | if (!dev || !profile || !governor_name) { |
356 | dev_err(dev, "%s: Invalid parameters.\n", __func__); | 439 | dev_err(dev, "%s: Invalid parameters.\n", __func__); |
357 | return ERR_PTR(-EINVAL); | 440 | return ERR_PTR(-EINVAL); |
358 | } | 441 | } |
359 | 442 | ||
360 | 443 | mutex_lock(&devfreq_list_lock); | |
361 | if (!governor->no_central_polling) { | 444 | devfreq = find_device_devfreq(dev); |
362 | mutex_lock(&devfreq_list_lock); | 445 | mutex_unlock(&devfreq_list_lock); |
363 | devfreq = find_device_devfreq(dev); | 446 | if (!IS_ERR(devfreq)) { |
364 | mutex_unlock(&devfreq_list_lock); | 447 | dev_err(dev, "%s: Unable to create devfreq for the device. It already has one.\n", __func__); |
365 | if (!IS_ERR(devfreq)) { | 448 | err = -EINVAL; |
366 | dev_err(dev, "%s: Unable to create devfreq for the device. It already has one.\n", __func__); | 449 | goto err_out; |
367 | err = -EINVAL; | ||
368 | goto err_out; | ||
369 | } | ||
370 | } | 450 | } |
371 | 451 | ||
372 | devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL); | 452 | devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL); |
@@ -383,92 +463,316 @@ struct devfreq *devfreq_add_device(struct device *dev, | |||
383 | devfreq->dev.class = devfreq_class; | 463 | devfreq->dev.class = devfreq_class; |
384 | devfreq->dev.release = devfreq_dev_release; | 464 | devfreq->dev.release = devfreq_dev_release; |
385 | devfreq->profile = profile; | 465 | devfreq->profile = profile; |
386 | devfreq->governor = governor; | 466 | strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN); |
387 | devfreq->previous_freq = profile->initial_freq; | 467 | devfreq->previous_freq = profile->initial_freq; |
388 | devfreq->data = data; | 468 | devfreq->data = data; |
389 | devfreq->next_polling = devfreq->polling_jiffies | ||
390 | = msecs_to_jiffies(devfreq->profile->polling_ms); | ||
391 | devfreq->nb.notifier_call = devfreq_notifier_call; | 469 | devfreq->nb.notifier_call = devfreq_notifier_call; |
392 | 470 | ||
471 | devfreq->trans_table = devm_kzalloc(dev, sizeof(unsigned int) * | ||
472 | devfreq->profile->max_state * | ||
473 | devfreq->profile->max_state, | ||
474 | GFP_KERNEL); | ||
475 | devfreq->time_in_state = devm_kzalloc(dev, sizeof(unsigned int) * | ||
476 | devfreq->profile->max_state, | ||
477 | GFP_KERNEL); | ||
478 | devfreq->last_stat_updated = jiffies; | ||
479 | |||
393 | dev_set_name(&devfreq->dev, dev_name(dev)); | 480 | dev_set_name(&devfreq->dev, dev_name(dev)); |
394 | err = device_register(&devfreq->dev); | 481 | err = device_register(&devfreq->dev); |
395 | if (err) { | 482 | if (err) { |
396 | put_device(&devfreq->dev); | 483 | put_device(&devfreq->dev); |
484 | mutex_unlock(&devfreq->lock); | ||
397 | goto err_dev; | 485 | goto err_dev; |
398 | } | 486 | } |
399 | 487 | ||
400 | if (governor->init) | ||
401 | err = governor->init(devfreq); | ||
402 | if (err) | ||
403 | goto err_init; | ||
404 | |||
405 | mutex_unlock(&devfreq->lock); | 488 | mutex_unlock(&devfreq->lock); |
406 | 489 | ||
407 | if (governor->no_central_polling) | ||
408 | goto out; | ||
409 | |||
410 | mutex_lock(&devfreq_list_lock); | 490 | mutex_lock(&devfreq_list_lock); |
411 | |||
412 | list_add(&devfreq->node, &devfreq_list); | 491 | list_add(&devfreq->node, &devfreq_list); |
413 | 492 | ||
414 | if (devfreq_wq && devfreq->next_polling && !polling) { | 493 | governor = find_devfreq_governor(devfreq->governor_name); |
415 | polling = true; | 494 | if (!IS_ERR(governor)) |
416 | queue_delayed_work(devfreq_wq, &devfreq_work, | 495 | devfreq->governor = governor; |
417 | devfreq->next_polling); | 496 | if (devfreq->governor) |
418 | } | 497 | err = devfreq->governor->event_handler(devfreq, |
498 | DEVFREQ_GOV_START, NULL); | ||
419 | mutex_unlock(&devfreq_list_lock); | 499 | mutex_unlock(&devfreq_list_lock); |
420 | out: | 500 | if (err) { |
501 | dev_err(dev, "%s: Unable to start governor for the device\n", | ||
502 | __func__); | ||
503 | goto err_init; | ||
504 | } | ||
505 | |||
421 | return devfreq; | 506 | return devfreq; |
422 | 507 | ||
423 | err_init: | 508 | err_init: |
509 | list_del(&devfreq->node); | ||
424 | device_unregister(&devfreq->dev); | 510 | device_unregister(&devfreq->dev); |
425 | err_dev: | 511 | err_dev: |
426 | mutex_unlock(&devfreq->lock); | ||
427 | kfree(devfreq); | 512 | kfree(devfreq); |
428 | err_out: | 513 | err_out: |
429 | return ERR_PTR(err); | 514 | return ERR_PTR(err); |
430 | } | 515 | } |
516 | EXPORT_SYMBOL(devfreq_add_device); | ||
431 | 517 | ||
432 | /** | 518 | /** |
433 | * devfreq_remove_device() - Remove devfreq feature from a device. | 519 | * devfreq_remove_device() - Remove devfreq feature from a device. |
434 | * @devfreq the devfreq instance to be removed | 520 | * @devfreq: the devfreq instance to be removed |
435 | */ | 521 | */ |
436 | int devfreq_remove_device(struct devfreq *devfreq) | 522 | int devfreq_remove_device(struct devfreq *devfreq) |
437 | { | 523 | { |
438 | bool central_polling; | 524 | if (!devfreq) |
525 | return -EINVAL; | ||
526 | |||
527 | _remove_devfreq(devfreq, false); | ||
439 | 528 | ||
529 | return 0; | ||
530 | } | ||
531 | EXPORT_SYMBOL(devfreq_remove_device); | ||
532 | |||
533 | /** | ||
534 | * devfreq_suspend_device() - Suspend devfreq of a device. | ||
535 | * @devfreq: the devfreq instance to be suspended | ||
536 | */ | ||
537 | int devfreq_suspend_device(struct devfreq *devfreq) | ||
538 | { | ||
440 | if (!devfreq) | 539 | if (!devfreq) |
441 | return -EINVAL; | 540 | return -EINVAL; |
442 | 541 | ||
443 | central_polling = !devfreq->governor->no_central_polling; | 542 | if (!devfreq->governor) |
543 | return 0; | ||
544 | |||
545 | return devfreq->governor->event_handler(devfreq, | ||
546 | DEVFREQ_GOV_SUSPEND, NULL); | ||
547 | } | ||
548 | EXPORT_SYMBOL(devfreq_suspend_device); | ||
549 | |||
550 | /** | ||
551 | * devfreq_resume_device() - Resume devfreq of a device. | ||
552 | * @devfreq: the devfreq instance to be resumed | ||
553 | */ | ||
554 | int devfreq_resume_device(struct devfreq *devfreq) | ||
555 | { | ||
556 | if (!devfreq) | ||
557 | return -EINVAL; | ||
558 | |||
559 | if (!devfreq->governor) | ||
560 | return 0; | ||
561 | |||
562 | return devfreq->governor->event_handler(devfreq, | ||
563 | DEVFREQ_GOV_RESUME, NULL); | ||
564 | } | ||
565 | EXPORT_SYMBOL(devfreq_resume_device); | ||
566 | |||
567 | /** | ||
568 | * devfreq_add_governor() - Add devfreq governor | ||
569 | * @governor: the devfreq governor to be added | ||
570 | */ | ||
571 | int devfreq_add_governor(struct devfreq_governor *governor) | ||
572 | { | ||
573 | struct devfreq_governor *g; | ||
574 | struct devfreq *devfreq; | ||
575 | int err = 0; | ||
576 | |||
577 | if (!governor) { | ||
578 | pr_err("%s: Invalid parameters.\n", __func__); | ||
579 | return -EINVAL; | ||
580 | } | ||
581 | |||
582 | mutex_lock(&devfreq_list_lock); | ||
583 | g = find_devfreq_governor(governor->name); | ||
584 | if (!IS_ERR(g)) { | ||
585 | pr_err("%s: governor %s already registered\n", __func__, | ||
586 | g->name); | ||
587 | err = -EINVAL; | ||
588 | goto err_out; | ||
589 | } | ||
444 | 590 | ||
445 | if (central_polling) { | 591 | list_add(&governor->node, &devfreq_governor_list); |
446 | mutex_lock(&devfreq_list_lock); | 592 | |
447 | while (wait_remove_device == devfreq) { | 593 | list_for_each_entry(devfreq, &devfreq_list, node) { |
448 | mutex_unlock(&devfreq_list_lock); | 594 | int ret = 0; |
449 | schedule(); | 595 | struct device *dev = devfreq->dev.parent; |
450 | mutex_lock(&devfreq_list_lock); | 596 | |
597 | if (!strncmp(devfreq->governor_name, governor->name, | ||
598 | DEVFREQ_NAME_LEN)) { | ||
599 | /* The following should never occur */ | ||
600 | if (devfreq->governor) { | ||
601 | dev_warn(dev, | ||
602 | "%s: Governor %s already present\n", | ||
603 | __func__, devfreq->governor->name); | ||
604 | ret = devfreq->governor->event_handler(devfreq, | ||
605 | DEVFREQ_GOV_STOP, NULL); | ||
606 | if (ret) { | ||
607 | dev_warn(dev, | ||
608 | "%s: Governor %s stop = %d\n", | ||
609 | __func__, | ||
610 | devfreq->governor->name, ret); | ||
611 | } | ||
612 | /* Fall through */ | ||
613 | } | ||
614 | devfreq->governor = governor; | ||
615 | ret = devfreq->governor->event_handler(devfreq, | ||
616 | DEVFREQ_GOV_START, NULL); | ||
617 | if (ret) { | ||
618 | dev_warn(dev, "%s: Governor %s start=%d\n", | ||
619 | __func__, devfreq->governor->name, | ||
620 | ret); | ||
621 | } | ||
451 | } | 622 | } |
452 | } | 623 | } |
453 | 624 | ||
454 | mutex_lock(&devfreq->lock); | 625 | err_out: |
455 | _remove_devfreq(devfreq, false); /* it unlocks devfreq->lock */ | 626 | mutex_unlock(&devfreq_list_lock); |
456 | 627 | ||
457 | if (central_polling) | 628 | return err; |
458 | mutex_unlock(&devfreq_list_lock); | 629 | } |
630 | EXPORT_SYMBOL(devfreq_add_governor); | ||
459 | 631 | ||
460 | return 0; | 632 | /** |
633 | * devfreq_remove_device() - Remove devfreq feature from a device. | ||
634 | * @governor: the devfreq governor to be removed | ||
635 | */ | ||
636 | int devfreq_remove_governor(struct devfreq_governor *governor) | ||
637 | { | ||
638 | struct devfreq_governor *g; | ||
639 | struct devfreq *devfreq; | ||
640 | int err = 0; | ||
641 | |||
642 | if (!governor) { | ||
643 | pr_err("%s: Invalid parameters.\n", __func__); | ||
644 | return -EINVAL; | ||
645 | } | ||
646 | |||
647 | mutex_lock(&devfreq_list_lock); | ||
648 | g = find_devfreq_governor(governor->name); | ||
649 | if (IS_ERR(g)) { | ||
650 | pr_err("%s: governor %s not registered\n", __func__, | ||
651 | governor->name); | ||
652 | err = PTR_ERR(g); | ||
653 | goto err_out; | ||
654 | } | ||
655 | list_for_each_entry(devfreq, &devfreq_list, node) { | ||
656 | int ret; | ||
657 | struct device *dev = devfreq->dev.parent; | ||
658 | |||
659 | if (!strncmp(devfreq->governor_name, governor->name, | ||
660 | DEVFREQ_NAME_LEN)) { | ||
661 | /* we should have a devfreq governor! */ | ||
662 | if (!devfreq->governor) { | ||
663 | dev_warn(dev, "%s: Governor %s NOT present\n", | ||
664 | __func__, governor->name); | ||
665 | continue; | ||
666 | /* Fall through */ | ||
667 | } | ||
668 | ret = devfreq->governor->event_handler(devfreq, | ||
669 | DEVFREQ_GOV_STOP, NULL); | ||
670 | if (ret) { | ||
671 | dev_warn(dev, "%s: Governor %s stop=%d\n", | ||
672 | __func__, devfreq->governor->name, | ||
673 | ret); | ||
674 | } | ||
675 | devfreq->governor = NULL; | ||
676 | } | ||
677 | } | ||
678 | |||
679 | list_del(&governor->node); | ||
680 | err_out: | ||
681 | mutex_unlock(&devfreq_list_lock); | ||
682 | |||
683 | return err; | ||
461 | } | 684 | } |
685 | EXPORT_SYMBOL(devfreq_remove_governor); | ||
462 | 686 | ||
463 | static ssize_t show_governor(struct device *dev, | 687 | static ssize_t show_governor(struct device *dev, |
464 | struct device_attribute *attr, char *buf) | 688 | struct device_attribute *attr, char *buf) |
465 | { | 689 | { |
690 | if (!to_devfreq(dev)->governor) | ||
691 | return -EINVAL; | ||
692 | |||
466 | return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name); | 693 | return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name); |
467 | } | 694 | } |
468 | 695 | ||
696 | static ssize_t store_governor(struct device *dev, struct device_attribute *attr, | ||
697 | const char *buf, size_t count) | ||
698 | { | ||
699 | struct devfreq *df = to_devfreq(dev); | ||
700 | int ret; | ||
701 | char str_governor[DEVFREQ_NAME_LEN + 1]; | ||
702 | struct devfreq_governor *governor; | ||
703 | |||
704 | ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor); | ||
705 | if (ret != 1) | ||
706 | return -EINVAL; | ||
707 | |||
708 | mutex_lock(&devfreq_list_lock); | ||
709 | governor = find_devfreq_governor(str_governor); | ||
710 | if (IS_ERR(governor)) { | ||
711 | ret = PTR_ERR(governor); | ||
712 | goto out; | ||
713 | } | ||
714 | if (df->governor == governor) | ||
715 | goto out; | ||
716 | |||
717 | if (df->governor) { | ||
718 | ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL); | ||
719 | if (ret) { | ||
720 | dev_warn(dev, "%s: Governor %s not stopped(%d)\n", | ||
721 | __func__, df->governor->name, ret); | ||
722 | goto out; | ||
723 | } | ||
724 | } | ||
725 | df->governor = governor; | ||
726 | strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN); | ||
727 | ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL); | ||
728 | if (ret) | ||
729 | dev_warn(dev, "%s: Governor %s not started(%d)\n", | ||
730 | __func__, df->governor->name, ret); | ||
731 | out: | ||
732 | mutex_unlock(&devfreq_list_lock); | ||
733 | |||
734 | if (!ret) | ||
735 | ret = count; | ||
736 | return ret; | ||
737 | } | ||
738 | static ssize_t show_available_governors(struct device *d, | ||
739 | struct device_attribute *attr, | ||
740 | char *buf) | ||
741 | { | ||
742 | struct devfreq_governor *tmp_governor; | ||
743 | ssize_t count = 0; | ||
744 | |||
745 | mutex_lock(&devfreq_list_lock); | ||
746 | list_for_each_entry(tmp_governor, &devfreq_governor_list, node) | ||
747 | count += scnprintf(&buf[count], (PAGE_SIZE - count - 2), | ||
748 | "%s ", tmp_governor->name); | ||
749 | mutex_unlock(&devfreq_list_lock); | ||
750 | |||
751 | /* Truncate the trailing space */ | ||
752 | if (count) | ||
753 | count--; | ||
754 | |||
755 | count += sprintf(&buf[count], "\n"); | ||
756 | |||
757 | return count; | ||
758 | } | ||
759 | |||
469 | static ssize_t show_freq(struct device *dev, | 760 | static ssize_t show_freq(struct device *dev, |
470 | struct device_attribute *attr, char *buf) | 761 | struct device_attribute *attr, char *buf) |
471 | { | 762 | { |
763 | unsigned long freq; | ||
764 | struct devfreq *devfreq = to_devfreq(dev); | ||
765 | |||
766 | if (devfreq->profile->get_cur_freq && | ||
767 | !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq)) | ||
768 | return sprintf(buf, "%lu\n", freq); | ||
769 | |||
770 | return sprintf(buf, "%lu\n", devfreq->previous_freq); | ||
771 | } | ||
772 | |||
773 | static ssize_t show_target_freq(struct device *dev, | ||
774 | struct device_attribute *attr, char *buf) | ||
775 | { | ||
472 | return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq); | 776 | return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq); |
473 | } | 777 | } |
474 | 778 | ||
@@ -486,39 +790,19 @@ static ssize_t store_polling_interval(struct device *dev, | |||
486 | unsigned int value; | 790 | unsigned int value; |
487 | int ret; | 791 | int ret; |
488 | 792 | ||
793 | if (!df->governor) | ||
794 | return -EINVAL; | ||
795 | |||
489 | ret = sscanf(buf, "%u", &value); | 796 | ret = sscanf(buf, "%u", &value); |
490 | if (ret != 1) | 797 | if (ret != 1) |
491 | goto out; | 798 | return -EINVAL; |
492 | |||
493 | mutex_lock(&df->lock); | ||
494 | df->profile->polling_ms = value; | ||
495 | df->next_polling = df->polling_jiffies | ||
496 | = msecs_to_jiffies(value); | ||
497 | mutex_unlock(&df->lock); | ||
498 | 799 | ||
800 | df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value); | ||
499 | ret = count; | 801 | ret = count; |
500 | 802 | ||
501 | if (df->governor->no_central_polling) | ||
502 | goto out; | ||
503 | |||
504 | mutex_lock(&devfreq_list_lock); | ||
505 | if (df->next_polling > 0 && !polling) { | ||
506 | polling = true; | ||
507 | queue_delayed_work(devfreq_wq, &devfreq_work, | ||
508 | df->next_polling); | ||
509 | } | ||
510 | mutex_unlock(&devfreq_list_lock); | ||
511 | out: | ||
512 | return ret; | 803 | return ret; |
513 | } | 804 | } |
514 | 805 | ||
515 | static ssize_t show_central_polling(struct device *dev, | ||
516 | struct device_attribute *attr, char *buf) | ||
517 | { | ||
518 | return sprintf(buf, "%d\n", | ||
519 | !to_devfreq(dev)->governor->no_central_polling); | ||
520 | } | ||
521 | |||
522 | static ssize_t store_min_freq(struct device *dev, struct device_attribute *attr, | 806 | static ssize_t store_min_freq(struct device *dev, struct device_attribute *attr, |
523 | const char *buf, size_t count) | 807 | const char *buf, size_t count) |
524 | { | 808 | { |
@@ -529,7 +813,7 @@ static ssize_t store_min_freq(struct device *dev, struct device_attribute *attr, | |||
529 | 813 | ||
530 | ret = sscanf(buf, "%lu", &value); | 814 | ret = sscanf(buf, "%lu", &value); |
531 | if (ret != 1) | 815 | if (ret != 1) |
532 | goto out; | 816 | return -EINVAL; |
533 | 817 | ||
534 | mutex_lock(&df->lock); | 818 | mutex_lock(&df->lock); |
535 | max = df->max_freq; | 819 | max = df->max_freq; |
@@ -543,7 +827,6 @@ static ssize_t store_min_freq(struct device *dev, struct device_attribute *attr, | |||
543 | ret = count; | 827 | ret = count; |
544 | unlock: | 828 | unlock: |
545 | mutex_unlock(&df->lock); | 829 | mutex_unlock(&df->lock); |
546 | out: | ||
547 | return ret; | 830 | return ret; |
548 | } | 831 | } |
549 | 832 | ||
@@ -563,7 +846,7 @@ static ssize_t store_max_freq(struct device *dev, struct device_attribute *attr, | |||
563 | 846 | ||
564 | ret = sscanf(buf, "%lu", &value); | 847 | ret = sscanf(buf, "%lu", &value); |
565 | if (ret != 1) | 848 | if (ret != 1) |
566 | goto out; | 849 | return -EINVAL; |
567 | 850 | ||
568 | mutex_lock(&df->lock); | 851 | mutex_lock(&df->lock); |
569 | min = df->min_freq; | 852 | min = df->min_freq; |
@@ -577,7 +860,6 @@ static ssize_t store_max_freq(struct device *dev, struct device_attribute *attr, | |||
577 | ret = count; | 860 | ret = count; |
578 | unlock: | 861 | unlock: |
579 | mutex_unlock(&df->lock); | 862 | mutex_unlock(&df->lock); |
580 | out: | ||
581 | return ret; | 863 | return ret; |
582 | } | 864 | } |
583 | 865 | ||
@@ -587,34 +869,92 @@ static ssize_t show_max_freq(struct device *dev, struct device_attribute *attr, | |||
587 | return sprintf(buf, "%lu\n", to_devfreq(dev)->max_freq); | 869 | return sprintf(buf, "%lu\n", to_devfreq(dev)->max_freq); |
588 | } | 870 | } |
589 | 871 | ||
872 | static ssize_t show_available_freqs(struct device *d, | ||
873 | struct device_attribute *attr, | ||
874 | char *buf) | ||
875 | { | ||
876 | struct devfreq *df = to_devfreq(d); | ||
877 | struct device *dev = df->dev.parent; | ||
878 | struct opp *opp; | ||
879 | ssize_t count = 0; | ||
880 | unsigned long freq = 0; | ||
881 | |||
882 | rcu_read_lock(); | ||
883 | do { | ||
884 | opp = opp_find_freq_ceil(dev, &freq); | ||
885 | if (IS_ERR(opp)) | ||
886 | break; | ||
887 | |||
888 | count += scnprintf(&buf[count], (PAGE_SIZE - count - 2), | ||
889 | "%lu ", freq); | ||
890 | freq++; | ||
891 | } while (1); | ||
892 | rcu_read_unlock(); | ||
893 | |||
894 | /* Truncate the trailing space */ | ||
895 | if (count) | ||
896 | count--; | ||
897 | |||
898 | count += sprintf(&buf[count], "\n"); | ||
899 | |||
900 | return count; | ||
901 | } | ||
902 | |||
903 | static ssize_t show_trans_table(struct device *dev, struct device_attribute *attr, | ||
904 | char *buf) | ||
905 | { | ||
906 | struct devfreq *devfreq = to_devfreq(dev); | ||
907 | ssize_t len; | ||
908 | int i, j, err; | ||
909 | unsigned int max_state = devfreq->profile->max_state; | ||
910 | |||
911 | err = devfreq_update_status(devfreq, devfreq->previous_freq); | ||
912 | if (err) | ||
913 | return 0; | ||
914 | |||
915 | len = sprintf(buf, " From : To\n"); | ||
916 | len += sprintf(buf + len, " :"); | ||
917 | for (i = 0; i < max_state; i++) | ||
918 | len += sprintf(buf + len, "%8u", | ||
919 | devfreq->profile->freq_table[i]); | ||
920 | |||
921 | len += sprintf(buf + len, " time(ms)\n"); | ||
922 | |||
923 | for (i = 0; i < max_state; i++) { | ||
924 | if (devfreq->profile->freq_table[i] | ||
925 | == devfreq->previous_freq) { | ||
926 | len += sprintf(buf + len, "*"); | ||
927 | } else { | ||
928 | len += sprintf(buf + len, " "); | ||
929 | } | ||
930 | len += sprintf(buf + len, "%8u:", | ||
931 | devfreq->profile->freq_table[i]); | ||
932 | for (j = 0; j < max_state; j++) | ||
933 | len += sprintf(buf + len, "%8u", | ||
934 | devfreq->trans_table[(i * max_state) + j]); | ||
935 | len += sprintf(buf + len, "%10u\n", | ||
936 | jiffies_to_msecs(devfreq->time_in_state[i])); | ||
937 | } | ||
938 | |||
939 | len += sprintf(buf + len, "Total transition : %u\n", | ||
940 | devfreq->total_trans); | ||
941 | return len; | ||
942 | } | ||
943 | |||
590 | static struct device_attribute devfreq_attrs[] = { | 944 | static struct device_attribute devfreq_attrs[] = { |
591 | __ATTR(governor, S_IRUGO, show_governor, NULL), | 945 | __ATTR(governor, S_IRUGO | S_IWUSR, show_governor, store_governor), |
946 | __ATTR(available_governors, S_IRUGO, show_available_governors, NULL), | ||
592 | __ATTR(cur_freq, S_IRUGO, show_freq, NULL), | 947 | __ATTR(cur_freq, S_IRUGO, show_freq, NULL), |
593 | __ATTR(central_polling, S_IRUGO, show_central_polling, NULL), | 948 | __ATTR(available_frequencies, S_IRUGO, show_available_freqs, NULL), |
949 | __ATTR(target_freq, S_IRUGO, show_target_freq, NULL), | ||
594 | __ATTR(polling_interval, S_IRUGO | S_IWUSR, show_polling_interval, | 950 | __ATTR(polling_interval, S_IRUGO | S_IWUSR, show_polling_interval, |
595 | store_polling_interval), | 951 | store_polling_interval), |
596 | __ATTR(min_freq, S_IRUGO | S_IWUSR, show_min_freq, store_min_freq), | 952 | __ATTR(min_freq, S_IRUGO | S_IWUSR, show_min_freq, store_min_freq), |
597 | __ATTR(max_freq, S_IRUGO | S_IWUSR, show_max_freq, store_max_freq), | 953 | __ATTR(max_freq, S_IRUGO | S_IWUSR, show_max_freq, store_max_freq), |
954 | __ATTR(trans_stat, S_IRUGO, show_trans_table, NULL), | ||
598 | { }, | 955 | { }, |
599 | }; | 956 | }; |
600 | 957 | ||
601 | /** | ||
602 | * devfreq_start_polling() - Initialize data structure for devfreq framework and | ||
603 | * start polling registered devfreq devices. | ||
604 | */ | ||
605 | static int __init devfreq_start_polling(void) | ||
606 | { | ||
607 | mutex_lock(&devfreq_list_lock); | ||
608 | polling = false; | ||
609 | devfreq_wq = create_freezable_workqueue("devfreq_wq"); | ||
610 | INIT_DEFERRABLE_WORK(&devfreq_work, devfreq_monitor); | ||
611 | mutex_unlock(&devfreq_list_lock); | ||
612 | |||
613 | devfreq_monitor(&devfreq_work.work); | ||
614 | return 0; | ||
615 | } | ||
616 | late_initcall(devfreq_start_polling); | ||
617 | |||
618 | static int __init devfreq_init(void) | 958 | static int __init devfreq_init(void) |
619 | { | 959 | { |
620 | devfreq_class = class_create(THIS_MODULE, "devfreq"); | 960 | devfreq_class = class_create(THIS_MODULE, "devfreq"); |
@@ -622,7 +962,15 @@ static int __init devfreq_init(void) | |||
622 | pr_err("%s: couldn't create class\n", __FILE__); | 962 | pr_err("%s: couldn't create class\n", __FILE__); |
623 | return PTR_ERR(devfreq_class); | 963 | return PTR_ERR(devfreq_class); |
624 | } | 964 | } |
965 | |||
966 | devfreq_wq = create_freezable_workqueue("devfreq_wq"); | ||
967 | if (IS_ERR(devfreq_wq)) { | ||
968 | class_destroy(devfreq_class); | ||
969 | pr_err("%s: couldn't create workqueue\n", __FILE__); | ||
970 | return PTR_ERR(devfreq_wq); | ||
971 | } | ||
625 | devfreq_class->dev_attrs = devfreq_attrs; | 972 | devfreq_class->dev_attrs = devfreq_attrs; |
973 | |||
626 | return 0; | 974 | return 0; |
627 | } | 975 | } |
628 | subsys_initcall(devfreq_init); | 976 | subsys_initcall(devfreq_init); |
@@ -630,6 +978,7 @@ subsys_initcall(devfreq_init); | |||
630 | static void __exit devfreq_exit(void) | 978 | static void __exit devfreq_exit(void) |
631 | { | 979 | { |
632 | class_destroy(devfreq_class); | 980 | class_destroy(devfreq_class); |
981 | destroy_workqueue(devfreq_wq); | ||
633 | } | 982 | } |
634 | module_exit(devfreq_exit); | 983 | module_exit(devfreq_exit); |
635 | 984 | ||
@@ -641,9 +990,9 @@ module_exit(devfreq_exit); | |||
641 | /** | 990 | /** |
642 | * devfreq_recommended_opp() - Helper function to get proper OPP for the | 991 | * devfreq_recommended_opp() - Helper function to get proper OPP for the |
643 | * freq value given to target callback. | 992 | * freq value given to target callback. |
644 | * @dev The devfreq user device. (parent of devfreq) | 993 | * @dev: The devfreq user device. (parent of devfreq) |
645 | * @freq The frequency given to target function | 994 | * @freq: The frequency given to target function |
646 | * @flags Flags handed from devfreq framework. | 995 | * @flags: Flags handed from devfreq framework. |
647 | * | 996 | * |
648 | */ | 997 | */ |
649 | struct opp *devfreq_recommended_opp(struct device *dev, unsigned long *freq, | 998 | struct opp *devfreq_recommended_opp(struct device *dev, unsigned long *freq, |
@@ -656,14 +1005,14 @@ struct opp *devfreq_recommended_opp(struct device *dev, unsigned long *freq, | |||
656 | opp = opp_find_freq_floor(dev, freq); | 1005 | opp = opp_find_freq_floor(dev, freq); |
657 | 1006 | ||
658 | /* If not available, use the closest opp */ | 1007 | /* If not available, use the closest opp */ |
659 | if (opp == ERR_PTR(-ENODEV)) | 1008 | if (opp == ERR_PTR(-ERANGE)) |
660 | opp = opp_find_freq_ceil(dev, freq); | 1009 | opp = opp_find_freq_ceil(dev, freq); |
661 | } else { | 1010 | } else { |
662 | /* The freq is an lower bound. opp should be higher */ | 1011 | /* The freq is an lower bound. opp should be higher */ |
663 | opp = opp_find_freq_ceil(dev, freq); | 1012 | opp = opp_find_freq_ceil(dev, freq); |
664 | 1013 | ||
665 | /* If not available, use the closest opp */ | 1014 | /* If not available, use the closest opp */ |
666 | if (opp == ERR_PTR(-ENODEV)) | 1015 | if (opp == ERR_PTR(-ERANGE)) |
667 | opp = opp_find_freq_floor(dev, freq); | 1016 | opp = opp_find_freq_floor(dev, freq); |
668 | } | 1017 | } |
669 | 1018 | ||
@@ -674,35 +1023,49 @@ struct opp *devfreq_recommended_opp(struct device *dev, unsigned long *freq, | |||
674 | * devfreq_register_opp_notifier() - Helper function to get devfreq notified | 1023 | * devfreq_register_opp_notifier() - Helper function to get devfreq notified |
675 | * for any changes in the OPP availability | 1024 | * for any changes in the OPP availability |
676 | * changes | 1025 | * changes |
677 | * @dev The devfreq user device. (parent of devfreq) | 1026 | * @dev: The devfreq user device. (parent of devfreq) |
678 | * @devfreq The devfreq object. | 1027 | * @devfreq: The devfreq object. |
679 | */ | 1028 | */ |
680 | int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq) | 1029 | int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq) |
681 | { | 1030 | { |
682 | struct srcu_notifier_head *nh = opp_get_notifier(dev); | 1031 | struct srcu_notifier_head *nh; |
1032 | int ret = 0; | ||
683 | 1033 | ||
1034 | rcu_read_lock(); | ||
1035 | nh = opp_get_notifier(dev); | ||
684 | if (IS_ERR(nh)) | 1036 | if (IS_ERR(nh)) |
685 | return PTR_ERR(nh); | 1037 | ret = PTR_ERR(nh); |
686 | return srcu_notifier_chain_register(nh, &devfreq->nb); | 1038 | rcu_read_unlock(); |
1039 | if (!ret) | ||
1040 | ret = srcu_notifier_chain_register(nh, &devfreq->nb); | ||
1041 | |||
1042 | return ret; | ||
687 | } | 1043 | } |
688 | 1044 | ||
689 | /** | 1045 | /** |
690 | * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq | 1046 | * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq |
691 | * notified for any changes in the OPP | 1047 | * notified for any changes in the OPP |
692 | * availability changes anymore. | 1048 | * availability changes anymore. |
693 | * @dev The devfreq user device. (parent of devfreq) | 1049 | * @dev: The devfreq user device. (parent of devfreq) |
694 | * @devfreq The devfreq object. | 1050 | * @devfreq: The devfreq object. |
695 | * | 1051 | * |
696 | * At exit() callback of devfreq_dev_profile, this must be included if | 1052 | * At exit() callback of devfreq_dev_profile, this must be included if |
697 | * devfreq_recommended_opp is used. | 1053 | * devfreq_recommended_opp is used. |
698 | */ | 1054 | */ |
699 | int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq) | 1055 | int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq) |
700 | { | 1056 | { |
701 | struct srcu_notifier_head *nh = opp_get_notifier(dev); | 1057 | struct srcu_notifier_head *nh; |
1058 | int ret = 0; | ||
702 | 1059 | ||
1060 | rcu_read_lock(); | ||
1061 | nh = opp_get_notifier(dev); | ||
703 | if (IS_ERR(nh)) | 1062 | if (IS_ERR(nh)) |
704 | return PTR_ERR(nh); | 1063 | ret = PTR_ERR(nh); |
705 | return srcu_notifier_chain_unregister(nh, &devfreq->nb); | 1064 | rcu_read_unlock(); |
1065 | if (!ret) | ||
1066 | ret = srcu_notifier_chain_unregister(nh, &devfreq->nb); | ||
1067 | |||
1068 | return ret; | ||
706 | } | 1069 | } |
707 | 1070 | ||
708 | MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); | 1071 | MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); |