a5xx_power.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /* Copyright (c) 2016 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/pm_opp.h>
  14. #include "a5xx_gpu.h"
  15. /*
  16. * The GPMU data block is a block of shared registers that can be used to
  17. * communicate back and forth. These "registers" are by convention with the GPMU
  18. * firwmare and not bound to any specific hardware design
  19. */
  20. #define AGC_INIT_BASE REG_A5XX_GPMU_DATA_RAM_BASE
  21. #define AGC_INIT_MSG_MAGIC (AGC_INIT_BASE + 5)
  22. #define AGC_MSG_BASE (AGC_INIT_BASE + 7)
  23. #define AGC_MSG_STATE (AGC_MSG_BASE + 0)
  24. #define AGC_MSG_COMMAND (AGC_MSG_BASE + 1)
  25. #define AGC_MSG_PAYLOAD_SIZE (AGC_MSG_BASE + 3)
  26. #define AGC_MSG_PAYLOAD(_o) ((AGC_MSG_BASE + 5) + (_o))
  27. #define AGC_POWER_CONFIG_PRODUCTION_ID 1
  28. #define AGC_INIT_MSG_VALUE 0xBABEFACE
  29. static struct {
  30. uint32_t reg;
  31. uint32_t value;
  32. } a5xx_sequence_regs[] = {
  33. { 0xB9A1, 0x00010303 },
  34. { 0xB9A2, 0x13000000 },
  35. { 0xB9A3, 0x00460020 },
  36. { 0xB9A4, 0x10000000 },
  37. { 0xB9A5, 0x040A1707 },
  38. { 0xB9A6, 0x00010000 },
  39. { 0xB9A7, 0x0E000904 },
  40. { 0xB9A8, 0x10000000 },
  41. { 0xB9A9, 0x01165000 },
  42. { 0xB9AA, 0x000E0002 },
  43. { 0xB9AB, 0x03884141 },
  44. { 0xB9AC, 0x10000840 },
  45. { 0xB9AD, 0x572A5000 },
  46. { 0xB9AE, 0x00000003 },
  47. { 0xB9AF, 0x00000000 },
  48. { 0xB9B0, 0x10000000 },
  49. { 0xB828, 0x6C204010 },
  50. { 0xB829, 0x6C204011 },
  51. { 0xB82A, 0x6C204012 },
  52. { 0xB82B, 0x6C204013 },
  53. { 0xB82C, 0x6C204014 },
  54. { 0xB90F, 0x00000004 },
  55. { 0xB910, 0x00000002 },
  56. { 0xB911, 0x00000002 },
  57. { 0xB912, 0x00000002 },
  58. { 0xB913, 0x00000002 },
  59. { 0xB92F, 0x00000004 },
  60. { 0xB930, 0x00000005 },
  61. { 0xB931, 0x00000005 },
  62. { 0xB932, 0x00000005 },
  63. { 0xB933, 0x00000005 },
  64. { 0xB96F, 0x00000001 },
  65. { 0xB970, 0x00000003 },
  66. { 0xB94F, 0x00000004 },
  67. { 0xB950, 0x0000000B },
  68. { 0xB951, 0x0000000B },
  69. { 0xB952, 0x0000000B },
  70. { 0xB953, 0x0000000B },
  71. { 0xB907, 0x00000019 },
  72. { 0xB927, 0x00000019 },
  73. { 0xB947, 0x00000019 },
  74. { 0xB967, 0x00000019 },
  75. { 0xB987, 0x00000019 },
  76. { 0xB906, 0x00220001 },
  77. { 0xB926, 0x00220001 },
  78. { 0xB946, 0x00220001 },
  79. { 0xB966, 0x00220001 },
  80. { 0xB986, 0x00300000 },
  81. { 0xAC40, 0x0340FF41 },
  82. { 0xAC41, 0x03BEFED0 },
  83. { 0xAC42, 0x00331FED },
  84. { 0xAC43, 0x021FFDD3 },
  85. { 0xAC44, 0x5555AAAA },
  86. { 0xAC45, 0x5555AAAA },
  87. { 0xB9BA, 0x00000008 },
  88. };
  89. /*
  90. * Get the actual voltage value for the operating point at the specified
  91. * frequency
  92. */
  93. static inline uint32_t _get_mvolts(struct msm_gpu *gpu, uint32_t freq)
  94. {
  95. struct drm_device *dev = gpu->dev;
  96. struct msm_drm_private *priv = dev->dev_private;
  97. struct platform_device *pdev = priv->gpu_pdev;
  98. struct dev_pm_opp *opp;
  99. u32 ret = 0;
  100. opp = dev_pm_opp_find_freq_exact(&pdev->dev, freq, true);
  101. if (!IS_ERR(opp)) {
  102. ret = dev_pm_opp_get_voltage(opp) / 1000;
  103. dev_pm_opp_put(opp);
  104. }
  105. return ret;
  106. }
  107. /* Setup thermal limit management */
  108. static void a5xx_lm_setup(struct msm_gpu *gpu)
  109. {
  110. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  111. struct a5xx_gpu *a5xx_gpu = to_a5xx_gpu(adreno_gpu);
  112. unsigned int i;
  113. /* Write the block of sequence registers */
  114. for (i = 0; i < ARRAY_SIZE(a5xx_sequence_regs); i++)
  115. gpu_write(gpu, a5xx_sequence_regs[i].reg,
  116. a5xx_sequence_regs[i].value);
  117. /* Hard code the A530 GPU thermal sensor ID for the GPMU */
  118. gpu_write(gpu, REG_A5XX_GPMU_TEMP_SENSOR_ID, 0x60007);
  119. gpu_write(gpu, REG_A5XX_GPMU_DELTA_TEMP_THRESHOLD, 0x01);
  120. gpu_write(gpu, REG_A5XX_GPMU_TEMP_SENSOR_CONFIG, 0x01);
  121. /* Until we get clock scaling 0 is always the active power level */
  122. gpu_write(gpu, REG_A5XX_GPMU_GPMU_VOLTAGE, 0x80000000 | 0);
  123. gpu_write(gpu, REG_A5XX_GPMU_BASE_LEAKAGE, a5xx_gpu->lm_leakage);
  124. /* The threshold is fixed at 6000 for A530 */
  125. gpu_write(gpu, REG_A5XX_GPMU_GPMU_PWR_THRESHOLD, 0x80000000 | 6000);
  126. gpu_write(gpu, REG_A5XX_GPMU_BEC_ENABLE, 0x10001FFF);
  127. gpu_write(gpu, REG_A5XX_GDPM_CONFIG1, 0x00201FF1);
  128. /* Write the voltage table */
  129. gpu_write(gpu, REG_A5XX_GPMU_BEC_ENABLE, 0x10001FFF);
  130. gpu_write(gpu, REG_A5XX_GDPM_CONFIG1, 0x201FF1);
  131. gpu_write(gpu, AGC_MSG_STATE, 1);
  132. gpu_write(gpu, AGC_MSG_COMMAND, AGC_POWER_CONFIG_PRODUCTION_ID);
  133. /* Write the max power - hard coded to 5448 for A530 */
  134. gpu_write(gpu, AGC_MSG_PAYLOAD(0), 5448);
  135. gpu_write(gpu, AGC_MSG_PAYLOAD(1), 1);
  136. /*
  137. * For now just write the one voltage level - we will do more when we
  138. * can do scaling
  139. */
  140. gpu_write(gpu, AGC_MSG_PAYLOAD(2), _get_mvolts(gpu, gpu->fast_rate));
  141. gpu_write(gpu, AGC_MSG_PAYLOAD(3), gpu->fast_rate / 1000000);
  142. gpu_write(gpu, AGC_MSG_PAYLOAD_SIZE, 4 * sizeof(uint32_t));
  143. gpu_write(gpu, AGC_INIT_MSG_MAGIC, AGC_INIT_MSG_VALUE);
  144. }
  145. /* Enable SP/TP cpower collapse */
  146. static void a5xx_pc_init(struct msm_gpu *gpu)
  147. {
  148. gpu_write(gpu, REG_A5XX_GPMU_PWR_COL_INTER_FRAME_CTRL, 0x7F);
  149. gpu_write(gpu, REG_A5XX_GPMU_PWR_COL_BINNING_CTRL, 0);
  150. gpu_write(gpu, REG_A5XX_GPMU_PWR_COL_INTER_FRAME_HYST, 0xA0080);
  151. gpu_write(gpu, REG_A5XX_GPMU_PWR_COL_STAGGER_DELAY, 0x600040);
  152. }
  153. /* Enable the GPMU microcontroller */
  154. static int a5xx_gpmu_init(struct msm_gpu *gpu)
  155. {
  156. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  157. struct a5xx_gpu *a5xx_gpu = to_a5xx_gpu(adreno_gpu);
  158. struct msm_ringbuffer *ring = gpu->rb[0];
  159. if (!a5xx_gpu->gpmu_dwords)
  160. return 0;
  161. /* Turn off protected mode for this operation */
  162. OUT_PKT7(ring, CP_SET_PROTECTED_MODE, 1);
  163. OUT_RING(ring, 0);
  164. /* Kick off the IB to load the GPMU microcode */
  165. OUT_PKT7(ring, CP_INDIRECT_BUFFER_PFE, 3);
  166. OUT_RING(ring, lower_32_bits(a5xx_gpu->gpmu_iova));
  167. OUT_RING(ring, upper_32_bits(a5xx_gpu->gpmu_iova));
  168. OUT_RING(ring, a5xx_gpu->gpmu_dwords);
  169. /* Turn back on protected mode */
  170. OUT_PKT7(ring, CP_SET_PROTECTED_MODE, 1);
  171. OUT_RING(ring, 1);
  172. gpu->funcs->flush(gpu, ring);
  173. if (!a5xx_idle(gpu, ring)) {
  174. DRM_ERROR("%s: Unable to load GPMU firmware. GPMU will not be active\n",
  175. gpu->name);
  176. return -EINVAL;
  177. }
  178. gpu_write(gpu, REG_A5XX_GPMU_WFI_CONFIG, 0x4014);
  179. /* Kick off the GPMU */
  180. gpu_write(gpu, REG_A5XX_GPMU_CM3_SYSRESET, 0x0);
  181. /*
  182. * Wait for the GPMU to respond. It isn't fatal if it doesn't, we just
  183. * won't have advanced power collapse.
  184. */
  185. if (spin_usecs(gpu, 25, REG_A5XX_GPMU_GENERAL_0, 0xFFFFFFFF,
  186. 0xBABEFACE))
  187. DRM_ERROR("%s: GPMU firmware initialization timed out\n",
  188. gpu->name);
  189. return 0;
  190. }
  191. /* Enable limits management */
  192. static void a5xx_lm_enable(struct msm_gpu *gpu)
  193. {
  194. gpu_write(gpu, REG_A5XX_GDPM_INT_MASK, 0x0);
  195. gpu_write(gpu, REG_A5XX_GDPM_INT_EN, 0x0A);
  196. gpu_write(gpu, REG_A5XX_GPMU_GPMU_VOLTAGE_INTR_EN_MASK, 0x01);
  197. gpu_write(gpu, REG_A5XX_GPMU_TEMP_THRESHOLD_INTR_EN_MASK, 0x50000);
  198. gpu_write(gpu, REG_A5XX_GPMU_THROTTLE_UNMASK_FORCE_CTRL, 0x30000);
  199. gpu_write(gpu, REG_A5XX_GPMU_CLOCK_THROTTLE_CTRL, 0x011);
  200. }
  201. int a5xx_power_init(struct msm_gpu *gpu)
  202. {
  203. int ret;
  204. /* Set up the limits management */
  205. a5xx_lm_setup(gpu);
  206. /* Set up SP/TP power collpase */
  207. a5xx_pc_init(gpu);
  208. /* Start the GPMU */
  209. ret = a5xx_gpmu_init(gpu);
  210. if (ret)
  211. return ret;
  212. /* Start the limits management */
  213. a5xx_lm_enable(gpu);
  214. return 0;
  215. }
  216. void a5xx_gpmu_ucode_init(struct msm_gpu *gpu)
  217. {
  218. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  219. struct a5xx_gpu *a5xx_gpu = to_a5xx_gpu(adreno_gpu);
  220. struct drm_device *drm = gpu->dev;
  221. uint32_t dwords = 0, offset = 0, bosize;
  222. unsigned int *data, *ptr, *cmds;
  223. unsigned int cmds_size;
  224. if (a5xx_gpu->gpmu_bo)
  225. return;
  226. data = (unsigned int *) adreno_gpu->fw[ADRENO_FW_GPMU]->data;
  227. /*
  228. * The first dword is the size of the remaining data in dwords. Use it
  229. * as a checksum of sorts and make sure it matches the actual size of
  230. * the firmware that we read
  231. */
  232. if (adreno_gpu->fw[ADRENO_FW_GPMU]->size < 8 ||
  233. (data[0] < 2) || (data[0] >=
  234. (adreno_gpu->fw[ADRENO_FW_GPMU]->size >> 2)))
  235. return;
  236. /* The second dword is an ID - look for 2 (GPMU_FIRMWARE_ID) */
  237. if (data[1] != 2)
  238. return;
  239. cmds = data + data[2] + 3;
  240. cmds_size = data[0] - data[2] - 2;
  241. /*
  242. * A single type4 opcode can only have so many values attached so
  243. * add enough opcodes to load the all the commands
  244. */
  245. bosize = (cmds_size + (cmds_size / TYPE4_MAX_PAYLOAD) + 1) << 2;
  246. ptr = msm_gem_kernel_new_locked(drm, bosize,
  247. MSM_BO_UNCACHED | MSM_BO_GPU_READONLY, gpu->aspace,
  248. &a5xx_gpu->gpmu_bo, &a5xx_gpu->gpmu_iova);
  249. if (IS_ERR(ptr))
  250. goto err;
  251. while (cmds_size > 0) {
  252. int i;
  253. uint32_t _size = cmds_size > TYPE4_MAX_PAYLOAD ?
  254. TYPE4_MAX_PAYLOAD : cmds_size;
  255. ptr[dwords++] = PKT4(REG_A5XX_GPMU_INST_RAM_BASE + offset,
  256. _size);
  257. for (i = 0; i < _size; i++)
  258. ptr[dwords++] = *cmds++;
  259. offset += _size;
  260. cmds_size -= _size;
  261. }
  262. msm_gem_put_vaddr(a5xx_gpu->gpmu_bo);
  263. a5xx_gpu->gpmu_dwords = dwords;
  264. return;
  265. err:
  266. if (a5xx_gpu->gpmu_iova)
  267. msm_gem_put_iova(a5xx_gpu->gpmu_bo, gpu->aspace);
  268. if (a5xx_gpu->gpmu_bo)
  269. drm_gem_object_unreference(a5xx_gpu->gpmu_bo);
  270. a5xx_gpu->gpmu_bo = NULL;
  271. a5xx_gpu->gpmu_iova = 0;
  272. a5xx_gpu->gpmu_dwords = 0;
  273. }