process.c 8.0 KB

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