main.c 10 KB

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