1 /*
2 *
3 * (C) COPYRIGHT 2012-2017 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 */
kbasep_mem_profile_seq_show(struct seq_file * sfile,void * data)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 */
kbasep_mem_profile_debugfs_open(struct inode * in,struct file * file)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
kbasep_mem_profile_debugfs_insert(struct kbase_context * kctx,char * data,size_t size)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 } else {
88 kfree(data);
89 }
90
91 dev_dbg(kctx->kbdev->dev, "returning: %d, initialised: %d",
92 err, kbase_ctx_flag(kctx, KCTX_MEM_PROFILE_INITIALIZED));
93
94 mutex_unlock(&kctx->mem_profile_lock);
95
96 return err;
97 }
98
kbasep_mem_profile_debugfs_remove(struct kbase_context * kctx)99 void kbasep_mem_profile_debugfs_remove(struct kbase_context *kctx)
100 {
101 mutex_lock(&kctx->mem_profile_lock);
102
103 dev_dbg(kctx->kbdev->dev, "initialised: %d",
104 kbase_ctx_flag(kctx, KCTX_MEM_PROFILE_INITIALIZED));
105
106 kfree(kctx->mem_profile_data);
107 kctx->mem_profile_data = NULL;
108 kctx->mem_profile_size = 0;
109
110 mutex_unlock(&kctx->mem_profile_lock);
111 }
112
113 #else /* CONFIG_DEBUG_FS */
114
kbasep_mem_profile_debugfs_insert(struct kbase_context * kctx,char * data,size_t size)115 int kbasep_mem_profile_debugfs_insert(struct kbase_context *kctx, char *data,
116 size_t size)
117 {
118 kfree(data);
119 return 0;
120 }
121 #endif /* CONFIG_DEBUG_FS */
122