videobuf2-dma-sg.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. /*
  2. * videobuf2-dma-sg.c - dma scatter/gather memory allocator for videobuf2
  3. *
  4. * Copyright (C) 2010 Samsung Electronics
  5. *
  6. * Author: Andrzej Pietrasiewicz <[email protected]>
  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/module.h>
  13. #include <linux/mm.h>
  14. #include <linux/refcount.h>
  15. #include <linux/scatterlist.h>
  16. #include <linux/sched.h>
  17. #include <linux/slab.h>
  18. #include <linux/vmalloc.h>
  19. #include <media/videobuf2-v4l2.h>
  20. #include <media/videobuf2-memops.h>
  21. #include <media/videobuf2-dma-sg.h>
  22. static int debug;
  23. module_param(debug, int, 0644);
  24. #define dprintk(level, fmt, arg...) \
  25. do { \
  26. if (debug >= level) \
  27. printk(KERN_DEBUG "vb2-dma-sg: " fmt, ## arg); \
  28. } while (0)
  29. struct vb2_dma_sg_buf {
  30. struct device *dev;
  31. void *vaddr;
  32. struct page **pages;
  33. struct frame_vector *vec;
  34. int offset;
  35. enum dma_data_direction dma_dir;
  36. struct sg_table sg_table;
  37. /*
  38. * This will point to sg_table when used with the MMAP or USERPTR
  39. * memory model, and to the dma_buf sglist when used with the
  40. * DMABUF memory model.
  41. */
  42. struct sg_table *dma_sgt;
  43. size_t size;
  44. unsigned int num_pages;
  45. refcount_t refcount;
  46. struct vb2_vmarea_handler handler;
  47. struct dma_buf_attachment *db_attach;
  48. struct vb2_buffer *vb;
  49. };
  50. static void vb2_dma_sg_put(void *buf_priv);
  51. static int vb2_dma_sg_alloc_compacted(struct vb2_dma_sg_buf *buf,
  52. gfp_t gfp_flags)
  53. {
  54. unsigned int last_page = 0;
  55. unsigned long size = buf->size;
  56. while (size > 0) {
  57. struct page *pages;
  58. int order;
  59. int i;
  60. order = get_order(size);
  61. /* Don't over allocate*/
  62. if ((PAGE_SIZE << order) > size)
  63. order--;
  64. pages = NULL;
  65. while (!pages) {
  66. pages = alloc_pages(GFP_KERNEL | __GFP_ZERO |
  67. __GFP_NOWARN | gfp_flags, order);
  68. if (pages)
  69. break;
  70. if (order == 0) {
  71. while (last_page--)
  72. __free_page(buf->pages[last_page]);
  73. return -ENOMEM;
  74. }
  75. order--;
  76. }
  77. split_page(pages, order);
  78. for (i = 0; i < (1 << order); i++)
  79. buf->pages[last_page++] = &pages[i];
  80. size -= PAGE_SIZE << order;
  81. }
  82. return 0;
  83. }
  84. static void *vb2_dma_sg_alloc(struct vb2_buffer *vb, struct device *dev,
  85. unsigned long size)
  86. {
  87. struct vb2_dma_sg_buf *buf;
  88. struct sg_table *sgt;
  89. int ret;
  90. int num_pages;
  91. if (WARN_ON(!dev) || WARN_ON(!size))
  92. return ERR_PTR(-EINVAL);
  93. buf = kzalloc(sizeof *buf, GFP_KERNEL);
  94. if (!buf)
  95. return ERR_PTR(-ENOMEM);
  96. buf->vaddr = NULL;
  97. buf->dma_dir = vb->vb2_queue->dma_dir;
  98. buf->offset = 0;
  99. buf->size = size;
  100. /* size is already page aligned */
  101. buf->num_pages = size >> PAGE_SHIFT;
  102. buf->dma_sgt = &buf->sg_table;
  103. /*
  104. * NOTE: dma-sg allocates memory using the page allocator directly, so
  105. * there is no memory consistency guarantee, hence dma-sg ignores DMA
  106. * attributes passed from the upper layer.
  107. */
  108. buf->pages = kvcalloc(buf->num_pages, sizeof(struct page *), GFP_KERNEL);
  109. if (!buf->pages)
  110. goto fail_pages_array_alloc;
  111. ret = vb2_dma_sg_alloc_compacted(buf, vb->vb2_queue->gfp_flags);
  112. if (ret)
  113. goto fail_pages_alloc;
  114. ret = sg_alloc_table_from_pages(buf->dma_sgt, buf->pages,
  115. buf->num_pages, 0, size, GFP_KERNEL);
  116. if (ret)
  117. goto fail_table_alloc;
  118. /* Prevent the device from being released while the buffer is used */
  119. buf->dev = get_device(dev);
  120. sgt = &buf->sg_table;
  121. /*
  122. * No need to sync to the device, this will happen later when the
  123. * prepare() memop is called.
  124. */
  125. if (dma_map_sgtable(buf->dev, sgt, buf->dma_dir,
  126. DMA_ATTR_SKIP_CPU_SYNC))
  127. goto fail_map;
  128. buf->handler.refcount = &buf->refcount;
  129. buf->handler.put = vb2_dma_sg_put;
  130. buf->handler.arg = buf;
  131. buf->vb = vb;
  132. refcount_set(&buf->refcount, 1);
  133. dprintk(1, "%s: Allocated buffer of %d pages\n",
  134. __func__, buf->num_pages);
  135. return buf;
  136. fail_map:
  137. put_device(buf->dev);
  138. sg_free_table(buf->dma_sgt);
  139. fail_table_alloc:
  140. num_pages = buf->num_pages;
  141. while (num_pages--)
  142. __free_page(buf->pages[num_pages]);
  143. fail_pages_alloc:
  144. kvfree(buf->pages);
  145. fail_pages_array_alloc:
  146. kfree(buf);
  147. return ERR_PTR(-ENOMEM);
  148. }
  149. static void vb2_dma_sg_put(void *buf_priv)
  150. {
  151. struct vb2_dma_sg_buf *buf = buf_priv;
  152. struct sg_table *sgt = &buf->sg_table;
  153. int i = buf->num_pages;
  154. if (refcount_dec_and_test(&buf->refcount)) {
  155. dprintk(1, "%s: Freeing buffer of %d pages\n", __func__,
  156. buf->num_pages);
  157. dma_unmap_sgtable(buf->dev, sgt, buf->dma_dir,
  158. DMA_ATTR_SKIP_CPU_SYNC);
  159. if (buf->vaddr)
  160. vm_unmap_ram(buf->vaddr, buf->num_pages);
  161. sg_free_table(buf->dma_sgt);
  162. while (--i >= 0)
  163. __free_page(buf->pages[i]);
  164. kvfree(buf->pages);
  165. put_device(buf->dev);
  166. kfree(buf);
  167. }
  168. }
  169. static void vb2_dma_sg_prepare(void *buf_priv)
  170. {
  171. struct vb2_dma_sg_buf *buf = buf_priv;
  172. struct sg_table *sgt = buf->dma_sgt;
  173. if (buf->vb->skip_cache_sync_on_prepare)
  174. return;
  175. dma_sync_sgtable_for_device(buf->dev, sgt, buf->dma_dir);
  176. }
  177. static void vb2_dma_sg_finish(void *buf_priv)
  178. {
  179. struct vb2_dma_sg_buf *buf = buf_priv;
  180. struct sg_table *sgt = buf->dma_sgt;
  181. if (buf->vb->skip_cache_sync_on_finish)
  182. return;
  183. dma_sync_sgtable_for_cpu(buf->dev, sgt, buf->dma_dir);
  184. }
  185. static void *vb2_dma_sg_get_userptr(struct vb2_buffer *vb, struct device *dev,
  186. unsigned long vaddr, unsigned long size)
  187. {
  188. struct vb2_dma_sg_buf *buf;
  189. struct sg_table *sgt;
  190. struct frame_vector *vec;
  191. if (WARN_ON(!dev))
  192. return ERR_PTR(-EINVAL);
  193. buf = kzalloc(sizeof *buf, GFP_KERNEL);
  194. if (!buf)
  195. return ERR_PTR(-ENOMEM);
  196. buf->vaddr = NULL;
  197. buf->dev = dev;
  198. buf->dma_dir = vb->vb2_queue->dma_dir;
  199. buf->offset = vaddr & ~PAGE_MASK;
  200. buf->size = size;
  201. buf->dma_sgt = &buf->sg_table;
  202. buf->vb = vb;
  203. vec = vb2_create_framevec(vaddr, size);
  204. if (IS_ERR(vec))
  205. goto userptr_fail_pfnvec;
  206. buf->vec = vec;
  207. buf->pages = frame_vector_pages(vec);
  208. if (IS_ERR(buf->pages))
  209. goto userptr_fail_sgtable;
  210. buf->num_pages = frame_vector_count(vec);
  211. if (sg_alloc_table_from_pages(buf->dma_sgt, buf->pages,
  212. buf->num_pages, buf->offset, size, 0))
  213. goto userptr_fail_sgtable;
  214. sgt = &buf->sg_table;
  215. /*
  216. * No need to sync to the device, this will happen later when the
  217. * prepare() memop is called.
  218. */
  219. if (dma_map_sgtable(buf->dev, sgt, buf->dma_dir,
  220. DMA_ATTR_SKIP_CPU_SYNC))
  221. goto userptr_fail_map;
  222. return buf;
  223. userptr_fail_map:
  224. sg_free_table(&buf->sg_table);
  225. userptr_fail_sgtable:
  226. vb2_destroy_framevec(vec);
  227. userptr_fail_pfnvec:
  228. kfree(buf);
  229. return ERR_PTR(-ENOMEM);
  230. }
  231. /*
  232. * @put_userptr: inform the allocator that a USERPTR buffer will no longer
  233. * be used
  234. */
  235. static void vb2_dma_sg_put_userptr(void *buf_priv)
  236. {
  237. struct vb2_dma_sg_buf *buf = buf_priv;
  238. struct sg_table *sgt = &buf->sg_table;
  239. int i = buf->num_pages;
  240. dprintk(1, "%s: Releasing userspace buffer of %d pages\n",
  241. __func__, buf->num_pages);
  242. dma_unmap_sgtable(buf->dev, sgt, buf->dma_dir, DMA_ATTR_SKIP_CPU_SYNC);
  243. if (buf->vaddr)
  244. vm_unmap_ram(buf->vaddr, buf->num_pages);
  245. sg_free_table(buf->dma_sgt);
  246. if (buf->dma_dir == DMA_FROM_DEVICE ||
  247. buf->dma_dir == DMA_BIDIRECTIONAL)
  248. while (--i >= 0)
  249. set_page_dirty_lock(buf->pages[i]);
  250. vb2_destroy_framevec(buf->vec);
  251. kfree(buf);
  252. }
  253. static void *vb2_dma_sg_vaddr(struct vb2_buffer *vb, void *buf_priv)
  254. {
  255. struct vb2_dma_sg_buf *buf = buf_priv;
  256. struct iosys_map map;
  257. int ret;
  258. BUG_ON(!buf);
  259. if (!buf->vaddr) {
  260. if (buf->db_attach) {
  261. ret = dma_buf_vmap(buf->db_attach->dmabuf, &map);
  262. buf->vaddr = ret ? NULL : map.vaddr;
  263. } else {
  264. buf->vaddr = vm_map_ram(buf->pages, buf->num_pages, -1);
  265. }
  266. }
  267. /* add offset in case userptr is not page-aligned */
  268. return buf->vaddr ? buf->vaddr + buf->offset : NULL;
  269. }
  270. static unsigned int vb2_dma_sg_num_users(void *buf_priv)
  271. {
  272. struct vb2_dma_sg_buf *buf = buf_priv;
  273. return refcount_read(&buf->refcount);
  274. }
  275. static int vb2_dma_sg_mmap(void *buf_priv, struct vm_area_struct *vma)
  276. {
  277. struct vb2_dma_sg_buf *buf = buf_priv;
  278. int err;
  279. if (!buf) {
  280. printk(KERN_ERR "No memory to map\n");
  281. return -EINVAL;
  282. }
  283. err = vm_map_pages(vma, buf->pages, buf->num_pages);
  284. if (err) {
  285. printk(KERN_ERR "Remapping memory, error: %d\n", err);
  286. return err;
  287. }
  288. /*
  289. * Use common vm_area operations to track buffer refcount.
  290. */
  291. vma->vm_private_data = &buf->handler;
  292. vma->vm_ops = &vb2_common_vm_ops;
  293. vma->vm_ops->open(vma);
  294. return 0;
  295. }
  296. /*********************************************/
  297. /* DMABUF ops for exporters */
  298. /*********************************************/
  299. struct vb2_dma_sg_attachment {
  300. struct sg_table sgt;
  301. enum dma_data_direction dma_dir;
  302. };
  303. static int vb2_dma_sg_dmabuf_ops_attach(struct dma_buf *dbuf,
  304. struct dma_buf_attachment *dbuf_attach)
  305. {
  306. struct vb2_dma_sg_attachment *attach;
  307. unsigned int i;
  308. struct scatterlist *rd, *wr;
  309. struct sg_table *sgt;
  310. struct vb2_dma_sg_buf *buf = dbuf->priv;
  311. int ret;
  312. attach = kzalloc(sizeof(*attach), GFP_KERNEL);
  313. if (!attach)
  314. return -ENOMEM;
  315. sgt = &attach->sgt;
  316. /* Copy the buf->base_sgt scatter list to the attachment, as we can't
  317. * map the same scatter list to multiple attachments at the same time.
  318. */
  319. ret = sg_alloc_table(sgt, buf->dma_sgt->orig_nents, GFP_KERNEL);
  320. if (ret) {
  321. kfree(attach);
  322. return -ENOMEM;
  323. }
  324. rd = buf->dma_sgt->sgl;
  325. wr = sgt->sgl;
  326. for (i = 0; i < sgt->orig_nents; ++i) {
  327. sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
  328. rd = sg_next(rd);
  329. wr = sg_next(wr);
  330. }
  331. attach->dma_dir = DMA_NONE;
  332. dbuf_attach->priv = attach;
  333. return 0;
  334. }
  335. static void vb2_dma_sg_dmabuf_ops_detach(struct dma_buf *dbuf,
  336. struct dma_buf_attachment *db_attach)
  337. {
  338. struct vb2_dma_sg_attachment *attach = db_attach->priv;
  339. struct sg_table *sgt;
  340. if (!attach)
  341. return;
  342. sgt = &attach->sgt;
  343. /* release the scatterlist cache */
  344. if (attach->dma_dir != DMA_NONE)
  345. dma_unmap_sgtable(db_attach->dev, sgt, attach->dma_dir, 0);
  346. sg_free_table(sgt);
  347. kfree(attach);
  348. db_attach->priv = NULL;
  349. }
  350. static struct sg_table *vb2_dma_sg_dmabuf_ops_map(
  351. struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir)
  352. {
  353. struct vb2_dma_sg_attachment *attach = db_attach->priv;
  354. /* stealing dmabuf mutex to serialize map/unmap operations */
  355. struct mutex *lock = &db_attach->dmabuf->lock;
  356. struct sg_table *sgt;
  357. mutex_lock(lock);
  358. sgt = &attach->sgt;
  359. /* return previously mapped sg table */
  360. if (attach->dma_dir == dma_dir) {
  361. mutex_unlock(lock);
  362. return sgt;
  363. }
  364. /* release any previous cache */
  365. if (attach->dma_dir != DMA_NONE) {
  366. dma_unmap_sgtable(db_attach->dev, sgt, attach->dma_dir, 0);
  367. attach->dma_dir = DMA_NONE;
  368. }
  369. /* mapping to the client with new direction */
  370. if (dma_map_sgtable(db_attach->dev, sgt, dma_dir, 0)) {
  371. pr_err("failed to map scatterlist\n");
  372. mutex_unlock(lock);
  373. return ERR_PTR(-EIO);
  374. }
  375. attach->dma_dir = dma_dir;
  376. mutex_unlock(lock);
  377. return sgt;
  378. }
  379. static void vb2_dma_sg_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
  380. struct sg_table *sgt, enum dma_data_direction dma_dir)
  381. {
  382. /* nothing to be done here */
  383. }
  384. static void vb2_dma_sg_dmabuf_ops_release(struct dma_buf *dbuf)
  385. {
  386. /* drop reference obtained in vb2_dma_sg_get_dmabuf */
  387. vb2_dma_sg_put(dbuf->priv);
  388. }
  389. static int
  390. vb2_dma_sg_dmabuf_ops_begin_cpu_access(struct dma_buf *dbuf,
  391. enum dma_data_direction direction)
  392. {
  393. struct vb2_dma_sg_buf *buf = dbuf->priv;
  394. struct sg_table *sgt = buf->dma_sgt;
  395. dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
  396. return 0;
  397. }
  398. static int
  399. vb2_dma_sg_dmabuf_ops_end_cpu_access(struct dma_buf *dbuf,
  400. enum dma_data_direction direction)
  401. {
  402. struct vb2_dma_sg_buf *buf = dbuf->priv;
  403. struct sg_table *sgt = buf->dma_sgt;
  404. dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
  405. return 0;
  406. }
  407. static int vb2_dma_sg_dmabuf_ops_vmap(struct dma_buf *dbuf,
  408. struct iosys_map *map)
  409. {
  410. struct vb2_dma_sg_buf *buf = dbuf->priv;
  411. iosys_map_set_vaddr(map, buf->vaddr);
  412. return 0;
  413. }
  414. static int vb2_dma_sg_dmabuf_ops_mmap(struct dma_buf *dbuf,
  415. struct vm_area_struct *vma)
  416. {
  417. return vb2_dma_sg_mmap(dbuf->priv, vma);
  418. }
  419. static const struct dma_buf_ops vb2_dma_sg_dmabuf_ops = {
  420. .attach = vb2_dma_sg_dmabuf_ops_attach,
  421. .detach = vb2_dma_sg_dmabuf_ops_detach,
  422. .map_dma_buf = vb2_dma_sg_dmabuf_ops_map,
  423. .unmap_dma_buf = vb2_dma_sg_dmabuf_ops_unmap,
  424. .begin_cpu_access = vb2_dma_sg_dmabuf_ops_begin_cpu_access,
  425. .end_cpu_access = vb2_dma_sg_dmabuf_ops_end_cpu_access,
  426. .vmap = vb2_dma_sg_dmabuf_ops_vmap,
  427. .mmap = vb2_dma_sg_dmabuf_ops_mmap,
  428. .release = vb2_dma_sg_dmabuf_ops_release,
  429. };
  430. static struct dma_buf *vb2_dma_sg_get_dmabuf(struct vb2_buffer *vb,
  431. void *buf_priv,
  432. unsigned long flags)
  433. {
  434. struct vb2_dma_sg_buf *buf = buf_priv;
  435. struct dma_buf *dbuf;
  436. DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
  437. exp_info.ops = &vb2_dma_sg_dmabuf_ops;
  438. exp_info.size = buf->size;
  439. exp_info.flags = flags;
  440. exp_info.priv = buf;
  441. if (WARN_ON(!buf->dma_sgt))
  442. return NULL;
  443. dbuf = dma_buf_export(&exp_info);
  444. if (IS_ERR(dbuf))
  445. return NULL;
  446. /* dmabuf keeps reference to vb2 buffer */
  447. refcount_inc(&buf->refcount);
  448. return dbuf;
  449. }
  450. /*********************************************/
  451. /* callbacks for DMABUF buffers */
  452. /*********************************************/
  453. static int vb2_dma_sg_map_dmabuf(void *mem_priv)
  454. {
  455. struct vb2_dma_sg_buf *buf = mem_priv;
  456. struct sg_table *sgt;
  457. if (WARN_ON(!buf->db_attach)) {
  458. pr_err("trying to pin a non attached buffer\n");
  459. return -EINVAL;
  460. }
  461. if (WARN_ON(buf->dma_sgt)) {
  462. pr_err("dmabuf buffer is already pinned\n");
  463. return 0;
  464. }
  465. /* get the associated scatterlist for this buffer */
  466. sgt = dma_buf_map_attachment(buf->db_attach, buf->dma_dir);
  467. if (IS_ERR(sgt)) {
  468. pr_err("Error getting dmabuf scatterlist\n");
  469. return -EINVAL;
  470. }
  471. buf->dma_sgt = sgt;
  472. buf->vaddr = NULL;
  473. return 0;
  474. }
  475. static void vb2_dma_sg_unmap_dmabuf(void *mem_priv)
  476. {
  477. struct vb2_dma_sg_buf *buf = mem_priv;
  478. struct sg_table *sgt = buf->dma_sgt;
  479. struct iosys_map map = IOSYS_MAP_INIT_VADDR(buf->vaddr);
  480. if (WARN_ON(!buf->db_attach)) {
  481. pr_err("trying to unpin a not attached buffer\n");
  482. return;
  483. }
  484. if (WARN_ON(!sgt)) {
  485. pr_err("dmabuf buffer is already unpinned\n");
  486. return;
  487. }
  488. if (buf->vaddr) {
  489. dma_buf_vunmap(buf->db_attach->dmabuf, &map);
  490. buf->vaddr = NULL;
  491. }
  492. dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
  493. buf->dma_sgt = NULL;
  494. }
  495. static void vb2_dma_sg_detach_dmabuf(void *mem_priv)
  496. {
  497. struct vb2_dma_sg_buf *buf = mem_priv;
  498. /* if vb2 works correctly you should never detach mapped buffer */
  499. if (WARN_ON(buf->dma_sgt))
  500. vb2_dma_sg_unmap_dmabuf(buf);
  501. /* detach this attachment */
  502. dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach);
  503. kfree(buf);
  504. }
  505. static void *vb2_dma_sg_attach_dmabuf(struct vb2_buffer *vb, struct device *dev,
  506. struct dma_buf *dbuf, unsigned long size)
  507. {
  508. struct vb2_dma_sg_buf *buf;
  509. struct dma_buf_attachment *dba;
  510. if (WARN_ON(!dev))
  511. return ERR_PTR(-EINVAL);
  512. if (dbuf->size < size)
  513. return ERR_PTR(-EFAULT);
  514. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  515. if (!buf)
  516. return ERR_PTR(-ENOMEM);
  517. buf->dev = dev;
  518. /* create attachment for the dmabuf with the user device */
  519. dba = dma_buf_attach(dbuf, buf->dev);
  520. if (IS_ERR(dba)) {
  521. pr_err("failed to attach dmabuf\n");
  522. kfree(buf);
  523. return dba;
  524. }
  525. buf->dma_dir = vb->vb2_queue->dma_dir;
  526. buf->size = size;
  527. buf->db_attach = dba;
  528. buf->vb = vb;
  529. return buf;
  530. }
  531. static void *vb2_dma_sg_cookie(struct vb2_buffer *vb, void *buf_priv)
  532. {
  533. struct vb2_dma_sg_buf *buf = buf_priv;
  534. return buf->dma_sgt;
  535. }
  536. const struct vb2_mem_ops vb2_dma_sg_memops = {
  537. .alloc = vb2_dma_sg_alloc,
  538. .put = vb2_dma_sg_put,
  539. .get_userptr = vb2_dma_sg_get_userptr,
  540. .put_userptr = vb2_dma_sg_put_userptr,
  541. .prepare = vb2_dma_sg_prepare,
  542. .finish = vb2_dma_sg_finish,
  543. .vaddr = vb2_dma_sg_vaddr,
  544. .mmap = vb2_dma_sg_mmap,
  545. .num_users = vb2_dma_sg_num_users,
  546. .get_dmabuf = vb2_dma_sg_get_dmabuf,
  547. .map_dmabuf = vb2_dma_sg_map_dmabuf,
  548. .unmap_dmabuf = vb2_dma_sg_unmap_dmabuf,
  549. .attach_dmabuf = vb2_dma_sg_attach_dmabuf,
  550. .detach_dmabuf = vb2_dma_sg_detach_dmabuf,
  551. .cookie = vb2_dma_sg_cookie,
  552. };
  553. EXPORT_SYMBOL_GPL(vb2_dma_sg_memops);
  554. MODULE_DESCRIPTION("dma scatter/gather memory handling routines for videobuf2");
  555. MODULE_AUTHOR("Andrzej Pietrasiewicz");
  556. MODULE_LICENSE("GPL");
  557. MODULE_IMPORT_NS(DMA_BUF);