videobuf2-dma-sg.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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 <andrzej.p@samsung.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/module.h>
  13. #include <linux/mm.h>
  14. #include <linux/scatterlist.h>
  15. #include <linux/sched.h>
  16. #include <linux/slab.h>
  17. #include <linux/vmalloc.h>
  18. #include <media/videobuf2-core.h>
  19. #include <media/videobuf2-memops.h>
  20. #include <media/videobuf2-dma-sg.h>
  21. static int debug;
  22. module_param(debug, int, 0644);
  23. #define dprintk(level, fmt, arg...) \
  24. do { \
  25. if (debug >= level) \
  26. printk(KERN_DEBUG "vb2-dma-sg: " fmt, ## arg); \
  27. } while (0)
  28. struct vb2_dma_sg_conf {
  29. struct device *dev;
  30. };
  31. struct vb2_dma_sg_buf {
  32. struct device *dev;
  33. void *vaddr;
  34. struct page **pages;
  35. struct frame_vector *vec;
  36. int offset;
  37. enum dma_data_direction dma_dir;
  38. struct sg_table sg_table;
  39. /*
  40. * This will point to sg_table when used with the MMAP or USERPTR
  41. * memory model, and to the dma_buf sglist when used with the
  42. * DMABUF memory model.
  43. */
  44. struct sg_table *dma_sgt;
  45. size_t size;
  46. unsigned int num_pages;
  47. atomic_t refcount;
  48. struct vb2_vmarea_handler handler;
  49. struct dma_buf_attachment *db_attach;
  50. };
  51. static void vb2_dma_sg_put(void *buf_priv);
  52. static int vb2_dma_sg_alloc_compacted(struct vb2_dma_sg_buf *buf,
  53. gfp_t gfp_flags)
  54. {
  55. unsigned int last_page = 0;
  56. int size = buf->size;
  57. while (size > 0) {
  58. struct page *pages;
  59. int order;
  60. int i;
  61. order = get_order(size);
  62. /* Dont over allocate*/
  63. if ((PAGE_SIZE << order) > size)
  64. order--;
  65. pages = NULL;
  66. while (!pages) {
  67. pages = alloc_pages(GFP_KERNEL | __GFP_ZERO |
  68. __GFP_NOWARN | gfp_flags, order);
  69. if (pages)
  70. break;
  71. if (order == 0) {
  72. while (last_page--)
  73. __free_page(buf->pages[last_page]);
  74. return -ENOMEM;
  75. }
  76. order--;
  77. }
  78. split_page(pages, order);
  79. for (i = 0; i < (1 << order); i++)
  80. buf->pages[last_page++] = &pages[i];
  81. size -= PAGE_SIZE << order;
  82. }
  83. return 0;
  84. }
  85. static void *vb2_dma_sg_alloc(void *alloc_ctx, unsigned long size,
  86. enum dma_data_direction dma_dir, gfp_t gfp_flags)
  87. {
  88. struct vb2_dma_sg_conf *conf = alloc_ctx;
  89. struct vb2_dma_sg_buf *buf;
  90. struct sg_table *sgt;
  91. int ret;
  92. int num_pages;
  93. DEFINE_DMA_ATTRS(attrs);
  94. dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
  95. if (WARN_ON(alloc_ctx == NULL))
  96. return NULL;
  97. buf = kzalloc(sizeof *buf, GFP_KERNEL);
  98. if (!buf)
  99. return NULL;
  100. buf->vaddr = NULL;
  101. buf->dma_dir = dma_dir;
  102. buf->offset = 0;
  103. buf->size = size;
  104. /* size is already page aligned */
  105. buf->num_pages = size >> PAGE_SHIFT;
  106. buf->dma_sgt = &buf->sg_table;
  107. buf->pages = kzalloc(buf->num_pages * sizeof(struct page *),
  108. GFP_KERNEL);
  109. if (!buf->pages)
  110. goto fail_pages_array_alloc;
  111. ret = vb2_dma_sg_alloc_compacted(buf, 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(conf->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. sgt->nents = dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
  126. buf->dma_dir, &attrs);
  127. if (!sgt->nents)
  128. goto fail_map;
  129. buf->handler.refcount = &buf->refcount;
  130. buf->handler.put = vb2_dma_sg_put;
  131. buf->handler.arg = buf;
  132. atomic_inc(&buf->refcount);
  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. kfree(buf->pages);
  145. fail_pages_array_alloc:
  146. kfree(buf);
  147. return NULL;
  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 (atomic_dec_and_test(&buf->refcount)) {
  155. DEFINE_DMA_ATTRS(attrs);
  156. dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
  157. dprintk(1, "%s: Freeing buffer of %d pages\n", __func__,
  158. buf->num_pages);
  159. dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
  160. buf->dma_dir, &attrs);
  161. if (buf->vaddr)
  162. vm_unmap_ram(buf->vaddr, buf->num_pages);
  163. sg_free_table(buf->dma_sgt);
  164. while (--i >= 0)
  165. __free_page(buf->pages[i]);
  166. kfree(buf->pages);
  167. put_device(buf->dev);
  168. kfree(buf);
  169. }
  170. }
  171. static void vb2_dma_sg_prepare(void *buf_priv)
  172. {
  173. struct vb2_dma_sg_buf *buf = buf_priv;
  174. struct sg_table *sgt = buf->dma_sgt;
  175. /* DMABUF exporter will flush the cache for us */
  176. if (buf->db_attach)
  177. return;
  178. dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
  179. }
  180. static void vb2_dma_sg_finish(void *buf_priv)
  181. {
  182. struct vb2_dma_sg_buf *buf = buf_priv;
  183. struct sg_table *sgt = buf->dma_sgt;
  184. /* DMABUF exporter will flush the cache for us */
  185. if (buf->db_attach)
  186. return;
  187. dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
  188. }
  189. static void *vb2_dma_sg_get_userptr(void *alloc_ctx, unsigned long vaddr,
  190. unsigned long size,
  191. enum dma_data_direction dma_dir)
  192. {
  193. struct vb2_dma_sg_conf *conf = alloc_ctx;
  194. struct vb2_dma_sg_buf *buf;
  195. struct sg_table *sgt;
  196. DEFINE_DMA_ATTRS(attrs);
  197. struct frame_vector *vec;
  198. dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
  199. buf = kzalloc(sizeof *buf, GFP_KERNEL);
  200. if (!buf)
  201. return NULL;
  202. buf->vaddr = NULL;
  203. buf->dev = conf->dev;
  204. buf->dma_dir = dma_dir;
  205. buf->offset = vaddr & ~PAGE_MASK;
  206. buf->size = size;
  207. buf->dma_sgt = &buf->sg_table;
  208. vec = vb2_create_framevec(vaddr, size, buf->dma_dir == DMA_FROM_DEVICE);
  209. if (IS_ERR(vec))
  210. goto userptr_fail_pfnvec;
  211. buf->vec = vec;
  212. buf->pages = frame_vector_pages(vec);
  213. if (IS_ERR(buf->pages))
  214. goto userptr_fail_sgtable;
  215. buf->num_pages = frame_vector_count(vec);
  216. if (sg_alloc_table_from_pages(buf->dma_sgt, buf->pages,
  217. buf->num_pages, buf->offset, size, 0))
  218. goto userptr_fail_sgtable;
  219. sgt = &buf->sg_table;
  220. /*
  221. * No need to sync to the device, this will happen later when the
  222. * prepare() memop is called.
  223. */
  224. sgt->nents = dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
  225. buf->dma_dir, &attrs);
  226. if (!sgt->nents)
  227. goto userptr_fail_map;
  228. return buf;
  229. userptr_fail_map:
  230. sg_free_table(&buf->sg_table);
  231. userptr_fail_sgtable:
  232. vb2_destroy_framevec(vec);
  233. userptr_fail_pfnvec:
  234. kfree(buf);
  235. return NULL;
  236. }
  237. /*
  238. * @put_userptr: inform the allocator that a USERPTR buffer will no longer
  239. * be used
  240. */
  241. static void vb2_dma_sg_put_userptr(void *buf_priv)
  242. {
  243. struct vb2_dma_sg_buf *buf = buf_priv;
  244. struct sg_table *sgt = &buf->sg_table;
  245. int i = buf->num_pages;
  246. DEFINE_DMA_ATTRS(attrs);
  247. dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
  248. dprintk(1, "%s: Releasing userspace buffer of %d pages\n",
  249. __func__, buf->num_pages);
  250. dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir,
  251. &attrs);
  252. if (buf->vaddr)
  253. vm_unmap_ram(buf->vaddr, buf->num_pages);
  254. sg_free_table(buf->dma_sgt);
  255. while (--i >= 0) {
  256. if (buf->dma_dir == DMA_FROM_DEVICE)
  257. set_page_dirty_lock(buf->pages[i]);
  258. }
  259. vb2_destroy_framevec(buf->vec);
  260. kfree(buf);
  261. }
  262. static void *vb2_dma_sg_vaddr(void *buf_priv)
  263. {
  264. struct vb2_dma_sg_buf *buf = buf_priv;
  265. BUG_ON(!buf);
  266. if (!buf->vaddr) {
  267. if (buf->db_attach)
  268. buf->vaddr = dma_buf_vmap(buf->db_attach->dmabuf);
  269. else
  270. buf->vaddr = vm_map_ram(buf->pages,
  271. buf->num_pages, -1, PAGE_KERNEL);
  272. }
  273. /* add offset in case userptr is not page-aligned */
  274. return buf->vaddr ? buf->vaddr + buf->offset : NULL;
  275. }
  276. static unsigned int vb2_dma_sg_num_users(void *buf_priv)
  277. {
  278. struct vb2_dma_sg_buf *buf = buf_priv;
  279. return atomic_read(&buf->refcount);
  280. }
  281. static int vb2_dma_sg_mmap(void *buf_priv, struct vm_area_struct *vma)
  282. {
  283. struct vb2_dma_sg_buf *buf = buf_priv;
  284. unsigned long uaddr = vma->vm_start;
  285. unsigned long usize = vma->vm_end - vma->vm_start;
  286. int i = 0;
  287. if (!buf) {
  288. printk(KERN_ERR "No memory to map\n");
  289. return -EINVAL;
  290. }
  291. do {
  292. int ret;
  293. ret = vm_insert_page(vma, uaddr, buf->pages[i++]);
  294. if (ret) {
  295. printk(KERN_ERR "Remapping memory, error: %d\n", ret);
  296. return ret;
  297. }
  298. uaddr += PAGE_SIZE;
  299. usize -= PAGE_SIZE;
  300. } while (usize > 0);
  301. /*
  302. * Use common vm_area operations to track buffer refcount.
  303. */
  304. vma->vm_private_data = &buf->handler;
  305. vma->vm_ops = &vb2_common_vm_ops;
  306. vma->vm_ops->open(vma);
  307. return 0;
  308. }
  309. /*********************************************/
  310. /* DMABUF ops for exporters */
  311. /*********************************************/
  312. struct vb2_dma_sg_attachment {
  313. struct sg_table sgt;
  314. enum dma_data_direction dma_dir;
  315. };
  316. static int vb2_dma_sg_dmabuf_ops_attach(struct dma_buf *dbuf, struct device *dev,
  317. struct dma_buf_attachment *dbuf_attach)
  318. {
  319. struct vb2_dma_sg_attachment *attach;
  320. unsigned int i;
  321. struct scatterlist *rd, *wr;
  322. struct sg_table *sgt;
  323. struct vb2_dma_sg_buf *buf = dbuf->priv;
  324. int ret;
  325. attach = kzalloc(sizeof(*attach), GFP_KERNEL);
  326. if (!attach)
  327. return -ENOMEM;
  328. sgt = &attach->sgt;
  329. /* Copy the buf->base_sgt scatter list to the attachment, as we can't
  330. * map the same scatter list to multiple attachments at the same time.
  331. */
  332. ret = sg_alloc_table(sgt, buf->dma_sgt->orig_nents, GFP_KERNEL);
  333. if (ret) {
  334. kfree(attach);
  335. return -ENOMEM;
  336. }
  337. rd = buf->dma_sgt->sgl;
  338. wr = sgt->sgl;
  339. for (i = 0; i < sgt->orig_nents; ++i) {
  340. sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
  341. rd = sg_next(rd);
  342. wr = sg_next(wr);
  343. }
  344. attach->dma_dir = DMA_NONE;
  345. dbuf_attach->priv = attach;
  346. return 0;
  347. }
  348. static void vb2_dma_sg_dmabuf_ops_detach(struct dma_buf *dbuf,
  349. struct dma_buf_attachment *db_attach)
  350. {
  351. struct vb2_dma_sg_attachment *attach = db_attach->priv;
  352. struct sg_table *sgt;
  353. if (!attach)
  354. return;
  355. sgt = &attach->sgt;
  356. /* release the scatterlist cache */
  357. if (attach->dma_dir != DMA_NONE)
  358. dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
  359. attach->dma_dir);
  360. sg_free_table(sgt);
  361. kfree(attach);
  362. db_attach->priv = NULL;
  363. }
  364. static struct sg_table *vb2_dma_sg_dmabuf_ops_map(
  365. struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir)
  366. {
  367. struct vb2_dma_sg_attachment *attach = db_attach->priv;
  368. /* stealing dmabuf mutex to serialize map/unmap operations */
  369. struct mutex *lock = &db_attach->dmabuf->lock;
  370. struct sg_table *sgt;
  371. mutex_lock(lock);
  372. sgt = &attach->sgt;
  373. /* return previously mapped sg table */
  374. if (attach->dma_dir == dma_dir) {
  375. mutex_unlock(lock);
  376. return sgt;
  377. }
  378. /* release any previous cache */
  379. if (attach->dma_dir != DMA_NONE) {
  380. dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
  381. attach->dma_dir);
  382. attach->dma_dir = DMA_NONE;
  383. }
  384. /* mapping to the client with new direction */
  385. sgt->nents = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
  386. dma_dir);
  387. if (!sgt->nents) {
  388. pr_err("failed to map scatterlist\n");
  389. mutex_unlock(lock);
  390. return ERR_PTR(-EIO);
  391. }
  392. attach->dma_dir = dma_dir;
  393. mutex_unlock(lock);
  394. return sgt;
  395. }
  396. static void vb2_dma_sg_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
  397. struct sg_table *sgt, enum dma_data_direction dma_dir)
  398. {
  399. /* nothing to be done here */
  400. }
  401. static void vb2_dma_sg_dmabuf_ops_release(struct dma_buf *dbuf)
  402. {
  403. /* drop reference obtained in vb2_dma_sg_get_dmabuf */
  404. vb2_dma_sg_put(dbuf->priv);
  405. }
  406. static void *vb2_dma_sg_dmabuf_ops_kmap(struct dma_buf *dbuf, unsigned long pgnum)
  407. {
  408. struct vb2_dma_sg_buf *buf = dbuf->priv;
  409. return buf->vaddr ? buf->vaddr + pgnum * PAGE_SIZE : NULL;
  410. }
  411. static void *vb2_dma_sg_dmabuf_ops_vmap(struct dma_buf *dbuf)
  412. {
  413. struct vb2_dma_sg_buf *buf = dbuf->priv;
  414. return vb2_dma_sg_vaddr(buf);
  415. }
  416. static int vb2_dma_sg_dmabuf_ops_mmap(struct dma_buf *dbuf,
  417. struct vm_area_struct *vma)
  418. {
  419. return vb2_dma_sg_mmap(dbuf->priv, vma);
  420. }
  421. static struct dma_buf_ops vb2_dma_sg_dmabuf_ops = {
  422. .attach = vb2_dma_sg_dmabuf_ops_attach,
  423. .detach = vb2_dma_sg_dmabuf_ops_detach,
  424. .map_dma_buf = vb2_dma_sg_dmabuf_ops_map,
  425. .unmap_dma_buf = vb2_dma_sg_dmabuf_ops_unmap,
  426. .kmap = vb2_dma_sg_dmabuf_ops_kmap,
  427. .kmap_atomic = vb2_dma_sg_dmabuf_ops_kmap,
  428. .vmap = vb2_dma_sg_dmabuf_ops_vmap,
  429. .mmap = vb2_dma_sg_dmabuf_ops_mmap,
  430. .release = vb2_dma_sg_dmabuf_ops_release,
  431. };
  432. static struct dma_buf *vb2_dma_sg_get_dmabuf(void *buf_priv, 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. atomic_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. if (WARN_ON(!buf->db_attach)) {
  480. pr_err("trying to unpin a not attached buffer\n");
  481. return;
  482. }
  483. if (WARN_ON(!sgt)) {
  484. pr_err("dmabuf buffer is already unpinned\n");
  485. return;
  486. }
  487. if (buf->vaddr) {
  488. dma_buf_vunmap(buf->db_attach->dmabuf, buf->vaddr);
  489. buf->vaddr = NULL;
  490. }
  491. dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
  492. buf->dma_sgt = NULL;
  493. }
  494. static void vb2_dma_sg_detach_dmabuf(void *mem_priv)
  495. {
  496. struct vb2_dma_sg_buf *buf = mem_priv;
  497. /* if vb2 works correctly you should never detach mapped buffer */
  498. if (WARN_ON(buf->dma_sgt))
  499. vb2_dma_sg_unmap_dmabuf(buf);
  500. /* detach this attachment */
  501. dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach);
  502. kfree(buf);
  503. }
  504. static void *vb2_dma_sg_attach_dmabuf(void *alloc_ctx, struct dma_buf *dbuf,
  505. unsigned long size, enum dma_data_direction dma_dir)
  506. {
  507. struct vb2_dma_sg_conf *conf = alloc_ctx;
  508. struct vb2_dma_sg_buf *buf;
  509. struct dma_buf_attachment *dba;
  510. if (dbuf->size < size)
  511. return ERR_PTR(-EFAULT);
  512. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  513. if (!buf)
  514. return ERR_PTR(-ENOMEM);
  515. buf->dev = conf->dev;
  516. /* create attachment for the dmabuf with the user device */
  517. dba = dma_buf_attach(dbuf, buf->dev);
  518. if (IS_ERR(dba)) {
  519. pr_err("failed to attach dmabuf\n");
  520. kfree(buf);
  521. return dba;
  522. }
  523. buf->dma_dir = dma_dir;
  524. buf->size = size;
  525. buf->db_attach = dba;
  526. return buf;
  527. }
  528. static void *vb2_dma_sg_cookie(void *buf_priv)
  529. {
  530. struct vb2_dma_sg_buf *buf = buf_priv;
  531. return buf->dma_sgt;
  532. }
  533. const struct vb2_mem_ops vb2_dma_sg_memops = {
  534. .alloc = vb2_dma_sg_alloc,
  535. .put = vb2_dma_sg_put,
  536. .get_userptr = vb2_dma_sg_get_userptr,
  537. .put_userptr = vb2_dma_sg_put_userptr,
  538. .prepare = vb2_dma_sg_prepare,
  539. .finish = vb2_dma_sg_finish,
  540. .vaddr = vb2_dma_sg_vaddr,
  541. .mmap = vb2_dma_sg_mmap,
  542. .num_users = vb2_dma_sg_num_users,
  543. .get_dmabuf = vb2_dma_sg_get_dmabuf,
  544. .map_dmabuf = vb2_dma_sg_map_dmabuf,
  545. .unmap_dmabuf = vb2_dma_sg_unmap_dmabuf,
  546. .attach_dmabuf = vb2_dma_sg_attach_dmabuf,
  547. .detach_dmabuf = vb2_dma_sg_detach_dmabuf,
  548. .cookie = vb2_dma_sg_cookie,
  549. };
  550. EXPORT_SYMBOL_GPL(vb2_dma_sg_memops);
  551. void *vb2_dma_sg_init_ctx(struct device *dev)
  552. {
  553. struct vb2_dma_sg_conf *conf;
  554. conf = kzalloc(sizeof(*conf), GFP_KERNEL);
  555. if (!conf)
  556. return ERR_PTR(-ENOMEM);
  557. conf->dev = dev;
  558. return conf;
  559. }
  560. EXPORT_SYMBOL_GPL(vb2_dma_sg_init_ctx);
  561. void vb2_dma_sg_cleanup_ctx(void *alloc_ctx)
  562. {
  563. if (!IS_ERR_OR_NULL(alloc_ctx))
  564. kfree(alloc_ctx);
  565. }
  566. EXPORT_SYMBOL_GPL(vb2_dma_sg_cleanup_ctx);
  567. MODULE_DESCRIPTION("dma scatter/gather memory handling routines for videobuf2");
  568. MODULE_AUTHOR("Andrzej Pietrasiewicz");
  569. MODULE_LICENSE("GPL");