xref: /linux/drivers/accel/ivpu/ivpu_debugfs.c (revision 69050f8d6d075dc01af7a5f2f550a8067510366f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2020-2024 Intel Corporation
4  */
5 
6 #include <linux/debugfs.h>
7 #include <linux/fault-inject.h>
8 
9 #include <drm/drm_debugfs.h>
10 #include <drm/drm_file.h>
11 #include <drm/drm_print.h>
12 
13 #include <uapi/drm/ivpu_accel.h>
14 
15 #include "ivpu_debugfs.h"
16 #include "ivpu_drv.h"
17 #include "ivpu_fw.h"
18 #include "ivpu_fw_log.h"
19 #include "ivpu_gem.h"
20 #include "ivpu_hw.h"
21 #include "ivpu_jsm_msg.h"
22 #include "ivpu_pm.h"
23 #include "vpu_boot_api.h"
24 
25 static inline struct ivpu_device *seq_to_ivpu(struct seq_file *s)
26 {
27 	struct drm_debugfs_entry *entry = s->private;
28 
29 	return to_ivpu_device(entry->dev);
30 }
31 
32 static int bo_list_show(struct seq_file *s, void *v)
33 {
34 	struct drm_printer p = drm_seq_file_printer(s);
35 	struct ivpu_device *vdev = seq_to_ivpu(s);
36 
37 	ivpu_bo_list(&vdev->drm, &p);
38 
39 	return 0;
40 }
41 
42 static int fw_name_show(struct seq_file *s, void *v)
43 {
44 	struct ivpu_device *vdev = seq_to_ivpu(s);
45 
46 	seq_printf(s, "%s\n", vdev->fw->name);
47 	return 0;
48 }
49 
50 static int fw_version_show(struct seq_file *s, void *v)
51 {
52 	struct ivpu_device *vdev = seq_to_ivpu(s);
53 
54 	seq_printf(s, "%s\n", vdev->fw->version);
55 	return 0;
56 }
57 
58 static int fw_trace_capability_show(struct seq_file *s, void *v)
59 {
60 	struct ivpu_device *vdev = seq_to_ivpu(s);
61 	u64 trace_hw_component_mask;
62 	u32 trace_destination_mask;
63 	int ret;
64 
65 	ret = ivpu_jsm_trace_get_capability(vdev, &trace_destination_mask,
66 					    &trace_hw_component_mask);
67 	if (!ret) {
68 		seq_printf(s,
69 			   "trace_destination_mask:  %#18x\n"
70 			   "trace_hw_component_mask: %#18llx\n",
71 			   trace_destination_mask, trace_hw_component_mask);
72 	}
73 	return 0;
74 }
75 
76 static int fw_trace_config_show(struct seq_file *s, void *v)
77 {
78 	struct ivpu_device *vdev = seq_to_ivpu(s);
79 	/**
80 	 * WA: VPU_JSM_MSG_TRACE_GET_CONFIG command is not working yet,
81 	 * so we use values from vdev->fw instead of calling ivpu_jsm_trace_get_config()
82 	 */
83 	u32 trace_level = vdev->fw->trace_level;
84 	u32 trace_destination_mask = vdev->fw->trace_destination_mask;
85 	u64 trace_hw_component_mask = vdev->fw->trace_hw_component_mask;
86 
87 	seq_printf(s,
88 		   "trace_level:             %#18x\n"
89 		   "trace_destination_mask:  %#18x\n"
90 		   "trace_hw_component_mask: %#18llx\n",
91 		   trace_level, trace_destination_mask, trace_hw_component_mask);
92 
93 	return 0;
94 }
95 
96 static int last_bootmode_show(struct seq_file *s, void *v)
97 {
98 	struct ivpu_device *vdev = seq_to_ivpu(s);
99 
100 	seq_printf(s, "%s\n", (vdev->fw->last_boot_mode == VPU_BOOT_TYPE_WARMBOOT) ?
101 		   "warm boot" : "cold boot");
102 
103 	return 0;
104 }
105 
106 static int reset_counter_show(struct seq_file *s, void *v)
107 {
108 	struct ivpu_device *vdev = seq_to_ivpu(s);
109 
110 	seq_printf(s, "%d\n", atomic_read(&vdev->pm->reset_counter));
111 	return 0;
112 }
113 
114 static int reset_pending_show(struct seq_file *s, void *v)
115 {
116 	struct ivpu_device *vdev = seq_to_ivpu(s);
117 
118 	seq_printf(s, "%d\n", atomic_read(&vdev->pm->reset_pending));
119 	return 0;
120 }
121 
122 static int firewall_irq_counter_show(struct seq_file *s, void *v)
123 {
124 	struct ivpu_device *vdev = seq_to_ivpu(s);
125 
126 	seq_printf(s, "%d\n", atomic_read(&vdev->hw->firewall_irq_counter));
127 	return 0;
128 }
129 
130 static const struct drm_debugfs_info vdev_debugfs_list[] = {
131 	{"bo_list", bo_list_show, 0},
132 	{"fw_name", fw_name_show, 0},
133 	{"fw_version", fw_version_show, 0},
134 	{"fw_trace_capability", fw_trace_capability_show, 0},
135 	{"fw_trace_config", fw_trace_config_show, 0},
136 	{"last_bootmode", last_bootmode_show, 0},
137 	{"reset_counter", reset_counter_show, 0},
138 	{"reset_pending", reset_pending_show, 0},
139 	{"firewall_irq_counter", firewall_irq_counter_show, 0},
140 };
141 
142 static int dvfs_mode_get(void *data, u64 *dvfs_mode)
143 {
144 	struct ivpu_device *vdev = (struct ivpu_device *)data;
145 
146 	*dvfs_mode = vdev->fw->dvfs_mode;
147 	return 0;
148 }
149 
150 static int dvfs_mode_set(void *data, u64 dvfs_mode)
151 {
152 	struct ivpu_device *vdev = (struct ivpu_device *)data;
153 
154 	vdev->fw->dvfs_mode = (u32)dvfs_mode;
155 	return pci_try_reset_function(to_pci_dev(vdev->drm.dev));
156 }
157 
158 DEFINE_DEBUGFS_ATTRIBUTE(dvfs_mode_fops, dvfs_mode_get, dvfs_mode_set, "%llu\n");
159 
160 static ssize_t
161 fw_dyndbg_fops_write(struct file *file, const char __user *user_buf, size_t size, loff_t *pos)
162 {
163 	struct ivpu_device *vdev = file->private_data;
164 	char buffer[VPU_DYNDBG_CMD_MAX_LEN] = {};
165 	int ret;
166 
167 	if (size >= VPU_DYNDBG_CMD_MAX_LEN)
168 		return -EINVAL;
169 
170 	ret = strncpy_from_user(buffer, user_buf, size);
171 	if (ret < 0)
172 		return ret;
173 
174 	ivpu_jsm_dyndbg_control(vdev, buffer, size);
175 	return size;
176 }
177 
178 static const struct file_operations fw_dyndbg_fops = {
179 	.owner = THIS_MODULE,
180 	.open = simple_open,
181 	.write = fw_dyndbg_fops_write,
182 };
183 
184 static int fw_log_show(struct seq_file *s, void *v)
185 {
186 	struct ivpu_device *vdev = s->private;
187 	struct drm_printer p = drm_seq_file_printer(s);
188 
189 	ivpu_fw_log_print(vdev, true, &p);
190 	return 0;
191 }
192 
193 static int fw_log_fops_open(struct inode *inode, struct file *file)
194 {
195 	return single_open(file, fw_log_show, inode->i_private);
196 }
197 
198 static ssize_t
199 fw_log_fops_write(struct file *file, const char __user *user_buf, size_t size, loff_t *pos)
200 {
201 	struct seq_file *s = file->private_data;
202 	struct ivpu_device *vdev = s->private;
203 
204 	if (!size)
205 		return -EINVAL;
206 
207 	ivpu_fw_log_mark_read(vdev);
208 	return size;
209 }
210 
211 static const struct file_operations fw_log_fops = {
212 	.owner = THIS_MODULE,
213 	.open = fw_log_fops_open,
214 	.write = fw_log_fops_write,
215 	.read = seq_read,
216 	.llseek = seq_lseek,
217 	.release = single_release,
218 };
219 
220 static ssize_t
221 fw_profiling_freq_fops_write(struct file *file, const char __user *user_buf,
222 			     size_t size, loff_t *pos)
223 {
224 	struct ivpu_device *vdev = file->private_data;
225 	bool enable;
226 	int ret;
227 
228 	ret = kstrtobool_from_user(user_buf, size, &enable);
229 	if (ret < 0)
230 		return ret;
231 
232 	ivpu_hw_profiling_freq_drive(vdev, enable);
233 
234 	ret = pci_try_reset_function(to_pci_dev(vdev->drm.dev));
235 	if (ret)
236 		return ret;
237 
238 	return size;
239 }
240 
241 static const struct file_operations fw_profiling_freq_fops = {
242 	.owner = THIS_MODULE,
243 	.open = simple_open,
244 	.write = fw_profiling_freq_fops_write,
245 };
246 
247 static ssize_t
248 fw_trace_destination_mask_fops_write(struct file *file, const char __user *user_buf,
249 				     size_t size, loff_t *pos)
250 {
251 	struct ivpu_device *vdev = file->private_data;
252 	struct ivpu_fw_info *fw = vdev->fw;
253 	u32 trace_destination_mask;
254 	int ret;
255 
256 	ret = kstrtou32_from_user(user_buf, size, 0, &trace_destination_mask);
257 	if (ret < 0)
258 		return ret;
259 
260 	fw->trace_destination_mask = trace_destination_mask;
261 
262 	ivpu_jsm_trace_set_config(vdev, fw->trace_level, trace_destination_mask,
263 				  fw->trace_hw_component_mask);
264 
265 	return size;
266 }
267 
268 static const struct file_operations fw_trace_destination_mask_fops = {
269 	.owner = THIS_MODULE,
270 	.open = simple_open,
271 	.write = fw_trace_destination_mask_fops_write,
272 };
273 
274 static ssize_t
275 fw_trace_hw_comp_mask_fops_write(struct file *file, const char __user *user_buf,
276 				 size_t size, loff_t *pos)
277 {
278 	struct ivpu_device *vdev = file->private_data;
279 	struct ivpu_fw_info *fw = vdev->fw;
280 	u64 trace_hw_component_mask;
281 	int ret;
282 
283 	ret = kstrtou64_from_user(user_buf, size, 0, &trace_hw_component_mask);
284 	if (ret < 0)
285 		return ret;
286 
287 	fw->trace_hw_component_mask = trace_hw_component_mask;
288 
289 	ivpu_jsm_trace_set_config(vdev, fw->trace_level, fw->trace_destination_mask,
290 				  trace_hw_component_mask);
291 
292 	return size;
293 }
294 
295 static const struct file_operations fw_trace_hw_comp_mask_fops = {
296 	.owner = THIS_MODULE,
297 	.open = simple_open,
298 	.write = fw_trace_hw_comp_mask_fops_write,
299 };
300 
301 static ssize_t
302 fw_trace_level_fops_write(struct file *file, const char __user *user_buf, size_t size, loff_t *pos)
303 {
304 	struct ivpu_device *vdev = file->private_data;
305 	struct ivpu_fw_info *fw = vdev->fw;
306 	u32 trace_level;
307 	int ret;
308 
309 	ret = kstrtou32_from_user(user_buf, size, 0, &trace_level);
310 	if (ret < 0)
311 		return ret;
312 
313 	fw->trace_level = trace_level;
314 
315 	ivpu_jsm_trace_set_config(vdev, trace_level, fw->trace_destination_mask,
316 				  fw->trace_hw_component_mask);
317 
318 	return size;
319 }
320 
321 static const struct file_operations fw_trace_level_fops = {
322 	.owner = THIS_MODULE,
323 	.open = simple_open,
324 	.write = fw_trace_level_fops_write,
325 };
326 
327 static ssize_t
328 ivpu_force_recovery_fn(struct file *file, const char __user *user_buf, size_t size, loff_t *pos)
329 {
330 	struct ivpu_device *vdev = file->private_data;
331 	int ret;
332 
333 	if (!size)
334 		return -EINVAL;
335 
336 	ret = ivpu_rpm_get(vdev);
337 	if (ret < 0)
338 		return ret;
339 
340 	ivpu_pm_trigger_recovery(vdev, "debugfs");
341 	flush_work(&vdev->pm->recovery_work);
342 	ivpu_rpm_put(vdev);
343 	return size;
344 }
345 
346 static const struct file_operations ivpu_force_recovery_fops = {
347 	.owner = THIS_MODULE,
348 	.open = simple_open,
349 	.write = ivpu_force_recovery_fn,
350 };
351 
352 static int ivpu_reset_engine_fn(void *data, u64 val)
353 {
354 	struct ivpu_device *vdev = (struct ivpu_device *)data;
355 
356 	return ivpu_jsm_reset_engine(vdev, (u32)val);
357 }
358 
359 DEFINE_DEBUGFS_ATTRIBUTE(ivpu_reset_engine_fops, NULL, ivpu_reset_engine_fn, "0x%02llx\n");
360 
361 static int ivpu_resume_engine_fn(void *data, u64 val)
362 {
363 	struct ivpu_device *vdev = (struct ivpu_device *)data;
364 
365 	return ivpu_jsm_hws_resume_engine(vdev, (u32)val);
366 }
367 
368 DEFINE_DEBUGFS_ATTRIBUTE(ivpu_resume_engine_fops, NULL, ivpu_resume_engine_fn, "0x%02llx\n");
369 
370 static int dct_active_get(void *data, u64 *active_percent)
371 {
372 	struct ivpu_device *vdev = data;
373 
374 	*active_percent = vdev->pm->dct_active_percent;
375 
376 	return 0;
377 }
378 
379 static int dct_active_set(void *data, u64 active_percent)
380 {
381 	struct ivpu_device *vdev = data;
382 	int ret;
383 
384 	if (active_percent > 100)
385 		return -EINVAL;
386 
387 	ret = ivpu_rpm_get(vdev);
388 	if (ret < 0)
389 		return ret;
390 
391 	if (active_percent)
392 		ret = ivpu_pm_dct_enable(vdev, active_percent);
393 	else
394 		ret = ivpu_pm_dct_disable(vdev);
395 
396 	ivpu_rpm_put(vdev);
397 
398 	return ret;
399 }
400 
401 DEFINE_DEBUGFS_ATTRIBUTE(ivpu_dct_fops, dct_active_get, dct_active_set, "%llu\n");
402 
403 static void print_priority_band(struct seq_file *s, struct ivpu_hw_info *hw,
404 				int band, const char *name)
405 {
406 	seq_printf(s, "%-9s: grace_period %9u process_grace_period %9u process_quantum %9u\n",
407 		   name,
408 		   hw->hws.grace_period[band],
409 		   hw->hws.process_grace_period[band],
410 		   hw->hws.process_quantum[band]);
411 }
412 
413 static int priority_bands_show(struct seq_file *s, void *v)
414 {
415 	struct ivpu_device *vdev = s->private;
416 	struct ivpu_hw_info *hw = vdev->hw;
417 
418 	print_priority_band(s, hw, VPU_JOB_SCHEDULING_PRIORITY_BAND_IDLE, "Idle");
419 	print_priority_band(s, hw, VPU_JOB_SCHEDULING_PRIORITY_BAND_NORMAL, "Normal");
420 	print_priority_band(s, hw, VPU_JOB_SCHEDULING_PRIORITY_BAND_FOCUS, "Focus");
421 	print_priority_band(s, hw, VPU_JOB_SCHEDULING_PRIORITY_BAND_REALTIME, "Realtime");
422 
423 	return 0;
424 }
425 
426 static int priority_bands_fops_open(struct inode *inode, struct file *file)
427 {
428 	return single_open(file, priority_bands_show, inode->i_private);
429 }
430 
431 static ssize_t
432 priority_bands_fops_write(struct file *file, const char __user *user_buf, size_t size, loff_t *pos)
433 {
434 	struct seq_file *s = file->private_data;
435 	struct ivpu_device *vdev = s->private;
436 	char buf[64];
437 	u32 grace_period;
438 	u32 process_grace_period;
439 	u32 process_quantum;
440 	u32 band;
441 	int ret;
442 
443 	if (size >= sizeof(buf))
444 		return -EINVAL;
445 
446 	ret = simple_write_to_buffer(buf, sizeof(buf) - 1, pos, user_buf, size);
447 	if (ret < 0)
448 		return ret;
449 
450 	buf[ret] = '\0';
451 	ret = sscanf(buf, "%u %u %u %u", &band, &grace_period, &process_grace_period,
452 		     &process_quantum);
453 	if (ret != 4)
454 		return -EINVAL;
455 
456 	if (band >= VPU_JOB_SCHEDULING_PRIORITY_BAND_COUNT)
457 		return -EINVAL;
458 
459 	vdev->hw->hws.grace_period[band] = grace_period;
460 	vdev->hw->hws.process_grace_period[band] = process_grace_period;
461 	vdev->hw->hws.process_quantum[band] = process_quantum;
462 
463 	return size;
464 }
465 
466 static const struct file_operations ivpu_hws_priority_bands_fops = {
467 	.owner = THIS_MODULE,
468 	.open = priority_bands_fops_open,
469 	.write = priority_bands_fops_write,
470 	.read = seq_read,
471 	.llseek = seq_lseek,
472 	.release = single_release,
473 };
474 
475 void ivpu_debugfs_init(struct ivpu_device *vdev)
476 {
477 	struct dentry *debugfs_root = vdev->drm.debugfs_root;
478 
479 	drm_debugfs_add_files(&vdev->drm, vdev_debugfs_list, ARRAY_SIZE(vdev_debugfs_list));
480 
481 	debugfs_create_file("force_recovery", 0200, debugfs_root, vdev,
482 			    &ivpu_force_recovery_fops);
483 
484 	debugfs_create_file("dvfs_mode", 0644, debugfs_root, vdev,
485 			    &dvfs_mode_fops);
486 
487 	debugfs_create_file("fw_dyndbg", 0200, debugfs_root, vdev,
488 			    &fw_dyndbg_fops);
489 	debugfs_create_file("fw_log", 0644, debugfs_root, vdev,
490 			    &fw_log_fops);
491 	debugfs_create_file("fw_trace_destination_mask", 0200, debugfs_root, vdev,
492 			    &fw_trace_destination_mask_fops);
493 	debugfs_create_file("fw_trace_hw_comp_mask", 0200, debugfs_root, vdev,
494 			    &fw_trace_hw_comp_mask_fops);
495 	debugfs_create_file("fw_trace_level", 0200, debugfs_root, vdev,
496 			    &fw_trace_level_fops);
497 	debugfs_create_file("hws_priority_bands", 0200, debugfs_root, vdev,
498 			    &ivpu_hws_priority_bands_fops);
499 
500 	debugfs_create_file("reset_engine", 0200, debugfs_root, vdev,
501 			    &ivpu_reset_engine_fops);
502 	debugfs_create_file("resume_engine", 0200, debugfs_root, vdev,
503 			    &ivpu_resume_engine_fops);
504 
505 	if (ivpu_hw_ip_gen(vdev) >= IVPU_HW_IP_40XX) {
506 		debugfs_create_file("fw_profiling_freq_drive", 0200,
507 				    debugfs_root, vdev, &fw_profiling_freq_fops);
508 		debugfs_create_file("dct", 0644, debugfs_root, vdev, &ivpu_dct_fops);
509 	}
510 
511 #ifdef CONFIG_FAULT_INJECTION
512 	fault_create_debugfs_attr("fail_hw", debugfs_root, &ivpu_hw_failure);
513 #endif
514 }
515