xref: /linux/drivers/gpu/drm/xe/xe_gt_debugfs.c (revision 1b5d39e6672fdee158c3306f5cb2df8975c77e5a)
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 
109 	guard(xe_pm_runtime)(xe);
110 	return xe_gt_debugfs_simple_show(m, data);
111 }
112 
113 static int hw_engines(struct xe_gt *gt, struct drm_printer *p)
114 {
115 	struct xe_hw_engine *hwe;
116 	enum xe_hw_engine_id id;
117 
118 	CLASS(xe_force_wake, fw_ref)(gt_to_fw(gt), XE_FORCEWAKE_ALL);
119 	if (!xe_force_wake_ref_has_domain(fw_ref.domains, XE_FORCEWAKE_ALL))
120 		return -ETIMEDOUT;
121 
122 	for_each_hw_engine(hwe, gt, id)
123 		xe_hw_engine_print(hwe, p);
124 
125 	return 0;
126 }
127 
128 static int steering(struct xe_gt *gt, struct drm_printer *p)
129 {
130 	xe_gt_mcr_steering_dump(gt, p);
131 	return 0;
132 }
133 
134 static int register_save_restore(struct xe_gt *gt, struct drm_printer *p)
135 {
136 	struct xe_hw_engine *hwe;
137 	enum xe_hw_engine_id id;
138 
139 	xe_reg_sr_dump(&gt->reg_sr, p);
140 	drm_printf(p, "\n");
141 
142 	drm_printf(p, "Engine\n");
143 	for_each_hw_engine(hwe, gt, id)
144 		xe_reg_sr_dump(&hwe->reg_sr, p);
145 	drm_printf(p, "\n");
146 
147 	drm_printf(p, "LRC\n");
148 	for_each_hw_engine(hwe, gt, id)
149 		xe_reg_sr_dump(&hwe->reg_lrc, p);
150 	drm_printf(p, "\n");
151 
152 	drm_printf(p, "Whitelist\n");
153 	for_each_hw_engine(hwe, gt, id)
154 		xe_reg_whitelist_dump(&hwe->reg_whitelist, p);
155 
156 	return 0;
157 }
158 
159 static int rcs_default_lrc(struct xe_gt *gt, struct drm_printer *p)
160 {
161 	xe_lrc_dump_default(p, gt, XE_ENGINE_CLASS_RENDER);
162 	return 0;
163 }
164 
165 static int ccs_default_lrc(struct xe_gt *gt, struct drm_printer *p)
166 {
167 	xe_lrc_dump_default(p, gt, XE_ENGINE_CLASS_COMPUTE);
168 	return 0;
169 }
170 
171 static int bcs_default_lrc(struct xe_gt *gt, struct drm_printer *p)
172 {
173 	xe_lrc_dump_default(p, gt, XE_ENGINE_CLASS_COPY);
174 	return 0;
175 }
176 
177 static int vcs_default_lrc(struct xe_gt *gt, struct drm_printer *p)
178 {
179 	xe_lrc_dump_default(p, gt, XE_ENGINE_CLASS_VIDEO_DECODE);
180 	return 0;
181 }
182 
183 static int vecs_default_lrc(struct xe_gt *gt, struct drm_printer *p)
184 {
185 	xe_lrc_dump_default(p, gt, XE_ENGINE_CLASS_VIDEO_ENHANCE);
186 	return 0;
187 }
188 
189 static int hwconfig(struct xe_gt *gt, struct drm_printer *p)
190 {
191 	xe_guc_hwconfig_dump(&gt->uc.guc, p);
192 	return 0;
193 }
194 
195 /*
196  * only for GT debugfs files which can be safely used on the VF as well:
197  * - without access to the GT privileged registers
198  * - without access to the PF specific data
199  */
200 static const struct drm_info_list vf_safe_debugfs_list[] = {
201 	{ "topology", .show = xe_gt_debugfs_show_with_rpm, .data = xe_gt_topology_dump },
202 	{ "register-save-restore",
203 		.show = xe_gt_debugfs_show_with_rpm, .data = register_save_restore },
204 	{ "workarounds", .show = xe_gt_debugfs_show_with_rpm, .data = xe_wa_gt_dump },
205 	{ "tunings", .show = xe_gt_debugfs_show_with_rpm, .data = xe_tuning_dump },
206 	{ "default_lrc_rcs", .show = xe_gt_debugfs_show_with_rpm, .data = rcs_default_lrc },
207 	{ "default_lrc_ccs", .show = xe_gt_debugfs_show_with_rpm, .data = ccs_default_lrc },
208 	{ "default_lrc_bcs", .show = xe_gt_debugfs_show_with_rpm, .data = bcs_default_lrc },
209 	{ "default_lrc_vcs", .show = xe_gt_debugfs_show_with_rpm, .data = vcs_default_lrc },
210 	{ "default_lrc_vecs", .show = xe_gt_debugfs_show_with_rpm, .data = vecs_default_lrc },
211 	{ "hwconfig", .show = xe_gt_debugfs_show_with_rpm, .data = hwconfig },
212 	{ "pat_sw_config", .show = xe_gt_debugfs_simple_show, .data = xe_pat_dump_sw_config },
213 };
214 
215 /* everything else should be added here */
216 static const struct drm_info_list pf_only_debugfs_list[] = {
217 	{ "hw_engines", .show = xe_gt_debugfs_show_with_rpm, .data = hw_engines },
218 	{ "mocs", .show = xe_gt_debugfs_show_with_rpm, .data = xe_mocs_dump },
219 	{ "pat", .show = xe_gt_debugfs_show_with_rpm, .data = xe_pat_dump },
220 	{ "powergate_info", .show = xe_gt_debugfs_show_with_rpm, .data = xe_gt_idle_pg_print },
221 	{ "steering", .show = xe_gt_debugfs_show_with_rpm, .data = steering },
222 };
223 
224 static ssize_t write_to_gt_call(const char __user *userbuf, size_t count, loff_t *ppos,
225 				void (*call)(struct xe_gt *), struct xe_gt *gt)
226 {
227 	bool yes;
228 	int ret;
229 
230 	if (*ppos)
231 		return -EINVAL;
232 	ret = kstrtobool_from_user(userbuf, count, &yes);
233 	if (ret < 0)
234 		return ret;
235 	if (yes)
236 		call(gt);
237 	return count;
238 }
239 
240 static ssize_t stats_write(struct file *file, const char __user *userbuf,
241 			   size_t count, loff_t *ppos)
242 {
243 	struct seq_file *s = file->private_data;
244 	struct xe_gt *gt = s->private;
245 
246 	return write_to_gt_call(userbuf, count, ppos, xe_gt_stats_clear, gt);
247 }
248 
249 static int stats_show(struct seq_file *s, void *unused)
250 {
251 	struct drm_printer p = drm_seq_file_printer(s);
252 	struct xe_gt *gt = s->private;
253 
254 	return xe_gt_stats_print_info(gt, &p);
255 }
256 DEFINE_SHOW_STORE_ATTRIBUTE(stats);
257 
258 static void force_reset(struct xe_gt *gt)
259 {
260 	struct xe_device *xe = gt_to_xe(gt);
261 
262 	guard(xe_pm_runtime)(xe);
263 	xe_gt_reset_async(gt);
264 }
265 
266 static ssize_t force_reset_write(struct file *file,
267 				 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, force_reset, gt);
274 }
275 
276 static int force_reset_show(struct seq_file *s, void *unused)
277 {
278 	struct xe_gt *gt = s->private;
279 
280 	force_reset(gt); /* to be deprecated! */
281 	return 0;
282 }
283 DEFINE_SHOW_STORE_ATTRIBUTE(force_reset);
284 
285 static void force_reset_sync(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(gt);
291 }
292 
293 static ssize_t force_reset_sync_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_sync, gt);
301 }
302 
303 static int force_reset_sync_show(struct seq_file *s, void *unused)
304 {
305 	struct xe_gt *gt = s->private;
306 
307 	force_reset_sync(gt); /* to be deprecated! */
308 	return 0;
309 }
310 DEFINE_SHOW_STORE_ATTRIBUTE(force_reset_sync);
311 
312 void xe_gt_debugfs_register(struct xe_gt *gt)
313 {
314 	struct xe_device *xe = gt_to_xe(gt);
315 	struct drm_minor *minor = gt_to_xe(gt)->drm.primary;
316 	struct dentry *parent = gt->tile->debugfs;
317 	struct dentry *root;
318 	char symlink[16];
319 	char name[8];
320 
321 	xe_gt_assert(gt, minor->debugfs_root);
322 
323 	if (IS_ERR(parent))
324 		return;
325 
326 	snprintf(name, sizeof(name), "gt%d", gt->info.id);
327 	root = debugfs_create_dir(name, parent);
328 	if (IS_ERR(root)) {
329 		drm_warn(&xe->drm, "Create GT directory failed");
330 		return;
331 	}
332 
333 	/*
334 	 * Store the xe_gt pointer as private data of the gt/ directory node
335 	 * so other GT specific attributes under that directory may refer to
336 	 * it by looking at its parent node private data.
337 	 */
338 	root->d_inode->i_private = gt;
339 
340 	/* VF safe */
341 	debugfs_create_file("stats", 0600, root, gt, &stats_fops);
342 	debugfs_create_file("force_reset", 0600, root, gt, &force_reset_fops);
343 	debugfs_create_file("force_reset_sync", 0600, root, gt, &force_reset_sync_fops);
344 
345 	drm_debugfs_create_files(vf_safe_debugfs_list,
346 				 ARRAY_SIZE(vf_safe_debugfs_list),
347 				 root, minor);
348 
349 	if (!IS_SRIOV_VF(xe))
350 		drm_debugfs_create_files(pf_only_debugfs_list,
351 					 ARRAY_SIZE(pf_only_debugfs_list),
352 					 root, minor);
353 
354 	xe_uc_debugfs_register(&gt->uc, root);
355 
356 	if (IS_SRIOV_PF(xe))
357 		xe_gt_sriov_pf_debugfs_register(gt, root);
358 	else if (IS_SRIOV_VF(xe))
359 		xe_gt_sriov_vf_debugfs_register(gt, root);
360 
361 	/*
362 	 * Backwards compatibility only: create a link for the legacy clients
363 	 * who may expect gt/ directory at the root level, not the tile level.
364 	 */
365 	snprintf(symlink, sizeof(symlink), "tile%u/%s", gt->tile->id, name);
366 	debugfs_create_symlink(name, minor->debugfs_root, symlink);
367 }
368