1 // SPDX-License-Identifier: MIT
3 * Copyright © 2019 Intel Corporation
6 #include <linux/kobject.h>
7 #include <linux/sysfs.h>
10 #include "intel_engine.h"
11 #include "intel_engine_heartbeat.h"
12 #include "sysfs_engines.h"
16 struct intel_engine_cs *engine;
19 static struct intel_engine_cs *kobj_to_engine(struct kobject *kobj)
21 return container_of(kobj, struct kobj_engine, base)->engine;
25 name_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
27 return sprintf(buf, "%s\n", kobj_to_engine(kobj)->name);
30 static struct kobj_attribute name_attr =
31 __ATTR(name, 0444, name_show, NULL);
34 class_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
36 return sprintf(buf, "%d\n", kobj_to_engine(kobj)->uabi_class);
39 static struct kobj_attribute class_attr =
40 __ATTR(class, 0444, class_show, NULL);
43 inst_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
45 return sprintf(buf, "%d\n", kobj_to_engine(kobj)->uabi_instance);
48 static struct kobj_attribute inst_attr =
49 __ATTR(instance, 0444, inst_show, NULL);
52 mmio_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
54 return sprintf(buf, "0x%x\n", kobj_to_engine(kobj)->mmio_base);
57 static struct kobj_attribute mmio_attr =
58 __ATTR(mmio_base, 0444, mmio_show, NULL);
60 static const char * const vcs_caps[] = {
61 [ilog2(I915_VIDEO_CLASS_CAPABILITY_HEVC)] = "hevc",
62 [ilog2(I915_VIDEO_AND_ENHANCE_CLASS_CAPABILITY_SFC)] = "sfc",
65 static const char * const vecs_caps[] = {
66 [ilog2(I915_VIDEO_AND_ENHANCE_CLASS_CAPABILITY_SFC)] = "sfc",
69 static ssize_t repr_trim(char *buf, ssize_t len)
71 /* Trim off the trailing space and replace with a newline */
81 __caps_show(struct intel_engine_cs *engine,
82 u32 caps, char *buf, bool show_unknown)
84 const char * const *repr;
88 BUILD_BUG_ON(!typecheck(typeof(caps), engine->uabi_capabilities));
90 switch (engine->class) {
91 case VIDEO_DECODE_CLASS:
93 count = ARRAY_SIZE(vcs_caps);
96 case VIDEO_ENHANCEMENT_CLASS:
98 count = ARRAY_SIZE(vecs_caps);
106 GEM_BUG_ON(count > BITS_PER_TYPE(typeof(caps)));
110 (unsigned long *)&caps,
111 show_unknown ? BITS_PER_TYPE(typeof(caps)) : count) {
112 if (n >= count || !repr[n]) {
113 if (GEM_WARN_ON(show_unknown))
114 len += snprintf(buf + len, PAGE_SIZE - len,
117 len += snprintf(buf + len, PAGE_SIZE - len,
120 if (GEM_WARN_ON(len >= PAGE_SIZE))
123 return repr_trim(buf, len);
127 caps_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
129 struct intel_engine_cs *engine = kobj_to_engine(kobj);
131 return __caps_show(engine, engine->uabi_capabilities, buf, true);
134 static struct kobj_attribute caps_attr =
135 __ATTR(capabilities, 0444, caps_show, NULL);
138 all_caps_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
140 return __caps_show(kobj_to_engine(kobj), -1, buf, false);
143 static struct kobj_attribute all_caps_attr =
144 __ATTR(known_capabilities, 0444, all_caps_show, NULL);
147 max_spin_store(struct kobject *kobj, struct kobj_attribute *attr,
148 const char *buf, size_t count)
150 struct intel_engine_cs *engine = kobj_to_engine(kobj);
151 unsigned long long duration;
155 * When waiting for a request, if is it currently being executed
156 * on the GPU, we busywait for a short while before sleeping. The
157 * premise is that most requests are short, and if it is already
158 * executing then there is a good chance that it will complete
159 * before we can setup the interrupt handler and go to sleep.
160 * We try to offset the cost of going to sleep, by first spinning
161 * on the request -- if it completed in less time than it would take
162 * to go sleep, process the interrupt and return back to the client,
163 * then we have saved the client some latency, albeit at the cost
164 * of spinning on an expensive CPU core.
166 * While we try to avoid waiting at all for a request that is unlikely
167 * to complete, deciding how long it is worth spinning is for is an
168 * arbitrary decision: trading off power vs latency.
171 err = kstrtoull(buf, 0, &duration);
175 if (duration > jiffies_to_nsecs(2))
178 WRITE_ONCE(engine->props.max_busywait_duration_ns, duration);
184 max_spin_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
186 struct intel_engine_cs *engine = kobj_to_engine(kobj);
188 return sprintf(buf, "%lu\n", engine->props.max_busywait_duration_ns);
191 static struct kobj_attribute max_spin_attr =
192 __ATTR(max_busywait_duration_ns, 0644, max_spin_show, max_spin_store);
195 max_spin_default(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
197 struct intel_engine_cs *engine = kobj_to_engine(kobj);
199 return sprintf(buf, "%lu\n", engine->defaults.max_busywait_duration_ns);
202 static struct kobj_attribute max_spin_def =
203 __ATTR(max_busywait_duration_ns, 0444, max_spin_default, NULL);
206 timeslice_store(struct kobject *kobj, struct kobj_attribute *attr,
207 const char *buf, size_t count)
209 struct intel_engine_cs *engine = kobj_to_engine(kobj);
210 unsigned long long duration;
214 * Execlists uses a scheduling quantum (a timeslice) to alternate
215 * execution between ready-to-run contexts of equal priority. This
216 * ensures that all users (though only if they of equal importance)
217 * have the opportunity to run and prevents livelocks where contexts
218 * may have implicit ordering due to userspace semaphores.
221 err = kstrtoull(buf, 0, &duration);
225 if (duration > jiffies_to_msecs(MAX_SCHEDULE_TIMEOUT))
228 WRITE_ONCE(engine->props.timeslice_duration_ms, duration);
230 if (execlists_active(&engine->execlists))
231 set_timer_ms(&engine->execlists.timer, duration);
237 timeslice_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
239 struct intel_engine_cs *engine = kobj_to_engine(kobj);
241 return sprintf(buf, "%lu\n", engine->props.timeslice_duration_ms);
244 static struct kobj_attribute timeslice_duration_attr =
245 __ATTR(timeslice_duration_ms, 0644, timeslice_show, timeslice_store);
248 timeslice_default(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
250 struct intel_engine_cs *engine = kobj_to_engine(kobj);
252 return sprintf(buf, "%lu\n", engine->defaults.timeslice_duration_ms);
255 static struct kobj_attribute timeslice_duration_def =
256 __ATTR(timeslice_duration_ms, 0444, timeslice_default, NULL);
259 stop_store(struct kobject *kobj, struct kobj_attribute *attr,
260 const char *buf, size_t count)
262 struct intel_engine_cs *engine = kobj_to_engine(kobj);
263 unsigned long long duration;
267 * When we allow ourselves to sleep before a GPU reset after disabling
268 * submission, even for a few milliseconds, gives an innocent context
269 * the opportunity to clear the GPU before the reset occurs. However,
270 * how long to sleep depends on the typical non-preemptible duration
271 * (a similar problem to determining the ideal preempt-reset timeout
272 * or even the heartbeat interval).
275 err = kstrtoull(buf, 0, &duration);
279 if (duration > jiffies_to_msecs(MAX_SCHEDULE_TIMEOUT))
282 WRITE_ONCE(engine->props.stop_timeout_ms, duration);
287 stop_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
289 struct intel_engine_cs *engine = kobj_to_engine(kobj);
291 return sprintf(buf, "%lu\n", engine->props.stop_timeout_ms);
294 static struct kobj_attribute stop_timeout_attr =
295 __ATTR(stop_timeout_ms, 0644, stop_show, stop_store);
298 stop_default(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
300 struct intel_engine_cs *engine = kobj_to_engine(kobj);
302 return sprintf(buf, "%lu\n", engine->defaults.stop_timeout_ms);
305 static struct kobj_attribute stop_timeout_def =
306 __ATTR(stop_timeout_ms, 0444, stop_default, NULL);
309 preempt_timeout_store(struct kobject *kobj, struct kobj_attribute *attr,
310 const char *buf, size_t count)
312 struct intel_engine_cs *engine = kobj_to_engine(kobj);
313 unsigned long long timeout;
317 * After initialising a preemption request, we give the current
318 * resident a small amount of time to vacate the GPU. The preemption
319 * request is for a higher priority context and should be immediate to
320 * maintain high quality of service (and avoid priority inversion).
321 * However, the preemption granularity of the GPU can be quite coarse
322 * and so we need a compromise.
325 err = kstrtoull(buf, 0, &timeout);
329 if (timeout > jiffies_to_msecs(MAX_SCHEDULE_TIMEOUT))
332 WRITE_ONCE(engine->props.preempt_timeout_ms, timeout);
334 if (READ_ONCE(engine->execlists.pending[0]))
335 set_timer_ms(&engine->execlists.preempt, timeout);
341 preempt_timeout_show(struct kobject *kobj, struct kobj_attribute *attr,
344 struct intel_engine_cs *engine = kobj_to_engine(kobj);
346 return sprintf(buf, "%lu\n", engine->props.preempt_timeout_ms);
349 static struct kobj_attribute preempt_timeout_attr =
350 __ATTR(preempt_timeout_ms, 0644, preempt_timeout_show, preempt_timeout_store);
353 preempt_timeout_default(struct kobject *kobj, struct kobj_attribute *attr,
356 struct intel_engine_cs *engine = kobj_to_engine(kobj);
358 return sprintf(buf, "%lu\n", engine->defaults.preempt_timeout_ms);
361 static struct kobj_attribute preempt_timeout_def =
362 __ATTR(preempt_timeout_ms, 0444, preempt_timeout_default, NULL);
365 heartbeat_store(struct kobject *kobj, struct kobj_attribute *attr,
366 const char *buf, size_t count)
368 struct intel_engine_cs *engine = kobj_to_engine(kobj);
369 unsigned long long delay;
373 * We monitor the health of the system via periodic heartbeat pulses.
374 * The pulses also provide the opportunity to perform garbage
375 * collection. However, we interpret an incomplete pulse (a missed
376 * heartbeat) as an indication that the system is no longer responsive,
377 * i.e. hung, and perform an engine or full GPU reset. Given that the
378 * preemption granularity can be very coarse on a system, the optimal
379 * value for any workload is unknowable!
382 err = kstrtoull(buf, 0, &delay);
386 if (delay >= jiffies_to_msecs(MAX_SCHEDULE_TIMEOUT))
389 err = intel_engine_set_heartbeat(engine, delay);
397 heartbeat_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
399 struct intel_engine_cs *engine = kobj_to_engine(kobj);
401 return sprintf(buf, "%lu\n", engine->props.heartbeat_interval_ms);
404 static struct kobj_attribute heartbeat_interval_attr =
405 __ATTR(heartbeat_interval_ms, 0644, heartbeat_show, heartbeat_store);
408 heartbeat_default(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
410 struct intel_engine_cs *engine = kobj_to_engine(kobj);
412 return sprintf(buf, "%lu\n", engine->defaults.heartbeat_interval_ms);
415 static struct kobj_attribute heartbeat_interval_def =
416 __ATTR(heartbeat_interval_ms, 0444, heartbeat_default, NULL);
418 static void kobj_engine_release(struct kobject *kobj)
423 static struct kobj_type kobj_engine_type = {
424 .release = kobj_engine_release,
425 .sysfs_ops = &kobj_sysfs_ops
428 static struct kobject *
429 kobj_engine(struct kobject *dir, struct intel_engine_cs *engine)
431 struct kobj_engine *ke;
433 ke = kzalloc(sizeof(*ke), GFP_KERNEL);
437 kobject_init(&ke->base, &kobj_engine_type);
440 if (kobject_add(&ke->base, dir, "%s", engine->name)) {
441 kobject_put(&ke->base);
445 /* xfer ownership to sysfs tree */
449 static void add_defaults(struct kobj_engine *parent)
451 static const struct attribute *files[] = {
453 &stop_timeout_def.attr,
454 #if CONFIG_DRM_I915_HEARTBEAT_INTERVAL
455 &heartbeat_interval_def.attr,
459 struct kobj_engine *ke;
461 ke = kzalloc(sizeof(*ke), GFP_KERNEL);
465 kobject_init(&ke->base, &kobj_engine_type);
466 ke->engine = parent->engine;
468 if (kobject_add(&ke->base, &parent->base, "%s", ".defaults")) {
469 kobject_put(&ke->base);
473 if (sysfs_create_files(&ke->base, files))
476 if (intel_engine_has_timeslices(ke->engine) &&
477 sysfs_create_file(&ke->base, ×lice_duration_def.attr))
480 if (intel_engine_has_preempt_reset(ke->engine) &&
481 sysfs_create_file(&ke->base, &preempt_timeout_def.attr))
485 void intel_engines_add_sysfs(struct drm_i915_private *i915)
487 static const struct attribute *files[] = {
495 &stop_timeout_attr.attr,
496 #if CONFIG_DRM_I915_HEARTBEAT_INTERVAL
497 &heartbeat_interval_attr.attr,
502 struct device *kdev = i915->drm.primary->kdev;
503 struct intel_engine_cs *engine;
506 dir = kobject_create_and_add("engine", &kdev->kobj);
510 for_each_uabi_engine(engine, i915) {
511 struct kobject *kobj;
513 kobj = kobj_engine(dir, engine);
517 if (sysfs_create_files(kobj, files))
520 if (intel_engine_has_timeslices(engine) &&
521 sysfs_create_file(kobj, ×lice_duration_attr.attr))
524 if (intel_engine_has_preempt_reset(engine) &&
525 sysfs_create_file(kobj, &preempt_timeout_attr.attr))
528 add_defaults(container_of(kobj, struct kobj_engine, base));
534 dev_err(kdev, "Failed to add sysfs engine '%s'\n",