Merge branch 'android-4.4' of https://android.googlesource.com/kernel/common
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / arm / midgard / mali_kbase_mem_profile_debugfs.c
1 /*
2  *
3  * (C) COPYRIGHT 2012-2015 ARM Limited. All rights reserved.
4  *
5  * This program is free software and is provided to you under the terms of the
6  * GNU General Public License version 2 as published by the Free Software
7  * Foundation, and any use by you of this program is subject to the terms
8  * of such GNU licence.
9  *
10  * A copy of the licence is included with the program, and can also be obtained
11  * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
12  * Boston, MA  02110-1301, USA.
13  *
14  */
15
16
17
18 #include <mali_kbase_gpu_memory_debugfs.h>
19
20 #ifdef CONFIG_DEBUG_FS
21
22 /** Show callback for the @c mem_profile debugfs file.
23  *
24  * This function is called to get the contents of the @c mem_profile debugfs
25  * file. This is a report of current memory usage and distribution in userspace.
26  *
27  * @param sfile The debugfs entry
28  * @param data Data associated with the entry
29  *
30  * @return 0 if it successfully prints data in debugfs entry file, non-zero otherwise
31  */
32 static int kbasep_mem_profile_seq_show(struct seq_file *sfile, void *data)
33 {
34         struct kbase_context *kctx = sfile->private;
35         int err = 0;
36
37         mutex_lock(&kctx->mem_profile_lock);
38
39         err = seq_write(sfile, kctx->mem_profile_data, kctx->mem_profile_size);
40
41         if (!err)
42                 seq_putc(sfile, '\n');
43
44         mutex_unlock(&kctx->mem_profile_lock);
45
46         return err;
47 }
48
49 /*
50  *  File operations related to debugfs entry for mem_profile
51  */
52 static int kbasep_mem_profile_debugfs_open(struct inode *in, struct file *file)
53 {
54         return single_open(file, kbasep_mem_profile_seq_show, in->i_private);
55 }
56
57 static const struct file_operations kbasep_mem_profile_debugfs_fops = {
58         .open = kbasep_mem_profile_debugfs_open,
59         .read = seq_read,
60         .llseek = seq_lseek,
61         .release = single_release,
62 };
63
64 int kbasep_mem_profile_debugfs_insert(struct kbase_context *kctx, char *data,
65                                         size_t size)
66 {
67         int err = 0;
68
69         mutex_lock(&kctx->mem_profile_lock);
70
71         dev_dbg(kctx->kbdev->dev, "initialised: %d",
72                                 kctx->mem_profile_initialized);
73
74         if (!kctx->mem_profile_initialized) {
75                 if (!debugfs_create_file("mem_profile", S_IRUGO,
76                                         kctx->kctx_dentry, kctx,
77                                         &kbasep_mem_profile_debugfs_fops)) {
78                         err = -EAGAIN;
79                 } else {
80                         kctx->mem_profile_initialized = true;
81                 }
82         }
83
84         if (kctx->mem_profile_initialized) {
85                 kfree(kctx->mem_profile_data);
86                 kctx->mem_profile_data = data;
87                 kctx->mem_profile_size = size;
88         }
89
90         dev_dbg(kctx->kbdev->dev, "returning: %d, initialised: %d",
91                                 err, kctx->mem_profile_initialized);
92
93         mutex_unlock(&kctx->mem_profile_lock);
94
95         return err;
96 }
97
98 void kbasep_mem_profile_debugfs_remove(struct kbase_context *kctx)
99 {
100         mutex_lock(&kctx->mem_profile_lock);
101
102         dev_dbg(kctx->kbdev->dev, "initialised: %d",
103                                 kctx->mem_profile_initialized);
104
105         kfree(kctx->mem_profile_data);
106         kctx->mem_profile_data = NULL;
107         kctx->mem_profile_size = 0;
108
109         mutex_unlock(&kctx->mem_profile_lock);
110 }
111
112 #else /* CONFIG_DEBUG_FS */
113
114 int kbasep_mem_profile_debugfs_insert(struct kbase_context *kctx, char *data,
115                                         size_t size)
116 {
117         kfree(data);
118         return 0;
119 }
120 #endif /* CONFIG_DEBUG_FS */