main.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. * kernel/power/main.c - PM subsystem core functionality.
  3. *
  4. * Copyright (c) 2003 Patrick Mochel
  5. * Copyright (c) 2003 Open Source Development Lab
  6. *
  7. * This file is released under the GPLv2
  8. *
  9. */
  10. #include <linux/module.h>
  11. #include <linux/suspend.h>
  12. #include <linux/kobject.h>
  13. #include <linux/string.h>
  14. #include <linux/delay.h>
  15. #include <linux/errno.h>
  16. #include <linux/init.h>
  17. #include <linux/console.h>
  18. #include <linux/cpu.h>
  19. #include <linux/resume-trace.h>
  20. #include <linux/freezer.h>
  21. #include <linux/vmstat.h>
  22. #include "power.h"
  23. BLOCKING_NOTIFIER_HEAD(pm_chain_head);
  24. DEFINE_MUTEX(pm_mutex);
  25. #ifdef CONFIG_SUSPEND
  26. /* This is just an arbitrary number */
  27. #define FREE_PAGE_NUMBER (100)
  28. struct platform_suspend_ops *suspend_ops;
  29. /**
  30. * suspend_set_ops - Set the global suspend method table.
  31. * @ops: Pointer to ops structure.
  32. */
  33. void suspend_set_ops(struct platform_suspend_ops *ops)
  34. {
  35. mutex_lock(&pm_mutex);
  36. suspend_ops = ops;
  37. mutex_unlock(&pm_mutex);
  38. }
  39. /**
  40. * suspend_valid_only_mem - generic memory-only valid callback
  41. *
  42. * Platform drivers that implement mem suspend only and only need
  43. * to check for that in their .valid callback can use this instead
  44. * of rolling their own .valid callback.
  45. */
  46. int suspend_valid_only_mem(suspend_state_t state)
  47. {
  48. return state == PM_SUSPEND_MEM;
  49. }
  50. /**
  51. * suspend_prepare - Do prep work before entering low-power state.
  52. *
  53. * This is common code that is called for each state that we're entering.
  54. * Run suspend notifiers, allocate a console and stop all processes.
  55. */
  56. static int suspend_prepare(void)
  57. {
  58. int error;
  59. unsigned int free_pages;
  60. if (!suspend_ops || !suspend_ops->enter)
  61. return -EPERM;
  62. error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);
  63. if (error)
  64. goto Finish;
  65. pm_prepare_console();
  66. if (freeze_processes()) {
  67. error = -EAGAIN;
  68. goto Thaw;
  69. }
  70. free_pages = global_page_state(NR_FREE_PAGES);
  71. if (free_pages < FREE_PAGE_NUMBER) {
  72. pr_debug("PM: free some memory\n");
  73. shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
  74. if (nr_free_pages() < FREE_PAGE_NUMBER) {
  75. error = -ENOMEM;
  76. printk(KERN_ERR "PM: No enough memory\n");
  77. }
  78. }
  79. if (!error)
  80. return 0;
  81. Thaw:
  82. thaw_processes();
  83. pm_restore_console();
  84. Finish:
  85. pm_notifier_call_chain(PM_POST_SUSPEND);
  86. return error;
  87. }
  88. /* default implementation */
  89. void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
  90. {
  91. local_irq_disable();
  92. }
  93. /* default implementation */
  94. void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
  95. {
  96. local_irq_enable();
  97. }
  98. /**
  99. * suspend_enter - enter the desired system sleep state.
  100. * @state: state to enter
  101. *
  102. * This function should be called after devices have been suspended.
  103. */
  104. static int suspend_enter(suspend_state_t state)
  105. {
  106. int error = 0;
  107. arch_suspend_disable_irqs();
  108. BUG_ON(!irqs_disabled());
  109. if ((error = device_power_down(PMSG_SUSPEND))) {
  110. printk(KERN_ERR "Some devices failed to power down\n");
  111. goto Done;
  112. }
  113. error = suspend_ops->enter(state);
  114. device_power_up();
  115. Done:
  116. arch_suspend_enable_irqs();
  117. BUG_ON(irqs_disabled());
  118. return error;
  119. }
  120. /**
  121. * suspend_devices_and_enter - suspend devices and enter the desired system sleep
  122. * state.
  123. * @state: state to enter
  124. */
  125. int suspend_devices_and_enter(suspend_state_t state)
  126. {
  127. int error;
  128. if (!suspend_ops)
  129. return -ENOSYS;
  130. if (suspend_ops->set_target) {
  131. error = suspend_ops->set_target(state);
  132. if (error)
  133. return error;
  134. }
  135. suspend_console();
  136. error = device_suspend(PMSG_SUSPEND);
  137. if (error) {
  138. printk(KERN_ERR "Some devices failed to suspend\n");
  139. goto Resume_console;
  140. }
  141. if (suspend_ops->prepare) {
  142. error = suspend_ops->prepare();
  143. if (error)
  144. goto Resume_devices;
  145. }
  146. error = disable_nonboot_cpus();
  147. if (!error)
  148. suspend_enter(state);
  149. enable_nonboot_cpus();
  150. if (suspend_ops->finish)
  151. suspend_ops->finish();
  152. Resume_devices:
  153. device_resume();
  154. Resume_console:
  155. resume_console();
  156. return error;
  157. }
  158. /**
  159. * suspend_finish - Do final work before exiting suspend sequence.
  160. *
  161. * Call platform code to clean up, restart processes, and free the
  162. * console that we've allocated. This is not called for suspend-to-disk.
  163. */
  164. static void suspend_finish(void)
  165. {
  166. thaw_processes();
  167. pm_restore_console();
  168. pm_notifier_call_chain(PM_POST_SUSPEND);
  169. }
  170. static const char * const pm_states[PM_SUSPEND_MAX] = {
  171. [PM_SUSPEND_STANDBY] = "standby",
  172. [PM_SUSPEND_MEM] = "mem",
  173. };
  174. static inline int valid_state(suspend_state_t state)
  175. {
  176. /* All states need lowlevel support and need to be valid
  177. * to the lowlevel implementation, no valid callback
  178. * implies that none are valid. */
  179. if (!suspend_ops || !suspend_ops->valid || !suspend_ops->valid(state))
  180. return 0;
  181. return 1;
  182. }
  183. /**
  184. * enter_state - Do common work of entering low-power state.
  185. * @state: pm_state structure for state we're entering.
  186. *
  187. * Make sure we're the only ones trying to enter a sleep state. Fail
  188. * if someone has beat us to it, since we don't want anything weird to
  189. * happen when we wake up.
  190. * Then, do the setup for suspend, enter the state, and cleaup (after
  191. * we've woken up).
  192. */
  193. static int enter_state(suspend_state_t state)
  194. {
  195. int error;
  196. if (!valid_state(state))
  197. return -ENODEV;
  198. if (!mutex_trylock(&pm_mutex))
  199. return -EBUSY;
  200. pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
  201. if ((error = suspend_prepare()))
  202. goto Unlock;
  203. pr_debug("PM: Entering %s sleep\n", pm_states[state]);
  204. error = suspend_devices_and_enter(state);
  205. pr_debug("PM: Finishing wakeup.\n");
  206. suspend_finish();
  207. Unlock:
  208. mutex_unlock(&pm_mutex);
  209. return error;
  210. }
  211. /**
  212. * pm_suspend - Externally visible function for suspending system.
  213. * @state: Enumerated value of state to enter.
  214. *
  215. * Determine whether or not value is within range, get state
  216. * structure, and enter (above).
  217. */
  218. int pm_suspend(suspend_state_t state)
  219. {
  220. if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
  221. return enter_state(state);
  222. return -EINVAL;
  223. }
  224. EXPORT_SYMBOL(pm_suspend);
  225. #endif /* CONFIG_SUSPEND */
  226. decl_subsys(power,NULL,NULL);
  227. /**
  228. * state - control system power state.
  229. *
  230. * show() returns what states are supported, which is hard-coded to
  231. * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
  232. * 'disk' (Suspend-to-Disk).
  233. *
  234. * store() accepts one of those strings, translates it into the
  235. * proper enumerated value, and initiates a suspend transition.
  236. */
  237. static ssize_t state_show(struct kset *kset, char *buf)
  238. {
  239. char *s = buf;
  240. #ifdef CONFIG_SUSPEND
  241. int i;
  242. for (i = 0; i < PM_SUSPEND_MAX; i++) {
  243. if (pm_states[i] && valid_state(i))
  244. s += sprintf(s,"%s ", pm_states[i]);
  245. }
  246. #endif
  247. #ifdef CONFIG_HIBERNATION
  248. s += sprintf(s, "%s\n", "disk");
  249. #else
  250. if (s != buf)
  251. /* convert the last space to a newline */
  252. *(s-1) = '\n';
  253. #endif
  254. return (s - buf);
  255. }
  256. static ssize_t state_store(struct kset *kset, const char *buf, size_t n)
  257. {
  258. #ifdef CONFIG_SUSPEND
  259. suspend_state_t state = PM_SUSPEND_STANDBY;
  260. const char * const *s;
  261. #endif
  262. char *p;
  263. int len;
  264. int error = -EINVAL;
  265. p = memchr(buf, '\n', n);
  266. len = p ? p - buf : n;
  267. /* First, check if we are requested to hibernate */
  268. if (len == 4 && !strncmp(buf, "disk", len)) {
  269. error = hibernate();
  270. goto Exit;
  271. }
  272. #ifdef CONFIG_SUSPEND
  273. for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
  274. if (*s && len == strlen(*s) && !strncmp(buf, *s, len))
  275. break;
  276. }
  277. if (state < PM_SUSPEND_MAX && *s)
  278. error = enter_state(state);
  279. #endif
  280. Exit:
  281. return error ? error : n;
  282. }
  283. power_attr(state);
  284. #ifdef CONFIG_PM_TRACE
  285. int pm_trace_enabled;
  286. static ssize_t pm_trace_show(struct kset *kset, char *buf)
  287. {
  288. return sprintf(buf, "%d\n", pm_trace_enabled);
  289. }
  290. static ssize_t
  291. pm_trace_store(struct kset *kset, const char *buf, size_t n)
  292. {
  293. int val;
  294. if (sscanf(buf, "%d", &val) == 1) {
  295. pm_trace_enabled = !!val;
  296. return n;
  297. }
  298. return -EINVAL;
  299. }
  300. power_attr(pm_trace);
  301. static struct attribute * g[] = {
  302. &state_attr.attr,
  303. &pm_trace_attr.attr,
  304. NULL,
  305. };
  306. #else
  307. static struct attribute * g[] = {
  308. &state_attr.attr,
  309. NULL,
  310. };
  311. #endif /* CONFIG_PM_TRACE */
  312. static struct attribute_group attr_group = {
  313. .attrs = g,
  314. };
  315. static int __init pm_init(void)
  316. {
  317. int error = subsystem_register(&power_subsys);
  318. if (!error)
  319. error = sysfs_create_group(&power_subsys.kobj,&attr_group);
  320. return error;
  321. }
  322. core_initcall(pm_init);