ion_priv.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * drivers/gpu/ion/ion_priv.h
  3. *
  4. * Copyright (C) 2011 Google, Inc.
  5. * Copyright (c) 2011-2014, The Linux Foundation. All rights reserved.
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #ifndef _ION_PRIV_H
  18. #define _ION_PRIV_H
  19. #include <linux/ion.h>
  20. #include <linux/kref.h>
  21. #include <linux/mm_types.h>
  22. #include <linux/mutex.h>
  23. #include <linux/rbtree.h>
  24. #include <linux/seq_file.h>
  25. #include "msm_ion_priv.h"
  26. #include <linux/sched.h>
  27. #include <linux/shrinker.h>
  28. #include <linux/types.h>
  29. struct ion_buffer *ion_handle_buffer(struct ion_handle *handle);
  30. /**
  31. * struct ion_buffer - metadata for a particular buffer
  32. * @ref: refernce count
  33. * @node: node in the ion_device buffers tree
  34. * @dev: back pointer to the ion_device
  35. * @heap: back pointer to the heap the buffer came from
  36. * @flags: buffer specific flags
  37. * @size: size of the buffer
  38. * @priv_virt: private data to the buffer representable as
  39. * a void *
  40. * @priv_phys: private data to the buffer representable as
  41. * an ion_phys_addr_t (and someday a phys_addr_t)
  42. * @lock: protects the buffers cnt fields
  43. * @kmap_cnt: number of times the buffer is mapped to the kernel
  44. * @vaddr: the kenrel mapping if kmap_cnt is not zero
  45. * @dmap_cnt: number of times the buffer is mapped for dma
  46. * @sg_table: the sg table for the buffer if dmap_cnt is not zero
  47. * @dirty: bitmask representing which pages of this buffer have
  48. * been dirtied by the cpu and need cache maintenance
  49. * before dma
  50. * @vmas: list of vma's mapping this buffer
  51. * @handle_count: count of handles referencing this buffer
  52. * @task_comm: taskcomm of last client to reference this buffer in a
  53. * handle, used for debugging
  54. * @pid: pid of last client to reference this buffer in a
  55. * handle, used for debugging
  56. */
  57. struct ion_buffer {
  58. struct kref ref;
  59. union {
  60. struct rb_node node;
  61. struct list_head list;
  62. };
  63. struct ion_device *dev;
  64. struct ion_heap *heap;
  65. unsigned long flags;
  66. size_t size;
  67. union {
  68. void *priv_virt;
  69. ion_phys_addr_t priv_phys;
  70. };
  71. struct mutex lock;
  72. int kmap_cnt;
  73. void *vaddr;
  74. int dmap_cnt;
  75. struct sg_table *sg_table;
  76. unsigned long *dirty;
  77. struct list_head vmas;
  78. /* used to track orphaned buffers */
  79. int handle_count;
  80. char task_comm[TASK_COMM_LEN];
  81. pid_t pid;
  82. };
  83. void ion_buffer_destroy(struct ion_buffer *buffer);
  84. /**
  85. * struct ion_heap_ops - ops to operate on a given heap
  86. * @allocate: allocate memory
  87. * @free: free memory. Will be called with
  88. * ION_FLAG_FREED_FROM_SHRINKER set in buffer flags when
  89. * called from a shrinker. In that case, the pages being
  90. * free'd must be truly free'd back to the system, not put
  91. * in a page pool or otherwise cached.
  92. * @phys get physical address of a buffer (only define on
  93. * physically contiguous heaps)
  94. * @map_dma map the memory for dma to a scatterlist
  95. * @unmap_dma unmap the memory for dma
  96. * @map_kernel map memory to the kernel
  97. * @unmap_kernel unmap memory to the kernel
  98. * @map_user map memory to userspace
  99. * @unmap_user unmap memory to userspace
  100. */
  101. struct ion_heap_ops {
  102. int (*allocate) (struct ion_heap *heap,
  103. struct ion_buffer *buffer, unsigned long len,
  104. unsigned long align, unsigned long flags);
  105. void (*free) (struct ion_buffer *buffer);
  106. int (*phys) (struct ion_heap *heap, struct ion_buffer *buffer,
  107. ion_phys_addr_t *addr, size_t *len);
  108. struct sg_table *(*map_dma) (struct ion_heap *heap,
  109. struct ion_buffer *buffer);
  110. void (*unmap_dma) (struct ion_heap *heap, struct ion_buffer *buffer);
  111. void * (*map_kernel) (struct ion_heap *heap, struct ion_buffer *buffer);
  112. void (*unmap_kernel) (struct ion_heap *heap, struct ion_buffer *buffer);
  113. int (*map_user) (struct ion_heap *mapper, struct ion_buffer *buffer,
  114. struct vm_area_struct *vma);
  115. void (*unmap_user) (struct ion_heap *mapper, struct ion_buffer *buffer);
  116. int (*print_debug)(struct ion_heap *heap, struct seq_file *s,
  117. const struct list_head *mem_map);
  118. int (*secure_heap)(struct ion_heap *heap, int version, void *data);
  119. int (*unsecure_heap)(struct ion_heap *heap, int version, void *data);
  120. int (*secure_buffer)(struct ion_buffer *buffer, int version,
  121. void *data, int flags);
  122. int (*unsecure_buffer)(struct ion_buffer *buffer, int force_unsecure);
  123. };
  124. /**
  125. * heap flags - flags between the heaps and core ion code
  126. */
  127. #define ION_HEAP_FLAG_DEFER_FREE (1 << 0)
  128. /**
  129. * struct ion_heap - represents a heap in the system
  130. * @node: rb node to put the heap on the device's tree of heaps
  131. * @dev: back pointer to the ion_device
  132. * @type: type of heap
  133. * @ops: ops struct as above
  134. * @flags: flags
  135. * @id: id of heap, also indicates priority of this heap when
  136. * allocating. These are specified by platform data and
  137. * MUST be unique
  138. * @name: used for debugging
  139. * @shrinker: a shrinker for the heap, if the heap caches system
  140. * memory, it must define a shrinker to return it on low
  141. * memory conditions, this includes system memory cached
  142. * in the deferred free lists for heaps that support it
  143. * @priv: private heap data
  144. * @free_list: free list head if deferred free is used
  145. * @free_list_size size of the deferred free list in bytes
  146. * @lock: protects the free list
  147. * @waitqueue: queue to wait on from deferred free thread
  148. * @task: task struct of deferred free thread
  149. * @debug_show: called when heap debug file is read to add any
  150. * heap specific debug info to output
  151. *
  152. * Represents a pool of memory from which buffers can be made. In some
  153. * systems the only heap is regular system memory allocated via vmalloc.
  154. * On others, some blocks might require large physically contiguous buffers
  155. * that are allocated from a specially reserved heap.
  156. */
  157. struct ion_heap {
  158. struct plist_node node;
  159. struct ion_device *dev;
  160. enum ion_heap_type type;
  161. struct ion_heap_ops *ops;
  162. unsigned long flags;
  163. unsigned int id;
  164. const char *name;
  165. struct shrinker shrinker;
  166. void *priv;
  167. struct list_head free_list;
  168. size_t free_list_size;
  169. struct rt_mutex lock;
  170. wait_queue_head_t waitqueue;
  171. struct task_struct *task;
  172. int (*debug_show)(struct ion_heap *heap, struct seq_file *, void *);
  173. };
  174. /**
  175. * ion_buffer_cached - this ion buffer is cached
  176. * @buffer: buffer
  177. *
  178. * indicates whether this ion buffer is cached
  179. */
  180. bool ion_buffer_cached(struct ion_buffer *buffer);
  181. /**
  182. * ion_buffer_fault_user_mappings - fault in user mappings of this buffer
  183. * @buffer: buffer
  184. *
  185. * indicates whether userspace mappings of this buffer will be faulted
  186. * in, this can affect how buffers are allocated from the heap.
  187. */
  188. bool ion_buffer_fault_user_mappings(struct ion_buffer *buffer);
  189. /**
  190. * ion_device_create - allocates and returns an ion device
  191. * @custom_ioctl: arch specific ioctl function if applicable
  192. *
  193. * returns a valid device or -PTR_ERR
  194. */
  195. struct ion_device *ion_device_create(long (*custom_ioctl)
  196. (struct ion_client *client,
  197. unsigned int cmd,
  198. unsigned long arg));
  199. /**
  200. * ion_device_destroy - free and device and it's resource
  201. * @dev: the device
  202. */
  203. void ion_device_destroy(struct ion_device *dev);
  204. /**
  205. * ion_device_add_heap - adds a heap to the ion device
  206. * @dev: the device
  207. * @heap: the heap to add
  208. */
  209. void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap);
  210. struct pages_mem {
  211. struct page **pages;
  212. u32 size;
  213. void (*free_fn) (const void *);
  214. };
  215. /**
  216. * some helpers for common operations on buffers using the sg_table
  217. * and vaddr fields
  218. */
  219. void *ion_heap_map_kernel(struct ion_heap *, struct ion_buffer *);
  220. void ion_heap_unmap_kernel(struct ion_heap *, struct ion_buffer *);
  221. int ion_heap_map_user(struct ion_heap *, struct ion_buffer *,
  222. struct vm_area_struct *);
  223. int ion_heap_pages_zero(struct page **pages, int num_pages);
  224. int ion_heap_buffer_zero(struct ion_buffer *buffer);
  225. int ion_heap_high_order_page_zero(struct page *page, int order);
  226. int ion_heap_alloc_pages_mem(struct pages_mem *pages_mem);
  227. void ion_heap_free_pages_mem(struct pages_mem *pages_mem);
  228. /**
  229. * ion_heap_init_deferred_free -- initialize deferred free functionality
  230. * @heap: the heap
  231. *
  232. * If a heap sets the ION_HEAP_FLAG_DEFER_FREE flag this function will
  233. * be called to setup deferred frees. Calls to free the buffer will
  234. * return immediately and the actual free will occur some time later
  235. */
  236. int ion_heap_init_deferred_free(struct ion_heap *heap);
  237. /**
  238. * ion_heap_freelist_add - add a buffer to the deferred free list
  239. * @heap: the heap
  240. * @buffer: the buffer
  241. *
  242. * Adds an item to the deferred freelist.
  243. */
  244. void ion_heap_freelist_add(struct ion_heap *heap, struct ion_buffer *buffer);
  245. /**
  246. * ion_heap_freelist_drain - drain the deferred free list
  247. * @heap: the heap
  248. * @size: ammount of memory to drain in bytes
  249. *
  250. * Drains the indicated amount of memory from the deferred freelist immediately.
  251. * Returns the total amount freed. The total freed may be higher depending
  252. * on the size of the items in the list, or lower if there is insufficient
  253. * total memory on the freelist.
  254. */
  255. size_t ion_heap_freelist_drain(struct ion_heap *heap, size_t size);
  256. /**
  257. * ion_heap_freelist_drain_from_shrinker - drain the deferred free
  258. * list, skipping any heap-specific
  259. * pooling or caching mechanisms
  260. *
  261. * @heap: the heap
  262. * @size: amount of memory to drain in bytes
  263. *
  264. * Drains the indicated amount of memory from the deferred freelist immediately.
  265. * Returns the total amount freed. The total freed may be higher depending
  266. * on the size of the items in the list, or lower if there is insufficient
  267. * total memory on the freelist.
  268. *
  269. * Unlike with @ion_heap_freelist_drain, don't put any pages back into
  270. * page pools or otherwise cache the pages. Everything must be
  271. * genuinely free'd back to the system. If you're free'ing from a
  272. * shrinker you probably want to use this. Note that this relies on
  273. * the heap.ops.free callback honoring the
  274. * ION_FLAG_FREED_FROM_SHRINKER flag.
  275. */
  276. size_t ion_heap_freelist_drain_from_shrinker(struct ion_heap *heap,
  277. size_t size);
  278. /**
  279. * ion_heap_freelist_size - returns the size of the freelist in bytes
  280. * @heap: the heap
  281. */
  282. size_t ion_heap_freelist_size(struct ion_heap *heap);
  283. /**
  284. * functions for creating and destroying the built in ion heaps.
  285. * architectures can add their own custom architecture specific
  286. * heaps as appropriate.
  287. */
  288. struct ion_heap *ion_heap_create(struct ion_platform_heap *);
  289. void ion_heap_destroy(struct ion_heap *);
  290. struct ion_heap *ion_system_heap_create(struct ion_platform_heap *);
  291. void ion_system_heap_destroy(struct ion_heap *);
  292. struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *);
  293. void ion_system_contig_heap_destroy(struct ion_heap *);
  294. struct ion_heap *ion_carveout_heap_create(struct ion_platform_heap *);
  295. void ion_carveout_heap_destroy(struct ion_heap *);
  296. struct ion_heap *ion_chunk_heap_create(struct ion_platform_heap *);
  297. void ion_chunk_heap_destroy(struct ion_heap *);
  298. /**
  299. * kernel api to allocate/free from carveout -- used when carveout is
  300. * used to back an architecture specific custom heap
  301. */
  302. ion_phys_addr_t ion_carveout_allocate(struct ion_heap *heap, unsigned long size,
  303. unsigned long align);
  304. void ion_carveout_free(struct ion_heap *heap, ion_phys_addr_t addr,
  305. unsigned long size);
  306. /**
  307. * The carveout heap returns physical addresses, since 0 may be a valid
  308. * physical address, this is used to indicate allocation failed
  309. */
  310. #define ION_CARVEOUT_ALLOCATE_FAIL -1
  311. /**
  312. * functions for creating and destroying a heap pool -- allows you
  313. * to keep a pool of pre allocated memory to use from your heap. Keeping
  314. * a pool of memory that is ready for dma, ie any cached mapping have been
  315. * invalidated from the cache, provides a significant peformance benefit on
  316. * many systems */
  317. /**
  318. * struct ion_page_pool - pagepool struct
  319. * @high_count: number of highmem items in the pool
  320. * @low_count: number of lowmem items in the pool
  321. * @high_items: list of highmem items
  322. * @low_items: list of lowmem items
  323. * @shrinker: a shrinker for the items
  324. * @mutex: lock protecting this struct and especially the count
  325. * item list
  326. * @alloc: function to be used to allocate pageory when the pool
  327. * is empty
  328. * @free: function to be used to free pageory back to the system
  329. * when the shrinker fires
  330. * @gfp_mask: gfp_mask to use from alloc
  331. * @order: order of pages in the pool
  332. * @list: plist node for list of pools
  333. *
  334. * Allows you to keep a pool of pre allocated pages to use from your heap.
  335. * Keeping a pool of pages that is ready for dma, ie any cached mapping have
  336. * been invalidated from the cache, provides a significant peformance benefit
  337. * on many systems
  338. */
  339. struct ion_page_pool {
  340. int high_count;
  341. int low_count;
  342. struct list_head high_items;
  343. struct list_head low_items;
  344. struct mutex mutex;
  345. gfp_t gfp_mask;
  346. unsigned int order;
  347. struct plist_node list;
  348. };
  349. struct ion_page_pool *ion_page_pool_create(gfp_t gfp_mask, unsigned int order);
  350. void ion_page_pool_destroy(struct ion_page_pool *);
  351. void *ion_page_pool_alloc(struct ion_page_pool *, bool *from_pool);
  352. void ion_page_pool_free(struct ion_page_pool *, struct page *);
  353. /** ion_page_pool_shrink - shrinks the size of the memory cached in the pool
  354. * @pool: the pool
  355. * @gfp_mask: the memory type to reclaim
  356. * @nr_to_scan: number of items to shrink in pages
  357. *
  358. * returns the number of items freed in pages
  359. */
  360. int ion_page_pool_shrink(struct ion_page_pool *pool, gfp_t gfp_mask,
  361. int nr_to_scan);
  362. int ion_walk_heaps(struct ion_client *client, int heap_id, void *data,
  363. int (*f)(struct ion_heap *heap, void *data));
  364. struct ion_handle *ion_handle_get_by_id(struct ion_client *client,
  365. int id);
  366. int ion_handle_put(struct ion_handle *handle);
  367. #endif /* _ION_PRIV_H */