blob: 10e221981a38f10d30e8a465611a225fdccb3457 [file] [log] [blame]
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001/*
2 * drivers/gpu/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/ion.h>
25
26struct ion_mapping;
27
28struct ion_dma_mapping {
29 struct kref ref;
30 struct scatterlist *sglist;
31};
32
33struct ion_kernel_mapping {
34 struct kref ref;
35 void *vaddr;
36};
37
38struct ion_buffer *ion_handle_buffer(struct ion_handle *handle);
39
40/**
41 * struct ion_buffer - metadata for a particular buffer
42 * @ref: refernce count
43 * @node: node in the ion_device buffers tree
44 * @dev: back pointer to the ion_device
45 * @heap: back pointer to the heap the buffer came from
46 * @flags: buffer specific flags
47 * @size: size of the buffer
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -070048 * @priv_virt: private data to the buffer representable as
49 * a void *
50 * @priv_phys: private data to the buffer representable as
51 * an ion_phys_addr_t (and someday a phys_addr_t)
52 * @lock: protects the buffers cnt fields
53 * @kmap_cnt: number of times the buffer is mapped to the kernel
54 * @vaddr: the kenrel mapping if kmap_cnt is not zero
55 * @dmap_cnt: number of times the buffer is mapped for dma
56 * @sglist: the scatterlist for the buffer is dmap_cnt is not zero
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070057*/
58struct ion_buffer {
59 struct kref ref;
60 struct rb_node node;
61 struct ion_device *dev;
62 struct ion_heap *heap;
63 unsigned long flags;
64 size_t size;
65 union {
66 void *priv_virt;
67 ion_phys_addr_t priv_phys;
68 };
69 struct mutex lock;
70 int kmap_cnt;
71 void *vaddr;
72 int dmap_cnt;
73 struct scatterlist *sglist;
Laura Abbott894fd582011-08-19 13:33:56 -070074 int umap_cnt;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070075};
76
77/**
78 * struct ion_heap_ops - ops to operate on a given heap
79 * @allocate: allocate memory
80 * @free: free memory
81 * @phys get physical address of a buffer (only define on
82 * physically contiguous heaps)
83 * @map_dma map the memory for dma to a scatterlist
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -070084 * @unmap_dma unmap the memory for dma
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070085 * @map_kernel map memory to the kernel
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -070086 * @unmap_kernel unmap memory to the kernel
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070087 * @map_user map memory to userspace
88 */
89struct ion_heap_ops {
90 int (*allocate) (struct ion_heap *heap,
91 struct ion_buffer *buffer, unsigned long len,
92 unsigned long align, unsigned long flags);
93 void (*free) (struct ion_buffer *buffer);
94 int (*phys) (struct ion_heap *heap, struct ion_buffer *buffer,
95 ion_phys_addr_t *addr, size_t *len);
96 struct scatterlist *(*map_dma) (struct ion_heap *heap,
97 struct ion_buffer *buffer);
98 void (*unmap_dma) (struct ion_heap *heap, struct ion_buffer *buffer);
Laura Abbott894fd582011-08-19 13:33:56 -070099 void * (*map_kernel) (struct ion_heap *heap, struct ion_buffer *buffer,
100 unsigned long flags);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700101 void (*unmap_kernel) (struct ion_heap *heap, struct ion_buffer *buffer);
102 int (*map_user) (struct ion_heap *mapper, struct ion_buffer *buffer,
Laura Abbott894fd582011-08-19 13:33:56 -0700103 struct vm_area_struct *vma, unsigned long flags);
Laura Abbottabcb6f72011-10-04 16:26:49 -0700104 int (*cache_op)(struct ion_heap *heap, struct ion_buffer *buffer,
105 void *vaddr, unsigned int offset,
106 unsigned int length, unsigned int cmd);
Laura Abbott68c80642011-10-21 17:32:27 -0700107 unsigned long (*get_allocated)(struct ion_heap *heap);
108 unsigned long (*get_total)(struct ion_heap *heap);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700109};
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
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700117 * @id: id of heap, also indicates priority of this heap when
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700118 * allocating. These are specified by platform data and
119 * MUST be unique
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700120 * @name: used for debugging
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700121 *
122 * Represents a pool of memory from which buffers can be made. In some
123 * systems the only heap is regular system memory allocated via vmalloc.
124 * On others, some blocks might require large physically contiguous buffers
125 * that are allocated from a specially reserved heap.
126 */
127struct ion_heap {
128 struct rb_node node;
129 struct ion_device *dev;
130 enum ion_heap_type type;
131 struct ion_heap_ops *ops;
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700132 int id;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700133 const char *name;
134};
135
136/**
137 * ion_device_create - allocates and returns an ion device
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700138 * @custom_ioctl: arch specific ioctl function if applicable
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700139 *
140 * returns a valid device or -PTR_ERR
141 */
142struct ion_device *ion_device_create(long (*custom_ioctl)
143 (struct ion_client *client,
144 unsigned int cmd,
145 unsigned long arg));
146
147/**
148 * ion_device_destroy - free and device and it's resource
149 * @dev: the device
150 */
151void ion_device_destroy(struct ion_device *dev);
152
153/**
154 * ion_device_add_heap - adds a heap to the ion device
155 * @dev: the device
156 * @heap: the heap to add
157 */
158void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap);
159
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700160/**
161 * functions for creating and destroying the built in ion heaps.
162 * architectures can add their own custom architecture specific
163 * heaps as appropriate.
164 */
165
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700166struct ion_heap *ion_heap_create(struct ion_platform_heap *);
167void ion_heap_destroy(struct ion_heap *);
168
169struct ion_heap *ion_system_heap_create(struct ion_platform_heap *);
170void ion_system_heap_destroy(struct ion_heap *);
171
172struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *);
173void ion_system_contig_heap_destroy(struct ion_heap *);
174
175struct ion_heap *ion_carveout_heap_create(struct ion_platform_heap *);
176void ion_carveout_heap_destroy(struct ion_heap *);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700177/**
178 * kernel api to allocate/free from carveout -- used when carveout is
179 * used to back an architecture specific custom heap
180 */
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700181ion_phys_addr_t ion_carveout_allocate(struct ion_heap *heap, unsigned long size,
182 unsigned long align);
183void ion_carveout_free(struct ion_heap *heap, ion_phys_addr_t addr,
184 unsigned long size);
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -0700185/**
186 * The carveout heap returns physical addresses, since 0 may be a valid
187 * physical address, this is used to indicate allocation failed
188 */
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700189#define ION_CARVEOUT_ALLOCATE_FAIL -1
190
191#endif /* _ION_PRIV_H */