Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2013 Red Hat
0004  * Author: Rob Clark <robdclark@gmail.com>
0005  */
0006 
0007 /* For profiling, userspace can:
0008  *
0009  *   tail -f /sys/kernel/debug/dri/<minor>/gpu
0010  *
0011  * This will enable performance counters/profiling to track the busy time
0012  * and any gpu specific performance counters that are supported.
0013  */
0014 
0015 #ifdef CONFIG_DEBUG_FS
0016 
0017 #include <linux/debugfs.h>
0018 #include <linux/uaccess.h>
0019 
0020 #include <drm/drm_file.h>
0021 
0022 #include "msm_drv.h"
0023 #include "msm_gpu.h"
0024 
0025 struct msm_perf_state {
0026     struct drm_device *dev;
0027 
0028     bool open;
0029     int cnt;
0030     struct mutex read_lock;
0031 
0032     char buf[256];
0033     int buftot, bufpos;
0034 
0035     unsigned long next_jiffies;
0036 };
0037 
0038 #define SAMPLE_TIME (HZ/4)
0039 
0040 /* wait for next sample time: */
0041 static int wait_sample(struct msm_perf_state *perf)
0042 {
0043     unsigned long start_jiffies = jiffies;
0044 
0045     if (time_after(perf->next_jiffies, start_jiffies)) {
0046         unsigned long remaining_jiffies =
0047             perf->next_jiffies - start_jiffies;
0048         int ret = schedule_timeout_interruptible(remaining_jiffies);
0049         if (ret > 0) {
0050             /* interrupted */
0051             return -ERESTARTSYS;
0052         }
0053     }
0054     perf->next_jiffies += SAMPLE_TIME;
0055     return 0;
0056 }
0057 
0058 static int refill_buf(struct msm_perf_state *perf)
0059 {
0060     struct msm_drm_private *priv = perf->dev->dev_private;
0061     struct msm_gpu *gpu = priv->gpu;
0062     char *ptr = perf->buf;
0063     int rem = sizeof(perf->buf);
0064     int i, n;
0065 
0066     if ((perf->cnt++ % 32) == 0) {
0067         /* Header line: */
0068         n = snprintf(ptr, rem, "%%BUSY");
0069         ptr += n;
0070         rem -= n;
0071 
0072         for (i = 0; i < gpu->num_perfcntrs; i++) {
0073             const struct msm_gpu_perfcntr *perfcntr = &gpu->perfcntrs[i];
0074             n = snprintf(ptr, rem, "\t%s", perfcntr->name);
0075             ptr += n;
0076             rem -= n;
0077         }
0078     } else {
0079         /* Sample line: */
0080         uint32_t activetime = 0, totaltime = 0;
0081         uint32_t cntrs[5];
0082         uint32_t val;
0083         int ret;
0084 
0085         /* sleep until next sample time: */
0086         ret = wait_sample(perf);
0087         if (ret)
0088             return ret;
0089 
0090         ret = msm_gpu_perfcntr_sample(gpu, &activetime, &totaltime,
0091                 ARRAY_SIZE(cntrs), cntrs);
0092         if (ret < 0)
0093             return ret;
0094 
0095         val = totaltime ? 1000 * activetime / totaltime : 0;
0096         n = snprintf(ptr, rem, "%3d.%d%%", val / 10, val % 10);
0097         ptr += n;
0098         rem -= n;
0099 
0100         for (i = 0; i < ret; i++) {
0101             /* cycle counters (I think).. convert to MHz.. */
0102             val = cntrs[i] / 10000;
0103             n = snprintf(ptr, rem, "\t%5d.%02d",
0104                     val / 100, val % 100);
0105             ptr += n;
0106             rem -= n;
0107         }
0108     }
0109 
0110     n = snprintf(ptr, rem, "\n");
0111     ptr += n;
0112     rem -= n;
0113 
0114     perf->bufpos = 0;
0115     perf->buftot = ptr - perf->buf;
0116 
0117     return 0;
0118 }
0119 
0120 static ssize_t perf_read(struct file *file, char __user *buf,
0121         size_t sz, loff_t *ppos)
0122 {
0123     struct msm_perf_state *perf = file->private_data;
0124     int n = 0, ret = 0;
0125 
0126     mutex_lock(&perf->read_lock);
0127 
0128     if (perf->bufpos >= perf->buftot) {
0129         ret = refill_buf(perf);
0130         if (ret)
0131             goto out;
0132     }
0133 
0134     n = min((int)sz, perf->buftot - perf->bufpos);
0135     if (copy_to_user(buf, &perf->buf[perf->bufpos], n)) {
0136         ret = -EFAULT;
0137         goto out;
0138     }
0139 
0140     perf->bufpos += n;
0141     *ppos += n;
0142 
0143 out:
0144     mutex_unlock(&perf->read_lock);
0145     if (ret)
0146         return ret;
0147     return n;
0148 }
0149 
0150 static int perf_open(struct inode *inode, struct file *file)
0151 {
0152     struct msm_perf_state *perf = inode->i_private;
0153     struct drm_device *dev = perf->dev;
0154     struct msm_drm_private *priv = dev->dev_private;
0155     struct msm_gpu *gpu = priv->gpu;
0156     int ret = 0;
0157 
0158     if (!gpu)
0159         return -ENODEV;
0160 
0161     mutex_lock(&gpu->lock);
0162 
0163     if (perf->open) {
0164         ret = -EBUSY;
0165         goto out;
0166     }
0167 
0168     file->private_data = perf;
0169     perf->open = true;
0170     perf->cnt = 0;
0171     perf->buftot = 0;
0172     perf->bufpos = 0;
0173     msm_gpu_perfcntr_start(gpu);
0174     perf->next_jiffies = jiffies + SAMPLE_TIME;
0175 
0176 out:
0177     mutex_unlock(&gpu->lock);
0178     return ret;
0179 }
0180 
0181 static int perf_release(struct inode *inode, struct file *file)
0182 {
0183     struct msm_perf_state *perf = inode->i_private;
0184     struct msm_drm_private *priv = perf->dev->dev_private;
0185     msm_gpu_perfcntr_stop(priv->gpu);
0186     perf->open = false;
0187     return 0;
0188 }
0189 
0190 
0191 static const struct file_operations perf_debugfs_fops = {
0192     .owner = THIS_MODULE,
0193     .open = perf_open,
0194     .read = perf_read,
0195     .llseek = no_llseek,
0196     .release = perf_release,
0197 };
0198 
0199 int msm_perf_debugfs_init(struct drm_minor *minor)
0200 {
0201     struct msm_drm_private *priv = minor->dev->dev_private;
0202     struct msm_perf_state *perf;
0203 
0204     /* only create on first minor: */
0205     if (priv->perf)
0206         return 0;
0207 
0208     perf = kzalloc(sizeof(*perf), GFP_KERNEL);
0209     if (!perf)
0210         return -ENOMEM;
0211 
0212     perf->dev = minor->dev;
0213 
0214     mutex_init(&perf->read_lock);
0215     priv->perf = perf;
0216 
0217     debugfs_create_file("perf", S_IFREG | S_IRUGO, minor->debugfs_root,
0218                 perf, &perf_debugfs_fops);
0219     return 0;
0220 }
0221 
0222 void msm_perf_debugfs_cleanup(struct msm_drm_private *priv)
0223 {
0224     struct msm_perf_state *perf = priv->perf;
0225 
0226     if (!perf)
0227         return;
0228 
0229     priv->perf = NULL;
0230 
0231     mutex_destroy(&perf->read_lock);
0232 
0233     kfree(perf);
0234 }
0235 
0236 #endif