Merge tag 'lsk-v3.10-android-15.01'
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / arm / midgard / mali_kbase_mem_profile_debugfs.c
1 /*
2  *
3  * (C) COPYRIGHT 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 /* mam_profile file name max length 22 based on format <int>_<int>\0 */
23 #define KBASEP_DEBUGFS_FNAME_SIZE_MAX (10+1+10+1)
24
25 void kbasep_mem_profile_debugfs_insert(struct kbase_context *kctx, char *data,
26                 size_t size)
27 {
28         spin_lock(&kctx->mem_profile_lock);
29         kfree(kctx->mem_profile_data);
30         kctx->mem_profile_data = data;
31         kctx->mem_profile_size = size;
32         spin_unlock(&kctx->mem_profile_lock);
33 }
34
35 /** Show callback for the @c mem_profile debugfs file.
36  *
37  * This function is called to get the contents of the @c mem_profile debugfs
38  * file. This is a report of current memory usage and distribution in userspace.
39  *
40  * @param sfile The debugfs entry
41  * @param data Data associated with the entry
42  *
43  * @return 0 if successfully prints data in debugfs entry file
44  *         -1 if it encountered an error
45  */
46 static int kbasep_mem_profile_seq_show(struct seq_file *sfile, void *data)
47 {
48         struct kbase_context *kctx = sfile->private;
49
50         KBASE_DEBUG_ASSERT(kctx != NULL);
51
52         spin_lock(&kctx->mem_profile_lock);
53         seq_write(sfile, kctx->mem_profile_data, kctx->mem_profile_size);
54         seq_putc(sfile, '\n');
55         spin_unlock(&kctx->mem_profile_lock);
56
57         return 0;
58 }
59
60 /*
61  *  File operations related to debugfs entry for mem_profile
62  */
63 STATIC int kbasep_mem_profile_debugfs_open(struct inode *in, struct file *file)
64 {
65         return single_open(file, kbasep_mem_profile_seq_show, in->i_private);
66 }
67
68 static const struct file_operations kbasep_mem_profile_debugfs_fops = {
69         .open = kbasep_mem_profile_debugfs_open,
70         .read = seq_read,
71         .llseek = seq_lseek,
72         .release = single_release,
73 };
74
75 mali_error kbasep_mem_profile_debugfs_add(struct kbase_context *kctx)
76 {
77         char name[KBASEP_DEBUGFS_FNAME_SIZE_MAX];
78
79         KBASE_DEBUG_ASSERT(kctx != NULL);
80
81         spin_lock_init(&kctx->mem_profile_lock);
82
83         scnprintf(name, KBASEP_DEBUGFS_FNAME_SIZE_MAX, "%d_%d", kctx->pid,
84                         kctx->id);
85
86         kctx->mem_dentry = debugfs_create_file(name, S_IRUGO,
87                         kctx->kbdev->memory_profile_directory,
88                         kctx, &kbasep_mem_profile_debugfs_fops);
89         if (IS_ERR(kctx->mem_dentry))
90                 goto error_out;
91
92         return MALI_ERROR_NONE;
93
94 error_out:
95         return MALI_ERROR_FUNCTION_FAILED;
96 }
97
98 void kbasep_mem_profile_debugfs_remove(struct kbase_context *kctx)
99 {
100         KBASE_DEBUG_ASSERT(kctx != NULL);
101
102         spin_lock(&kctx->mem_profile_lock);
103         kfree(kctx->mem_profile_data);
104         kctx->mem_profile_data = NULL;
105         spin_unlock(&kctx->mem_profile_lock);
106
107         if (IS_ERR(kctx->mem_dentry))
108                 return;
109         debugfs_remove(kctx->mem_dentry);
110 }
111
112 #else /* CONFIG_DEBUG_FS */
113
114 /**
115  * @brief Stub function for when debugfs is disabled
116  */
117 mali_error kbasep_mem_profile_debugfs_add(struct kbase_context *ctx)
118 {
119         return MALI_ERROR_NONE;
120 }
121
122 /**
123  * @brief Stub function for when debugfs is disabled
124  */
125 void kbasep_mem_profile_debugfs_remove(struct kbase_context *ctx)
126 {
127 }
128
129 /**
130  * @brief Stub function for when debugfs is disabled
131  */
132 void kbasep_mem_profile_debugfs_insert(struct kbase_context *kctx, char *data,
133                 size_t size)
134 {
135         kfree(data);
136 }
137 #endif /* CONFIG_DEBUG_FS */