hc
2024-05-16 8d2a02b24d66aa359e83eebc1ed3c0f85367a1cb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note
/*
 *
 * (C) COPYRIGHT 2022 ARM Limited. All rights reserved.
 *
 * This program is free software and is provided to you under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation, and any use by you of this program is subject to the terms
 * of such GNU license.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you can access it online at
 * http://www.gnu.org/licenses/gpl-2.0.html.
 *
 */
 
#include <mali_kbase.h>
 
#if IS_ENABLED(CONFIG_DEBUG_FS)
 
/**
 * kbasep_fault_occurred - Check if fault occurred.
 *
 * @kbdev:  Device pointer
 *
 * Return: true if a fault occurred.
 */
static bool kbasep_fault_occurred(struct kbase_device *kbdev)
{
   unsigned long flags;
   bool ret;
 
   spin_lock_irqsave(&kbdev->csf.dof.lock, flags);
   ret = (kbdev->csf.dof.error_code != DF_NO_ERROR);
   spin_unlock_irqrestore(&kbdev->csf.dof.lock, flags);
 
   return ret;
}
 
void kbase_debug_csf_fault_wait_completion(struct kbase_device *kbdev)
{
   if (likely(!kbase_debug_csf_fault_dump_enabled(kbdev))) {
       dev_dbg(kbdev->dev, "No userspace client for dumping exists");
       return;
   }
 
   wait_event(kbdev->csf.dof.dump_wait_wq, kbase_debug_csf_fault_dump_complete(kbdev));
}
KBASE_EXPORT_TEST_API(kbase_debug_csf_fault_wait_completion);
 
/**
 * kbase_debug_csf_fault_wakeup - Wake up a waiting user space client.
 *
 * @kbdev:   Kbase device
 */
static void kbase_debug_csf_fault_wakeup(struct kbase_device *kbdev)
{
   wake_up_interruptible(&kbdev->csf.dof.fault_wait_wq);
}
 
bool kbase_debug_csf_fault_notify(struct kbase_device *kbdev,
   struct kbase_context *kctx, enum dumpfault_error_type error)
{
   unsigned long flags;
 
   if (likely(!kbase_debug_csf_fault_dump_enabled(kbdev)))
       return false;
 
   if (WARN_ON(error == DF_NO_ERROR))
       return false;
 
   if (kctx && kbase_ctx_flag(kctx, KCTX_DYING)) {
       dev_info(kbdev->dev, "kctx %d_%d is dying when error %d is reported",
           kctx->tgid, kctx->id, error);
       kctx = NULL;
   }
 
   spin_lock_irqsave(&kbdev->csf.dof.lock, flags);
 
   /* Only one fault at a time can be processed */
   if (kbdev->csf.dof.error_code) {
       dev_info(kbdev->dev, "skip this fault as there's a pending fault");
       goto unlock;
   }
 
   kbdev->csf.dof.kctx_tgid = kctx ? kctx->tgid : 0;
   kbdev->csf.dof.kctx_id = kctx ? kctx->id : 0;
   kbdev->csf.dof.error_code = error;
   kbase_debug_csf_fault_wakeup(kbdev);
 
unlock:
   spin_unlock_irqrestore(&kbdev->csf.dof.lock, flags);
   return true;
}
 
static ssize_t debug_csf_fault_read(struct file *file, char __user *buffer, size_t size,
                   loff_t *f_pos)
{
#define BUF_SIZE 64
   struct kbase_device *kbdev;
   unsigned long flags;
   int count;
   char buf[BUF_SIZE];
   u32 tgid, ctx_id;
   enum dumpfault_error_type error_code;
 
   if (unlikely(!file)) {
       pr_warn("%s: file is NULL", __func__);
       return -EINVAL;
   }
 
   kbdev = file->private_data;
   if (unlikely(!buffer)) {
       dev_warn(kbdev->dev, "%s: buffer is NULL", __func__);
       return -EINVAL;
   }
 
   if (unlikely(*f_pos < 0)) {
       dev_warn(kbdev->dev, "%s: f_pos is negative", __func__);
       return -EINVAL;
   }
 
   if (size < sizeof(buf)) {
       dev_warn(kbdev->dev, "%s: buffer is too small", __func__);
       return -EINVAL;
   }
 
   if (wait_event_interruptible(kbdev->csf.dof.fault_wait_wq, kbasep_fault_occurred(kbdev)))
       return -ERESTARTSYS;
 
   spin_lock_irqsave(&kbdev->csf.dof.lock, flags);
   tgid = kbdev->csf.dof.kctx_tgid;
   ctx_id = kbdev->csf.dof.kctx_id;
   error_code = kbdev->csf.dof.error_code;
   BUILD_BUG_ON(sizeof(buf) < (sizeof(tgid) + sizeof(ctx_id) + sizeof(error_code)));
   count = scnprintf(buf, sizeof(buf), "%u_%u_%u\n", tgid, ctx_id, error_code);
   spin_unlock_irqrestore(&kbdev->csf.dof.lock, flags);
 
   dev_info(kbdev->dev, "debug csf fault info read");
   return simple_read_from_buffer(buffer, size, f_pos, buf, count);
}
 
