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