xref: /optee_os/core/kernel/msg_param.c (revision 50f243138c228046a9313eac7d530b6f001db115)
1 /*
2  * Copyright (c) 2017, EPAM Systems
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <optee_msg.h>
29 #include <stdio.h>
30 #include <types_ext.h>
31 #include <kernel/msg_param.h>
32 #include <mm/mobj.h>
33 
34 bool msg_param_init_memparam(struct optee_msg_param *param, struct mobj *mobj,
35 			     size_t offset, size_t size,
36 			     uint64_t cookie, enum msg_param_mem_dir dir)
37 {
38 	if (mobj_matches(mobj, CORE_MEM_REG_SHM)) {
39 		/* Registered SHM mobj */
40 		switch (dir) {
41 		case MSG_PARAM_MEM_DIR_IN:
42 			param->attr = OPTEE_MSG_ATTR_TYPE_RMEM_INPUT;
43 			break;
44 		case MSG_PARAM_MEM_DIR_OUT:
45 			param->attr = OPTEE_MSG_ATTR_TYPE_RMEM_OUTPUT;
46 			break;
47 		case MSG_PARAM_MEM_DIR_INOUT:
48 			param->attr = OPTEE_MSG_ATTR_TYPE_RMEM_INOUT;
49 			break;
50 		default:
51 			return false;
52 		}
53 
54 		param->u.rmem.size = size;
55 		param->u.rmem.offs = offset;
56 		param->u.rmem.shm_ref = cookie;
57 	} else if (mobj_matches(mobj, CORE_MEM_NSEC_SHM)) {
58 		/* MOBJ from from predefined pool */
59 		paddr_t pa;
60 
61 		if (mobj_get_pa(mobj, 0, 0, &pa) != TEE_SUCCESS)
62 			return false;
63 
64 		switch (dir) {
65 		case MSG_PARAM_MEM_DIR_IN:
66 			param->attr = OPTEE_MSG_ATTR_TYPE_TMEM_INPUT;
67 			break;
68 		case MSG_PARAM_MEM_DIR_OUT:
69 			param->attr = OPTEE_MSG_ATTR_TYPE_TMEM_OUTPUT;
70 			break;
71 		case MSG_PARAM_MEM_DIR_INOUT:
72 			param->attr = OPTEE_MSG_ATTR_TYPE_TMEM_INOUT;
73 			break;
74 		default:
75 			return false;
76 		}
77 
78 		param->u.tmem.buf_ptr = pa + offset;
79 		param->u.tmem.shm_ref = cookie;
80 		param->u.tmem.size = size;
81 	} else
82 		return false;
83 	return true;
84 }
85