mock_dmabuf.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright © 2016 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. */
  24. #include "mock_dmabuf.h"
  25. static struct sg_table *mock_map_dma_buf(struct dma_buf_attachment *attachment,
  26. enum dma_data_direction dir)
  27. {
  28. struct mock_dmabuf *mock = to_mock(attachment->dmabuf);
  29. struct sg_table *st;
  30. struct scatterlist *sg;
  31. int i, err;
  32. st = kmalloc(sizeof(*st), GFP_KERNEL);
  33. if (!st)
  34. return ERR_PTR(-ENOMEM);
  35. err = sg_alloc_table(st, mock->npages, GFP_KERNEL);
  36. if (err)
  37. goto err_free;
  38. sg = st->sgl;
  39. for (i = 0; i < mock->npages; i++) {
  40. sg_set_page(sg, mock->pages[i], PAGE_SIZE, 0);
  41. sg = sg_next(sg);
  42. }
  43. if (!dma_map_sg(attachment->dev, st->sgl, st->nents, dir)) {
  44. err = -ENOMEM;
  45. goto err_st;
  46. }
  47. return st;
  48. err_st:
  49. sg_free_table(st);
  50. err_free:
  51. kfree(st);
  52. return ERR_PTR(err);
  53. }
  54. static void mock_unmap_dma_buf(struct dma_buf_attachment *attachment,
  55. struct sg_table *st,
  56. enum dma_data_direction dir)
  57. {
  58. dma_unmap_sg(attachment->dev, st->sgl, st->nents, dir);
  59. sg_free_table(st);
  60. kfree(st);
  61. }
  62. static void mock_dmabuf_release(struct dma_buf *dma_buf)
  63. {
  64. struct mock_dmabuf *mock = to_mock(dma_buf);
  65. int i;
  66. for (i = 0; i < mock->npages; i++)
  67. put_page(mock->pages[i]);
  68. kfree(mock);
  69. }
  70. static void *mock_dmabuf_vmap(struct dma_buf *dma_buf)
  71. {
  72. struct mock_dmabuf *mock = to_mock(dma_buf);
  73. return vm_map_ram(mock->pages, mock->npages, 0, PAGE_KERNEL);
  74. }
  75. static void mock_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
  76. {
  77. struct mock_dmabuf *mock = to_mock(dma_buf);
  78. vm_unmap_ram(vaddr, mock->npages);
  79. }
  80. static void *mock_dmabuf_kmap_atomic(struct dma_buf *dma_buf, unsigned long page_num)
  81. {
  82. struct mock_dmabuf *mock = to_mock(dma_buf);
  83. return kmap_atomic(mock->pages[page_num]);
  84. }
  85. static void mock_dmabuf_kunmap_atomic(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
  86. {
  87. kunmap_atomic(addr);
  88. }
  89. static void *mock_dmabuf_kmap(struct dma_buf *dma_buf, unsigned long page_num)
  90. {
  91. struct mock_dmabuf *mock = to_mock(dma_buf);
  92. return kmap(mock->pages[page_num]);
  93. }
  94. static void mock_dmabuf_kunmap(struct dma_buf *dma_buf, unsigned long page_num, void *addr)
  95. {
  96. struct mock_dmabuf *mock = to_mock(dma_buf);
  97. return kunmap(mock->pages[page_num]);
  98. }
  99. static int mock_dmabuf_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma)
  100. {
  101. return -ENODEV;
  102. }
  103. static const struct dma_buf_ops mock_dmabuf_ops = {
  104. .map_dma_buf = mock_map_dma_buf,
  105. .unmap_dma_buf = mock_unmap_dma_buf,
  106. .release = mock_dmabuf_release,
  107. .map = mock_dmabuf_kmap,
  108. .map_atomic = mock_dmabuf_kmap_atomic,
  109. .unmap = mock_dmabuf_kunmap,
  110. .unmap_atomic = mock_dmabuf_kunmap_atomic,
  111. .mmap = mock_dmabuf_mmap,
  112. .vmap = mock_dmabuf_vmap,
  113. .vunmap = mock_dmabuf_vunmap,
  114. };
  115. static struct dma_buf *mock_dmabuf(int npages)
  116. {
  117. struct mock_dmabuf *mock;
  118. DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
  119. struct dma_buf *dmabuf;
  120. int i;
  121. mock = kmalloc(sizeof(*mock) + npages * sizeof(struct page *),
  122. GFP_KERNEL);
  123. if (!mock)
  124. return ERR_PTR(-ENOMEM);
  125. mock->npages = npages;
  126. for (i = 0; i < npages; i++) {
  127. mock->pages[i] = alloc_page(GFP_KERNEL);
  128. if (!mock->pages[i])
  129. goto err;
  130. }
  131. exp_info.ops = &mock_dmabuf_ops;
  132. exp_info.size = npages * PAGE_SIZE;
  133. exp_info.flags = O_CLOEXEC;
  134. exp_info.priv = mock;
  135. dmabuf = dma_buf_export(&exp_info);
  136. if (IS_ERR(dmabuf))
  137. goto err;
  138. return dmabuf;
  139. err:
  140. while (i--)
  141. put_page(mock->pages[i]);
  142. kfree(mock);
  143. return ERR_PTR(-ENOMEM);
  144. }