2 * Copyright (c) 2010 Mellanox Technologies. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 #include <linux/sched.h>
35 #include <linux/mutex.h>
36 #include <asm/atomic.h>
40 #if defined(CONFIG_X86) && defined(CONFIG_APM_MODULE)
44 /* Each CPU is put into a group. In most cases, the group number is
45 * equal to the CPU number of one of the CPUs in the group. The
46 * exception is group NR_CPUS which is the default group. This is
47 * protected by sys_tune_startup_mutex. */
48 DEFINE_PER_CPU(int, idle_cpu_group) = NR_CPUS;
50 /* For each group, a count of the number of CPUs in the group which
51 * are known to be busy. A busy CPU might be running the busy loop
52 * below or general kernel code. The count is decremented on entry to
53 * the old pm_idle handler and incremented on exit. The aim is to
54 * avoid the count going to zero or negative. This situation can
55 * occur temporarily during module unload or CPU hot-plug but
56 * normality will be restored when the affected CPUs next exit the
58 static atomic_t busy_cpu_count[NR_CPUS+1];
60 /* A workqueue item to be executed to cause the CPU to exit from the
62 DEFINE_PER_CPU(struct work_struct, sys_tune_cpu_work);
64 #define sys_tune_set_state(CPU,STATE) \
68 /* A mutex to protect most of the module datastructures. */
69 static DEFINE_MUTEX(sys_tune_startup_mutex);
71 /* The old pm_idle handler. */
72 static void (*old_pm_idle)(void) = NULL;
74 static void sys_tune_pm_idle(void)
76 atomic_t *busy_cpus_ptr;
78 int cpu = smp_processor_id();
80 busy_cpus_ptr = &(busy_cpu_count[per_cpu(idle_cpu_group, cpu)]);
82 sys_tune_set_state(cpu, 2);
85 while (!need_resched()) {
86 busy_cpus = atomic_read(busy_cpus_ptr);
88 /* If other CPUs in this group are busy then let this
89 * CPU go idle. We mustn't let the number of busy
90 * CPUs drop below 1. */
92 old_pm_idle != NULL &&
93 ( atomic_cmpxchg(busy_cpus_ptr, busy_cpus,
94 busy_cpus-1) == busy_cpus ) ) {
96 sys_tune_set_state(cpu, 3);
97 /* This check might not be necessary, but it
98 * seems safest to include it because there
99 * might be a kernel version which requires
105 /* This CPU is busy again. */
106 sys_tune_set_state(cpu, 1);
107 atomic_add(1, busy_cpus_ptr);
113 sys_tune_set_state(cpu, 0);
117 void sys_tune_work_func(struct work_struct *work)
119 /* Do nothing. Since this function is running in process
120 * context, the idle thread isn't running on this CPU. */
125 static void sys_tune_smp_call(void *info)
127 schedule_work(&get_cpu_var(sys_tune_cpu_work));
128 put_cpu_var(sys_tune_cpu_work);
134 static void sys_tune_refresh(void)
136 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
137 on_each_cpu(&sys_tune_smp_call, NULL, 0, 1);
139 on_each_cpu(&sys_tune_smp_call, NULL, 1);
143 static void sys_tune_refresh(void)
145 /* The current thread is executing on the one and only CPU so
146 * the idle thread isn't running. */
152 static int sys_tune_cpu_group(int cpu)
155 const cpumask_t *mask;
159 #if defined(topology_thread_cpumask) && defined(ST_HAVE_EXPORTED_CPU_SIBLING_MAP)
160 /* Keep one hyperthread busy per core. */
161 mask = topology_thread_cpumask(cpu);
165 for_each_cpu_mask(cpu, *(mask)) {
166 group = per_cpu(idle_cpu_group, other_cpu);
167 if (group != NR_CPUS)
176 static void sys_tune_add_cpu(int cpu)
180 /* Do nothing if this CPU has already been added. */
181 if (per_cpu(idle_cpu_group, cpu) != NR_CPUS)
184 group = sys_tune_cpu_group(cpu);
185 per_cpu(idle_cpu_group, cpu) = group;
186 atomic_inc(&(busy_cpu_count[group]));
190 static void sys_tune_del_cpu(int cpu)
195 if (per_cpu(idle_cpu_group, cpu) == NR_CPUS)
198 group = per_cpu(idle_cpu_group, cpu);
199 /* If the CPU was busy, this can cause the count to drop to
200 * zero. To rectify this, we need to cause one of the other
201 * CPUs in the group to exit the idle loop. If the CPU was
202 * not busy then this causes the contribution for this CPU to
203 * go to -1 which can cause the overall count to drop to zero
204 * or go negative. To rectify this situation we need to cause
205 * this CPU to exit the idle loop. */
206 atomic_dec(&(busy_cpu_count[group]));
207 per_cpu(idle_cpu_group, cpu) = NR_CPUS;
212 static int sys_tune_cpu_notify(struct notifier_block *self,
213 unsigned long action, void *hcpu)
215 int cpu = (long)hcpu;
218 #ifdef CPU_ONLINE_FROZEN
219 case CPU_ONLINE_FROZEN:
222 mutex_lock(&sys_tune_startup_mutex);
223 sys_tune_add_cpu(cpu);
224 mutex_unlock(&sys_tune_startup_mutex);
225 /* The CPU might have already entered the idle loop in
226 * the wrong group. Make sure it exits the idle loop
227 * so that it picks up the correct group. */
231 #ifdef CPU_DEAD_FROZEN
232 case CPU_DEAD_FROZEN:
235 mutex_lock(&sys_tune_startup_mutex);
236 sys_tune_del_cpu(cpu);
237 mutex_unlock(&sys_tune_startup_mutex);
238 /* The deleted CPU may have been the only busy CPU in
239 * the group. Make sure one of the other CPUs in the
240 * group exits the idle loop. */
248 static struct notifier_block sys_tune_cpu_nb = {
249 .notifier_call = sys_tune_cpu_notify,
253 static void sys_tune_ensure_init(void)
255 BUG_ON (old_pm_idle != NULL);
257 /* Atomically update pm_idle to &sys_tune_pm_idle. The old value
258 * is stored in old_pm_idle before installing the new
261 old_pm_idle = pm_idle;
262 } while (cmpxchg(&pm_idle, old_pm_idle, &sys_tune_pm_idle) !=
267 void sys_tune_fini(void)
269 #if defined(CONFIG_X86) && defined(CONFIG_APM_MODULE)
273 unregister_cpu_notifier(&sys_tune_cpu_nb);
275 mutex_lock(&sys_tune_startup_mutex);
278 old = cmpxchg(&pm_idle, &sys_tune_pm_idle, old_pm_idle);
280 for_each_online_cpu(cpu)
281 sys_tune_del_cpu(cpu);
283 mutex_unlock(&sys_tune_startup_mutex);
285 /* Our handler may still be executing on other CPUs.
286 * Schedule this thread on all CPUs to make sure all
287 * idle threads get interrupted. */
290 /* Make sure the work item has finished executing on all CPUs.
291 * This in turn ensures that all idle threads have been
293 flush_scheduled_work();
294 #endif /* CONFIG_X86 */
297 void sys_tune_init(void)
299 #if defined(CONFIG_X86) && defined(CONFIG_APM_MODULE)
302 for_each_possible_cpu(cpu) {
303 INIT_WORK(&per_cpu(sys_tune_cpu_work, cpu),
307 /* Start by registering the handler to ensure we don't miss
309 register_cpu_notifier(&sys_tune_cpu_nb);
311 mutex_lock(&sys_tune_startup_mutex);
313 for_each_online_cpu(cpu)
314 sys_tune_add_cpu(cpu);
316 sys_tune_ensure_init();
319 mutex_unlock(&sys_tune_startup_mutex);
321 /* Ensure our idle handler starts to run. */