summaryrefslogtreecommitdiff
path: root/drivers/gpu/mali/umplock/umplock_driver.c
blob: 825f16fa86170725afa4fd3644e2becf9153086a (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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/*
 * Copyright (C) 2012-2014 ARM Limited. All rights reserved.
 * 
 * This program is free software and is provided to you under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation, and any use by you of this program is subject to the terms of such GNU licence.
 * 
 * A copy of the licence is included with the program, and can also be obtained from Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <asm/uaccess.h>
#include "umplock_ioctl.h"
#include <linux/sched.h>

#define MAX_ITEMS 1024
#define MAX_PIDS 128

typedef struct lock_cmd_priv {
	uint32_t msg[128];    /*ioctl args*/
	u32 pid;              /*process id*/
} _lock_cmd_priv;

typedef struct lock_ref {
	int ref_count;
	u32 pid;
} _lock_ref;

typedef struct umplock_item {
	u32 secure_id;
	u32 id_ref_count;
	u32 owner;
	_lock_access_usage usage;
	_lock_ref references[MAX_PIDS];
	struct semaphore item_lock;
} umplock_item;

typedef struct umplock_device_private {
	struct mutex item_list_lock;
	atomic_t sessions;
	umplock_item items[MAX_ITEMS];
	u32 pids[MAX_PIDS];
} umplock_device_private;

struct umplock_device {
	struct cdev cdev;
	struct class *umplock_class;
};

static struct umplock_device umplock_device;
static umplock_device_private device;
static dev_t umplock_dev;
static char umplock_dev_name[] = "umplock";

int umplock_debug_level = 0;
module_param(umplock_debug_level, int, S_IRUSR | S_IWUSR | S_IWGRP | S_IRGRP | S_IROTH); /* rw-rw-r-- */
MODULE_PARM_DESC(umplock_debug_level, "set umplock_debug_level to print debug messages");

#define PDEBUG(level, fmt, args...) do { if ((level) <= umplock_debug_level) printk(KERN_DEBUG "umplock: " fmt, ##args); } while (0)
#define PERROR(fmt, args...) do { printk(KERN_ERR "umplock: " fmt, ##args); } while (0)

int umplock_find_item(u32 secure_id)
{
	int i;
	for (i = 0; i < MAX_ITEMS; i++) {
		if (device.items[i].secure_id == secure_id) {
			return i;
		}
	}

	return -1;
}

static int umplock_find_item_by_pid(_lock_cmd_priv *lock_cmd, int *item_slot, int *ref_slot)
{
	_lock_item_s *lock_item;
	int i, j;

	lock_item = (_lock_item_s *)&lock_cmd->msg;

	i = umplock_find_item(lock_item->secure_id);

	if (i < 0) {
		return -1;
	}

	for (j = 0; j < MAX_PIDS; j++) {
		if (device.items[i].references[j].pid == lock_cmd->pid) {
			*item_slot = i;
			*ref_slot = j;
			return 0;
		}
	}
	return -1 ;
}

static int umplock_find_client_valid(u32 pid)
{
	int i;

	if (pid == 0) {
		return -1;
	}

	for (i = 0; i < MAX_PIDS; i++) {
		if (device.pids[i] == pid) {
			return i;
		}
	}

	return -1;
}

