xref: /linux/drivers/gpu/drm/i915/gvt/debugfs.c (revision 9156bf442ee56c0f883aa4c81af9c8471eef6846)
1 /*
2  * Copyright(c) 2011-2017 Intel Corporation. All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 
24 #include <linux/debugfs.h>
25 #include <linux/list_sort.h>
26 
27 #include "gvt.h"
28 #include "i915_drv.h"
29 
30 struct mmio_diff_param {
31 	struct intel_vgpu *vgpu;
32 	int total;
33 	int diff;
34 	struct list_head diff_mmio_list;
35 };
36 
37 struct diff_mmio {
38 	struct list_head node;
39 	u32 offset;
40 	u32 preg;
41 	u32 vreg;
42 };
43 
44 /* Compare two diff_mmio items. */
45 static int mmio_offset_compare(void *priv,
46 	const struct list_head *a, const struct list_head *b)
47 {
48 	struct diff_mmio *ma;
49 	struct diff_mmio *mb;
50 
51 	ma = container_of(a, struct diff_mmio, node);
52 	mb = container_of(b, struct diff_mmio, node);
53 	if (ma->offset < mb->offset)
54 		return -1;
55 	else if (ma->offset > mb->offset)
56 		return 1;
57 	return 0;
58 }
59 
60 static inline int mmio_diff_handler(struct intel_gvt *gvt,
61 				    u32 offset, void *data)
62 {
63 	struct mmio_diff_param *param = data;
64 	struct diff_mmio *node;
65 	u32 preg, vreg;
66 
67 	preg = intel_uncore_read_notrace(gvt->gt->uncore, _MMIO(offset));
68 	vreg = vgpu_vreg(param->vgpu, offset);
69 
70 	if (preg != vreg) {
71 		node = kmalloc(sizeof(*node), GFP_ATOMIC);
72 		if (!node)
73 			return -ENOMEM;
74 
75 		node->offset = offset;
76 		node->preg = preg;
77 		node->vreg = vreg;
78 		list_add(&node->node, &param->diff_mmio_list);
79 		param->diff++;
80 	}
81 	param->total++;
82 	return 0;
83 }
84 
85 /* Show the all the different values of tracked mmio. */
86 static int vgpu_mmio_diff_show(struct seq_file *s, void *unused)
87 {
88 	struct intel_vgpu *vgpu = s->private;
89 	struct intel_gvt *gvt = vgpu->gvt;
90 	struct mmio_diff_param param = {
91 		.vgpu = vgpu,
92 		.total = 0,
93 		.diff = 0,
94 	};
95 	struct diff_mmio *node, *next;
96 	intel_wakeref_t wakeref;
97 
98 	INIT_LIST_HEAD(&param.diff_mmio_list);
99 
100 	mutex_lock(&gvt->lock);
101 	spin_lock_bh(&gvt->scheduler.mmio_context_lock);
102 
103 	wakeref = mmio_hw_access_pre(gvt->gt);
104 	/* Recognize all the diff mmios to list. */
105 	intel_gvt_for_each_tracked_mmio(gvt, mmio_diff_handler, &param);
106 	mmio_hw_access_post(gvt->gt, wakeref);
107 
108 	spin_unlock_bh(&gvt->scheduler.mmio_context_lock);
109 	mutex_unlock(&gvt->lock);
110 
111 	/* In an ascending order by mmio offset. */
112 	list_sort(NULL, &param.diff_mmio_list, mmio_offset_compare);
113 
114 	seq_printf(s, "%-8s %-8s %-8s %-8s\n", "Offset", "HW", "vGPU", "Diff");
115 	list_for_each_entry_safe(node, next, &param.diff_mmio_list, node) {
116 		u32 diff = node->preg ^ node->vreg;
117 
118 		seq_printf(s, "%08x %08x %08x %*pbl\n",
119 			   node->offset, node->preg, node->vreg,
120 			   32, &diff);
121 		list_del(&node->node);
122 		kfree(node);
123 	}
124 	seq_printf(s, "Total: %d, Diff: %d\n", param.total, param.diff);
125 	return 0;
126 }
127 DEFINE_SHOW_ATTRIBUTE(vgpu_mmio_diff);
128 
129 static int
130 vgpu_scan_nonprivbb_get(void *data, u64 *val)
131 {
132 	struct intel_vgpu *vgpu = (struct intel_vgpu *)data;
133 
134 	*val = vgpu->scan_nonprivbb;
135 	return 0;
136 }
137 
138 /*
139  * set/unset bit engine_id of vgpu->scan_nonprivbb to turn on/off scanning
140  * of non-privileged batch buffer. e.g.
141  * if vgpu->scan_nonprivbb=3, then it will scan non-privileged batch buffer
142  * on engine 0 and 1.
143  */
144 static int
145 vgpu_scan_nonprivbb_set(void *data, u64 val)
146 {
147 	struct intel_vgpu *vgpu = (struct intel_vgpu *)data;
148 
149 	vgpu->scan_nonprivbb = val;
150 	return 0;
151 }
152 
153 DEFINE_DEBUGFS_ATTRIBUTE(vgpu_scan_nonprivbb_fops,
154 			 vgpu_scan_nonprivbb_get, vgpu_scan_nonprivbb_set,
155 			 "0x%llx\n");
156 
157 static int vgpu_status_get(void *data, u64 *val)
158 {
159 	struct intel_vgpu *vgpu = (struct intel_vgpu *)data;
160 
161 	*val = 0;
162 
163 	if (test_bit(INTEL_VGPU_STATUS_ATTACHED, vgpu->status))
164 		*val |= (1 << INTEL_VGPU_STATUS_ATTACHED);
165 	if (test_bit(INTEL_VGPU_STATUS_ACTIVE, vgpu->status))
166 		*val |= (1 << INTEL_VGPU_STATUS_ACTIVE);
167 
168 	return 0;
169 }
170 
171 DEFINE_DEBUGFS_ATTRIBUTE(vgpu_status_fops, vgpu_status_get, NULL, "0x%llx\n");
172 
173 /**
174  * intel_gvt_debugfs_add_vgpu - register debugfs entries for a vGPU
175  * @vgpu: a vGPU
176  */
177 void intel_gvt_debugfs_add_vgpu(struct intel_vgpu *vgpu)
178 {
179 	char name[16] = "";
180 
181 	snprintf(name, 16, "vgpu%d", vgpu->id);
182 	vgpu->debugfs = debugfs_create_dir(name, vgpu->gvt->debugfs_root);
183 
184 	debugfs_create_file("mmio_diff", 0444, vgpu->debugfs, vgpu,
185 			    &vgpu_mmio_diff_fops);
186 	debugfs_create_file_unsafe("scan_nonprivbb", 0644, vgpu->debugfs, vgpu,
187 				   &vgpu_scan_nonprivbb_fops);
188 	debugfs_create_file_unsafe("status", 0644, vgpu->debugfs, vgpu,
189 				   &vgpu_status_fops);
190 }
191 
192 /**
193  * intel_gvt_debugfs_remove_vgpu - remove debugfs entries of a vGPU
194  * @vgpu: a vGPU
195  */
196 void intel_gvt_debugfs_remove_vgpu(struct intel_vgpu *vgpu)
197 {
198 	struct intel_gvt *gvt = vgpu->gvt;
199 	struct dentry *debugfs_root = gvt->gt->i915->drm.debugfs_root;
200 
201 	if (debugfs_root && gvt->debugfs_root) {
202 		debugfs_remove_recursive(vgpu->debugfs);
203 		vgpu->debugfs = NULL;
204 	}
205 }
206 
207 /**
208  * intel_gvt_debugfs_init - register gvt debugfs root entry
209  * @gvt: GVT device
210  */
211 void intel_gvt_debugfs_init(struct intel_gvt *gvt)
212 {
213 	struct dentry *debugfs_root = gvt->gt->i915->drm.debugfs_root;
214 
215 	gvt->debugfs_root = debugfs_create_dir("gvt", debugfs_root);
216 
217 	debugfs_create_ulong("num_tracked_mmio", 0444, gvt->debugfs_root,
218 			     &gvt->mmio.num_tracked_mmio);
219 }
220 
221 /**
222  * intel_gvt_debugfs_clean - remove debugfs entries
223  * @gvt: GVT device
224  */
225 void intel_gvt_debugfs_clean(struct intel_gvt *gvt)
226 {
227 	struct dentry *debugfs_root = gvt->gt->i915->drm.debugfs_root;
228 
229 	if (debugfs_root) {
230 		debugfs_remove_recursive(gvt->debugfs_root);
231 		gvt->debugfs_root = NULL;
232 	}
233 }
234