msm_submitqueue.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* Copyright (c) 2017 The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. */
  13. #include <linux/kref.h>
  14. #include "msm_gpu.h"
  15. void msm_submitqueue_destroy(struct kref *kref)
  16. {
  17. struct msm_gpu_submitqueue *queue = container_of(kref,
  18. struct msm_gpu_submitqueue, ref);
  19. kfree(queue);
  20. }
  21. struct msm_gpu_submitqueue *msm_submitqueue_get(struct msm_file_private *ctx,
  22. u32 id)
  23. {
  24. struct msm_gpu_submitqueue *entry;
  25. if (!ctx)
  26. return NULL;
  27. read_lock(&ctx->queuelock);
  28. list_for_each_entry(entry, &ctx->submitqueues, node) {
  29. if (entry->id == id) {
  30. kref_get(&entry->ref);
  31. read_unlock(&ctx->queuelock);
  32. return entry;
  33. }
  34. }
  35. read_unlock(&ctx->queuelock);
  36. return NULL;
  37. }
  38. void msm_submitqueue_close(struct msm_file_private *ctx)
  39. {
  40. struct msm_gpu_submitqueue *entry, *tmp;
  41. if (!ctx)
  42. return;
  43. /*
  44. * No lock needed in close and there won't
  45. * be any more user ioctls coming our way
  46. */
  47. list_for_each_entry_safe(entry, tmp, &ctx->submitqueues, node)
  48. msm_submitqueue_put(entry);
  49. }
  50. int msm_submitqueue_create(struct drm_device *drm, struct msm_file_private *ctx,
  51. u32 prio, u32 flags, u32 *id)
  52. {
  53. struct msm_drm_private *priv = drm->dev_private;
  54. struct msm_gpu_submitqueue *queue;
  55. if (!ctx)
  56. return -ENODEV;
  57. queue = kzalloc(sizeof(*queue), GFP_KERNEL);
  58. if (!queue)
  59. return -ENOMEM;
  60. kref_init(&queue->ref);
  61. queue->flags = flags;
  62. if (priv->gpu) {
  63. if (prio >= priv->gpu->nr_rings)
  64. return -EINVAL;
  65. queue->prio = prio;
  66. }
  67. write_lock(&ctx->queuelock);
  68. queue->id = ctx->queueid++;
  69. if (id)
  70. *id = queue->id;
  71. list_add_tail(&queue->node, &ctx->submitqueues);
  72. write_unlock(&ctx->queuelock);
  73. return 0;
  74. }
  75. int msm_submitqueue_init(struct drm_device *drm, struct msm_file_private *ctx)
  76. {
  77. struct msm_drm_private *priv = drm->dev_private;
  78. int default_prio;
  79. if (!ctx)
  80. return 0;
  81. /*
  82. * Select priority 2 as the "default priority" unless nr_rings is less
  83. * than 2 and then pick the lowest pirority
  84. */
  85. default_prio = priv->gpu ?
  86. clamp_t(uint32_t, 2, 0, priv->gpu->nr_rings - 1) : 0;
  87. INIT_LIST_HEAD(&ctx->submitqueues);
  88. rwlock_init(&ctx->queuelock);
  89. return msm_submitqueue_create(drm, ctx, default_prio, 0, NULL);
  90. }
  91. int msm_submitqueue_remove(struct msm_file_private *ctx, u32 id)
  92. {
  93. struct msm_gpu_submitqueue *entry;
  94. if (!ctx)
  95. return 0;
  96. /*
  97. * id 0 is the "default" queue and can't be destroyed
  98. * by the user
  99. */
  100. if (!id)
  101. return -ENOENT;
  102. write_lock(&ctx->queuelock);
  103. list_for_each_entry(entry, &ctx->submitqueues, node) {
  104. if (entry->id == id) {
  105. list_del(&entry->node);
  106. write_unlock(&ctx->queuelock);
  107. msm_submitqueue_put(entry);
  108. return 0;
  109. }
  110. }
  111. write_unlock(&ctx->queuelock);
  112. return -ENOENT;
  113. }