xref: /openbmc/linux/drivers/devfreq/devfreq.c (revision d0676871)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework
4  *	    for Non-CPU Devices.
5  *
6  * Copyright (C) 2011 Samsung Electronics
7  *	MyungJoo Ham <myungjoo.ham@samsung.com>
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/kmod.h>
12 #include <linux/sched.h>
13 #include <linux/debugfs.h>
14 #include <linux/errno.h>
15 #include <linux/err.h>
16 #include <linux/init.h>
17 #include <linux/export.h>
18 #include <linux/slab.h>
19 #include <linux/stat.h>
20 #include <linux/pm_opp.h>
21 #include <linux/devfreq.h>
22 #include <linux/workqueue.h>
23 #include <linux/platform_device.h>
24 #include <linux/list.h>
25 #include <linux/printk.h>
26 #include <linux/hrtimer.h>
27 #include <linux/of.h>
28 #include <linux/pm_qos.h>
29 #include "governor.h"
30 
31 #define CREATE_TRACE_POINTS
32 #include <trace/events/devfreq.h>
33 
34 #define HZ_PER_KHZ	1000
35 
36 static struct class *devfreq_class;
37 static struct dentry *devfreq_debugfs;
38 
39 /*
40  * devfreq core provides delayed work based load monitoring helper
41  * functions. Governors can use these or can implement their own
42  * monitoring mechanism.
43  */
44 static struct workqueue_struct *devfreq_wq;
45 
46 /* The list of all device-devfreq governors */
47 static LIST_HEAD(devfreq_governor_list);
48 /* The list of all device-devfreq */
49 static LIST_HEAD(devfreq_list);
50 static DEFINE_MUTEX(devfreq_list_lock);
51 
52 /**
53  * find_device_devfreq() - find devfreq struct using device pointer
54  * @dev:	device pointer used to lookup device devfreq.
55  *
56  * Search the list of device devfreqs and return the matched device's
57  * devfreq info. devfreq_list_lock should be held by the caller.
58  */
59 static struct devfreq *find_device_devfreq(struct device *dev)
60 {
61 	struct devfreq *tmp_devfreq;
62 
63 	if (IS_ERR_OR_NULL(dev)) {
64 		pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
65 		return ERR_PTR(-EINVAL);
66 	}
67 	WARN(!mutex_is_locked(&devfreq_list_lock),
68 	     "devfreq_list_lock must be locked.");
69 
70 	list_for_each_entry(tmp_devfreq, &devfreq_list, node) {
71 		if (tmp_devfreq->dev.parent == dev)
72 			return tmp_devfreq;
73 	}
74 
75 	return ERR_PTR(-ENODEV);
76 }
77 
78 static unsigned long find_available_min_freq(struct devfreq *devfreq)
79 {
80 	struct dev_pm_opp *opp;
81 	unsigned long min_freq = 0;
82 
83 	opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &min_freq);
84 	if (IS_ERR(opp))
85 		min_freq = 0;
86 	else
87 		dev_pm_opp_put(opp);
88 
89 	return min_freq;
90 }
91 
92 static unsigned long find_available_max_freq(struct devfreq *devfreq)
93 {
94 	struct dev_pm_opp *opp;
95 	unsigned long max_freq = ULONG_MAX;
96 
97 	opp = dev_pm_opp_find_freq_floor(devfreq->dev.parent, &max_freq);
98 	if (IS_ERR(opp))
99 		max_freq = 0;
100 	else
101 		dev_pm_opp_put(opp);
102 
103 	return max_freq;
104 }
105 
106 /**
107  * get_freq_range() - Get the current freq range
108  * @devfreq:	the devfreq instance
109  * @min_freq:	the min frequency
110  * @max_freq:	the max frequency
111  *
112  * This takes into consideration all constraints.
113  */
114 static void get_freq_range(struct devfreq *devfreq,
115 			   unsigned long *min_freq,
116 			   unsigned long *max_freq)
117 {
118 	unsigned long *freq_table = devfreq->profile->freq_table;
119 	s32 qos_min_freq, qos_max_freq;
120 
121 	lockdep_assert_held(&devfreq->lock);
122 
123 	/*
124 	 * Initialize minimum/maximum frequency from freq table.
125 	 * The devfreq drivers can initialize this in either ascending or
126 	 * descending order and devfreq core supports both.
127 	 */
128 	if (freq_table[0] < freq_table[devfreq->profile->max_state - 1]) {
129 		*min_freq = freq_table[0];
130 		*max_freq = freq_table[devfreq->profile->max_state - 1];
131 	} else {
132 		*min_freq = freq_table[devfreq->profile->max_state - 1];
133 		*max_freq = freq_table[0];
134 	}
135 
136 	/* Apply constraints from PM QoS */
137 	qos_min_freq = dev_pm_qos_read_value(devfreq->dev.parent,
138 					     DEV_PM_QOS_MIN_FREQUENCY);
139 	qos_max_freq = dev_pm_qos_read_value(devfreq->dev.parent,
140 					     DEV_PM_QOS_MAX_FREQUENCY);
141 	*min_freq = max(*min_freq, (unsigned long)HZ_PER_KHZ * qos_min_freq);
142 	if (qos_max_freq != PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE)
143 		*max_freq = min(*max_freq,
144 				(unsigned long)HZ_PER_KHZ * qos_max_freq);
145 
146 	/* Apply constraints from OPP interface */
147 	*min_freq = max(*min_freq, devfreq->scaling_min_freq);
148 	*max_freq = min(*max_freq, devfreq->scaling_max_freq);
149 
150 	if (*min_freq > *max_freq)
151 		*min_freq = *max_freq;
152 }
153 
154 /**
155  * devfreq_get_freq_level() - Lookup freq_table for the frequency
156  * @devfreq:	the devfreq instance
157  * @freq:	the target frequency
158  */
159 static int devfreq_get_freq_level(struct devfreq *devfreq, unsigned long freq)
160 {
161 	int lev;
162 
163 	for (lev = 0; lev < devfreq->profile->max_state; lev++)
164 		if (freq == devfreq->profile->freq_table[lev])
165 			return lev;
166 
167 	return -EINVAL;
168 }
169 
170 static int set_freq_table(struct devfreq *devfreq)
171 {
172 	struct devfreq_dev_profile *profile = devfreq->profile;
173 	struct dev_pm_opp *opp;
174 	unsigned long freq;
175 	int i, count;
176 
177 	/* Initialize the freq_table from OPP table */
178 	count = dev_pm_opp_get_opp_count(devfreq->dev.parent);
179 	if (count <= 0)
180 		return -EINVAL;
181 
182 	profile->max_state = count;
183 	profile->freq_table = devm_kcalloc(devfreq->dev.parent,
184 					profile->max_state,
185 					sizeof(*profile->freq_table),
186 					GFP_KERNEL);
187 	if (!profile->freq_table) {
188 		profile->max_state = 0;
189 		return -ENOMEM;
190 	}
191 
192 	for (i = 0, freq = 0; i < profile->max_state; i++, freq++) {
193 		opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &freq);
194 		if (IS_ERR(opp)) {
195 			devm_kfree(devfreq->dev.parent, profile->freq_table);
196 			profile->max_state = 0;
197 			return PTR_ERR(opp);
198 		}
199 		dev_pm_opp_put(opp);
200 		profile->freq_table[i] = freq;
201 	}
202 
203 	return 0;
204 }
205 
206 /**
207  * devfreq_update_status() - Update statistics of devfreq behavior
208  * @devfreq:	the devfreq instance
209  * @freq:	the update target frequency
210  */
211 int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
212 {
213 	int lev, prev_lev, ret = 0;
214 	u64 cur_time;
215 
216 	lockdep_assert_held(&devfreq->lock);
217 	cur_time = get_jiffies_64();
218 
219 	/* Immediately exit if previous_freq is not initialized yet. */
220 	if (!devfreq->previous_freq)
221 		goto out;
222 
223 	prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq);
224 	if (prev_lev < 0) {
225 		ret = prev_lev;
226 		goto out;
227 	}
228 
229 	devfreq->stats.time_in_state[prev_lev] +=
230 			cur_time - devfreq->stats.last_update;
231 
232 	lev = devfreq_get_freq_level(devfreq, freq);
233 	if (lev < 0) {
234 		ret = lev;
235 		goto out;
236 	}
237 
238 	if (lev != prev_lev) {
239 		devfreq->stats.trans_table[
240 			(prev_lev * devfreq->profile->max_state) + lev]++;
241 		devfreq->stats.total_trans++;
242 	}
243 
244 out:
245 	devfreq->stats.last_update = cur_time;
246 	return ret;
247 }
248 EXPORT_SYMBOL(devfreq_update_status);
249 
250 /**
251  * find_devfreq_governor() - find devfreq governor from name
252  * @name:	name of the governor
253  *
254  * Search the list of devfreq governors and return the matched
255  * governor's pointer. devfreq_list_lock should be held by the caller.
256  */
257 static struct devfreq_governor *find_devfreq_governor(const char *name)
258 {
259 	struct devfreq_governor *tmp_governor;
260 
261 	if (IS_ERR_OR_NULL(name)) {
262 		pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
263 		return ERR_PTR(-EINVAL);
264 	}
265 	WARN(!mutex_is_locked(&devfreq_list_lock),
266 	     "devfreq_list_lock must be locked.");
267 
268 	list_for_each_entry(tmp_governor, &devfreq_governor_list, node) {
269 		if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN))
270 			return tmp_governor;
271 	}
272 
273 	return ERR_PTR(-ENODEV);
274 }
275 
276 /**
277  * try_then_request_governor() - Try to find the governor and request the
278  *                               module if is not found.
279  * @name:	name of the governor
280  *
281  * Search the list of devfreq governors and request the module and try again
282  * if is not found. This can happen when both drivers (the governor driver
283  * and the driver that call devfreq_add_device) are built as modules.
284  * devfreq_list_lock should be held by the caller. Returns the matched
285  * governor's pointer or an error pointer.
286  */
287 static struct devfreq_governor *try_then_request_governor(const char *name)
288 {
289 	struct devfreq_governor *governor;
290 	int err = 0;
291 
292 	if (IS_ERR_OR_NULL(name)) {
293 		pr_err("DEVFREQ: %s: Invalid parameters\n", __func__);
294 		return ERR_PTR(-EINVAL);
295 	}
296 	WARN(!mutex_is_locked(&devfreq_list_lock),
297 	     "devfreq_list_lock must be locked.");
298 
299 	governor = find_devfreq_governor(name);
300 	if (IS_ERR(governor)) {
301 		mutex_unlock(&devfreq_list_lock);
302 
303 		if (!strncmp(name, DEVFREQ_GOV_SIMPLE_ONDEMAND,
304 			     DEVFREQ_NAME_LEN))
305 			err = request_module("governor_%s", "simpleondemand");
306 		else
307 			err = request_module("governor_%s", name);
308 		/* Restore previous state before return */
309 		mutex_lock(&devfreq_list_lock);
310 		if (err)
311 			return (err < 0) ? ERR_PTR(err) : ERR_PTR(-EINVAL);
312 
313 		governor = find_devfreq_governor(name);
314 	}
315 
316 	return governor;
317 }
318 
319 static int devfreq_notify_transition(struct devfreq *devfreq,
320 		struct devfreq_freqs *freqs, unsigned int state)
321 {
322 	if (!devfreq)
323 		return -EINVAL;
324 
325 	switch (state) {
326 	case DEVFREQ_PRECHANGE:
327 		srcu_notifier_call_chain(&devfreq->transition_notifier_list,
328 				DEVFREQ_PRECHANGE, freqs);
329 		break;
330 
331 	case DEVFREQ_POSTCHANGE:
332 		srcu_notifier_call_chain(&devfreq->transition_notifier_list,
333 				DEVFREQ_POSTCHANGE, freqs);
334 		break;
335 	default:
336 		return -EINVAL;
337 	}
338 
339 	return 0;
340 }
341 
342 static int devfreq_set_target(struct devfreq *devfreq, unsigned long new_freq,
343 			      u32 flags)
344 {
345 	struct devfreq_freqs freqs;
346 	unsigned long cur_freq;
347 	int err = 0;
348 
349 	if (devfreq->profile->get_cur_freq)
350 		devfreq->profile->get_cur_freq(devfreq->dev.parent, &cur_freq);
351 	else
352 		cur_freq = devfreq->previous_freq;
353 
354 	freqs.old = cur_freq;
355 	freqs.new = new_freq;
356 	devfreq_notify_transition(devfreq, &freqs, DEVFREQ_PRECHANGE);
357 
358 	err = devfreq->profile->target(devfreq->dev.parent, &new_freq, flags);
359 	if (err) {
360 		freqs.new = cur_freq;
361 		devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
362 		return err;
363 	}
364 
365 	freqs.new = new_freq;
366 	devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE);
367 
368 	if (devfreq_update_status(devfreq, new_freq))
369 		dev_err(&devfreq->dev,
370 			"Couldn't update frequency transition information.\n");
371 
372 	devfreq->previous_freq = new_freq;
373 
374 	if (devfreq->suspend_freq)
375 		devfreq->resume_freq = cur_freq;
376 
377 	return err;
378 }
379 
380 /* Load monitoring helper functions for governors use */
381 
382 /**
383  * update_devfreq() - Reevaluate the device and configure frequency.
384  * @devfreq:	the devfreq instance.
385  *
386  * Note: Lock devfreq->lock before calling update_devfreq
387  *	 This function is exported for governors.
388  */
389 int update_devfreq(struct devfreq *devfreq)
390 {
391 	unsigned long freq, min_freq, max_freq;
392 	int err = 0;
393 	u32 flags = 0;
394 
395 	if (!mutex_is_locked(&devfreq->lock)) {
396 		WARN(true, "devfreq->lock must be locked by the caller.\n");
397 		return -EINVAL;
398 	}
399 
400 	if (!devfreq->governor)
401 		return -EINVAL;
402 
403 	/* Reevaluate the proper frequency */
404 	err = devfreq->governor->get_target_freq(devfreq, &freq);
405 	if (err)
406 		return err;
407 	get_freq_range(devfreq, &min_freq, &max_freq);
408 
409 	if (freq < min_freq) {
410 		freq = min_freq;
411 		flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */
412 	}
413 	if (freq > max_freq) {
414 		freq = max_freq;
415 		flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */
416 	}
417 
418 	return devfreq_set_target(devfreq, freq, flags);
419 
420 }
421 EXPORT_SYMBOL(update_devfreq);
422 
423 /**
424  * devfreq_monitor() - Periodically poll devfreq objects.
425  * @work:	the work struct used to run devfreq_monitor periodically.
426  *
427  */
428 static void devfreq_monitor(struct work_struct *work)
429 {
430 	int err;
431 	struct devfreq *devfreq = container_of(work,
432 					struct devfreq, work.work);
433 
434 	mutex_lock(&devfreq->lock);
435 	err = update_devfreq(devfreq);
436 	if (err)
437 		dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err);
438 
439 	queue_delayed_work(devfreq_wq, &devfreq->work,
440 				msecs_to_jiffies(devfreq->profile->polling_ms));
441 	mutex_unlock(&devfreq->lock);
442 
443 	trace_devfreq_monitor(devfreq);
444 }
445 
446 /**
447  * devfreq_monitor_start() - Start load monitoring of devfreq instance
448  * @devfreq:	the devfreq instance.
449  *
450  * Helper function for starting devfreq device load monitoring. By
451  * default delayed work based monitoring is supported. Function
452  * to be called from governor in response to DEVFREQ_GOV_START
453  * event when device is added to devfreq framework.
454  */
455 void devfreq_monitor_start(struct devfreq *devfreq)
456 {
457 	if (devfreq->governor->interrupt_driven)
458 		return;
459 
460 	INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor);
461 	if (devfreq->profile->polling_ms)
462 		queue_delayed_work(devfreq_wq, &devfreq->work,
463 			msecs_to_jiffies(devfreq->profile->polling_ms));
464 }
465 EXPORT_SYMBOL(devfreq_monitor_start);
466 
467 /**
468  * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance
469  * @devfreq:	the devfreq instance.
470  *
471  * Helper function to stop devfreq device load monitoring. Function
472  * to be called from governor in response to DEVFREQ_GOV_STOP
473  * event when device is removed from devfreq framework.
474  */
475 void devfreq_monitor_stop(struct devfreq *devfreq)
476 {
477 	if (devfreq->governor->interrupt_driven)
478 		return;
479 
480 	cancel_delayed_work_sync(&devfreq->work);
481 }
482 EXPORT_SYMBOL(devfreq_monitor_stop);
483 
484 /**
485  * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance
486  * @devfreq:	the devfreq instance.
487  *
488  * Helper function to suspend devfreq device load monitoring. Function
489  * to be called from governor in response to DEVFREQ_GOV_SUSPEND
490  * event or when polling interval is set to zero.
491  *
492  * Note: Though this function is same as devfreq_monitor_stop(),
493  * intentionally kept separate to provide hooks for collecting
494  * transition statistics.
495  */
496 void devfreq_monitor_suspend(struct devfreq *devfreq)
497 {
498 	mutex_lock(&devfreq->lock);
499 	if (devfreq->stop_polling) {
500 		mutex_unlock(&devfreq->lock);
501 		return;
502 	}
503 
504 	devfreq_update_status(devfreq, devfreq->previous_freq);
505 	devfreq->stop_polling = true;
506 	mutex_unlock(&devfreq->lock);
507 
508 	if (devfreq->governor->interrupt_driven)
509 		return;
510 
511 	cancel_delayed_work_sync(&devfreq->work);
512 }
513 EXPORT_SYMBOL(devfreq_monitor_suspend);
514 
515 /**
516  * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance
517  * @devfreq:    the devfreq instance.
518  *
519  * Helper function to resume devfreq device load monitoring. Function
520  * to be called from governor in response to DEVFREQ_GOV_RESUME
521  * event or when polling interval is set to non-zero.
522  */
523 void devfreq_monitor_resume(struct devfreq *devfreq)
524 {
525 	unsigned long freq;
526 
527 	mutex_lock(&devfreq->lock);
528 	if (!devfreq->stop_polling)
529 		goto out;
530 
531 	if (devfreq->governor->interrupt_driven)
532 		goto out_update;
533 
534 	if (!delayed_work_pending(&devfreq->work) &&
535 			devfreq->profile->polling_ms)
536 		queue_delayed_work(devfreq_wq, &devfreq->work,
537 			msecs_to_jiffies(devfreq->profile->polling_ms));
538 
539 out_update:
540 	devfreq->stats.last_update = get_jiffies_64();
541 	devfreq->stop_polling = false;
542 
543 	if (devfreq->profile->get_cur_freq &&
544 		!devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
545 		devfreq->previous_freq = freq;
546 
547 out:
548 	mutex_unlock(&devfreq->lock);
549 }
550 EXPORT_SYMBOL(devfreq_monitor_resume);
551 
552 /**
553  * devfreq_update_interval() - Update device devfreq monitoring interval
554  * @devfreq:    the devfreq instance.
555  * @delay:      new polling interval to be set.
556  *
557  * Helper function to set new load monitoring polling interval. Function
558  * to be called from governor in response to DEVFREQ_GOV_UPDATE_INTERVAL event.
559  */
560 void devfreq_update_interval(struct devfreq *devfreq, unsigned int *delay)
561 {
562 	unsigned int cur_delay = devfreq->profile->polling_ms;
563 	unsigned int new_delay = *delay;
564 
565 	mutex_lock(&devfreq->lock);
566 	devfreq->profile->polling_ms = new_delay;
567 
568 	if (devfreq->stop_polling)
569 		goto out;
570 
571 	if (devfreq->governor->interrupt_driven)
572 		goto out;
573 
574 	/* if new delay is zero, stop polling */
575 	if (!new_delay) {
576 		mutex_unlock(&devfreq->lock);
577 		cancel_delayed_work_sync(&devfreq->work);
578 		return;
579 	}
580 
581 	/* if current delay is zero, start polling with new delay */
582 	if (!cur_delay) {
583 		queue_delayed_work(devfreq_wq, &devfreq->work,
584 			msecs_to_jiffies(devfreq->profile->polling_ms));
585 		goto out;
586 	}
587 
588 	/* if current delay is greater than new delay, restart polling */
589 	if (cur_delay > new_delay) {
590 		mutex_unlock(&devfreq->lock);
591 		cancel_delayed_work_sync(&devfreq->work);
592 		mutex_lock(&devfreq->lock);
593 		if (!devfreq->stop_polling)
594 			queue_delayed_work(devfreq_wq, &devfreq->work,
595 				msecs_to_jiffies(devfreq->profile->polling_ms));
596 	}
597 out:
598 	mutex_unlock(&devfreq->lock);
599 }
600 EXPORT_SYMBOL(devfreq_update_interval);
601 
602 /**
603  * devfreq_notifier_call() - Notify that the device frequency requirements
604  *			     has been changed out of devfreq framework.
605  * @nb:		the notifier_block (supposed to be devfreq->nb)
606  * @type:	not used
607  * @devp:	not used
608  *
609  * Called by a notifier that uses devfreq->nb.
610  */
611 static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type,
612 				 void *devp)
613 {
614 	struct devfreq *devfreq = container_of(nb, struct devfreq, nb);
615 	int err = -EINVAL;
616 
617 	mutex_lock(&devfreq->lock);
618 
619 	devfreq->scaling_min_freq = find_available_min_freq(devfreq);
620 	if (!devfreq->scaling_min_freq)
621 		goto out;
622 
623 	devfreq->scaling_max_freq = find_available_max_freq(devfreq);
624 	if (!devfreq->scaling_max_freq) {
625 		devfreq->scaling_max_freq = ULONG_MAX;
626 		goto out;
627 	}
628 
629 	err = update_devfreq(devfreq);
630 
631 out:
632 	mutex_unlock(&devfreq->lock);
633 	if (err)
634 		dev_err(devfreq->dev.parent,
635 			"failed to update frequency from OPP notifier (%d)\n",
636 			err);
637 
638 	return NOTIFY_OK;
639 }
640 
641 /**
642  * qos_notifier_call() - Common handler for QoS constraints.
643  * @devfreq:    the devfreq instance.
644  */
645 static int qos_notifier_call(struct devfreq *devfreq)
646 {
647 	int err;
648 
649 	mutex_lock(&devfreq->lock);
650 	err = update_devfreq(devfreq);
651 	mutex_unlock(&devfreq->lock);
652 	if (err)
653 		dev_err(devfreq->dev.parent,
654 			"failed to update frequency from PM QoS (%d)\n",
655 			err);
656 
657 	return NOTIFY_OK;
658 }
659 
660 /**
661  * qos_min_notifier_call() - Callback for QoS min_freq changes.
662  * @nb:		Should be devfreq->nb_min
663  */
664 static int qos_min_notifier_call(struct notifier_block *nb,
665 					 unsigned long val, void *ptr)
666 {
667 	return qos_notifier_call(container_of(nb, struct devfreq, nb_min));
668 }
669 
670 /**
671  * qos_max_notifier_call() - Callback for QoS max_freq changes.
672  * @nb:		Should be devfreq->nb_max
673  */
674 static int qos_max_notifier_call(struct notifier_block *nb,
675 					 unsigned long val, void *ptr)
676 {
677 	return qos_notifier_call(container_of(nb, struct devfreq, nb_max));
678 }
679 
680 /**
681  * devfreq_dev_release() - Callback for struct device to release the device.
682  * @dev:	the devfreq device
683  *
684  * Remove devfreq from the list and release its resources.
685  */
686 static void devfreq_dev_release(struct device *dev)
687 {
688 	struct devfreq *devfreq = to_devfreq(dev);
689 	int err;
690 
691 	mutex_lock(&devfreq_list_lock);
692 	list_del(&devfreq->node);
693 	mutex_unlock(&devfreq_list_lock);
694 
695 	err = dev_pm_qos_remove_notifier(devfreq->dev.parent, &devfreq->nb_max,
696 					 DEV_PM_QOS_MAX_FREQUENCY);
697 	if (err && err != -ENOENT)
698 		dev_warn(dev->parent,
699 			"Failed to remove max_freq notifier: %d\n", err);
700 	err = dev_pm_qos_remove_notifier(devfreq->dev.parent, &devfreq->nb_min,
701 					 DEV_PM_QOS_MIN_FREQUENCY);
702 	if (err && err != -ENOENT)
703 		dev_warn(dev->parent,
704 			"Failed to remove min_freq notifier: %d\n", err);
705 
706 	if (dev_pm_qos_request_active(&devfreq->user_max_freq_req)) {
707 		err = dev_pm_qos_remove_request(&devfreq->user_max_freq_req);
708 		if (err < 0)
709 			dev_warn(dev->parent,
710 				"Failed to remove max_freq request: %d\n", err);
711 	}
712 	if (dev_pm_qos_request_active(&devfreq->user_min_freq_req)) {
713 		err = dev_pm_qos_remove_request(&devfreq->user_min_freq_req);
714 		if (err < 0)
715 			dev_warn(dev->parent,
716 				"Failed to remove min_freq request: %d\n", err);
717 	}
718 
719 	if (devfreq->profile->exit)
720 		devfreq->profile->exit(devfreq->dev.parent);
721 
722 	mutex_destroy(&devfreq->lock);
723 	kfree(devfreq);
724 }
725 
726 /**
727  * devfreq_add_device() - Add devfreq feature to the device
728  * @dev:	the device to add devfreq feature.
729  * @profile:	device-specific profile to run devfreq.
730  * @governor_name:	name of the policy to choose frequency.
731  * @data:	private data for the governor. The devfreq framework does not
732  *		touch this value.
733  */
734 struct devfreq *devfreq_add_device(struct device *dev,
735 				   struct devfreq_dev_profile *profile,
736 				   const char *governor_name,
737 				   void *data)
738 {
739 	struct devfreq *devfreq;
740 	struct devfreq_governor *governor;
741 	int err = 0;
742 
743 	if (!dev || !profile || !governor_name) {
744 		dev_err(dev, "%s: Invalid parameters.\n", __func__);
745 		return ERR_PTR(-EINVAL);
746 	}
747 
748 	mutex_lock(&devfreq_list_lock);
749 	devfreq = find_device_devfreq(dev);
750 	mutex_unlock(&devfreq_list_lock);
751 	if (!IS_ERR(devfreq)) {
752 		dev_err(dev, "%s: devfreq device already exists!\n",
753 			__func__);
754 		err = -EINVAL;
755 		goto err_out;
756 	}
757 
758 	devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL);
759 	if (!devfreq) {
760 		err = -ENOMEM;
761 		goto err_out;
762 	}
763 
764 	mutex_init(&devfreq->lock);
765 	mutex_lock(&devfreq->lock);
766 	devfreq->dev.parent = dev;
767 	devfreq->dev.class = devfreq_class;
768 	devfreq->dev.release = devfreq_dev_release;
769 	INIT_LIST_HEAD(&devfreq->node);
770 	devfreq->profile = profile;
771 	strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN);
772 	devfreq->previous_freq = profile->initial_freq;
773 	devfreq->last_status.current_frequency = profile->initial_freq;
774 	devfreq->data = data;
775 	devfreq->nb.notifier_call = devfreq_notifier_call;
776 
777 	if (!devfreq->profile->max_state && !devfreq->profile->freq_table) {
778 		mutex_unlock(&devfreq->lock);
779 		err = set_freq_table(devfreq);
780 		if (err < 0)
781 			goto err_dev;
782 		mutex_lock(&devfreq->lock);
783 	}
784 
785 	devfreq->scaling_min_freq = find_available_min_freq(devfreq);
786 	if (!devfreq->scaling_min_freq) {
787 		mutex_unlock(&devfreq->lock);
788 		err = -EINVAL;
789 		goto err_dev;
790 	}
791 
792 	devfreq->scaling_max_freq = find_available_max_freq(devfreq);
793 	if (!devfreq->scaling_max_freq) {
794 		mutex_unlock(&devfreq->lock);
795 		err = -EINVAL;
796 		goto err_dev;
797 	}
798 
799 	devfreq->suspend_freq = dev_pm_opp_get_suspend_opp_freq(dev);
800 	atomic_set(&devfreq->suspend_count, 0);
801 
802 	dev_set_name(&devfreq->dev, "%s", dev_name(dev));
803 	err = device_register(&devfreq->dev);
804 	if (err) {
805 		mutex_unlock(&devfreq->lock);
806 		put_device(&devfreq->dev);
807 		goto err_out;
808 	}
809 
810 	devfreq->stats.trans_table = devm_kzalloc(&devfreq->dev,
811 			array3_size(sizeof(unsigned int),
812 				    devfreq->profile->max_state,
813 				    devfreq->profile->max_state),
814 			GFP_KERNEL);
815 	if (!devfreq->stats.trans_table) {
816 		mutex_unlock(&devfreq->lock);
817 		err = -ENOMEM;
818 		goto err_devfreq;
819 	}
820 
821 	devfreq->stats.time_in_state = devm_kcalloc(&devfreq->dev,
822 			devfreq->profile->max_state,
823 			sizeof(*devfreq->stats.time_in_state),
824 			GFP_KERNEL);
825 	if (!devfreq->stats.time_in_state) {
826 		mutex_unlock(&devfreq->lock);
827 		err = -ENOMEM;
828 		goto err_devfreq;
829 	}
830 
831 	devfreq->stats.total_trans = 0;
832 	devfreq->stats.last_update = get_jiffies_64();
833 
834 	srcu_init_notifier_head(&devfreq->transition_notifier_list);
835 
836 	mutex_unlock(&devfreq->lock);
837 
838 	err = dev_pm_qos_add_request(dev, &devfreq->user_min_freq_req,
839 				     DEV_PM_QOS_MIN_FREQUENCY, 0);
840 	if (err < 0)
841 		goto err_devfreq;
842 	err = dev_pm_qos_add_request(dev, &devfreq->user_max_freq_req,
843 				     DEV_PM_QOS_MAX_FREQUENCY,
844 				     PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE);
845 	if (err < 0)
846 		goto err_devfreq;
847 
848 	devfreq->nb_min.notifier_call = qos_min_notifier_call;
849 	err = dev_pm_qos_add_notifier(devfreq->dev.parent, &devfreq->nb_min,
850 				      DEV_PM_QOS_MIN_FREQUENCY);
851 	if (err)
852 		goto err_devfreq;
853 
854 	devfreq->nb_max.notifier_call = qos_max_notifier_call;
855 	err = dev_pm_qos_add_notifier(devfreq->dev.parent, &devfreq->nb_max,
856 				      DEV_PM_QOS_MAX_FREQUENCY);
857 	if (err)
858 		goto err_devfreq;
859 
860 	mutex_lock(&devfreq_list_lock);
861 
862 	governor = try_then_request_governor(devfreq->governor_name);
863 	if (IS_ERR(governor)) {
864 		dev_err(dev, "%s: Unable to find governor for the device\n",
865 			__func__);
866 		err = PTR_ERR(governor);
867 		goto err_init;
868 	}
869 
870 	devfreq->governor = governor;
871 	err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START,
872 						NULL);
873 	if (err) {
874 		dev_err(dev, "%s: Unable to start governor for the device\n",
875 			__func__);
876 		goto err_init;
877 	}
878 
879 	list_add(&devfreq->node, &devfreq_list);
880 
881 	mutex_unlock(&devfreq_list_lock);
882 
883 	return devfreq;
884 
885 err_init:
886 	mutex_unlock(&devfreq_list_lock);
887 err_devfreq:
888 	devfreq_remove_device(devfreq);
889 	devfreq = NULL;
890 err_dev:
891 	kfree(devfreq);
892 err_out:
893 	return ERR_PTR(err);
894 }
895 EXPORT_SYMBOL(devfreq_add_device);
896 
897 /**
898  * devfreq_remove_device() - Remove devfreq feature from a device.
899  * @devfreq:	the devfreq instance to be removed
900  *
901  * The opposite of devfreq_add_device().
902  */
903 int devfreq_remove_device(struct devfreq *devfreq)
904 {
905 	if (!devfreq)
906 		return -EINVAL;
907 
908 	if (devfreq->governor)
909 		devfreq->governor->event_handler(devfreq,
910 						 DEVFREQ_GOV_STOP, NULL);
911 	device_unregister(&devfreq->dev);
912 
913 	return 0;
914 }
915 EXPORT_SYMBOL(devfreq_remove_device);
916 
917 static int devm_devfreq_dev_match(struct device *dev, void *res, void *data)
918 {
919 	struct devfreq **r = res;
920 
921 	if (WARN_ON(!r || !*r))
922 		return 0;
923 
924 	return *r == data;
925 }
926 
927 static void devm_devfreq_dev_release(struct device *dev, void *res)
928 {
929 	devfreq_remove_device(*(struct devfreq **)res);
930 }
931 
932 /**
933  * devm_devfreq_add_device() - Resource-managed devfreq_add_device()
934  * @dev:	the device to add devfreq feature.
935  * @profile:	device-specific profile to run devfreq.
936  * @governor_name:	name of the policy to choose frequency.
937  * @data:	private data for the governor. The devfreq framework does not
938  *		touch this value.
939  *
940  * This function manages automatically the memory of devfreq device using device
941  * resource management and simplify the free operation for memory of devfreq
942  * device.
943  */
944 struct devfreq *devm_devfreq_add_device(struct device *dev,
945 					struct devfreq_dev_profile *profile,
946 					const char *governor_name,
947 					void *data)
948 {
949 	struct devfreq **ptr, *devfreq;
950 
951 	ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL);
952 	if (!ptr)
953 		return ERR_PTR(-ENOMEM);
954 
955 	devfreq = devfreq_add_device(dev, profile, governor_name, data);
956 	if (IS_ERR(devfreq)) {
957 		devres_free(ptr);
958 		return devfreq;
959 	}
960 
961 	*ptr = devfreq;
962 	devres_add(dev, ptr);
963 
964 	return devfreq;
965 }
966 EXPORT_SYMBOL(devm_devfreq_add_device);
967 
968 #ifdef CONFIG_OF
969 /*
970  * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree
971  * @dev - instance to the given device
972  * @index - index into list of devfreq
973  *
974  * return the instance of devfreq device
975  */
976 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
977 {
978 	struct device_node *node;
979 	struct devfreq *devfreq;
980 
981 	if (!dev)
982 		return ERR_PTR(-EINVAL);
983 
984 	if (!dev->of_node)
985 		return ERR_PTR(-EINVAL);
986 
987 	node = of_parse_phandle(dev->of_node, "devfreq", index);
988 	if (!node)
989 		return ERR_PTR(-ENODEV);
990 
991 	mutex_lock(&devfreq_list_lock);
992 	list_for_each_entry(devfreq, &devfreq_list, node) {
993 		if (devfreq->dev.parent
994 			&& devfreq->dev.parent->of_node == node) {
995 			mutex_unlock(&devfreq_list_lock);
996 			of_node_put(node);
997 			return devfreq;
998 		}
999 	}
1000 	mutex_unlock(&devfreq_list_lock);
1001 	of_node_put(node);
1002 
1003 	return ERR_PTR(-EPROBE_DEFER);
1004 }
1005 #else
1006 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index)
1007 {
1008 	return ERR_PTR(-ENODEV);
1009 }
1010 #endif /* CONFIG_OF */
1011 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle);
1012 
1013 /**
1014  * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device()
1015  * @dev:	the device from which to remove devfreq feature.
1016  * @devfreq:	the devfreq instance to be removed
1017  */
1018 void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq)
1019 {
1020 	WARN_ON(devres_release(dev, devm_devfreq_dev_release,
1021 			       devm_devfreq_dev_match, devfreq));
1022 }
1023 EXPORT_SYMBOL(devm_devfreq_remove_device);
1024 
1025 /**
1026  * devfreq_suspend_device() - Suspend devfreq of a device.
1027  * @devfreq: the devfreq instance to be suspended
1028  *
1029  * This function is intended to be called by the pm callbacks
1030  * (e.g., runtime_suspend, suspend) of the device driver that
1031  * holds the devfreq.
1032  */
1033 int devfreq_suspend_device(struct devfreq *devfreq)
1034 {
1035 	int ret;
1036 
1037 	if (!devfreq)
1038 		return -EINVAL;
1039 
1040 	if (atomic_inc_return(&devfreq->suspend_count) > 1)
1041 		return 0;
1042 
1043 	if (devfreq->governor) {
1044 		ret = devfreq->governor->event_handler(devfreq,
1045 					DEVFREQ_GOV_SUSPEND, NULL);
1046 		if (ret)
1047 			return ret;
1048 	}
1049 
1050 	if (devfreq->suspend_freq) {
1051 		mutex_lock(&devfreq->lock);
1052 		ret = devfreq_set_target(devfreq, devfreq->suspend_freq, 0);
1053 		mutex_unlock(&devfreq->lock);
1054 		if (ret)
1055 			return ret;
1056 	}
1057 
1058 	return 0;
1059 }
1060 EXPORT_SYMBOL(devfreq_suspend_device);
1061 
1062 /**
1063  * devfreq_resume_device() - Resume devfreq of a device.
1064  * @devfreq: the devfreq instance to be resumed
1065  *
1066  * This function is intended to be called by the pm callbacks
1067  * (e.g., runtime_resume, resume) of the device driver that
1068  * holds the devfreq.
1069  */
1070 int devfreq_resume_device(struct devfreq *devfreq)
1071 {
1072 	int ret;
1073 
1074 	if (!devfreq)
1075 		return -EINVAL;
1076 
1077 	if (atomic_dec_return(&devfreq->suspend_count) >= 1)
1078 		return 0;
1079 
1080 	if (devfreq->resume_freq) {
1081 		mutex_lock(&devfreq->lock);
1082 		ret = devfreq_set_target(devfreq, devfreq->resume_freq, 0);
1083 		mutex_unlock(&devfreq->lock);
1084 		if (ret)
1085 			return ret;
1086 	}
1087 
1088 	if (devfreq->governor) {
1089 		ret = devfreq->governor->event_handler(devfreq,
1090 					DEVFREQ_GOV_RESUME, NULL);
1091 		if (ret)
1092 			return ret;
1093 	}
1094 
1095 	return 0;
1096 }
1097 EXPORT_SYMBOL(devfreq_resume_device);
1098 
1099 /**
1100  * devfreq_suspend() - Suspend devfreq governors and devices
1101  *
1102  * Called during system wide Suspend/Hibernate cycles for suspending governors
1103  * and devices preserving the state for resume. On some platforms the devfreq
1104  * device must have precise state (frequency) after resume in order to provide
1105  * fully operating setup.
1106  */
1107 void devfreq_suspend(void)
1108 {
1109 	struct devfreq *devfreq;
1110 	int ret;
1111 
1112 	mutex_lock(&devfreq_list_lock);
1113 	list_for_each_entry(devfreq, &devfreq_list, node) {
1114 		ret = devfreq_suspend_device(devfreq);
1115 		if (ret)
1116 			dev_err(&devfreq->dev,
1117 				"failed to suspend devfreq device\n");
1118 	}
1119 	mutex_unlock(&devfreq_list_lock);
1120 }
1121 
1122 /**
1123  * devfreq_resume() - Resume devfreq governors and devices
1124  *
1125  * Called during system wide Suspend/Hibernate cycle for resuming governors and
1126  * devices that are suspended with devfreq_suspend().
1127  */
1128 void devfreq_resume(void)
1129 {
1130 	struct devfreq *devfreq;
1131 	int ret;
1132 
1133 	mutex_lock(&devfreq_list_lock);
1134 	list_for_each_entry(devfreq, &devfreq_list, node) {
1135 		ret = devfreq_resume_device(devfreq);
1136 		if (ret)
1137 			dev_warn(&devfreq->dev,
1138 				 "failed to resume devfreq device\n");
1139 	}
1140 	mutex_unlock(&devfreq_list_lock);
1141 }
1142 
1143 /**
1144  * devfreq_add_governor() - Add devfreq governor
1145  * @governor:	the devfreq governor to be added
1146  */
1147 int devfreq_add_governor(struct devfreq_governor *governor)
1148 {
1149 	struct devfreq_governor *g;
1150 	struct devfreq *devfreq;
1151 	int err = 0;
1152 
1153 	if (!governor) {
1154 		pr_err("%s: Invalid parameters.\n", __func__);
1155 		return -EINVAL;
1156 	}
1157 
1158 	mutex_lock(&devfreq_list_lock);
1159 	g = find_devfreq_governor(governor->name);
1160 	if (!IS_ERR(g)) {
1161 		pr_err("%s: governor %s already registered\n", __func__,
1162 		       g->name);
1163 		err = -EINVAL;
1164 		goto err_out;
1165 	}
1166 
1167 	list_add(&governor->node, &devfreq_governor_list);
1168 
1169 	list_for_each_entry(devfreq, &devfreq_list, node) {
1170 		int ret = 0;
1171 		struct device *dev = devfreq->dev.parent;
1172 
1173 		if (!strncmp(devfreq->governor_name, governor->name,
1174 			     DEVFREQ_NAME_LEN)) {
1175 			/* The following should never occur */
1176 			if (devfreq->governor) {
1177 				dev_warn(dev,
1178 					 "%s: Governor %s already present\n",
1179 					 __func__, devfreq->governor->name);
1180 				ret = devfreq->governor->event_handler(devfreq,
1181 							DEVFREQ_GOV_STOP, NULL);
1182 				if (ret) {
1183 					dev_warn(dev,
1184 						 "%s: Governor %s stop = %d\n",
1185 						 __func__,
1186 						 devfreq->governor->name, ret);
1187 				}
1188 				/* Fall through */
1189 			}
1190 			devfreq->governor = governor;
1191 			ret = devfreq->governor->event_handler(devfreq,
1192 						DEVFREQ_GOV_START, NULL);
1193 			if (ret) {
1194 				dev_warn(dev, "%s: Governor %s start=%d\n",
1195 					 __func__, devfreq->governor->name,
1196 					 ret);
1197 			}
1198 		}
1199 	}
1200 
1201 err_out:
1202 	mutex_unlock(&devfreq_list_lock);
1203 
1204 	return err;
1205 }
1206 EXPORT_SYMBOL(devfreq_add_governor);
1207 
1208 /**
1209  * devfreq_remove_governor() - Remove devfreq feature from a device.
1210  * @governor:	the devfreq governor to be removed
1211  */
1212 int devfreq_remove_governor(struct devfreq_governor *governor)
1213 {
1214 	struct devfreq_governor *g;
1215 	struct devfreq *devfreq;
1216 	int err = 0;
1217 
1218 	if (!governor) {
1219 		pr_err("%s: Invalid parameters.\n", __func__);
1220 		return -EINVAL;
1221 	}
1222 
1223 	mutex_lock(&devfreq_list_lock);
1224 	g = find_devfreq_governor(governor->name);
1225 	if (IS_ERR(g)) {
1226 		pr_err("%s: governor %s not registered\n", __func__,
1227 		       governor->name);
1228 		err = PTR_ERR(g);
1229 		goto err_out;
1230 	}
1231 	list_for_each_entry(devfreq, &devfreq_list, node) {
1232 		int ret;
1233 		struct device *dev = devfreq->dev.parent;
1234 
1235 		if (!strncmp(devfreq->governor_name, governor->name,
1236 			     DEVFREQ_NAME_LEN)) {
1237 			/* we should have a devfreq governor! */
1238 			if (!devfreq->governor) {
1239 				dev_warn(dev, "%s: Governor %s NOT present\n",
1240 					 __func__, governor->name);
1241 				continue;
1242 				/* Fall through */
1243 			}
1244 			ret = devfreq->governor->event_handler(devfreq,
1245 						DEVFREQ_GOV_STOP, NULL);
1246 			if (ret) {
1247 				dev_warn(dev, "%s: Governor %s stop=%d\n",
1248 					 __func__, devfreq->governor->name,
1249 					 ret);
1250 			}
1251 			devfreq->governor = NULL;
1252 		}
1253 	}
1254 
1255 	list_del(&governor->node);
1256 err_out:
1257 	mutex_unlock(&devfreq_list_lock);
1258 
1259 	return err;
1260 }
1261 EXPORT_SYMBOL(devfreq_remove_governor);
1262 
1263 static ssize_t name_show(struct device *dev,
1264 			struct device_attribute *attr, char *buf)
1265 {
1266 	struct devfreq *devfreq = to_devfreq(dev);
1267 	return sprintf(buf, "%s\n", dev_name(devfreq->dev.parent));
1268 }
1269 static DEVICE_ATTR_RO(name);
1270 
1271 static ssize_t governor_show(struct device *dev,
1272 			     struct device_attribute *attr, char *buf)
1273 {
1274 	if (!to_devfreq(dev)->governor)
1275 		return -EINVAL;
1276 
1277 	return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name);
1278 }
1279 
1280 static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
1281 			      const char *buf, size_t count)
1282 {
1283 	struct devfreq *df = to_devfreq(dev);
1284 	int ret;
1285 	char str_governor[DEVFREQ_NAME_LEN + 1];
1286 	const struct devfreq_governor *governor, *prev_governor;
1287 
1288 	ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor);
1289 	if (ret != 1)
1290 		return -EINVAL;
1291 
1292 	mutex_lock(&devfreq_list_lock);
1293 	governor = try_then_request_governor(str_governor);
1294 	if (IS_ERR(governor)) {
1295 		ret = PTR_ERR(governor);
1296 		goto out;
1297 	}
1298 	if (df->governor == governor) {
1299 		ret = 0;
1300 		goto out;
1301 	} else if ((df->governor && df->governor->immutable) ||
1302 					governor->immutable) {
1303 		ret = -EINVAL;
1304 		goto out;
1305 	}
1306 
1307 	if (df->governor) {
1308 		ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL);
1309 		if (ret) {
1310 			dev_warn(dev, "%s: Governor %s not stopped(%d)\n",
1311 				 __func__, df->governor->name, ret);
1312 			goto out;
1313 		}
1314 	}
1315 	prev_governor = df->governor;
1316 	df->governor = governor;
1317 	strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN);
1318 	ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1319 	if (ret) {
1320 		dev_warn(dev, "%s: Governor %s not started(%d)\n",
1321 			 __func__, df->governor->name, ret);
1322 		df->governor = prev_governor;
1323 		strncpy(df->governor_name, prev_governor->name,
1324 			DEVFREQ_NAME_LEN);
1325 		ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL);
1326 		if (ret) {
1327 			dev_err(dev,
1328 				"%s: reverting to Governor %s failed (%d)\n",
1329 				__func__, df->governor_name, ret);
1330 			df->governor = NULL;
1331 		}
1332 	}
1333 out:
1334 	mutex_unlock(&devfreq_list_lock);
1335 
1336 	if (!ret)
1337 		ret = count;
1338 	return ret;
1339 }
1340 static DEVICE_ATTR_RW(governor);
1341 
1342 static ssize_t available_governors_show(struct device *d,
1343 					struct device_attribute *attr,
1344 					char *buf)
1345 {
1346 	struct devfreq *df = to_devfreq(d);
1347 	ssize_t count = 0;
1348 
1349 	mutex_lock(&devfreq_list_lock);
1350 
1351 	/*
1352 	 * The devfreq with immutable governor (e.g., passive) shows
1353 	 * only own governor.
1354 	 */
1355 	if (df->governor && df->governor->immutable) {
1356 		count = scnprintf(&buf[count], DEVFREQ_NAME_LEN,
1357 				  "%s ", df->governor_name);
1358 	/*
1359 	 * The devfreq device shows the registered governor except for
1360 	 * immutable governors such as passive governor .
1361 	 */
1362 	} else {
1363 		struct devfreq_governor *governor;
1364 
1365 		list_for_each_entry(governor, &devfreq_governor_list, node) {
1366 			if (governor->immutable)
1367 				continue;
1368 			count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1369 					   "%s ", governor->name);
1370 		}
1371 	}
1372 
1373 	mutex_unlock(&devfreq_list_lock);
1374 
1375 	/* Truncate the trailing space */
1376 	if (count)
1377 		count--;
1378 
1379 	count += sprintf(&buf[count], "\n");
1380 
1381 	return count;
1382 }
1383 static DEVICE_ATTR_RO(available_governors);
1384 
1385 static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr,
1386 			     char *buf)
1387 {
1388 	unsigned long freq;
1389 	struct devfreq *devfreq = to_devfreq(dev);
1390 
1391 	if (devfreq->profile->get_cur_freq &&
1392 		!devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq))
1393 		return sprintf(buf, "%lu\n", freq);
1394 
1395 	return sprintf(buf, "%lu\n", devfreq->previous_freq);
1396 }
1397 static DEVICE_ATTR_RO(cur_freq);
1398 
1399 static ssize_t target_freq_show(struct device *dev,
1400 				struct device_attribute *attr, char *buf)
1401 {
1402 	return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq);
1403 }
1404 static DEVICE_ATTR_RO(target_freq);
1405 
1406 static ssize_t polling_interval_show(struct device *dev,
1407 				     struct device_attribute *attr, char *buf)
1408 {
1409 	return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms);
1410 }
1411 
1412 static ssize_t polling_interval_store(struct device *dev,
1413 				      struct device_attribute *attr,
1414 				      const char *buf, size_t count)
1415 {
1416 	struct devfreq *df = to_devfreq(dev);
1417 	unsigned int value;
1418 	int ret;
1419 
1420 	if (!df->governor)
1421 		return -EINVAL;
1422 
1423 	ret = sscanf(buf, "%u", &value);
1424 	if (ret != 1)
1425 		return -EINVAL;
1426 
1427 	df->governor->event_handler(df, DEVFREQ_GOV_UPDATE_INTERVAL, &value);
1428 	ret = count;
1429 
1430 	return ret;
1431 }
1432 static DEVICE_ATTR_RW(polling_interval);
1433 
1434 static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr,
1435 			      const char *buf, size_t count)
1436 {
1437 	struct devfreq *df = to_devfreq(dev);
1438 	unsigned long value;
1439 	int ret;
1440 
1441 	/*
1442 	 * Protect against theoretical sysfs writes between
1443 	 * device_add and dev_pm_qos_add_request
1444 	 */
1445 	if (!dev_pm_qos_request_active(&df->user_min_freq_req))
1446 		return -EAGAIN;
1447 
1448 	ret = sscanf(buf, "%lu", &value);
1449 	if (ret != 1)
1450 		return -EINVAL;
1451 
1452 	/* Round down to kHz for PM QoS */
1453 	ret = dev_pm_qos_update_request(&df->user_min_freq_req,
1454 					value / HZ_PER_KHZ);
1455 	if (ret < 0)
1456 		return ret;
1457 
1458 	return count;
1459 }
1460 
1461 static ssize_t min_freq_show(struct device *dev, struct device_attribute *attr,
1462 			     char *buf)
1463 {
1464 	struct devfreq *df = to_devfreq(dev);
1465 	unsigned long min_freq, max_freq;
1466 
1467 	mutex_lock(&df->lock);
1468 	get_freq_range(df, &min_freq, &max_freq);
1469 	mutex_unlock(&df->lock);
1470 
1471 	return sprintf(buf, "%lu\n", min_freq);
1472 }
1473 static DEVICE_ATTR_RW(min_freq);
1474 
1475 static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr,
1476 			      const char *buf, size_t count)
1477 {
1478 	struct devfreq *df = to_devfreq(dev);
1479 	unsigned long value;
1480 	int ret;
1481 
1482 	/*
1483 	 * Protect against theoretical sysfs writes between
1484 	 * device_add and dev_pm_qos_add_request
1485 	 */
1486 	if (!dev_pm_qos_request_active(&df->user_max_freq_req))
1487 		return -EINVAL;
1488 
1489 	ret = sscanf(buf, "%lu", &value);
1490 	if (ret != 1)
1491 		return -EINVAL;
1492 
1493 	/*
1494 	 * PM QoS frequencies are in kHz so we need to convert. Convert by
1495 	 * rounding upwards so that the acceptable interval never shrinks.
1496 	 *
1497 	 * For example if the user writes "666666666" to sysfs this value will
1498 	 * be converted to 666667 kHz and back to 666667000 Hz before an OPP
1499 	 * lookup, this ensures that an OPP of 666666666Hz is still accepted.
1500 	 *
1501 	 * A value of zero means "no limit".
1502 	 */
1503 	if (value)
1504 		value = DIV_ROUND_UP(value, HZ_PER_KHZ);
1505 	else
1506 		value = PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE;
1507 
1508 	ret = dev_pm_qos_update_request(&df->user_max_freq_req, value);
1509 	if (ret < 0)
1510 		return ret;
1511 
1512 	return count;
1513 }
1514 
1515 static ssize_t max_freq_show(struct device *dev, struct device_attribute *attr,
1516 			     char *buf)
1517 {
1518 	struct devfreq *df = to_devfreq(dev);
1519 	unsigned long min_freq, max_freq;
1520 
1521 	mutex_lock(&df->lock);
1522 	get_freq_range(df, &min_freq, &max_freq);
1523 	mutex_unlock(&df->lock);
1524 
1525 	return sprintf(buf, "%lu\n", max_freq);
1526 }
1527 static DEVICE_ATTR_RW(max_freq);
1528 
1529 static ssize_t available_frequencies_show(struct device *d,
1530 					  struct device_attribute *attr,
1531 					  char *buf)
1532 {
1533 	struct devfreq *df = to_devfreq(d);
1534 	ssize_t count = 0;
1535 	int i;
1536 
1537 	mutex_lock(&df->lock);
1538 
1539 	for (i = 0; i < df->profile->max_state; i++)
1540 		count += scnprintf(&buf[count], (PAGE_SIZE - count - 2),
1541 				"%lu ", df->profile->freq_table[i]);
1542 
1543 	mutex_unlock(&df->lock);
1544 	/* Truncate the trailing space */
1545 	if (count)
1546 		count--;
1547 
1548 	count += sprintf(&buf[count], "\n");
1549 
1550 	return count;
1551 }
1552 static DEVICE_ATTR_RO(available_frequencies);
1553 
1554 static ssize_t trans_stat_show(struct device *dev,
1555 			       struct device_attribute *attr, char *buf)
1556 {
1557 	struct devfreq *devfreq = to_devfreq(dev);
1558 	ssize_t len;
1559 	int i, j;
1560 	unsigned int max_state = devfreq->profile->max_state;
1561 
1562 	if (max_state == 0)
1563 		return sprintf(buf, "Not Supported.\n");
1564 
1565 	mutex_lock(&devfreq->lock);
1566 	if (!devfreq->stop_polling &&
1567 			devfreq_update_status(devfreq, devfreq->previous_freq)) {
1568 		mutex_unlock(&devfreq->lock);
1569 		return 0;
1570 	}
1571 	mutex_unlock(&devfreq->lock);
1572 
1573 	len = sprintf(buf, "     From  :   To\n");
1574 	len += sprintf(buf + len, "           :");
1575 	for (i = 0; i < max_state; i++)
1576 		len += sprintf(buf + len, "%10lu",
1577 				devfreq->profile->freq_table[i]);
1578 
1579 	len += sprintf(buf + len, "   time(ms)\n");
1580 
1581 	for (i = 0; i < max_state; i++) {
1582 		if (devfreq->profile->freq_table[i]
1583 					== devfreq->previous_freq) {
1584 			len += sprintf(buf + len, "*");
1585 		} else {
1586 			len += sprintf(buf + len, " ");
1587 		}
1588 		len += sprintf(buf + len, "%10lu:",
1589 				devfreq->profile->freq_table[i]);
1590 		for (j = 0; j < max_state; j++)
1591 			len += sprintf(buf + len, "%10u",
1592 				devfreq->stats.trans_table[(i * max_state) + j]);
1593 
1594 		len += sprintf(buf + len, "%10llu\n", (u64)
1595 			jiffies64_to_msecs(devfreq->stats.time_in_state[i]));
1596 	}
1597 
1598 	len += sprintf(buf + len, "Total transition : %u\n",
1599 					devfreq->stats.total_trans);
1600 	return len;
1601 }
1602 
1603 static ssize_t trans_stat_store(struct device *dev,
1604 				struct device_attribute *attr,
1605 				const char *buf, size_t count)
1606 {
1607 	struct devfreq *df = to_devfreq(dev);
1608 	int err, value;
1609 
1610 	if (df->profile->max_state == 0)
1611 		return count;
1612 
1613 	err = kstrtoint(buf, 10, &value);
1614 	if (err || value != 0)
1615 		return -EINVAL;
1616 
1617 	mutex_lock(&df->lock);
1618 	memset(df->stats.time_in_state, 0, (df->profile->max_state *
1619 					sizeof(*df->stats.time_in_state)));
1620 	memset(df->stats.trans_table, 0, array3_size(sizeof(unsigned int),
1621 					df->profile->max_state,
1622 					df->profile->max_state));
1623 	df->stats.total_trans = 0;
1624 	df->stats.last_update = get_jiffies_64();
1625 	mutex_unlock(&df->lock);
1626 
1627 	return count;
1628 }
1629 static DEVICE_ATTR_RW(trans_stat);
1630 
1631 static struct attribute *devfreq_attrs[] = {
1632 	&dev_attr_name.attr,
1633 	&dev_attr_governor.attr,
1634 	&dev_attr_available_governors.attr,
1635 	&dev_attr_cur_freq.attr,
1636 	&dev_attr_available_frequencies.attr,
1637 	&dev_attr_target_freq.attr,
1638 	&dev_attr_polling_interval.attr,
1639 	&dev_attr_min_freq.attr,
1640 	&dev_attr_max_freq.attr,
1641 	&dev_attr_trans_stat.attr,
1642 	NULL,
1643 };
1644 ATTRIBUTE_GROUPS(devfreq);
1645 
1646 /**
1647  * devfreq_summary_show() - Show the summary of the devfreq devices
1648  * @s:		seq_file instance to show the summary of devfreq devices
1649  * @data:	not used
1650  *
1651  * Show the summary of the devfreq devices via 'devfreq_summary' debugfs file.
1652  * It helps that user can know the detailed information of the devfreq devices.
1653  *
1654  * Return 0 always because it shows the information without any data change.
1655  */
1656 static int devfreq_summary_show(struct seq_file *s, void *data)
1657 {
1658 	struct devfreq *devfreq;
1659 	struct devfreq *p_devfreq = NULL;
1660 	unsigned long cur_freq, min_freq, max_freq;
1661 	unsigned int polling_ms;
1662 
1663 	seq_printf(s, "%-30s %-10s %-10s %-15s %10s %12s %12s %12s\n",
1664 			"dev_name",
1665 			"dev",
1666 			"parent_dev",
1667 			"governor",
1668 			"polling_ms",
1669 			"cur_freq_Hz",
1670 			"min_freq_Hz",
1671 			"max_freq_Hz");
1672 	seq_printf(s, "%30s %10s %10s %15s %10s %12s %12s %12s\n",
1673 			"------------------------------",
1674 			"----------",
1675 			"----------",
1676 			"---------------",
1677 			"----------",
1678 			"------------",
1679 			"------------",
1680 			"------------");
1681 
1682 	mutex_lock(&devfreq_list_lock);
1683 
1684 	list_for_each_entry_reverse(devfreq, &devfreq_list, node) {
1685 #if IS_ENABLED(CONFIG_DEVFREQ_GOV_PASSIVE)
1686 		if (!strncmp(devfreq->governor_name, DEVFREQ_GOV_PASSIVE,
1687 							DEVFREQ_NAME_LEN)) {
1688 			struct devfreq_passive_data *data = devfreq->data;
1689 
1690 			if (data)
1691 				p_devfreq = data->parent;
1692 		} else {
1693 			p_devfreq = NULL;
1694 		}
1695 #endif
1696 
1697 		mutex_lock(&devfreq->lock);
1698 		cur_freq = devfreq->previous_freq,
1699 		get_freq_range(devfreq, &min_freq, &max_freq);
1700 		polling_ms = devfreq->profile->polling_ms,
1701 		mutex_unlock(&devfreq->lock);
1702 
1703 		seq_printf(s,
1704 			"%-30s %-10s %-10s %-15s %10d %12ld %12ld %12ld\n",
1705 			dev_name(devfreq->dev.parent),
1706 			dev_name(&devfreq->dev),
1707 			p_devfreq ? dev_name(&p_devfreq->dev) : "null",
1708 			devfreq->governor_name,
1709 			polling_ms,
1710 			cur_freq,
1711 			min_freq,
1712 			max_freq);
1713 	}
1714 
1715 	mutex_unlock(&devfreq_list_lock);
1716 
1717 	return 0;
1718 }
1719 DEFINE_SHOW_ATTRIBUTE(devfreq_summary);
1720 
1721 static int __init devfreq_init(void)
1722 {
1723 	devfreq_class = class_create(THIS_MODULE, "devfreq");
1724 	if (IS_ERR(devfreq_class)) {
1725 		pr_err("%s: couldn't create class\n", __FILE__);
1726 		return PTR_ERR(devfreq_class);
1727 	}
1728 
1729 	devfreq_wq = create_freezable_workqueue("devfreq_wq");
1730 	if (!devfreq_wq) {
1731 		class_destroy(devfreq_class);
1732 		pr_err("%s: couldn't create workqueue\n", __FILE__);
1733 		return -ENOMEM;
1734 	}
1735 	devfreq_class->dev_groups = devfreq_groups;
1736 
1737 	devfreq_debugfs = debugfs_create_dir("devfreq", NULL);
1738 	debugfs_create_file("devfreq_summary", 0444,
1739 				devfreq_debugfs, NULL,
1740 				&devfreq_summary_fops);
1741 
1742 	return 0;
1743 }
1744 subsys_initcall(devfreq_init);
1745 
1746 /*
1747  * The following are helper functions for devfreq user device drivers with
1748  * OPP framework.
1749  */
1750 
1751 /**
1752  * devfreq_recommended_opp() - Helper function to get proper OPP for the
1753  *			     freq value given to target callback.
1754  * @dev:	The devfreq user device. (parent of devfreq)
1755  * @freq:	The frequency given to target function
1756  * @flags:	Flags handed from devfreq framework.
1757  *
1758  * The callers are required to call dev_pm_opp_put() for the returned OPP after
1759  * use.
1760  */
1761 struct dev_pm_opp *devfreq_recommended_opp(struct device *dev,
1762 					   unsigned long *freq,
1763 					   u32 flags)
1764 {
1765 	struct dev_pm_opp *opp;
1766 
1767 	if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) {
1768 		/* The freq is an upper bound. opp should be lower */
1769 		opp = dev_pm_opp_find_freq_floor(dev, freq);
1770 
1771 		/* If not available, use the closest opp */
1772 		if (opp == ERR_PTR(-ERANGE))
1773 			opp = dev_pm_opp_find_freq_ceil(dev, freq);
1774 	} else {
1775 		/* The freq is an lower bound. opp should be higher */
1776 		opp = dev_pm_opp_find_freq_ceil(dev, freq);
1777 
1778 		/* If not available, use the closest opp */
1779 		if (opp == ERR_PTR(-ERANGE))
1780 			opp = dev_pm_opp_find_freq_floor(dev, freq);
1781 	}
1782 
1783 	return opp;
1784 }
1785 EXPORT_SYMBOL(devfreq_recommended_opp);
1786 
1787 /**
1788  * devfreq_register_opp_notifier() - Helper function to get devfreq notified
1789  *				     for any changes in the OPP availability
1790  *				     changes
1791  * @dev:	The devfreq user device. (parent of devfreq)
1792  * @devfreq:	The devfreq object.
1793  */
1794 int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq)
1795 {
1796 	return dev_pm_opp_register_notifier(dev, &devfreq->nb);
1797 }
1798 EXPORT_SYMBOL(devfreq_register_opp_notifier);
1799 
1800 /**
1801  * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq
1802  *				       notified for any changes in the OPP
1803  *				       availability changes anymore.
1804  * @dev:	The devfreq user device. (parent of devfreq)
1805  * @devfreq:	The devfreq object.
1806  *
1807  * At exit() callback of devfreq_dev_profile, this must be included if
1808  * devfreq_recommended_opp is used.
1809  */
1810 int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq)
1811 {
1812 	return dev_pm_opp_unregister_notifier(dev, &devfreq->nb);
1813 }
1814 EXPORT_SYMBOL(devfreq_unregister_opp_notifier);
1815 
1816 static void devm_devfreq_opp_release(struct device *dev, void *res)
1817 {
1818 	devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res);
1819 }
1820 
1821 /**
1822  * devm_devfreq_register_opp_notifier() - Resource-managed
1823  *					  devfreq_register_opp_notifier()
1824  * @dev:	The devfreq user device. (parent of devfreq)
1825  * @devfreq:	The devfreq object.
1826  */
1827 int devm_devfreq_register_opp_notifier(struct device *dev,
1828 				       struct devfreq *devfreq)
1829 {
1830 	struct devfreq **ptr;
1831 	int ret;
1832 
1833 	ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL);
1834 	if (!ptr)
1835 		return -ENOMEM;
1836 
1837 	ret = devfreq_register_opp_notifier(dev, devfreq);
1838 	if (ret) {
1839 		devres_free(ptr);
1840 		return ret;
1841 	}
1842 
1843 	*ptr = devfreq;
1844 	devres_add(dev, ptr);
1845 
1846 	return 0;
1847 }
1848 EXPORT_SYMBOL(devm_devfreq_register_opp_notifier);
1849 
1850 /**
1851  * devm_devfreq_unregister_opp_notifier() - Resource-managed
1852  *					    devfreq_unregister_opp_notifier()
1853  * @dev:	The devfreq user device. (parent of devfreq)
1854  * @devfreq:	The devfreq object.
1855  */
1856 void devm_devfreq_unregister_opp_notifier(struct device *dev,
1857 					 struct devfreq *devfreq)
1858 {
1859 	WARN_ON(devres_release(dev, devm_devfreq_opp_release,
1860 			       devm_devfreq_dev_match, devfreq));
1861 }
1862 EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier);
1863 
1864 /**
1865  * devfreq_register_notifier() - Register a driver with devfreq
1866  * @devfreq:	The devfreq object.
1867  * @nb:		The notifier block to register.
1868  * @list:	DEVFREQ_TRANSITION_NOTIFIER.
1869  */
1870 int devfreq_register_notifier(struct devfreq *devfreq,
1871 			      struct notifier_block *nb,
1872 			      unsigned int list)
1873 {
1874 	int ret = 0;
1875 
1876 	if (!devfreq)
1877 		return -EINVAL;
1878 
1879 	switch (list) {
1880 	case DEVFREQ_TRANSITION_NOTIFIER:
1881 		ret = srcu_notifier_chain_register(
1882 				&devfreq->transition_notifier_list, nb);
1883 		break;
1884 	default:
1885 		ret = -EINVAL;
1886 	}
1887 
1888 	return ret;
1889 }
1890 EXPORT_SYMBOL(devfreq_register_notifier);
1891 
1892 /*
1893  * devfreq_unregister_notifier() - Unregister a driver with devfreq
1894  * @devfreq:	The devfreq object.
1895  * @nb:		The notifier block to be unregistered.
1896  * @list:	DEVFREQ_TRANSITION_NOTIFIER.
1897  */
1898 int devfreq_unregister_notifier(struct devfreq *devfreq,
1899 				struct notifier_block *nb,
1900 				unsigned int list)
1901 {
1902 	int ret = 0;
1903 
1904 	if (!devfreq)
1905 		return -EINVAL;
1906 
1907 	switch (list) {
1908 	case DEVFREQ_TRANSITION_NOTIFIER:
1909 		ret = srcu_notifier_chain_unregister(
1910 				&devfreq->transition_notifier_list, nb);
1911 		break;
1912 	default:
1913 		ret = -EINVAL;
1914 	}
1915 
1916 	return ret;
1917 }
1918 EXPORT_SYMBOL(devfreq_unregister_notifier);
1919 
1920 struct devfreq_notifier_devres {
1921 	struct devfreq *devfreq;
1922 	struct notifier_block *nb;
1923 	unsigned int list;
1924 };
1925 
1926 static void devm_devfreq_notifier_release(struct device *dev, void *res)
1927 {
1928 	struct devfreq_notifier_devres *this = res;
1929 
1930 	devfreq_unregister_notifier(this->devfreq, this->nb, this->list);
1931 }
1932 
1933 /**
1934  * devm_devfreq_register_notifier()
1935  *	- Resource-managed devfreq_register_notifier()
1936  * @dev:	The devfreq user device. (parent of devfreq)
1937  * @devfreq:	The devfreq object.
1938  * @nb:		The notifier block to be unregistered.
1939  * @list:	DEVFREQ_TRANSITION_NOTIFIER.
1940  */
1941 int devm_devfreq_register_notifier(struct device *dev,
1942 				struct devfreq *devfreq,
1943 				struct notifier_block *nb,
1944 				unsigned int list)
1945 {
1946 	struct devfreq_notifier_devres *ptr;
1947 	int ret;
1948 
1949 	ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr),
1950 				GFP_KERNEL);
1951 	if (!ptr)
1952 		return -ENOMEM;
1953 
1954 	ret = devfreq_register_notifier(devfreq, nb, list);
1955 	if (ret) {
1956 		devres_free(ptr);
1957 		return ret;
1958 	}
1959 
1960 	ptr->devfreq = devfreq;
1961 	ptr->nb = nb;
1962 	ptr->list = list;
1963 	devres_add(dev, ptr);
1964 
1965 	return 0;
1966 }
1967 EXPORT_SYMBOL(devm_devfreq_register_notifier);
1968 
1969 /**
1970  * devm_devfreq_unregister_notifier()
1971  *	- Resource-managed devfreq_unregister_notifier()
1972  * @dev:	The devfreq user device. (parent of devfreq)
1973  * @devfreq:	The devfreq object.
1974  * @nb:		The notifier block to be unregistered.
1975  * @list:	DEVFREQ_TRANSITION_NOTIFIER.
1976  */
1977 void devm_devfreq_unregister_notifier(struct device *dev,
1978 				      struct devfreq *devfreq,
1979 				      struct notifier_block *nb,
1980 				      unsigned int list)
1981 {
1982 	WARN_ON(devres_release(dev, devm_devfreq_notifier_release,
1983 			       devm_devfreq_dev_match, devfreq));
1984 }
1985 EXPORT_SYMBOL(devm_devfreq_unregister_notifier);
1986