Commit | Line | Data |
---|---|---|
4f86d3a8 LB |
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> | |
a06df062 DL |
14 | #include <linux/cpumask.h> |
15 | #include <linux/clockchips.h> | |
4f86d3a8 LB |
16 | |
17 | #include "cpuidle.h" | |
18 | ||
4f86d3a8 LB |
19 | DEFINE_SPINLOCK(cpuidle_driver_lock); |
20 | ||
bf4d1b5d DL |
21 | static void __cpuidle_set_cpu_driver(struct cpuidle_driver *drv, int cpu); |
22 | static struct cpuidle_driver * __cpuidle_get_cpu_driver(int cpu); | |
23 | ||
a06df062 | 24 | static void cpuidle_setup_broadcast_timer(void *arg) |
13dd52f1 | 25 | { |
a06df062 DL |
26 | int cpu = smp_processor_id(); |
27 | clockevents_notify((long)(arg), &cpu); | |
28 | } | |
29 | ||
30 | static void __cpuidle_driver_init(struct cpuidle_driver *drv, int cpu) | |
31 | { | |
32 | int i; | |
33 | ||
13dd52f1 | 34 | drv->refcnt = 0; |
a06df062 DL |
35 | |
36 | for (i = drv->state_count - 1; i >= 0 ; i--) { | |
37 | ||
38 | if (!(drv->states[i].flags & CPUIDLE_FLAG_TIMER_STOP)) | |
39 | continue; | |
40 | ||
41 | drv->bctimer = 1; | |
42 | on_each_cpu_mask(get_cpu_mask(cpu), cpuidle_setup_broadcast_timer, | |
43 | (void *)CLOCK_EVT_NOTIFY_BROADCAST_ON, 1); | |
44 | break; | |
45 | } | |
13dd52f1 DL |
46 | } |
47 | ||
bf4d1b5d | 48 | static int __cpuidle_register_driver(struct cpuidle_driver *drv, int cpu) |
4f86d3a8 | 49 | { |
fc850f39 | 50 | if (!drv || !drv->state_count) |
4f86d3a8 LB |
51 | return -EINVAL; |
52 | ||
62027aea LB |
53 | if (cpuidle_disabled()) |
54 | return -ENODEV; | |
55 | ||
bf4d1b5d | 56 | if (__cpuidle_get_cpu_driver(cpu)) |
4f86d3a8 | 57 | return -EBUSY; |
ed953472 | 58 | |
a06df062 | 59 | __cpuidle_driver_init(drv, cpu); |
ed953472 | 60 | |
bf4d1b5d | 61 | __cpuidle_set_cpu_driver(drv, cpu); |
42f67f2a | 62 | |
13dd52f1 DL |
63 | return 0; |
64 | } | |
65 | ||
bf4d1b5d | 66 | static void __cpuidle_unregister_driver(struct cpuidle_driver *drv, int cpu) |
13dd52f1 | 67 | { |
bf4d1b5d | 68 | if (drv != __cpuidle_get_cpu_driver(cpu)) |
13dd52f1 DL |
69 | return; |
70 | ||
71 | if (!WARN_ON(drv->refcnt > 0)) | |
bf4d1b5d | 72 | __cpuidle_set_cpu_driver(NULL, cpu); |
a06df062 DL |
73 | |
74 | if (drv->bctimer) { | |
75 | drv->bctimer = 0; | |
76 | on_each_cpu_mask(get_cpu_mask(cpu), cpuidle_setup_broadcast_timer, | |
77 | (void *)CLOCK_EVT_NOTIFY_BROADCAST_OFF, 1); | |
78 | } | |
bf4d1b5d DL |
79 | } |
80 | ||
81 | #ifdef CONFIG_CPU_IDLE_MULTIPLE_DRIVERS | |
82 | ||
83 | static DEFINE_PER_CPU(struct cpuidle_driver *, cpuidle_drivers); | |
84 | ||
85 | static void __cpuidle_set_cpu_driver(struct cpuidle_driver *drv, int cpu) | |
86 | { | |
87 | per_cpu(cpuidle_drivers, cpu) = drv; | |
88 | } | |
89 | ||
90 | static struct cpuidle_driver *__cpuidle_get_cpu_driver(int cpu) | |
91 | { | |
92 | return per_cpu(cpuidle_drivers, cpu); | |
93 | } | |
94 | ||
95 | static void __cpuidle_unregister_all_cpu_driver(struct cpuidle_driver *drv) | |
96 | { | |
97 | int cpu; | |
98 | for_each_present_cpu(cpu) | |
99 | __cpuidle_unregister_driver(drv, cpu); | |
100 | } | |
101 | ||
102 | static int __cpuidle_register_all_cpu_driver(struct cpuidle_driver *drv) | |
103 | { | |
104 | int ret = 0; | |
105 | int i, cpu; | |
106 | ||
107 | for_each_present_cpu(cpu) { | |
108 | ret = __cpuidle_register_driver(drv, cpu); | |
109 | if (ret) | |
110 | break; | |
111 | } | |
112 | ||
113 | if (ret) | |
114 | for_each_present_cpu(i) { | |
115 | if (i == cpu) | |
116 | break; | |
117 | __cpuidle_unregister_driver(drv, i); | |
118 | } | |
119 | ||
120 | ||
121 | return ret; | |
122 | } | |
123 | ||
124 | int cpuidle_register_cpu_driver(struct cpuidle_driver *drv, int cpu) | |
125 | { | |
126 | int ret; | |
127 | ||
128 | spin_lock(&cpuidle_driver_lock); | |
129 | ret = __cpuidle_register_driver(drv, cpu); | |
130 | spin_unlock(&cpuidle_driver_lock); | |
131 | ||
132 | return ret; | |
133 | } | |
134 | ||
135 | void cpuidle_unregister_cpu_driver(struct cpuidle_driver *drv, int cpu) | |
136 | { | |
137 | spin_lock(&cpuidle_driver_lock); | |
138 | __cpuidle_unregister_driver(drv, cpu); | |
139 | spin_unlock(&cpuidle_driver_lock); | |
13dd52f1 | 140 | } |
ed953472 | 141 | |
13dd52f1 DL |
142 | /** |
143 | * cpuidle_register_driver - registers a driver | |
144 | * @drv: the driver | |
145 | */ | |
146 | int cpuidle_register_driver(struct cpuidle_driver *drv) | |
147 | { | |
148 | int ret; | |
149 | ||
150 | spin_lock(&cpuidle_driver_lock); | |
bf4d1b5d | 151 | ret = __cpuidle_register_all_cpu_driver(drv); |
4f86d3a8 LB |
152 | spin_unlock(&cpuidle_driver_lock); |
153 | ||
13dd52f1 | 154 | return ret; |
4f86d3a8 | 155 | } |
4f86d3a8 LB |
156 | EXPORT_SYMBOL_GPL(cpuidle_register_driver); |
157 | ||
752138df | 158 | /** |
bf4d1b5d DL |
159 | * cpuidle_unregister_driver - unregisters a driver |
160 | * @drv: the driver | |
752138df | 161 | */ |
bf4d1b5d DL |
162 | void cpuidle_unregister_driver(struct cpuidle_driver *drv) |
163 | { | |
164 | spin_lock(&cpuidle_driver_lock); | |
165 | __cpuidle_unregister_all_cpu_driver(drv); | |
166 | spin_unlock(&cpuidle_driver_lock); | |
167 | } | |
168 | EXPORT_SYMBOL_GPL(cpuidle_unregister_driver); | |
169 | ||
170 | #else | |
171 | ||
172 | static struct cpuidle_driver *cpuidle_curr_driver; | |
173 | ||
174 | static inline void __cpuidle_set_cpu_driver(struct cpuidle_driver *drv, int cpu) | |
175 | { | |
176 | cpuidle_curr_driver = drv; | |
177 | } | |
178 | ||
179 | static inline struct cpuidle_driver *__cpuidle_get_cpu_driver(int cpu) | |
752138df LB |
180 | { |
181 | return cpuidle_curr_driver; | |
182 | } | |
bf4d1b5d DL |
183 | |
184 | /** | |
185 | * cpuidle_register_driver - registers a driver | |
186 | * @drv: the driver | |
187 | */ | |
188 | int cpuidle_register_driver(struct cpuidle_driver *drv) | |
189 | { | |
190 | int ret, cpu; | |
191 | ||
192 | cpu = get_cpu(); | |
193 | spin_lock(&cpuidle_driver_lock); | |
194 | ret = __cpuidle_register_driver(drv, cpu); | |
195 | spin_unlock(&cpuidle_driver_lock); | |
196 | put_cpu(); | |
197 | ||
198 | return ret; | |
199 | } | |
200 | EXPORT_SYMBOL_GPL(cpuidle_register_driver); | |
752138df | 201 | |
4f86d3a8 LB |
202 | /** |
203 | * cpuidle_unregister_driver - unregisters a driver | |
204 | * @drv: the driver | |
205 | */ | |
206 | void cpuidle_unregister_driver(struct cpuidle_driver *drv) | |
207 | { | |
bf4d1b5d DL |
208 | int cpu; |
209 | ||
210 | cpu = get_cpu(); | |
4f86d3a8 | 211 | spin_lock(&cpuidle_driver_lock); |
bf4d1b5d | 212 | __cpuidle_unregister_driver(drv, cpu); |
4f86d3a8 | 213 | spin_unlock(&cpuidle_driver_lock); |
bf4d1b5d | 214 | put_cpu(); |
4f86d3a8 | 215 | } |
4f86d3a8 | 216 | EXPORT_SYMBOL_GPL(cpuidle_unregister_driver); |
bf4d1b5d DL |
217 | #endif |
218 | ||
219 | /** | |
220 | * cpuidle_get_driver - return the current driver | |
221 | */ | |
222 | struct cpuidle_driver *cpuidle_get_driver(void) | |
223 | { | |
224 | struct cpuidle_driver *drv; | |
225 | int cpu; | |
226 | ||
227 | cpu = get_cpu(); | |
228 | drv = __cpuidle_get_cpu_driver(cpu); | |
229 | put_cpu(); | |
230 | ||
231 | return drv; | |
232 | } | |
233 | EXPORT_SYMBOL_GPL(cpuidle_get_driver); | |
234 | ||
235 | /** | |
236 | * cpuidle_get_cpu_driver - return the driver tied with a cpu | |
237 | */ | |
238 | struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev) | |
239 | { | |
bf4d1b5d DL |
240 | if (!dev) |
241 | return NULL; | |
242 | ||
ac34d7c8 | 243 | return __cpuidle_get_cpu_driver(dev->cpu); |
bf4d1b5d DL |
244 | } |
245 | EXPORT_SYMBOL_GPL(cpuidle_get_cpu_driver); | |
6e797a07 RW |
246 | |
247 | struct cpuidle_driver *cpuidle_driver_ref(void) | |
248 | { | |
249 | struct cpuidle_driver *drv; | |
250 | ||
251 | spin_lock(&cpuidle_driver_lock); | |
252 | ||
13dd52f1 | 253 | drv = cpuidle_get_driver(); |
42f67f2a | 254 | drv->refcnt++; |
6e797a07 RW |
255 | |
256 | spin_unlock(&cpuidle_driver_lock); | |
257 | return drv; | |
258 | } | |
259 | ||
260 | void cpuidle_driver_unref(void) | |
261 | { | |
13dd52f1 | 262 | struct cpuidle_driver *drv = cpuidle_get_driver(); |
42f67f2a | 263 | |
6e797a07 RW |
264 | spin_lock(&cpuidle_driver_lock); |
265 | ||
42f67f2a DL |
266 | if (drv && !WARN_ON(drv->refcnt <= 0)) |
267 | drv->refcnt--; | |
6e797a07 RW |
268 | |
269 | spin_unlock(&cpuidle_driver_lock); | |
270 | } |