summaryrefslogtreecommitdiff
path: root/drivers/video/tegra/host/nvhost_memmgr.c
blob: 1c633a8bc5af0881978eed51994b7cbf154f7455 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
 * drivers/video/tegra/host/nvhost_memmgr.c
 *
 * Tegra Graphics Host Memory Management Abstraction
 *
 * Copyright (c) 2012, NVIDIA Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <linux/kernel.h>
#include <linux/err.h>

#include "nvhost_memmgr.h"
#ifdef CONFIG_TEGRA_GRHOST_USE_NVMAP
#include "nvmap.h"
#endif
#ifdef CONFIG_TEGRA_GRHOST_USE_DMABUF
#include "dmabuf.h"
#endif
#include "chip_support.h"

struct mem_mgr *nvhost_memmgr_alloc_mgr(void)
{
	struct mem_mgr *mgr = NULL;
#ifdef CONFIG_TEGRA_GRHOST_USE_NVMAP
	mgr = nvhost_nvmap_alloc_mgr();
#else
#ifdef CONFIG_TEGRA_GRHOST_USE_DMABUF
	mgr = (struct mem_mgr)1;
#endif
#endif

	return mgr;
}

void nvhost_memmgr_put_mgr(struct mem_mgr *mgr)
{
#ifdef CONFIG_TEGRA_GRHOST_USE_NVMAP
	nvhost_nvmap_put_mgr(mgr);
#else
#ifdef CONFIG_TEGRA_GRHOST_USE_DMABUF
	mgr = (struct mem_mgr)1;
#endif
#endif
}

struct mem_mgr *nvhost_memmgr_get_mgr(struct mem_mgr *_mgr)
{
	struct mem_mgr *mgr = NULL;
#ifdef CONFIG_TEGRA_GRHOST_USE_NVMAP
	mgr = nvhost_nvmap_get_mgr(_mgr);
#else
#ifdef CONFIG_TEGRA_GRHOST_USE_DMABUF
	mgr = (struct mem_mgr)1;
#endif
#endif

	return mgr;
}

struct mem_mgr *nvhost_memmgr_get_mgr_file(int fd)
{
	struct mem_mgr *mgr = NULL;
#ifdef CONFIG_TEGRA_GRHOST_USE_NVMAP
	mgr = nvhost_nvmap_get_mgr_file(fd);
#else
#ifdef CONFIG_TEGRA_GRHOST_USE_DMABUF
	mgr = (struct mem_mgr)1;
#endif
#endif

	return mgr;
}

struct mem_handle *nvhost_memmgr_alloc(struct mem_mgr *mgr,
		size_t size, size_t align, int flags)
{
	struct mem_handle *h = NULL;
#ifdef CONFIG_TEGRA_GRHOST_USE_NVMAP
	h = nvhost_nvmap_alloc(mgr, size, align, flags);
#else
#ifdef CONFIG_TEGRA_GRHOST_USE_DMABUF
	h = nvhost_dmabuf_alloc(mgr, size, align, flags);
#endif
#endif

	return h;
}

struct mem_handle *nvhost_memmgr_get(struct mem_mgr *mgr,
		u32 id, struct platform_device *dev)
{
	struct mem_handle *h = NULL;

	switch (nvhost_memmgr_type(id)) {
#ifdef CONFIG_TEGRA_GRHOST_USE_NVMAP
	case mem_mgr_type_nvmap:
		h = (struct mem_handle *) nvhost_nvmap_get(mgr, id, dev);
		break;
#endif
#ifdef CONFIG_TEGRA_GRHOST_USE_DMABUF
	case mem_mgr_type_dmabuf:
		h = (struct mem_handle *) nvhost_dmabuf_get(id, dev);
		break;
#endif
	default:
		break;
	}

	return h;
}

void nvhost_memmgr_put(struct mem_mgr *mgr, struct mem_handle *handle)
{
	switch (nvhost_memmgr_type((u32)handle)) {
#ifdef CONFIG_TEGRA_GRHOST_USE_NVMAP
	case mem_mgr_type_nvmap:
		nvhost_nvmap_put(mgr, handle);
		break;
#endif
#ifdef CONFIG_TEGRA_GRHOST_USE_DMABUF
	case mem_mgr_type_dmabuf:
		nvhost_dmabuf_put(handle);
		break;
#endif
	default:
		break;
	}
}

struct sg_table *nvhost_memmgr_pin(struct mem_mgr *mgr,
		struct mem_handle *handle)
{
	switch (nvhost_memmgr_type((u32)handle)) {
#ifdef CONFIG_TEGRA_GRHOST_USE_NVMAP
	case mem_mgr_type_nvmap:
		return nvhost_nvmap_pin(mgr, handle);
		break;
#endif
#ifdef CONFIG_TEGRA_GRHOST_USE_DMABUF
	case mem_mgr_type_dmabuf:
		return nvhost_dmabuf_pin(handle);
		break;
#endif
	default:
		return 0;
		break;
	}
}

void nvhost_memmgr_unpin(struct mem_mgr *mgr,
		struct mem_handle *handle, struct sg_table *sgt)
{
	switch (nvhost_memmgr_type((u32)handle)) {
#ifdef CONFIG_TEGRA_GRHOST_USE_NVMAP
	case mem_mgr_type_nvmap:
		nvhost_nvmap_unpin(mgr, handle, sgt);
		break;
#endif
#ifdef CONFIG_TEGRA_GRHOST_USE_DMABUF
	case mem_mgr_type_dmabuf:
		nvhost_dmabuf_unpin(handle, sgt);
		break;
#endif
	default:
		break;
	}
}

