main.c 8.4 KB

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