debugfs.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. struct mmio_diff_param {
  28. struct intel_vgpu *vgpu;
  29. int total;
  30. int diff;
  31. struct list_head diff_mmio_list;
  32. };
  33. struct diff_mmio {
  34. struct list_head node;
  35. u32 offset;
  36. u32 preg;
  37. u32 vreg;
  38. };
  39. /* Compare two diff_mmio items. */
  40. static int mmio_offset_compare(void *priv,
  41. struct list_head *a, struct list_head *b)
  42. {
  43. struct diff_mmio *ma;
  44. struct diff_mmio *mb;
  45. ma = container_of(a, struct diff_mmio, node);
  46. mb = container_of(b, struct diff_mmio, node);
  47. if (ma->offset < mb->offset)
  48. return -1;
  49. else if (ma->offset > mb->offset)
  50. return 1;
  51. return 0;
  52. }
  53. static inline int mmio_diff_handler(struct intel_gvt *gvt,
  54. u32 offset, void *data)
  55. {
  56. struct drm_i915_private *dev_priv = gvt->dev_priv;
  57. struct mmio_diff_param *param = data;
  58. struct diff_mmio *node;
  59. u32 preg, vreg;
  60. preg = I915_READ_NOTRACE(_MMIO(offset));
  61. vreg = vgpu_vreg(param->vgpu, offset);
  62. if (preg != vreg) {
  63. node = kmalloc(sizeof(*node), GFP_KERNEL);
  64. if (!node)
  65. return -ENOMEM;
  66. node->offset = offset;
  67. node->preg = preg;
  68. node->vreg = vreg;
  69. list_add(&node->node, &param->diff_mmio_list);
  70. param->diff++;
  71. }
  72. param->total++;
  73. return 0;
  74. }
  75. /* Show the all the different values of tracked mmio. */
  76. static int vgpu_mmio_diff_show(struct seq_file *s, void *unused)
  77. {
  78. struct intel_vgpu *vgpu = s->private;
  79. struct intel_gvt *gvt = vgpu->gvt;
  80. struct mmio_diff_param param = {
  81. .vgpu = vgpu,
  82. .total = 0,
  83. .diff = 0,
  84. };
  85. struct diff_mmio *node, *next;
  86. INIT_LIST_HEAD(&param.diff_mmio_list);
  87. mutex_lock(&gvt->lock);
  88. spin_lock_bh(&gvt->scheduler.mmio_context_lock);
  89. mmio_hw_access_pre(gvt->dev_priv);
  90. /* Recognize all the diff mmios to list. */
  91. intel_gvt_for_each_tracked_mmio(gvt, mmio_diff_handler, &param);
  92. mmio_hw_access_post(gvt->dev_priv);
  93. spin_unlock_bh(&gvt->scheduler.mmio_context_lock);
  94. mutex_unlock(&gvt->lock);
  95. /* In an ascending order by mmio offset. */
  96. list_sort(NULL, &param.diff_mmio_list, mmio_offset_compare);
  97. seq_printf(s, "%-8s %-8s %-8s %-8s\n", "Offset", "HW", "vGPU", "Diff");
  98. list_for_each_entry_safe(node, next, &param.diff_mmio_list, node) {
  99. u32 diff = node->preg ^ node->vreg;
  100. seq_printf(s, "%08x %08x %08x %*pbl\n",
  101. node->offset, node->preg, node->vreg,
  102. 32, &diff);
  103. list_del(&node->node);
  104. kfree(node);
  105. }
  106. seq_printf(s, "Total: %d, Diff: %d\n", param.total, param.diff);
  107. return 0;
  108. }
  109. static int vgpu_mmio_diff_open(struct inode *inode, struct file *file)
  110. {
  111. return single_open(file, vgpu_mmio_diff_show, inode->i_private);
  112. }
  113. static const struct file_operations vgpu_mmio_diff_fops = {
  114. .open = vgpu_mmio_diff_open,
  115. .read = seq_read,
  116. .llseek = seq_lseek,
  117. .release = single_release,
  118. };
  119. /**
  120. * intel_gvt_debugfs_add_vgpu - register debugfs entries for a vGPU
  121. * @vgpu: a vGPU
  122. *
  123. * Returns:
  124. * Zero on success, negative error code if failed.
  125. */
  126. int intel_gvt_debugfs_add_vgpu(struct intel_vgpu *vgpu)
  127. {
  128. struct dentry *ent;
  129. char name[10] = "";
  130. sprintf(name, "vgpu%d", vgpu->id);
  131. vgpu->debugfs = debugfs_create_dir(name, vgpu->gvt->debugfs_root);
  132. if (!vgpu->debugfs)
  133. return -ENOMEM;
  134. ent = debugfs_create_bool("active", 0444, vgpu->debugfs,
  135. &vgpu->active);
  136. if (!ent)
  137. return -ENOMEM;
  138. ent = debugfs_create_file("mmio_diff", 0444, vgpu->debugfs,
  139. vgpu, &vgpu_mmio_diff_fops);
  140. if (!ent)
  141. return -ENOMEM;
  142. return 0;
  143. }
  144. /**
  145. * intel_gvt_debugfs_remove_vgpu - remove debugfs entries of a vGPU
  146. * @vgpu: a vGPU
  147. */
  148. void intel_gvt_debugfs_remove_vgpu(struct intel_vgpu *vgpu)
  149. {
  150. debugfs_remove_recursive(vgpu->debugfs);
  151. vgpu->debugfs = NULL;
  152. }
  153. /**
  154. * intel_gvt_debugfs_init - register gvt debugfs root entry
  155. * @gvt: GVT device
  156. *
  157. * Returns:
  158. * zero on success, negative if failed.
  159. */
  160. int intel_gvt_debugfs_init(struct intel_gvt *gvt)
  161. {
  162. struct drm_minor *minor = gvt->dev_priv->drm.primary;
  163. struct dentry *ent;
  164. gvt->debugfs_root = debugfs_create_dir("gvt", minor->debugfs_root);
  165. if (!gvt->debugfs_root) {
  166. gvt_err("Cannot create debugfs dir\n");
  167. return -ENOMEM;
  168. }
  169. ent = debugfs_create_ulong("num_tracked_mmio", 0444, gvt->debugfs_root,
  170. &gvt->mmio.num_tracked_mmio);
  171. if (!ent)
  172. return -ENOMEM;
  173. return 0;
  174. }
  175. /**
  176. * intel_gvt_debugfs_clean - remove debugfs entries
  177. * @gvt: GVT device
  178. */
  179. void intel_gvt_debugfs_clean(struct intel_gvt *gvt)
  180. {
  181. debugfs_remove_recursive(gvt->debugfs_root);
  182. gvt->debugfs_root = NULL;
  183. }