android 休眠唤醒机制分析(三) — suspend

2019-07-14 02:09发布

前面我们分析了休眠的第一个阶段即浅度休眠,现在我们继续看休眠的第二个阶段 — 深度休眠。在深度休眠的过程中系统会首先冻结所有可以冻结的进程,然后依次挂起所有设备的电源,挂起顺序与设备注册的顺序相反,这样保证了设备之间电源的依赖性;直至最后进入省电模式,等待用户或者RTC唤醒;在唤醒过程中则会按照设备注册的顺序依次恢复每个设备的电源进入正常工作状态,解冻相关的进程,然后再进行浅度休眠的唤醒流程。 1、深度休眠入口 根据wake_lock一节的分析我们知道driver层进入深度休眠的入口有4个,分别为expire_timer、wake_lock、wake_lock_timeout、wake_unlock,这几个入口函数将根据相应的条件启动suspend_work里面的pm_suspend()函数进入深度休眠流程,代码在Linux/kernel/power/suspend.c中: [cpp] view plain copy print?
  1. // 进入深度休眠流程  
  2. int enter_state(suspend_state_t state)  
  3. {  
  4.     int error;  
  5.     // 判断平台是否支持该状态  
  6.     if (!valid_state(state))  
  7.         return -ENODEV;  
  8.   
  9.     if (!mutex_trylock(&pm_mutex))  
  10.         return -EBUSY;  
  11.     // 同步缓存  
  12.     printk(KERN_INFO "PM: Syncing filesystems ... ");  
  13.     sys_sync();  
  14.     printk("done. ");  
  15.   
  16.     pr_debug("PM: Preparing system for %s sleep ", pm_states[state]);  
  17.     // 做好休眠准备  
  18.     error = suspend_prepare();  
  19.     if (error)  
  20.         goto Unlock;  
  21.     // suspend_test  
  22.     if (suspend_test(TEST_FREEZER))  
  23.         goto Finish;  
  24.   
  25.     pr_debug("PM: Entering %s sleep ", pm_states[state]);  
  26.     // 设备休眠  
  27.     error = suspend_devices_and_enter(state);  
  28.   
  29.  Finish:  
  30.     pr_debug("PM: Finishing wakeup. ");  
  31.     suspend_finish();  
  32.  Unlock:  
  33.     mutex_unlock(&pm_mutex);  
  34.     return error;  
  35. }  
  36.   
  37. int pm_suspend(suspend_state_t state)  
  38. {  
  39.     if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX)  
  40.         return enter_state(state);  
  41.     return -EINVAL;  
  42. }  
  43. EXPORT_SYMBOL(pm_suspend);  
save_snippets.png // 进入深度休眠流程 int enter_state(suspend_state_t state) { int error; // 判断平台是否支持该状态 if (!valid_state(state)) return -ENODEV; if (!mutex_trylock(&pm_mutex)) return -EBUSY; // 同步缓存 printk(KERN_INFO "PM: Syncing filesystems ... "); sys_sync(); printk("done. "); pr_debug("PM: Preparing system for %s sleep ", pm_states[state]); // 做好休眠准备 error = suspend_prepare(); if (error) goto Unlock; // suspend_test if (suspend_test(TEST_FREEZER)) goto Finish; pr_debug("PM: Entering %s sleep ", pm_states[state]); // 设备休眠 error = suspend_devices_and_enter(state); Finish: pr_debug("PM: Finishing wakeup. "); suspend_finish(); Unlock: mutex_unlock(&pm_mutex); return error; } int pm_suspend(suspend_state_t state) { if (state > PM_SUSPEND_ON && state <= PM_SUSPEND_MAX) return enter_state(state); return -EINVAL; } EXPORT_SYMBOL(pm_suspend); 在enter_state()中首先进入状态的判断,根据平台的特性判断是否支持此状态;然后再同步缓存;接着调用suspend_prepare()冻结大部分进程;然后再通过suspend_devices_and_enter()开始挂起设备。 2、冻结进程 [cpp] view plain copy print?
  1. static int suspend_prepare(void)  
  2. {  
  3.     int error;  
  4.   
  5.     if (!suspend_ops || !suspend_ops->enter)  
  6.         return -EPERM;  
  7.   
  8.     pm_prepare_console();  
  9.   
  10.     // 通知进行休眠准备  
  11.     error = pm_notifier_call_chain(PM_SUSPEND_PREPARE);  
  12.     if (error)  
  13.         goto Finish;  
  14.     // 禁止usermodehelper  
  15.     error = usermodehelper_disable();  
  16.     if (error)  
  17.         goto Finish;  
  18.     // 冻结所有可以冻结的进程  
  19.     error = suspend_freeze_processes();  
  20.     if (!error)  
  21.         return 0;  
  22.   
  23.     // 解冻所有进程  
  24.     suspend_thaw_processes();  
  25.     // 使能usermodehelper  
  26.     usermodehelper_enable();  
  27.  Finish:  
  28.     // 通知休眠结束  
  29.     pm_notifier_call_chain(PM_POST_SUSPEND);  
  30.     pm_restore_console();  
  31.     return error;  
  32. }  
