1 /*
2  * Copyright (c) 2012-2022 Huawei Technologies Co., Ltd.
3  * Description: memory init, register for mailbox pool.
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  */
14 
15 #include "static_ion_mem.h"
16 #include <linux/slab.h>
17 #include <linux/uaccess.h>
18 #include <linux/module.h>
19 #include <linux/mempool.h>
20 #include <linux/vmalloc.h>
21 #ifdef DEF_ENG
22 #include <asm/io.h>
23 #include <linux/mman.h>
24 #endif
25 #include "smc_smp.h"
26 #include "teek_ns_client.h"
27 #include "mailbox_mempool.h"
28 #include "tc_ns_log.h"
29 #include "declare_static_ion.h"
30 
31 /* send the ion static memory to tee */
tc_ns_register_ion_mem(void)32 int tc_ns_register_ion_mem(void)
33 {
34 	struct tc_ns_smc_cmd smc_cmd = {{0}, 0};
35 	int ret = 0;
36 	struct mb_cmd_pack *mb_pack = NULL;
37 	struct register_ion_mem_tag *memtag = NULL;
38 
39 	mb_pack = mailbox_alloc_cmd_pack();
40 	if (!mb_pack) {
41 		tloge("mailbox alloc failed\n");
42 		return -ENOMEM;
43 	}
44 	memtag = mailbox_alloc(sizeof(*memtag), 0);
45 	if (!memtag) {
46 		mailbox_free(mb_pack);
47 		return -ENOMEM;
48 	}
49 	set_ion_mem_info(memtag);
50 	smc_cmd.cmd_type = CMD_TYPE_GLOBAL;
51 	smc_cmd.cmd_id = GLOBAL_CMD_ID_REGISTER_ION_MEM;
52 
53 	mb_pack->operation.paramtypes = TEE_PARAM_TYPE_MEMREF_INPUT;
54 	mb_pack->operation.params[0].memref.buffer =
55 	mailbox_virt_to_phys((uintptr_t)(void *)memtag);
56 	mb_pack->operation.buffer_h_addr[0] =
57 	(uint64_t)mailbox_virt_to_phys((uintptr_t)(void *)memtag) >> ADDR_TRANS_NUM;
58 	mb_pack->operation.params[0].memref.size = sizeof(*memtag);
59 
60 	smc_cmd.operation_phys = mailbox_virt_to_phys((uintptr_t)&mb_pack->operation);
61 	smc_cmd.operation_h_phys =
62 	(uint64_t)mailbox_virt_to_phys((uintptr_t)&mb_pack->operation) >> ADDR_TRANS_NUM;
63 
64 	if (tc_ns_smc(&smc_cmd)) {
65 		ret = -EPERM;
66 		tloge("send ion mem info failed\n");
67 	}
68 	mailbox_free(mb_pack);
69 	mailbox_free(memtag);
70 
71 	return ret;
72 }