[关闭]
@khan-lau 2014-08-22T05:06:26.000000Z 字数 21392 阅读 3429

android休眠与唤醒驱动流程分析

未分类


标准linux休眠过程:

标准linux唤醒过程:

用户可以通过sys文件系统控制系统进入休眠:

查看系统支持的休眠方式:

  1. #cat/sys/power/state

常见有standby(suspendto RAM)mem(suspend toRAM)disk(suspend todisk),只是standby耗电更多,返回到正常工作状态的时间更短。
通过 #echo mem > /sys/power/state 让系统进入休眠。

Android休眠与唤醒

android是在传统的linux内核电源管理设计的基础上,结合手机设计的实际需求而进化出的一套电源管理系统,其核心内容有:wakelockearly_suspendlate_resume
wakelock在Android的电源管理系统中扮演一个核心的角色。wakelock是一种锁的机制,只要有人拿着这个锁,系统就无法进入休眠,可以被用户态程序和内核获得。这个锁可以是有超时的或者是没有超时的,超时的锁会在时间过去以后自动解锁。如果没有锁了或者超时了,内核就会启动休眠的那套机制来进入休眠。
当系统在启动完毕后,会自己去加一把名为“main“的锁,而当系统有意愿去睡眠时则会先去释放这把“main”锁,在android中,在early_suspend的最后一步会去释放“main”锁(wake_unlock:main)。释放完后则会去检查是否还有其他存在的锁,如果没有则直接进入睡眠过程。
它的缺点是,如果有某一应用获锁而不释放或者因一直在执行某种操作而没时间来释放的话,则会导致系统一直进入不了睡眠状态,功耗过大。

early_suspend:先与linux内核的睡眠过程被调用。一般在手机系统的设计中对背光的操作等采用此类方法,因为背光需要的能耗过大。当然此操作与late_resume是配套使用的。一些在内核中要预先进行处理的事件可以先注册上early_suspend函数,当系统要进入睡眠之前会首先调用这些注册的函数。

本文中,linuxkernel版本为 linux-2.6.29,android版本为android 2.1
与android休眠唤醒主要相关的文件主要有:

Android休眠过程如下:

当用户读写/sys/power/state时,linux_source/kernel/power/main.c中的state_store()函数会被调用。其中,android的early_suspend会执行request_suspend_state(state);而标准的linux休眠则执行error = enter_state(state);

  1. static ssize_t state_store(structkobject *kobj, struct kobj_attribute *attr, const char *buf, size_t n)
  2. {
  3. #ifdef CONFIG_SUSPEND
  4. #ifdef CONFIG_EARLYSUSPEND
  5. suspend_state_t state = PM_SUSPEND_ON;
  6. #else
  7. suspend_state_t state = PM_SUSPEND_STANDBY;
  8. #endif
  9. const char * const *s;
  10. #endif
  11. char *p;
  12. int len;
  13. int error = -EINVAL;
  14. p = memchr(buf, '\n', n);
  15. len = p ? p - buf : n;
  16. /* First, check if we are requestedto hibernate */
  17. if (len == 4 &&!strncmp(buf, "disk", len)) {
  18. error = hibernate();
  19. goto Exit;
  20. }
  21. #ifdef CONFIG_SUSPEND
  22. for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
  23. if ( *s && len ==strlen(*s) && !strncmp(buf, *s, len) )
  24. break;
  25. }
  26. if (state < PM_SUSPEND_MAX && *s)
  27. #ifdefCONFIG_EARLYSUSPEND
  28. if (state == PM_SUSPEND_ON ||valid_state(state)) {
  29. error = 0;
  30. request_suspend_state(state);
  31. }
  32. #else
  33. error= enter_state(state);§
  34. #endif
  35. #endif
  36. Exit:
  37. return error ? error : n;
  38. }

request_suspend_state(state)函数中,会调用early_suspend_work的工作队列,从而进入early_suspend()函数中。

static DECLARE_WORK(early_suspend_work, early_suspend);

  1. void request_suspend_state(suspend_state_t new_state)
  2. {
  3. unsigned long irqflags;
  4. int old_sleep;
  5. spin_lock_irqsave(&state_lock,irqflags);
  6. old_sleep = state &SUSPEND_REQUESTED;
  7. if (debug_mask & DEBUG_USER_STATE) {
  8. struct timespec ts;
  9. struct rtc_time tm;
  10. getnstimeofday(&ts);
  11. rtc_time_to_tm(ts.tv_sec, &tm);
  12. pr_info("request_suspend_state:%s (%d->%d) at %lld "
  13. "(%d-%02d-%02d%02d:%02d:%02d.%09lu UTC)\n",
  14. new_state != PM_SUSPEND_ON ? "sleep" : "wakeup",
  15. requested_suspend_state, new_state,
  16. ktime_to_ns(ktime_get()),
  17. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
  18. tm.tm_hour, tm.tm_min, tm.tm_sec, ts.tv_nsec);
  19. }
  20. if (!old_sleep && new_state!= PM_SUSPEND_ON) {
  21. state |= SUSPEND_REQUESTED;
  22. queue_work(suspend_work_queue,&early_suspend_work);
  23. } else if (old_sleep &&new_state == PM_SUSPEND_ON) {
  24. state &= ~SUSPEND_REQUESTED;
  25. wake_lock(&main_wake_lock);
  26. queue_work(suspend_work_queue,&late_resume_work);§
  27. }
  28. requested_suspend_state =new_state;
  29. spin_unlock_irqrestore(&state_lock,irqflags);
  30. }

