Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame] | 1 | #include <linux/errno.h> |
| 2 | #include <linux/kernel.h> |
| 3 | #include <linux/mm.h> |
| 4 | #include <linux/smp.h> |
| 5 | #include <linux/slab.h> |
| 6 | #include <linux/sched.h> |
Peter Zijlstra | 7f424a8 | 2008-04-25 17:39:01 +0200 | [diff] [blame] | 7 | #include <linux/module.h> |
| 8 | #include <linux/pm.h> |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame] | 9 | |
Suresh Siddha | aa283f4 | 2008-03-10 15:28:05 -0700 | [diff] [blame] | 10 | struct kmem_cache *task_xstate_cachep; |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame] | 11 | |
| 12 | int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src) |
| 13 | { |
| 14 | *dst = *src; |
Suresh Siddha | aa283f4 | 2008-03-10 15:28:05 -0700 | [diff] [blame] | 15 | if (src->thread.xstate) { |
| 16 | dst->thread.xstate = kmem_cache_alloc(task_xstate_cachep, |
| 17 | GFP_KERNEL); |
| 18 | if (!dst->thread.xstate) |
| 19 | return -ENOMEM; |
| 20 | WARN_ON((unsigned long)dst->thread.xstate & 15); |
| 21 | memcpy(dst->thread.xstate, src->thread.xstate, xstate_size); |
| 22 | } |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame] | 23 | return 0; |
| 24 | } |
| 25 | |
Suresh Siddha | aa283f4 | 2008-03-10 15:28:05 -0700 | [diff] [blame] | 26 | void free_thread_xstate(struct task_struct *tsk) |
| 27 | { |
| 28 | if (tsk->thread.xstate) { |
| 29 | kmem_cache_free(task_xstate_cachep, tsk->thread.xstate); |
| 30 | tsk->thread.xstate = NULL; |
| 31 | } |
| 32 | } |
| 33 | |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame] | 34 | void free_thread_info(struct thread_info *ti) |
| 35 | { |
Suresh Siddha | aa283f4 | 2008-03-10 15:28:05 -0700 | [diff] [blame] | 36 | free_thread_xstate(ti->task); |
Suresh Siddha | 1679f27 | 2008-04-16 10:27:53 +0200 | [diff] [blame] | 37 | free_pages((unsigned long)ti, get_order(THREAD_SIZE)); |
Suresh Siddha | 61c4628 | 2008-03-10 15:28:04 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | void arch_task_cache_init(void) |
| 41 | { |
| 42 | task_xstate_cachep = |
| 43 | kmem_cache_create("task_xstate", xstate_size, |
| 44 | __alignof__(union thread_xstate), |
| 45 | SLAB_PANIC, NULL); |
| 46 | } |
Peter Zijlstra | 7f424a8 | 2008-04-25 17:39:01 +0200 | [diff] [blame] | 47 | |
Thomas Gleixner | 00dba56 | 2008-06-09 18:35:28 +0200 | [diff] [blame^] | 48 | /* |
| 49 | * Idle related variables and functions |
| 50 | */ |
| 51 | unsigned long boot_option_idle_override = 0; |
| 52 | EXPORT_SYMBOL(boot_option_idle_override); |
| 53 | |
| 54 | /* |
| 55 | * Powermanagement idle function, if any.. |
| 56 | */ |
| 57 | void (*pm_idle)(void); |
| 58 | EXPORT_SYMBOL(pm_idle); |
| 59 | |
| 60 | #ifdef CONFIG_X86_32 |
| 61 | /* |
| 62 | * This halt magic was a workaround for ancient floppy DMA |
| 63 | * wreckage. It should be safe to remove. |
| 64 | */ |
| 65 | static int hlt_counter; |
| 66 | void disable_hlt(void) |
| 67 | { |
| 68 | hlt_counter++; |
| 69 | } |
| 70 | EXPORT_SYMBOL(disable_hlt); |
| 71 | |
| 72 | void enable_hlt(void) |
| 73 | { |
| 74 | hlt_counter--; |
| 75 | } |
| 76 | EXPORT_SYMBOL(enable_hlt); |
| 77 | |
| 78 | static inline int hlt_use_halt(void) |
| 79 | { |
| 80 | return (!hlt_counter && boot_cpu_data.hlt_works_ok); |
| 81 | } |
| 82 | #else |
| 83 | static inline int hlt_use_halt(void) |
| 84 | { |
| 85 | return 1; |
| 86 | } |
| 87 | #endif |
| 88 | |
| 89 | /* |
| 90 | * We use this if we don't have any better |
| 91 | * idle routine.. |
| 92 | */ |
| 93 | void default_idle(void) |
| 94 | { |
| 95 | if (hlt_use_halt()) { |
| 96 | current_thread_info()->status &= ~TS_POLLING; |
| 97 | /* |
| 98 | * TS_POLLING-cleared state must be visible before we |
| 99 | * test NEED_RESCHED: |
| 100 | */ |
| 101 | smp_mb(); |
| 102 | |
| 103 | if (!need_resched()) |
| 104 | safe_halt(); /* enables interrupts racelessly */ |
| 105 | else |
| 106 | local_irq_enable(); |
| 107 | current_thread_info()->status |= TS_POLLING; |
| 108 | } else { |
| 109 | local_irq_enable(); |
| 110 | /* loop is done by the caller */ |
| 111 | cpu_relax(); |
| 112 | } |
| 113 | } |
| 114 | #ifdef CONFIG_APM_MODULE |
| 115 | EXPORT_SYMBOL(default_idle); |
| 116 | #endif |
| 117 | |
Peter Zijlstra | 7f424a8 | 2008-04-25 17:39:01 +0200 | [diff] [blame] | 118 | static void do_nothing(void *unused) |
| 119 | { |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | * cpu_idle_wait - Used to ensure that all the CPUs discard old value of |
| 124 | * pm_idle and update to new pm_idle value. Required while changing pm_idle |
| 125 | * handler on SMP systems. |
| 126 | * |
| 127 | * Caller must have changed pm_idle to the new value before the call. Old |
| 128 | * pm_idle value will not be used by any CPU after the return of this function. |
| 129 | */ |
| 130 | void cpu_idle_wait(void) |
| 131 | { |
| 132 | smp_mb(); |
| 133 | /* kick all the CPUs so that they exit out of pm_idle */ |
| 134 | smp_call_function(do_nothing, NULL, 0, 1); |
| 135 | } |
| 136 | EXPORT_SYMBOL_GPL(cpu_idle_wait); |
| 137 | |
| 138 | /* |
| 139 | * This uses new MONITOR/MWAIT instructions on P4 processors with PNI, |
| 140 | * which can obviate IPI to trigger checking of need_resched. |
| 141 | * We execute MONITOR against need_resched and enter optimized wait state |
| 142 | * through MWAIT. Whenever someone changes need_resched, we would be woken |
| 143 | * up from MWAIT (without an IPI). |
| 144 | * |
| 145 | * New with Core Duo processors, MWAIT can take some hints based on CPU |
| 146 | * capability. |
| 147 | */ |
| 148 | void mwait_idle_with_hints(unsigned long ax, unsigned long cx) |
| 149 | { |
| 150 | if (!need_resched()) { |
| 151 | __monitor((void *)¤t_thread_info()->flags, 0, 0); |
| 152 | smp_mb(); |
| 153 | if (!need_resched()) |
| 154 | __mwait(ax, cx); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /* Default MONITOR/MWAIT with no hints, used for default C1 state */ |
| 159 | static void mwait_idle(void) |
| 160 | { |
| 161 | if (!need_resched()) { |
| 162 | __monitor((void *)¤t_thread_info()->flags, 0, 0); |
| 163 | smp_mb(); |
| 164 | if (!need_resched()) |
| 165 | __sti_mwait(0, 0); |
| 166 | else |
| 167 | local_irq_enable(); |
| 168 | } else |
| 169 | local_irq_enable(); |
| 170 | } |
| 171 | |
Peter Zijlstra | 7f424a8 | 2008-04-25 17:39:01 +0200 | [diff] [blame] | 172 | /* |
| 173 | * On SMP it's slightly faster (but much more power-consuming!) |
| 174 | * to poll the ->work.need_resched flag instead of waiting for the |
| 175 | * cross-CPU IPI to arrive. Use this option with caution. |
| 176 | */ |
| 177 | static void poll_idle(void) |
| 178 | { |
| 179 | local_irq_enable(); |
| 180 | cpu_relax(); |
| 181 | } |
| 182 | |
Thomas Gleixner | e9623b3 | 2008-05-16 22:55:26 +0200 | [diff] [blame] | 183 | /* |
| 184 | * mwait selection logic: |
| 185 | * |
| 186 | * It depends on the CPU. For AMD CPUs that support MWAIT this is |
| 187 | * wrong. Family 0x10 and 0x11 CPUs will enter C1 on HLT. Powersavings |
| 188 | * then depend on a clock divisor and current Pstate of the core. If |
| 189 | * all cores of a processor are in halt state (C1) the processor can |
| 190 | * enter the C1E (C1 enhanced) state. If mwait is used this will never |
| 191 | * happen. |
| 192 | * |
| 193 | * idle=mwait overrides this decision and forces the usage of mwait. |
| 194 | */ |
Thomas Gleixner | 09fd4b4 | 2008-06-09 18:04:27 +0200 | [diff] [blame] | 195 | |
| 196 | #define MWAIT_INFO 0x05 |
| 197 | #define MWAIT_ECX_EXTENDED_INFO 0x01 |
| 198 | #define MWAIT_EDX_C1 0xf0 |
| 199 | |
Thomas Gleixner | e9623b3 | 2008-05-16 22:55:26 +0200 | [diff] [blame] | 200 | static int __cpuinit mwait_usable(const struct cpuinfo_x86 *c) |
| 201 | { |
Thomas Gleixner | 09fd4b4 | 2008-06-09 18:04:27 +0200 | [diff] [blame] | 202 | u32 eax, ebx, ecx, edx; |
| 203 | |
Thomas Gleixner | e9623b3 | 2008-05-16 22:55:26 +0200 | [diff] [blame] | 204 | if (force_mwait) |
| 205 | return 1; |
| 206 | |
Thomas Gleixner | 09fd4b4 | 2008-06-09 18:04:27 +0200 | [diff] [blame] | 207 | if (c->cpuid_level < MWAIT_INFO) |
| 208 | return 0; |
| 209 | |
| 210 | cpuid(MWAIT_INFO, &eax, &ebx, &ecx, &edx); |
| 211 | /* Check, whether EDX has extended info about MWAIT */ |
| 212 | if (!(ecx & MWAIT_ECX_EXTENDED_INFO)) |
| 213 | return 1; |
| 214 | |
| 215 | /* |
| 216 | * edx enumeratios MONITOR/MWAIT extensions. Check, whether |
| 217 | * C1 supports MWAIT |
| 218 | */ |
| 219 | return (edx & MWAIT_EDX_C1); |
Thomas Gleixner | e9623b3 | 2008-05-16 22:55:26 +0200 | [diff] [blame] | 220 | } |
| 221 | |
Peter Zijlstra | 7f424a8 | 2008-04-25 17:39:01 +0200 | [diff] [blame] | 222 | void __cpuinit select_idle_routine(const struct cpuinfo_x86 *c) |
| 223 | { |
Peter Zijlstra | 7f424a8 | 2008-04-25 17:39:01 +0200 | [diff] [blame] | 224 | #ifdef CONFIG_X86_SMP |
| 225 | if (pm_idle == poll_idle && smp_num_siblings > 1) { |
| 226 | printk(KERN_WARNING "WARNING: polling idle and HT enabled," |
| 227 | " performance may degrade.\n"); |
| 228 | } |
| 229 | #endif |
Thomas Gleixner | 6ddd2a2 | 2008-06-09 16:59:53 +0200 | [diff] [blame] | 230 | if (pm_idle) |
| 231 | return; |
| 232 | |
Thomas Gleixner | e9623b3 | 2008-05-16 22:55:26 +0200 | [diff] [blame] | 233 | if (cpu_has(c, X86_FEATURE_MWAIT) && mwait_usable(c)) { |
Peter Zijlstra | 7f424a8 | 2008-04-25 17:39:01 +0200 | [diff] [blame] | 234 | /* |
Peter Zijlstra | 7f424a8 | 2008-04-25 17:39:01 +0200 | [diff] [blame] | 235 | * One CPU supports mwait => All CPUs supports mwait |
| 236 | */ |
Thomas Gleixner | 6ddd2a2 | 2008-06-09 16:59:53 +0200 | [diff] [blame] | 237 | printk(KERN_INFO "using mwait in idle threads.\n"); |
| 238 | pm_idle = mwait_idle; |
| 239 | } else |
| 240 | pm_idle = default_idle; |
Peter Zijlstra | 7f424a8 | 2008-04-25 17:39:01 +0200 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | static int __init idle_setup(char *str) |
| 244 | { |
| 245 | if (!strcmp(str, "poll")) { |
| 246 | printk("using polling idle threads.\n"); |
| 247 | pm_idle = poll_idle; |
| 248 | } else if (!strcmp(str, "mwait")) |
| 249 | force_mwait = 1; |
| 250 | else |
| 251 | return -1; |
| 252 | |
| 253 | boot_option_idle_override = 1; |
| 254 | return 0; |
| 255 | } |
| 256 | early_param("idle", idle_setup); |
| 257 | |