summaryrefstree
path: root/kernel/drivers/misc/tzdev/tzdev.c
blob: 4d4a25b13ffb09e2886503e7d3e79ef361523a2d (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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
/*
 * Copyright (C) 2012-2018, Samsung Electronics Co., Ltd.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that 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.
 */

#include <linux/atomic.h>
#include <linux/buffer_head.h>
#include <linux/completion.h>
#include <linux/cpu.h>
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/interrupt.h>
#include <linux/ioctl.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/miscdevice.h>
#include <linux/mmzone.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/of.h>
#include <linux/of_irq.h>
#include <linux/pid.h>
#include <linux/platform_device.h>
#include <linux/reboot.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/syscore_ops.h>
#include <linux/time.h>
#include <linux/uaccess.h>
#include <linux/workqueue.h>

#include <asm/segment.h>
#include <asm/uaccess.h>

#include "sysdep.h"
#include "tz_boost.h"
#include "tz_cdev.h"
#include "tz_cma.h"
#include "tz_deploy_tzar.h"
#include "tz_fsdev.h"
#include "tz_iw_boot_log.h"
#include "tz_iwio.h"
#include "tz_iwlog.h"
#include "tz_iwservice.h"
#include "tz_iwsock.h"
#include "tz_kthread_pool.h"
#include "tz_mem.h"
#include "tz_panic_dump.h"
#include "tz_platform.h"
#include "tz_uiwsock.h"
#include "tzdev.h"
#include "tzlog.h"
#include "tzprofiler.h"

MODULE_AUTHOR("Jaemin Ryu <jm77.ryu@samsung.com>");
MODULE_AUTHOR("Vasily Leonenko <v.leonenko@samsung.com>");
MODULE_AUTHOR("Alex Matveev <alex.matveev@samsung.com>");
MODULE_DESCRIPTION("TZDEV driver");
MODULE_LICENSE("GPL");

unsigned int tzdev_verbosity;
unsigned int tzdev_teec_verbosity;
unsigned int tzdev_kthread_verbosity;
unsigned int tzdev_uiwsock_verbosity;

module_param(tzdev_verbosity, uint, 0644);
MODULE_PARM_DESC(tzdev_verbosity, "0: normal, 1: verbose, 2: debug");

module_param(tzdev_teec_verbosity, uint, 0644);
MODULE_PARM_DESC(tzdev_teec_verbosity, "0: error, 1: info, 2: debug");

module_param(tzdev_kthread_verbosity, uint, 0644);
MODULE_PARM_DESC(tzdev_kthread_verbosity, "0: error, 1: info, 2: debug");

module_param(tzdev_uiwsock_verbosity, uint, 0644);
MODULE_PARM_DESC(tzdev_uiwsock_verbosity, "0: error, 1: info, 2: debug");

enum tzdev_nwd_state {
	TZDEV_NWD_DOWN,
	TZDEV_NWD_UP,
};

enum tzdev_swd_state {
	TZDEV_SWD_DOWN,
	TZDEV_SWD_UP,
	TZDEV_SWD_DEAD
};

struct tzdev_shmem {
	struct list_head link;
	unsigned int id;
};

static atomic_t tzdev_nwd_state = ATOMIC_INIT(TZDEV_NWD_DOWN);
static atomic_t tzdev_swd_state = ATOMIC_INIT(TZDEV_SWD_DOWN);

static struct tzio_sysconf tz_sysconf;

static int tzdev_alloc_aux_channels(void)
{
	int ret;
	unsigned int i;

	for (i = 0; i < NR_SW_CPU_IDS; ++i) {
		ret = tz_iwio_alloc_aux_channel(i);
		if (ret)
			return ret;
	}

	return 0;
}

static int tzdev_sysconf(struct tzio_sysconf *sysconf)
{
	int ret = 0;
	struct tz_iwio_aux_channel *ch;

	ch = tz_iwio_get_aux_channel();

	sysconf->nwd_sysconf.flags = tzdev_platform_get_sysconf_flags();

#ifdef CONFIG_TZDEV_HOTPLUG
	sysconf->nwd_sysconf.flags |= SYSCONF_NWD_CPU_HOTPLUG;
#endif /* CONFIG_TZDEV_HOTPLUG */

#ifdef CONFIG_TZDEV_DEPLOY_TZAR
	sysconf->nwd_sysconf.flags |= SYSCONF_NWD_TZDEV_DEPLOY_TZAR;
#endif /* CONFIG_TZDEV_DEPLOY_TZAR */

	memcpy(ch->buffer, &sysconf->nwd_sysconf, sizeof(struct tzio_nwd_sysconf));

	ret = tzdev_smc_sysconf();
	if (ret) {
		tzdev_print(0, "tzdev_smc_sysconf() failed with %d\n", ret);
		goto out;
	}

	memcpy(&sysconf->swd_sysconf, ch->buffer, sizeof(struct tzio_swd_sysconf));
out:
	tz_iwio_put_aux_channel();

	return ret;
}

static irqreturn_t tzdev_event_handler(int irq, void *ptr)
{
	tz_kthread_pool_cmd_send();

	return IRQ_HANDLED;
}

#if CONFIG_TZDEV_IWI_PANIC != 0
static void dump_kernel_panic_bh(struct work_struct *work)
{
	atomic_set(&tzdev_swd_state, TZDEV_SWD_DEAD);
	if (atomic_read(&tzdev_nwd_state) == TZDEV_NWD_UP) {
		tz_iw_boot_log_read();
		tz_iwlog_read_buffers();
		tz_iwsock_kernel_panic_handler();
		tz_kthread_pool_fini();
		tzdev_mem_release_panic_handler();
		panic("tzdev: IWI_PANIC raised\n");
	}
}

static DECLARE_WORK(dump_kernel_panic, dump_kernel_panic_bh);

static irqreturn_t tzdev_panic_handler(int irq, void *ptr)
{
	schedule_work(&dump_kernel_panic);
	return IRQ_HANDLED;
}
#endif

static void tzdev_resolve_iwis_id(unsigned int *iwi_event, unsigned int *iwi_panic)
{
	struct device_node *node;

	node = of_find_compatible_node(NULL, NULL, "samsung,blowfish");
	if (!node)
		node = of_find_compatible_node(NULL, NULL, "samsung,teegris");

	if (!node) {
		*iwi_event = CONFIG_TZDEV_IWI_EVENT;
#if CONFIG_TZDEV_IWI_PANIC != 0
		*iwi_panic = CONFIG_TZDEV_IWI_PANIC;
#endif
		tzdev_print(0, "of_find_compatible_node() failed\n");
		return;
	}

	*iwi_event = irq_of_parse_and_map(node, 0);
	if (!*iwi_event) {
		*iwi_event = CONFIG_TZDEV_IWI_EVENT;
		tzdev_print(0, "Use IWI event IRQ number from config %d\n",
			CONFIG_TZDEV_IWI_EVENT);
	}

#if CONFIG_TZDEV_IWI_PANIC != 0
	*iwi_panic = irq_of_parse_and_map(node, 1);
	if (!*iwi_panic) {
		*iwi_panic = CONFIG_TZDEV_IWI_PANIC;
		tzdev_print(0, "Use IWI panic IRQ number from config %d\n",
			CONFIG_TZDEV_IWI_PANIC);
	}
#endif
	of_node_put(node);
}

static void tzdev_register_iwis(void)
{
	int ret;
	unsigned int iwi_event;
	unsigned int iwi_panic;

	tzdev_resolve_iwis_id(&iwi_event, &iwi_panic);

	ret = request_irq(iwi_event, tzdev_event_handler, 0, "tzdev_iwi_event", NULL);
	if (ret)
		tzdev_print(0, "TZDEV_IWI_EVENT registration failed: %d\n", ret);

#if CONFIG_TZDEV_IWI_PANIC != 0
	ret = request_irq(iwi_panic, tzdev_panic_handler, 0, "tzdev_iwi_panic", NULL);
	if (ret)
		tzdev_print(0, "TZDEV_IWI_PANIC registration failed: %d\n", ret);
#endif
}

int __tzdev_smc_cmd(struct tzdev_smc_data *data)
{
	int ret;

	tzprofiler_enter_sw();
	tz_iwlog_schedule_delayed_work();

	tzdev_print(2, "tzdev_smc_cmd: enter: args={0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x}\n",
			data->args[0], data->args[1], data->args[2], data->args[3],
			data->args[4], data->args[5], data->args[6]);

	ret = tzdev_platform_smc_call(data);

	tzdev_print(2, "tzdev_smc_cmd: exit: args={0x%x 0x%x 0x%x 0x%x} ret=%d\n",
			data->args[0], data->args[1], data->args[2], data->args[3], ret);

	tz_iwlog_cancel_delayed_work();
	tz_iwlog_read_buffers();

	tzprofiler_wait_for_bufs();
	tzprofiler_exit_sw();

	tz_iwsock_check_notifications();
	tz_iwservice_handle_swd_request();

	return ret;
}

/*  TZDEV interface functions */
unsigned int tzdev_is_up(void)
{
	return atomic_read(&tzdev_swd_state) == TZDEV_SWD_UP;
}

int tzdev_run_init_sequence(void)
{
	int ret = 0;

	if (atomic_read(&tzdev_swd_state) == TZDEV_SWD_DOWN) {
		/* check kernel and driver version compatibility with TEEGRIS */
		ret = tzdev_smc_check_version();
		if (ret == -ENOSYS || ret == -EINVAL) {
			/* version is not compatibile. Not critical, continue ... */
			ret = 0;
		} else if (ret) {
			tzdev_print(0, "tzdev_smc_check_version() failed\n");
			goto out;
		}

		if (tzdev_cma_mem_register()) {
			tzdev_print(0, "tzdev_cma_mem_register() failed\n");
			ret = -ESHUTDOWN;
			goto out;
		}

		if (tzdev_alloc_aux_channels()) {
			tzdev_print(0, "tzdev_alloc_AUX_Channels() failed\n");
			ret = -ESHUTDOWN;
			goto out;
		}

		if (tz_iwservice_alloc()) {
			tzdev_print(0, "tzdev_alloc_service_channel() failed\n");
			ret = -ESHUTDOWN;
			goto out;
		}

		if (tzdev_mem_init()) {
			tzdev_print(0, "tzdev_mem_init() failed\n");
			ret = -ESHUTDOWN;
			goto out;
		}

		if (tz_iwlog_initialize()) {
			tzdev_print(0, "tz_iwlog_initialize() failed\n");
			ret = -ESHUTDOWN;
			goto out;
		}

		if (tzprofiler_initialize()) {
			tzdev_print(0, "tzprofiler_initialize() failed\n");
			ret = -ESHUTDOWN;
			goto out;
		}

		if (tz_panic_dump_alloc_buffer()) {
			tzdev_print(0, "tz_panic_dump_alloc_buffer() failed\n");
			ret = -ESHUTDOWN;
			goto out;
		}

		if (tzdev_sysconf(&tz_sysconf)) {
			tzdev_print(0, "tzdev_sysconf() failed\n");
			ret = -ESHUTDOWN;
			goto out;
		}

		if (tz_iwsock_init()) {
			tzdev_print(0, "tz_iwsock_init failed\n");
			ret = -ESHUTDOWN;
			goto out;
		}

		tzdev_register_iwis();
		tz_boost_set_boost_mask(tz_sysconf.swd_sysconf.big_cpus_mask);

		if (tz_fsdev_initialize()) {
			tzdev_print(0, "tz_fsdev_initialize() failed\n");
			ret = -ESHUTDOWN;
			goto out;
		}

		if (atomic_cmpxchg(&tzdev_swd_state, TZDEV_SWD_DOWN, TZDEV_SWD_UP)) {
			ret = -ESHUTDOWN;
			goto out;
		}

		if (tzdev_deploy_tzar()) {
			tzdev_print(0, "tzdev_deploy_tzar() failed\n");
			ret = -ESHUTDOWN;
			goto out;
		}
	}
out:
	if (ret == -ESHUTDOWN) {
		atomic_set(&tzdev_swd_state, TZDEV_SWD_DEAD);
		tz_iw_boot_log_read();
		tz_iwlog_read_buffers();
	}

	return ret;
}

static int tzdev_get_sysconf(struct file *filp, unsigned long arg)
{
	struct tzio_sysconf __user *argp = (struct tzio_sysconf __user *)arg;

	if (copy_to_user(argp, &tz_sysconf, sizeof(struct tzio_sysconf)))
		return -EFAULT;

	return 0;
}

static int tzdev_register_shared_memory(struct file *filp, unsigned long arg)
{
	int ret;
	struct tzdev_shmem *shmem;
	struct tzdev_fd_data *data = filp->private_data;
	struct tzio_mem_register __user *argp = (struct tzio_mem_register __user *)arg;
	struct tzio_mem_register s;

	if (copy_from_user(&s, argp, sizeof(struct tzio_mem_register)))
		return -EFAULT;

	shmem = kzalloc(sizeof(struct tzdev_shmem), GFP_KERNEL);
	if (!shmem) {
		tzdev_print(0, "Failed to allocate shmem structure\n");
		return -ENOMEM;
	}

	ret = tzdev_mem_register_user(UINT_PTR(s.ptr), s.size, s.write);
	if (ret < 0) {
		kfree(shmem);
		return ret;
	}

	INIT_LIST_HEAD(&shmem->link);
	shmem->id = ret;

	spin_lock(&data->shmem_list_lock);
	list_add(&shmem->link, &data->shmem_list);
	spin_unlock(&data->shmem_list_lock);

	return shmem->id;
}

static int tzdev_release_shared_memory(struct file *filp, unsigned int id)
{
	struct tzdev_shmem *shmem;
	struct tzdev_fd_data *data = filp->private_data;
	unsigned int found = 0;

	spin_lock(&data->shmem_list_lock);
	list_for_each_entry(shmem, &data->shmem_list, link) {
		if (shmem->id == id) {
			list_del(&shmem->link);
			found = 1;
			break;
		}
	}
	spin_unlock(&data->shmem_list_lock);

	if (!found)
		return -EINVAL;

	kfree(shmem);

	return tzdev_mem_release_user(id);
}

static int tzdev_boost_control(struct file *filp, unsigned int state)
{
	struct tzdev_fd_data *data = filp->private_data;
	int ret = 0;

	mutex_lock(&data->mutex);

	switch (state) {
	case TZIO_BOOST_ON:
		if (!data->boost_state) {
			tz_boost_enable();
			data->boost_state = 1;
		} else {
			tzdev_print(0, "Trying to enable boost twice, filp=%pK\n", filp);
			ret = -EBUSY;
		}
		break;
	case TZIO_BOOST_OFF:
		if (data->boost_state) {
			tz_boost_disable();
			data->boost_state = 0;
		} else {
			tzdev_print(0, "Trying to disable boost twice, filp=%pK\n", filp);
			ret = -EBUSY;
		}
		break;
	default:
		tzdev_print(0, "Unknown boost request, state=%u filp=%pK\n", state, filp);
		ret = -EINVAL;
		break;
	}

	mutex_unlock(&data->mutex);

	return ret;
}

static int tzdev_open(struct inode *inode, struct file *filp)
{
	struct tzdev_fd_data *data;

	if (!tzdev_is_up())
		return -EPERM;

	data = kzalloc(sizeof(struct tzdev_fd_data), GFP_KERNEL);
	if (!data) {
		tzdev_print(0, "Failed to allocate private data\n");
		return -ENOMEM;
	}

	INIT_LIST_HEAD(&data->shmem_list);
	spin_lock_init(&data->shmem_list_lock);
	mutex_init(&data->mutex);

	filp->private_data = data;

	return 0;
}

static int tzdev_release(struct inode *inode, struct file *filp)
{
	struct tzdev_shmem *shmem, *tmp;
	struct tzdev_fd_data *data = filp->private_data;

	list_for_each_entry_safe(shmem, tmp, &data->shmem_list, link) {
		list_del(&shmem->link);
		tzdev_mem_release_user(shmem->id);
		kfree(shmem);
	}

	if (data->boost_state)
		tzdev_boost_control(filp, TZIO_BOOST_OFF);

	tzdev_fd_platform_close(inode, filp);

	mutex_destroy(&data->mutex);

	kfree(data);

	return 0;
}

static long tzdev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
	switch (cmd) {
	case TZIO_GET_SYSCONF:
		return tzdev_get_sysconf(filp, arg);
	case TZIO_MEM_REGISTER:
		return tzdev_register_shared_memory(filp, arg);
	case TZIO_MEM_RELEASE:
		return tzdev_release_shared_memory(filp, arg);
	case TZIO_BOOST_CONTROL:
		return tzdev_boost_control(filp, arg);
	default:
		return tzdev_fd_platform_ioctl(filp, cmd, arg);
	}
}

