main.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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/pm.h>
  18. #include <linux/console.h>
  19. #include <linux/cpu.h>
  20. #include <linux/resume-trace.h>
  21. #include <linux/freezer.h>
  22. #include <linux/vmstat.h>
  23. #include "power.h"
  24. #if 0
  25. /* Qisda, ShiYong Lin, 2009/07/18, Send message when sleep{*/
  26. #include <linux/input.h>
  27. /* Qisda, ShiYong Lin, 2009/07/18, Send message when sleep}*/
  28. #endif
  29. #ifdef CONFIG_PM_CPU_MODE
  30. extern unsigned char pm_cpu_mode;
  31. #endif
  32. /*This is just an arbitrary number */
  33. #define FREE_PAGE_NUMBER (100)
  34. DEFINE_MUTEX(pm_mutex);
  35. struct pm_ops *pm_ops;
  36. suspend_disk_method_t pm_disk_mode = PM_DISK_PLATFORM;
  37. /* Qisda, ShiYong Lin, 2009/07/18, Send message when sleep{*/
  38. extern void pm_keypad_message_to_ap (void);
  39. /* Qisda, ShiYong Lin, 2009/07/18, Send message when sleep}*/
  40. /**
  41. * pm_set_ops - Set the global power method table.
  42. * @ops: Pointer to ops structure.
  43. */
  44. void pm_set_ops(struct pm_ops * ops)
  45. {
  46. mutex_lock(&pm_mutex);
  47. pm_ops = ops;
  48. mutex_unlock(&pm_mutex);
  49. }
  50. static inline void pm_finish(suspend_state_t state)
  51. {
  52. if (pm_ops->finish)
  53. pm_ops->finish(state);
  54. }
  55. /**
  56. * suspend_prepare - Do prep work before entering low-power state.
  57. * @state: State we're entering.
  58. *
  59. * This is common code that is called for each state that we're
  60. * entering. Allocate a console, stop all processes, then make sure
  61. * the platform can enter the requested state.
  62. */
  63. static int suspend_prepare(suspend_state_t state)
  64. {
  65. int error;
  66. unsigned int free_pages;
  67. if (!pm_ops || !pm_ops->enter)
  68. return -EPERM;
  69. pm_prepare_console();
  70. if (freeze_processes()) {
  71. error = -EAGAIN;
  72. goto Thaw;
  73. }
  74. if ((free_pages = global_page_state(NR_FREE_PAGES))
  75. < FREE_PAGE_NUMBER) {
  76. pr_debug("PM: free some memory\n");
  77. shrink_all_memory(FREE_PAGE_NUMBER - free_pages);
  78. if (nr_free_pages() < FREE_PAGE_NUMBER) {
  79. error = -ENOMEM;
  80. printk(KERN_ERR "PM: No enough memory\n");
  81. goto Thaw;
  82. }
  83. }
  84. if (pm_ops->prepare) {
  85. if ((error = pm_ops->prepare(state)))
  86. goto Thaw;
  87. }
  88. suspend_console();
  89. error = device_suspend(PMSG_SUSPEND);
  90. if (error) {
  91. printk(KERN_ERR "Some devices failed to suspend\n");
  92. goto Resume_devices;
  93. }
  94. error = disable_nonboot_cpus();
  95. if (!error)
  96. return 0;
  97. enable_nonboot_cpus();
  98. Resume_devices:
  99. pm_finish(state);
  100. device_resume();
  101. resume_console();
  102. Thaw:
  103. thaw_processes();
  104. pm_restore_console();
  105. return error;
  106. }
  107. int suspend_enter(suspend_state_t state)
  108. {
  109. int error = 0;
  110. unsigned long flags;
  111. local_irq_save(flags);
  112. if ((error = device_power_down(PMSG_SUSPEND))) {
  113. printk(KERN_ERR "Some devices failed to power down\n");
  114. goto Done;
  115. }
  116. error = pm_ops->enter(state);
  117. device_power_up();
  118. Done:
  119. local_irq_restore(flags);
  120. return error;
  121. }
  122. /**
  123. * suspend_finish - Do final work before exiting suspend sequence.
  124. * @state: State we're coming out of.
  125. *
  126. * Call platform code to clean up, restart processes, and free the
  127. * console that we've allocated. This is not called for suspend-to-disk.
  128. */
  129. static void suspend_finish(suspend_state_t state)
  130. {
  131. enable_nonboot_cpus();
  132. pm_finish(state);
  133. device_resume();
  134. resume_console();
  135. thaw_processes();
  136. pm_restore_console();
  137. }
  138. static const char * const pm_states[PM_SUSPEND_MAX] = {
  139. [PM_SUSPEND_STANDBY] = "standby",
  140. [PM_SUSPEND_MEM] = "mem",
  141. #ifdef CONFIG_PM_CPU_MODE
  142. [PM_SUSPEND_CPU_MODE] = "cpu",
  143. #endif
  144. #ifdef CONFIG_SOFTWARE_SUSPEND
  145. [PM_SUSPEND_DISK] = "disk",
  146. #endif
  147. };
  148. static inline int valid_state(suspend_state_t state)
  149. {
  150. /* Suspend-to-disk does not really need low-level support.
  151. * It can work with reboot if needed. */
  152. if (state == PM_SUSPEND_DISK)
  153. return 1;
  154. /* all other states need lowlevel support and need to be
  155. * valid to the lowlevel implementation, no valid callback
  156. * implies that all are valid. */
  157. if (!pm_ops || (pm_ops->valid && !pm_ops->valid(state)))
  158. return 0;
  159. return 1;
  160. }
  161. #ifdef CONFIG_PM_CPU_MODE
  162. static int suspend_pm_cpu_mode_prepare(suspend_state_t state)
  163. {
  164. return suspend_prepare(state);
  165. }
  166. int suspend_pm_cpu_mode_enter(suspend_state_t state)
  167. {
  168. int error = 0;
  169. unsigned long flags;
  170. printk("suspend_cpu_mode_enter\n");
  171. local_irq_save(flags);
  172. /*
  173. if ((error = device_power_down(PMSG_SUSPEND))) {
  174. printk(KERN_ERR "Some devices failed to power down\n");
  175. goto Done;
  176. }
  177. */
  178. error = pm_ops->enter(state);
  179. device_power_up();
  180. Done:
  181. local_irq_restore(flags);
  182. return error;
  183. }
  184. static void suspend_pm_cpu_mode_finish(suspend_state_t state)
  185. {
  186. enable_nonboot_cpus();
  187. pm_finish(state);
  188. device_resume();
  189. // s3c24xx_cpu_mode_serial_resume();
  190. resume_console();
  191. thaw_processes();
  192. pm_restore_console();
  193. }
  194. static int enter_pm_cpu_mode(suspend_state_t state)
  195. {
  196. int error;
  197. if (!valid_state(state))
  198. return -ENODEV;
  199. if (!mutex_trylock(&pm_mutex))
  200. return -EBUSY;
  201. if (state == PM_SUSPEND_DISK) {
  202. error = pm_suspend_disk();
  203. goto Unlock;
  204. }
  205. pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
  206. if ((error = suspend_pm_cpu_mode_prepare(state)))
  207. goto Unlock;
  208. // pr_debug("PM: Entering %s sleep\n", pm_states[state]);
  209. error = suspend_pm_cpu_mode_enter(state);
  210. pr_debug("PM: Finishing wakeup.\n");
  211. suspend_pm_cpu_mode_finish(state);
  212. Unlock:
  213. mutex_unlock(&pm_mutex);
  214. /* Qisda, ShiYong Lin, 2009/09/28, Add the sleep event message when sleep {*/
  215. //s3c_keypad_pm_sleep_message_to_ap(0);
  216. // printk(KERN_ERR "Sleep end enter_pm_cpu_mode, %d\n", state);
  217. /* } Qisda, ShiYong Lin, 2009/09/28, Add the sleep event message when sleep */
  218. return error;
  219. }
  220. #endif
  221. /**
  222. * enter_state - Do common work of entering low-power state.
  223. * @state: pm_state structure for state we're entering.
  224. *
  225. * Make sure we're the only ones trying to enter a sleep state. Fail
  226. * if someone has beat us to it, since we don't want anything weird to
  227. * happen when we wake up.
  228. * Then, do the setup for suspend, enter the state, and cleaup (after
  229. * we've woken up).
  230. */
  231. static int enter_state(suspend_state_t state)
  232. {
  233. int error;
  234. if (!valid_state(state))
  235. return -ENODEV;
  236. if (!mutex_trylock(&pm_mutex))
  237. return -EBUSY;
  238. if (state == PM_SUSPEND_DISK) {
  239. error = pm_suspend_disk();
  240. goto Unlock;
  241. }
  242. pr_debug("PM: Preparing system for %s sleep\n", pm_states[state]);
  243. if ((error = suspend_prepare(state)))
  244. goto Unlock;
  245. pr_debug("PM: Entering %s sleep\n", pm_states[state]);
  246. error = suspend_enter(state);
  247. pr_debug("PM: Finishing wakeup.\n");
  248. suspend_finish(state);
  249. Unlock:
  250. mutex_unlock(&pm_mutex);
  251. /* Qisda, ShiYong Lin, 2009/09/28, Add the sleep event message when sleep {*/
  252. //s3c_keypad_pm_sleep_message_to_ap(0);
  253. /* } Qisda, ShiYong Lin, 2009/09/28, Add the sleep event message when sleep */
  254. return error;
  255. }
  256. /*
  257. * This is main interface to the outside world. It needs to be
  258. * called from process context.
  259. */
  260. int software_suspend(void)
  261. {
  262. return enter_state(PM_SUSPEND_DISK);
  263. }
  264. /**
  265. * pm_suspend - Externally visible function for suspending system.
  266. * @state: Enumarted value of state to enter.
  267. *
  268. * Determine whether or not value is within range, get state
  269. * structure, and enter (above).
  270. */
  271. int pm_suspend(suspend_state_t state)
  272. {
  273. if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)
  274. return enter_state(state);
  275. return -EINVAL;
  276. }
  277. EXPORT_SYMBOL(pm_suspend);
  278. decl_subsys(power,NULL,NULL);
  279. /**
  280. * state - control system power state.
  281. *
  282. * show() returns what states are supported, which is hard-coded to
  283. * 'standby' (Power-On Suspend), 'mem' (Suspend-to-RAM), and
  284. * 'disk' (Suspend-to-Disk).
  285. *
  286. * store() accepts one of those strings, translates it into the
  287. * proper enumerated value, and initiates a suspend transition.
  288. */
  289. static ssize_t state_show(struct subsystem * subsys, char * buf)
  290. {
  291. int i;
  292. char * s = buf;
  293. for (i = 0; i < PM_SUSPEND_MAX; i++) {
  294. if (pm_states[i] && valid_state(i))
  295. s += sprintf(s,"%s ", pm_states[i]);
  296. }
  297. s += sprintf(s,"\n");
  298. return (s - buf);
  299. }
  300. static ssize_t state_store(struct subsystem * subsys, const char * buf, size_t n)
  301. {
  302. suspend_state_t state = PM_SUSPEND_STANDBY;
  303. const char * const *s;
  304. char *p;
  305. int error;
  306. int len;
  307. p = memchr(buf, '\n', n);
  308. len = p ? p - buf : n;
  309. for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
  310. if (*s && !strncmp(buf, *s, len))
  311. break;
  312. }
  313. #ifdef CONFIG_PM_CPU_MODE
  314. printk(KERN_ERR "state_store, %d\n", state);
  315. if (state < PM_SUSPEND_MAX && (state == PM_SUSPEND_CPU_MODE ||
  316. state == PM_SUSPEND_MEM) ){
  317. if(pm_cpu_mode){
  318. // state = 0;
  319. error = enter_pm_cpu_mode(state);
  320. }
  321. else{
  322. error = enter_state(state);
  323. }
  324. }
  325. else{
  326. error = -EINVAL;
  327. }
  328. printk(KERN_ERR "end, leave state_store, %d\n", state);
  329. #else
  330. if (state < PM_SUSPEND_MAX && *s)
  331. error = enter_state(state);
  332. else
  333. error = -EINVAL;
  334. #endif
  335. return error ? error : n;
  336. }
  337. power_attr(state);
  338. #ifdef CONFIG_PM_TRACE
  339. int pm_trace_enabled;
  340. static ssize_t pm_trace_show(struct subsystem * subsys, char * buf)
  341. {
  342. return sprintf(buf, "%d\n", pm_trace_enabled);
  343. }
  344. static ssize_t
  345. pm_trace_store(struct subsystem * subsys, const char * buf, size_t n)
  346. {
  347. int val;
  348. if (sscanf(buf, "%d", &val) == 1) {
  349. pm_trace_enabled = !!val;
  350. return n;
  351. }
  352. return -EINVAL;
  353. }
  354. power_attr(pm_trace);
  355. static struct attribute * g[] = {
  356. &state_attr.attr,
  357. &pm_trace_attr.attr,
  358. NULL,
  359. };
  360. #else
  361. static struct attribute * g[] = {
  362. &state_attr.attr,
  363. NULL,
  364. };
  365. #endif /* CONFIG_PM_TRACE */
  366. static struct attribute_group attr_group = {
  367. .attrs = g,
  368. };
  369. static int __init pm_init(void)
  370. {
  371. int error = subsystem_register(&power_subsys);
  372. if (!error)
  373. error = sysfs_create_group(&power_subsys.kset.kobj,&attr_group);
  374. return error;
  375. }
  376. core_initcall(pm_init);