kgsl_bus.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2019-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <dt-bindings/interconnect/qcom,icc.h>
  7. #include <linux/interconnect.h>
  8. #include <linux/of.h>
  9. #include <soc/qcom/of_common.h>
  10. #include "kgsl_bus.h"
  11. #include "kgsl_device.h"
  12. #include "kgsl_trace.h"
  13. static u32 _ab_buslevel_update(struct kgsl_pwrctrl *pwr,
  14. u32 ib)
  15. {
  16. if (!ib)
  17. return 0;
  18. /*
  19. * In the absence of any other settings, make ab 25% of ib
  20. * where the ib vote is in kbps
  21. */
  22. if ((!pwr->bus_percent_ab) && (!pwr->bus_ab_mbytes))
  23. return 25 * ib / 100000;
  24. if (pwr->bus_width)
  25. return pwr->bus_ab_mbytes;
  26. return (pwr->bus_percent_ab * pwr->bus_max) / 100;
  27. }
  28. int kgsl_bus_update(struct kgsl_device *device,
  29. enum kgsl_bus_vote vote_state)
  30. {
  31. struct kgsl_pwrctrl *pwr = &device->pwrctrl;
  32. int buslevel;
  33. u32 ab;
  34. /* the bus should be ON to update the active frequency */
  35. if ((vote_state != KGSL_BUS_VOTE_OFF) &&
  36. !(test_bit(KGSL_PWRFLAGS_AXI_ON, &pwr->power_flags)))
  37. return 0;
  38. /*
  39. * If the bus should remain on calculate our request and submit it,
  40. * otherwise request bus level 0, off.
  41. */
  42. switch (vote_state) {
  43. case KGSL_BUS_VOTE_OFF:
  44. /* If the bus is being turned off, reset to default level */
  45. pwr->cur_dcvs_buslevel = 0;
  46. pwr->bus_mod = 0;
  47. pwr->bus_percent_ab = 0;
  48. pwr->bus_ab_mbytes = 0;
  49. ab = 0;
  50. break;
  51. case KGSL_BUS_VOTE_ON:
  52. {
  53. /* FIXME: this might be wrong? */
  54. int cur = pwr->pwrlevels[pwr->active_pwrlevel].bus_freq;
  55. buslevel = min_t(int, pwr->pwrlevels[0].bus_max,
  56. cur + pwr->bus_mod);
  57. buslevel = max_t(int, buslevel, 1);
  58. pwr->cur_dcvs_buslevel = buslevel;
  59. ab = _ab_buslevel_update(pwr, pwr->ddr_table[buslevel]);
  60. break;
  61. }
  62. case KGSL_BUS_VOTE_MINIMUM:
  63. /* Request bus level 1, minimum non-zero value */
  64. pwr->cur_dcvs_buslevel = 1;
  65. pwr->bus_mod = 0;
  66. pwr->bus_percent_ab = 0;
  67. pwr->bus_ab_mbytes = 0;
  68. ab = _ab_buslevel_update(pwr,
  69. pwr->ddr_table[pwr->cur_dcvs_buslevel]);
  70. break;
  71. case KGSL_BUS_VOTE_RT_HINT_ON:
  72. pwr->rt_bus_hint_active = true;
  73. /* Only update IB during bus hint */
  74. ab = pwr->cur_ab;
  75. break;
  76. case KGSL_BUS_VOTE_RT_HINT_OFF:
  77. pwr->rt_bus_hint_active = false;
  78. /* Only update IB during bus hint */
  79. ab = pwr->cur_ab;
  80. break;
  81. }
  82. buslevel = pwr->rt_bus_hint_active ?
  83. max(pwr->cur_dcvs_buslevel, pwr->rt_bus_hint) :
  84. pwr->cur_dcvs_buslevel;
  85. return device->ftbl->gpu_bus_set(device, buslevel, ab);
  86. }
  87. void kgsl_icc_set_tag(struct kgsl_pwrctrl *pwr, int buslevel)
  88. {
  89. if (buslevel == pwr->pwrlevels[0].bus_max)
  90. icc_set_tag(pwr->icc_path, QCOM_ICC_TAG_ALWAYS | QCOM_ICC_TAG_PERF_MODE);
  91. else
  92. icc_set_tag(pwr->icc_path, QCOM_ICC_TAG_ALWAYS);
  93. }
  94. static void validate_pwrlevels(struct kgsl_device *device, u32 *ibs,
  95. int count)
  96. {
  97. struct kgsl_pwrctrl *pwr = &device->pwrctrl;
  98. int i;
  99. for (i = 0; i < pwr->num_pwrlevels - 1; i++) {
  100. struct kgsl_pwrlevel *pwrlevel = &pwr->pwrlevels[i];
  101. if (pwrlevel->bus_freq >= count) {
  102. dev_err(device->dev, "Bus setting for GPU freq %d is out of bounds\n",
  103. pwrlevel->gpu_freq);
  104. pwrlevel->bus_freq = count - 1;
  105. }
  106. if (pwrlevel->bus_max >= count) {
  107. dev_err(device->dev, "Bus max for GPU freq %d is out of bounds\n",
  108. pwrlevel->gpu_freq);
  109. pwrlevel->bus_max = count - 1;
  110. }
  111. if (pwrlevel->bus_min >= count) {
  112. dev_err(device->dev, "Bus min for GPU freq %d is out of bounds\n",
  113. pwrlevel->gpu_freq);
  114. pwrlevel->bus_min = count - 1;
  115. }
  116. if (pwrlevel->bus_min > pwrlevel->bus_max) {
  117. dev_err(device->dev, "Bus min is bigger than bus max for GPU freq %d\n",
  118. pwrlevel->gpu_freq);
  119. pwrlevel->bus_min = pwrlevel->bus_max;
  120. }
  121. }
  122. }
  123. u32 *kgsl_bus_get_table(struct platform_device *pdev,
  124. const char *name, int *count)
  125. {
  126. u32 *levels;
  127. int i, num = of_property_count_elems_of_size(pdev->dev.of_node,
  128. name, sizeof(u32));
  129. /* If the bus wasn't specified, then build a static table */
  130. if (num <= 0)
  131. return ERR_PTR(-EINVAL);
  132. levels = kcalloc(num, sizeof(*levels), GFP_KERNEL);
  133. if (!levels)
  134. return ERR_PTR(-ENOMEM);
  135. for (i = 0; i < num; i++)
  136. of_property_read_u32_index(pdev->dev.of_node,
  137. name, i, &levels[i]);
  138. *count = num;
  139. return levels;
  140. }
  141. int kgsl_bus_init(struct kgsl_device *device, struct platform_device *pdev)
  142. {
  143. struct kgsl_pwrctrl *pwr = &device->pwrctrl;
  144. int count;
  145. int ddr = of_fdt_get_ddrtype();
  146. if (ddr >= 0) {
  147. char str[32];
  148. snprintf(str, sizeof(str), "qcom,bus-table-ddr%d", ddr);
  149. pwr->ddr_table = kgsl_bus_get_table(pdev, str, &count);
  150. if (!IS_ERR(pwr->ddr_table))
  151. goto done;
  152. }
  153. /* Look if a generic table is present */
  154. pwr->ddr_table = kgsl_bus_get_table(pdev, "qcom,bus-table-ddr", &count);
  155. if (IS_ERR(pwr->ddr_table)) {
  156. int ret = PTR_ERR(pwr->ddr_table);
  157. pwr->ddr_table = NULL;
  158. return ret;
  159. }
  160. done:
  161. pwr->ddr_table_count = count;
  162. validate_pwrlevels(device, pwr->ddr_table, pwr->ddr_table_count);
  163. pwr->icc_path = of_icc_get(&pdev->dev, "gpu_icc_path");
  164. if (IS_ERR(pwr->icc_path) && !gmu_core_scales_bandwidth(device)) {
  165. WARN(1, "The CPU has no way to set the GPU bus levels\n");
  166. kfree(pwr->ddr_table);
  167. pwr->ddr_table = NULL;
  168. return PTR_ERR(pwr->icc_path);
  169. }
  170. return 0;
  171. }
  172. void kgsl_bus_close(struct kgsl_device *device)
  173. {
  174. kfree(device->pwrctrl.ddr_table);
  175. device->pwrctrl.ddr_table = NULL;
  176. icc_put(device->pwrctrl.icc_path);
  177. device->pwrctrl.icc_path = NULL;
  178. }