msm_ringbuffer.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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->cur = ring->start;
  44. ring->memptrs = memptrs;
  45. ring->memptrs_iova = memptrs_iova;
  46. INIT_LIST_HEAD(&ring->submits);
  47. snprintf(name, sizeof(name), "gpu-ring-%d", ring->id);
  48. ring->fctx = msm_fence_context_alloc(gpu->dev, name);
  49. return ring;
  50. fail:
  51. msm_ringbuffer_destroy(ring);
  52. return ERR_PTR(ret);
  53. }
  54. void msm_ringbuffer_destroy(struct msm_ringbuffer *ring)
  55. {
  56. if (IS_ERR_OR_NULL(ring))
  57. return;
  58. msm_fence_context_free(ring->fctx);
  59. if (ring->bo) {
  60. msm_gem_put_iova(ring->bo, ring->gpu->aspace);
  61. msm_gem_put_vaddr(ring->bo);
  62. drm_gem_object_unreference_unlocked(ring->bo);
  63. }
  64. kfree(ring);
  65. }