early_suspend()函数中,首先要判断当前请求的状态是否还是suspend,若不是,则直接退出了;若是,函数会调用已经注册的early_suspend的函数。然后同步文件系统,最后释放main_wake_lock

  1. static void early_suspend(structwork_struct *work)
  2. {
  3. struct early_suspend *pos;
  4. unsigned long irqflags;
  5. int abort = 0;
  6. mutex_lock(&early_suspend_lock);
  7. spin_lock_irqsave(&state_lock,irqflags);
  8. if (state ==SUSPEND_REQUESTED)
  9. state |= SUSPENDED;
  10. else
  11. abort = 1;
  12. spin_unlock_irqrestore(&state_lock,irqflags);
  13. if (abort) {
  14. if (debug_mask &DEBUG_SUSPEND)
  15. pr_info("early_suspend:abort, state %d\n", state);
  16. mutex_unlock(&early_suspend_lock);
  17. goto abort;
  18. }
  19. if (debug_mask & DEBUG_SUSPEND)
  20. pr_info("early_suspend: callhandlers\n");
  21. list_for_each_entry(pos, &early_suspend_handlers, link) {
  22. if(pos->suspend != NULL)
  23. pos->suspend(pos);
  24. }
  25. mutex_unlock(&early_suspend_lock);
  26. if (debug_mask & DEBUG_SUSPEND)
  27. pr_info("early_suspend:sync\n");
  28. sys_sync();
  29. abort:
  30. spin_lock_irqsave(&state_lock,irqflags);
  31. if (state ==SUSPEND_REQUESTED_AND_SUSPENDED)
  32. wake_unlock(&main_wake_lock);
  33. spin_unlock_irqrestore(&state_lock,irqflags);
  34. }

在wake_unlock()中,删除链表中wake_lock节点,判断当前是否存在wake_lock,若wake_lock的数目为0,则调用工作队列suspend_work,进入suspend状态。

  1. staticDECLARE_WORK(suspend_work, suspend);
  2. void wake_unlock(struct wake_lock*lock)
  3. {
  4. int type;
  5. unsigned long irqflags;
  6. spin_lock_irqsave(&list_lock,irqflags);
  7. type = lock->flags &WAKE_LOCK_TYPE_MASK;
  8. #ifdef CONFIG_WAKELOCK_STAT
  9. wake_unlock_stat_locked(lock, 0);
  10. #endif
  11. if (debug_mask &DEBUG_WAKE_LOCK)
  12. pr_info("wake_unlock: %s\n",lock->name);
  13. lock->flags &=~(WAKE_LOCK_ACTIVE | WAKE_LOCK_AUTO_EXPIRE);
  14. list_del(&lock->link);
  15. list_add(&lock->link,&inactive_locks);
  16. if (type == WAKE_LOCK_SUSPEND) {
  17. longhas_lock = has_wake_lock_locked(type);
  18. if (has_lock > 0) {
  19. if (debug_mask &DEBUG_EXPIRE)
  20. pr_info("wake_unlock: %s,start expire timer, %ld\n", lock->name,has_lock);
  21. mod_timer(&expire_timer,jiffies + has_lock);
  22. } else {
  23. if (del_timer(&expire_timer))
  24. if (debug_mask &DEBUG_EXPIRE)
  25. pr_info("wake_unlock: %s,stop expire timer\n",lock->name);
  26. if(has_lock == 0)
  27. queue_work(suspend_work_queue,&suspend_work);
  28. }
  29. if (lock == &main_wake_lock) {
  30. if (debug_mask &DEBUG_SUSPEND)
  31. print_active_locks(WAKE_LOCK_SUSPEND);
  32. #ifdef CONFIG_WAKELOCK_STAT
  33. update_sleep_wait_stats_locked(0);
  34. #endif
  35. }
  36. }
  37. spin_unlock_irqrestore(&list_lock,irqflags);
  38. }