static const struct file_operations tzdev_fops = {
	.owner = THIS_MODULE,
	.open = tzdev_open,
	.release = tzdev_release,
	.unlocked_ioctl = tzdev_ioctl,
#ifdef CONFIG_COMPAT
	.compat_ioctl = tzdev_ioctl,
#endif /* CONFIG_COMPAT */
};

static struct tz_cdev tzdev_cdev = {
	.name = "tzdev",
	.fops = &tzdev_fops,
	.owner = THIS_MODULE,
};

struct tz_cdev *tzdev_get_cdev(void)
{
	return &tzdev_cdev;
}

static int exit_tzdev(struct notifier_block *cb, unsigned long code, void *unused)
{
	(void)cb;
	(void)code;
	(void)unused;

	atomic_set(&tzdev_nwd_state, TZDEV_NWD_DOWN);

	tzdev_platform_unregister();
	tz_cdev_unregister(&tzdev_cdev);
	tzdev_cma_mem_release(tzdev_cdev.device);
	tz_kthread_pool_fini();
	tz_hotplug_exit();

	tz_uiwsock_fini();
	tz_iwsock_fini();

	tzdev_print(0, "tzdev exit done.\n");

	return NOTIFY_DONE;
}

static struct notifier_block tzdev_reboot_nb = {
	.notifier_call = exit_tzdev,
};

