MALI: rockchip: upgrade midgard DDK to r14p0-01rel0
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / arm / midgard / mali_kbase_mem_profile_debugfs.c
1 /*
2  *
3  * (C) COPYRIGHT 2012-2016 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.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
36         mutex_lock(&kctx->mem_profile_lock);
37
38         seq_write(sfile, kctx->mem_profile_data, kctx->mem_profile_size);
39
40         seq_putc(sfile, '\n');
41
42         mutex_unlock(&kctx->mem_profile_lock);
43
44         return 0;
45 }
46
47 /*
48  *  File operations related to debugfs entry for mem_profile
49  */
50 static int kbasep_mem_profile_debugfs_open(struct inode *in, struct file *file)
51 {
52         return single_open(file, kbasep_mem_profile_seq_show, in->i_private);
53 }
54
55 static const struct file_operations kbasep_mem_profile_debugfs_fops = {
56         .open = kbasep_mem_profile_debugfs_open,
57         .read = seq_read,
58         .llseek = seq_lseek,
59         .release = single_release,
60 };
61
62 int kbasep_mem_profile_debugfs_insert(struct kbase_context *kctx, char *data,
63                                         size_t size)
64 {
65         int err = 0;
66
67         mutex_lock(&kctx->mem_profile_lock);
68
69         dev_dbg(kctx->kbdev->dev, "initialised: %d",
70                 kbase_ctx_flag(kctx, KCTX_MEM_PROFILE_INITIALIZED));
71
72         if (!kbase_ctx_flag(kctx, KCTX_MEM_PROFILE_INITIALIZED)) {
73                 if (!debugfs_create_file("mem_profile", S_IRUGO,
74                                         kctx->kctx_dentry, kctx,
75                                         &kbasep_mem_profile_debugfs_fops)) {
76                         err = -EAGAIN;
77                 } else {
78                         kbase_ctx_flag_set(kctx,
79                                            KCTX_MEM_PROFILE_INITIALIZED);
80                 }
81         }
82
83         if (kbase_ctx_flag(kctx, KCTX_MEM_PROFILE_INITIALIZED)) {
84                 kfree(kctx->mem_profile_data);
85                 kctx->mem_profile_data = data;
86                 kctx->mem_profile_size = size;
87         }
88
89         dev_dbg(kctx->kbdev->dev, "returning: %d, initialised: %d",
90                 err, kbase_ctx_flag(kctx, KCTX_MEM_PROFILE_INITIALIZED));
91
92         mutex_unlock(&kctx->mem_profile_lock);
93
94         return err;
95 }
96
97 void kbasep_mem_profile_debugfs_remove(struct kbase_context *kctx)
98 {
99         mutex_lock(&kctx->mem_profile_lock);
100
101         dev_dbg(kctx->kbdev->dev, "initialised: %d",
102                                 kbase_ctx_flag(kctx, KCTX_MEM_PROFILE_INITIALIZED));
103
104         kfree(kctx->mem_profile_data);
105         kctx->mem_profile_data = NULL;
106         kctx->mem_profile_size = 0;
107
108         mutex_unlock(&kctx->mem_profile_lock);
109 }
110
111 #else /* CONFIG_DEBUG_FS */
112
113 int kbasep_mem_profile_debugfs_insert(struct kbase_context *kctx, char *data,
114                                         size_t size)
115 {
116         kfree(data);
117         return 0;
118 }
119 #endif /* CONFIG_DEBUG_FS */