suspend()函数中,先判断当前是否有wake_lock,若有,则退出;然后同步文件系统,最后调用pm_suspend()函数。

  1. static void suspend(structwork_struct *work)
  2. {
  3. int ret;
  4. int entry_event_num;
  5. if(has_wake_lock(WAKE_LOCK_SUSPEND)) {
  6. if (debug_mask &DEBUG_SUSPEND)
  7. pr_info("suspend: abortsuspend\n");
  8. return;
  9. }
  10. entry_event_num =current_event_num;
  11. sys_sync();
  12. if (debug_mask & DEBUG_SUSPEND)
  13. pr_info("suspend: entersuspend\n");
  14. ret =pm_suspend(requested_suspend_state);
  15. if (debug_mask &DEBUG_EXIT_SUSPEND) {
  16. struct timespec ts;
  17. struct rtc_time tm;
  18. getnstimeofday(&ts);
  19. rtc_time_to_tm(ts.tv_sec, &tm);
  20. pr_info("suspend: exitsuspend, ret = %d "
  21. "(%d-%02d-%02d%02d:%02d:%02d.%09lu UTC)\n", ret,
  22. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
  23. tm.tm_hour, tm.tm_min, tm.tm_sec, ts.tv_nsec);
  24. }
  25. if (current_event_num ==entry_event_num) {
  26. if (debug_mask &DEBUG_SUSPEND)
  27. pr_info("suspend: pm_suspendreturned with no event\n");
  28. wake_lock_timeout(&unknown_wakeup,HZ / 2);
  29. }
  30. }

pm_suspend()函数中,enter_state()函数被调用,从而进入标准linux休眠过程。

  1. int pm_suspend(suspend_state_tstate)
  2. {
  3. if (state > PM_SUSPEND_ON &&state <= PM_SUSPEND_MAX)
  4. return enter_state(state);
  5. return -EINVAL;
  6. }

enter_state()函数中,首先检查一些状态参数,再同步文件系统,然后调用suspend_prepare()来冻结进程,最后调用suspend_devices_and_enter()让外设进入休眠。

  1. static intenter_state(suspend_state_t state)
  2. {
  3. int error;
  4. if (!valid_state(state))
  5. return -ENODEV;
  6. if (!mutex_trylock(&pm_mutex))
  7. return -EBUSY;
  8. printk(KERN_INFO "PM: Syncingfilesystems ... ");
  9. sys_sync();
  10. printk("done.\n");
  11. pr_debug("PM: Preparing systemfor %s sleep\n", pm_states[state]);
  12. error =suspend_prepare();
  13. if (error)
  14. goto Unlock;
  15. if (suspend_test(TEST_FREEZER))
  16. goto Finish;
  17. pr_debug("PM: Entering %ssleep\n", pm_states[state]);
  18. error =suspend_devices_and_enter(state);
  19. Finish:
  20. pr_debug("PM: Finishingwakeup.\n");
  21. suspend_finish();
  22. Unlock:
  23. mutex_unlock(&pm_mutex);
  24. return error;
  25. }

suspend_prepare()函数中,先通过pm_prepare_console();给suspend分配一个虚拟终端来输出信息,再广播一个系统进入suspend的通报,关闭用户态的helper进程,然后调用suspend_freeze_processes()来冻结进程,最后会尝试释放一些内存。

  1. static int suspend_prepare(void)
  2. {
  3. int error;
  4. unsigned int free_pages;
  5. if (!suspend_ops ||!suspend_ops->enter)
  6. return -EPERM;
  7. pm_prepare_console();
  8. error =pm_notifier_call_chain(PM_SUSPEND_PREPARE);
  9. if (error)
  10. goto Finish;
  11. error =usermodehelper_disable();
  12. if (error)
  13. goto Finish;
  14. if(suspend_freeze_processes()) {
  15. error = -EAGAIN;
  16. goto Thaw;
  17. }
  18. free_pages =global_page_state(NR_FREE_PAGES);
  19. if (free_pages <FREE_PAGE_NUMBER) {
  20. pr_debug("PM: free somememory\n");
  21. shrink_all_memory(FREE_PAGE_NUMBER- free_pages);
  22. if (nr_free_pages() <FREE_PAGE_NUMBER) {
  23. error = -ENOMEM;
  24. printk(KERN_ERR "PM: Noenough memory\n");
  25. }
  26. }
  27. if (!error)
  28. return 0;
  29. Thaw:
  30. suspend_thaw_processes();
  31. usermodehelper_enable();
  32. Finish:
  33. pm_notifier_call_chain(PM_POST_SUSPEND);
  34. pm_restore_console();
  35. return error;
  36. }