static int do_umplock_create_locked(_lock_cmd_priv *lock_cmd)
{
	int i_index, ref_index;
	int ret;
	_lock_item_s *lock_item = (_lock_item_s *)&lock_cmd->msg;

	i_index = ref_index = -1;

	ret = umplock_find_client_valid(lock_cmd->pid);
	if (ret < 0) {
		/*lock request from an invalid client pid, do nothing*/
		return -EINVAL;
	}

	ret = umplock_find_item_by_pid(lock_cmd, &i_index, &ref_index);
	if (ret >= 0) {
	} else if ((i_index = umplock_find_item(lock_item->secure_id)) >= 0) {
		for (ref_index = 0; ref_index < MAX_PIDS; ref_index++) {
			if (device.items[i_index].references[ref_index].pid == 0) {
				break;
			}
		}
		if (ref_index < MAX_PIDS) {
			device.items[i_index].references[ref_index].pid = lock_cmd->pid;
			device.items[i_index].references[ref_index].ref_count = 0;
		} else {
			PERROR("whoops, item ran out of available reference slots\n");
			return -EINVAL;

		}
	} else {
		i_index = umplock_find_item(0);

		if (i_index >= 0) {
			device.items[i_index].secure_id = lock_item->secure_id;
			device.items[i_index].id_ref_count = 0;
			device.items[i_index].usage = lock_item->usage;
			device.items[i_index].references[0].pid = lock_cmd->pid;
			device.items[i_index].references[0].ref_count = 0;
			sema_init(&device.items[i_index].item_lock, 1);
		} else {
			PERROR("whoops, ran out of available slots\n");
			return -EINVAL;
		}
	}

	return 0;
}
/** IOCTLs **/

static int do_umplock_create(_lock_cmd_priv *lock_cmd)
{
	return 0;
}

static int do_umplock_process(_lock_cmd_priv *lock_cmd)
{
	int ret, i_index, ref_index;
	_lock_item_s *lock_item = (_lock_item_s *)&lock_cmd->msg;

	mutex_lock(&device.item_list_lock);

	if (0 == lock_item->secure_id) {
		PERROR("IOCTL_UMPLOCK_PROCESS called with secure_id is 0, pid: %d\n", lock_cmd->pid);
		mutex_unlock(&device.item_list_lock);
		return -EINVAL;
	}

	ret = do_umplock_create_locked(lock_cmd);
	if (ret < 0) {
		mutex_unlock(&device.item_list_lock);
		return -EINVAL;
	}

	ret = umplock_find_item_by_pid(lock_cmd, &i_index, &ref_index);
	if (ret < 0) {
		/*fail to find a item*/
		PERROR("IOCTL_UMPLOCK_PROCESS called with invalid parameter, pid: %d\n", lock_cmd->pid);
		mutex_unlock(&device.item_list_lock);
		return -EINVAL;
	}
	device.items[i_index].references[ref_index].ref_count++;
	device.items[i_index].id_ref_count++;
	PDEBUG(1, "try to lock, pid: %d, secure_id: 0x%x, ref_count: %d\n", lock_cmd->pid, lock_item->secure_id, device.items[i_index].references[ref_index].ref_count);

	if (lock_cmd->pid == device.items[i_index].owner) {
		PDEBUG(1, "already own the lock, pid: %d, secure_id: 0x%x, ref_count: %d\n", lock_cmd->pid, lock_item->secure_id, device.items[i_index].references[ref_index].ref_count);
		mutex_unlock(&device.item_list_lock);
		return 0;
	}

	mutex_unlock(&device.item_list_lock);
	if (down_interruptible(&device.items[i_index].item_lock)) {
		/*wait up without hold the umplock. restore previous state and return*/
		mutex_lock(&device.item_list_lock);
		device.items[i_index].references[ref_index].ref_count--;
		device.items[i_index].id_ref_count--;
		if (0 == device.items[i_index].references[ref_index].ref_count) {
			device.items[i_index].references[ref_index].pid = 0;
			if (0 == device.items[i_index].id_ref_count) {
				PDEBUG(1, "release item, pid: %d, secure_id: 0x%x\n", lock_cmd->pid, lock_item->secure_id);
				device.items[i_index].secure_id = 0;
			}
		}

		PERROR("failed lock, pid: %d, secure_id: 0x%x, ref_count: %d\n", lock_cmd->pid, lock_item->secure_id, device.items[i_index].references[ref_index].ref_count);

		mutex_unlock(&device.item_list_lock);
		return -ERESTARTSYS;
	}

	mutex_lock(&device.item_list_lock);
	PDEBUG(1, "got lock, pid: %d, secure_id: 0x%x, ref_count: %d\n", lock_cmd->pid, lock_item->secure_id, device.items[i_index].references[ref_index].ref_count);
	device.items[i_index].owner = lock_cmd->pid;
	mutex_unlock(&device.item_list_lock);

	return 0;
}

