msm_ringbuffer.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (C) 2013 Red Hat
  3. * Author: Rob Clark <robdclark@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "msm_ringbuffer.h"
  18. #include "msm_gpu.h"
  19. struct msm_ringbuffer *msm_ringbuffer_new(struct msm_gpu *gpu, int id,
  20. void *memptrs, uint64_t memptrs_iova)
  21. {
  22. struct msm_ringbuffer *ring;
  23. char name[32];
  24. int ret;
  25. /* We assume everwhere that MSM_GPU_RINGBUFFER_SZ is a power of 2 */
  26. BUILD_BUG_ON(!is_power_of_2(MSM_GPU_RINGBUFFER_SZ));
  27. ring = kzalloc(sizeof(*ring), GFP_KERNEL);
  28. if (!ring) {
  29. ret = -ENOMEM;
  30. goto fail;
  31. }
  32. ring->gpu = gpu;
  33. ring->id = id;
  34. /* Pass NULL for the iova pointer - we will map it later */
  35. ring->start = msm_gem_kernel_new(gpu->dev, MSM_GPU_RINGBUFFER_SZ,
  36. MSM_BO_WC, gpu->aspace, &ring->bo, NULL);
  37. if (IS_ERR(ring->start)) {
  38. ret = PTR_ERR(ring->start);
  39. ring->start = 0;
  40. goto fail;
  41. }
  42. ring->end = ring->start + (MSM_GPU_RINGBUFFER_SZ >> 2);
  43. ring->next = ring->start;
  44. ring->cur = ring->start;
  45. ring->memptrs = memptrs;
  46. ring->memptrs_iova = memptrs_iova;
  47. INIT_LIST_HEAD(&ring->submits);
  48. spin_lock_init(&ring->lock);
  49. snprintf(name, sizeof(name), "gpu-ring-%d", ring->id);
  50. ring->fctx = msm_fence_context_alloc(gpu->dev, name);
  51. return ring;
  52. fail:
  53. msm_ringbuffer_destroy(ring);
  54. return ERR_PTR(ret);
  55. }
  56. void msm_ringbuffer_destroy(struct msm_ringbuffer *ring)
  57. {
  58. if (IS_ERR_OR_NULL(ring))
  59. return;
  60. msm_fence_context_free(ring->fctx);
  61. if (ring->bo) {
  62. msm_gem_put_iova(ring->bo, ring->gpu->aspace);
  63. msm_gem_put_vaddr(ring->bo);
  64. drm_gem_object_put_unlocked(ring->bo);
  65. }
  66. kfree(ring);
  67. }