2 * Copyright (C) 2013 Red Hat
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
18 /* For profiling, userspace can:
20 * tail -f /sys/kernel/debug/dri/<minor>/gpu
22 * This will enable performance counters/profiling to track the busy time
23 * and any gpu specific performance counters that are supported.
26 #ifdef CONFIG_DEBUG_FS
28 #include <linux/debugfs.h>
33 struct msm_perf_state {
34 struct drm_device *dev;
38 struct mutex read_lock;
43 unsigned long next_jiffies;
46 #define SAMPLE_TIME (HZ/4)
48 /* wait for next sample time: */
49 static int wait_sample(struct msm_perf_state *perf)
51 unsigned long start_jiffies = jiffies;
53 if (time_after(perf->next_jiffies, start_jiffies)) {
54 unsigned long remaining_jiffies =
55 perf->next_jiffies - start_jiffies;
56 int ret = schedule_timeout_interruptible(remaining_jiffies);
62 perf->next_jiffies += SAMPLE_TIME;
66 static int refill_buf(struct msm_perf_state *perf)
68 struct msm_drm_private *priv = perf->dev->dev_private;
69 struct msm_gpu *gpu = priv->gpu;
70 char *ptr = perf->buf;
71 int rem = sizeof(perf->buf);
74 if ((perf->cnt++ % 32) == 0) {
76 n = snprintf(ptr, rem, "%%BUSY");
80 for (i = 0; i < gpu->num_perfcntrs; i++) {
81 const struct msm_gpu_perfcntr *perfcntr = &gpu->perfcntrs[i];
82 n = snprintf(ptr, rem, "\t%s", perfcntr->name);
88 uint32_t activetime = 0, totaltime = 0;
93 /* sleep until next sample time: */
94 ret = wait_sample(perf);
98 ret = msm_gpu_perfcntr_sample(gpu, &activetime, &totaltime,
99 ARRAY_SIZE(cntrs), cntrs);
103 val = totaltime ? 1000 * activetime / totaltime : 0;
104 n = snprintf(ptr, rem, "%3d.%d%%", val / 10, val % 10);
108 for (i = 0; i < ret; i++) {
109 /* cycle counters (I think).. convert to MHz.. */
110 val = cntrs[i] / 10000;
111 n = snprintf(ptr, rem, "\t%5d.%02d",
112 val / 100, val % 100);
118 n = snprintf(ptr, rem, "\n");
123 perf->buftot = ptr - perf->buf;
128 static ssize_t perf_read(struct file *file, char __user *buf,
129 size_t sz, loff_t *ppos)
131 struct msm_perf_state *perf = file->private_data;
134 mutex_lock(&perf->read_lock);
136 if (perf->bufpos >= perf->buftot) {
137 ret = refill_buf(perf);
142 n = min((int)sz, perf->buftot - perf->bufpos);
143 if (copy_to_user(buf, &perf->buf[perf->bufpos], n)) {
152 mutex_unlock(&perf->read_lock);
158 static int perf_open(struct inode *inode, struct file *file)
160 struct msm_perf_state *perf = inode->i_private;
161 struct drm_device *dev = perf->dev;
162 struct msm_drm_private *priv = dev->dev_private;
163 struct msm_gpu *gpu = priv->gpu;
166 mutex_lock(&dev->struct_mutex);
168 if (perf->open || !gpu) {
173 file->private_data = perf;
178 msm_gpu_perfcntr_start(gpu);
179 perf->next_jiffies = jiffies + SAMPLE_TIME;
182 mutex_unlock(&dev->struct_mutex);
186 static int perf_release(struct inode *inode, struct file *file)
188 struct msm_perf_state *perf = inode->i_private;
189 struct msm_drm_private *priv = perf->dev->dev_private;
190 msm_gpu_perfcntr_stop(priv->gpu);
196 static const struct file_operations perf_debugfs_fops = {
197 .owner = THIS_MODULE,
201 .release = perf_release,
204 int msm_perf_debugfs_init(struct drm_minor *minor)
206 struct msm_drm_private *priv = minor->dev->dev_private;
207 struct msm_perf_state *perf;
210 /* only create on first minor: */
214 perf = kzalloc(sizeof(*perf), GFP_KERNEL);
218 perf->dev = minor->dev;
220 mutex_init(&perf->read_lock);
223 ent = debugfs_create_file("perf", S_IFREG | S_IRUGO,
224 minor->debugfs_root, perf, &perf_debugfs_fops);
226 DRM_ERROR("Cannot create /sys/kernel/debug/dri/%pd/perf\n",
227 minor->debugfs_root);
234 msm_perf_debugfs_cleanup(priv);
238 void msm_perf_debugfs_cleanup(struct msm_drm_private *priv)
240 struct msm_perf_state *perf = priv->perf;
247 mutex_destroy(&perf->read_lock);