early_suspend【轉】

android 休眠喚醒機制分析(二) — early_suspend linux

early_suspend是Android休眠流程的第一階段即淺度休眠,不會受到wake_lock的阻止,通常用於關閉lcd、tp等設備爲運行的應用節約電能。Android的PowerManagerService會根據用戶的操做狀況調整電源狀態,若是須要休眠則會調用到HAL層的set_screen_state()接口,在set_screen_state()中會向/sys/power/state節點寫入"mem"值讓驅動層開始進入休眠流程。android

1、休眠喚醒機制及其用戶空間接口

Linux系統支持以下休眠喚醒等級緩存

const char *const pm_states[PM_SUSPEND_MAX] = {
#ifdef CONFIG_EARLYSUSPEND
	[PM_SUSPEND_ON]		= "on",
#endif
	[PM_SUSPEND_STANDBY]	= "standby",
	[PM_SUSPEND_MEM]	= "mem",
};

但在Android中通常只支持"on"和"mem",其中"on"爲喚醒設備,"mem"爲休眠設備。/sys/power/state節點的讀寫操做以下:函數

static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr,
			  char *buf)
{
	char *s = buf;
#ifdef CONFIG_SUSPEND
	int i;
 
	for (i = 0; i < PM_SUSPEND_MAX; i++) {
		if (pm_states[i] && valid_state(i))
			s += sprintf(s,"%s ", pm_states[i]);  // 打印系統支持的休眠等級
	}
#endif
#ifdef CONFIG_HIBERNATION
	s += sprintf(s, "%s\n", "disk");
#else
	if (s != buf)
		/* convert the last space to a newline */
		*(s-1) = '\n';
#endif
	return (s - buf);
}
 
static ssize_t state_store(struct kobject *kobj, struct kobj_attribute *attr,
			   const char *buf, size_t n)
{
#ifdef CONFIG_SUSPEND
#ifdef CONFIG_EARLYSUSPEND
	suspend_state_t state = PM_SUSPEND_ON;
#else
	suspend_state_t state = PM_SUSPEND_STANDBY;
#endif
	const char * const *s;
#endif
	char *p;
	int len;
	int error = -EINVAL;
 
	p = memchr(buf, '\n', n);
	len = p ? p - buf : n;
 
	/* First, check if we are requested to hibernate */
	if (len == 4 && !strncmp(buf, "disk", len)) {
		error = hibernate();
  goto Exit;
	}
 
#ifdef CONFIG_SUSPEND
	for (s = &pm_states[state]; state < PM_SUSPEND_MAX; s++, state++) {
		if (*s && len == strlen(*s) && !strncmp(buf, *s, len))
			break;
	}
	if (state < PM_SUSPEND_MAX && *s)
#ifdef CONFIG_EARLYSUSPEND
		if (state == PM_SUSPEND_ON || valid_state(state)) {
			error = 0;
			request_suspend_state(state);  // 請求進入android的休眠流程
		}
#else
		error = enter_state(state);  // linux的標準休眠流程
#endif
#endif
 
 Exit:
	return error ? error : n;
}
 
power_attr(state);

其中state_show()爲節點的讀函數,主要打印出系統支持的休眠等級;state_store()爲節點的寫函數,根據參數請求休眠或者喚醒流程。節點的建立代碼以下:url

static struct attribute * g[] = {
	&state_attr.attr,        // state節點
#ifdef CONFIG_PM_TRACE
	&pm_trace_attr.attr,
#endif
#if defined(CONFIG_PM_SLEEP) && defined(CONFIG_PM_DEBUG)
	&pm_test_attr.attr,      // pm_test節點
#endif
#ifdef CONFIG_USER_WAKELOCK
	&wake_lock_attr.attr,    // wake_lock節點
	&wake_unlock_attr.attr,  // wake_unlock節點
#endif
	NULL,
};
 
static struct attribute_group attr_group = {
	.attrs = g,
};
 
static int __init pm_init(void)
{
	int error = pm_start_workqueue();
	if (error)
		return error;
	power_kobj = kobject_create_and_add("power", NULL);  // 建立power節點
	if (!power_kobj)
		return -ENOMEM;
	return sysfs_create_group(power_kobj, &attr_group);  // 建立一組屬性節點
}
 
core_initcall(pm_init);

2、early_suspend 實現

一、early_suspend 定義、接口及其用法

enum {
	EARLY_SUSPEND_LEVEL_BLANK_SCREEN = 50,
	EARLY_SUSPEND_LEVEL_STOP_DRAWING = 100,
	EARLY_SUSPEND_LEVEL_DISABLE_FB = 150,
};
struct early_suspend {
#ifdef CONFIG_HAS_EARLYSUSPEND
	struct list_head link;  // 鏈表節點
	int level;              // 優先等級
	void (*suspend)(struct early_suspend *h);
	void (*resume)(struct early_suspend *h);
#endif
};

