governor_msm_adreno_tz.c 8.8 KB

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