etnaviv_cmdbuf.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (C) 2017 Etnaviv Project
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License version 2 as published by
  6. * the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <drm/drm_mm.h>
  17. #include "etnaviv_cmdbuf.h"
  18. #include "etnaviv_gpu.h"
  19. #include "etnaviv_mmu.h"
  20. #include "etnaviv_perfmon.h"
  21. #define SUBALLOC_SIZE SZ_256K
  22. #define SUBALLOC_GRANULE SZ_4K
  23. #define SUBALLOC_GRANULES (SUBALLOC_SIZE / SUBALLOC_GRANULE)
  24. struct etnaviv_cmdbuf_suballoc {
  25. /* suballocated dma buffer properties */
  26. struct etnaviv_gpu *gpu;
  27. void *vaddr;
  28. dma_addr_t paddr;
  29. /* GPU mapping */
  30. u32 iova;
  31. struct drm_mm_node vram_node; /* only used on MMUv2 */
  32. /* allocation management */
  33. struct mutex lock;
  34. DECLARE_BITMAP(granule_map, SUBALLOC_GRANULES);
  35. int free_space;
  36. wait_queue_head_t free_event;
  37. };
  38. struct etnaviv_cmdbuf_suballoc *
  39. etnaviv_cmdbuf_suballoc_new(struct etnaviv_gpu * gpu)
  40. {
  41. struct etnaviv_cmdbuf_suballoc *suballoc;
  42. int ret;
  43. suballoc = kzalloc(sizeof(*suballoc), GFP_KERNEL);
  44. if (!suballoc)
  45. return ERR_PTR(-ENOMEM);
  46. suballoc->gpu = gpu;
  47. mutex_init(&suballoc->lock);
  48. init_waitqueue_head(&suballoc->free_event);
  49. suballoc->vaddr = dma_alloc_wc(gpu->dev, SUBALLOC_SIZE,
  50. &suballoc->paddr, GFP_KERNEL);
  51. if (!suballoc->vaddr)
  52. goto free_suballoc;
  53. ret = etnaviv_iommu_get_suballoc_va(gpu, suballoc->paddr,
  54. &suballoc->vram_node, SUBALLOC_SIZE,
  55. &suballoc->iova);
  56. if (ret)
  57. goto free_dma;
  58. return suballoc;
  59. free_dma:
  60. dma_free_wc(gpu->dev, SUBALLOC_SIZE, suballoc->vaddr, suballoc->paddr);
  61. free_suballoc:
  62. kfree(suballoc);
  63. return NULL;
  64. }
  65. void etnaviv_cmdbuf_suballoc_destroy(struct etnaviv_cmdbuf_suballoc *suballoc)
  66. {
  67. etnaviv_iommu_put_suballoc_va(suballoc->gpu, &suballoc->vram_node,
  68. SUBALLOC_SIZE, suballoc->iova);
  69. dma_free_wc(suballoc->gpu->dev, SUBALLOC_SIZE, suballoc->vaddr,
  70. suballoc->paddr);
  71. kfree(suballoc);
  72. }
  73. int etnaviv_cmdbuf_init(struct etnaviv_cmdbuf_suballoc *suballoc,
  74. struct etnaviv_cmdbuf *cmdbuf, u32 size)
  75. {
  76. int granule_offs, order, ret;
  77. cmdbuf->suballoc = suballoc;
  78. cmdbuf->size = size;
  79. order = order_base_2(ALIGN(size, SUBALLOC_GRANULE) / SUBALLOC_GRANULE);
  80. retry:
  81. mutex_lock(&suballoc->lock);
  82. granule_offs = bitmap_find_free_region(suballoc->granule_map,
  83. SUBALLOC_GRANULES, order);
  84. if (granule_offs < 0) {
  85. suballoc->free_space = 0;
  86. mutex_unlock(&suballoc->lock);
  87. ret = wait_event_interruptible_timeout(suballoc->free_event,
  88. suballoc->free_space,
  89. msecs_to_jiffies(10 * 1000));
  90. if (!ret) {
  91. dev_err(suballoc->gpu->dev,
  92. "Timeout waiting for cmdbuf space\n");
  93. return -ETIMEDOUT;
  94. }
  95. goto retry;
  96. }
  97. mutex_unlock(&suballoc->lock);
  98. cmdbuf->suballoc_offset = granule_offs * SUBALLOC_GRANULE;
  99. cmdbuf->vaddr = suballoc->vaddr + cmdbuf->suballoc_offset;
  100. return 0;
  101. }
  102. void etnaviv_cmdbuf_free(struct etnaviv_cmdbuf *cmdbuf)
  103. {
  104. struct etnaviv_cmdbuf_suballoc *suballoc = cmdbuf->suballoc;
  105. int order = order_base_2(ALIGN(cmdbuf->size, SUBALLOC_GRANULE) /
  106. SUBALLOC_GRANULE);
  107. mutex_lock(&suballoc->lock);
  108. bitmap_release_region(suballoc->granule_map,
  109. cmdbuf->suballoc_offset / SUBALLOC_GRANULE,
  110. order);
  111. suballoc->free_space = 1;
  112. mutex_unlock(&suballoc->lock);
  113. wake_up_all(&suballoc->free_event);
  114. }
  115. u32 etnaviv_cmdbuf_get_va(struct etnaviv_cmdbuf *buf)
  116. {
  117. return buf->suballoc->iova + buf->suballoc_offset;
  118. }
  119. dma_addr_t etnaviv_cmdbuf_get_pa(struct etnaviv_cmdbuf *buf)
  120. {
  121. return buf->suballoc->paddr + buf->suballoc_offset;
  122. }