driver.c 4.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * driver.c - driver support
 *
 * (C) 2006-2007 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
 *               Shaohua Li <shaohua.li@intel.com>
 *               Adam Belay <abelay@novell.com>
 *
 * This code is licenced under the GPL.
 */

#include <linux/mutex.h>
#include <linux/module.h>
#include <linux/cpuidle.h>
14 15
#include <linux/cpumask.h>
#include <linux/clockchips.h>
16 17 18 19 20

#include "cpuidle.h"

DEFINE_SPINLOCK(cpuidle_driver_lock);

21
#ifdef CONFIG_CPU_IDLE_MULTIPLE_DRIVERS
D
Daniel Lezcano 已提交
22

23 24 25
static DEFINE_PER_CPU(struct cpuidle_driver *, cpuidle_drivers);

static struct cpuidle_driver *__cpuidle_get_cpu_driver(int cpu)
26
{
27
	return per_cpu(cpuidle_drivers, cpu);
28 29
}

30
static inline void __cpuidle_unset_driver(struct cpuidle_driver *drv)
31
{
32
	int cpu;
33

34
	for_each_cpu(cpu, drv->cpumask) {
35

36
		if (drv != __cpuidle_get_cpu_driver(cpu))
37 38
			continue;

39
		per_cpu(cpuidle_drivers, cpu) = NULL;
40
	}
41 42
}

43
static inline int __cpuidle_set_driver(struct cpuidle_driver *drv)
44
{
45
	int cpu;
46

47
	for_each_cpu(cpu, drv->cpumask) {
48

49 50 51 52
		if (__cpuidle_get_cpu_driver(cpu)) {
			__cpuidle_unset_driver(drv);
			return -EBUSY;
		}
53

54 55
		per_cpu(cpuidle_drivers, cpu) = drv;
	}
56

57 58 59
	return 0;
}

60
#else
61

62
static struct cpuidle_driver *cpuidle_curr_driver;
63

64 65 66
static inline struct cpuidle_driver *__cpuidle_get_cpu_driver(int cpu)
{
	return cpuidle_curr_driver;
D
Daniel Lezcano 已提交
67 68
}

69 70 71 72
static inline int __cpuidle_set_driver(struct cpuidle_driver *drv)
{
	if (cpuidle_curr_driver)
		return -EBUSY;
D
Daniel Lezcano 已提交
73

74
	cpuidle_curr_driver = drv;
D
Daniel Lezcano 已提交
75

76
	return 0;
D
Daniel Lezcano 已提交
77 78
}

79
static inline void __cpuidle_unset_driver(struct cpuidle_driver *drv)
D
Daniel Lezcano 已提交
80
{
81 82
	if (drv == cpuidle_curr_driver)
		cpuidle_curr_driver = NULL;
D
Daniel Lezcano 已提交
83 84
}

85 86 87
#endif

static void cpuidle_setup_broadcast_timer(void *arg)
D
Daniel Lezcano 已提交
88
{
89 90
	int cpu = smp_processor_id();
	clockevents_notify((long)(arg), &cpu);
D
Daniel Lezcano 已提交
91 92
}

93
static int __cpuidle_driver_init(struct cpuidle_driver *drv)
D
Daniel Lezcano 已提交
94
{
95
	int i;
D
Daniel Lezcano 已提交
96

97
	drv->refcnt = 0;
D
Daniel Lezcano 已提交
98

99 100
	if (!drv->cpumask)
		drv->cpumask = (struct cpumask *)cpu_possible_mask;
D
Daniel Lezcano 已提交
101

102
	for (i = drv->state_count - 1; i >= 0 ; i--) {
D
Daniel Lezcano 已提交
103

104 105 106 107 108 109 110 111
		if (!(drv->states[i].flags & CPUIDLE_FLAG_TIMER_STOP))
			continue;

		drv->bctimer = 1;
		break;
	}

	return 0;
D
Daniel Lezcano 已提交
112 113
}

