img_mem_man.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*!
  2. *****************************************************************************
  3. * Copyright (c) Imagination Technologies Ltd.
  4. *
  5. * The contents of this file are subject to the MIT license as set out below.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. *
  25. * Alternatively, the contents of this file may be used under the terms of the
  26. * GNU General Public License Version 2 ("GPL")in which case the provisions of
  27. * GPL are applicable instead of those above.
  28. *
  29. * If you wish to allow use of your version of this file only under the terms
  30. * of GPL, and not to allow others to use your version of this file under the
  31. * terms of the MIT license, indicate your decision by deleting the provisions
  32. * above and replace them with the notice and other provisions required by GPL
  33. * as set out in the file called "GPLHEADER" included in this distribution. If
  34. * you do not delete the provisions above, a recipient may use your version of
  35. * this file under the terms of either the MIT license or GPL.
  36. *
  37. * This License is also included in this distribution in the file called
  38. * "MIT_COPYING".
  39. *
  40. *****************************************************************************/
  41. #ifndef IMG_MEM_MAN_H
  42. #define IMG_MEM_MAN_H
  43. #include <linux/version.h>
  44. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4,14,0)
  45. #define KERNEL_DMA_FENCE_SUPPORT
  46. #endif
  47. #include <linux/mm.h>
  48. #include <linux/types.h>
  49. #include <linux/device.h>
  50. #ifdef KERNEL_DMA_FENCE_SUPPORT
  51. #include <linux/dma-fence.h>
  52. #endif
  53. #include "uapi/img_mem_man.h"
  54. /* Defines allocation order range for platforms
  55. * that do not explicitly defined it.
  56. * NOTE: applicable for unified heap type only */
  57. #define IMG_MIN_ALLOC_ORDER_DEFAULT 0
  58. #define IMG_MAX_ALLOC_ORDER_DEFAULT 0
  59. /* Page catalogue address shift */
  60. #define IMG_MMU_PC_ADDR_SHIFT 12
  61. /* MMUv3 PTE entry flags */
  62. enum {
  63. IMG_MMU_PTE_FLAG_NONE = 0x0,
  64. IMG_MMU_PTE_FLAG_VALID = 0x1,
  65. IMG_MMU_PTE_FLAG_READ_ONLY = 0x2,
  66. IMG_MMU_PTE_FLAG_CACHE_COHERENCY = 0x4,
  67. };
  68. /* All level entries flags are stored under 4lsb bits */
  69. #define IMG_MMU_ENTRY_FLAGS_MASK 0xf
  70. /* Each entry can store 40bit physical address */
  71. #define IMG_MMU_PHY_ADDR_MASK ((1ULL<<40)-1)
  72. struct mmu_config {
  73. uint32_t addr_width; /* physical */
  74. bool bypass_hw; /* MMU bypass mode */
  75. size_t bypass_offset; /* Optional offset in physical space for MMU bypass mode */
  76. bool use_pte_parity; /* enables parity calculation for PTEs */
  77. /* memory attributes to be used when allocating mmu pages */
  78. enum img_mem_attr alloc_attr;
  79. int page_size;
  80. };
  81. union heap_options {
  82. struct {
  83. gfp_t gfp_type; /* pool and flags for buffer allocations */
  84. int min_order; /* minimum page allocation order */
  85. int max_order; /* maximum page allocation order */
  86. } unified;
  87. #ifdef CONFIG_ION
  88. struct {
  89. struct ion_client *client; /* must be provided by platform */
  90. } ion;
  91. #endif
  92. #ifdef CONFIG_GENERIC_ALLOCATOR
  93. struct {
  94. void *kptr; /* static pointer to kernel mapping of memory */
  95. /* Optional hooks to obtain kernel mapping dynamically */
  96. void* (*get_kptr)(
  97. phys_addr_t addr,
  98. size_t size,
  99. enum img_mem_attr mattr);
  100. int (*put_kptr)(void *);
  101. phys_addr_t phys; /* physical address start of memory */
  102. size_t size; /* size of memory */
  103. unsigned long offs; /* optional offset of the start
  104. of memory as seen from device,
  105. zero by default */
  106. int pool_order; /* allocation order */
  107. } carveout;
  108. #endif
  109. struct {
  110. bool use_sg_dma; /* Forces sg_dma physical address instead of CPU physical address*/
  111. } dmabuf;
  112. struct {
  113. gfp_t gfp_flags; /* for buffer allocations */
  114. } coherent;
  115. struct {
  116. phys_addr_t phys; /* physical address start of memory */
  117. size_t size; /* size of memory */
  118. enum img_mem_heap_attrs hattr; /* User attributes */
  119. } ocm;
  120. };
  121. struct heap_config {
  122. enum img_mem_heap_type type;
  123. union heap_options options;
  124. /* (optional) functions to convert a physical address as seen from
  125. the CPU to the physical address as seen from the vha device and
  126. vice versa. When not implemented,
  127. it is assumed that physical addresses are the
  128. same regardless of viewpoint */
  129. phys_addr_t (*to_dev_addr)(union heap_options *opts, phys_addr_t addr);
  130. phys_addr_t (*to_host_addr)(union heap_options *opts, phys_addr_t addr);
  131. /* Cache attribute,
  132. * could be platform specific if provided - overwrites the global cache policy */
  133. enum img_mem_attr cache_attr;
  134. };
  135. enum img_mmu_callback_type {
  136. IMG_MMU_CALLBACK_MAP = 1,
  137. IMG_MMU_CALLBACK_UNMAP,
  138. };
  139. struct mem_ctx;
  140. struct mmu_ctx;
  141. int img_mem_add_heap(const struct heap_config *heap_cfg, int *heap_id);
  142. void img_mem_del_heap(int heap_id);
  143. int img_mem_get_heap_info(int heap_id, uint8_t *type, uint32_t *attrs);
  144. /*
  145. * related to process context (contains SYSMEM heap's functionality in general)
  146. */
  147. int img_mem_create_proc_ctx(struct mem_ctx **ctx);
  148. void img_mem_destroy_proc_ctx(struct mem_ctx *ctx);
  149. int img_mem_alloc(struct device *device, struct mem_ctx *ctx, int heap_id,
  150. size_t size, enum img_mem_attr attributes, int *buf_id);
  151. int img_mem_import(struct device *device, struct mem_ctx *ctx, int heap_id,
  152. size_t size, enum img_mem_attr attributes, uint64_t buf_hnd,
  153. int *buf_id);
  154. int img_mem_export(struct device *device, struct mem_ctx *ctx, int buf_id,
  155. size_t size, enum img_mem_attr attributes, uint64_t *buf_hnd);
  156. void img_mem_free(struct mem_ctx *ctx, int buf_id);
  157. int img_mem_map_um(struct mem_ctx *ctx, int buf_id, struct vm_area_struct *vma);
  158. int img_mem_unmap_um(struct mem_ctx *ctx, int buf_id);
  159. int img_mem_map_km(struct mem_ctx *ctx, int buf_id);
  160. int img_mem_unmap_km(struct mem_ctx *ctx, int buf_id);
  161. void *img_mem_get_kptr(struct mem_ctx *ctx, int buf_id);
  162. uint64_t *img_mem_get_page_array(struct mem_ctx *mem_ctx, int buf_id);
  163. uint64_t img_mem_get_single_page(struct mem_ctx *mem_ctx, int buf_id,
  164. unsigned int offset);
  165. phys_addr_t img_mem_get_dev_addr(struct mem_ctx *mem_ctx, int buf_id,
  166. phys_addr_t addr);
  167. int img_mem_sync_cpu_to_device(struct mem_ctx *ctx, int buf_id);
  168. int img_mem_sync_device_to_cpu(struct mem_ctx *ctx, int buf_id);
  169. int img_mem_get_usage(const struct mem_ctx *ctx, size_t *max, size_t *curr);
  170. int img_mmu_get_usage(const struct mem_ctx *ctx, size_t *max, size_t *curr);
  171. #ifdef KERNEL_DMA_FENCE_SUPPORT
  172. struct dma_fence * img_mem_add_fence(struct mem_ctx *ctx, int buf_id);
  173. void img_mem_remove_fence(struct mem_ctx *ctx, int buf_id);
  174. int img_mem_signal_fence(struct mem_ctx *ctx, int buf_id);
  175. #endif
  176. /*
  177. * related to stream MMU context (constains IMGMMU functionality in general)
  178. */
  179. int img_mmu_ctx_create(struct device *device, const struct mmu_config *config,
  180. struct mem_ctx *mem_ctx, int heap_id,
  181. int (*callback_fn)(enum img_mmu_callback_type type,
  182. int buf_id, void *data),
  183. void *callback_data,
  184. struct mmu_ctx **mmu_ctx);
  185. void img_mmu_ctx_destroy(struct mmu_ctx *mmu);
  186. int img_mmu_map(struct mmu_ctx *mmu_ctx, struct mem_ctx *mem_ctx, int buf_id,
  187. uint64_t virt_addr, unsigned int map_flags);
  188. int img_mmu_unmap(struct mmu_ctx *mmu_ctx, struct mem_ctx *mem_ctx, int buf_id);
  189. int img_mmu_get_pc(const struct mmu_ctx *ctx,
  190. unsigned int *pc_reg, int *buf_id);
  191. int img_mmu_get_conf(size_t *page_size, size_t *virt_size);
  192. phys_addr_t img_mmu_get_paddr(const struct mmu_ctx *ctx,
  193. uint64_t vaddr, uint8_t *flags);
  194. int img_mmu_init_cache(struct mmu_ctx *mmu_ctx, unsigned long cache_phys_start,
  195. uint32_t cache_size);
  196. int img_mmu_clear_cache(struct mmu_ctx *mmu_ctx);
  197. int img_mmu_move_pg_to_cache(struct mmu_ctx *mmu_ctx, struct mem_ctx *mem_ctx,
  198. int buf_id, uint64_t virt_addr, uint32_t page_size, uint32_t page_idx);
  199. /*
  200. * virtual address allocation
  201. */
  202. struct mmu_vaa;
  203. int img_mmu_vaa_create(struct device *device,
  204. uint32_t base, size_t size, struct mmu_vaa **vaa);
  205. int img_mmu_vaa_destroy(struct mmu_vaa *vaa);
  206. int img_mmu_vaa_alloc(struct mmu_vaa *vaa, size_t size, uint32_t *addr);
  207. int img_mmu_vaa_free(struct mmu_vaa *vaa, uint32_t addr, size_t size);
  208. bool img_mem_calc_parity(unsigned long long input);
  209. /*
  210. * PDUMP generation:
  211. * img_pdump_txt_create creates a TXT buffer in RAM,
  212. * which is used by img_pdump_txt_printf
  213. * img_pdump_bin_create creates a PRM or RES buffer in RAM,
  214. * which is used by img_pdump_bin_write
  215. *
  216. */
  217. struct pdump_buf {
  218. char *ptr;
  219. size_t size; /* allocated size of buffer */
  220. size_t len; /* how full is the buffer */
  221. bool drop_data; /* do not store data in file */
  222. };
  223. #define PDUMP_TXT 0 /* eg pdump.txt */
  224. #define PDUMP_PRM 1 /* eg pdump.prm */
  225. #define PDUMP_RES 2 /* eg pdump.res */
  226. #define PDUMP_DBG 3 /* eg pdump.dbg */
  227. #define PDUMP_CRC 4 /* eg pdump.crc */
  228. #define PDUMP_CRC_CMB 5 /* eg pdump.crc_cmb */
  229. #define PDUMP_MAX 6
  230. /*
  231. * VHA PDUMPs.
  232. * Uses img_pdump buffers to collect pdump information.
  233. * there are 3 different PDUMP files: TXT, PRM and RES.
  234. * they are simply buffers in ram.
  235. * They are mapped into debugfs: /sys/kernel/debug/vhaN/pdump.*
  236. */
  237. struct pdump_descr {
  238. struct pdump_buf pbufs[PDUMP_MAX];
  239. struct mutex lock;
  240. };
  241. #ifndef OSID
  242. #define _PMEM_ ":MEM"
  243. #else
  244. #define _PMEM_ ":MEM_OS"__stringify(OSID)
  245. #endif
  246. struct pdump_buf *img_pdump_create(struct pdump_descr* pdump, uint32_t pdump_num, size_t size);
  247. int img_pdump_write(struct pdump_descr* pdump, uint32_t pdump_num, const void *ptr, size_t size);
  248. int __img_pdump_printf(struct device* dev, const char *fmt, ...) __printf(2, 3);
  249. #define img_pdump_printf(fmt, ...) __img_pdump_printf(vha->dev, fmt, ##__VA_ARGS__)
  250. void img_pdump_destroy(struct pdump_descr* pdump);
  251. bool img_pdump_enabled(struct pdump_descr* pdump);
  252. #endif /* IMG_MEM_MAN_H */
  253. /*
  254. * coding style for emacs
  255. *
  256. * Local variables:
  257. * indent-tabs-mode: t
  258. * tab-width: 8
  259. * c-basic-offset: 8
  260. * End:
  261. */