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