PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/cpuidle/cpuidle.c

https://bitbucket.org/davinchi/kernel-ics
C | 426 lines | 261 code | 83 blank | 82 comment | 34 complexity | 22898777e3f2bca2ea3662be459bc4e6 MD5 | raw file
  1. /*
  2. * cpuidle.c - core cpuidle infrastructure
  3. *
  4. * (C) 2006-2007 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
  5. * Shaohua Li <shaohua.li@intel.com>
  6. * Adam Belay <abelay@novell.com>
  7. *
  8. * This code is licenced under the GPL.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/mutex.h>
  12. #include <linux/sched.h>
  13. #include <linux/notifier.h>
  14. #include <linux/pm_qos_params.h>
  15. #include <linux/cpu.h>
  16. #include <linux/cpuidle.h>
  17. #include <linux/ktime.h>
  18. #include <linux/hrtimer.h>
  19. #include <linux/moduleparam.h>
  20. #include <trace/events/power.h>
  21. #include "cpuidle.h"
  22. DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
  23. DEFINE_MUTEX(cpuidle_lock);
  24. LIST_HEAD(cpuidle_detected_devices);
  25. static int enabled_devices;
  26. static int off __read_mostly;
  27. static int initialized __read_mostly;
  28. int cpuidle_disabled(void)
  29. {
  30. return off;
  31. }
  32. void disable_cpuidle(void)
  33. {
  34. off = 1;
  35. }
  36. #if defined(CONFIG_ARCH_HAS_CPU_IDLE_WAIT)
  37. static void cpuidle_kick_cpus(void)
  38. {
  39. cpu_idle_wait();
  40. }
  41. #elif defined(CONFIG_SMP)
  42. # error "Arch needs cpu_idle_wait() equivalent here"
  43. #else /* !CONFIG_ARCH_HAS_CPU_IDLE_WAIT && !CONFIG_SMP */
  44. static void cpuidle_kick_cpus(void) {}
  45. #endif
  46. static int __cpuidle_register_device(struct cpuidle_device *dev);
  47. /**
  48. * cpuidle_idle_call - the main idle loop
  49. *
  50. * NOTE: no locks or semaphores should be used here
  51. */
  52. static void cpuidle_idle_call(void)
  53. {
  54. struct cpuidle_device *dev = __get_cpu_var(cpuidle_devices);
  55. struct cpuidle_state *target_state;
  56. int next_state;
  57. /* check if the device is ready */
  58. if (!dev || !dev->enabled) {
  59. if (pm_idle_old)
  60. pm_idle_old();
  61. else
  62. #if defined(CONFIG_ARCH_HAS_DEFAULT_IDLE)
  63. default_idle();
  64. #else
  65. local_irq_enable();
  66. #endif
  67. return;
  68. }
  69. #if 0
  70. /* shows regressions, re-enable for 2.6.29 */
  71. /*
  72. * run any timers that can be run now, at this point
  73. * before calculating the idle duration etc.
  74. */
  75. hrtimer_peek_ahead_timers();
  76. #endif
  77. /*
  78. * Call the device's prepare function before calling the
  79. * governor's select function. ->prepare gives the device's
  80. * cpuidle driver a chance to update any dynamic information
  81. * of its cpuidle states for the current idle period, e.g.
  82. * state availability, latencies, residencies, etc.
  83. */
  84. if (dev->prepare)
  85. dev->prepare(dev);
  86. /* ask the governor for the next state */
  87. next_state = cpuidle_curr_governor->select(dev);
  88. if (need_resched()) {
  89. local_irq_enable();
  90. return;
  91. }
  92. target_state = &dev->states[next_state];
  93. /* enter the state and update stats */
  94. dev->last_state = target_state;
  95. dev->last_residency = target_state->enter(dev, target_state);
  96. if (dev->last_state)
  97. target_state = dev->last_state;
  98. target_state->time += (unsigned long long)dev->last_residency;
  99. target_state->usage++;
  100. /* give the governor an opportunity to reflect on the outcome */
  101. if (cpuidle_curr_governor->reflect)
  102. cpuidle_curr_governor->reflect(dev);
  103. trace_power_end(0);
  104. }
  105. /**
  106. * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
  107. */
  108. void cpuidle_install_idle_handler(void)
  109. {
  110. if (enabled_devices) {
  111. /* Make sure all changes finished before we switch to new idle */
  112. smp_wmb();
  113. initialized = 1;
  114. }
  115. }
  116. /**
  117. * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
  118. */
  119. void cpuidle_uninstall_idle_handler(void)
  120. {
  121. if (enabled_devices) {
  122. initialized = 0;
  123. cpuidle_kick_cpus();
  124. }
  125. }
  126. /**
  127. * cpuidle_pause_and_lock - temporarily disables CPUIDLE
  128. */
  129. void cpuidle_pause_and_lock(void)
  130. {
  131. mutex_lock(&cpuidle_lock);
  132. cpuidle_uninstall_idle_handler();
  133. }
  134. EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
  135. /**
  136. * cpuidle_resume_and_unlock - resumes CPUIDLE operation
  137. */
  138. void cpuidle_resume_and_unlock(void)
  139. {
  140. cpuidle_install_idle_handler();
  141. mutex_unlock(&cpuidle_lock);
  142. }
  143. EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
  144. /**
  145. * cpuidle_enable_device - enables idle PM for a CPU
  146. * @dev: the CPU
  147. *
  148. * This function must be called between cpuidle_pause_and_lock and
  149. * cpuidle_resume_and_unlock when used externally.
  150. */
  151. int cpuidle_enable_device(struct cpuidle_device *dev)
  152. {
  153. int ret, i;
  154. if (dev->enabled)
  155. return 0;
  156. if (!cpuidle_get_driver() || !cpuidle_curr_governor)
  157. return -EIO;
  158. if (!dev->state_count)
  159. return -EINVAL;
  160. if (dev->registered == 0) {
  161. ret = __cpuidle_register_device(dev);
  162. if (ret)
  163. return ret;
  164. }
  165. if ((ret = cpuidle_add_state_sysfs(dev)))
  166. return ret;
  167. if (cpuidle_curr_governor->enable &&
  168. (ret = cpuidle_curr_governor->enable(dev)))
  169. goto fail_sysfs;
  170. for (i = 0; i < dev->state_count; i++) {
  171. dev->states[i].usage = 0;
  172. dev->states[i].time = 0;
  173. }
  174. dev->last_residency = 0;
  175. dev->last_state = NULL;
  176. smp_wmb();
  177. dev->enabled = 1;
  178. enabled_devices++;
  179. return 0;
  180. fail_sysfs:
  181. cpuidle_remove_state_sysfs(dev);
  182. return ret;
  183. }
  184. EXPORT_SYMBOL_GPL(cpuidle_enable_device);
  185. /**
  186. * cpuidle_disable_device - disables idle PM for a CPU
  187. * @dev: the CPU
  188. *
  189. * This function must be called between cpuidle_pause_and_lock and
  190. * cpuidle_resume_and_unlock when used externally.
  191. */
  192. void cpuidle_disable_device(struct cpuidle_device *dev)
  193. {
  194. if (!dev->enabled)
  195. return;
  196. if (!cpuidle_get_driver() || !cpuidle_curr_governor)
  197. return;
  198. dev->enabled = 0;
  199. if (cpuidle_curr_governor->disable)
  200. cpuidle_curr_governor->disable(dev);
  201. cpuidle_remove_state_sysfs(dev);
  202. enabled_devices--;
  203. }
  204. EXPORT_SYMBOL_GPL(cpuidle_disable_device);
  205. #ifdef CONFIG_ARCH_HAS_CPU_RELAX
  206. static int poll_idle(struct cpuidle_device *dev,
  207. struct cpuidle_driver *drv, int index)
  208. {
  209. ktime_t t1, t2;
  210. s64 diff;
  211. t1 = ktime_get();
  212. local_irq_enable();
  213. while (!need_resched())
  214. cpu_relax();
  215. t2 = ktime_get();
  216. diff = ktime_to_us(ktime_sub(t2, t1));
  217. if (diff > INT_MAX)
  218. diff = INT_MAX;
  219. dev->last_residency = (int) diff;
  220. return index;
  221. }
  222. static void poll_idle_init(struct cpuidle_driver *drv)
  223. {
  224. struct cpuidle_state *state = &drv->states[0];
  225. snprintf(state->name, CPUIDLE_NAME_LEN, "C0");
  226. snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
  227. state->exit_latency = 0;
  228. state->target_residency = 0;
  229. state->power_usage = -1;
  230. state->flags = CPUIDLE_FLAG_POLL;
  231. state->enter = poll_idle;
  232. }
  233. #else
  234. static void poll_idle_init(struct cpuidle_driver *drv) {}
  235. #endif /* CONFIG_ARCH_HAS_CPU_RELAX */
  236. /**
  237. * __cpuidle_register_device - internal register function called before register
  238. * and enable routines
  239. * @dev: the cpu
  240. *
  241. * cpuidle_lock mutex must be held before this is called
  242. */
  243. static int __cpuidle_register_device(struct cpuidle_device *dev)
  244. {
  245. int ret;
  246. struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
  247. struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
  248. if (!sys_dev)
  249. return -EINVAL;
  250. if (!try_module_get(cpuidle_driver->owner))
  251. return -EINVAL;
  252. init_completion(&dev->kobj_unregister);
  253. poll_idle_init(dev);
  254. per_cpu(cpuidle_devices, dev->cpu) = dev;
  255. list_add(&dev->device_list, &cpuidle_detected_devices);
  256. if ((ret = cpuidle_add_sysfs(sys_dev))) {
  257. module_put(cpuidle_driver->owner);
  258. return ret;
  259. }
  260. dev->registered = 1;
  261. return 0;
  262. }
  263. /**
  264. * cpuidle_register_device - registers a CPU's idle PM feature
  265. * @dev: the cpu
  266. */
  267. int cpuidle_register_device(struct cpuidle_device *dev)
  268. {
  269. int ret;
  270. mutex_lock(&cpuidle_lock);
  271. if ((ret = __cpuidle_register_device(dev))) {
  272. mutex_unlock(&cpuidle_lock);
  273. return ret;
  274. }
  275. cpuidle_enable_device(dev);
  276. cpuidle_install_idle_handler();
  277. mutex_unlock(&cpuidle_lock);
  278. return 0;
  279. }
  280. EXPORT_SYMBOL_GPL(cpuidle_register_device);
  281. /**
  282. * cpuidle_unregister_device - unregisters a CPU's idle PM feature
  283. * @dev: the cpu
  284. */
  285. void cpuidle_unregister_device(struct cpuidle_device *dev)
  286. {
  287. struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
  288. struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
  289. if (dev->registered == 0)
  290. return;
  291. cpuidle_pause_and_lock();
  292. cpuidle_disable_device(dev);
  293. cpuidle_remove_sysfs(sys_dev);
  294. list_del(&dev->device_list);
  295. wait_for_completion(&dev->kobj_unregister);
  296. per_cpu(cpuidle_devices, dev->cpu) = NULL;
  297. cpuidle_resume_and_unlock();
  298. module_put(cpuidle_driver->owner);
  299. }
  300. EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
  301. #ifdef CONFIG_SMP
  302. static void smp_callback(void *v)
  303. {
  304. /* we already woke the CPU up, nothing more to do */
  305. }
  306. /*
  307. * This function gets called when a part of the kernel has a new latency
  308. * requirement. This means we need to get all processors out of their C-state,
  309. * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
  310. * wakes them all right up.
  311. */
  312. static int cpuidle_latency_notify(struct notifier_block *b,
  313. unsigned long l, void *v)
  314. {
  315. smp_call_function(smp_callback, NULL, 1);
  316. return NOTIFY_OK;
  317. }
  318. static struct notifier_block cpuidle_latency_notifier = {
  319. .notifier_call = cpuidle_latency_notify,
  320. };
  321. static inline void latency_notifier_init(struct notifier_block *n)
  322. {
  323. pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
  324. }
  325. #else /* CONFIG_SMP */
  326. #define latency_notifier_init(x) do { } while (0)
  327. #endif /* CONFIG_SMP */
  328. /**
  329. * cpuidle_init - core initializer
  330. */
  331. static int __init cpuidle_init(void)
  332. {
  333. int ret;
  334. if (cpuidle_disabled())
  335. return -ENODEV;
  336. ret = cpuidle_add_class_sysfs(&cpu_sysdev_class);
  337. if (ret)
  338. return ret;
  339. latency_notifier_init(&cpuidle_latency_notifier);
  340. return 0;
  341. }
  342. module_param(off, int, 0444);
  343. core_initcall(cpuidle_init);