suspend_freeze_processes()函数中调用了freeze_processes()函数,而freeze_processes()函数中又调用了try_to_freeze_tasks()来完成冻结任务。在冻结过程中,会判断当前进程是否有wake_lock,若有,则冻结失败,函数会放弃冻结。

  1. static int try_to_freeze_tasks(boolsig_only)
  2. {
  3. struct task_struct *g, *p;
  4. unsigned long end_time;
  5. unsigned int todo;
  6. struct timeval start, end;
  7. u64 elapsed_csecs64;
  8. unsigned int elapsed_csecs;
  9. unsigned int wakeup = 0;
  10. do_gettimeofday(&start);
  11. end_time = jiffies + TIMEOUT;
  12. do {
  13. todo = 0;
  14. read_lock(&tasklist_lock);
  15. do_each_thread(g,p) {
  16. if (frozen(p) || !freezeable(p))
  17. continue;
  18. if(!freeze_task(p, sig_only))
  19. continue;
  20. /*
  21. * Now that we've doneset_freeze_flag, don't
  22. * perturb a task in TASK_STOPPEDor TASK_TRACED.
  23. * It is "frozen enough". If the task does wake
  24. * up, it will immediately calltry_to_freeze.
  25. */
  26. if ( !task_is_stopped_or_traced(p)&& !freezer_should_skip(p) )
  27. todo++;
  28. } while_each_thread(g, p);
  29. read_unlock(&tasklist_lock);
  30. yield(); /* Yield is okay here*/
  31. if (todo &&has_wake_lock(WAKE_LOCK_SUSPEND)) {
  32. wakeup = 1;
  33. break;
  34. }
  35. if (time_after(jiffies, end_time))
  36. break;
  37. } while (todo);
  38. do_gettimeofday(&end);
  39. elapsed_csecs64 =timeval_to_ns(&end) - timeval_to_ns(&start);
  40. do_div(elapsed_csecs64,NSEC_PER_SEC / 100);
  41. elapsed_csecs = elapsed_csecs64;
  42. if (todo) {
  43. /* This does not unfreezeprocesses that are already frozen
  44. * (we have slightly ugly callingconvention in that respect,
  45. * and caller must callthaw_processes() if something fails),
  46. * but it cleans up leftoverPF_FREEZE requests.
  47. */
  48. if(wakeup) {
  49. printk("\n");
  50. printk(KERN_ERR "Freezing of%s aborted\n", sig_only ? "user space ": "tasks ");
  51. } else {
  52. printk("\n");
  53. printk(KERN_ERR "Freezing oftasks failed after %d.%02d seconds "
  54. "(%d tasks refusing tofreeze):\n",
  55. elapsed_csecs / 100,elapsed_csecs % 100, todo);
  56. show_state();
  57. }
  58. read_lock(&tasklist_lock);
  59. do_each_thread(g, p) {
  60. task_lock(p);
  61. if (freezing(p) &&!freezer_should_skip(p))
  62. printk(KERN_ERR " %s\n",p->comm);
  63. cancel_freezing(p);
  64. task_unlock(p);
  65. }
  66. while_each_thread(g, p);
  67. read_unlock(&tasklist_lock);
  68. } else {
  69. printk("(elapsed %d.%02dseconds) ", elapsed_csecs / 100,
  70. elapsed_csecs % 100);
  71. }
  72. return todo ? -EBUSY : 0;
  73. }

到现在,所有的进程(也包括workqueue/kthread)都已经停止了,内核态进程有可能在停止的时候握有一些信号量,所以如果这时候在外设里面去解锁这个信号量有可能会发生死锁,所以在外设suspend()函数里面作lock/unlock锁要非常小心,建议不要在外设的suspend()里面等待锁。而且suspend的过程中,有一些log是无法输出的,所以一旦出现问题,非常难调试。

回到enter_state()函数中,再冻结进程完成后,调用suspend_devices_and_enter()函数让外设进入休眠。该函数中,首先休眠串口(之后不能再显示log,解决方法为在kernel配置选项的cmd_line中,添加”no_console_suspend”选项),再通过device_suspend()函数调用各驱动的suspend函数。
当外设进入休眠后,suspend_ops->prepare()被调用,suspend_ops是板级的PM操作(本文中粉红色的函数,依赖于具体的平台),以s3c6410为例,其注册在linux_source/arch/arm/plat-s3c64xx/pm.c中,只定义了suspend_ops->enter()函数。

  1. static struct platform_suspend_opss3c6410_pm_ops = {
  2. .enter = s3c6410_pm_enter,
  3. .valid = suspend_valid_only_mem,
  4. };

