Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 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> |
Daniel Lezcano | a06df06 | 2013-03-27 10:22:10 +0000 | [diff] [blame] | 14 | #include <linux/cpumask.h> |
| 15 | #include <linux/clockchips.h> |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 16 | |
| 17 | #include "cpuidle.h" |
| 18 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 19 | DEFINE_SPINLOCK(cpuidle_driver_lock); |
| 20 | |
Daniel Lezcano | 82467a5 | 2013-06-07 21:53:09 +0000 | [diff] [blame^] | 21 | #ifdef CONFIG_CPU_IDLE_MULTIPLE_DRIVERS |
| 22 | |
| 23 | static DEFINE_PER_CPU(struct cpuidle_driver *, cpuidle_drivers); |
| 24 | |
| 25 | static struct cpuidle_driver *__cpuidle_get_cpu_driver(int cpu) |
| 26 | { |
| 27 | return per_cpu(cpuidle_drivers, cpu); |
| 28 | } |
| 29 | |
| 30 | static inline void __cpuidle_unset_driver(struct cpuidle_driver *drv) |
| 31 | { |
| 32 | int cpu; |
| 33 | |
| 34 | for_each_cpu(cpu, drv->cpumask) { |
| 35 | |
| 36 | if (drv != __cpuidle_get_cpu_driver(cpu)) |
| 37 | continue; |
| 38 | |
| 39 | per_cpu(cpuidle_drivers, cpu) = NULL; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | static inline int __cpuidle_set_driver(struct cpuidle_driver *drv) |
| 44 | { |
| 45 | int cpu; |
| 46 | |
| 47 | for_each_cpu(cpu, drv->cpumask) { |
| 48 | |
| 49 | if (__cpuidle_get_cpu_driver(cpu)) { |
| 50 | __cpuidle_unset_driver(drv); |
| 51 | return -EBUSY; |
| 52 | } |
| 53 | |
| 54 | per_cpu(cpuidle_drivers, cpu) = drv; |
| 55 | } |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | #else |
| 61 | |
| 62 | static struct cpuidle_driver *cpuidle_curr_driver; |
| 63 | |
| 64 | static inline struct cpuidle_driver *__cpuidle_get_cpu_driver(int cpu) |
| 65 | { |
| 66 | return cpuidle_curr_driver; |
| 67 | } |
| 68 | |
| 69 | static inline int __cpuidle_set_driver(struct cpuidle_driver *drv) |
| 70 | { |
| 71 | if (cpuidle_curr_driver) |
| 72 | return -EBUSY; |
| 73 | |
| 74 | cpuidle_curr_driver = drv; |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | static inline void __cpuidle_unset_driver(struct cpuidle_driver *drv) |
| 80 | { |
| 81 | if (drv == cpuidle_curr_driver) |
| 82 | cpuidle_curr_driver = NULL; |
| 83 | } |
| 84 | |
| 85 | #endif |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 86 | |
Daniel Lezcano | a06df06 | 2013-03-27 10:22:10 +0000 | [diff] [blame] | 87 | static void cpuidle_setup_broadcast_timer(void *arg) |
Daniel Lezcano | 13dd52f | 2012-10-31 16:44:47 +0000 | [diff] [blame] | 88 | { |
Daniel Lezcano | a06df06 | 2013-03-27 10:22:10 +0000 | [diff] [blame] | 89 | int cpu = smp_processor_id(); |
| 90 | clockevents_notify((long)(arg), &cpu); |
| 91 | } |
| 92 | |
Daniel Lezcano | 82467a5 | 2013-06-07 21:53:09 +0000 | [diff] [blame^] | 93 | static int __cpuidle_driver_init(struct cpuidle_driver *drv) |
Daniel Lezcano | a06df06 | 2013-03-27 10:22:10 +0000 | [diff] [blame] | 94 | { |
| 95 | int i; |
| 96 | |
Daniel Lezcano | 13dd52f | 2012-10-31 16:44:47 +0000 | [diff] [blame] | 97 | drv->refcnt = 0; |
Daniel Lezcano | a06df06 | 2013-03-27 10:22:10 +0000 | [diff] [blame] | 98 | |
Daniel Lezcano | 82467a5 | 2013-06-07 21:53:09 +0000 | [diff] [blame^] | 99 | if (!drv->cpumask) |
| 100 | drv->cpumask = (struct cpumask *)cpu_possible_mask; |
| 101 | |
Daniel Lezcano | a06df06 | 2013-03-27 10:22:10 +0000 | [diff] [blame] | 102 | for (i = drv->state_count - 1; i >= 0 ; i--) { |
| 103 | |
| 104 | if (!(drv->states[i].flags & CPUIDLE_FLAG_TIMER_STOP)) |
| 105 | continue; |
| 106 | |
| 107 | drv->bctimer = 1; |
Daniel Lezcano | a06df06 | 2013-03-27 10:22:10 +0000 | [diff] [blame] | 108 | break; |
| 109 | } |
Daniel Lezcano | 82467a5 | 2013-06-07 21:53:09 +0000 | [diff] [blame^] | 110 | |
| 111 | return 0; |
Daniel Lezcano | 13dd52f | 2012-10-31 16:44:47 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Daniel Lezcano | 82467a5 | 2013-06-07 21:53:09 +0000 | [diff] [blame^] | 114 | static int __cpuidle_register_driver(struct cpuidle_driver *drv) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 115 | { |
Daniel Lezcano | 82467a5 | 2013-06-07 21:53:09 +0000 | [diff] [blame^] | 116 | int ret; |
| 117 | |
Daniel Lezcano | fc850f3 | 2012-03-26 14:51:26 +0200 | [diff] [blame] | 118 | if (!drv || !drv->state_count) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 119 | return -EINVAL; |
| 120 | |
Len Brown | 62027ae | 2011-04-01 18:13:10 -0400 | [diff] [blame] | 121 | if (cpuidle_disabled()) |
| 122 | return -ENODEV; |
| 123 | |
Daniel Lezcano | 82467a5 | 2013-06-07 21:53:09 +0000 | [diff] [blame^] | 124 | ret = __cpuidle_driver_init(drv); |
| 125 | if (ret) |
| 126 | return ret; |
Daniel Lezcano | ed95347 | 2012-09-22 00:38:32 +0200 | [diff] [blame] | 127 | |
Daniel Lezcano | 82467a5 | 2013-06-07 21:53:09 +0000 | [diff] [blame^] | 128 | ret = __cpuidle_set_driver(drv); |
| 129 | if (ret) |
| 130 | return ret; |
Daniel Lezcano | ed95347 | 2012-09-22 00:38:32 +0200 | [diff] [blame] | 131 | |
Daniel Lezcano | 82467a5 | 2013-06-07 21:53:09 +0000 | [diff] [blame^] | 132 | if (drv->bctimer) |
| 133 | on_each_cpu_mask(drv->cpumask, cpuidle_setup_broadcast_timer, |
| 134 | (void *)CLOCK_EVT_NOTIFY_BROADCAST_ON, 1); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 135 | |
| 136 | return 0; |
| 137 | } |
Daniel Lezcano | 13dd52f | 2012-10-31 16:44:47 +0000 | [diff] [blame] | 138 | |
Daniel Lezcano | 82467a5 | 2013-06-07 21:53:09 +0000 | [diff] [blame^] | 139 | /** |
| 140 | * cpuidle_unregister_driver - unregisters a driver |
| 141 | * @drv: the driver |
| 142 | */ |
| 143 | static void __cpuidle_unregister_driver(struct cpuidle_driver *drv) |
Daniel Lezcano | 13dd52f | 2012-10-31 16:44:47 +0000 | [diff] [blame] | 144 | { |
Daniel Lezcano | 82467a5 | 2013-06-07 21:53:09 +0000 | [diff] [blame^] | 145 | if (WARN_ON(drv->refcnt > 0)) |
Daniel Lezcano | 13dd52f | 2012-10-31 16:44:47 +0000 | [diff] [blame] | 146 | return; |
| 147 | |
Daniel Lezcano | a06df06 | 2013-03-27 10:22:10 +0000 | [diff] [blame] | 148 | if (drv->bctimer) { |
| 149 | drv->bctimer = 0; |
Daniel Lezcano | 82467a5 | 2013-06-07 21:53:09 +0000 | [diff] [blame^] | 150 | on_each_cpu_mask(drv->cpumask, cpuidle_setup_broadcast_timer, |
Daniel Lezcano | a06df06 | 2013-03-27 10:22:10 +0000 | [diff] [blame] | 151 | (void *)CLOCK_EVT_NOTIFY_BROADCAST_OFF, 1); |
| 152 | } |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 153 | |
Daniel Lezcano | 82467a5 | 2013-06-07 21:53:09 +0000 | [diff] [blame^] | 154 | __cpuidle_unset_driver(drv); |
Daniel Lezcano | 13dd52f | 2012-10-31 16:44:47 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /** |
| 158 | * cpuidle_register_driver - registers a driver |
| 159 | * @drv: the driver |
| 160 | */ |
| 161 | int cpuidle_register_driver(struct cpuidle_driver *drv) |
| 162 | { |
| 163 | int ret; |
| 164 | |
| 165 | spin_lock(&cpuidle_driver_lock); |
Daniel Lezcano | 82467a5 | 2013-06-07 21:53:09 +0000 | [diff] [blame^] | 166 | ret = __cpuidle_register_driver(drv); |
Daniel Lezcano | 13dd52f | 2012-10-31 16:44:47 +0000 | [diff] [blame] | 167 | spin_unlock(&cpuidle_driver_lock); |
| 168 | |
| 169 | return ret; |
| 170 | } |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 171 | EXPORT_SYMBOL_GPL(cpuidle_register_driver); |
| 172 | |
| 173 | /** |
| 174 | * cpuidle_unregister_driver - unregisters a driver |
| 175 | * @drv: the driver |
| 176 | */ |
| 177 | void cpuidle_unregister_driver(struct cpuidle_driver *drv) |
| 178 | { |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 179 | spin_lock(&cpuidle_driver_lock); |
Daniel Lezcano | 82467a5 | 2013-06-07 21:53:09 +0000 | [diff] [blame^] | 180 | __cpuidle_unregister_driver(drv); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 181 | spin_unlock(&cpuidle_driver_lock); |
| 182 | } |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 183 | EXPORT_SYMBOL_GPL(cpuidle_unregister_driver); |
Rafael J. Wysocki | 6e797a0 | 2012-06-16 15:20:11 +0200 | [diff] [blame] | 184 | |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 185 | /** |
| 186 | * cpuidle_get_driver - return the current driver |
| 187 | */ |
| 188 | struct cpuidle_driver *cpuidle_get_driver(void) |
| 189 | { |
| 190 | struct cpuidle_driver *drv; |
| 191 | int cpu; |
| 192 | |
| 193 | cpu = get_cpu(); |
| 194 | drv = __cpuidle_get_cpu_driver(cpu); |
| 195 | put_cpu(); |
| 196 | |
| 197 | return drv; |
| 198 | } |
| 199 | EXPORT_SYMBOL_GPL(cpuidle_get_driver); |
| 200 | |
| 201 | /** |
| 202 | * cpuidle_get_cpu_driver - return the driver tied with a cpu |
| 203 | */ |
| 204 | struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev) |
| 205 | { |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 206 | if (!dev) |
| 207 | return NULL; |
| 208 | |
Daniel Lezcano | ac34d7c | 2013-01-03 13:03:18 +0100 | [diff] [blame] | 209 | return __cpuidle_get_cpu_driver(dev->cpu); |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 210 | } |
| 211 | EXPORT_SYMBOL_GPL(cpuidle_get_cpu_driver); |
| 212 | |
Rafael J. Wysocki | 6e797a0 | 2012-06-16 15:20:11 +0200 | [diff] [blame] | 213 | struct cpuidle_driver *cpuidle_driver_ref(void) |
| 214 | { |
| 215 | struct cpuidle_driver *drv; |
| 216 | |
| 217 | spin_lock(&cpuidle_driver_lock); |
| 218 | |
Daniel Lezcano | 13dd52f | 2012-10-31 16:44:47 +0000 | [diff] [blame] | 219 | drv = cpuidle_get_driver(); |
Daniel Lezcano | 42f67f2 | 2012-10-31 16:44:45 +0000 | [diff] [blame] | 220 | drv->refcnt++; |
Rafael J. Wysocki | 6e797a0 | 2012-06-16 15:20:11 +0200 | [diff] [blame] | 221 | |
| 222 | spin_unlock(&cpuidle_driver_lock); |
| 223 | return drv; |
| 224 | } |
| 225 | |
| 226 | void cpuidle_driver_unref(void) |
| 227 | { |
Daniel Lezcano | 13dd52f | 2012-10-31 16:44:47 +0000 | [diff] [blame] | 228 | struct cpuidle_driver *drv = cpuidle_get_driver(); |
Daniel Lezcano | 42f67f2 | 2012-10-31 16:44:45 +0000 | [diff] [blame] | 229 | |
Rafael J. Wysocki | 6e797a0 | 2012-06-16 15:20:11 +0200 | [diff] [blame] | 230 | spin_lock(&cpuidle_driver_lock); |
| 231 | |
Daniel Lezcano | 42f67f2 | 2012-10-31 16:44:45 +0000 | [diff] [blame] | 232 | if (drv && !WARN_ON(drv->refcnt <= 0)) |
| 233 | drv->refcnt--; |
Rafael J. Wysocki | 6e797a0 | 2012-06-16 15:20:11 +0200 | [diff] [blame] | 234 | |
| 235 | spin_unlock(&cpuidle_driver_lock); |
| 236 | } |