能夠看到early_suspend由兩個函數指針、鏈表節點、優先等級組成;內核默認定義了3個優先等級,在suspend的時候先執行優先等級低的handler,在resume的時候則先執行等級高的handler,用戶能夠定義本身的優先等級;early_suspend向內核空間提供了2個接口用於註冊和註銷handler:spa

void register_early_suspend(struct early_suspend *handler);
void unregister_early_suspend(struct early_suspend *handler);

其中register_early_suspend()用於註冊,unregister_early_suspend用於註銷;通常early_suspend的使用方式以下:.net

ts->earlysuspend.suspend = sitronix_i2c_suspend_early;
ts->earlysuspend.resume = sitronix_i2c_resume_late;
ts->earlysuspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN;
register_early_suspend(&ts->earlysuspend);

設置好suspendresume接口,定義優先等級,而後註冊結構便可。hibernate

二、初始化信息

咱們看一下early_suspend須要用到的一些數據:線程

static DEFINE_MUTEX(early_suspend_lock);
static LIST_HEAD(early_suspend_handlers);  // 初始化淺度休眠鏈表
// 聲明3個工做隊列用於同步、淺度休眠和喚醒
static void early_sys_sync(struct work_struct *work);
static void early_suspend(struct work_struct *work);
static void late_resume(struct work_struct *work);
static DECLARE_WORK(early_sys_sync_work,early_sys_sync);
static DECLARE_WORK(early_suspend_work, early_suspend);
static DECLARE_WORK(late_resume_work, late_resume);
static DEFINE_SPINLOCK(state_lock);
enum {
	SUSPEND_REQUESTED = 0x1,  // 當前正在請求淺度休眠
	SUSPENDED = 0x2,          // 淺度休眠完成
	SUSPEND_REQUESTED_AND_SUSPENDED = SUSPEND_REQUESTED | SUSPENDED,
};
static int state;

初始化了一個鏈表early_suspend_handlers用於管理early_suspend,還定義讀寫鏈表用到的互斥體;另外還聲明瞭3個工做隊列,分別用於緩存同步、淺度休眠和喚醒;還聲明瞭early_suspend操做的3個狀態。debug

三、register_early_suspend 和 unregister_early_suspend

void register_early_suspend(struct early_suspend *handler)
{
	struct list_head *pos;
 
	mutex_lock(&early_suspend_lock);
	// 遍歷淺度休眠鏈表
	list_for_each(pos, &early_suspend_handlers) {
		struct early_suspend *e;
		e = list_entry(pos, struct early_suspend, link);
		// 判斷當前節點的優先等級是否大於handler的優先等級
		// 以此決定handler在鏈表中的順序
		if (e->level > handler->level)
			break;
	}
	// 將handler加入當前節點以前,優先等級越低越靠前
	list_add_tail(&handler->link, pos);
	if ((state & SUSPENDED) && handler->suspend)
		handler->suspend(handler);
	mutex_unlock(&early_suspend_lock);
}
EXPORT_SYMBOL(register_early_suspend);

註冊的流程比較簡單,首先遍歷鏈表,依次比較每一個節點的優先等級,若是遇到優先等級比新節點優先等級高則跳出,而後將新節點加入優先等級較高的節點前面,這樣就確保了鏈表是優先等級低在前高在後的順序;在將節點加入鏈表後查看當前狀態是否爲淺度休眠完成狀態,若是是則執行handler的suspend函數。

四、request_suspend_state

前面咱們看到用戶空間在寫/sys/power/state節點的時候會執行request_suspend_state()函數,該函數代碼以下:

void request_suspend_state(suspend_state_t new_state)
{
	unsigned long irqflags;
	int old_sleep;
 
	spin_lock_irqsave(&state_lock, irqflags);
	old_sleep = state & SUSPEND_REQUESTED;
	// 打印當前狀態
	if (debug_mask & DEBUG_USER_STATE) {
		struct timespec ts;
		struct rtc_time tm;
		getnstimeofday(&ts);
		rtc_time_to_tm(ts.tv_sec, &tm);
		pr_info("request_suspend_state: %s (%d->%d) at %lld "
			"(%d-%02d-%02d %02d:%02d:%02d.%09lu UTC)\n",
			new_state != PM_SUSPEND_ON ? "sleep" : "wakeup",
			requested_suspend_state, new_state,
			ktime_to_ns(ktime_get()),
			tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
			tm.tm_hour, tm.tm_min, tm.tm_sec, ts.tv_nsec);
	}
	// 若是新狀態是休眠狀態
	if (!old_sleep && new_state != PM_SUSPEND_ON) {
		state |= SUSPEND_REQUESTED;
		pr_info("sys_sync_work_queue early_sys_sync_work.\n");
		// 執行緩存同步與淺度休眠的工做隊列
		queue_work(sys_sync_work_queue, &early_sys_sync_work);
		queue_work(suspend_work_queue, &early_suspend_work);
	} else if (old_sleep && new_state == PM_SUSPEND_ON) {
	// 若是新狀態是喚醒狀態
		state &= ~SUSPEND_REQUESTED;
		// 激活內核鎖
		wake_lock(&main_wake_lock);
		// 執行淺度喚醒的工做隊列
		queue_work(suspend_work_queue, &late_resume_work);
	}
	// 更新全局狀態
	requested_suspend_state = new_state;
	spin_unlock_irqrestore(&state_lock, irqflags);
}

