1419b0af8Sopenharmony_ci/*
2419b0af8Sopenharmony_ci * Copyright (c) 2012-2022 Huawei Technologies Co., Ltd.
3419b0af8Sopenharmony_ci * Description: memory init, register for mailbox pool.
4419b0af8Sopenharmony_ci *
5419b0af8Sopenharmony_ci * This software is licensed under the terms of the GNU General Public
6419b0af8Sopenharmony_ci * License version 2, as published by the Free Software Foundation, and
7419b0af8Sopenharmony_ci * may be copied, distributed, and modified under those terms.
8419b0af8Sopenharmony_ci *
9419b0af8Sopenharmony_ci * This program is distributed in the hope that it will be useful,
10419b0af8Sopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
11419b0af8Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12419b0af8Sopenharmony_ci * GNU General Public License for more details.
13419b0af8Sopenharmony_ci */
14419b0af8Sopenharmony_ci
15419b0af8Sopenharmony_ci#include "static_ion_mem.h"
16419b0af8Sopenharmony_ci#include <linux/slab.h>
17419b0af8Sopenharmony_ci#include <linux/uaccess.h>
18419b0af8Sopenharmony_ci#include <linux/module.h>
19419b0af8Sopenharmony_ci#include <linux/mempool.h>
20419b0af8Sopenharmony_ci#include <linux/vmalloc.h>
21419b0af8Sopenharmony_ci#ifdef DEF_ENG
22419b0af8Sopenharmony_ci#include <asm/io.h>
23419b0af8Sopenharmony_ci#include <linux/mman.h>
24419b0af8Sopenharmony_ci#endif
25419b0af8Sopenharmony_ci#include "smc_smp.h"
26419b0af8Sopenharmony_ci#include "teek_ns_client.h"
27419b0af8Sopenharmony_ci#include "mailbox_mempool.h"
28419b0af8Sopenharmony_ci#include "tc_ns_log.h"
29419b0af8Sopenharmony_ci#include "declare_static_ion.h"
30419b0af8Sopenharmony_ci
31419b0af8Sopenharmony_ci/* send the ion static memory to tee */
32419b0af8Sopenharmony_ciint tc_ns_register_ion_mem(void)
33419b0af8Sopenharmony_ci{
34419b0af8Sopenharmony_ci	struct tc_ns_smc_cmd smc_cmd = {{0}, 0};
35419b0af8Sopenharmony_ci	int ret = 0;
36419b0af8Sopenharmony_ci	struct mb_cmd_pack *mb_pack = NULL;
37419b0af8Sopenharmony_ci	struct register_ion_mem_tag *memtag = NULL;
38419b0af8Sopenharmony_ci
39419b0af8Sopenharmony_ci	mb_pack = mailbox_alloc_cmd_pack();
40419b0af8Sopenharmony_ci	if (!mb_pack) {
41419b0af8Sopenharmony_ci		tloge("mailbox alloc failed\n");
42419b0af8Sopenharmony_ci		return -ENOMEM;
43419b0af8Sopenharmony_ci	}
44419b0af8Sopenharmony_ci	memtag = mailbox_alloc(sizeof(*memtag), 0);
45419b0af8Sopenharmony_ci	if (!memtag) {
46419b0af8Sopenharmony_ci		mailbox_free(mb_pack);
47419b0af8Sopenharmony_ci		return -ENOMEM;
48419b0af8Sopenharmony_ci	}
49419b0af8Sopenharmony_ci	set_ion_mem_info(memtag);
50419b0af8Sopenharmony_ci	smc_cmd.cmd_type = CMD_TYPE_GLOBAL;
51419b0af8Sopenharmony_ci	smc_cmd.cmd_id = GLOBAL_CMD_ID_REGISTER_ION_MEM;
52419b0af8Sopenharmony_ci
53419b0af8Sopenharmony_ci	mb_pack->operation.paramtypes = TEE_PARAM_TYPE_MEMREF_INPUT;
54419b0af8Sopenharmony_ci	mb_pack->operation.params[0].memref.buffer =
55419b0af8Sopenharmony_ci	mailbox_virt_to_phys((uintptr_t)(void *)memtag);
56419b0af8Sopenharmony_ci	mb_pack->operation.buffer_h_addr[0] =
57419b0af8Sopenharmony_ci	(uint64_t)mailbox_virt_to_phys((uintptr_t)(void *)memtag) >> ADDR_TRANS_NUM;
58419b0af8Sopenharmony_ci	mb_pack->operation.params[0].memref.size = sizeof(*memtag);
59419b0af8Sopenharmony_ci
60419b0af8Sopenharmony_ci	smc_cmd.operation_phys = mailbox_virt_to_phys((uintptr_t)&mb_pack->operation);
61419b0af8Sopenharmony_ci	smc_cmd.operation_h_phys =
62419b0af8Sopenharmony_ci	(uint64_t)mailbox_virt_to_phys((uintptr_t)&mb_pack->operation) >> ADDR_TRANS_NUM;
63419b0af8Sopenharmony_ci
64419b0af8Sopenharmony_ci	if (tc_ns_smc(&smc_cmd)) {
65419b0af8Sopenharmony_ci		ret = -EPERM;
66419b0af8Sopenharmony_ci		tloge("send ion mem info failed\n");
67419b0af8Sopenharmony_ci	}
68419b0af8Sopenharmony_ci	mailbox_free(mb_pack);
69419b0af8Sopenharmony_ci	mailbox_free(memtag);
70419b0af8Sopenharmony_ci
71419b0af8Sopenharmony_ci	return ret;
72419b0af8Sopenharmony_ci}