Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/gpu/ion/ion_priv.h |
| 3 | * |
| 4 | * Copyright (C) 2011 Google, Inc. |
Laura Abbott | a8c373f | 2013-02-15 09:25:35 -0800 | [diff] [blame] | 5 | * Copyright (c) 2011-2013, The Linux Foundation. All rights reserved. |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 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 | |
| 18 | #ifndef _ION_PRIV_H |
| 19 | #define _ION_PRIV_H |
| 20 | |
Rebecca Schultz Zavin | 050372e | 2012-06-07 16:36:44 -0700 | [diff] [blame] | 21 | #include <linux/ion.h> |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 22 | #include <linux/kref.h> |
| 23 | #include <linux/mm_types.h> |
| 24 | #include <linux/mutex.h> |
| 25 | #include <linux/rbtree.h> |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 26 | #include <linux/seq_file.h> |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 27 | |
Laura Abbott | 61e8959 | 2013-03-21 10:55:17 -0700 | [diff] [blame] | 28 | #include "msm_ion_priv.h" |
Rebecca Schultz Zavin | 050372e | 2012-06-07 16:36:44 -0700 | [diff] [blame] | 29 | #include <linux/sched.h> |
| 30 | #include <linux/shrinker.h> |
| 31 | #include <linux/types.h> |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 32 | |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 33 | struct ion_buffer *ion_handle_buffer(struct ion_handle *handle); |
| 34 | |
| 35 | /** |
| 36 | * struct ion_buffer - metadata for a particular buffer |
| 37 | * @ref: refernce count |
| 38 | * @node: node in the ion_device buffers tree |
| 39 | * @dev: back pointer to the ion_device |
| 40 | * @heap: back pointer to the heap the buffer came from |
| 41 | * @flags: buffer specific flags |
| 42 | * @size: size of the buffer |
| 43 | * @priv_virt: private data to the buffer representable as |
| 44 | * a void * |
| 45 | * @priv_phys: private data to the buffer representable as |
| 46 | * an ion_phys_addr_t (and someday a phys_addr_t) |
| 47 | * @lock: protects the buffers cnt fields |
| 48 | * @kmap_cnt: number of times the buffer is mapped to the kernel |
| 49 | * @vaddr: the kenrel mapping if kmap_cnt is not zero |
| 50 | * @dmap_cnt: number of times the buffer is mapped for dma |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 51 | * @sg_table: the sg table for the buffer if dmap_cnt is not zero |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 52 | */ |
| 53 | struct ion_buffer { |
| 54 | struct kref ref; |
| 55 | struct rb_node node; |
| 56 | struct ion_device *dev; |
| 57 | struct ion_heap *heap; |
| 58 | unsigned long flags; |
| 59 | size_t size; |
| 60 | union { |
| 61 | void *priv_virt; |
| 62 | ion_phys_addr_t priv_phys; |
| 63 | }; |
| 64 | struct mutex lock; |
| 65 | int kmap_cnt; |
| 66 | void *vaddr; |
| 67 | int dmap_cnt; |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 68 | struct sg_table *sg_table; |
Rebecca Schultz Zavin | b179067 | 2012-06-14 15:08:53 -0700 | [diff] [blame] | 69 | unsigned long *dirty; |
| 70 | struct list_head vmas; |
Laura Abbott | 404f824 | 2011-10-31 14:22:53 -0700 | [diff] [blame] | 71 | int marked; |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | /** |
| 75 | * struct ion_heap_ops - ops to operate on a given heap |
| 76 | * @allocate: allocate memory |
| 77 | * @free: free memory |
| 78 | * @phys get physical address of a buffer (only define on |
| 79 | * physically contiguous heaps) |
| 80 | * @map_dma map the memory for dma to a scatterlist |
| 81 | * @unmap_dma unmap the memory for dma |
| 82 | * @map_kernel map memory to the kernel |
| 83 | * @unmap_kernel unmap memory to the kernel |
| 84 | * @map_user map memory to userspace |
Alex Bird | 8a3ede3 | 2011-11-07 12:33:42 -0800 | [diff] [blame] | 85 | * @unmap_user unmap memory to userspace |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 86 | */ |
| 87 | struct ion_heap_ops { |
| 88 | int (*allocate) (struct ion_heap *heap, |
| 89 | struct ion_buffer *buffer, unsigned long len, |
| 90 | unsigned long align, unsigned long flags); |
| 91 | void (*free) (struct ion_buffer *buffer); |
| 92 | int (*phys) (struct ion_heap *heap, struct ion_buffer *buffer, |
| 93 | ion_phys_addr_t *addr, size_t *len); |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 94 | struct sg_table *(*map_dma) (struct ion_heap *heap, |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 95 | struct ion_buffer *buffer); |
| 96 | void (*unmap_dma) (struct ion_heap *heap, struct ion_buffer *buffer); |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 97 | void * (*map_kernel) (struct ion_heap *heap, struct ion_buffer *buffer); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 98 | void (*unmap_kernel) (struct ion_heap *heap, struct ion_buffer *buffer); |
| 99 | int (*map_user) (struct ion_heap *mapper, struct ion_buffer *buffer, |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 100 | struct vm_area_struct *vma); |
Alex Bird | 8a3ede3 | 2011-11-07 12:33:42 -0800 | [diff] [blame] | 101 | void (*unmap_user) (struct ion_heap *mapper, struct ion_buffer *buffer); |
Olav Haugan | 0671b9a | 2012-05-25 11:58:56 -0700 | [diff] [blame] | 102 | int (*print_debug)(struct ion_heap *heap, struct seq_file *s, |
| 103 | const struct rb_root *mem_map); |
Laura Abbott | 7e44648 | 2012-06-13 15:59:39 -0700 | [diff] [blame] | 104 | int (*secure_heap)(struct ion_heap *heap, int version, void *data); |
| 105 | int (*unsecure_heap)(struct ion_heap *heap, int version, void *data); |
Laura Abbott | 9361930 | 2012-10-11 11:51:40 -0700 | [diff] [blame] | 106 | int (*secure_buffer)(struct ion_buffer *buffer, int version, |
| 107 | void *data, int flags); |
| 108 | int (*unsecure_buffer)(struct ion_buffer *buffer, int force_unsecure); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | /** |
| 112 | * struct ion_heap - represents a heap in the system |
| 113 | * @node: rb node to put the heap on the device's tree of heaps |
| 114 | * @dev: back pointer to the ion_device |
| 115 | * @type: type of heap |
| 116 | * @ops: ops struct as above |
| 117 | * @id: id of heap, also indicates priority of this heap when |
| 118 | * allocating. These are specified by platform data and |
| 119 | * MUST be unique |
| 120 | * @name: used for debugging |
Benjamin Gaignard | 8dff0a6 | 2012-06-25 15:30:18 -0700 | [diff] [blame] | 121 | * @priv: private heap data |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 122 | * @debug_show: called when heap debug file is read to add any |
| 123 | * heap specific debug info to output |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 124 | * |
| 125 | * Represents a pool of memory from which buffers can be made. In some |
| 126 | * systems the only heap is regular system memory allocated via vmalloc. |
| 127 | * On others, some blocks might require large physically contiguous buffers |
| 128 | * that are allocated from a specially reserved heap. |
| 129 | */ |
| 130 | struct ion_heap { |
| 131 | struct rb_node node; |
| 132 | struct ion_device *dev; |
| 133 | enum ion_heap_type type; |
| 134 | struct ion_heap_ops *ops; |
| 135 | int id; |
| 136 | const char *name; |
Benjamin Gaignard | 8dff0a6 | 2012-06-25 15:30:18 -0700 | [diff] [blame] | 137 | void *priv; |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 138 | int (*debug_show)(struct ion_heap *heap, struct seq_file *, void *); |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | /** |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 142 | * ion_buffer_cached - this ion buffer is cached |
| 143 | * @buffer: buffer |
| 144 | * |
| 145 | * indicates whether this ion buffer is cached |
| 146 | */ |
| 147 | bool ion_buffer_cached(struct ion_buffer *buffer); |
| 148 | |
| 149 | /** |
Rebecca Schultz Zavin | f858ba4 | 2012-09-21 11:46:06 -0700 | [diff] [blame] | 150 | * ion_buffer_fault_user_mappings - fault in user mappings of this buffer |
| 151 | * @buffer: buffer |
| 152 | * |
| 153 | * indicates whether userspace mappings of this buffer will be faulted |
| 154 | * in, this can affect how buffers are allocated from the heap. |
| 155 | */ |
| 156 | bool ion_buffer_fault_user_mappings(struct ion_buffer *buffer); |
| 157 | |
| 158 | /** |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 159 | * ion_device_create - allocates and returns an ion device |
| 160 | * @custom_ioctl: arch specific ioctl function if applicable |
| 161 | * |
| 162 | * returns a valid device or -PTR_ERR |
| 163 | */ |
| 164 | struct ion_device *ion_device_create(long (*custom_ioctl) |
| 165 | (struct ion_client *client, |
| 166 | unsigned int cmd, |
| 167 | unsigned long arg)); |
| 168 | |
| 169 | /** |
| 170 | * ion_device_destroy - free and device and it's resource |
| 171 | * @dev: the device |
| 172 | */ |
| 173 | void ion_device_destroy(struct ion_device *dev); |
| 174 | |
| 175 | /** |
| 176 | * ion_device_add_heap - adds a heap to the ion device |
| 177 | * @dev: the device |
| 178 | * @heap: the heap to add |
| 179 | */ |
| 180 | void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap); |
| 181 | |
| 182 | /** |
| 183 | * functions for creating and destroying the built in ion heaps. |
| 184 | * architectures can add their own custom architecture specific |
| 185 | * heaps as appropriate. |
| 186 | */ |
| 187 | |
| 188 | struct ion_heap *ion_heap_create(struct ion_platform_heap *); |
| 189 | void ion_heap_destroy(struct ion_heap *); |
| 190 | |
| 191 | struct ion_heap *ion_system_heap_create(struct ion_platform_heap *); |
| 192 | void ion_system_heap_destroy(struct ion_heap *); |
| 193 | |
| 194 | struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *); |
| 195 | void ion_system_contig_heap_destroy(struct ion_heap *); |
| 196 | |
| 197 | struct ion_heap *ion_carveout_heap_create(struct ion_platform_heap *); |
| 198 | void ion_carveout_heap_destroy(struct ion_heap *); |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 199 | |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 200 | /** |
| 201 | * kernel api to allocate/free from carveout -- used when carveout is |
| 202 | * used to back an architecture specific custom heap |
| 203 | */ |
| 204 | ion_phys_addr_t ion_carveout_allocate(struct ion_heap *heap, unsigned long size, |
| 205 | unsigned long align); |
| 206 | void ion_carveout_free(struct ion_heap *heap, ion_phys_addr_t addr, |
| 207 | unsigned long size); |
Laura Abbott | 8c01736 | 2011-09-22 20:59:12 -0700 | [diff] [blame] | 208 | |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 209 | /** |
Laura Abbott | 61e8959 | 2013-03-21 10:55:17 -0700 | [diff] [blame] | 210 | * The carveout heap returns physical addresses, since 0 may be a valid |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 211 | * physical address, this is used to indicate allocation failed |
| 212 | */ |
| 213 | #define ION_CARVEOUT_ALLOCATE_FAIL -1 |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 214 | |
Rebecca Schultz Zavin | 050372e | 2012-06-07 16:36:44 -0700 | [diff] [blame] | 215 | |
| 216 | /** |
| 217 | * functions for creating and destroying a heap pool -- allows you |
| 218 | * to keep a pool of pre allocated memory to use from your heap. Keeping |
| 219 | * a pool of memory that is ready for dma, ie any cached mapping have been |
| 220 | * invalidated from the cache, provides a significant peformance benefit on |
| 221 | * many systems */ |
| 222 | |
| 223 | /** |
| 224 | * struct ion_page_pool - pagepool struct |
| 225 | * @count: number of items in the pool |
| 226 | * @items: list of items |
| 227 | * @shrinker: a shrinker for the items |
| 228 | * @mutex: lock protecting this struct and especially the count |
| 229 | * item list |
| 230 | * @alloc: function to be used to allocate pageory when the pool |
| 231 | * is empty |
| 232 | * @free: function to be used to free pageory back to the system |
| 233 | * when the shrinker fires |
| 234 | * @gfp_mask: gfp_mask to use from alloc |
| 235 | * @order: order of pages in the pool |
| 236 | * |
| 237 | * Allows you to keep a pool of pre allocated pages to use from your heap. |
| 238 | * Keeping a pool of pages that is ready for dma, ie any cached mapping have |
| 239 | * been invalidated from the cache, provides a significant peformance benefit |
| 240 | * on many systems |
| 241 | */ |
| 242 | struct ion_page_pool { |
| 243 | int count; |
| 244 | struct list_head items; |
| 245 | struct shrinker shrinker; |
| 246 | struct mutex mutex; |
| 247 | void *(*alloc)(struct ion_page_pool *pool); |
| 248 | void (*free)(struct ion_page_pool *pool, struct page *page); |
| 249 | gfp_t gfp_mask; |
| 250 | unsigned int order; |
| 251 | }; |
| 252 | |
| 253 | struct ion_page_pool *ion_page_pool_create(gfp_t gfp_mask, unsigned int order); |
| 254 | void ion_page_pool_destroy(struct ion_page_pool *); |
| 255 | void *ion_page_pool_alloc(struct ion_page_pool *); |
| 256 | void ion_page_pool_free(struct ion_page_pool *, struct page *); |
| 257 | |
Rebecca Schultz Zavin | 0c38bfd | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 258 | #endif /* _ION_PRIV_H */ |