videobuf2-dma-contig.c 19 KB

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