cpuidle: move driver's refcount to cpuidle
[linux-3.10.git] / drivers / cpuidle / driver.c
1 /*
2  * driver.c - driver support
3  *
4  * (C) 2006-2007 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
5  *               Shaohua Li <shaohua.li@intel.com>
6  *               Adam Belay <abelay@novell.com>
7  *
8  * This code is licenced under the GPL.
9  */
10
11 #include <linux/mutex.h>
12 #include <linux/module.h>
13 #include <linux/cpuidle.h>
14
15 #include "cpuidle.h"
16
17 static struct cpuidle_driver *cpuidle_curr_driver;
18 DEFINE_SPINLOCK(cpuidle_driver_lock);
19
20 static void set_power_states(struct cpuidle_driver *drv)
21 {
22         int i;
23
24         /*
25          * cpuidle driver should set the drv->power_specified bit
26          * before registering if the driver provides
27          * power_usage numbers.
28          *
29          * If power_specified is not set,
30          * we fill in power_usage with decreasing values as the
31          * cpuidle code has an implicit assumption that state Cn
32          * uses less power than C(n-1).
33          *
34          * With CONFIG_ARCH_HAS_CPU_RELAX, C0 is already assigned
35          * an power value of -1.  So we use -2, -3, etc, for other
36          * c-states.
37          */
38         for (i = CPUIDLE_DRIVER_STATE_START; i < drv->state_count; i++)
39                 drv->states[i].power_usage = -1 - i;
40 }
41
42 /**
43  * cpuidle_register_driver - registers a driver
44  * @drv: the driver
45  */
46 int cpuidle_register_driver(struct cpuidle_driver *drv)
47 {
48         if (!drv || !drv->state_count)
49                 return -EINVAL;
50
51         if (cpuidle_disabled())
52                 return -ENODEV;
53
54         spin_lock(&cpuidle_driver_lock);
55         if (cpuidle_curr_driver) {
56                 spin_unlock(&cpuidle_driver_lock);
57                 return -EBUSY;
58         }
59
60         if (!drv->power_specified)
61                 set_power_states(drv);
62
63         drv->refcnt = 0;
64
65         cpuidle_curr_driver = drv;
66
67         spin_unlock(&cpuidle_driver_lock);
68
69         return 0;
70 }
71 EXPORT_SYMBOL_GPL(cpuidle_register_driver);
72
73 /**
74  * cpuidle_get_driver - return the current driver
75  */
76 struct cpuidle_driver *cpuidle_get_driver(void)
77 {
78         return cpuidle_curr_driver;
79 }
80 EXPORT_SYMBOL_GPL(cpuidle_get_driver);
81
82 /**
83  * cpuidle_unregister_driver - unregisters a driver
84  * @drv: the driver
85  */
86 void cpuidle_unregister_driver(struct cpuidle_driver *drv)
87 {
88         if (drv != cpuidle_curr_driver) {
89                 WARN(1, "invalid cpuidle_unregister_driver(%s)\n",
90                         drv->name);
91                 return;
92         }
93
94         spin_lock(&cpuidle_driver_lock);
95
96         if (!WARN_ON(drv->refcnt > 0))
97                 cpuidle_curr_driver = NULL;
98
99         spin_unlock(&cpuidle_driver_lock);
100 }
101 EXPORT_SYMBOL_GPL(cpuidle_unregister_driver);
102
103 struct cpuidle_driver *cpuidle_driver_ref(void)
104 {
105         struct cpuidle_driver *drv;
106
107         spin_lock(&cpuidle_driver_lock);
108
109         drv = cpuidle_curr_driver;
110         drv->refcnt++;
111
112         spin_unlock(&cpuidle_driver_lock);
113         return drv;
114 }
115
116 void cpuidle_driver_unref(void)
117 {
118         struct cpuidle_driver *drv = cpuidle_curr_driver;
119
120         spin_lock(&cpuidle_driver_lock);
121
122         if (drv && !WARN_ON(drv->refcnt <= 0))
123                 drv->refcnt--;
124
125         spin_unlock(&cpuidle_driver_lock);
126 }