videobuf2-dma-sg.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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-v4l2.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->orig_nents,
  179. buf->dma_dir);
  180. }
  181. static void vb2_dma_sg_finish(void *buf_priv)
  182. {
  183. struct vb2_dma_sg_buf *buf = buf_priv;
  184. struct sg_table *sgt = buf->dma_sgt;
  185. /* DMABUF exporter will flush the cache for us */
  186. if (buf->db_attach)
  187. return;
  188. dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir);
  189. }
  190. static void *vb2_dma_sg_get_userptr(void *alloc_ctx, unsigned long vaddr,
  191. unsigned long size,
  192. enum dma_data_direction dma_dir)
  193. {
  194. struct vb2_dma_sg_conf *conf = alloc_ctx;
  195. struct vb2_dma_sg_buf *buf;
  196. struct sg_table *sgt;
  197. DEFINE_DMA_ATTRS(attrs);
  198. struct frame_vector *vec;
  199. dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
  200. buf = kzalloc(sizeof *buf, GFP_KERNEL);
  201. if (!buf)
  202. return NULL;
  203. buf->vaddr = NULL;
  204. buf->dev = conf->dev;
  205. buf->dma_dir = dma_dir;
  206. buf->offset = vaddr & ~PAGE_MASK;
  207. buf->size = size;
  208. buf->dma_sgt = &buf->sg_table;
  209. vec = vb2_create_framevec(vaddr, size, buf->dma_dir == DMA_FROM_DEVICE);
  210. if (IS_ERR(vec))
  211. goto userptr_fail_pfnvec;
  212. buf->vec = vec;
  213. buf->pages = frame_vector_pages(vec);
  214. if (IS_ERR(buf->pages))
  215. goto userptr_fail_sgtable;
  216. buf->num_pages = frame_vector_count(vec);
  217. if (sg_alloc_table_from_pages(buf->dma_sgt, buf->pages,
  218. buf->num_pages, buf->offset, size, 0))
  219. goto userptr_fail_sgtable;
  220. sgt = &buf->sg_table;
  221. /*
  222. * No need to sync to the device, this will happen later when the
  223. * prepare() memop is called.
  224. */
  225. sgt->nents = dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents,
  226. buf->dma_dir, &attrs);
  227. if (!sgt->nents)
  228. goto userptr_fail_map;
  229. return buf;
  230. userptr_fail_map:
  231. sg_free_table(&buf->sg_table);
  232. userptr_fail_sgtable:
  233. vb2_destroy_framevec(vec);
  234. userptr_fail_pfnvec:
  235. kfree(buf);
  236. return NULL;
  237. }
  238. /*
  239. * @put_userptr: inform the allocator that a USERPTR buffer will no longer
  240. * be used
  241. */
  242. static void vb2_dma_sg_put_userptr(void *buf_priv)
  243. {
  244. struct vb2_dma_sg_buf *buf = buf_priv;
  245. struct sg_table *sgt = &buf->sg_table;
  246. int i = buf->num_pages;
  247. DEFINE_DMA_ATTRS(attrs);
  248. dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
  249. dprintk(1, "%s: Releasing userspace buffer of %d pages\n",
  250. __func__, buf->num_pages);
  251. dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->orig_nents, buf->dma_dir,
  252. &attrs);
  253. if (buf->vaddr)
  254. vm_unmap_ram(buf->vaddr, buf->num_pages);
  255. sg_free_table(buf->dma_sgt);
  256. while (--i >= 0) {
  257. if (buf->dma_dir == DMA_FROM_DEVICE)
  258. set_page_dirty_lock(buf->pages[i]);
  259. }
  260. vb2_destroy_framevec(buf->vec);
  261. kfree(buf);
  262. }
  263. static void *vb2_dma_sg_vaddr(void *buf_priv)
  264. {
  265. struct vb2_dma_sg_buf *buf = buf_priv;
  266. BUG_ON(!buf);
  267. if (!buf->vaddr) {
  268. if (buf->db_attach)
  269. buf->vaddr = dma_buf_vmap(buf->db_attach->dmabuf);
  270. else
  271. buf->vaddr = vm_map_ram(buf->pages,
  272. buf->num_pages, -1, PAGE_KERNEL);
  273. }
  274. /* add offset in case userptr is not page-aligned */
  275. return buf->vaddr ? buf->vaddr + buf->offset : NULL;
  276. }
  277. static unsigned int vb2_dma_sg_num_users(void *buf_priv)
  278. {
  279. struct vb2_dma_sg_buf *buf = buf_priv;
  280. return atomic_read(&buf->refcount);
  281. }
  282. static int vb2_dma_sg_mmap(void *buf_priv, struct vm_area_struct *vma)
  283. {
  284. struct vb2_dma_sg_buf *buf = buf_priv;
  285. unsigned long uaddr = vma->vm_start;
  286. unsigned long usize = vma->vm_end - vma->vm_start;
  287. int i = 0;
  288. if (!buf) {
  289. printk(KERN_ERR "No memory to map\n");
  290. return -EINVAL;
  291. }
  292. do {
  293. int ret;
  294. ret = vm_insert_page(vma, uaddr, buf->pages[i++]);
  295. if (ret) {
  296. printk(KERN_ERR "Remapping memory, error: %d\n", ret);
  297. return ret;
  298. }
  299. uaddr += PAGE_SIZE;
  300. usize -= PAGE_SIZE;
  301. } while (usize > 0);
  302. /*
  303. * Use common vm_area operations to track buffer refcount.
  304. */
  305. vma->vm_private_data = &buf->handler;
  306. vma->vm_ops = &vb2_common_vm_ops;
  307. vma->vm_ops->open(vma);
  308. return 0;
  309. }
  310. /*********************************************/
  311. /* DMABUF ops for exporters */
  312. /*********************************************/
  313. struct vb2_dma_sg_attachment {
  314. struct sg_table sgt;
  315. enum dma_data_direction dma_dir;
  316. };
  317. static int vb2_dma_sg_dmabuf_ops_attach(struct dma_buf *dbuf, struct device *dev,
  318. struct dma_buf_attachment *dbuf_attach)
  319. {
  320. struct vb2_dma_sg_attachment *attach;
  321. unsigned int i;
  322. struct scatterlist *rd, *wr;
  323. struct sg_table *sgt;
  324. struct vb2_dma_sg_buf *buf = dbuf->priv;
  325. int ret;
  326. attach = kzalloc(sizeof(*attach), GFP_KERNEL);
  327. if (!attach)
  328. return -ENOMEM;
  329. sgt = &attach->sgt;
  330. /* Copy the buf->base_sgt scatter list to the attachment, as we can't
  331. * map the same scatter list to multiple attachments at the same time.
  332. */
  333. ret = sg_alloc_table(sgt, buf->dma_sgt->orig_nents, GFP_KERNEL);
  334. if (ret) {
  335. kfree(attach);
  336. return -ENOMEM;
  337. }
  338. rd = buf->dma_sgt->sgl;
  339. wr = sgt->sgl;
  340. for (i = 0; i < sgt->orig_nents; ++i) {
  341. sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
  342. rd = sg_next(rd);
  343. wr = sg_next(wr);
  344. }
  345. attach->dma_dir = DMA_NONE;
  346. dbuf_attach->priv = attach;
  347. return 0;
  348. }
  349. static void vb2_dma_sg_dmabuf_ops_detach(struct dma_buf *dbuf,
  350. struct dma_buf_attachment *db_attach)
  351. {
  352. struct vb2_dma_sg_attachment *attach = db_attach->priv;
  353. struct sg_table *sgt;
  354. if (!attach)
  355. return;
  356. sgt = &attach->sgt;
  357. /* release the scatterlist cache */
  358. if (attach->dma_dir != DMA_NONE)
  359. dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
  360. attach->dma_dir);
  361. sg_free_table(sgt);
  362. kfree(attach);
  363. db_attach->priv = NULL;
  364. }
  365. static struct sg_table *vb2_dma_sg_dmabuf_ops_map(
  366. struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir)
  367. {
  368. struct vb2_dma_sg_attachment *attach = db_attach->priv;
  369. /* stealing dmabuf mutex to serialize map/unmap operations */
  370. struct mutex *lock = &db_attach->dmabuf->lock;
  371. struct sg_table *sgt;
  372. mutex_lock(lock);
  373. sgt = &attach->sgt;
  374. /* return previously mapped sg table */
  375. if (attach->dma_dir == dma_dir) {
  376. mutex_unlock(lock);
  377. return sgt;
  378. }
  379. /* release any previous cache */
  380. if (attach->dma_dir != DMA_NONE) {
  381. dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
  382. attach->dma_dir);
  383. attach->dma_dir = DMA_NONE;
  384. }
  385. /* mapping to the client with new direction */
  386. sgt->nents = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
  387. dma_dir);
  388. if (!sgt->nents) {
  389. pr_err("failed to map scatterlist\n");
  390. mutex_unlock(lock);
  391. return ERR_PTR(-EIO);
  392. }
  393. attach->dma_dir = dma_dir;
  394. mutex_unlock(lock);
  395. return sgt;
  396. }
  397. static void vb2_dma_sg_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
  398. struct sg_table *sgt, enum dma_data_direction dma_dir)
  399. {
  400. /* nothing to be done here */
  401. }
  402. static void vb2_dma_sg_dmabuf_ops_release(struct dma_buf *dbuf)
  403. {
  404. /* drop reference obtained in vb2_dma_sg_get_dmabuf */
  405. vb2_dma_sg_put(dbuf->priv);
  406. }
  407. static void *vb2_dma_sg_dmabuf_ops_kmap(struct dma_buf *dbuf, unsigned long pgnum)
  408. {
  409. struct vb2_dma_sg_buf *buf = dbuf->priv;
  410. return buf->vaddr ? buf->vaddr + pgnum * PAGE_SIZE : NULL;
  411. }
  412. static void *vb2_dma_sg_dmabuf_ops_vmap(struct dma_buf *dbuf)
  413. {
  414. struct vb2_dma_sg_buf *buf = dbuf->priv;
  415. return vb2_dma_sg_vaddr(buf);
  416. }
  417. static int vb2_dma_sg_dmabuf_ops_mmap(struct dma_buf *dbuf,
  418. struct vm_area_struct *vma)
  419. {
  420. return vb2_dma_sg_mmap(dbuf->priv, vma);
  421. }
  422. static struct dma_buf_ops vb2_dma_sg_dmabuf_ops = {
  423. .attach = vb2_dma_sg_dmabuf_ops_attach,
  424. .detach = vb2_dma_sg_dmabuf_ops_detach,
  425. .map_dma_buf = vb2_dma_sg_dmabuf_ops_map,
  426. .unmap_dma_buf = vb2_dma_sg_dmabuf_ops_unmap,
  427. .kmap = vb2_dma_sg_dmabuf_ops_kmap,
  428. .kmap_atomic = vb2_dma_sg_dmabuf_ops_kmap,
  429. .vmap = vb2_dma_sg_dmabuf_ops_vmap,
  430. .mmap = vb2_dma_sg_dmabuf_ops_mmap,
  431. .release = vb2_dma_sg_dmabuf_ops_release,
  432. };
  433. static struct dma_buf *vb2_dma_sg_get_dmabuf(void *buf_priv, unsigned long flags)
  434. {
  435. struct vb2_dma_sg_buf *buf = buf_priv;
  436. struct dma_buf *dbuf;
  437. DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
  438. exp_info.ops = &vb2_dma_sg_dmabuf_ops;
  439. exp_info.size = buf->size;
  440. exp_info.flags = flags;
  441. exp_info.priv = buf;
  442. if (WARN_ON(!buf->dma_sgt))
  443. return NULL;
  444. dbuf = dma_buf_export(&exp_info);
  445. if (IS_ERR(dbuf))
  446. return NULL;
  447. /* dmabuf keeps reference to vb2 buffer */
  448. atomic_inc(&buf->refcount);
  449. return dbuf;
  450. }
  451. /*********************************************/
  452. /* callbacks for DMABUF buffers */
  453. /*********************************************/
  454. static int vb2_dma_sg_map_dmabuf(void *mem_priv)
  455. {
  456. struct vb2_dma_sg_buf *buf = mem_priv;
  457. struct sg_table *sgt;
  458. if (WARN_ON(!buf->db_attach)) {
  459. pr_err("trying to pin a non attached buffer\n");
  460. return -EINVAL;
  461. }
  462. if (WARN_ON(buf->dma_sgt)) {
  463. pr_err("dmabuf buffer is already pinned\n");
  464. return 0;
  465. }
  466. /* get the associated scatterlist for this buffer */
  467. sgt = dma_buf_map_attachment(buf->db_attach, buf->dma_dir);
  468. if (IS_ERR(sgt)) {
  469. pr_err("Error getting dmabuf scatterlist\n");
  470. return -EINVAL;
  471. }
  472. buf->dma_sgt = sgt;
  473. buf->vaddr = NULL;
  474. return 0;
  475. }
  476. static void vb2_dma_sg_unmap_dmabuf(void *mem_priv)
  477. {
  478. struct vb2_dma_sg_buf *buf = mem_priv;
  479. struct sg_table *sgt = buf->dma_sgt;
  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, buf->vaddr);
  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(void *alloc_ctx, struct dma_buf *dbuf,
  506. unsigned long size, enum dma_data_direction dma_dir)
  507. {
  508. struct vb2_dma_sg_conf *conf = alloc_ctx;
  509. struct vb2_dma_sg_buf *buf;
  510. struct dma_buf_attachment *dba;
  511. if (dbuf->size < size)
  512. return ERR_PTR(-EFAULT);
  513. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  514. if (!buf)
  515. return ERR_PTR(-ENOMEM);
  516. buf->dev = conf->dev;
  517. /* create attachment for the dmabuf with the user device */
  518. dba = dma_buf_attach(dbuf, buf->dev);
  519. if (IS_ERR(dba)) {
  520. pr_err("failed to attach dmabuf\n");
  521. kfree(buf);
  522. return dba;
  523. }
  524. buf->dma_dir = dma_dir;
  525. buf->size = size;
  526. buf->db_attach = dba;
  527. return buf;
  528. }
  529. static void *vb2_dma_sg_cookie(void *buf_priv)
  530. {
  531. struct vb2_dma_sg_buf *buf = buf_priv;
  532. return buf->dma_sgt;
  533. }
  534. const struct vb2_mem_ops vb2_dma_sg_memops = {
  535. .alloc = vb2_dma_sg_alloc,
  536. .put = vb2_dma_sg_put,
  537. .get_userptr = vb2_dma_sg_get_userptr,
  538. .put_userptr = vb2_dma_sg_put_userptr,
  539. .prepare = vb2_dma_sg_prepare,
  540. .finish = vb2_dma_sg_finish,
  541. .vaddr = vb2_dma_sg_vaddr,
  542. .mmap = vb2_dma_sg_mmap,
  543. .num_users = vb2_dma_sg_num_users,
  544. .get_dmabuf = vb2_dma_sg_get_dmabuf,
  545. .map_dmabuf = vb2_dma_sg_map_dmabuf,
  546. .unmap_dmabuf = vb2_dma_sg_unmap_dmabuf,
  547. .attach_dmabuf = vb2_dma_sg_attach_dmabuf,
  548. .detach_dmabuf = vb2_dma_sg_detach_dmabuf,
  549. .cookie = vb2_dma_sg_cookie,
  550. };
  551. EXPORT_SYMBOL_GPL(vb2_dma_sg_memops);
  552. void *vb2_dma_sg_init_ctx(struct device *dev)
  553. {
  554. struct vb2_dma_sg_conf *conf;
  555. conf = kzalloc(sizeof(*conf), GFP_KERNEL);
  556. if (!conf)
  557. return ERR_PTR(-ENOMEM);
  558. conf->dev = dev;
  559. return conf;
  560. }
  561. EXPORT_SYMBOL_GPL(vb2_dma_sg_init_ctx);
  562. void vb2_dma_sg_cleanup_ctx(void *alloc_ctx)
  563. {
  564. if (!IS_ERR_OR_NULL(alloc_ctx))
  565. kfree(alloc_ctx);
  566. }
  567. EXPORT_SYMBOL_GPL(vb2_dma_sg_cleanup_ctx);
  568. MODULE_DESCRIPTION("dma scatter/gather memory handling routines for videobuf2");
  569. MODULE_AUTHOR("Andrzej Pietrasiewicz");
  570. MODULE_LICENSE("GPL");