1 /*
2  * Copyright (c) 2014, Mentor Graphics Corporation
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #ifndef RPMSG_RETARGET_H
9 #define RPMSG_RETARGET_H
10 
11 #include <metal/mutex.h>
12 #include <openamp/open_amp.h>
13 #include <stdint.h>
14 
15 #if defined __cplusplus
16 extern "C" {
17 #endif
18 
19 /* File Operations System call definitions */
20 #define OPEN_SYSCALL_ID  0x1UL
21 #define CLOSE_SYSCALL_ID 0x2UL
22 #define WRITE_SYSCALL_ID 0x3UL
23 #define READ_SYSCALL_ID  0x4UL
24 #define ACK_STATUS_ID    0x5UL
25 
26 #define TERM_SYSCALL_ID  0x6UL
27 
28 #define DEFAULT_PROXY_ENDPOINT  0xFFUL
29 
30 struct rpmsg_rpc_data;
31 
32 typedef int (*rpmsg_rpc_poll)(void *arg);
33 typedef void (*rpmsg_rpc_shutdown_cb)(struct rpmsg_rpc_data *rpc);
34 
35 struct rpmsg_rpc_syscall_header {
36 	int32_t int_field1;
37 	int32_t int_field2;
38 	uint32_t data_len;
39 };
40 
41 struct rpmsg_rpc_syscall {
42 	uint32_t id;
43 	struct rpmsg_rpc_syscall_header args;
44 };
45 
46 struct rpmsg_rpc_data {
47 	struct rpmsg_endpoint ept;
48 	int ept_destroyed;
49 	atomic_flag nacked;
50 	void *respbuf;
51 	size_t respbuf_len;
52 	rpmsg_rpc_poll poll;
53 	void *poll_arg;
54 	rpmsg_rpc_shutdown_cb shutdown_cb;
55 	metal_mutex_t lock;
56 	struct metal_spinlock buflock;
57 };
58 
59 /**
60  * @internal
61  *
62  * @brief Initialize RPMsg remote procedure call
63  *
64  * This function is to initialize the remote procedure call
65  * global data. RPMsg RPC will send request to remote and
66  * wait for callback.
67  *
68  * @param rpc		Pointer to the global remote procedure call data
69  * @param rdev		Pointer to the rpmsg device
70  * @param ept_name	Name of the endpoint used by RPC
71  * @param ept_addr	Address of the endpoint used by RPC
72  * @param ept_raddr	Remote address of the endpoint used by RPC
73  * @param poll_arg	Pointer to poll function argument
74  * @param poll		Poll function
75  * @param shutdown_cb	Shutdown callback function
76  *
77  * @return 0 for success, and negative value for failure.
78  */
79 int rpmsg_rpc_init(struct rpmsg_rpc_data *rpc,
80 		   struct rpmsg_device *rdev,
81 		   const char *ept_name, uint32_t ept_addr,
82 		   uint32_t ept_raddr,
83 		   void *poll_arg, rpmsg_rpc_poll poll,
84 		   rpmsg_rpc_shutdown_cb shutdown_cb);
85 
86 /**
87  * @internal
88  *
89  * @brief Release RPMsg remote procedure call
90  *
91  * This function is to release remoteproc procedure call
92  * global data.
93  *
94  * @param rpc	Pointer to the global remote procedure call
95  */
96 void rpmsg_rpc_release(struct rpmsg_rpc_data *rpc);
97 
98 /**
99  * @internal
100  *
101  * @brief Request RPMsg RPC call
102  *
103  * This function sends RPC request it will return with the length
104  * of data and the response buffer.
105  *
106  * @param rpc		Pointer to remoteproc procedure call data struct
107  * @param req		Pointer to request buffer
108  * @param len		Length of the request data
109  * @param resp		Pointer to where store the response buffer
110  * @param resp_len	Length of the response buffer
111  *
112  * @return Length of the received response, negative value for failure.
113  */
114 int rpmsg_rpc_send(struct rpmsg_rpc_data *rpc,
115 		   void *req, size_t len,
116 		   void *resp, size_t resp_len);
117 
118 /**
119  * @internal
120  *
121  * @brief Set default RPMsg RPC data
122  *
123  * The default RPC data is used to redirect standard C file operations
124  * to RPMsg channels.
125  *
126  * @param rpc	Pointer to remoteproc procedure call data struct
127  */
128 void rpmsg_set_default_rpc(struct rpmsg_rpc_data *rpc);
129 
130 #if defined __cplusplus
131 }
132 #endif
133 
134 #endif /* RPMSG_RETARGET_H */
135