函數首先打印出當前狀態變化的log,而後判斷新狀態,若是是休眠狀態則置位SUSPEND_REQUESTED標誌,而後將同步緩存、淺度休眠工做隊列加入相應的內核線程執行;若是新狀態是喚醒則首先將main_wake_lock激活,而後再將淺度喚醒工做隊列加入內核線程執行;最後更新全局狀態變量,由於提供了一個內核空間接口用於獲取當前休眠喚醒狀態:

// 返回系統狀態值
suspend_state_t get_suspend_state(void)
{
	return requested_suspend_state;
}

五、early_suspend_work、late_resume_work 和 early_sys_sync

static void early_suspend(struct work_struct *work)
{
	struct early_suspend *pos;
	unsigned long irqflags;
	int abort = 0;
 
	mutex_lock(&early_suspend_lock);
	spin_lock_irqsave(&state_lock, irqflags);
	if (state == SUSPEND_REQUESTED)  // 判斷當前狀態是否在請求淺度休眠
		state |= SUSPENDED;      // 若是是則置位SUSPENDED
	else
		abort = 1;
	spin_unlock_irqrestore(&state_lock, irqflags);
 
	if (abort) {  // 取消early_suspend
		if (debug_mask & DEBUG_SUSPEND)
			pr_info("early_suspend: abort, state %d\n", state);
		mutex_unlock(&early_suspend_lock);
		goto abort;
	}
 
	if (debug_mask & DEBUG_SUSPEND)
		pr_info("early_suspend: call handlers\n");
	// 遍歷淺度休眠鏈表並執行其中全部suspend函數
	// 執行順序根據優先等級而定,等級越低越先執行
	list_for_each_entry(pos, &early_suspend_handlers, link) {
		if (pos->suspend != NULL)
			pos->suspend(pos);
	}
	mutex_unlock(&early_suspend_lock);
 
	if (debug_mask & DEBUG_SUSPEND)
		pr_info("early_suspend: sync\n");
 
	/* Remove sys_sync from early_suspend, and use work queue to complete sys_sync */
	//sys_sync();
abort:
	spin_lock_irqsave(&state_lock, irqflags);
	if (state == SUSPEND_REQUESTED_AND_SUSPENDED)
		wake_unlock(&main_wake_lock);
	spin_unlock_irqrestore(&state_lock, irqflags);
}

在suspend流程中首先判斷當前狀態是否爲SUSPEND_REQUESTED,若是是則置位SUSPENDED標誌,若是不是則取消suspend流程;而後遍歷淺度休眠鏈表,從鏈表頭部到尾部依次調用各節點的suspend()函數,執行完後判斷當前狀態是否爲SUSPEND_REQUESTED_AND_SUSPENDED,若是是則釋放main_wake_lock,當前系統中若是隻存在main_wake_lock這個有效鎖,則會在wake_unlock()裏面啓動深度休眠線程,若是還有其餘其餘wake_lock則保持當前狀態。

static void late_resume(struct work_struct *work)
{
	struct early_suspend *pos;
	unsigned long irqflags;
	int abort = 0;
 
	mutex_lock(&early_suspend_lock);
	spin_lock_irqsave(&state_lock, irqflags);
	if (state == SUSPENDED)  // 清除淺度休眠完成標誌
		state &= ~SUSPENDED;
	else
		abort = 1;
	spin_unlock_irqrestore(&state_lock, irqflags);
 
	if (abort) {
		if (debug_mask & DEBUG_SUSPEND)
			pr_info("late_resume: abort, state %d\n", state);
		goto abort;
	}
	if (debug_mask & DEBUG_SUSPEND)
		pr_info("late_resume: call handlers\n");
	// 反向遍歷淺度休眠鏈表並執行其中全部resume函數
	// 執行順序根據優先等級而定,等級越高越先執行
	list_for_each_entry_reverse(pos, &early_suspend_handlers, link)
		if (pos->resume != NULL)
			pos->resume(pos);
	if (debug_mask & DEBUG_SUSPEND)
		pr_info("late_resume: done\n");
abort:
	mutex_unlock(&early_suspend_lock);
}

在resume流程中一樣首先判斷當前狀態是否爲SUSPENDED,若是是則清除SUSPENDED標誌,而後反向遍歷淺度休眠鏈表,按照優先等級從高到低的順序執行節點的resume()函數。

static void early_sys_sync(struct work_struct *work)
{
	wake_lock(&sys_sync_wake_lock);
	sys_sync();
	wake_unlock(&sys_sync_wake_lock);
}

內核專門爲緩存同步創建了一個線程,同時還建立了sys_sync_wake_lock防止在同步緩存時系統進入深度休眠。

相關文章
相關標籤/搜索