/*
 * 设置profile
 * success: return BSP_OK
 * fail:    return BSP_ERROR
 */
int pwrctrl_dfs_set_profile(int profile)
{
	struct cpufreq_msg set_msg = {0,0,0,0};
	if ((profile < BALONG_FREQ_MIN) || (profile > BALONG_FREQ_MAX))
	{
		cpufreq_err("profile in right bound??%d\n", profile);
		return BSP_ERROR;
	}
	set_msg.msg_type = CPUFREQ_ADJUST_FREQ;
	set_msg.source = CPUFREQ_ACORE;
	if (pwrctrl_dfs_get_profile() < profile)
	{
		set_msg.content = DFS_PROFILE_UP_TARGET;
	}
	else if (pwrctrl_dfs_get_profile() > profile)
	{
		set_msg.content = DFS_PROFILE_DOWN_TARGET;
	}
	else
	{
		return BSP_OK;
	}
	set_msg.profile = (u32)profile;
	return balong_cpufreq_icc_send(&set_msg);
}
static void  pwrctrl_dfs_mgrmsg_task(void)
{
    int cur_profile = 0;
    unsigned int flowctrl_cpuload = 0;
    PWRCTRLFUNCPTR pRoutine = NULL;
    struct cpufreq_msg task_msg = {0,0,0,0};
	g_stDfsCpuControl.ulStartTime = bsp_get_slice_value();
	/* coverity[INFINITE_LOOP] */
	/* coverity[no_escape] */
    for (;;)
    {
		if (NULL != g_sem_calccpu_flag)
		{	
			semTake(g_sem_calccpu_flag, DFS_WAIT_FOREVER);
			 /*调用ttf回调函数*/
		    if ((NULL != FlowCtrlCallBack) && (g_flowctrl_in_interr_times >= 200))
		    {
		    	 flowctrl_cpuload = cpufreq_calccpu_cpuload();
		        pRoutine = FlowCtrlCallBack;
		        (void)(*pRoutine)(flowctrl_cpuload);
		    }
		}
		else
		{
			taskDelay((int)g_stDfsCpuConfigInfo.ulTimerLen);
			g_calccpu_load_result = cpufreq_calccpu_result(&g_next_freq);
		}
		if (!g_cpufreq_lock_status_flag)
		{
			continue;
		}
		
		cur_profile = pwrctrl_dfs_get_profile();
		cpufreq_assistant_regulate_ddr(cur_profile);
		if (DFS_PROFILE_NOCHANGE != g_calccpu_load_result)
		{
			if (g_icc_run_flag == 1)
			{
				task_msg.msg_type = CPUFREQ_ADJUST_FREQ;
				task_msg.source = CPUFREQ_CCORE;
				task_msg.content = g_calccpu_load_result;
				if (DFS_PROFILE_UP_TARGET == g_calccpu_load_result)
				{
					cur_profile = DC_RESV;
				}
				else if ((u32)cur_profile == CPUFREQ_MIN_PROFILE_LIMIT)
				{
					continue;
				}
				task_msg.profile = (unsigned int)cur_profile - 1;
				balong_cpufreq_icc_send(&task_msg);
			}
			else if (g_icc_run_flag == 2)
			{
				cpufreq_excute_result_cpu(g_calccpu_load_result, g_next_freq);
			}
        }

    }
}
void test_send_msg(unsigned int msg_type, unsigned int source, unsigned int content, unsigned int profile)
{
	struct cpufreq_msg task_msg = {0,0,0,0};
	task_msg.msg_type = msg_type;
	task_msg.source = source;
	task_msg.content = content;
	task_msg.profile = profile;
	balong_cpufreq_icc_send(&task_msg);
}
/*
 * 锁定调频 DFS_PROFILE_LOCKFREQ=0锁定;DFS_PROFILE_LOCKFREQ=1解锁
 */
void pwrctrl_dfs_lock(u32 lock)
{
	struct cpufreq_msg set_msg = {0,0,0,0};
	cpufreq_debug("cpufreq lock status is: %d\n", g_cpufreq_lock_status_flag);
	g_cpufreq_lock_status_flag = (s32)lock;

	set_msg.content = lock;
	
	set_msg.msg_type = CPUFREQ_LOCK_MCORE_ACTION;
	set_msg.source = CPUFREQ_ACORE;
	balong_cpufreq_icc_send(&set_msg);
}
/*
 * 设置profile下限
 * success: return BSP_OK
 * fail:    return BSP_ERROR
 */
