a6xx_gpu.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2017-2019 The Linux Foundation. All rights reserved. */
  3. #include "msm_gem.h"
  4. #include "msm_mmu.h"
  5. #include "msm_gpu_trace.h"
  6. #include "a6xx_gpu.h"
  7. #include "a6xx_gmu.xml.h"
  8. #include <linux/bitfield.h>
  9. #include <linux/devfreq.h>
  10. #include <linux/firmware/qcom/qcom_scm.h>
  11. #include <linux/pm_domain.h>
  12. #include <linux/soc/qcom/llcc-qcom.h>
  13. #define GPU_PAS_ID 13
  14. static inline bool _a6xx_check_idle(struct msm_gpu *gpu)
  15. {
  16. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  17. struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
  18. /* Check that the GMU is idle */
  19. if (!adreno_has_gmu_wrapper(adreno_gpu) && !a6xx_gmu_isidle(&a6xx_gpu->gmu))
  20. return false;
  21. /* Check tha the CX master is idle */
  22. if (gpu_read(gpu, REG_A6XX_RBBM_STATUS) &
  23. ~A6XX_RBBM_STATUS_CP_AHB_BUSY_CX_MASTER)
  24. return false;
  25. return !(gpu_read(gpu, REG_A6XX_RBBM_INT_0_STATUS) &
  26. A6XX_RBBM_INT_0_MASK_RBBM_HANG_DETECT);
  27. }
  28. static bool a6xx_idle(struct msm_gpu *gpu, struct msm_ringbuffer *ring)
  29. {
  30. /* wait for CP to drain ringbuffer: */
  31. if (!adreno_idle(gpu, ring))
  32. return false;
  33. if (spin_until(_a6xx_check_idle(gpu))) {
  34. DRM_ERROR("%s: %ps: timeout waiting for GPU to idle: status %8.8X irq %8.8X rptr/wptr %d/%d\n",
  35. gpu->name, __builtin_return_address(0),
  36. gpu_read(gpu, REG_A6XX_RBBM_STATUS),
  37. gpu_read(gpu, REG_A6XX_RBBM_INT_0_STATUS),
  38. gpu_read(gpu, REG_A6XX_CP_RB_RPTR),
  39. gpu_read(gpu, REG_A6XX_CP_RB_WPTR));
  40. return false;
  41. }
  42. return true;
  43. }
  44. static void update_shadow_rptr(struct msm_gpu *gpu, struct msm_ringbuffer *ring)
  45. {
  46. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  47. struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
  48. /* Expanded APRIV doesn't need to issue the WHERE_AM_I opcode */
  49. if (a6xx_gpu->has_whereami && !adreno_gpu->base.hw_apriv) {
  50. OUT_PKT7(ring, CP_WHERE_AM_I, 2);
  51. OUT_RING(ring, lower_32_bits(shadowptr(a6xx_gpu, ring)));
  52. OUT_RING(ring, upper_32_bits(shadowptr(a6xx_gpu, ring)));
  53. }
  54. }
  55. static void a6xx_flush(struct msm_gpu *gpu, struct msm_ringbuffer *ring)
  56. {
  57. uint32_t wptr;
  58. unsigned long flags;
  59. update_shadow_rptr(gpu, ring);
  60. spin_lock_irqsave(&ring->preempt_lock, flags);
  61. /* Copy the shadow to the actual register */
  62. ring->cur = ring->next;
  63. /* Make sure to wrap wptr if we need to */
  64. wptr = get_wptr(ring);
  65. spin_unlock_irqrestore(&ring->preempt_lock, flags);
  66. /* Make sure everything is posted before making a decision */
  67. mb();
  68. gpu_write(gpu, REG_A6XX_CP_RB_WPTR, wptr);
  69. }
  70. static void get_stats_counter(struct msm_ringbuffer *ring, u32 counter,
  71. u64 iova)
  72. {
  73. OUT_PKT7(ring, CP_REG_TO_MEM, 3);
  74. OUT_RING(ring, CP_REG_TO_MEM_0_REG(counter) |
  75. CP_REG_TO_MEM_0_CNT(2) |
  76. CP_REG_TO_MEM_0_64B);
  77. OUT_RING(ring, lower_32_bits(iova));
  78. OUT_RING(ring, upper_32_bits(iova));
  79. }
  80. static void a6xx_set_pagetable(struct a6xx_gpu *a6xx_gpu,
  81. struct msm_ringbuffer *ring, struct msm_gem_submit *submit)
  82. {
  83. bool sysprof = refcount_read(&a6xx_gpu->base.base.sysprof_active) > 1;
  84. struct msm_file_private *ctx = submit->queue->ctx;
  85. struct adreno_gpu *adreno_gpu = &a6xx_gpu->base;
  86. phys_addr_t ttbr;
  87. u32 asid;
  88. u64 memptr = rbmemptr(ring, ttbr0);
  89. if (ctx->seqno == a6xx_gpu->base.base.cur_ctx_seqno)
  90. return;
  91. if (msm_iommu_pagetable_params(ctx->aspace->mmu, &ttbr, &asid))
  92. return;
  93. if (adreno_gpu->info->family >= ADRENO_7XX_GEN1) {
  94. /* Wait for previous submit to complete before continuing: */
  95. OUT_PKT7(ring, CP_WAIT_TIMESTAMP, 4);
  96. OUT_RING(ring, 0);
  97. OUT_RING(ring, lower_32_bits(rbmemptr(ring, fence)));
  98. OUT_RING(ring, upper_32_bits(rbmemptr(ring, fence)));
  99. OUT_RING(ring, submit->seqno - 1);
  100. OUT_PKT7(ring, CP_THREAD_CONTROL, 1);
  101. OUT_RING(ring, CP_SET_THREAD_BOTH);
  102. /* Reset state used to synchronize BR and BV */
  103. OUT_PKT7(ring, CP_RESET_CONTEXT_STATE, 1);
  104. OUT_RING(ring,
  105. CP_RESET_CONTEXT_STATE_0_CLEAR_ON_CHIP_TS |
  106. CP_RESET_CONTEXT_STATE_0_CLEAR_RESOURCE_TABLE |
  107. CP_RESET_CONTEXT_STATE_0_CLEAR_BV_BR_COUNTER |
  108. CP_RESET_CONTEXT_STATE_0_RESET_GLOBAL_LOCAL_TS);
  109. OUT_PKT7(ring, CP_THREAD_CONTROL, 1);
  110. OUT_RING(ring, CP_SET_THREAD_BR);
  111. }
  112. if (!sysprof) {
  113. if (!adreno_is_a7xx(adreno_gpu)) {
  114. /* Turn off protected mode to write to special registers */
  115. OUT_PKT7(ring, CP_SET_PROTECTED_MODE, 1);
  116. OUT_RING(ring, 0);
  117. }
  118. OUT_PKT4(ring, REG_A6XX_RBBM_PERFCTR_SRAM_INIT_CMD, 1);
  119. OUT_RING(ring, 1);
  120. }
  121. /* Execute the table update */
  122. OUT_PKT7(ring, CP_SMMU_TABLE_UPDATE, 4);
  123. OUT_RING(ring, CP_SMMU_TABLE_UPDATE_0_TTBR0_LO(lower_32_bits(ttbr)));
  124. OUT_RING(ring,
  125. CP_SMMU_TABLE_UPDATE_1_TTBR0_HI(upper_32_bits(ttbr)) |
  126. CP_SMMU_TABLE_UPDATE_1_ASID(asid));
  127. OUT_RING(ring, CP_SMMU_TABLE_UPDATE_2_CONTEXTIDR(0));
  128. OUT_RING(ring, CP_SMMU_TABLE_UPDATE_3_CONTEXTBANK(0));
  129. /*
  130. * Write the new TTBR0 to the memstore. This is good for debugging.
  131. */
  132. OUT_PKT7(ring, CP_MEM_WRITE, 4);
  133. OUT_RING(ring, CP_MEM_WRITE_0_ADDR_LO(lower_32_bits(memptr)));
  134. OUT_RING(ring, CP_MEM_WRITE_1_ADDR_HI(upper_32_bits(memptr)));
  135. OUT_RING(ring, lower_32_bits(ttbr));
  136. OUT_RING(ring, (asid << 16) | upper_32_bits(ttbr));
  137. /*
  138. * Sync both threads after switching pagetables and enable BR only
  139. * to make sure BV doesn't race ahead while BR is still switching
  140. * pagetables.
  141. */
  142. if (adreno_is_a7xx(&a6xx_gpu->base)) {
  143. OUT_PKT7(ring, CP_THREAD_CONTROL, 1);
  144. OUT_RING(ring, CP_THREAD_CONTROL_0_SYNC_THREADS | CP_SET_THREAD_BR);
  145. }
  146. /*
  147. * And finally, trigger a uche flush to be sure there isn't anything
  148. * lingering in that part of the GPU
  149. */
  150. OUT_PKT7(ring, CP_EVENT_WRITE, 1);
  151. OUT_RING(ring, CACHE_INVALIDATE);
  152. if (!sysprof) {
  153. /*
  154. * Wait for SRAM clear after the pgtable update, so the
  155. * two can happen in parallel:
  156. */
  157. OUT_PKT7(ring, CP_WAIT_REG_MEM, 6);
  158. OUT_RING(ring, CP_WAIT_REG_MEM_0_FUNCTION(WRITE_EQ));
  159. OUT_RING(ring, CP_WAIT_REG_MEM_1_POLL_ADDR_LO(
  160. REG_A6XX_RBBM_PERFCTR_SRAM_INIT_STATUS));
  161. OUT_RING(ring, CP_WAIT_REG_MEM_2_POLL_ADDR_HI(0));
  162. OUT_RING(ring, CP_WAIT_REG_MEM_3_REF(0x1));
  163. OUT_RING(ring, CP_WAIT_REG_MEM_4_MASK(0x1));
  164. OUT_RING(ring, CP_WAIT_REG_MEM_5_DELAY_LOOP_CYCLES(0));
  165. if (!adreno_is_a7xx(adreno_gpu)) {
  166. /* Re-enable protected mode: */
  167. OUT_PKT7(ring, CP_SET_PROTECTED_MODE, 1);
  168. OUT_RING(ring, 1);
  169. }
  170. }
  171. }
  172. static void a6xx_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit)
  173. {
  174. unsigned int index = submit->seqno % MSM_GPU_SUBMIT_STATS_COUNT;
  175. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  176. struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
  177. struct msm_ringbuffer *ring = submit->ring;
  178. unsigned int i, ibs = 0;
  179. a6xx_set_pagetable(a6xx_gpu, ring, submit);
  180. get_stats_counter(ring, REG_A6XX_RBBM_PERFCTR_CP(0),
  181. rbmemptr_stats(ring, index, cpcycles_start));
  182. /*
  183. * For PM4 the GMU register offsets are calculated from the base of the
  184. * GPU registers so we need to add 0x1a800 to the register value on A630
  185. * to get the right value from PM4.
  186. */
  187. get_stats_counter(ring, REG_A6XX_CP_ALWAYS_ON_COUNTER,
  188. rbmemptr_stats(ring, index, alwayson_start));
  189. /* Invalidate CCU depth and color */
  190. OUT_PKT7(ring, CP_EVENT_WRITE, 1);
  191. OUT_RING(ring, CP_EVENT_WRITE_0_EVENT(PC_CCU_INVALIDATE_DEPTH));
  192. OUT_PKT7(ring, CP_EVENT_WRITE, 1);
  193. OUT_RING(ring, CP_EVENT_WRITE_0_EVENT(PC_CCU_INVALIDATE_COLOR));
  194. /* Submit the commands */
  195. for (i = 0; i < submit->nr_cmds; i++) {
  196. switch (submit->cmd[i].type) {
  197. case MSM_SUBMIT_CMD_IB_TARGET_BUF:
  198. break;
  199. case MSM_SUBMIT_CMD_CTX_RESTORE_BUF:
  200. if (gpu->cur_ctx_seqno == submit->queue->ctx->seqno)
  201. break;
  202. fallthrough;
  203. case MSM_SUBMIT_CMD_BUF:
  204. OUT_PKT7(ring, CP_INDIRECT_BUFFER, 3);
  205. OUT_RING(ring, lower_32_bits(submit->cmd[i].iova));
  206. OUT_RING(ring, upper_32_bits(submit->cmd[i].iova));
  207. OUT_RING(ring, A5XX_CP_INDIRECT_BUFFER_2_IB_SIZE(submit->cmd[i].size));
  208. ibs++;
  209. break;
  210. }
  211. /*
  212. * Periodically update shadow-wptr if needed, so that we
  213. * can see partial progress of submits with large # of
  214. * cmds.. otherwise we could needlessly stall waiting for
  215. * ringbuffer state, simply due to looking at a shadow
  216. * rptr value that has not been updated
  217. */
  218. if ((ibs % 32) == 0)
  219. update_shadow_rptr(gpu, ring);
  220. }
  221. get_stats_counter(ring, REG_A6XX_RBBM_PERFCTR_CP(0),
  222. rbmemptr_stats(ring, index, cpcycles_end));
  223. get_stats_counter(ring, REG_A6XX_CP_ALWAYS_ON_COUNTER,
  224. rbmemptr_stats(ring, index, alwayson_end));
  225. /* Write the fence to the scratch register */
  226. OUT_PKT4(ring, REG_A6XX_CP_SCRATCH_REG(2), 1);
  227. OUT_RING(ring, submit->seqno);
  228. /*
  229. * Execute a CACHE_FLUSH_TS event. This will ensure that the
  230. * timestamp is written to the memory and then triggers the interrupt
  231. */
  232. OUT_PKT7(ring, CP_EVENT_WRITE, 4);
  233. OUT_RING(ring, CP_EVENT_WRITE_0_EVENT(CACHE_FLUSH_TS) |
  234. CP_EVENT_WRITE_0_IRQ);
  235. OUT_RING(ring, lower_32_bits(rbmemptr(ring, fence)));
  236. OUT_RING(ring, upper_32_bits(rbmemptr(ring, fence)));
  237. OUT_RING(ring, submit->seqno);
  238. trace_msm_gpu_submit_flush(submit,
  239. gpu_read64(gpu, REG_A6XX_CP_ALWAYS_ON_COUNTER));
  240. a6xx_flush(gpu, ring);
  241. }
  242. static void a7xx_submit(struct msm_gpu *gpu, struct msm_gem_submit *submit)
  243. {
  244. unsigned int index = submit->seqno % MSM_GPU_SUBMIT_STATS_COUNT;
  245. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  246. struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
  247. struct msm_ringbuffer *ring = submit->ring;
  248. unsigned int i, ibs = 0;
  249. /*
  250. * Toggle concurrent binning for pagetable switch and set the thread to
  251. * BR since only it can execute the pagetable switch packets.
  252. */
  253. OUT_PKT7(ring, CP_THREAD_CONTROL, 1);
  254. OUT_RING(ring, CP_THREAD_CONTROL_0_SYNC_THREADS | CP_SET_THREAD_BR);
  255. a6xx_set_pagetable(a6xx_gpu, ring, submit);
  256. get_stats_counter(ring, REG_A7XX_RBBM_PERFCTR_CP(0),
  257. rbmemptr_stats(ring, index, cpcycles_start));
  258. get_stats_counter(ring, REG_A6XX_CP_ALWAYS_ON_COUNTER,
  259. rbmemptr_stats(ring, index, alwayson_start));
  260. OUT_PKT7(ring, CP_THREAD_CONTROL, 1);
  261. OUT_RING(ring, CP_SET_THREAD_BOTH);
  262. OUT_PKT7(ring, CP_SET_MARKER, 1);
  263. OUT_RING(ring, 0x101); /* IFPC disable */
  264. OUT_PKT7(ring, CP_SET_MARKER, 1);
  265. OUT_RING(ring, 0x00d); /* IB1LIST start */
  266. /* Submit the commands */
  267. for (i = 0; i < submit->nr_cmds; i++) {
  268. switch (submit->cmd[i].type) {
  269. case MSM_SUBMIT_CMD_IB_TARGET_BUF:
  270. break;
  271. case MSM_SUBMIT_CMD_CTX_RESTORE_BUF:
  272. if (gpu->cur_ctx_seqno == submit->queue->ctx->seqno)
  273. break;
  274. fallthrough;
  275. case MSM_SUBMIT_CMD_BUF:
  276. OUT_PKT7(ring, CP_INDIRECT_BUFFER, 3);
  277. OUT_RING(ring, lower_32_bits(submit->cmd[i].iova));
  278. OUT_RING(ring, upper_32_bits(submit->cmd[i].iova));
  279. OUT_RING(ring, A5XX_CP_INDIRECT_BUFFER_2_IB_SIZE(submit->cmd[i].size));
  280. ibs++;
  281. break;
  282. }
  283. /*
  284. * Periodically update shadow-wptr if needed, so that we
  285. * can see partial progress of submits with large # of
  286. * cmds.. otherwise we could needlessly stall waiting for
  287. * ringbuffer state, simply due to looking at a shadow
  288. * rptr value that has not been updated
  289. */
  290. if ((ibs % 32) == 0)
  291. update_shadow_rptr(gpu, ring);
  292. }
  293. OUT_PKT7(ring, CP_SET_MARKER, 1);
  294. OUT_RING(ring, 0x00e); /* IB1LIST end */
  295. get_stats_counter(ring, REG_A7XX_RBBM_PERFCTR_CP(0),
  296. rbmemptr_stats(ring, index, cpcycles_end));
  297. get_stats_counter(ring, REG_A6XX_CP_ALWAYS_ON_COUNTER,
  298. rbmemptr_stats(ring, index, alwayson_end));
  299. /* Write the fence to the scratch register */
  300. OUT_PKT4(ring, REG_A6XX_CP_SCRATCH_REG(2), 1);
  301. OUT_RING(ring, submit->seqno);
  302. OUT_PKT7(ring, CP_THREAD_CONTROL, 1);
  303. OUT_RING(ring, CP_SET_THREAD_BR);
  304. OUT_PKT7(ring, CP_EVENT_WRITE, 1);
  305. OUT_RING(ring, CCU_INVALIDATE_DEPTH);
  306. OUT_PKT7(ring, CP_EVENT_WRITE, 1);
  307. OUT_RING(ring, CCU_INVALIDATE_COLOR);
  308. OUT_PKT7(ring, CP_THREAD_CONTROL, 1);
  309. OUT_RING(ring, CP_SET_THREAD_BV);
  310. /*
  311. * Make sure the timestamp is committed once BV pipe is
  312. * completely done with this submission.
  313. */
  314. OUT_PKT7(ring, CP_EVENT_WRITE, 4);
  315. OUT_RING(ring, CACHE_CLEAN | BIT(27));
  316. OUT_RING(ring, lower_32_bits(rbmemptr(ring, bv_fence)));
  317. OUT_RING(ring, upper_32_bits(rbmemptr(ring, bv_fence)));
  318. OUT_RING(ring, submit->seqno);
  319. OUT_PKT7(ring, CP_THREAD_CONTROL, 1);
  320. OUT_RING(ring, CP_SET_THREAD_BR);
  321. /*
  322. * This makes sure that BR doesn't race ahead and commit
  323. * timestamp to memstore while BV is still processing
  324. * this submission.
  325. */
  326. OUT_PKT7(ring, CP_WAIT_TIMESTAMP, 4);
  327. OUT_RING(ring, 0);
  328. OUT_RING(ring, lower_32_bits(rbmemptr(ring, bv_fence)));
  329. OUT_RING(ring, upper_32_bits(rbmemptr(ring, bv_fence)));
  330. OUT_RING(ring, submit->seqno);
  331. /* write the ringbuffer timestamp */
  332. OUT_PKT7(ring, CP_EVENT_WRITE, 4);
  333. OUT_RING(ring, CACHE_CLEAN | CP_EVENT_WRITE_0_IRQ | BIT(27));
  334. OUT_RING(ring, lower_32_bits(rbmemptr(ring, fence)));
  335. OUT_RING(ring, upper_32_bits(rbmemptr(ring, fence)));
  336. OUT_RING(ring, submit->seqno);
  337. OUT_PKT7(ring, CP_THREAD_CONTROL, 1);
  338. OUT_RING(ring, CP_SET_THREAD_BOTH);
  339. OUT_PKT7(ring, CP_SET_MARKER, 1);
  340. OUT_RING(ring, 0x100); /* IFPC enable */
  341. trace_msm_gpu_submit_flush(submit,
  342. gpu_read64(gpu, REG_A6XX_CP_ALWAYS_ON_COUNTER));
  343. a6xx_flush(gpu, ring);
  344. }
  345. static void a6xx_set_hwcg(struct msm_gpu *gpu, bool state)
  346. {
  347. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  348. struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
  349. struct a6xx_gmu *gmu = &a6xx_gpu->gmu;
  350. const struct adreno_reglist *reg;
  351. unsigned int i;
  352. u32 cgc_delay, cgc_hyst;
  353. u32 val, clock_cntl_on;
  354. if (!(adreno_gpu->info->a6xx->hwcg || adreno_is_a7xx(adreno_gpu)))
  355. return;
  356. if (adreno_is_a630(adreno_gpu))
  357. clock_cntl_on = 0x8aa8aa02;
  358. else if (adreno_is_a610(adreno_gpu))
  359. clock_cntl_on = 0xaaa8aa82;
  360. else if (adreno_is_a702(adreno_gpu))
  361. clock_cntl_on = 0xaaaaaa82;
  362. else
  363. clock_cntl_on = 0x8aa8aa82;
  364. cgc_delay = adreno_is_a615_family(adreno_gpu) ? 0x111 : 0x10111;
  365. cgc_hyst = adreno_is_a615_family(adreno_gpu) ? 0x555 : 0x5555;
  366. gmu_write(&a6xx_gpu->gmu, REG_A6XX_GPU_GMU_AO_GMU_CGC_MODE_CNTL,
  367. state ? adreno_gpu->info->a6xx->gmu_cgc_mode : 0);
  368. gmu_write(&a6xx_gpu->gmu, REG_A6XX_GPU_GMU_AO_GMU_CGC_DELAY_CNTL,
  369. state ? cgc_delay : 0);
  370. gmu_write(&a6xx_gpu->gmu, REG_A6XX_GPU_GMU_AO_GMU_CGC_HYST_CNTL,
  371. state ? cgc_hyst : 0);
  372. if (!adreno_gpu->info->a6xx->hwcg) {
  373. gpu_write(gpu, REG_A7XX_RBBM_CLOCK_CNTL_GLOBAL, 1);
  374. gpu_write(gpu, REG_A7XX_RBBM_CGC_GLOBAL_LOAD_CMD, state ? 1 : 0);
  375. if (state) {
  376. gpu_write(gpu, REG_A7XX_RBBM_CGC_P2S_TRIG_CMD, 1);
  377. if (gpu_poll_timeout(gpu, REG_A7XX_RBBM_CGC_P2S_STATUS, val,
  378. val & A7XX_RBBM_CGC_P2S_STATUS_TXDONE, 1, 10)) {
  379. dev_err(&gpu->pdev->dev, "RBBM_CGC_P2S_STATUS TXDONE Poll failed\n");
  380. return;
  381. }
  382. gpu_write(gpu, REG_A7XX_RBBM_CLOCK_CNTL_GLOBAL, 0);
  383. }
  384. return;
  385. }
  386. val = gpu_read(gpu, REG_A6XX_RBBM_CLOCK_CNTL);
  387. /* Don't re-program the registers if they are already correct */
  388. if ((!state && !val) || (state && (val == clock_cntl_on)))
  389. return;
  390. /* Disable SP clock before programming HWCG registers */
  391. if (!adreno_is_a610_family(adreno_gpu) && !adreno_is_a7xx(adreno_gpu))
  392. gmu_rmw(gmu, REG_A6XX_GPU_GMU_GX_SPTPRAC_CLOCK_CONTROL, 1, 0);
  393. for (i = 0; (reg = &adreno_gpu->info->a6xx->hwcg[i], reg->offset); i++)
  394. gpu_write(gpu, reg->offset, state ? reg->value : 0);
  395. /* Enable SP clock */
  396. if (!adreno_is_a610_family(adreno_gpu) && !adreno_is_a7xx(adreno_gpu))
  397. gmu_rmw(gmu, REG_A6XX_GPU_GMU_GX_SPTPRAC_CLOCK_CONTROL, 0, 1);
  398. gpu_write(gpu, REG_A6XX_RBBM_CLOCK_CNTL, state ? clock_cntl_on : 0);
  399. }
  400. static void a6xx_set_cp_protect(struct msm_gpu *gpu)
  401. {
  402. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  403. const struct adreno_protect *protect = adreno_gpu->info->a6xx->protect;
  404. unsigned i;
  405. /*
  406. * Enable access protection to privileged registers, fault on an access
  407. * protect violation and select the last span to protect from the start
  408. * address all the way to the end of the register address space
  409. */
  410. gpu_write(gpu, REG_A6XX_CP_PROTECT_CNTL,
  411. A6XX_CP_PROTECT_CNTL_ACCESS_PROT_EN |
  412. A6XX_CP_PROTECT_CNTL_ACCESS_FAULT_ON_VIOL_EN |
  413. A6XX_CP_PROTECT_CNTL_LAST_SPAN_INF_RANGE);
  414. for (i = 0; i < protect->count - 1; i++) {
  415. /* Intentionally skip writing to some registers */
  416. if (protect->regs[i])
  417. gpu_write(gpu, REG_A6XX_CP_PROTECT(i), protect->regs[i]);
  418. }
  419. /* last CP_PROTECT to have "infinite" length on the last entry */
  420. gpu_write(gpu, REG_A6XX_CP_PROTECT(protect->count_max - 1), protect->regs[i]);
  421. }
  422. static void a6xx_calc_ubwc_config(struct adreno_gpu *gpu)
  423. {
  424. gpu->ubwc_config.rgb565_predicator = 0;
  425. gpu->ubwc_config.uavflagprd_inv = 0;
  426. gpu->ubwc_config.min_acc_len = 0;
  427. gpu->ubwc_config.ubwc_swizzle = 0x6;
  428. gpu->ubwc_config.macrotile_mode = 0;
  429. gpu->ubwc_config.highest_bank_bit = 15;
  430. if (adreno_is_a610(gpu)) {
  431. gpu->ubwc_config.highest_bank_bit = 13;
  432. gpu->ubwc_config.min_acc_len = 1;
  433. gpu->ubwc_config.ubwc_swizzle = 0x7;
  434. }
  435. if (adreno_is_a618(gpu))
  436. gpu->ubwc_config.highest_bank_bit = 14;
  437. if (adreno_is_a619(gpu))
  438. /* TODO: Should be 14 but causes corruption at e.g. 1920x1200 on DP */
  439. gpu->ubwc_config.highest_bank_bit = 13;
  440. if (adreno_is_a619_holi(gpu))
  441. gpu->ubwc_config.highest_bank_bit = 13;
  442. if (adreno_is_a621(gpu)) {
  443. gpu->ubwc_config.highest_bank_bit = 13;
  444. gpu->ubwc_config.amsbc = 1;
  445. gpu->ubwc_config.uavflagprd_inv = 2;
  446. }
  447. if (adreno_is_a640_family(gpu))
  448. gpu->ubwc_config.amsbc = 1;
  449. if (adreno_is_a680(gpu))
  450. gpu->ubwc_config.macrotile_mode = 1;
  451. if (adreno_is_a650(gpu) ||
  452. adreno_is_a660(gpu) ||
  453. adreno_is_a690(gpu) ||
  454. adreno_is_a730(gpu) ||
  455. adreno_is_a740_family(gpu)) {
  456. /* TODO: get ddr type from bootloader and use 2 for LPDDR4 */
  457. gpu->ubwc_config.highest_bank_bit = 16;
  458. gpu->ubwc_config.amsbc = 1;
  459. gpu->ubwc_config.rgb565_predicator = 1;
  460. gpu->ubwc_config.uavflagprd_inv = 2;
  461. gpu->ubwc_config.macrotile_mode = 1;
  462. }
  463. if (adreno_is_7c3(gpu)) {
  464. gpu->ubwc_config.highest_bank_bit = 14;
  465. gpu->ubwc_config.amsbc = 1;
  466. gpu->ubwc_config.uavflagprd_inv = 2;
  467. gpu->ubwc_config.macrotile_mode = 1;
  468. }
  469. if (adreno_is_a702(gpu)) {
  470. gpu->ubwc_config.highest_bank_bit = 14;
  471. gpu->ubwc_config.min_acc_len = 1;
  472. }
  473. }
  474. static void a6xx_set_ubwc_config(struct msm_gpu *gpu)
  475. {
  476. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  477. /*
  478. * We subtract 13 from the highest bank bit (13 is the minimum value
  479. * allowed by hw) and write the lowest two bits of the remaining value
  480. * as hbb_lo and the one above it as hbb_hi to the hardware.
  481. */
  482. BUG_ON(adreno_gpu->ubwc_config.highest_bank_bit < 13);
  483. u32 hbb = adreno_gpu->ubwc_config.highest_bank_bit - 13;
  484. u32 hbb_hi = hbb >> 2;
  485. u32 hbb_lo = hbb & 3;
  486. u32 ubwc_mode = adreno_gpu->ubwc_config.ubwc_swizzle & 1;
  487. u32 level2_swizzling_dis = !(adreno_gpu->ubwc_config.ubwc_swizzle & 2);
  488. gpu_write(gpu, REG_A6XX_RB_NC_MODE_CNTL,
  489. level2_swizzling_dis << 12 |
  490. adreno_gpu->ubwc_config.rgb565_predicator << 11 |
  491. hbb_hi << 10 | adreno_gpu->ubwc_config.amsbc << 4 |
  492. adreno_gpu->ubwc_config.min_acc_len << 3 |
  493. hbb_lo << 1 | ubwc_mode);
  494. gpu_write(gpu, REG_A6XX_TPL1_NC_MODE_CNTL,
  495. level2_swizzling_dis << 6 | hbb_hi << 4 |
  496. adreno_gpu->ubwc_config.min_acc_len << 3 |
  497. hbb_lo << 1 | ubwc_mode);
  498. gpu_write(gpu, REG_A6XX_SP_NC_MODE_CNTL,
  499. level2_swizzling_dis << 12 | hbb_hi << 10 |
  500. adreno_gpu->ubwc_config.uavflagprd_inv << 4 |
  501. adreno_gpu->ubwc_config.min_acc_len << 3 |
  502. hbb_lo << 1 | ubwc_mode);
  503. if (adreno_is_a7xx(adreno_gpu))
  504. gpu_write(gpu, REG_A7XX_GRAS_NC_MODE_CNTL,
  505. FIELD_PREP(GENMASK(8, 5), hbb_lo));
  506. gpu_write(gpu, REG_A6XX_UCHE_MODE_CNTL,
  507. adreno_gpu->ubwc_config.min_acc_len << 23 | hbb_lo << 21);
  508. gpu_write(gpu, REG_A6XX_RBBM_NC_MODE_CNTL,
  509. adreno_gpu->ubwc_config.macrotile_mode);
  510. }
  511. static int a6xx_cp_init(struct msm_gpu *gpu)
  512. {
  513. struct msm_ringbuffer *ring = gpu->rb[0];
  514. OUT_PKT7(ring, CP_ME_INIT, 8);
  515. OUT_RING(ring, 0x0000002f);
  516. /* Enable multiple hardware contexts */
  517. OUT_RING(ring, 0x00000003);
  518. /* Enable error detection */
  519. OUT_RING(ring, 0x20000000);
  520. /* Don't enable header dump */
  521. OUT_RING(ring, 0x00000000);
  522. OUT_RING(ring, 0x00000000);
  523. /* No workarounds enabled */
  524. OUT_RING(ring, 0x00000000);
  525. /* Pad rest of the cmds with 0's */
  526. OUT_RING(ring, 0x00000000);
  527. OUT_RING(ring, 0x00000000);
  528. a6xx_flush(gpu, ring);
  529. return a6xx_idle(gpu, ring) ? 0 : -EINVAL;
  530. }
  531. static int a7xx_cp_init(struct msm_gpu *gpu)
  532. {
  533. struct msm_ringbuffer *ring = gpu->rb[0];
  534. u32 mask;
  535. /* Disable concurrent binning before sending CP init */
  536. OUT_PKT7(ring, CP_THREAD_CONTROL, 1);
  537. OUT_RING(ring, BIT(27));
  538. OUT_PKT7(ring, CP_ME_INIT, 7);
  539. /* Use multiple HW contexts */
  540. mask = BIT(0);
  541. /* Enable error detection */
  542. mask |= BIT(1);
  543. /* Set default reset state */
  544. mask |= BIT(3);
  545. /* Disable save/restore of performance counters across preemption */
  546. mask |= BIT(6);
  547. /* Enable the register init list with the spinlock */
  548. mask |= BIT(8);
  549. OUT_RING(ring, mask);
  550. /* Enable multiple hardware contexts */
  551. OUT_RING(ring, 0x00000003);
  552. /* Enable error detection */
  553. OUT_RING(ring, 0x20000000);
  554. /* Operation mode mask */
  555. OUT_RING(ring, 0x00000002);
  556. /* *Don't* send a power up reg list for concurrent binning (TODO) */
  557. /* Lo address */
  558. OUT_RING(ring, 0x00000000);
  559. /* Hi address */
  560. OUT_RING(ring, 0x00000000);
  561. /* BIT(31) set => read the regs from the list */
  562. OUT_RING(ring, 0x00000000);
  563. a6xx_flush(gpu, ring);
  564. return a6xx_idle(gpu, ring) ? 0 : -EINVAL;
  565. }
  566. /*
  567. * Check that the microcode version is new enough to include several key
  568. * security fixes. Return true if the ucode is safe.
  569. */
  570. static bool a6xx_ucode_check_version(struct a6xx_gpu *a6xx_gpu,
  571. struct drm_gem_object *obj)
  572. {
  573. struct adreno_gpu *adreno_gpu = &a6xx_gpu->base;
  574. struct msm_gpu *gpu = &adreno_gpu->base;
  575. const char *sqe_name = adreno_gpu->info->fw[ADRENO_FW_SQE];
  576. u32 *buf = msm_gem_get_vaddr(obj);
  577. bool ret = false;
  578. if (IS_ERR(buf))
  579. return false;
  580. /* A7xx is safe! */
  581. if (adreno_is_a7xx(adreno_gpu) || adreno_is_a702(adreno_gpu))
  582. return true;
  583. /*
  584. * Targets up to a640 (a618, a630 and a640) need to check for a
  585. * microcode version that is patched to support the whereami opcode or
  586. * one that is new enough to include it by default.
  587. *
  588. * a650 tier targets don't need whereami but still need to be
  589. * equal to or newer than 0.95 for other security fixes
  590. *
  591. * a660 targets have all the critical security fixes from the start
  592. */
  593. if (!strcmp(sqe_name, "a630_sqe.fw")) {
  594. /*
  595. * If the lowest nibble is 0xa that is an indication that this
  596. * microcode has been patched. The actual version is in dword
  597. * [3] but we only care about the patchlevel which is the lowest
  598. * nibble of dword [3]
  599. *
  600. * Otherwise check that the firmware is greater than or equal
  601. * to 1.90 which was the first version that had this fix built
  602. * in
  603. */
  604. if ((((buf[0] & 0xf) == 0xa) && (buf[2] & 0xf) >= 1) ||
  605. (buf[0] & 0xfff) >= 0x190) {
  606. a6xx_gpu->has_whereami = true;
  607. ret = true;
  608. goto out;
  609. }
  610. DRM_DEV_ERROR(&gpu->pdev->dev,
  611. "a630 SQE ucode is too old. Have version %x need at least %x\n",
  612. buf[0] & 0xfff, 0x190);
  613. } else if (!strcmp(sqe_name, "a650_sqe.fw")) {
  614. if ((buf[0] & 0xfff) >= 0x095) {
  615. ret = true;
  616. goto out;
  617. }
  618. DRM_DEV_ERROR(&gpu->pdev->dev,
  619. "a650 SQE ucode is too old. Have version %x need at least %x\n",
  620. buf[0] & 0xfff, 0x095);
  621. } else if (!strcmp(sqe_name, "a660_sqe.fw")) {
  622. ret = true;
  623. } else {
  624. DRM_DEV_ERROR(&gpu->pdev->dev,
  625. "unknown GPU, add it to a6xx_ucode_check_version()!!\n");
  626. }
  627. out:
  628. msm_gem_put_vaddr(obj);
  629. return ret;
  630. }
  631. static int a6xx_ucode_load(struct msm_gpu *gpu)
  632. {
  633. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  634. struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
  635. if (!a6xx_gpu->sqe_bo) {
  636. a6xx_gpu->sqe_bo = adreno_fw_create_bo(gpu,
  637. adreno_gpu->fw[ADRENO_FW_SQE], &a6xx_gpu->sqe_iova);
  638. if (IS_ERR(a6xx_gpu->sqe_bo)) {
  639. int ret = PTR_ERR(a6xx_gpu->sqe_bo);
  640. a6xx_gpu->sqe_bo = NULL;
  641. DRM_DEV_ERROR(&gpu->pdev->dev,
  642. "Could not allocate SQE ucode: %d\n", ret);
  643. return ret;
  644. }
  645. msm_gem_object_set_name(a6xx_gpu->sqe_bo, "sqefw");
  646. if (!a6xx_ucode_check_version(a6xx_gpu, a6xx_gpu->sqe_bo)) {
  647. msm_gem_unpin_iova(a6xx_gpu->sqe_bo, gpu->aspace);
  648. drm_gem_object_put(a6xx_gpu->sqe_bo);
  649. a6xx_gpu->sqe_bo = NULL;
  650. return -EPERM;
  651. }
  652. }
  653. /*
  654. * Expanded APRIV and targets that support WHERE_AM_I both need a
  655. * privileged buffer to store the RPTR shadow
  656. */
  657. if ((adreno_gpu->base.hw_apriv || a6xx_gpu->has_whereami) &&
  658. !a6xx_gpu->shadow_bo) {
  659. a6xx_gpu->shadow = msm_gem_kernel_new(gpu->dev,
  660. sizeof(u32) * gpu->nr_rings,
  661. MSM_BO_WC | MSM_BO_MAP_PRIV,
  662. gpu->aspace, &a6xx_gpu->shadow_bo,
  663. &a6xx_gpu->shadow_iova);
  664. if (IS_ERR(a6xx_gpu->shadow))
  665. return PTR_ERR(a6xx_gpu->shadow);
  666. msm_gem_object_set_name(a6xx_gpu->shadow_bo, "shadow");
  667. }
  668. return 0;
  669. }
  670. static int a6xx_zap_shader_init(struct msm_gpu *gpu)
  671. {
  672. static bool loaded;
  673. int ret;
  674. if (loaded)
  675. return 0;
  676. ret = adreno_zap_shader_load(gpu, GPU_PAS_ID);
  677. loaded = !ret;
  678. return ret;
  679. }
  680. #define A6XX_INT_MASK (A6XX_RBBM_INT_0_MASK_CP_AHB_ERROR | \
  681. A6XX_RBBM_INT_0_MASK_RBBM_ATB_ASYNCFIFO_OVERFLOW | \
  682. A6XX_RBBM_INT_0_MASK_CP_HW_ERROR | \
  683. A6XX_RBBM_INT_0_MASK_CP_IB2 | \
  684. A6XX_RBBM_INT_0_MASK_CP_IB1 | \
  685. A6XX_RBBM_INT_0_MASK_CP_RB | \
  686. A6XX_RBBM_INT_0_MASK_CP_CACHE_FLUSH_TS | \
  687. A6XX_RBBM_INT_0_MASK_RBBM_ATB_BUS_OVERFLOW | \
  688. A6XX_RBBM_INT_0_MASK_RBBM_HANG_DETECT | \
  689. A6XX_RBBM_INT_0_MASK_UCHE_OOB_ACCESS | \
  690. A6XX_RBBM_INT_0_MASK_UCHE_TRAP_INTR)
  691. #define A7XX_INT_MASK (A6XX_RBBM_INT_0_MASK_CP_AHB_ERROR | \
  692. A6XX_RBBM_INT_0_MASK_RBBM_ATB_ASYNCFIFO_OVERFLOW | \
  693. A6XX_RBBM_INT_0_MASK_RBBM_GPC_ERROR | \
  694. A6XX_RBBM_INT_0_MASK_CP_SW | \
  695. A6XX_RBBM_INT_0_MASK_CP_HW_ERROR | \
  696. A6XX_RBBM_INT_0_MASK_PM4CPINTERRUPT | \
  697. A6XX_RBBM_INT_0_MASK_CP_RB_DONE_TS | \
  698. A6XX_RBBM_INT_0_MASK_CP_CACHE_FLUSH_TS | \
  699. A6XX_RBBM_INT_0_MASK_RBBM_ATB_BUS_OVERFLOW | \
  700. A6XX_RBBM_INT_0_MASK_RBBM_HANG_DETECT | \
  701. A6XX_RBBM_INT_0_MASK_UCHE_OOB_ACCESS | \
  702. A6XX_RBBM_INT_0_MASK_UCHE_TRAP_INTR | \
  703. A6XX_RBBM_INT_0_MASK_TSBWRITEERROR | \
  704. A6XX_RBBM_INT_0_MASK_SWFUSEVIOLATION)
  705. #define A7XX_APRIV_MASK (A6XX_CP_APRIV_CNTL_ICACHE | \
  706. A6XX_CP_APRIV_CNTL_RBFETCH | \
  707. A6XX_CP_APRIV_CNTL_RBPRIVLEVEL | \
  708. A6XX_CP_APRIV_CNTL_RBRPWB)
  709. #define A7XX_BR_APRIVMASK (A7XX_APRIV_MASK | \
  710. A6XX_CP_APRIV_CNTL_CDREAD | \
  711. A6XX_CP_APRIV_CNTL_CDWRITE)
  712. static int hw_init(struct msm_gpu *gpu)
  713. {
  714. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  715. struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
  716. struct a6xx_gmu *gmu = &a6xx_gpu->gmu;
  717. u64 gmem_range_min;
  718. int ret;
  719. if (!adreno_has_gmu_wrapper(adreno_gpu)) {
  720. /* Make sure the GMU keeps the GPU on while we set it up */
  721. ret = a6xx_gmu_set_oob(&a6xx_gpu->gmu, GMU_OOB_GPU_SET);
  722. if (ret)
  723. return ret;
  724. }
  725. /* Clear GBIF halt in case GX domain was not collapsed */
  726. if (adreno_is_a619_holi(adreno_gpu)) {
  727. gpu_write(gpu, REG_A6XX_GBIF_HALT, 0);
  728. gpu_read(gpu, REG_A6XX_GBIF_HALT);
  729. gpu_write(gpu, REG_A6XX_RBBM_GPR0_CNTL, 0);
  730. gpu_read(gpu, REG_A6XX_RBBM_GPR0_CNTL);
  731. } else if (a6xx_has_gbif(adreno_gpu)) {
  732. gpu_write(gpu, REG_A6XX_GBIF_HALT, 0);
  733. gpu_read(gpu, REG_A6XX_GBIF_HALT);
  734. gpu_write(gpu, REG_A6XX_RBBM_GBIF_HALT, 0);
  735. gpu_read(gpu, REG_A6XX_RBBM_GBIF_HALT);
  736. }
  737. gpu_write(gpu, REG_A6XX_RBBM_SECVID_TSB_CNTL, 0);
  738. if (adreno_is_a619_holi(adreno_gpu))
  739. a6xx_sptprac_enable(gmu);
  740. /*
  741. * Disable the trusted memory range - we don't actually supported secure
  742. * memory rendering at this point in time and we don't want to block off
  743. * part of the virtual memory space.
  744. */
  745. gpu_write64(gpu, REG_A6XX_RBBM_SECVID_TSB_TRUSTED_BASE, 0x00000000);
  746. gpu_write(gpu, REG_A6XX_RBBM_SECVID_TSB_TRUSTED_SIZE, 0x00000000);
  747. if (!adreno_is_a7xx(adreno_gpu)) {
  748. /* Turn on 64 bit addressing for all blocks */
  749. gpu_write(gpu, REG_A6XX_CP_ADDR_MODE_CNTL, 0x1);
  750. gpu_write(gpu, REG_A6XX_VSC_ADDR_MODE_CNTL, 0x1);
  751. gpu_write(gpu, REG_A6XX_GRAS_ADDR_MODE_CNTL, 0x1);
  752. gpu_write(gpu, REG_A6XX_RB_ADDR_MODE_CNTL, 0x1);
  753. gpu_write(gpu, REG_A6XX_PC_ADDR_MODE_CNTL, 0x1);
  754. gpu_write(gpu, REG_A6XX_HLSQ_ADDR_MODE_CNTL, 0x1);
  755. gpu_write(gpu, REG_A6XX_VFD_ADDR_MODE_CNTL, 0x1);
  756. gpu_write(gpu, REG_A6XX_VPC_ADDR_MODE_CNTL, 0x1);
  757. gpu_write(gpu, REG_A6XX_UCHE_ADDR_MODE_CNTL, 0x1);
  758. gpu_write(gpu, REG_A6XX_SP_ADDR_MODE_CNTL, 0x1);
  759. gpu_write(gpu, REG_A6XX_TPL1_ADDR_MODE_CNTL, 0x1);
  760. gpu_write(gpu, REG_A6XX_RBBM_SECVID_TSB_ADDR_MODE_CNTL, 0x1);
  761. }
  762. /* enable hardware clockgating */
  763. a6xx_set_hwcg(gpu, true);
  764. /* VBIF/GBIF start*/
  765. if (adreno_is_a610_family(adreno_gpu) ||
  766. adreno_is_a640_family(adreno_gpu) ||
  767. adreno_is_a650_family(adreno_gpu) ||
  768. adreno_is_a7xx(adreno_gpu)) {
  769. gpu_write(gpu, REG_A6XX_GBIF_QSB_SIDE0, 0x00071620);
  770. gpu_write(gpu, REG_A6XX_GBIF_QSB_SIDE1, 0x00071620);
  771. gpu_write(gpu, REG_A6XX_GBIF_QSB_SIDE2, 0x00071620);
  772. gpu_write(gpu, REG_A6XX_GBIF_QSB_SIDE3, 0x00071620);
  773. gpu_write(gpu, REG_A6XX_RBBM_GBIF_CLIENT_QOS_CNTL,
  774. adreno_is_a7xx(adreno_gpu) ? 0x2120212 : 0x3);
  775. } else {
  776. gpu_write(gpu, REG_A6XX_RBBM_VBIF_CLIENT_QOS_CNTL, 0x3);
  777. }
  778. if (adreno_is_a630(adreno_gpu))
  779. gpu_write(gpu, REG_A6XX_VBIF_GATE_OFF_WRREQ_EN, 0x00000009);
  780. if (adreno_is_a7xx(adreno_gpu))
  781. gpu_write(gpu, REG_A6XX_UCHE_GBIF_GX_CONFIG, 0x10240e0);
  782. /* Make all blocks contribute to the GPU BUSY perf counter */
  783. gpu_write(gpu, REG_A6XX_RBBM_PERFCTR_GPU_BUSY_MASKED, 0xffffffff);
  784. /* Disable L2 bypass in the UCHE */
  785. if (adreno_is_a7xx(adreno_gpu)) {
  786. gpu_write64(gpu, REG_A6XX_UCHE_TRAP_BASE, 0x0001fffffffff000llu);
  787. gpu_write64(gpu, REG_A6XX_UCHE_WRITE_THRU_BASE, 0x0001fffffffff000llu);
  788. } else {
  789. gpu_write64(gpu, REG_A6XX_UCHE_WRITE_RANGE_MAX, 0x0001ffffffffffc0llu);
  790. gpu_write64(gpu, REG_A6XX_UCHE_TRAP_BASE, 0x0001fffffffff000llu);
  791. gpu_write64(gpu, REG_A6XX_UCHE_WRITE_THRU_BASE, 0x0001fffffffff000llu);
  792. }
  793. if (!(adreno_is_a650_family(adreno_gpu) ||
  794. adreno_is_a702(adreno_gpu) ||
  795. adreno_is_a730(adreno_gpu))) {
  796. gmem_range_min = adreno_is_a740_family(adreno_gpu) ? SZ_16M : SZ_1M;
  797. /* Set the GMEM VA range [0x100000:0x100000 + gpu->gmem - 1] */
  798. gpu_write64(gpu, REG_A6XX_UCHE_GMEM_RANGE_MIN, gmem_range_min);
  799. gpu_write64(gpu, REG_A6XX_UCHE_GMEM_RANGE_MAX,
  800. gmem_range_min + adreno_gpu->info->gmem - 1);
  801. }
  802. if (adreno_is_a7xx(adreno_gpu))
  803. gpu_write(gpu, REG_A6XX_UCHE_CACHE_WAYS, BIT(23));
  804. else {
  805. gpu_write(gpu, REG_A6XX_UCHE_FILTER_CNTL, 0x804);
  806. gpu_write(gpu, REG_A6XX_UCHE_CACHE_WAYS, 0x4);
  807. }
  808. if (adreno_is_a640_family(adreno_gpu) || adreno_is_a650_family(adreno_gpu)) {
  809. gpu_write(gpu, REG_A6XX_CP_ROQ_THRESHOLDS_2, 0x02000140);
  810. gpu_write(gpu, REG_A6XX_CP_ROQ_THRESHOLDS_1, 0x8040362c);
  811. } else if (adreno_is_a610_family(adreno_gpu)) {
  812. gpu_write(gpu, REG_A6XX_CP_ROQ_THRESHOLDS_2, 0x00800060);
  813. gpu_write(gpu, REG_A6XX_CP_ROQ_THRESHOLDS_1, 0x40201b16);
  814. } else if (!adreno_is_a7xx(adreno_gpu)) {
  815. gpu_write(gpu, REG_A6XX_CP_ROQ_THRESHOLDS_2, 0x010000c0);
  816. gpu_write(gpu, REG_A6XX_CP_ROQ_THRESHOLDS_1, 0x8040362c);
  817. }
  818. if (adreno_is_a660_family(adreno_gpu))
  819. gpu_write(gpu, REG_A6XX_CP_LPAC_PROG_FIFO_SIZE, 0x00000020);
  820. /* Setting the mem pool size */
  821. if (adreno_is_a610(adreno_gpu)) {
  822. gpu_write(gpu, REG_A6XX_CP_MEM_POOL_SIZE, 48);
  823. gpu_write(gpu, REG_A6XX_CP_MEM_POOL_DBG_ADDR, 47);
  824. } else if (adreno_is_a702(adreno_gpu)) {
  825. gpu_write(gpu, REG_A6XX_CP_MEM_POOL_SIZE, 64);
  826. gpu_write(gpu, REG_A6XX_CP_MEM_POOL_DBG_ADDR, 63);
  827. } else if (!adreno_is_a7xx(adreno_gpu))
  828. gpu_write(gpu, REG_A6XX_CP_MEM_POOL_SIZE, 128);
  829. /* Set the default primFifo threshold values */
  830. if (adreno_gpu->info->a6xx->prim_fifo_threshold)
  831. gpu_write(gpu, REG_A6XX_PC_DBG_ECO_CNTL,
  832. adreno_gpu->info->a6xx->prim_fifo_threshold);
  833. /* Set the AHB default slave response to "ERROR" */
  834. gpu_write(gpu, REG_A6XX_CP_AHB_CNTL, 0x1);
  835. /* Turn on performance counters */
  836. gpu_write(gpu, REG_A6XX_RBBM_PERFCTR_CNTL, 0x1);
  837. if (adreno_is_a7xx(adreno_gpu)) {
  838. /* Turn on the IFPC counter (countable 4 on XOCLK4) */
  839. gmu_write(&a6xx_gpu->gmu, REG_A6XX_GMU_CX_GMU_POWER_COUNTER_SELECT_1,
  840. FIELD_PREP(GENMASK(7, 0), 0x4));
  841. }
  842. /* Select CP0 to always count cycles */
  843. gpu_write(gpu, REG_A6XX_CP_PERFCTR_CP_SEL(0), PERF_CP_ALWAYS_COUNT);
  844. a6xx_set_ubwc_config(gpu);
  845. /* Enable fault detection */
  846. if (adreno_is_a730(adreno_gpu) ||
  847. adreno_is_a740_family(adreno_gpu))
  848. gpu_write(gpu, REG_A6XX_RBBM_INTERFACE_HANG_INT_CNTL, (1 << 30) | 0xcfffff);
  849. else if (adreno_is_a690(adreno_gpu))
  850. gpu_write(gpu, REG_A6XX_RBBM_INTERFACE_HANG_INT_CNTL, (1 << 30) | 0x4fffff);
  851. else if (adreno_is_a619(adreno_gpu))
  852. gpu_write(gpu, REG_A6XX_RBBM_INTERFACE_HANG_INT_CNTL, (1 << 30) | 0x3fffff);
  853. else if (adreno_is_a610(adreno_gpu) || adreno_is_a702(adreno_gpu))
  854. gpu_write(gpu, REG_A6XX_RBBM_INTERFACE_HANG_INT_CNTL, (1 << 30) | 0x3ffff);
  855. else
  856. gpu_write(gpu, REG_A6XX_RBBM_INTERFACE_HANG_INT_CNTL, (1 << 30) | 0x1fffff);
  857. gpu_write(gpu, REG_A6XX_UCHE_CLIENT_PF, BIT(7) | 0x1);
  858. /* Set weights for bicubic filtering */
  859. if (adreno_is_a650_family(adreno_gpu) || adreno_is_x185(adreno_gpu)) {
  860. gpu_write(gpu, REG_A6XX_TPL1_BICUBIC_WEIGHTS_TABLE_0, 0);
  861. gpu_write(gpu, REG_A6XX_TPL1_BICUBIC_WEIGHTS_TABLE_1,
  862. 0x3fe05ff4);
  863. gpu_write(gpu, REG_A6XX_TPL1_BICUBIC_WEIGHTS_TABLE_2,
  864. 0x3fa0ebee);
  865. gpu_write(gpu, REG_A6XX_TPL1_BICUBIC_WEIGHTS_TABLE_3,
  866. 0x3f5193ed);
  867. gpu_write(gpu, REG_A6XX_TPL1_BICUBIC_WEIGHTS_TABLE_4,
  868. 0x3f0243f0);
  869. }
  870. /* Set up the CX GMU counter 0 to count busy ticks */
  871. gmu_write(gmu, REG_A6XX_GPU_GMU_AO_GPU_CX_BUSY_MASK, 0xff000000);
  872. /* Enable the power counter */
  873. gmu_rmw(gmu, REG_A6XX_GMU_CX_GMU_POWER_COUNTER_SELECT_0, 0xff, BIT(5));
  874. gmu_write(gmu, REG_A6XX_GMU_CX_GMU_POWER_COUNTER_ENABLE, 1);
  875. /* Protect registers from the CP */
  876. a6xx_set_cp_protect(gpu);
  877. if (adreno_is_a660_family(adreno_gpu)) {
  878. if (adreno_is_a690(adreno_gpu))
  879. gpu_write(gpu, REG_A6XX_CP_CHICKEN_DBG, 0x00028801);
  880. else
  881. gpu_write(gpu, REG_A6XX_CP_CHICKEN_DBG, 0x1);
  882. gpu_write(gpu, REG_A6XX_RBBM_GBIF_CLIENT_QOS_CNTL, 0x0);
  883. } else if (adreno_is_a702(adreno_gpu)) {
  884. /* Something to do with the HLSQ cluster */
  885. gpu_write(gpu, REG_A6XX_CP_CHICKEN_DBG, BIT(24));
  886. }
  887. if (adreno_is_a690(adreno_gpu))
  888. gpu_write(gpu, REG_A6XX_UCHE_CMDQ_CONFIG, 0x90);
  889. /* Set dualQ + disable afull for A660 GPU */
  890. else if (adreno_is_a660(adreno_gpu))
  891. gpu_write(gpu, REG_A6XX_UCHE_CMDQ_CONFIG, 0x66906);
  892. else if (adreno_is_a7xx(adreno_gpu))
  893. gpu_write(gpu, REG_A6XX_UCHE_CMDQ_CONFIG,
  894. FIELD_PREP(GENMASK(19, 16), 6) |
  895. FIELD_PREP(GENMASK(15, 12), 6) |
  896. FIELD_PREP(GENMASK(11, 8), 9) |
  897. BIT(3) | BIT(2) |
  898. FIELD_PREP(GENMASK(1, 0), 2));
  899. /* Enable expanded apriv for targets that support it */
  900. if (gpu->hw_apriv) {
  901. if (adreno_is_a7xx(adreno_gpu)) {
  902. gpu_write(gpu, REG_A6XX_CP_APRIV_CNTL,
  903. A7XX_BR_APRIVMASK);
  904. gpu_write(gpu, REG_A7XX_CP_BV_APRIV_CNTL,
  905. A7XX_APRIV_MASK);
  906. gpu_write(gpu, REG_A7XX_CP_LPAC_APRIV_CNTL,
  907. A7XX_APRIV_MASK);
  908. } else
  909. gpu_write(gpu, REG_A6XX_CP_APRIV_CNTL,
  910. BIT(6) | BIT(5) | BIT(3) | BIT(2) | BIT(1));
  911. }
  912. if (adreno_is_a750(adreno_gpu)) {
  913. /* Disable ubwc merged UFC request feature */
  914. gpu_rmw(gpu, REG_A6XX_RB_CMP_DBG_ECO_CNTL, BIT(19), BIT(19));
  915. /* Enable TP flaghint and other performance settings */
  916. gpu_write(gpu, REG_A6XX_TPL1_DBG_ECO_CNTL1, 0xc0700);
  917. } else if (adreno_is_a7xx(adreno_gpu)) {
  918. /* Disable non-ubwc read reqs from passing write reqs */
  919. gpu_rmw(gpu, REG_A6XX_RB_CMP_DBG_ECO_CNTL, BIT(11), BIT(11));
  920. }
  921. /* Enable interrupts */
  922. gpu_write(gpu, REG_A6XX_RBBM_INT_0_MASK,
  923. adreno_is_a7xx(adreno_gpu) ? A7XX_INT_MASK : A6XX_INT_MASK);
  924. ret = adreno_hw_init(gpu);
  925. if (ret)
  926. goto out;
  927. gpu_write64(gpu, REG_A6XX_CP_SQE_INSTR_BASE, a6xx_gpu->sqe_iova);
  928. /* Set the ringbuffer address */
  929. gpu_write64(gpu, REG_A6XX_CP_RB_BASE, gpu->rb[0]->iova);
  930. /* Targets that support extended APRIV can use the RPTR shadow from
  931. * hardware but all the other ones need to disable the feature. Targets
  932. * that support the WHERE_AM_I opcode can use that instead
  933. */
  934. if (adreno_gpu->base.hw_apriv)
  935. gpu_write(gpu, REG_A6XX_CP_RB_CNTL, MSM_GPU_RB_CNTL_DEFAULT);
  936. else
  937. gpu_write(gpu, REG_A6XX_CP_RB_CNTL,
  938. MSM_GPU_RB_CNTL_DEFAULT | AXXX_CP_RB_CNTL_NO_UPDATE);
  939. /* Configure the RPTR shadow if needed: */
  940. if (a6xx_gpu->shadow_bo) {
  941. gpu_write64(gpu, REG_A6XX_CP_RB_RPTR_ADDR,
  942. shadowptr(a6xx_gpu, gpu->rb[0]));
  943. }
  944. /* ..which means "always" on A7xx, also for BV shadow */
  945. if (adreno_is_a7xx(adreno_gpu)) {
  946. gpu_write64(gpu, REG_A7XX_CP_BV_RB_RPTR_ADDR,
  947. rbmemptr(gpu->rb[0], bv_fence));
  948. }
  949. /* Always come up on rb 0 */
  950. a6xx_gpu->cur_ring = gpu->rb[0];
  951. gpu->cur_ctx_seqno = 0;
  952. /* Enable the SQE_to start the CP engine */
  953. gpu_write(gpu, REG_A6XX_CP_SQE_CNTL, 1);
  954. ret = adreno_is_a7xx(adreno_gpu) ? a7xx_cp_init(gpu) : a6xx_cp_init(gpu);
  955. if (ret)
  956. goto out;
  957. /*
  958. * Try to load a zap shader into the secure world. If successful
  959. * we can use the CP to switch out of secure mode. If not then we
  960. * have no resource but to try to switch ourselves out manually. If we
  961. * guessed wrong then access to the RBBM_SECVID_TRUST_CNTL register will
  962. * be blocked and a permissions violation will soon follow.
  963. */
  964. ret = a6xx_zap_shader_init(gpu);
  965. if (!ret) {
  966. OUT_PKT7(gpu->rb[0], CP_SET_SECURE_MODE, 1);
  967. OUT_RING(gpu->rb[0], 0x00000000);
  968. a6xx_flush(gpu, gpu->rb[0]);
  969. if (!a6xx_idle(gpu, gpu->rb[0]))
  970. return -EINVAL;
  971. } else if (ret == -ENODEV) {
  972. /*
  973. * This device does not use zap shader (but print a warning
  974. * just in case someone got their dt wrong.. hopefully they
  975. * have a debug UART to realize the error of their ways...
  976. * if you mess this up you are about to crash horribly)
  977. */
  978. dev_warn_once(gpu->dev->dev,
  979. "Zap shader not enabled - using SECVID_TRUST_CNTL instead\n");
  980. gpu_write(gpu, REG_A6XX_RBBM_SECVID_TRUST_CNTL, 0x0);
  981. ret = 0;
  982. } else {
  983. return ret;
  984. }
  985. out:
  986. if (adreno_has_gmu_wrapper(adreno_gpu))
  987. return ret;
  988. /*
  989. * Tell the GMU that we are done touching the GPU and it can start power
  990. * management
  991. */
  992. a6xx_gmu_clear_oob(&a6xx_gpu->gmu, GMU_OOB_GPU_SET);
  993. if (a6xx_gpu->gmu.legacy) {
  994. /* Take the GMU out of its special boot mode */
  995. a6xx_gmu_clear_oob(&a6xx_gpu->gmu, GMU_OOB_BOOT_SLUMBER);
  996. }
  997. return ret;
  998. }
  999. static int a6xx_hw_init(struct msm_gpu *gpu)
  1000. {
  1001. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  1002. struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
  1003. int ret;
  1004. mutex_lock(&a6xx_gpu->gmu.lock);
  1005. ret = hw_init(gpu);
  1006. mutex_unlock(&a6xx_gpu->gmu.lock);
  1007. return ret;
  1008. }
  1009. static void a6xx_dump(struct msm_gpu *gpu)
  1010. {
  1011. DRM_DEV_INFO(&gpu->pdev->dev, "status: %08x\n",
  1012. gpu_read(gpu, REG_A6XX_RBBM_STATUS));
  1013. adreno_dump(gpu);
  1014. }
  1015. static void a6xx_recover(struct msm_gpu *gpu)
  1016. {
  1017. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  1018. struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
  1019. struct a6xx_gmu *gmu = &a6xx_gpu->gmu;
  1020. int i, active_submits;
  1021. adreno_dump_info(gpu);
  1022. for (i = 0; i < 8; i++)
  1023. DRM_DEV_INFO(&gpu->pdev->dev, "CP_SCRATCH_REG%d: %u\n", i,
  1024. gpu_read(gpu, REG_A6XX_CP_SCRATCH_REG(i)));
  1025. if (hang_debug)
  1026. a6xx_dump(gpu);
  1027. /*
  1028. * To handle recovery specific sequences during the rpm suspend we are
  1029. * about to trigger
  1030. */
  1031. a6xx_gpu->hung = true;
  1032. /* Halt SQE first */
  1033. gpu_write(gpu, REG_A6XX_CP_SQE_CNTL, 3);
  1034. pm_runtime_dont_use_autosuspend(&gpu->pdev->dev);
  1035. /* active_submit won't change until we make a submission */
  1036. mutex_lock(&gpu->active_lock);
  1037. active_submits = gpu->active_submits;
  1038. /*
  1039. * Temporarily clear active_submits count to silence a WARN() in the
  1040. * runtime suspend cb
  1041. */
  1042. gpu->active_submits = 0;
  1043. if (adreno_has_gmu_wrapper(adreno_gpu)) {
  1044. /* Drain the outstanding traffic on memory buses */
  1045. a6xx_bus_clear_pending_transactions(adreno_gpu, true);
  1046. /* Reset the GPU to a clean state */
  1047. a6xx_gpu_sw_reset(gpu, true);
  1048. a6xx_gpu_sw_reset(gpu, false);
  1049. }
  1050. reinit_completion(&gmu->pd_gate);
  1051. dev_pm_genpd_add_notifier(gmu->cxpd, &gmu->pd_nb);
  1052. dev_pm_genpd_synced_poweroff(gmu->cxpd);
  1053. /* Drop the rpm refcount from active submits */
  1054. if (active_submits)
  1055. pm_runtime_put(&gpu->pdev->dev);
  1056. /* And the final one from recover worker */
  1057. pm_runtime_put_sync(&gpu->pdev->dev);
  1058. if (!wait_for_completion_timeout(&gmu->pd_gate, msecs_to_jiffies(1000)))
  1059. DRM_DEV_ERROR(&gpu->pdev->dev, "cx gdsc didn't collapse\n");
  1060. dev_pm_genpd_remove_notifier(gmu->cxpd);
  1061. pm_runtime_use_autosuspend(&gpu->pdev->dev);
  1062. if (active_submits)
  1063. pm_runtime_get(&gpu->pdev->dev);
  1064. pm_runtime_get_sync(&gpu->pdev->dev);
  1065. gpu->active_submits = active_submits;
  1066. mutex_unlock(&gpu->active_lock);
  1067. msm_gpu_hw_init(gpu);
  1068. a6xx_gpu->hung = false;
  1069. }
  1070. static const char *a6xx_uche_fault_block(struct msm_gpu *gpu, u32 mid)
  1071. {
  1072. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  1073. static const char *uche_clients[7] = {
  1074. "VFD", "SP", "VSC", "VPC", "HLSQ", "PC", "LRZ",
  1075. };
  1076. u32 val;
  1077. if (adreno_is_a7xx(adreno_gpu)) {
  1078. if (mid != 1 && mid != 2 && mid != 3 && mid != 8)
  1079. return "UNKNOWN";
  1080. } else {
  1081. if (mid < 1 || mid > 3)
  1082. return "UNKNOWN";
  1083. }
  1084. /*
  1085. * The source of the data depends on the mid ID read from FSYNR1.
  1086. * and the client ID read from the UCHE block
  1087. */
  1088. val = gpu_read(gpu, REG_A6XX_UCHE_CLIENT_PF);
  1089. if (adreno_is_a7xx(adreno_gpu)) {
  1090. /* Bit 3 for mid=3 indicates BR or BV */
  1091. static const char *uche_clients_a7xx[16] = {
  1092. "BR_VFD", "BR_SP", "BR_VSC", "BR_VPC",
  1093. "BR_HLSQ", "BR_PC", "BR_LRZ", "BR_TP",
  1094. "BV_VFD", "BV_SP", "BV_VSC", "BV_VPC",
  1095. "BV_HLSQ", "BV_PC", "BV_LRZ", "BV_TP",
  1096. };
  1097. /* LPAC has the same clients as BR and BV, but because it is
  1098. * compute-only some of them do not exist and there are holes
  1099. * in the array.
  1100. */
  1101. static const char *uche_clients_lpac_a7xx[8] = {
  1102. "-", "LPAC_SP", "-", "-",
  1103. "LPAC_HLSQ", "-", "-", "LPAC_TP",
  1104. };
  1105. val &= GENMASK(6, 0);
  1106. /* mid=3 refers to BR or BV */
  1107. if (mid == 3) {
  1108. if (val < ARRAY_SIZE(uche_clients_a7xx))
  1109. return uche_clients_a7xx[val];
  1110. else
  1111. return "UCHE";
  1112. }
  1113. /* mid=8 refers to LPAC */
  1114. if (mid == 8) {
  1115. if (val < ARRAY_SIZE(uche_clients_lpac_a7xx))
  1116. return uche_clients_lpac_a7xx[val];
  1117. else
  1118. return "UCHE_LPAC";
  1119. }
  1120. /* mid=2 is a catchall for everything else in LPAC */
  1121. if (mid == 2)
  1122. return "UCHE_LPAC";
  1123. /* mid=1 is a catchall for everything else in BR/BV */
  1124. return "UCHE";
  1125. } else if (adreno_is_a660_family(adreno_gpu)) {
  1126. static const char *uche_clients_a660[8] = {
  1127. "VFD", "SP", "VSC", "VPC", "HLSQ", "PC", "LRZ", "TP",
  1128. };
  1129. static const char *uche_clients_a660_not[8] = {
  1130. "not VFD", "not SP", "not VSC", "not VPC",
  1131. "not HLSQ", "not PC", "not LRZ", "not TP",
  1132. };
  1133. val &= GENMASK(6, 0);
  1134. if (mid == 3 && val < ARRAY_SIZE(uche_clients_a660))
  1135. return uche_clients_a660[val];
  1136. if (mid == 1 && val < ARRAY_SIZE(uche_clients_a660_not))
  1137. return uche_clients_a660_not[val];
  1138. return "UCHE";
  1139. } else {
  1140. /* mid = 3 is most precise and refers to only one block per client */
  1141. if (mid == 3)
  1142. return uche_clients[val & 7];
  1143. /* For mid=2 the source is TP or VFD except when the client id is 0 */
  1144. if (mid == 2)
  1145. return ((val & 7) == 0) ? "TP" : "TP|VFD";
  1146. /* For mid=1 just return "UCHE" as a catchall for everything else */
  1147. return "UCHE";
  1148. }
  1149. }
  1150. static const char *a6xx_fault_block(struct msm_gpu *gpu, u32 id)
  1151. {
  1152. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  1153. if (id == 0)
  1154. return "CP";
  1155. else if (id == 4)
  1156. return "CCU";
  1157. else if (id == 6)
  1158. return "CDP Prefetch";
  1159. else if (id == 7)
  1160. return "GMU";
  1161. else if (id == 5 && adreno_is_a7xx(adreno_gpu))
  1162. return "Flag cache";
  1163. return a6xx_uche_fault_block(gpu, id);
  1164. }
  1165. static int a6xx_fault_handler(void *arg, unsigned long iova, int flags, void *data)
  1166. {
  1167. struct msm_gpu *gpu = arg;
  1168. struct adreno_smmu_fault_info *info = data;
  1169. const char *block = "unknown";
  1170. u32 scratch[] = {
  1171. gpu_read(gpu, REG_A6XX_CP_SCRATCH_REG(4)),
  1172. gpu_read(gpu, REG_A6XX_CP_SCRATCH_REG(5)),
  1173. gpu_read(gpu, REG_A6XX_CP_SCRATCH_REG(6)),
  1174. gpu_read(gpu, REG_A6XX_CP_SCRATCH_REG(7)),
  1175. };
  1176. if (info)
  1177. block = a6xx_fault_block(gpu, info->fsynr1 & 0xff);
  1178. return adreno_fault_handler(gpu, iova, flags, info, block, scratch);
  1179. }
  1180. static void a6xx_cp_hw_err_irq(struct msm_gpu *gpu)
  1181. {
  1182. u32 status = gpu_read(gpu, REG_A6XX_CP_INTERRUPT_STATUS);
  1183. if (status & A6XX_CP_INT_CP_OPCODE_ERROR) {
  1184. u32 val;
  1185. gpu_write(gpu, REG_A6XX_CP_SQE_STAT_ADDR, 1);
  1186. val = gpu_read(gpu, REG_A6XX_CP_SQE_STAT_DATA);
  1187. dev_err_ratelimited(&gpu->pdev->dev,
  1188. "CP | opcode error | possible opcode=0x%8.8X\n",
  1189. val);
  1190. }
  1191. if (status & A6XX_CP_INT_CP_UCODE_ERROR)
  1192. dev_err_ratelimited(&gpu->pdev->dev,
  1193. "CP ucode error interrupt\n");
  1194. if (status & A6XX_CP_INT_CP_HW_FAULT_ERROR)
  1195. dev_err_ratelimited(&gpu->pdev->dev, "CP | HW fault | status=0x%8.8X\n",
  1196. gpu_read(gpu, REG_A6XX_CP_HW_FAULT));
  1197. if (status & A6XX_CP_INT_CP_REGISTER_PROTECTION_ERROR) {
  1198. u32 val = gpu_read(gpu, REG_A6XX_CP_PROTECT_STATUS);
  1199. dev_err_ratelimited(&gpu->pdev->dev,
  1200. "CP | protected mode error | %s | addr=0x%8.8X | status=0x%8.8X\n",
  1201. val & (1 << 20) ? "READ" : "WRITE",
  1202. (val & 0x3ffff), val);
  1203. }
  1204. if (status & A6XX_CP_INT_CP_AHB_ERROR && !adreno_is_a7xx(to_adreno_gpu(gpu)))
  1205. dev_err_ratelimited(&gpu->pdev->dev, "CP AHB error interrupt\n");
  1206. if (status & A6XX_CP_INT_CP_VSD_PARITY_ERROR)
  1207. dev_err_ratelimited(&gpu->pdev->dev, "CP VSD decoder parity error\n");
  1208. if (status & A6XX_CP_INT_CP_ILLEGAL_INSTR_ERROR)
  1209. dev_err_ratelimited(&gpu->pdev->dev, "CP illegal instruction error\n");
  1210. }
  1211. static void a6xx_fault_detect_irq(struct msm_gpu *gpu)
  1212. {
  1213. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  1214. struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
  1215. struct msm_ringbuffer *ring = gpu->funcs->active_ring(gpu);
  1216. /*
  1217. * If stalled on SMMU fault, we could trip the GPU's hang detection,
  1218. * but the fault handler will trigger the devcore dump, and we want
  1219. * to otherwise resume normally rather than killing the submit, so
  1220. * just bail.
  1221. */
  1222. if (gpu_read(gpu, REG_A6XX_RBBM_STATUS3) & A6XX_RBBM_STATUS3_SMMU_STALLED_ON_FAULT)
  1223. return;
  1224. /*
  1225. * Force the GPU to stay on until after we finish
  1226. * collecting information
  1227. */
  1228. if (!adreno_has_gmu_wrapper(adreno_gpu))
  1229. gmu_write(&a6xx_gpu->gmu, REG_A6XX_GMU_GMU_PWR_COL_KEEPALIVE, 1);
  1230. DRM_DEV_ERROR(&gpu->pdev->dev,
  1231. "gpu fault ring %d fence %x status %8.8X rb %4.4x/%4.4x ib1 %16.16llX/%4.4x ib2 %16.16llX/%4.4x\n",
  1232. ring ? ring->id : -1, ring ? ring->fctx->last_fence : 0,
  1233. gpu_read(gpu, REG_A6XX_RBBM_STATUS),
  1234. gpu_read(gpu, REG_A6XX_CP_RB_RPTR),
  1235. gpu_read(gpu, REG_A6XX_CP_RB_WPTR),
  1236. gpu_read64(gpu, REG_A6XX_CP_IB1_BASE),
  1237. gpu_read(gpu, REG_A6XX_CP_IB1_REM_SIZE),
  1238. gpu_read64(gpu, REG_A6XX_CP_IB2_BASE),
  1239. gpu_read(gpu, REG_A6XX_CP_IB2_REM_SIZE));
  1240. /* Turn off the hangcheck timer to keep it from bothering us */
  1241. del_timer(&gpu->hangcheck_timer);
  1242. kthread_queue_work(gpu->worker, &gpu->recover_work);
  1243. }
  1244. static void a7xx_sw_fuse_violation_irq(struct msm_gpu *gpu)
  1245. {
  1246. u32 status;
  1247. status = gpu_read(gpu, REG_A7XX_RBBM_SW_FUSE_INT_STATUS);
  1248. gpu_write(gpu, REG_A7XX_RBBM_SW_FUSE_INT_MASK, 0);
  1249. dev_err_ratelimited(&gpu->pdev->dev, "SW fuse violation status=%8.8x\n", status);
  1250. /*
  1251. * Ignore FASTBLEND violations, because the HW will silently fall back
  1252. * to legacy blending.
  1253. */
  1254. if (status & (A7XX_CX_MISC_SW_FUSE_VALUE_RAYTRACING |
  1255. A7XX_CX_MISC_SW_FUSE_VALUE_LPAC)) {
  1256. del_timer(&gpu->hangcheck_timer);
  1257. kthread_queue_work(gpu->worker, &gpu->recover_work);
  1258. }
  1259. }
  1260. static irqreturn_t a6xx_irq(struct msm_gpu *gpu)
  1261. {
  1262. struct msm_drm_private *priv = gpu->dev->dev_private;
  1263. u32 status = gpu_read(gpu, REG_A6XX_RBBM_INT_0_STATUS);
  1264. gpu_write(gpu, REG_A6XX_RBBM_INT_CLEAR_CMD, status);
  1265. if (priv->disable_err_irq)
  1266. status &= A6XX_RBBM_INT_0_MASK_CP_CACHE_FLUSH_TS;
  1267. if (status & A6XX_RBBM_INT_0_MASK_RBBM_HANG_DETECT)
  1268. a6xx_fault_detect_irq(gpu);
  1269. if (status & A6XX_RBBM_INT_0_MASK_CP_AHB_ERROR)
  1270. dev_err_ratelimited(&gpu->pdev->dev, "CP | AHB bus error\n");
  1271. if (status & A6XX_RBBM_INT_0_MASK_CP_HW_ERROR)
  1272. a6xx_cp_hw_err_irq(gpu);
  1273. if (status & A6XX_RBBM_INT_0_MASK_RBBM_ATB_ASYNCFIFO_OVERFLOW)
  1274. dev_err_ratelimited(&gpu->pdev->dev, "RBBM | ATB ASYNC overflow\n");
  1275. if (status & A6XX_RBBM_INT_0_MASK_RBBM_ATB_BUS_OVERFLOW)
  1276. dev_err_ratelimited(&gpu->pdev->dev, "RBBM | ATB bus overflow\n");
  1277. if (status & A6XX_RBBM_INT_0_MASK_UCHE_OOB_ACCESS)
  1278. dev_err_ratelimited(&gpu->pdev->dev, "UCHE | Out of bounds access\n");
  1279. if (status & A6XX_RBBM_INT_0_MASK_SWFUSEVIOLATION)
  1280. a7xx_sw_fuse_violation_irq(gpu);
  1281. if (status & A6XX_RBBM_INT_0_MASK_CP_CACHE_FLUSH_TS)
  1282. msm_gpu_retire(gpu);
  1283. return IRQ_HANDLED;
  1284. }
  1285. static void a6xx_llc_deactivate(struct a6xx_gpu *a6xx_gpu)
  1286. {
  1287. llcc_slice_deactivate(a6xx_gpu->llc_slice);
  1288. llcc_slice_deactivate(a6xx_gpu->htw_llc_slice);
  1289. }
  1290. static void a6xx_llc_activate(struct a6xx_gpu *a6xx_gpu)
  1291. {
  1292. struct adreno_gpu *adreno_gpu = &a6xx_gpu->base;
  1293. struct msm_gpu *gpu = &adreno_gpu->base;
  1294. u32 cntl1_regval = 0;
  1295. if (IS_ERR(a6xx_gpu->llc_mmio))
  1296. return;
  1297. if (!llcc_slice_activate(a6xx_gpu->llc_slice)) {
  1298. u32 gpu_scid = llcc_get_slice_id(a6xx_gpu->llc_slice);
  1299. gpu_scid &= 0x1f;
  1300. cntl1_regval = (gpu_scid << 0) | (gpu_scid << 5) | (gpu_scid << 10) |
  1301. (gpu_scid << 15) | (gpu_scid << 20);
  1302. /* On A660, the SCID programming for UCHE traffic is done in
  1303. * A6XX_GBIF_SCACHE_CNTL0[14:10]
  1304. */
  1305. if (adreno_is_a660_family(adreno_gpu))
  1306. gpu_rmw(gpu, REG_A6XX_GBIF_SCACHE_CNTL0, (0x1f << 10) |
  1307. (1 << 8), (gpu_scid << 10) | (1 << 8));
  1308. }
  1309. /*
  1310. * For targets with a MMU500, activate the slice but don't program the
  1311. * register. The XBL will take care of that.
  1312. */
  1313. if (!llcc_slice_activate(a6xx_gpu->htw_llc_slice)) {
  1314. if (!a6xx_gpu->have_mmu500) {
  1315. u32 gpuhtw_scid = llcc_get_slice_id(a6xx_gpu->htw_llc_slice);
  1316. gpuhtw_scid &= 0x1f;
  1317. cntl1_regval |= FIELD_PREP(GENMASK(29, 25), gpuhtw_scid);
  1318. }
  1319. }
  1320. if (!cntl1_regval)
  1321. return;
  1322. /*
  1323. * Program the slice IDs for the various GPU blocks and GPU MMU
  1324. * pagetables
  1325. */
  1326. if (!a6xx_gpu->have_mmu500) {
  1327. a6xx_llc_write(a6xx_gpu,
  1328. REG_A6XX_CX_MISC_SYSTEM_CACHE_CNTL_1, cntl1_regval);
  1329. /*
  1330. * Program cacheability overrides to not allocate cache
  1331. * lines on a write miss
  1332. */
  1333. a6xx_llc_rmw(a6xx_gpu,
  1334. REG_A6XX_CX_MISC_SYSTEM_CACHE_CNTL_0, 0xF, 0x03);
  1335. return;
  1336. }
  1337. gpu_rmw(gpu, REG_A6XX_GBIF_SCACHE_CNTL1, GENMASK(24, 0), cntl1_regval);
  1338. }
  1339. static void a7xx_llc_activate(struct a6xx_gpu *a6xx_gpu)
  1340. {
  1341. struct adreno_gpu *adreno_gpu = &a6xx_gpu->base;
  1342. struct msm_gpu *gpu = &adreno_gpu->base;
  1343. if (IS_ERR(a6xx_gpu->llc_mmio))
  1344. return;
  1345. if (!llcc_slice_activate(a6xx_gpu->llc_slice)) {
  1346. u32 gpu_scid = llcc_get_slice_id(a6xx_gpu->llc_slice);
  1347. gpu_scid &= GENMASK(4, 0);
  1348. gpu_write(gpu, REG_A6XX_GBIF_SCACHE_CNTL1,
  1349. FIELD_PREP(GENMASK(29, 25), gpu_scid) |
  1350. FIELD_PREP(GENMASK(24, 20), gpu_scid) |
  1351. FIELD_PREP(GENMASK(19, 15), gpu_scid) |
  1352. FIELD_PREP(GENMASK(14, 10), gpu_scid) |
  1353. FIELD_PREP(GENMASK(9, 5), gpu_scid) |
  1354. FIELD_PREP(GENMASK(4, 0), gpu_scid));
  1355. gpu_write(gpu, REG_A6XX_GBIF_SCACHE_CNTL0,
  1356. FIELD_PREP(GENMASK(14, 10), gpu_scid) |
  1357. BIT(8));
  1358. }
  1359. llcc_slice_activate(a6xx_gpu->htw_llc_slice);
  1360. }
  1361. static void a6xx_llc_slices_destroy(struct a6xx_gpu *a6xx_gpu)
  1362. {
  1363. /* No LLCC on non-RPMh (and by extension, non-GMU) SoCs */
  1364. if (adreno_has_gmu_wrapper(&a6xx_gpu->base))
  1365. return;
  1366. llcc_slice_putd(a6xx_gpu->llc_slice);
  1367. llcc_slice_putd(a6xx_gpu->htw_llc_slice);
  1368. }
  1369. static void a6xx_llc_slices_init(struct platform_device *pdev,
  1370. struct a6xx_gpu *a6xx_gpu, bool is_a7xx)
  1371. {
  1372. struct device_node *phandle;
  1373. /* No LLCC on non-RPMh (and by extension, non-GMU) SoCs */
  1374. if (adreno_has_gmu_wrapper(&a6xx_gpu->base))
  1375. return;
  1376. /*
  1377. * There is a different programming path for A6xx targets with an
  1378. * mmu500 attached, so detect if that is the case
  1379. */
  1380. phandle = of_parse_phandle(pdev->dev.of_node, "iommus", 0);
  1381. a6xx_gpu->have_mmu500 = (phandle &&
  1382. of_device_is_compatible(phandle, "arm,mmu-500"));
  1383. of_node_put(phandle);
  1384. if (is_a7xx || !a6xx_gpu->have_mmu500)
  1385. a6xx_gpu->llc_mmio = msm_ioremap(pdev, "cx_mem");
  1386. else
  1387. a6xx_gpu->llc_mmio = NULL;
  1388. a6xx_gpu->llc_slice = llcc_slice_getd(LLCC_GPU);
  1389. a6xx_gpu->htw_llc_slice = llcc_slice_getd(LLCC_GPUHTW);
  1390. if (IS_ERR_OR_NULL(a6xx_gpu->llc_slice) && IS_ERR_OR_NULL(a6xx_gpu->htw_llc_slice))
  1391. a6xx_gpu->llc_mmio = ERR_PTR(-EINVAL);
  1392. }
  1393. static int a7xx_cx_mem_init(struct a6xx_gpu *a6xx_gpu)
  1394. {
  1395. struct adreno_gpu *adreno_gpu = &a6xx_gpu->base;
  1396. struct msm_gpu *gpu = &adreno_gpu->base;
  1397. u32 fuse_val;
  1398. int ret;
  1399. if (adreno_is_a750(adreno_gpu)) {
  1400. /*
  1401. * Assume that if qcom scm isn't available, that whatever
  1402. * replacement allows writing the fuse register ourselves.
  1403. * Users of alternative firmware need to make sure this
  1404. * register is writeable or indicate that it's not somehow.
  1405. * Print a warning because if you mess this up you're about to
  1406. * crash horribly.
  1407. */
  1408. if (!qcom_scm_is_available()) {
  1409. dev_warn_once(gpu->dev->dev,
  1410. "SCM is not available, poking fuse register\n");
  1411. a6xx_llc_write(a6xx_gpu, REG_A7XX_CX_MISC_SW_FUSE_VALUE,
  1412. A7XX_CX_MISC_SW_FUSE_VALUE_RAYTRACING |
  1413. A7XX_CX_MISC_SW_FUSE_VALUE_FASTBLEND |
  1414. A7XX_CX_MISC_SW_FUSE_VALUE_LPAC);
  1415. adreno_gpu->has_ray_tracing = true;
  1416. return 0;
  1417. }
  1418. ret = qcom_scm_gpu_init_regs(QCOM_SCM_GPU_ALWAYS_EN_REQ |
  1419. QCOM_SCM_GPU_TSENSE_EN_REQ);
  1420. if (ret)
  1421. return ret;
  1422. /*
  1423. * On a750 raytracing may be disabled by the firmware, find out
  1424. * whether that's the case. The scm call above sets the fuse
  1425. * register.
  1426. */
  1427. fuse_val = a6xx_llc_read(a6xx_gpu,
  1428. REG_A7XX_CX_MISC_SW_FUSE_VALUE);
  1429. adreno_gpu->has_ray_tracing =
  1430. !!(fuse_val & A7XX_CX_MISC_SW_FUSE_VALUE_RAYTRACING);
  1431. } else if (adreno_is_a740(adreno_gpu)) {
  1432. /* Raytracing is always enabled on a740 */
  1433. adreno_gpu->has_ray_tracing = true;
  1434. }
  1435. return 0;
  1436. }
  1437. #define GBIF_CLIENT_HALT_MASK BIT(0)
  1438. #define GBIF_ARB_HALT_MASK BIT(1)
  1439. #define VBIF_XIN_HALT_CTRL0_MASK GENMASK(3, 0)
  1440. #define VBIF_RESET_ACK_MASK 0xF0
  1441. #define GPR0_GBIF_HALT_REQUEST 0x1E0
  1442. void a6xx_bus_clear_pending_transactions(struct adreno_gpu *adreno_gpu, bool gx_off)
  1443. {
  1444. struct msm_gpu *gpu = &adreno_gpu->base;
  1445. if (adreno_is_a619_holi(adreno_gpu)) {
  1446. gpu_write(gpu, REG_A6XX_RBBM_GPR0_CNTL, GPR0_GBIF_HALT_REQUEST);
  1447. spin_until((gpu_read(gpu, REG_A6XX_RBBM_VBIF_GX_RESET_STATUS) &
  1448. (VBIF_RESET_ACK_MASK)) == VBIF_RESET_ACK_MASK);
  1449. } else if (!a6xx_has_gbif(adreno_gpu)) {
  1450. gpu_write(gpu, REG_A6XX_VBIF_XIN_HALT_CTRL0, VBIF_XIN_HALT_CTRL0_MASK);
  1451. spin_until((gpu_read(gpu, REG_A6XX_VBIF_XIN_HALT_CTRL1) &
  1452. (VBIF_XIN_HALT_CTRL0_MASK)) == VBIF_XIN_HALT_CTRL0_MASK);
  1453. gpu_write(gpu, REG_A6XX_VBIF_XIN_HALT_CTRL0, 0);
  1454. return;
  1455. }
  1456. if (gx_off) {
  1457. /* Halt the gx side of GBIF */
  1458. gpu_write(gpu, REG_A6XX_RBBM_GBIF_HALT, 1);
  1459. spin_until(gpu_read(gpu, REG_A6XX_RBBM_GBIF_HALT_ACK) & 1);
  1460. }
  1461. /* Halt new client requests on GBIF */
  1462. gpu_write(gpu, REG_A6XX_GBIF_HALT, GBIF_CLIENT_HALT_MASK);
  1463. spin_until((gpu_read(gpu, REG_A6XX_GBIF_HALT_ACK) &
  1464. (GBIF_CLIENT_HALT_MASK)) == GBIF_CLIENT_HALT_MASK);
  1465. /* Halt all AXI requests on GBIF */
  1466. gpu_write(gpu, REG_A6XX_GBIF_HALT, GBIF_ARB_HALT_MASK);
  1467. spin_until((gpu_read(gpu, REG_A6XX_GBIF_HALT_ACK) &
  1468. (GBIF_ARB_HALT_MASK)) == GBIF_ARB_HALT_MASK);
  1469. /* The GBIF halt needs to be explicitly cleared */
  1470. gpu_write(gpu, REG_A6XX_GBIF_HALT, 0x0);
  1471. }
  1472. void a6xx_gpu_sw_reset(struct msm_gpu *gpu, bool assert)
  1473. {
  1474. /* 11nm chips (e.g. ones with A610) have hw issues with the reset line! */
  1475. if (adreno_is_a610(to_adreno_gpu(gpu)))
  1476. return;
  1477. gpu_write(gpu, REG_A6XX_RBBM_SW_RESET_CMD, assert);
  1478. /* Perform a bogus read and add a brief delay to ensure ordering. */
  1479. gpu_read(gpu, REG_A6XX_RBBM_SW_RESET_CMD);
  1480. udelay(1);
  1481. /* The reset line needs to be asserted for at least 100 us */
  1482. if (assert)
  1483. udelay(100);
  1484. }
  1485. static int a6xx_gmu_pm_resume(struct msm_gpu *gpu)
  1486. {
  1487. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  1488. struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
  1489. int ret;
  1490. gpu->needs_hw_init = true;
  1491. trace_msm_gpu_resume(0);
  1492. mutex_lock(&a6xx_gpu->gmu.lock);
  1493. ret = a6xx_gmu_resume(a6xx_gpu);
  1494. mutex_unlock(&a6xx_gpu->gmu.lock);
  1495. if (ret)
  1496. return ret;
  1497. msm_devfreq_resume(gpu);
  1498. adreno_is_a7xx(adreno_gpu) ? a7xx_llc_activate(a6xx_gpu) : a6xx_llc_activate(a6xx_gpu);
  1499. return ret;
  1500. }
  1501. static int a6xx_pm_resume(struct msm_gpu *gpu)
  1502. {
  1503. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  1504. struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
  1505. struct a6xx_gmu *gmu = &a6xx_gpu->gmu;
  1506. unsigned long freq = gpu->fast_rate;
  1507. struct dev_pm_opp *opp;
  1508. int ret;
  1509. gpu->needs_hw_init = true;
  1510. trace_msm_gpu_resume(0);
  1511. mutex_lock(&a6xx_gpu->gmu.lock);
  1512. opp = dev_pm_opp_find_freq_ceil(&gpu->pdev->dev, &freq);
  1513. if (IS_ERR(opp)) {
  1514. ret = PTR_ERR(opp);
  1515. goto err_set_opp;
  1516. }
  1517. dev_pm_opp_put(opp);
  1518. /* Set the core clock and bus bw, having VDD scaling in mind */
  1519. dev_pm_opp_set_opp(&gpu->pdev->dev, opp);
  1520. pm_runtime_resume_and_get(gmu->dev);
  1521. pm_runtime_resume_and_get(gmu->gxpd);
  1522. ret = clk_bulk_prepare_enable(gpu->nr_clocks, gpu->grp_clks);
  1523. if (ret)
  1524. goto err_bulk_clk;
  1525. if (adreno_is_a619_holi(adreno_gpu))
  1526. a6xx_sptprac_enable(gmu);
  1527. /* If anything goes south, tear the GPU down piece by piece.. */
  1528. if (ret) {
  1529. err_bulk_clk:
  1530. pm_runtime_put(gmu->gxpd);
  1531. pm_runtime_put(gmu->dev);
  1532. dev_pm_opp_set_opp(&gpu->pdev->dev, NULL);
  1533. }
  1534. err_set_opp:
  1535. mutex_unlock(&a6xx_gpu->gmu.lock);
  1536. if (!ret)
  1537. msm_devfreq_resume(gpu);
  1538. return ret;
  1539. }
  1540. static int a6xx_gmu_pm_suspend(struct msm_gpu *gpu)
  1541. {
  1542. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  1543. struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
  1544. int i, ret;
  1545. trace_msm_gpu_suspend(0);
  1546. a6xx_llc_deactivate(a6xx_gpu);
  1547. msm_devfreq_suspend(gpu);
  1548. mutex_lock(&a6xx_gpu->gmu.lock);
  1549. ret = a6xx_gmu_stop(a6xx_gpu);
  1550. mutex_unlock(&a6xx_gpu->gmu.lock);
  1551. if (ret)
  1552. return ret;
  1553. if (a6xx_gpu->shadow_bo)
  1554. for (i = 0; i < gpu->nr_rings; i++)
  1555. a6xx_gpu->shadow[i] = 0;
  1556. gpu->suspend_count++;
  1557. return 0;
  1558. }
  1559. static int a6xx_pm_suspend(struct msm_gpu *gpu)
  1560. {
  1561. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  1562. struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
  1563. struct a6xx_gmu *gmu = &a6xx_gpu->gmu;
  1564. int i;
  1565. trace_msm_gpu_suspend(0);
  1566. msm_devfreq_suspend(gpu);
  1567. mutex_lock(&a6xx_gpu->gmu.lock);
  1568. /* Drain the outstanding traffic on memory buses */
  1569. a6xx_bus_clear_pending_transactions(adreno_gpu, true);
  1570. if (adreno_is_a619_holi(adreno_gpu))
  1571. a6xx_sptprac_disable(gmu);
  1572. clk_bulk_disable_unprepare(gpu->nr_clocks, gpu->grp_clks);
  1573. pm_runtime_put_sync(gmu->gxpd);
  1574. dev_pm_opp_set_opp(&gpu->pdev->dev, NULL);
  1575. pm_runtime_put_sync(gmu->dev);
  1576. mutex_unlock(&a6xx_gpu->gmu.lock);
  1577. if (a6xx_gpu->shadow_bo)
  1578. for (i = 0; i < gpu->nr_rings; i++)
  1579. a6xx_gpu->shadow[i] = 0;
  1580. gpu->suspend_count++;
  1581. return 0;
  1582. }
  1583. static int a6xx_gmu_get_timestamp(struct msm_gpu *gpu, uint64_t *value)
  1584. {
  1585. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  1586. struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
  1587. mutex_lock(&a6xx_gpu->gmu.lock);
  1588. /* Force the GPU power on so we can read this register */
  1589. a6xx_gmu_set_oob(&a6xx_gpu->gmu, GMU_OOB_PERFCOUNTER_SET);
  1590. *value = gpu_read64(gpu, REG_A6XX_CP_ALWAYS_ON_COUNTER);
  1591. a6xx_gmu_clear_oob(&a6xx_gpu->gmu, GMU_OOB_PERFCOUNTER_SET);
  1592. mutex_unlock(&a6xx_gpu->gmu.lock);
  1593. return 0;
  1594. }
  1595. static int a6xx_get_timestamp(struct msm_gpu *gpu, uint64_t *value)
  1596. {
  1597. *value = gpu_read64(gpu, REG_A6XX_CP_ALWAYS_ON_COUNTER);
  1598. return 0;
  1599. }
  1600. static struct msm_ringbuffer *a6xx_active_ring(struct msm_gpu *gpu)
  1601. {
  1602. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  1603. struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
  1604. return a6xx_gpu->cur_ring;
  1605. }
  1606. static void a6xx_destroy(struct msm_gpu *gpu)
  1607. {
  1608. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  1609. struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
  1610. if (a6xx_gpu->sqe_bo) {
  1611. msm_gem_unpin_iova(a6xx_gpu->sqe_bo, gpu->aspace);
  1612. drm_gem_object_put(a6xx_gpu->sqe_bo);
  1613. }
  1614. if (a6xx_gpu->shadow_bo) {
  1615. msm_gem_unpin_iova(a6xx_gpu->shadow_bo, gpu->aspace);
  1616. drm_gem_object_put(a6xx_gpu->shadow_bo);
  1617. }
  1618. a6xx_llc_slices_destroy(a6xx_gpu);
  1619. a6xx_gmu_remove(a6xx_gpu);
  1620. adreno_gpu_cleanup(adreno_gpu);
  1621. kfree(a6xx_gpu);
  1622. }
  1623. static u64 a6xx_gpu_busy(struct msm_gpu *gpu, unsigned long *out_sample_rate)
  1624. {
  1625. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  1626. struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
  1627. u64 busy_cycles;
  1628. /* 19.2MHz */
  1629. *out_sample_rate = 19200000;
  1630. busy_cycles = gmu_read64(&a6xx_gpu->gmu,
  1631. REG_A6XX_GMU_CX_GMU_POWER_COUNTER_XOCLK_0_L,
  1632. REG_A6XX_GMU_CX_GMU_POWER_COUNTER_XOCLK_0_H);
  1633. return busy_cycles;
  1634. }
  1635. static void a6xx_gpu_set_freq(struct msm_gpu *gpu, struct dev_pm_opp *opp,
  1636. bool suspended)
  1637. {
  1638. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  1639. struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
  1640. mutex_lock(&a6xx_gpu->gmu.lock);
  1641. a6xx_gmu_set_freq(gpu, opp, suspended);
  1642. mutex_unlock(&a6xx_gpu->gmu.lock);
  1643. }
  1644. static struct msm_gem_address_space *
  1645. a6xx_create_address_space(struct msm_gpu *gpu, struct platform_device *pdev)
  1646. {
  1647. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  1648. struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
  1649. unsigned long quirks = 0;
  1650. /*
  1651. * This allows GPU to set the bus attributes required to use system
  1652. * cache on behalf of the iommu page table walker.
  1653. */
  1654. if (!IS_ERR_OR_NULL(a6xx_gpu->htw_llc_slice) &&
  1655. !device_iommu_capable(&pdev->dev, IOMMU_CAP_CACHE_COHERENCY))
  1656. quirks |= IO_PGTABLE_QUIRK_ARM_OUTER_WBWA;
  1657. return adreno_iommu_create_address_space(gpu, pdev, quirks);
  1658. }
  1659. static struct msm_gem_address_space *
  1660. a6xx_create_private_address_space(struct msm_gpu *gpu)
  1661. {
  1662. struct msm_mmu *mmu;
  1663. mmu = msm_iommu_pagetable_create(gpu->aspace->mmu);
  1664. if (IS_ERR(mmu))
  1665. return ERR_CAST(mmu);
  1666. return msm_gem_address_space_create(mmu,
  1667. "gpu", 0x100000000ULL,
  1668. adreno_private_address_space_size(gpu));
  1669. }
  1670. static uint32_t a6xx_get_rptr(struct msm_gpu *gpu, struct msm_ringbuffer *ring)
  1671. {
  1672. struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
  1673. struct a6xx_gpu *a6xx_gpu = to_a6xx_gpu(adreno_gpu);
  1674. if (adreno_gpu->base.hw_apriv || a6xx_gpu->has_whereami)
  1675. return a6xx_gpu->shadow[ring->id];
  1676. return ring->memptrs->rptr = gpu_read(gpu, REG_A6XX_CP_RB_RPTR);
  1677. }
  1678. static bool a6xx_progress(struct msm_gpu *gpu, struct msm_ringbuffer *ring)
  1679. {
  1680. struct msm_cp_state cp_state = {
  1681. .ib1_base = gpu_read64(gpu, REG_A6XX_CP_IB1_BASE),
  1682. .ib2_base = gpu_read64(gpu, REG_A6XX_CP_IB2_BASE),
  1683. .ib1_rem = gpu_read(gpu, REG_A6XX_CP_IB1_REM_SIZE),
  1684. .ib2_rem = gpu_read(gpu, REG_A6XX_CP_IB2_REM_SIZE),
  1685. };
  1686. bool progress;
  1687. /*
  1688. * Adjust the remaining data to account for what has already been
  1689. * fetched from memory, but not yet consumed by the SQE.
  1690. *
  1691. * This is not *technically* correct, the amount buffered could
  1692. * exceed the IB size due to hw prefetching ahead, but:
  1693. *
  1694. * (1) We aren't trying to find the exact position, just whether
  1695. * progress has been made
  1696. * (2) The CP_REG_TO_MEM at the end of a submit should be enough
  1697. * to prevent prefetching into an unrelated submit. (And
  1698. * either way, at some point the ROQ will be full.)
  1699. */
  1700. cp_state.ib1_rem += gpu_read(gpu, REG_A6XX_CP_ROQ_AVAIL_IB1) >> 16;
  1701. cp_state.ib2_rem += gpu_read(gpu, REG_A6XX_CP_ROQ_AVAIL_IB2) >> 16;
  1702. progress = !!memcmp(&cp_state, &ring->last_cp_state, sizeof(cp_state));
  1703. ring->last_cp_state = cp_state;
  1704. return progress;
  1705. }
  1706. static u32 fuse_to_supp_hw(const struct adreno_info *info, u32 fuse)
  1707. {
  1708. if (!info->speedbins)
  1709. return UINT_MAX;
  1710. for (int i = 0; info->speedbins[i].fuse != SHRT_MAX; i++)
  1711. if (info->speedbins[i].fuse == fuse)
  1712. return BIT(info->speedbins[i].speedbin);
  1713. return UINT_MAX;
  1714. }
  1715. static int a6xx_set_supported_hw(struct device *dev, const struct adreno_info *info)
  1716. {
  1717. u32 supp_hw;
  1718. u32 speedbin;
  1719. int ret;
  1720. ret = adreno_read_speedbin(dev, &speedbin);
  1721. /*
  1722. * -ENOENT means that the platform doesn't support speedbin which is
  1723. * fine
  1724. */
  1725. if (ret == -ENOENT) {
  1726. return 0;
  1727. } else if (ret) {
  1728. dev_err_probe(dev, ret,
  1729. "failed to read speed-bin. Some OPPs may not be supported by hardware\n");
  1730. return ret;
  1731. }
  1732. supp_hw = fuse_to_supp_hw(info, speedbin);
  1733. if (supp_hw == UINT_MAX) {
  1734. DRM_DEV_ERROR(dev,
  1735. "missing support for speed-bin: %u. Some OPPs may not be supported by hardware\n",
  1736. speedbin);
  1737. supp_hw = BIT(0); /* Default */
  1738. }
  1739. ret = devm_pm_opp_set_supported_hw(dev, &supp_hw, 1);
  1740. if (ret)
  1741. return ret;
  1742. return 0;
  1743. }
  1744. static const struct adreno_gpu_funcs funcs = {
  1745. .base = {
  1746. .get_param = adreno_get_param,
  1747. .set_param = adreno_set_param,
  1748. .hw_init = a6xx_hw_init,
  1749. .ucode_load = a6xx_ucode_load,
  1750. .pm_suspend = a6xx_gmu_pm_suspend,
  1751. .pm_resume = a6xx_gmu_pm_resume,
  1752. .recover = a6xx_recover,
  1753. .submit = a6xx_submit,
  1754. .active_ring = a6xx_active_ring,
  1755. .irq = a6xx_irq,
  1756. .destroy = a6xx_destroy,
  1757. #if defined(CONFIG_DRM_MSM_GPU_STATE)
  1758. .show = a6xx_show,
  1759. #endif
  1760. .gpu_busy = a6xx_gpu_busy,
  1761. .gpu_get_freq = a6xx_gmu_get_freq,
  1762. .gpu_set_freq = a6xx_gpu_set_freq,
  1763. #if defined(CONFIG_DRM_MSM_GPU_STATE)
  1764. .gpu_state_get = a6xx_gpu_state_get,
  1765. .gpu_state_put = a6xx_gpu_state_put,
  1766. #endif
  1767. .create_address_space = a6xx_create_address_space,
  1768. .create_private_address_space = a6xx_create_private_address_space,
  1769. .get_rptr = a6xx_get_rptr,
  1770. .progress = a6xx_progress,
  1771. },
  1772. .get_timestamp = a6xx_gmu_get_timestamp,
  1773. };
  1774. static const struct adreno_gpu_funcs funcs_gmuwrapper = {
  1775. .base = {
  1776. .get_param = adreno_get_param,
  1777. .set_param = adreno_set_param,
  1778. .hw_init = a6xx_hw_init,
  1779. .ucode_load = a6xx_ucode_load,
  1780. .pm_suspend = a6xx_pm_suspend,
  1781. .pm_resume = a6xx_pm_resume,
  1782. .recover = a6xx_recover,
  1783. .submit = a6xx_submit,
  1784. .active_ring = a6xx_active_ring,
  1785. .irq = a6xx_irq,
  1786. .destroy = a6xx_destroy,
  1787. #if defined(CONFIG_DRM_MSM_GPU_STATE)
  1788. .show = a6xx_show,
  1789. #endif
  1790. .gpu_busy = a6xx_gpu_busy,
  1791. #if defined(CONFIG_DRM_MSM_GPU_STATE)
  1792. .gpu_state_get = a6xx_gpu_state_get,
  1793. .gpu_state_put = a6xx_gpu_state_put,
  1794. #endif
  1795. .create_address_space = a6xx_create_address_space,
  1796. .create_private_address_space = a6xx_create_private_address_space,
  1797. .get_rptr = a6xx_get_rptr,
  1798. .progress = a6xx_progress,
  1799. },
  1800. .get_timestamp = a6xx_get_timestamp,
  1801. };
  1802. static const struct adreno_gpu_funcs funcs_a7xx = {
  1803. .base = {
  1804. .get_param = adreno_get_param,
  1805. .set_param = adreno_set_param,
  1806. .hw_init = a6xx_hw_init,
  1807. .ucode_load = a6xx_ucode_load,
  1808. .pm_suspend = a6xx_gmu_pm_suspend,
  1809. .pm_resume = a6xx_gmu_pm_resume,
  1810. .recover = a6xx_recover,
  1811. .submit = a7xx_submit,
  1812. .active_ring = a6xx_active_ring,
  1813. .irq = a6xx_irq,
  1814. .destroy = a6xx_destroy,
  1815. #if defined(CONFIG_DRM_MSM_GPU_STATE)
  1816. .show = a6xx_show,
  1817. #endif
  1818. .gpu_busy = a6xx_gpu_busy,
  1819. .gpu_get_freq = a6xx_gmu_get_freq,
  1820. .gpu_set_freq = a6xx_gpu_set_freq,
  1821. #if defined(CONFIG_DRM_MSM_GPU_STATE)
  1822. .gpu_state_get = a6xx_gpu_state_get,
  1823. .gpu_state_put = a6xx_gpu_state_put,
  1824. #endif
  1825. .create_address_space = a6xx_create_address_space,
  1826. .create_private_address_space = a6xx_create_private_address_space,
  1827. .get_rptr = a6xx_get_rptr,
  1828. .progress = a6xx_progress,
  1829. },
  1830. .get_timestamp = a6xx_gmu_get_timestamp,
  1831. };
  1832. struct msm_gpu *a6xx_gpu_init(struct drm_device *dev)
  1833. {
  1834. struct msm_drm_private *priv = dev->dev_private;
  1835. struct platform_device *pdev = priv->gpu_pdev;
  1836. struct adreno_platform_config *config = pdev->dev.platform_data;
  1837. struct device_node *node;
  1838. struct a6xx_gpu *a6xx_gpu;
  1839. struct adreno_gpu *adreno_gpu;
  1840. struct msm_gpu *gpu;
  1841. bool is_a7xx;
  1842. int ret;
  1843. a6xx_gpu = kzalloc(sizeof(*a6xx_gpu), GFP_KERNEL);
  1844. if (!a6xx_gpu)
  1845. return ERR_PTR(-ENOMEM);
  1846. adreno_gpu = &a6xx_gpu->base;
  1847. gpu = &adreno_gpu->base;
  1848. mutex_init(&a6xx_gpu->gmu.lock);
  1849. adreno_gpu->registers = NULL;
  1850. /* Check if there is a GMU phandle and set it up */
  1851. node = of_parse_phandle(pdev->dev.of_node, "qcom,gmu", 0);
  1852. /* FIXME: How do we gracefully handle this? */
  1853. BUG_ON(!node);
  1854. adreno_gpu->gmu_is_wrapper = of_device_is_compatible(node, "qcom,adreno-gmu-wrapper");
  1855. adreno_gpu->base.hw_apriv =
  1856. !!(config->info->quirks & ADRENO_QUIRK_HAS_HW_APRIV);
  1857. /* gpu->info only gets assigned in adreno_gpu_init() */
  1858. is_a7xx = config->info->family == ADRENO_7XX_GEN1 ||
  1859. config->info->family == ADRENO_7XX_GEN2 ||
  1860. config->info->family == ADRENO_7XX_GEN3;
  1861. a6xx_llc_slices_init(pdev, a6xx_gpu, is_a7xx);
  1862. ret = a6xx_set_supported_hw(&pdev->dev, config->info);
  1863. if (ret) {
  1864. a6xx_llc_slices_destroy(a6xx_gpu);
  1865. kfree(a6xx_gpu);
  1866. return ERR_PTR(ret);
  1867. }
  1868. if (is_a7xx)
  1869. ret = adreno_gpu_init(dev, pdev, adreno_gpu, &funcs_a7xx, 1);
  1870. else if (adreno_has_gmu_wrapper(adreno_gpu))
  1871. ret = adreno_gpu_init(dev, pdev, adreno_gpu, &funcs_gmuwrapper, 1);
  1872. else
  1873. ret = adreno_gpu_init(dev, pdev, adreno_gpu, &funcs, 1);
  1874. if (ret) {
  1875. a6xx_destroy(&(a6xx_gpu->base.base));
  1876. return ERR_PTR(ret);
  1877. }
  1878. /*
  1879. * For now only clamp to idle freq for devices where this is known not
  1880. * to cause power supply issues:
  1881. */
  1882. if (adreno_is_a618(adreno_gpu) || adreno_is_7c3(adreno_gpu))
  1883. priv->gpu_clamp_to_idle = true;
  1884. if (adreno_has_gmu_wrapper(adreno_gpu))
  1885. ret = a6xx_gmu_wrapper_init(a6xx_gpu, node);
  1886. else
  1887. ret = a6xx_gmu_init(a6xx_gpu, node);
  1888. of_node_put(node);
  1889. if (ret) {
  1890. a6xx_destroy(&(a6xx_gpu->base.base));
  1891. return ERR_PTR(ret);
  1892. }
  1893. if (adreno_is_a7xx(adreno_gpu)) {
  1894. ret = a7xx_cx_mem_init(a6xx_gpu);
  1895. if (ret) {
  1896. a6xx_destroy(&(a6xx_gpu->base.base));
  1897. return ERR_PTR(ret);
  1898. }
  1899. }
  1900. if (gpu->aspace)
  1901. msm_mmu_set_fault_handler(gpu->aspace->mmu, gpu,
  1902. a6xx_fault_handler);
  1903. a6xx_calc_ubwc_config(adreno_gpu);
  1904. return gpu;
  1905. }