save_snippets.png static int suspend_prepare(void) { int error; if (!suspend_ops || !suspend_ops->enter) return -EPERM; pm_prepare_console(); // 通知进行休眠准备 error = pm_notifier_call_chain(PM_SUSPEND_PREPARE); if (error) goto Finish; // 禁止usermodehelper error = usermodehelper_disable(); if (error) goto Finish; // 冻结所有可以冻结的进程 error = suspend_freeze_processes(); if (!error) return 0; // 解冻所有进程 suspend_thaw_processes(); // 使能usermodehelper usermodehelper_enable(); Finish: // 通知休眠结束 pm_notifier_call_chain(PM_POST_SUSPEND); pm_restore_console(); return error; } 这里有一个notifier机制后面要专门分析下。 3、挂起设备 [cpp] view plain copy print?
  1. int suspend_devices_and_enter(suspend_state_t state)  
  2. {  
  3.     int error;  
  4.   
  5.     if (!suspend_ops)  
  6.         return -ENOSYS;  
  7.     // 处理器的休眠开始函数  
  8.     if (suspend_ops->begin) {  
  9.         error = suspend_ops->begin(state);  
  10.         if (error)  
  11.             goto Close;  
  12.     }  
  13.     // 休眠串口  
  14.     suspend_console();  
  15.     suspend_test_start();  
  16.     // 设备休眠  
  17.     error = dpm_suspend_start(PMSG_SUSPEND);  
  18.     if (error) {  
  19.         printk(KERN_ERR "PM: Some devices failed to suspend ");  
  20.         goto Recover_platform;  
  21.     }  
  22.     suspend_test_finish("suspend devices");  
  23.     if (suspend_test(TEST_DEVICES))  
  24.         goto Recover_platform;  
  25.     // 处理器休眠  
  26.     suspend_enter(state);  
  27.   
  28.  Resume_devices:  
  29.     suspend_test_start();  
  30.     // 设备唤醒  
  31.     dpm_resume_end(PMSG_RESUME);  
  32.     suspend_test_finish("resume devices");  
  33.     // 唤醒串口  
  34.     resume_console();  
  35.  Close:  
  36.     // 处理器的休眠结束函数  
  37.     if (suspend_ops->end)  
  38.         suspend_ops->end();  
  39.     return error;  
  40.   
  41.  Recover_platform:  
  42.     if (suspend_ops->recover)  
  43.         suspend_ops->recover();  
  44.     goto Resume_devices;  
  45. }  
save_snippets.png int suspend_devices_and_enter(suspend_state_t state) { int error; if (!suspend_ops) return -ENOSYS; // 处理器的休眠开始函数 if (suspend_ops->begin) { error = suspend_ops->begin(state); if (error) goto Close; } // 休眠串口 suspend_console(); suspend_test_start(); // 设备休眠 error = dpm_suspend_start(PMSG_SUSPEND); if (error) { printk(KERN_ERR "PM: Some devices failed to suspend "); goto Recover_platform; } suspend_test_finish("suspend devices"); if (suspend_test(TEST_DEVICES)) goto Recover_platform; // 处理器休眠 suspend_enter(state); Resume_devices: suspend_test_start(); // 设备唤醒 dpm_resume_end(PMSG_RESUME); suspend_test_finish("resume devices"); // 唤醒串口 resume_console(); Close: // 处理器的休眠结束函数 if (suspend_ops->end) suspend_ops->end(); return error; Recover_platform: if (suspend_ops->recover) suspend_ops->recover(); goto Resume_devices; }可以看到设备挂起流程先从处理器自身开始,平台一般不需要做特殊的处理;接着关闭串口,然后调用dpm_suspend_start()开始挂起设备,如果成功挂起所有设备则调用suspend_enter()挂起处理器。挂起设备部分的代码在linux/driver/base/power/main.c中 [cpp] view plain copy print?
  1. int dpm_suspend_start(pm_message_t state)  
  2. {  
  3.     int error;  
  4.   
  5.     might_sleep();  
  6.     error = dpm_prepare(state);  
  7.     if (!error)  
  8.         error = dpm_suspend(state);  
  9.     return error;  
  10. }  
  11. EXPORT_SYMBOL_GPL(dpm_suspend_start);  
save_snippets.png int dpm_suspend_start(pm_message_t state) { int error; might_sleep(); error = dpm_prepare(state); if (!error) error = dpm_suspend(state); return error; } EXPORT_SYMBOL_GPL(dpm_suspend_start);挂起设备分为2个步骤,首先执行设备的prepare函数,然后再执行suspend函数。
[cpp] view plain copy print?
  1. // 函数将会调用所有的非sysdev设备的prepare()接口  
  2. static int dpm_prepare(pm_message_t state)  
  3. {  
  4.     struct list_head list;  
  5.     int error = 0;  
  6.   
  7.     INIT_LIST_HEAD(&list);  
  8.     mutex_lock(&dpm_list_mtx);  
  9.     transition_started = true;  
  10.     // 遍历设备链表  
  11.     while (!list_empty(&dpm_list)) {  
  12.         // 从最先初始化的节点开始遍历  
  13.         struct device *dev = to_device(dpm_list.next);  
  14.         // 获取设备  
  15.         get_device(dev);  
  16.         // 更新设备状态  
  17.         dev->power.status = DPM_PREPARING;  
  18.         mutex_unlock(&dpm_list_mtx);  
  19.   
  20.         pm_runtime_get_noresume(dev);  
  21.         // 在系统休眠期间有可能受到唤醒请求  
  22.         if (pm_runtime_barrier(dev) && device_may_wakeup(dev)) {  
  23.             /* Wake-up requested during system sleep transition. */  
  24.             pm_runtime_put_noidle(dev);  
  25.             error = -EBUSY;  
  26.         } else {  // 执行prepare()函数  
  27.             error = device_prepare(dev, state);  
  28.         }  
  29.   
  30.         mutex_lock(&dpm_list_mtx);  
  31.         // 如果出错则跳出循环  
  32.         if (error) {  
  33.             dev->power.status = DPM_ON;  
  34.             if (error == -EAGAIN) {  
  35.                 put_device(dev);  
  36.                 error = 0;  
  37.                 continue;  
  38.             }  
  39.             printk(KERN_ERR "PM: Failed to prepare device %s "  
  40.                 "for power transition: error %d ",  
  41.                 kobject_name(&dev->kobj), error);  
  42.             put_device(dev);  
  43.             break;  
  44.         }  
  45.         // 更新状态  
  46.         dev->power.status = DPM_SUSPENDING;  
  47.         if (!list_empty(&dev->power.entry))  
  48.             // 将设备节点移动到list链表中  
  49.             list_move_tail(&dev->power.entry, &list);  
  50.         put_device(dev);  
  51.     }  
  52.     // 拼接链表  
  53.     list_splice(&list, &dpm_list);  
  54.     mutex_unlock(&dpm_list_mtx);  
  55.     return error;  
  56. }  
save_snippets.png // 函数将会调用所有的非sysdev设备的prepare()接口 static int dpm_prepare(pm_message_t state) { struct list_head list; int error = 0; INIT_LIST_HEAD(&list); mutex_lock(&dpm_list_mtx); transition_started = true; // 遍历设备链表 while (!list_empty(&dpm_list)) { // 从最先初始化的节点开始遍历 struct device *dev = to_device(dpm_list.next); // 获取设备 get_device(dev); // 更新设备状态 dev->power.status = DPM_PREPARING; mutex_unlock(&dpm_list_mtx); pm_runtime_get_noresume(dev); // 在系统休眠期间有可能受到唤醒请求 if (pm_runtime_barrier(dev) && device_may_wakeup(dev)) { /* Wake-up requested during system sleep transition. */ pm_runtime_put_noidle(dev); error = -EBUSY; } else { // 执行prepare()函数 error = device_prepare(dev, state); } mutex_lock(&dpm_list_mtx); // 如果出错则跳出循环 if (error) { dev->power.status = DPM_ON; if (error == -EAGAIN) { put_device(dev); error = 0; continue; } printk(KERN_ERR "PM: Failed to prepare device %s " "for power transition: error %d ", kobject_name(&dev->kobj), error); put_device(dev); break; } // 更新状态 dev->power.status = DPM_SUSPENDING; if (!list_empty(&dev->power.entry)) // 将设备节点移动到list链表中 list_move_tail(&dev->power.entry, &list); put_device(dev); } // 拼接链表 list_splice(&list, &dpm_list); mutex_unlock(&dpm_list_mtx); return error; }可以看到函数将遍历dpm_list链表,并执行每个设备的prepare函数,内核规定prepare函数的实现不能改变硬件的状态;系统中每一个设备注册时都将被加入dpm_list链表的尾部,所以链表排序为设备注册的顺序。
[cpp] view plain copy print?
  1. static int dpm_suspend(pm_message_t state)  
  2. {  
  3.     struct list_head list;  
  4.     int error = 0;  
  5.   
  6.     INIT_LIST_HEAD(&list);  
  7.     mutex_lock(&dpm_list_mtx);  
  8.     while (!list_empty(&dpm_list)) {  
  9.         // 逆序遍历链表,即先suspend后注册的设备,符合设备与父设备电源挂起的先后原则  
  10.         struct device *dev = to_device(dpm_list.prev);  
  11.   
  12.         get_device(dev);  
  13.         mutex_unlock(&dpm_list_mtx);  
  14.   
  15.         dpm_drv_wdset(dev);  
  16.         error = device_suspend(dev, state);  
  17.         dpm_drv_wdclr(dev);  
  18.   
  19.         mutex_lock(&dpm_list_mtx);  
  20.         if (error) {  
  21.             pm_dev_err(dev, state, "", error);  
  22.             put_device(dev);  
  23.             break;  
  24.         }  
  25.         dev->power.status = DPM_OFF;  
  26.         if (!list_empty(&dev->power.entry))  
  27.             list_move(&dev->power.entry, &list);  
  28.         put_device(dev);  
  29.     }  
  30.     list_splice(&list, dpm_list.prev);  
  31.     mutex_unlock(&dpm_list_mtx);  
  32.     return error;  
  33. }  
save_snippets.png static int dpm_suspend(pm_message_t state) { struct list_head list; int error = 0; INIT_LIST_HEAD(&list); mutex_lock(&dpm_list_mtx); while (!list_empty(&dpm_list)) { // 逆序遍历链表,即先suspend后注册的设备,符合设备与父设备电源挂起的先后原则 struct device *dev = to_device(dpm_list.prev); get_device(dev); mutex_unlock(&dpm_list_mtx); dpm_drv_wdset(dev); error = device_suspend(dev, state); dpm_drv_wdclr(dev); mutex_lock(&dpm_list_mtx); if (error) { pm_dev_err(dev, state, "", error); put_device(dev); break; } dev->power.status = DPM_OFF; if (!list_empty(&dev->power.entry)) list_move(&dev->power.entry, &list); put_device(dev); } list_splice(&list, dpm_list.prev); mutex_unlock(&dpm_list_mtx); return error; } 函数将设备按照注册顺序反向挂起,挂起执行的流程如下: [cpp] view plain copy print?
  1. static int device_suspend(struct device *dev, pm_message_t state)  
  2. {  
  3.     int error = 0;  
  4.   
  5.     down(&dev->sem);  
  6.   
  7.     if (dev->class) {  // 类的suspend优先  
  8.         if (dev->class->pm) {  
  9.             pm_dev_dbg(dev, state, "class ");  
  10.             error = pm_op(dev, dev->class->pm, state);  
  11.         } else if (dev->class->suspend) {  
  12.             pm_dev_dbg(dev, state, "legacy class ");  
  13.             error = dev->class->suspend(dev, state);  
  14.             suspend_report_result(dev->class->suspend, error);  
  15.         }  
  16.         if (error)  
  17.             goto End;  
  18.     }  
  19.   
  20.     if (dev->type) {  // device_type次之  
  21.         if (dev->type->pm) {  
  22.             pm_dev_dbg(dev, state, "type ");  
  23.             error = pm_op(dev, dev->type->pm, state);  
  24.         }  
  25.         if (error)  
  26.             goto End;  
  27.     }  
  28.   
  29.     if (dev->bus) {  // bus优先级最低  
  30.         if (dev->bus->pm) {  
  31.             pm_dev_dbg(dev, state, "");  
  32.             error = pm_op(dev, dev->bus->pm, state);  
  33.         } else if (dev->bus->suspend) {  
  34.             pm_dev_dbg(dev, state, "legacy ");  
  35.             error = dev->bus->suspend(dev, state);  
  36.             suspend_report_result(dev->bus->suspend, error);  
  37.         }  
  38.     }  
  39.  End:  
  40.     up(&dev->sem);  
  41.   
  42.     return error;  
  43. }  
save_snippets.png static int device_suspend(struct device *dev, pm_message_t state) { int error = 0; down(&dev->sem); if (dev->class) { // 类的suspend优先 if (dev->class->pm) { pm_dev_dbg(dev, state, "class "); error = pm_op(dev, dev->class->pm, state); } else if (dev->class->suspend) { pm_dev_dbg(dev, state, "legacy class "); error = dev->class->suspend(dev, state); suspend_report_result(dev->class->suspend, error); } if (error) goto End; } if (dev->type) { // device_type次之 if (dev->type->pm) { pm_dev_dbg(dev, state, "type "); error = pm_op(dev, dev->type->pm, state); } if (error) goto End; } if (dev->bus) { // bus优先级最低 if (dev->bus->pm) { pm_dev_dbg(dev, state, ""); error = pm_op(dev, dev->bus->pm, state); } else if (dev->bus->suspend) { pm_dev_dbg(dev, state, "legacy "); error = dev->bus->suspend(dev, state); suspend_report_result(dev->bus->suspend, error); } } End: up(&dev->sem); return error; }可以看到类中的suspend优先级最高,之后是device_type的,最后是bus的,大部分设备只注册了bus下的suspend。
4、挂起处理器
[cpp] view plain copy print?
  1. static int suspend_enter(suspend_state_t state)  
  2. {  
  3.     int error;  
  4.     // 处理器的休眠准备函数  
  5.     if (suspend_ops->prepare) {  
  6.         error = suspend_ops->prepare();  
  7.         if (error)  
  8.             return error;  
  9.     }  
  10.     // 执行非sysdev的late suspend函数  
  11.     error = dpm_suspend_noirq(PMSG_SUSPEND);  
  12.     if (error) {  
  13.         printk(KERN_ERR "PM: Some devices failed to power down ");  
  14.         goto Platfrom_finish;  
  15.     }  
  16.     // 处理器休眠最后的准备  
  17.     if (suspend_ops->prepare_late) {  
  18.         error = suspend_ops->prepare_late();  
  19.         if (error)  
  20.             goto Power_up_devices;  
  21.     }  
  22.   
  23.     if (suspend_test(TEST_PLATFORM))  
  24.         goto Platform_wake;  
  25.     // 关闭非启动cpu  
  26.     error = disable_nonboot_cpus();  
  27.     if (error || suspend_test(TEST_CPUS))  
  28.         goto Enable_cpus;  
  29.     // 挂起中断  
  30.     arch_suspend_disable_irqs();  
  31.     BUG_ON(!irqs_disabled());  
  32.     // 挂起sysdev  
  33.     error = sysdev_suspend(PMSG_SUSPEND);  
  34.     if (!error) {  
  35.         if (!suspend_test(TEST_CORE))  
  36.             // 处理器的休眠进入函数,休眠流程运行至此  
  37.             error = suspend_ops->enter(state);  
  38.         // 唤醒sysdev  
  39.         sysdev_resume();  
  40.     }  
  41.     // 使能中断  
  42.     arch_suspend_enable_irqs();  
  43.     BUG_ON(irqs_disabled());  
  44.   
  45.  Enable_cpus:  
  46.     // 使能非启动cpu  
  47.     enable_nonboot_cpus();  
  48.   
  49.  Platform_wake:  
  50.     // 处理器开始唤醒  
  51.     if (suspend_ops->wake)  
  52.         suspend_ops->wake();  
  53.   
  54.  Power_up_devices:  
  55.     // 执行非sysdev的early resume函数  
  56.     dpm_resume_noirq(PMSG_RESUME);  
  57.   
  58.  Platfrom_finish:  
  59.     // 处理器休眠结束  
  60.     if (suspend_ops->finish)  
  61.         suspend_ops->finish();  
  62.   
  63.     return error;  
  64. }  
save_snippets.png static int suspend_enter(suspend_state_t state) { int error; // 处理器的休眠准备函数 if (suspend_ops->prepare) { error = suspend_ops->prepare(); if (error) return error; } // 执行非sysdev的late suspend函数 error = dpm_suspend_noirq(PMSG_SUSPEND); if (error) { printk(KERN_ERR "PM: Some devices failed to power down "); goto Platfrom_finish; } // 处理器休眠最后的准备 if (suspend_ops->prepare_late) { error = suspend_ops->prepare_late(); if (error) goto Power_up_devices; } if (suspend_test(TEST_PLATFORM)) goto Platform_wake; // 关闭非启动cpu error = disable_nonboot_cpus(); if (error || suspend_test(TEST_CPUS)) goto Enable_cpus; // 挂起中断 arch_suspend_disable_irqs(); BUG_ON(!irqs_disabled()); // 挂起sysdev error = sysdev_suspend(PMSG_SUSPEND); if (!error) { if (!suspend_test(TEST_CORE)) // 处理器的休眠进入函数,休眠流程运行至此 error = suspend_ops->enter(state); // 唤醒sysdev sysdev_resume(); } // 使能中断 arch_suspend_enable_irqs(); BUG_ON(irqs_disabled()); Enable_cpus: // 使能非启动cpu enable_nonboot_cpus(); Platform_wake: // 处理器开始唤醒 if (suspend_ops->wake) suspend_ops->wake(); Power_up_devices: // 执行非sysdev的early resume函数 dpm_resume_noirq(PMSG_RESUME); Platfrom_finish: // 处理器休眠结束 if (suspend_ops->finish) suspend_ops->finish(); return error; } 在这个阶段首先看处理器是否需要做一些准备,接下来执行非sysdev的late suspend函数,然后处理器做休眠前最后的准备、关闭非启动cpu、挂起中断,再挂起sysdev,最后进入处理器的挂起函数,至此休眠流程结束,处理器等待用户或者RTC唤醒。 附1、late suspend 在这里我们看到了一种新的suspend机制 — late suspend,是在所有的suspend执行完后再开始执行,接口为dev->bus->pm->suspend_noirq;这样early_suspend、suspend以及late suspend构成了suspend的三部曲,late suspend是在中断关闭的情况下进行的;前面我们分析的wake_lock就有用到,用于检测在suspend阶段是否有锁被激活。late suspend的实现如下: [cpp] view plain copy print?
  1. int dpm_suspend_noirq(pm_message_t state)  
  2. {  
  3.     struct device *dev;  
  4.     int error = 0;  
  5.   
  6.     suspend_device_irqs();  // 关闭除唤醒系统以外的所有中断  
  7.     mutex_lock(&dpm_list_mtx);  
  8.     list_for_each_entry_reverse(dev, &dpm_list, power.entry) {  
  9.         // 执行所有设备的late suspend函数  
  10.         error = device_suspend_noirq(dev, state);  
  11.         if (error) {  
  12.             pm_dev_err(dev, state, " late", error);  
  13.             break;  
  14.         }  
  15.         dev->power.status = DPM_OFF_IRQ;  
  16.     }  
  17.     mutex_unlock(&dpm_list_mtx);  
  18.     if (error)  
  19.         dpm_resume_noirq(resume_event(state));  
  20.     return error;  
  21. }  
  22. EXPORT_SYMBOL_GPL(dpm_suspend_noirq);  
save_snippets.png int dpm_suspend_noirq(pm_message_t state) { struct device *dev; int error = 0; suspend_device_irqs(); // 关闭除唤醒系统以外的所有中断 mutex_lock(&dpm_list_mtx); list_for_each_entry_reverse(dev, &dpm_list, power.entry) { // 执行所有设备的late suspend函数 error = device_suspend_noirq(dev, state); if (error) { pm_dev_err(dev, state, " late", error); break; } dev->power.status = DPM_OFF_IRQ; } mutex_unlock(&dpm_list_mtx); if (error) dpm_resume_noirq(resume_event(state)); return error; } EXPORT_SYMBOL_GPL(dpm_suspend_noirq); 附2、中断关闭流程 在late suspend机制中我们看到了休眠流程中关闭系统中断的地方: [cpp] view plain copy print?
  1. void suspend_device_irqs(void)  
  2. {  
  3.     struct irq_desc *desc;  
  4.     int irq;  
  5.   
  6.     for_each_irq_desc(irq, desc) {  // 遍历系统的中断  
  7.         unsigned long flags;  
  8.   
  9.         spin_lock_irqsave(&desc->lock, flags);  
  10.         __disable_irq(desc, irq, true);  // 关闭中断  
  11.         spin_unlock_irqrestore(&desc->lock, flags);  
  12.     }  
  13.   
  14.     for_each_irq_desc(irq, desc)  
  15.         if (desc->status & IRQ_SUSPENDED)  
  16.             synchronize_irq(irq);  
  17. }  
  18. EXPORT_SYMBOL_GPL(suspend_device_irqs);  
save_snippets.png void suspend_device_irqs(void) { struct irq_desc *desc; int irq; for_each_irq_desc(irq, desc) { // 遍历系统的中断 unsigned long flags; spin_lock_irqsave(&desc->lock, flags); __disable_irq(desc, irq, true); // 关闭中断 spin_unlock_irqrestore(&desc->lock, flags); } for_each_irq_desc(irq, desc) if (desc->status & IRQ_SUSPENDED) synchronize_irq(irq); } EXPORT_SYMBOL_GPL(suspend_device_irqs);函数调用了__disable_irq()来关闭中断,我们看一下这个函数的实现: [cpp] view plain copy print?
  1. void __disable_irq(struct irq_desc *desc, unsigned int irq, bool suspend)  
  2. {  
  3.     if (suspend) {  
  4.         // 如果中断没有被激活或者中断的IRQF_TIMER标志被置位则不关闭中断  
  5.         // 在以后的内核版本中这个标志位被换成了IRQF_NO_SUSPEND  
  6.         // 新版的IRQF_TIMER = (__IRQF_TIMER | IRQF_NO_SUSPEND)  
  7.         if (!desc->action || (desc->action->flags & IRQF_TIMER))  
  8.             return;  
  9.         desc->status |= IRQ_SUSPENDED;  
  10.     }  
  11.     // 判断中断是否被打开  
  12.     if (!desc->depth++) {  
  13.         // 更新标志位  
  14.         desc->status |= IRQ_DISABLED;  
  15.         // 关闭中断  
  16.         desc->chip->disable(irq);  
  17.     }  
  18. }  
save_snippets.png void __disable_irq(struct irq_desc *desc, unsigned int irq, bool suspend) { if (suspend) { // 如果中断没有被激活或者中断的IRQF_TIMER标志被置位则不关闭中断 // 在以后的内核版本中这个标志位被换成了IRQF_NO_SUSPEND // 新版的IRQF_TIMER = (__IRQF_TIMER | IRQF_NO_SUSPEND) if (!desc->action || (desc->action->flags & IRQF_TIMER)) return; desc->status |= IRQ_SUSPENDED; } // 判断中断是否被打开 if (!desc->depth++) { // 更新标志位 desc->status |= IRQ_DISABLED; // 关闭中断 desc->chip->disable(irq); } }可以看到如果该中断没有被激活或者中断的IRQF_TIMER标志被置位就不会关闭中断,在新的内核版本中增加了专门的 IRQF_NO_SUSPEND 标志位,用来置位在休眠状态下唤醒系统的中断,如RTC、按键等;如果是其他中断则将打开的中断关闭掉。 附3、dpm_list链表 dpm_list是内核中用于设备电源管理的链表,设备注册时通过一系列的调用 device_register() -> device_add() -> device_pm_add() 最后在device_pm_add()中将设备加入dpm_list链表中:
[cpp] view plain copy print?
  1. // 设备创建时都会调用的函数,将设备加入dpm_list链表  
  2. void device_pm_add(struct device *dev)  
  3. {  
  4.     pr_debug("PM: Adding info for %s:%s ",  
  5.          dev->bus ? dev->bus->name : "No Bus",  
  6.          kobject_name(&dev->kobj));  
  7.     mutex_lock(&dpm_list_mtx);  
  8.     if (dev->parent) {  
  9.         if (dev->parent->power.status >= DPM_SUSPENDING)  
  10.             dev_warn(dev, "parent %s should not be sleeping ",  
  11.                  dev_name(dev->parent));  
  12.     } else if (transition_started) {  
  13.         /* 
  14.          * We refuse to register parentless devices while a PM 
  15.          * transition is in progress in order to avoid leaving them 
  16.          * unhandled down the road 
  17.          */  
  18.         dev_WARN(dev, "Parentless device registered during a PM transaction ");  
  19.     }  
  20.     // 将设备节点添加到链表尾部,即设备按注册的先后顺序从链表头部到尾部  
  21.     list_add_tail(&dev->power.entry, &dpm_list);  
  22.     mutex_unlock(&dpm_list_mtx);  
  23. }  
save_snippets.png // 设备创建时都会调用的函数,将设备加入dpm_list链表 void device_pm_add(struct device *dev) { pr_debug("PM: Adding info for %s:%s ", dev->bus ? dev->bus->name : "No Bus", kobject_name(&dev->kobj)); mutex_lock(&dpm_list_mtx); if (dev->parent) { if (dev->parent->power.status >= DPM_SUSPENDING) dev_warn(dev, "parent %s should not be sleeping ", dev_name(dev->parent)); } else if (transition_started) { /* * We refuse to register parentless devices while a PM * transition is in progress in order to avoid leaving them * unhandled down the road */ dev_WARN(dev, "Parentless device registered during a PM transaction "); } // 将设备节点添加到链表尾部,即设备按注册的先后顺序从链表头部到尾部 list_add_tail(&dev->power.entry, &dpm_list); mutex_unlock(&dpm_list_mtx); }而设备注销的时候会调用device_pm_remove()将设备从dpm_list链表中移除: [cpp] view plain copy print?
  1. // 设备注销时都会调用的函数,将设备从dpm_list链表中移除  
  2. void device_pm_remove(struct device *dev)  
  3. {  
  4.     pr_debug("PM: Removing info for %s:%s ",  
  5.          dev->bus ? dev->bus->name : "No Bus",  
  6.          kobject_name(&dev->kobj));  
  7.     mutex_lock(&dpm_list_mtx);  
  8.     list_del_init(&dev->power.entry);  
  9.     mutex_unlock(&dpm_list_mtx);  
  10.     pm_runtime_remove(dev);  
  11. }  
save_snippets.png // 设备注销时都会调用的函数,将设备从dpm_list链表中移除 void device_pm_remove(struct device *dev) { pr_debug("PM: Removing info for %s:%s ", dev->bus ? dev->bus->name : "No Bus", kobject_name(&dev->kobj)); mutex_lock(&dpm_list_mtx); list_del_init(&dev->power.entry); mutex_unlock(&dpm_list_mtx); pm_runtime_remove(dev); }