adreno_gen7_ringbuffer.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include "adreno.h"
  7. #include "adreno_gen7.h"
  8. #include "adreno_pm4types.h"
  9. #include "adreno_ringbuffer.h"
  10. #include "adreno_trace.h"
  11. #include "kgsl_trace.h"
  12. static bool is_concurrent_binning(struct adreno_context *drawctxt)
  13. {
  14. if (!drawctxt)
  15. return false;
  16. return !(drawctxt->base.flags & KGSL_CONTEXT_SECURE);
  17. }
  18. static int gen7_rb_pagetable_switch(struct adreno_device *adreno_dev,
  19. struct adreno_ringbuffer *rb, struct adreno_context *drawctxt,
  20. struct kgsl_pagetable *pagetable, u32 *cmds)
  21. {
  22. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  23. u64 ttbr0 = kgsl_mmu_pagetable_get_ttbr0(pagetable);
  24. int count = 0;
  25. u32 id = drawctxt ? drawctxt->base.id : 0;
  26. if (pagetable == device->mmu.defaultpagetable)
  27. return 0;
  28. /* CP switches the pagetable and flushes the Caches */
  29. cmds[count++] = cp_type7_packet(CP_SMMU_TABLE_UPDATE, 3);
  30. cmds[count++] = lower_32_bits(ttbr0);
  31. cmds[count++] = upper_32_bits(ttbr0);
  32. cmds[count++] = id;
  33. cmds[count++] = cp_type7_packet(CP_MEM_WRITE, 5);
  34. cmds[count++] = lower_32_bits(SCRATCH_RB_GPU_ADDR(device,
  35. rb->id, ttbr0));
  36. cmds[count++] = upper_32_bits(SCRATCH_RB_GPU_ADDR(device,
  37. rb->id, ttbr0));
  38. cmds[count++] = lower_32_bits(ttbr0);
  39. cmds[count++] = upper_32_bits(ttbr0);
  40. cmds[count++] = id;
  41. /*
  42. * Sync both threads after switching pagetables and enable BR only
  43. * to make sure BV doesn't race ahead while BR is still switching
  44. * pagetables.
  45. */
  46. cmds[count++] = cp_type7_packet(CP_THREAD_CONTROL, 1);
  47. cmds[count++] = CP_SYNC_THREADS | CP_SET_THREAD_BR;
  48. return count;
  49. }
  50. static int gen7_rb_context_switch(struct adreno_device *adreno_dev,
  51. struct adreno_ringbuffer *rb,
  52. struct adreno_context *drawctxt)
  53. {
  54. struct kgsl_pagetable *pagetable =
  55. adreno_drawctxt_get_pagetable(drawctxt);
  56. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  57. int count = 0;
  58. u32 cmds[55];
  59. /* Sync both threads */
  60. cmds[count++] = cp_type7_packet(CP_THREAD_CONTROL, 1);
  61. cmds[count++] = CP_SYNC_THREADS | CP_SET_THREAD_BOTH;
  62. /* Reset context state */
  63. cmds[count++] = cp_type7_packet(CP_RESET_CONTEXT_STATE, 1);
  64. cmds[count++] = CP_RESET_GLOBAL_LOCAL_TS | CP_CLEAR_BV_BR_COUNTER |
  65. CP_CLEAR_RESOURCE_TABLE | CP_CLEAR_ON_CHIP_TS;
  66. /*
  67. * Enable/disable concurrent binning for pagetable switch and
  68. * set the thread to BR since only BR can execute the pagetable
  69. * switch packets.
  70. */
  71. /* Sync both threads and enable BR only */
  72. cmds[count++] = cp_type7_packet(CP_THREAD_CONTROL, 1);
  73. cmds[count++] = CP_SYNC_THREADS | CP_SET_THREAD_BR;
  74. if (adreno_drawctxt_get_pagetable(rb->drawctxt_active) != pagetable) {
  75. /* Clear performance counters during context switches */
  76. if (!adreno_dev->perfcounter) {
  77. cmds[count++] = cp_type4_packet(GEN7_RBBM_PERFCTR_SRAM_INIT_CMD, 1);
  78. cmds[count++] = 0x1;
  79. }
  80. count += gen7_rb_pagetable_switch(adreno_dev, rb,
  81. drawctxt, pagetable, &cmds[count]);
  82. /* Wait for performance counter clear to finish */
  83. if (!adreno_dev->perfcounter) {
  84. cmds[count++] = cp_type7_packet(CP_WAIT_REG_MEM, 6);
  85. cmds[count++] = 0x3;
  86. cmds[count++] = GEN7_RBBM_PERFCTR_SRAM_INIT_STATUS;
  87. cmds[count++] = 0x0;
  88. cmds[count++] = 0x1;
  89. cmds[count++] = 0x1;
  90. cmds[count++] = 0x0;
  91. }
  92. } else {
  93. struct kgsl_iommu *iommu = KGSL_IOMMU(device);
  94. u32 offset = GEN7_SMMU_BASE + (iommu->cb0_offset >> 2) + 0x0d;
  95. /*
  96. * Set the CONTEXTIDR register to the current context id so we
  97. * can use it in pagefault debugging. Unlike TTBR0 we don't
  98. * need any special sequence or locking to change it
  99. */
  100. cmds[count++] = cp_type4_packet(offset, 1);
  101. cmds[count++] = drawctxt->base.id;
  102. }
  103. cmds[count++] = cp_type7_packet(CP_NOP, 1);
  104. cmds[count++] = CONTEXT_TO_MEM_IDENTIFIER;
  105. cmds[count++] = cp_type7_packet(CP_MEM_WRITE, 3);
  106. cmds[count++] = lower_32_bits(MEMSTORE_RB_GPU_ADDR(device, rb,
  107. current_context));
  108. cmds[count++] = upper_32_bits(MEMSTORE_RB_GPU_ADDR(device, rb,
  109. current_context));
  110. cmds[count++] = drawctxt->base.id;
  111. cmds[count++] = cp_type7_packet(CP_MEM_WRITE, 3);
  112. cmds[count++] = lower_32_bits(MEMSTORE_ID_GPU_ADDR(device,
  113. KGSL_MEMSTORE_GLOBAL, current_context));
  114. cmds[count++] = upper_32_bits(MEMSTORE_ID_GPU_ADDR(device,
  115. KGSL_MEMSTORE_GLOBAL, current_context));
  116. cmds[count++] = drawctxt->base.id;
  117. cmds[count++] = cp_type7_packet(CP_EVENT_WRITE, 1);
  118. cmds[count++] = 0x31;
  119. if (adreno_is_preemption_enabled(adreno_dev)) {
  120. u64 gpuaddr = drawctxt->base.user_ctxt_record->memdesc.gpuaddr;
  121. cmds[count++] = cp_type7_packet(CP_SET_PSEUDO_REGISTER, 3);
  122. cmds[count++] = SET_PSEUDO_NON_PRIV_SAVE_ADDR;
  123. cmds[count++] = lower_32_bits(gpuaddr);
  124. cmds[count++] = upper_32_bits(gpuaddr);
  125. }
  126. return gen7_ringbuffer_addcmds(adreno_dev, rb, NULL, F_NOTPROTECTED,
  127. cmds, count, 0, NULL);
  128. }
  129. #define RB_SOPTIMESTAMP(device, rb) \
  130. MEMSTORE_RB_GPU_ADDR(device, rb, soptimestamp)
  131. #define CTXT_SOPTIMESTAMP(device, drawctxt) \
  132. MEMSTORE_ID_GPU_ADDR(device, (drawctxt)->base.id, soptimestamp)
  133. #define RB_EOPTIMESTAMP(device, rb) \
  134. MEMSTORE_RB_GPU_ADDR(device, rb, eoptimestamp)
  135. #define CTXT_EOPTIMESTAMP(device, drawctxt) \
  136. MEMSTORE_ID_GPU_ADDR(device, (drawctxt)->base.id, eoptimestamp)
  137. int gen7_ringbuffer_submit(struct adreno_ringbuffer *rb,
  138. struct adreno_submit_time *time)
  139. {
  140. struct adreno_device *adreno_dev = ADRENO_RB_DEVICE(rb);
  141. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  142. int ret = 0;
  143. unsigned long flags;
  144. adreno_get_submit_time(adreno_dev, rb, time);
  145. adreno_profile_submit_time(time);
  146. spin_lock_irqsave(&rb->preempt_lock, flags);
  147. if (adreno_in_preempt_state(adreno_dev, ADRENO_PREEMPT_NONE)) {
  148. if (adreno_dev->cur_rb == rb) {
  149. kgsl_pwrscale_busy(device);
  150. ret = gen7_fenced_write(adreno_dev,
  151. GEN7_CP_RB_WPTR, rb->_wptr,
  152. FENCE_STATUS_WRITEDROPPED0_MASK);
  153. rb->skip_inline_wptr = false;
  154. }
  155. } else {
  156. if (adreno_dev->cur_rb == rb)
  157. rb->skip_inline_wptr = true;
  158. }
  159. rb->wptr = rb->_wptr;
  160. spin_unlock_irqrestore(&rb->preempt_lock, flags);
  161. if (ret) {
  162. /*
  163. * If WPTR update fails, take inline snapshot and trigger
  164. * recovery.
  165. */
  166. gmu_core_fault_snapshot(device);
  167. adreno_dispatcher_fault(adreno_dev,
  168. ADRENO_GMU_FAULT_SKIP_SNAPSHOT);
  169. }
  170. return ret;
  171. }
  172. int gen7_ringbuffer_init(struct adreno_device *adreno_dev)
  173. {
  174. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  175. int i, ret;
  176. ret = adreno_allocate_global(device, &device->scratch, PAGE_SIZE,
  177. 0, 0, KGSL_MEMDESC_RANDOM | KGSL_MEMDESC_PRIVILEGED,
  178. "scratch");
  179. if (ret)
  180. return ret;
  181. adreno_dev->cur_rb = &(adreno_dev->ringbuffers[0]);
  182. if (!adreno_preemption_feature_set(adreno_dev)) {
  183. adreno_dev->num_ringbuffers = 1;
  184. return adreno_ringbuffer_setup(adreno_dev,
  185. &adreno_dev->ringbuffers[0], 0);
  186. }
  187. adreno_dev->num_ringbuffers = ARRAY_SIZE(adreno_dev->ringbuffers);
  188. for (i = 0; i < adreno_dev->num_ringbuffers; i++) {
  189. int ret;
  190. ret = adreno_ringbuffer_setup(adreno_dev,
  191. &adreno_dev->ringbuffers[i], i);
  192. if (ret)
  193. return ret;
  194. }
  195. timer_setup(&adreno_dev->preempt.timer, adreno_preemption_timer, 0);
  196. gen7_preemption_init(adreno_dev);
  197. return 0;
  198. }
  199. #define GEN7_SUBMIT_MAX 104
  200. int gen7_ringbuffer_addcmds(struct adreno_device *adreno_dev,
  201. struct adreno_ringbuffer *rb, struct adreno_context *drawctxt,
  202. u32 flags, u32 *in, u32 dwords, u32 timestamp,
  203. struct adreno_submit_time *time)
  204. {
  205. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  206. u32 size = GEN7_SUBMIT_MAX + dwords;
  207. u32 *cmds, index = 0;
  208. u64 profile_gpuaddr;
  209. u32 profile_dwords;
  210. if (adreno_drawctxt_detached(drawctxt))
  211. return -ENOENT;
  212. if (adreno_gpu_fault(adreno_dev) != 0)
  213. return -EPROTO;
  214. rb->timestamp++;
  215. if (drawctxt)
  216. drawctxt->internal_timestamp = rb->timestamp;
  217. /* All submissions are run with protected mode off due to APRIV */
  218. flags &= ~F_NOTPROTECTED;
  219. cmds = adreno_ringbuffer_allocspace(rb, size);
  220. if (IS_ERR(cmds))
  221. return PTR_ERR(cmds);
  222. /* Identify the start of a command */
  223. cmds[index++] = cp_type7_packet(CP_NOP, 1);
  224. cmds[index++] = drawctxt ? CMD_IDENTIFIER : CMD_INTERNAL_IDENTIFIER;
  225. /* This is 25 dwords when drawctxt is not NULL and perfcounter needs to be zapped*/
  226. index += gen7_preemption_pre_ibsubmit(adreno_dev, rb, drawctxt,
  227. &cmds[index]);
  228. cmds[index++] = cp_type7_packet(CP_THREAD_CONTROL, 1);
  229. cmds[index++] = CP_SET_THREAD_BOTH;
  230. cmds[index++] = cp_type7_packet(CP_SET_MARKER, 1);
  231. cmds[index++] = 0x101; /* IFPC disable */
  232. cmds[index++] = cp_type7_packet(CP_THREAD_CONTROL, 1);
  233. cmds[index++] = CP_SET_THREAD_BR;
  234. profile_gpuaddr = adreno_profile_preib_processing(adreno_dev,
  235. drawctxt, &profile_dwords);
  236. if (profile_gpuaddr) {
  237. cmds[index++] = cp_type7_packet(CP_INDIRECT_BUFFER_PFE, 3);
  238. cmds[index++] = lower_32_bits(profile_gpuaddr);
  239. cmds[index++] = upper_32_bits(profile_gpuaddr);
  240. cmds[index++] = profile_dwords;
  241. }
  242. if (drawctxt) {
  243. cmds[index++] = cp_type7_packet(CP_MEM_WRITE, 3);
  244. cmds[index++] = lower_32_bits(CTXT_SOPTIMESTAMP(device,
  245. drawctxt));
  246. cmds[index++] = upper_32_bits(CTXT_SOPTIMESTAMP(device,
  247. drawctxt));
  248. cmds[index++] = timestamp;
  249. }
  250. cmds[index++] = cp_type7_packet(CP_MEM_WRITE, 3);
  251. cmds[index++] = lower_32_bits(RB_SOPTIMESTAMP(device, rb));
  252. cmds[index++] = upper_32_bits(RB_SOPTIMESTAMP(device, rb));
  253. cmds[index++] = rb->timestamp;
  254. if (IS_SECURE(flags)) {
  255. /* Sync BV and BR if entering secure mode */
  256. cmds[index++] = cp_type7_packet(CP_THREAD_CONTROL, 1);
  257. cmds[index++] = CP_SYNC_THREADS | CP_CONCURRENT_BIN_DISABLE;
  258. cmds[index++] = cp_type7_packet(CP_SET_SECURE_MODE, 1);
  259. cmds[index++] = 1;
  260. }
  261. memcpy(&cmds[index], in, dwords << 2);
  262. index += dwords;
  263. profile_gpuaddr = adreno_profile_postib_processing(adreno_dev,
  264. drawctxt, &dwords);
  265. if (profile_gpuaddr) {
  266. cmds[index++] = cp_type7_packet(CP_INDIRECT_BUFFER_PFE, 3);
  267. cmds[index++] = lower_32_bits(profile_gpuaddr);
  268. cmds[index++] = upper_32_bits(profile_gpuaddr);
  269. cmds[index++] = profile_dwords;
  270. }
  271. if (test_bit(KGSL_FT_PAGEFAULT_GPUHALT_ENABLE, &device->mmu.pfpolicy))
  272. cmds[index++] = cp_type7_packet(CP_WAIT_MEM_WRITES, 0);
  273. if (is_concurrent_binning(drawctxt)) {
  274. u64 addr = SCRATCH_RB_GPU_ADDR(device, rb->id, bv_ts);
  275. cmds[index++] = cp_type7_packet(CP_THREAD_CONTROL, 1);
  276. cmds[index++] = CP_SET_THREAD_BV;
  277. /*
  278. * Make sure the timestamp is committed once BV pipe is
  279. * completely done with this submission.
  280. */
  281. cmds[index++] = cp_type7_packet(CP_EVENT_WRITE, 4);
  282. cmds[index++] = CACHE_CLEAN | BIT(27);
  283. cmds[index++] = lower_32_bits(addr);
  284. cmds[index++] = upper_32_bits(addr);
  285. cmds[index++] = rb->timestamp;
  286. cmds[index++] = cp_type7_packet(CP_THREAD_CONTROL, 1);
  287. cmds[index++] = CP_SET_THREAD_BR;
  288. /*
  289. * This makes sure that BR doesn't race ahead and commit
  290. * timestamp to memstore while BV is still processing
  291. * this submission.
  292. */
  293. cmds[index++] = cp_type7_packet(CP_WAIT_TIMESTAMP, 4);
  294. cmds[index++] = 0;
  295. cmds[index++] = lower_32_bits(addr);
  296. cmds[index++] = upper_32_bits(addr);
  297. cmds[index++] = rb->timestamp;
  298. }
  299. /*
  300. * If this is an internal command, just write the ringbuffer timestamp,
  301. * otherwise, write both
  302. */
  303. if (!drawctxt) {
  304. cmds[index++] = cp_type7_packet(CP_EVENT_WRITE, 4);
  305. cmds[index++] = CACHE_CLEAN | BIT(31) | BIT(27);
  306. cmds[index++] = lower_32_bits(RB_EOPTIMESTAMP(device, rb));
  307. cmds[index++] = upper_32_bits(RB_EOPTIMESTAMP(device, rb));
  308. cmds[index++] = rb->timestamp;
  309. } else {
  310. cmds[index++] = cp_type7_packet(CP_EVENT_WRITE, 4);
  311. cmds[index++] = CACHE_CLEAN | BIT(31) | BIT(27);
  312. cmds[index++] = lower_32_bits(CTXT_EOPTIMESTAMP(device,
  313. drawctxt));
  314. cmds[index++] = upper_32_bits(CTXT_EOPTIMESTAMP(device,
  315. drawctxt));
  316. cmds[index++] = timestamp;
  317. cmds[index++] = cp_type7_packet(CP_EVENT_WRITE, 4);
  318. cmds[index++] = CACHE_CLEAN | BIT(27);
  319. cmds[index++] = lower_32_bits(RB_EOPTIMESTAMP(device, rb));
  320. cmds[index++] = upper_32_bits(RB_EOPTIMESTAMP(device, rb));
  321. cmds[index++] = rb->timestamp;
  322. }
  323. if (IS_WFI(flags))
  324. cmds[index++] = cp_type7_packet(CP_WAIT_FOR_IDLE, 0);
  325. if (IS_SECURE(flags)) {
  326. cmds[index++] = cp_type7_packet(CP_THREAD_CONTROL, 1);
  327. cmds[index++] = CP_CONCURRENT_BIN_DISABLE;
  328. cmds[index++] = cp_type7_packet(CP_SET_SECURE_MODE, 1);
  329. cmds[index++] = 0;
  330. cmds[index++] = cp_type7_packet(CP_THREAD_CONTROL, 1);
  331. cmds[index++] = CP_SYNC_THREADS;
  332. }
  333. cmds[index++] = cp_type7_packet(CP_THREAD_CONTROL, 1);
  334. cmds[index++] = CP_SET_THREAD_BOTH;
  335. cmds[index++] = cp_type7_packet(CP_SET_MARKER, 1);
  336. cmds[index++] = 0x100; /* IFPC enable */
  337. cmds[index++] = cp_type7_packet(CP_THREAD_CONTROL, 1);
  338. cmds[index++] = CP_SET_THREAD_BR;
  339. /* 10 dwords */
  340. index += gen7_preemption_post_ibsubmit(adreno_dev, &cmds[index]);
  341. /* Adjust the thing for the number of bytes we actually wrote */
  342. rb->_wptr -= (size - index);
  343. return gen7_ringbuffer_submit(rb, time);
  344. }
  345. static u32 gen7_get_alwayson_counter(u32 *cmds, u64 gpuaddr)
  346. {
  347. cmds[0] = cp_type7_packet(CP_REG_TO_MEM, 3);
  348. cmds[1] = GEN7_CP_ALWAYS_ON_COUNTER_LO | (1 << 30) | (2 << 18);
  349. cmds[2] = lower_32_bits(gpuaddr);
  350. cmds[3] = upper_32_bits(gpuaddr);
  351. return 4;
  352. }
  353. static u32 gen7_get_alwayson_context(u32 *cmds, u64 gpuaddr)
  354. {
  355. cmds[0] = cp_type7_packet(CP_REG_TO_MEM, 3);
  356. cmds[1] = GEN7_CP_ALWAYS_ON_CONTEXT_LO | (1 << 30) | (2 << 18);
  357. cmds[2] = lower_32_bits(gpuaddr);
  358. cmds[3] = upper_32_bits(gpuaddr);
  359. return 4;
  360. }
  361. #define PROFILE_IB_DWORDS 4
  362. #define PROFILE_IB_SLOTS (PAGE_SIZE / (PROFILE_IB_DWORDS << 2))
  363. static u64 gen7_get_user_profiling_ib(struct adreno_ringbuffer *rb,
  364. struct kgsl_drawobj_cmd *cmdobj, u32 target_offset, u32 *cmds)
  365. {
  366. u32 offset, *ib, dwords;
  367. if (IS_ERR(rb->profile_desc))
  368. return 0;
  369. offset = rb->profile_index * (PROFILE_IB_DWORDS << 2);
  370. ib = rb->profile_desc->hostptr + offset;
  371. dwords = gen7_get_alwayson_counter(ib,
  372. cmdobj->profiling_buffer_gpuaddr + target_offset);
  373. cmds[0] = cp_type7_packet(CP_INDIRECT_BUFFER_PFE, 3);
  374. cmds[1] = lower_32_bits(rb->profile_desc->gpuaddr + offset);
  375. cmds[2] = upper_32_bits(rb->profile_desc->gpuaddr + offset);
  376. cmds[3] = dwords;
  377. rb->profile_index = (rb->profile_index + 1) % PROFILE_IB_SLOTS;
  378. return 4;
  379. }
  380. static int gen7_drawctxt_switch(struct adreno_device *adreno_dev,
  381. struct adreno_ringbuffer *rb,
  382. struct adreno_context *drawctxt)
  383. {
  384. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  385. int ret;
  386. if (rb->drawctxt_active == drawctxt)
  387. return 0;
  388. if (kgsl_context_detached(&drawctxt->base))
  389. return -ENOENT;
  390. if (!_kgsl_context_get(&drawctxt->base))
  391. return -ENOENT;
  392. ret = gen7_rb_context_switch(adreno_dev, rb, drawctxt);
  393. if (ret) {
  394. kgsl_context_put(&drawctxt->base);
  395. return ret;
  396. }
  397. trace_adreno_drawctxt_switch(rb, drawctxt);
  398. /* Release the current drawctxt as soon as the new one is switched */
  399. adreno_put_drawctxt_on_timestamp(device, rb->drawctxt_active,
  400. rb, rb->timestamp);
  401. rb->drawctxt_active = drawctxt;
  402. return 0;
  403. }
  404. #define GEN7_USER_PROFILE_IB(rb, cmdobj, cmds, field) \
  405. gen7_get_user_profiling_ib((rb), (cmdobj), \
  406. offsetof(struct kgsl_drawobj_profiling_buffer, field), \
  407. (cmds))
  408. #define GEN7_KERNEL_PROFILE(dev, cmdobj, cmds, field) \
  409. gen7_get_alwayson_counter((cmds), \
  410. (dev)->profile_buffer->gpuaddr + \
  411. ADRENO_DRAWOBJ_PROFILE_OFFSET((cmdobj)->profile_index, \
  412. field))
  413. #define GEN7_KERNEL_PROFILE_CONTEXT(dev, cmdobj, cmds, field) \
  414. gen7_get_alwayson_context((cmds), \
  415. (dev)->profile_buffer->gpuaddr + \
  416. ADRENO_DRAWOBJ_PROFILE_OFFSET((cmdobj)->profile_index, \
  417. field))
  418. #define GEN7_COMMAND_DWORDS 60
  419. int gen7_ringbuffer_submitcmd(struct adreno_device *adreno_dev,
  420. struct kgsl_drawobj_cmd *cmdobj, u32 flags,
  421. struct adreno_submit_time *time)
  422. {
  423. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  424. struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
  425. struct adreno_context *drawctxt = ADRENO_CONTEXT(drawobj->context);
  426. struct adreno_ringbuffer *rb = drawctxt->rb;
  427. int ret = 0, numibs = 0, index = 0;
  428. u32 *cmds;
  429. /* Count the number of IBs (if we are not skipping) */
  430. if (!IS_SKIP(flags)) {
  431. struct list_head *tmp;
  432. list_for_each(tmp, &cmdobj->cmdlist)
  433. numibs++;
  434. }
  435. cmds = kvmalloc((GEN7_COMMAND_DWORDS + (numibs * 5)) << 2, GFP_KERNEL);
  436. if (!cmds) {
  437. ret = -ENOMEM;
  438. goto done;
  439. }
  440. cmds[index++] = cp_type7_packet(CP_NOP, 1);
  441. cmds[index++] = START_IB_IDENTIFIER;
  442. /* Kernel profiling: 8 dwords */
  443. if (IS_KERNEL_PROFILE(flags)) {
  444. index += GEN7_KERNEL_PROFILE(adreno_dev, cmdobj, &cmds[index],
  445. started);
  446. index += GEN7_KERNEL_PROFILE_CONTEXT(adreno_dev, cmdobj, &cmds[index],
  447. ctx_start);
  448. }
  449. /* User profiling: 4 dwords */
  450. if (IS_USER_PROFILE(flags))
  451. index += GEN7_USER_PROFILE_IB(rb, cmdobj, &cmds[index],
  452. gpu_ticks_submitted);
  453. if (is_concurrent_binning(drawctxt)) {
  454. cmds[index++] = cp_type7_packet(CP_THREAD_CONTROL, 1);
  455. cmds[index++] = CP_SET_THREAD_BOTH;
  456. }
  457. if (numibs) {
  458. struct kgsl_memobj_node *ib;
  459. cmds[index++] = cp_type7_packet(CP_SET_MARKER, 1);
  460. cmds[index++] = 0x00d; /* IB1LIST start */
  461. list_for_each_entry(ib, &cmdobj->cmdlist, node) {
  462. if (ib->priv & MEMOBJ_SKIP ||
  463. (ib->flags & KGSL_CMDLIST_CTXTSWITCH_PREAMBLE &&
  464. !IS_PREAMBLE(flags)))
  465. cmds[index++] = cp_type7_packet(CP_NOP, 4);
  466. cmds[index++] =
  467. cp_type7_packet(CP_INDIRECT_BUFFER_PFE, 3);
  468. cmds[index++] = lower_32_bits(ib->gpuaddr);
  469. cmds[index++] = upper_32_bits(ib->gpuaddr);
  470. /* Double check that IB_PRIV is never set */
  471. cmds[index++] = (ib->size >> 2) & 0xfffff;
  472. }
  473. cmds[index++] = cp_type7_packet(CP_SET_MARKER, 1);
  474. cmds[index++] = 0x00e; /* IB1LIST end */
  475. }
  476. if (is_concurrent_binning(drawctxt)) {
  477. cmds[index++] = cp_type7_packet(CP_THREAD_CONTROL, 1);
  478. cmds[index++] = CP_SET_THREAD_BR;
  479. }
  480. /* CCU invalidate depth */
  481. cmds[index++] = cp_type7_packet(CP_EVENT_WRITE, 1);
  482. cmds[index++] = 24;
  483. /* CCU invalidate color */
  484. cmds[index++] = cp_type7_packet(CP_EVENT_WRITE, 1);
  485. cmds[index++] = 25;
  486. /* 8 dwords */
  487. if (IS_KERNEL_PROFILE(flags)) {
  488. index += GEN7_KERNEL_PROFILE(adreno_dev, cmdobj, &cmds[index],
  489. retired);
  490. index += GEN7_KERNEL_PROFILE_CONTEXT(adreno_dev, cmdobj, &cmds[index],
  491. ctx_end);
  492. }
  493. /* 4 dwords */
  494. if (IS_USER_PROFILE(flags))
  495. index += GEN7_USER_PROFILE_IB(rb, cmdobj, &cmds[index],
  496. gpu_ticks_retired);
  497. cmds[index++] = cp_type7_packet(CP_NOP, 1);
  498. cmds[index++] = END_IB_IDENTIFIER;
  499. ret = gen7_drawctxt_switch(adreno_dev, rb, drawctxt);
  500. /*
  501. * In the unlikely event of an error in the drawctxt switch,
  502. * treat it like a hang
  503. */
  504. if (ret) {
  505. /*
  506. * It is "normal" to get a -ENOSPC or a -ENOENT. Don't log it,
  507. * the upper layers know how to handle it
  508. */
  509. if (ret != -ENOSPC && ret != -ENOENT)
  510. dev_err(device->dev,
  511. "Unable to switch draw context: %d\n", ret);
  512. goto done;
  513. }
  514. adreno_drawobj_set_constraint(device, drawobj);
  515. ret = gen7_ringbuffer_addcmds(adreno_dev, drawctxt->rb, drawctxt,
  516. flags, cmds, index, drawobj->timestamp, time);
  517. done:
  518. trace_kgsl_issueibcmds(device, drawctxt->base.id, numibs,
  519. drawobj->timestamp, drawobj->flags, ret, drawctxt->type);
  520. kvfree(cmds);
  521. return ret;
  522. }