void *nvhost_memmgr_mmap(struct mem_handle *handle)
{
	switch (nvhost_memmgr_type((u32)handle)) {
#ifdef CONFIG_TEGRA_GRHOST_USE_NVMAP
	case mem_mgr_type_nvmap:
		return nvhost_nvmap_mmap(handle);
		break;
#endif
#ifdef CONFIG_TEGRA_GRHOST_USE_DMABUF
	case mem_mgr_type_dmabuf:
		return nvhost_dmabuf_mmap(handle);
		break;
#endif
	default:
		return 0;
		break;
	}
}

void nvhost_memmgr_munmap(struct mem_handle *handle, void *addr)
{
	switch (nvhost_memmgr_type((u32)handle)) {
#ifdef CONFIG_TEGRA_GRHOST_USE_NVMAP
	case mem_mgr_type_nvmap:
		nvhost_nvmap_munmap(handle, addr);
		break;
#endif
#ifdef CONFIG_TEGRA_GRHOST_USE_DMABUF
	case mem_mgr_type_dmabuf:
		nvhost_dmabuf_munmap(handle, addr);
		break;
#endif
	default:
		break;
	}
}

void *nvhost_memmgr_kmap(struct mem_handle *handle, unsigned int pagenum)
{
	switch (nvhost_memmgr_type((u32)handle)) {
#ifdef CONFIG_TEGRA_GRHOST_USE_NVMAP
	case mem_mgr_type_nvmap:
		return nvhost_nvmap_kmap(handle, pagenum);
		break;
#endif
#ifdef CONFIG_TEGRA_GRHOST_USE_DMABUF
	case mem_mgr_type_dmabuf:
		return nvhost_dmabuf_kmap(handle, pagenum);
		break;
#endif
	default:
		return 0;
		break;
	}
}

void nvhost_memmgr_kunmap(struct mem_handle *handle, unsigned int pagenum,
		void *addr)
{
	switch (nvhost_memmgr_type((u32)handle)) {
#ifdef CONFIG_TEGRA_GRHOST_USE_NVMAP
	case mem_mgr_type_nvmap:
		nvhost_nvmap_kunmap(handle, pagenum, addr);
		break;
#endif
#ifdef CONFIG_TEGRA_GRHOST_USE_DMABUF
	case mem_mgr_type_dmabuf:
		nvhost_dmabuf_kunmap(handle, pagenum, addr);
		break;
#endif
	default:
		break;
	}
}

int nvhost_memmgr_pin_array_ids(struct mem_mgr *mgr,
		struct platform_device *dev,
		long unsigned *ids,
		dma_addr_t *phys_addr,
		u32 count,
		struct nvhost_job_unpin *unpin_data)
{
	int pin_count = 0;

#ifdef CONFIG_TEGRA_GRHOST_USE_NVMAP
	{
		int nvmap_count = 0;
		nvmap_count = nvhost_nvmap_pin_array_ids(mgr,
			ids, MEMMGR_TYPE_MASK,
			mem_mgr_type_nvmap,
			count, unpin_data,
			phys_addr);
		if (nvmap_count < 0)
			return nvmap_count;
		pin_count += nvmap_count;
	}
#endif
#ifdef CONFIG_TEGRA_GRHOST_USE_DMABUF
	{
		int dmabuf_count = 0;
		dmabuf_count = nvhost_dmabuf_pin_array_ids(dev,
			ids, MEMMGR_TYPE_MASK,
			mem_mgr_type_dmabuf,
			count, &unpin_data[pin_count],
			phys_addr);

		if (dmabuf_count < 0) {
			/* clean up previous handles */
			while (pin_count) {
				pin_count--;
				/* unpin, put */
				nvhost_memmgr_unpin(mgr,
						unpin_data[pin_count].h,
						unpin_data[pin_count].mem);
				nvhost_memmgr_put(mgr,
						unpin_data[pin_count].h);
			}
			return dmabuf_count;
		}
		pin_count += dmabuf_count;
	}
#endif
	return pin_count;
}

static const struct nvhost_mem_ops mem_ops = {
	.alloc_mgr = nvhost_memmgr_alloc_mgr,
	.put_mgr = nvhost_memmgr_put_mgr,
	.get_mgr = nvhost_memmgr_get_mgr,
	.get_mgr_file = nvhost_memmgr_get_mgr_file,
	.alloc = nvhost_memmgr_alloc,
	.get = nvhost_memmgr_get,
	.put = nvhost_memmgr_put,
	.pin = nvhost_memmgr_pin,
	.unpin = nvhost_memmgr_unpin,
	.mmap = nvhost_memmgr_mmap,
	.munmap = nvhost_memmgr_munmap,
	.kmap = nvhost_memmgr_kmap,
	.kunmap = nvhost_memmgr_kunmap,
	.pin_array_ids = nvhost_memmgr_pin_array_ids,
};

int nvhost_memmgr_init(struct nvhost_chip_support *chip)
{
	chip->mem = mem_ops;
	return 0;
}