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