1 // SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note
2 /*
3 *
4 * (C) COPYRIGHT 2022-2023 ARM Limited. All rights reserved.
5 *
6 * This program is free software and is provided to you under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation, and any use by you of this program is subject to the terms
9 * of such GNU license.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you can access it online at
18 * http://www.gnu.org/licenses/gpl-2.0.html.
19 *
20 */
21
22 /*
23 * Debugfs interface to dump information about GPU allocations in kctx
24 */
25
26 #include "mali_kbase_debug_mem_allocs.h"
27 #include "mali_kbase.h"
28
29 #include <linux/string.h>
30 #include <linux/list.h>
31 #include <linux/file.h>
32
33 #if IS_ENABLED(CONFIG_DEBUG_FS)
34
35 /**
36 * debug_zone_mem_allocs_show - Show information from specific rbtree
37 * @zone: Name of GPU virtual memory zone
38 * @rbtree: Pointer to the root of the rbtree associated with @zone
39 * @sfile: The debugfs entry
40 *
41 * This function is called to show information about all the GPU allocations of a
42 * a particular zone within GPU virtual memory space of a context.
43 * The information like the start virtual address and size (in bytes) is shown for
44 * every GPU allocation mapped in the zone.
45 */
debug_zone_mem_allocs_show(char * zone,struct rb_root * rbtree,struct seq_file * sfile)46 static void debug_zone_mem_allocs_show(char *zone, struct rb_root *rbtree, struct seq_file *sfile)
47 {
48 struct rb_node *p;
49 struct kbase_va_region *reg;
50 const char *type_names[5] = {
51 "Native",
52 "Imported UMM",
53 "Imported user buf",
54 "Alias",
55 "Raw"
56 };
57
58 #define MEM_ALLOCS_HEADER \
59 " VA, VA size, Commit size, Flags, Mem type\n"
60 seq_printf(sfile, "Zone name: %s\n:", zone);
61 seq_printf(sfile, MEM_ALLOCS_HEADER);
62 for (p = rb_first(rbtree); p; p = rb_next(p)) {
63 reg = rb_entry(p, struct kbase_va_region, rblink);
64 if (!(reg->flags & KBASE_REG_FREE)) {
65 seq_printf(sfile, "%16llx, %16zx, %16zx, %8lx, %s\n",
66 reg->start_pfn << PAGE_SHIFT, reg->nr_pages << PAGE_SHIFT,
67 kbase_reg_current_backed_size(reg) << PAGE_SHIFT,
68 reg->flags, type_names[reg->gpu_alloc->type]);
69 }
70 }
71 }
72
73 /**
74 * debug_ctx_mem_allocs_show - Show information about GPU allocations in a kctx
75 * @sfile: The debugfs entry
76 * @data: Data associated with the entry
77 *
78 * Return:
79 * 0 if successfully prints data in debugfs entry file
80 * -1 if it encountered an error
81 */
debug_ctx_mem_allocs_show(struct seq_file * sfile,void * data)82 static int debug_ctx_mem_allocs_show(struct seq_file *sfile, void *data)
83 {
84 struct kbase_context *const kctx = sfile->private;
85
86 kbase_gpu_vm_lock(kctx);
87
88 debug_zone_mem_allocs_show("SAME_VA:", &kctx->reg_rbtree_same, sfile);
89 debug_zone_mem_allocs_show("CUSTOM_VA:", &kctx->reg_rbtree_custom, sfile);
90 debug_zone_mem_allocs_show("EXEC_VA:", &kctx->reg_rbtree_exec, sfile);
91
92 #if MALI_USE_CSF
93 debug_zone_mem_allocs_show("EXEC_VA_FIXED:", &kctx->reg_rbtree_exec_fixed, sfile);
94 debug_zone_mem_allocs_show("FIXED_VA:", &kctx->reg_rbtree_fixed, sfile);
95 #endif /* MALI_USE_CSF */
96
97 kbase_gpu_vm_unlock(kctx);
98 return 0;
99 }
100
101 /*
102 * File operations related to debugfs entry for mem_zones
103 */
debug_mem_allocs_open(struct inode * in,struct file * file)104 static int debug_mem_allocs_open(struct inode *in, struct file *file)
105 {
106 return single_open(file, debug_ctx_mem_allocs_show, in->i_private);
107 }
108
109 static const struct file_operations kbase_debug_mem_allocs_fops = {
110 .owner = THIS_MODULE,
111 .open = debug_mem_allocs_open,
112 .read = seq_read,
113 .llseek = seq_lseek,
114 .release = single_release,
115 };
116
117 /*
118 * Initialize debugfs entry for mem_allocs
119 */
kbase_debug_mem_allocs_init(struct kbase_context * const kctx)120 void kbase_debug_mem_allocs_init(struct kbase_context *const kctx)
121 {
122 /* Caller already ensures this, but we keep the pattern for
123 * maintenance safety.
124 */
125 if (WARN_ON(!kctx) || WARN_ON(IS_ERR_OR_NULL(kctx->kctx_dentry)))
126 return;
127
128 debugfs_create_file("mem_allocs", 0400, kctx->kctx_dentry, kctx,
129 &kbase_debug_mem_allocs_fops);
130 }
131 #else
132 /*
133 * Stub functions for when debugfs is disabled
134 */
kbase_debug_mem_allocs_init(struct kbase_context * const kctx)135 void kbase_debug_mem_allocs_init(struct kbase_context *const kctx)
136 {
137 }
138 #endif
139