debugfs.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. DEFINE_SHOW_ATTRIBUTE(vgpu_mmio_diff);
  110. static int
  111. vgpu_scan_nonprivbb_get(void *data, u64 *val)
  112. {
  113. struct intel_vgpu *vgpu = (struct intel_vgpu *)data;
  114. *val = vgpu->scan_nonprivbb;
  115. return 0;
  116. }
  117. /*
  118. * set/unset bit engine_id of vgpu->scan_nonprivbb to turn on/off scanning
  119. * of non-privileged batch buffer. e.g.
  120. * if vgpu->scan_nonprivbb=3, then it will scan non-privileged batch buffer
  121. * on engine 0 and 1.
  122. */
  123. static int
  124. vgpu_scan_nonprivbb_set(void *data, u64 val)
  125. {
  126. struct intel_vgpu *vgpu = (struct intel_vgpu *)data;
  127. struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
  128. enum intel_engine_id id;
  129. char buf[128], *s;
  130. int len;
  131. val &= (1 << I915_NUM_ENGINES) - 1;
  132. if (vgpu->scan_nonprivbb == val)
  133. return 0;
  134. if (!val)
  135. goto done;
  136. len = sprintf(buf,
  137. "gvt: vgpu %d turns on non-privileged batch buffers scanning on Engines:",
  138. vgpu->id);
  139. s = buf + len;
  140. for (id = 0; id < I915_NUM_ENGINES; id++) {
  141. struct intel_engine_cs *engine;
  142. engine = dev_priv->engine[id];
  143. if (engine && (val & (1 << id))) {
  144. len = snprintf(s, 4, "%d, ", engine->id);
  145. s += len;
  146. } else
  147. val &= ~(1 << id);
  148. }
  149. if (val)
  150. sprintf(s, "low performance expected.");
  151. pr_warn("%s\n", buf);
  152. done:
  153. vgpu->scan_nonprivbb = val;
  154. return 0;
  155. }
  156. DEFINE_SIMPLE_ATTRIBUTE(vgpu_scan_nonprivbb_fops,
  157. vgpu_scan_nonprivbb_get, vgpu_scan_nonprivbb_set,
  158. "0x%llx\n");
  159. /**
  160. * intel_gvt_debugfs_add_vgpu - register debugfs entries for a vGPU
  161. * @vgpu: a vGPU
  162. *
  163. * Returns:
  164. * Zero on success, negative error code if failed.
  165. */
  166. int intel_gvt_debugfs_add_vgpu(struct intel_vgpu *vgpu)
  167. {
  168. struct dentry *ent;
  169. char name[10] = "";
  170. sprintf(name, "vgpu%d", vgpu->id);
  171. vgpu->debugfs = debugfs_create_dir(name, vgpu->gvt->debugfs_root);
  172. if (!vgpu->debugfs)
  173. return -ENOMEM;
  174. ent = debugfs_create_bool("active", 0444, vgpu->debugfs,
  175. &vgpu->active);
  176. if (!ent)
  177. return -ENOMEM;
  178. ent = debugfs_create_file("mmio_diff", 0444, vgpu->debugfs,
  179. vgpu, &vgpu_mmio_diff_fops);
  180. if (!ent)
  181. return -ENOMEM;
  182. ent = debugfs_create_file("scan_nonprivbb", 0644, vgpu->debugfs,
  183. vgpu, &vgpu_scan_nonprivbb_fops);
  184. if (!ent)
  185. return -ENOMEM;
  186. return 0;
  187. }
  188. /**
  189. * intel_gvt_debugfs_remove_vgpu - remove debugfs entries of a vGPU
  190. * @vgpu: a vGPU
  191. */
  192. void intel_gvt_debugfs_remove_vgpu(struct intel_vgpu *vgpu)
  193. {
  194. debugfs_remove_recursive(vgpu->debugfs);
  195. vgpu->debugfs = NULL;
  196. }
  197. /**
  198. * intel_gvt_debugfs_init - register gvt debugfs root entry
  199. * @gvt: GVT device
  200. *
  201. * Returns:
  202. * zero on success, negative if failed.
  203. */
  204. int intel_gvt_debugfs_init(struct intel_gvt *gvt)
  205. {
  206. struct drm_minor *minor = gvt->dev_priv->drm.primary;
  207. struct dentry *ent;
  208. gvt->debugfs_root = debugfs_create_dir("gvt", minor->debugfs_root);
  209. if (!gvt->debugfs_root) {
  210. gvt_err("Cannot create debugfs dir\n");
  211. return -ENOMEM;
  212. }
  213. ent = debugfs_create_ulong("num_tracked_mmio", 0444, gvt->debugfs_root,
  214. &gvt->mmio.num_tracked_mmio);
  215. if (!ent)
  216. return -ENOMEM;
  217. return 0;
  218. }
  219. /**
  220. * intel_gvt_debugfs_clean - remove debugfs entries
  221. * @gvt: GVT device
  222. */
  223. void intel_gvt_debugfs_clean(struct intel_gvt *gvt)
  224. {
  225. debugfs_remove_recursive(gvt->debugfs_root);
  226. gvt->debugfs_root = NULL;
  227. }