i915_drm_client.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // SPDX-License-Identifier: MIT
  2. /*
  3. * Copyright © 2020 Intel Corporation
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/slab.h>
  7. #include <linux/types.h>
  8. #include <uapi/drm/i915_drm.h>
  9. #include <drm/drm_print.h>
  10. #include "gem/i915_gem_context.h"
  11. #include "i915_drm_client.h"
  12. #include "i915_file_private.h"
  13. #include "i915_gem.h"
  14. #include "i915_utils.h"
  15. struct i915_drm_client *i915_drm_client_alloc(void)
  16. {
  17. struct i915_drm_client *client;
  18. client = kzalloc_obj(*client);
  19. if (!client)
  20. return NULL;
  21. kref_init(&client->kref);
  22. spin_lock_init(&client->ctx_lock);
  23. INIT_LIST_HEAD(&client->ctx_list);
  24. #ifdef CONFIG_PROC_FS
  25. spin_lock_init(&client->objects_lock);
  26. INIT_LIST_HEAD(&client->objects_list);
  27. #endif
  28. return client;
  29. }
  30. void __i915_drm_client_free(struct kref *kref)
  31. {
  32. struct i915_drm_client *client =
  33. container_of(kref, typeof(*client), kref);
  34. kfree(client);
  35. }
  36. #ifdef CONFIG_PROC_FS
  37. static void
  38. obj_meminfo(struct drm_i915_gem_object *obj,
  39. struct drm_memory_stats stats[INTEL_REGION_UNKNOWN])
  40. {
  41. const enum intel_region_id id = obj->mm.region ?
  42. obj->mm.region->id : INTEL_REGION_SMEM;
  43. const u64 sz = obj->base.size;
  44. if (drm_gem_object_is_shared_for_memory_stats(&obj->base))
  45. stats[id].shared += sz;
  46. else
  47. stats[id].private += sz;
  48. if (i915_gem_object_has_pages(obj)) {
  49. stats[id].resident += sz;
  50. if (!dma_resv_test_signaled(obj->base.resv,
  51. DMA_RESV_USAGE_BOOKKEEP))
  52. stats[id].active += sz;
  53. else if (i915_gem_object_is_shrinkable(obj) &&
  54. obj->mm.madv == I915_MADV_DONTNEED)
  55. stats[id].purgeable += sz;
  56. }
  57. }
  58. static void show_meminfo(struct drm_printer *p, struct drm_file *file)
  59. {
  60. struct drm_memory_stats stats[INTEL_REGION_UNKNOWN] = {};
  61. struct drm_i915_file_private *fpriv = file->driver_priv;
  62. struct i915_drm_client *client = fpriv->client;
  63. struct drm_i915_private *i915 = fpriv->i915;
  64. struct drm_i915_gem_object *obj;
  65. struct intel_memory_region *mr;
  66. struct list_head __rcu *pos;
  67. unsigned int id;
  68. /* Public objects. */
  69. spin_lock(&file->table_lock);
  70. idr_for_each_entry(&file->object_idr, obj, id)
  71. obj_meminfo(obj, stats);
  72. spin_unlock(&file->table_lock);
  73. /* Internal objects. */
  74. rcu_read_lock();
  75. list_for_each_rcu(pos, &client->objects_list) {
  76. obj = i915_gem_object_get_rcu(list_entry(pos, typeof(*obj),
  77. client_link));
  78. if (!obj)
  79. continue;
  80. obj_meminfo(obj, stats);
  81. i915_gem_object_put(obj);
  82. }
  83. rcu_read_unlock();
  84. for_each_memory_region(mr, i915, id)
  85. drm_print_memory_stats(p,
  86. &stats[id],
  87. DRM_GEM_OBJECT_ACTIVE |
  88. DRM_GEM_OBJECT_RESIDENT |
  89. DRM_GEM_OBJECT_PURGEABLE,
  90. mr->uabi_name);
  91. }
  92. static const char * const uabi_class_names[] = {
  93. [I915_ENGINE_CLASS_RENDER] = "render",
  94. [I915_ENGINE_CLASS_COPY] = "copy",
  95. [I915_ENGINE_CLASS_VIDEO] = "video",
  96. [I915_ENGINE_CLASS_VIDEO_ENHANCE] = "video-enhance",
  97. [I915_ENGINE_CLASS_COMPUTE] = "compute",
  98. };
  99. static u64 busy_add(struct i915_gem_context *ctx, unsigned int class)
  100. {
  101. struct i915_gem_engines_iter it;
  102. struct intel_context *ce;
  103. u64 total = 0;
  104. for_each_gem_engine(ce, rcu_dereference(ctx->engines), it) {
  105. if (ce->engine->uabi_class != class)
  106. continue;
  107. total += intel_context_get_total_runtime_ns(ce);
  108. }
  109. return total;
  110. }
  111. static void
  112. show_client_class(struct drm_printer *p,
  113. struct drm_i915_private *i915,
  114. struct i915_drm_client *client,
  115. unsigned int class)
  116. {
  117. const unsigned int capacity = i915->engine_uabi_class_count[class];
  118. u64 total = atomic64_read(&client->past_runtime[class]);
  119. struct i915_gem_context *ctx;
  120. rcu_read_lock();
  121. list_for_each_entry_rcu(ctx, &client->ctx_list, client_link)
  122. total += busy_add(ctx, class);
  123. rcu_read_unlock();
  124. if (capacity)
  125. drm_printf(p, "drm-engine-%s:\t%llu ns\n",
  126. uabi_class_names[class], total);
  127. if (capacity > 1)
  128. drm_printf(p, "drm-engine-capacity-%s:\t%u\n",
  129. uabi_class_names[class],
  130. capacity);
  131. }
  132. void i915_drm_client_fdinfo(struct drm_printer *p, struct drm_file *file)
  133. {
  134. struct drm_i915_file_private *file_priv = file->driver_priv;
  135. struct drm_i915_private *i915 = file_priv->i915;
  136. unsigned int i;
  137. /*
  138. * ******************************************************************
  139. * For text output format description please see drm-usage-stats.rst!
  140. * ******************************************************************
  141. */
  142. show_meminfo(p, file);
  143. if (GRAPHICS_VER(i915) < 8)
  144. return;
  145. for (i = 0; i < ARRAY_SIZE(uabi_class_names); i++)
  146. show_client_class(p, i915, file_priv->client, i);
  147. }
  148. void i915_drm_client_add_object(struct i915_drm_client *client,
  149. struct drm_i915_gem_object *obj)
  150. {
  151. unsigned long flags;
  152. GEM_WARN_ON(obj->client);
  153. GEM_WARN_ON(!list_empty(&obj->client_link));
  154. spin_lock_irqsave(&client->objects_lock, flags);
  155. obj->client = i915_drm_client_get(client);
  156. list_add_tail_rcu(&obj->client_link, &client->objects_list);
  157. spin_unlock_irqrestore(&client->objects_lock, flags);
  158. }
  159. void i915_drm_client_remove_object(struct drm_i915_gem_object *obj)
  160. {
  161. struct i915_drm_client *client = fetch_and_zero(&obj->client);
  162. unsigned long flags;
  163. /* Object may not be associated with a client. */
  164. if (!client)
  165. return;
  166. spin_lock_irqsave(&client->objects_lock, flags);
  167. list_del_rcu(&obj->client_link);
  168. spin_unlock_irqrestore(&client->objects_lock, flags);
  169. i915_drm_client_put(client);
  170. }
  171. void i915_drm_client_add_context_objects(struct i915_drm_client *client,
  172. struct intel_context *ce)
  173. {
  174. if (ce->state)
  175. i915_drm_client_add_object(client, ce->state->obj);
  176. if (ce->ring != ce->engine->legacy.ring && ce->ring->vma)
  177. i915_drm_client_add_object(client, ce->ring->vma->obj);
  178. }
  179. #endif