ion_priv.h revision 13ba7805f9bf710016ffde5e24437fd6e5a798dc
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
26#include "ion.h"
27
28struct ion_buffer *ion_handle_buffer(struct ion_handle *handle);
29
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*/
57struct ion_buffer {
58	struct kref ref;
59	struct rb_node node;
60	struct ion_device *dev;
61	struct ion_heap *heap;
62	unsigned long flags;
63	size_t size;
64	union {
65		void *priv_virt;
66		ion_phys_addr_t priv_phys;
67	};
68	struct mutex lock;
69	int kmap_cnt;
70	void *vaddr;
71	int dmap_cnt;
72	struct sg_table *sg_table;
73	unsigned long *dirty;
74	struct list_head vmas;
75	/* used to track orphaned buffers */
76	int handle_count;
77	char task_comm[TASK_COMM_LEN];
78	pid_t pid;
79};
80
81/**
82 * struct ion_heap_ops - ops to operate on a given heap
83 * @allocate:		allocate memory
84 * @free:		free memory
85 * @phys		get physical address of a buffer (only define on
86 *			physically contiguous heaps)
87 * @map_dma		map the memory for dma to a scatterlist
88 * @unmap_dma		unmap the memory for dma
89 * @map_kernel		map memory to the kernel
90 * @unmap_kernel	unmap memory to the kernel
91 * @map_user		map memory to userspace
92 */
93struct ion_heap_ops {
94	int (*allocate) (struct ion_heap *heap,
95			 struct ion_buffer *buffer, unsigned long len,
96			 unsigned long align, unsigned long flags);
97	void (*free) (struct ion_buffer *buffer);
98	int (*phys) (struct ion_heap *heap, struct ion_buffer *buffer,
99		     ion_phys_addr_t *addr, size_t *len);
100	struct sg_table *(*map_dma) (struct ion_heap *heap,
101					struct ion_buffer *buffer);
102	void (*unmap_dma) (struct ion_heap *heap, struct ion_buffer *buffer);
103	void * (*map_kernel) (struct ion_heap *heap, struct ion_buffer *buffer);
104	void (*unmap_kernel) (struct ion_heap *heap, struct ion_buffer *buffer);
105	int (*map_user) (struct ion_heap *mapper, struct ion_buffer *buffer,
106			 struct vm_area_struct *vma);
107};
108
109/**
110 * struct ion_heap - represents a heap in the system
111 * @node:		rb node to put the heap on the device's tree of heaps
112 * @dev:		back pointer to the ion_device
113 * @type:		type of heap
114 * @ops:		ops struct as above
115 * @id:			id of heap, also indicates priority of this heap when
116 *			allocating.  These are specified by platform data and
117 *			MUST be unique
118 * @name:		used for debugging
119 *
120 * Represents a pool of memory from which buffers can be made.  In some
121 * systems the only heap is regular system memory allocated via vmalloc.
122 * On others, some blocks might require large physically contiguous buffers
123 * that are allocated from a specially reserved heap.
124 */
125struct ion_heap {
126	struct rb_node node;
127	struct ion_device *dev;
128	enum ion_heap_type type;
129	struct ion_heap_ops *ops;
130	int id;
131	const char *name;
132};
133
134/**
135 * ion_buffer_fault_user_mappings - fault in user mappings of this buffer
136 * @buffer:		buffer
137 *
138 * indicates whether userspace mappings of this buffer will be faulted
139 * in, this can affect how buffers are allocated from the heap.
140 */
141bool ion_buffer_fault_user_mappings(struct ion_buffer *buffer);
142
143/**
144 * ion_device_create - allocates and returns an ion device
145 * @custom_ioctl:	arch specific ioctl function if applicable
146 *
147 * returns a valid device or -PTR_ERR
148 */
149struct ion_device *ion_device_create(long (*custom_ioctl)
150				     (struct ion_client *client,
151				      unsigned int cmd,
152				      unsigned long arg));
153
154/**
155 * ion_device_destroy - free and device and it's resource
156 * @dev:		the device
157 */
158void ion_device_destroy(struct ion_device *dev);
159
160/**
161 * ion_device_add_heap - adds a heap to the ion device
162 * @dev:		the device
163 * @heap:		the heap to add
164 */
165void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap);
166
167/**
168 * functions for creating and destroying the built in ion heaps.
169 * architectures can add their own custom architecture specific
170 * heaps as appropriate.
171 */
172
173struct ion_heap *ion_heap_create(struct ion_platform_heap *);
174void ion_heap_destroy(struct ion_heap *);
175
176struct ion_heap *ion_system_heap_create(struct ion_platform_heap *);
177void ion_system_heap_destroy(struct ion_heap *);
178
179struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *);
180void ion_system_contig_heap_destroy(struct ion_heap *);
181
182struct ion_heap *ion_carveout_heap_create(struct ion_platform_heap *);
183void ion_carveout_heap_destroy(struct ion_heap *);
184/**
185 * kernel api to allocate/free from carveout -- used when carveout is
186 * used to back an architecture specific custom heap
187 */
188ion_phys_addr_t ion_carveout_allocate(struct ion_heap *heap, unsigned long size,
189				      unsigned long align);
190void ion_carveout_free(struct ion_heap *heap, ion_phys_addr_t addr,
191		       unsigned long size);
192/**
193 * The carveout heap returns physical addresses, since 0 may be a valid
194 * physical address, this is used to indicate allocation failed
195 */
196#define ION_CARVEOUT_ALLOCATE_FAIL -1
197
198#endif /* _ION_PRIV_H */
199