Merge tag 'v4.4-rc1'
[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 /* 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 void kbasep_mem_profile_debugfs_add(struct kbase_context *kctx)
76 {
77         KBASE_DEBUG_ASSERT(kctx != NULL);
78
79         spin_lock_init(&kctx->mem_profile_lock);
80
81         debugfs_create_file("mem_profile", S_IRUGO, kctx->kctx_dentry, kctx,
82                         &kbasep_mem_profile_debugfs_fops);
83 }
84
85 void kbasep_mem_profile_debugfs_remove(struct kbase_context *kctx)
86 {
87         KBASE_DEBUG_ASSERT(kctx != NULL);
88
89         spin_lock(&kctx->mem_profile_lock);
90         kfree(kctx->mem_profile_data);
91         kctx->mem_profile_data = NULL;
92         spin_unlock(&kctx->mem_profile_lock);
93 }
94
95 #else /* CONFIG_DEBUG_FS */
96
97 /**
98  * @brief Stub function for when debugfs is disabled
99  */
100 void kbasep_mem_profile_debugfs_insert(struct kbase_context *kctx, char *data,
101                 size_t size)
102 {
103         kfree(data);
104 }
105 #endif /* CONFIG_DEBUG_FS */