summaryrefslogtreecommitdiff
path: root/drivers/misc/hwmem/hwmem-ioctl.c
blob: e9e50de78bdafdbd2ea2adc051722565cc47b3e1 (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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/*
 * Copyright (C) ST-Ericsson SA 2010
 *
 * Hardware memory driver, hwmem
 *
 * Author: Marcus Lorentzon <marcus.xm.lorentzon@stericsson.com>
 * for ST-Ericsson.
 *
 * License terms: GNU General Public License (GPL), version 2.
 */

#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/idr.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <linux/miscdevice.h>
#include <linux/uaccess.h>
#include <linux/mm_types.h>
#include <linux/hwmem.h>
#include <linux/device.h>
#include <linux/sched.h>

static int hwmem_open(struct inode *inode, struct file *file);
static int hwmem_ioctl_mmap(struct file *file, struct vm_area_struct *vma);
static int hwmem_release_fop(struct inode *inode, struct file *file);
static long hwmem_ioctl(struct file *file, unsigned int cmd,
	unsigned long arg);
static unsigned long hwmem_get_unmapped_area(struct file *file,
	unsigned long addr, unsigned long len, unsigned long pgoff,
	unsigned long flags);

static const struct file_operations hwmem_fops = {
	.open = hwmem_open,
	.mmap = hwmem_ioctl_mmap,
	.unlocked_ioctl = hwmem_ioctl,
	.release = hwmem_release_fop,
	.get_unmapped_area = hwmem_get_unmapped_area,
};

static struct miscdevice hwmem_device = {
	.minor = MISC_DYNAMIC_MINOR,
	.name = "hwmem",
	.fops = &hwmem_fops,
};

struct hwmem_file {
	struct mutex lock;
	struct idr idr; /* id -> struct hwmem_alloc*, ref counted */
	struct hwmem_alloc *fd_alloc; /* Ref counted */
};

static s32 create_id(struct hwmem_file *hwfile, struct hwmem_alloc *alloc)
{
	int id, ret;

	while (true) {
		if (idr_pre_get(&hwfile->idr, GFP_KERNEL) == 0)
			return -ENOMEM;

		ret = idr_get_new_above(&hwfile->idr, alloc, 1, &id);
		if (ret == 0)
			break;
		else if (ret != -EAGAIN)
			return -ENOMEM;
	}

	/*
	 * IDR always returns the lowest free id so there is no wrapping issue
	 * because of this.
	 */
	if (id >= (s32)1 << (31 - PAGE_SHIFT)) {
		dev_err(hwmem_device.this_device, "Out of IDs!\n");
		idr_remove(&hwfile->idr, id);
		return -ENOMSG;
	}

	return (s32)id << PAGE_SHIFT;
}

static void remove_id(struct hwmem_file *hwfile, s32 id)
{
	idr_remove(&hwfile->idr, id >> PAGE_SHIFT);
}

static struct hwmem_alloc *resolve_id(struct hwmem_file *hwfile, s32 id)
{
	struct hwmem_alloc *alloc;

	alloc = id ? idr_find(&hwfile->idr, id >> PAGE_SHIFT) :
							hwfile->fd_alloc;
	if (alloc == NULL)
		alloc = ERR_PTR(-EINVAL);

	return alloc;
}

static s32 alloc(struct hwmem_file *hwfile, struct hwmem_alloc_request *req)
{
	s32 ret = 0;
	struct hwmem_alloc *alloc;

	alloc = hwmem_alloc(req->size, req->flags, req->default_access,
								req->mem_type);
	if (IS_ERR(alloc))
		return PTR_ERR(alloc);

	ret = create_id(hwfile, alloc);
	if (ret < 0)
		hwmem_release(alloc);

	return ret;
}

static int alloc_fd(struct hwmem_file *hwfile, struct hwmem_alloc_request *req)
{
	struct hwmem_alloc *alloc;

	if (hwfile->fd_alloc)
		return -EINVAL;

	alloc = hwmem_alloc(req->size, req->flags, req->default_access,
								req->mem_type);
	if (IS_ERR(alloc))
		return PTR_ERR(alloc);

	hwfile->fd_alloc = alloc;

	return 0;
}

static int release(struct hwmem_file *hwfile, s32 id)
{
	struct hwmem_alloc *alloc;

	if (id == 0)
		return -EINVAL;

	alloc = resolve_id(hwfile, id);
	if (IS_ERR(alloc))
		return PTR_ERR(alloc);

	remove_id(hwfile, id);
	hwmem_release(alloc);

	return 0;
}

static int set_cpu_domain(struct hwmem_file *hwfile,
					struct hwmem_set_domain_request *req)
{
	struct hwmem_alloc *alloc;

	alloc = resolve_id(hwfile, req->id);
	if (IS_ERR(alloc))
		return PTR_ERR(alloc);

	return hwmem_set_domain(alloc, req->access, HWMEM_DOMAIN_CPU,
					(struct hwmem_region *)&req->region);
}

static int set_sync_domain(struct hwmem_file *hwfile,
					struct hwmem_set_domain_request *req)
{
	struct hwmem_alloc *alloc;

	alloc = resolve_id(hwfile, req->id);
	if (IS_ERR(alloc))
		return PTR_ERR(alloc);

	return hwmem_set_domain(alloc, req->access, HWMEM_DOMAIN_SYNC,
					(struct hwmem_region *)&req->region);
}

static int pin(struct hwmem_file *hwfile, struct hwmem_pin_request *req)
{
	int ret;
	struct hwmem_alloc *alloc;
	enum hwmem_mem_type mem_type;
	struct hwmem_mem_chunk mem_chunk;
	size_t mem_chunk_length = 1;

	alloc = resolve_id(hwfile, req->id);
	if (IS_ERR(alloc))
		return PTR_ERR(alloc);

	hwmem_get_info(alloc, NULL, &mem_type, NULL);
	if (mem_type != HWMEM_MEM_CONTIGUOUS_SYS)
		return -EINVAL;

	ret = hwmem_pin(alloc, &mem_chunk, &mem_chunk_length);
	if (ret < 0)
		return ret;

	req->phys_addr = mem_chunk.paddr;

	return 0;
}

static int unpin(struct hwmem_file *hwfile, s32 id)
{
	struct hwmem_alloc *alloc;

	alloc = resolve_id(hwfile, id);
	if (IS_ERR(alloc))
		return PTR_ERR(alloc);

	hwmem_unpin(alloc);

	return 0;
}

static int set_access(struct hwmem_file *hwfile,
		struct hwmem_set_access_request *req)
{
	struct hwmem_alloc *alloc;

	alloc = resolve_id(hwfile, req->id);
	if (IS_ERR(alloc))
		return PTR_ERR(alloc);

	return hwmem_set_access(alloc, req->access, req->pid);
}

static int get_info(struct hwmem_file *hwfile,
		struct hwmem_get_info_request *req)
{
	struct hwmem_alloc *alloc;

	alloc = resolve_id(hwfile, req->id);
	if (IS_ERR(alloc))
		return PTR_ERR(alloc);

	hwmem_get_info(alloc, &req->size, &req->mem_type, &req->access);

	return 0;
}

static s32 export(struct hwmem_file *hwfile, s32 id)
{
	s32 ret;
	struct hwmem_alloc *alloc;
	enum hwmem_access access;

	alloc = resolve_id(hwfile, id);
	if (IS_ERR(alloc))
		return PTR_ERR(alloc);

	/*
	 * The user could be about to send the buffer to a driver but
	 * there is a chance the current thread group don't have import rights
	 * if it gained access to the buffer via a inter-process fd transfer
	 * (fork, Android binder), if this is the case the driver will not be
	 * able to resolve the buffer name. To avoid this situation we give the
	 * current thread group import rights. This will not breach the
	 * security as the process already has access to the buffer (otherwise
	 * it would not be able to get here).
	 */
	hwmem_get_info(alloc, NULL, NULL, &access);

	ret = hwmem_set_access(alloc, (access | HWMEM_ACCESS_IMPORT),
							task_tgid_nr(current));
	if (ret < 0)
		return ret;

	return hwmem_get_name(alloc);
}

static s32 import(struct hwmem_file *hwfile, s32 name)
{
	s32 ret = 0;
	struct hwmem_alloc *alloc;
	enum hwmem_access access;

	alloc = hwmem_resolve_by_name(name);
	if (IS_ERR(alloc))
		return PTR_ERR(alloc);

	/* Check access permissions for process */
	hwmem_get_info(alloc, NULL, NULL, &access);
	if (!(access & HWMEM_ACCESS_IMPORT)) {
		ret = -EPERM;
		goto error;
	}

	ret = create_id(hwfile, alloc);
	if (ret < 0)
		goto error;

	return ret;

error:
	hwmem_release(alloc);

	return ret;
}

static int import_fd(struct hwmem_file *hwfile, s32 name)
{
	int ret;
	struct hwmem_alloc *alloc;
	enum hwmem_access access;

	if (hwfile->fd_alloc)
		return -EINVAL;

	alloc = hwmem_resolve_by_name(name);
	if (IS_ERR(alloc))
		return PTR_ERR(alloc);

	/* Check access permissions for process */
	hwmem_get_info(alloc, NULL, NULL, &access);
	if (!(access & HWMEM_ACCESS_IMPORT)) {
		ret = -EPERM;
		goto error;
	}

	hwfile->fd_alloc = alloc;

	return 0;

error:
	hwmem_release(alloc);

	return ret;
}

static int hwmem_open(struct inode *inode, struct file *file)
{
	struct hwmem_file *hwfile;

	hwfile = kzalloc(sizeof(struct hwmem_file), GFP_KERNEL);
	if (hwfile == NULL)
		return -ENOMEM;

	idr_init(&hwfile->idr);
	mutex_init(&hwfile->lock);
	file->private_data = hwfile;

	return 0;
}

static int hwmem_ioctl_mmap(struct file *file, struct vm_area_struct *vma)
{
	int ret;
	struct hwmem_file *hwfile = (struct hwmem_file *)file->private_data;
	struct hwmem_alloc *alloc;

	mutex_lock(&hwfile->lock);

	alloc = resolve_id(hwfile, (s32)vma->vm_pgoff << PAGE_SHIFT);
	if (IS_ERR(alloc)) {
		ret = PTR_ERR(alloc);
		goto out;
	}

	ret = hwmem_mmap(alloc, vma);

out:
	mutex_unlock(&hwfile->lock);

	return ret;
}

static int hwmem_release_idr_for_each_wrapper(int id, void *ptr, void *data)
{
	hwmem_release((struct hwmem_alloc *)ptr);

	return 0;
}

static int hwmem_release_fop(struct inode *inode, struct file *file)
{
	struct hwmem_file *hwfile = (struct hwmem_file *)file->private_data;

	idr_for_each(&hwfile->idr, hwmem_release_idr_for_each_wrapper, NULL);
	idr_remove_all(&hwfile->idr);
	idr_destroy(&hwfile->idr);

	if (hwfile->fd_alloc)
		hwmem_release(hwfile->fd_alloc);

	mutex_destroy(&hwfile->lock);

	kfree(hwfile);

	return 0;
}

static long hwmem_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
	int ret = -ENOSYS;
	struct hwmem_file *hwfile = (struct hwmem_file *)file->private_data;

	mutex_lock(&hwfile->lock);

	switch (cmd) {
	case HWMEM_ALLOC_IOC:
		{
			struct hwmem_alloc_request req;
			if (copy_from_user(&req, (void __user *)arg,
					sizeof(struct hwmem_alloc_request)))
				ret = -EFAULT;
			else
				ret = alloc(hwfile, &req);
		}
		break;
	case HWMEM_ALLOC_FD_IOC:
		{
			struct hwmem_alloc_request req;
			if (copy_from_user(&req, (void __user *)arg,
					sizeof(struct hwmem_alloc_request)))
				ret = -EFAULT;
			else
				ret = alloc_fd(hwfile, &req);
		}
		break;
	case HWMEM_RELEASE_IOC:
		ret = release(hwfile, (s32)arg);
		break;
	case HWMEM_SET_CPU_DOMAIN_IOC:
		{
			struct hwmem_set_domain_request req;
			if (copy_from_user(&req, (void __user *)arg,
				sizeof(struct hwmem_set_domain_request)))
				ret = -EFAULT;
			else
				ret = set_cpu_domain(hwfile, &req);
		}
		break;
	case HWMEM_SET_SYNC_DOMAIN_IOC:
		{
			struct hwmem_set_domain_request req;
			if (copy_from_user(&req, (void __user *)arg,
				sizeof(struct hwmem_set_domain_request)))
				ret = -EFAULT;
			else
				ret = set_sync_domain(hwfile, &req);
		}
		break;
	case HWMEM_PIN_IOC:
		{
			struct hwmem_pin_request req;
			if (copy_from_user(&req, (void __user *)arg,
				sizeof(struct hwmem_pin_request)))
				ret = -EFAULT;
			else
				ret = pin(hwfile, &req);
			if (ret == 0 && copy_to_user((void __user *)arg, &req,
					sizeof(struct hwmem_pin_request)))
				ret = -EFAULT;
		}
		break;
	case HWMEM_UNPIN_IOC:
		ret = unpin(hwfile, (s32)arg);
		break;
	case HWMEM_SET_ACCESS_IOC:
		{
			struct hwmem_set_access_request req;
			if (copy_from_user(&req, (void __user *)arg,
				sizeof(struct hwmem_set_access_request)))
				ret = -EFAULT;
			else
				ret = set_access(hwfile, &req);
		}
		break;
	case HWMEM_GET_INFO_IOC:
		{
			struct hwmem_get_info_request req;
			if (copy_from_user(&req, (void __user *)arg,
				sizeof(struct hwmem_get_info_request)))
				ret = -EFAULT;
			else
				ret = get_info(hwfile, &req);
			if (ret == 0 && copy_to_user((void __user *)arg, &req,
					sizeof(struct hwmem_get_info_request)))
				ret = -EFAULT;
		}
		break;
	case HWMEM_EXPORT_IOC:
		ret = export(hwfile, (s32)arg);
		break;
	case HWMEM_IMPORT_IOC:
		ret = import(hwfile, (s32)arg);
		break;
	case HWMEM_IMPORT_FD_IOC:
		ret = import_fd(hwfile, (s32)arg);
		break;
	}

	mutex_unlock(&hwfile->lock);

	return ret;
}

