adreno_gpu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * Copyright (C) 2013 Red Hat
  3. * Author: Rob Clark <robdclark@gmail.com>
  4. *
  5. * Copyright (c) 2014 The Linux Foundation. All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "adreno_gpu.h"
  20. #include "msm_gem.h"
  21. #include "msm_mmu.h"
  22. #define RB_SIZE SZ_32K
  23. #define RB_BLKSIZE 16
  24. int adreno_get_param(struct msm_gpu *gpu, uint32_t param, uint64_t *value)
  25. {
  26. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  27. switch (param) {
  28. case MSM_PARAM_GPU_ID:
  29. *value = adreno_gpu->info->revn;
  30. return 0;
  31. case MSM_PARAM_GMEM_SIZE:
  32. *value = adreno_gpu->gmem;
  33. return 0;
  34. case MSM_PARAM_CHIP_ID:
  35. *value = adreno_gpu->rev.patchid |
  36. (adreno_gpu->rev.minor << 8) |
  37. (adreno_gpu->rev.major << 16) |
  38. (adreno_gpu->rev.core << 24);
  39. return 0;
  40. default:
  41. DBG("%s: invalid param: %u", gpu->name, param);
  42. return -EINVAL;
  43. }
  44. }
  45. #define rbmemptr(adreno_gpu, member) \
  46. ((adreno_gpu)->memptrs_iova + offsetof(struct adreno_rbmemptrs, member))
  47. int adreno_hw_init(struct msm_gpu *gpu)
  48. {
  49. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  50. int ret;
  51. DBG("%s", gpu->name);
  52. ret = msm_gem_get_iova(gpu->rb->bo, gpu->id, &gpu->rb_iova);
  53. if (ret) {
  54. gpu->rb_iova = 0;
  55. dev_err(gpu->dev->dev, "could not map ringbuffer: %d\n", ret);
  56. return ret;
  57. }
  58. /* Setup REG_CP_RB_CNTL: */
  59. adreno_gpu_write(adreno_gpu, REG_ADRENO_CP_RB_CNTL,
  60. /* size is log2(quad-words): */
  61. AXXX_CP_RB_CNTL_BUFSZ(ilog2(gpu->rb->size / 8)) |
  62. AXXX_CP_RB_CNTL_BLKSZ(ilog2(RB_BLKSIZE / 8)));
  63. /* Setup ringbuffer address: */
  64. adreno_gpu_write(adreno_gpu, REG_ADRENO_CP_RB_BASE, gpu->rb_iova);
  65. adreno_gpu_write(adreno_gpu, REG_ADRENO_CP_RB_RPTR_ADDR,
  66. rbmemptr(adreno_gpu, rptr));
  67. /* Setup scratch/timestamp: */
  68. adreno_gpu_write(adreno_gpu, REG_ADRENO_SCRATCH_ADDR,
  69. rbmemptr(adreno_gpu, fence));
  70. adreno_gpu_write(adreno_gpu, REG_ADRENO_SCRATCH_UMSK, 0x1);
  71. return 0;
  72. }
  73. static uint32_t get_wptr(struct msm_ringbuffer *ring)
  74. {
  75. return ring->cur - ring->start;
  76. }
  77. uint32_t adreno_last_fence(struct msm_gpu *gpu)
  78. {
  79. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  80. return adreno_gpu->memptrs->fence;
  81. }
  82. void adreno_recover(struct msm_gpu *gpu)
  83. {
  84. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  85. struct drm_device *dev = gpu->dev;
  86. int ret;
  87. gpu->funcs->pm_suspend(gpu);
  88. /* reset ringbuffer: */
  89. gpu->rb->cur = gpu->rb->start;
  90. /* reset completed fence seqno, just discard anything pending: */
  91. adreno_gpu->memptrs->fence = gpu->submitted_fence;
  92. adreno_gpu->memptrs->rptr = 0;
  93. adreno_gpu->memptrs->wptr = 0;
  94. gpu->funcs->pm_resume(gpu);
  95. ret = gpu->funcs->hw_init(gpu);
  96. if (ret) {
  97. dev_err(dev->dev, "gpu hw init failed: %d\n", ret);
  98. /* hmm, oh well? */
  99. }
  100. }
  101. int adreno_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit,
  102. struct msm_file_private *ctx)
  103. {
  104. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  105. struct msm_drm_private *priv = gpu->dev->dev_private;
  106. struct msm_ringbuffer *ring = gpu->rb;
  107. unsigned i, ibs = 0;
  108. for (i = 0; i < submit->nr_cmds; i++) {
  109. switch (submit->cmd[i].type) {
  110. case MSM_SUBMIT_CMD_IB_TARGET_BUF:
  111. /* ignore IB-targets */
  112. break;
  113. case MSM_SUBMIT_CMD_CTX_RESTORE_BUF:
  114. /* ignore if there has not been a ctx switch: */
  115. if (priv->lastctx == ctx)
  116. break;
  117. case MSM_SUBMIT_CMD_BUF:
  118. OUT_PKT3(ring, CP_INDIRECT_BUFFER_PFD, 2);
  119. OUT_RING(ring, submit->cmd[i].iova);
  120. OUT_RING(ring, submit->cmd[i].size);
  121. ibs++;
  122. break;
  123. }
  124. }
  125. /* on a320, at least, we seem to need to pad things out to an
  126. * even number of qwords to avoid issue w/ CP hanging on wrap-
  127. * around:
  128. */
  129. if (ibs % 2)
  130. OUT_PKT2(ring);
  131. OUT_PKT0(ring, REG_AXXX_CP_SCRATCH_REG2, 1);
  132. OUT_RING(ring, submit->fence);
  133. if (adreno_is_a3xx(adreno_gpu) || adreno_is_a4xx(adreno_gpu)) {
  134. /* Flush HLSQ lazy updates to make sure there is nothing
  135. * pending for indirect loads after the timestamp has
  136. * passed:
  137. */
  138. OUT_PKT3(ring, CP_EVENT_WRITE, 1);
  139. OUT_RING(ring, HLSQ_FLUSH);
  140. OUT_PKT3(ring, CP_WAIT_FOR_IDLE, 1);
  141. OUT_RING(ring, 0x00000000);
  142. }
  143. OUT_PKT3(ring, CP_EVENT_WRITE, 3);
  144. OUT_RING(ring, CACHE_FLUSH_TS);
  145. OUT_RING(ring, rbmemptr(adreno_gpu, fence));
  146. OUT_RING(ring, submit->fence);
  147. /* we could maybe be clever and only CP_COND_EXEC the interrupt: */
  148. OUT_PKT3(ring, CP_INTERRUPT, 1);
  149. OUT_RING(ring, 0x80000000);
  150. /* Workaround for missing irq issue on 8x16/a306. Unsure if the
  151. * root cause is a platform issue or some a306 quirk, but this
  152. * keeps things humming along:
  153. */
  154. if (adreno_is_a306(adreno_gpu)) {
  155. OUT_PKT3(ring, CP_WAIT_FOR_IDLE, 1);
  156. OUT_RING(ring, 0x00000000);
  157. OUT_PKT3(ring, CP_INTERRUPT, 1);
  158. OUT_RING(ring, 0x80000000);
  159. }
  160. #if 0
  161. if (adreno_is_a3xx(adreno_gpu)) {
  162. /* Dummy set-constant to trigger context rollover */
  163. OUT_PKT3(ring, CP_SET_CONSTANT, 2);
  164. OUT_RING(ring, CP_REG(REG_A3XX_HLSQ_CL_KERNEL_GROUP_X_REG));
  165. OUT_RING(ring, 0x00000000);
  166. }
  167. #endif
  168. gpu->funcs->flush(gpu);
  169. return 0;
  170. }
  171. void adreno_flush(struct msm_gpu *gpu)
  172. {
  173. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  174. uint32_t wptr;
  175. /*
  176. * Mask wptr value that we calculate to fit in the HW range. This is
  177. * to account for the possibility that the last command fit exactly into
  178. * the ringbuffer and rb->next hasn't wrapped to zero yet
  179. */
  180. wptr = get_wptr(gpu->rb) & ((gpu->rb->size / 4) - 1);
  181. /* ensure writes to ringbuffer have hit system memory: */
  182. mb();
  183. adreno_gpu_write(adreno_gpu, REG_ADRENO_CP_RB_WPTR, wptr);
  184. }
  185. void adreno_idle(struct msm_gpu *gpu)
  186. {
  187. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  188. uint32_t wptr = get_wptr(gpu->rb);
  189. /* wait for CP to drain ringbuffer: */
  190. if (spin_until(adreno_gpu->memptrs->rptr == wptr))
  191. DRM_ERROR("%s: timeout waiting to drain ringbuffer!\n", gpu->name);
  192. /* TODO maybe we need to reset GPU here to recover from hang? */
  193. }
  194. #ifdef CONFIG_DEBUG_FS
  195. void adreno_show(struct msm_gpu *gpu, struct seq_file *m)
  196. {
  197. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  198. int i;
  199. seq_printf(m, "revision: %d (%d.%d.%d.%d)\n",
  200. adreno_gpu->info->revn, adreno_gpu->rev.core,
  201. adreno_gpu->rev.major, adreno_gpu->rev.minor,
  202. adreno_gpu->rev.patchid);
  203. seq_printf(m, "fence: %d/%d\n", adreno_gpu->memptrs->fence,
  204. gpu->submitted_fence);
  205. seq_printf(m, "rptr: %d\n", adreno_gpu->memptrs->rptr);
  206. seq_printf(m, "wptr: %d\n", adreno_gpu->memptrs->wptr);
  207. seq_printf(m, "rb wptr: %d\n", get_wptr(gpu->rb));
  208. gpu->funcs->pm_resume(gpu);
  209. /* dump these out in a form that can be parsed by demsm: */
  210. seq_printf(m, "IO:region %s 00000000 00020000\n", gpu->name);
  211. for (i = 0; adreno_gpu->registers[i] != ~0; i += 2) {
  212. uint32_t start = adreno_gpu->registers[i];
  213. uint32_t end = adreno_gpu->registers[i+1];
  214. uint32_t addr;
  215. for (addr = start; addr <= end; addr++) {
  216. uint32_t val = gpu_read(gpu, addr);
  217. seq_printf(m, "IO:R %08x %08x\n", addr<<2, val);
  218. }
  219. }
  220. gpu->funcs->pm_suspend(gpu);
  221. }
  222. #endif
  223. /* Dump common gpu status and scratch registers on any hang, to make
  224. * the hangcheck logs more useful. The scratch registers seem always
  225. * safe to read when GPU has hung (unlike some other regs, depending
  226. * on how the GPU hung), and they are useful to match up to cmdstream
  227. * dumps when debugging hangs:
  228. */
  229. void adreno_dump_info(struct msm_gpu *gpu)
  230. {
  231. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  232. int i;
  233. printk("revision: %d (%d.%d.%d.%d)\n",
  234. adreno_gpu->info->revn, adreno_gpu->rev.core,
  235. adreno_gpu->rev.major, adreno_gpu->rev.minor,
  236. adreno_gpu->rev.patchid);
  237. printk("fence: %d/%d\n", adreno_gpu->memptrs->fence,
  238. gpu->submitted_fence);
  239. printk("rptr: %d\n", adreno_gpu->memptrs->rptr);
  240. printk("wptr: %d\n", adreno_gpu->memptrs->wptr);
  241. printk("rb wptr: %d\n", get_wptr(gpu->rb));
  242. for (i = 0; i < 8; i++) {
  243. printk("CP_SCRATCH_REG%d: %u\n", i,
  244. gpu_read(gpu, REG_AXXX_CP_SCRATCH_REG0 + i));
  245. }
  246. }
  247. /* would be nice to not have to duplicate the _show() stuff with printk(): */
  248. void adreno_dump(struct msm_gpu *gpu)
  249. {
  250. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  251. int i;
  252. /* dump these out in a form that can be parsed by demsm: */
  253. printk("IO:region %s 00000000 00020000\n", gpu->name);
  254. for (i = 0; adreno_gpu->registers[i] != ~0; i += 2) {
  255. uint32_t start = adreno_gpu->registers[i];
  256. uint32_t end = adreno_gpu->registers[i+1];
  257. uint32_t addr;
  258. for (addr = start; addr <= end; addr++) {
  259. uint32_t val = gpu_read(gpu, addr);
  260. printk("IO:R %08x %08x\n", addr<<2, val);
  261. }
  262. }
  263. }
  264. static uint32_t ring_freewords(struct msm_gpu *gpu)
  265. {
  266. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  267. uint32_t size = gpu->rb->size / 4;
  268. uint32_t wptr = get_wptr(gpu->rb);
  269. uint32_t rptr = adreno_gpu->memptrs->rptr;
  270. return (rptr + (size - 1) - wptr) % size;
  271. }
  272. void adreno_wait_ring(struct msm_gpu *gpu, uint32_t ndwords)
  273. {
  274. if (spin_until(ring_freewords(gpu) >= ndwords))
  275. DRM_ERROR("%s: timeout waiting for ringbuffer space\n", gpu->name);
  276. }
  277. static const char *iommu_ports[] = {
  278. "gfx3d_user", "gfx3d_priv",
  279. "gfx3d1_user", "gfx3d1_priv",
  280. };
  281. int adreno_gpu_init(struct drm_device *drm, struct platform_device *pdev,
  282. struct adreno_gpu *adreno_gpu, const struct adreno_gpu_funcs *funcs)
  283. {
  284. struct adreno_platform_config *config = pdev->dev.platform_data;
  285. struct msm_gpu *gpu = &adreno_gpu->base;
  286. struct msm_mmu *mmu;
  287. int ret;
  288. adreno_gpu->funcs = funcs;
  289. adreno_gpu->info = adreno_info(config->rev);
  290. adreno_gpu->gmem = adreno_gpu->info->gmem;
  291. adreno_gpu->revn = adreno_gpu->info->revn;
  292. adreno_gpu->rev = config->rev;
  293. gpu->fast_rate = config->fast_rate;
  294. gpu->slow_rate = config->slow_rate;
  295. gpu->bus_freq = config->bus_freq;
  296. #ifdef DOWNSTREAM_CONFIG_MSM_BUS_SCALING
  297. gpu->bus_scale_table = config->bus_scale_table;
  298. #endif
  299. DBG("fast_rate=%u, slow_rate=%u, bus_freq=%u",
  300. gpu->fast_rate, gpu->slow_rate, gpu->bus_freq);
  301. ret = msm_gpu_init(drm, pdev, &adreno_gpu->base, &funcs->base,
  302. adreno_gpu->info->name, "kgsl_3d0_reg_memory", "kgsl_3d0_irq",
  303. RB_SIZE);
  304. if (ret)
  305. return ret;
  306. ret = request_firmware(&adreno_gpu->pm4, adreno_gpu->info->pm4fw, drm->dev);
  307. if (ret) {
  308. dev_err(drm->dev, "failed to load %s PM4 firmware: %d\n",
  309. adreno_gpu->info->pm4fw, ret);
  310. return ret;
  311. }
  312. ret = request_firmware(&adreno_gpu->pfp, adreno_gpu->info->pfpfw, drm->dev);
  313. if (ret) {
  314. dev_err(drm->dev, "failed to load %s PFP firmware: %d\n",
  315. adreno_gpu->info->pfpfw, ret);
  316. return ret;
  317. }
  318. mmu = gpu->mmu;
  319. if (mmu) {
  320. ret = mmu->funcs->attach(mmu, iommu_ports,
  321. ARRAY_SIZE(iommu_ports));
  322. if (ret)
  323. return ret;
  324. }
  325. mutex_lock(&drm->struct_mutex);
  326. adreno_gpu->memptrs_bo = msm_gem_new(drm, sizeof(*adreno_gpu->memptrs),
  327. MSM_BO_UNCACHED);
  328. mutex_unlock(&drm->struct_mutex);
  329. if (IS_ERR(adreno_gpu->memptrs_bo)) {
  330. ret = PTR_ERR(adreno_gpu->memptrs_bo);
  331. adreno_gpu->memptrs_bo = NULL;
  332. dev_err(drm->dev, "could not allocate memptrs: %d\n", ret);
  333. return ret;
  334. }
  335. adreno_gpu->memptrs = msm_gem_vaddr(adreno_gpu->memptrs_bo);
  336. if (!adreno_gpu->memptrs) {
  337. dev_err(drm->dev, "could not vmap memptrs\n");
  338. return -ENOMEM;
  339. }
  340. ret = msm_gem_get_iova(adreno_gpu->memptrs_bo, gpu->id,
  341. &adreno_gpu->memptrs_iova);
  342. if (ret) {
  343. dev_err(drm->dev, "could not map memptrs: %d\n", ret);
  344. return ret;
  345. }
  346. return 0;
  347. }
  348. void adreno_gpu_cleanup(struct adreno_gpu *gpu)
  349. {
  350. if (gpu->memptrs_bo) {
  351. if (gpu->memptrs_iova)
  352. msm_gem_put_iova(gpu->memptrs_bo, gpu->base.id);
  353. drm_gem_object_unreference_unlocked(gpu->memptrs_bo);
  354. }
  355. release_firmware(gpu->pm4);
  356. release_firmware(gpu->pfp);
  357. msm_gpu_cleanup(&gpu->base);
  358. }