process.c 8.8 KB

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