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