videobuf2-dma-contig.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  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/scatterlist.h>
  15. #include <linux/sched.h>
  16. #include <linux/slab.h>
  17. #include <linux/dma-mapping.h>
  18. #include <media/videobuf2-core.h>
  19. #include <media/videobuf2-dma-contig.h>
  20. #include <media/videobuf2-memops.h>
  21. struct vb2_dc_conf {
  22. struct device *dev;
  23. };
  24. struct vb2_dc_buf {
  25. struct device *dev;
  26. void *vaddr;
  27. unsigned long size;
  28. dma_addr_t dma_addr;
  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. atomic_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 atomic_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->nents, buf->dma_dir);
  84. }
  85. static void vb2_dc_finish(void *buf_priv)
  86. {
  87. struct vb2_dc_buf *buf = buf_priv;
  88. struct sg_table *sgt = buf->dma_sgt;
  89. /* DMABUF exporter will flush the cache for us */
  90. if (!sgt || buf->db_attach)
  91. return;
  92. dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
  93. }
  94. /*********************************************/
  95. /* callbacks for MMAP buffers */
  96. /*********************************************/
  97. static void vb2_dc_put(void *buf_priv)
  98. {
  99. struct vb2_dc_buf *buf = buf_priv;
  100. if (!atomic_dec_and_test(&buf->refcount))
  101. return;
  102. if (buf->sgt_base) {
  103. sg_free_table(buf->sgt_base);
  104. kfree(buf->sgt_base);
  105. }
  106. dma_free_coherent(buf->dev, buf->size, buf->vaddr, buf->dma_addr);
  107. put_device(buf->dev);
  108. kfree(buf);
  109. }
  110. static void *vb2_dc_alloc(void *alloc_ctx, unsigned long size,
  111. enum dma_data_direction dma_dir, gfp_t gfp_flags)
  112. {
  113. struct vb2_dc_conf *conf = alloc_ctx;
  114. struct device *dev = conf->dev;
  115. struct vb2_dc_buf *buf;
  116. buf = kzalloc(sizeof *buf, GFP_KERNEL);
  117. if (!buf)
  118. return ERR_PTR(-ENOMEM);
  119. buf->vaddr = dma_alloc_coherent(dev, size, &buf->dma_addr,
  120. GFP_KERNEL | gfp_flags);
  121. if (!buf->vaddr) {
  122. dev_err(dev, "dma_alloc_coherent of size %ld failed\n", size);
  123. kfree(buf);
  124. return ERR_PTR(-ENOMEM);
  125. }
  126. /* Prevent the device from being released while the buffer is used */
  127. buf->dev = get_device(dev);
  128. buf->size = size;
  129. buf->dma_dir = dma_dir;
  130. buf->handler.refcount = &buf->refcount;
  131. buf->handler.put = vb2_dc_put;
  132. buf->handler.arg = buf;
  133. atomic_inc(&buf->refcount);
  134. return buf;
  135. }
  136. static int vb2_dc_mmap(void *buf_priv, struct vm_area_struct *vma)
  137. {
  138. struct vb2_dc_buf *buf = buf_priv;
  139. int ret;
  140. if (!buf) {
  141. printk(KERN_ERR "No buffer to map\n");
  142. return -EINVAL;
  143. }
  144. /*
  145. * dma_mmap_* uses vm_pgoff as in-buffer offset, but we want to
  146. * map whole buffer
  147. */
  148. vma->vm_pgoff = 0;
  149. ret = dma_mmap_coherent(buf->dev, vma, buf->vaddr,
  150. buf->dma_addr, buf->size);
  151. if (ret) {
  152. pr_err("Remapping memory failed, error: %d\n", ret);
  153. return ret;
  154. }
  155. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  156. vma->vm_private_data = &buf->handler;
  157. vma->vm_ops = &vb2_common_vm_ops;
  158. vma->vm_ops->open(vma);
  159. pr_debug("%s: mapped dma addr 0x%08lx at 0x%08lx, size %ld\n",
  160. __func__, (unsigned long)buf->dma_addr, vma->vm_start,
  161. buf->size);
  162. return 0;
  163. }
  164. /*********************************************/
  165. /* DMABUF ops for exporters */
  166. /*********************************************/
  167. struct vb2_dc_attachment {
  168. struct sg_table sgt;
  169. enum dma_data_direction dma_dir;
  170. };
  171. static int vb2_dc_dmabuf_ops_attach(struct dma_buf *dbuf, struct device *dev,
  172. struct dma_buf_attachment *dbuf_attach)
  173. {
  174. struct vb2_dc_attachment *attach;
  175. unsigned int i;
  176. struct scatterlist *rd, *wr;
  177. struct sg_table *sgt;
  178. struct vb2_dc_buf *buf = dbuf->priv;
  179. int ret;
  180. attach = kzalloc(sizeof(*attach), GFP_KERNEL);
  181. if (!attach)
  182. return -ENOMEM;
  183. sgt = &attach->sgt;
  184. /* Copy the buf->base_sgt scatter list to the attachment, as we can't
  185. * map the same scatter list to multiple attachments at the same time.
  186. */
  187. ret = sg_alloc_table(sgt, buf->sgt_base->orig_nents, GFP_KERNEL);
  188. if (ret) {
  189. kfree(attach);
  190. return -ENOMEM;
  191. }
  192. rd = buf->sgt_base->sgl;
  193. wr = sgt->sgl;
  194. for (i = 0; i < sgt->orig_nents; ++i) {
  195. sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
  196. rd = sg_next(rd);
  197. wr = sg_next(wr);
  198. }
  199. attach->dma_dir = DMA_NONE;
  200. dbuf_attach->priv = attach;
  201. return 0;
  202. }
  203. static void vb2_dc_dmabuf_ops_detach(struct dma_buf *dbuf,
  204. struct dma_buf_attachment *db_attach)
  205. {
  206. struct vb2_dc_attachment *attach = db_attach->priv;
  207. struct sg_table *sgt;
  208. if (!attach)
  209. return;
  210. sgt = &attach->sgt;
  211. /* release the scatterlist cache */
  212. if (attach->dma_dir != DMA_NONE)
  213. dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
  214. attach->dma_dir);
  215. sg_free_table(sgt);
  216. kfree(attach);
  217. db_attach->priv = NULL;
  218. }
  219. static struct sg_table *vb2_dc_dmabuf_ops_map(
  220. struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir)
  221. {
  222. struct vb2_dc_attachment *attach = db_attach->priv;
  223. /* stealing dmabuf mutex to serialize map/unmap operations */
  224. struct mutex *lock = &db_attach->dmabuf->lock;
  225. struct sg_table *sgt;
  226. mutex_lock(lock);
  227. sgt = &attach->sgt;
  228. /* return previously mapped sg table */
  229. if (attach->dma_dir == dma_dir) {
  230. mutex_unlock(lock);
  231. return sgt;
  232. }
  233. /* release any previous cache */
  234. if (attach->dma_dir != DMA_NONE) {
  235. dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
  236. attach->dma_dir);
  237. attach->dma_dir = DMA_NONE;
  238. }
  239. /* mapping to the client with new direction */
  240. sgt->nents = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
  241. dma_dir);
  242. if (!sgt->nents) {
  243. pr_err("failed to map scatterlist\n");
  244. mutex_unlock(lock);
  245. return ERR_PTR(-EIO);
  246. }
  247. attach->dma_dir = dma_dir;
  248. mutex_unlock(lock);
  249. return sgt;
  250. }
  251. static void vb2_dc_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
  252. struct sg_table *sgt, enum dma_data_direction dma_dir)
  253. {
  254. /* nothing to be done here */
  255. }
  256. static void vb2_dc_dmabuf_ops_release(struct dma_buf *dbuf)
  257. {
  258. /* drop reference obtained in vb2_dc_get_dmabuf */
  259. vb2_dc_put(dbuf->priv);
  260. }
  261. static void *vb2_dc_dmabuf_ops_kmap(struct dma_buf *dbuf, unsigned long pgnum)
  262. {
  263. struct vb2_dc_buf *buf = dbuf->priv;
  264. return buf->vaddr + pgnum * PAGE_SIZE;
  265. }
  266. static void *vb2_dc_dmabuf_ops_vmap(struct dma_buf *dbuf)
  267. {
  268. struct vb2_dc_buf *buf = dbuf->priv;
  269. return buf->vaddr;
  270. }
  271. static int vb2_dc_dmabuf_ops_mmap(struct dma_buf *dbuf,
  272. struct vm_area_struct *vma)
  273. {
  274. return vb2_dc_mmap(dbuf->priv, vma);
  275. }
  276. static struct dma_buf_ops vb2_dc_dmabuf_ops = {
  277. .attach = vb2_dc_dmabuf_ops_attach,
  278. .detach = vb2_dc_dmabuf_ops_detach,
  279. .map_dma_buf = vb2_dc_dmabuf_ops_map,
  280. .unmap_dma_buf = vb2_dc_dmabuf_ops_unmap,
  281. .kmap = vb2_dc_dmabuf_ops_kmap,
  282. .kmap_atomic = vb2_dc_dmabuf_ops_kmap,
  283. .vmap = vb2_dc_dmabuf_ops_vmap,
  284. .mmap = vb2_dc_dmabuf_ops_mmap,
  285. .release = vb2_dc_dmabuf_ops_release,
  286. };
  287. static struct sg_table *vb2_dc_get_base_sgt(struct vb2_dc_buf *buf)
  288. {
  289. int ret;
  290. struct sg_table *sgt;
  291. sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
  292. if (!sgt) {
  293. dev_err(buf->dev, "failed to alloc sg table\n");
  294. return NULL;
  295. }
  296. ret = dma_get_sgtable(buf->dev, sgt, buf->vaddr, buf->dma_addr,
  297. buf->size);
  298. if (ret < 0) {
  299. dev_err(buf->dev, "failed to get scatterlist from DMA API\n");
  300. kfree(sgt);
  301. return NULL;
  302. }
  303. return sgt;
  304. }
  305. static struct dma_buf *vb2_dc_get_dmabuf(void *buf_priv, unsigned long flags)
  306. {
  307. struct vb2_dc_buf *buf = buf_priv;
  308. struct dma_buf *dbuf;
  309. DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
  310. exp_info.ops = &vb2_dc_dmabuf_ops;
  311. exp_info.size = buf->size;
  312. exp_info.flags = flags;
  313. exp_info.priv = buf;
  314. if (!buf->sgt_base)
  315. buf->sgt_base = vb2_dc_get_base_sgt(buf);
  316. if (WARN_ON(!buf->sgt_base))
  317. return NULL;
  318. dbuf = dma_buf_export(&exp_info);
  319. if (IS_ERR(dbuf))
  320. return NULL;
  321. /* dmabuf keeps reference to vb2 buffer */
  322. atomic_inc(&buf->refcount);
  323. return dbuf;
  324. }
  325. /*********************************************/
  326. /* callbacks for USERPTR buffers */
  327. /*********************************************/
  328. static void vb2_dc_put_userptr(void *buf_priv)
  329. {
  330. struct vb2_dc_buf *buf = buf_priv;
  331. struct sg_table *sgt = buf->dma_sgt;
  332. int i;
  333. struct page **pages;
  334. if (sgt) {
  335. DEFINE_DMA_ATTRS(attrs);
  336. dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
  337. /*
  338. * No need to sync to CPU, it's already synced to the CPU
  339. * since the finish() memop will have been called before this.
  340. */
  341. dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
  342. buf->dma_dir, &attrs);
  343. pages = frame_vector_pages(buf->vec);
  344. /* sgt should exist only if vector contains pages... */
  345. BUG_ON(IS_ERR(pages));
  346. for (i = 0; i < frame_vector_count(buf->vec); i++)
  347. set_page_dirty_lock(pages[i]);
  348. sg_free_table(sgt);
  349. kfree(sgt);
  350. }
  351. vb2_destroy_framevec(buf->vec);
  352. kfree(buf);
  353. }
  354. /*
  355. * For some kind of reserved memory there might be no struct page available,
  356. * so all that can be done to support such 'pages' is to try to convert
  357. * pfn to dma address or at the last resort just assume that
  358. * dma address == physical address (like it has been assumed in earlier version
  359. * of videobuf2-dma-contig
  360. */
  361. #ifdef __arch_pfn_to_dma
  362. static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
  363. {
  364. return (dma_addr_t)__arch_pfn_to_dma(dev, pfn);
  365. }
  366. #elif defined(__pfn_to_bus)
  367. static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
  368. {
  369. return (dma_addr_t)__pfn_to_bus(pfn);
  370. }
  371. #elif defined(__pfn_to_phys)
  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_phys(pfn);
  375. }
  376. #else
  377. static inline dma_addr_t vb2_dc_pfn_to_dma(struct device *dev, unsigned long pfn)
  378. {
  379. /* really, we cannot do anything better at this point */
  380. return (dma_addr_t)(pfn) << PAGE_SHIFT;
  381. }
  382. #endif
  383. static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr,
  384. unsigned long size, enum dma_data_direction dma_dir)
  385. {
  386. struct vb2_dc_conf *conf = alloc_ctx;
  387. struct vb2_dc_buf *buf;
  388. struct frame_vector *vec;
  389. unsigned long offset;
  390. int n_pages, i;
  391. int ret = 0;
  392. struct sg_table *sgt;
  393. unsigned long contig_size;
  394. unsigned long dma_align = dma_get_cache_alignment();
  395. DEFINE_DMA_ATTRS(attrs);
  396. dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
  397. /* Only cache aligned DMA transfers are reliable */
  398. if (!IS_ALIGNED(vaddr | size, dma_align)) {
  399. pr_debug("user data must be aligned to %lu bytes\n", dma_align);
  400. return ERR_PTR(-EINVAL);
  401. }
  402. if (!size) {
  403. pr_debug("size is zero\n");
  404. return ERR_PTR(-EINVAL);
  405. }
  406. buf = kzalloc(sizeof *buf, GFP_KERNEL);
  407. if (!buf)
  408. return ERR_PTR(-ENOMEM);
  409. buf->dev = conf->dev;
  410. buf->dma_dir = dma_dir;
  411. offset = vaddr & ~PAGE_MASK;
  412. vec = vb2_create_framevec(vaddr, size, dma_dir == DMA_FROM_DEVICE);
  413. if (IS_ERR(vec)) {
  414. ret = PTR_ERR(vec);
  415. goto fail_buf;
  416. }
  417. buf->vec = vec;
  418. n_pages = frame_vector_count(vec);
  419. ret = frame_vector_to_pages(vec);
  420. if (ret < 0) {
  421. unsigned long *nums = frame_vector_pfns(vec);
  422. /*
  423. * Failed to convert to pages... Check the memory is physically
  424. * contiguous and use direct mapping
  425. */
  426. for (i = 1; i < n_pages; i++)
  427. if (nums[i-1] + 1 != nums[i])
  428. goto fail_pfnvec;
  429. buf->dma_addr = vb2_dc_pfn_to_dma(buf->dev, nums[0]);
  430. goto out;
  431. }
  432. sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
  433. if (!sgt) {
  434. pr_err("failed to allocate sg table\n");
  435. ret = -ENOMEM;
  436. goto fail_pfnvec;
  437. }
  438. ret = sg_alloc_table_from_pages(sgt, frame_vector_pages(vec), n_pages,
  439. offset, size, GFP_KERNEL);
  440. if (ret) {
  441. pr_err("failed to initialize sg table\n");
  442. goto fail_sgt;
  443. }
  444. /*
  445. * No need to sync to the device, this will happen later when the
  446. * prepare() memop is called.
  447. */
  448. sgt->nents = dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
  449. buf->dma_dir, &attrs);
  450. if (sgt->nents <= 0) {
  451. pr_err("failed to map scatterlist\n");
  452. ret = -EIO;
  453. goto fail_sgt_init;
  454. }
  455. contig_size = vb2_dc_get_contiguous_size(sgt);
  456. if (contig_size < size) {
  457. pr_err("contiguous mapping is too small %lu/%lu\n",
  458. contig_size, size);
  459. ret = -EFAULT;
  460. goto fail_map_sg;
  461. }
  462. buf->dma_addr = sg_dma_address(sgt->sgl);
  463. buf->dma_sgt = sgt;
  464. out:
  465. buf->size = size;
  466. return buf;
  467. fail_map_sg:
  468. dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
  469. buf->dma_dir, &attrs);
  470. fail_sgt_init:
  471. sg_free_table(sgt);
  472. fail_sgt:
  473. kfree(sgt);
  474. fail_pfnvec:
  475. vb2_destroy_framevec(vec);
  476. fail_buf:
  477. kfree(buf);
  478. return ERR_PTR(ret);
  479. }
  480. /*********************************************/
  481. /* callbacks for DMABUF buffers */
  482. /*********************************************/
  483. static int vb2_dc_map_dmabuf(void *mem_priv)
  484. {
  485. struct vb2_dc_buf *buf = mem_priv;
  486. struct sg_table *sgt;
  487. unsigned long contig_size;
  488. if (WARN_ON(!buf->db_attach)) {
  489. pr_err("trying to pin a non attached buffer\n");
  490. return -EINVAL;
  491. }
  492. if (WARN_ON(buf->dma_sgt)) {
  493. pr_err("dmabuf buffer is already pinned\n");
  494. return 0;
  495. }
  496. /* get the associated scatterlist for this buffer */
  497. sgt = dma_buf_map_attachment(buf->db_attach, buf->dma_dir);
  498. if (IS_ERR(sgt)) {
  499. pr_err("Error getting dmabuf scatterlist\n");
  500. return -EINVAL;
  501. }
  502. /* checking if dmabuf is big enough to store contiguous chunk */
  503. contig_size = vb2_dc_get_contiguous_size(sgt);
  504. if (contig_size < buf->size) {
  505. pr_err("contiguous chunk is too small %lu/%lu b\n",
  506. contig_size, buf->size);
  507. dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
  508. return -EFAULT;
  509. }
  510. buf->dma_addr = sg_dma_address(sgt->sgl);
  511. buf->dma_sgt = sgt;
  512. buf->vaddr = NULL;
  513. return 0;
  514. }
  515. static void vb2_dc_unmap_dmabuf(void *mem_priv)
  516. {
  517. struct vb2_dc_buf *buf = mem_priv;
  518. struct sg_table *sgt = buf->dma_sgt;
  519. if (WARN_ON(!buf->db_attach)) {
  520. pr_err("trying to unpin a not attached buffer\n");
  521. return;
  522. }
  523. if (WARN_ON(!sgt)) {
  524. pr_err("dmabuf buffer is already unpinned\n");
  525. return;
  526. }
  527. if (buf->vaddr) {
  528. dma_buf_vunmap(buf->db_attach->dmabuf, buf->vaddr);
  529. buf->vaddr = NULL;
  530. }
  531. dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
  532. buf->dma_addr = 0;
  533. buf->dma_sgt = NULL;
  534. }
  535. static void vb2_dc_detach_dmabuf(void *mem_priv)
  536. {
  537. struct vb2_dc_buf *buf = mem_priv;
  538. /* if vb2 works correctly you should never detach mapped buffer */
  539. if (WARN_ON(buf->dma_addr))
  540. vb2_dc_unmap_dmabuf(buf);
  541. /* detach this attachment */
  542. dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach);
  543. kfree(buf);
  544. }
  545. static void *vb2_dc_attach_dmabuf(void *alloc_ctx, struct dma_buf *dbuf,
  546. unsigned long size, enum dma_data_direction dma_dir)
  547. {
  548. struct vb2_dc_conf *conf = alloc_ctx;
  549. struct vb2_dc_buf *buf;
  550. struct dma_buf_attachment *dba;
  551. if (dbuf->size < size)
  552. return ERR_PTR(-EFAULT);
  553. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  554. if (!buf)
  555. return ERR_PTR(-ENOMEM);
  556. buf->dev = conf->dev;
  557. /* create attachment for the dmabuf with the user device */
  558. dba = dma_buf_attach(dbuf, buf->dev);
  559. if (IS_ERR(dba)) {
  560. pr_err("failed to attach dmabuf\n");
  561. kfree(buf);
  562. return dba;
  563. }
  564. buf->dma_dir = dma_dir;
  565. buf->size = size;
  566. buf->db_attach = dba;
  567. return buf;
  568. }
  569. /*********************************************/
  570. /* DMA CONTIG exported functions */
  571. /*********************************************/
  572. const struct vb2_mem_ops vb2_dma_contig_memops = {
  573. .alloc = vb2_dc_alloc,
  574. .put = vb2_dc_put,
  575. .get_dmabuf = vb2_dc_get_dmabuf,
  576. .cookie = vb2_dc_cookie,
  577. .vaddr = vb2_dc_vaddr,
  578. .mmap = vb2_dc_mmap,
  579. .get_userptr = vb2_dc_get_userptr,
  580. .put_userptr = vb2_dc_put_userptr,
  581. .prepare = vb2_dc_prepare,
  582. .finish = vb2_dc_finish,
  583. .map_dmabuf = vb2_dc_map_dmabuf,
  584. .unmap_dmabuf = vb2_dc_unmap_dmabuf,
  585. .attach_dmabuf = vb2_dc_attach_dmabuf,
  586. .detach_dmabuf = vb2_dc_detach_dmabuf,
  587. .num_users = vb2_dc_num_users,
  588. };
  589. EXPORT_SYMBOL_GPL(vb2_dma_contig_memops);
  590. void *vb2_dma_contig_init_ctx(struct device *dev)
  591. {
  592. struct vb2_dc_conf *conf;
  593. conf = kzalloc(sizeof *conf, GFP_KERNEL);
  594. if (!conf)
  595. return ERR_PTR(-ENOMEM);
  596. conf->dev = dev;
  597. return conf;
  598. }
  599. EXPORT_SYMBOL_GPL(vb2_dma_contig_init_ctx);
  600. void vb2_dma_contig_cleanup_ctx(void *alloc_ctx)
  601. {
  602. if (!IS_ERR_OR_NULL(alloc_ctx))
  603. kfree(alloc_ctx);
  604. }
  605. EXPORT_SYMBOL_GPL(vb2_dma_contig_cleanup_ctx);
  606. MODULE_DESCRIPTION("DMA-contig memory handling routines for videobuf2");
  607. MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>");
  608. MODULE_LICENSE("GPL");