int pwrctrl_dfs_set_baseprofile(int baseprofile)
{
	struct cpufreq_msg set_msg = {0,0,0,0};
	if ((baseprofile < BALONG_FREQ_MIN) || (baseprofile > BALONG_FREQ_MAX))
	{
		cpufreq_err("profile in right bound??%d\n", baseprofile);
		return BSP_ERROR;
	}
	set_msg.msg_type = CPUFREQ_ADJUST_FREQ;
	set_msg.source = CPUFREQ_ACORE;
	set_msg.content = DFS_PROFILE_DOWN_LIMIT;
	set_msg.profile = (u32)baseprofile;
	return balong_cpufreq_icc_send(&set_msg);
}
/*
 * Here we notify other drivers of the proposed change and the final change.
 *
 *
 *根据relation做相应动作
 */
static s32 balong_cpufreq_target(struct cpufreq_policy *policy,
				     u32 target_freq,
				     u32 relation)
{
	u32 result = 2;
	u32 new_index = 0;
	int cur_profile = 0;
	struct cpufreq_msg task_msg = {0,0,0,0};
	
	
	cpufreq_frequency_table_target(policy, balong_clockrate_table,
					   target_freq, relation, &new_index);
					   
	cpufreq_debug("target_freq %d new_index%d\n", target_freq, new_index);

	cur_profile = pwrctrl_dfs_get_profile();
	
	if ((CPUFREQ_RELATION_H == relation) && (BALONG_FREQ_MAX != cur_profile))
	{
		result = DFS_PROFILE_UP_TARGET;
	}
	else if ((CPUFREQ_RELATION_L == relation) && (BALONG_FREQ_MIN != cur_profile))
	{
		result = DFS_PROFILE_DOWN_TARGET;
	}
	else
	{
		policy->cur = balong_clockrate_table[cur_profile].frequency;
		g_cur_freq = policy->cur;
		cpufreq_info("set target relation %d, cur pro %d\n", relation, policy->cur);
		return BSP_ERROR;
	}
	
	cpufreq_frequency_target_profile(balong_clockrate_table[new_index].frequency, relation, &new_index);

	task_msg.msg_type = CPUFREQ_ADJUST_FREQ;
	task_msg.source = CPUFREQ_ACORE;
	task_msg.content = result;
	task_msg.profile = new_index;
	balong_cpufreq_icc_send(&task_msg);
	
	policy->cur = balong_clockrate_table[cur_profile].frequency;
	g_cur_freq = policy->cur;
	return BSP_OK;
}
/*
 * 该接口负责cpu负载检测,
 * 并根据预设阈值判决是否需要向M3请求调频
 */
void cpufreq_update_frequency(void)
{
    u32 cpuload = 0;
    int cur_profile = 0;
    struct cpufreq_msg task_msg = {CPUFREQ_ADJUST_FREQ, CPUFREQ_ACORE, 0, BALONG_FREQ_MAX};
    cpuload = (u32)cpufreq_calccpu_cpuload();
    cur_profile = (int)pwrctrl_dfs_get_profile();
    if (cpuload > dbs_tuners_ins.up_threshold)
    {
        task_msg.content = DFS_PROFILE_UP_TARGET;
    }
    else if (cpuload < dbs_tuners_ins.down_threshold)
    {
        task_msg.profile = (cur_profile != BALONG_FREQ_MIN) ? (cur_profile - 1) : (BALONG_FREQ_MIN);
        task_msg.content = DFS_PROFILE_DOWN;
    }
    else
    {
        return;
    }
    balong_cpufreq_icc_send(&task_msg);
}
/*
 * 该接口负责cpu负载检测,
 * 并根据预设阈值判决是否需要向M3请求调频
 */
