/** Function to handle a GPU state change for the demand power policy
 *
 * This function is called whenever the GPU has transitioned to another state. It first checks that the transition is 
 * complete and then moves the state machine to the next state.
 */
static void demand_state_changed(kbase_device *kbdev)
{
	kbasep_pm_policy_demand *data = &kbdev->pm.policy_data.demand;

	switch(data->state) {
		case KBASEP_PM_DEMAND_STATE_CHANGING_POLICY:
		case KBASEP_PM_DEMAND_STATE_POWERING_UP:
		case KBASEP_PM_DEMAND_STATE_POWERING_DOWN:
			if (kbase_pm_get_pwr_active(kbdev)) {
				/* Cores are still transitioning - ignore the event */
				return;
			}
			break;
		default:
			/* Must not call kbase_pm_get_pwr_active here as the clock may be turned off */
			break;
	}

	switch(data->state)
	{
		case KBASEP_PM_DEMAND_STATE_CHANGING_POLICY:
			/* Signal power events before switching the policy */
			kbase_pm_power_up_done(kbdev);
			kbase_pm_power_down_done(kbdev);
			kbase_pm_change_policy(kbdev);
			break;
		case KBASEP_PM_DEMAND_STATE_POWERING_UP:
			data->state = KBASEP_PM_DEMAND_STATE_POWERED_UP;
			kbase_pm_power_up_done(kbdev);
			/* State changed, try to run jobs */
			kbase_js_try_run_jobs(kbdev);
			break;
		case KBASEP_PM_DEMAND_STATE_POWERING_DOWN:
			data->state = KBASEP_PM_DEMAND_STATE_POWERED_DOWN;
			/* Disable interrupts and turn the clock off */
			kbase_pm_disable_interrupts(kbdev);
			kbase_pm_clock_off(kbdev);
			kbase_pm_power_down_done(kbdev);
			break;
		case KBASEP_PM_DEMAND_STATE_POWERED_UP:
			/* Core states may have been changed, try to run jobs */
			kbase_js_try_run_jobs(kbdev);
			break;
		default:
			break;
	}
}
/** Function to handle a GPU state change for the always_on power policy.
 *
 * This function is called whenever the GPU has transitioned to another state. It first checks that the transition is 
 * complete and then moves the state machine to the next state.
 *
 * @param kbdev     The kbase device structure for the device
 */
static void always_on_state_changed(kbase_device *kbdev)
{
	kbasep_pm_policy_always_on *data = &kbdev->pm.policy_data.always_on;

	switch(data->state)
	{
	case KBASEP_PM_ALWAYS_ON_STATE_POWERING_UP:
		if (kbase_pm_get_pwr_active(kbdev))
		{
			/* Cores still transitioning */
			return;
		}
		/* All cores have transitioned, inform the OS */
		kbase_pm_power_up_done(kbdev);
		data->state = KBASEP_PM_ALWAYS_ON_STATE_POWERED_UP;

		break;
	case KBASEP_PM_ALWAYS_ON_STATE_POWERING_DOWN:
		if (kbase_pm_get_pwr_active(kbdev))
		{
			/* Cores still transitioning */
			return;
		}
		/* All cores have transitioned, turn the clock and interrupts off */
		kbase_pm_disable_interrupts(kbdev);
		kbase_pm_clock_off(kbdev);

		/* Inform the OS */
		kbase_pm_power_down_done(kbdev);

		data->state = KBASEP_PM_ALWAYS_ON_STATE_POWERED_DOWN;

		break;
	case KBASEP_PM_ALWAYS_ON_STATE_CHANGING_POLICY:
		if (kbase_pm_get_pwr_active(kbdev))
		{
			/* Cores still transitioning */
			return;
		}
		/* All cores have transitioned, inform the system we can change policy*/
		kbase_pm_change_policy(kbdev);

		break;
	default:
		break;
	}
}
/** Function to handle a GPU state change for the coarse_demand power policy.
 *
 * This function is called whenever the GPU has transitioned to another state. It first checks that the transition is
 * complete and then moves the state machine to the next state.
 *
 * @param kbdev     The kbase device structure for the device
 */
