etnaviv_cmdbuf.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #define SUBALLOC_SIZE SZ_256K
  21. #define SUBALLOC_GRANULE SZ_4K
  22. #define SUBALLOC_GRANULES (SUBALLOC_SIZE / SUBALLOC_GRANULE)
  23. struct etnaviv_cmdbuf_suballoc {
  24. /* suballocated dma buffer properties */
  25. struct etnaviv_gpu *gpu;
  26. void *vaddr;
  27. dma_addr_t paddr;
  28. /* GPU mapping */
  29. u32 iova;
  30. struct drm_mm_node vram_node; /* only used on MMUv2 */
  31. /* allocation management */
  32. struct mutex lock;
  33. DECLARE_BITMAP(granule_map, SUBALLOC_GRANULES);
  34. int free_space;
  35. wait_queue_head_t free_event;
  36. };
  37. struct etnaviv_cmdbuf_suballoc *
  38. etnaviv_cmdbuf_suballoc_new(struct etnaviv_gpu * gpu)
  39. {
  40. struct etnaviv_cmdbuf_suballoc *suballoc;
  41. int ret;
  42. suballoc = kzalloc(sizeof(*suballoc), GFP_KERNEL);
  43. if (!suballoc)
  44. return ERR_PTR(-ENOMEM);
  45. suballoc->gpu = gpu;
  46. mutex_init(&suballoc->lock);
  47. init_waitqueue_head(&suballoc->free_event);
  48. suballoc->vaddr = dma_alloc_wc(gpu->dev, SUBALLOC_SIZE,
  49. &suballoc->paddr, GFP_KERNEL);
  50. if (!suballoc->vaddr)
  51. goto free_suballoc;
  52. ret = etnaviv_iommu_get_suballoc_va(gpu, suballoc->paddr,
  53. &suballoc->vram_node, SUBALLOC_SIZE,
  54. &suballoc->iova);
  55. if (ret)
  56. goto free_dma;
  57. return suballoc;
  58. free_dma:
  59. dma_free_wc(gpu->dev, SUBALLOC_SIZE, suballoc->vaddr, suballoc->paddr);
  60. free_suballoc:
  61. kfree(suballoc);
  62. return NULL;
  63. }
  64. void etnaviv_cmdbuf_suballoc_destroy(struct etnaviv_cmdbuf_suballoc *suballoc)
  65. {
  66. etnaviv_iommu_put_suballoc_va(suballoc->gpu, &suballoc->vram_node,
  67. SUBALLOC_SIZE, suballoc->iova);
  68. dma_free_wc(suballoc->gpu->dev, SUBALLOC_SIZE, suballoc->vaddr,
  69. suballoc->paddr);
  70. kfree(suballoc);
  71. }
  72. struct etnaviv_cmdbuf *
  73. etnaviv_cmdbuf_new(struct etnaviv_cmdbuf_suballoc *suballoc, u32 size,
  74. size_t nr_bos)
  75. {
  76. struct etnaviv_cmdbuf *cmdbuf;
  77. size_t sz = size_vstruct(nr_bos, sizeof(cmdbuf->bo_map[0]),
  78. sizeof(*cmdbuf));
  79. int granule_offs, order, ret;
  80. cmdbuf = kzalloc(sz, GFP_KERNEL);
  81. if (!cmdbuf)
  82. return NULL;
  83. cmdbuf->suballoc = suballoc;
  84. cmdbuf->size = size;
  85. order = order_base_2(ALIGN(size, SUBALLOC_GRANULE) / SUBALLOC_GRANULE);
  86. retry:
  87. mutex_lock(&suballoc->lock);
  88. granule_offs = bitmap_find_free_region(suballoc->granule_map,
  89. SUBALLOC_GRANULES, order);
  90. if (granule_offs < 0) {
  91. suballoc->free_space = 0;
  92. mutex_unlock(&suballoc->lock);
  93. ret = wait_event_interruptible_timeout(suballoc->free_event,
  94. suballoc->free_space,
  95. msecs_to_jiffies(10 * 1000));
  96. if (!ret) {
  97. dev_err(suballoc->gpu->dev,
  98. "Timeout waiting for cmdbuf space\n");
  99. return NULL;
  100. }
  101. goto retry;
  102. }
  103. mutex_unlock(&suballoc->lock);
  104. cmdbuf->suballoc_offset = granule_offs * SUBALLOC_GRANULE;
  105. cmdbuf->vaddr = suballoc->vaddr + cmdbuf->suballoc_offset;
  106. return cmdbuf;
  107. }
  108. void etnaviv_cmdbuf_free(struct etnaviv_cmdbuf *cmdbuf)
  109. {
  110. struct etnaviv_cmdbuf_suballoc *suballoc = cmdbuf->suballoc;
  111. int order = order_base_2(ALIGN(cmdbuf->size, SUBALLOC_GRANULE) /
  112. SUBALLOC_GRANULE);
  113. mutex_lock(&suballoc->lock);
  114. bitmap_release_region(suballoc->granule_map,
  115. cmdbuf->suballoc_offset / SUBALLOC_GRANULE,
  116. order);
  117. suballoc->free_space = 1;
  118. mutex_unlock(&suballoc->lock);
  119. wake_up_all(&suballoc->free_event);
  120. kfree(cmdbuf);
  121. }
  122. u32 etnaviv_cmdbuf_get_va(struct etnaviv_cmdbuf *buf)
  123. {
  124. return buf->suballoc->iova + buf->suballoc_offset;
  125. }
  126. dma_addr_t etnaviv_cmdbuf_get_pa(struct etnaviv_cmdbuf *buf)
  127. {
  128. return buf->suballoc->paddr + buf->suballoc_offset;
  129. }