void cpufreq_update_frequency(void)
{
	u32 cpuload = 0;
	int cur_profile = 0;
	struct cpufreq_msg task_msg = {CPUFREQ_ADJUST_FREQ, CPUFREQ_CCORE, 0, BALONG_FREQ_MAX};
	cpuload = cpufreq_calccpu_cpuload();
	cur_profile = pwrctrl_dfs_get_profile();
	if (cpuload > g_stDfsCpuConfigInfo.astThresHold[0].usProfileUpLimit)
   {
		task_msg.content = DFS_PROFILE_UP_TARGET;
	}
    else if (cpuload < g_stDfsCpuConfigInfo.astThresHold[0].usProfileDownLimit)
    {
		task_msg.profile = (cur_profile != BALONG_FREQ_MIN) ? (cur_profile - 1) : (BALONG_FREQ_MIN);
    	task_msg.content = DFS_PROFILE_DOWN;
    }
    else
    {
		return;
    }
    balong_cpufreq_icc_send(&task_msg);
}
/*find a freq in all table*/
static int cpufreq_frequency_table_target(struct cpufreq_frequency_table *table,
				   unsigned int target_freq,
				   unsigned int relation,
				   unsigned int *index)
{/*lint !e578 */
	struct cpufreq_frequency_table optimal = {
		.index = ~0,
		.frequency = 0,
	};
	struct cpufreq_frequency_table suboptimal = {
		.index = ~0,
		.frequency = 0,
	};
	unsigned int i;
	/*lint --e{744} */
	switch (relation) {
	case DFS_PROFILE_UP:
		suboptimal.frequency = ~0;
		break;
	case DFS_PROFILE_DOWN:
		optimal.frequency = ~0;
		break;
	}

	for (i = 0; (table[i].frequency != (u32)CPUFREQ_TABLE_END); i++) {
		unsigned int freq = table[i].frequency;
		if (freq == (u32)CPUFREQ_ENTRY_INVALID)
			continue;
		if ((freq < balong_query_profile_table[BALONG_FREQ_MIN].cpu_frequency) || (freq > balong_query_profile_table[BALONG_FREQ_MAX].cpu_frequency))
			continue;
		switch (relation) {
		case DFS_PROFILE_UP:
			if (freq <= target_freq) {
				if (freq >= optimal.frequency) {
					optimal.frequency = freq;
					optimal.index = i;
				}
			} else {
				if (freq <= suboptimal.frequency) {
					suboptimal.frequency = freq;
					suboptimal.index = i;
				}
			}
			break;
		case DFS_PROFILE_DOWN:
			if (freq >= target_freq) {
				if (freq <= optimal.frequency) {
					optimal.frequency = freq;
					optimal.index = i;
				}
			} else {
				if (freq >= suboptimal.frequency) {
					suboptimal.frequency = freq;
					suboptimal.index = i;
				}
			}
			break;
		}
	}
	if (optimal.index > i) {
		if (suboptimal.index > i)
			return BSP_ERROR;
		*index = suboptimal.index;
	} else
		*index = optimal.index;

	return BSP_OK;
}
/*****************************************************************************
Function:   PWRCTRL_DfsMgrExcuteVoteResultCpu
Description:Handle the Profile Vote Result
Input:      enResult:   The Vote Value
Output:     None
Return:     None
Others:
*****************************************************************************/
static int  cpufreq_excute_result_cpu(u32 relation, u32 target_freq)
{
	u32 result = 2;
	u32 new_index = 0;
	int cur_profile = 0;
	struct cpufreq_msg task_msg = {0,0,0,0};

	cpufreq_frequency_table_target(balong_clockrate_table,
					   target_freq, relation, &new_index);

	cpufreq_debug("target_freq %d new_index%d\n", target_freq, new_index);

	cur_profile = pwrctrl_dfs_get_profile();
	if ((DFS_PROFILE_UP == relation) && (BALONG_FREQ_MAX != cur_profile))
	{
		result = DFS_PROFILE_UP_TARGET;
	}
	else if ((DFS_PROFILE_DOWN == relation) && (BALONG_FREQ_MIN != cur_profile))
	{
		result = DFS_PROFILE_DOWN_TARGET;
	}
	else
	{
		cpufreq_err("set target relation %d, cur pro %d\n", relation, cur_profile);
		return BSP_ERROR;
	}

	cpufreq_frequency_target_profile(balong_clockrate_table[new_index].frequency, relation, &new_index);

	task_msg.msg_type = CPUFREQ_ADJUST_FREQ;
	task_msg.source = CPUFREQ_CCORE;
	task_msg.content = result;
	task_msg.profile = new_index;
	balong_cpufreq_icc_send(&task_msg);
	g_stDfsCpuControl.enCurProfile = (u32)cur_profile;
	return BSP_OK;

}

unsigned int cpufreq_calccpu_cpuload(void)
{
	u32 end_time = 0;
	u32 idle_time = 0;
	u32 wall_time = 0;
	u32 cpu_load = 0;
	unsigned long irqlock = 0;
	local_irq_save(irqlock);
	end_time = bsp_get_slice_value();
	wall_time = get_timer_slice_delta(g_cpufreq_start_time, end_time);
	idle_time = g_ulDfsCcpuIdleTime_long;
	g_cpufreq_start_time = end_time;
	g_ulDfsCcpuIdleTime_long = 0;
	g_flowctrl_in_interr_times = 0;
	cpu_load = (wall_time == 0) ? (0) : (((wall_time - idle_time) * 100) / wall_time);
	local_irq_restore(irqlock);
	if (cpu_load > 100)
	{
		cpu_load = g_ulCCpuload;
		cpufreq_info("cpuload: %d, wall:%d, idle:%d\n", cpu_load, wall_time, idle_time);
	}
	return cpu_load;
}