aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/tiler/tiler-iface.c
blob: 3d5917f3fece410145c8b65fc00a2d8698ec4734 (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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
 * tiler-iface.c
 *
 * TILER driver interace functions for TI TILER hardware block.
 *
 * Authors: Lajos Molnar <molnar@ti.com>
 *          David Sin <davidsin@ti.com>
 *
 * Copyright (C) 2009-2010 Texas Instruments, Inc.
 *
 * This package is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/fs.h>		/* fops */
#include <linux/uaccess.h>	/* copy_to_user */
#include <linux/slab.h>		/* kmalloc */
#include <linux/sched.h>	/* current */
#include <linux/mm.h>
#include <linux/mm_types.h>
#include <asm/mach/map.h>	/* for ioremap_page */

#include "_tiler.h"

static bool security = CONFIG_TILER_SECURITY;

module_param(security, bool, 0644);
MODULE_PARM_DESC(security,
	"Separate allocations by different security ids (pid or token)");

static struct list_head procs;	/* list of process info structs */
static struct tiler_ops *ops;	/* shared methods and variables */

/*
 *  security_info handling methods
 *  ==========================================================================
 */

/* get security info, and increment refs for device tracking */
struct security_info *__get_si(int token, bool kernel,
				enum secure_id_type sec_type)
{
	struct security_info *si;

	/*
	 * treat all processes as the same, kernel processes are still treated
	 * differently so not to free kernel allocated areas when a user process
	 * closes the tiler driver
	 */
	if (!security)
		token = 0;

	/* find process context */
	mutex_lock(&ops->mtx);
	list_for_each_entry(si, &procs, list) {
		if (si->token == token &&
			si->kernel == kernel &&
			si->sec_type == sec_type)
			goto done;
	}

	/* create process context */
	si = kmalloc(sizeof(*si), GFP_KERNEL);
	if (!si)
		goto done;
	memset(si, 0, sizeof(*si));

	si->token = token;
	si->sec_type = sec_type;
	si->kernel = kernel;
	INIT_LIST_HEAD(&si->groups);
	INIT_LIST_HEAD(&si->bufs);
	list_add(&si->list, &procs);
done:
	/* increment reference count */
	if (si && !kernel)
		si->refs++;
	mutex_unlock(&ops->mtx);
	return si;
}

/**
 * Free all info kept by a process: all registered buffers, allocated blocks,
 * and unreferenced blocks.  Any blocks/areas still referenced will move to the
 * orphaned lists to avoid issues if a new process is created with the same pid.
 *
 *    caller MUST already have mtx
 */
void _m_free_security_info(struct security_info *si)
{
	struct gid_info *gi, *gi_;
#ifdef CONFIG_TILER_ENABLE_USERSPACE
	struct __buf_info *_b = NULL, *_b_ = NULL;

	if (!list_empty(&si->bufs))
		tiler_notify_event(TILER_DEVICE_CLOSE, NULL);

	/* unregister all buffers */
	list_for_each_entry_safe(_b, _b_, &si->bufs, by_sid)
		_m_unregister_buf(_b);
#endif
	BUG_ON(!list_empty(&si->bufs));

	/* free all allocated blocks, and remove unreferenced ones */
	list_for_each_entry_safe(gi, gi_, &si->groups, by_sid)
		ops->destroy_group(gi);

	BUG_ON(!list_empty(&si->groups));
	list_del(&si->list);
	kfree(si);
}

static void destroy_processes(void)
{
	struct security_info *si, *si_;

	mutex_lock(&ops->mtx);

	list_for_each_entry_safe(si, si_, &procs, list)
		_m_free_security_info(si);
	BUG_ON(!list_empty(&procs));

	mutex_unlock(&ops->mtx);
}


/* initialize tiler interface */
void tiler_iface_init(struct tiler_ops *tiler)
{
	ops = tiler;
	ops->cleanup = destroy_processes;

#ifdef CONFIG_TILER_SECURE
	security = true;
#endif
	INIT_LIST_HEAD(&procs);
}

/*
 *  Kernel APIs
 *  ==========================================================================
 */

u32 tiler_virt2phys(u32 usr)
{
	pmd_t *pmd;
	pte_t *ptep;
	pgd_t *pgd = pgd_offset(current->mm, usr);

	if (pgd_none(*pgd) || pgd_bad(*pgd))
		return 0;

	pmd = pmd_offset(pgd, usr);
	if (pmd_none(*pmd) || pmd_bad(*pmd))
		return 0;

	ptep = pte_offset_map(pmd, usr);
	if (ptep && pte_present(*ptep))
		return (*ptep & PAGE_MASK) | (~PAGE_MASK & usr);

	return 0;
}
EXPORT_SYMBOL(tiler_virt2phys);

void tiler_reservex(u32 n, enum tiler_fmt fmt, u32 width, u32 height,
		   u32 gid, pid_t pid)
{
	struct security_info *si = __get_si(pid, true, SECURE_BY_PID);

	if (si)
		ops->reserve(n, fmt, width, height, PAGE_SIZE, 0, gid, si);
}
EXPORT_SYMBOL(tiler_reservex);

void tiler_reserve(u32 n, enum tiler_fmt fmt, u32 width, u32 height)
{
	tiler_reservex(n, fmt, width, height, 0, current->tgid);
}
EXPORT_SYMBOL(tiler_reserve);

#ifdef CONFIG_TILER_ENABLE_NV12
void tiler_reservex_nv12(u32 n, u32 width, u32 height,
			u32 gid, pid_t pid)
{
	struct security_info *si = __get_si(0, true, SECURE_BY_PID);

	if (si)
		ops->reserve_nv12(n, width, height, gid, si);
}
EXPORT_SYMBOL(tiler_reservex_nv12);

void tiler_reserve_nv12(u32 n, u32 width, u32 height)
{
	tiler_reservex_nv12(n, width, height, 0, current->tgid);
}
EXPORT_SYMBOL(tiler_reserve_nv12);
#endif

s32 tiler_allocx(struct tiler_block_t *blk, enum tiler_fmt fmt,
				u32 gid, pid_t pid)
{
	struct mem_info *mi;
	struct security_info *si;
	s32 res;

	BUG_ON(!blk || blk->phys);

	si = __get_si(pid, true, SECURE_BY_PID);
	if (!si)
		return -ENOMEM;

	res = ops->alloc(fmt, blk->width, blk->height, blk->key, gid, si, &mi);
	if (mi) {
		blk->phys = mi->blk.phys;
		blk->id = mi->blk.id;
	}
	return res;
}
EXPORT_SYMBOL(tiler_allocx);

s32 tiler_alloc(struct tiler_block_t *blk, enum tiler_fmt fmt)
{
	return tiler_allocx(blk, fmt, 0, current->tgid);
}
EXPORT_SYMBOL(tiler_alloc);

s32 tiler_mapx(struct tiler_block_t *blk, enum tiler_fmt fmt, u32 gid,
				pid_t pid, u32 usr_addr)
{
	struct mem_info *mi;
	struct security_info *si;
	s32 res;

	BUG_ON(!blk || blk->phys);

	si = __get_si(pid, true, SECURE_BY_PID);
	if (!si)
		return -ENOMEM;

	res = ops->pin(fmt, blk->width, blk->height, blk->key, gid, si, &mi,
								usr_addr);
	if (mi) {
		blk->phys = mi->blk.phys;
		blk->id = mi->blk.id;
	}
	return res;

}
EXPORT_SYMBOL(tiler_mapx);

s32 tiler_map(struct tiler_block_t *blk, enum tiler_fmt fmt, u32 usr_addr)
{
	return tiler_mapx(blk, fmt, 0, current->tgid, usr_addr);
}
EXPORT_SYMBOL(tiler_map);

s32 tiler_mmap_blk(struct tiler_block_t *blk, u32 offs, u32 size,
				struct vm_area_struct *vma, u32 voffs)
{
	u32 v, p, len;

	/* mapping must fit into vma */
	BUG_ON(vma->vm_start > vma->vm_start + voffs ||
		vma->vm_start + voffs > vma->vm_start + voffs + size ||
		vma->vm_start + voffs + size > vma->vm_end);

	/* mapping must fit into block */
	BUG_ON(offs > offs + size || offs + size > tiler_size(blk));

	v = tiler_vstride(blk);
	p = tiler_pstride(blk);

	/* remap block portion */
	len = v - (offs % v);	/* initial area to map */
	while (size) {
		/* restrict to size still needs mapping */
		if (len > size)
			len = size;

		vma->vm_pgoff = (blk->phys + offs) >> PAGE_SHIFT;
		if (remap_pfn_range(vma, vma->vm_start + voffs, vma->vm_pgoff,
				    len, vma->vm_page_prot))
			return -EAGAIN;
		voffs += len;
		offs += len + p - v;
		size -= len;
		len = v;	/* subsequent area to map */
	}
	return 0;
}
EXPORT_SYMBOL(tiler_mmap_blk);

s32 tiler_ioremap_blk(struct tiler_block_t *blk, u32 offs, u32 size,
				u32 addr, u32 mtype)
{
	u32 v, p;
	u32 len;		/* area to map */
	const struct mem_type *type = get_mem_type(mtype);

	/* mapping must fit into address space */
	BUG_ON(addr > addr + size);

	/* mapping must fit into block */
	BUG_ON(offs > offs + size || offs + size > tiler_size(blk));

	v = tiler_vstride(blk);
	p = tiler_pstride(blk);

	/* move offset and address to end */
	offs += blk->phys + size;
	addr += size;

	len = v - (offs % v);	/* initial area to map */
	while (size) {
		while (len && size) {
			if (ioremap_page(addr - size, offs - size, type))
				return -EAGAIN;
			len  -= PAGE_SIZE;
			size -= PAGE_SIZE;
		}

		offs += p - v;
		len = v;	/* subsequent area to map */
	}
	return 0;
}
EXPORT_SYMBOL(tiler_ioremap_blk);

void tiler_free(struct tiler_block_t *blk)
{
	/* find block */
	struct mem_info *mi = ops->lock(blk->key, blk->id, NULL);
	if (mi)
		ops->unlock_free(mi, true);
	blk->phys = blk->id = 0;
}
EXPORT_SYMBOL(tiler_free);