1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2020-2021 NXP
6 #include <linux/init.h>
7 #include <linux/device.h>
8 #include <linux/ioctl.h>
9 #include <linux/list.h>
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/types.h>
13 #include <linux/pm_runtime.h>
14 #include <media/v4l2-device.h>
15 #include <linux/debugfs.h>
19 #include "vpu_helpers.h"
24 struct print_buf_desc {
34 static char *vb2_stat_name[] = {
35 [VB2_BUF_STATE_DEQUEUED] = "dequeued",
36 [VB2_BUF_STATE_IN_REQUEST] = "in_request",
37 [VB2_BUF_STATE_PREPARING] = "preparing",
38 [VB2_BUF_STATE_QUEUED] = "queued",
39 [VB2_BUF_STATE_ACTIVE] = "active",
40 [VB2_BUF_STATE_DONE] = "done",
41 [VB2_BUF_STATE_ERROR] = "error",
44 static char *vpu_stat_name[] = {
45 [VPU_BUF_STATE_IDLE] = "idle",
46 [VPU_BUF_STATE_INUSE] = "inuse",
47 [VPU_BUF_STATE_DECODED] = "decoded",
48 [VPU_BUF_STATE_READY] = "ready",
49 [VPU_BUF_STATE_SKIP] = "skip",
50 [VPU_BUF_STATE_ERROR] = "error",
53 static int vpu_dbg_instance(struct seq_file *s, void *data)
55 struct vpu_inst *inst = s->private;
61 if (!inst->fh.m2m_ctx)
63 num = scnprintf(str, sizeof(str), "[%s]\n", vpu_core_type_desc(inst->type));
64 if (seq_write(s, str, num))
67 num = scnprintf(str, sizeof(str), "tgig = %d,pid = %d\n", inst->tgid, inst->pid);
68 if (seq_write(s, str, num))
70 num = scnprintf(str, sizeof(str), "state = %d\n", inst->state);
71 if (seq_write(s, str, num))
73 num = scnprintf(str, sizeof(str),
74 "min_buffer_out = %d, min_buffer_cap = %d\n",
75 inst->min_buffer_out, inst->min_buffer_cap);
76 if (seq_write(s, str, num))
79 vq = v4l2_m2m_get_src_vq(inst->fh.m2m_ctx);
80 num = scnprintf(str, sizeof(str),
81 "output (%2d, %2d): fmt = %c%c%c%c %d x %d, %d;",
84 inst->out_format.pixfmt,
85 inst->out_format.pixfmt >> 8,
86 inst->out_format.pixfmt >> 16,
87 inst->out_format.pixfmt >> 24,
88 inst->out_format.width,
89 inst->out_format.height,
90 vq->last_buffer_dequeued);
91 if (seq_write(s, str, num))
93 for (i = 0; i < inst->out_format.mem_planes; i++) {
94 num = scnprintf(str, sizeof(str), " %d(%d)",
95 vpu_get_fmt_plane_size(&inst->out_format, i),
96 inst->out_format.bytesperline[i]);
97 if (seq_write(s, str, num))
100 if (seq_write(s, "\n", 1))
103 vq = v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx);
104 num = scnprintf(str, sizeof(str),
105 "capture(%2d, %2d): fmt = %c%c%c%c %d x %d, %d;",
106 vb2_is_streaming(vq),
108 inst->cap_format.pixfmt,
109 inst->cap_format.pixfmt >> 8,
110 inst->cap_format.pixfmt >> 16,
111 inst->cap_format.pixfmt >> 24,
112 inst->cap_format.width,
113 inst->cap_format.height,
114 vq->last_buffer_dequeued);
115 if (seq_write(s, str, num))
117 for (i = 0; i < inst->cap_format.mem_planes; i++) {
118 num = scnprintf(str, sizeof(str), " %d(%d)",
119 vpu_get_fmt_plane_size(&inst->cap_format, i),
120 inst->cap_format.bytesperline[i]);
121 if (seq_write(s, str, num))
124 if (seq_write(s, "\n", 1))
126 num = scnprintf(str, sizeof(str), "crop: (%d, %d) %d x %d\n",
131 if (seq_write(s, str, num))
134 vq = v4l2_m2m_get_src_vq(inst->fh.m2m_ctx);
135 for (i = 0; i < vq->num_buffers; i++) {
136 struct vb2_buffer *vb = vq->bufs[i];
137 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
139 if (vb->state == VB2_BUF_STATE_DEQUEUED)
141 num = scnprintf(str, sizeof(str),
142 "output [%2d] state = %10s, %8s\n",
143 i, vb2_stat_name[vb->state],
144 vpu_stat_name[vpu_get_buffer_state(vbuf)]);
145 if (seq_write(s, str, num))
149 vq = v4l2_m2m_get_dst_vq(inst->fh.m2m_ctx);
150 for (i = 0; i < vq->num_buffers; i++) {
151 struct vb2_buffer *vb = vq->bufs[i];
152 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
154 if (vb->state == VB2_BUF_STATE_DEQUEUED)
156 num = scnprintf(str, sizeof(str),
157 "capture[%2d] state = %10s, %8s\n",
158 i, vb2_stat_name[vb->state],
159 vpu_stat_name[vpu_get_buffer_state(vbuf)]);
160 if (seq_write(s, str, num))
164 num = scnprintf(str, sizeof(str), "sequence = %d\n", inst->sequence);
165 if (seq_write(s, str, num))
168 if (inst->use_stream_buffer) {
169 num = scnprintf(str, sizeof(str), "stream_buffer = %d / %d, <%pad, 0x%x>\n",
170 vpu_helper_get_used_space(inst),
171 inst->stream_buffer.length,
172 &inst->stream_buffer.phys,
173 inst->stream_buffer.length);
174 if (seq_write(s, str, num))
177 num = scnprintf(str, sizeof(str), "kfifo len = 0x%x\n", kfifo_len(&inst->msg_fifo));
178 if (seq_write(s, str, num))
181 num = scnprintf(str, sizeof(str), "flow :\n");
182 if (seq_write(s, str, num))
185 mutex_lock(&inst->core->cmd_lock);
186 for (i = 0; i < ARRAY_SIZE(inst->flows); i++) {
187 u32 idx = (inst->flow_idx + i) % (ARRAY_SIZE(inst->flows));
189 if (!inst->flows[idx])
191 num = scnprintf(str, sizeof(str), "\t[%s]0x%x\n",
192 inst->flows[idx] >= VPU_MSG_ID_NOOP ? "M" : "C",
194 if (seq_write(s, str, num)) {
195 mutex_unlock(&inst->core->cmd_lock);
199 mutex_unlock(&inst->core->cmd_lock);
203 num = call_vop(inst, get_debug_info, str, sizeof(str), i++);
206 if (seq_write(s, str, num))
213 static int vpu_dbg_core(struct seq_file *s, void *data)
215 struct vpu_core *core = s->private;
216 struct vpu_shared_addr *iface = core->iface;
220 num = scnprintf(str, sizeof(str), "[%s]\n", vpu_core_type_desc(core->type));
221 if (seq_write(s, str, num))
224 num = scnprintf(str, sizeof(str), "boot_region = <%pad, 0x%x>\n",
225 &core->fw.phys, core->fw.length);
226 if (seq_write(s, str, num))
228 num = scnprintf(str, sizeof(str), "rpc_region = <%pad, 0x%x> used = 0x%x\n",
229 &core->rpc.phys, core->rpc.length, core->rpc.bytesused);
230 if (seq_write(s, str, num))
232 num = scnprintf(str, sizeof(str), "fwlog_region = <%pad, 0x%x>\n",
233 &core->log.phys, core->log.length);
234 if (seq_write(s, str, num))
237 num = scnprintf(str, sizeof(str), "power %s\n",
238 vpu_iface_get_power_state(core) ? "on" : "off");
239 if (seq_write(s, str, num))
241 num = scnprintf(str, sizeof(str), "state = %d\n", core->state);
242 if (seq_write(s, str, num))
244 if (core->state == VPU_CORE_DEINIT)
246 num = scnprintf(str, sizeof(str), "fw version = %d.%d.%d\n",
247 (core->fw_version >> 16) & 0xff,
248 (core->fw_version >> 8) & 0xff,
249 core->fw_version & 0xff);
250 if (seq_write(s, str, num))
252 num = scnprintf(str, sizeof(str), "instances = %d/%d (0x%02lx), %d\n",
253 hweight32(core->instance_mask),
254 core->supported_instance_count,
256 core->request_count);
257 if (seq_write(s, str, num))
259 num = scnprintf(str, sizeof(str), "kfifo len = 0x%x\n", kfifo_len(&core->msg_fifo));
260 if (seq_write(s, str, num))
262 num = scnprintf(str, sizeof(str),
263 "cmd_buf:[0x%x, 0x%x], wptr = 0x%x, rptr = 0x%x\n",
264 iface->cmd_desc->start,
265 iface->cmd_desc->end,
266 iface->cmd_desc->wptr,
267 iface->cmd_desc->rptr);
268 if (seq_write(s, str, num))
270 num = scnprintf(str, sizeof(str),
271 "msg_buf:[0x%x, 0x%x], wptr = 0x%x, rptr = 0x%x\n",
272 iface->msg_desc->start,
273 iface->msg_desc->end,
274 iface->msg_desc->wptr,
275 iface->msg_desc->rptr);
276 if (seq_write(s, str, num))
282 static int vpu_dbg_fwlog(struct seq_file *s, void *data)
284 struct vpu_core *core = s->private;
285 struct print_buf_desc *print_buf;
291 if (!core->log.virt || core->state == VPU_CORE_DEINIT)
294 print_buf = core->log.virt;
295 rptr = print_buf->read;
296 wptr = print_buf->write;
300 else if (rptr < wptr)
301 length = wptr - rptr;
303 length = print_buf->bytes + wptr - rptr;
305 if (s->count + length >= s->size) {
310 if (rptr + length >= print_buf->bytes) {
311 int num = print_buf->bytes - rptr;
313 if (seq_write(s, print_buf->buffer + rptr, num))
320 if (seq_write(s, print_buf->buffer + rptr, length))
325 print_buf->read = rptr;
330 static int vpu_dbg_inst_open(struct inode *inode, struct file *filp)
332 return single_open(filp, vpu_dbg_instance, inode->i_private);
335 static ssize_t vpu_dbg_inst_write(struct file *file,
336 const char __user *user_buf, size_t size, loff_t *ppos)
338 struct seq_file *s = file->private_data;
339 struct vpu_inst *inst = s->private;
341 vpu_session_debug(inst);
346 static ssize_t vpu_dbg_core_write(struct file *file,
347 const char __user *user_buf, size_t size, loff_t *ppos)
349 struct seq_file *s = file->private_data;
350 struct vpu_core *core = s->private;
352 pm_runtime_resume_and_get(core->dev);
353 mutex_lock(&core->lock);
354 if (vpu_iface_get_power_state(core) && !core->request_count) {
355 dev_info(core->dev, "reset\n");
356 if (!vpu_core_sw_reset(core)) {
357 vpu_core_set_state(core, VPU_CORE_ACTIVE);
361 mutex_unlock(&core->lock);
362 pm_runtime_put_sync(core->dev);
367 static int vpu_dbg_core_open(struct inode *inode, struct file *filp)
369 return single_open(filp, vpu_dbg_core, inode->i_private);
372 static int vpu_dbg_fwlog_open(struct inode *inode, struct file *filp)
374 return single_open(filp, vpu_dbg_fwlog, inode->i_private);
377 static const struct file_operations vpu_dbg_inst_fops = {
378 .owner = THIS_MODULE,
379 .open = vpu_dbg_inst_open,
380 .release = single_release,
382 .write = vpu_dbg_inst_write,
385 static const struct file_operations vpu_dbg_core_fops = {
386 .owner = THIS_MODULE,
387 .open = vpu_dbg_core_open,
388 .release = single_release,
390 .write = vpu_dbg_core_write,
393 static const struct file_operations vpu_dbg_fwlog_fops = {
394 .owner = THIS_MODULE,
395 .open = vpu_dbg_fwlog_open,
396 .release = single_release,
400 int vpu_inst_create_dbgfs_file(struct vpu_inst *inst)
405 if (!inst || !inst->core || !inst->core->vpu)
408 vpu = inst->core->vpu;
415 scnprintf(name, sizeof(name), "instance.%d.%d", inst->core->id, inst->id);
416 inst->debugfs = debugfs_create_file((const char *)name,
417 VERIFY_OCTAL_PERMISSIONS(0644),
425 int vpu_inst_remove_dbgfs_file(struct vpu_inst *inst)
430 debugfs_remove(inst->debugfs);
431 inst->debugfs = NULL;
436 int vpu_core_create_dbgfs_file(struct vpu_core *core)
441 if (!core || !core->vpu)
448 if (!core->debugfs) {
449 scnprintf(name, sizeof(name), "core.%d", core->id);
450 core->debugfs = debugfs_create_file((const char *)name,
451 VERIFY_OCTAL_PERMISSIONS(0644),
456 if (!core->debugfs_fwlog) {
457 scnprintf(name, sizeof(name), "fwlog.%d", core->id);
458 core->debugfs_fwlog = debugfs_create_file((const char *)name,
459 VERIFY_OCTAL_PERMISSIONS(0444),
462 &vpu_dbg_fwlog_fops);
468 int vpu_core_remove_dbgfs_file(struct vpu_core *core)
472 debugfs_remove(core->debugfs);
473 core->debugfs = NULL;
474 debugfs_remove(core->debugfs_fwlog);
475 core->debugfs_fwlog = NULL;
480 void vpu_inst_record_flow(struct vpu_inst *inst, u32 flow)
485 inst->flows[inst->flow_idx] = flow;
486 inst->flow_idx = (inst->flow_idx + 1) % (ARRAY_SIZE(inst->flows));