接下来,多CPU中的非启动CPU被关闭。

  1. intsuspend_devices_and_enter(suspend_state_t state)
  2. {
  3. int error;
  4. if (!suspend_ops)
  5. return -ENOSYS;
  6. if (suspend_ops->begin) {
  7. error =suspend_ops->begin(state);
  8. if (error)
  9. goto Close;
  10. }
  11. suspend_console();
  12. suspend_test_start();
  13. error =device_suspend(PMSG_SUSPEND);
  14. if (error) {
  15. printk(KERN_ERR "PM: Somedevices failed to suspend\n");
  16. goto Recover_platform;
  17. }
  18. suspend_test_finish("suspenddevices");
  19. if (suspend_test(TEST_DEVICES))
  20. goto Recover_platform;
  21. if (suspend_ops->prepare) {
  22. error =suspend_ops->prepare();
  23. if (error)
  24. goto Resume_devices;
  25. }
  26. if (suspend_test(TEST_PLATFORM))
  27. goto Finish;
  28. error =disable_nonboot_cpus();
  29. if (!error &&!suspend_test(TEST_CPUS))
  30. suspend_enter(state);
  31. enable_nonboot_cpus();
  32. Finish:
  33. if (suspend_ops->finish)
  34. suspend_ops->finish();
  35. Resume_devices:
  36. suspend_test_start();
  37. device_resume(PMSG_RESUME);
  38. suspend_test_finish("resumedevices");
  39. resume_console();
  40. Close:
  41. if (suspend_ops->end)
  42. suspend_ops->end();
  43. return error;
  44. Recover_platform:
  45. if (suspend_ops->recover)
  46. suspend_ops->recover();
  47. goto Resume_devices;
  48. }

接下来suspend_enter()被调用,该函数首先关闭IRQ,然后调用device_power_down(),它会调用suspend_late()函数,这个函数是系统真正进入休眠最后调用的函数,通常会在这个函数中作最后的检查,接下来休眠所有的系统设备和总线。最后调用suspend_pos->enter()来使CPU进入省电状态。这时候,整个休眠过程完成,代码的执行也就停在这里了。

  1. static intsuspend_enter(suspend_state_t state)
  2. {
  3. int error = 0;
  4. device_pm_lock();
  5. #ifdef CONFIG_CPU_FREQ
  6. cpufreq_get_cpufreq_name(0);
  7. strcpy(governor_name,cpufreq_governor_name);
  8. if(strnicmp(governor_name,userspace_governor, CPUFREQ_NAME_LEN)) {
  9. cpufreq_set_policy(0,"performance");
  10. }
  11. #endif /* CONFIG_CPU_FREQ */
  12. arch_suspend_disable_irqs();
  13. BUG_ON(!irqs_disabled());
  14. if ((error =device_power_down(PMSG_SUSPEND))) {
  15. printk(KERN_ERR "PM: Somedevices failed to power down\n");
  16. goto Done;
  17. }
  18. error =sysdev_suspend(PMSG_SUSPEND);
  19. if (!error) {
  20. if (!suspend_test(TEST_CORE))
  21. error =suspend_ops->enter(state);
  22. sysdev_resume();
  23. }
  24. device_power_up(PMSG_RESUME);
  25. Done:
  26. arch_suspend_enable_irqs();
  27. #ifdef CONFIG_CPU_FREQ
  28. if(strnicmp(governor_name,userspace_governor, CPUFREQ_NAME_LEN)) {
  29. cpufreq_set_policy(0,governor_name);
  30. }
  31. #endif /* CONFIG_CPU_FREQ */
  32. BUG_ON(irqs_disabled());
  33. device_pm_unlock();
  34. return error;
  35. }

suspend_pos->enter()所对应的函数中,代码最终停止在pm_cpu_sleep();处。

  1. static ints3c6410_pm_enter(suspend_state_t state)
  2. {
  3. ……
  4. s3c6410_pm_do_save(gpio_save,ARRAY_SIZE(gpio_save));
  5. s3c6410_pm_do_save(irq_save,ARRAY_SIZE(irq_save));
  6. s3c6410_pm_do_save(core_save,ARRAY_SIZE(core_save));
  7. s3c6410_pm_do_save(sromc_save,ARRAY_SIZE(sromc_save));
  8. /* Clear WAKEUP_STAT register fornext wakeup -jc.lee */
  9. /* If this register do not becleared, Wakeup will be failed */
  10. __raw_writel(__raw_readl(S3C_WAKEUP_STAT),S3C_WAKEUP_STAT);
  11. #ifdef CONFIG_MACH_SMDK6410
  12. /* ALL sub block "ON"before enterring sleep mode - EVT0 bug*/
  13. __raw_writel(0xffffff00,S3C_NORMAL_CFG);
  14. /* Open all clock gate to entersleep mode - EVT0 bug*/
  15. __raw_writel(0xffffffff,S3C_HCLK_GATE);
  16. __raw_writel(0xffffffff,S3C_PCLK_GATE);
  17. __raw_writel(0xffffffff,S3C_SCLK_GATE);
  18. ……
  19. /* s3c6410_cpu_save will also actas our return point from when
  20. * we resume as it saves its ownregister state, so use the return
  21. * code to differentiate returnfrom save and return from sleep */
  22. if(s3c6410_cpu_save(regs_save) == 0) {
  23. flush_cache_all();
  24. pm_cpu_sleep();
  25. }
  26. /* restorethe cpu state */
  27. cpu_init();
  28. __raw_writel(s3c_eint_mask_val,S3C_EINT_MASK);
  29. /* restore the system state */
  30. s3c6410_pm_do_restore_core(core_save,ARRAY_SIZE(core_save));
  31. s3c6410_pm_do_restore(sromc_save,ARRAY_SIZE(sromc_save));
  32. ……
  33. }

