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