Skip to content

Commit 82467a5

Browse files
Daniel Lezcanorafaeljw
authored andcommitted
cpuidle: simplify multiple driver support
Commit bf4d1b5 (cpuidle: support multiple drivers) introduced support for using multiple cpuidle drivers at the same time. It added a couple of new APIs to register the driver per CPU, but that led to some unnecessary code complexity related to the kernel config options deciding whether or not the multiple driver support is enabled. The code has to work as it did before when the multiple driver support is not enabled and the multiple driver support has to be compatible with the previously existing API. Remove the new API, not used by any driver in the tree yet (but needed for the HMP cpuidle drivers that will be submitted soon), and add a new cpumask pointer to the cpuidle driver structure that will point to the mask of CPUs handled by the given driver. That will allow the cpuidle_[un]register_driver() API to be used for the multiple driver support along with the cpuidle_[un]register() functions added recently. [rjw: Changelog] Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1 parent bd2a337 commit 82467a5

3 files changed

Lines changed: 84 additions & 118 deletions

File tree

‎drivers/cpuidle/cpuidle.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ void cpuidle_unregister(struct cpuidle_driver *drv)
466466
int cpu;
467467
struct cpuidle_device *device;
468468

469-
for_each_possible_cpu(cpu) {
469+
for_each_cpu(cpu, drv->cpumask) {
470470
device = &per_cpu(cpuidle_dev, cpu);
471471
cpuidle_unregister_device(device);
472472
}
@@ -498,7 +498,7 @@ int cpuidle_register(struct cpuidle_driver *drv,
498498
return ret;
499499
}
500500

501-
for_each_possible_cpu(cpu) {
501+
for_each_cpu(cpu, drv->cpumask) {
502502
device = &per_cpu(cpuidle_dev, cpu);
503503
device->cpu = cpu;
504504

‎drivers/cpuidle/driver.c‎

Lines changed: 79 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -18,167 +18,140 @@
1818

1919
DEFINE_SPINLOCK(cpuidle_driver_lock);
2020

21-
static void __cpuidle_set_cpu_driver(struct cpuidle_driver *drv, int cpu);
22-
static struct cpuidle_driver * __cpuidle_get_cpu_driver(int cpu);
21+
#ifdef CONFIG_CPU_IDLE_MULTIPLE_DRIVERS
2322

24-
static void cpuidle_setup_broadcast_timer(void *arg)
23+
static DEFINE_PER_CPU(struct cpuidle_driver *, cpuidle_drivers);
24+
25+
static struct cpuidle_driver *__cpuidle_get_cpu_driver(int cpu)
2526
{
26-
int cpu = smp_processor_id();
27-
clockevents_notify((long)(arg), &cpu);
27+
return per_cpu(cpuidle_drivers, cpu);
2828
}
2929

30-
static void __cpuidle_driver_init(struct cpuidle_driver *drv, int cpu)
30+
static inline void __cpuidle_unset_driver(struct cpuidle_driver *drv)
3131
{
32-
int i;
33-
34-
drv->refcnt = 0;
32+
int cpu;
3533

36-
for (i = drv->state_count - 1; i >= 0 ; i--) {
34+
for_each_cpu(cpu, drv->cpumask) {
3735

38-
if (!(drv->states[i].flags & CPUIDLE_FLAG_TIMER_STOP))
36+
if (drv != __cpuidle_get_cpu_driver(cpu))
3937
continue;
4038

41-
drv->bctimer = 1;
42-
on_each_cpu_mask(get_cpu_mask(cpu), cpuidle_setup_broadcast_timer,
43-
(void *)CLOCK_EVT_NOTIFY_BROADCAST_ON, 1);
44-
break;
39+
per_cpu(cpuidle_drivers, cpu) = NULL;
4540
}
4641
}
4742

48-
static int __cpuidle_register_driver(struct cpuidle_driver *drv, int cpu)
43+
static inline int __cpuidle_set_driver(struct cpuidle_driver *drv)
4944
{
50-
if (!drv || !drv->state_count)
51-
return -EINVAL;
52-
53-
if (cpuidle_disabled())
54-
return -ENODEV;
45+
int cpu;
5546

56-
if (__cpuidle_get_cpu_driver(cpu))
57-
return -EBUSY;
47+
for_each_cpu(cpu, drv->cpumask) {
5848

59-
__cpuidle_driver_init(drv, cpu);
49+
if (__cpuidle_get_cpu_driver(cpu)) {
50+
__cpuidle_unset_driver(drv);
51+
return -EBUSY;
52+
}
6053

61-
__cpuidle_set_cpu_driver(drv, cpu);
54+
per_cpu(cpuidle_drivers, cpu) = drv;
55+
}
6256

6357
return 0;
6458
}
6559

66-
static void __cpuidle_unregister_driver(struct cpuidle_driver *drv, int cpu)
67-
{
68-
if (drv != __cpuidle_get_cpu_driver(cpu))
69-
return;
60+
#else
7061

71-
if (!WARN_ON(drv->refcnt > 0))
72-
__cpuidle_set_cpu_driver(NULL, cpu);
62+
static struct cpuidle_driver *cpuidle_curr_driver;
7363

74-
if (drv->bctimer) {
75-
drv->bctimer = 0;
76-
on_each_cpu_mask(get_cpu_mask(cpu), cpuidle_setup_broadcast_timer,
77-
(void *)CLOCK_EVT_NOTIFY_BROADCAST_OFF, 1);
78-
}
64+
static inline struct cpuidle_driver *__cpuidle_get_cpu_driver(int cpu)
65+
{
66+
return cpuidle_curr_driver;
7967
}
8068

81-
#ifdef CONFIG_CPU_IDLE_MULTIPLE_DRIVERS
69+
static inline int __cpuidle_set_driver(struct cpuidle_driver *drv)
70+
{
71+
if (cpuidle_curr_driver)
72+
return -EBUSY;
8273

83-
static DEFINE_PER_CPU(struct cpuidle_driver *, cpuidle_drivers);
74+
cpuidle_curr_driver = drv;
8475

85-
static void __cpuidle_set_cpu_driver(struct cpuidle_driver *drv, int cpu)
86-
{
87-
per_cpu(cpuidle_drivers, cpu) = drv;
76+
return 0;
8877
}
8978

90-
static struct cpuidle_driver *__cpuidle_get_cpu_driver(int cpu)
79+
static inline void __cpuidle_unset_driver(struct cpuidle_driver *drv)
9180
{
92-
return per_cpu(cpuidle_drivers, cpu);
81+
if (drv == cpuidle_curr_driver)
82+
cpuidle_curr_driver = NULL;
9383
}
9484

95-
static void __cpuidle_unregister_all_cpu_driver(struct cpuidle_driver *drv)
85+
#endif
86+
87+
static void cpuidle_setup_broadcast_timer(void *arg)
9688
{
97-
int cpu;
98-
for_each_present_cpu(cpu)
99-
__cpuidle_unregister_driver(drv, cpu);
89+
int cpu = smp_processor_id();
90+
clockevents_notify((long)(arg), &cpu);
10091
}
10192

102-
static int __cpuidle_register_all_cpu_driver(struct cpuidle_driver *drv)
93+
static int __cpuidle_driver_init(struct cpuidle_driver *drv)
10394
{
104-
int ret = 0;
105-
int i, cpu;
95+
int i;
10696

107-
for_each_present_cpu(cpu) {
108-
ret = __cpuidle_register_driver(drv, cpu);
109-
if (ret)
110-
break;
111-
}
97+
drv->refcnt = 0;
11298

113-
if (ret)
114-
for_each_present_cpu(i) {
115-
if (i == cpu)
116-
break;
117-
__cpuidle_unregister_driver(drv, i);
118-
}
99+
if (!drv->cpumask)
100+
drv->cpumask = (struct cpumask *)cpu_possible_mask;
119101

102+
for (i = drv->state_count - 1; i >= 0 ; i--) {
120103

121-
return ret;
104+
if (!(drv->states[i].flags & CPUIDLE_FLAG_TIMER_STOP))
105+
continue;
106+
107+
drv->bctimer = 1;
108+
break;
109+
}
110+
111+
return 0;
122112
}
123113

124-
int cpuidle_register_cpu_driver(struct cpuidle_driver *drv, int cpu)
114+
static int __cpuidle_register_driver(struct cpuidle_driver *drv)
125115
{
126116
int ret;
127117

128-
spin_lock(&cpuidle_driver_lock);
129-
ret = __cpuidle_register_driver(drv, cpu);
130-
spin_unlock(&cpuidle_driver_lock);
118+
if (!drv || !drv->state_count)
119+
return -EINVAL;
131120

132-
return ret;
133-
}
121+
if (cpuidle_disabled())
122+
return -ENODEV;
134123

135-
void cpuidle_unregister_cpu_driver(struct cpuidle_driver *drv, int cpu)
136-
{
137-
spin_lock(&cpuidle_driver_lock);
138-
__cpuidle_unregister_driver(drv, cpu);
139-
spin_unlock(&cpuidle_driver_lock);
140-
}
124+
ret = __cpuidle_driver_init(drv);
125+
if (ret)
126+
return ret;
141127

142-
/**
143-
* cpuidle_register_driver - registers a driver
144-
* @drv: the driver
145-
*/
146-
int cpuidle_register_driver(struct cpuidle_driver *drv)
147-
{
148-
int ret;
128+
ret = __cpuidle_set_driver(drv);
129+
if (ret)
130+
return ret;
149131

150-
spin_lock(&cpuidle_driver_lock);
151-
ret = __cpuidle_register_all_cpu_driver(drv);
152-
spin_unlock(&cpuidle_driver_lock);
132+
if (drv->bctimer)
133+
on_each_cpu_mask(drv->cpumask, cpuidle_setup_broadcast_timer,
134+
(void *)CLOCK_EVT_NOTIFY_BROADCAST_ON, 1);
153135

154-
return ret;
136+
return 0;
155137
}
156-
EXPORT_SYMBOL_GPL(cpuidle_register_driver);
157138

158139
/**
159140
* cpuidle_unregister_driver - unregisters a driver
160141
* @drv: the driver
161142
*/
162-
void cpuidle_unregister_driver(struct cpuidle_driver *drv)
143+
static void __cpuidle_unregister_driver(struct cpuidle_driver *drv)
163144
{
164-
spin_lock(&cpuidle_driver_lock);
165-
__cpuidle_unregister_all_cpu_driver(drv);
166-
spin_unlock(&cpuidle_driver_lock);
167-
}
168-
EXPORT_SYMBOL_GPL(cpuidle_unregister_driver);
169-
170-
#else
171-
172-
static struct cpuidle_driver *cpuidle_curr_driver;
145+
if (WARN_ON(drv->refcnt > 0))
146+
return;
173147

174-
static inline void __cpuidle_set_cpu_driver(struct cpuidle_driver *drv, int cpu)
175-
{
176-
cpuidle_curr_driver = drv;
177-
}
148+
if (drv->bctimer) {
149+
drv->bctimer = 0;
150+
on_each_cpu_mask(drv->cpumask, cpuidle_setup_broadcast_timer,
151+
(void *)CLOCK_EVT_NOTIFY_BROADCAST_OFF, 1);
152+
}
178153

179-
static inline struct cpuidle_driver *__cpuidle_get_cpu_driver(int cpu)
180-
{
181-
return cpuidle_curr_driver;
154+
__cpuidle_unset_driver(drv);
182155
}
183156

184157
/**
@@ -187,13 +160,11 @@ static inline struct cpuidle_driver *__cpuidle_get_cpu_driver(int cpu)
187160
*/
188161
int cpuidle_register_driver(struct cpuidle_driver *drv)
189162
{
190-
int ret, cpu;
163+
int ret;
191164

192-
cpu = get_cpu();
193165
spin_lock(&cpuidle_driver_lock);
194-
ret = __cpuidle_register_driver(drv, cpu);
166+
ret = __cpuidle_register_driver(drv);
195167
spin_unlock(&cpuidle_driver_lock);
196-
put_cpu();
197168

198169
return ret;
199170
}
@@ -205,16 +176,11 @@ EXPORT_SYMBOL_GPL(cpuidle_register_driver);
205176
*/
206177
void cpuidle_unregister_driver(struct cpuidle_driver *drv)
207178
{
208-
int cpu;
209-
210-
cpu = get_cpu();
211179
spin_lock(&cpuidle_driver_lock);
212-
__cpuidle_unregister_driver(drv, cpu);
180+
__cpuidle_unregister_driver(drv);
213181
spin_unlock(&cpuidle_driver_lock);
214-
put_cpu();
215182
}
216183
EXPORT_SYMBOL_GPL(cpuidle_unregister_driver);
217-
#endif
218184

219185
/**
220186
* cpuidle_get_driver - return the current driver

‎include/linux/cpuidle.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ struct cpuidle_driver {
111111
struct cpuidle_state states[CPUIDLE_STATE_MAX];
112112
int state_count;
113113
int safe_state_index;
114+
115+
/* the driver handles the cpus in cpumask */
116+
struct cpumask *cpumask;
114117
};
115118

116119
#ifdef CONFIG_CPU_IDLE
@@ -135,9 +138,6 @@ extern void cpuidle_disable_device(struct cpuidle_device *dev);
135138
extern int cpuidle_play_dead(void);
136139

137140
extern struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev);
138-
extern int cpuidle_register_cpu_driver(struct cpuidle_driver *drv, int cpu);
139-
extern void cpuidle_unregister_cpu_driver(struct cpuidle_driver *drv, int cpu);
140-
141141
#else
142142
static inline void disable_cpuidle(void) { }
143143
static inline int cpuidle_idle_call(void) { return -ENODEV; }

0 commit comments

Comments
 (0)