static int do_umplock_release(_lock_cmd_priv *lock_cmd)
{
	int ret, i_index, ref_index;
	_lock_item_s *lock_item = (_lock_item_s *)&lock_cmd->msg;

	mutex_lock(&device.item_list_lock);

	if (0 == lock_item->secure_id) {
		PERROR("IOCTL_UMPLOCK_RELEASE called with secure_id is 0, pid: %d\n", lock_cmd->pid);
		mutex_unlock(&device.item_list_lock);
		return -EINVAL;
	}

	ret = umplock_find_client_valid(lock_cmd->pid);
	if (ret < 0) {
		/*lock request from an invalid client pid, do nothing*/
		mutex_unlock(&device.item_list_lock);
		return -EPERM;
	}

	i_index = ref_index = -1;

	ret = umplock_find_item_by_pid(lock_cmd, &i_index, &ref_index);
	if (ret < 0) {
		/*fail to find item*/
		PERROR("IOCTL_UMPLOCK_RELEASE called with invalid parameter pid: %d, secid: 0x%x\n", lock_cmd->pid, lock_item->secure_id);
		mutex_unlock(&device.item_list_lock);
		return -EINVAL;
	}

	/* if the lock is not owned by this process */
	if (lock_cmd->pid != device.items[i_index].owner) {
		mutex_unlock(&device.item_list_lock);
		return -EPERM;
	}

	/* if the ref_count is 0, that means nothing to unlock, just return */
	if (0 == device.items[i_index].references[ref_index].ref_count) {
		mutex_unlock(&device.item_list_lock);
		return 0;
	}

	device.items[i_index].references[ref_index].ref_count--;
	device.items[i_index].id_ref_count--;
	PDEBUG(1, "unlock, pid: %d, secure_id: 0x%x, ref_count: %d\n", lock_cmd->pid, lock_item->secure_id, device.items[i_index].references[ref_index].ref_count);

	if (0 == device.items[i_index].references[ref_index].ref_count) {
		device.items[i_index].references[ref_index].pid = 0;
		if (0 == device.items[i_index].id_ref_count) {
			PDEBUG(1, "release item, pid: %d, secure_id: 0x%x\n", lock_cmd->pid, lock_item->secure_id);
			device.items[i_index].secure_id = 0;
		}
		device.items[i_index].owner = 0;
		up(&device.items[i_index].item_lock);
	}
	mutex_unlock(&device.item_list_lock);

	return 0;
}

static int do_umplock_zap(void)
{
	int i;

	PDEBUG(1, "ZAP ALL ENTRIES!\n");

	mutex_lock(&device.item_list_lock);

	for (i = 0; i < MAX_ITEMS; i++) {
		device.items[i].secure_id = 0;
		memset(&device.items[i].references, 0, sizeof(_lock_ref) * MAX_PIDS);
		sema_init(&device.items[i].item_lock, 1);
	}

	for (i = 0; i < MAX_PIDS; i++) {
		device.pids[i] = 0;
	}
	mutex_unlock(&device.item_list_lock);

	return 0;
}

static int do_umplock_dump(void)
{
	int i, j;

	mutex_lock(&device.item_list_lock);
	PERROR("dump all the items begin\n");
	for (i = 0; i < MAX_ITEMS; i++) {
		for (j = 0; j < MAX_PIDS; j++) {
			if (device.items[i].secure_id != 0 && device.items[i].references[j].pid != 0) {
				PERROR("item[%d]->secure_id=0x%x, owner=%d\t reference[%d].ref_count=%d.pid=%d\n",
				       i,
				       device.items[i].secure_id,
				       device.items[i].owner,
				       j,
				       device.items[i].references[j].ref_count,
				       device.items[i].references[j].pid);
			}
		}
	}
	PERROR("dump all the items end\n");
	mutex_unlock(&device.item_list_lock);

	return 0;
}

