1 /* This is a test application that runs baremetal code on the remote core
2 and responds to commands from master core to test the usage of rpmsg APIs. */
4 /* Including required headers */
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdlib.h>
8 #include "openamp/open_amp.h"
9 #include "rsc_table.h"
10 #include "test_suite.h"
12 #define EPT_ADDR 59
14 #define LPRINTF(format, ...) printf(format, ##__VA_ARGS__)
15 #define LPERROR(format, ...) LPRINTF("ERROR: " format, ##__VA_ARGS__)
17 /* Application provided callbacks */
18 void rpmsg_channel_created(struct rpmsg_channel *rp_chnl);
19 void rpmsg_channel_deleted(struct rpmsg_channel *rp_chnl);
20 void rpmsg_read_default_cb(struct rpmsg_channel *rp_chnl, void *data, int len,
21 void *pric, unsigned long src);
22 void rpmsg_read_ept_cb(struct rpmsg_channel *rp_chnl, void *data, int len,
23 void *pric, unsigned long src);
25 static void sleep();
27 /* Globals */
28 static volatile int intr_flag = 0;
29 static struct rpmsg_endpoint *rp_ept;
30 static struct rpmsg_channel *app_rp_chnl;
31 static unsigned int Src;
32 static unsigned int Len;
33 static char firmware_name[] = "baremetal-fn-test-suite-remote-firmware";
34 static char r_buffer[512];
35 static struct rsc_table_info rsc_info;
37 /* External functions */
38 extern void init_system();
39 extern void cleanup_system();
40 extern struct hil_proc *platform_create_proc(int proc_index);
41 extern void *get_resource_table (int rsc_id, int *len);
43 int app (struct hil_proc *hproc)
44 {
45 struct remote_proc *proc;
46 int uninit = 0;
47 struct ept_cmd_data *ept_data;
49 /* This API creates the virtio devices for this remote node and initializes
50 other relevant resources defined in the resource table */
51 remoteproc_resource_init(&rsc_info, hproc,
52 rpmsg_channel_created,
53 rpmsg_channel_deleted, rpmsg_read_default_cb,
54 &proc, 0);
56 for (;;) {
58 if (intr_flag) {
59 struct command *cmd = (struct command *)r_buffer;
60 if (cmd->comm_start == CMD_START) {
61 unsigned int cm_code = cmd->comm_code;
62 void *data = cmd->data;
64 switch (cm_code) {
65 case CREATE_EPT:
66 ept_data = (struct ept_cmd_data *)data;
67 rp_ept =
68 rpmsg_create_ept(app_rp_chnl,
69 rpmsg_read_ept_cb,
70 RPMSG_NULL,
71 ept_data->dst);
72 if (rp_ept) {
73 /* Send data back to ack. */
74 rpmsg_sendto(app_rp_chnl,
75 r_buffer, Len,
76 Src);
77 }
78 break;
79 case DELETE_EPT:
80 rpmsg_destroy_ept(rp_ept);
81 rpmsg_sendto(app_rp_chnl, r_buffer, Len,
82 Src);
84 break;
85 case CREATE_CHNL:
86 break;
87 case DELETE_CHNL:
88 rpmsg_sendto(app_rp_chnl, r_buffer, Len,
89 Src);
90 remoteproc_resource_deinit(proc);
91 uninit = 1;
92 break;
93 case QUERY_FW_NAME:
94 rpmsg_send(app_rp_chnl,
95 &firmware_name[0],
96 strlen(firmware_name) + 1);
97 break;
98 default:
99 rpmsg_sendto(app_rp_chnl, r_buffer, Len,
100 Src);
101 break;
102 }
103 } else {
104 rpmsg_sendto(app_rp_chnl, r_buffer, Len, Src);
105 }
106 intr_flag = 0;
107 if (uninit)
108 break;
109 }
111 hil_poll(proc, 0);
112 }
114 cleanup_system();
115 return 0;
116 }
118 /* This callback gets invoked when the remote chanl is created */
119 void rpmsg_channel_created(struct rpmsg_channel *rp_chnl)
120 {
121 app_rp_chnl = rp_chnl;
122 }
124 /* This callback gets invoked when the remote channel is deleted */
125 void rpmsg_channel_deleted(struct rpmsg_channel *rp_chnl)
126 {
128 }
130 void rpmsg_read_default_cb(struct rpmsg_channel *rp_chnl, void *data, int len,
131 void *priv, unsigned long src)
132 {
133 memcpy(r_buffer, data, len);
134 Src = src;
135 Len = len;
136 intr_flag = 1;
137 }
139 void rpmsg_read_ept_cb(struct rpmsg_channel *rp_chnl, void *data, int len,
140 void *priv, unsigned long src)
141 {
142 rpmsg_send_offchannel(rp_chnl, rp_ept->addr, src, data, len);
143 }
145 void sleep()
146 {
147 int i;
148 for (i = 0; i < 1000; i++) ;
149 }
151 int main(int argc, char *argv[])
152 {
153 unsigned long proc_id = 0;
154 unsigned long rsc_id = 0;
155 struct hil_proc *hproc;
157 /* Initialize HW system components */
158 init_system();
160 if (argc >= 2) {
161 proc_id = strtoul(argv[1], NULL, 0);
162 }
164 if (argc >= 3) {
165 rsc_id = strtoul(argv[2], NULL, 0);
166 }
168 /* Create HIL proc */
169 hproc = platform_create_proc(proc_id);
170 if (!hproc) {
171 LPERROR("Failed to create hil proc.\n");
172 return -1;
173 }
174 rsc_info.rsc_tab = get_resource_table(
175 (int)rsc_id, &rsc_info.size);
176 if (!rsc_info.rsc_tab) {
177 LPRINTF("Failed to get resource table data.\n");
178 return -1;
179 }
180 return app(hproc);
181 }