governor_gpubw_mon.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2014-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/devfreq.h>
  7. #include <linux/slab.h>
  8. #include "governor.h"
  9. #include "msm_adreno_devfreq.h"
  10. #define MIN_BUSY 1000
  11. #define LONG_FLOOR 50000
  12. #define HIST 5
  13. #define TARGET 80
  14. #define CAP 75
  15. #define WAIT_THRESHOLD 10
  16. /* AB vote is in multiple of BW_STEP Mega bytes */
  17. #define BW_STEP 50
  18. static void _update_cutoff(struct devfreq_msm_adreno_tz_data *priv,
  19. unsigned int norm_max)
  20. {
  21. int i;
  22. priv->bus.max = norm_max;
  23. for (i = 0; i < priv->bus.num; i++) {
  24. priv->bus.up[i] = priv->bus.p_up[i] * norm_max / 100;
  25. priv->bus.down[i] = priv->bus.p_down[i] * norm_max / 100;
  26. }
  27. }
  28. static ssize_t cur_ab_show(struct device *dev,
  29. struct device_attribute *attr,
  30. char *buf)
  31. {
  32. struct devfreq *df = to_devfreq(dev);
  33. struct msm_busmon_extended_profile *bus_profile = container_of(
  34. (df->profile),
  35. struct msm_busmon_extended_profile,
  36. profile);
  37. return scnprintf(buf, PAGE_SIZE, "%lu\n", bus_profile->ab_mbytes);
  38. }
  39. static ssize_t sampling_interval_show(struct device *dev,
  40. struct device_attribute *attr, char *buf)
  41. {
  42. struct devfreq *df = to_devfreq(dev);
  43. struct msm_busmon_extended_profile *bus_profile = container_of(
  44. (df->profile),
  45. struct msm_busmon_extended_profile,
  46. profile);
  47. return scnprintf(buf, PAGE_SIZE, "%d\n", bus_profile->sampling_ms);
  48. }
  49. static ssize_t sampling_interval_store(struct device *dev,
  50. struct device_attribute *attr,
  51. const char *buf, size_t count)
  52. {
  53. struct devfreq *df = to_devfreq(dev);
  54. struct msm_busmon_extended_profile *bus_profile = container_of(
  55. (df->profile),
  56. struct msm_busmon_extended_profile,
  57. profile);
  58. u32 value;
  59. int ret;
  60. ret = kstrtou32(buf, 0, &value);
  61. if (ret)
  62. return ret;
  63. bus_profile->sampling_ms = value;
  64. return count;
  65. }
  66. static DEVICE_ATTR_RW(sampling_interval);
  67. static DEVICE_ATTR_RO(cur_ab);
  68. static const struct device_attribute *gpubw_attr_list[] = {
  69. &dev_attr_sampling_interval,
  70. &dev_attr_cur_ab,
  71. NULL
  72. };
  73. static u32 generate_hint(struct devfreq_msm_adreno_tz_data *priv, int buslevel,
  74. unsigned long freq, unsigned long minfreq)
  75. {
  76. int act_level;
  77. int norm_max_cycles;
  78. int norm_cycles;
  79. int wait_active_percent;
  80. int gpu_percent;
  81. norm_max_cycles = (unsigned int)(priv->bus.ram_time) /
  82. (unsigned int) priv->bus.total_time;
  83. norm_cycles = (unsigned int)(priv->bus.ram_time + priv->bus.ram_wait) /
  84. (unsigned int) priv->bus.total_time;
  85. wait_active_percent = (100 * (unsigned int)priv->bus.ram_wait) /
  86. (unsigned int) priv->bus.ram_time;
  87. gpu_percent = (100 * (unsigned int)priv->bus.gpu_time) /
  88. (unsigned int) priv->bus.total_time;
  89. /*
  90. * If there's a new high watermark, update the cutoffs and send the
  91. * FAST hint, provided that we are using a floating watermark.
  92. * Otherwise check the current value against the current
  93. * cutoffs.
  94. */
  95. if (norm_max_cycles > priv->bus.max && priv->bus.floating) {
  96. _update_cutoff(priv, norm_max_cycles);
  97. return BUSMON_FLAG_FAST_HINT;
  98. }
  99. /* Increase BW vote to avoid starving GPU for BW if required */
  100. if (priv->fast_bus_hint && minfreq == freq) {
  101. if (wait_active_percent > 95)
  102. return BUSMON_FLAG_SUPER_FAST_HINT;
  103. if (wait_active_percent > 80)
  104. return BUSMON_FLAG_FAST_HINT;
  105. }
  106. /* GPU votes for IB not AB so don't under vote the system */
  107. norm_cycles = (100 * norm_cycles) / TARGET;
  108. act_level = max_t(int, buslevel, 0);
  109. act_level = min_t(int, act_level, priv->bus.num - 1);
  110. if ((norm_cycles > priv->bus.up[act_level] ||
  111. wait_active_percent > WAIT_THRESHOLD) &&
  112. gpu_percent > CAP)
  113. return BUSMON_FLAG_FAST_HINT;
  114. if (norm_cycles < priv->bus.down[act_level] && buslevel)
  115. return BUSMON_FLAG_SLOW_HINT;
  116. return 0;
  117. }
  118. static int devfreq_gpubw_get_target(struct devfreq *df,
  119. unsigned long *freq)
  120. {
  121. struct devfreq_msm_adreno_tz_data *priv = df->data;
  122. struct msm_busmon_extended_profile *bus_profile = container_of(
  123. (df->profile),
  124. struct msm_busmon_extended_profile,
  125. profile);
  126. struct devfreq_dev_status *stats = &df->last_status;
  127. struct xstats b = {0};
  128. int result;
  129. int norm_ab;
  130. unsigned long ab_mbytes = 0;
  131. /*
  132. * Normalized AB should at max usage be the gpu_bimc frequency in MHz.
  133. * Start with a reasonable value and let the system push it up to max.
  134. */
  135. static int norm_ab_max = 300;
  136. if (priv == NULL)
  137. return 0;
  138. stats->private_data = &b;
  139. result = devfreq_update_stats(df);
  140. /* Return if devfreq is not enabled */
  141. if (result)
  142. return result;
  143. *freq = stats->current_frequency;
  144. priv->bus.total_time += stats->total_time;
  145. priv->bus.gpu_time += stats->busy_time;
  146. priv->bus.ram_time += b.ram_time;
  147. priv->bus.ram_wait += b.ram_wait;
  148. if (priv->bus.total_time < bus_profile->sampling_ms)
  149. return result;
  150. bus_profile->flag = generate_hint(priv, b.buslevel, *freq,
  151. b.gpu_minfreq);
  152. /* Calculate the AB vote based on bus width if defined */
  153. if (priv->bus.width) {
  154. norm_ab = (unsigned int)priv->bus.ram_time /
  155. (unsigned int) priv->bus.total_time;
  156. /* Calculate AB in Mega Bytes and roundup in BW_STEP */
  157. ab_mbytes = (norm_ab * priv->bus.width * 1000000ULL) >> 20;
  158. bus_profile->ab_mbytes = roundup(ab_mbytes, BW_STEP);
  159. } else if (bus_profile->flag) {
  160. /* Re-calculate the AB percentage for a new IB vote */
  161. norm_ab = (unsigned int)priv->bus.ram_time /
  162. (unsigned int) priv->bus.total_time;
  163. if (norm_ab > norm_ab_max)
  164. norm_ab_max = norm_ab;
  165. bus_profile->percent_ab = (100 * norm_ab) / norm_ab_max;
  166. }
  167. priv->bus.total_time = 0;
  168. priv->bus.gpu_time = 0;
  169. priv->bus.ram_time = 0;
  170. priv->bus.ram_wait = 0;
  171. return result;
  172. }
  173. static int gpubw_start(struct devfreq *devfreq)
  174. {
  175. struct devfreq_msm_adreno_tz_data *priv;
  176. struct msm_busmon_extended_profile *bus_profile = container_of(
  177. (devfreq->profile),
  178. struct msm_busmon_extended_profile,
  179. profile);
  180. unsigned int t1, t2 = 2 * HIST;
  181. int i, bus_size;
  182. devfreq->data = bus_profile->private_data;
  183. priv = devfreq->data;
  184. bus_size = sizeof(u32) * priv->bus.num;
  185. priv->bus.up = kzalloc(bus_size, GFP_KERNEL);
  186. priv->bus.down = kzalloc(bus_size, GFP_KERNEL);
  187. priv->bus.p_up = kzalloc(bus_size, GFP_KERNEL);
  188. priv->bus.p_down = kzalloc(bus_size, GFP_KERNEL);
  189. if (priv->bus.up == NULL || priv->bus.down == NULL ||
  190. priv->bus.p_up == NULL || priv->bus.p_down == NULL)
  191. return -ENOMEM;
  192. /* Set up the cut-over percentages for the bus calculation. */
  193. for (i = 0; i < priv->bus.num; i++) {
  194. t1 = (u32)(100 * priv->bus.ib_kbps[i]) /
  195. (u32)priv->bus.ib_kbps[priv->bus.num - 1];
  196. priv->bus.p_up[i] = t1 - HIST;
  197. priv->bus.p_down[i] = t2 - 2 * HIST;
  198. t2 = t1;
  199. }
  200. /* Set the upper-most and lower-most bounds correctly. */
  201. priv->bus.p_down[0] = 0;
  202. for (i = 0; i < priv->bus.num; i++) {
  203. if (priv->bus.p_down[i] < 2 * HIST)
  204. priv->bus.p_down[i] = 2 * HIST;
  205. }
  206. if (priv->bus.num >= 1)
  207. priv->bus.p_up[priv->bus.num - 1] = 100;
  208. _update_cutoff(priv, priv->bus.max);
  209. bus_profile->sampling_ms = LONG_FLOOR;
  210. for (i = 0; gpubw_attr_list[i] != NULL; i++)
  211. device_create_file(&devfreq->dev, gpubw_attr_list[i]);
  212. return 0;
  213. }
  214. static int gpubw_stop(struct devfreq *devfreq)
  215. {
  216. struct devfreq_msm_adreno_tz_data *priv = devfreq->data;
  217. int i;
  218. for (i = 0; gpubw_attr_list[i] != NULL; i++)
  219. device_remove_file(&devfreq->dev, gpubw_attr_list[i]);
  220. if (priv) {
  221. kfree(priv->bus.up);
  222. kfree(priv->bus.down);
  223. kfree(priv->bus.p_up);
  224. kfree(priv->bus.p_down);
  225. }
  226. devfreq->data = NULL;
  227. return 0;
  228. }
  229. static int devfreq_gpubw_event_handler(struct devfreq *devfreq,
  230. unsigned int event, void *data)
  231. {
  232. int result = 0;
  233. unsigned long freq;
  234. if (strcmp(dev_name(devfreq->dev.parent), "kgsl-busmon"))
  235. return -EINVAL;
  236. mutex_lock(&devfreq->lock);
  237. freq = devfreq->previous_freq;
  238. switch (event) {
  239. case DEVFREQ_GOV_START:
  240. result = gpubw_start(devfreq);
  241. break;
  242. case DEVFREQ_GOV_STOP:
  243. result = gpubw_stop(devfreq);
  244. break;
  245. case DEVFREQ_GOV_RESUME:
  246. /* TODO ..... */
  247. /* ret = update_devfreq(devfreq); */
  248. break;
  249. case DEVFREQ_GOV_SUSPEND:
  250. {
  251. struct devfreq_msm_adreno_tz_data *priv = devfreq->data;
  252. if (priv) {
  253. priv->bus.total_time = 0;
  254. priv->bus.gpu_time = 0;
  255. priv->bus.ram_time = 0;
  256. }
  257. }
  258. break;
  259. default:
  260. result = 0;
  261. break;
  262. }
  263. mutex_unlock(&devfreq->lock);
  264. return result;
  265. }
  266. static struct devfreq_governor devfreq_gpubw = {
  267. .name = "gpubw_mon",
  268. .get_target_freq = devfreq_gpubw_get_target,
  269. .event_handler = devfreq_gpubw_event_handler,
  270. .flags = DEVFREQ_GOV_FLAG_IMMUTABLE,
  271. };
  272. int devfreq_gpubw_init(void)
  273. {
  274. return devfreq_add_governor(&devfreq_gpubw);
  275. }
  276. void devfreq_gpubw_exit(void)
  277. {
  278. int ret;
  279. ret = devfreq_remove_governor(&devfreq_gpubw);
  280. if (ret)
  281. pr_err("%s: failed remove governor %d\n", __func__, ret);
  282. }