static int __init init_tzdev(void)
{
	int rc;

	rc = tz_cdev_register(&tzdev_cdev);
	if (rc) {
		tzdev_print(0, "tz_cdev_register() for device failed with error=%d\n", rc);
		return rc;
	}

	rc = tz_uiwsock_init();
	if (rc) {
		tzdev_print(0, "tz_uiwsock_init() failed with error=%d\n", rc);
		goto uiwsock_initialization_failed;
	}

	rc = tzdev_platform_register();
	if (rc) {
		tzdev_print(0, "tzdev_platform_register() failed with error=%d\n", rc);
		goto platform_driver_registration_failed;
	}

	rc = tz_hotplug_init();
	if (rc) {
		tzdev_print(0, "tzdev_init_hotplug() failed with error=%d\n", rc);
		goto hotplug_initialization_failed;
	}

	tzdev_cma_mem_init(tzdev_cdev.device);

	rc = tzdev_platform_init();
	if (rc) {
		tzdev_print(0, "tzdev_platform_init() failed with error=%d\n", rc);
		goto platform_initialization_failed;
	}

	rc = register_reboot_notifier(&tzdev_reboot_nb);
	if (rc) {
		tzdev_print(0, "reboot notifier registration failed() failed with error=%d\n", rc);
		goto reboot_notifier_registration_failed;
	}

	atomic_set(&tzdev_nwd_state, TZDEV_NWD_UP);

	return rc;

reboot_notifier_registration_failed:
	tzdev_platform_fini();
platform_initialization_failed:
	tzdev_cma_mem_release(tzdev_cdev.device);
	tz_hotplug_exit();
hotplug_initialization_failed:
	tzdev_platform_unregister();
platform_driver_registration_failed:
	tz_uiwsock_fini();
uiwsock_initialization_failed:
	tz_cdev_unregister(&tzdev_cdev);
	panic("tzdev: failed to initialize device, rc=%d\n", rc);

	return rc;
}

module_init(init_tzdev);