adreno_a6xx_preempt.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-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_a6xx.h"
  8. #include "adreno_pm4types.h"
  9. #include "adreno_trace.h"
  10. #define PREEMPT_RECORD(_field) \
  11. offsetof(struct a6xx_cp_preemption_record, _field)
  12. #define PREEMPT_SMMU_RECORD(_field) \
  13. offsetof(struct a6xx_cp_smmu_info, _field)
  14. static void _update_wptr(struct adreno_device *adreno_dev, bool reset_timer,
  15. bool atomic)
  16. {
  17. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  18. struct adreno_ringbuffer *rb = adreno_dev->cur_rb;
  19. unsigned long flags;
  20. int ret = 0;
  21. spin_lock_irqsave(&rb->preempt_lock, flags);
  22. if (!atomic) {
  23. /*
  24. * We might have skipped updating the wptr in case we are in
  25. * dispatcher context. Do it now.
  26. */
  27. if (rb->skip_inline_wptr) {
  28. ret = a6xx_fenced_write(adreno_dev,
  29. A6XX_CP_RB_WPTR, rb->wptr,
  30. FENCE_STATUS_WRITEDROPPED0_MASK);
  31. reset_timer = true;
  32. rb->skip_inline_wptr = false;
  33. }
  34. } else {
  35. unsigned int wptr;
  36. kgsl_regread(device, A6XX_CP_RB_WPTR, &wptr);
  37. if (wptr != rb->wptr) {
  38. kgsl_regwrite(device, A6XX_CP_RB_WPTR, rb->wptr);
  39. reset_timer = true;
  40. }
  41. }
  42. if (reset_timer)
  43. rb->dispatch_q.expires = jiffies +
  44. msecs_to_jiffies(adreno_drawobj_timeout);
  45. spin_unlock_irqrestore(&rb->preempt_lock, flags);
  46. if (!atomic) {
  47. /* If WPTR update fails, set the fault and trigger recovery */
  48. if (ret) {
  49. gmu_core_fault_snapshot(device);
  50. adreno_dispatcher_fault(adreno_dev,
  51. ADRENO_GMU_FAULT_SKIP_SNAPSHOT);
  52. }
  53. }
  54. }
  55. static void _power_collapse_set(struct adreno_device *adreno_dev, bool val)
  56. {
  57. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  58. if (!gmu_core_isenabled(device))
  59. return;
  60. if (val) {
  61. if (adreno_is_a660(adreno_dev) ||
  62. adreno_is_a663(adreno_dev))
  63. gmu_core_regwrite(device,
  64. A6XX_GMU_PWR_COL_PREEMPT_KEEPALIVE, 0x1);
  65. else
  66. gmu_core_regrmw(device,
  67. A6XX_GMU_AO_SPARE_CNTL, 0x0, 0x2);
  68. } else {
  69. if (adreno_is_a660(adreno_dev) ||
  70. adreno_is_a663(adreno_dev))
  71. gmu_core_regwrite(device,
  72. A6XX_GMU_PWR_COL_PREEMPT_KEEPALIVE, 0x0);
  73. else
  74. gmu_core_regrmw(device,
  75. A6XX_GMU_AO_SPARE_CNTL, 0x2, 0x0);
  76. }
  77. }
  78. static void _a6xx_preemption_done(struct adreno_device *adreno_dev)
  79. {
  80. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  81. unsigned int status;
  82. /*
  83. * In the very unlikely case that the power is off, do nothing - the
  84. * state will be reset on power up and everybody will be happy
  85. */
  86. if (!kgsl_state_is_awake(device))
  87. return;
  88. kgsl_regread(device, A6XX_CP_CONTEXT_SWITCH_CNTL, &status);
  89. if (status & 0x1) {
  90. dev_err(device->dev,
  91. "Preemption not complete: status=%X cur=%d R/W=%X/%X next=%d R/W=%X/%X\n",
  92. status, adreno_dev->cur_rb->id,
  93. adreno_get_rptr(adreno_dev->cur_rb),
  94. adreno_dev->cur_rb->wptr,
  95. adreno_dev->next_rb->id,
  96. adreno_get_rptr(adreno_dev->next_rb),
  97. adreno_dev->next_rb->wptr);
  98. /* Set a fault and restart */
  99. adreno_dispatcher_fault(adreno_dev, ADRENO_PREEMPT_FAULT);
  100. return;
  101. }
  102. adreno_dev->preempt.count++;
  103. /*
  104. * In normal scenarios, preemption keep alive bit is cleared during
  105. * CP interrupt callback. However, if preemption is successful
  106. * immediately after preemption timer expires or there is a preemption
  107. * interrupt with non-zero status, the state is transitioned to complete
  108. * state. Once dispatcher is scheduled, it calls this function.
  109. * We can now safely clear the preemption keepalive bit, allowing
  110. * power collapse to resume its regular activity.
  111. */
  112. _power_collapse_set(adreno_dev, false);
  113. del_timer_sync(&adreno_dev->preempt.timer);
  114. kgsl_regread(device, A6XX_CP_CONTEXT_SWITCH_LEVEL_STATUS, &status);
  115. trace_adreno_preempt_done(adreno_dev->cur_rb->id, adreno_dev->next_rb->id,
  116. status, 0);
  117. /* Clean up all the bits */
  118. adreno_dev->prev_rb = adreno_dev->cur_rb;
  119. adreno_dev->cur_rb = adreno_dev->next_rb;
  120. adreno_dev->next_rb = NULL;
  121. /* Update the wptr for the new command queue */
  122. _update_wptr(adreno_dev, true, false);
  123. /* Update the dispatcher timer for the new command queue */
  124. mod_timer(&adreno_dev->dispatcher.timer,
  125. adreno_dev->cur_rb->dispatch_q.expires);
  126. /* Clear the preempt state */
  127. adreno_set_preempt_state(adreno_dev, ADRENO_PREEMPT_NONE);
  128. }
  129. static void _a6xx_preemption_fault(struct adreno_device *adreno_dev)
  130. {
  131. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  132. unsigned int status;
  133. /*
  134. * If the power is on check the preemption status one more time - if it
  135. * was successful then just transition to the complete state
  136. */
  137. if (kgsl_state_is_awake(device)) {
  138. kgsl_regread(device, A6XX_CP_CONTEXT_SWITCH_CNTL, &status);
  139. if (!(status & 0x1)) {
  140. adreno_set_preempt_state(adreno_dev,
  141. ADRENO_PREEMPT_COMPLETE);
  142. adreno_dispatcher_schedule(device);
  143. return;
  144. }
  145. }
  146. dev_err(device->dev,
  147. "Preemption Fault: cur=%d R/W=0x%x/0x%x, next=%d R/W=0x%x/0x%x\n",
  148. adreno_dev->cur_rb->id,
  149. adreno_get_rptr(adreno_dev->cur_rb),
  150. adreno_dev->cur_rb->wptr,
  151. adreno_dev->next_rb->id,
  152. adreno_get_rptr(adreno_dev->next_rb),
  153. adreno_dev->next_rb->wptr);
  154. adreno_dispatcher_fault(adreno_dev, ADRENO_PREEMPT_FAULT);
  155. }
  156. static void _a6xx_preemption_worker(struct work_struct *work)
  157. {
  158. struct adreno_preemption *preempt = container_of(work,
  159. struct adreno_preemption, work);
  160. struct adreno_device *adreno_dev = container_of(preempt,
  161. struct adreno_device, preempt);
  162. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  163. /* Need to take the mutex to make sure that the power stays on */
  164. mutex_lock(&device->mutex);
  165. if (adreno_in_preempt_state(adreno_dev, ADRENO_PREEMPT_FAULTED))
  166. _a6xx_preemption_fault(adreno_dev);
  167. mutex_unlock(&device->mutex);
  168. }
  169. /* Find the highest priority active ringbuffer */
  170. static struct adreno_ringbuffer *a6xx_next_ringbuffer(
  171. struct adreno_device *adreno_dev)
  172. {
  173. struct adreno_ringbuffer *rb;
  174. unsigned long flags;
  175. unsigned int i;
  176. FOR_EACH_RINGBUFFER(adreno_dev, rb, i) {
  177. bool empty;
  178. spin_lock_irqsave(&rb->preempt_lock, flags);
  179. empty = adreno_rb_empty(rb);
  180. spin_unlock_irqrestore(&rb->preempt_lock, flags);
  181. if (!empty)
  182. return rb;
  183. }
  184. return NULL;
  185. }
  186. void a6xx_preemption_trigger(struct adreno_device *adreno_dev, bool atomic)
  187. {
  188. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  189. struct kgsl_iommu *iommu = KGSL_IOMMU(device);
  190. struct adreno_ringbuffer *next;
  191. uint64_t ttbr0, gpuaddr;
  192. unsigned int contextidr, cntl;
  193. unsigned long flags;
  194. struct adreno_preemption *preempt = &adreno_dev->preempt;
  195. /* Put ourselves into a possible trigger state */
  196. if (!adreno_move_preempt_state(adreno_dev,
  197. ADRENO_PREEMPT_NONE, ADRENO_PREEMPT_START))
  198. return;
  199. /* Get the next ringbuffer to preempt in */
  200. next = a6xx_next_ringbuffer(adreno_dev);
  201. /*
  202. * Nothing to do if every ringbuffer is empty or if the current
  203. * ringbuffer is the only active one
  204. */
  205. if (next == NULL || next == adreno_dev->cur_rb) {
  206. /*
  207. * Update any critical things that might have been skipped while
  208. * we were looking for a new ringbuffer
  209. */
  210. if (next != NULL) {
  211. _update_wptr(adreno_dev, false, atomic);
  212. mod_timer(&adreno_dev->dispatcher.timer,
  213. adreno_dev->cur_rb->dispatch_q.expires);
  214. }
  215. adreno_set_preempt_state(adreno_dev, ADRENO_PREEMPT_NONE);
  216. return;
  217. }
  218. /* Turn off the dispatcher timer */
  219. del_timer(&adreno_dev->dispatcher.timer);
  220. /*
  221. * This is the most critical section - we need to take care not to race
  222. * until we have programmed the CP for the switch
  223. */
  224. spin_lock_irqsave(&next->preempt_lock, flags);
  225. /* Get the pagetable from the pagetable info. */
  226. kgsl_sharedmem_readq(device->scratch, &ttbr0,
  227. SCRATCH_RB_OFFSET(next->id, ttbr0));
  228. kgsl_sharedmem_readl(device->scratch, &contextidr,
  229. SCRATCH_RB_OFFSET(next->id, contextidr));
  230. kgsl_sharedmem_writel(next->preemption_desc,
  231. PREEMPT_RECORD(wptr), next->wptr);
  232. spin_unlock_irqrestore(&next->preempt_lock, flags);
  233. /* And write it to the smmu info */
  234. if (kgsl_mmu_is_perprocess(&device->mmu)) {
  235. kgsl_sharedmem_writeq(iommu->smmu_info,
  236. PREEMPT_SMMU_RECORD(ttbr0), ttbr0);
  237. kgsl_sharedmem_writel(iommu->smmu_info,
  238. PREEMPT_SMMU_RECORD(context_idr), contextidr);
  239. }
  240. kgsl_sharedmem_readq(preempt->scratch, &gpuaddr,
  241. next->id * sizeof(u64));
  242. /*
  243. * Set a keepalive bit before the first preemption register write.
  244. * This is required since while each individual write to the context
  245. * switch registers will wake the GPU from collapse, it will not in
  246. * itself cause GPU activity. Thus, the GPU could technically be
  247. * re-collapsed between subsequent register writes leading to a
  248. * prolonged preemption sequence. The keepalive bit prevents any
  249. * further power collapse while it is set.
  250. * It is more efficient to use a keepalive+wake-on-fence approach here
  251. * rather than an OOB. Both keepalive and the fence are effectively
  252. * free when the GPU is already powered on, whereas an OOB requires an
  253. * unconditional handshake with the GMU.
  254. */
  255. _power_collapse_set(adreno_dev, true);
  256. /*
  257. * Fenced writes on this path will make sure the GPU is woken up
  258. * in case it was power collapsed by the GMU.
  259. */
  260. if (a6xx_fenced_write(adreno_dev,
  261. A6XX_CP_CONTEXT_SWITCH_PRIV_NON_SECURE_RESTORE_ADDR_LO,
  262. lower_32_bits(next->preemption_desc->gpuaddr),
  263. FENCE_STATUS_WRITEDROPPED1_MASK))
  264. goto err;
  265. /*
  266. * Above fence writes will make sure GMU comes out of
  267. * IFPC state if its was in IFPC state but it doesn't
  268. * guarantee that GMU FW actually moved to ACTIVE state
  269. * i.e. wake-up from IFPC is complete.
  270. * Wait for GMU to move to ACTIVE state before triggering
  271. * preemption. This is require to make sure CP doesn't
  272. * interrupt GMU during wake-up from IFPC.
  273. */
  274. if (!atomic && gmu_core_dev_wait_for_active_transition(device))
  275. goto err;
  276. if (a6xx_fenced_write(adreno_dev,
  277. A6XX_CP_CONTEXT_SWITCH_PRIV_NON_SECURE_RESTORE_ADDR_HI,
  278. upper_32_bits(next->preemption_desc->gpuaddr),
  279. FENCE_STATUS_WRITEDROPPED1_MASK))
  280. goto err;
  281. if (a6xx_fenced_write(adreno_dev,
  282. A6XX_CP_CONTEXT_SWITCH_PRIV_SECURE_RESTORE_ADDR_LO,
  283. lower_32_bits(next->secure_preemption_desc->gpuaddr),
  284. FENCE_STATUS_WRITEDROPPED1_MASK))
  285. goto err;
  286. if (a6xx_fenced_write(adreno_dev,
  287. A6XX_CP_CONTEXT_SWITCH_PRIV_SECURE_RESTORE_ADDR_HI,
  288. upper_32_bits(next->secure_preemption_desc->gpuaddr),
  289. FENCE_STATUS_WRITEDROPPED1_MASK))
  290. goto err;
  291. if (a6xx_fenced_write(adreno_dev,
  292. A6XX_CP_CONTEXT_SWITCH_NON_PRIV_RESTORE_ADDR_LO,
  293. lower_32_bits(gpuaddr),
  294. FENCE_STATUS_WRITEDROPPED1_MASK))
  295. goto err;
  296. if (a6xx_fenced_write(adreno_dev,
  297. A6XX_CP_CONTEXT_SWITCH_NON_PRIV_RESTORE_ADDR_HI,
  298. upper_32_bits(gpuaddr),
  299. FENCE_STATUS_WRITEDROPPED1_MASK))
  300. goto err;
  301. adreno_dev->next_rb = next;
  302. /* Start the timer to detect a stuck preemption */
  303. mod_timer(&adreno_dev->preempt.timer,
  304. jiffies + msecs_to_jiffies(ADRENO_PREEMPT_TIMEOUT));
  305. cntl = (preempt->preempt_level << 6) | 0x01;
  306. /* Skip save/restore during L1 preemption */
  307. if (preempt->skipsaverestore)
  308. cntl |= (1 << 9);
  309. /* Enable GMEM save/restore across preemption */
  310. if (preempt->usesgmem)
  311. cntl |= (1 << 8);
  312. trace_adreno_preempt_trigger(adreno_dev->cur_rb->id, adreno_dev->next_rb->id,
  313. cntl, 0);
  314. adreno_set_preempt_state(adreno_dev, ADRENO_PREEMPT_TRIGGERED);
  315. /* Trigger the preemption */
  316. if (a6xx_fenced_write(adreno_dev, A6XX_CP_CONTEXT_SWITCH_CNTL, cntl,
  317. FENCE_STATUS_WRITEDROPPED1_MASK)) {
  318. adreno_dev->next_rb = NULL;
  319. del_timer(&adreno_dev->preempt.timer);
  320. goto err;
  321. }
  322. return;
  323. err:
  324. /* If fenced write fails, take inline snapshot and trigger recovery */
  325. if (!atomic) {
  326. gmu_core_fault_snapshot(device);
  327. adreno_dispatcher_fault(adreno_dev,
  328. ADRENO_GMU_FAULT_SKIP_SNAPSHOT);
  329. } else {
  330. adreno_dispatcher_fault(adreno_dev, ADRENO_GMU_FAULT);
  331. }
  332. adreno_set_preempt_state(adreno_dev, ADRENO_PREEMPT_NONE);
  333. /* Clear the keep alive */
  334. _power_collapse_set(adreno_dev, false);
  335. }
  336. void a6xx_preemption_callback(struct adreno_device *adreno_dev, int bit)
  337. {
  338. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  339. unsigned int status;
  340. if (!adreno_move_preempt_state(adreno_dev,
  341. ADRENO_PREEMPT_TRIGGERED, ADRENO_PREEMPT_PENDING))
  342. return;
  343. kgsl_regread(device, A6XX_CP_CONTEXT_SWITCH_CNTL, &status);
  344. if (status & 0x1) {
  345. dev_err(KGSL_DEVICE(adreno_dev)->dev,
  346. "preempt interrupt with non-zero status: %X\n",
  347. status);
  348. /*
  349. * Under the assumption that this is a race between the
  350. * interrupt and the register, schedule the worker to clean up.
  351. * If the status still hasn't resolved itself by the time we get
  352. * there then we have to assume something bad happened
  353. */
  354. adreno_set_preempt_state(adreno_dev, ADRENO_PREEMPT_COMPLETE);
  355. adreno_dispatcher_schedule(KGSL_DEVICE(adreno_dev));
  356. return;
  357. }
  358. adreno_dev->preempt.count++;
  359. /*
  360. * We can now safely clear the preemption keepalive bit, allowing
  361. * power collapse to resume its regular activity.
  362. */
  363. _power_collapse_set(adreno_dev, false);
  364. del_timer(&adreno_dev->preempt.timer);
  365. kgsl_regread(device, A6XX_CP_CONTEXT_SWITCH_LEVEL_STATUS, &status);
  366. trace_adreno_preempt_done(adreno_dev->cur_rb->id, adreno_dev->next_rb->id,
  367. status, 0);
  368. adreno_dev->prev_rb = adreno_dev->cur_rb;
  369. adreno_dev->cur_rb = adreno_dev->next_rb;
  370. adreno_dev->next_rb = NULL;
  371. /* Update the wptr if it changed while preemption was ongoing */
  372. _update_wptr(adreno_dev, true, true);
  373. /* Update the dispatcher timer for the new command queue */
  374. mod_timer(&adreno_dev->dispatcher.timer,
  375. adreno_dev->cur_rb->dispatch_q.expires);
  376. adreno_set_preempt_state(adreno_dev, ADRENO_PREEMPT_NONE);
  377. a6xx_preemption_trigger(adreno_dev, true);
  378. }
  379. void a6xx_preemption_schedule(struct adreno_device *adreno_dev)
  380. {
  381. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  382. if (!adreno_is_preemption_enabled(adreno_dev))
  383. return;
  384. mutex_lock(&device->mutex);
  385. if (adreno_in_preempt_state(adreno_dev, ADRENO_PREEMPT_COMPLETE))
  386. _a6xx_preemption_done(adreno_dev);
  387. a6xx_preemption_trigger(adreno_dev, false);
  388. mutex_unlock(&device->mutex);
  389. }
  390. u32 a6xx_preemption_pre_ibsubmit(struct adreno_device *adreno_dev,
  391. struct adreno_ringbuffer *rb, struct adreno_context *drawctxt,
  392. u32 *cmds)
  393. {
  394. unsigned int *cmds_orig = cmds;
  395. uint64_t gpuaddr = 0;
  396. if (!adreno_is_preemption_enabled(adreno_dev))
  397. return 0;
  398. if (drawctxt) {
  399. gpuaddr = drawctxt->base.user_ctxt_record->memdesc.gpuaddr;
  400. *cmds++ = cp_type7_packet(CP_SET_PSEUDO_REGISTER, 15);
  401. } else {
  402. *cmds++ = cp_type7_packet(CP_SET_PSEUDO_REGISTER, 12);
  403. }
  404. /* NULL SMMU_INFO buffer - we track in KMD */
  405. *cmds++ = SET_PSEUDO_SMMU_INFO;
  406. cmds += cp_gpuaddr(adreno_dev, cmds, 0x0);
  407. *cmds++ = SET_PSEUDO_PRIV_NON_SECURE_SAVE_ADDR;
  408. cmds += cp_gpuaddr(adreno_dev, cmds, rb->preemption_desc->gpuaddr);
  409. *cmds++ = SET_PSEUDO_PRIV_SECURE_SAVE_ADDR;
  410. cmds += cp_gpuaddr(adreno_dev, cmds,
  411. rb->secure_preemption_desc->gpuaddr);
  412. if (drawctxt) {
  413. *cmds++ = SET_PSEUDO_NON_PRIV_SAVE_ADDR;
  414. cmds += cp_gpuaddr(adreno_dev, cmds, gpuaddr);
  415. }
  416. /*
  417. * There is no need to specify this address when we are about to
  418. * trigger preemption. This is because CP internally stores this
  419. * address specified here in the CP_SET_PSEUDO_REGISTER payload to
  420. * the context record and thus knows from where to restore
  421. * the saved perfcounters for the new ringbuffer.
  422. */
  423. *cmds++ = SET_PSEUDO_COUNTER;
  424. cmds += cp_gpuaddr(adreno_dev, cmds,
  425. rb->perfcounter_save_restore_desc->gpuaddr);
  426. if (drawctxt) {
  427. struct adreno_ringbuffer *rb = drawctxt->rb;
  428. uint64_t dest = PREEMPT_SCRATCH_ADDR(adreno_dev, rb->id);
  429. *cmds++ = cp_mem_packet(adreno_dev, CP_MEM_WRITE, 2, 2);
  430. cmds += cp_gpuaddr(adreno_dev, cmds, dest);
  431. *cmds++ = lower_32_bits(gpuaddr);
  432. *cmds++ = upper_32_bits(gpuaddr);
  433. /* Add a KMD post amble to clear the perf counters during preemption */
  434. if (!adreno_dev->perfcounter) {
  435. u64 kmd_postamble_addr = SCRATCH_POSTAMBLE_ADDR(KGSL_DEVICE(adreno_dev));
  436. *cmds++ = cp_type7_packet(CP_SET_AMBLE, 3);
  437. *cmds++ = lower_32_bits(kmd_postamble_addr);
  438. *cmds++ = upper_32_bits(kmd_postamble_addr);
  439. *cmds++ = FIELD_PREP(GENMASK(22, 20), CP_KMD_AMBLE_TYPE)
  440. | (FIELD_PREP(GENMASK(19, 0), adreno_dev->preempt.postamble_len));
  441. }
  442. }
  443. return (unsigned int) (cmds - cmds_orig);
  444. }
  445. u32 a6xx_preemption_post_ibsubmit(struct adreno_device *adreno_dev,
  446. u32 *cmds)
  447. {
  448. u32 index = 0;
  449. if (!adreno_is_preemption_enabled(adreno_dev))
  450. return 0;
  451. if (adreno_dev->cur_rb) {
  452. u64 dest = PREEMPT_SCRATCH_ADDR(adreno_dev, adreno_dev->cur_rb->id);
  453. cmds[index++] = cp_type7_packet(CP_MEM_WRITE, 4);
  454. cmds[index++] = lower_32_bits(dest);
  455. cmds[index++] = upper_32_bits(dest);
  456. cmds[index++] = 0;
  457. cmds[index++] = 0;
  458. }
  459. cmds[index++] = cp_type7_packet(CP_CONTEXT_SWITCH_YIELD, 4);
  460. cmds[index++] = 0;
  461. cmds[index++] = 0;
  462. cmds[index++] = 1;
  463. cmds[index++] = 0;
  464. return index;
  465. }
  466. void a6xx_preemption_start(struct adreno_device *adreno_dev)
  467. {
  468. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  469. struct kgsl_iommu *iommu = KGSL_IOMMU(device);
  470. struct adreno_ringbuffer *rb;
  471. unsigned int i;
  472. if (!adreno_is_preemption_enabled(adreno_dev))
  473. return;
  474. /* Force the state to be clear */
  475. adreno_set_preempt_state(adreno_dev, ADRENO_PREEMPT_NONE);
  476. if (kgsl_mmu_is_perprocess(&device->mmu)) {
  477. /* smmu_info is allocated and mapped in a6xx_preemption_iommu_init */
  478. kgsl_sharedmem_writel(iommu->smmu_info,
  479. PREEMPT_SMMU_RECORD(magic), A6XX_CP_SMMU_INFO_MAGIC_REF);
  480. kgsl_sharedmem_writeq(iommu->smmu_info,
  481. PREEMPT_SMMU_RECORD(ttbr0), MMU_DEFAULT_TTBR0(device));
  482. /* The CP doesn't use the asid record, so poison it */
  483. kgsl_sharedmem_writel(iommu->smmu_info,
  484. PREEMPT_SMMU_RECORD(asid), 0xDECAFBAD);
  485. kgsl_sharedmem_writel(iommu->smmu_info,
  486. PREEMPT_SMMU_RECORD(context_idr), 0);
  487. kgsl_regwrite(device, A6XX_CP_CONTEXT_SWITCH_SMMU_INFO_LO,
  488. lower_32_bits(iommu->smmu_info->gpuaddr));
  489. kgsl_regwrite(device, A6XX_CP_CONTEXT_SWITCH_SMMU_INFO_HI,
  490. upper_32_bits(iommu->smmu_info->gpuaddr));
  491. }
  492. FOR_EACH_RINGBUFFER(adreno_dev, rb, i) {
  493. kgsl_sharedmem_writel(rb->preemption_desc,
  494. PREEMPT_RECORD(rptr), 0);
  495. kgsl_sharedmem_writel(rb->preemption_desc,
  496. PREEMPT_RECORD(wptr), 0);
  497. adreno_ringbuffer_set_pagetable(device, rb,
  498. device->mmu.defaultpagetable);
  499. }
  500. }
  501. static void reset_rb_preempt_record(struct adreno_device *adreno_dev,
  502. struct adreno_ringbuffer *rb)
  503. {
  504. u32 cp_rb_cntl = A6XX_CP_RB_CNTL_DEFAULT |
  505. (ADRENO_FEATURE(adreno_dev, ADRENO_APRIV) ? 0 : (1 << 27));
  506. memset(rb->preemption_desc->hostptr, 0x0, rb->preemption_desc->size);
  507. kgsl_sharedmem_writel(rb->preemption_desc,
  508. PREEMPT_RECORD(magic), A6XX_CP_CTXRECORD_MAGIC_REF);
  509. kgsl_sharedmem_writel(rb->preemption_desc,
  510. PREEMPT_RECORD(cntl), cp_rb_cntl);
  511. kgsl_sharedmem_writeq(rb->preemption_desc,
  512. PREEMPT_RECORD(rptr_addr), SCRATCH_RB_GPU_ADDR(
  513. KGSL_DEVICE(adreno_dev), rb->id, rptr));
  514. kgsl_sharedmem_writeq(rb->preemption_desc,
  515. PREEMPT_RECORD(rbase), rb->buffer_desc->gpuaddr);
  516. }
  517. void a6xx_reset_preempt_records(struct adreno_device *adreno_dev)
  518. {
  519. int i;
  520. struct adreno_ringbuffer *rb;
  521. if (!adreno_is_preemption_enabled(adreno_dev))
  522. return;
  523. FOR_EACH_RINGBUFFER(adreno_dev, rb, i) {
  524. reset_rb_preempt_record(adreno_dev, rb);
  525. }
  526. }
  527. static int a6xx_preemption_ringbuffer_init(struct adreno_device *adreno_dev,
  528. struct adreno_ringbuffer *rb)
  529. {
  530. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  531. const struct adreno_a6xx_core *a6xx_core = to_a6xx_core(adreno_dev);
  532. u64 ctxt_record_size = A6XX_CP_CTXRECORD_SIZE_IN_BYTES;
  533. int ret;
  534. if (a6xx_core->ctxt_record_size)
  535. ctxt_record_size = a6xx_core->ctxt_record_size;
  536. ret = adreno_allocate_global(device, &rb->preemption_desc,
  537. ctxt_record_size, SZ_16K, 0, KGSL_MEMDESC_PRIVILEGED,
  538. "preemption_desc");
  539. if (ret)
  540. return ret;
  541. ret = adreno_allocate_global(device, &rb->secure_preemption_desc,
  542. ctxt_record_size, 0, KGSL_MEMFLAGS_SECURE,
  543. KGSL_MEMDESC_PRIVILEGED, "preemption_desc");
  544. if (ret)
  545. return ret;
  546. ret = adreno_allocate_global(device, &rb->perfcounter_save_restore_desc,
  547. A6XX_CP_PERFCOUNTER_SAVE_RESTORE_SIZE, 0, 0,
  548. KGSL_MEMDESC_PRIVILEGED,
  549. "perfcounter_save_restore_desc");
  550. if (ret)
  551. return ret;
  552. reset_rb_preempt_record(adreno_dev, rb);
  553. return 0;
  554. }
  555. int a6xx_preemption_init(struct adreno_device *adreno_dev)
  556. {
  557. u32 flags = ADRENO_FEATURE(adreno_dev, ADRENO_APRIV) ? KGSL_MEMDESC_PRIVILEGED : 0;
  558. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  559. struct kgsl_iommu *iommu = KGSL_IOMMU(device);
  560. struct adreno_preemption *preempt = &adreno_dev->preempt;
  561. struct adreno_ringbuffer *rb;
  562. int ret;
  563. unsigned int i;
  564. /* We are dependent on IOMMU to make preemption go on the CP side */
  565. if (kgsl_mmu_get_mmutype(device) != KGSL_MMU_TYPE_IOMMU)
  566. return -ENODEV;
  567. INIT_WORK(&preempt->work, _a6xx_preemption_worker);
  568. /* Allocate mem for storing preemption switch record */
  569. FOR_EACH_RINGBUFFER(adreno_dev, rb, i) {
  570. ret = a6xx_preemption_ringbuffer_init(adreno_dev, rb);
  571. if (ret)
  572. return ret;
  573. }
  574. ret = adreno_allocate_global(device, &preempt->scratch,
  575. PAGE_SIZE, 0, 0, flags, "preempt_scratch");
  576. if (ret)
  577. return ret;
  578. /* Allocate mem for storing preemption smmu record */
  579. if (kgsl_mmu_is_perprocess(&device->mmu)) {
  580. ret = adreno_allocate_global(device, &iommu->smmu_info, PAGE_SIZE, 0,
  581. KGSL_MEMFLAGS_GPUREADONLY, KGSL_MEMDESC_PRIVILEGED,
  582. "smmu_info");
  583. if (ret)
  584. return ret;
  585. }
  586. /*
  587. * First 28 dwords of the device scratch buffer are used to store shadow rb data.
  588. * Reserve 11 dwords in the device scratch buffer from SCRATCH_POSTAMBLE_OFFSET for
  589. * KMD postamble pm4 packets. This should be in *device->scratch* so that userspace
  590. * cannot access it.
  591. */
  592. if (!adreno_dev->perfcounter) {
  593. u32 *postamble = device->scratch->hostptr + SCRATCH_POSTAMBLE_OFFSET;
  594. u32 count = 0;
  595. postamble[count++] = cp_type7_packet(CP_REG_RMW, 3);
  596. postamble[count++] = A6XX_RBBM_PERFCTR_SRAM_INIT_CMD;
  597. postamble[count++] = 0x0;
  598. postamble[count++] = 0x1;
  599. postamble[count++] = cp_type7_packet(CP_WAIT_REG_MEM, 6);
  600. postamble[count++] = 0x3;
  601. postamble[count++] = A6XX_RBBM_PERFCTR_SRAM_INIT_STATUS;
  602. postamble[count++] = 0x0;
  603. postamble[count++] = 0x1;
  604. postamble[count++] = 0x1;
  605. postamble[count++] = 0x0;
  606. preempt->postamble_len = count;
  607. }
  608. set_bit(ADRENO_DEVICE_PREEMPTION, &adreno_dev->priv);
  609. return 0;
  610. }
  611. int a6xx_preemption_context_init(struct kgsl_context *context)
  612. {
  613. struct kgsl_device *device = context->device;
  614. struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
  615. uint64_t flags = 0;
  616. if (!adreno_preemption_feature_set(adreno_dev))
  617. return 0;
  618. if (context->flags & KGSL_CONTEXT_SECURE)
  619. flags |= KGSL_MEMFLAGS_SECURE;
  620. if (is_compat_task())
  621. flags |= KGSL_MEMFLAGS_FORCE_32BIT;
  622. /*
  623. * gpumem_alloc_entry takes an extra refcount. Put it only when
  624. * destroying the context to keep the context record valid
  625. */
  626. context->user_ctxt_record = gpumem_alloc_entry(context->dev_priv,
  627. A6XX_CP_CTXRECORD_USER_RESTORE_SIZE, flags);
  628. if (IS_ERR(context->user_ctxt_record)) {
  629. int ret = PTR_ERR(context->user_ctxt_record);
  630. context->user_ctxt_record = NULL;
  631. return ret;
  632. }
  633. return 0;
  634. }