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_RESIDENT |
106 DRM_GEM_OBJECT_PURGEABLE,
107 mr->uabi_name);
108 }
109
110 static const char * const uabi_class_names[] = {
111 [I915_ENGINE_CLASS_RENDER] = "render",
112 [I915_ENGINE_CLASS_COPY] = "copy",
113 [I915_ENGINE_CLASS_VIDEO] = "video",
114 [I915_ENGINE_CLASS_VIDEO_ENHANCE] = "video-enhance",
115 [I915_ENGINE_CLASS_COMPUTE] = "compute",
116 };
117
busy_add(struct i915_gem_context * ctx,unsigned int class)118 static u64 busy_add(struct i915_gem_context *ctx, unsigned int class)
119 {
120 struct i915_gem_engines_iter it;
121 struct intel_context *ce;
122 u64 total = 0;
123
124 for_each_gem_engine(ce, rcu_dereference(ctx->engines), it) {
125 if (ce->engine->uabi_class != class)
126 continue;
127
128 total += intel_context_get_total_runtime_ns(ce);
129 }
130
131 return total;
132 }
133
134 static void
show_client_class(struct drm_printer * p,struct drm_i915_private * i915,struct i915_drm_client * client,unsigned int class)135 show_client_class(struct drm_printer *p,
136 struct drm_i915_private *i915,
137 struct i915_drm_client *client,
138 unsigned int class)
139 {
140 const unsigned int capacity = i915->engine_uabi_class_count[class];
141 u64 total = atomic64_read(&client->past_runtime[class]);
142 struct i915_gem_context *ctx;
143
144 rcu_read_lock();
145 list_for_each_entry_rcu(ctx, &client->ctx_list, client_link)
146 total += busy_add(ctx, class);
147 rcu_read_unlock();
148
149 if (capacity)
150 drm_printf(p, "drm-engine-%s:\t%llu ns\n",
151 uabi_class_names[class], total);
152
153 if (capacity > 1)
154 drm_printf(p, "drm-engine-capacity-%s:\t%u\n",
155 uabi_class_names[class],
156 capacity);
157 }
158
i915_drm_client_fdinfo(struct drm_printer * p,struct drm_file * file)159 void i915_drm_client_fdinfo(struct drm_printer *p, struct drm_file *file)
160 {
161 struct drm_i915_file_private *file_priv = file->driver_priv;
162 struct drm_i915_private *i915 = file_priv->i915;
163 unsigned int i;
164
165 /*
166 * ******************************************************************
167 * For text output format description please see drm-usage-stats.rst!
168 * ******************************************************************
169 */
170
171 show_meminfo(p, file);
172
173 if (GRAPHICS_VER(i915) < 8)
174 return;
175
176 for (i = 0; i < ARRAY_SIZE(uabi_class_names); i++)
177 show_client_class(p, i915, file_priv->client, i);
178 }
179
i915_drm_client_add_object(struct i915_drm_client * client,struct drm_i915_gem_object * obj)180 void i915_drm_client_add_object(struct i915_drm_client *client,
181 struct drm_i915_gem_object *obj)
182 {
183 unsigned long flags;
184
185 GEM_WARN_ON(obj->client);
186 GEM_WARN_ON(!list_empty(&obj->client_link));
187
188 spin_lock_irqsave(&client->objects_lock, flags);
189 obj->client = i915_drm_client_get(client);
190 list_add_tail_rcu(&obj->client_link, &client->objects_list);
191 spin_unlock_irqrestore(&client->objects_lock, flags);
192 }
193
i915_drm_client_remove_object(struct drm_i915_gem_object * obj)194 void i915_drm_client_remove_object(struct drm_i915_gem_object *obj)
195 {
196 struct i915_drm_client *client = fetch_and_zero(&obj->client);
197 unsigned long flags;
198
199 /* Object may not be associated with a client. */
200 if (!client)
201 return;
202
203 spin_lock_irqsave(&client->objects_lock, flags);
204 list_del_rcu(&obj->client_link);
205 spin_unlock_irqrestore(&client->objects_lock, flags);
206
207 i915_drm_client_put(client);
208 }
209
i915_drm_client_add_context_objects(struct i915_drm_client * client,struct intel_context * ce)210 void i915_drm_client_add_context_objects(struct i915_drm_client *client,
211 struct intel_context *ce)
212 {
213 if (ce->state)
214 i915_drm_client_add_object(client, ce->state->obj);
215
216 if (ce->ring != ce->engine->legacy.ring && ce->ring->vma)
217 i915_drm_client_add_object(client, ce->ring->vma->obj);
218 }
219 #endif
220