cpuidle: move driver checking within the lock section
[deliverable/linux.git] / drivers / cpuidle / driver.c
CommitLineData
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>
14
15#include "cpuidle.h"
16
752138df 17static struct cpuidle_driver *cpuidle_curr_driver;
4f86d3a8
LB
18DEFINE_SPINLOCK(cpuidle_driver_lock);
19
ed953472 20static void set_power_states(struct cpuidle_driver *drv)
46bcfad7
DD
21{
22 int i;
ed953472 23
46bcfad7
DD
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 */
ed953472
DL
38 for (i = CPUIDLE_DRIVER_STATE_START; i < drv->state_count; i++)
39 drv->states[i].power_usage = -1 - i;
46bcfad7
DD
40}
41
4f86d3a8
LB
42/**
43 * cpuidle_register_driver - registers a driver
44 * @drv: the driver
45 */
46int cpuidle_register_driver(struct cpuidle_driver *drv)
47{
fc850f39 48 if (!drv || !drv->state_count)
4f86d3a8
LB
49 return -EINVAL;
50
62027aea
LB
51 if (cpuidle_disabled())
52 return -ENODEV;
53
4f86d3a8
LB
54 spin_lock(&cpuidle_driver_lock);
55 if (cpuidle_curr_driver) {
56 spin_unlock(&cpuidle_driver_lock);
57 return -EBUSY;
58 }
ed953472
DL
59
60 if (!drv->power_specified)
61 set_power_states(drv);
62
42f67f2a
DL
63 drv->refcnt = 0;
64
4f86d3a8 65 cpuidle_curr_driver = drv;
ed953472 66
4f86d3a8
LB
67 spin_unlock(&cpuidle_driver_lock);
68
69 return 0;
70}
4f86d3a8
LB
71EXPORT_SYMBOL_GPL(cpuidle_register_driver);
72
752138df
LB
73/**
74 * cpuidle_get_driver - return the current driver
75 */
76struct cpuidle_driver *cpuidle_get_driver(void)
77{
78 return cpuidle_curr_driver;
79}
80EXPORT_SYMBOL_GPL(cpuidle_get_driver);
81
4f86d3a8
LB
82/**
83 * cpuidle_unregister_driver - unregisters a driver
84 * @drv: the driver
85 */
86void cpuidle_unregister_driver(struct cpuidle_driver *drv)
87{
4f86d3a8 88 spin_lock(&cpuidle_driver_lock);
41682032 89 if (drv == cpuidle_curr_driver && !WARN_ON(drv->refcnt > 0))
6e797a07 90 cpuidle_curr_driver = NULL;
4f86d3a8
LB
91 spin_unlock(&cpuidle_driver_lock);
92}
4f86d3a8 93EXPORT_SYMBOL_GPL(cpuidle_unregister_driver);
6e797a07
RW
94
95struct cpuidle_driver *cpuidle_driver_ref(void)
96{
97 struct cpuidle_driver *drv;
98
99 spin_lock(&cpuidle_driver_lock);
100
101 drv = cpuidle_curr_driver;
42f67f2a 102 drv->refcnt++;
6e797a07
RW
103
104 spin_unlock(&cpuidle_driver_lock);
105 return drv;
106}
107
108void cpuidle_driver_unref(void)
109{
42f67f2a
DL
110 struct cpuidle_driver *drv = cpuidle_curr_driver;
111
6e797a07
RW
112 spin_lock(&cpuidle_driver_lock);
113
42f67f2a
DL
114 if (drv && !WARN_ON(drv->refcnt <= 0))
115 drv->refcnt--;
6e797a07
RW
116
117 spin_unlock(&cpuidle_driver_lock);
118}
This page took 0.627827 seconds and 5 git commands to generate.