main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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. device_pm_lock();
  190. arch_suspend_disable_irqs();
  191. BUG_ON(!irqs_disabled());
  192. if ((error = device_power_down(PMSG_SUSPEND))) {
  193. printk(KERN_ERR "PM: 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(PMSG_RESUME);
  199. Done:
  200. arch_suspend_enable_irqs();
  201. BUG_ON(irqs_disabled());
  202. device_pm_unlock();
  203. return error;
  204. }
  205. /**
  206. * suspend_devices_and_enter - suspend devices and enter the desired system
  207. * sleep state.
  208. * @state: state to enter
  209. */
  210. int suspend_devices_and_enter(suspend_state_t state)
  211. {
  212. int error;
  213. if (!suspend_ops)
  214. return -ENOSYS;
  215. if (suspend_ops->begin) {
  216. error = suspend_ops->begin(state);
  217. if (error)
  218. goto Close;
  219. }
  220. suspend_console();
  221. error = device_suspend(PMSG_SUSPEND);
  222. if (error) {
  223. printk(KERN_ERR "PM: Some devices failed to suspend\n");
  224. goto Recover_platform;
  225. }
  226. if (suspend_test(TEST_DEVICES))
  227. goto Recover_platform;
  228. if (suspend_ops->prepare) {
  229. error = suspend_ops->prepare();
  230. if (error)
  231. goto Resume_devices;
  232. }
  233. if (suspend_test(TEST_PLATFORM))
  234. goto Finish;
  235. error = disable_nonboot_cpus();
  236. if (!error && !suspend_test(TEST_CPUS))
  237. suspend_enter(state);
  238. enable_nonboot_cpus();
  239. Finish:
  240. if (suspend_ops->finish)
  241. suspend_ops->finish();
  242. Resume_devices:
  243. device_resume(PMSG_RESUME);
  244. resume_console();
  245. Close:
  246. if (suspend_ops->end)
  247. suspend_ops->end();
  248. return error;
  249. Recover_platform:
  250. if (suspend_ops->recover)
  251. suspend_ops->recover();
  252. goto Resume_devices;
  253. }
  254. /**
  255. * suspend_finish - Do final work before exiting suspend sequence.
  256. *
  257. * Call platform code to clean up, restart processes, and free the
  258. * console that we've allocated. This is not called for suspend-to-disk.
  259. */
  260. static void suspend_finish(void)
  261. {
  262. suspend_thaw_processes();
  263. pm_notifier_call_chain(PM_POST_SUSPEND);
  264. pm_restore_console();
  265. }
  266. static const char * const pm_states[PM_SUSPEND_MAX] = {
  267. [PM_SUSPEND_STANDBY] = "standby",
  268. [PM_SUSPEND_MEM] = "mem",
  269. };
  270. static inline int valid_state(suspend_state_t state)
  271. {
  272. /* All states need lowlevel support and need to be valid
  273. * to the lowlevel implementation, no valid callback
  274. * implies that none are valid. */
  275. if (!suspend_ops || !suspend_ops->valid || !suspend_ops->valid(state))
  276. return 0;
  277. return 1;
  278. }
  279. /**
  280. * enter_state - Do common work of entering low-power state.
  281. * @state: pm_state structure for state we're entering.
  282. *
  283. * Make sure we're the only ones trying to enter a sleep state. Fail
  284. * if someone has beat us to it, since we don't want anything weird to
  285. * happen when we wake up.
  286. * Then, do the setup for suspend, enter the state, and cleaup (after
  287. * we've woken up).
  288. */
  289. static int enter_state(suspend_state_t state)
  290. {
  291. int error;
  292. if (!valid_state(state))
  293. return -ENODEV;
  294. if (!mutex_trylock(&pm_mutex))
  295. return -EBUSY;
  296. printk(KERN_INFO "PM: Syncing filesystems ... ");
  297. sys_sync();
  298. printk("done.\n");
  299. pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
  300. error = suspend_prepare();
  301. if (error)
  302. goto Unlock;
  303. if (suspend_test(TEST_FREEZER))
  304. goto Finish;
  305. pr_debug("PM: Entering %s sleep\n", pm_states[state]);
  306. error = suspend_devices_and_enter(state);
  307. Finish:
  308. pr_debug("PM: Finishing wakeup.\n");
  309. suspend_finish();
  310. Unlock:
  311. mutex_unlock(&pm_mutex);
  312. return error;
  313. }
  314. /**
  315. * pm_suspend - Externally visible function for suspending system.
  316. * @state: Enumerated value of state to enter.
  317. *
  318. * Determine whether or not value is within range, get state
  319. * structure, and enter (above).
  320. */
  321. int pm_suspend(suspend_state_t state)
  322. {
  323. if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
  324. return enter_state(state);
  325. return -EINVAL;
  326. }
  327. EXPORT_SYMBOL(pm_suspend);
  328. #endif /* CONFIG_SUSPEND */
  329. struct kobject *power_kobj;
  330. /**
  331. * state - control system power state.
  332. *
  333. * show() returns what states are supported, which is hard-coded to
  334. * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
  335. * 'disk' (Suspend-to-Disk).
  336. *
  337. * store() accepts one of those strings, translates it into the
  338. * proper enumerated value, and initiates a suspend transition.
  339. */
  340. static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
  341. char *buf)
  342. {
  343. char *s = buf;
  344. #ifdef CONFIG_SUSPEND
  345. int i;
  346. for (i = 0; i < PM_SUSPEND_MAX; i++) {
  347. if (pm_states[i] && valid_state(i))
  348. s += sprintf(s,"%s ", pm_states[i]);
  349. }
  350. #endif
  351. #ifdef CONFIG_HIBERNATION
  352. s += sprintf(s, "%s\n", "disk");
  353. #else
  354. if (s != buf)
  355. /* convert the last space to a newline */
  356. *(s-1) = '\n';
  357. #endif
  358. return (s - buf);
  359. }
  360. static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
  361. const char *buf, size_t n)
  362. {
  363. #ifdef CONFIG_SUSPEND
  364. suspend_state_t state = PM_SUSPEND_STANDBY;
  365. const char * const *s;
  366. #endif
  367. char *p;
  368. int len;
  369. int error = -EINVAL;
  370. p = memchr(buf, '\n', n);
  371. len = p ? p - buf : n;
  372. /* First, check if we are requested to hibernate */
  373. if (len == 4 && !strncmp(buf, "disk", len)) {
  374. error = hibernate();
  375. goto Exit;
  376. }
  377. #ifdef CONFIG_SUSPEND
  378. for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
  379. if (*s && len == strlen(*s) && !strncmp(buf, *s, len))
  380. break;
  381. }
  382. if (state < PM_SUSPEND_MAX && *s)
  383. error = enter_state(state);
  384. #endif
  385. Exit:
  386. return error ? error : n;
  387. }
  388. power_attr(state);
  389. #ifdef CONFIG_PM_TRACE
  390. int pm_trace_enabled;
  391. static ssize_t pm_trace_show(struct kobject *kobj, struct kobj_attribute *attr,
  392. char *buf)
  393. {
  394. return sprintf(buf, "%d\n", pm_trace_enabled);
  395. }
  396. static ssize_t
  397. pm_trace_store(struct kobject *kobj, struct kobj_attribute *attr,
  398. const char *buf, size_t n)
  399. {
  400. int val;
  401. if (sscanf(buf, "%d", &val) == 1) {
  402. pm_trace_enabled = !!val;
  403. return n;
  404. }
  405. return -EINVAL;
  406. }
  407. power_attr(pm_trace);
  408. #endif /* CONFIG_PM_TRACE */
  409. static struct attribute * g[] = {
  410. &state_attr.attr,
  411. #ifdef CONFIG_PM_TRACE
  412. &pm_trace_attr.attr,
  413. #endif
  414. #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_PM_DEBUG)
  415. &pm_test_attr.attr,
  416. #endif
  417. NULL,
  418. };
  419. static struct attribute_group attr_group = {
  420. .attrs = g,
  421. };
  422. static int __init pm_init(void)
  423. {
  424. power_kobj = kobject_create_and_add("power", NULL);
  425. if (!power_kobj)
  426. return -ENOMEM;
  427. return sysfs_create_group(power_kobj, &attr_group);
  428. }
  429. core_initcall(pm_init);