1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2020-2021 NXP
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>
25 struct vpu_cmd_request {
32 struct list_head list;
34 struct vpu_cmd_request *request;
35 struct vpu_rpc_event *pkt;
39 static struct vpu_cmd_request vpu_cmd_requests[] = {
41 .request = VPU_CMD_ID_CONFIGURE_CODEC,
42 .response = VPU_MSG_ID_MEM_REQUEST,
46 .request = VPU_CMD_ID_START,
47 .response = VPU_MSG_ID_START_DONE,
51 .request = VPU_CMD_ID_STOP,
52 .response = VPU_MSG_ID_STOP_DONE,
56 .request = VPU_CMD_ID_ABORT,
57 .response = VPU_MSG_ID_ABORT_DONE,
61 .request = VPU_CMD_ID_RST_BUF,
62 .response = VPU_MSG_ID_BUF_RST,
67 static int vpu_cmd_send(struct vpu_core *core, struct vpu_rpc_event *pkt)
71 ret = vpu_iface_send_cmd(core, pkt);
75 /*write cmd data to cmd buffer before trigger a cmd interrupt*/
77 vpu_mbox_send_type(core, COMMAND);
82 static struct vpu_cmd_t *vpu_alloc_cmd(struct vpu_inst *inst, u32 id, void *data)
84 struct vpu_cmd_t *cmd;
88 cmd = vzalloc(sizeof(*cmd));
92 cmd->pkt = vzalloc(sizeof(*cmd->pkt));
99 ret = vpu_iface_pack_cmd(inst->core, cmd->pkt, inst->id, id, data);
101 dev_err(inst->dev, "iface pack cmd(%d) fail\n", id);
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];
116 static void vpu_free_cmd(struct vpu_cmd_t *cmd)
124 static int vpu_session_process_cmd(struct vpu_inst *inst, struct vpu_cmd_t *cmd)
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);
132 vpu_iface_post_send_cmd(inst);
133 vpu_inst_record_flow(inst, cmd->id);
135 dev_err(inst->dev, "[%d] iface send cmd(0x%x) fail\n", inst->id, cmd->id);
141 static void vpu_process_cmd_request(struct vpu_inst *inst)
143 struct vpu_cmd_t *cmd;
144 struct vpu_cmd_t *tmp;
146 if (!inst || inst->pending)
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);
154 inst->pending = (void *)cmd;
161 static int vpu_request_cmd(struct vpu_inst *inst, u32 id, void *data,
162 unsigned long *key, int *sync)
164 struct vpu_core *core;
165 struct vpu_cmd_t *cmd;
167 if (!inst || !inst->core)
171 cmd = vpu_alloc_cmd(inst, id, data);
175 mutex_lock(&core->cmd_lock);
176 cmd->key = core->cmd_seq++;
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);
188 static void vpu_clear_pending(struct vpu_inst *inst)
190 if (!inst || !inst->pending)
193 vpu_free_cmd(inst->pending);
194 wake_up_all(&inst->core->ack_wq);
195 inst->pending = NULL;
198 static bool vpu_check_response(struct vpu_cmd_t *cmd, u32 response, u32 handled)
200 struct vpu_cmd_request *request;
202 if (!cmd || !cmd->request)
205 request = cmd->request;
206 if (request->response != response)
208 if (request->handled != handled)
214 int vpu_response_cmd(struct vpu_inst *inst, u32 response, u32 handled)
216 struct vpu_core *core;
218 if (!inst || !inst->core)
222 mutex_lock(&core->cmd_lock);
223 if (vpu_check_response(inst->pending, response, handled))
224 vpu_clear_pending(inst);
226 vpu_process_cmd_request(inst);
227 mutex_unlock(&core->cmd_lock);
232 void vpu_clear_request(struct vpu_inst *inst)
234 struct vpu_cmd_t *cmd;
235 struct vpu_cmd_t *tmp;
237 mutex_lock(&inst->core->cmd_lock);
239 vpu_clear_pending(inst);
241 list_for_each_entry_safe(cmd, tmp, &inst->cmd_q, list) {
242 list_del_init(&cmd->list);
245 mutex_unlock(&inst->core->cmd_lock);
248 static bool check_is_responsed(struct vpu_inst *inst, unsigned long key)
250 struct vpu_core *core = inst->core;
251 struct vpu_cmd_t *cmd;
254 mutex_lock(&core->cmd_lock);
256 if (cmd && key == cmd->key) {
260 list_for_each_entry(cmd, &inst->cmd_q, list) {
261 if (key == cmd->key) {
267 mutex_unlock(&core->cmd_lock);
272 static int sync_session_response(struct vpu_inst *inst, unsigned long key, long timeout, int try)
274 struct vpu_core *core;
276 if (!inst || !inst->core)
281 call_void_vop(inst, wait_prepare);
282 wait_event_timeout(core->ack_wq, check_is_responsed(inst, key), timeout);
283 call_void_vop(inst, wait_finish);
285 if (!check_is_responsed(inst, key)) {
288 dev_err(inst->dev, "[%d] sync session timeout\n", inst->id);
289 set_bit(inst->id, &core->hang_mask);
290 mutex_lock(&inst->core->cmd_lock);
291 vpu_clear_pending(inst);
292 mutex_unlock(&inst->core->cmd_lock);
299 static void vpu_core_keep_active(struct vpu_core *core)
301 struct vpu_rpc_event pkt;
303 memset(&pkt, 0, sizeof(pkt));
304 vpu_iface_pack_cmd(core, &pkt, 0, VPU_CMD_ID_NOOP, NULL);
306 dev_dbg(core->dev, "try to wake up\n");
307 mutex_lock(&core->cmd_lock);
308 vpu_cmd_send(core, &pkt);
309 mutex_unlock(&core->cmd_lock);
312 static int vpu_session_send_cmd(struct vpu_inst *inst, u32 id, void *data)
321 ret = vpu_request_cmd(inst, id, data, &key, &sync);
325 /* workaround for a firmware issue,
326 * firmware should be waked up by start or configure command,
327 * but there is a very small change that firmware failed to wakeup.
328 * in such case, try to wakeup firmware again by sending a noop command
330 if (sync && (id == VPU_CMD_ID_CONFIGURE_CODEC || id == VPU_CMD_ID_START)) {
331 if (sync_session_response(inst, key, VPU_TIMEOUT_WAKEUP, 1))
332 vpu_core_keep_active(inst->core);
338 ret = sync_session_response(inst, key, VPU_TIMEOUT, 0);
342 dev_err(inst->dev, "[%d] send cmd(0x%x) fail\n", inst->id, id);
347 int vpu_session_configure_codec(struct vpu_inst *inst)
349 return vpu_session_send_cmd(inst, VPU_CMD_ID_CONFIGURE_CODEC, NULL);
352 int vpu_session_start(struct vpu_inst *inst)
354 vpu_trace(inst->dev, "[%d]\n", inst->id);
356 return vpu_session_send_cmd(inst, VPU_CMD_ID_START, NULL);
359 int vpu_session_stop(struct vpu_inst *inst)
363 vpu_trace(inst->dev, "[%d]\n", inst->id);
365 ret = vpu_session_send_cmd(inst, VPU_CMD_ID_STOP, NULL);
366 /* workaround for a firmware bug,
367 * if the next command is too close after stop cmd,
368 * the firmware may enter wfi wrongly.
370 usleep_range(3000, 5000);
374 int vpu_session_encode_frame(struct vpu_inst *inst, s64 timestamp)
376 return vpu_session_send_cmd(inst, VPU_CMD_ID_FRAME_ENCODE, ×tamp);
379 int vpu_session_alloc_fs(struct vpu_inst *inst, struct vpu_fs_info *fs)
381 return vpu_session_send_cmd(inst, VPU_CMD_ID_FS_ALLOC, fs);
384 int vpu_session_release_fs(struct vpu_inst *inst, struct vpu_fs_info *fs)
386 return vpu_session_send_cmd(inst, VPU_CMD_ID_FS_RELEASE, fs);
389 int vpu_session_abort(struct vpu_inst *inst)
391 return vpu_session_send_cmd(inst, VPU_CMD_ID_ABORT, NULL);
394 int vpu_session_rst_buf(struct vpu_inst *inst)
396 return vpu_session_send_cmd(inst, VPU_CMD_ID_RST_BUF, NULL);
399 int vpu_session_fill_timestamp(struct vpu_inst *inst, struct vpu_ts_info *info)
401 return vpu_session_send_cmd(inst, VPU_CMD_ID_TIMESTAMP, info);
404 int vpu_session_update_parameters(struct vpu_inst *inst, void *arg)
406 if (inst->type & VPU_CORE_TYPE_DEC)
407 vpu_iface_set_decode_params(inst, arg, 1);
409 vpu_iface_set_encode_params(inst, arg, 1);
411 return vpu_session_send_cmd(inst, VPU_CMD_ID_UPDATE_PARAMETER, arg);
414 int vpu_session_debug(struct vpu_inst *inst)
416 return vpu_session_send_cmd(inst, VPU_CMD_ID_DEBUG, NULL);
419 int vpu_core_snapshot(struct vpu_core *core)
421 struct vpu_inst *inst;
424 if (!core || list_empty(&core->instances))
427 inst = list_first_entry(&core->instances, struct vpu_inst, list);
429 reinit_completion(&core->cmp);
430 ret = vpu_session_send_cmd(inst, VPU_CMD_ID_SNAPSHOT, NULL);
433 ret = wait_for_completion_timeout(&core->cmp, VPU_TIMEOUT);
435 dev_err(core->dev, "snapshot timeout\n");
442 int vpu_core_sw_reset(struct vpu_core *core)
444 struct vpu_rpc_event pkt;
447 memset(&pkt, 0, sizeof(pkt));
448 vpu_iface_pack_cmd(core, &pkt, 0, VPU_CMD_ID_FIRM_RESET, NULL);
450 reinit_completion(&core->cmp);
451 mutex_lock(&core->cmd_lock);
452 ret = vpu_cmd_send(core, &pkt);
453 mutex_unlock(&core->cmd_lock);
456 ret = wait_for_completion_timeout(&core->cmp, VPU_TIMEOUT);
458 dev_err(core->dev, "sw reset timeout\n");