msm_submitqueue.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2017 The Linux Foundation. All rights reserved.
  3. */
  4. #include <linux/kref.h>
  5. #include <linux/uaccess.h>
  6. #include "msm_gpu.h"
  7. int msm_context_set_sysprof(struct msm_context *ctx, struct msm_gpu *gpu, int sysprof)
  8. {
  9. /*
  10. * Since pm_runtime and sysprof_active are both refcounts, we
  11. * call apply the new value first, and then unwind the previous
  12. * value
  13. */
  14. switch (sysprof) {
  15. default:
  16. return UERR(EINVAL, gpu->dev, "Invalid sysprof: %d", sysprof);
  17. case 2:
  18. pm_runtime_get_sync(&gpu->pdev->dev);
  19. fallthrough;
  20. case 1:
  21. refcount_inc(&gpu->sysprof_active);
  22. fallthrough;
  23. case 0:
  24. break;
  25. }
  26. /* unwind old value: */
  27. switch (ctx->sysprof) {
  28. case 2:
  29. pm_runtime_put_autosuspend(&gpu->pdev->dev);
  30. fallthrough;
  31. case 1:
  32. refcount_dec(&gpu->sysprof_active);
  33. fallthrough;
  34. case 0:
  35. break;
  36. }
  37. /* Some gpu families require additional setup for sysprof */
  38. if (gpu->funcs->sysprof_setup)
  39. gpu->funcs->sysprof_setup(gpu);
  40. ctx->sysprof = sysprof;
  41. return 0;
  42. }
  43. void __msm_context_destroy(struct kref *kref)
  44. {
  45. struct msm_context *ctx = container_of(kref,
  46. struct msm_context, ref);
  47. int i;
  48. for (i = 0; i < ARRAY_SIZE(ctx->entities); i++) {
  49. if (!ctx->entities[i])
  50. continue;
  51. drm_sched_entity_destroy(ctx->entities[i]);
  52. kfree(ctx->entities[i]);
  53. }
  54. drm_gpuvm_put(ctx->vm);
  55. kfree(ctx->comm);
  56. kfree(ctx->cmdline);
  57. kfree(ctx);
  58. }
  59. void msm_submitqueue_destroy(struct kref *kref)
  60. {
  61. struct msm_gpu_submitqueue *queue = container_of(kref,
  62. struct msm_gpu_submitqueue, ref);
  63. idr_destroy(&queue->fence_idr);
  64. if (queue->entity == &queue->_vm_bind_entity[0])
  65. drm_sched_entity_destroy(queue->entity);
  66. msm_context_put(queue->ctx);
  67. kfree(queue);
  68. }
  69. struct msm_gpu_submitqueue *msm_submitqueue_get(struct msm_context *ctx,
  70. u32 id)
  71. {
  72. struct msm_gpu_submitqueue *entry;
  73. if (!ctx)
  74. return NULL;
  75. read_lock(&ctx->queuelock);
  76. list_for_each_entry(entry, &ctx->submitqueues, node) {
  77. if (entry->id == id) {
  78. kref_get(&entry->ref);
  79. read_unlock(&ctx->queuelock);
  80. return entry;
  81. }
  82. }
  83. read_unlock(&ctx->queuelock);
  84. return NULL;
  85. }
  86. void msm_submitqueue_close(struct msm_context *ctx)
  87. {
  88. struct msm_gpu_submitqueue *queue, *tmp;
  89. if (!ctx)
  90. return;
  91. /*
  92. * No lock needed in close and there won't
  93. * be any more user ioctls coming our way
  94. */
  95. list_for_each_entry_safe(queue, tmp, &ctx->submitqueues, node) {
  96. if (queue->entity == &queue->_vm_bind_entity[0])
  97. drm_sched_entity_flush(queue->entity, MAX_WAIT_SCHED_ENTITY_Q_EMPTY);
  98. list_del(&queue->node);
  99. msm_submitqueue_put(queue);
  100. }
  101. if (!ctx->vm)
  102. return;
  103. msm_gem_vm_close(ctx->vm);
  104. }
  105. static struct drm_sched_entity *
  106. get_sched_entity(struct msm_context *ctx, struct msm_ringbuffer *ring,
  107. unsigned ring_nr, enum drm_sched_priority sched_prio)
  108. {
  109. static DEFINE_MUTEX(entity_lock);
  110. unsigned idx = (ring_nr * NR_SCHED_PRIORITIES) + sched_prio;
  111. /* We should have already validated that the requested priority is
  112. * valid by the time we get here.
  113. */
  114. if (WARN_ON(idx >= ARRAY_SIZE(ctx->entities)))
  115. return ERR_PTR(-EINVAL);
  116. mutex_lock(&entity_lock);
  117. if (!ctx->entities[idx]) {
  118. struct drm_sched_entity *entity;
  119. struct drm_gpu_scheduler *sched = &ring->sched;
  120. int ret;
  121. entity = kzalloc_obj(*ctx->entities[idx]);
  122. ret = drm_sched_entity_init(entity, sched_prio, &sched, 1, NULL);
  123. if (ret) {
  124. mutex_unlock(&entity_lock);
  125. kfree(entity);
  126. return ERR_PTR(ret);
  127. }
  128. ctx->entities[idx] = entity;
  129. }
  130. mutex_unlock(&entity_lock);
  131. return ctx->entities[idx];
  132. }
  133. int msm_submitqueue_create(struct drm_device *drm, struct msm_context *ctx,
  134. u32 prio, u32 flags, u32 *id)
  135. {
  136. struct msm_drm_private *priv = drm->dev_private;
  137. struct msm_gpu_submitqueue *queue;
  138. enum drm_sched_priority sched_prio;
  139. unsigned ring_nr;
  140. int ret;
  141. if (!ctx)
  142. return -ENODEV;
  143. if (!priv->gpu)
  144. return -ENODEV;
  145. if (flags & MSM_SUBMITQUEUE_VM_BIND) {
  146. unsigned sz;
  147. /* Not allowed for kernel managed VMs (ie. kernel allocs VA) */
  148. if (!msm_context_is_vmbind(ctx))
  149. return -EINVAL;
  150. if (prio)
  151. return -EINVAL;
  152. sz = struct_size(queue, _vm_bind_entity, 1);
  153. queue = kzalloc(sz, GFP_KERNEL);
  154. } else {
  155. extern int enable_preemption;
  156. bool preemption_supported =
  157. priv->gpu->nr_rings == 1 && enable_preemption != 0;
  158. if (flags & MSM_SUBMITQUEUE_ALLOW_PREEMPT && preemption_supported)
  159. return -EINVAL;
  160. ret = msm_gpu_convert_priority(priv->gpu, prio, &ring_nr, &sched_prio);
  161. if (ret)
  162. return ret;
  163. queue = kzalloc_obj(*queue);
  164. }
  165. if (!queue)
  166. return -ENOMEM;
  167. kref_init(&queue->ref);
  168. queue->flags = flags;
  169. if (flags & MSM_SUBMITQUEUE_VM_BIND) {
  170. struct drm_gpu_scheduler *sched = &to_msm_vm(msm_context_vm(drm, ctx))->sched;
  171. queue->entity = &queue->_vm_bind_entity[0];
  172. drm_sched_entity_init(queue->entity, DRM_SCHED_PRIORITY_KERNEL,
  173. &sched, 1, NULL);
  174. } else {
  175. queue->ring_nr = ring_nr;
  176. queue->entity = get_sched_entity(ctx, priv->gpu->rb[ring_nr],
  177. ring_nr, sched_prio);
  178. }
  179. if (IS_ERR(queue->entity)) {
  180. ret = PTR_ERR(queue->entity);
  181. kfree(queue);
  182. return ret;
  183. }
  184. write_lock(&ctx->queuelock);
  185. queue->ctx = msm_context_get(ctx);
  186. queue->id = ctx->queueid++;
  187. if (id)
  188. *id = queue->id;
  189. idr_init(&queue->fence_idr);
  190. spin_lock_init(&queue->idr_lock);
  191. mutex_init(&queue->lock);
  192. list_add_tail(&queue->node, &ctx->submitqueues);
  193. write_unlock(&ctx->queuelock);
  194. return 0;
  195. }
  196. /*
  197. * Create the default submit-queue (id==0), used for backwards compatibility
  198. * for userspace that pre-dates the introduction of submitqueues.
  199. */
  200. int msm_submitqueue_init(struct drm_device *drm, struct msm_context *ctx)
  201. {
  202. struct msm_drm_private *priv = drm->dev_private;
  203. int default_prio, max_priority;
  204. if (!priv->gpu)
  205. return -ENODEV;
  206. max_priority = (priv->gpu->nr_rings * NR_SCHED_PRIORITIES) - 1;
  207. /*
  208. * Pick a medium priority level as default. Lower numeric value is
  209. * higher priority, so round-up to pick a priority that is not higher
  210. * than the middle priority level.
  211. */
  212. default_prio = DIV_ROUND_UP(max_priority, 2);
  213. return msm_submitqueue_create(drm, ctx, default_prio, 0, NULL);
  214. }
  215. static int msm_submitqueue_query_faults(struct msm_gpu_submitqueue *queue,
  216. struct drm_msm_submitqueue_query *args)
  217. {
  218. size_t size = min_t(size_t, args->len, sizeof(queue->faults));
  219. int ret;
  220. /* If a zero length was passed in, return the data size we expect */
  221. if (!args->len) {
  222. args->len = sizeof(queue->faults);
  223. return 0;
  224. }
  225. /* Set the length to the actual size of the data */
  226. args->len = size;
  227. ret = copy_to_user(u64_to_user_ptr(args->data), &queue->faults, size);
  228. return ret ? -EFAULT : 0;
  229. }
  230. int msm_submitqueue_query(struct drm_device *drm, struct msm_context *ctx,
  231. struct drm_msm_submitqueue_query *args)
  232. {
  233. struct msm_gpu_submitqueue *queue;
  234. int ret = -EINVAL;
  235. if (args->pad)
  236. return -EINVAL;
  237. queue = msm_submitqueue_get(ctx, args->id);
  238. if (!queue)
  239. return -ENOENT;
  240. if (args->param == MSM_SUBMITQUEUE_PARAM_FAULTS)
  241. ret = msm_submitqueue_query_faults(queue, args);
  242. msm_submitqueue_put(queue);
  243. return ret;
  244. }
  245. int msm_submitqueue_remove(struct msm_context *ctx, u32 id)
  246. {
  247. struct msm_gpu_submitqueue *entry;
  248. if (!ctx)
  249. return 0;
  250. /*
  251. * id 0 is the "default" queue and can't be destroyed
  252. * by the user
  253. */
  254. if (!id)
  255. return -ENOENT;
  256. write_lock(&ctx->queuelock);
  257. list_for_each_entry(entry, &ctx->submitqueues, node) {
  258. if (entry->id == id) {
  259. list_del(&entry->node);
  260. write_unlock(&ctx->queuelock);
  261. msm_submitqueue_put(entry);
  262. return 0;
  263. }
  264. }
  265. write_unlock(&ctx->queuelock);
  266. return -ENOENT;
  267. }