2 * Copyright (C) 2013 Red Hat
3 * Author: Rob Clark <robdclark@gmail.com>
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 struct drm_info_node
*node
;
49 #define SAMPLE_TIME (HZ/4)
51 /* wait for next sample time: */
52 static int wait_sample(struct msm_perf_state
*perf
)
54 unsigned long start_jiffies
= jiffies
;
56 if (time_after(perf
->next_jiffies
, start_jiffies
)) {
57 unsigned long remaining_jiffies
=
58 perf
->next_jiffies
- start_jiffies
;
59 int ret
= schedule_timeout_interruptible(remaining_jiffies
);
65 perf
->next_jiffies
+= SAMPLE_TIME
;
69 static int refill_buf(struct msm_perf_state
*perf
)
71 struct msm_drm_private
*priv
= perf
->dev
->dev_private
;
72 struct msm_gpu
*gpu
= priv
->gpu
;
73 char *ptr
= perf
->buf
;
74 int rem
= sizeof(perf
->buf
);
77 if ((perf
->cnt
++ % 32) == 0) {
79 n
= snprintf(ptr
, rem
, "%%BUSY");
83 for (i
= 0; i
< gpu
->num_perfcntrs
; i
++) {
84 const struct msm_gpu_perfcntr
*perfcntr
= &gpu
->perfcntrs
[i
];
85 n
= snprintf(ptr
, rem
, "\t%s", perfcntr
->name
);
91 uint32_t activetime
= 0, totaltime
= 0;
96 /* sleep until next sample time: */
97 ret
= wait_sample(perf
);
101 ret
= msm_gpu_perfcntr_sample(gpu
, &activetime
, &totaltime
,
102 ARRAY_SIZE(cntrs
), cntrs
);
106 val
= totaltime
? 1000 * activetime
/ totaltime
: 0;
107 n
= snprintf(ptr
, rem
, "%3d.%d%%", val
/ 10, val
% 10);
111 for (i
= 0; i
< ret
; i
++) {
112 /* cycle counters (I think).. convert to MHz.. */
113 val
= cntrs
[i
] / 10000;
114 n
= snprintf(ptr
, rem
, "\t%5d.%02d",
115 val
/ 100, val
% 100);
121 n
= snprintf(ptr
, rem
, "\n");
126 perf
->buftot
= ptr
- perf
->buf
;
131 static ssize_t
perf_read(struct file
*file
, char __user
*buf
,
132 size_t sz
, loff_t
*ppos
)
134 struct msm_perf_state
*perf
= file
->private_data
;
137 mutex_lock(&perf
->read_lock
);
139 if (perf
->bufpos
>= perf
->buftot
) {
140 ret
= refill_buf(perf
);
145 n
= min((int)sz
, perf
->buftot
- perf
->bufpos
);
146 ret
= copy_to_user(buf
, &perf
->buf
[perf
->bufpos
], n
);
154 mutex_unlock(&perf
->read_lock
);
160 static int perf_open(struct inode
*inode
, struct file
*file
)
162 struct msm_perf_state
*perf
= inode
->i_private
;
163 struct drm_device
*dev
= perf
->dev
;
164 struct msm_drm_private
*priv
= dev
->dev_private
;
165 struct msm_gpu
*gpu
= priv
->gpu
;
168 mutex_lock(&dev
->struct_mutex
);
170 if (perf
->open
|| !gpu
) {
175 file
->private_data
= perf
;
180 msm_gpu_perfcntr_start(gpu
);
181 perf
->next_jiffies
= jiffies
+ SAMPLE_TIME
;
184 mutex_unlock(&dev
->struct_mutex
);
188 static int perf_release(struct inode
*inode
, struct file
*file
)
190 struct msm_perf_state
*perf
= inode
->i_private
;
191 struct msm_drm_private
*priv
= perf
->dev
->dev_private
;
192 msm_gpu_perfcntr_stop(priv
->gpu
);
198 static const struct file_operations perf_debugfs_fops
= {
199 .owner
= THIS_MODULE
,
203 .release
= perf_release
,
206 int msm_perf_debugfs_init(struct drm_minor
*minor
)
208 struct msm_drm_private
*priv
= minor
->dev
->dev_private
;
209 struct msm_perf_state
*perf
;
211 /* only create on first minor: */
215 perf
= kzalloc(sizeof(*perf
), GFP_KERNEL
);
219 perf
->dev
= minor
->dev
;
221 mutex_init(&perf
->read_lock
);
224 perf
->node
= kzalloc(sizeof(*perf
->node
), GFP_KERNEL
);
228 perf
->ent
= debugfs_create_file("perf", S_IFREG
| S_IRUGO
,
229 minor
->debugfs_root
, perf
, &perf_debugfs_fops
);
231 DRM_ERROR("Cannot create /sys/kernel/debug/dri/%s/perf\n",
232 minor
->debugfs_root
->d_name
.name
);
236 perf
->node
->minor
= minor
;
237 perf
->node
->dent
= perf
->ent
;
238 perf
->node
->info_ent
= NULL
;
240 mutex_lock(&minor
->debugfs_lock
);
241 list_add(&perf
->node
->list
, &minor
->debugfs_list
);
242 mutex_unlock(&minor
->debugfs_lock
);
247 msm_perf_debugfs_cleanup(minor
);
251 void msm_perf_debugfs_cleanup(struct drm_minor
*minor
)
253 struct msm_drm_private
*priv
= minor
->dev
->dev_private
;
254 struct msm_perf_state
*perf
= priv
->perf
;
261 debugfs_remove(perf
->ent
);
264 mutex_lock(&minor
->debugfs_lock
);
265 list_del(&perf
->node
->list
);
266 mutex_unlock(&minor
->debugfs_lock
);
270 mutex_destroy(&perf
->read_lock
);