videobuf2-dma-sg.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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. int offset;
  36. enum dma_data_direction dma_dir;
  37. struct sg_table sg_table;
  38. /*
  39. * This will point to sg_table when used with the MMAP or USERPTR
  40. * memory model, and to the dma_buf sglist when used with the
  41. * DMABUF memory model.
  42. */
  43. struct sg_table *dma_sgt;
  44. size_t size;
  45. unsigned int num_pages;
  46. atomic_t refcount;
  47. struct vb2_vmarea_handler handler;
  48. struct vm_area_struct *vma;
  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. if (dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->nents,
  126. buf->dma_dir, &attrs) == 0)
  127. goto fail_map;
  128. buf->handler.refcount = &buf->refcount;
  129. buf->handler.put = vb2_dma_sg_put;
  130. buf->handler.arg = buf;
  131. atomic_inc(&buf->refcount);
  132. dprintk(1, "%s: Allocated buffer of %d pages\n",
  133. __func__, buf->num_pages);
  134. return buf;
  135. fail_map:
  136. put_device(buf->dev);
  137. sg_free_table(buf->dma_sgt);
  138. fail_table_alloc:
  139. num_pages = buf->num_pages;
  140. while (num_pages--)
  141. __free_page(buf->pages[num_pages]);
  142. fail_pages_alloc:
  143. kfree(buf->pages);
  144. fail_pages_array_alloc:
  145. kfree(buf);
  146. return NULL;
  147. }
  148. static void vb2_dma_sg_put(void *buf_priv)
  149. {
  150. struct vb2_dma_sg_buf *buf = buf_priv;
  151. struct sg_table *sgt = &buf->sg_table;
  152. int i = buf->num_pages;
  153. if (atomic_dec_and_test(&buf->refcount)) {
  154. DEFINE_DMA_ATTRS(attrs);
  155. dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
  156. dprintk(1, "%s: Freeing buffer of %d pages\n", __func__,
  157. buf->num_pages);
  158. dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->nents,
  159. buf->dma_dir, &attrs);
  160. if (buf->vaddr)
  161. vm_unmap_ram(buf->vaddr, buf->num_pages);
  162. sg_free_table(buf->dma_sgt);
  163. while (--i >= 0)
  164. __free_page(buf->pages[i]);
  165. kfree(buf->pages);
  166. put_device(buf->dev);
  167. kfree(buf);
  168. }
  169. }
  170. static void vb2_dma_sg_prepare(void *buf_priv)
  171. {
  172. struct vb2_dma_sg_buf *buf = buf_priv;
  173. struct sg_table *sgt = buf->dma_sgt;
  174. /* DMABUF exporter will flush the cache for us */
  175. if (buf->db_attach)
  176. return;
  177. dma_sync_sg_for_device(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
  178. }
  179. static void vb2_dma_sg_finish(void *buf_priv)
  180. {
  181. struct vb2_dma_sg_buf *buf = buf_priv;
  182. struct sg_table *sgt = buf->dma_sgt;
  183. /* DMABUF exporter will flush the cache for us */
  184. if (buf->db_attach)
  185. return;
  186. dma_sync_sg_for_cpu(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir);
  187. }
  188. static inline int vma_is_io(struct vm_area_struct *vma)
  189. {
  190. return !!(vma->vm_flags & (VM_IO | VM_PFNMAP));
  191. }
  192. static void *vb2_dma_sg_get_userptr(void *alloc_ctx, unsigned long vaddr,
  193. unsigned long size,
  194. enum dma_data_direction dma_dir)
  195. {
  196. struct vb2_dma_sg_conf *conf = alloc_ctx;
  197. struct vb2_dma_sg_buf *buf;
  198. unsigned long first, last;
  199. int num_pages_from_user;
  200. struct vm_area_struct *vma;
  201. struct sg_table *sgt;
  202. DEFINE_DMA_ATTRS(attrs);
  203. dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
  204. buf = kzalloc(sizeof *buf, GFP_KERNEL);
  205. if (!buf)
  206. return NULL;
  207. buf->vaddr = NULL;
  208. buf->dev = conf->dev;
  209. buf->dma_dir = dma_dir;
  210. buf->offset = vaddr & ~PAGE_MASK;
  211. buf->size = size;
  212. buf->dma_sgt = &buf->sg_table;
  213. first = (vaddr & PAGE_MASK) >> PAGE_SHIFT;
  214. last = ((vaddr + size - 1) & PAGE_MASK) >> PAGE_SHIFT;
  215. buf->num_pages = last - first + 1;
  216. buf->pages = kzalloc(buf->num_pages * sizeof(struct page *),
  217. GFP_KERNEL);
  218. if (!buf->pages)
  219. goto userptr_fail_alloc_pages;
  220. vma = find_vma(current->mm, vaddr);
  221. if (!vma) {
  222. dprintk(1, "no vma for address %lu\n", vaddr);
  223. goto userptr_fail_find_vma;
  224. }
  225. if (vma->vm_end < vaddr + size) {
  226. dprintk(1, "vma at %lu is too small for %lu bytes\n",
  227. vaddr, size);
  228. goto userptr_fail_find_vma;
  229. }
  230. buf->vma = vb2_get_vma(vma);
  231. if (!buf->vma) {
  232. dprintk(1, "failed to copy vma\n");
  233. goto userptr_fail_find_vma;
  234. }
  235. if (vma_is_io(buf->vma)) {
  236. for (num_pages_from_user = 0;
  237. num_pages_from_user < buf->num_pages;
  238. ++num_pages_from_user, vaddr += PAGE_SIZE) {
  239. unsigned long pfn;
  240. if (follow_pfn(vma, vaddr, &pfn)) {
  241. dprintk(1, "no page for address %lu\n", vaddr);
  242. break;
  243. }
  244. buf->pages[num_pages_from_user] = pfn_to_page(pfn);
  245. }
  246. } else
  247. num_pages_from_user = get_user_pages(current, current->mm,
  248. vaddr & PAGE_MASK,
  249. buf->num_pages,
  250. buf->dma_dir == DMA_FROM_DEVICE,
  251. 1, /* force */
  252. buf->pages,
  253. NULL);
  254. if (num_pages_from_user != buf->num_pages)
  255. goto userptr_fail_get_user_pages;
  256. if (sg_alloc_table_from_pages(buf->dma_sgt, buf->pages,
  257. buf->num_pages, buf->offset, size, 0))
  258. goto userptr_fail_alloc_table_from_pages;
  259. sgt = &buf->sg_table;
  260. /*
  261. * No need to sync to the device, this will happen later when the
  262. * prepare() memop is called.
  263. */
  264. if (dma_map_sg_attrs(buf->dev, sgt->sgl, sgt->nents,
  265. buf->dma_dir, &attrs) == 0)
  266. goto userptr_fail_map;
  267. return buf;
  268. userptr_fail_map:
  269. sg_free_table(&buf->sg_table);
  270. userptr_fail_alloc_table_from_pages:
  271. userptr_fail_get_user_pages:
  272. dprintk(1, "get_user_pages requested/got: %d/%d]\n",
  273. buf->num_pages, num_pages_from_user);
  274. if (!vma_is_io(buf->vma))
  275. while (--num_pages_from_user >= 0)
  276. put_page(buf->pages[num_pages_from_user]);
  277. vb2_put_vma(buf->vma);
  278. userptr_fail_find_vma:
  279. kfree(buf->pages);
  280. userptr_fail_alloc_pages:
  281. kfree(buf);
  282. return NULL;
  283. }
  284. /*
  285. * @put_userptr: inform the allocator that a USERPTR buffer will no longer
  286. * be used
  287. */
  288. static void vb2_dma_sg_put_userptr(void *buf_priv)
  289. {
  290. struct vb2_dma_sg_buf *buf = buf_priv;
  291. struct sg_table *sgt = &buf->sg_table;
  292. int i = buf->num_pages;
  293. DEFINE_DMA_ATTRS(attrs);
  294. dma_set_attr(DMA_ATTR_SKIP_CPU_SYNC, &attrs);
  295. dprintk(1, "%s: Releasing userspace buffer of %d pages\n",
  296. __func__, buf->num_pages);
  297. dma_unmap_sg_attrs(buf->dev, sgt->sgl, sgt->nents, buf->dma_dir, &attrs);
  298. if (buf->vaddr)
  299. vm_unmap_ram(buf->vaddr, buf->num_pages);
  300. sg_free_table(buf->dma_sgt);
  301. while (--i >= 0) {
  302. if (buf->dma_dir == DMA_FROM_DEVICE)
  303. set_page_dirty_lock(buf->pages[i]);
  304. if (!vma_is_io(buf->vma))
  305. put_page(buf->pages[i]);
  306. }
  307. kfree(buf->pages);
  308. vb2_put_vma(buf->vma);
  309. kfree(buf);
  310. }
  311. static void *vb2_dma_sg_vaddr(void *buf_priv)
  312. {
  313. struct vb2_dma_sg_buf *buf = buf_priv;
  314. BUG_ON(!buf);
  315. if (!buf->vaddr) {
  316. if (buf->db_attach)
  317. buf->vaddr = dma_buf_vmap(buf->db_attach->dmabuf);
  318. else
  319. buf->vaddr = vm_map_ram(buf->pages,
  320. buf->num_pages, -1, PAGE_KERNEL);
  321. }
  322. /* add offset in case userptr is not page-aligned */
  323. return buf->vaddr ? buf->vaddr + buf->offset : NULL;
  324. }
  325. static unsigned int vb2_dma_sg_num_users(void *buf_priv)
  326. {
  327. struct vb2_dma_sg_buf *buf = buf_priv;
  328. return atomic_read(&buf->refcount);
  329. }
  330. static int vb2_dma_sg_mmap(void *buf_priv, struct vm_area_struct *vma)
  331. {
  332. struct vb2_dma_sg_buf *buf = buf_priv;
  333. unsigned long uaddr = vma->vm_start;
  334. unsigned long usize = vma->vm_end - vma->vm_start;
  335. int i = 0;
  336. if (!buf) {
  337. printk(KERN_ERR "No memory to map\n");
  338. return -EINVAL;
  339. }
  340. do {
  341. int ret;
  342. ret = vm_insert_page(vma, uaddr, buf->pages[i++]);
  343. if (ret) {
  344. printk(KERN_ERR "Remapping memory, error: %d\n", ret);
  345. return ret;
  346. }
  347. uaddr += PAGE_SIZE;
  348. usize -= PAGE_SIZE;
  349. } while (usize > 0);
  350. /*
  351. * Use common vm_area operations to track buffer refcount.
  352. */
  353. vma->vm_private_data = &buf->handler;
  354. vma->vm_ops = &vb2_common_vm_ops;
  355. vma->vm_ops->open(vma);
  356. return 0;
  357. }
  358. /*********************************************/
  359. /* DMABUF ops for exporters */
  360. /*********************************************/
  361. struct vb2_dma_sg_attachment {
  362. struct sg_table sgt;
  363. enum dma_data_direction dma_dir;
  364. };
  365. static int vb2_dma_sg_dmabuf_ops_attach(struct dma_buf *dbuf, struct device *dev,
  366. struct dma_buf_attachment *dbuf_attach)
  367. {
  368. struct vb2_dma_sg_attachment *attach;
  369. unsigned int i;
  370. struct scatterlist *rd, *wr;
  371. struct sg_table *sgt;
  372. struct vb2_dma_sg_buf *buf = dbuf->priv;
  373. int ret;
  374. attach = kzalloc(sizeof(*attach), GFP_KERNEL);
  375. if (!attach)
  376. return -ENOMEM;
  377. sgt = &attach->sgt;
  378. /* Copy the buf->base_sgt scatter list to the attachment, as we can't
  379. * map the same scatter list to multiple attachments at the same time.
  380. */
  381. ret = sg_alloc_table(sgt, buf->dma_sgt->orig_nents, GFP_KERNEL);
  382. if (ret) {
  383. kfree(attach);
  384. return -ENOMEM;
  385. }
  386. rd = buf->dma_sgt->sgl;
  387. wr = sgt->sgl;
  388. for (i = 0; i < sgt->orig_nents; ++i) {
  389. sg_set_page(wr, sg_page(rd), rd->length, rd->offset);
  390. rd = sg_next(rd);
  391. wr = sg_next(wr);
  392. }
  393. attach->dma_dir = DMA_NONE;
  394. dbuf_attach->priv = attach;
  395. return 0;
  396. }
  397. static void vb2_dma_sg_dmabuf_ops_detach(struct dma_buf *dbuf,
  398. struct dma_buf_attachment *db_attach)
  399. {
  400. struct vb2_dma_sg_attachment *attach = db_attach->priv;
  401. struct sg_table *sgt;
  402. if (!attach)
  403. return;
  404. sgt = &attach->sgt;
  405. /* release the scatterlist cache */
  406. if (attach->dma_dir != DMA_NONE)
  407. dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
  408. attach->dma_dir);
  409. sg_free_table(sgt);
  410. kfree(attach);
  411. db_attach->priv = NULL;
  412. }
  413. static struct sg_table *vb2_dma_sg_dmabuf_ops_map(
  414. struct dma_buf_attachment *db_attach, enum dma_data_direction dma_dir)
  415. {
  416. struct vb2_dma_sg_attachment *attach = db_attach->priv;
  417. /* stealing dmabuf mutex to serialize map/unmap operations */
  418. struct mutex *lock = &db_attach->dmabuf->lock;
  419. struct sg_table *sgt;
  420. int ret;
  421. mutex_lock(lock);
  422. sgt = &attach->sgt;
  423. /* return previously mapped sg table */
  424. if (attach->dma_dir == dma_dir) {
  425. mutex_unlock(lock);
  426. return sgt;
  427. }
  428. /* release any previous cache */
  429. if (attach->dma_dir != DMA_NONE) {
  430. dma_unmap_sg(db_attach->dev, sgt->sgl, sgt->orig_nents,
  431. attach->dma_dir);
  432. attach->dma_dir = DMA_NONE;
  433. }
  434. /* mapping to the client with new direction */
  435. ret = dma_map_sg(db_attach->dev, sgt->sgl, sgt->orig_nents, dma_dir);
  436. if (ret <= 0) {
  437. pr_err("failed to map scatterlist\n");
  438. mutex_unlock(lock);
  439. return ERR_PTR(-EIO);
  440. }
  441. attach->dma_dir = dma_dir;
  442. mutex_unlock(lock);
  443. return sgt;
  444. }
  445. static void vb2_dma_sg_dmabuf_ops_unmap(struct dma_buf_attachment *db_attach,
  446. struct sg_table *sgt, enum dma_data_direction dma_dir)
  447. {
  448. /* nothing to be done here */
  449. }
  450. static void vb2_dma_sg_dmabuf_ops_release(struct dma_buf *dbuf)
  451. {
  452. /* drop reference obtained in vb2_dma_sg_get_dmabuf */
  453. vb2_dma_sg_put(dbuf->priv);
  454. }
  455. static void *vb2_dma_sg_dmabuf_ops_kmap(struct dma_buf *dbuf, unsigned long pgnum)
  456. {
  457. struct vb2_dma_sg_buf *buf = dbuf->priv;
  458. return buf->vaddr ? buf->vaddr + pgnum * PAGE_SIZE : NULL;
  459. }
  460. static void *vb2_dma_sg_dmabuf_ops_vmap(struct dma_buf *dbuf)
  461. {
  462. struct vb2_dma_sg_buf *buf = dbuf->priv;
  463. return vb2_dma_sg_vaddr(buf);
  464. }
  465. static int vb2_dma_sg_dmabuf_ops_mmap(struct dma_buf *dbuf,
  466. struct vm_area_struct *vma)
  467. {
  468. return vb2_dma_sg_mmap(dbuf->priv, vma);
  469. }
  470. static struct dma_buf_ops vb2_dma_sg_dmabuf_ops = {
  471. .attach = vb2_dma_sg_dmabuf_ops_attach,
  472. .detach = vb2_dma_sg_dmabuf_ops_detach,
  473. .map_dma_buf = vb2_dma_sg_dmabuf_ops_map,
  474. .unmap_dma_buf = vb2_dma_sg_dmabuf_ops_unmap,
  475. .kmap = vb2_dma_sg_dmabuf_ops_kmap,
  476. .kmap_atomic = vb2_dma_sg_dmabuf_ops_kmap,
  477. .vmap = vb2_dma_sg_dmabuf_ops_vmap,
  478. .mmap = vb2_dma_sg_dmabuf_ops_mmap,
  479. .release = vb2_dma_sg_dmabuf_ops_release,
  480. };
  481. static struct dma_buf *vb2_dma_sg_get_dmabuf(void *buf_priv, unsigned long flags)
  482. {
  483. struct vb2_dma_sg_buf *buf = buf_priv;
  484. struct dma_buf *dbuf;
  485. DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
  486. exp_info.ops = &vb2_dma_sg_dmabuf_ops;
  487. exp_info.size = buf->size;
  488. exp_info.flags = flags;
  489. exp_info.priv = buf;
  490. if (WARN_ON(!buf->dma_sgt))
  491. return NULL;
  492. dbuf = dma_buf_export(&exp_info);
  493. if (IS_ERR(dbuf))
  494. return NULL;
  495. /* dmabuf keeps reference to vb2 buffer */
  496. atomic_inc(&buf->refcount);
  497. return dbuf;
  498. }
  499. /*********************************************/
  500. /* callbacks for DMABUF buffers */
  501. /*********************************************/
  502. static int vb2_dma_sg_map_dmabuf(void *mem_priv)
  503. {
  504. struct vb2_dma_sg_buf *buf = mem_priv;
  505. struct sg_table *sgt;
  506. if (WARN_ON(!buf->db_attach)) {
  507. pr_err("trying to pin a non attached buffer\n");
  508. return -EINVAL;
  509. }
  510. if (WARN_ON(buf->dma_sgt)) {
  511. pr_err("dmabuf buffer is already pinned\n");
  512. return 0;
  513. }
  514. /* get the associated scatterlist for this buffer */
  515. sgt = dma_buf_map_attachment(buf->db_attach, buf->dma_dir);
  516. if (IS_ERR(sgt)) {
  517. pr_err("Error getting dmabuf scatterlist\n");
  518. return -EINVAL;
  519. }
  520. buf->dma_sgt = sgt;
  521. buf->vaddr = NULL;
  522. return 0;
  523. }
  524. static void vb2_dma_sg_unmap_dmabuf(void *mem_priv)
  525. {
  526. struct vb2_dma_sg_buf *buf = mem_priv;
  527. struct sg_table *sgt = buf->dma_sgt;
  528. if (WARN_ON(!buf->db_attach)) {
  529. pr_err("trying to unpin a not attached buffer\n");
  530. return;
  531. }
  532. if (WARN_ON(!sgt)) {
  533. pr_err("dmabuf buffer is already unpinned\n");
  534. return;
  535. }
  536. if (buf->vaddr) {
  537. dma_buf_vunmap(buf->db_attach->dmabuf, buf->vaddr);
  538. buf->vaddr = NULL;
  539. }
  540. dma_buf_unmap_attachment(buf->db_attach, sgt, buf->dma_dir);
  541. buf->dma_sgt = NULL;
  542. }
  543. static void vb2_dma_sg_detach_dmabuf(void *mem_priv)
  544. {
  545. struct vb2_dma_sg_buf *buf = mem_priv;
  546. /* if vb2 works correctly you should never detach mapped buffer */
  547. if (WARN_ON(buf->dma_sgt))
  548. vb2_dma_sg_unmap_dmabuf(buf);
  549. /* detach this attachment */
  550. dma_buf_detach(buf->db_attach->dmabuf, buf->db_attach);
  551. kfree(buf);
  552. }
  553. static void *vb2_dma_sg_attach_dmabuf(void *alloc_ctx, struct dma_buf *dbuf,
  554. unsigned long size, enum dma_data_direction dma_dir)
  555. {
  556. struct vb2_dma_sg_conf *conf = alloc_ctx;
  557. struct vb2_dma_sg_buf *buf;
  558. struct dma_buf_attachment *dba;
  559. if (dbuf->size < size)
  560. return ERR_PTR(-EFAULT);
  561. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  562. if (!buf)
  563. return ERR_PTR(-ENOMEM);
  564. buf->dev = conf->dev;
  565. /* create attachment for the dmabuf with the user device */
  566. dba = dma_buf_attach(dbuf, buf->dev);
  567. if (IS_ERR(dba)) {
  568. pr_err("failed to attach dmabuf\n");
  569. kfree(buf);
  570. return dba;
  571. }
  572. buf->dma_dir = dma_dir;
  573. buf->size = size;
  574. buf->db_attach = dba;
  575. return buf;
  576. }
  577. static void *vb2_dma_sg_cookie(void *buf_priv)
  578. {
  579. struct vb2_dma_sg_buf *buf = buf_priv;
  580. return buf->dma_sgt;
  581. }
  582. const struct vb2_mem_ops vb2_dma_sg_memops = {
  583. .alloc = vb2_dma_sg_alloc,
  584. .put = vb2_dma_sg_put,
  585. .get_userptr = vb2_dma_sg_get_userptr,
  586. .put_userptr = vb2_dma_sg_put_userptr,
  587. .prepare = vb2_dma_sg_prepare,
  588. .finish = vb2_dma_sg_finish,
  589. .vaddr = vb2_dma_sg_vaddr,
  590. .mmap = vb2_dma_sg_mmap,
  591. .num_users = vb2_dma_sg_num_users,
  592. .get_dmabuf = vb2_dma_sg_get_dmabuf,
  593. .map_dmabuf = vb2_dma_sg_map_dmabuf,
  594. .unmap_dmabuf = vb2_dma_sg_unmap_dmabuf,
  595. .attach_dmabuf = vb2_dma_sg_attach_dmabuf,
  596. .detach_dmabuf = vb2_dma_sg_detach_dmabuf,
  597. .cookie = vb2_dma_sg_cookie,
  598. };
  599. EXPORT_SYMBOL_GPL(vb2_dma_sg_memops);
  600. void *vb2_dma_sg_init_ctx(struct device *dev)
  601. {
  602. struct vb2_dma_sg_conf *conf;
  603. conf = kzalloc(sizeof(*conf), GFP_KERNEL);
  604. if (!conf)
  605. return ERR_PTR(-ENOMEM);
  606. conf->dev = dev;
  607. return conf;
  608. }
  609. EXPORT_SYMBOL_GPL(vb2_dma_sg_init_ctx);
  610. void vb2_dma_sg_cleanup_ctx(void *alloc_ctx)
  611. {
  612. if (!IS_ERR_OR_NULL(alloc_ctx))
  613. kfree(alloc_ctx);
  614. }
  615. EXPORT_SYMBOL_GPL(vb2_dma_sg_cleanup_ctx);
  616. MODULE_DESCRIPTION("dma scatter/gather memory handling routines for videobuf2");
  617. MODULE_AUTHOR("Andrzej Pietrasiewicz");
  618. MODULE_LICENSE("GPL");