main.c 11 KB

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