adreno_ringbuffer.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2002,2007-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/interconnect.h>
  7. #include <linux/sched/clock.h>
  8. #include <linux/slab.h>
  9. #include <soc/qcom/dcvs.h>
  10. #include "a3xx_reg.h"
  11. #include "a5xx_reg.h"
  12. #include "a6xx_reg.h"
  13. #include "adreno.h"
  14. #include "adreno_pm4types.h"
  15. #include "adreno_ringbuffer.h"
  16. #include "adreno_trace.h"
  17. #include "kgsl_trace.h"
  18. #define RB_HOSTPTR(_rb, _pos) \
  19. ((unsigned int *) ((_rb)->buffer_desc->hostptr + \
  20. ((_pos) * sizeof(unsigned int))))
  21. #define RB_GPUADDR(_rb, _pos) \
  22. ((_rb)->buffer_desc->gpuaddr + ((_pos) * sizeof(unsigned int)))
  23. void adreno_get_submit_time(struct adreno_device *adreno_dev,
  24. struct adreno_ringbuffer *rb,
  25. struct adreno_submit_time *time)
  26. {
  27. const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
  28. unsigned long flags;
  29. struct adreno_context *drawctxt = rb->drawctxt_active;
  30. struct kgsl_context *context = &drawctxt->base;
  31. if (!time)
  32. return;
  33. /*
  34. * Here we are attempting to create a mapping between the
  35. * GPU time domain (alwayson counter) and the CPU time domain
  36. * (local_clock) by sampling both values as close together as
  37. * possible. This is useful for many types of debugging and
  38. * profiling. In order to make this mapping as accurate as
  39. * possible, we must turn off interrupts to avoid running
  40. * interrupt handlers between the two samples.
  41. */
  42. local_irq_save(flags);
  43. time->ticks = gpudev->read_alwayson(adreno_dev);
  44. /* Trace the GPU time to create a mapping to ftrace time */
  45. trace_adreno_cmdbatch_sync(context->id, context->priority,
  46. drawctxt->timestamp, time->ticks);
  47. /* Get the kernel clock for time since boot */
  48. time->ktime = local_clock();
  49. /* Get the timeofday for the wall time (for the user) */
  50. ktime_get_real_ts64(&time->utime);
  51. local_irq_restore(flags);
  52. }
  53. unsigned int *adreno_ringbuffer_allocspace(struct adreno_ringbuffer *rb,
  54. unsigned int dwords)
  55. {
  56. struct adreno_device *adreno_dev = ADRENO_RB_DEVICE(rb);
  57. unsigned int rptr = adreno_get_rptr(rb);
  58. unsigned int ret;
  59. if (rptr <= rb->_wptr) {
  60. unsigned int *cmds;
  61. if (rb->_wptr + dwords <= (KGSL_RB_DWORDS - 2)) {
  62. ret = rb->_wptr;
  63. rb->_wptr = (rb->_wptr + dwords) % KGSL_RB_DWORDS;
  64. return RB_HOSTPTR(rb, ret);
  65. }
  66. /*
  67. * There isn't enough space toward the end of ringbuffer. So
  68. * look for space from the beginning of ringbuffer upto the
  69. * read pointer.
  70. */
  71. if (dwords < rptr) {
  72. cmds = RB_HOSTPTR(rb, rb->_wptr);
  73. *cmds = cp_packet(adreno_dev, CP_NOP,
  74. KGSL_RB_DWORDS - rb->_wptr - 1);
  75. rb->_wptr = dwords;
  76. return RB_HOSTPTR(rb, 0);
  77. }
  78. }
  79. if (rb->_wptr + dwords < rptr) {
  80. ret = rb->_wptr;
  81. rb->_wptr = (rb->_wptr + dwords) % KGSL_RB_DWORDS;
  82. return RB_HOSTPTR(rb, ret);
  83. }
  84. return ERR_PTR(-ENOSPC);
  85. }
  86. void adreno_ringbuffer_stop(struct adreno_device *adreno_dev)
  87. {
  88. struct adreno_ringbuffer *rb;
  89. int i;
  90. FOR_EACH_RINGBUFFER(adreno_dev, rb, i)
  91. kgsl_cancel_events(KGSL_DEVICE(adreno_dev), &(rb->events));
  92. }
  93. static int _rb_readtimestamp(struct kgsl_device *device,
  94. void *priv, enum kgsl_timestamp_type type,
  95. unsigned int *timestamp)
  96. {
  97. return adreno_rb_readtimestamp(ADRENO_DEVICE(device), priv, type,
  98. timestamp);
  99. }
  100. int adreno_ringbuffer_setup(struct adreno_device *adreno_dev,
  101. struct adreno_ringbuffer *rb, int id)
  102. {
  103. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  104. unsigned int priv = 0;
  105. int ret;
  106. /* allocate a chunk of memory to create user profiling IB1s */
  107. adreno_allocate_global(device, &rb->profile_desc, PAGE_SIZE,
  108. 0, KGSL_MEMFLAGS_GPUREADONLY, 0, "profile_desc");
  109. if (ADRENO_FEATURE(adreno_dev, ADRENO_APRIV))
  110. priv |= KGSL_MEMDESC_PRIVILEGED;
  111. ret = adreno_allocate_global(device, &rb->buffer_desc, KGSL_RB_SIZE,
  112. SZ_4K, KGSL_MEMFLAGS_GPUREADONLY, priv, "ringbuffer");
  113. if (ret)
  114. return ret;
  115. if (!list_empty(&rb->events.group))
  116. return 0;
  117. rb->id = id;
  118. kgsl_add_event_group(device, &rb->events, NULL, _rb_readtimestamp, rb,
  119. "rb_events-%d", id);
  120. rb->timestamp = 0;
  121. init_waitqueue_head(&rb->ts_expire_waitq);
  122. spin_lock_init(&rb->preempt_lock);
  123. return 0;
  124. }
  125. void adreno_preemption_timer(struct timer_list *t)
  126. {
  127. struct adreno_preemption *preempt = from_timer(preempt, t, timer);
  128. struct adreno_device *adreno_dev = container_of(preempt,
  129. struct adreno_device, preempt);
  130. /* We should only be here from a triggered state */
  131. if (!adreno_move_preempt_state(adreno_dev,
  132. ADRENO_PREEMPT_TRIGGERED, ADRENO_PREEMPT_FAULTED))
  133. return;
  134. /* Schedule the worker to take care of the details */
  135. queue_work(system_unbound_wq, &adreno_dev->preempt.work);
  136. }
  137. void adreno_drawobj_set_constraint(struct kgsl_device *device,
  138. struct kgsl_drawobj *drawobj)
  139. {
  140. struct kgsl_context *context = drawobj->context;
  141. unsigned long flags = drawobj->flags;
  142. /*
  143. * Check if the context has a constraint and constraint flags are
  144. * set.
  145. */
  146. if (context->pwr_constraint.type &&
  147. ((context->flags & KGSL_CONTEXT_PWR_CONSTRAINT) ||
  148. (drawobj->flags & KGSL_CONTEXT_PWR_CONSTRAINT)))
  149. kgsl_pwrctrl_set_constraint(device, &context->pwr_constraint,
  150. context->id, drawobj->timestamp);
  151. if (context->l3_pwr_constraint.type &&
  152. ((context->flags & KGSL_CONTEXT_PWR_CONSTRAINT) ||
  153. (flags & KGSL_CONTEXT_PWR_CONSTRAINT))) {
  154. if (!device->num_l3_pwrlevels) {
  155. dev_err_once(device->dev,
  156. "l3 voting not available\n");
  157. return;
  158. }
  159. switch (context->l3_pwr_constraint.type) {
  160. case KGSL_CONSTRAINT_L3_PWRLEVEL: {
  161. unsigned int sub_type;
  162. unsigned int new_l3;
  163. int ret = 0;
  164. struct dcvs_freq freq = {0};
  165. if (!device->l3_vote)
  166. return;
  167. sub_type = context->l3_pwr_constraint.sub_type;
  168. /*
  169. * If an L3 constraint is already set, set the new
  170. * one only if it is higher.
  171. */
  172. new_l3 = max_t(unsigned int, sub_type + 1,
  173. device->cur_l3_pwrlevel);
  174. new_l3 = min_t(unsigned int, new_l3,
  175. device->num_l3_pwrlevels - 1);
  176. if (device->cur_l3_pwrlevel == new_l3)
  177. return;
  178. freq.ib = device->l3_freq[new_l3];
  179. freq.hw_type = DCVS_L3;
  180. ret = qcom_dcvs_update_votes(KGSL_L3_DEVICE, &freq, 1,
  181. DCVS_SLOW_PATH);
  182. if (!ret) {
  183. trace_kgsl_constraint(device,
  184. KGSL_CONSTRAINT_L3_PWRLEVEL, new_l3, 1);
  185. device->cur_l3_pwrlevel = new_l3;
  186. } else {
  187. dev_err_ratelimited(device->dev,
  188. "Could not set l3_vote: %d\n",
  189. ret);
  190. }
  191. break;
  192. }
  193. }
  194. }
  195. }
  196. int adreno_ringbuffer_submitcmd(struct adreno_device *adreno_dev,
  197. struct kgsl_drawobj_cmd *cmdobj,
  198. struct adreno_submit_time *time)
  199. {
  200. struct adreno_submit_time local = { 0 };
  201. struct kgsl_drawobj *drawobj = DRAWOBJ(cmdobj);
  202. struct adreno_context *drawctxt = ADRENO_CONTEXT(drawobj->context);
  203. struct adreno_ringbuffer *rb = drawctxt->rb;
  204. const struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
  205. u32 flags = 0;
  206. int ret;
  207. /*
  208. * If SKIP CMD flag is set for current context
  209. * a) set SKIPCMD as fault_recovery for current commandbatch
  210. * b) store context's commandbatch fault_policy in current
  211. * commandbatch fault_policy and clear context's commandbatch
  212. * fault_policy
  213. * c) force preamble for commandbatch
  214. */
  215. if (test_bit(ADRENO_CONTEXT_SKIP_CMD, &drawctxt->base.priv) &&
  216. (!test_bit(CMDOBJ_SKIP, &cmdobj->priv))) {
  217. set_bit(KGSL_FT_SKIPCMD, &cmdobj->fault_recovery);
  218. cmdobj->fault_policy = drawctxt->fault_policy;
  219. set_bit(CMDOBJ_FORCE_PREAMBLE, &cmdobj->priv);
  220. /* if context is detached print fault recovery */
  221. adreno_fault_skipcmd_detached(adreno_dev, drawctxt, drawobj);
  222. /* clear the drawctxt flags */
  223. clear_bit(ADRENO_CONTEXT_SKIP_CMD, &drawctxt->base.priv);
  224. drawctxt->fault_policy = 0;
  225. }
  226. /* Check if user profiling should be enabled */
  227. if ((drawobj->flags & KGSL_DRAWOBJ_PROFILING) &&
  228. cmdobj->profiling_buf_entry) {
  229. flags |= F_USER_PROFILE;
  230. /*
  231. * we want to use an adreno_submit_time struct to get the
  232. * precise moment when the command is submitted to the
  233. * ringbuffer. If an upstream caller already passed down a
  234. * pointer piggyback on that otherwise use a local struct
  235. */
  236. if (!time)
  237. time = &local;
  238. time->drawobj = drawobj;
  239. }
  240. flags |= F_PREAMBLE;
  241. /*
  242. * When preamble is enabled, the preamble buffer with state restoration
  243. * commands are stored in the first node of the IB chain.
  244. * We can skip that if a context switch hasn't occurred.
  245. */
  246. if ((drawctxt->base.flags & KGSL_CONTEXT_PREAMBLE) &&
  247. !test_bit(CMDOBJ_FORCE_PREAMBLE, &cmdobj->priv) &&
  248. (rb->drawctxt_active == drawctxt))
  249. flags &= ~F_PREAMBLE;
  250. /*
  251. * In skip mode don't issue the draw IBs but keep all the other
  252. * accoutrements of a submision (including the interrupt) to keep
  253. * the accounting sane. Set start_index and numibs to 0 to just
  254. * generate the start and end markers and skip everything else
  255. */
  256. if (test_bit(CMDOBJ_SKIP, &cmdobj->priv)) {
  257. flags &= ~F_PREAMBLE;
  258. flags |= F_SKIP;
  259. }
  260. /* Enable kernel profiling */
  261. if (test_bit(CMDOBJ_PROFILE, &cmdobj->priv))
  262. flags |= F_KERNEL_PROFILE;
  263. /* Add a WFI to the end of the submission */
  264. if (test_bit(CMDOBJ_WFI, &cmdobj->priv))
  265. flags |= F_WFI;
  266. /*
  267. * For some targets, we need to execute a dummy shader operation after a
  268. * power collapse
  269. */
  270. if (test_and_clear_bit(ADRENO_DEVICE_PWRON, &adreno_dev->priv) &&
  271. test_bit(ADRENO_DEVICE_PWRON_FIXUP, &adreno_dev->priv))
  272. flags |= F_PWRON_FIXUP;
  273. /* Check to see the submission should be secure */
  274. if (drawobj->context->flags & KGSL_CONTEXT_SECURE)
  275. flags |= F_SECURE;
  276. /* process any profiling results that are available into the log_buf */
  277. adreno_profile_process_results(adreno_dev);
  278. ret = gpudev->ringbuffer_submitcmd(adreno_dev, cmdobj,
  279. flags, time);
  280. if (!ret) {
  281. set_bit(KGSL_CONTEXT_PRIV_SUBMITTED, &drawobj->context->priv);
  282. cmdobj->global_ts = drawctxt->internal_timestamp;
  283. }
  284. return ret;
  285. }
  286. /**
  287. * adreno_ringbuffer_wait_callback() - Callback function for event registered
  288. * on a ringbuffer timestamp
  289. * @device: Device for which the callback is valid
  290. * @context: The context of the event
  291. * @priv: The private parameter of the event
  292. * @result: Result of the event trigger
  293. */
  294. static void adreno_ringbuffer_wait_callback(struct kgsl_device *device,
  295. struct kgsl_event_group *group,
  296. void *priv, int result)
  297. {
  298. struct adreno_ringbuffer *rb = group->priv;
  299. wake_up_all(&rb->ts_expire_waitq);
  300. }
  301. /* check if timestamp is greater than the current rb timestamp */
  302. static inline int adreno_ringbuffer_check_timestamp(
  303. struct adreno_ringbuffer *rb,
  304. unsigned int timestamp, int type)
  305. {
  306. struct adreno_device *adreno_dev = ADRENO_RB_DEVICE(rb);
  307. unsigned int ts;
  308. adreno_rb_readtimestamp(adreno_dev, rb, type, &ts);
  309. return (timestamp_cmp(ts, timestamp) >= 0);
  310. }
  311. /**
  312. * adreno_ringbuffer_waittimestamp() - Wait for a RB timestamp
  313. * @rb: The ringbuffer to wait on
  314. * @timestamp: The timestamp to wait for
  315. * @msecs: The wait timeout period
  316. */
  317. int adreno_ringbuffer_waittimestamp(struct adreno_ringbuffer *rb,
  318. unsigned int timestamp,
  319. unsigned int msecs)
  320. {
  321. struct adreno_device *adreno_dev = ADRENO_RB_DEVICE(rb);
  322. struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
  323. int ret;
  324. unsigned long wait_time;
  325. /* check immediately if timeout is 0 */
  326. if (msecs == 0)
  327. return adreno_ringbuffer_check_timestamp(rb,
  328. timestamp, KGSL_TIMESTAMP_RETIRED) ? 0 : -EBUSY;
  329. ret = kgsl_add_event(device, &rb->events, timestamp,
  330. adreno_ringbuffer_wait_callback, NULL);
  331. if (ret)
  332. return ret;
  333. mutex_unlock(&device->mutex);
  334. wait_time = msecs_to_jiffies(msecs);
  335. if (wait_event_timeout(rb->ts_expire_waitq,
  336. !kgsl_event_pending(device, &rb->events, timestamp,
  337. adreno_ringbuffer_wait_callback, NULL),
  338. wait_time) == 0)
  339. ret = -ETIMEDOUT;
  340. mutex_lock(&device->mutex);
  341. /*
  342. * after wake up make sure that expected timestamp has retired
  343. * because the wakeup could have happened due to a cancel event
  344. */
  345. if (!ret && !adreno_ringbuffer_check_timestamp(rb,
  346. timestamp, KGSL_TIMESTAMP_RETIRED)) {
  347. ret = -EAGAIN;
  348. }
  349. return ret;
  350. }