videobuf2-dma-contig.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. /*
  2. * videobuf2-dma-contig.c - DMA contig memory allocator for videobuf2
  3. *
  4. * Copyright (C) 2010 Samsung Electronics
  5. *
  6. * Author: Pawel Osciak <pawel@osciak.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation.
  11. */
  12. #include <linux/dma-buf.h>
  13. #include <linux/module.h>
  14. #include <linux/refcount.h>
  15. #include <linux/scatterlist.h>
  16. #include <linux/sched.h>
  17. #include <linux/slab.h>
  18. #include <linux/dma-mapping.h>
  19. #include <media/videobuf2-v4l2.h>
  20. #include <media/videobuf2-dma-contig.h>
  21. #include <media/videobuf2-memops.h>
  22. struct vb2_dc_buf {
  23. struct device *dev;
  24. void *vaddr;
  25. unsigned long size;
  26. void *cookie;
  27. dma_addr_t dma_addr;
  28. unsigned long attrs;
  29. enum dma_data_direction dma_dir;
  30. struct sg_table *dma_sgt;
  31. struct frame_vector *vec;
  32. /* MMAP related */
  33. struct vb2_vmarea_handler handler;
  34. refcount_t refcount;
  35. struct sg_table *sgt_base;
  36. /* DMABUF related */
  37. struct dma_buf_attachment *db_attach;
  38. };
  39. /*********************************************/
  40. /* scatterlist table functions */
  41. /*********************************************/
  42. static unsigned long vb2_dc_get_contiguous_size(struct sg_table *sgt)
  43. {
  44. struct scatterlist *s;
  45. dma_addr_t expected = sg_dma_address(sgt->sgl);
  46. unsigned int i;
  47. unsigned long size = 0;
  48. for_each_sg(sgt->sgl, s, sgt->nents, i) {
  49. if (sg_dma_address(s) != expected)
  50. break;
  51. expected = sg_dma_address(s) + sg_dma_len(s);
  52. size += sg_dma_len(s);
  53. }
  54. return size;
  55. }
  56. /*********************************************/
  57. /* callbacks for all buffers */
  58. /*********************************************/
  59. static void *vb2_dc_cookie(void *buf_priv)
  60. {
  61. struct vb2_dc_buf *buf = buf_priv;
  62. return &buf->dma_addr;
  63. }
  64. static void *vb2_dc_vaddr(void *buf_priv)
  65. {
  66. struct vb2_dc_buf *buf = buf_priv;
  67. if (!buf->vaddr && buf->db_attach)
  68. buf->vaddr = dma_buf_vmap(buf->db_attach->dmabuf);
  69. return buf->vaddr;
  70. }
  71. static unsigned int vb2_dc_num_users(void *buf_priv)
  72. {
  73. struct vb2_dc_buf *buf = buf_priv;
  74. return refcount_read(&buf->refcount);
  75. }
  76. static void vb2_dc_prepare(void *buf_priv)
  77. {
  78. struct vb2_dc_buf *buf = buf_priv;
  79. struct sg_table *sgt = buf->dma_sgt;
  80. /* DMABUF exporter will flush the cache for us */
  81. if (!sgt || buf->db_attach)
  82. return;
  83. dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->orig_nents,
  84. buf->dma_dir);
  85. }
  86. static void vb2_dc_finish(void *buf_priv)
  87. {
  88. struct vb2_dc_buf *buf = buf_priv;
  89. struct sg_table *sgt = buf->dma_sgt;
  90. /* DMABUF exporter will flush the cache for us */
  91. if (!sgt || buf->db_attach)
  92. return;
  93. dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir);
  94. }
  95. /*********************************************/
  96. /* callbacks for MMAP buffers */
  97. /*********************************************/
  98. static void vb2_dc_put(void *buf_priv)
  99. {
  100. struct vb2_dc_buf *buf = buf_priv;
  101. if (!refcount_dec_and_test(&buf->refcount))
  102. return;
  103. if (buf->sgt_base) {
  104. sg_free_table(buf->sgt_base);
  105. kfree(buf->sgt_base);
  106. }
  107. dma_free_attrs(buf->dev, buf->size, buf->cookie, buf->dma_addr,
  108. buf->attrs);
  109. put_device(buf->dev);
  110. kfree(buf);
  111. }
  112. static void *vb2_dc_alloc(struct device *dev, unsigned long attrs,
  113. unsigned long size, enum dma_data_direction dma_dir,
  114. gfp_t gfp_flags)
  115. {
  116. struct vb2_dc_buf *buf;
  117. if (WARN_ON(!dev))
  118. return ERR_PTR(-EINVAL);
  119. buf = kzalloc(sizeof *buf, GFP_KERNEL);
  120. if (!buf)
  121. return ERR_PTR(-ENOMEM);
  122. if (attrs)
  123. buf->attrs = attrs;
  124. buf->cookie = dma_alloc_attrs(dev, size, &buf->dma_addr,
  125. GFP_KERNEL | gfp_flags, buf->attrs);
  126. if (!buf->cookie) {
  127. dev_err(dev, "dma_alloc_coherent of size %ld failed\n", size);
  128. kfree(buf);
  129. return ERR_PTR(-ENOMEM);
  130. }
  131. if ((buf->attrs & DMA_ATTR_NO_KERNEL_MAPPING) == 0)
  132. buf->vaddr = buf->cookie;
  133. /* Prevent the device from being released while the buffer is used */
  134. buf->dev = get_device(dev);
  135. buf->size = size;
  136. buf->dma_dir = dma_dir;
  137. buf->handler.refcount = &buf->refcount;
  138. buf->handler.put = vb2_dc_put;
  139. buf->handler.arg = buf;
  140. refcount_set(&buf->refcount, 1);
  141. return buf;
  142. }
  143. static int vb2_dc_mmap(void *buf_priv, struct vm_area_struct *vma)
  144. {
  145. struct vb2_dc_buf *buf = buf_priv;
  146. int ret;
  147. if (!buf) {
  148. printk(KERN_ERR "No buffer to map\n");
  149. return -EINVAL;
  150. }
  151. /*
  152. * dma_mmap_* uses vm_pgoff as in-buffer offset, but we want to
  153. * map whole buffer
  154. */
  155. vma->vm_pgoff = 0;
  156. ret = dma_mmap_attrs(buf->dev, vma, buf->cookie,
  157. buf->dma_addr, buf->size, buf->attrs);
  158. if (ret) {
  159. pr_err("Remapping memory failed, error: %d\n", ret);
  160. return ret;
  161. }
  162. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  163. vma->vm_private_data = &buf->handler;
  164. vma->vm_ops = &vb2_common_vm_ops;
  165. vma->vm_ops->open(vma);
  166. pr_debug("%s: mapped dma addr 0x%08lx at 0x%08lx, size %ld\n",
  167. __func__, (unsigned long)buf->dma_addr, vma->vm_start,
  168. buf->size);
  169. return 0;
  170. }
  171. /*********************************************/
  172. /* DMABUF ops for exporters */
  173. /*********************************************/
  174. struct vb2_dc_attachment {
  175. struct sg_table sgt;
  176. enum dma_data_direction dma_dir;
  177. };
  178. static int vb2_dc_dmabuf_ops_attach(struct dma_buf *dbuf, struct device *dev,
  179. struct dma_buf_attachment *dbuf_attach)
  180. {
  181. struct vb2_dc_attachment *attach;
  182. unsigned int i;
  183. struct scatterlist *rd, *wr;
  184. struct sg_table *sgt;
  185. struct vb2_dc_buf *buf = dbuf->priv;
  186. int ret;
  187. attach = kzalloc(sizeof(*attach), GFP_KERNEL);
  188. if (!attach)
  189. return -ENOMEM;
  190. sgt = &attach->sgt;
  191. /* Copy the buf->base_sgt scatter list to the attachment, as we can't
  192. * map the same scatter list to multiple attachments at the same time.
  193. */
  194. ret = sg_alloc_table(sgt, buf->sgt_base->orig_nents, GFP_KERNEL);
  195. if (ret) {
  196. kfree(attach);
  197. return -ENOMEM;
  198. }
  199. rd = buf->sgt_base->sgl;
  200. wr = sgt->sgl;
  201. for (i = 0; i < sgt->orig_nents; ++i) {
  202. sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
  203. rd = sg_next(rd);
  204. wr = sg_next(wr);
  205. }
  206. attach->dma_dir = DMA_NONE;
  207. dbuf_attach->priv = attach;
  208. return 0;
  209. }
  210. static void vb2_dc_dmabuf_ops_detach(struct dma_buf *dbuf,
  211. struct dma_buf_attachment *db_attach)
  212. {
  213. struct vb2_dc_attachment *attach = db_attach->priv;
  214. struct sg_table *sgt;
  215. if (!attach)
  216. return;
  217. sgt = &attach->sgt;
  218. /* release the scatterlist cache */
  219. if (attach->dma_dir != DMA_NONE)
  220. dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
  221. attach->dma_dir);
  222. sg_free_table(sgt);
  223. kfree(attach);
  224. db_attach->priv = NULL;
  225. }
  226. static struct sg_table *vb2_dc_dmabuf_ops_map(
  227. struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir)
  228. {
  229. struct vb2_dc_attachment *attach = db_attach->priv;
  230. /* stealing dmabuf mutex to serialize map/unmap operations */
  231. struct mutex *lock = &db_attach->dmabuf->lock;
  232. struct sg_table *sgt;
  233. mutex_lock(lock);
  234. sgt = &attach->sgt;
  235. /* return previously mapped sg table */
  236. if (attach->dma_dir == dma_dir) {
  237. mutex_unlock(lock);
  238. return sgt;
  239. }
  240. /* release any previous cache */
  241. if (attach->dma_dir != DMA_NONE) {
  242. dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
  243. attach->dma_dir);
  244. attach->dma_dir = DMA_NONE;
  245. }
  246. /* mapping to the client with new direction */
  247. sgt->nents = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
  248. dma_dir);
  249. if (!sgt->nents) {
  250. pr_err("failed to map scatterlist\n");
  251. mutex_unlock(lock);
  252. return ERR_PTR(-EIO);
  253. }
  254. attach->dma_dir = dma_dir;
  255. mutex_unlock(lock);
  256. return sgt;
  257. }
  258. static void vb2_dc_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
  259. struct sg_table *sgt, enum dma_data_direction dma_dir)
  260. {
  261. /* nothing to be done here */
  262. }
  263. static void vb2_dc_dmabuf_ops_release(struct dma_buf *dbuf)
  264. {
  265. /* drop reference obtained in vb2_dc_get_dmabuf */
  266. vb2_dc_put(dbuf->priv);
  267. }
  268. static void *vb2_dc_dmabuf_ops_kmap(struct dma_buf *dbuf, unsigned long pgnum)
  269. {
  270. struct vb2_dc_buf *buf = dbuf->priv;
  271. return buf->vaddr ? buf->vaddr + pgnum * PAGE_SIZE : NULL;
  272. }
  273. static void *vb2_dc_dmabuf_ops_vmap(struct dma_buf *dbuf)
  274. {
  275. struct vb2_dc_buf *buf = dbuf->priv;
  276. return buf->vaddr;
  277. }
  278. static int vb2_dc_dmabuf_ops_mmap(struct dma_buf *dbuf,
  279. struct vm_area_struct *vma)
  280. {
  281. return vb2_dc_mmap(dbuf->priv, vma);
  282. }
  283. static struct dma_buf_ops vb2_dc_dmabuf_ops = {
  284. .attach = vb2_dc_dmabuf_ops_attach,
  285. .detach = vb2_dc_dmabuf_ops_detach,
  286. .map_dma_buf = vb2_dc_dmabuf_ops_map,
  287. .unmap_dma_buf = vb2_dc_dmabuf_ops_unmap,
  288. .map = vb2_dc_dmabuf_ops_kmap,
  289. .map_atomic = vb2_dc_dmabuf_ops_kmap,
  290. .vmap = vb2_dc_dmabuf_ops_vmap,
  291. .mmap = vb2_dc_dmabuf_ops_mmap,
  292. .release = vb2_dc_dmabuf_ops_release,
  293. };
  294. static struct sg_table *vb2_dc_get_base_sgt(struct vb2_dc_buf *buf)
  295. {
  296. int ret;
  297. struct sg_table *sgt;
  298. sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
  299. if (!sgt) {
  300. dev_err(buf->dev, "failed to alloc sg table\n");
  301. return NULL;
  302. }
  303. ret = dma_get_sgtable_attrs(buf->dev, sgt, buf->cookie, buf->dma_addr,
  304. buf->size, buf->attrs);
  305. if (ret < 0) {
  306. dev_err(buf->dev, "failed to get scatterlist from DMA API\n");
  307. kfree(sgt);
  308. return NULL;
  309. }
  310. return sgt;
  311. }
  312. static struct dma_buf *vb2_dc_get_dmabuf(void *buf_priv, unsigned long flags)
  313. {
  314. struct vb2_dc_buf *buf = buf_priv;
  315. struct dma_buf *dbuf;
  316. DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
  317. exp_info.ops = &vb2_dc_dmabuf_ops;
  318. exp_info.size = buf->size;
  319. exp_info.flags = flags;
  320. exp_info.priv = buf;
  321. if (!buf->sgt_base)
  322. buf->sgt_base = vb2_dc_get_base_sgt(buf);
  323. if (WARN_ON(!buf->sgt_base))
  324. return NULL;
  325. dbuf = dma_buf_export(&exp_info);
  326. if (IS_ERR(dbuf))
  327. return NULL;
  328. /* dmabuf keeps reference to vb2 buffer */
  329. refcount_inc(&buf->refcount);
  330. return dbuf;
  331. }
  332. /*********************************************/
  333. /* callbacks for USERPTR buffers */
  334. /*********************************************/
  335. static void vb2_dc_put_userptr(void *buf_priv)
  336. {
  337. struct vb2_dc_buf *buf = buf_priv;
  338. struct sg_table *sgt = buf->dma_sgt;
  339. int i;
  340. struct page **pages;
  341. if (sgt) {
  342. /*
  343. * No need to sync to CPU, it's already synced to the CPU
  344. * since the finish() memop will have been called before this.
  345. */
  346. dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
  347. buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
  348. pages = frame_vector_pages(buf->vec);
  349. /* sgt should exist only if vector contains pages... */
  350. BUG_ON(IS_ERR(pages));
  351. for (i = 0; i < frame_vector_count(buf->vec); i++)
  352. set_page_dirty_lock(pages[i]);
  353. sg_free_table(sgt);
  354. kfree(sgt);
  355. }
  356. vb2_destroy_framevec(buf->vec);
  357. kfree(buf);
  358. }
  359. /*
  360. * For some kind of reserved memory there might be no struct page available,
  361. * so all that can be done to support such 'pages' is to try to convert
  362. * pfn to dma address or at the last resort just assume that
  363. * dma address == physical address (like it has been assumed in earlier version
  364. * of videobuf2-dma-contig
  365. */
  366. #ifdef __arch_pfn_to_dma
  367. static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
  368. {
  369. return (dma_addr_t)__arch_pfn_to_dma(dev, pfn);
  370. }
  371. #elif defined(__pfn_to_bus)
  372. static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
  373. {
  374. return (dma_addr_t)__pfn_to_bus(pfn);
  375. }
  376. #elif defined(__pfn_to_phys)
  377. static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
  378. {
  379. return (dma_addr_t)__pfn_to_phys(pfn);
  380. }
  381. #else
  382. static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
  383. {
  384. /* really, we cannot do anything better at this point */
  385. return (dma_addr_t)(pfn) << PAGE_SHIFT;
  386. }
  387. #endif
  388. static void *vb2_dc_get_userptr(struct device *dev, unsigned long vaddr,
  389. unsigned long size, enum dma_data_direction dma_dir)
  390. {
  391. struct vb2_dc_buf *buf;
  392. struct frame_vector *vec;
  393. unsigned long offset;
  394. int n_pages, i;
  395. int ret = 0;
  396. struct sg_table *sgt;
  397. unsigned long contig_size;
  398. unsigned long dma_align = dma_get_cache_alignment();
  399. /* Only cache aligned DMA transfers are reliable */
  400. if (!IS_ALIGNED(vaddr | size, dma_align)) {
  401. pr_debug("user data must be aligned to %lu bytes\n", dma_align);
  402. return ERR_PTR(-EINVAL);
  403. }
  404. if (!size) {
  405. pr_debug("size is zero\n");
  406. return ERR_PTR(-EINVAL);
  407. }
  408. if (WARN_ON(!dev))
  409. return ERR_PTR(-EINVAL);
  410. buf = kzalloc(sizeof *buf, GFP_KERNEL);
  411. if (!buf)
  412. return ERR_PTR(-ENOMEM);
  413. buf->dev = dev;
  414. buf->dma_dir = dma_dir;
  415. offset = vaddr & ~PAGE_MASK;
  416. vec = vb2_create_framevec(vaddr, size, dma_dir == DMA_FROM_DEVICE);
  417. if (IS_ERR(vec)) {
  418. ret = PTR_ERR(vec);
  419. goto fail_buf;
  420. }
  421. buf->vec = vec;
  422. n_pages = frame_vector_count(vec);
  423. ret = frame_vector_to_pages(vec);
  424. if (ret < 0) {
  425. unsigned long *nums = frame_vector_pfns(vec);
  426. /*
  427. * Failed to convert to pages... Check the memory is physically
  428. * contiguous and use direct mapping
  429. */
  430. for (i = 1; i < n_pages; i++)
  431. if (nums[i-1] + 1 != nums[i])
  432. goto fail_pfnvec;
  433. buf->dma_addr = vb2_dc_pfn_to_dma(buf->dev, nums[0]);
  434. goto out;
  435. }
  436. sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
  437. if (!sgt) {
  438. pr_err("failed to allocate sg table\n");
  439. ret = -ENOMEM;
  440. goto fail_pfnvec;
  441. }
  442. ret = sg_alloc_table_from_pages(sgt, frame_vector_pages(vec), n_pages,
  443. offset, size, GFP_KERNEL);
  444. if (ret) {
  445. pr_err("failed to initialize sg table\n");
  446. goto fail_sgt;
  447. }
  448. /*
  449. * No need to sync to the device, this will happen later when the
  450. * prepare() memop is called.
  451. */
  452. sgt->nents = dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
  453. buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
  454. if (sgt->nents <= 0) {
  455. pr_err("failed to map scatterlist\n");
  456. ret = -EIO;
  457. goto fail_sgt_init;
  458. }
  459. contig_size = vb2_dc_get_contiguous_size(sgt);
  460. if (contig_size < size) {
  461. pr_err("contiguous mapping is too small %lu/%lu\n",
  462. contig_size, size);
  463. ret = -EFAULT;
  464. goto fail_map_sg;
  465. }
  466. buf->dma_addr = sg_dma_address(sgt->sgl);
  467. buf->dma_sgt = sgt;
  468. out:
  469. buf->size = size;
  470. return buf;
  471. fail_map_sg:
  472. dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
  473. buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
  474. fail_sgt_init:
  475. sg_free_table(sgt);
  476. fail_sgt:
  477. kfree(sgt);
  478. fail_pfnvec:
  479. vb2_destroy_framevec(vec);
  480. fail_buf:
  481. kfree(buf);
  482. return ERR_PTR(ret);
  483. }
  484. /*********************************************/
  485. /* callbacks for DMABUF buffers */
  486. /*********************************************/
  487. static int vb2_dc_map_dmabuf(void *mem_priv)
  488. {
  489. struct vb2_dc_buf *buf = mem_priv;
  490. struct sg_table *sgt;
  491. unsigned long contig_size;
  492. if (WARN_ON(!buf->db_attach)) {
  493. pr_err("trying to pin a non attached buffer\n");
  494. return -EINVAL;
  495. }
  496. if (WARN_ON(buf->dma_sgt)) {
  497. pr_err("dmabuf buffer is already pinned\n");
  498. return 0;
  499. }
  500. /* get the associated scatterlist for this buffer */
  501. sgt = dma_buf_map_attachment(buf->db_attach, buf->dma_dir);
  502. if (IS_ERR(sgt)) {
  503. pr_err("Error getting dmabuf scatterlist\n");
  504. return -EINVAL;
  505. }
  506. /* checking if dmabuf is big enough to store contiguous chunk */
  507. contig_size = vb2_dc_get_contiguous_size(sgt);
  508. if (contig_size < buf->size) {
  509. pr_err("contiguous chunk is too small %lu/%lu b\n",
  510. contig_size, buf->size);
  511. dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
  512. return -EFAULT;
  513. }
  514. buf->dma_addr = sg_dma_address(sgt->sgl);
  515. buf->dma_sgt = sgt;
  516. buf->vaddr = NULL;
  517. return 0;
  518. }
  519. static void vb2_dc_unmap_dmabuf(void *mem_priv)
  520. {
  521. struct vb2_dc_buf *buf = mem_priv;
  522. struct sg_table *sgt = buf->dma_sgt;
  523. if (WARN_ON(!buf->db_attach)) {
  524. pr_err("trying to unpin a not attached buffer\n");
  525. return;
  526. }
  527. if (WARN_ON(!sgt)) {
  528. pr_err("dmabuf buffer is already unpinned\n");
  529. return;
  530. }
  531. if (buf->vaddr) {
  532. dma_buf_vunmap(buf->db_attach->dmabuf, buf->vaddr);
  533. buf->vaddr = NULL;
  534. }
  535. dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
  536. buf->dma_addr = 0;
  537. buf->dma_sgt = NULL;
  538. }
  539. static void vb2_dc_detach_dmabuf(void *mem_priv)
  540. {
  541. struct vb2_dc_buf *buf = mem_priv;
  542. /* if vb2 works correctly you should never detach mapped buffer */
  543. if (WARN_ON(buf->dma_addr))
  544. vb2_dc_unmap_dmabuf(buf);
  545. /* detach this attachment */
  546. dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach);
  547. kfree(buf);
  548. }
  549. static void *vb2_dc_attach_dmabuf(struct device *dev, struct dma_buf *dbuf,
  550. unsigned long size, enum dma_data_direction dma_dir)
  551. {
  552. struct vb2_dc_buf *buf;
  553. struct dma_buf_attachment *dba;
  554. if (dbuf->size < size)
  555. return ERR_PTR(-EFAULT);
  556. if (WARN_ON(!dev))
  557. return ERR_PTR(-EINVAL);
  558. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  559. if (!buf)
  560. return ERR_PTR(-ENOMEM);
  561. buf->dev = dev;
  562. /* create attachment for the dmabuf with the user device */
  563. dba = dma_buf_attach(dbuf, buf->dev);
  564. if (IS_ERR(dba)) {
  565. pr_err("failed to attach dmabuf\n");
  566. kfree(buf);
  567. return dba;
  568. }
  569. buf->dma_dir = dma_dir;
  570. buf->size = size;
  571. buf->db_attach = dba;
  572. return buf;
  573. }
  574. /*********************************************/
  575. /* DMA CONTIG exported functions */
  576. /*********************************************/
  577. const struct vb2_mem_ops vb2_dma_contig_memops = {
  578. .alloc = vb2_dc_alloc,
  579. .put = vb2_dc_put,
  580. .get_dmabuf = vb2_dc_get_dmabuf,
  581. .cookie = vb2_dc_cookie,
  582. .vaddr = vb2_dc_vaddr,
  583. .mmap = vb2_dc_mmap,
  584. .get_userptr = vb2_dc_get_userptr,
  585. .put_userptr = vb2_dc_put_userptr,
  586. .prepare = vb2_dc_prepare,
  587. .finish = vb2_dc_finish,
  588. .map_dmabuf = vb2_dc_map_dmabuf,
  589. .unmap_dmabuf = vb2_dc_unmap_dmabuf,
  590. .attach_dmabuf = vb2_dc_attach_dmabuf,
  591. .detach_dmabuf = vb2_dc_detach_dmabuf,
  592. .num_users = vb2_dc_num_users,
  593. };
  594. EXPORT_SYMBOL_GPL(vb2_dma_contig_memops);
  595. /**
  596. * vb2_dma_contig_set_max_seg_size() - configure DMA max segment size
  597. * @dev: device for configuring DMA parameters
  598. * @size: size of DMA max segment size to set
  599. *
  600. * To allow mapping the scatter-list into a single chunk in the DMA
  601. * address space, the device is required to have the DMA max segment
  602. * size parameter set to a value larger than the buffer size. Otherwise,
  603. * the DMA-mapping subsystem will split the mapping into max segment
  604. * size chunks. This function sets the DMA max segment size
  605. * parameter to let DMA-mapping map a buffer as a single chunk in DMA
  606. * address space.
  607. * This code assumes that the DMA-mapping subsystem will merge all
  608. * scatterlist segments if this is really possible (for example when
  609. * an IOMMU is available and enabled).
  610. * Ideally, this parameter should be set by the generic bus code, but it
  611. * is left with the default 64KiB value due to historical litmiations in
  612. * other subsystems (like limited USB host drivers) and there no good
  613. * place to set it to the proper value.
  614. * This function should be called from the drivers, which are known to
  615. * operate on platforms with IOMMU and provide access to shared buffers
  616. * (either USERPTR or DMABUF). This should be done before initializing
  617. * videobuf2 queue.
  618. */
  619. int vb2_dma_contig_set_max_seg_size(struct device *dev, unsigned int size)
  620. {
  621. if (!dev->dma_parms) {
  622. dev->dma_parms = kzalloc(sizeof(*dev->dma_parms), GFP_KERNEL);
  623. if (!dev->dma_parms)
  624. return -ENOMEM;
  625. }
  626. if (dma_get_max_seg_size(dev) < size)
  627. return dma_set_max_seg_size(dev, size);
  628. return 0;
  629. }
  630. EXPORT_SYMBOL_GPL(vb2_dma_contig_set_max_seg_size);
  631. /*
  632. * vb2_dma_contig_clear_max_seg_size() - release resources for DMA parameters
  633. * @dev: device for configuring DMA parameters
  634. *
  635. * This function releases resources allocated to configure DMA parameters
  636. * (see vb2_dma_contig_set_max_seg_size() function). It should be called from
  637. * device drivers on driver remove.
  638. */
  639. void vb2_dma_contig_clear_max_seg_size(struct device *dev)
  640. {
  641. kfree(dev->dma_parms);
  642. dev->dma_parms = NULL;
  643. }
  644. EXPORT_SYMBOL_GPL(vb2_dma_contig_clear_max_seg_size);
  645. MODULE_DESCRIPTION("DMA-contig memory handling routines for videobuf2");
  646. MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>");
  647. MODULE_LICENSE("GPL");