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_int 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  * rpmsg_rpc_init - initialize RPMsg remote procedure call
61  *
62  * This function is to initialize the remote procedure call
63  * global data. RPMsg RPC will send request to remote and
64  * wait for callback.
65  *
66  * @rpc: pointer to the global remote procedure call data
67  * @rdev: pointer to the rpmsg device
68  * @ept_name: name of the endpoint used by RPC
69  * @ept_addr: address of the endpoint used by RPC
70  * @ept_raddr: remote address of the endpoint used by RPC
71  * @poll_arg: pointer to poll function argument
72  * @poll: poll function
73  * @shutdown_cb: shutdown callback function
74  *
75  * return 0 for success, and negative value for failure.
76  */
77 int rpmsg_rpc_init(struct rpmsg_rpc_data *rpc,
78 		   struct rpmsg_device *rdev,
79 		   const char *ept_name, uint32_t ept_addr,
80 		   uint32_t ept_raddr,
81 		   void *poll_arg, rpmsg_rpc_poll poll,
82 		   rpmsg_rpc_shutdown_cb shutdown_cb);
83 
84 /**
85  * rpmsg_rpc_release - release RPMsg remote procedure call
86  *
87  * This function is to release remoteproc procedure call
88  * global data.
89  *
90  * @rpc: pointer to the globacl remote procedure call
91  */
92 void rpmsg_rpc_release(struct rpmsg_rpc_data *rpc);
93 
94 /**
95  * rpmsg_rpc_send - Request RPMsg RPC call
96  *
97  * This function sends RPC request it will return with the length
98  * of data and the response buffer.
99  *
100  * @rpc: pointer to remoteproc procedure call data struct
101  * @req: pointer to request buffer
102  * @len: length of the request data
103  * @resp: pointer to where store the response buffer
104  * @resp_len: length of the response buffer
105  *
106  * return length of the received response, negative value for failure.
107  */
108 int rpmsg_rpc_send(struct rpmsg_rpc_data *rpc,
109 		   void *req, size_t len,
110 		   void *resp, size_t resp_len);
111 
112 /**
113  * rpmsg_set_default_rpc - set default RPMsg RPC data
114  *
115  * The default RPC data is used to redirect standard C file operations
116  * to RPMsg channels.
117  *
118  * @rpc: pointer to remoteproc procedure call data struct
119  */
120 void rpmsg_set_default_rpc(struct rpmsg_rpc_data *rpc);
121 
122 #if defined __cplusplus
123 }
124 #endif
125 
126 #endif /* RPMSG_RETARGET_H */
127