etnaviv_cmdbuf.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2017-2018 Etnaviv Project
  4. */
  5. #include <linux/dma-mapping.h>
  6. #include "etnaviv_cmdbuf.h"
  7. #include "etnaviv_gem.h"
  8. #include "etnaviv_gpu.h"
  9. #include "etnaviv_mmu.h"
  10. #define SUBALLOC_SIZE SZ_512K
  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 device *dev;
  16. void *vaddr;
  17. dma_addr_t paddr;
  18. /* allocation management */
  19. struct mutex lock;
  20. DECLARE_BITMAP(granule_map, SUBALLOC_GRANULES);
  21. int free_space;
  22. wait_queue_head_t free_event;
  23. };
  24. struct etnaviv_cmdbuf_suballoc *
  25. etnaviv_cmdbuf_suballoc_new(struct device *dev)
  26. {
  27. struct etnaviv_cmdbuf_suballoc *suballoc;
  28. int ret;
  29. suballoc = kzalloc_obj(*suballoc);
  30. if (!suballoc)
  31. return ERR_PTR(-ENOMEM);
  32. suballoc->dev = dev;
  33. mutex_init(&suballoc->lock);
  34. init_waitqueue_head(&suballoc->free_event);
  35. BUILD_BUG_ON(ETNAVIV_SOFTPIN_START_ADDRESS < SUBALLOC_SIZE);
  36. suballoc->vaddr = dma_alloc_wc(dev, SUBALLOC_SIZE,
  37. &suballoc->paddr, GFP_KERNEL);
  38. if (!suballoc->vaddr) {
  39. ret = -ENOMEM;
  40. goto free_suballoc;
  41. }
  42. return suballoc;
  43. free_suballoc:
  44. mutex_destroy(&suballoc->lock);
  45. kfree(suballoc);
  46. return ERR_PTR(ret);
  47. }
  48. int etnaviv_cmdbuf_suballoc_map(struct etnaviv_cmdbuf_suballoc *suballoc,
  49. struct etnaviv_iommu_context *context,
  50. struct etnaviv_vram_mapping *mapping,
  51. u32 memory_base)
  52. {
  53. return etnaviv_iommu_get_suballoc_va(context, mapping, memory_base,
  54. suballoc->paddr, SUBALLOC_SIZE);
  55. }
  56. void etnaviv_cmdbuf_suballoc_unmap(struct etnaviv_iommu_context *context,
  57. struct etnaviv_vram_mapping *mapping)
  58. {
  59. etnaviv_iommu_put_suballoc_va(context, mapping);
  60. }
  61. void etnaviv_cmdbuf_suballoc_destroy(struct etnaviv_cmdbuf_suballoc *suballoc)
  62. {
  63. dma_free_wc(suballoc->dev, SUBALLOC_SIZE, suballoc->vaddr,
  64. suballoc->paddr);
  65. mutex_destroy(&suballoc->lock);
  66. kfree(suballoc);
  67. }
  68. int etnaviv_cmdbuf_init(struct etnaviv_cmdbuf_suballoc *suballoc,
  69. struct etnaviv_cmdbuf *cmdbuf, u32 size)
  70. {
  71. int granule_offs, order, ret;
  72. cmdbuf->suballoc = suballoc;
  73. cmdbuf->size = size;
  74. order = order_base_2(ALIGN(size, SUBALLOC_GRANULE) / SUBALLOC_GRANULE);
  75. retry:
  76. mutex_lock(&suballoc->lock);
  77. granule_offs = bitmap_find_free_region(suballoc->granule_map,
  78. SUBALLOC_GRANULES, order);
  79. if (granule_offs < 0) {
  80. suballoc->free_space = 0;
  81. mutex_unlock(&suballoc->lock);
  82. ret = wait_event_interruptible_timeout(suballoc->free_event,
  83. suballoc->free_space,
  84. secs_to_jiffies(10));
  85. if (!ret) {
  86. dev_err(suballoc->dev,
  87. "Timeout waiting for cmdbuf space\n");
  88. return -ETIMEDOUT;
  89. }
  90. goto retry;
  91. }
  92. mutex_unlock(&suballoc->lock);
  93. cmdbuf->suballoc_offset = granule_offs * SUBALLOC_GRANULE;
  94. cmdbuf->vaddr = suballoc->vaddr + cmdbuf->suballoc_offset;
  95. return 0;
  96. }
  97. void etnaviv_cmdbuf_free(struct etnaviv_cmdbuf *cmdbuf)
  98. {
  99. struct etnaviv_cmdbuf_suballoc *suballoc = cmdbuf->suballoc;
  100. int order = order_base_2(ALIGN(cmdbuf->size, SUBALLOC_GRANULE) /
  101. SUBALLOC_GRANULE);
  102. if (!suballoc)
  103. return;
  104. mutex_lock(&suballoc->lock);
  105. bitmap_release_region(suballoc->granule_map,
  106. cmdbuf->suballoc_offset / SUBALLOC_GRANULE,
  107. order);
  108. suballoc->free_space = 1;
  109. mutex_unlock(&suballoc->lock);
  110. wake_up_all(&suballoc->free_event);
  111. }
  112. u32 etnaviv_cmdbuf_get_va(struct etnaviv_cmdbuf *buf,
  113. struct etnaviv_vram_mapping *mapping)
  114. {
  115. return mapping->iova + buf->suballoc_offset;
  116. }
  117. dma_addr_t etnaviv_cmdbuf_get_pa(struct etnaviv_cmdbuf *buf)
  118. {
  119. return buf->suballoc->paddr + buf->suballoc_offset;
  120. }