summaryrefslogtreecommitdiff
path: root/libsecurepath/sec_g2ddrm.cpp
blob: ba36321a3364e5d708884a694f976f51fdab9517 (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
/*
 * Copyright (C) 2012 Samsung Electronics Co., LTD
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ioctl.h>

#include "tlsecdrm_api.h"
#define LOG_TAG "sec_g2ddrm"
#include "log.h"
#include "tlc_communication.h"
#include "sec_g2ddrm.h"

#define SMEM_PATH	"/dev/s5p-smem"
#define SECMEM_IOC_GET_FD_PHYS_ADDR _IOWR('S', 8, struct secfd_info)
#define ion_phys_addr_t unsigned long

struct secfd_info {
	int fd;
	ion_phys_addr_t phys;
};

mc_comm_ctx ctx;
int g_fd_secmem;

static mcResult_t tlc_initialize(void) {
	mcResult_t mcRet;

	memset(&ctx, 0x00, sizeof(ctx));
	ctx.device_id = MC_DEVICE_ID_DEFAULT;
	ctx.uuid = (mcUuid_t)TL_SECDRM_UUID;
	ctx.initialized = false;

	mcRet = tlc_open(&ctx);
	if (MC_DRV_OK != mcRet) {
		   LOG_E("open TL session failed!");
		   return mcRet;
	}

	ctx.initialized = true;

	return MC_DRV_OK;
}

static mcResult_t tlc_terminate(void) {
	mcResult_t mcRet;

	if (ctx.initialized == true) {
		mcRet = tlc_close(&ctx);
		if (MC_DRV_OK != mcRet) {
			   LOG_E("close TL session failed!");
			   return mcRet;
		}

		memset(&ctx, 0x00, sizeof(ctx));
		ctx.initialized = false;
	}

	return MC_DRV_OK;
}

int get_fd_phyaddr_from_kernel(struct secfd_info *secfd)
{
	int ret;

	ret = ioctl(g_fd_secmem, SECMEM_IOC_GET_FD_PHYS_ADDR, secfd);
	if (ret != 0) {
		LOG_E("Fail to get SECFD info: ret(%s)", strerror(errno));
		return -1;
	}

	LOG_I("___ION_FD_KERNEL::fd(%d), phyaddr(0x%x)", secfd->fd, secfd->phys);
	return 0;
}

g2ddrmResult_t sec_g2d_activate(enum driver_act act)
{
	int m_g2dFd;

	m_g2dFd = open(SEC_G2D_DEV_NAME, O_RDWR);
	if (m_g2dFd < 0) {
		LOG_E("%s::open(%s) fail(%s)\n", __func__, SEC_G2D_DEV_NAME, strerror(errno));
		return G2DDRM_ERROR_INIT_FAILED;
	}

	enum driver_act g2d_act = act;
	if (ioctl(m_g2dFd, FIMG2D_BITBLT_ACTIVATE, &g2d_act) < 0) {
		LOG_E("%s::ioctl(%s) %d fail(%s)\n", __func__, SEC_G2D_DEV_NAME, act, strerror(errno));
		close(m_g2dFd);
		return G2DDRM_ERROR_INIT_FAILED;
	}
	close(m_g2dFd);
	LOG_I("G2D %s is completed", act==DRV_DEACT? "DEACT":"ACT");

	return G2DDRM_SUCCESS;
}

extern "C" g2ddrmResult_t G2DDRM_Initialize(void)
{
	mcResult_t mcRet;
	g2ddrmResult_t ret = G2DDRM_SUCCESS;
	struct tciMessage_t *tci = NULL;

	LOG_I("G2DDRM_Initialize(): secure G2D driver initialization");
	do {
		if (sec_g2d_activate(DRV_DEACT) != 0) {
			ret = G2DDRM_ERROR_INIT_FAILED;
			break;
		}

		LOG_I("Open the Trustlet");

		g_fd_secmem = open(SMEM_PATH, O_RDWR);
		if (g_fd_secmem < 0) {
			LOG_E("open S5P-MEM device error");
			ret = G2DDRM_ERROR_INIT_FAILED;
			break;
		}

		mcRet = tlc_initialize();
		if (MC_DRV_OK != mcRet) {
			LOG_E("Tlc Open Error");
			ret = G2DDRM_ERROR_INIT_FAILED;
			close(g_fd_secmem);
			break;
		}

		LOG_I("Check TCI buffer");
		tci = ctx.tci_msg;
		if (NULL == tci) {
			LOG_E("TCI has not been set up properly - exiting");
			ret = G2DDRM_ERROR_INIT_FAILED;
			close(g_fd_secmem);
			break;
		}

		LOG_I("Prepare command message in TCI");
		tci->cmd.id = CMD_G2DDRM_INITIALIZE;
		mcRet = tlc_communicate(&ctx);
		if (MC_DRV_OK != mcRet) {
			LOG_E("tlc_communicate Error!");
			ret = G2DDRM_ERROR_INIT_FAILED;
			close(g_fd_secmem);
			break;
		}

		if ((RSP_ID(CMD_G2DDRM_INITIALIZE) != tci->resp.id)) {
			LOG_E("Trustlet did not send a response : %d", tci->resp.id);
			ret = G2DDRM_ERROR_INIT_FAILED;
			close(g_fd_secmem);
			break;
		}
		LOG_I("Trustlet response is completed");

		if (tci->resp.return_code != RET_TL_G2DDRM_OK) {
			LOG_E("Trustlet did not send a valid return code : %d", tci->resp.return_code);
			ret = G2DDRM_ERROR_INIT_FAILED;
			close(g_fd_secmem);
			break;
		}
		LOG_I("Check the Trustlet return code is completed");

		ret = G2DDRM_SUCCESS;
	} while (false);

	LOG_I("G2DDRM_Initialize(): secure G2D driver is initialized. ret(%d)", ret);
	return ret;
}

extern "C" g2ddrmResult_t G2DDRM_Blit(struct fimg2d_blit_raw *cmd)
{
	mcResult_t mcRet;
	g2ddrmResult_t ret = G2DDRM_SUCCESS;
	mcBulkMap_t mapInfo;
	struct tciMessage_t *tci = NULL;
	struct secfd_info secfd;

	mapInfo.sVirtualAddr = NULL;

	do {
		secfd.fd = cmd->src.addr.start;
		if (get_fd_phyaddr_from_kernel(&secfd) < 0) {
			LOG_E("fail to get src phyaddr from fd(%d)", secfd.fd);
			ret = G2DDRM_ERROR_BLIT_FAILED;
			break;
		}
		cmd->src.addr.start = secfd.phys;

		secfd.fd = cmd->dst.addr.start;
		if (get_fd_phyaddr_from_kernel(&secfd) < 0) {
			LOG_E("fail to get dst phyaddr from fd(%d)", secfd.fd);
			ret = G2DDRM_ERROR_BLIT_FAILED;
			break;
		}
		cmd->dst.addr.start = secfd.phys;

		secfd.fd = cmd->dst.plane2.start;
		if (get_fd_phyaddr_from_kernel(&secfd) < 0) {
			LOG_E("fail to get plane2 phyaddr from fd(%d)", secfd.fd);
			ret = G2DDRM_ERROR_BLIT_FAILED;
			break;
		}
		cmd->dst.plane2.start = secfd.phys;

		LOG_I("Check TCI buffer");
		tci = ctx.tci_msg;
		if (NULL == tci) {
			LOG_E("TCI has not been set up properly - exiting");
			ret = G2DDRM_ERROR_BLIT_FAILED;
			close(g_fd_secmem);
			break;
		}
		LOG_I("Prepare command message in TCI");

		tci->cmd.id = CMD_G2DDRM_BLIT;
		tci->blit.op = cmd->op;
		tci->blit.param = cmd->param;
		tci->blit.src = cmd->src;
		tci->blit.dst = cmd->dst;
		tci->blit.msk = cmd->msk;
		tci->blit.tmp = cmd->tmp;
		tci->blit.sync = cmd->sync;
		tci->blit.seq_no = cmd->seq_no;

		mcRet = tlc_communicate(&ctx);
		if (MC_DRV_OK != mcRet) {
			LOG_E("tlc_communicate Error!");
			ret = G2DDRM_ERROR_BLIT_FAILED;
			break;
		}

		if ((RSP_ID(CMD_G2DDRM_BLIT) != tci->resp.id)) {
			LOG_E("Trustlet did not send a response : %d", tci->resp.id);
			ret = G2DDRM_ERROR_BLIT_FAILED;
			break;
		}
		LOG_I("Trustlet response is completed");

		if (tci->resp.return_code != RET_TL_G2DDRM_OK) {
			LOG_E("Trustlet did not send a valid return code : %d", tci->resp.return_code);
			ret = G2DDRM_ERROR_BLIT_FAILED;
			break;
		}
		LOG_I("Check the Trustlet return code is completed");

		ret = G2DDRM_SUCCESS;
	} while (false);

	LOG_I("G2DDRM_Blit(): secure G2D driver blit is done. ret(%d)", ret);
	return ret;
}

extern "C" g2ddrmResult_t G2DDRM_Terminate(void)
{
	mcResult_t mcRet;
	g2ddrmResult_t ret = G2DDRM_SUCCESS;
	struct tciMessage_t *tci = NULL;

	LOG_I("G2DDRM_Terminate(): secure G2D driver termination");
	do {
		LOG_I("Check TCI buffer");

		tci = ctx.tci_msg;
		if (NULL == tci) {
			LOG_E("TCI has not been set up properly - exiting");
			ret = G2DDRM_ERROR_EXIT_FAILED;
			close(g_fd_secmem);
			break;
		}
		LOG_I("Prepare command message in TCI");
		tci->cmd.id = CMD_G2DDRM_TERMINATE;

		mcRet = tlc_communicate(&ctx);
		if (MC_DRV_OK != mcRet) {
			LOG_E("tlc_communicate Error!");
			ret = G2DDRM_ERROR_EXIT_FAILED;
			break;
		}

		if ((RSP_ID(CMD_G2DDRM_TERMINATE) != tci->resp.id)) {
			LOG_E("Trustlet did not send a response : %d", tci->resp.id);
			ret = G2DDRM_ERROR_EXIT_FAILED;
			break;
		}
		LOG_I("Trustlet response is completed");

		if (tci->resp.return_code != RET_TL_G2DDRM_OK) {
			LOG_E("Trustlet did not send a valid return code : %d", tci->resp.return_code);
			ret = G2DDRM_ERROR_EXIT_FAILED;
			break;
		}

		mcRet = tlc_terminate();
		if (MC_DRV_OK != mcRet) {
			LOG_E("Tlc Close Error");
			ret = G2DDRM_ERROR_EXIT_FAILED;
			break;
		}

		LOG_I("Check the Trustlet return code is completed");

		close(g_fd_secmem);
		if (sec_g2d_activate(DRV_ACT) != 0) {
			ret = G2DDRM_ERROR_EXIT_FAILED;
			break;
		}

		ret = G2DDRM_SUCCESS;
	} while (false);

	LOG_I("G2DDRM_Terminate(): secure G2D driver is terminated. ret(%d)", ret);
	return ret;
}