main.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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/pm.h>
  18. #include <linux/console.h>
  19. #include <linux/cpu.h>
  20. #include <linux/resume-trace.h>
  21. #include <linux/freezer.h>
  22. #include <linux/vmstat.h>
  23. #include "power.h"
  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. suspend_disk_method_t pm_disk_mode = PM_DISK_SHUTDOWN;
  29. /**
  30. * pm_set_ops - Set the global power method table.
  31. * @ops: Pointer to ops structure.
  32. */
  33. void pm_set_ops(struct pm_ops * ops)
  34. {
  35. mutex_lock(&pm_mutex);
  36. pm_ops = ops;
  37. if (ops && ops->pm_disk_mode != PM_DISK_INVALID) {
  38. pm_disk_mode = ops->pm_disk_mode;
  39. } else
  40. pm_disk_mode = PM_DISK_SHUTDOWN;
  41. mutex_unlock(&pm_mutex);
  42. }
  43. /**
  44. * pm_valid_only_mem - generic memory-only valid callback
  45. *
  46. * pm_ops drivers that implement mem suspend only and only need
  47. * to check for that in their .valid callback can use this instead
  48. * of rolling their own .valid callback.
  49. */
  50. int pm_valid_only_mem(suspend_state_t state)
  51. {
  52. return state == PM_SUSPEND_MEM;
  53. }
  54. static inline void pm_finish(suspend_state_t state)
  55. {
  56. if (pm_ops->finish)
  57. pm_ops->finish(state);
  58. }
  59. /**
  60. * suspend_prepare - Do prep work before entering low-power state.
  61. * @state: State we're entering.
  62. *
  63. * This is common code that is called for each state that we're
  64. * entering. Allocate a console, stop all processes, then make sure
  65. * the platform can enter the requested state.
  66. */
  67. static int suspend_prepare(suspend_state_t state)
  68. {
  69. int error;
  70. unsigned int free_pages;
  71. if (!pm_ops || !pm_ops->enter)
  72. return -EPERM;
  73. pm_prepare_console();
  74. if (freeze_processes()) {
  75. error = -EAGAIN;
  76. goto Thaw;
  77. }
  78. if ((free_pages = global_page_state(NR_FREE_PAGES))
  79. < FREE_PAGE_NUMBER) {
  80. pr_debug("PM: free some memory\n");
  81. shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
  82. if (nr_free_pages() < FREE_PAGE_NUMBER) {
  83. error = -ENOMEM;
  84. printk(KERN_ERR "PM: No enough memory\n");
  85. goto Thaw;
  86. }
  87. }
  88. if (pm_ops->prepare) {
  89. if ((error = pm_ops->prepare(state)))
  90. goto Thaw;
  91. }
  92. suspend_console();
  93. error = device_suspend(PMSG_SUSPEND);
  94. if (error) {
  95. printk(KERN_ERR "Some devices failed to suspend\n");
  96. goto Resume_devices;
  97. }
  98. error = disable_nonboot_cpus();
  99. if (!error)
  100. return 0;
  101. enable_nonboot_cpus();
  102. Resume_devices:
  103. pm_finish(state);
  104. device_resume();
  105. resume_console();
  106. Thaw:
  107. thaw_processes();
  108. pm_restore_console();
  109. return error;
  110. }
  111. /* default implementation */
  112. void __attribute__ ((weak)) arch_suspend_disable_irqs(void)
  113. {
  114. local_irq_disable();
  115. }
  116. /* default implementation */
  117. void __attribute__ ((weak)) arch_suspend_enable_irqs(void)
  118. {
  119. local_irq_enable();
  120. }
  121. int suspend_enter(suspend_state_t state)
  122. {
  123. int error = 0;
  124. arch_suspend_disable_irqs();
  125. BUG_ON(!irqs_disabled());
  126. if ((error = device_power_down(PMSG_SUSPEND))) {
  127. printk(KERN_ERR "Some devices failed to power down\n");
  128. goto Done;
  129. }
  130. error = pm_ops->enter(state);
  131. device_power_up();
  132. Done:
  133. arch_suspend_enable_irqs();
  134. BUG_ON(irqs_disabled());
  135. return error;
  136. }
  137. /**
  138. * suspend_finish - Do final work before exiting suspend sequence.
  139. * @state: State we're coming out of.
  140. *
  141. * Call platform code to clean up, restart processes, and free the
  142. * console that we've allocated. This is not called for suspend-to-disk.
  143. */
  144. static void suspend_finish(suspend_state_t state)
  145. {
  146. enable_nonboot_cpus();
  147. pm_finish(state);
  148. device_resume();
  149. resume_console();
  150. thaw_processes();
  151. pm_restore_console();
  152. }
  153. static const char * const pm_states[PM_SUSPEND_MAX] = {
  154. [PM_SUSPEND_STANDBY] = "standby",
  155. [PM_SUSPEND_MEM] = "mem",
  156. #ifdef CONFIG_SOFTWARE_SUSPEND
  157. [PM_SUSPEND_DISK] = "disk",
  158. #endif
  159. };
  160. static inline int valid_state(suspend_state_t state)
  161. {
  162. /* Suspend-to-disk does not really need low-level support.
  163. * It can work with reboot if needed. */
  164. if (state == PM_SUSPEND_DISK)
  165. return 1;
  166. /* all other states need lowlevel support and need to be
  167. * valid 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. if (state == PM_SUSPEND_DISK) {
  191. error = pm_suspend_disk();
  192. goto Unlock;
  193. }
  194. pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
  195. if ((error = suspend_prepare(state)))
  196. goto Unlock;
  197. pr_debug("PM: Entering %s sleep\n", pm_states[state]);
  198. error = suspend_enter(state);
  199. pr_debug("PM: Finishing wakeup.\n");
  200. suspend_finish(state);
  201. Unlock:
  202. mutex_unlock(&pm_mutex);
  203. return error;
  204. }
  205. #ifdef CONFIG_SOFTWARE_SUSPEND
  206. /*
  207. * This is main interface to the outside world. It needs to be
  208. * called from process context.
  209. */
  210. int software_suspend(void)
  211. {
  212. return enter_state(PM_SUSPEND_DISK);
  213. }
  214. #endif
  215. /**
  216. * pm_suspend - Externally visible function for suspending system.
  217. * @state: Enumarted 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. decl_subsys(power,NULL,NULL);
  230. /**
  231. * state - control system power state.
  232. *
  233. * show() returns what states are supported, which is hard-coded to
  234. * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
  235. * 'disk' (Suspend-to-Disk).
  236. *
  237. * store() accepts one of those strings, translates it into the
  238. * proper enumerated value, and initiates a suspend transition.
  239. */
  240. static ssize_t state_show(struct kset *kset, char *buf)
  241. {
  242. int i;
  243. char * s = buf;
  244. for (i = 0; i < PM_SUSPEND_MAX; i++) {
  245. if (pm_states[i] && valid_state(i))
  246. s += sprintf(s,"%s ", pm_states[i]);
  247. }
  248. s += sprintf(s,"\n");
  249. return (s - buf);
  250. }
  251. static ssize_t state_store(struct kset *kset, const char *buf, size_t n)
  252. {
  253. suspend_state_t state = PM_SUSPEND_STANDBY;
  254. const char * const *s;
  255. char *p;
  256. int error;
  257. int len;
  258. p = memchr(buf, '\n', n);
  259. len = p ? p - buf : n;
  260. for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
  261. if (*s && !strncmp(buf, *s, len))
  262. break;
  263. }
  264. if (state < PM_SUSPEND_MAX && *s)
  265. error = enter_state(state);
  266. else
  267. error = -EINVAL;
  268. return error ? error : n;
  269. }
  270. power_attr(state);
  271. #ifdef CONFIG_PM_TRACE
  272. int pm_trace_enabled;
  273. static ssize_t pm_trace_show(struct kset *kset, char *buf)
  274. {
  275. return sprintf(buf, "%d\n", pm_trace_enabled);
  276. }
  277. static ssize_t
  278. pm_trace_store(struct kset *kset, const char *buf, size_t n)
  279. {
  280. int val;
  281. if (sscanf(buf, "%d", &val) == 1) {
  282. pm_trace_enabled = !!val;
  283. return n;
  284. }
  285. return -EINVAL;
  286. }
  287. power_attr(pm_trace);
  288. static struct attribute * g[] = {
  289. &state_attr.attr,
  290. &pm_trace_attr.attr,
  291. NULL,
  292. };
  293. #else
  294. static struct attribute * g[] = {
  295. &state_attr.attr,
  296. NULL,
  297. };
  298. #endif /* CONFIG_PM_TRACE */
  299. static struct attribute_group attr_group = {
  300. .attrs = g,
  301. };
  302. static int __init pm_init(void)
  303. {
  304. int error = subsystem_register(&power_subsys);
  305. if (!error)
  306. error = sysfs_create_group(&power_subsys.kobj,&attr_group);
  307. return error;
  308. }
  309. core_initcall(pm_init);