114
static int __cpuidle_register_driver(struct cpuidle_driver *drv)
D
Daniel Lezcano 已提交
115 116 117
{
	int ret;

118 119
	if (!drv || !drv->state_count)
		return -EINVAL;
D
Daniel Lezcano 已提交
120

121 122
	if (cpuidle_disabled())
		return -ENODEV;
D
Daniel Lezcano 已提交
123

124 125 126
	ret = __cpuidle_driver_init(drv);
	if (ret)
		return ret;
127

128 129 130
	ret = __cpuidle_set_driver(drv);
	if (ret)
		return ret;
131

132 133 134
	if (drv->bctimer)
		on_each_cpu_mask(drv->cpumask, cpuidle_setup_broadcast_timer,
				 (void *)CLOCK_EVT_NOTIFY_BROADCAST_ON, 1);
135

136
	return 0;
137 138
}

139
/**
D
Daniel Lezcano 已提交
140 141
 * cpuidle_unregister_driver - unregisters a driver
 * @drv: the driver
142
 */
143
static void __cpuidle_unregister_driver(struct cpuidle_driver *drv)
D
Daniel Lezcano 已提交
144
{
145 146
	if (WARN_ON(drv->refcnt > 0))
		return;
D
Daniel Lezcano 已提交
147

148 149 150 151 152
	if (drv->bctimer) {
		drv->bctimer = 0;
		on_each_cpu_mask(drv->cpumask, cpuidle_setup_broadcast_timer,
				 (void *)CLOCK_EVT_NOTIFY_BROADCAST_OFF, 1);
	}
D
Daniel Lezcano 已提交
153

154
	__cpuidle_unset_driver(drv);
155
}
D
Daniel Lezcano 已提交
156 157 158 159 160 161 162

/**
 * cpuidle_register_driver - registers a driver
 * @drv: the driver
 */
int cpuidle_register_driver(struct cpuidle_driver *drv)
{
163
	int ret;
D
Daniel Lezcano 已提交
164 165

	spin_lock(&cpuidle_driver_lock);
166
	ret = __cpuidle_register_driver(drv);
D
Daniel Lezcano 已提交
167 168 169 170 171
	spin_unlock(&cpuidle_driver_lock);

	return ret;
}
EXPORT_SYMBOL_GPL(cpuidle_register_driver);
172

173 174 175 176 177 178 179
/**
 * cpuidle_unregister_driver - unregisters a driver
 * @drv: the driver
 */
void cpuidle_unregister_driver(struct cpuidle_driver *drv)
{
	spin_lock(&cpuidle_driver_lock);
180
	__cpuidle_unregister_driver(drv);
181 182 183
	spin_unlock(&cpuidle_driver_lock);
}
EXPORT_SYMBOL_GPL(cpuidle_unregister_driver);
D
Daniel Lezcano 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208

/**
 * cpuidle_get_driver - return the current driver
 */
struct cpuidle_driver *cpuidle_get_driver(void)
{
	struct cpuidle_driver *drv;
	int cpu;

	cpu = get_cpu();
	drv = __cpuidle_get_cpu_driver(cpu);
	put_cpu();

	return drv;
}
EXPORT_SYMBOL_GPL(cpuidle_get_driver);

/**
 * cpuidle_get_cpu_driver - return the driver tied with a cpu
 */
struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev)
{
	if (!dev)
		return NULL;

209
	return __cpuidle_get_cpu_driver(dev->cpu);
D
Daniel Lezcano 已提交
210 211
}
EXPORT_SYMBOL_GPL(cpuidle_get_cpu_driver);
212 213 214 215 216 217 218

struct cpuidle_driver *cpuidle_driver_ref(void)
{
	struct cpuidle_driver *drv;

	spin_lock(&cpuidle_driver_lock);

219
	drv = cpuidle_get_driver();
220
	drv->refcnt++;
221 222 223 224 225 226 227

	spin_unlock(&cpuidle_driver_lock);
	return drv;
}

void cpuidle_driver_unref(void)
{
228
	struct cpuidle_driver *drv = cpuidle_get_driver();
229

230 231
	spin_lock(&cpuidle_driver_lock);

232 233
	if (drv && !WARN_ON(drv->refcnt <= 0))
		drv->refcnt--;
234 235 236

	spin_unlock(&cpuidle_driver_lock);
}