mtk_vcodec_mem.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (c) 2017 MediaTek Inc.
  3. * Author: Yunfei Dong <yunfei.dong@mediatek.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #ifndef MTK_VCODEC_MEM_H
  15. #define MTK_VCODEC_MEM_H
  16. #include <media/videobuf2-dma-contig.h>
  17. #include <uapi/linux/mtk_vcu_controls.h>
  18. #include <linux/slab.h>
  19. #include <asm/cacheflush.h>
  20. #include <linux/mm.h>
  21. #include <linux/dma-mapping.h>
  22. #include <mailbox/cmdq-sec.h>
  23. #ifndef CONFIG_ARM64
  24. #include "mm/dma.h"
  25. #endif
  26. #ifndef dmac_map_area
  27. #define dmac_map_area __dma_map_area
  28. #endif
  29. #ifndef dmac_unmap_area
  30. #define dmac_unmap_area __dma_unmap_area
  31. #endif
  32. #ifndef dmac_flush_range
  33. #define dmac_flush_range __dma_flush_range
  34. #endif
  35. #define CODEC_MAX_BUFFER 512U
  36. #define CODEC_ALLOCATE_MAX_BUFFER_SIZE 0x8000000UL /*128MB*/
  37. #define CODEC_MSK(addr) ((addr >> PAGE_SHIFT) & 0xFFFF)
  38. /**
  39. * struct mtk_vcu_mem - memory buffer allocated in kernel
  40. *
  41. * @mem_priv: vcu allocated buffer vb2_dc_buf
  42. * @size: buffer size
  43. * @dbuf: io buffer dma_buff
  44. * @iova: io buffer iova
  45. */
  46. struct mtk_vcu_mem {
  47. void *mem_priv;
  48. size_t size;
  49. struct dma_buf *dbuf;
  50. dma_addr_t iova;
  51. atomic_t ref_cnt;
  52. };
  53. struct vcu_pa_pages {
  54. unsigned long pa;
  55. unsigned long kva;
  56. atomic_t ref_cnt;
  57. struct list_head list;
  58. };
  59. /**
  60. * struct mtk_vcu_queue - the allocated buffer queue
  61. *
  62. * @vcu: struct mtk_vcu
  63. * @mmap_lock: the lock to protect allocated buffer
  64. * @dev: device
  65. * @num_buffers: allocated buffer number
  66. * @mem_ops: the file operation of memory allocated
  67. * @bufs: store the information of allocated buffers
  68. * @map_buf_pa: store map pa and it's flag
  69. */
  70. struct mtk_vcu_queue {
  71. void *vcu;
  72. struct mutex mmap_lock;
  73. struct device *dev;
  74. struct device *cmdq_dev;
  75. unsigned int num_buffers;
  76. const struct vb2_mem_ops *mem_ops;
  77. struct mtk_vcu_mem bufs[CODEC_MAX_BUFFER];
  78. uint64_t map_buf_pa;
  79. struct vcu_pa_pages pa_pages;
  80. };
  81. /**
  82. * mtk_vcu_mem_init - just init vcu_queue
  83. *
  84. * @dev: vcu device.
  85. * @cmdq_dev: cmdq device.
  86. *
  87. * Return: Return NULL if it is failed.
  88. * otherwise it is vcu queue to store the allocated buffer
  89. **/
  90. struct mtk_vcu_queue *mtk_vcu_mem_init(struct device *dev,
  91. struct device *cmdq_dev);
  92. /**
  93. * mtk_vcu_mem_release - just release the vcu_queue
  94. *
  95. * @vcu_queue: the queue to store allocated buffer.
  96. *
  97. * Return: void
  98. **/
  99. void mtk_vcu_mem_release(struct mtk_vcu_queue *vcu_queue);
  100. /**
  101. * mtk_vcu_set_buffer - set the allocated buffer iova/va
  102. *
  103. * @vcu_queue: the queue to store allocated buffer.
  104. * @mem_buff_data: store iova/va.
  105. * @src_vb/dst_vb: set io buffer dma to vcu_queue for cache sync.
  106. *
  107. * Return: Return real address if it is ok, otherwise failed
  108. **/
  109. void *mtk_vcu_set_buffer(struct mtk_vcu_queue *vcu_queue,
  110. struct mem_obj *mem_buff_data, struct vb2_buffer *src_vb,
  111. struct vb2_buffer *dst_vb);
  112. /**
  113. * mtk_vcu_get_buffer/mtk_vcu_get_page - get the allocated buffer iova/va/pa
  114. *
  115. * @vcu_queue: the queue to store allocated buffer.
  116. * @mem_buff_data: store iova/va.
  117. *
  118. * Return: Return real address if it is ok, otherwise failed
  119. **/
  120. void *mtk_vcu_get_buffer(struct mtk_vcu_queue *vcu_queue,
  121. struct mem_obj *mem_buff_data);
  122. void *mtk_vcu_get_page(struct mtk_vcu_queue *vcu_queue,
  123. struct mem_obj *mem_buff_data);
  124. /**
  125. * mtk_vcu_free_buffer/mtk_vcu_free_page - just free unused buffer iova/va/pa
  126. *
  127. * @vcu_queue: the queue to store allocated buffer.
  128. * @mem_buff_data: store iova/va to free.
  129. *
  130. * Return: Return 0 if it is ok, otherwise failed
  131. **/
  132. int mtk_vcu_free_buffer(struct mtk_vcu_queue *vcu_queue,
  133. struct mem_obj *mem_buff_data);
  134. int mtk_vcu_free_page(struct mtk_vcu_queue *vcu_queue,
  135. struct mem_obj *mem_buff_data);
  136. /**
  137. * mtk_vcu_free_buffer - decreas reference count for mem_priv
  138. *
  139. * @vcu_queue: the queue to store allocated buffer.
  140. * @mem_buff_data: store iova/va to free.
  141. *
  142. * Return: Return 0 if it is ok, otherwise failed
  143. **/
  144. void mtk_vcu_buffer_ref_dec(struct mtk_vcu_queue *vcu_queue,
  145. void *mem_priv);
  146. /**
  147. * vcu_buffer_flush_all - flush all VCU buffer cache for device
  148. *
  149. * @dev: vcu device.
  150. * @vcu_queue: the queue to store allocated buffer.
  151. *
  152. * Return: Return 0 if it is ok, otherwise failed
  153. **/
  154. int vcu_buffer_flush_all(struct device *dev, struct mtk_vcu_queue *vcu_queue);
  155. /**
  156. * vcu_buffer_cache_sync - VCU buffer cache sync by dma_addr for device
  157. *
  158. * @dev: vcu device.
  159. * @vcu_queue: the queue to store allocated buffer.
  160. * @dma_addr: the buffer to be flushed dma addr
  161. * @dma_addr: the corresponding flushed size
  162. * @op: DMA_TO_DEVICE or DMA_FROM_DEVICE
  163. *
  164. * Return: Return 0 if it is ok, otherwise failed
  165. **/
  166. int vcu_buffer_cache_sync(struct device *dev, struct mtk_vcu_queue *vcu_queue,
  167. dma_addr_t dma_addr, size_t size, int op);
  168. #endif