1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2013 Red Hat
7 /* For profiling, userspace can:
9 * tail -f /sys/kernel/debug/dri/<minor>/gpu
11 * This will enable performance counters/profiling to track the busy time
12 * and any gpu specific performance counters that are supported.
15 #ifdef CONFIG_DEBUG_FS
17 #include <linux/debugfs.h>
22 struct msm_perf_state {
23 struct drm_device *dev;
27 struct mutex read_lock;
32 unsigned long next_jiffies;
35 #define SAMPLE_TIME (HZ/4)
37 /* wait for next sample time: */
38 static int wait_sample(struct msm_perf_state *perf)
40 unsigned long start_jiffies = jiffies;
42 if (time_after(perf->next_jiffies, start_jiffies)) {
43 unsigned long remaining_jiffies =
44 perf->next_jiffies - start_jiffies;
45 int ret = schedule_timeout_interruptible(remaining_jiffies);
51 perf->next_jiffies += SAMPLE_TIME;
55 static int refill_buf(struct msm_perf_state *perf)
57 struct msm_drm_private *priv = perf->dev->dev_private;
58 struct msm_gpu *gpu = priv->gpu;
59 char *ptr = perf->buf;
60 int rem = sizeof(perf->buf);
63 if ((perf->cnt++ % 32) == 0) {
65 n = snprintf(ptr, rem, "%%BUSY");
69 for (i = 0; i < gpu->num_perfcntrs; i++) {
70 const struct msm_gpu_perfcntr *perfcntr = &gpu->perfcntrs[i];
71 n = snprintf(ptr, rem, "\t%s", perfcntr->name);
77 uint32_t activetime = 0, totaltime = 0;
82 /* sleep until next sample time: */
83 ret = wait_sample(perf);
87 ret = msm_gpu_perfcntr_sample(gpu, &activetime, &totaltime,
88 ARRAY_SIZE(cntrs), cntrs);
92 val = totaltime ? 1000 * activetime / totaltime : 0;
93 n = snprintf(ptr, rem, "%3d.%d%%", val / 10, val % 10);
97 for (i = 0; i < ret; i++) {
98 /* cycle counters (I think).. convert to MHz.. */
99 val = cntrs[i] / 10000;
100 n = snprintf(ptr, rem, "\t%5d.%02d",
101 val / 100, val % 100);
107 n = snprintf(ptr, rem, "\n");
112 perf->buftot = ptr - perf->buf;
117 static ssize_t perf_read(struct file *file, char __user *buf,
118 size_t sz, loff_t *ppos)
120 struct msm_perf_state *perf = file->private_data;
123 mutex_lock(&perf->read_lock);
125 if (perf->bufpos >= perf->buftot) {
126 ret = refill_buf(perf);
131 n = min((int)sz, perf->buftot - perf->bufpos);
132 if (copy_to_user(buf, &perf->buf[perf->bufpos], n)) {
141 mutex_unlock(&perf->read_lock);
147 static int perf_open(struct inode *inode, struct file *file)
149 struct msm_perf_state *perf = inode->i_private;
150 struct drm_device *dev = perf->dev;
151 struct msm_drm_private *priv = dev->dev_private;
152 struct msm_gpu *gpu = priv->gpu;
155 mutex_lock(&dev->struct_mutex);
157 if (perf->open || !gpu) {
162 file->private_data = perf;
167 msm_gpu_perfcntr_start(gpu);
168 perf->next_jiffies = jiffies + SAMPLE_TIME;
171 mutex_unlock(&dev->struct_mutex);
175 static int perf_release(struct inode *inode, struct file *file)
177 struct msm_perf_state *perf = inode->i_private;
178 struct msm_drm_private *priv = perf->dev->dev_private;
179 msm_gpu_perfcntr_stop(priv->gpu);
185 static const struct file_operations perf_debugfs_fops = {
186 .owner = THIS_MODULE,
190 .release = perf_release,
193 int msm_perf_debugfs_init(struct drm_minor *minor)
195 struct msm_drm_private *priv = minor->dev->dev_private;
196 struct msm_perf_state *perf;
199 /* only create on first minor: */
203 perf = kzalloc(sizeof(*perf), GFP_KERNEL);
207 perf->dev = minor->dev;
209 mutex_init(&perf->read_lock);
212 ent = debugfs_create_file("perf", S_IFREG | S_IRUGO,
213 minor->debugfs_root, perf, &perf_debugfs_fops);
215 DRM_ERROR("Cannot create /sys/kernel/debug/dri/%pd/perf\n",
216 minor->debugfs_root);
223 msm_perf_debugfs_cleanup(priv);
227 void msm_perf_debugfs_cleanup(struct msm_drm_private *priv)
229 struct msm_perf_state *perf = priv->perf;
236 mutex_destroy(&perf->read_lock);