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 
20 void i915_drm_clients_init(struct i915_drm_clients *clients,
21 			   struct drm_i915_private *i915)
22 {
23 	clients->i915 = i915;
24 	clients->next_id = 0;
25 
26 	xa_init_flags(&clients->xarray, XA_FLAGS_ALLOC | XA_FLAGS_LOCK_IRQ);
27 }
28 
29 struct i915_drm_client *i915_drm_client_add(struct i915_drm_clients *clients)
30 {
31 	struct i915_drm_client *client;
32 	struct xarray *xa = &clients->xarray;
33 	int ret;
34 
35 	client = kzalloc(sizeof(*client), GFP_KERNEL);
36 	if (!client)
37 		return ERR_PTR(-ENOMEM);
38 
39 	xa_lock_irq(xa);
40 	ret = __xa_alloc_cyclic(xa, &client->id, client, xa_limit_32b,
41 				&clients->next_id, GFP_KERNEL);
42 	xa_unlock_irq(xa);
43 	if (ret < 0)
44 		goto err;
45 
46 	kref_init(&client->kref);
47 	spin_lock_init(&client->ctx_lock);
48 	INIT_LIST_HEAD(&client->ctx_list);
49 	client->clients = clients;
50 
51 	return client;
52 
53 err:
54 	kfree(client);
55 
56 	return ERR_PTR(ret);
57 }
58 
59 void __i915_drm_client_free(struct kref *kref)
60 {
61 	struct i915_drm_client *client =
62 		container_of(kref, typeof(*client), kref);
63 	struct xarray *xa = &client->clients->xarray;
64 	unsigned long flags;
65 
66 	xa_lock_irqsave(xa, flags);
67 	__xa_erase(xa, client->id);
68 	xa_unlock_irqrestore(xa, flags);
69 	kfree(client);
70 }
71 
72 void i915_drm_clients_fini(struct i915_drm_clients *clients)
73 {
74 	GEM_BUG_ON(!xa_empty(&clients->xarray));
75 	xa_destroy(&clients->xarray);
76 }
77 
78 #ifdef CONFIG_PROC_FS
79 static const char * const uabi_class_names[] = {
80 	[I915_ENGINE_CLASS_RENDER] = "render",
81 	[I915_ENGINE_CLASS_COPY] = "copy",
82 	[I915_ENGINE_CLASS_VIDEO] = "video",
83 	[I915_ENGINE_CLASS_VIDEO_ENHANCE] = "video-enhance",
84 };
85 
86 static u64 busy_add(struct i915_gem_context *ctx, unsigned int class)
87 {
88 	struct i915_gem_engines_iter it;
89 	struct intel_context *ce;
90 	u64 total = 0;
91 
92 	for_each_gem_engine(ce, rcu_dereference(ctx->engines), it) {
93 		if (ce->engine->uabi_class != class)
94 			continue;
95 
96 		total += intel_context_get_total_runtime_ns(ce);
97 	}
98 
99 	return total;
100 }
101 
102 static void
103 show_client_class(struct seq_file *m,
104 		  struct i915_drm_client *client,
105 		  unsigned int class)
106 {
107 	const struct list_head *list = &client->ctx_list;
108 	u64 total = atomic64_read(&client->past_runtime[class]);
109 	const unsigned int capacity =
110 		client->clients->i915->engine_uabi_class_count[class];
111 	struct i915_gem_context *ctx;
112 
113 	rcu_read_lock();
114 	list_for_each_entry_rcu(ctx, list, client_link)
115 		total += busy_add(ctx, class);
116 	rcu_read_unlock();
117 
118 	seq_printf(m, "drm-engine-%s:\t%llu ns\n",
119 		   uabi_class_names[class], total);
120 
121 	if (capacity > 1)
122 		seq_printf(m, "drm-engine-capacity-%s:\t%u\n",
123 			   uabi_class_names[class],
124 			   capacity);
125 }
126 
127 void i915_drm_client_fdinfo(struct seq_file *m, struct file *f)
128 {
129 	struct drm_file *file = f->private_data;
130 	struct drm_i915_file_private *file_priv = file->driver_priv;
131 	struct drm_i915_private *i915 = file_priv->dev_priv;
132 	struct i915_drm_client *client = file_priv->client;
133 	struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
134 	unsigned int i;
135 
136 	/*
137 	 * ******************************************************************
138 	 * For text output format description please see drm-usage-stats.rst!
139 	 * ******************************************************************
140 	 */
141 
142 	seq_printf(m, "drm-driver:\t%s\n", i915->drm.driver->name);
143 	seq_printf(m, "drm-pdev:\t%04x:%02x:%02x.%d\n",
144 		   pci_domain_nr(pdev->bus), pdev->bus->number,
145 		   PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
146 	seq_printf(m, "drm-client-id:\t%u\n", client->id);
147 
148 	/*
149 	 * Temporarily skip showing client engine information with GuC submission till
150 	 * fetching engine busyness is implemented in the GuC submission backend
151 	 */
152 	if (GRAPHICS_VER(i915) < 8 || intel_uc_uses_guc_submission(&i915->gt0.uc))
153 		return;
154 
155 	for (i = 0; i < ARRAY_SIZE(uabi_class_names); i++)
156 		show_client_class(m, client, i);
157 }
158 #endif
159