videobuf2-dma-contig.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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. /* MMAP related */
  32. struct vb2_vmarea_handler handler;
  33. atomic_t refcount;
  34. struct sg_table *sgt_base;
  35. /* USERPTR related */
  36. struct vm_area_struct *vma;
  37. /* DMABUF related */
  38. struct dma_buf_attachment *db_attach;
  39. };
  40. /*********************************************/
  41. /* scatterlist table functions */
  42. /*********************************************/
  43. static void vb2_dc_sgt_foreach_page(struct sg_table *sgt,
  44. void (*cb)(struct page *pg))
  45. {
  46. struct scatterlist *s;
  47. unsigned int i;
  48. for_each_sg(sgt->sgl, s, sgt->orig_nents, i) {
  49. struct page *page = sg_page(s);
  50. unsigned int n_pages = PAGE_ALIGN(s->offset + s->length)
  51. >> PAGE_SHIFT;
  52. unsigned int j;
  53. for (j = 0; j < n_pages; ++j, ++page)
  54. cb(page);
  55. }
  56. }
  57. static unsigned long vb2_dc_get_contiguous_size(struct sg_table *sgt)
  58. {
  59. struct scatterlist *s;
  60. dma_addr_t expected = sg_dma_address(sgt->sgl);
  61. unsigned int i;
  62. unsigned long size = 0;
  63. for_each_sg(sgt->sgl, s, sgt->nents, i) {
  64. if (sg_dma_address(s) != expected)
  65. break;
  66. expected = sg_dma_address(s) + sg_dma_len(s);
  67. size += sg_dma_len(s);
  68. }
  69. return size;
  70. }
  71. /*********************************************/
  72. /* callbacks for all buffers */
  73. /*********************************************/
  74. static void *vb2_dc_cookie(void *buf_priv)
  75. {
  76. struct vb2_dc_buf *buf = buf_priv;
  77. return &buf->dma_addr;
  78. }
  79. static void *vb2_dc_vaddr(void *buf_priv)
  80. {
  81. struct vb2_dc_buf *buf = buf_priv;
  82. return buf->vaddr;
  83. }
  84. static unsigned int vb2_dc_num_users(void *buf_priv)
  85. {
  86. struct vb2_dc_buf *buf = buf_priv;
  87. return atomic_read(&buf->refcount);
  88. }
  89. static void vb2_dc_prepare(void *buf_priv)
  90. {
  91. struct vb2_dc_buf *buf = buf_priv;
  92. struct sg_table *sgt = buf->dma_sgt;
  93. /* DMABUF exporter will flush the cache for us */
  94. if (!sgt || buf->db_attach)
  95. return;
  96. dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
  97. }
  98. static void vb2_dc_finish(void *buf_priv)
  99. {
  100. struct vb2_dc_buf *buf = buf_priv;
  101. struct sg_table *sgt = buf->dma_sgt;
  102. /* DMABUF exporter will flush the cache for us */
  103. if (!sgt || buf->db_attach)
  104. return;
  105. dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
  106. }
  107. /*********************************************/
  108. /* callbacks for MMAP buffers */
  109. /*********************************************/
  110. static void vb2_dc_put(void *buf_priv)
  111. {
  112. struct vb2_dc_buf *buf = buf_priv;
  113. if (!atomic_dec_and_test(&buf->refcount))
  114. return;
  115. if (buf->sgt_base) {
  116. sg_free_table(buf->sgt_base);
  117. kfree(buf->sgt_base);
  118. }
  119. dma_free_coherent(buf->dev, buf->size, buf->vaddr, buf->dma_addr);
  120. put_device(buf->dev);
  121. kfree(buf);
  122. }
  123. static void *vb2_dc_alloc(void *alloc_ctx, unsigned long size, gfp_t gfp_flags)
  124. {
  125. struct vb2_dc_conf *conf = alloc_ctx;
  126. struct device *dev = conf->dev;
  127. struct vb2_dc_buf *buf;
  128. buf = kzalloc(sizeof *buf, GFP_KERNEL);
  129. if (!buf)
  130. return ERR_PTR(-ENOMEM);
  131. buf->vaddr = dma_alloc_coherent(dev, size, &buf->dma_addr,
  132. GFP_KERNEL | gfp_flags);
  133. if (!buf->vaddr) {
  134. dev_err(dev, "dma_alloc_coherent of size %ld failed\n", size);
  135. kfree(buf);
  136. return ERR_PTR(-ENOMEM);
  137. }
  138. /* Prevent the device from being released while the buffer is used */
  139. buf->dev = get_device(dev);
  140. buf->size = size;
  141. buf->handler.refcount = &buf->refcount;
  142. buf->handler.put = vb2_dc_put;
  143. buf->handler.arg = buf;
  144. atomic_inc(&buf->refcount);
  145. return buf;
  146. }
  147. static int vb2_dc_mmap(void *buf_priv, struct vm_area_struct *vma)
  148. {
  149. struct vb2_dc_buf *buf = buf_priv;
  150. int ret;
  151. if (!buf) {
  152. printk(KERN_ERR "No buffer to map\n");
  153. return -EINVAL;
  154. }
  155. /*
  156. * dma_mmap_* uses vm_pgoff as in-buffer offset, but we want to
  157. * map whole buffer
  158. */
  159. vma->vm_pgoff = 0;
  160. ret = dma_mmap_coherent(buf->dev, vma, buf->vaddr,
  161. buf->dma_addr, buf->size);
  162. if (ret) {
  163. pr_err("Remapping memory failed, error: %d\n", ret);
  164. return ret;
  165. }
  166. vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
  167. vma->vm_private_data = &buf->handler;
  168. vma->vm_ops = &vb2_common_vm_ops;
  169. vma->vm_ops->open(vma);
  170. pr_debug("%s: mapped dma addr 0x%08lx at 0x%08lx, size %ld\n",
  171. __func__, (unsigned long)buf->dma_addr, vma->vm_start,
  172. buf->size);
  173. return 0;
  174. }
  175. /*********************************************/
  176. /* DMABUF ops for exporters */
  177. /*********************************************/
  178. struct vb2_dc_attachment {
  179. struct sg_table sgt;
  180. enum dma_data_direction dir;
  181. };
  182. static int vb2_dc_dmabuf_ops_attach(struct dma_buf *dbuf, struct device *dev,
  183. struct dma_buf_attachment *dbuf_attach)
  184. {
  185. struct vb2_dc_attachment *attach;
  186. unsigned int i;
  187. struct scatterlist *rd, *wr;
  188. struct sg_table *sgt;
  189. struct vb2_dc_buf *buf = dbuf->priv;
  190. int ret;
  191. attach = kzalloc(sizeof(*attach), GFP_KERNEL);
  192. if (!attach)
  193. return -ENOMEM;
  194. sgt = &attach->sgt;
  195. /* Copy the buf->base_sgt scatter list to the attachment, as we can't
  196. * map the same scatter list to multiple attachments at the same time.
  197. */
  198. ret = sg_alloc_table(sgt, buf->sgt_base->orig_nents, GFP_KERNEL);
  199. if (ret) {
  200. kfree(attach);
  201. return -ENOMEM;
  202. }
  203. rd = buf->sgt_base->sgl;
  204. wr = sgt->sgl;
  205. for (i = 0; i < sgt->orig_nents; ++i) {
  206. sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
  207. rd = sg_next(rd);
  208. wr = sg_next(wr);
  209. }
  210. attach->dir = DMA_NONE;
  211. dbuf_attach->priv = attach;
  212. return 0;
  213. }
  214. static void vb2_dc_dmabuf_ops_detach(struct dma_buf *dbuf,
  215. struct dma_buf_attachment *db_attach)
  216. {
  217. struct vb2_dc_attachment *attach = db_attach->priv;
  218. struct sg_table *sgt;
  219. if (!attach)
  220. return;
  221. sgt = &attach->sgt;
  222. /* release the scatterlist cache */
  223. if (attach->dir != DMA_NONE)
  224. dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
  225. attach->dir);
  226. sg_free_table(sgt);
  227. kfree(attach);
  228. db_attach->priv = NULL;
  229. }
  230. static struct sg_table *vb2_dc_dmabuf_ops_map(
  231. struct dma_buf_attachment *db_attach, enum dma_data_direction dir)
  232. {
  233. struct vb2_dc_attachment *attach = db_attach->priv;
  234. /* stealing dmabuf mutex to serialize map/unmap operations */
  235. struct mutex *lock = &db_attach->dmabuf->lock;
  236. struct sg_table *sgt;
  237. int ret;
  238. mutex_lock(lock);
  239. sgt = &attach->sgt;
  240. /* return previously mapped sg table */
  241. if (attach->dir == dir) {
  242. mutex_unlock(lock);
  243. return sgt;
  244. }
  245. /* release any previous cache */
  246. if (attach->dir != DMA_NONE) {
  247. dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
  248. attach->dir);
  249. attach->dir = DMA_NONE;
  250. }
  251. /* mapping to the client with new direction */
  252. ret = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents, dir);
  253. if (ret <= 0) {
  254. pr_err("failed to map scatterlist\n");
  255. mutex_unlock(lock);
  256. return ERR_PTR(-EIO);
  257. }
  258. attach->dir = dir;
  259. mutex_unlock(lock);
  260. return sgt;
  261. }
  262. static void vb2_dc_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
  263. struct sg_table *sgt, enum dma_data_direction dir)
  264. {
  265. /* nothing to be done here */
  266. }
  267. static void vb2_dc_dmabuf_ops_release(struct dma_buf *dbuf)
  268. {
  269. /* drop reference obtained in vb2_dc_get_dmabuf */
  270. vb2_dc_put(dbuf->priv);
  271. }
  272. static void *vb2_dc_dmabuf_ops_kmap(struct dma_buf *dbuf, unsigned long pgnum)
  273. {
  274. struct vb2_dc_buf *buf = dbuf->priv;
  275. return buf->vaddr + pgnum * PAGE_SIZE;
  276. }
  277. static void *vb2_dc_dmabuf_ops_vmap(struct dma_buf *dbuf)
  278. {
  279. struct vb2_dc_buf *buf = dbuf->priv;
  280. return buf->vaddr;
  281. }
  282. static int vb2_dc_dmabuf_ops_mmap(struct dma_buf *dbuf,
  283. struct vm_area_struct *vma)
  284. {
  285. return vb2_dc_mmap(dbuf->priv, vma);
  286. }
  287. static struct dma_buf_ops vb2_dc_dmabuf_ops = {
  288. .attach = vb2_dc_dmabuf_ops_attach,
  289. .detach = vb2_dc_dmabuf_ops_detach,
  290. .map_dma_buf = vb2_dc_dmabuf_ops_map,
  291. .unmap_dma_buf = vb2_dc_dmabuf_ops_unmap,
  292. .kmap = vb2_dc_dmabuf_ops_kmap,
  293. .kmap_atomic = vb2_dc_dmabuf_ops_kmap,
  294. .vmap = vb2_dc_dmabuf_ops_vmap,
  295. .mmap = vb2_dc_dmabuf_ops_mmap,
  296. .release = vb2_dc_dmabuf_ops_release,
  297. };
  298. static struct sg_table *vb2_dc_get_base_sgt(struct vb2_dc_buf *buf)
  299. {
  300. int ret;
  301. struct sg_table *sgt;
  302. sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
  303. if (!sgt) {
  304. dev_err(buf->dev, "failed to alloc sg table\n");
  305. return NULL;
  306. }
  307. ret = dma_get_sgtable(buf->dev, sgt, buf->vaddr, buf->dma_addr,
  308. buf->size);
  309. if (ret < 0) {
  310. dev_err(buf->dev, "failed to get scatterlist from DMA API\n");
  311. kfree(sgt);
  312. return NULL;
  313. }
  314. return sgt;
  315. }
  316. static struct dma_buf *vb2_dc_get_dmabuf(void *buf_priv)
  317. {
  318. struct vb2_dc_buf *buf = buf_priv;
  319. struct dma_buf *dbuf;
  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(buf, &vb2_dc_dmabuf_ops, buf->size, 0);
  325. if (IS_ERR(dbuf))
  326. return NULL;
  327. /* dmabuf keeps reference to vb2 buffer */
  328. atomic_inc(&buf->refcount);
  329. return dbuf;
  330. }
  331. /*********************************************/
  332. /* callbacks for USERPTR buffers */
  333. /*********************************************/
  334. static inline int vma_is_io(struct vm_area_struct *vma)
  335. {
  336. return !!(vma->vm_flags & (VM_IO | VM_PFNMAP));
  337. }
  338. static int vb2_dc_get_user_pages(unsigned long start, struct page **pages,
  339. int n_pages, struct vm_area_struct *vma, int write)
  340. {
  341. if (vma_is_io(vma)) {
  342. unsigned int i;
  343. for (i = 0; i < n_pages; ++i, start += PAGE_SIZE) {
  344. unsigned long pfn;
  345. int ret = follow_pfn(vma, start, &pfn);
  346. if (ret) {
  347. pr_err("no page for address %lu\n", start);
  348. return ret;
  349. }
  350. pages[i] = pfn_to_page(pfn);
  351. }
  352. } else {
  353. int n;
  354. n = get_user_pages(current, current->mm, start & PAGE_MASK,
  355. n_pages, write, 1, pages, NULL);
  356. /* negative error means that no page was pinned */
  357. n = max(n, 0);
  358. if (n != n_pages) {
  359. pr_err("got only %d of %d user pages\n", n, n_pages);
  360. while (n)
  361. put_page(pages[--n]);
  362. return -EFAULT;
  363. }
  364. }
  365. return 0;
  366. }
  367. static void vb2_dc_put_dirty_page(struct page *page)
  368. {
  369. set_page_dirty_lock(page);
  370. put_page(page);
  371. }
  372. static void vb2_dc_put_userptr(void *buf_priv)
  373. {
  374. struct vb2_dc_buf *buf = buf_priv;
  375. struct sg_table *sgt = buf->dma_sgt;
  376. dma_unmap_sg(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir);
  377. if (!vma_is_io(buf->vma))
  378. vb2_dc_sgt_foreach_page(sgt, vb2_dc_put_dirty_page);
  379. sg_free_table(sgt);
  380. kfree(sgt);
  381. vb2_put_vma(buf->vma);
  382. kfree(buf);
  383. }
  384. static void *vb2_dc_get_userptr(void *alloc_ctx, unsigned long vaddr,
  385. unsigned long size, int write)
  386. {
  387. struct vb2_dc_conf *conf = alloc_ctx;
  388. struct vb2_dc_buf *buf;
  389. unsigned long start;
  390. unsigned long end;
  391. unsigned long offset;
  392. struct page **pages;
  393. int n_pages;
  394. int ret = 0;
  395. struct vm_area_struct *vma;
  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. buf = kzalloc(sizeof *buf, GFP_KERNEL);
  409. if (!buf)
  410. return ERR_PTR(-ENOMEM);
  411. buf->dev = conf->dev;
  412. buf->dma_dir = write ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  413. start = vaddr & PAGE_MASK;
  414. offset = vaddr & ~PAGE_MASK;
  415. end = PAGE_ALIGN(vaddr + size);
  416. n_pages = (end - start) >> PAGE_SHIFT;
  417. pages = kmalloc(n_pages * sizeof(pages[0]), GFP_KERNEL);
  418. if (!pages) {
  419. ret = -ENOMEM;
  420. pr_err("failed to allocate pages table\n");
  421. goto fail_buf;
  422. }
  423. /* current->mm->mmap_sem is taken by videobuf2 core */
  424. vma = find_vma(current->mm, vaddr);
  425. if (!vma) {
  426. pr_err("no vma for address %lu\n", vaddr);
  427. ret = -EFAULT;
  428. goto fail_pages;
  429. }
  430. if (vma->vm_end < vaddr + size) {
  431. pr_err("vma at %lu is too small for %lu bytes\n", vaddr, size);
  432. ret = -EFAULT;
  433. goto fail_pages;
  434. }
  435. buf->vma = vb2_get_vma(vma);
  436. if (!buf->vma) {
  437. pr_err("failed to copy vma\n");
  438. ret = -ENOMEM;
  439. goto fail_pages;
  440. }
  441. /* extract page list from userspace mapping */
  442. ret = vb2_dc_get_user_pages(start, pages, n_pages, vma, write);
  443. if (ret) {
  444. pr_err("failed to get user pages\n");
  445. goto fail_vma;
  446. }
  447. sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
  448. if (!sgt) {
  449. pr_err("failed to allocate sg table\n");
  450. ret = -ENOMEM;
  451. goto fail_get_user_pages;
  452. }
  453. ret = sg_alloc_table_from_pages(sgt, pages, n_pages,
  454. offset, size, GFP_KERNEL);
  455. if (ret) {
  456. pr_err("failed to initialize sg table\n");
  457. goto fail_sgt;
  458. }
  459. /* pages are no longer needed */
  460. kfree(pages);
  461. pages = NULL;
  462. sgt->nents = dma_map_sg(buf->dev, sgt->sgl, sgt->orig_nents,
  463. buf->dma_dir);
  464. if (sgt->nents <= 0) {
  465. pr_err("failed to map scatterlist\n");
  466. ret = -EIO;
  467. goto fail_sgt_init;
  468. }
  469. contig_size = vb2_dc_get_contiguous_size(sgt);
  470. if (contig_size < size) {
  471. pr_err("contiguous mapping is too small %lu/%lu\n",
  472. contig_size, size);
  473. ret = -EFAULT;
  474. goto fail_map_sg;
  475. }
  476. buf->dma_addr = sg_dma_address(sgt->sgl);
  477. buf->size = size;
  478. buf->dma_sgt = sgt;
  479. return buf;
  480. fail_map_sg:
  481. dma_unmap_sg(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir);
  482. fail_sgt_init:
  483. if (!vma_is_io(buf->vma))
  484. vb2_dc_sgt_foreach_page(sgt, put_page);
  485. sg_free_table(sgt);
  486. fail_sgt:
  487. kfree(sgt);
  488. fail_get_user_pages:
  489. if (pages && !vma_is_io(buf->vma))
  490. while (n_pages)
  491. put_page(pages[--n_pages]);
  492. fail_vma:
  493. vb2_put_vma(buf->vma);
  494. fail_pages:
  495. kfree(pages); /* kfree is NULL-proof */
  496. fail_buf:
  497. kfree(buf);
  498. return ERR_PTR(ret);
  499. }
  500. /*********************************************/
  501. /* callbacks for DMABUF buffers */
  502. /*********************************************/
  503. static int vb2_dc_map_dmabuf(void *mem_priv)
  504. {
  505. struct vb2_dc_buf *buf = mem_priv;
  506. struct sg_table *sgt;
  507. unsigned long contig_size;
  508. if (WARN_ON(!buf->db_attach)) {
  509. pr_err("trying to pin a non attached buffer\n");
  510. return -EINVAL;
  511. }
  512. if (WARN_ON(buf->dma_sgt)) {
  513. pr_err("dmabuf buffer is already pinned\n");
  514. return 0;
  515. }
  516. /* get the associated scatterlist for this buffer */
  517. sgt = dma_buf_map_attachment(buf->db_attach, buf->dma_dir);
  518. if (IS_ERR_OR_NULL(sgt)) {
  519. pr_err("Error getting dmabuf scatterlist\n");
  520. return -EINVAL;
  521. }
  522. /* checking if dmabuf is big enough to store contiguous chunk */
  523. contig_size = vb2_dc_get_contiguous_size(sgt);
  524. if (contig_size < buf->size) {
  525. pr_err("contiguous chunk is too small %lu/%lu b\n",
  526. contig_size, buf->size);
  527. dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
  528. return -EFAULT;
  529. }
  530. buf->dma_addr = sg_dma_address(sgt->sgl);
  531. buf->dma_sgt = sgt;
  532. return 0;
  533. }
  534. static void vb2_dc_unmap_dmabuf(void *mem_priv)
  535. {
  536. struct vb2_dc_buf *buf = mem_priv;
  537. struct sg_table *sgt = buf->dma_sgt;
  538. if (WARN_ON(!buf->db_attach)) {
  539. pr_err("trying to unpin a not attached buffer\n");
  540. return;
  541. }
  542. if (WARN_ON(!sgt)) {
  543. pr_err("dmabuf buffer is already unpinned\n");
  544. return;
  545. }
  546. dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
  547. buf->dma_addr = 0;
  548. buf->dma_sgt = NULL;
  549. }
  550. static void vb2_dc_detach_dmabuf(void *mem_priv)
  551. {
  552. struct vb2_dc_buf *buf = mem_priv;
  553. /* if vb2 works correctly you should never detach mapped buffer */
  554. if (WARN_ON(buf->dma_addr))
  555. vb2_dc_unmap_dmabuf(buf);
  556. /* detach this attachment */
  557. dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach);
  558. kfree(buf);
  559. }
  560. static void *vb2_dc_attach_dmabuf(void *alloc_ctx, struct dma_buf *dbuf,
  561. unsigned long size, int write)
  562. {
  563. struct vb2_dc_conf *conf = alloc_ctx;
  564. struct vb2_dc_buf *buf;
  565. struct dma_buf_attachment *dba;
  566. if (dbuf->size < size)
  567. return ERR_PTR(-EFAULT);
  568. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  569. if (!buf)
  570. return ERR_PTR(-ENOMEM);
  571. buf->dev = conf->dev;
  572. /* create attachment for the dmabuf with the user device */
  573. dba = dma_buf_attach(dbuf, buf->dev);
  574. if (IS_ERR(dba)) {
  575. pr_err("failed to attach dmabuf\n");
  576. kfree(buf);
  577. return dba;
  578. }
  579. buf->dma_dir = write ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  580. buf->size = size;
  581. buf->db_attach = dba;
  582. return buf;
  583. }
  584. /*********************************************/
  585. /* DMA CONTIG exported functions */
  586. /*********************************************/
  587. const struct vb2_mem_ops vb2_dma_contig_memops = {
  588. .alloc = vb2_dc_alloc,
  589. .put = vb2_dc_put,
  590. .get_dmabuf = vb2_dc_get_dmabuf,
  591. .cookie = vb2_dc_cookie,
  592. .vaddr = vb2_dc_vaddr,
  593. .mmap = vb2_dc_mmap,
  594. .get_userptr = vb2_dc_get_userptr,
  595. .put_userptr = vb2_dc_put_userptr,
  596. .prepare = vb2_dc_prepare,
  597. .finish = vb2_dc_finish,
  598. .map_dmabuf = vb2_dc_map_dmabuf,
  599. .unmap_dmabuf = vb2_dc_unmap_dmabuf,
  600. .attach_dmabuf = vb2_dc_attach_dmabuf,
  601. .detach_dmabuf = vb2_dc_detach_dmabuf,
  602. .num_users = vb2_dc_num_users,
  603. };
  604. EXPORT_SYMBOL_GPL(vb2_dma_contig_memops);
  605. void *vb2_dma_contig_init_ctx(struct device *dev)
  606. {
  607. struct vb2_dc_conf *conf;
  608. conf = kzalloc(sizeof *conf, GFP_KERNEL);
  609. if (!conf)
  610. return ERR_PTR(-ENOMEM);
  611. conf->dev = dev;
  612. return conf;
  613. }
  614. EXPORT_SYMBOL_GPL(vb2_dma_contig_init_ctx);
  615. void vb2_dma_contig_cleanup_ctx(void *alloc_ctx)
  616. {
  617. kfree(alloc_ctx);
  618. }
  619. EXPORT_SYMBOL_GPL(vb2_dma_contig_cleanup_ctx);
  620. MODULE_DESCRIPTION("DMA-contig memory handling routines for videobuf2");
  621. MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>");
  622. MODULE_LICENSE("GPL");