1 /* Copyright (c) 2015-2018, The Linux Foundation. All rights reserved.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 and
5  * only version 2 as published by the Free Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  */
12 
13 #ifndef _SPCOM_H_
14 #define _SPCOM_H_
15 
16 #include <linux/types.h>	/* uint32_t, bool */
17 #ifndef BIT
18 	#define BIT(x) (1 << x)
19 #endif
20 
21 #ifndef PAGE_SIZE
22 	#define PAGE_SIZE 4096
23 #endif
24 
25 /**
26  * @brief - Secure Processor Communication interface to user space spcomlib.
27  *
28  * Sending data and control commands by write() file operation.
29  * Receiving data by read() file operation.
30  * Getting the next request size by read() file operation,
31  * with special size SPCOM_GET_NEXT_REQUEST_SIZE.
32  */
33 
34 /*
35  * Maximum number of channel between Secure Processor and HLOS.
36  * including predefined channels, like "sp_kernel".
37  */
38 #define SPCOM_MAX_CHANNELS	0x20
39 
40 /* Maximum size (including null) for channel names */
41 #define SPCOM_CHANNEL_NAME_SIZE		32
42 /*
43  * file read(fd, buf, size) with this size,
44  * hints the kernel that user space wants to read the next-req-size.
45  * This size is bigger than both SPCOM_MAX_REQUEST_SIZE and
46  * SPCOM_MAX_RESPONSE_SIZE , so it is not a valid data size.
47  */
48 #define SPCOM_GET_NEXT_REQUEST_SIZE	(PAGE_SIZE-1)
49 
50 /* Command Id between spcomlib and spcom driver, on write() */
51 enum spcom_cmd_id {
52 	SPCOM_CMD_LOAD_APP	= 0x4C4F4144, /* "LOAD" = 0x4C4F4144 */
53 	SPCOM_CMD_RESET_SP	= 0x52455354, /* "REST" = 0x52455354 */
54 	SPCOM_CMD_SEND		= 0x53454E44, /* "SEND" = 0x53454E44 */
55 	SPCOM_CMD_SEND_MODIFIED	= 0x534E444D, /* "SNDM" = 0x534E444D */
56 	SPCOM_CMD_LOCK_ION_BUF  = 0x4C4F434B, /* "LOCK" = 0x4C4F434B */
57 	SPCOM_CMD_UNLOCK_ION_BUF = 0x554C434B, /* "ULCK" = 0x4C4F434B */
58 	SPCOM_CMD_FSSR		= 0x46535352, /* "FSSR" = 0x46535352 */
59 	SPCOM_CMD_CREATE_CHANNEL = 0x43524554, /* "CRET" = 0x43524554 */
60 #define SPCOM_CMD_RESTART_SP \
61 	SPCOM_CMD_RESTART_SP
62 	SPCOM_CMD_RESTART_SP    = 0x52535452, /* "RSTR" = 0x52535452 */
63 };
64 
65 /*
66  * @note: Event types that are always implicitly polled:
67  * POLLERR=0x08 | POLLHUP=0x10 | POLLNVAL=0x20
68  * so bits 3,4,5 can't be used
69  */
70 enum spcom_poll_events {
71 	SPCOM_POLL_LINK_STATE	= BIT(1),
72 	SPCOM_POLL_CH_CONNECT	= BIT(2),
73 	SPCOM_POLL_READY_FLAG	= BIT(14), /* output */
74 	SPCOM_POLL_WAIT_FLAG	= BIT(15), /* if set , wait for the event */
75 };
76 
77 /* Common Command structure between User Space and spcom driver, on write() */
78 struct spcom_user_command {
79 	enum spcom_cmd_id cmd_id;
80 	uint32_t arg;
81 } __attribute__((packed));
82 
83 /* Command structure between User Space and spcom driver, on write() */
84 struct spcom_send_command {
85 	enum spcom_cmd_id cmd_id;
86 	uint32_t timeout_msec;
87 	uint32_t buf_size;
88 	char buf[0]; /* Variable buffer size - must be last field */
89 } __attribute__((packed));
90 
91 /* Command structure between userspace spcomlib and spcom driver, on write() */
92 struct spcom_user_create_channel_command {
93 	enum spcom_cmd_id cmd_id;
94 	char ch_name[SPCOM_CHANNEL_NAME_SIZE];
95 } __attribute__((packed));
96 
97 /* Command structure between userspace spcomlib and spcom driver, on write() */
98 #define SPCOM_USER_RESTART_SP_CMD
99 struct spcom_user_restart_sp_command {
100 	enum spcom_cmd_id cmd_id;
101 	uint32_t arg;
102 } __attribute__((packed));
103 
104 /* maximum ION buf for send-modfied-command */
105 #define SPCOM_MAX_ION_BUF 4
106 
107 struct spcom_ion_info {
108 	int32_t fd; /* ION buffer File Descriptor, set -1 for invalid fd */
109 	uint32_t buf_offset; /* virtual address offset in request/response */
110 };
111 
112 /* Pass this FD to unlock all ION buffer for the specific channel */
113 #define SPCOM_ION_FD_UNLOCK_ALL	0xFFFF
114 
115 struct spcom_ion_handle {
116 	int32_t fd;		/* File Descriptor associated with the buffer */
117 };
118 
119 /* Command structure between User Space and spcom driver, on write() */
120 struct spcom_user_send_modified_command {
121 	enum spcom_cmd_id cmd_id;
122 	struct spcom_ion_info ion_info[SPCOM_MAX_ION_BUF];
123 	uint32_t timeout_msec;
124 	uint32_t buf_size;
125 	char buf[0]; /* Variable buffer size - must be last field */
126 } __attribute__((packed));
127 
128 
129 #endif /* _SPCOM_H_ */
130