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