videobuf2-dma-contig.c 17 KB

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