msm: kgsl: Fix a dead loop issue while changing gpu frequency

There is a dead loop in kgsl driver function, while changing governor
frequency. In governor 'userspace' mode, If the requested frequency
(set_freq) is more than the configured frequency and max_freq is also
set to more than the configured frequency, then there is a dead loop
while changing target frequency.

Dead loop occurs due to comparing signed integer 'i' with unsigned integer
'max_pwrlevel'. Decrementing 'i' beyond zero will cause dead loop.
So, change the array index 'i' of pwrlevels[] to unsigned int, which can't
be decremented beyond zero. Also, changing loop condition to restrict 'i'
within permitted levels.

Change-Id: Ic82b7477d50d1abcd348b011f64246066887922c
Signed-off-by: Venkateswara Rao Tadikonda <vtadik@codeaurora.org>
diff --git a/drivers/gpu/msm/kgsl_pwrscale.c b/drivers/gpu/msm/kgsl_pwrscale.c
index 7636a42..6fb81ee 100644
--- a/drivers/gpu/msm/kgsl_pwrscale.c
+++ b/drivers/gpu/msm/kgsl_pwrscale.c
@@ -530,7 +530,8 @@
 	struct kgsl_device *device = dev_get_drvdata(dev);
 	struct kgsl_pwrctrl *pwr;
 	struct kgsl_pwrlevel *pwr_level;
-	int level, i;
+	int level;
+	unsigned int i;
 	unsigned long cur_freq, rec_freq;
 	struct dev_pm_opp *opp;
 
@@ -574,7 +575,12 @@
 	/* If the governor recommends a new frequency, update it here */
 	if (rec_freq != cur_freq) {
 		level = pwr->max_pwrlevel;
-		for (i = pwr->min_pwrlevel; i >= pwr->max_pwrlevel; i--)
+		/*
+		 * Array index of pwrlevels[] should be within the permitted
+		 * power levels, i.e., from max_pwrlevel to min_pwrlevel.
+		 */
+		for (i = pwr->min_pwrlevel; (i >= pwr->max_pwrlevel
+					  && i <= pwr->min_pwrlevel); i--)
 			if (rec_freq <= pwr->pwrlevels[i].gpu_freq) {
 				if (pwr->thermal_cycle == CYCLE_ACTIVE)
 					level = _thermal_adjust(pwr, i);