main.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. [PM_SUSPEND_DISK] = "disk",
  157. };
  158. static inline int valid_state(suspend_state_t state)
  159. {
  160. /* Suspend-to-disk does not really need low-level support.
  161. * It can work with shutdown/reboot if needed. If it isn't
  162. * configured, then it cannot be supported.
  163. */
  164. if (state == PM_SUSPEND_DISK)
  165. #ifdef CONFIG_SOFTWARE_SUSPEND
  166. return 1;
  167. #else
  168. return 0;
  169. #endif
  170. /* all other states need lowlevel support and need to be
  171. * valid to the lowlevel implementation, no valid callback
  172. * implies that none are valid. */
  173. if (!pm_ops || !pm_ops->valid || !pm_ops->valid(state))
  174. return 0;
  175. return 1;
  176. }
  177. /**
  178. * enter_state - Do common work of entering low-power state.
  179. * @state: pm_state structure for state we're entering.
  180. *
  181. * Make sure we're the only ones trying to enter a sleep state. Fail
  182. * if someone has beat us to it, since we don't want anything weird to
  183. * happen when we wake up.
  184. * Then, do the setup for suspend, enter the state, and cleaup (after
  185. * we've woken up).
  186. */
  187. static int enter_state(suspend_state_t state)
  188. {
  189. int error;
  190. if (!valid_state(state))
  191. return -ENODEV;
  192. if (!mutex_trylock(&pm_mutex))
  193. return -EBUSY;
  194. if (state == PM_SUSPEND_DISK) {
  195. error = pm_suspend_disk();
  196. goto Unlock;
  197. }
  198. pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
  199. if ((error = suspend_prepare(state)))
  200. goto Unlock;
  201. pr_debug("PM: Entering %s sleep\n", pm_states[state]);
  202. error = suspend_enter(state);
  203. pr_debug("PM: Finishing wakeup.\n");
  204. suspend_finish(state);
  205. Unlock:
  206. mutex_unlock(&pm_mutex);
  207. return error;
  208. }
  209. /**
  210. * pm_suspend - Externally visible function for suspending system.
  211. * @state: Enumarted value of state to enter.
  212. *
  213. * Determine whether or not value is within range, get state
  214. * structure, and enter (above).
  215. */
  216. int pm_suspend(suspend_state_t state)
  217. {
  218. if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
  219. return enter_state(state);
  220. return -EINVAL;
  221. }
  222. EXPORT_SYMBOL(pm_suspend);
  223. decl_subsys(power,NULL,NULL);
  224. /**
  225. * state - control system power state.
  226. *
  227. * show() returns what states are supported, which is hard-coded to
  228. * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
  229. * 'disk' (Suspend-to-Disk).
  230. *
  231. * store() accepts one of those strings, translates it into the
  232. * proper enumerated value, and initiates a suspend transition.
  233. */
  234. static ssize_t state_show(struct kset *kset, char *buf)
  235. {
  236. int i;
  237. char * s = buf;
  238. for (i = 0; i < PM_SUSPEND_MAX; i++) {
  239. if (pm_states[i] && valid_state(i))
  240. s += sprintf(s,"%s ", pm_states[i]);
  241. }
  242. s += sprintf(s,"\n");
  243. return (s - buf);
  244. }
  245. static ssize_t state_store(struct kset *kset, const char *buf, size_t n)
  246. {
  247. suspend_state_t state = PM_SUSPEND_STANDBY;
  248. const char * const *s;
  249. char *p;
  250. int error;
  251. int len;
  252. p = memchr(buf, '\n', n);
  253. len = p ? p - buf : n;
  254. for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
  255. if (*s && !strncmp(buf, *s, len))
  256. break;
  257. }
  258. if (state < PM_SUSPEND_MAX && *s)
  259. error = enter_state(state);
  260. else
  261. error = -EINVAL;
  262. return error ? error : n;
  263. }
  264. power_attr(state);
  265. #ifdef CONFIG_PM_TRACE
  266. int pm_trace_enabled;
  267. static ssize_t pm_trace_show(struct kset *kset, char *buf)
  268. {
  269. return sprintf(buf, "%d\n", pm_trace_enabled);
  270. }
  271. static ssize_t
  272. pm_trace_store(struct kset *kset, const char *buf, size_t n)
  273. {
  274. int val;
  275. if (sscanf(buf, "%d", &val) == 1) {
  276. pm_trace_enabled = !!val;
  277. return n;
  278. }
  279. return -EINVAL;
  280. }
  281. power_attr(pm_trace);
  282. static struct attribute * g[] = {
  283. &state_attr.attr,
  284. &pm_trace_attr.attr,
  285. NULL,
  286. };
  287. #else
  288. static struct attribute * g[] = {
  289. &state_attr.attr,
  290. NULL,
  291. };
  292. #endif /* CONFIG_PM_TRACE */
  293. static struct attribute_group attr_group = {
  294. .attrs = g,
  295. };
  296. static int __init pm_init(void)
  297. {
  298. int error = subsystem_register(&power_subsys);
  299. if (!error)
  300. error = sysfs_create_group(&power_subsys.kobj,&attr_group);
  301. return error;
  302. }
  303. core_initcall(pm_init);