mtk-dma-contig.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Copyright (c) 2016 MediaTek Inc.
  3. * Author: Rick Chang <rick.chang@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. #include <linux/dma-buf.h>
  15. #include <linux/module.h>
  16. #include <linux/scatterlist.h>
  17. #include <linux/sched.h>
  18. #include <linux/slab.h>
  19. #include <linux/dma-mapping.h>
  20. #include "mtk-dma-contig.h"
  21. static int mtk_secure_mode;
  22. static int debug;
  23. module_param(debug, int, 0644);
  24. #define CRITICAL 0
  25. #define ALWAYS 0
  26. #define VPUDBG 1
  27. #define SPEW 2
  28. #define INFO 3
  29. #define dprintk(level, fmt, args...) \
  30. do { \
  31. if (debug >= level) \
  32. pr_info("mtk-dma-contig: " fmt, ##args); \
  33. } while (0)
  34. /*********************************************/
  35. /* scatterlist table functions */
  36. /*********************************************/
  37. static unsigned long vb2_dc_get_contiguous_size(struct sg_table *sgt)
  38. {
  39. struct scatterlist *s;
  40. dma_addr_t expected = sg_dma_address(sgt->sgl);
  41. unsigned int i;
  42. unsigned long size = 0;
  43. for_each_sg(sgt->sgl, s, sgt->nents, i) {
  44. if (sg_dma_address(s) != expected)
  45. break;
  46. expected = sg_dma_address(s) + sg_dma_len(s);
  47. size += sg_dma_len(s);
  48. }
  49. return size;
  50. }
  51. /*********************************************/
  52. /* callbacks for all buffers */
  53. /*********************************************/
  54. static void *vb2_dc_cookie(void *buf_priv)
  55. {
  56. struct vb2_dc_buf *buf = buf_priv;
  57. return &buf->dma_addr;
  58. }
  59. static void *vb2_dc_vaddr(void *buf_priv)
  60. {
  61. struct vb2_dc_buf *buf = buf_priv;
  62. if (!buf->vaddr && buf->db_attach)
  63. buf->vaddr = dma_buf_vmap(buf->db_attach->dmabuf);
  64. return buf->vaddr;
  65. }
  66. static unsigned int vb2_dc_num_users(void *buf_priv)
  67. {
  68. struct vb2_dc_buf *buf = buf_priv;
  69. return atomic_read(&buf->refcount);
  70. }
  71. static void vb2_dc_prepare(void *buf_priv)
  72. {
  73. struct vb2_dc_buf *buf = buf_priv;
  74. struct sg_table *sgt = buf->dma_sgt;
  75. /* DMABUF exporter will flush the cache for us */
  76. if (!sgt || buf->db_attach)
  77. return;
  78. dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->orig_nents,
  79. buf->dma_dir);
  80. }
  81. static void vb2_dc_finish(void *buf_priv)
  82. {
  83. struct vb2_dc_buf *buf = buf_priv;
  84. struct sg_table *sgt = buf->dma_sgt;
  85. /* DMABUF exporter will flush the cache for us */
  86. if (!sgt || buf->db_attach)
  87. return;
  88. dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir);
  89. }
  90. /*********************************************/
  91. /* callbacks for DMABUF buffers */
  92. /*********************************************/
  93. static int vb2_dc_map_dmabuf(void *mem_priv)
  94. {
  95. struct vb2_dc_buf *buf = mem_priv;
  96. struct sg_table *sgt;
  97. unsigned long contig_size;
  98. if (WARN_ON(!buf->db_attach)) {
  99. dprintk(CRITICAL, "trying to pin a non attached buffer\n");
  100. return -EINVAL;
  101. }
  102. if (WARN_ON(buf->dma_sgt)) {
  103. dprintk(CRITICAL, "dmabuf buffer is already pinned\n");
  104. return 0;
  105. }
  106. /* get the associated scatterlist for this buffer */
  107. sgt = dma_buf_map_attachment(buf->db_attach, buf->dma_dir);
  108. if (IS_ERR(sgt)) {
  109. dprintk(CRITICAL, "Error getting dmabuf scatterlist\n");
  110. return -EINVAL;
  111. }
  112. /* checking if dmabuf is big enough to store contiguous chunk */
  113. contig_size = vb2_dc_get_contiguous_size(sgt);
  114. if (contig_size < buf->size && mtk_secure_mode != 1) {
  115. #ifdef CONFIG_MTK_IOMMU_V2
  116. dprintk(CRITICAL,
  117. "contiguous chunk is too small %lu/%lu b\n",
  118. contig_size, buf->size);
  119. #endif
  120. dma_buf_unmap_attachment(buf->db_attach,
  121. sgt, buf->dma_dir);
  122. #ifdef CONFIG_MTK_IOMMU_V2
  123. return -EFAULT;
  124. #endif
  125. }
  126. buf->dma_addr = sg_dma_address(sgt->sgl);
  127. buf->dma_sgt = sgt;
  128. buf->vaddr = NULL;
  129. return 0;
  130. }
  131. static void vb2_dc_unmap_dmabuf(void *mem_priv)
  132. {
  133. struct vb2_dc_buf *buf = mem_priv;
  134. struct sg_table *sgt = buf->dma_sgt;
  135. if (WARN_ON(!buf->db_attach)) {
  136. dprintk(CRITICAL, "trying to unpin a not attached buffer\n");
  137. return;
  138. }
  139. if (WARN_ON(!sgt)) {
  140. dprintk(CRITICAL,
  141. "dmabuf buffer is already unpinned\n");
  142. return;
  143. }
  144. if (buf->vaddr) {
  145. dma_buf_vunmap(buf->db_attach->dmabuf, buf->vaddr);
  146. buf->vaddr = NULL;
  147. }
  148. dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
  149. buf->dma_addr = 0;
  150. buf->dma_sgt = NULL;
  151. }
  152. static void vb2_dc_detach_dmabuf(void *mem_priv)
  153. {
  154. struct vb2_dc_buf *buf = mem_priv;
  155. /* if vb2 works correctly you should never detach mapped buffer */
  156. if (WARN_ON(buf->dma_addr))
  157. vb2_dc_unmap_dmabuf(buf);
  158. /* detach this attachment */
  159. dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach);
  160. kfree(buf);
  161. }
  162. static void *vb2_dc_attach_dmabuf(struct device *dev, struct dma_buf *dbuf,
  163. unsigned long size, enum dma_data_direction dma_dir)
  164. {
  165. struct vb2_dc_buf *buf;
  166. struct dma_buf_attachment *dba;
  167. if (dbuf->size < size)
  168. return ERR_PTR(-EFAULT);
  169. if (WARN_ON(!dev))
  170. return ERR_PTR(-EINVAL);
  171. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  172. if (!buf)
  173. return ERR_PTR(-ENOMEM);
  174. buf->dev = dev;
  175. /* create attachment for the dmabuf with the user device */
  176. dba = dma_buf_attach(dbuf, buf->dev);
  177. if (IS_ERR(dba)) {
  178. dprintk(CRITICAL, "failed to attach dmabuf\n");
  179. kfree(buf);
  180. return dba;
  181. }
  182. buf->dma_dir = dma_dir;
  183. buf->size = size;
  184. buf->db_attach = dba;
  185. return buf;
  186. }
  187. /*********************************************/
  188. /* DMA CONTIG exported functions */
  189. /*********************************************/
  190. const struct vb2_mem_ops mtk_dma_contig_memops = {
  191. .cookie = vb2_dc_cookie,
  192. .vaddr = vb2_dc_vaddr,
  193. .prepare = vb2_dc_prepare,
  194. .finish = vb2_dc_finish,
  195. .map_dmabuf = vb2_dc_map_dmabuf,
  196. .unmap_dmabuf = vb2_dc_unmap_dmabuf,
  197. .attach_dmabuf = vb2_dc_attach_dmabuf,
  198. .detach_dmabuf = vb2_dc_detach_dmabuf,
  199. .num_users = vb2_dc_num_users,
  200. };
  201. EXPORT_SYMBOL_GPL(mtk_dma_contig_memops);
  202. /**
  203. * mtk_dma_contig_set_max_seg_size() - configure DMA max segment size
  204. * @dev: device for configuring DMA parameters
  205. * @size: size of DMA max segment size to set
  206. *
  207. * To allow mapping the scatter-list into a single chunk in the DMA
  208. * address space, the device is required to have the DMA max segment
  209. * size parameter set to a value larger than the buffer size. Otherwise,
  210. * the DMA-mapping subsystem will split the mapping into max segment
  211. * size chunks. This function sets the DMA max segment size
  212. * parameter to let DMA-mapping map a buffer as a single chunk in DMA
  213. * address space.
  214. * This code assumes that the DMA-mapping subsystem will merge all
  215. * scatterlist segments if this is really possible (for example when
  216. * an IOMMU is available and enabled).
  217. * Ideally, this parameter should be set by the generic bus code, but it
  218. * is left with the default 64KiB value due to historical litmiations in
  219. * other subsystems (like limited USB host drivers) and there no good
  220. * place to set it to the proper value.
  221. * This function should be called from the drivers, which are known to
  222. * operate on platforms with IOMMU and provide access to shared buffers
  223. * (either USERPTR or DMABUF). This should be done before initializing
  224. * videobuf2 queue.
  225. */
  226. int mtk_dma_contig_set_max_seg_size(struct device *dev, unsigned int size)
  227. {
  228. if (!dev->dma_parms) {
  229. dev->dma_parms = kzalloc(sizeof(*dev->dma_parms), GFP_KERNEL);
  230. if (!dev->dma_parms)
  231. return -ENOMEM;
  232. }
  233. if (dma_get_max_seg_size(dev) < size)
  234. return dma_set_max_seg_size(dev, size);
  235. return 0;
  236. }
  237. EXPORT_SYMBOL_GPL(mtk_dma_contig_set_max_seg_size);
  238. /*
  239. * mtk_dma_contig_clear_max_seg_size() - release resources for DMA parameters
  240. * @dev: device for configuring DMA parameters
  241. *
  242. * This function releases resources allocated to configure DMA parameters
  243. * (see vb2_dma_contig_set_max_seg_size() function). It should be called from
  244. * device drivers on driver remove.
  245. */
  246. void mtk_dma_contig_clear_max_seg_size(struct device *dev)
  247. {
  248. kfree(dev->dma_parms);
  249. dev->dma_parms = NULL;
  250. }
  251. EXPORT_SYMBOL_GPL(mtk_dma_contig_clear_max_seg_size);
  252. /*
  253. * mtk_dma_contig_set_secure_mode() - set secure mode to bypass buffer processes
  254. * @dev: device for configuring DMA parameters
  255. *
  256. * This function is used for set hint for normal and secure buffer processes.
  257. */
  258. void mtk_dma_contig_set_secure_mode(struct device *dev, int secure_mode)
  259. {
  260. mtk_secure_mode = secure_mode;
  261. dprintk(CRITICAL, "Set secure mode : %d\n", secure_mode);
  262. }
  263. EXPORT_SYMBOL_GPL(mtk_dma_contig_set_secure_mode);
  264. MODULE_DESCRIPTION("DMA-contig memory handling routines for videobuf2");
  265. MODULE_LICENSE("GPL");