ion_priv.h revision cd69488c7be3abb0767b49f313b12b8591abe5dc
1/* 2 * drivers/staging/android/ion/ion_priv.h 3 * 4 * Copyright (C) 2011 Google, Inc. 5 * 6 * This software is licensed under the terms of the GNU General Public 7 * License version 2, as published by the Free Software Foundation, and 8 * may be copied, distributed, and modified under those terms. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 */ 16 17#ifndef _ION_PRIV_H 18#define _ION_PRIV_H 19 20#include <linux/kref.h> 21#include <linux/mm_types.h> 22#include <linux/mutex.h> 23#include <linux/rbtree.h> 24#include <linux/sched.h> 25#include <linux/shrinker.h> 26#include <linux/types.h> 27 28#include "ion.h" 29 30struct ion_buffer *ion_handle_buffer(struct ion_handle *handle); 31 32/** 33 * struct ion_buffer - metadata for a particular buffer 34 * @ref: refernce count 35 * @node: node in the ion_device buffers tree 36 * @dev: back pointer to the ion_device 37 * @heap: back pointer to the heap the buffer came from 38 * @flags: buffer specific flags 39 * @size: size of the buffer 40 * @priv_virt: private data to the buffer representable as 41 * a void * 42 * @priv_phys: private data to the buffer representable as 43 * an ion_phys_addr_t (and someday a phys_addr_t) 44 * @lock: protects the buffers cnt fields 45 * @kmap_cnt: number of times the buffer is mapped to the kernel 46 * @vaddr: the kenrel mapping if kmap_cnt is not zero 47 * @dmap_cnt: number of times the buffer is mapped for dma 48 * @sg_table: the sg table for the buffer if dmap_cnt is not zero 49 * @dirty: bitmask representing which pages of this buffer have 50 * been dirtied by the cpu and need cache maintenance 51 * before dma 52 * @vmas: list of vma's mapping this buffer 53 * @handle_count: count of handles referencing this buffer 54 * @task_comm: taskcomm of last client to reference this buffer in a 55 * handle, used for debugging 56 * @pid: pid of last client to reference this buffer in a 57 * handle, used for debugging 58*/ 59struct ion_buffer { 60 struct kref ref; 61 struct rb_node node; 62 struct ion_device *dev; 63 struct ion_heap *heap; 64 unsigned long flags; 65 size_t size; 66 union { 67 void *priv_virt; 68 ion_phys_addr_t priv_phys; 69 }; 70 struct mutex lock; 71 int kmap_cnt; 72 void *vaddr; 73 int dmap_cnt; 74 struct sg_table *sg_table; 75 unsigned long *dirty; 76 struct list_head vmas; 77 /* used to track orphaned buffers */ 78 int handle_count; 79 char task_comm[TASK_COMM_LEN]; 80 pid_t pid; 81}; 82 83/** 84 * struct ion_heap_ops - ops to operate on a given heap 85 * @allocate: allocate memory 86 * @free: free memory 87 * @phys get physical address of a buffer (only define on 88 * physically contiguous heaps) 89 * @map_dma map the memory for dma to a scatterlist 90 * @unmap_dma unmap the memory for dma 91 * @map_kernel map memory to the kernel 92 * @unmap_kernel unmap memory to the kernel 93 * @map_user map memory to userspace 94 */ 95struct ion_heap_ops { 96 int (*allocate) (struct ion_heap *heap, 97 struct ion_buffer *buffer, unsigned long len, 98 unsigned long align, unsigned long flags); 99 void (*free) (struct ion_buffer *buffer); 100 int (*phys) (struct ion_heap *heap, struct ion_buffer *buffer, 101 ion_phys_addr_t *addr, size_t *len); 102 struct sg_table *(*map_dma) (struct ion_heap *heap, 103 struct ion_buffer *buffer); 104 void (*unmap_dma) (struct ion_heap *heap, struct ion_buffer *buffer); 105 void * (*map_kernel) (struct ion_heap *heap, struct ion_buffer *buffer); 106 void (*unmap_kernel) (struct ion_heap *heap, struct ion_buffer *buffer); 107 int (*map_user) (struct ion_heap *mapper, struct ion_buffer *buffer, 108 struct vm_area_struct *vma); 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 121 * @debug_show: called when heap debug file is read to add any 122 * heap specific debug info to output 123 * 124 * Represents a pool of memory from which buffers can be made. In some 125 * systems the only heap is regular system memory allocated via vmalloc. 126 * On others, some blocks might require large physically contiguous buffers 127 * that are allocated from a specially reserved heap. 128 */ 129struct ion_heap { 130 struct plist_node node; 131 struct ion_device *dev; 132 enum ion_heap_type type; 133 struct ion_heap_ops *ops; 134 unsigned int id; 135 const char *name; 136 int (*debug_show)(struct ion_heap *heap, struct seq_file *, void *); 137}; 138 139/** 140 * ion_buffer_cached - this ion buffer is cached 141 * @buffer: buffer 142 * 143 * indicates whether this ion buffer is cached 144 */ 145bool ion_buffer_cached(struct ion_buffer *buffer); 146 147/** 148 * ion_buffer_fault_user_mappings - fault in user mappings of this buffer 149 * @buffer: buffer 150 * 151 * indicates whether userspace mappings of this buffer will be faulted 152 * in, this can affect how buffers are allocated from the heap. 153 */ 154bool ion_buffer_fault_user_mappings(struct ion_buffer *buffer); 155 156/** 157 * ion_device_create - allocates and returns an ion device 158 * @custom_ioctl: arch specific ioctl function if applicable 159 * 160 * returns a valid device or -PTR_ERR 161 */ 162struct ion_device *ion_device_create(long (*custom_ioctl) 163 (struct ion_client *client, 164 unsigned int cmd, 165 unsigned long arg)); 166 167/** 168 * ion_device_destroy - free and device and it's resource 169 * @dev: the device 170 */ 171void ion_device_destroy(struct ion_device *dev); 172 173/** 174 * ion_device_add_heap - adds a heap to the ion device 175 * @dev: the device 176 * @heap: the heap to add 177 */ 178void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap); 179 180/** 181 * functions for creating and destroying the built in ion heaps. 182 * architectures can add their own custom architecture specific 183 * heaps as appropriate. 184 */ 185 186struct ion_heap *ion_heap_create(struct ion_platform_heap *); 187void ion_heap_destroy(struct ion_heap *); 188 189struct ion_heap *ion_system_heap_create(struct ion_platform_heap *); 190void ion_system_heap_destroy(struct ion_heap *); 191 192struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *); 193void ion_system_contig_heap_destroy(struct ion_heap *); 194 195struct ion_heap *ion_carveout_heap_create(struct ion_platform_heap *); 196void ion_carveout_heap_destroy(struct ion_heap *); 197/** 198 * kernel api to allocate/free from carveout -- used when carveout is 199 * used to back an architecture specific custom heap 200 */ 201ion_phys_addr_t ion_carveout_allocate(struct ion_heap *heap, unsigned long size, 202 unsigned long align); 203void ion_carveout_free(struct ion_heap *heap, ion_phys_addr_t addr, 204 unsigned long size); 205/** 206 * The carveout heap returns physical addresses, since 0 may be a valid 207 * physical address, this is used to indicate allocation failed 208 */ 209#define ION_CARVEOUT_ALLOCATE_FAIL -1 210 211/** 212 * functions for creating and destroying a heap pool -- allows you 213 * to keep a pool of pre allocated memory to use from your heap. Keeping 214 * a pool of memory that is ready for dma, ie any cached mapping have been 215 * invalidated from the cache, provides a significant peformance benefit on 216 * many systems */ 217 218/** 219 * struct ion_page_pool - pagepool struct 220 * @high_count: number of highmem items in the pool 221 * @low_count: number of lowmem items in the pool 222 * @high_items: list of highmem items 223 * @low_items: list of lowmem items 224 * @shrinker: a shrinker for the items 225 * @mutex: lock protecting this struct and especially the count 226 * item list 227 * @alloc: function to be used to allocate pageory when the pool 228 * is empty 229 * @free: function to be used to free pageory back to the system 230 * when the shrinker fires 231 * @gfp_mask: gfp_mask to use from alloc 232 * @order: order of pages in the pool 233 * @list: plist node for list of pools 234 * 235 * Allows you to keep a pool of pre allocated pages to use from your heap. 236 * Keeping a pool of pages that is ready for dma, ie any cached mapping have 237 * been invalidated from the cache, provides a significant peformance benefit 238 * on many systems 239 */ 240struct ion_page_pool { 241 int high_count; 242 int low_count; 243 struct list_head high_items; 244 struct list_head low_items; 245 struct mutex mutex; 246 void *(*alloc)(struct ion_page_pool *pool); 247 void (*free)(struct ion_page_pool *pool, struct page *page); 248 gfp_t gfp_mask; 249 unsigned int order; 250 struct plist_node list; 251}; 252 253struct ion_page_pool *ion_page_pool_create(gfp_t gfp_mask, unsigned int order); 254void ion_page_pool_destroy(struct ion_page_pool *); 255void *ion_page_pool_alloc(struct ion_page_pool *); 256void ion_page_pool_free(struct ion_page_pool *, struct page *); 257 258#endif /* _ION_PRIV_H */ 259