img_mem_man_priv.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*!
  2. *****************************************************************************
  3. *
  4. * @File img_mem_man_priv.h
  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. #ifndef IMG_MEM_MAN_PRIV_H
  46. #define IMG_MEM_MAN_PRIV_H
  47. #include <linux/list.h>
  48. #include <linux/idr.h>
  49. #include <linux/scatterlist.h>
  50. #include <linux/device.h>
  51. #include <img_mem_man.h>
  52. /* Memory context : one per process */
  53. struct mem_ctx {
  54. unsigned id;
  55. struct idr buffers;
  56. struct list_head mmu_ctxs;
  57. /* Used to track memory usage for all buffers and
  58. * separately for MMU page tables only */
  59. size_t mem_usage_max;
  60. size_t mem_usage_curr;
  61. size_t mmu_usage_max;
  62. size_t mmu_usage_curr;
  63. };
  64. /* An MMU mapping of a buffer */
  65. struct mmu_ctx_mapping {
  66. struct mmu_ctx *mmu_ctx;
  67. struct buffer *buffer;
  68. struct imgmmu_map *map;
  69. uint64_t virt_addr;
  70. uint64_t cache_offset;
  71. struct list_head mmu_ctx_entry; /* Entry in <mmu_ctx:mappings> */
  72. struct list_head buffer_entry; /* Entry in <buffer:mappings> */
  73. };
  74. /* mmu context : one per session */
  75. struct mmu_ctx {
  76. unsigned id;
  77. struct device *device;
  78. struct mmu_config config;
  79. struct mem_ctx *mem_ctx; /* for memory allocations */
  80. struct heap *heap; /* for memory allocations */
  81. struct imgmmu_cat *mmu_cat;
  82. struct list_head mappings; /* contains <struct mmu_ctx_mapping> */
  83. struct list_head mem_ctx_entry; /* Entry in <mem_ctx:mmu_ctxs> */
  84. int (*callback_fn)(enum img_mmu_callback_type type, int buf_id,
  85. void *data);
  86. void *callback_data;
  87. unsigned long cache_phys_start;
  88. uint32_t cache_size;
  89. };
  90. #ifdef KERNEL_DMA_FENCE_SUPPORT
  91. struct buffer_fence {
  92. struct dma_fence fence;
  93. spinlock_t lock;
  94. };
  95. #endif
  96. /* Pdump cache info */
  97. struct buffer_pcache {
  98. unsigned int last_offset;
  99. struct scatterlist *last_sgl;
  100. int last_idx;
  101. };
  102. /* buffer : valid in the context of a mem_ctx */
  103. struct buffer {
  104. int id; /* Generated in <mem_ctx:buffers> */
  105. size_t request_size;
  106. size_t actual_size;
  107. struct device *device;
  108. struct mem_ctx *mem_ctx;
  109. struct heap *heap;
  110. struct list_head mappings; /* contains <struct mmu_ctx_mapping> */
  111. void *kptr;
  112. void *priv;
  113. unsigned map_flags;
  114. #ifdef KERNEL_DMA_FENCE_SUPPORT
  115. struct buffer_fence *fence;
  116. #endif
  117. struct buffer_pcache pcache;
  118. };
  119. /* vaa_entry : represents single entry in mmu_vaa */
  120. struct vaa_entry {
  121. struct imgmmu_halloc *alloc;
  122. struct list_head mmu_vaa_entry; /* links to mmu_vaa */
  123. };
  124. /* mmu vaa : one per session */
  125. struct mmu_vaa {
  126. struct device *device;
  127. struct imgmmu_heap *heap;
  128. struct list_head entries; /* contains <struct vaa_entry> */
  129. };
  130. struct heap_ops {
  131. int (*alloc)(struct device *device, struct heap *heap,
  132. size_t size, enum img_mem_attr attr,
  133. struct buffer *buffer);
  134. int (*import)(struct device *device, struct heap *heap,
  135. size_t size, enum img_mem_attr attr, uint64_t buf_hnd,
  136. struct buffer *buffer);
  137. int (*export)(struct device *device, struct heap *heap,
  138. size_t size, enum img_mem_attr attr, struct buffer *buffer,
  139. uint64_t *buf_hnd);
  140. void (*free)(struct heap *heap, struct buffer *buffer);
  141. int (*map_um)(struct heap *heap, struct buffer *buffer,
  142. struct vm_area_struct *vma);
  143. int (*unmap_um)(struct heap *heap, struct buffer *buffer);
  144. int (*map_km)(struct heap *heap, struct buffer *buffer);
  145. int (*unmap_km)(struct heap *heap, struct buffer *buffer);
  146. int (*get_sg_table)(struct heap *heap, struct buffer *buffer,
  147. struct sg_table **sg_table, bool *use_sg_dma);
  148. int (*get_page_array)(struct heap *heap, struct buffer *buffer,
  149. uint64_t **addrs);
  150. void (*sync_cpu_to_dev)(struct heap *heap, struct buffer *buffer);
  151. void (*sync_dev_to_cpu)(struct heap *heap, struct buffer *buffer);
  152. int (*set_offset)(struct heap *heap, size_t offs);
  153. void (*destroy)(struct heap *heap);
  154. };
  155. struct heap {
  156. int id; /* Generated in <mem_man:heaps> */
  157. enum img_mem_heap_type type;
  158. struct heap_ops *ops;
  159. union heap_options options;
  160. phys_addr_t (*to_dev_addr)(union heap_options *opts, phys_addr_t addr);
  161. phys_addr_t (*to_host_addr)(union heap_options *opts, phys_addr_t addr);
  162. bool cache_sync;
  163. enum img_mem_attr alt_cache_attr;
  164. void *priv;
  165. };
  166. int img_mem_unified_init(const struct heap_config *config, struct heap *heap);
  167. int img_mem_coherent_init(const struct heap_config *config, struct heap *heap);
  168. #ifdef CONFIG_DMA_SHARED_BUFFER
  169. int img_mem_dmabuf_init(const struct heap_config *config, struct heap *heap);
  170. #endif
  171. #ifdef CONFIG_ION
  172. int img_mem_ion_init(const struct heap_config *config, struct heap *heap);
  173. #endif
  174. #ifdef CONFIG_GENERIC_ALLOCATOR
  175. int img_mem_carveout_init(const struct heap_config *config, struct heap *heap);
  176. #endif
  177. int img_mem_anonymous_init(const struct heap_config *config, struct heap *heap);
  178. int img_mem_ocm_init(const struct heap_config *config, struct heap *heap);
  179. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0)
  180. typedef int vm_fault_t;
  181. #endif
  182. #endif /* IMG_MEM_MAN_PRIV_H */
  183. /*
  184. * coding style for emacs
  185. *
  186. * Local variables:
  187. * indent-tabs-mode: t
  188. * tab-width: 8
  189. * c-basic-offset: 8
  190. * End:
  191. */