18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright(c) 2011-2017 Intel Corporation. All rights reserved.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
58c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
68c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation
78c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
88c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
98c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice (including the next
128c2ecf20Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
138c2ecf20Sopenharmony_ci * Software.
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
168c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
178c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
188c2ecf20Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
198c2ecf20Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
208c2ecf20Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
218c2ecf20Sopenharmony_ci * SOFTWARE.
228c2ecf20Sopenharmony_ci */
238c2ecf20Sopenharmony_ci#include <linux/debugfs.h>
248c2ecf20Sopenharmony_ci#include <linux/list_sort.h>
258c2ecf20Sopenharmony_ci#include "i915_drv.h"
268c2ecf20Sopenharmony_ci#include "gvt.h"
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistruct mmio_diff_param {
298c2ecf20Sopenharmony_ci	struct intel_vgpu *vgpu;
308c2ecf20Sopenharmony_ci	int total;
318c2ecf20Sopenharmony_ci	int diff;
328c2ecf20Sopenharmony_ci	struct list_head diff_mmio_list;
338c2ecf20Sopenharmony_ci};
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistruct diff_mmio {
368c2ecf20Sopenharmony_ci	struct list_head node;
378c2ecf20Sopenharmony_ci	u32 offset;
388c2ecf20Sopenharmony_ci	u32 preg;
398c2ecf20Sopenharmony_ci	u32 vreg;
408c2ecf20Sopenharmony_ci};
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci/* Compare two diff_mmio items. */
438c2ecf20Sopenharmony_cistatic int mmio_offset_compare(void *priv,
448c2ecf20Sopenharmony_ci	const struct list_head *a, const struct list_head *b)
458c2ecf20Sopenharmony_ci{
468c2ecf20Sopenharmony_ci	struct diff_mmio *ma;
478c2ecf20Sopenharmony_ci	struct diff_mmio *mb;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	ma = container_of(a, struct diff_mmio, node);
508c2ecf20Sopenharmony_ci	mb = container_of(b, struct diff_mmio, node);
518c2ecf20Sopenharmony_ci	if (ma->offset < mb->offset)
528c2ecf20Sopenharmony_ci		return -1;
538c2ecf20Sopenharmony_ci	else if (ma->offset > mb->offset)
548c2ecf20Sopenharmony_ci		return 1;
558c2ecf20Sopenharmony_ci	return 0;
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cistatic inline int mmio_diff_handler(struct intel_gvt *gvt,
598c2ecf20Sopenharmony_ci				    u32 offset, void *data)
608c2ecf20Sopenharmony_ci{
618c2ecf20Sopenharmony_ci	struct mmio_diff_param *param = data;
628c2ecf20Sopenharmony_ci	struct diff_mmio *node;
638c2ecf20Sopenharmony_ci	u32 preg, vreg;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	preg = intel_uncore_read_notrace(gvt->gt->uncore, _MMIO(offset));
668c2ecf20Sopenharmony_ci	vreg = vgpu_vreg(param->vgpu, offset);
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	if (preg != vreg) {
698c2ecf20Sopenharmony_ci		node = kmalloc(sizeof(*node), GFP_ATOMIC);
708c2ecf20Sopenharmony_ci		if (!node)
718c2ecf20Sopenharmony_ci			return -ENOMEM;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci		node->offset = offset;
748c2ecf20Sopenharmony_ci		node->preg = preg;
758c2ecf20Sopenharmony_ci		node->vreg = vreg;
768c2ecf20Sopenharmony_ci		list_add(&node->node, &param->diff_mmio_list);
778c2ecf20Sopenharmony_ci		param->diff++;
788c2ecf20Sopenharmony_ci	}
798c2ecf20Sopenharmony_ci	param->total++;
808c2ecf20Sopenharmony_ci	return 0;
818c2ecf20Sopenharmony_ci}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci/* Show the all the different values of tracked mmio. */
848c2ecf20Sopenharmony_cistatic int vgpu_mmio_diff_show(struct seq_file *s, void *unused)
858c2ecf20Sopenharmony_ci{
868c2ecf20Sopenharmony_ci	struct intel_vgpu *vgpu = s->private;
878c2ecf20Sopenharmony_ci	struct intel_gvt *gvt = vgpu->gvt;
888c2ecf20Sopenharmony_ci	struct mmio_diff_param param = {
898c2ecf20Sopenharmony_ci		.vgpu = vgpu,
908c2ecf20Sopenharmony_ci		.total = 0,
918c2ecf20Sopenharmony_ci		.diff = 0,
928c2ecf20Sopenharmony_ci	};
938c2ecf20Sopenharmony_ci	struct diff_mmio *node, *next;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&param.diff_mmio_list);
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	mutex_lock(&gvt->lock);
988c2ecf20Sopenharmony_ci	spin_lock_bh(&gvt->scheduler.mmio_context_lock);
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	mmio_hw_access_pre(gvt->gt);
1018c2ecf20Sopenharmony_ci	/* Recognize all the diff mmios to list. */
1028c2ecf20Sopenharmony_ci	intel_gvt_for_each_tracked_mmio(gvt, mmio_diff_handler, &param);
1038c2ecf20Sopenharmony_ci	mmio_hw_access_post(gvt->gt);
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	spin_unlock_bh(&gvt->scheduler.mmio_context_lock);
1068c2ecf20Sopenharmony_ci	mutex_unlock(&gvt->lock);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	/* In an ascending order by mmio offset. */
1098c2ecf20Sopenharmony_ci	list_sort(NULL, &param.diff_mmio_list, mmio_offset_compare);
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	seq_printf(s, "%-8s %-8s %-8s %-8s\n", "Offset", "HW", "vGPU", "Diff");
1128c2ecf20Sopenharmony_ci	list_for_each_entry_safe(node, next, &param.diff_mmio_list, node) {
1138c2ecf20Sopenharmony_ci		u32 diff = node->preg ^ node->vreg;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci		seq_printf(s, "%08x %08x %08x %*pbl\n",
1168c2ecf20Sopenharmony_ci			   node->offset, node->preg, node->vreg,
1178c2ecf20Sopenharmony_ci			   32, &diff);
1188c2ecf20Sopenharmony_ci		list_del(&node->node);
1198c2ecf20Sopenharmony_ci		kfree(node);
1208c2ecf20Sopenharmony_ci	}
1218c2ecf20Sopenharmony_ci	seq_printf(s, "Total: %d, Diff: %d\n", param.total, param.diff);
1228c2ecf20Sopenharmony_ci	return 0;
1238c2ecf20Sopenharmony_ci}
1248c2ecf20Sopenharmony_ciDEFINE_SHOW_ATTRIBUTE(vgpu_mmio_diff);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistatic int
1278c2ecf20Sopenharmony_civgpu_scan_nonprivbb_get(void *data, u64 *val)
1288c2ecf20Sopenharmony_ci{
1298c2ecf20Sopenharmony_ci	struct intel_vgpu *vgpu = (struct intel_vgpu *)data;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	*val = vgpu->scan_nonprivbb;
1328c2ecf20Sopenharmony_ci	return 0;
1338c2ecf20Sopenharmony_ci}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci/*
1368c2ecf20Sopenharmony_ci * set/unset bit engine_id of vgpu->scan_nonprivbb to turn on/off scanning
1378c2ecf20Sopenharmony_ci * of non-privileged batch buffer. e.g.
1388c2ecf20Sopenharmony_ci * if vgpu->scan_nonprivbb=3, then it will scan non-privileged batch buffer
1398c2ecf20Sopenharmony_ci * on engine 0 and 1.
1408c2ecf20Sopenharmony_ci */
1418c2ecf20Sopenharmony_cistatic int
1428c2ecf20Sopenharmony_civgpu_scan_nonprivbb_set(void *data, u64 val)
1438c2ecf20Sopenharmony_ci{
1448c2ecf20Sopenharmony_ci	struct intel_vgpu *vgpu = (struct intel_vgpu *)data;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	vgpu->scan_nonprivbb = val;
1478c2ecf20Sopenharmony_ci	return 0;
1488c2ecf20Sopenharmony_ci}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ciDEFINE_SIMPLE_ATTRIBUTE(vgpu_scan_nonprivbb_fops,
1518c2ecf20Sopenharmony_ci			vgpu_scan_nonprivbb_get, vgpu_scan_nonprivbb_set,
1528c2ecf20Sopenharmony_ci			"0x%llx\n");
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci/**
1558c2ecf20Sopenharmony_ci * intel_gvt_debugfs_add_vgpu - register debugfs entries for a vGPU
1568c2ecf20Sopenharmony_ci * @vgpu: a vGPU
1578c2ecf20Sopenharmony_ci */
1588c2ecf20Sopenharmony_civoid intel_gvt_debugfs_add_vgpu(struct intel_vgpu *vgpu)
1598c2ecf20Sopenharmony_ci{
1608c2ecf20Sopenharmony_ci	char name[16] = "";
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	snprintf(name, 16, "vgpu%d", vgpu->id);
1638c2ecf20Sopenharmony_ci	vgpu->debugfs = debugfs_create_dir(name, vgpu->gvt->debugfs_root);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	debugfs_create_bool("active", 0444, vgpu->debugfs, &vgpu->active);
1668c2ecf20Sopenharmony_ci	debugfs_create_file("mmio_diff", 0444, vgpu->debugfs, vgpu,
1678c2ecf20Sopenharmony_ci			    &vgpu_mmio_diff_fops);
1688c2ecf20Sopenharmony_ci	debugfs_create_file("scan_nonprivbb", 0644, vgpu->debugfs, vgpu,
1698c2ecf20Sopenharmony_ci			    &vgpu_scan_nonprivbb_fops);
1708c2ecf20Sopenharmony_ci}
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci/**
1738c2ecf20Sopenharmony_ci * intel_gvt_debugfs_remove_vgpu - remove debugfs entries of a vGPU
1748c2ecf20Sopenharmony_ci * @vgpu: a vGPU
1758c2ecf20Sopenharmony_ci */
1768c2ecf20Sopenharmony_civoid intel_gvt_debugfs_remove_vgpu(struct intel_vgpu *vgpu)
1778c2ecf20Sopenharmony_ci{
1788c2ecf20Sopenharmony_ci	struct intel_gvt *gvt = vgpu->gvt;
1798c2ecf20Sopenharmony_ci	struct drm_minor *minor = gvt->gt->i915->drm.primary;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	if (minor->debugfs_root && gvt->debugfs_root) {
1828c2ecf20Sopenharmony_ci		debugfs_remove_recursive(vgpu->debugfs);
1838c2ecf20Sopenharmony_ci		vgpu->debugfs = NULL;
1848c2ecf20Sopenharmony_ci	}
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci/**
1888c2ecf20Sopenharmony_ci * intel_gvt_debugfs_init - register gvt debugfs root entry
1898c2ecf20Sopenharmony_ci * @gvt: GVT device
1908c2ecf20Sopenharmony_ci */
1918c2ecf20Sopenharmony_civoid intel_gvt_debugfs_init(struct intel_gvt *gvt)
1928c2ecf20Sopenharmony_ci{
1938c2ecf20Sopenharmony_ci	struct drm_minor *minor = gvt->gt->i915->drm.primary;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	gvt->debugfs_root = debugfs_create_dir("gvt", minor->debugfs_root);
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	debugfs_create_ulong("num_tracked_mmio", 0444, gvt->debugfs_root,
1988c2ecf20Sopenharmony_ci			     &gvt->mmio.num_tracked_mmio);
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci/**
2028c2ecf20Sopenharmony_ci * intel_gvt_debugfs_clean - remove debugfs entries
2038c2ecf20Sopenharmony_ci * @gvt: GVT device
2048c2ecf20Sopenharmony_ci */
2058c2ecf20Sopenharmony_civoid intel_gvt_debugfs_clean(struct intel_gvt *gvt)
2068c2ecf20Sopenharmony_ci{
2078c2ecf20Sopenharmony_ci	struct drm_minor *minor = gvt->gt->i915->drm.primary;
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	if (minor->debugfs_root) {
2108c2ecf20Sopenharmony_ci		debugfs_remove_recursive(gvt->debugfs_root);
2118c2ecf20Sopenharmony_ci		gvt->debugfs_root = NULL;
2128c2ecf20Sopenharmony_ci	}
2138c2ecf20Sopenharmony_ci}
214