etnaviv_cmdbuf.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2017-2018 Etnaviv Project
  4. */
  5. #include <linux/dma-mapping.h>
  6. #include <drm/drm_mm.h>
  7. #include "etnaviv_cmdbuf.h"
  8. #include "etnaviv_gem.h"
  9. #include "etnaviv_gpu.h"
  10. #include "etnaviv_mmu.h"
  11. #include "etnaviv_perfmon.h"
  12. #define SUBALLOC_SIZE SZ_512K
  13. #define SUBALLOC_GRANULE SZ_4K
  14. #define SUBALLOC_GRANULES (SUBALLOC_SIZE / SUBALLOC_GRANULE)
  15. struct etnaviv_cmdbuf_suballoc {
  16. /* suballocated dma buffer properties */
  17. struct device *dev;
  18. void *vaddr;
  19. dma_addr_t paddr;
  20. /* allocation management */
  21. struct mutex lock;
  22. DECLARE_BITMAP(granule_map, SUBALLOC_GRANULES);
  23. int free_space;
  24. wait_queue_head_t free_event;
  25. };
  26. struct etnaviv_cmdbuf_suballoc *
  27. etnaviv_cmdbuf_suballoc_new(struct device *dev)
  28. {
  29. struct etnaviv_cmdbuf_suballoc *suballoc;
  30. int ret;
  31. suballoc = kzalloc(sizeof(*suballoc), GFP_KERNEL);
  32. if (!suballoc)
  33. return ERR_PTR(-ENOMEM);
  34. suballoc->dev = dev;
  35. mutex_init(&suballoc->lock);
  36. init_waitqueue_head(&suballoc->free_event);
  37. BUILD_BUG_ON(ETNAVIV_SOFTPIN_START_ADDRESS < SUBALLOC_SIZE);
  38. suballoc->vaddr = dma_alloc_wc(dev, SUBALLOC_SIZE,
  39. &suballoc->paddr, GFP_KERNEL);
  40. if (!suballoc->vaddr) {
  41. ret = -ENOMEM;
  42. goto free_suballoc;
  43. }
  44. return suballoc;
  45. free_suballoc:
  46. kfree(suballoc);
  47. return ERR_PTR(ret);
  48. }
  49. int etnaviv_cmdbuf_suballoc_map(struct etnaviv_cmdbuf_suballoc *suballoc,
  50. struct etnaviv_iommu_context *context,
  51. struct etnaviv_vram_mapping *mapping,
  52. u32 memory_base)
  53. {
  54. return etnaviv_iommu_get_suballoc_va(context, mapping, memory_base,
  55. suballoc->paddr, SUBALLOC_SIZE);
  56. }
  57. void etnaviv_cmdbuf_suballoc_unmap(struct etnaviv_iommu_context *context,
  58. struct etnaviv_vram_mapping *mapping)
  59. {
  60. etnaviv_iommu_put_suballoc_va(context, mapping);
  61. }
  62. void etnaviv_cmdbuf_suballoc_destroy(struct etnaviv_cmdbuf_suballoc *suballoc)
  63. {
  64. dma_free_wc(suballoc->dev, SUBALLOC_SIZE, suballoc->vaddr,
  65. suballoc->paddr);
  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. msecs_to_jiffies(10 * 1000));
  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. mutex_lock(&suballoc->lock);
  103. bitmap_release_region(suballoc->granule_map,
  104. cmdbuf->suballoc_offset / SUBALLOC_GRANULE,
  105. order);
  106. suballoc->free_space = 1;
  107. mutex_unlock(&suballoc->lock);
  108. wake_up_all(&suballoc->free_event);
  109. }
  110. u32 etnaviv_cmdbuf_get_va(struct etnaviv_cmdbuf *buf,
  111. struct etnaviv_vram_mapping *mapping)
  112. {
  113. return mapping->iova + buf->suballoc_offset;
  114. }
  115. dma_addr_t etnaviv_cmdbuf_get_pa(struct etnaviv_cmdbuf *buf)
  116. {
  117. return buf->suballoc->paddr + buf->suballoc_offset;
  118. }