summaryrefslogtreecommitdiff
path: root/security/tlk_driver/ote_fs.c
blob: f58bdd64aecb0cc5957dd90068cbbf713ab0025a (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
/*
 * Copyright (c) 2013-2014 NVIDIA Corporation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <linux/slab.h>
#include <linux/syscalls.h>
#include <linux/list.h>
#include <linux/completion.h>
#include <linux/workqueue.h>
#include <linux/bitops.h>
#include <linux/uaccess.h>
#include <linux/dma-mapping.h>

#include "ote_protocol.h"

static DECLARE_COMPLETION(req_ready);
static DECLARE_COMPLETION(req_complete);

static struct te_ss_op_legacy *ss_op_shmem_legacy;
static struct te_ss_op *ss_op_shmem;
static uint32_t ss_op_size;

static void indicate_ss_op_complete(void)
{
	tlk_generic_smc(TE_SMC_SS_REQ_COMPLETE, 0, 0);
}

int te_handle_ss_ioctl_legacy(struct file *file, unsigned int ioctl_num,
	unsigned long ioctl_param)
{
	switch (ioctl_num) {
	case TE_IOCTL_SS_NEW_REQ_LEGACY:
		/* wait for a new request */
		if (wait_for_completion_interruptible(&req_ready))
			return -ENODATA;

		/* transfer pending request to daemon's buffer */
		if (copy_to_user((void __user *)ioctl_param, ss_op_shmem_legacy,
					ss_op_size)) {
			pr_err("copy_to_user failed for new request\n");
			return -EFAULT;
		}
		break;

	case TE_IOCTL_SS_REQ_COMPLETE_LEGACY: /* request complete */
		if (copy_from_user(ss_op_shmem_legacy,
			(void __user *)ioctl_param, ss_op_size)) {
			pr_err("copy_from_user failed for request\n");
			return -EFAULT;
		}

		/* signal the producer */
		complete(&req_complete);
		break;
	}

	return 0;
}

void tlk_ss_op_legacy(uint32_t size)
{
	/* store size of request */
	ss_op_size = size;

	/* signal consumer */
	complete(&req_ready);

	/* wait for the consumer's signal */
	wait_for_completion(&req_complete);

	/* signal completion to the secure world */
	indicate_ss_op_complete();
}

static int __init tlk_ss_init_legacy(void)
{
	dma_addr_t ss_op_shmem_dma;

	/* allocate shared memory buffer */
	ss_op_shmem_legacy = dma_alloc_coherent(NULL,
		sizeof(struct te_ss_op_legacy), &ss_op_shmem_dma, GFP_KERNEL);
	if (!ss_op_shmem_legacy) {
		pr_err("%s: no memory available for fs operations\n", __func__);
		return -ENOMEM;
	}

	tlk_generic_smc(TE_SMC_SS_REGISTER_HANDLER_LEGACY,
		(uintptr_t)tlk_ss_op_legacy, (uintptr_t)ss_op_shmem_legacy);

	return 0;
}

arch_initcall(tlk_ss_init_legacy);

int te_handle_ss_ioctl(struct file *file, unsigned int ioctl_num,
	unsigned long ioctl_param)
{
	switch (ioctl_num) {
	case TE_IOCTL_SS_NEW_REQ:
		/* wait for a new request */
		if (wait_for_completion_interruptible(&req_ready))
			return -ENODATA;

		/* transfer pending request to daemon's buffer */
		if (copy_to_user((void __user *)ioctl_param, ss_op_shmem->data,
					ss_op_shmem->req_size)) {
			pr_err("copy_to_user failed for new request\n");
			return -EFAULT;
		}
		break;

	case TE_IOCTL_SS_REQ_COMPLETE: /* request complete */
		if (copy_from_user(ss_op_shmem->data,
			(void __user *)ioctl_param, ss_op_shmem->req_size)) {
			pr_err("copy_from_user failed for request\n");
			return -EFAULT;
		}

		/* signal the producer */
		complete(&req_complete);
		break;
	}

	return 0;
}

void tlk_ss_op(void)
{
	/* signal consumer */
	complete(&req_ready);

	/* wait for the consumer's signal */
	wait_for_completion(&req_complete);
}

static int __init tlk_ss_init(void)
{
	dma_addr_t ss_op_shmem_dma;
	int32_t ret;

	/* allocate shared memory buffer */
	ss_op_shmem = dma_alloc_coherent(NULL, sizeof(struct te_ss_op),
			&ss_op_shmem_dma, GFP_KERNEL);
	if (!ss_op_shmem) {
		pr_err("%s: no memory available for fs operations\n", __func__);
		return -ENOMEM;
	}

	ret = tlk_generic_smc(TE_SMC_SS_REGISTER_HANDLER,
			(uintptr_t)ss_op_shmem, 0);
	if (ret != 0) {
		dma_free_coherent(NULL, sizeof(struct te_ss_op),
			(void *)ss_op_shmem, ss_op_shmem_dma);
		ss_op_shmem = NULL;
		return -ENOTSUPP;
	}

	return 0;
}

arch_initcall(tlk_ss_init);