img_mem_ion.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*!
  2. *****************************************************************************
  3. *
  4. * @File img_mem_ion.c
  5. * ---------------------------------------------------------------------------
  6. *
  7. * Copyright (c) Imagination Technologies Ltd.
  8. *
  9. * The contents of this file are subject to the MIT license as set out below.
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a
  12. * copy of this software and associated documentation files (the "Software"),
  13. * to deal in the Software without restriction, including without limitation
  14. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  15. * and/or sell copies of the Software, and to permit persons to whom the
  16. * Software is furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. *
  29. * Alternatively, the contents of this file may be used under the terms of the
  30. * GNU General Public License Version 2 ("GPL")in which case the provisions of
  31. * GPL are applicable instead of those above.
  32. *
  33. * If you wish to allow use of your version of this file only under the terms
  34. * of GPL, and not to allow others to use your version of this file under the
  35. * terms of the MIT license, indicate your decision by deleting the provisions
  36. * above and replace them with the notice and other provisions required by GPL
  37. * as set out in the file called "GPLHEADER" included in this distribution. If
  38. * you do not delete the provisions above, a recipient may use your version of
  39. * this file under the terms of either the MIT license or GPL.
  40. *
  41. * This License is also included in this distribution in the file called
  42. * "MIT_COPYING".
  43. *
  44. *****************************************************************************/
  45. #include <linux/version.h>
  46. #include <linux/module.h>
  47. #include <linux/mm.h>
  48. #include <linux/slab.h>
  49. #include <linux/scatterlist.h>
  50. #include <linux/device.h>
  51. #include <ion.h>
  52. #include <img_mem_man.h>
  53. #include "img_mem_man_priv.h"
  54. static int trace_physical_pages;
  55. struct buffer_data {
  56. struct ion_client *client;
  57. struct ion_handle *handle;
  58. struct sg_table *sgt;
  59. };
  60. static int ion_heap_import(struct device *device, struct heap *heap,
  61. size_t size, enum img_mem_attr attr, uint64_t buf_hnd,
  62. struct buffer *buffer)
  63. {
  64. struct buffer_data *data;
  65. int ret;
  66. int buf_fd = (int)buf_hnd;
  67. pr_debug("%s:%d buffer %d (0x%p) buf_fd %d\n", __func__, __LINE__,
  68. buffer->id, buffer, buf_fd);
  69. data = kmalloc(sizeof(struct buffer_data), GFP_KERNEL);
  70. if (!data)
  71. return -ENOMEM;
  72. data->client = heap->priv;
  73. data->handle = ion_import_dma_buf(data->client, buf_fd);
  74. if (IS_ERR_OR_NULL(data->handle)) {
  75. pr_err("%s ion_import_dma_buf fd %d\n", __func__, buf_fd);
  76. ret = -EINVAL;
  77. goto ion_import_dma_buf_failed;
  78. }
  79. pr_debug("%s:%d buffer %d ion_handle %p\n", __func__, __LINE__,
  80. buffer->id, data->handle);
  81. data->sgt = ion_sg_table(data->client, data->handle);
  82. if (IS_ERR(data->sgt)) {
  83. pr_err("%s ion_sg_table fd %d\n", __func__, buf_fd);
  84. ret = -EINVAL;
  85. goto ion_sg_table_failed;
  86. }
  87. if (trace_physical_pages) {
  88. struct scatterlist *sgl = data->sgt->sgl;
  89. while (sgl) {
  90. pr_info("%s:%d phys %#llx length %d\n",
  91. __func__, __LINE__,
  92. (unsigned long long)sg_phys(sgl), sgl->length);
  93. sgl = sg_next(sgl);
  94. }
  95. }
  96. buffer->priv = data;
  97. return 0;
  98. ion_sg_table_failed:
  99. ion_free(data->client, data->handle);
  100. ion_import_dma_buf_failed:
  101. kfree(data);
  102. return ret;
  103. }
  104. static void ion_heap_free(struct heap *heap, struct buffer *buffer)
  105. {
  106. struct buffer_data *data = buffer->priv;
  107. pr_debug("%s:%d buffer %d (0x%p)\n", __func__, __LINE__,
  108. buffer->id, buffer);
  109. if (buffer->kptr)
  110. ion_unmap_kernel(data->client, data->handle);
  111. ion_free(data->client, data->handle);
  112. kfree(data);
  113. }
  114. static int ion_heap_map_um(struct heap *heap, struct buffer *buffer,
  115. struct vm_area_struct *vma)
  116. {
  117. struct buffer_data *data = buffer->priv;
  118. struct scatterlist *sgl;
  119. unsigned long addr;
  120. pr_debug("%s:%d buffer %d (0x%p)\n", __func__, __LINE__,
  121. buffer->id, buffer);
  122. pr_debug("%s:%d vm_start %#lx vm_end %#lx size %ld\n",
  123. __func__, __LINE__,
  124. vma->vm_start, vma->vm_end, vma->vm_end - vma->vm_start);
  125. vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
  126. sgl = data->sgt->sgl;
  127. addr = vma->vm_start;
  128. while (sgl) {
  129. dma_addr_t phys = sg_phys(sgl); /* sg_dma_address ? */
  130. unsigned long pfn = phys >> PAGE_SHIFT;
  131. unsigned int len = sgl->length;
  132. int ret;
  133. ret = remap_pfn_range(vma, addr, pfn, len, vma->vm_page_prot);
  134. if (ret)
  135. return ret;
  136. addr += len;
  137. sgl = sg_next(sgl);
  138. }
  139. return 0;
  140. }
  141. static int ion_heap_map_km(struct heap *heap, struct buffer *buffer)
  142. {
  143. struct buffer_data *data = buffer->priv;
  144. pr_debug("%s:%d buffer %d (0x%p)\n", __func__, __LINE__,
  145. buffer->id, buffer);
  146. if (buffer->kptr) {
  147. pr_warn("%s called for already mapped buffer %d\n",
  148. __func__, buffer->id);
  149. return 0;
  150. }
  151. buffer->kptr = ion_map_kernel(data->client, data->handle);
  152. if (!buffer->kptr) {
  153. pr_err("%s ion_map_kernel failed!\n", __func__);
  154. return -EFAULT;
  155. }
  156. pr_debug("%s:%d buffer %d map to 0x%p\n", __func__, __LINE__,
  157. buffer->id, buffer->kptr);
  158. return 0;
  159. }
  160. static int ion_heap_unmap_km(struct heap *heap, struct buffer *buffer)
  161. {
  162. struct buffer_data *data = buffer->priv;
  163. pr_debug("%s:%d buffer %d (0x%p)\n", __func__, __LINE__,
  164. buffer->id, buffer);
  165. if (!buffer->kptr) {
  166. pr_warn("%s called for unmapped buffer %d\n",
  167. __func__, buffer->id);
  168. return 0;
  169. }
  170. ion_unmap_kernel(data->client, data->handle);
  171. pr_debug("%s:%d buffer %d unmap from 0x%p\n", __func__, __LINE__,
  172. buffer->id, buffer->kptr);
  173. buffer->kptr = NULL;
  174. return 0;
  175. }
  176. static int ion_heap_get_sg_table(struct heap *heap, struct buffer *buffer,
  177. struct sg_table **sg_table, bool *use_sg_dma)
  178. {
  179. struct buffer_data *data = buffer->priv;
  180. *sg_table = data->sgt;
  181. *use_sg_dma = false;
  182. return 0;
  183. }
  184. static void ion_heap_destroy(struct heap *heap)
  185. {
  186. pr_debug("%s:%d\n", __func__, __LINE__);
  187. }
  188. static struct heap_ops ion_heap_ops = {
  189. .alloc = NULL,
  190. .import = ion_heap_import,
  191. .free = ion_heap_free,
  192. .map_um = ion_heap_map_um,
  193. .unmap_um = NULL,
  194. .map_km = ion_heap_map_km,
  195. .unmap_km = ion_heap_unmap_km,
  196. .get_sg_table = ion_heap_get_sg_table,
  197. .get_page_array = NULL,
  198. .sync_cpu_to_dev = NULL,
  199. .sync_dev_to_cpu = NULL,
  200. .set_offset = NULL,
  201. .destroy = ion_heap_destroy,
  202. };
  203. int img_mem_ion_init(const struct heap_config *heap_cfg, struct heap *heap)
  204. {
  205. pr_debug("%s:%d\n", __func__, __LINE__);
  206. if (!heap_cfg->options.ion.client) {
  207. pr_err("%s no ion client defined\n", __func__);
  208. return -EINVAL;
  209. }
  210. heap->ops = &ion_heap_ops;
  211. heap->priv = heap_cfg->options.ion.client;
  212. return 0;
  213. }
  214. /*
  215. * coding style for emacs
  216. *
  217. * Local variables:
  218. * indent-tabs-mode: t
  219. * tab-width: 8
  220. * c-basic-offset: 8
  221. * End:
  222. */