main.c 8.2 KB

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