1419b0af8Sopenharmony_ci/*
2419b0af8Sopenharmony_ci * Copyright (C) 2022 Huawei Technologies Co., Ltd.
3419b0af8Sopenharmony_ci * Decription: agent manager function definition, such as register and send cmd
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#ifndef AGENT_H
15419b0af8Sopenharmony_ci#define AGENT_H
16419b0af8Sopenharmony_ci#include <linux/fs.h>
17419b0af8Sopenharmony_ci#include "teek_ns_client.h"
18419b0af8Sopenharmony_ci
19419b0af8Sopenharmony_ci#define MAX_PATH_SIZE		 512
20419b0af8Sopenharmony_ci#define AGENT_FS_ID		     0x46536673 /* FSfs */
21419b0af8Sopenharmony_ci#define AGENT_MISC_ID		 0x4d495343 /* MISC */
22419b0af8Sopenharmony_ci
23419b0af8Sopenharmony_ci#ifdef CONFIG_RPMB_AGENT
24419b0af8Sopenharmony_ci#define TEE_RPMB_AGENT_ID	 0x4abe6198 /* RPMB */
25419b0af8Sopenharmony_ci#endif
26419b0af8Sopenharmony_ci
27419b0af8Sopenharmony_ci#define AGENT_SOCKET_ID	      0x69e85664 /* socket */
28419b0af8Sopenharmony_ci#define SECFILE_LOAD_AGENT_ID 0x4c4f4144 /* SECFILE-LOAD-AGENT */
29419b0af8Sopenharmony_ci#define TEE_SECE_AGENT_ID     0x53656345 /* npu agent id */
30419b0af8Sopenharmony_ci#define TEE_FACE_AGENT1_ID    0x46616365 /* face agent id */
31419b0af8Sopenharmony_ci#define TEE_FACE_AGENT2_ID    0x46616345 /* face agent id */
32419b0af8Sopenharmony_ci#define TEE_VLTMM_AGENT_ID    0x564c544d /* vltmm agent id */
33419b0af8Sopenharmony_ci#define SYSTEM_UID		      1000
34419b0af8Sopenharmony_ci#define MS_TO_NS			  1000000
35419b0af8Sopenharmony_ci
36419b0af8Sopenharmony_cienum agent_state_type {
37419b0af8Sopenharmony_ci	AGENT_CRASHED = 0,
38419b0af8Sopenharmony_ci	AGENT_REGISTERED,
39419b0af8Sopenharmony_ci	AGENT_READY,
40419b0af8Sopenharmony_ci};
41419b0af8Sopenharmony_ci
42419b0af8Sopenharmony_cienum agent_status {
43419b0af8Sopenharmony_ci	AGENT_ALIVE = 1,
44419b0af8Sopenharmony_ci	AGENT_DEAD = 0,
45419b0af8Sopenharmony_ci};
46419b0af8Sopenharmony_ci
47419b0af8Sopenharmony_ci/* for secure agent */
48419b0af8Sopenharmony_cistruct smc_event_data {
49419b0af8Sopenharmony_ci	unsigned int agent_id;
50419b0af8Sopenharmony_ci	atomic_t agent_ready;
51419b0af8Sopenharmony_ci	wait_queue_head_t wait_event_wq;
52419b0af8Sopenharmony_ci	int ret_flag; /* indicate whether agent is returned from TEE */
53419b0af8Sopenharmony_ci	wait_queue_head_t send_response_wq;
54419b0af8Sopenharmony_ci	struct list_head head;
55419b0af8Sopenharmony_ci	struct tc_ns_smc_cmd cmd;
56419b0af8Sopenharmony_ci	struct tc_ns_dev_file *owner;
57419b0af8Sopenharmony_ci	void *agent_buff_kernel;
58419b0af8Sopenharmony_ci	void *agent_buff_user; /* used for unmap */
59419b0af8Sopenharmony_ci	unsigned int agent_buff_size;
60419b0af8Sopenharmony_ci	atomic_t usage;
61419b0af8Sopenharmony_ci	wait_queue_head_t ca_pending_wq;
62419b0af8Sopenharmony_ci	/* indicate whether agent is allowed to return to TEE */
63419b0af8Sopenharmony_ci	atomic_t ca_run;
64419b0af8Sopenharmony_ci};
65419b0af8Sopenharmony_ci
66419b0af8Sopenharmony_cistruct tee_agent_kernel_ops {
67419b0af8Sopenharmony_ci	const char *agent_name;
68419b0af8Sopenharmony_ci	unsigned int agent_id;
69419b0af8Sopenharmony_ci	int (*tee_agent_init)(struct tee_agent_kernel_ops *agent_instance);
70419b0af8Sopenharmony_ci	int (*tee_agent_run)(struct tee_agent_kernel_ops *agent_instance);
71419b0af8Sopenharmony_ci	int (*tee_agent_work)(struct tee_agent_kernel_ops *agent_instance);
72419b0af8Sopenharmony_ci	int (*tee_agent_stop)(struct tee_agent_kernel_ops *agent_instance);
73419b0af8Sopenharmony_ci	int (*tee_agent_exit)(struct tee_agent_kernel_ops *agent_instance);
74419b0af8Sopenharmony_ci	int (*tee_agent_crash_work)(
75419b0af8Sopenharmony_ci		struct tee_agent_kernel_ops *agent_instance,
76419b0af8Sopenharmony_ci		struct tc_ns_client_context *context,
77419b0af8Sopenharmony_ci		unsigned int dev_file_id);
78419b0af8Sopenharmony_ci	struct task_struct *agent_thread;
79419b0af8Sopenharmony_ci	void *agent_data;
80419b0af8Sopenharmony_ci	void *agent_buff;
81419b0af8Sopenharmony_ci	unsigned int agent_buff_size;
82419b0af8Sopenharmony_ci	struct list_head list;
83419b0af8Sopenharmony_ci};
84419b0af8Sopenharmony_ci
85419b0af8Sopenharmony_cistruct ca_info {
86419b0af8Sopenharmony_ci	char path[MAX_PATH_SIZE];
87419b0af8Sopenharmony_ci	uint32_t uid;
88419b0af8Sopenharmony_ci	uint32_t agent_id;
89419b0af8Sopenharmony_ci};
90419b0af8Sopenharmony_ci
91419b0af8Sopenharmony_cistatic inline void get_agent_event(struct smc_event_data *event_data)
92419b0af8Sopenharmony_ci{
93419b0af8Sopenharmony_ci	if (event_data)
94419b0af8Sopenharmony_ci		atomic_inc(&event_data->usage);
95419b0af8Sopenharmony_ci}
96419b0af8Sopenharmony_ci
97419b0af8Sopenharmony_cistatic inline void put_agent_event(struct smc_event_data *event_data)
98419b0af8Sopenharmony_ci{
99419b0af8Sopenharmony_ci	if (event_data) {
100419b0af8Sopenharmony_ci		if (atomic_dec_and_test(&event_data->usage))
101419b0af8Sopenharmony_ci			kfree(event_data);
102419b0af8Sopenharmony_ci	}
103419b0af8Sopenharmony_ci}
104419b0af8Sopenharmony_ci
105419b0af8Sopenharmony_ciint is_allowed_agent_ca(const struct ca_info *ca,
106419b0af8Sopenharmony_ci	bool check_agent_id);
107419b0af8Sopenharmony_civoid agent_init(void);
108419b0af8Sopenharmony_civoid free_agent(void);
109419b0af8Sopenharmony_cistruct smc_event_data *find_event_control(unsigned int agent_id);
110419b0af8Sopenharmony_civoid send_event_response(unsigned int agent_id);
111419b0af8Sopenharmony_ciint agent_process_work(const struct tc_ns_smc_cmd *smc_cmd, unsigned int agent_id);
112419b0af8Sopenharmony_ciint is_agent_alive(unsigned int agent_id);
113419b0af8Sopenharmony_ciint tc_ns_set_native_hash(unsigned long arg, unsigned int cmd_id);
114419b0af8Sopenharmony_ciint tc_ns_late_init(unsigned long arg);
115419b0af8Sopenharmony_ciint tc_ns_register_agent(struct tc_ns_dev_file *dev_file, unsigned int agent_id,
116419b0af8Sopenharmony_ci	unsigned int buffer_size, void **buffer, bool user_agent);
117419b0af8Sopenharmony_ciint tc_ns_unregister_agent(unsigned int agent_id);
118419b0af8Sopenharmony_civoid send_crashed_event_response_all(const struct tc_ns_dev_file *dev_file);
119419b0af8Sopenharmony_ciint tc_ns_wait_event(unsigned int agent_id);
120419b0af8Sopenharmony_ciint tc_ns_send_event_response(unsigned int agent_id);
121419b0af8Sopenharmony_civoid send_event_response_single(const struct tc_ns_dev_file *dev_file);
122419b0af8Sopenharmony_ciint sync_system_time_from_user(const struct tc_ns_client_time *user_time);
123419b0af8Sopenharmony_civoid sync_system_time_from_kernel(void);
124419b0af8Sopenharmony_ciint tee_agent_clear_work(struct tc_ns_client_context *context,
125419b0af8Sopenharmony_ci	unsigned int dev_file_id);
126419b0af8Sopenharmony_ciint tee_agent_kernel_register(struct tee_agent_kernel_ops *new_agent);
127419b0af8Sopenharmony_cibool is_system_agent(const struct tc_ns_dev_file *dev_file);
128419b0af8Sopenharmony_civoid tee_agent_clear_dev_owner(const struct tc_ns_dev_file *dev_file);
129419b0af8Sopenharmony_cichar *get_proc_dpath(char *path, int path_len);
130419b0af8Sopenharmony_ciint check_ext_agent_access(uint32_t agent_id);
131419b0af8Sopenharmony_ci
132419b0af8Sopenharmony_ci#endif
133