xref: /linux/drivers/gpu/drm/xe/xe_gt_debugfs.c (revision 72856afd33f2fa166c06f1536003e300e51ecf09)
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 		xe_reg_whitelist_dump(&hwe->oa_whitelist, p);
155 	}
156 
157 	return 0;
158 }
159 
160 /*
161  * Check the registers referenced on a save-restore list and report any
162  * save-restore entries that did not get applied.
163  */
164 static int register_save_restore_check(struct xe_gt *gt, struct drm_printer *p)
165 {
166 	struct xe_hw_engine *hwe;
167 	enum xe_hw_engine_id id;
168 
169 	CLASS(xe_force_wake, fw_ref)(gt_to_fw(gt), XE_FORCEWAKE_ALL);
170 	if (!xe_force_wake_ref_has_domain(fw_ref.domains, XE_FORCEWAKE_ALL)) {
171 		drm_printf(p, "ERROR: Could not acquire forcewake\n");
172 		return -ETIMEDOUT;
173 	}
174 
175 	xe_reg_sr_readback_check(&gt->reg_sr, gt, p);
176 	for_each_hw_engine(hwe, gt, id)
177 		xe_reg_sr_readback_check(&hwe->reg_sr, gt, p);
178 	for_each_hw_engine(hwe, gt, id)
179 		xe_reg_sr_lrc_check(&hwe->reg_lrc, gt, hwe, p);
180 
181 	return 0;
182 }
183 
184 static int rcs_default_lrc(struct xe_gt *gt, struct drm_printer *p)
185 {
186 	xe_lrc_dump_default(p, gt, XE_ENGINE_CLASS_RENDER);
187 	return 0;
188 }
189 
190 static int ccs_default_lrc(struct xe_gt *gt, struct drm_printer *p)
191 {
192 	xe_lrc_dump_default(p, gt, XE_ENGINE_CLASS_COMPUTE);
193 	return 0;
194 }
195 
196 static int bcs_default_lrc(struct xe_gt *gt, struct drm_printer *p)
197 {
198 	xe_lrc_dump_default(p, gt, XE_ENGINE_CLASS_COPY);
199 	return 0;
200 }
201 
202 static int vcs_default_lrc(struct xe_gt *gt, struct drm_printer *p)
203 {
204 	xe_lrc_dump_default(p, gt, XE_ENGINE_CLASS_VIDEO_DECODE);
205 	return 0;
206 }
207 
208 static int vecs_default_lrc(struct xe_gt *gt, struct drm_printer *p)
209 {
210 	xe_lrc_dump_default(p, gt, XE_ENGINE_CLASS_VIDEO_ENHANCE);
211 	return 0;
212 }
213 
214 static int hwconfig(struct xe_gt *gt, struct drm_printer *p)
215 {
216 	xe_guc_hwconfig_dump(&gt->uc.guc, p);
217 	return 0;
218 }
219 
220 /*
221  * only for GT debugfs files which can be safely used on the VF as well:
222  * - without access to the GT privileged registers
223  * - without access to the PF specific data
224  */
225 static const struct drm_info_list vf_safe_debugfs_list[] = {
226 	{ "topology", .show = xe_gt_debugfs_show_with_rpm, .data = xe_gt_topology_dump },
227 	{ "register-save-restore",
228 		.show = xe_gt_debugfs_show_with_rpm, .data = register_save_restore },
229 	{ "workarounds", .show = xe_gt_debugfs_show_with_rpm, .data = xe_wa_gt_dump },
230 	{ "tunings", .show = xe_gt_debugfs_show_with_rpm, .data = xe_tuning_dump },
231 	{ "default_lrc_rcs", .show = xe_gt_debugfs_show_with_rpm, .data = rcs_default_lrc },
232 	{ "default_lrc_ccs", .show = xe_gt_debugfs_show_with_rpm, .data = ccs_default_lrc },
233 	{ "default_lrc_bcs", .show = xe_gt_debugfs_show_with_rpm, .data = bcs_default_lrc },
234 	{ "default_lrc_vcs", .show = xe_gt_debugfs_show_with_rpm, .data = vcs_default_lrc },
235 	{ "default_lrc_vecs", .show = xe_gt_debugfs_show_with_rpm, .data = vecs_default_lrc },
236 	{ "hwconfig", .show = xe_gt_debugfs_show_with_rpm, .data = hwconfig },
237 	{ "pat_sw_config", .show = xe_gt_debugfs_simple_show, .data = xe_pat_dump_sw_config },
238 	{ "register-save-restore-check",
239 		.show = xe_gt_debugfs_show_with_rpm, .data = register_save_restore_check },
240 };
241 
242 /* everything else should be added here */
243 static const struct drm_info_list pf_only_debugfs_list[] = {
244 	{ "hw_engines", .show = xe_gt_debugfs_show_with_rpm, .data = hw_engines },
245 	{ "mocs", .show = xe_gt_debugfs_show_with_rpm, .data = xe_mocs_dump },
246 	{ "pat", .show = xe_gt_debugfs_show_with_rpm, .data = xe_pat_dump },
247 	{ "powergate_info", .show = xe_gt_debugfs_show_with_rpm, .data = xe_gt_idle_pg_print },
248 	{ "steering", .show = xe_gt_debugfs_show_with_rpm, .data = steering },
249 };
250 
251 static ssize_t write_to_gt_call(const char __user *userbuf, size_t count, loff_t *ppos,
252 				void (*call)(struct xe_gt *), struct xe_gt *gt)
253 {
254 	bool yes;
255 	int ret;
256 
257 	if (*ppos)
258 		return -EINVAL;
259 	ret = kstrtobool_from_user(userbuf, count, &yes);
260 	if (ret < 0)
261 		return ret;
262 	if (yes)
263 		call(gt);
264 	return count;
265 }
266 
267 static ssize_t stats_write(struct file *file, const char __user *userbuf,
268 			   size_t count, loff_t *ppos)
269 {
270 	struct seq_file *s = file->private_data;
271 	struct xe_gt *gt = s->private;
272 
273 	return write_to_gt_call(userbuf, count, ppos, xe_gt_stats_clear, gt);
274 }
275 
276 static int stats_show(struct seq_file *s, void *unused)
277 {
278 	struct drm_printer p = drm_seq_file_printer(s);
279 	struct xe_gt *gt = s->private;
280 
281 	return xe_gt_stats_print_info(gt, &p);
282 }
283 DEFINE_SHOW_STORE_ATTRIBUTE(stats);
284 
285 static void force_reset(struct xe_gt *gt)
286 {
287 	struct xe_device *xe = gt_to_xe(gt);
288 
289 	guard(xe_pm_runtime)(xe);
290 	xe_gt_reset_async(gt);
291 }
292 
293 static ssize_t force_reset_write(struct file *file,
294 				 const char __user *userbuf,
295 				 size_t count, loff_t *ppos)
296 {
297 	struct seq_file *s = file->private_data;
298 	struct xe_gt *gt = s->private;
299 
300 	return write_to_gt_call(userbuf, count, ppos, force_reset, gt);
301 }
302 
303 static int force_reset_show(struct seq_file *s, void *unused)
304 {
305 	struct xe_gt *gt = s->private;
306 
307 	force_reset(gt); /* to be deprecated! */
308 	return 0;
309 }
310 DEFINE_SHOW_STORE_ATTRIBUTE(force_reset);
311 
312 static void force_reset_sync(struct xe_gt *gt)
313 {
314 	struct xe_device *xe = gt_to_xe(gt);
315 
316 	guard(xe_pm_runtime)(xe);
317 	xe_gt_reset(gt);
318 }
319 
320 static ssize_t force_reset_sync_write(struct file *file,
321 				      const char __user *userbuf,
322 				      size_t count, loff_t *ppos)
323 {
324 	struct seq_file *s = file->private_data;
325 	struct xe_gt *gt = s->private;
326 
327 	return write_to_gt_call(userbuf, count, ppos, force_reset_sync, gt);
328 }
329 
330 static int force_reset_sync_show(struct seq_file *s, void *unused)
331 {
332 	struct xe_gt *gt = s->private;
333 
334 	force_reset_sync(gt); /* to be deprecated! */
335 	return 0;
336 }
337 DEFINE_SHOW_STORE_ATTRIBUTE(force_reset_sync);
338 
339 void xe_gt_debugfs_register(struct xe_gt *gt)
340 {
341 	struct xe_device *xe = gt_to_xe(gt);
342 	struct drm_minor *minor = gt_to_xe(gt)->drm.primary;
343 	struct dentry *parent = gt->tile->debugfs;
344 	struct dentry *root;
345 	char symlink[16];
346 	char name[8];
347 
348 	xe_gt_assert(gt, minor->debugfs_root);
349 
350 	if (IS_ERR(parent))
351 		return;
352 
353 	snprintf(name, sizeof(name), "gt%d", gt->info.id);
354 	root = debugfs_create_dir(name, parent);
355 	if (IS_ERR(root)) {
356 		drm_warn(&xe->drm, "Create GT directory failed");
357 		return;
358 	}
359 
360 	/*
361 	 * Store the xe_gt pointer as private data of the gt/ directory node
362 	 * so other GT specific attributes under that directory may refer to
363 	 * it by looking at its parent node private data.
364 	 */
365 	root->d_inode->i_private = gt;
366 
367 	/* VF safe */
368 	debugfs_create_file("stats", 0600, root, gt, &stats_fops);
369 	debugfs_create_file("force_reset", 0600, root, gt, &force_reset_fops);
370 	debugfs_create_file("force_reset_sync", 0600, root, gt, &force_reset_sync_fops);
371 
372 	drm_debugfs_create_files(vf_safe_debugfs_list,
373 				 ARRAY_SIZE(vf_safe_debugfs_list),
374 				 root, minor);
375 
376 	if (!IS_SRIOV_VF(xe))
377 		drm_debugfs_create_files(pf_only_debugfs_list,
378 					 ARRAY_SIZE(pf_only_debugfs_list),
379 					 root, minor);
380 
381 	xe_uc_debugfs_register(&gt->uc, root);
382 
383 	if (IS_SRIOV_PF(xe))
384 		xe_gt_sriov_pf_debugfs_register(gt, root);
385 	else if (IS_SRIOV_VF(xe))
386 		xe_gt_sriov_vf_debugfs_register(gt, root);
387 
388 	/*
389 	 * Backwards compatibility only: create a link for the legacy clients
390 	 * who may expect gt/ directory at the root level, not the tile level.
391 	 */
392 	snprintf(symlink, sizeof(symlink), "tile%u/%s", gt->tile->id, name);
393 	debugfs_create_symlink(name, minor->debugfs_root, symlink);
394 }
395