xref: /linux/drivers/accel/ivpu/ivpu_debugfs.c (revision 24168c5e6dfbdd5b414f048f47f75d64533296ca)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2020-2023 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_trace_capability_show(struct seq_file *s, void *v)
49 {
50 	struct ivpu_device *vdev = seq_to_ivpu(s);
51 	u64 trace_hw_component_mask;
52 	u32 trace_destination_mask;
53 	int ret;
54 
55 	ret = ivpu_jsm_trace_get_capability(vdev, &trace_destination_mask,
56 					    &trace_hw_component_mask);
57 	if (!ret) {
58 		seq_printf(s,
59 			   "trace_destination_mask:  %#18x\n"
60 			   "trace_hw_component_mask: %#18llx\n",
61 			   trace_destination_mask, trace_hw_component_mask);
62 	}
63 	return 0;
64 }
65 
66 static int fw_trace_config_show(struct seq_file *s, void *v)
67 {
68 	struct ivpu_device *vdev = seq_to_ivpu(s);
69 	/**
70 	 * WA: VPU_JSM_MSG_TRACE_GET_CONFIG command is not working yet,
71 	 * so we use values from vdev->fw instead of calling ivpu_jsm_trace_get_config()
72 	 */
73 	u32 trace_level = vdev->fw->trace_level;
74 	u32 trace_destination_mask = vdev->fw->trace_destination_mask;
75 	u64 trace_hw_component_mask = vdev->fw->trace_hw_component_mask;
76 
77 	seq_printf(s,
78 		   "trace_level:             %#18x\n"
79 		   "trace_destination_mask:  %#18x\n"
80 		   "trace_hw_component_mask: %#18llx\n",
81 		   trace_level, trace_destination_mask, trace_hw_component_mask);
82 
83 	return 0;
84 }
85 
86 static int last_bootmode_show(struct seq_file *s, void *v)
87 {
88 	struct ivpu_device *vdev = seq_to_ivpu(s);
89 
90 	seq_printf(s, "%s\n", (vdev->pm->is_warmboot) ? "warmboot" : "coldboot");
91 
92 	return 0;
93 }
94 
95 static int reset_counter_show(struct seq_file *s, void *v)
96 {
97 	struct ivpu_device *vdev = seq_to_ivpu(s);
98 
99 	seq_printf(s, "%d\n", atomic_read(&vdev->pm->reset_counter));
100 	return 0;
101 }
102 
103 static int reset_pending_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_pending));
108 	return 0;
109 }
110 
111 static const struct drm_debugfs_info vdev_debugfs_list[] = {
112 	{"bo_list", bo_list_show, 0},
113 	{"fw_name", fw_name_show, 0},
114 	{"fw_trace_capability", fw_trace_capability_show, 0},
115 	{"fw_trace_config", fw_trace_config_show, 0},
116 	{"last_bootmode", last_bootmode_show, 0},
117 	{"reset_counter", reset_counter_show, 0},
118 	{"reset_pending", reset_pending_show, 0},
119 };
120 
121 static ssize_t
122 dvfs_mode_fops_write(struct file *file, const char __user *user_buf, size_t size, loff_t *pos)
123 {
124 	struct ivpu_device *vdev = file->private_data;
125 	struct ivpu_fw_info *fw = vdev->fw;
126 	u32 dvfs_mode;
127 	int ret;
128 
129 	ret = kstrtou32_from_user(user_buf, size, 0, &dvfs_mode);
130 	if (ret < 0)
131 		return ret;
132 
133 	fw->dvfs_mode = dvfs_mode;
134 
135 	ret = pci_try_reset_function(to_pci_dev(vdev->drm.dev));
136 	if (ret)
137 		return ret;
138 
139 	return size;
140 }
141 
142 static const struct file_operations dvfs_mode_fops = {
143 	.owner = THIS_MODULE,
144 	.open = simple_open,
145 	.write = dvfs_mode_fops_write,
146 };
147 
148 static int fw_log_show(struct seq_file *s, void *v)
149 {
150 	struct ivpu_device *vdev = s->private;
151 	struct drm_printer p = drm_seq_file_printer(s);
152 
153 	ivpu_fw_log_print(vdev, true, &p);
154 	return 0;
155 }
156 
157 static int fw_log_fops_open(struct inode *inode, struct file *file)
158 {
159 	return single_open(file, fw_log_show, inode->i_private);
160 }
161 
162 static ssize_t
163 fw_log_fops_write(struct file *file, const char __user *user_buf, size_t size, loff_t *pos)
164 {
165 	struct seq_file *s = file->private_data;
166 	struct ivpu_device *vdev = s->private;
167 
168 	if (!size)
169 		return -EINVAL;
170 
171 	ivpu_fw_log_clear(vdev);
172 	return size;
173 }
174 
175 static const struct file_operations fw_log_fops = {
176 	.owner = THIS_MODULE,
177 	.open = fw_log_fops_open,
178 	.write = fw_log_fops_write,
179 	.read = seq_read,
180 	.llseek = seq_lseek,
181 	.release = single_release,
182 };
183 
184 static ssize_t
185 fw_profiling_freq_fops_write(struct file *file, const char __user *user_buf,
186 			     size_t size, loff_t *pos)
187 {
188 	struct ivpu_device *vdev = file->private_data;
189 	bool enable;
190 	int ret;
191 
192 	ret = kstrtobool_from_user(user_buf, size, &enable);
193 	if (ret < 0)
194 		return ret;
195 
196 	ivpu_hw_profiling_freq_drive(vdev, enable);
197 
198 	ret = pci_try_reset_function(to_pci_dev(vdev->drm.dev));
199 	if (ret)
200 		return ret;
201 
202 	return size;
203 }
204 
205 static const struct file_operations fw_profiling_freq_fops = {
206 	.owner = THIS_MODULE,
207 	.open = simple_open,
208 	.write = fw_profiling_freq_fops_write,
209 };
210 
211 static ssize_t
212 fw_trace_destination_mask_fops_write(struct file *file, const char __user *user_buf,
213 				     size_t size, loff_t *pos)
214 {
215 	struct ivpu_device *vdev = file->private_data;
216 	struct ivpu_fw_info *fw = vdev->fw;
217 	u32 trace_destination_mask;
218 	int ret;
219 
220 	ret = kstrtou32_from_user(user_buf, size, 0, &trace_destination_mask);
221 	if (ret < 0)
222 		return ret;
223 
224 	fw->trace_destination_mask = trace_destination_mask;
225 
226 	ivpu_jsm_trace_set_config(vdev, fw->trace_level, trace_destination_mask,
227 				  fw->trace_hw_component_mask);
228 
229 	return size;
230 }
231 
232 static const struct file_operations fw_trace_destination_mask_fops = {
233 	.owner = THIS_MODULE,
234 	.open = simple_open,
235 	.write = fw_trace_destination_mask_fops_write,
236 };
237 
238 static ssize_t
239 fw_trace_hw_comp_mask_fops_write(struct file *file, const char __user *user_buf,
240 				 size_t size, loff_t *pos)
241 {
242 	struct ivpu_device *vdev = file->private_data;
243 	struct ivpu_fw_info *fw = vdev->fw;
244 	u64 trace_hw_component_mask;
245 	int ret;
246 
247 	ret = kstrtou64_from_user(user_buf, size, 0, &trace_hw_component_mask);
248 	if (ret < 0)
249 		return ret;
250 
251 	fw->trace_hw_component_mask = trace_hw_component_mask;
252 
253 	ivpu_jsm_trace_set_config(vdev, fw->trace_level, fw->trace_destination_mask,
254 				  trace_hw_component_mask);
255 
256 	return size;
257 }
258 
259 static const struct file_operations fw_trace_hw_comp_mask_fops = {
260 	.owner = THIS_MODULE,
261 	.open = simple_open,
262 	.write = fw_trace_hw_comp_mask_fops_write,
263 };
264 
265 static ssize_t
266 fw_trace_level_fops_write(struct file *file, const char __user *user_buf, size_t size, loff_t *pos)
267 {
268 	struct ivpu_device *vdev = file->private_data;
269 	struct ivpu_fw_info *fw = vdev->fw;
270 	u32 trace_level;
271 	int ret;
272 
273 	ret = kstrtou32_from_user(user_buf, size, 0, &trace_level);
274 	if (ret < 0)
275 		return ret;
276 
277 	fw->trace_level = trace_level;
278 
279 	ivpu_jsm_trace_set_config(vdev, trace_level, fw->trace_destination_mask,
280 				  fw->trace_hw_component_mask);
281 
282 	return size;
283 }
284 
285 static const struct file_operations fw_trace_level_fops = {
286 	.owner = THIS_MODULE,
287 	.open = simple_open,
288 	.write = fw_trace_level_fops_write,
289 };
290 
291 static ssize_t
292 ivpu_force_recovery_fn(struct file *file, const char __user *user_buf, size_t size, loff_t *pos)
293 {
294 	struct ivpu_device *vdev = file->private_data;
295 	int ret;
296 
297 	if (!size)
298 		return -EINVAL;
299 
300 	ret = ivpu_rpm_get(vdev);
301 	if (ret)
302 		return ret;
303 
304 	ivpu_pm_trigger_recovery(vdev, "debugfs");
305 	flush_work(&vdev->pm->recovery_work);
306 	ivpu_rpm_put(vdev);
307 	return size;
308 }
309 
310 static const struct file_operations ivpu_force_recovery_fops = {
311 	.owner = THIS_MODULE,
312 	.open = simple_open,
313 	.write = ivpu_force_recovery_fn,
314 };
315 
316 static ssize_t
317 ivpu_reset_engine_fn(struct file *file, const char __user *user_buf, size_t size, loff_t *pos)
318 {
319 	struct ivpu_device *vdev = file->private_data;
320 
321 	if (!size)
322 		return -EINVAL;
323 
324 	if (ivpu_jsm_reset_engine(vdev, DRM_IVPU_ENGINE_COMPUTE))
325 		return -ENODEV;
326 	if (ivpu_jsm_reset_engine(vdev, DRM_IVPU_ENGINE_COPY))
327 		return -ENODEV;
328 
329 	return size;
330 }
331 
332 static const struct file_operations ivpu_reset_engine_fops = {
333 	.owner = THIS_MODULE,
334 	.open = simple_open,
335 	.write = ivpu_reset_engine_fn,
336 };
337 
338 void ivpu_debugfs_init(struct ivpu_device *vdev)
339 {
340 	struct dentry *debugfs_root = vdev->drm.debugfs_root;
341 
342 	drm_debugfs_add_files(&vdev->drm, vdev_debugfs_list, ARRAY_SIZE(vdev_debugfs_list));
343 
344 	debugfs_create_file("force_recovery", 0200, debugfs_root, vdev,
345 			    &ivpu_force_recovery_fops);
346 
347 	debugfs_create_file("dvfs_mode", 0200, debugfs_root, vdev,
348 			    &dvfs_mode_fops);
349 
350 	debugfs_create_file("fw_log", 0644, debugfs_root, vdev,
351 			    &fw_log_fops);
352 	debugfs_create_file("fw_trace_destination_mask", 0200, debugfs_root, vdev,
353 			    &fw_trace_destination_mask_fops);
354 	debugfs_create_file("fw_trace_hw_comp_mask", 0200, debugfs_root, vdev,
355 			    &fw_trace_hw_comp_mask_fops);
356 	debugfs_create_file("fw_trace_level", 0200, debugfs_root, vdev,
357 			    &fw_trace_level_fops);
358 
359 	debugfs_create_file("reset_engine", 0200, debugfs_root, vdev,
360 			    &ivpu_reset_engine_fops);
361 
362 	if (ivpu_hw_gen(vdev) >= IVPU_HW_40XX)
363 		debugfs_create_file("fw_profiling_freq_drive", 0200,
364 				    debugfs_root, vdev, &fw_profiling_freq_fops);
365 }
366