Android唤醒过程如下:

如果在休眠中系统被中断或者其他事件唤醒,接下来的代码就从suspend完成的地方开始执行,以s3c6410为例,即pm.c中的s3c6410_pm_enter()中的cpu_init(),然后执行suspend_enter()sysdev_resume()函数,唤醒系统设备和总线,使能系统中断。

  1. static intsuspend_enter(suspend_state_t state)
  2. {
  3. int error = 0;
  4. device_pm_lock();
  5. #ifdef CONFIG_CPU_FREQ
  6. cpufreq_get_cpufreq_name(0);
  7. strcpy(governor_name,cpufreq_governor_name);
  8. if(strnicmp(governor_name,userspace_governor, CPUFREQ_NAME_LEN)) {
  9. cpufreq_set_policy(0,"performance");
  10. }
  11. #endif /* CONFIG_CPU_FREQ */
  12. arch_suspend_disable_irqs();
  13. BUG_ON(!irqs_disabled());
  14. if ((error =device_power_down(PMSG_SUSPEND))) {
  15. printk(KERN_ERR "PM: Somedevices failed to power down\n");
  16. goto Done;
  17. }
  18. error =sysdev_suspend(PMSG_SUSPEND);
  19. if (!error) {
  20. if (!suspend_test(TEST_CORE))
  21. error =suspend_ops->enter(state); //suspend过程完成处
  22. sysdev_resume();
  23. }
  24. device_power_up(PMSG_RESUME);
  25. Done:
  26. arch_suspend_enable_irqs();
  27. #ifdef CONFIG_CPU_FREQ
  28. if(strnicmp(governor_name,userspace_governor, CPUFREQ_NAME_LEN)) {
  29. cpufreq_set_policy(0,governor_name);
  30. }
  31. #endif /* CONFIG_CPU_FREQ */
  32. BUG_ON(irqs_disabled());
  33. device_pm_unlock();
  34. return error;
  35. }

然后回到suspend_devices_and_enter()函数中,使能休眠时候停止掉的非启动CPU,继续唤醒每个设备,使能终端。

  1. intsuspend_devices_and_enter(suspend_state_t state)
  2. {
  3. int error;
  4. if (!suspend_ops)
  5. return -ENOSYS;
  6. if (suspend_ops->begin) {
  7. error =suspend_ops->begin(state);
  8. if (error)
  9. goto Close;
  10. }
  11. suspend_console();
  12. suspend_test_start();
  13. error =device_suspend(PMSG_SUSPEND);
  14. if (error) {
  15. printk(KERN_ERR "PM: Somedevices failed to suspend\n");
  16. goto Recover_platform;
  17. }
  18. suspend_test_finish("suspenddevices");
  19. if (suspend_test(TEST_DEVICES))
  20. goto Recover_platform;
  21. if (suspend_ops->prepare) {
  22. error =suspend_ops->prepare();
  23. if (error)
  24. goto Resume_devices;
  25. }
  26. if ( suspend_test(TEST_PLATFORM) )
  27. goto Finish;
  28. error = disable_nonboot_cpus();
  29. if ( !error &&!suspend_test(TEST_CPUS) )
  30. suspend_enter(state); //suspend过程完成处
  31. enable_nonboot_cpus();
  32. Finish:
  33. if (suspend_ops->finish)
  34. suspend_ops->finish();
  35. Resume_devices:
  36. suspend_test_start();
  37. device_resume(PMSG_RESUME);
  38. suspend_test_finish("resumedevices");
  39. resume_console();
  40. Close:
  41. if (suspend_ops->end)
  42. suspend_ops->end();
  43. return error;
  44. Recover_platform:
  45. if (suspend_ops->recover)
  46. suspend_ops->recover();
  47. goto Resume_devices;
  48. }

