etnaviv_cmdbuf.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2017-2018 Etnaviv Project
  4. */
  5. #include <drm/drm_mm.h>
  6. #include "etnaviv_cmdbuf.h"
  7. #include "etnaviv_gpu.h"
  8. #include "etnaviv_mmu.h"
  9. #include "etnaviv_perfmon.h"
  10. #define SUBALLOC_SIZE SZ_256K
  11. #define SUBALLOC_GRANULE SZ_4K
  12. #define SUBALLOC_GRANULES (SUBALLOC_SIZE / SUBALLOC_GRANULE)
  13. struct etnaviv_cmdbuf_suballoc {
  14. /* suballocated dma buffer properties */
  15. struct etnaviv_gpu *gpu;
  16. void *vaddr;
  17. dma_addr_t paddr;
  18. /* GPU mapping */
  19. u32 iova;
  20. struct drm_mm_node vram_node; /* only used on MMUv2 */
  21. /* allocation management */
  22. struct mutex lock;
  23. DECLARE_BITMAP(granule_map, SUBALLOC_GRANULES);
  24. int free_space;
  25. wait_queue_head_t free_event;
  26. };
  27. struct etnaviv_cmdbuf_suballoc *
  28. etnaviv_cmdbuf_suballoc_new(struct etnaviv_gpu * gpu)
  29. {
  30. struct etnaviv_cmdbuf_suballoc *suballoc;
  31. int ret;
  32. suballoc = kzalloc(sizeof(*suballoc), GFP_KERNEL);
  33. if (!suballoc)
  34. return ERR_PTR(-ENOMEM);
  35. suballoc->gpu = gpu;
  36. mutex_init(&suballoc->lock);
  37. init_waitqueue_head(&suballoc->free_event);
  38. suballoc->vaddr = dma_alloc_wc(gpu->dev, SUBALLOC_SIZE,
  39. &suballoc->paddr, GFP_KERNEL);
  40. if (!suballoc->vaddr)
  41. goto free_suballoc;
  42. ret = etnaviv_iommu_get_suballoc_va(gpu, suballoc->paddr,
  43. &suballoc->vram_node, SUBALLOC_SIZE,
  44. &suballoc->iova);
  45. if (ret)
  46. goto free_dma;
  47. return suballoc;
  48. free_dma:
  49. dma_free_wc(gpu->dev, SUBALLOC_SIZE, suballoc->vaddr, suballoc->paddr);
  50. free_suballoc:
  51. kfree(suballoc);
  52. return NULL;
  53. }
  54. void etnaviv_cmdbuf_suballoc_destroy(struct etnaviv_cmdbuf_suballoc *suballoc)
  55. {
  56. etnaviv_iommu_put_suballoc_va(suballoc->gpu, &suballoc->vram_node,
  57. SUBALLOC_SIZE, suballoc->iova);
  58. dma_free_wc(suballoc->gpu->dev, SUBALLOC_SIZE, suballoc->vaddr,
  59. suballoc->paddr);
  60. kfree(suballoc);
  61. }
  62. int etnaviv_cmdbuf_init(struct etnaviv_cmdbuf_suballoc *suballoc,
  63. struct etnaviv_cmdbuf *cmdbuf, u32 size)
  64. {
  65. int granule_offs, order, ret;
  66. cmdbuf->suballoc = suballoc;
  67. cmdbuf->size = size;
  68. order = order_base_2(ALIGN(size, SUBALLOC_GRANULE) / SUBALLOC_GRANULE);
  69. retry:
  70. mutex_lock(&suballoc->lock);
  71. granule_offs = bitmap_find_free_region(suballoc->granule_map,
  72. SUBALLOC_GRANULES, order);
  73. if (granule_offs < 0) {
  74. suballoc->free_space = 0;
  75. mutex_unlock(&suballoc->lock);
  76. ret = wait_event_interruptible_timeout(suballoc->free_event,
  77. suballoc->free_space,
  78. msecs_to_jiffies(10 * 1000));
  79. if (!ret) {
  80. dev_err(suballoc->gpu->dev,
  81. "Timeout waiting for cmdbuf space\n");
  82. return -ETIMEDOUT;
  83. }
  84. goto retry;
  85. }
  86. mutex_unlock(&suballoc->lock);
  87. cmdbuf->suballoc_offset = granule_offs * SUBALLOC_GRANULE;
  88. cmdbuf->vaddr = suballoc->vaddr + cmdbuf->suballoc_offset;
  89. return 0;
  90. }
  91. void etnaviv_cmdbuf_free(struct etnaviv_cmdbuf *cmdbuf)
  92. {
  93. struct etnaviv_cmdbuf_suballoc *suballoc = cmdbuf->suballoc;
  94. int order = order_base_2(ALIGN(cmdbuf->size, SUBALLOC_GRANULE) /
  95. SUBALLOC_GRANULE);
  96. mutex_lock(&suballoc->lock);
  97. bitmap_release_region(suballoc->granule_map,
  98. cmdbuf->suballoc_offset / SUBALLOC_GRANULE,
  99. order);
  100. suballoc->free_space = 1;
  101. mutex_unlock(&suballoc->lock);
  102. wake_up_all(&suballoc->free_event);
  103. }
  104. u32 etnaviv_cmdbuf_get_va(struct etnaviv_cmdbuf *buf)
  105. {
  106. return buf->suballoc->iova + buf->suballoc_offset;
  107. }
  108. dma_addr_t etnaviv_cmdbuf_get_pa(struct etnaviv_cmdbuf *buf)
  109. {
  110. return buf->suballoc->paddr + buf->suballoc_offset;
  111. }