1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2020-2021 NXP
4  */
5 
6 #include <linux/init.h>
7 #include <linux/interconnect.h>
8 #include <linux/ioctl.h>
9 #include <linux/list.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/of_device.h>
13 #include <linux/of_address.h>
14 #include <linux/platform_device.h>
15 #include <linux/slab.h>
16 #include <linux/types.h>
17 #include <linux/delay.h>
18 #include <linux/vmalloc.h>
19 #include "vpu.h"
20 #include "vpu_defs.h"
21 #include "vpu_cmds.h"
22 #include "vpu_rpc.h"
23 #include "vpu_mbox.h"
24 
25 struct vpu_cmd_request {
26 	u32 request;
27 	u32 response;
28 	u32 handled;
29 };
30 
31 struct vpu_cmd_t {
32 	struct list_head list;
33 	u32 id;
34 	struct vpu_cmd_request *request;
35 	struct vpu_rpc_event *pkt;
36 	unsigned long key;
37 };
38 
39 static struct vpu_cmd_request vpu_cmd_requests[] = {
40 	{
41 		.request = VPU_CMD_ID_CONFIGURE_CODEC,
42 		.response = VPU_MSG_ID_MEM_REQUEST,
43 		.handled = 1,
44 	},
45 	{
46 		.request = VPU_CMD_ID_START,
47 		.response = VPU_MSG_ID_START_DONE,
48 		.handled = 0,
49 	},
50 	{
51 		.request = VPU_CMD_ID_STOP,
52 		.response = VPU_MSG_ID_STOP_DONE,
53 		.handled = 0,
54 	},
55 	{
56 		.request = VPU_CMD_ID_ABORT,
57 		.response = VPU_MSG_ID_ABORT_DONE,
58 		.handled = 0,
59 	},
60 	{
61 		.request = VPU_CMD_ID_RST_BUF,
62 		.response = VPU_MSG_ID_BUF_RST,
63 		.handled = 1,
64 	},
65 };
66 
67 static int vpu_cmd_send(struct vpu_core *core, struct vpu_rpc_event *pkt)
68 {
69 	int ret = 0;
70 
71 	ret = vpu_iface_send_cmd(core, pkt);
72 	if (ret)
73 		return ret;
74 
75 	/*write cmd data to cmd buffer before trigger a cmd interrupt*/
76 	mb();
77 	vpu_mbox_send_type(core, COMMAND);
78 
79 	return ret;
80 }
81 
82 static struct vpu_cmd_t *vpu_alloc_cmd(struct vpu_inst *inst, u32 id, void *data)
83 {
84 	struct vpu_cmd_t *cmd;
85 	int i;
86 	int ret;
87 
88 	cmd = vzalloc(sizeof(*cmd));
89 	if (!cmd)
90 		return NULL;
91 
92 	cmd->pkt = vzalloc(sizeof(*cmd->pkt));
93 	if (!cmd->pkt) {
94 		vfree(cmd);
95 		return NULL;
96 	}
97 
98 	cmd->id = id;
99 	ret = vpu_iface_pack_cmd(inst->core, cmd->pkt, inst->id, id, data);
100 	if (ret) {
101 		dev_err(inst->dev, "iface pack cmd(%d) fail\n", id);
102 		vfree(cmd->pkt);
103 		vfree(cmd);
104 		return NULL;
105 	}
106 	for (i = 0; i < ARRAY_SIZE(vpu_cmd_requests); i++) {
107 		if (vpu_cmd_requests[i].request == id) {
108 			cmd->request = &vpu_cmd_requests[i];
109 			break;
110 		}
111 	}
112 
113 	return cmd;
114 }
115 
116 static void vpu_free_cmd(struct vpu_cmd_t *cmd)
117 {
118 	if (!cmd)
119 		return;
120 	vfree(cmd->pkt);
121 	vfree(cmd);
122 }
123 
124 static int vpu_session_process_cmd(struct vpu_inst *inst, struct vpu_cmd_t *cmd)
125 {
126 	int ret;
127 
128 	dev_dbg(inst->dev, "[%d]send cmd(0x%x)\n", inst->id, cmd->id);
129 	vpu_iface_pre_send_cmd(inst);
130 	ret = vpu_cmd_send(inst->core, cmd->pkt);
131 	if (!ret) {
132 		vpu_iface_post_send_cmd(inst);
133 		vpu_inst_record_flow(inst, cmd->id);
134 	} else {
135 		dev_err(inst->dev, "[%d] iface send cmd(0x%x) fail\n", inst->id, cmd->id);
136 	}
137 
138 	return ret;
139 }
140 
141 static void vpu_process_cmd_request(struct vpu_inst *inst)
142 {
143 	struct vpu_cmd_t *cmd;
144 	struct vpu_cmd_t *tmp;
145 
146 	if (!inst || inst->pending)
147 		return;
148 
149 	list_for_each_entry_safe(cmd, tmp, &inst->cmd_q, list) {
150 		list_del_init(&cmd->list);
151 		if (vpu_session_process_cmd(inst, cmd))
152 			dev_err(inst->dev, "[%d] process cmd(%d) fail\n", inst->id, cmd->id);
153 		if (cmd->request) {
154 			inst->pending = (void *)cmd;
155 			break;
156 		}
157 		vpu_free_cmd(cmd);
158 	}
159 }
160 
161 static int vpu_request_cmd(struct vpu_inst *inst, u32 id, void *data,
162 			   unsigned long *key, int *sync)
163 {
164 	struct vpu_core *core;
165 	struct vpu_cmd_t *cmd;
166 
167 	if (!inst || !inst->core)
168 		return -EINVAL;
169 
170 	core = inst->core;
171 	cmd = vpu_alloc_cmd(inst, id, data);
172 	if (!cmd)
173 		return -ENOMEM;
174 
175 	mutex_lock(&core->cmd_lock);
176 	cmd->key = core->cmd_seq++;
177 	if (key)
178 		*key = cmd->key;
179 	if (sync)
180 		*sync = cmd->request ? true : false;
181 	list_add_tail(&cmd->list, &inst->cmd_q);
182 	vpu_process_cmd_request(inst);
183 	mutex_unlock(&core->cmd_lock);
184 
185 	return 0;
186 }
187 
188 static void vpu_clear_pending(struct vpu_inst *inst)
189 {
190 	if (!inst || !inst->pending)
191 		return;
192 
193 	vpu_free_cmd(inst->pending);
194 	wake_up_all(&inst->core->ack_wq);
195 	inst->pending = NULL;
196 }
197 
198 static bool vpu_check_response(struct vpu_cmd_t *cmd, u32 response, u32 handled)
199 {
200 	struct vpu_cmd_request *request;
201 
202 	if (!cmd || !cmd->request)
203 		return false;
204 
205 	request = cmd->request;
206 	if (request->response != response)
207 		return false;
208 	if (request->handled != handled)
209 		return false;
210 
211 	return true;
212 }
213 
214 int vpu_response_cmd(struct vpu_inst *inst, u32 response, u32 handled)
215 {
216 	struct vpu_core *core;
217 
218 	if (!inst || !inst->core)
219 		return -EINVAL;
220 
221 	core = inst->core;
222 	mutex_lock(&core->cmd_lock);
223 	if (vpu_check_response(inst->pending, response, handled))
224 		vpu_clear_pending(inst);
225 
226 	vpu_process_cmd_request(inst);
227 	mutex_unlock(&core->cmd_lock);
228 
229 	return 0;
230 }
231 
232 void vpu_clear_request(struct vpu_inst *inst)
233 {
234 	struct vpu_cmd_t *cmd;
235 	struct vpu_cmd_t *tmp;
236 
237 	mutex_lock(&inst->core->cmd_lock);
238 	if (inst->pending)
239 		vpu_clear_pending(inst);
240 
241 	list_for_each_entry_safe(cmd, tmp, &inst->cmd_q, list) {
242 		list_del_init(&cmd->list);
243 		vpu_free_cmd(cmd);
244 	}
245 	mutex_unlock(&inst->core->cmd_lock);
246 }
247 
248 static bool check_is_responsed(struct vpu_inst *inst, unsigned long key)
249 {
250 	struct vpu_core *core = inst->core;
251 	struct vpu_cmd_t *cmd;
252 	bool flag = true;
253 
254 	mutex_lock(&core->cmd_lock);
255 	cmd = inst->pending;
256 	if (cmd && key == cmd->key) {
257 		flag = false;
258 		goto exit;
259 	}
260 	list_for_each_entry(cmd, &inst->cmd_q, list) {
261 		if (key == cmd->key) {
262 			flag = false;
263 			break;
264 		}
265 	}
266 exit:
267 	mutex_unlock(&core->cmd_lock);
268 
269 	return flag;
270 }
271 
272 static int sync_session_response(struct vpu_inst *inst, unsigned long key)
273 {
274 	struct vpu_core *core;
275 
276 	if (!inst || !inst->core)
277 		return -EINVAL;
278 
279 	core = inst->core;
280 
281 	call_void_vop(inst, wait_prepare);
282 	wait_event_timeout(core->ack_wq, check_is_responsed(inst, key), VPU_TIMEOUT);
283 	call_void_vop(inst, wait_finish);
284 
285 	if (!check_is_responsed(inst, key)) {
286 		dev_err(inst->dev, "[%d] sync session timeout\n", inst->id);
287 		set_bit(inst->id, &core->hang_mask);
288 		mutex_lock(&inst->core->cmd_lock);
289 		vpu_clear_pending(inst);
290 		mutex_unlock(&inst->core->cmd_lock);
291 		return -EINVAL;
292 	}
293 
294 	return 0;
295 }
296 
297 static int vpu_session_send_cmd(struct vpu_inst *inst, u32 id, void *data)
298 {
299 	unsigned long key;
300 	int sync = false;
301 	int ret = -EINVAL;
302 
303 	if (inst->id < 0)
304 		return -EINVAL;
305 
306 	ret = vpu_request_cmd(inst, id, data, &key, &sync);
307 	if (!ret && sync)
308 		ret = sync_session_response(inst, key);
309 
310 	if (ret)
311 		dev_err(inst->dev, "[%d] send cmd(0x%x) fail\n", inst->id, id);
312 
313 	return ret;
314 }
315 
316 int vpu_session_configure_codec(struct vpu_inst *inst)
317 {
318 	return vpu_session_send_cmd(inst, VPU_CMD_ID_CONFIGURE_CODEC, NULL);
319 }
320 
321 int vpu_session_start(struct vpu_inst *inst)
322 {
323 	vpu_trace(inst->dev, "[%d]\n", inst->id);
324 
325 	return vpu_session_send_cmd(inst, VPU_CMD_ID_START, NULL);
326 }
327 
328 int vpu_session_stop(struct vpu_inst *inst)
329 {
330 	int ret;
331 
332 	vpu_trace(inst->dev, "[%d]\n", inst->id);
333 
334 	ret = vpu_session_send_cmd(inst, VPU_CMD_ID_STOP, NULL);
335 	/* workaround for a firmware bug,
336 	 * if the next command is too close after stop cmd,
337 	 * the firmware may enter wfi wrongly.
338 	 */
339 	usleep_range(3000, 5000);
340 	return ret;
341 }
342 
343 int vpu_session_encode_frame(struct vpu_inst *inst, s64 timestamp)
344 {
345 	return vpu_session_send_cmd(inst, VPU_CMD_ID_FRAME_ENCODE, &timestamp);
346 }
347 
348 int vpu_session_alloc_fs(struct vpu_inst *inst, struct vpu_fs_info *fs)
349 {
350 	return vpu_session_send_cmd(inst, VPU_CMD_ID_FS_ALLOC, fs);
351 }
352 
353 int vpu_session_release_fs(struct vpu_inst *inst, struct vpu_fs_info *fs)
354 {
355 	return vpu_session_send_cmd(inst, VPU_CMD_ID_FS_RELEASE, fs);
356 }
357 
358 int vpu_session_abort(struct vpu_inst *inst)
359 {
360 	return vpu_session_send_cmd(inst, VPU_CMD_ID_ABORT, NULL);
361 }
362 
363 int vpu_session_rst_buf(struct vpu_inst *inst)
364 {
365 	return vpu_session_send_cmd(inst, VPU_CMD_ID_RST_BUF, NULL);
366 }
367 
368 int vpu_session_fill_timestamp(struct vpu_inst *inst, struct vpu_ts_info *info)
369 {
370 	return vpu_session_send_cmd(inst, VPU_CMD_ID_TIMESTAMP, info);
371 }
372 
373 int vpu_session_update_parameters(struct vpu_inst *inst, void *arg)
374 {
375 	if (inst->type & VPU_CORE_TYPE_DEC)
376 		vpu_iface_set_decode_params(inst, arg, 1);
377 	else
378 		vpu_iface_set_encode_params(inst, arg, 1);
379 
380 	return vpu_session_send_cmd(inst, VPU_CMD_ID_UPDATE_PARAMETER, arg);
381 }
382 
383 int vpu_session_debug(struct vpu_inst *inst)
384 {
385 	return vpu_session_send_cmd(inst, VPU_CMD_ID_DEBUG, NULL);
386 }
387 
388 int vpu_core_snapshot(struct vpu_core *core)
389 {
390 	struct vpu_inst *inst;
391 	int ret;
392 
393 	if (!core || list_empty(&core->instances))
394 		return 0;
395 
396 	inst = list_first_entry(&core->instances, struct vpu_inst, list);
397 
398 	reinit_completion(&core->cmp);
399 	ret = vpu_session_send_cmd(inst, VPU_CMD_ID_SNAPSHOT, NULL);
400 	if (ret)
401 		return ret;
402 	ret = wait_for_completion_timeout(&core->cmp, VPU_TIMEOUT);
403 	if (!ret) {
404 		dev_err(core->dev, "snapshot timeout\n");
405 		return -EINVAL;
406 	}
407 
408 	return 0;
409 }
410 
411 int vpu_core_sw_reset(struct vpu_core *core)
412 {
413 	struct vpu_rpc_event pkt;
414 	int ret;
415 
416 	memset(&pkt, 0, sizeof(pkt));
417 	vpu_iface_pack_cmd(core, &pkt, 0, VPU_CMD_ID_FIRM_RESET, NULL);
418 
419 	reinit_completion(&core->cmp);
420 	mutex_lock(&core->cmd_lock);
421 	ret = vpu_cmd_send(core, &pkt);
422 	mutex_unlock(&core->cmd_lock);
423 	if (ret)
424 		return ret;
425 	ret = wait_for_completion_timeout(&core->cmp, VPU_TIMEOUT);
426 	if (!ret) {
427 		dev_err(core->dev, "sw reset timeout\n");
428 		return -EINVAL;
429 	}
430 
431 	return 0;
432 }
433