videobuf2-dma-contig.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/dma-mapping.h>
  15. #include <media/videobuf2-core.h>
  16. #include <media/videobuf2-dma-contig.h>
  17. #include <media/videobuf2-memops.h>
  18. struct vb2_dc_conf {
  19. struct device *dev;
  20. };
  21. struct vb2_dc_buf {
  22. struct vb2_dc_conf *conf;
  23. void *vaddr;
  24. dma_addr_t dma_addr;
  25. unsigned long size;
  26. struct vm_area_struct *vma;
  27. atomic_t refcount;
  28. struct vb2_vmarea_handler handler;
  29. };
  30. static void vb2_dma_contig_put(void *buf_priv);
  31. static void *vb2_dma_contig_alloc(void *alloc_ctx, unsigned long size)
  32. {
  33. struct vb2_dc_conf *conf = alloc_ctx;
  34. struct vb2_dc_buf *buf;
  35. buf = kzalloc(sizeof *buf, GFP_KERNEL);
  36. if (!buf)
  37. return ERR_PTR(-ENOMEM);
  38. buf->vaddr = dma_alloc_coherent(conf->dev, size, &buf->dma_addr,
  39. GFP_KERNEL);
  40. if (!buf->vaddr) {
  41. dev_err(conf->dev, "dma_alloc_coherent of size %ld failed\n",
  42. size);
  43. kfree(buf);
  44. return ERR_PTR(-ENOMEM);
  45. }
  46. buf->conf = conf;
  47. buf->size = size;
  48. buf->handler.refcount = &buf->refcount;
  49. buf->handler.put = vb2_dma_contig_put;
  50. buf->handler.arg = buf;
  51. atomic_inc(&buf->refcount);
  52. return buf;
  53. }
  54. static void vb2_dma_contig_put(void *buf_priv)
  55. {
  56. struct vb2_dc_buf *buf = buf_priv;
  57. if (atomic_dec_and_test(&buf->refcount)) {
  58. dma_free_coherent(buf->conf->dev, buf->size, buf->vaddr,
  59. buf->dma_addr);
  60. kfree(buf);
  61. }
  62. }
  63. static void *vb2_dma_contig_cookie(void *buf_priv)
  64. {
  65. struct vb2_dc_buf *buf = buf_priv;
  66. return &buf->dma_addr;
  67. }
  68. static void *vb2_dma_contig_vaddr(void *buf_priv)
  69. {
  70. struct vb2_dc_buf *buf = buf_priv;
  71. if (!buf)
  72. return NULL;
  73. return buf->vaddr;
  74. }
  75. static unsigned int vb2_dma_contig_num_users(void *buf_priv)
  76. {
  77. struct vb2_dc_buf *buf = buf_priv;
  78. return atomic_read(&buf->refcount);
  79. }
  80. static int vb2_dma_contig_mmap(void *buf_priv, struct vm_area_struct *vma)
  81. {
  82. struct vb2_dc_buf *buf = buf_priv;
  83. if (!buf) {
  84. printk(KERN_ERR "No buffer to map\n");
  85. return -EINVAL;
  86. }
  87. return vb2_mmap_pfn_range(vma, buf->dma_addr, buf->size,
  88. &vb2_common_vm_ops, &buf->handler);
  89. }
  90. static void *vb2_dma_contig_get_userptr(void *alloc_ctx, unsigned long vaddr,
  91. unsigned long size, int write)
  92. {
  93. struct vb2_dc_buf *buf;
  94. struct vm_area_struct *vma;
  95. dma_addr_t dma_addr = 0;
  96. int ret;
  97. buf = kzalloc(sizeof *buf, GFP_KERNEL);
  98. if (!buf)
  99. return ERR_PTR(-ENOMEM);
  100. ret = vb2_get_contig_userptr(vaddr, size, &vma, &dma_addr);
  101. if (ret) {
  102. printk(KERN_ERR "Failed acquiring VMA for vaddr 0x%08lx\n",
  103. vaddr);
  104. kfree(buf);
  105. return ERR_PTR(ret);
  106. }
  107. buf->size = size;
  108. buf->dma_addr = dma_addr;
  109. buf->vma = vma;
  110. return buf;
  111. }
  112. static void vb2_dma_contig_put_userptr(void *mem_priv)
  113. {
  114. struct vb2_dc_buf *buf = mem_priv;
  115. if (!buf)
  116. return;
  117. vb2_put_vma(buf->vma);
  118. kfree(buf);
  119. }
  120. const struct vb2_mem_ops vb2_dma_contig_memops = {
  121. .alloc = vb2_dma_contig_alloc,
  122. .put = vb2_dma_contig_put,
  123. .cookie = vb2_dma_contig_cookie,
  124. .vaddr = vb2_dma_contig_vaddr,
  125. .mmap = vb2_dma_contig_mmap,
  126. .get_userptr = vb2_dma_contig_get_userptr,
  127. .put_userptr = vb2_dma_contig_put_userptr,
  128. .num_users = vb2_dma_contig_num_users,
  129. };
  130. EXPORT_SYMBOL_GPL(vb2_dma_contig_memops);
  131. void *vb2_dma_contig_init_ctx(struct device *dev)
  132. {
  133. struct vb2_dc_conf *conf;
  134. conf = kzalloc(sizeof *conf, GFP_KERNEL);
  135. if (!conf)
  136. return ERR_PTR(-ENOMEM);
  137. conf->dev = dev;
  138. return conf;
  139. }
  140. EXPORT_SYMBOL_GPL(vb2_dma_contig_init_ctx);
  141. void vb2_dma_contig_cleanup_ctx(void *alloc_ctx)
  142. {
  143. kfree(alloc_ctx);
  144. }
  145. EXPORT_SYMBOL_GPL(vb2_dma_contig_cleanup_ctx);
  146. MODULE_DESCRIPTION("DMA-contig memory handling routines for videobuf2");
  147. MODULE_AUTHOR("Pawel Osciak <pawel@osciak.com>");
  148. MODULE_LICENSE("GPL");