main.c 7.7 KB

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