de_heap_ion.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * de_heap_ion.c
  3. */
  4. #include <linux/dma-buf.h>
  5. #include <linux/err.h>
  6. #include <linux/types.h>
  7. #include <linux/module.h>
  8. #include <linux/slab.h>
  9. #include <linux/uaccess.h>
  10. #include <linux/miscdevice.h>
  11. #include "de_heap.h"
  12. #include "de_heap_ion.h"
  13. #if ((LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0)) && (LINUX_VERSION_CODE < KERNEL_VERSION(4, 5, 0)))
  14. static struct ion_client *dmabuf_ion_client;
  15. int de_heap_buffer_create(size_t size, unsigned long align, void **private_data)
  16. {
  17. struct ion_handle *ion_handle;
  18. unsigned int heap_mask;
  19. unsigned int heap_flags;
  20. pr_info("%s:check %zu\n", __func__, size);
  21. pr_debug("%s:ion size %zu\n", __func__, size);
  22. heap_mask = de_heap_ion_get_heap_mask();
  23. pr_info("%s: heap mask = %x\n", __func__, heap_mask);
  24. heap_flags = de_heap_ion_get_heap_flags();
  25. pr_info("%s: heap flags = %x\n", __func__, heap_flags);
  26. pr_info("%s:ion_alloc dmabuf_ion_client = %lx, size = %i, , align = %i \n", __func__, (long unsigned int)dmabuf_ion_client, (int)size, (int)align);
  27. ion_handle = ion_alloc(dmabuf_ion_client, size, align,
  28. heap_mask, heap_flags);
  29. if (IS_ERR_OR_NULL(ion_handle)) {
  30. pr_err("%s:ion ion_alloc failed, ion_handle = %li\n", __func__, PTR_ERR(ion_handle));
  31. if (IS_ERR(ion_handle)) {
  32. return PTR_ERR(ion_handle);
  33. } else {
  34. return -ENOMEM;
  35. }
  36. } else {
  37. pr_info("%s:ion handle %p size %zu\n", __func__, ion_handle, size);
  38. }
  39. *private_data = ion_handle;
  40. return 0;
  41. }
  42. int de_heap_export_fd(void *private_data, unsigned long flags)
  43. {
  44. struct ion_handle *ion_handle = private_data;
  45. int ret;
  46. pr_debug("%s:ion\n", __func__);
  47. ret = ion_share_dma_buf_fd(dmabuf_ion_client, ion_handle);
  48. if (ret < 0) {
  49. pr_err("%s:ion ion_share_dma_buf_fd failed\n", __func__);
  50. return ret;
  51. }
  52. pr_info("%s:ion handle %p export fd %d\n", __func__, ion_handle, ret);
  53. return ret;
  54. }
  55. void de_heap_buffer_free(void *private_data)
  56. {
  57. struct ion_handle *ion_handle = private_data;
  58. pr_info("%s:ion handle %p\n", __func__, ion_handle);
  59. ion_free(dmabuf_ion_client, ion_handle);
  60. }
  61. int de_heap_heap_init(void)
  62. {
  63. pr_info("%s:ion\n", __func__);
  64. dmabuf_ion_client = de_heap_ion_create_ion_client();
  65. pr_err("%s:dmabuf_ion_client = %li \n", __func__, (long)dmabuf_ion_client);
  66. if (!dmabuf_ion_client) {
  67. pr_err("%s:ion failed to get an ion client %lx \n", __func__, (long)dmabuf_ion_client);
  68. return -EFAULT;
  69. }
  70. return 0;
  71. }
  72. void de_heap_heap_deinit(void)
  73. {
  74. pr_info("%s:ion\n", __func__);
  75. de_heap_ion_destroy_ion_client(dmabuf_ion_client);
  76. }
  77. #elif ((LINUX_VERSION_CODE >= KERNEL_VERSION(4, 14, 0)) && (LINUX_VERSION_CODE < KERNEL_VERSION(4, 16, 0)))
  78. int de_heap_heap_init(void)
  79. {
  80. pr_info("%s:ion\n", __func__);
  81. return 0;
  82. }
  83. void de_heap_heap_deinit(void)
  84. {
  85. pr_info("%s:ion\n", __func__);
  86. }
  87. int de_heap_export_fd(void *private_data, unsigned long flags)
  88. {
  89. pr_info("%s:\n", __func__);
  90. return 0;
  91. }
  92. #elif ((LINUX_VERSION_CODE >= KERNEL_VERSION(5, 4, 0)) && (LINUX_VERSION_CODE < KERNEL_VERSION(5, 5, 0)))
  93. static struct ion_client *dmabuf_ion_client;
  94. int de_heap_buffer_create(size_t size, unsigned long align, void **private_data)
  95. {
  96. struct ion_handle *ion_handle;
  97. unsigned int heap_mask;
  98. unsigned int heap_flags;
  99. pr_info("%s:check %zu\n", __func__, size);
  100. pr_debug("%s:ion size %zu\n", __func__, size);
  101. heap_mask = de_heap_ion_get_heap_mask();
  102. pr_info("%s: heap mask = %x\n", __func__, heap_mask);
  103. heap_flags = de_heap_ion_get_heap_flags();
  104. pr_info("%s: heap flags = %x\n", __func__, heap_flags);
  105. pr_info("%s:ion_alloc dmabuf_ion_client = %lx, size = %i, , align = %i \n", __func__, (long unsigned int)dmabuf_ion_client, (int)size, (int)align);
  106. ion_handle = ion_alloc(dmabuf_ion_client, size, align,
  107. heap_mask, heap_flags);
  108. if (IS_ERR_OR_NULL(ion_handle)) {
  109. pr_err("%s:ion ion_alloc failed, ion_handle = %li\n", __func__, PTR_ERR(ion_handle));
  110. if (IS_ERR(ion_handle)) {
  111. return PTR_ERR(ion_handle);
  112. } else {
  113. return -ENOMEM;
  114. }
  115. } else {
  116. pr_info("%s:ion handle %p size %zu\n", __func__, ion_handle, size);
  117. }
  118. *private_data = ion_handle;
  119. return 0;
  120. }
  121. int de_heap_export_fd(void *private_data, unsigned long flags)
  122. {
  123. struct ion_handle *ion_handle = private_data;
  124. int ret;
  125. pr_debug("%s:ion\n", __func__);
  126. ret = ion_share_dma_buf_fd(dmabuf_ion_client, ion_handle);
  127. if (ret < 0) {
  128. pr_err("%s:ion ion_share_dma_buf_fd failed\n", __func__);
  129. return ret;
  130. }
  131. pr_info("%s:ion handle %p export fd %d\n", __func__, ion_handle, ret);
  132. return ret;
  133. }
  134. void de_heap_buffer_free(void *private_data)
  135. {
  136. struct ion_handle *ion_handle = private_data;
  137. pr_info("%s:ion handle %p\n", __func__, ion_handle);
  138. ion_free(dmabuf_ion_client, ion_handle);
  139. }
  140. int de_heap_heap_init(void)
  141. {
  142. pr_info("%s:ion\n", __func__);
  143. dmabuf_ion_client = de_heap_ion_create_ion_client();
  144. pr_err("%s:dmabuf_ion_client = %li \n", __func__, (long)dmabuf_ion_client);
  145. if (!dmabuf_ion_client) {
  146. pr_err("%s:ion failed to get an ion client %lx \n", __func__, (long)dmabuf_ion_client);
  147. return -EFAULT;
  148. }
  149. return 0;
  150. }
  151. void de_heap_heap_deinit(void)
  152. {
  153. pr_info("%s:ion\n", __func__);
  154. de_heap_ion_destroy_ion_client(dmabuf_ion_client);
  155. }
  156. #else
  157. #error "kernel not supported"
  158. #endif
  159. /*
  160. * coding style for emacs
  161. *
  162. * Local variables:
  163. * indent-tabs-mode: t
  164. * tab-width: 8
  165. * c-basic-offset: 8
  166. * End:
  167. */