int do_umplock_client_add(_lock_cmd_priv *lock_cmd)
{
	int i;
	mutex_lock(&device.item_list_lock);
	for (i = 0; i < MAX_PIDS; i++) {
		if (device.pids[i] == lock_cmd->pid) {
			mutex_unlock(&device.item_list_lock);
			return 0;
		}
	}
	for (i = 0; i < MAX_PIDS; i++) {
		if (device.pids[i] == 0) {
			device.pids[i] = lock_cmd->pid;
			break;
		}
	}
	mutex_unlock(&device.item_list_lock);
	if (i == MAX_PIDS) {
		PERROR("Oops, Run out of client slots\n ");
		return -EINVAL;
	}
	return 0;
}

int do_umplock_client_delete(_lock_cmd_priv *lock_cmd)
{
	int p_index = -1, i_index = -1, ref_index = -1;
	int ret;
	_lock_item_s *lock_item;
	lock_item = (_lock_item_s *)&lock_cmd->msg;

	mutex_lock(&device.item_list_lock);
	p_index = umplock_find_client_valid(lock_cmd->pid);
	/*lock item pid is not valid.*/
	if (p_index < 0) {
		mutex_unlock(&device.item_list_lock);
		return 0;
	}

	/*walk through umplock item list and release reference attached to this client*/
	for (i_index = 0; i_index < MAX_ITEMS; i_index++) {
		lock_item->secure_id = device.items[i_index].secure_id;

		/*find the item index and reference slot for the lock_item*/
		ret = umplock_find_item_by_pid(lock_cmd, &i_index, &ref_index);

		if (ret < 0) {
			/*client has no reference on this umplock item, skip*/
			continue;
		}
		while (device.items[i_index].references[ref_index].ref_count) {
			/*release references on this client*/

			PDEBUG(1, "delete client, pid: %d, ref_count: %d\n", lock_cmd->pid, device.items[i_index].references[ref_index].ref_count);

			mutex_unlock(&device.item_list_lock);
			do_umplock_release(lock_cmd);
			mutex_lock(&device.item_list_lock);
		}
	}

	/*remove the pid from umplock valid pid list*/
	device.pids[p_index] = 0;
	mutex_unlock(&device.item_list_lock);

	return 0;
}

static long umplock_driver_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
{
	int ret;
	uint32_t size = _IOC_SIZE(cmd);
	_lock_cmd_priv lock_cmd ;

	if (_IOC_TYPE(cmd) != LOCK_IOCTL_GROUP) {
		return -ENOTTY;
	}

	if (_IOC_NR(cmd) >= LOCK_IOCTL_MAX_CMDS) {
		return -ENOTTY;
	}

	switch (cmd) {
	case LOCK_IOCTL_CREATE:
		if (size != sizeof(_lock_item_s)) {
			return -ENOTTY;
		}

		if (copy_from_user(&lock_cmd.msg, (void __user *)arg, size)) {
			return -EFAULT;
		}
		lock_cmd.pid = (u32)current->tgid;
		ret = do_umplock_create(&lock_cmd);
		if (ret) {
			return ret;
		}
		return 0;

	case LOCK_IOCTL_PROCESS:
		if (size != sizeof(_lock_item_s)) {
			return -ENOTTY;
		}

		if (copy_from_user(&lock_cmd.msg, (void __user *)arg, size)) {
			return -EFAULT;
		}
		lock_cmd.pid = (u32)current->tgid;
		return do_umplock_process(&lock_cmd);

	case LOCK_IOCTL_RELEASE:
		if (size != sizeof(_lock_item_s)) {
			return -ENOTTY;
		}

		if (copy_from_user(&lock_cmd.msg, (void __user *)arg, size)) {
			return -EFAULT;
		}
		lock_cmd.pid = (u32)current->tgid;
		ret = do_umplock_release(&lock_cmd);
		if (ret) {
			return ret;
		}
		return 0;

	case LOCK_IOCTL_ZAP:
		do_umplock_zap();
		return 0;

	case LOCK_IOCTL_DUMP:
		do_umplock_dump();
		return 0;
	}

	return -ENOIOCTLCMD;
}

