1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2021 Intel Corporation
4  */
5 
6 #include <linux/debugfs.h>
7 #include <drm/drm_print.h>
8 
9 #include "gt/intel_gt_debugfs.h"
10 #include "pxp/intel_pxp.h"
11 #include "pxp/intel_pxp_irq.h"
12 #include "i915_drv.h"
13 
14 static int pxp_info_show(struct seq_file *m, void *data)
15 {
16 	struct intel_pxp *pxp = m->private;
17 	struct drm_printer p = drm_seq_file_printer(m);
18 	bool enabled = intel_pxp_is_enabled(pxp);
19 
20 	if (!enabled) {
21 		drm_printf(&p, "pxp disabled\n");
22 		return 0;
23 	}
24 
25 	drm_printf(&p, "active: %s\n", yesno(intel_pxp_is_active(pxp)));
26 	drm_printf(&p, "instance counter: %u\n", pxp->key_instance);
27 
28 	return 0;
29 }
30 DEFINE_INTEL_GT_DEBUGFS_ATTRIBUTE(pxp_info);
31 
32 static int pxp_terminate_get(void *data, u64 *val)
33 {
34 	/* nothing to read */
35 	return -EPERM;
36 }
37 
38 static int pxp_terminate_set(void *data, u64 val)
39 {
40 	struct intel_pxp *pxp = data;
41 	struct intel_gt *gt = pxp_to_gt(pxp);
42 
43 	if (!intel_pxp_is_active(pxp))
44 		return -ENODEV;
45 
46 	/* simulate a termination interrupt */
47 	spin_lock_irq(&gt->irq_lock);
48 	intel_pxp_irq_handler(pxp, GEN12_DISPLAY_PXP_STATE_TERMINATED_INTERRUPT);
49 	spin_unlock_irq(&gt->irq_lock);
50 
51 	if (!wait_for_completion_timeout(&pxp->termination,
52 					 msecs_to_jiffies(100)))
53 		return -ETIMEDOUT;
54 
55 	return 0;
56 }
57 
58 DEFINE_SIMPLE_ATTRIBUTE(pxp_terminate_fops, pxp_terminate_get, pxp_terminate_set, "%llx\n");
59 void intel_pxp_debugfs_register(struct intel_pxp *pxp, struct dentry *gt_root)
60 {
61 	static const struct intel_gt_debugfs_file files[] = {
62 		{ "info", &pxp_info_fops, NULL },
63 		{ "terminate_state", &pxp_terminate_fops, NULL },
64 	};
65 	struct dentry *root;
66 
67 	if (!gt_root)
68 		return;
69 
70 	if (!HAS_PXP((pxp_to_gt(pxp)->i915)))
71 		return;
72 
73 	root = debugfs_create_dir("pxp", gt_root);
74 	if (IS_ERR(root))
75 		return;
76 
77 	intel_gt_debugfs_register_files(root, files, ARRAY_SIZE(files), pxp);
78 }
79