static unsigned long hwmem_get_unmapped_area(struct file *file,
	unsigned long addr, unsigned long len, unsigned long pgoff,
	unsigned long flags)
{
	/*
	 * pgoff will not be valid as it contains a buffer id (right shifted
	 * PAGE_SHIFT bits). To not confuse get_unmapped_area we'll not pass
	 * on file or pgoff.
	 */
	return current->mm->get_unmapped_area(NULL, addr, len, 0, flags);
}

int __init hwmem_ioctl_init(void)
{
	if (PAGE_SHIFT < 1 || PAGE_SHIFT > 30 || sizeof(size_t) != 4 ||
		sizeof(int) > 4 || sizeof(enum hwmem_alloc_flags) != 4 ||
					sizeof(enum hwmem_access) != 4 ||
					 sizeof(enum hwmem_mem_type) != 4) {
		dev_err(hwmem_device.this_device, "PAGE_SHIFT < 1 || PAGE_SHIFT"
			" > 30 || sizeof(size_t) != 4 || sizeof(int) > 4 ||"
			" sizeof(enum hwmem_alloc_flags) != 4 || sizeof(enum"
			" hwmem_access) != 4 || sizeof(enum hwmem_mem_type)"
								" != 4\n");
		return -ENOMSG;
	}
	if (PAGE_SHIFT > 15)
		dev_warn(hwmem_device.this_device, "Due to the page size only"
				" %u id:s per file instance are available\n",
					((u32)1 << (31 - PAGE_SHIFT)) - 1);

	return misc_register(&hwmem_device);
}

void __exit hwmem_ioctl_exit(void)
{
	misc_deregister(&hwmem_device);
}