main.c 10 KB

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