blob: 87db3877fead7280d93eaf53baabcc6e4fe0f043 [file] [log] [blame]
Len Brown4f86d3a2007-10-03 18:58:00 -04001/*
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 Brown752138d2010-05-22 16:57:26 -040017static struct cpuidle_driver *cpuidle_curr_driver;
Len Brown4f86d3a2007-10-03 18:58:00 -040018DEFINE_SPINLOCK(cpuidle_driver_lock);
Rafael J. Wysocki6e797a02012-06-16 15:20:11 +020019int cpuidle_driver_refcount;
Len Brown4f86d3a2007-10-03 18:58:00 -040020
Daniel Lezcanoed953472012-09-22 00:38:32 +020021static void set_power_states(struct cpuidle_driver *drv)
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +053022{
23 int i;
Daniel Lezcanoed953472012-09-22 00:38:32 +020024
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +053025 /*
26 * cpuidle driver should set the drv->power_specified bit
27 * before registering if the driver provides
28 * power_usage numbers.
29 *
30 * If power_specified is not set,
31 * we fill in power_usage with decreasing values as the
32 * cpuidle code has an implicit assumption that state Cn
33 * uses less power than C(n-1).
34 *
35 * With CONFIG_ARCH_HAS_CPU_RELAX, C0 is already assigned
36 * an power value of -1. So we use -2, -3, etc, for other
37 * c-states.
38 */
Daniel Lezcanoed953472012-09-22 00:38:32 +020039 for (i = CPUIDLE_DRIVER_STATE_START; i < drv->state_count; i++)
40 drv->states[i].power_usage = -1 - i;
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +053041}
42
Len Brown4f86d3a2007-10-03 18:58:00 -040043/**
44 * cpuidle_register_driver - registers a driver
45 * @drv: the driver
46 */
47int cpuidle_register_driver(struct cpuidle_driver *drv)
48{
Daniel Lezcanofc850f32012-03-26 14:51:26 +020049 if (!drv || !drv->state_count)
Len Brown4f86d3a2007-10-03 18:58:00 -040050 return -EINVAL;
51
Len Brown62027ae2011-04-01 18:13:10 -040052 if (cpuidle_disabled())
53 return -ENODEV;
54
Len Brown4f86d3a2007-10-03 18:58:00 -040055 spin_lock(&cpuidle_driver_lock);
56 if (cpuidle_curr_driver) {
57 spin_unlock(&cpuidle_driver_lock);
58 return -EBUSY;
59 }
Daniel Lezcanoed953472012-09-22 00:38:32 +020060
61 if (!drv->power_specified)
62 set_power_states(drv);
63
Len Brown4f86d3a2007-10-03 18:58:00 -040064 cpuidle_curr_driver = drv;
Daniel Lezcanoed953472012-09-22 00:38:32 +020065
Len Brown4f86d3a2007-10-03 18:58:00 -040066 spin_unlock(&cpuidle_driver_lock);
67
68 return 0;
69}
Len Brown4f86d3a2007-10-03 18:58:00 -040070EXPORT_SYMBOL_GPL(cpuidle_register_driver);
71
72/**
Len Brown752138d2010-05-22 16:57:26 -040073 * cpuidle_get_driver - return the current driver
74 */
75struct cpuidle_driver *cpuidle_get_driver(void)
76{
77 return cpuidle_curr_driver;
78}
79EXPORT_SYMBOL_GPL(cpuidle_get_driver);
80
81/**
Len Brown4f86d3a2007-10-03 18:58:00 -040082 * cpuidle_unregister_driver - unregisters a driver
83 * @drv: the driver
84 */
85void cpuidle_unregister_driver(struct cpuidle_driver *drv)
86{
Len Brownc0d64cb2010-05-22 16:34:10 -040087 if (drv != cpuidle_curr_driver) {
88 WARN(1, "invalid cpuidle_unregister_driver(%s)\n",
89 drv->name);
Len Brown4f86d3a2007-10-03 18:58:00 -040090 return;
Len Brownc0d64cb2010-05-22 16:34:10 -040091 }
Len Brown4f86d3a2007-10-03 18:58:00 -040092
93 spin_lock(&cpuidle_driver_lock);
Rafael J. Wysocki6e797a02012-06-16 15:20:11 +020094
95 if (!WARN_ON(cpuidle_driver_refcount > 0))
96 cpuidle_curr_driver = NULL;
97
Len Brown4f86d3a2007-10-03 18:58:00 -040098 spin_unlock(&cpuidle_driver_lock);
99}
Len Brown4f86d3a2007-10-03 18:58:00 -0400100EXPORT_SYMBOL_GPL(cpuidle_unregister_driver);
Rafael J. Wysocki6e797a02012-06-16 15:20:11 +0200101
102struct cpuidle_driver *cpuidle_driver_ref(void)
103{
104 struct cpuidle_driver *drv;
105
106 spin_lock(&cpuidle_driver_lock);
107
108 drv = cpuidle_curr_driver;
109 cpuidle_driver_refcount++;
110
111 spin_unlock(&cpuidle_driver_lock);
112 return drv;
113}
114
115void cpuidle_driver_unref(void)
116{
117 spin_lock(&cpuidle_driver_lock);
118
119 if (!WARN_ON(cpuidle_driver_refcount <= 0))
120 cpuidle_driver_refcount--;
121
122 spin_unlock(&cpuidle_driver_lock);
123}