process.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. #include <linux/errno.h>
  2. #include <linux/kernel.h>
  3. #include <linux/mm.h>
  4. #include <asm/idle.h>
  5. #include <linux/smp.h>
  6. #include <linux/slab.h>
  7. #include <linux/sched.h>
  8. #include <linux/module.h>
  9. #include <linux/pm.h>
  10. #include <linux/clockchips.h>
  11. #include <asm/system.h>
  12. #include <asm/apic.h>
  13. unsigned long idle_halt;
  14. EXPORT_SYMBOL(idle_halt);
  15. unsigned long idle_nomwait;
  16. EXPORT_SYMBOL(idle_nomwait);
  17. struct kmem_cache *task_xstate_cachep;
  18. int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
  19. {
  20. *dst = *src;
  21. if (src->thread.xstate) {
  22. dst->thread.xstate = kmem_cache_alloc(task_xstate_cachep,
  23. GFP_KERNEL);
  24. if (!dst->thread.xstate)
  25. return -ENOMEM;
  26. WARN_ON((unsigned long)dst->thread.xstate & 15);
  27. memcpy(dst->thread.xstate, src->thread.xstate, xstate_size);
  28. }
  29. return 0;
  30. }
  31. void free_thread_xstate(struct task_struct *tsk)
  32. {
  33. if (tsk->thread.xstate) {
  34. kmem_cache_free(task_xstate_cachep, tsk->thread.xstate);
  35. tsk->thread.xstate = NULL;
  36. }
  37. }
  38. void free_thread_info(struct thread_info *ti)
  39. {
  40. free_thread_xstate(ti->task);
  41. free_pages((unsigned long)ti, get_order(THREAD_SIZE));
  42. }
  43. void arch_task_cache_init(void)
  44. {
  45. task_xstate_cachep =
  46. kmem_cache_create("task_xstate", xstate_size,
  47. __alignof__(union thread_xstate),
  48. SLAB_PANIC, NULL);
  49. }
  50. /*
  51. * Idle related variables and functions
  52. */
  53. unsigned long boot_option_idle_override = 0;
  54. EXPORT_SYMBOL(boot_option_idle_override);
  55. /*
  56. * Powermanagement idle function, if any..
  57. */
  58. void (*pm_idle)(void);
  59. EXPORT_SYMBOL(pm_idle);
  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. void enable_hlt(void)
  72. {
  73. hlt_counter--;
  74. }
  75. EXPORT_SYMBOL(enable_hlt);
  76. static inline int hlt_use_halt(void)
  77. {
  78. return (!hlt_counter && boot_cpu_data.hlt_works_ok);
  79. }
  80. #else
  81. static inline int hlt_use_halt(void)
  82. {
  83. return 1;
  84. }
  85. #endif
  86. /*
  87. * We use this if we don't have any better
  88. * idle routine..
  89. */
  90. void default_idle(void)
  91. {
  92. if (hlt_use_halt()) {
  93. current_thread_info()->status &= ~TS_POLLING;
  94. /*
  95. * TS_POLLING-cleared state must be visible before we
  96. * test NEED_RESCHED:
  97. */
  98. smp_mb();
  99. if (!need_resched())
  100. safe_halt(); /* enables interrupts racelessly */
  101. else
  102. local_irq_enable();
  103. current_thread_info()->status |= TS_POLLING;
  104. } else {
  105. local_irq_enable();
  106. /* loop is done by the caller */
  107. cpu_relax();
  108. }
  109. }
  110. #ifdef CONFIG_APM_MODULE
  111. EXPORT_SYMBOL(default_idle);
  112. #endif
  113. void stop_this_cpu(void *dummy)
  114. {
  115. local_irq_disable();
  116. /*
  117. * Remove this CPU:
  118. */
  119. cpu_clear(smp_processor_id(), cpu_online_map);
  120. disable_local_APIC();
  121. for (;;) {
  122. if (hlt_works(smp_processor_id()))
  123. halt();
  124. }
  125. }
  126. static void do_nothing(void *unused)
  127. {
  128. }
  129. /*
  130. * cpu_idle_wait - Used to ensure that all the CPUs discard old value of
  131. * pm_idle and update to new pm_idle value. Required while changing pm_idle
  132. * handler on SMP systems.
  133. *
  134. * Caller must have changed pm_idle to the new value before the call. Old
  135. * pm_idle value will not be used by any CPU after the return of this function.
  136. */
  137. void cpu_idle_wait(void)
  138. {
  139. smp_mb();
  140. /* kick all the CPUs so that they exit out of pm_idle */
  141. smp_call_function(do_nothing, NULL, 1);
  142. }
  143. EXPORT_SYMBOL_GPL(cpu_idle_wait);
  144. /*
  145. * This uses new MONITOR/MWAIT instructions on P4 processors with PNI,
  146. * which can obviate IPI to trigger checking of need_resched.
  147. * We execute MONITOR against need_resched and enter optimized wait state
  148. * through MWAIT. Whenever someone changes need_resched, we would be woken
  149. * up from MWAIT (without an IPI).
  150. *
  151. * New with Core Duo processors, MWAIT can take some hints based on CPU
  152. * capability.
  153. */
  154. void mwait_idle_with_hints(unsigned long ax, unsigned long cx)
  155. {
  156. if (!need_resched()) {
  157. __monitor((void *)&current_thread_info()->flags, 0, 0);
  158. smp_mb();
  159. if (!need_resched())
  160. __mwait(ax, cx);
  161. }
  162. }
  163. /* Default MONITOR/MWAIT with no hints, used for default C1 state */
  164. static void mwait_idle(void)
  165. {
  166. if (!need_resched()) {
  167. __monitor((void *)&current_thread_info()->flags, 0, 0);
  168. smp_mb();
  169. if (!need_resched())
  170. __sti_mwait(0, 0);
  171. else
  172. local_irq_enable();
  173. } else
  174. local_irq_enable();
  175. }
  176. /*
  177. * On SMP it's slightly faster (but much more power-consuming!)
  178. * to poll the ->work.need_resched flag instead of waiting for the
  179. * cross-CPU IPI to arrive. Use this option with caution.
  180. */
  181. static void poll_idle(void)
  182. {
  183. local_irq_enable();
  184. while (!need_resched())
  185. cpu_relax();
  186. }
  187. /*
  188. * mwait selection logic:
  189. *
  190. * It depends on the CPU. For AMD CPUs that support MWAIT this is
  191. * wrong. Family 0x10 and 0x11 CPUs will enter C1 on HLT. Powersavings
  192. * then depend on a clock divisor and current Pstate of the core. If
  193. * all cores of a processor are in halt state (C1) the processor can
  194. * enter the C1E (C1 enhanced) state. If mwait is used this will never
  195. * happen.
  196. *
  197. * idle=mwait overrides this decision and forces the usage of mwait.
  198. */
  199. static int __cpuinitdata force_mwait;
  200. #define MWAIT_INFO 0x05
  201. #define MWAIT_ECX_EXTENDED_INFO 0x01
  202. #define MWAIT_EDX_C1 0xf0
  203. static int __cpuinit mwait_usable(const struct cpuinfo_x86 *c)
  204. {
  205. u32 eax, ebx, ecx, edx;
  206. if (force_mwait)
  207. return 1;
  208. if (c->cpuid_level < MWAIT_INFO)
  209. return 0;
  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. * edx enumeratios MONITOR/MWAIT extensions. Check, whether
  216. * C1 supports MWAIT
  217. */
  218. return (edx & MWAIT_EDX_C1);
  219. }
  220. /*
  221. * Check for AMD CPUs, which have potentially C1E support
  222. */
  223. static int __cpuinit check_c1e_idle(const struct cpuinfo_x86 *c)
  224. {
  225. if (c->x86_vendor != X86_VENDOR_AMD)
  226. return 0;
  227. if (c->x86 < 0x0F)
  228. return 0;
  229. /* Family 0x0f models < rev F do not have C1E */
  230. if (c->x86 == 0x0f && c->x86_model < 0x40)
  231. return 0;
  232. return 1;
  233. }
  234. static cpumask_t c1e_mask = CPU_MASK_NONE;
  235. static int c1e_detected;
  236. void c1e_remove_cpu(int cpu)
  237. {
  238. cpu_clear(cpu, c1e_mask);
  239. }
  240. /*
  241. * C1E aware idle routine. We check for C1E active in the interrupt
  242. * pending message MSR. If we detect C1E, then we handle it the same
  243. * way as C3 power states (local apic timer and TSC stop)
  244. */
  245. static void c1e_idle(void)
  246. {
  247. if (need_resched())
  248. return;
  249. if (!c1e_detected) {
  250. u32 lo, hi;
  251. rdmsr(MSR_K8_INT_PENDING_MSG, lo, hi);
  252. if (lo & K8_INTP_C1E_ACTIVE_MASK) {
  253. c1e_detected = 1;
  254. if (!boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
  255. mark_tsc_unstable("TSC halt in AMD C1E");
  256. printk(KERN_INFO "System has AMD C1E enabled\n");
  257. set_cpu_cap(&boot_cpu_data, X86_FEATURE_AMDC1E);
  258. }
  259. }
  260. if (c1e_detected) {
  261. int cpu = smp_processor_id();
  262. if (!cpu_isset(cpu, c1e_mask)) {
  263. cpu_set(cpu, c1e_mask);
  264. /*
  265. * Force broadcast so ACPI can not interfere. Needs
  266. * to run with interrupts enabled as it uses
  267. * smp_function_call.
  268. */
  269. local_irq_enable();
  270. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_FORCE,
  271. &cpu);
  272. printk(KERN_INFO "Switch to broadcast mode on CPU%d\n",
  273. cpu);
  274. local_irq_disable();
  275. }
  276. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
  277. default_idle();
  278. /*
  279. * The switch back from broadcast mode needs to be
  280. * called with interrupts disabled.
  281. */
  282. local_irq_disable();
  283. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
  284. local_irq_enable();
  285. } else
  286. default_idle();
  287. }
  288. void __cpuinit select_idle_routine(const struct cpuinfo_x86 *c)
  289. {
  290. #ifdef CONFIG_X86_SMP
  291. if (pm_idle == poll_idle && smp_num_siblings > 1) {
  292. printk(KERN_WARNING "WARNING: polling idle and HT enabled,"
  293. " performance may degrade.\n");
  294. }
  295. #endif
  296. if (pm_idle)
  297. return;
  298. if (cpu_has(c, X86_FEATURE_MWAIT) && mwait_usable(c)) {
  299. /*
  300. * One CPU supports mwait => All CPUs supports mwait
  301. */
  302. printk(KERN_INFO "using mwait in idle threads.\n");
  303. pm_idle = mwait_idle;
  304. } else if (check_c1e_idle(c)) {
  305. printk(KERN_INFO "using C1E aware idle routine\n");
  306. pm_idle = c1e_idle;
  307. } else
  308. pm_idle = default_idle;
  309. }
  310. static int __init idle_setup(char *str)
  311. {
  312. if (!str)
  313. return -EINVAL;
  314. if (!strcmp(str, "poll")) {
  315. printk("using polling idle threads.\n");
  316. pm_idle = poll_idle;
  317. } else if (!strcmp(str, "mwait"))
  318. force_mwait = 1;
  319. else if (!strcmp(str, "halt")) {
  320. /*
  321. * When the boot option of idle=halt is added, halt is
  322. * forced to be used for CPU idle. In such case CPU C2/C3
  323. * won't be used again.
  324. * To continue to load the CPU idle driver, don't touch
  325. * the boot_option_idle_override.
  326. */
  327. pm_idle = default_idle;
  328. idle_halt = 1;
  329. return 0;
  330. } else if (!strcmp(str, "nomwait")) {
  331. /*
  332. * If the boot option of "idle=nomwait" is added,
  333. * it means that mwait will be disabled for CPU C2/C3
  334. * states. In such case it won't touch the variable
  335. * of boot_option_idle_override.
  336. */
  337. idle_nomwait = 1;
  338. return 0;
  339. } else
  340. return -1;
  341. boot_option_idle_override = 1;
  342. return 0;
  343. }
  344. early_param("idle", idle_setup);