blob: 39ba8e181e96a98885e1b910a87e5cba2beed37b [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);
19
Daniel Lezcanoed953472012-09-22 00:38:32 +020020static void set_power_states(struct cpuidle_driver *drv)
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +053021{
22 int i;
Daniel Lezcanoed953472012-09-22 00:38:32 +020023
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +053024 /*
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 Lezcanoed953472012-09-22 00:38:32 +020038 for (i = CPUIDLE_DRIVER_STATE_START; i < drv->state_count; i++)
39 drv->states[i].power_usage = -1 - i;
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +053040}
41
Len Brown4f86d3a2007-10-03 18:58:00 -040042/**
43 * cpuidle_register_driver - registers a driver
44 * @drv: the driver
45 */
46int cpuidle_register_driver(struct cpuidle_driver *drv)
47{
Daniel Lezcanofc850f32012-03-26 14:51:26 +020048 if (!drv || !drv->state_count)
Len Brown4f86d3a2007-10-03 18:58:00 -040049 return -EINVAL;
50
Len Brown62027ae2011-04-01 18:13:10 -040051 if (cpuidle_disabled())
52 return -ENODEV;
53
Len Brown4f86d3a2007-10-03 18:58:00 -040054 spin_lock(&cpuidle_driver_lock);
55 if (cpuidle_curr_driver) {
56 spin_unlock(&cpuidle_driver_lock);
57 return -EBUSY;
58 }
Daniel Lezcanoed953472012-09-22 00:38:32 +020059
60 if (!drv->power_specified)
61 set_power_states(drv);
62
Daniel Lezcano42f67f22012-10-31 16:44:45 +000063 drv->refcnt = 0;
64
Len Brown4f86d3a2007-10-03 18:58:00 -040065 cpuidle_curr_driver = drv;
Daniel Lezcanoed953472012-09-22 00:38:32 +020066
Len Brown4f86d3a2007-10-03 18:58:00 -040067 spin_unlock(&cpuidle_driver_lock);
68
69 return 0;
70}
Len Brown4f86d3a2007-10-03 18:58:00 -040071EXPORT_SYMBOL_GPL(cpuidle_register_driver);
72
73/**
Len Brown752138d2010-05-22 16:57:26 -040074 * 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
82/**
Len Brown4f86d3a2007-10-03 18:58:00 -040083 * cpuidle_unregister_driver - unregisters a driver
84 * @drv: the driver
85 */
86void cpuidle_unregister_driver(struct cpuidle_driver *drv)
87{
Len Brownc0d64cb2010-05-22 16:34:10 -040088 if (drv != cpuidle_curr_driver) {
89 WARN(1, "invalid cpuidle_unregister_driver(%s)\n",
90 drv->name);
Len Brown4f86d3a2007-10-03 18:58:00 -040091 return;
Len Brownc0d64cb2010-05-22 16:34:10 -040092 }
Len Brown4f86d3a2007-10-03 18:58:00 -040093
94 spin_lock(&cpuidle_driver_lock);
Rafael J. Wysocki6e797a02012-06-16 15:20:11 +020095
Daniel Lezcano42f67f22012-10-31 16:44:45 +000096 if (!WARN_ON(drv->refcnt > 0))
Rafael J. Wysocki6e797a02012-06-16 15:20:11 +020097 cpuidle_curr_driver = NULL;
98
Len Brown4f86d3a2007-10-03 18:58:00 -040099 spin_unlock(&cpuidle_driver_lock);
100}
Len Brown4f86d3a2007-10-03 18:58:00 -0400101EXPORT_SYMBOL_GPL(cpuidle_unregister_driver);
Rafael J. Wysocki6e797a02012-06-16 15:20:11 +0200102
103struct cpuidle_driver *cpuidle_driver_ref(void)
104{
105 struct cpuidle_driver *drv;
106
107 spin_lock(&cpuidle_driver_lock);
108
109 drv = cpuidle_curr_driver;
Daniel Lezcano42f67f22012-10-31 16:44:45 +0000110 drv->refcnt++;
Rafael J. Wysocki6e797a02012-06-16 15:20:11 +0200111
112 spin_unlock(&cpuidle_driver_lock);
113 return drv;
114}
115
116void cpuidle_driver_unref(void)
117{
Daniel Lezcano42f67f22012-10-31 16:44:45 +0000118 struct cpuidle_driver *drv = cpuidle_curr_driver;
119
Rafael J. Wysocki6e797a02012-06-16 15:20:11 +0200120 spin_lock(&cpuidle_driver_lock);
121
Daniel Lezcano42f67f22012-10-31 16:44:45 +0000122 if (drv && !WARN_ON(drv->refcnt <= 0))
123 drv->refcnt--;
Rafael J. Wysocki6e797a02012-06-16 15:20:11 +0200124
125 spin_unlock(&cpuidle_driver_lock);
126}