blob: 8246662f594ab9cdc1382180604e956830c4018b [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
Daniel Lezcano13dd52f2012-10-31 16:44:47 +000042static 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
50static void cpuidle_set_driver(struct cpuidle_driver *drv)
51{
52 cpuidle_curr_driver = drv;
53}
54
55static int __cpuidle_register_driver(struct cpuidle_driver *drv)
Len Brown4f86d3a2007-10-03 18:58:00 -040056{
Daniel Lezcanofc850f32012-03-26 14:51:26 +020057 if (!drv || !drv->state_count)
Len Brown4f86d3a2007-10-03 18:58:00 -040058 return -EINVAL;
59
Len Brown62027ae2011-04-01 18:13:10 -040060 if (cpuidle_disabled())
61 return -ENODEV;
62
Daniel Lezcano13dd52f2012-10-31 16:44:47 +000063 if (cpuidle_get_driver())
Len Brown4f86d3a2007-10-03 18:58:00 -040064 return -EBUSY;
Daniel Lezcanoed953472012-09-22 00:38:32 +020065
Daniel Lezcano13dd52f2012-10-31 16:44:47 +000066 __cpuidle_driver_init(drv);
Daniel Lezcanoed953472012-09-22 00:38:32 +020067
Daniel Lezcano13dd52f2012-10-31 16:44:47 +000068 cpuidle_set_driver(drv);
Len Brown4f86d3a2007-10-03 18:58:00 -040069
70 return 0;
71}
Daniel Lezcano13dd52f2012-10-31 16:44:47 +000072
73static 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 */
86int 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 Brown4f86d3a2007-10-03 18:58:00 -040096EXPORT_SYMBOL_GPL(cpuidle_register_driver);
97
98/**
Len Brown752138d2010-05-22 16:57:26 -040099 * cpuidle_get_driver - return the current driver
100 */
101struct cpuidle_driver *cpuidle_get_driver(void)
102{
103 return cpuidle_curr_driver;
104}
105EXPORT_SYMBOL_GPL(cpuidle_get_driver);
106
107/**
Len Brown4f86d3a2007-10-03 18:58:00 -0400108 * cpuidle_unregister_driver - unregisters a driver
109 * @drv: the driver
110 */
111void cpuidle_unregister_driver(struct cpuidle_driver *drv)
112{
Len Brown4f86d3a2007-10-03 18:58:00 -0400113 spin_lock(&cpuidle_driver_lock);
Daniel Lezcano13dd52f2012-10-31 16:44:47 +0000114 __cpuidle_unregister_driver(drv);
Len Brown4f86d3a2007-10-03 18:58:00 -0400115 spin_unlock(&cpuidle_driver_lock);
116}
Len Brown4f86d3a2007-10-03 18:58:00 -0400117EXPORT_SYMBOL_GPL(cpuidle_unregister_driver);
Rafael J. Wysocki6e797a02012-06-16 15:20:11 +0200118
119struct cpuidle_driver *cpuidle_driver_ref(void)
120{
121 struct cpuidle_driver *drv;
122
123 spin_lock(&cpuidle_driver_lock);
124
Daniel Lezcano13dd52f2012-10-31 16:44:47 +0000125 drv = cpuidle_get_driver();
Daniel Lezcano42f67f22012-10-31 16:44:45 +0000126 drv->refcnt++;
Rafael J. Wysocki6e797a02012-06-16 15:20:11 +0200127
128 spin_unlock(&cpuidle_driver_lock);
129 return drv;
130}
131
132void cpuidle_driver_unref(void)
133{
Daniel Lezcano13dd52f2012-10-31 16:44:47 +0000134 struct cpuidle_driver *drv = cpuidle_get_driver();
Daniel Lezcano42f67f22012-10-31 16:44:45 +0000135
Rafael J. Wysocki6e797a02012-06-16 15:20:11 +0200136 spin_lock(&cpuidle_driver_lock);
137
Daniel Lezcano42f67f22012-10-31 16:44:45 +0000138 if (drv && !WARN_ON(drv->refcnt <= 0))
139 drv->refcnt--;
Rafael J. Wysocki6e797a02012-06-16 15:20:11 +0200140
141 spin_unlock(&cpuidle_driver_lock);
142}