static void coarse_demand_state_changed(kbase_device *kbdev)
{
	kbasep_pm_policy_coarse_demand *data = &kbdev->pm.policy_data.coarse_demand;

	/* No need to early-out if cores transitioning during the POWERING_UP state */
	if (data->state != KBASEP_PM_COARSE_DEMAND_STATE_POWERING_UP
		&& kbase_pm_get_pwr_active(kbdev)) {
		/* Cores are still transitioning - ignore the event */
		return;
	}

	switch(data->state)
	{
	case KBASEP_PM_COARSE_DEMAND_STATE_POWERING_UP:
		/* All cores are ready, inform the OS */
		data->state = KBASEP_PM_COARSE_DEMAND_STATE_POWERED_UP;
		kbase_pm_power_up_done(kbdev);
		/*
		 * No need to submit jobs:
		 * - All cores will be powered on
		 * - Cores are made available even while they're transitioning to 'powered on'
		 * - We signal power_up_done after calling kbase_pm_check_transitions(), which makes the cores available.
		 * Hence, the submission of jobs will already be handled by the call-path that invoked kbase_pm_context_active()
		 */

		break;
	case KBASEP_PM_COARSE_DEMAND_STATE_POWERING_DOWN:
		data->state = KBASEP_PM_COARSE_DEMAND_STATE_POWERED_DOWN;
		/* All cores have transitioned, turn the clock and interrupts off */
		kbase_pm_clock_off(kbdev);

		/* Inform the OS */
		kbase_pm_power_down_done(kbdev);

		break;
	case KBASEP_PM_COARSE_DEMAND_STATE_CHANGING_POLICY:
		/* Signal power events before switching the policy */
		kbase_pm_power_up_done(kbdev);
		kbase_pm_power_down_done(kbdev);
		kbase_pm_change_policy(kbdev);

		break;
	default:
		break;
	}
}
void kbase_pm_do_poweroff(kbase_device *kbdev)
{
	unsigned long flags;
	mali_bool cores_are_available;

	lockdep_assert_held(&kbdev->pm.lock);

	spin_lock_irqsave(&kbdev->pm.power_change_lock, flags);

	/* Force all cores off */
	kbdev->pm.desired_shader_state = 0;

	/* Force all cores to be unavailable, in the situation where 
	 * transitions are in progress for some cores but not others,
	 * and kbase_pm_check_transitions_nolock can not immediately
	 * power off the cores */
	kbdev->shader_available_bitmap = 0;
	kbdev->tiler_available_bitmap = 0;
	kbdev->l2_available_bitmap = 0;

	KBASE_TIMELINE_PM_CHECKTRANS(kbdev, SW_FLOW_PM_CHECKTRANS_PM_DO_POWEROFF_START);
	cores_are_available = kbase_pm_check_transitions_nolock(kbdev);
	KBASE_TIMELINE_PM_CHECKTRANS(kbdev, SW_FLOW_PM_CHECKTRANS_PM_DO_POWEROFF_END);
	/* Don't need 'cores_are_available', because we don't return anything */
	CSTD_UNUSED(cores_are_available);

	spin_unlock_irqrestore(&kbdev->pm.power_change_lock, flags);

	/* NOTE: We won't wait to reach the core's desired state, even if we're
	 * powering off the GPU itself too. It's safe to cut the power whilst
	 * they're transitioning to off, because the cores should be idle and all
	 * cache flushes should already have occurred */

	/* Consume any change-state events */
	kbase_timeline_pm_check_handle_event(kbdev, KBASE_TIMELINE_PM_EVENT_GPU_STATE_CHANGED);
	/* Disable interrupts and turn the clock off */
	kbase_pm_clock_off(kbdev);
}