1/*
2 *
3 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
4 *
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 */
15
16#ifndef _LINUX_MSM_ION_H
17#define _LINUX_MSM_ION_H
18
19#include <linux/ion.h>
20
21#define ION_HEAP_TYPE_MSM_START (ION_HEAP_TYPE_CUSTOM + 1)
22#define ION_HEAP_TYPE_IOMMU	(ION_HEAP_TYPE_MSM_START)
23#define ION_HEAP_TYPE_CP	(ION_HEAP_TYPE_IOMMU + 1)
24
25/**
26 * These are the only ids that should be used for Ion heap ids.
27 * The ids listed are the order in which allocation will be attempted
28 * if specified. Don't swap the order of heap ids unless you know what
29 * you are doing!
30 * Id's are spaced by purpose to allow new Id's to be inserted in-between (for
31 * possible fallbacks)
32 */
33
34enum ion_heap_ids {
35	INVALID_HEAP_ID = -1,
36	ION_CP_MM_HEAP_ID = 8,
37	ION_CP_MFC_HEAP_ID = 12,
38	ION_CP_WB_HEAP_ID = 16, /* 8660 only */
39	ION_CAMERA_HEAP_ID = 20, /* 8660 only */
40	ION_ADSP_HEAP_ID = 22,
41	ION_SF_HEAP_ID = 24,
42	ION_IOMMU_HEAP_ID = 25,
43	ION_QSECOM_HEAP_ID = 27,
44	ION_AUDIO_HEAP_ID = 28,
45
46	ION_MM_FIRMWARE_HEAP_ID = 29,
47	ION_SYSTEM_HEAP_ID = 30,
48
49	ION_HEAP_ID_RESERVED = 31 /** Bit reserved for ION_SECURE flag */
50};
51
52enum ion_fixed_position {
53	NOT_FIXED,
54	FIXED_LOW,
55	FIXED_MIDDLE,
56	FIXED_HIGH,
57};
58
59enum cp_mem_usage {
60	VIDEO_BITSTREAM = 0x1,
61	VIDEO_PIXEL = 0x2,
62	VIDEO_NONPIXEL = 0x3,
63	MAX_USAGE = 0x4,
64	UNKNOWN = 0x7FFFFFFF,
65};
66
67#define ION_HEAP_CP_MASK		(1 << ION_HEAP_TYPE_CP)
68
69/**
70 * Flag to use when allocating to indicate that a heap is secure.
71 */
72#define ION_SECURE (1 << ION_HEAP_ID_RESERVED)
73
74/**
75 * Flag for clients to force contiguous memort allocation
76 *
77 * Use of this flag is carefully monitored!
78 */
79#define ION_FORCE_CONTIGUOUS (1 << 30)
80
81/**
82 * Macro should be used with ion_heap_ids defined above.
83 */
84#define ION_HEAP(bit) (1 << (bit))
85
86#define ION_ADSP_HEAP_NAME	"adsp"
87#define ION_VMALLOC_HEAP_NAME	"vmalloc"
88#define ION_AUDIO_HEAP_NAME	"audio"
89#define ION_SF_HEAP_NAME	"sf"
90#define ION_MM_HEAP_NAME	"mm"
91#define ION_CAMERA_HEAP_NAME	"camera_preview"
92#define ION_IOMMU_HEAP_NAME	"iommu"
93#define ION_MFC_HEAP_NAME	"mfc"
94#define ION_WB_HEAP_NAME	"wb"
95#define ION_MM_FIRMWARE_HEAP_NAME	"mm_fw"
96#define ION_QSECOM_HEAP_NAME	"qsecom"
97#define ION_FMEM_HEAP_NAME	"fmem"
98
99#define ION_SET_CACHED(__cache)		(__cache | ION_FLAG_CACHED)
100#define ION_SET_UNCACHED(__cache)	(__cache & ~ION_FLAG_CACHED)
101
102#define ION_IS_CACHED(__flags)	((__flags) & ION_FLAG_CACHED)
103
104#ifdef __KERNEL__
105
106/*
107 * This flag allows clients when mapping into the IOMMU to specify to
108 * defer un-mapping from the IOMMU until the buffer memory is freed.
109 */
110#define ION_IOMMU_UNMAP_DELAYED 1
111
112/**
113 * struct ion_cp_heap_pdata - defines a content protection heap in the given
114 * platform
115 * @permission_type:	Memory ID used to identify the memory to TZ
116 * @align:		Alignment requirement for the memory
117 * @secure_base:	Base address for securing the heap.
118 *			Note: This might be different from actual base address
119 *			of this heap in the case of a shared heap.
120 * @secure_size:	Memory size for securing the heap.
121 *			Note: This might be different from actual size
122 *			of this heap in the case of a shared heap.
123 * @reusable		Flag indicating whether this heap is reusable of not.
124 *			(see FMEM)
125 * @mem_is_fmem		Flag indicating whether this memory is coming from fmem
126 *			or not.
127 * @fixed_position	If nonzero, position in the fixed area.
128 * @virt_addr:		Virtual address used when using fmem.
129 * @iommu_map_all:	Indicates whether we should map whole heap into IOMMU.
130 * @iommu_2x_map_domain: Indicates the domain to use for overmapping.
131 * @request_region:	function to be called when the number of allocations
132 *			goes from 0 -> 1
133 * @release_region:	function to be called when the number of allocations
134 *			goes from 1 -> 0
135 * @setup_region:	function to be called upon ion registration
136 * @memory_type:Memory type used for the heap
137 * @no_nonsecure_alloc: don't allow non-secure allocations from this heap
138 *
139 */
140struct ion_cp_heap_pdata {
141	enum ion_permission_type permission_type;
142	unsigned int align;
143	ion_phys_addr_t secure_base; /* Base addr used when heap is shared */
144	size_t secure_size; /* Size used for securing heap when heap is shared*/
145	int reusable;
146	int mem_is_fmem;
147	int is_cma;
148	enum ion_fixed_position fixed_position;
149	int iommu_map_all;
150	int iommu_2x_map_domain;
151	ion_virt_addr_t *virt_addr;
152	int (*request_region)(void *);
153	int (*release_region)(void *);
154	void *(*setup_region)(void);
155	enum ion_memory_types memory_type;
156	int no_nonsecure_alloc;
157};
158
159/**
160 * struct ion_co_heap_pdata - defines a carveout heap in the given platform
161 * @adjacent_mem_id:	Id of heap that this heap must be adjacent to.
162 * @align:		Alignment requirement for the memory
163 * @mem_is_fmem		Flag indicating whether this memory is coming from fmem
164 *			or not.
165 * @fixed_position	If nonzero, position in the fixed area.
166 * @request_region:	function to be called when the number of allocations
167 *			goes from 0 -> 1
168 * @release_region:	function to be called when the number of allocations
169 *			goes from 1 -> 0
170 * @setup_region:	function to be called upon ion registration
171 * @memory_type:Memory type used for the heap
172 *
173 */
174struct ion_co_heap_pdata {
175	int adjacent_mem_id;
176	unsigned int align;
177	int mem_is_fmem;
178	enum ion_fixed_position fixed_position;
179	int (*request_region)(void *);
180	int (*release_region)(void *);
181	void *(*setup_region)(void);
182	enum ion_memory_types memory_type;
183};
184
185#ifdef CONFIG_ION
186/**
187 * msm_ion_secure_heap - secure a heap. Wrapper around ion_secure_heap.
188 *
189  * @heap_id - heap id to secure.
190 *
191 * Secure a heap
192 * Returns 0 on success
193 */
194int msm_ion_secure_heap(int heap_id);
195
196/**
197 * msm_ion_unsecure_heap - unsecure a heap. Wrapper around ion_unsecure_heap.
198 *
199  * @heap_id - heap id to secure.
200 *
201 * Un-secure a heap
202 * Returns 0 on success
203 */
204int msm_ion_unsecure_heap(int heap_id);
205
206/**
207 * msm_ion_secure_heap_2_0 - secure a heap using 2.0 APIs
208 *  Wrapper around ion_secure_heap.
209 *
210 * @heap_id - heap id to secure.
211 * @usage - usage hint to TZ
212 *
213 * Secure a heap
214 * Returns 0 on success
215 */
216int msm_ion_secure_heap_2_0(int heap_id, enum cp_mem_usage usage);
217
218/**
219 * msm_ion_unsecure_heap - unsecure a heap secured with 3.0 APIs.
220 * Wrapper around ion_unsecure_heap.
221 *
222 * @heap_id - heap id to secure.
223 * @usage - usage hint to TZ
224 *
225 * Un-secure a heap
226 * Returns 0 on success
227 */
228int msm_ion_unsecure_heap_2_0(int heap_id, enum cp_mem_usage usage);
229#else
230static inline int msm_ion_secure_heap(int heap_id)
231{
232	return -ENODEV;
233
234}
235
236static inline int msm_ion_unsecure_heap(int heap_id)
237{
238	return -ENODEV;
239}
240
241static inline int msm_ion_secure_heap_2_0(int heap_id, enum cp_mem_usage usage)
242{
243	return -ENODEV;
244}
245
246static inline int msm_ion_unsecure_heap_2_0(int heap_id,
247					enum cp_mem_usage usage)
248{
249	return -ENODEV;
250}
251#endif /* CONFIG_ION */
252
253#endif /* __KERNEL */
254
255/* struct ion_flush_data - data passed to ion for flushing caches
256 *
257 * @handle:	handle with data to flush
258 * @fd:		fd to flush
259 * @vaddr:	userspace virtual address mapped with mmap
260 * @offset:	offset into the handle to flush
261 * @length:	length of handle to flush
262 *
263 * Performs cache operations on the handle. If p is the start address
264 * of the handle, p + offset through p + offset + length will have
265 * the cache operations performed
266 */
267struct ion_flush_data {
268	struct ion_handle *handle;
269	int fd;
270	void *vaddr;
271	unsigned int offset;
272	unsigned int length;
273};
274
275/* struct ion_flag_data - information about flags for this buffer
276 *
277 * @handle:	handle to get flags from
278 * @flags:	flags of this handle
279 *
280 * Takes handle as an input and outputs the flags from the handle
281 * in the flag field.
282 */
283struct ion_flag_data {
284	struct ion_handle *handle;
285	unsigned long flags;
286};
287
288#define ION_IOC_MSM_MAGIC 'M'
289
290/**
291 * DOC: ION_IOC_CLEAN_CACHES - clean the caches
292 *
293 * Clean the caches of the handle specified.
294 */
295#define ION_IOC_CLEAN_CACHES	_IOWR(ION_IOC_MSM_MAGIC, 0, \
296						struct ion_flush_data)
297/**
298 * DOC: ION_IOC_INV_CACHES - invalidate the caches
299 *
300 * Invalidate the caches of the handle specified.
301 */
302#define ION_IOC_INV_CACHES	_IOWR(ION_IOC_MSM_MAGIC, 1, \
303						struct ion_flush_data)
304/**
305 * DOC: ION_IOC_CLEAN_INV_CACHES - clean and invalidate the caches
306 *
307 * Clean and invalidate the caches of the handle specified.
308 */
309#define ION_IOC_CLEAN_INV_CACHES	_IOWR(ION_IOC_MSM_MAGIC, 2, \
310						struct ion_flush_data)
311
312/**
313 * DOC: ION_IOC_GET_FLAGS - get the flags of the handle
314 *
315 * Gets the flags of the current handle which indicate cachability,
316 * secure state etc.
317 */
318#define ION_IOC_GET_FLAGS		_IOWR(ION_IOC_MSM_MAGIC, 3, \
319						struct ion_flag_data)
320
321#endif
322