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/platform_device.h>
13 #include <linux/slab.h>
14 #include <linux/types.h>
15 #include <linux/delay.h>
16 #include <linux/vmalloc.h>
23 struct vpu_cmd_request {
30 struct list_head list;
32 struct vpu_cmd_request *request;
33 struct vpu_rpc_event *pkt;
35 atomic_long_t *last_response_cmd;
38 static struct vpu_cmd_request vpu_cmd_requests[] = {
40 .request = VPU_CMD_ID_CONFIGURE_CODEC,
41 .response = VPU_MSG_ID_MEM_REQUEST,
45 .request = VPU_CMD_ID_START,
46 .response = VPU_MSG_ID_START_DONE,
50 .request = VPU_CMD_ID_STOP,
51 .response = VPU_MSG_ID_STOP_DONE,
55 .request = VPU_CMD_ID_ABORT,
56 .response = VPU_MSG_ID_ABORT_DONE,
60 .request = VPU_CMD_ID_RST_BUF,
61 .response = VPU_MSG_ID_BUF_RST,
66 static int vpu_cmd_send(struct vpu_core *core, struct vpu_rpc_event *pkt)
70 ret = vpu_iface_send_cmd(core, pkt);
74 /*write cmd data to cmd buffer before trigger a cmd interrupt*/
76 vpu_mbox_send_type(core, COMMAND);
81 static struct vpu_cmd_t *vpu_alloc_cmd(struct vpu_inst *inst, u32 id, void *data)
83 struct vpu_cmd_t *cmd;
87 cmd = vzalloc(sizeof(*cmd));
91 cmd->pkt = vzalloc(sizeof(*cmd->pkt));
98 ret = vpu_iface_pack_cmd(inst->core, cmd->pkt, inst->id, id, data);
100 dev_err(inst->dev, "iface pack cmd %s fail\n", vpu_id_name(id));
105 for (i = 0; i < ARRAY_SIZE(vpu_cmd_requests); i++) {
106 if (vpu_cmd_requests[i].request == id) {
107 cmd->request = &vpu_cmd_requests[i];
115 static void vpu_free_cmd(struct vpu_cmd_t *cmd)
119 if (cmd->last_response_cmd)
120 atomic_long_set(cmd->last_response_cmd, cmd->key);
125 static int vpu_session_process_cmd(struct vpu_inst *inst, struct vpu_cmd_t *cmd)
129 dev_dbg(inst->dev, "[%d]send cmd %s\n", inst->id, vpu_id_name(cmd->id));
130 vpu_iface_pre_send_cmd(inst);
131 ret = vpu_cmd_send(inst->core, cmd->pkt);
133 vpu_iface_post_send_cmd(inst);
134 vpu_inst_record_flow(inst, cmd->id);
136 dev_err(inst->dev, "[%d] iface send cmd %s fail\n", inst->id, vpu_id_name(cmd->id));
142 static void vpu_process_cmd_request(struct vpu_inst *inst)
144 struct vpu_cmd_t *cmd;
145 struct vpu_cmd_t *tmp;
147 if (!inst || inst->pending)
150 list_for_each_entry_safe(cmd, tmp, &inst->cmd_q, list) {
151 list_del_init(&cmd->list);
152 if (vpu_session_process_cmd(inst, cmd))
153 dev_err(inst->dev, "[%d] process cmd %s fail\n",
154 inst->id, vpu_id_name(cmd->id));
156 inst->pending = (void *)cmd;
163 static int vpu_request_cmd(struct vpu_inst *inst, u32 id, void *data,
164 unsigned long *key, int *sync)
166 struct vpu_core *core;
167 struct vpu_cmd_t *cmd;
169 if (!inst || !inst->core)
173 cmd = vpu_alloc_cmd(inst, id, data);
177 mutex_lock(&core->cmd_lock);
178 cmd->key = ++inst->cmd_seq;
179 cmd->last_response_cmd = &inst->last_response_cmd;
183 *sync = cmd->request ? true : false;
184 list_add_tail(&cmd->list, &inst->cmd_q);
185 vpu_process_cmd_request(inst);
186 mutex_unlock(&core->cmd_lock);
191 static void vpu_clear_pending(struct vpu_inst *inst)
193 if (!inst || !inst->pending)
196 vpu_free_cmd(inst->pending);
197 wake_up_all(&inst->core->ack_wq);
198 inst->pending = NULL;
201 static bool vpu_check_response(struct vpu_cmd_t *cmd, u32 response, u32 handled)
203 struct vpu_cmd_request *request;
205 if (!cmd || !cmd->request)
208 request = cmd->request;
209 if (request->response != response)
211 if (request->handled != handled)
217 int vpu_response_cmd(struct vpu_inst *inst, u32 response, u32 handled)
219 struct vpu_core *core;
221 if (!inst || !inst->core)
225 mutex_lock(&core->cmd_lock);
226 if (vpu_check_response(inst->pending, response, handled))
227 vpu_clear_pending(inst);
229 vpu_process_cmd_request(inst);
230 mutex_unlock(&core->cmd_lock);
235 void vpu_clear_request(struct vpu_inst *inst)
237 struct vpu_cmd_t *cmd;
238 struct vpu_cmd_t *tmp;
240 mutex_lock(&inst->core->cmd_lock);
242 vpu_clear_pending(inst);
244 list_for_each_entry_safe(cmd, tmp, &inst->cmd_q, list) {
245 list_del_init(&cmd->list);
248 mutex_unlock(&inst->core->cmd_lock);
251 static bool check_is_responsed(struct vpu_inst *inst, unsigned long key)
253 unsigned long last_response = atomic_long_read(&inst->last_response_cmd);
255 if (key <= last_response && (last_response - key) < (ULONG_MAX >> 1))
261 static int sync_session_response(struct vpu_inst *inst, unsigned long key, long timeout, int try)
263 struct vpu_core *core;
265 if (!inst || !inst->core)
270 call_void_vop(inst, wait_prepare);
271 wait_event_timeout(core->ack_wq, check_is_responsed(inst, key), timeout);
272 call_void_vop(inst, wait_finish);
274 if (!check_is_responsed(inst, key)) {
277 dev_err(inst->dev, "[%d] sync session timeout\n", inst->id);
278 set_bit(inst->id, &core->hang_mask);
279 mutex_lock(&inst->core->cmd_lock);
280 vpu_clear_pending(inst);
281 mutex_unlock(&inst->core->cmd_lock);
288 static void vpu_core_keep_active(struct vpu_core *core)
290 struct vpu_rpc_event pkt;
292 memset(&pkt, 0, sizeof(pkt));
293 vpu_iface_pack_cmd(core, &pkt, 0, VPU_CMD_ID_NOOP, NULL);
295 dev_dbg(core->dev, "try to wake up\n");
296 mutex_lock(&core->cmd_lock);
297 if (vpu_cmd_send(core, &pkt))
298 dev_err(core->dev, "fail to keep active\n");
299 mutex_unlock(&core->cmd_lock);
302 static int vpu_session_send_cmd(struct vpu_inst *inst, u32 id, void *data)
311 ret = vpu_request_cmd(inst, id, data, &key, &sync);
315 /* workaround for a firmware issue,
316 * firmware should be waked up by start or configure command,
317 * but there is a very small change that firmware failed to wakeup.
318 * in such case, try to wakeup firmware again by sending a noop command
320 if (sync && (id == VPU_CMD_ID_CONFIGURE_CODEC || id == VPU_CMD_ID_START)) {
321 if (sync_session_response(inst, key, VPU_TIMEOUT_WAKEUP, 1))
322 vpu_core_keep_active(inst->core);
328 ret = sync_session_response(inst, key, VPU_TIMEOUT, 0);
332 dev_err(inst->dev, "[%d] send cmd %s fail\n", inst->id, vpu_id_name(id));
337 int vpu_session_configure_codec(struct vpu_inst *inst)
339 return vpu_session_send_cmd(inst, VPU_CMD_ID_CONFIGURE_CODEC, NULL);
342 int vpu_session_start(struct vpu_inst *inst)
344 vpu_trace(inst->dev, "[%d]\n", inst->id);
346 return vpu_session_send_cmd(inst, VPU_CMD_ID_START, NULL);
349 int vpu_session_stop(struct vpu_inst *inst)
353 vpu_trace(inst->dev, "[%d]\n", inst->id);
355 ret = vpu_session_send_cmd(inst, VPU_CMD_ID_STOP, NULL);
356 /* workaround for a firmware bug,
357 * if the next command is too close after stop cmd,
358 * the firmware may enter wfi wrongly.
360 usleep_range(3000, 5000);
364 int vpu_session_encode_frame(struct vpu_inst *inst, s64 timestamp)
366 return vpu_session_send_cmd(inst, VPU_CMD_ID_FRAME_ENCODE, ×tamp);
369 int vpu_session_alloc_fs(struct vpu_inst *inst, struct vpu_fs_info *fs)
371 return vpu_session_send_cmd(inst, VPU_CMD_ID_FS_ALLOC, fs);
374 int vpu_session_release_fs(struct vpu_inst *inst, struct vpu_fs_info *fs)
376 return vpu_session_send_cmd(inst, VPU_CMD_ID_FS_RELEASE, fs);
379 int vpu_session_abort(struct vpu_inst *inst)
381 return vpu_session_send_cmd(inst, VPU_CMD_ID_ABORT, NULL);
384 int vpu_session_rst_buf(struct vpu_inst *inst)
386 return vpu_session_send_cmd(inst, VPU_CMD_ID_RST_BUF, NULL);
389 int vpu_session_fill_timestamp(struct vpu_inst *inst, struct vpu_ts_info *info)
391 return vpu_session_send_cmd(inst, VPU_CMD_ID_TIMESTAMP, info);
394 int vpu_session_update_parameters(struct vpu_inst *inst, void *arg)
396 if (inst->type & VPU_CORE_TYPE_DEC)
397 vpu_iface_set_decode_params(inst, arg, 1);
399 vpu_iface_set_encode_params(inst, arg, 1);
401 return vpu_session_send_cmd(inst, VPU_CMD_ID_UPDATE_PARAMETER, arg);
404 int vpu_session_debug(struct vpu_inst *inst)
406 return vpu_session_send_cmd(inst, VPU_CMD_ID_DEBUG, NULL);
409 int vpu_core_snapshot(struct vpu_core *core)
411 struct vpu_inst *inst;
414 if (!core || list_empty(&core->instances))
417 inst = list_first_entry(&core->instances, struct vpu_inst, list);
419 reinit_completion(&core->cmp);
420 ret = vpu_session_send_cmd(inst, VPU_CMD_ID_SNAPSHOT, NULL);
423 ret = wait_for_completion_timeout(&core->cmp, VPU_TIMEOUT);
425 dev_err(core->dev, "snapshot timeout\n");
432 int vpu_core_sw_reset(struct vpu_core *core)
434 struct vpu_rpc_event pkt;
437 memset(&pkt, 0, sizeof(pkt));
438 vpu_iface_pack_cmd(core, &pkt, 0, VPU_CMD_ID_FIRM_RESET, NULL);
440 reinit_completion(&core->cmp);
441 mutex_lock(&core->cmd_lock);
442 ret = vpu_cmd_send(core, &pkt);
443 mutex_unlock(&core->cmd_lock);
446 ret = wait_for_completion_timeout(&core->cmp, VPU_TIMEOUT);
448 dev_err(core->dev, "sw reset timeout\n");