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> |
| 14 | |
| 15 | #include "cpuidle.h" |
| 16 | |
Len Brown | 752138d | 2010-05-22 16:57:26 -0400 | [diff] [blame] | 17 | static struct cpuidle_driver *cpuidle_curr_driver; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 18 | DEFINE_SPINLOCK(cpuidle_driver_lock); |
| 19 | |
Daniel Lezcano | ed95347 | 2012-09-22 00:38:32 +0200 | [diff] [blame] | 20 | static void set_power_states(struct cpuidle_driver *drv) |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 21 | { |
| 22 | int i; |
Daniel Lezcano | ed95347 | 2012-09-22 00:38:32 +0200 | [diff] [blame] | 23 | |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 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 | */ |
Daniel Lezcano | ed95347 | 2012-09-22 00:38:32 +0200 | [diff] [blame] | 38 | for (i = CPUIDLE_DRIVER_STATE_START; i < drv->state_count; i++) |
| 39 | drv->states[i].power_usage = -1 - i; |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 40 | } |
| 41 | |
Daniel Lezcano | 13dd52f | 2012-10-31 16:44:47 +0000 | [diff] [blame^] | 42 | static void __cpuidle_driver_init(struct cpuidle_driver *drv) |
| 43 | { |
| 44 | drv->refcnt = 0; |
| 45 | |
| 46 | if (!drv->power_specified) |
| 47 | set_power_states(drv); |
| 48 | } |
| 49 | |
| 50 | static void cpuidle_set_driver(struct cpuidle_driver *drv) |
| 51 | { |
| 52 | cpuidle_curr_driver = drv; |
| 53 | } |
| 54 | |
| 55 | static int __cpuidle_register_driver(struct cpuidle_driver *drv) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 56 | { |
Daniel Lezcano | fc850f3 | 2012-03-26 14:51:26 +0200 | [diff] [blame] | 57 | if (!drv || !drv->state_count) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 58 | return -EINVAL; |
| 59 | |
Len Brown | 62027ae | 2011-04-01 18:13:10 -0400 | [diff] [blame] | 60 | if (cpuidle_disabled()) |
| 61 | return -ENODEV; |
| 62 | |
Daniel Lezcano | 13dd52f | 2012-10-31 16:44:47 +0000 | [diff] [blame^] | 63 | if (cpuidle_get_driver()) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 64 | return -EBUSY; |
Daniel Lezcano | ed95347 | 2012-09-22 00:38:32 +0200 | [diff] [blame] | 65 | |
Daniel Lezcano | 13dd52f | 2012-10-31 16:44:47 +0000 | [diff] [blame^] | 66 | __cpuidle_driver_init(drv); |
Daniel Lezcano | ed95347 | 2012-09-22 00:38:32 +0200 | [diff] [blame] | 67 | |
Daniel Lezcano | 13dd52f | 2012-10-31 16:44:47 +0000 | [diff] [blame^] | 68 | cpuidle_set_driver(drv); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 69 | |
| 70 | return 0; |
| 71 | } |
Daniel Lezcano | 13dd52f | 2012-10-31 16:44:47 +0000 | [diff] [blame^] | 72 | |
| 73 | static void __cpuidle_unregister_driver(struct cpuidle_driver *drv) |
| 74 | { |
| 75 | if (drv != cpuidle_get_driver()) |
| 76 | return; |
| 77 | |
| 78 | if (!WARN_ON(drv->refcnt > 0)) |
| 79 | cpuidle_set_driver(NULL); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * cpuidle_register_driver - registers a driver |
| 84 | * @drv: the driver |
| 85 | */ |
| 86 | int cpuidle_register_driver(struct cpuidle_driver *drv) |
| 87 | { |
| 88 | int ret; |
| 89 | |
| 90 | spin_lock(&cpuidle_driver_lock); |
| 91 | ret = __cpuidle_register_driver(drv); |
| 92 | spin_unlock(&cpuidle_driver_lock); |
| 93 | |
| 94 | return ret; |
| 95 | } |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 96 | EXPORT_SYMBOL_GPL(cpuidle_register_driver); |
| 97 | |
| 98 | /** |
Len Brown | 752138d | 2010-05-22 16:57:26 -0400 | [diff] [blame] | 99 | * cpuidle_get_driver - return the current driver |
| 100 | */ |
| 101 | struct cpuidle_driver *cpuidle_get_driver(void) |
| 102 | { |
| 103 | return cpuidle_curr_driver; |
| 104 | } |
| 105 | EXPORT_SYMBOL_GPL(cpuidle_get_driver); |
| 106 | |
| 107 | /** |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 108 | * cpuidle_unregister_driver - unregisters a driver |
| 109 | * @drv: the driver |
| 110 | */ |
| 111 | void cpuidle_unregister_driver(struct cpuidle_driver *drv) |
| 112 | { |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 113 | spin_lock(&cpuidle_driver_lock); |
Daniel Lezcano | 13dd52f | 2012-10-31 16:44:47 +0000 | [diff] [blame^] | 114 | __cpuidle_unregister_driver(drv); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 115 | spin_unlock(&cpuidle_driver_lock); |
| 116 | } |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 117 | EXPORT_SYMBOL_GPL(cpuidle_unregister_driver); |
Rafael J. Wysocki | 6e797a0 | 2012-06-16 15:20:11 +0200 | [diff] [blame] | 118 | |
| 119 | struct cpuidle_driver *cpuidle_driver_ref(void) |
| 120 | { |
| 121 | struct cpuidle_driver *drv; |
| 122 | |
| 123 | spin_lock(&cpuidle_driver_lock); |
| 124 | |
Daniel Lezcano | 13dd52f | 2012-10-31 16:44:47 +0000 | [diff] [blame^] | 125 | drv = cpuidle_get_driver(); |
Daniel Lezcano | 42f67f2 | 2012-10-31 16:44:45 +0000 | [diff] [blame] | 126 | drv->refcnt++; |
Rafael J. Wysocki | 6e797a0 | 2012-06-16 15:20:11 +0200 | [diff] [blame] | 127 | |
| 128 | spin_unlock(&cpuidle_driver_lock); |
| 129 | return drv; |
| 130 | } |
| 131 | |
| 132 | void cpuidle_driver_unref(void) |
| 133 | { |
Daniel Lezcano | 13dd52f | 2012-10-31 16:44:47 +0000 | [diff] [blame^] | 134 | struct cpuidle_driver *drv = cpuidle_get_driver(); |
Daniel Lezcano | 42f67f2 | 2012-10-31 16:44:45 +0000 | [diff] [blame] | 135 | |
Rafael J. Wysocki | 6e797a0 | 2012-06-16 15:20:11 +0200 | [diff] [blame] | 136 | spin_lock(&cpuidle_driver_lock); |
| 137 | |
Daniel Lezcano | 42f67f2 | 2012-10-31 16:44:45 +0000 | [diff] [blame] | 138 | if (drv && !WARN_ON(drv->refcnt <= 0)) |
| 139 | drv->refcnt--; |
Rafael J. Wysocki | 6e797a0 | 2012-06-16 15:20:11 +0200 | [diff] [blame] | 140 | |
| 141 | spin_unlock(&cpuidle_driver_lock); |
| 142 | } |