static int debug_csf_fault_open(struct inode *in, struct file *file)
{
   struct kbase_device *kbdev;
 
   if (unlikely(!in)) {
       pr_warn("%s: inode is NULL", __func__);
       return -EINVAL;
   }
 
   kbdev = in->i_private;
   if (unlikely(!file)) {
       dev_warn(kbdev->dev, "%s: file is NULL", __func__);
       return -EINVAL;
   }
 
   if (atomic_cmpxchg(&kbdev->csf.dof.enabled, 0, 1) == 1) {
       dev_warn(kbdev->dev, "Only one client is allowed for dump on fault");
       return -EBUSY;
   }
 
   dev_info(kbdev->dev, "debug csf fault file open");
 
   return simple_open(in, file);
}
 
static ssize_t debug_csf_fault_write(struct file *file, const char __user *ubuf, size_t count,
                    loff_t *ppos)
{
   struct kbase_device *kbdev;
   unsigned long flags;
 
   if (unlikely(!file)) {
       pr_warn("%s: file is NULL", __func__);
       return -EINVAL;
   }
 
   kbdev = file->private_data;
   spin_lock_irqsave(&kbdev->csf.dof.lock, flags);
   kbdev->csf.dof.error_code = DF_NO_ERROR;
   kbdev->csf.dof.kctx_tgid = 0;
   kbdev->csf.dof.kctx_id = 0;
   dev_info(kbdev->dev, "debug csf fault dump complete");
   spin_unlock_irqrestore(&kbdev->csf.dof.lock, flags);
 
   /* User space finished the dump.
    * Wake up blocked kernel threads to proceed.
    */
   wake_up(&kbdev->csf.dof.dump_wait_wq);
 
   return count;
}
 
static int debug_csf_fault_release(struct inode *in, struct file *file)
{
   struct kbase_device *kbdev;
   unsigned long flags;
 
   if (unlikely(!in)) {
       pr_warn("%s: inode is NULL", __func__);
       return -EINVAL;
   }
 
   kbdev = in->i_private;
   spin_lock_irqsave(&kbdev->csf.dof.lock, flags);
   kbdev->csf.dof.kctx_tgid = 0;
   kbdev->csf.dof.kctx_id = 0;
   kbdev->csf.dof.error_code = DF_NO_ERROR;
   spin_unlock_irqrestore(&kbdev->csf.dof.lock, flags);
 
   atomic_set(&kbdev->csf.dof.enabled, 0);
   dev_info(kbdev->dev, "debug csf fault file close");
 
   /* User space closed the debugfs file.
    * Wake up blocked kernel threads to resume.
    */
   wake_up(&kbdev->csf.dof.dump_wait_wq);
 
   return 0;
}
 
static const struct file_operations kbasep_debug_csf_fault_fops = {
   .owner = THIS_MODULE,
   .open = debug_csf_fault_open,
   .read = debug_csf_fault_read,
   .write = debug_csf_fault_write,
   .llseek = default_llseek,
   .release = debug_csf_fault_release,
};
 
void kbase_debug_csf_fault_debugfs_init(struct kbase_device *kbdev)
{
   const char *fname = "csf_fault";
 
   if (unlikely(!kbdev)) {
       pr_warn("%s: kbdev is NULL", __func__);
       return;
   }
 
   debugfs_create_file(fname, 0600, kbdev->mali_debugfs_directory, kbdev,
               &kbasep_debug_csf_fault_fops);
}
 
int kbase_debug_csf_fault_init(struct kbase_device *kbdev)
{
   if (unlikely(!kbdev)) {
       pr_warn("%s: kbdev is NULL", __func__);
       return -EINVAL;
   }
 
   init_waitqueue_head(&(kbdev->csf.dof.fault_wait_wq));
   init_waitqueue_head(&(kbdev->csf.dof.dump_wait_wq));
   spin_lock_init(&kbdev->csf.dof.lock);
   kbdev->csf.dof.kctx_tgid = 0;
   kbdev->csf.dof.kctx_id = 0;
   kbdev->csf.dof.error_code = DF_NO_ERROR;
   atomic_set(&kbdev->csf.dof.enabled, 0);
 
   return 0;
}
 
void kbase_debug_csf_fault_term(struct kbase_device *kbdev)
{
}
#endif /* CONFIG_DEBUG_FS */