governor_msm_adreno_tz.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /* Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. */
  13. #include <linux/errno.h>
  14. #include <linux/module.h>
  15. #include <linux/devfreq.h>
  16. #include <linux/math64.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/slab.h>
  19. #include <linux/io.h>
  20. #include <linux/ftrace.h>
  21. #include <linux/msm_adreno_devfreq.h>
  22. #include <mach/scm.h>
  23. #include "governor.h"
  24. static DEFINE_SPINLOCK(tz_lock);
  25. /*
  26. * FLOOR is 5msec to capture up to 3 re-draws
  27. * per frame for 60fps content.
  28. */
  29. #define FLOOR 5000
  30. #define LONG_FLOOR 50000
  31. #define HIST 5
  32. #define TARGET 80
  33. #define CAP 75
  34. /*
  35. * CEILING is 50msec, larger than any standard
  36. * frame length, but less than the idle timer.
  37. */
  38. #define CEILING 50000
  39. #define TZ_RESET_ID 0x3
  40. #define TZ_UPDATE_ID 0x4
  41. #define TZ_INIT_ID 0x6
  42. #define TAG "msm_adreno_tz: "
  43. /* Trap into the TrustZone, and call funcs there. */
  44. static int __secure_tz_entry2(u32 cmd, u32 val1, u32 val2)
  45. {
  46. int ret;
  47. spin_lock(&tz_lock);
  48. /* sync memory before sending the commands to tz*/
  49. __iowmb();
  50. ret = scm_call_atomic2(SCM_SVC_IO, cmd, val1, val2);
  51. spin_unlock(&tz_lock);
  52. return ret;
  53. }
  54. static int __secure_tz_entry3(u32 cmd, u32 val1, u32 val2, u32 val3)
  55. {
  56. int ret;
  57. spin_lock(&tz_lock);
  58. /* sync memory before sending the commands to tz*/
  59. __iowmb();
  60. ret = scm_call_atomic3(SCM_SVC_IO, cmd, val1, val2, val3);
  61. spin_unlock(&tz_lock);
  62. return ret;
  63. }
  64. static void _update_cutoff(struct devfreq_msm_adreno_tz_data *priv,
  65. unsigned int norm_max)
  66. {
  67. int i;
  68. priv->bus.max = norm_max;
  69. for (i = 0; i < priv->bus.num; i++) {
  70. priv->bus.up[i] = priv->bus.p_up[i] * norm_max / 100;
  71. priv->bus.down[i] = priv->bus.p_down[i] * norm_max / 100;
  72. }
  73. }
  74. static int tz_get_target_freq(struct devfreq *devfreq, unsigned long *freq,
  75. u32 *flag)
  76. {
  77. int result = 0;
  78. struct devfreq_msm_adreno_tz_data *priv = devfreq->data;
  79. struct devfreq_dev_status stats;
  80. struct xstats b;
  81. int val, level = 0;
  82. int act_level;
  83. int norm_cycles;
  84. int gpu_percent;
  85. if (priv->bus.num)
  86. stats.private_data = &b;
  87. else
  88. stats.private_data = NULL;
  89. result = devfreq->profile->get_dev_status(devfreq->dev.parent, &stats);
  90. if (result) {
  91. pr_err(TAG "get_status failed %d\n", result);
  92. return result;
  93. }
  94. *freq = stats.current_frequency;
  95. *flag = 0;
  96. priv->bin.total_time += stats.total_time;
  97. priv->bin.busy_time += stats.busy_time;
  98. if (priv->bus.num) {
  99. priv->bus.total_time += stats.total_time;
  100. priv->bus.gpu_time += stats.busy_time;
  101. priv->bus.ram_time += b.ram_time;
  102. priv->bus.ram_time += b.ram_wait;
  103. }
  104. /*
  105. * Do not waste CPU cycles running this algorithm if
  106. * the GPU just started, or if less than FLOOR time
  107. * has passed since the last run.
  108. */
  109. if ((stats.total_time == 0) ||
  110. (priv->bin.total_time < FLOOR)) {
  111. return 1;
  112. }
  113. level = devfreq_get_freq_level(devfreq, stats.current_frequency);
  114. if (level < 0) {
  115. pr_err(TAG "bad freq %ld\n", stats.current_frequency);
  116. return level;
  117. }
  118. /*
  119. * If there is an extended block of busy processing,
  120. * increase frequency. Otherwise run the normal algorithm.
  121. */
  122. if (priv->bin.busy_time > CEILING) {
  123. val = -1 * level;
  124. } else {
  125. val = __secure_tz_entry3(TZ_UPDATE_ID,
  126. level,
  127. priv->bin.total_time,
  128. priv->bin.busy_time);
  129. }
  130. priv->bin.total_time = 0;
  131. priv->bin.busy_time = 0;
  132. /*
  133. * If the decision is to move to a different level, make sure the GPU
  134. * frequency changes.
  135. */
  136. if (val) {
  137. level += val;
  138. level = max(level, 0);
  139. level = min_t(int, level, devfreq->profile->max_state);
  140. goto clear;
  141. }
  142. if (priv->bus.total_time < LONG_FLOOR)
  143. goto end;
  144. norm_cycles = (unsigned int)priv->bus.ram_time /
  145. (unsigned int) priv->bus.total_time;
  146. gpu_percent = (100 * (unsigned int)priv->bus.gpu_time) /
  147. (unsigned int) priv->bus.total_time;
  148. /*
  149. * If there's a new high watermark, update the cutoffs and send the
  150. * FAST hint. Otherwise check the current value against the current
  151. * cutoffs.
  152. */
  153. if (norm_cycles > priv->bus.max) {
  154. _update_cutoff(priv, norm_cycles);
  155. *flag = DEVFREQ_FLAG_FAST_HINT;
  156. } else {
  157. /*
  158. * Normalize by gpu_time unless it is a small fraction of
  159. * the total time interval.
  160. */
  161. norm_cycles = (100 * norm_cycles) / TARGET;
  162. act_level = priv->bus.index[level] + b.mod;
  163. act_level = (act_level < 0) ? 0 : act_level;
  164. act_level = (act_level >= priv->bus.num) ?
  165. (priv->bus.num - 1) : act_level;
  166. if (norm_cycles > priv->bus.up[act_level] &&
  167. gpu_percent > CAP)
  168. *flag = DEVFREQ_FLAG_FAST_HINT;
  169. else if (norm_cycles < priv->bus.down[act_level] && level)
  170. *flag = DEVFREQ_FLAG_SLOW_HINT;
  171. }
  172. clear:
  173. priv->bus.total_time = 0;
  174. priv->bus.gpu_time = 0;
  175. priv->bus.ram_time = 0;
  176. end:
  177. *freq = devfreq->profile->freq_table[level];
  178. return 0;
  179. }
  180. static int tz_notify(struct notifier_block *nb, unsigned long type, void *devp)
  181. {
  182. int result = 0;
  183. struct devfreq *devfreq = devp;
  184. switch (type) {
  185. case ADRENO_DEVFREQ_NOTIFY_IDLE:
  186. case ADRENO_DEVFREQ_NOTIFY_RETIRE:
  187. mutex_lock(&devfreq->lock);
  188. result = update_devfreq(devfreq);
  189. mutex_unlock(&devfreq->lock);
  190. break;
  191. /* ignored by this governor */
  192. case ADRENO_DEVFREQ_NOTIFY_SUBMIT:
  193. default:
  194. break;
  195. }
  196. return notifier_from_errno(result);
  197. }
  198. static int tz_start(struct devfreq *devfreq)
  199. {
  200. struct devfreq_msm_adreno_tz_data *priv;
  201. unsigned int tz_pwrlevels[MSM_ADRENO_MAX_PWRLEVELS + 1];
  202. unsigned int t1, t2 = 2 * HIST;
  203. int i, out, ret;
  204. if (devfreq->data == NULL) {
  205. pr_err(TAG "data is required for this governor\n");
  206. return -EINVAL;
  207. }
  208. priv = devfreq->data;
  209. priv->nb.notifier_call = tz_notify;
  210. out = 1;
  211. if (devfreq->profile->max_state < MSM_ADRENO_MAX_PWRLEVELS) {
  212. for (i = 0; i < devfreq->profile->max_state; i++)
  213. tz_pwrlevels[out++] = devfreq->profile->freq_table[i];
  214. tz_pwrlevels[0] = i;
  215. } else {
  216. pr_err(TAG "tz_pwrlevels[] is too short\n");
  217. return -EINVAL;
  218. }
  219. ret = scm_call(SCM_SVC_DCVS, TZ_INIT_ID, tz_pwrlevels,
  220. sizeof(tz_pwrlevels), NULL, 0);
  221. if (ret != 0)
  222. pr_err(TAG "tz_init failed\n");
  223. /* Set up the cut-over percentages for the bus calculation. */
  224. if (priv->bus.num) {
  225. for (i = 0; i < priv->bus.num; i++) {
  226. t1 = (u32)(100 * priv->bus.ib[i]) /
  227. (u32)priv->bus.ib[priv->bus.num - 1];
  228. priv->bus.p_up[i] = t1 - HIST;
  229. priv->bus.p_down[i] = t2 - 2 * HIST;
  230. t2 = t1;
  231. }
  232. /* Set the upper-most and lower-most bounds correctly. */
  233. priv->bus.p_down[0] = 0;
  234. priv->bus.p_down[1] = (priv->bus.p_down[1] > (2 * HIST)) ?
  235. priv->bus.p_down[1] : (2 * HIST);
  236. if (priv->bus.num - 1 >= 0)
  237. priv->bus.p_up[priv->bus.num - 1] = 100;
  238. _update_cutoff(priv, priv->bus.max);
  239. }
  240. return kgsl_devfreq_add_notifier(devfreq->dev.parent, &priv->nb);
  241. }
  242. static int tz_stop(struct devfreq *devfreq)
  243. {
  244. struct devfreq_msm_adreno_tz_data *priv = devfreq->data;
  245. kgsl_devfreq_del_notifier(devfreq->dev.parent, &priv->nb);
  246. return 0;
  247. }
  248. static int tz_resume(struct devfreq *devfreq)
  249. {
  250. struct devfreq_dev_profile *profile = devfreq->profile;
  251. unsigned long freq;
  252. freq = profile->initial_freq;
  253. return profile->target(devfreq->dev.parent, &freq, 0);
  254. }
  255. static int tz_suspend(struct devfreq *devfreq)
  256. {
  257. struct devfreq_msm_adreno_tz_data *priv = devfreq->data;
  258. __secure_tz_entry2(TZ_RESET_ID, 0, 0);
  259. priv->bin.total_time = 0;
  260. priv->bin.busy_time = 0;
  261. priv->bus.total_time = 0;
  262. priv->bus.gpu_time = 0;
  263. priv->bus.ram_time = 0;
  264. return 0;
  265. }
  266. static int tz_handler(struct devfreq *devfreq, unsigned int event, void *data)
  267. {
  268. int result;
  269. BUG_ON(devfreq == NULL);
  270. switch (event) {
  271. case DEVFREQ_GOV_START:
  272. result = tz_start(devfreq);
  273. break;
  274. case DEVFREQ_GOV_STOP:
  275. result = tz_stop(devfreq);
  276. break;
  277. case DEVFREQ_GOV_SUSPEND:
  278. result = tz_suspend(devfreq);
  279. break;
  280. case DEVFREQ_GOV_RESUME:
  281. result = tz_resume(devfreq);
  282. break;
  283. case DEVFREQ_GOV_INTERVAL:
  284. /* ignored, this governor doesn't use polling */
  285. default:
  286. result = 0;
  287. break;
  288. }
  289. return result;
  290. }
  291. static struct devfreq_governor msm_adreno_tz = {
  292. .name = "msm-adreno-tz",
  293. .get_target_freq = tz_get_target_freq,
  294. .event_handler = tz_handler,
  295. };
  296. static int __init msm_adreno_tz_init(void)
  297. {
  298. return devfreq_add_governor(&msm_adreno_tz);
  299. }
  300. subsys_initcall(msm_adreno_tz_init);
  301. static void __exit msm_adreno_tz_exit(void)
  302. {
  303. int ret;
  304. ret = devfreq_remove_governor(&msm_adreno_tz);
  305. if (ret)
  306. pr_err(TAG "failed to remove governor %d\n", ret);
  307. return;
  308. }
  309. module_exit(msm_adreno_tz_exit);
  310. MODULE_LICENSE("GPLv2");