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