i915_drm_client.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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(sizeof(*client), GFP_KERNEL);
  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_RESIDENT |
  88. DRM_GEM_OBJECT_PURGEABLE,
  89. mr->uabi_name);
  90. }
  91. static const char * const uabi_class_names[] = {
  92. [I915_ENGINE_CLASS_RENDER] = "render",
  93. [I915_ENGINE_CLASS_COPY] = "copy",
  94. [I915_ENGINE_CLASS_VIDEO] = "video",
  95. [I915_ENGINE_CLASS_VIDEO_ENHANCE] = "video-enhance",
  96. [I915_ENGINE_CLASS_COMPUTE] = "compute",
  97. };
  98. static u64 busy_add(struct i915_gem_context *ctx, unsigned int class)
  99. {
  100. struct i915_gem_engines_iter it;
  101. struct intel_context *ce;
  102. u64 total = 0;
  103. for_each_gem_engine(ce, rcu_dereference(ctx->engines), it) {
  104. if (ce->engine->uabi_class != class)
  105. continue;
  106. total += intel_context_get_total_runtime_ns(ce);
  107. }
  108. return total;
  109. }
  110. static void
  111. show_client_class(struct drm_printer *p,
  112. struct drm_i915_private *i915,
  113. struct i915_drm_client *client,
  114. unsigned int class)
  115. {
  116. const unsigned int capacity = i915->engine_uabi_class_count[class];
  117. u64 total = atomic64_read(&client->past_runtime[class]);
  118. struct i915_gem_context *ctx;
  119. rcu_read_lock();
  120. list_for_each_entry_rcu(ctx, &client->ctx_list, client_link)
  121. total += busy_add(ctx, class);
  122. rcu_read_unlock();
  123. if (capacity)
  124. drm_printf(p, "drm-engine-%s:\t%llu ns\n",
  125. uabi_class_names[class], total);
  126. if (capacity > 1)
  127. drm_printf(p, "drm-engine-capacity-%s:\t%u\n",
  128. uabi_class_names[class],
  129. capacity);
  130. }
  131. void i915_drm_client_fdinfo(struct drm_printer *p, struct drm_file *file)
  132. {
  133. struct drm_i915_file_private *file_priv = file->driver_priv;
  134. struct drm_i915_private *i915 = file_priv->i915;
  135. unsigned int i;
  136. /*
  137. * ******************************************************************
  138. * For text output format description please see drm-usage-stats.rst!
  139. * ******************************************************************
  140. */
  141. show_meminfo(p, file);
  142. if (GRAPHICS_VER(i915) < 8)
  143. return;
  144. for (i = 0; i < ARRAY_SIZE(uabi_class_names); i++)
  145. show_client_class(p, i915, file_priv->client, i);
  146. }
  147. void i915_drm_client_add_object(struct i915_drm_client *client,
  148. struct drm_i915_gem_object *obj)
  149. {
  150. unsigned long flags;
  151. GEM_WARN_ON(obj->client);
  152. GEM_WARN_ON(!list_empty(&obj->client_link));
  153. spin_lock_irqsave(&client->objects_lock, flags);
  154. obj->client = i915_drm_client_get(client);
  155. list_add_tail_rcu(&obj->client_link, &client->objects_list);
  156. spin_unlock_irqrestore(&client->objects_lock, flags);
  157. }
  158. void i915_drm_client_remove_object(struct drm_i915_gem_object *obj)
  159. {
  160. struct i915_drm_client *client = fetch_and_zero(&obj->client);
  161. unsigned long flags;
  162. /* Object may not be associated with a client. */
  163. if (!client)
  164. return;
  165. spin_lock_irqsave(&client->objects_lock, flags);
  166. list_del_rcu(&obj->client_link);
  167. spin_unlock_irqrestore(&client->objects_lock, flags);
  168. i915_drm_client_put(client);
  169. }
  170. void i915_drm_client_add_context_objects(struct i915_drm_client *client,
  171. struct intel_context *ce)
  172. {
  173. if (ce->state)
  174. i915_drm_client_add_object(client, ce->state->obj);
  175. if (ce->ring != ce->engine->legacy.ring && ce->ring->vma)
  176. i915_drm_client_add_object(client, ce->ring->vma->obj);
  177. }
  178. #endif