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#include <linux/debugfs.h>
24#include <linux/list_sort.h>
25#include "i915_drv.h"
26#include "gvt.h"
27
28struct mmio_diff_param {
29 struct intel_vgpu *vgpu;
30 int total;
31 int diff;
32 struct list_head diff_mmio_list;
33};
34
35struct diff_mmio {
36 struct list_head node;
37 u32 offset;
38 u32 preg;
39 u32 vreg;
40};
41
42/* Compare two diff_mmio items. */
43static int mmio_offset_compare(void *priv,
44 const struct list_head *a, const struct list_head *b)
45{
46 struct diff_mmio *ma;
47 struct diff_mmio *mb;
48
49 ma = container_of(a, struct diff_mmio, node);
50 mb = container_of(b, struct diff_mmio, node);
51 if (ma->offset < mb->offset)
52 return -1;
53 else if (ma->offset > mb->offset)
54 return 1;
55 return 0;
56}
57
58static inline int mmio_diff_handler(struct intel_gvt *gvt,
59 u32 offset, void *data)
60{
61 struct mmio_diff_param *param = data;
62 struct diff_mmio *node;
63 u32 preg, vreg;
64
65 preg = intel_uncore_read_notrace(uncore: gvt->gt->uncore, _MMIO(offset));
66 vreg = vgpu_vreg(param->vgpu, offset);
67
68 if (preg != vreg) {
69 node = kmalloc(sizeof(*node), GFP_ATOMIC);
70 if (!node)
71 return -ENOMEM;
72
73 node->offset = offset;
74 node->preg = preg;
75 node->vreg = vreg;
76 list_add(new: &node->node, head: &param->diff_mmio_list);
77 param->diff++;
78 }
79 param->total++;
80 return 0;
81}
82
83/* Show the all the different values of tracked mmio. */
84static int vgpu_mmio_diff_show(struct seq_file *s, void *unused)
85{
86 struct intel_vgpu *vgpu = s->private;
87 struct intel_gvt *gvt = vgpu->gvt;
88 struct mmio_diff_param param = {
89 .vgpu = vgpu,
90 .total = 0,
91 .diff = 0,
92 };
93 struct diff_mmio *node, *next;
94 intel_wakeref_t wakeref;
95
96 INIT_LIST_HEAD(list: &param.diff_mmio_list);
97
98 mutex_lock(&gvt->lock);
99 spin_lock_bh(lock: &gvt->scheduler.mmio_context_lock);
100
101 wakeref = mmio_hw_access_pre(gt: gvt->gt);
102 /* Recognize all the diff mmios to list. */
103 intel_gvt_for_each_tracked_mmio(gvt, handler: mmio_diff_handler, data: &param);
104 mmio_hw_access_post(gt: gvt->gt, wakeref);
105
106 spin_unlock_bh(lock: &gvt->scheduler.mmio_context_lock);
107 mutex_unlock(lock: &gvt->lock);
108
109 /* In an ascending order by mmio offset. */
110 list_sort(NULL, head: &param.diff_mmio_list, cmp: mmio_offset_compare);
111
112 seq_printf(m: s, fmt: "%-8s %-8s %-8s %-8s\n", "Offset", "HW", "vGPU", "Diff");
113 list_for_each_entry_safe(node, next, &param.diff_mmio_list, node) {
114 u32 diff = node->preg ^ node->vreg;
115
116 seq_printf(m: s, fmt: "%08x %08x %08x %*pbl\n",
117 node->offset, node->preg, node->vreg,
118 32, &diff);
119 list_del(entry: &node->node);
120 kfree(objp: node);
121 }
122 seq_printf(m: s, fmt: "Total: %d, Diff: %d\n", param.total, param.diff);
123 return 0;
124}
125DEFINE_SHOW_ATTRIBUTE(vgpu_mmio_diff);
126
127static int
128vgpu_scan_nonprivbb_get(void *data, u64 *val)
129{
130 struct intel_vgpu *vgpu = (struct intel_vgpu *)data;
131
132 *val = vgpu->scan_nonprivbb;
133 return 0;
134}
135
136/*
137 * set/unset bit engine_id of vgpu->scan_nonprivbb to turn on/off scanning
138 * of non-privileged batch buffer. e.g.
139 * if vgpu->scan_nonprivbb=3, then it will scan non-privileged batch buffer
140 * on engine 0 and 1.
141 */
142static int
143vgpu_scan_nonprivbb_set(void *data, u64 val)
144{
145 struct intel_vgpu *vgpu = (struct intel_vgpu *)data;
146
147 vgpu->scan_nonprivbb = val;
148 return 0;
149}
150
151DEFINE_DEBUGFS_ATTRIBUTE(vgpu_scan_nonprivbb_fops,
152 vgpu_scan_nonprivbb_get, vgpu_scan_nonprivbb_set,
153 "0x%llx\n");
154
155static int vgpu_status_get(void *data, u64 *val)
156{
157 struct intel_vgpu *vgpu = (struct intel_vgpu *)data;
158
159 *val = 0;
160
161 if (test_bit(INTEL_VGPU_STATUS_ATTACHED, vgpu->status))
162 *val |= (1 << INTEL_VGPU_STATUS_ATTACHED);
163 if (test_bit(INTEL_VGPU_STATUS_ACTIVE, vgpu->status))
164 *val |= (1 << INTEL_VGPU_STATUS_ACTIVE);
165
166 return 0;
167}
168
169DEFINE_DEBUGFS_ATTRIBUTE(vgpu_status_fops, vgpu_status_get, NULL, "0x%llx\n");
170
171/**
172 * intel_gvt_debugfs_add_vgpu - register debugfs entries for a vGPU
173 * @vgpu: a vGPU
174 */
175void intel_gvt_debugfs_add_vgpu(struct intel_vgpu *vgpu)
176{
177 char name[16] = "";
178
179 snprintf(buf: name, size: 16, fmt: "vgpu%d", vgpu->id);
180 vgpu->debugfs = debugfs_create_dir(name, parent: vgpu->gvt->debugfs_root);
181
182 debugfs_create_file("mmio_diff", 0444, vgpu->debugfs, vgpu,
183 &vgpu_mmio_diff_fops);
184 debugfs_create_file_unsafe(name: "scan_nonprivbb", mode: 0644, parent: vgpu->debugfs, data: vgpu,
185 fops: &vgpu_scan_nonprivbb_fops);
186 debugfs_create_file_unsafe(name: "status", mode: 0644, parent: vgpu->debugfs, data: vgpu,
187 fops: &vgpu_status_fops);
188}
189
190/**
191 * intel_gvt_debugfs_remove_vgpu - remove debugfs entries of a vGPU
192 * @vgpu: a vGPU
193 */
194void intel_gvt_debugfs_remove_vgpu(struct intel_vgpu *vgpu)
195{
196 struct intel_gvt *gvt = vgpu->gvt;
197 struct dentry *debugfs_root = gvt->gt->i915->drm.debugfs_root;
198
199 if (debugfs_root && gvt->debugfs_root) {
200 debugfs_remove_recursive(dentry: vgpu->debugfs);
201 vgpu->debugfs = NULL;
202 }
203}
204
205/**
206 * intel_gvt_debugfs_init - register gvt debugfs root entry
207 * @gvt: GVT device
208 */
209void intel_gvt_debugfs_init(struct intel_gvt *gvt)
210{
211 struct dentry *debugfs_root = gvt->gt->i915->drm.debugfs_root;
212
213 gvt->debugfs_root = debugfs_create_dir(name: "gvt", parent: debugfs_root);
214
215 debugfs_create_ulong(name: "num_tracked_mmio", mode: 0444, parent: gvt->debugfs_root,
216 value: &gvt->mmio.num_tracked_mmio);
217}
218
219/**
220 * intel_gvt_debugfs_clean - remove debugfs entries
221 * @gvt: GVT device
222 */
223void intel_gvt_debugfs_clean(struct intel_gvt *gvt)
224{
225 struct dentry *debugfs_root = gvt->gt->i915->drm.debugfs_root;
226
227 if (debugfs_root) {
228 debugfs_remove_recursive(dentry: gvt->debugfs_root);
229 gvt->debugfs_root = NULL;
230 }
231}
232

source code of linux/drivers/gpu/drm/i915/gvt/debugfs.c