suspend_devices_and_enter()执行完成后,系统外设已经唤醒,但进程依然是冻结的状态,返回到enter_state函数中,调用suspend_finish()函数。

  1. static intenter_state(suspend_state_t state)
  2. {
  3. int error;
  4. if (!valid_state(state))
  5. return -ENODEV;
  6. if (!mutex_trylock(&pm_mutex))
  7. return -EBUSY;
  8. printk(KERN_INFO "PM: Syncingfilesystems ... ");
  9. sys_sync();
  10. printk("done.\n");
  11. pr_debug("PM: Preparing systemfor %s sleep\n", pm_states[state]);
  12. error = suspend_prepare();
  13. if (error)
  14. goto Unlock;
  15. if (suspend_test(TEST_FREEZER))
  16. goto Finish;
  17. pr_debug("PM: Entering %ssleep\n", pm_states[state]);
  18. error =suspend_devices_and_enter(state); //suspend过程完成处
  19. Finish:
  20. pr_debug("PM: Finishingwakeup.\n");
  21. suspend_finish();
  22. Unlock:
  23. mutex_unlock(&pm_mutex);
  24. return error;
  25. }

suspend_finish()函数中,解冻进程和任务,使能用户空间helper进程,广播一个系统从suspend状态退出的notify,唤醒终端。

  1. static void suspend_finish(void)
  2. {
  3. suspend_thaw_processes();
  4. usermodehelper_enable();
  5. pm_notifier_call_chain(PM_POST_SUSPEND);
  6. pm_restore_console();
  7. }

当所有的唤醒已经结束以后,用户进程都已经开始运行了,但没点亮屏幕,唤醒通常会是以下的几种原因:
如果是来电,那么Modem会通过发送命令给rild来让rild通知WindowManager有来电响应,这样就会远程调用PowerManagerService来写”on”到/sys/power/state 来调用lateresume(),执行点亮屏幕等操作。
用户按键事件会送到WindowManager中,WindowManager会处理这些按键事件,按键分为几种情况,如果按键不是唤醒键,那么WindowManager会主动放弃wakeLock来使系统进入再次休眠;如果按键是唤醒键,那么WindowManger就会调用PowerManagerService中的接口来执行lateResume

”on”被写入到/sys/power/state之后,同early_suspend过程,request_suspend_state()被调用,只是执行的工作队列变为late_resume_work。在late_resume函数中,唤醒调用了early_suspend的设备。

  1. static DECLARE_WORK(late_resume_work, late_resume);
  2. static void late_resume(structwork_struct *work)
  3. {
  4. struct early_suspend *pos;
  5. unsigned long irqflags;
  6. int abort = 0;
  7. mutex_lock(&early_suspend_lock);
  8. spin_lock_irqsave(&state_lock,irqflags);
  9. if (state == SUSPENDED)
  10. state &= ~SUSPENDED;
  11. else
  12. abort = 1;
  13. spin_unlock_irqrestore(&state_lock,irqflags);
  14. if (abort) {
  15. if (debug_mask &DEBUG_SUSPEND)
  16. pr_info("late_resume: abort,state %d\n", state);
  17. goto Abort;
  18. }
  19. if (debug_mask & DEBUG_SUSPEND)
  20. pr_info("late_resume: callhandlers\n");
  21. list_for_each_entry_reverse(pos,&early_suspend_handlers, link)
  22. if(pos->resume != NULL)
  23. pos->resume(pos);
  24. if (debug_mask & DEBUG_SUSPEND)
  25. pr_info("late_resume:done\n");
  26. Abort:
  27. mutex_unlock(&early_suspend_lock);
  28. }

关于wake_lock
在上文中,已经介绍了wakelock机制,下面从代码的角度进行介绍。
wakelock有3种类型,常用为WAKE_LOCK_SUSPEND,作用是防止系统进入睡眠。其他类型不是很清楚。

  1. enum {
  2. WAKE_LOCK_SUSPEND, /* Preventsuspend */
  3. WAKE_LOCK_IDLE, /* Prevent lowpower idle */
  4. WAKE_LOCK_TYPE_COUNT
  5. };

Wakelock有加锁和解锁2种操作,加锁有2种方式,第一种是永久加锁(wake_lock),这种锁必须手动的解锁;另一种是超时锁(wake_lock_timeout),这种锁在过去指定时间后,会自动解锁。

  1. void wake_lock(struct wake_lock*lock)
  2. {
  3. wake_lock_internal(lock, 0, 0);
  4. }
  5. void wake_lock_timeout(structwake_lock *lock, long timeout)
  6. {
  7. wake_lock_internal(lock, timeout,1);
  8. }

