@@ -10,8 +10,13 @@
#include <drm/drm_print.h>
+#include <uapi/drm/i915_drm.h>
+
#include "i915_drv.h"
#include "i915_drm_client.h"
+#include "gem/i915_gem_context.h"
+#include "gt/intel_engine_user.h"
+#include "i915_drv.h"
#include "i915_gem.h"
#include "i915_utils.h"
@@ -47,13 +52,46 @@ show_client_pid(struct device *kdev, struct device_attribute *attr, char *buf)
return ret;
}
+static ssize_t
+show_client_busy(struct device *kdev, struct device_attribute *attr, char *buf)
+{
+ struct i915_engine_busy_attribute *i915_attr =
+ container_of(attr, typeof(*i915_attr), attr);
+ struct i915_drm_client_stats *cstats =
+ &i915_attr->client->stats[i915_attr->engine_class];
+ unsigned int seq;
+ u64 total;
+
+ if (i915_attr->no_busy_stats)
+ return -ENODEV;
+
+ do {
+ seq = read_seqbegin(&cstats->lock);
+ total = cstats->busy +
+ ktime_to_ns(ktime_sub(ktime_get(), cstats->start)) *
+ cstats->active;
+ } while (read_seqretry(&cstats->lock, seq));
+
+ return snprintf(buf, PAGE_SIZE, "%llu\n", total);
+}
+
+static const char *uabi_class_names[] = {
+ [I915_ENGINE_CLASS_RENDER] = "0",
+ [I915_ENGINE_CLASS_COPY] = "1",
+ [I915_ENGINE_CLASS_VIDEO] = "2",
+ [I915_ENGINE_CLASS_VIDEO_ENHANCE] = "3",
+};
+
static int
__i915_drm_client_register(struct i915_drm_client *client,
struct task_struct *task)
{
struct i915_drm_clients *clients = client->clients;
+ struct drm_i915_private *i915 =
+ container_of(clients, typeof(*i915), clients);
+ struct intel_engine_cs *engine;
struct device_attribute *attr;
- int ret = -ENOMEM;
+ int i, ret = -ENOMEM;
char idstr[32];
char *name;
@@ -92,8 +130,70 @@ __i915_drm_client_register(struct i915_drm_client *client,
if (ret)
goto err_attr;
+ if (i915->caps.scheduler & I915_SCHEDULER_CAP_ENGINE_BUSY_STATS) {
+ client->busy_root =
+ kobject_create_and_add("busy", client->root);
+ if (!client->busy_root)
+ goto err_attr;
+
+ for (i = 0; i < ARRAY_SIZE(uabi_class_names); i++) {
+ struct i915_engine_busy_attribute *i915_attr =
+ &client->attr.busy[i];
+
+ if (!intel_engine_lookup_user(i915, i, 0))
+ continue;
+
+ i915_attr->client = client;
+ i915_attr->engine_class = i;
+
+ attr = &i915_attr->attr;
+
+ sysfs_attr_init(&attr->attr);
+
+ attr->attr.name = uabi_class_names[i];
+ attr->attr.mode = 0444;
+ attr->show = show_client_busy;
+
+ ret = sysfs_create_file(client->busy_root,
+ (struct attribute *)attr);
+ if (ret)
+ goto err_busy;
+ }
+
+ /* Enable busy stats on all engines. */
+ i = 0;
+ for_each_uabi_engine(engine, i915) {
+ ret = intel_enable_engine_stats(engine);
+ if (ret) {
+ int j, k;
+
+ /* Unwind if not available. */
+ j = 0;
+ for_each_uabi_engine(engine, i915) {
+ if (j++ == i)
+ break;
+
+ intel_disable_engine_stats(engine);
+ }
+
+ for (k = 0;
+ k < ARRAY_SIZE(uabi_class_names);
+ k++)
+ client->attr.busy[k].no_busy_stats = true;
+
+ dev_notice_once(i915->drm.dev,
+ "Engine busy stats not available! (%d)",
+ ret);
+ break;
+ }
+ i++;
+ }
+ }
+
return 0;
+err_busy:
+ kobject_put(client->busy_root);
err_attr:
kobject_put(client->root);
err_client:
@@ -113,6 +213,16 @@ __i915_drm_client_unregister(struct i915_drm_client *client)
if (!client->root)
return; /* fbdev client or error during drm open */
+ if (client->busy_root && !client->attr.busy[0].no_busy_stats) {
+ struct drm_i915_private *i915 =
+ container_of(client->clients, typeof(*i915), clients);
+ struct intel_engine_cs *engine;
+
+ for_each_uabi_engine(engine, i915)
+ intel_disable_engine_stats(engine);
+ }
+
+ kobject_put(fetch_and_zero(&client->busy_root));
kobject_put(fetch_and_zero(&client->root));
}
@@ -24,6 +24,15 @@ struct i915_drm_clients {
struct kobject *root;
};
+struct i915_drm_client;
+
+struct i915_engine_busy_attribute {
+ struct device_attribute attr;
+ struct i915_drm_client *client;
+ unsigned int engine_class;
+ bool no_busy_stats;
+};
+
struct i915_drm_client {
struct kref kref;
@@ -46,9 +55,11 @@ struct i915_drm_client {
struct i915_drm_clients *clients;
struct kobject *root;
+ struct kobject *busy_root;
struct {
struct device_attribute pid;
struct device_attribute name;
+ struct i915_engine_busy_attribute busy[MAX_ENGINE_CLASS];
} attr;
};