static int umplock_driver_open(struct inode *inode, struct file *filp)
{
	_lock_cmd_priv lock_cmd;

	atomic_inc(&device.sessions);
	PDEBUG(1, "OPEN SESSION (%i references)\n", atomic_read(&device.sessions));

	lock_cmd.pid = (u32)current->tgid;
	do_umplock_client_add(&lock_cmd);

	return 0;
}

static int umplock_driver_release(struct inode *inode, struct file *filp)
{
	int sessions = 0;
	_lock_cmd_priv lock_cmd;

	lock_cmd.pid = (u32)current->tgid;
	do_umplock_client_delete(&lock_cmd);

	mutex_lock(&device.item_list_lock);
	atomic_dec(&device.sessions);
	sessions = atomic_read(&device.sessions);
	PDEBUG(1, "CLOSE SESSION (%i references)\n", sessions);
	mutex_unlock(&device.item_list_lock);
	if (sessions == 0) {
		do_umplock_zap();
	}

	return 0;
}

static struct file_operations umplock_fops = {
	.owner   = THIS_MODULE,
	.open    = umplock_driver_open,
	.release = umplock_driver_release,
	.unlocked_ioctl = umplock_driver_ioctl,
};

int umplock_device_initialize(void)
{
	int err;

	err = alloc_chrdev_region(&umplock_dev, 0, 1, umplock_dev_name);

	if (0 == err) {
		memset(&umplock_device, 0, sizeof(umplock_device));
		cdev_init(&umplock_device.cdev, &umplock_fops);
		umplock_device.cdev.owner = THIS_MODULE;
		umplock_device.cdev.ops = &umplock_fops;

		err = cdev_add(&umplock_device.cdev, umplock_dev, 1);
		if (0 == err) {
			umplock_device.umplock_class = class_create(THIS_MODULE, umplock_dev_name);
			if (IS_ERR(umplock_device.umplock_class)) {
				err = PTR_ERR(umplock_device.umplock_class);
			} else {
				struct device *mdev;
				mdev = device_create(umplock_device.umplock_class, NULL, umplock_dev, NULL, umplock_dev_name);
				if (!IS_ERR(mdev)) {
					return 0; /* all ok */
				}

				err = PTR_ERR(mdev);
				class_destroy(umplock_device.umplock_class);
			}
			cdev_del(&umplock_device.cdev);
		}

		unregister_chrdev_region(umplock_dev, 1);
	} else {
		PERROR("alloc chardev region failed\n");
	}

	return err;
}

void umplock_device_terminate(void)
{
	device_destroy(umplock_device.umplock_class, umplock_dev);
	class_destroy(umplock_device.umplock_class);

	cdev_del(&umplock_device.cdev);
	unregister_chrdev_region(umplock_dev, 1);
}

static int __init umplock_initialize_module(void)
{
	PDEBUG(1, "Inserting UMP lock device driver. Compiled: %s, time: %s\n", __DATE__, __TIME__);

	mutex_init(&device.item_list_lock);
	if (umplock_device_initialize() != 0) {
		PERROR("UMP lock device driver init failed\n");
		return -ENOTTY;
	}
	memset(&device.items, 0, sizeof(umplock_item) * MAX_ITEMS);
	memset(&device.pids, 0, sizeof(u32) * MAX_PIDS);
	atomic_set(&device.sessions, 0);

	PDEBUG(1, "UMP lock device driver loaded\n");

	return 0;
}

static void __exit umplock_cleanup_module(void)
{
	PDEBUG(1, "unloading UMP lock module\n");

	memset(&device.items, 0, sizeof(umplock_item) * MAX_ITEMS);
	memset(&device.pids, 0, sizeof(u32) * MAX_PIDS);
	umplock_device_terminate();
	mutex_destroy(&device.item_list_lock);

	PDEBUG(1, "UMP lock module unloaded\n");
}

module_init(umplock_initialize_module);
module_exit(umplock_cleanup_module);


MODULE_LICENSE("GPL");
MODULE_AUTHOR("ARM Ltd.");
MODULE_DESCRIPTION("ARM UMP locker");