对于wakelock,timeout = has_timeout = 0;直接加锁后,然后退出;

  1. static void wake_lock_internal( struct wake_lock *lock, longtimeout, int has_timeout)
  2. {
  3. int type;
  4. unsigned long irqflags;
  5. long expire_in;
  6. spin_lock_irqsave(&list_lock,irqflags);
  7. type = lock->flags &WAKE_LOCK_TYPE_MASK;
  8. BUG_ON(type >=WAKE_LOCK_TYPE_COUNT);
  9. BUG_ON(!(lock->flags &WAKE_LOCK_INITIALIZED));
  10. #ifdef CONFIG_WAKELOCK_STAT
  11. if (type == WAKE_LOCK_SUSPEND &&wait_for_wakeup) {
  12. if (debug_mask & DEBUG_WAKEUP)
  13. pr_info("wakeup wake lock:%s\n", lock->name);
  14. wait_for_wakeup = 0;
  15. lock->stat.wakeup_count++;
  16. }
  17. if ((lock->flags &WAKE_LOCK_AUTO_EXPIRE) && (long)(lock->expires -jiffies) <= 0) {
  18. wake_unlock_stat_locked(lock, 0);
  19. lock->stat.last_time =ktime_get();
  20. }
  21. #endif
  22. if (!(lock->flags &WAKE_LOCK_ACTIVE)) {
  23. lock->flags |=WAKE_LOCK_ACTIVE;
  24. #ifdef CONFIG_WAKELOCK_STAT
  25. lock->stat.last_time =ktime_get();
  26. #endif
  27. }
  28. list_del(&lock->link);
  29. if(has_timeout) {
  30. if (debug_mask &DEBUG_WAKE_LOCK)
  31. pr_info("wake_lock: %s, type%d, timeout %ld.%03lu\n",
  32. lock->name, type, timeout /HZ, (timeout % HZ) * MSEC_PER_SEC /HZ);
  33. lock->expires= jiffies + timeout;
  34. lock->flags |=WAKE_LOCK_AUTO_EXPIRE;
  35. list_add_tail(&lock->link,&active_wake_locks[type]);
  36. } else {
  37. if (debug_mask &DEBUG_WAKE_LOCK)
  38. pr_info("wake_lock: %s, type%d\n", lock->name, type);
  39. lock->expires = LONG_MAX;
  40. lock->flags &=~WAKE_LOCK_AUTO_EXPIRE;
  41. list_add(&lock->link,&active_wake_locks[type]);
  42. }
  43. if (type == WAKE_LOCK_SUSPEND) {
  44. current_event_num++;
  45. #ifdef CONFIG_WAKELOCK_STAT
  46. if (lock == &main_wake_lock)
  47. update_sleep_wait_stats_locked(1);
  48. else if(!wake_lock_active(&main_wake_lock))
  49. update_sleep_wait_stats_locked(0);
  50. #endif
  51. if (has_timeout)
  52. expire_in =has_wake_lock_locked(type);
  53. else
  54. expire_in = -1;
  55. if (expire_in > 0) {
  56. if (debug_mask &DEBUG_EXPIRE)
  57. pr_info("wake_lock: %s,start expire timer, %ld\n", lock->name,expire_in);
  58. mod_timer(&expire_timer,jiffies + expire_in);
  59. } else {
  60. if (del_timer(&expire_timer))
  61. if (debug_mask &DEBUG_EXPIRE)
  62. pr_info("wake_lock: %s,stop expire timer\n", lock->name);
  63. if (expire_in == 0)
  64. queue_work(suspend_work_queue,&suspend_work);
  65. }
  66. }
  67. spin_unlock_irqrestore(&list_lock,irqflags);
  68. }

而对于wake_lock_timeout,在经过timeout时间后,才加锁。再判断当前持有wakelock时,启动另一个定时器,在expire_timer的回调函数中再次判断是否持有wakelock

  1. static voidexpire_wake_locks(unsigned long data)
  2. {
  3. long has_lock;
  4. unsigned long irqflags;
  5. if (debug_mask & DEBUG_EXPIRE)
  6. pr_info("expire_wake_locks:start\n");
  7. spin_lock_irqsave(&list_lock,irqflags);
  8. if (debug_mask & DEBUG_SUSPEND)
  9. print_active_locks(WAKE_LOCK_SUSPEND);
  10. has_lock =has_wake_lock_locked(WAKE_LOCK_SUSPEND);
  11. if (debug_mask & DEBUG_EXPIRE)
  12. pr_info("expire_wake_locks:done, has_lock %ld\n", has_lock);
  13. if (has_lock== 0)
  14. queue_work(suspend_work_queue,&suspend_work);
  15. spin_unlock_irqrestore(&list_lock,irqflags);
  16. }
  17. static DEFINE_TIMER(expire_timer,expire_wake_locks, 0, 0);

wakelock中,有2个地方可以让系统从early_suspend进入suspend状态。分别是:
wake_unlock中,解锁之后,若没有其他的wakelock,则进入suspend
在超时锁的定时器超时后,定时器的回调函数,会判断有没有其他的wakelock,若没有,则进入suspend

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注