process.c 8.5 KB

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