videobuf2-dma-contig.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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,
  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 const 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. .vmap = vb2_dc_dmabuf_ops_vmap,
  290. .mmap = vb2_dc_dmabuf_ops_mmap,
  291. .release = vb2_dc_dmabuf_ops_release,
  292. };
  293. static struct sg_table *vb2_dc_get_base_sgt(struct vb2_dc_buf *buf)
  294. {
  295. int ret;
  296. struct sg_table *sgt;
  297. sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
  298. if (!sgt) {
  299. dev_err(buf->dev, "failed to alloc sg table\n");
  300. return NULL;
  301. }
  302. ret = dma_get_sgtable_attrs(buf->dev, sgt, buf->cookie, buf->dma_addr,
  303. buf->size, buf->attrs);
  304. if (ret < 0) {
  305. dev_err(buf->dev, "failed to get scatterlist from DMA API\n");
  306. kfree(sgt);
  307. return NULL;
  308. }
  309. return sgt;
  310. }
  311. static struct dma_buf *vb2_dc_get_dmabuf(void *buf_priv, unsigned long flags)
  312. {
  313. struct vb2_dc_buf *buf = buf_priv;
  314. struct dma_buf *dbuf;
  315. DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
  316. exp_info.ops = &vb2_dc_dmabuf_ops;
  317. exp_info.size = buf->size;
  318. exp_info.flags = flags;
  319. exp_info.priv = buf;
  320. if (!buf->sgt_base)
  321. buf->sgt_base = vb2_dc_get_base_sgt(buf);
  322. if (WARN_ON(!buf->sgt_base))
  323. return NULL;
  324. dbuf = dma_buf_export(&exp_info);
  325. if (IS_ERR(dbuf))
  326. return NULL;
  327. /* dmabuf keeps reference to vb2 buffer */
  328. refcount_inc(&buf->refcount);
  329. return dbuf;
  330. }
  331. /*********************************************/
  332. /* callbacks for USERPTR buffers */
  333. /*********************************************/
  334. static void vb2_dc_put_userptr(void *buf_priv)
  335. {
  336. struct vb2_dc_buf *buf = buf_priv;
  337. struct sg_table *sgt = buf->dma_sgt;
  338. int i;
  339. struct page **pages;
  340. if (sgt) {
  341. /*
  342. * No need to sync to CPU, it's already synced to the CPU
  343. * since the finish() memop will have been called before this.
  344. */
  345. dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
  346. buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
  347. pages = frame_vector_pages(buf->vec);
  348. /* sgt should exist only if vector contains pages... */
  349. BUG_ON(IS_ERR(pages));
  350. if (buf->dma_dir == DMA_FROM_DEVICE ||
  351. buf->dma_dir == DMA_BIDIRECTIONAL)
  352. for (i = 0; i < frame_vector_count(buf->vec); i++)
  353. set_page_dirty_lock(pages[i]);
  354. sg_free_table(sgt);
  355. kfree(sgt);
  356. }
  357. vb2_destroy_framevec(buf->vec);
  358. kfree(buf);
  359. }
  360. /*
  361. * For some kind of reserved memory there might be no struct page available,
  362. * so all that can be done to support such 'pages' is to try to convert
  363. * pfn to dma address or at the last resort just assume that
  364. * dma address == physical address (like it has been assumed in earlier version
  365. * of videobuf2-dma-contig
  366. */
  367. #ifdef __arch_pfn_to_dma
  368. static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
  369. {
  370. return (dma_addr_t)__arch_pfn_to_dma(dev, pfn);
  371. }
  372. #elif defined(__pfn_to_bus)
  373. static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
  374. {
  375. return (dma_addr_t)__pfn_to_bus(pfn);
  376. }
  377. #elif defined(__pfn_to_phys)
  378. static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
  379. {
  380. return (dma_addr_t)__pfn_to_phys(pfn);
  381. }
  382. #else
  383. static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
  384. {
  385. /* really, we cannot do anything better at this point */
  386. return (dma_addr_t)(pfn) << PAGE_SHIFT;
  387. }
  388. #endif
  389. static void *vb2_dc_get_userptr(struct device *dev, unsigned long vaddr,
  390. unsigned long size, enum dma_data_direction dma_dir)
  391. {
  392. struct vb2_dc_buf *buf;
  393. struct frame_vector *vec;
  394. unsigned int offset;
  395. int n_pages, i;
  396. int ret = 0;
  397. struct sg_table *sgt;
  398. unsigned long contig_size;
  399. unsigned long dma_align = dma_get_cache_alignment();
  400. /* Only cache aligned DMA transfers are reliable */
  401. if (!IS_ALIGNED(vaddr | size, dma_align)) {
  402. pr_debug("user data must be aligned to %lu bytes\n", dma_align);
  403. return ERR_PTR(-EINVAL);
  404. }
  405. if (!size) {
  406. pr_debug("size is zero\n");
  407. return ERR_PTR(-EINVAL);
  408. }
  409. if (WARN_ON(!dev))
  410. return ERR_PTR(-EINVAL);
  411. buf = kzalloc(sizeof *buf, GFP_KERNEL);
  412. if (!buf)
  413. return ERR_PTR(-ENOMEM);
  414. buf->dev = dev;
  415. buf->dma_dir = dma_dir;
  416. offset = lower_32_bits(offset_in_page(vaddr));
  417. vec = vb2_create_framevec(vaddr, size, dma_dir == DMA_FROM_DEVICE ||
  418. dma_dir == DMA_BIDIRECTIONAL);
  419. if (IS_ERR(vec)) {
  420. ret = PTR_ERR(vec);
  421. goto fail_buf;
  422. }
  423. buf->vec = vec;
  424. n_pages = frame_vector_count(vec);
  425. ret = frame_vector_to_pages(vec);
  426. if (ret < 0) {
  427. unsigned long *nums = frame_vector_pfns(vec);
  428. /*
  429. * Failed to convert to pages... Check the memory is physically
  430. * contiguous and use direct mapping
  431. */
  432. for (i = 1; i < n_pages; i++)
  433. if (nums[i-1] + 1 != nums[i])
  434. goto fail_pfnvec;
  435. buf->dma_addr = vb2_dc_pfn_to_dma(buf->dev, nums[0]);
  436. goto out;
  437. }
  438. sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
  439. if (!sgt) {
  440. pr_err("failed to allocate sg table\n");
  441. ret = -ENOMEM;
  442. goto fail_pfnvec;
  443. }
  444. ret = sg_alloc_table_from_pages(sgt, frame_vector_pages(vec), n_pages,
  445. offset, size, GFP_KERNEL);
  446. if (ret) {
  447. pr_err("failed to initialize sg table\n");
  448. goto fail_sgt;
  449. }
  450. /*
  451. * No need to sync to the device, this will happen later when the
  452. * prepare() memop is called.
  453. */
  454. sgt->nents = dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
  455. buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
  456. if (sgt->nents <= 0) {
  457. pr_err("failed to map scatterlist\n");
  458. ret = -EIO;
  459. goto fail_sgt_init;
  460. }
  461. contig_size = vb2_dc_get_contiguous_size(sgt);
  462. if (contig_size < size) {
  463. pr_err("contiguous mapping is too small %lu/%lu\n",
  464. contig_size, size);
  465. ret = -EFAULT;
  466. goto fail_map_sg;
  467. }
  468. buf->dma_addr = sg_dma_address(sgt->sgl);
  469. buf->dma_sgt = sgt;
  470. out:
  471. buf->size = size;
  472. return buf;
  473. fail_map_sg:
  474. dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
  475. buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
  476. fail_sgt_init:
  477. sg_free_table(sgt);
  478. fail_sgt:
  479. kfree(sgt);
  480. fail_pfnvec:
  481. vb2_destroy_framevec(vec);
  482. fail_buf:
  483. kfree(buf);
  484. return ERR_PTR(ret);
  485. }
  486. /*********************************************/
  487. /* callbacks for DMABUF buffers */
  488. /*********************************************/
  489. static int vb2_dc_map_dmabuf(void *mem_priv)
  490. {
  491. struct vb2_dc_buf *buf = mem_priv;
  492. struct sg_table *sgt;
  493. unsigned long contig_size;
  494. if (WARN_ON(!buf->db_attach)) {
  495. pr_err("trying to pin a non attached buffer\n");
  496. return -EINVAL;
  497. }
  498. if (WARN_ON(buf->dma_sgt)) {
  499. pr_err("dmabuf buffer is already pinned\n");
  500. return 0;
  501. }
  502. /* get the associated scatterlist for this buffer */
  503. sgt = dma_buf_map_attachment(buf->db_attach, buf->dma_dir);
  504. if (IS_ERR(sgt)) {
  505. pr_err("Error getting dmabuf scatterlist\n");
  506. return -EINVAL;
  507. }
  508. /* checking if dmabuf is big enough to store contiguous chunk */
  509. contig_size = vb2_dc_get_contiguous_size(sgt);
  510. if (contig_size < buf->size) {
  511. pr_err("contiguous chunk is too small %lu/%lu b\n",
  512. contig_size, buf->size);
  513. dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
  514. return -EFAULT;
  515. }
  516. buf->dma_addr = sg_dma_address(sgt->sgl);
  517. buf->dma_sgt = sgt;
  518. buf->vaddr = NULL;
  519. return 0;
  520. }
  521. static void vb2_dc_unmap_dmabuf(void *mem_priv)
  522. {
  523. struct vb2_dc_buf *buf = mem_priv;
  524. struct sg_table *sgt = buf->dma_sgt;
  525. if (WARN_ON(!buf->db_attach)) {
  526. pr_err("trying to unpin a not attached buffer\n");
  527. return;
  528. }
  529. if (WARN_ON(!sgt)) {
  530. pr_err("dmabuf buffer is already unpinned\n");
  531. return;
  532. }
  533. if (buf->vaddr) {
  534. dma_buf_vunmap(buf->db_attach->dmabuf, buf->vaddr);
  535. buf->vaddr = NULL;
  536. }
  537. dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
  538. buf->dma_addr = 0;
  539. buf->dma_sgt = NULL;
  540. }
  541. static void vb2_dc_detach_dmabuf(void *mem_priv)
  542. {
  543. struct vb2_dc_buf *buf = mem_priv;
  544. /* if vb2 works correctly you should never detach mapped buffer */
  545. if (WARN_ON(buf->dma_addr))
  546. vb2_dc_unmap_dmabuf(buf);
  547. /* detach this attachment */
  548. dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach);
  549. kfree(buf);
  550. }
  551. static void *vb2_dc_attach_dmabuf(struct device *dev, struct dma_buf *dbuf,
  552. unsigned long size, enum dma_data_direction dma_dir)
  553. {
  554. struct vb2_dc_buf *buf;
  555. struct dma_buf_attachment *dba;
  556. if (dbuf->size < size)
  557. return ERR_PTR(-EFAULT);
  558. if (WARN_ON(!dev))
  559. return ERR_PTR(-EINVAL);
  560. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  561. if (!buf)
  562. return ERR_PTR(-ENOMEM);
  563. buf->dev = dev;
  564. /* create attachment for the dmabuf with the user device */
  565. dba = dma_buf_attach(dbuf, buf->dev);
  566. if (IS_ERR(dba)) {
  567. pr_err("failed to attach dmabuf\n");
  568. kfree(buf);
  569. return dba;
  570. }
  571. buf->dma_dir = dma_dir;
  572. buf->size = size;
  573. buf->db_attach = dba;
  574. return buf;
  575. }
  576. /*********************************************/
  577. /* DMA CONTIG exported functions */
  578. /*********************************************/
  579. const struct vb2_mem_ops vb2_dma_contig_memops = {
  580. .alloc = vb2_dc_alloc,
  581. .put = vb2_dc_put,
  582. .get_dmabuf = vb2_dc_get_dmabuf,
  583. .cookie = vb2_dc_cookie,
  584. .vaddr = vb2_dc_vaddr,
  585. .mmap = vb2_dc_mmap,
  586. .get_userptr = vb2_dc_get_userptr,
  587. .put_userptr = vb2_dc_put_userptr,
  588. .prepare = vb2_dc_prepare,
  589. .finish = vb2_dc_finish,
  590. .map_dmabuf = vb2_dc_map_dmabuf,
  591. .unmap_dmabuf = vb2_dc_unmap_dmabuf,
  592. .attach_dmabuf = vb2_dc_attach_dmabuf,
  593. .detach_dmabuf = vb2_dc_detach_dmabuf,
  594. .num_users = vb2_dc_num_users,
  595. };
  596. EXPORT_SYMBOL_GPL(vb2_dma_contig_memops);
  597. /**
  598. * vb2_dma_contig_set_max_seg_size() - configure DMA max segment size
  599. * @dev: device for configuring DMA parameters
  600. * @size: size of DMA max segment size to set
  601. *
  602. * To allow mapping the scatter-list into a single chunk in the DMA
  603. * address space, the device is required to have the DMA max segment
  604. * size parameter set to a value larger than the buffer size. Otherwise,
  605. * the DMA-mapping subsystem will split the mapping into max segment
  606. * size chunks. This function sets the DMA max segment size
  607. * parameter to let DMA-mapping map a buffer as a single chunk in DMA
  608. * address space.
  609. * This code assumes that the DMA-mapping subsystem will merge all
  610. * scatterlist segments if this is really possible (for example when
  611. * an IOMMU is available and enabled).
  612. * Ideally, this parameter should be set by the generic bus code, but it
  613. * is left with the default 64KiB value due to historical litmiations in
  614. * other subsystems (like limited USB host drivers) and there no good
  615. * place to set it to the proper value.
  616. * This function should be called from the drivers, which are known to
  617. * operate on platforms with IOMMU and provide access to shared buffers
  618. * (either USERPTR or DMABUF). This should be done before initializing
  619. * videobuf2 queue.
  620. */
  621. int vb2_dma_contig_set_max_seg_size(struct device *dev, unsigned int size)
  622. {
  623. if (!dev->dma_parms) {
  624. dev->dma_parms = kzalloc(sizeof(*dev->dma_parms), GFP_KERNEL);
  625. if (!dev->dma_parms)
  626. return -ENOMEM;
  627. }
  628. if (dma_get_max_seg_size(dev) < size)
  629. return dma_set_max_seg_size(dev, size);
  630. return 0;
  631. }
  632. EXPORT_SYMBOL_GPL(vb2_dma_contig_set_max_seg_size);
  633. /*
  634. * vb2_dma_contig_clear_max_seg_size() - release resources for DMA parameters
  635. * @dev: device for configuring DMA parameters
  636. *
  637. * This function releases resources allocated to configure DMA parameters
  638. * (see vb2_dma_contig_set_max_seg_size() function). It should be called from
  639. * device drivers on driver remove.
  640. */
  641. void vb2_dma_contig_clear_max_seg_size(struct device *dev)
  642. {
  643. kfree(dev->dma_parms);
  644. dev->dma_parms = NULL;
  645. }
  646. EXPORT_SYMBOL_GPL(vb2_dma_contig_clear_max_seg_size);
  647. MODULE_DESCRIPTION("DMA-contig memory handling routines for videobuf2");
  648. MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>");
  649. MODULE_LICENSE("GPL");