xref: /linux/drivers/gpu/drm/xe/xe_gt_debugfs.c (revision aec2f682d47c54ef434b2d440992626d80b1ebdc)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2022 Intel Corporation
4  */
5 
6 #include "xe_gt_debugfs.h"
7 
8 #include <linux/debugfs.h>
9 
10 #include <drm/drm_debugfs.h>
11 #include <drm/drm_managed.h>
12 
13 #include "xe_device.h"
14 #include "xe_force_wake.h"
15 #include "xe_gt.h"
16 #include "xe_gt_mcr.h"
17 #include "xe_gt_idle.h"
18 #include "xe_gt_sriov_pf_debugfs.h"
19 #include "xe_gt_sriov_vf_debugfs.h"
20 #include "xe_gt_stats.h"
21 #include "xe_gt_topology.h"
22 #include "xe_guc_hwconfig.h"
23 #include "xe_hw_engine.h"
24 #include "xe_lrc.h"
25 #include "xe_mocs.h"
26 #include "xe_pat.h"
27 #include "xe_pm.h"
28 #include "xe_reg_sr.h"
29 #include "xe_reg_whitelist.h"
30 #include "xe_sa.h"
31 #include "xe_sriov.h"
32 #include "xe_sriov_vf_ccs.h"
33 #include "xe_tuning.h"
34 #include "xe_uc_debugfs.h"
35 #include "xe_wa.h"
36 
37 static struct xe_gt *node_to_gt(struct drm_info_node *node)
38 {
39 	return node->dent->d_parent->d_inode->i_private;
40 }
41 
42 /**
43  * xe_gt_debugfs_simple_show - A show callback for struct drm_info_list
44  * @m: the &seq_file
45  * @data: data used by the drm debugfs helpers
46  *
47  * This callback can be used in struct drm_info_list to describe debugfs
48  * files that are &xe_gt specific.
49  *
50  * It is assumed that those debugfs files will be created on directory entry
51  * which struct dentry d_inode->i_private points to &xe_gt.
52  *
53  * This function assumes that &m->private will be set to the &struct
54  * drm_info_node corresponding to the instance of the info on a given &struct
55  * drm_minor (see struct drm_info_list.show for details).
56  *
57  * This function also assumes that struct drm_info_list.data will point to the
58  * function code that will actually print a file content::
59  *
60  *   int (*print)(struct xe_gt *, struct drm_printer *)
61  *
62  * Example::
63  *
64  *    int foo(struct xe_gt *gt, struct drm_printer *p)
65  *    {
66  *        drm_printf(p, "GT%u\n", gt->info.id);
67  *        return 0;
68  *    }
69  *
70  *    static const struct drm_info_list bar[] = {
71  *        { name = "foo", .show = xe_gt_debugfs_simple_show, .data = foo },
72  *    };
73  *
74  *    dir = debugfs_create_dir("gt", parent);
75  *    dir->d_inode->i_private = gt;
76  *    drm_debugfs_create_files(bar, ARRAY_SIZE(bar), dir, minor);
77  *
78  * Return: 0 on success or a negative error code on failure.
79  */
80 int xe_gt_debugfs_simple_show(struct seq_file *m, void *data)
81 {
82 	struct drm_printer p = drm_seq_file_printer(m);
83 	struct drm_info_node *node = m->private;
84 	struct xe_gt *gt = node_to_gt(node);
85 	int (*print)(struct xe_gt *, struct drm_printer *) = node->info_ent->data;
86 
87 	if (WARN_ON(!print))
88 		return -EINVAL;
89 
90 	return print(gt, &p);
91 }
92 
93 /**
94  * xe_gt_debugfs_show_with_rpm - A show callback for struct drm_info_list
95  * @m: the &seq_file
96  * @data: data used by the drm debugfs helpers
97  *
98  * Similar to xe_gt_debugfs_simple_show() but implicitly takes a RPM ref.
99  *
100  * Return: 0 on success or a negative error code on failure.
101  */
102 int xe_gt_debugfs_show_with_rpm(struct seq_file *m, void *data)
103 {
104 	struct drm_info_node *node = m->private;
105 	struct xe_gt *gt = node_to_gt(node);
106 	struct xe_device *xe = gt_to_xe(gt);
107 
108 	guard(xe_pm_runtime)(xe);
109 	return xe_gt_debugfs_simple_show(m, data);
110 }
111 
112 static int hw_engines(struct xe_gt *gt, struct drm_printer *p)
113 {
114 	struct xe_hw_engine *hwe;
115 	enum xe_hw_engine_id id;
116 
117 	CLASS(xe_force_wake, fw_ref)(gt_to_fw(gt), XE_FORCEWAKE_ALL);
118 	if (!xe_force_wake_ref_has_domain(fw_ref.domains, XE_FORCEWAKE_ALL))
119 		return -ETIMEDOUT;
120 
121 	for_each_hw_engine(hwe, gt, id)
122 		xe_hw_engine_print(hwe, p);
123 
124 	return 0;
125 }
126 
127 static int steering(struct xe_gt *gt, struct drm_printer *p)
128 {
129 	xe_gt_mcr_steering_dump(gt, p);
130 	return 0;
131 }
132 
133 static int register_save_restore(struct xe_gt *gt, struct drm_printer *p)
134 {
135 	struct xe_hw_engine *hwe;
136 	enum xe_hw_engine_id id;
137 
138 	xe_reg_sr_dump(&gt->reg_sr, p);
139 	drm_printf(p, "\n");
140 
141 	drm_printf(p, "Engine\n");
142 	for_each_hw_engine(hwe, gt, id)
143 		xe_reg_sr_dump(&hwe->reg_sr, p);
144 	drm_printf(p, "\n");
145 
146 	drm_printf(p, "LRC\n");
147 	for_each_hw_engine(hwe, gt, id)
148 		xe_reg_sr_dump(&hwe->reg_lrc, p);
149 	drm_printf(p, "\n");
150 
151 	drm_printf(p, "Whitelist\n");
152 	for_each_hw_engine(hwe, gt, id)
153 		xe_reg_whitelist_dump(&hwe->reg_whitelist, p);
154 
155 	return 0;
156 }
157 
158 /*
159  * Check the registers referenced on a save-restore list and report any
160  * save-restore entries that did not get applied.
161  */
162 static int register_save_restore_check(struct xe_gt *gt, struct drm_printer *p)
163 {
164 	struct xe_hw_engine *hwe;
165 	enum xe_hw_engine_id id;
166 
167 	CLASS(xe_force_wake, fw_ref)(gt_to_fw(gt), XE_FORCEWAKE_ALL);
168 	if (!xe_force_wake_ref_has_domain(fw_ref.domains, XE_FORCEWAKE_ALL)) {
169 		drm_printf(p, "ERROR: Could not acquire forcewake\n");
170 		return -ETIMEDOUT;
171 	}
172 
173 	xe_reg_sr_readback_check(&gt->reg_sr, gt, p);
174 	for_each_hw_engine(hwe, gt, id)
175 		xe_reg_sr_readback_check(&hwe->reg_sr, gt, p);
176 	for_each_hw_engine(hwe, gt, id)
177 		xe_reg_sr_lrc_check(&hwe->reg_lrc, gt, hwe, p);
178 
179 	return 0;
180 }
181 
182 static int rcs_default_lrc(struct xe_gt *gt, struct drm_printer *p)
183 {
184 	xe_lrc_dump_default(p, gt, XE_ENGINE_CLASS_RENDER);
185 	return 0;
186 }
187 
188 static int ccs_default_lrc(struct xe_gt *gt, struct drm_printer *p)
189 {
190 	xe_lrc_dump_default(p, gt, XE_ENGINE_CLASS_COMPUTE);
191 	return 0;
192 }
193 
194 static int bcs_default_lrc(struct xe_gt *gt, struct drm_printer *p)
195 {
196 	xe_lrc_dump_default(p, gt, XE_ENGINE_CLASS_COPY);
197 	return 0;
198 }
199 
200 static int vcs_default_lrc(struct xe_gt *gt, struct drm_printer *p)
201 {
202 	xe_lrc_dump_default(p, gt, XE_ENGINE_CLASS_VIDEO_DECODE);
203 	return 0;
204 }
205 
206 static int vecs_default_lrc(struct xe_gt *gt, struct drm_printer *p)
207 {
208 	xe_lrc_dump_default(p, gt, XE_ENGINE_CLASS_VIDEO_ENHANCE);
209 	return 0;
210 }
211 
212 static int hwconfig(struct xe_gt *gt, struct drm_printer *p)
213 {
214 	xe_guc_hwconfig_dump(&gt->uc.guc, p);
215 	return 0;
216 }
217 
218 /*
219  * only for GT debugfs files which can be safely used on the VF as well:
220  * - without access to the GT privileged registers
221  * - without access to the PF specific data
222  */
223 static const struct drm_info_list vf_safe_debugfs_list[] = {
224 	{ "topology", .show = xe_gt_debugfs_show_with_rpm, .data = xe_gt_topology_dump },
225 	{ "register-save-restore",
226 		.show = xe_gt_debugfs_show_with_rpm, .data = register_save_restore },
227 	{ "workarounds", .show = xe_gt_debugfs_show_with_rpm, .data = xe_wa_gt_dump },
228 	{ "tunings", .show = xe_gt_debugfs_show_with_rpm, .data = xe_tuning_dump },
229 	{ "default_lrc_rcs", .show = xe_gt_debugfs_show_with_rpm, .data = rcs_default_lrc },
230 	{ "default_lrc_ccs", .show = xe_gt_debugfs_show_with_rpm, .data = ccs_default_lrc },
231 	{ "default_lrc_bcs", .show = xe_gt_debugfs_show_with_rpm, .data = bcs_default_lrc },
232 	{ "default_lrc_vcs", .show = xe_gt_debugfs_show_with_rpm, .data = vcs_default_lrc },
233 	{ "default_lrc_vecs", .show = xe_gt_debugfs_show_with_rpm, .data = vecs_default_lrc },
234 	{ "hwconfig", .show = xe_gt_debugfs_show_with_rpm, .data = hwconfig },
235 	{ "pat_sw_config", .show = xe_gt_debugfs_simple_show, .data = xe_pat_dump_sw_config },
236 	{ "register-save-restore-check",
237 		.show = xe_gt_debugfs_show_with_rpm, .data = register_save_restore_check },
238 };
239 
240 /* everything else should be added here */
241 static const struct drm_info_list pf_only_debugfs_list[] = {
242 	{ "hw_engines", .show = xe_gt_debugfs_show_with_rpm, .data = hw_engines },
243 	{ "mocs", .show = xe_gt_debugfs_show_with_rpm, .data = xe_mocs_dump },
244 	{ "pat", .show = xe_gt_debugfs_show_with_rpm, .data = xe_pat_dump },
245 	{ "powergate_info", .show = xe_gt_debugfs_show_with_rpm, .data = xe_gt_idle_pg_print },
246 	{ "steering", .show = xe_gt_debugfs_show_with_rpm, .data = steering },
247 };
248 
249 static ssize_t write_to_gt_call(const char __user *userbuf, size_t count, loff_t *ppos,
250 				void (*call)(struct xe_gt *), struct xe_gt *gt)
251 {
252 	bool yes;
253 	int ret;
254 
255 	if (*ppos)
256 		return -EINVAL;
257 	ret = kstrtobool_from_user(userbuf, count, &yes);
258 	if (ret < 0)
259 		return ret;
260 	if (yes)
261 		call(gt);
262 	return count;
263 }
264 
265 static ssize_t stats_write(struct file *file, const char __user *userbuf,
266 			   size_t count, loff_t *ppos)
267 {
268 	struct seq_file *s = file->private_data;
269 	struct xe_gt *gt = s->private;
270 
271 	return write_to_gt_call(userbuf, count, ppos, xe_gt_stats_clear, gt);
272 }
273 
274 static int stats_show(struct seq_file *s, void *unused)
275 {
276 	struct drm_printer p = drm_seq_file_printer(s);
277 	struct xe_gt *gt = s->private;
278 
279 	return xe_gt_stats_print_info(gt, &p);
280 }
281 DEFINE_SHOW_STORE_ATTRIBUTE(stats);
282 
283 static void force_reset(struct xe_gt *gt)
284 {
285 	struct xe_device *xe = gt_to_xe(gt);
286 
287 	guard(xe_pm_runtime)(xe);
288 	xe_gt_reset_async(gt);
289 }
290 
291 static ssize_t force_reset_write(struct file *file,
292 				 const char __user *userbuf,
293 				 size_t count, loff_t *ppos)
294 {
295 	struct seq_file *s = file->private_data;
296 	struct xe_gt *gt = s->private;
297 
298 	return write_to_gt_call(userbuf, count, ppos, force_reset, gt);
299 }
300 
301 static int force_reset_show(struct seq_file *s, void *unused)
302 {
303 	struct xe_gt *gt = s->private;
304 
305 	force_reset(gt); /* to be deprecated! */
306 	return 0;
307 }
308 DEFINE_SHOW_STORE_ATTRIBUTE(force_reset);
309 
310 static void force_reset_sync(struct xe_gt *gt)
311 {
312 	struct xe_device *xe = gt_to_xe(gt);
313 
314 	guard(xe_pm_runtime)(xe);
315 	xe_gt_reset(gt);
316 }
317 
318 static ssize_t force_reset_sync_write(struct file *file,
319 				      const char __user *userbuf,
320 				      size_t count, loff_t *ppos)
321 {
322 	struct seq_file *s = file->private_data;
323 	struct xe_gt *gt = s->private;
324 
325 	return write_to_gt_call(userbuf, count, ppos, force_reset_sync, gt);
326 }
327 
328 static int force_reset_sync_show(struct seq_file *s, void *unused)
329 {
330 	struct xe_gt *gt = s->private;
331 
332 	force_reset_sync(gt); /* to be deprecated! */
333 	return 0;
334 }
335 DEFINE_SHOW_STORE_ATTRIBUTE(force_reset_sync);
336 
337 void xe_gt_debugfs_register(struct xe_gt *gt)
338 {
339 	struct xe_device *xe = gt_to_xe(gt);
340 	struct drm_minor *minor = gt_to_xe(gt)->drm.primary;
341 	struct dentry *parent = gt->tile->debugfs;
342 	struct dentry *root;
343 	char symlink[16];
344 	char name[8];
345 
346 	xe_gt_assert(gt, minor->debugfs_root);
347 
348 	if (IS_ERR(parent))
349 		return;
350 
351 	snprintf(name, sizeof(name), "gt%d", gt->info.id);
352 	root = debugfs_create_dir(name, parent);
353 	if (IS_ERR(root)) {
354 		drm_warn(&xe->drm, "Create GT directory failed");
355 		return;
356 	}
357 
358 	/*
359 	 * Store the xe_gt pointer as private data of the gt/ directory node
360 	 * so other GT specific attributes under that directory may refer to
361 	 * it by looking at its parent node private data.
362 	 */
363 	root->d_inode->i_private = gt;
364 
365 	/* VF safe */
366 	debugfs_create_file("stats", 0600, root, gt, &stats_fops);
367 	debugfs_create_file("force_reset", 0600, root, gt, &force_reset_fops);
368 	debugfs_create_file("force_reset_sync", 0600, root, gt, &force_reset_sync_fops);
369 
370 	drm_debugfs_create_files(vf_safe_debugfs_list,
371 				 ARRAY_SIZE(vf_safe_debugfs_list),
372 				 root, minor);
373 
374 	if (!IS_SRIOV_VF(xe))
375 		drm_debugfs_create_files(pf_only_debugfs_list,
376 					 ARRAY_SIZE(pf_only_debugfs_list),
377 					 root, minor);
378 
379 	xe_uc_debugfs_register(&gt->uc, root);
380 
381 	if (IS_SRIOV_PF(xe))
382 		xe_gt_sriov_pf_debugfs_register(gt, root);
383 	else if (IS_SRIOV_VF(xe))
384 		xe_gt_sriov_vf_debugfs_register(gt, root);
385 
386 	/*
387 	 * Backwards compatibility only: create a link for the legacy clients
388 	 * who may expect gt/ directory at the root level, not the tile level.
389 	 */
390 	snprintf(symlink, sizeof(symlink), "tile%u/%s", gt->tile->id, name);
391 	debugfs_create_symlink(name, minor->debugfs_root, symlink);
392 }
393