summaryrefslogtreecommitdiffstats
path: root/sound/soc/intel/avs/debugfs.c
blob: e9042d4328c413d84e210bbcf5a0200155dda6a0 (plain)
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// SPDX-License-Identifier: GPL-2.0-only
//
// Copyright(c) 2021-2022 Intel Corporation. All rights reserved.
//
// Authors: Cezary Rojewski <cezary.rojewski@intel.com>
//          Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
//

#include <linux/debugfs.h>
#include <linux/kfifo.h>
#include <linux/wait.h>
#include <linux/sched/signal.h>
#include <sound/soc.h>
#include "avs.h"
#include "messages.h"

static unsigned int __kfifo_fromio(struct kfifo *fifo, const void __iomem *src, unsigned int len)
{
	struct __kfifo *__fifo = &fifo->kfifo;
	unsigned int l, off;

	len = min(len, kfifo_avail(fifo));
	off = __fifo->in & __fifo->mask;
	l = min(len, kfifo_size(fifo) - off);

	memcpy_fromio(__fifo->data + off, src, l);
	memcpy_fromio(__fifo->data, src + l, len - l);
	/* Make sure data copied from SRAM is visible to all CPUs. */
	smp_mb();
	__fifo->in += len;

	return len;
}

bool avs_logging_fw(struct avs_dev *adev)
{
	return kfifo_initialized(&adev->trace_fifo);
}

void avs_dump_fw_log(struct avs_dev *adev, const void __iomem *src, unsigned int len)
{
	__kfifo_fromio(&adev->trace_fifo, src, len);
}

void avs_dump_fw_log_wakeup(struct avs_dev *adev, const void __iomem *src, unsigned int len)
{
	avs_dump_fw_log(adev, src, len);
	wake_up(&adev->trace_waitq);
}

static ssize_t fw_regs_read(struct file *file, char __user *to, size_t count, loff_t *ppos)
{
	struct avs_dev *adev = file->private_data;
	char *buf;
	int ret;

	buf = kzalloc(AVS_FW_REGS_SIZE, GFP_KERNEL);
	if (!buf)
		return -ENOMEM;

	memcpy_fromio(buf, avs_sram_addr(adev, AVS_FW_REGS_WINDOW), AVS_FW_REGS_SIZE);

	ret = simple_read_from_buffer(to, count, ppos, buf, AVS_FW_REGS_SIZE);
	kfree(buf);
	return ret;
}

static const struct file_operations fw_regs_fops = {
	.open = simple_open,
	.read = fw_regs_read,
	.llseek = no_llseek,
};

static ssize_t probe_points_read(struct file *file, char __user *to, size_t count, loff_t *ppos)
{
	struct avs_dev *adev = file->private_data;
	struct avs_probe_point_desc *desc;
	size_t num_desc, len = 0;
	char *buf;
	int i, ret;

	/* Prevent chaining, send and dump IPC value just once. */
	if (*ppos)
		return 0;

	buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
	if (!buf)
		return -ENOMEM;

	ret = avs_ipc_probe_get_points(adev, &desc, &num_desc);
	if (ret) {
		ret = AVS_IPC_RET(ret);
		goto exit;
	}

	for (i = 0; i < num_desc; i++) {
		ret = snprintf(buf + len, PAGE_SIZE - len,
			       "Id: %#010x  Purpose: %d  Node id: %#x\n",
			       desc[i].id.value, desc[i].purpose, desc[i].node_id.val);
		if (ret < 0)
			goto free_desc;
		len += ret;
	}

	ret = simple_read_from_buffer(to, count, ppos, buf, len);
free_desc:
	kfree(desc);
exit:
	kfree(buf);
	return ret;
}

static ssize_t probe_points_write(struct file *file, const char __user *from, size_t count,
				  loff_t *ppos)
{
	struct avs_dev *adev = file->private_data;
	struct avs_probe_point_desc *desc;
	u32 *array, num_elems;
	size_t bytes;
	int ret;

	ret = parse_int_array_user(from, count, (int **)&array);
	if (ret < 0)
		return ret;

	num_elems = *array;
	bytes = sizeof(*array) * num_elems;
	if (bytes % sizeof(*desc)) {
		ret = -EINVAL;
		goto exit;
	}

	desc = (struct avs_probe_point_desc *)&array[1];
	ret = avs_ipc_probe_connect_points(adev, desc, bytes / sizeof(*desc));
	if (ret)
		ret = AVS_IPC_RET(ret);
	else
		ret = count;
exit:
	kfree(array);
	return ret;
}

static const struct file_operations probe_points_fops = {
	.open = simple_open,
	.read = probe_points_read,
	.write = probe_points_write,
	.llseek = no_llseek,
};

static ssize_t probe_points_disconnect_write(struct file *file, const char __user *from,
					     size_t count, loff_t *ppos)
{
	struct avs_dev *adev = file->private_data;
	union avs_probe_point_id *id;
	u32 *array, num_elems;
	size_t bytes;
	int ret;

	ret = parse_int_array_user(from, count, (int **)&array);
	if (ret < 0)
		return ret;

	num_elems = *array;
	bytes = sizeof(*array) * num_elems;
	if (bytes % sizeof(*id)) {
		ret = -EINVAL;
		goto exit;
	}

	id = (union avs_probe_point_id *)&array[1];
	ret = avs_ipc_probe_disconnect_points(adev, id, bytes / sizeof(*id));
	if (ret)
		ret = AVS_IPC_RET(ret);
	else
		ret = count;
exit:
	kfree(array);
	return ret;
}

static const struct file_operations probe_points_disconnect_fops = {
	.open = simple_open,
	.write = probe_points_disconnect_write,
	.llseek = default_llseek,
};

static ssize_t strace_read(struct file *file, char __user *to, size_t count, loff_t *ppos)
{
	struct avs_dev *adev = file->private_data;
	struct kfifo *fifo = &adev->trace_fifo;
	unsigned int copied;

	if (kfifo_is_empty(fifo)) {
		DEFINE_WAIT(wait);

		prepare_to_wait(&adev->trace_waitq, &wait, TASK_INTERRUPTIBLE);
		if (!signal_pending(current))
			schedule();
		finish_wait(&adev->trace_waitq, &wait);
	}

	if (kfifo_to_user(fifo, to, count, &copied))
		return -EFAULT;
	*ppos += copied;
	return copied;
}

static int strace_open(struct inode *inode, struct file *file)
{
	struct avs_dev *adev = inode->i_private;
	int ret;

	if (kfifo_initialized(&adev->trace_fifo))
		return -EBUSY;

	ret = kfifo_alloc(&adev->trace_fifo, PAGE_SIZE, GFP_KERNEL);
	if (ret < 0)
		return ret;

	file->private_data = adev;
	return 0;
}

static int strace_release(struct inode *inode, struct file *file)
{
	union avs_notify_msg msg = AVS_NOTIFICATION(LOG_BUFFER_STATUS);
	struct avs_dev *adev = file->private_data;
	unsigned long resource_mask;
	unsigned long flags, i;
	u32 num_cores;

	resource_mask = adev->logged_resources;
	num_cores = adev->hw_cfg.dsp_cores;

	spin_lock_irqsave(&adev->trace_lock, flags);

	/* Gather any remaining logs. */
	for_each_set_bit(i, &resource_mask, num_cores) {
		msg.log.core = i;
		avs_dsp_op(adev, log_buffer_status, &msg);
	}

	kfifo_free(&adev->trace_fifo);

	spin_unlock_irqrestore(&adev->trace_lock, flags);

	return 0;
}

static const struct file_operations strace_fops = {
	.llseek = default_llseek,
	.read = strace_read,
	.open = strace_open,
	.release = strace_release,
};

#define DISABLE_TIMERS	UINT_MAX

static int enable_logs(struct avs_dev *adev, u32 resource_mask, u32 *priorities)
{
	int ret;

	/* Logging demands D0i0 state from DSP. */
	if (!adev->logged_resources) {
		pm_runtime_get_sync(adev->dev);

		ret = avs_dsp_disable_d0ix(adev);
		if (ret)
			goto err_d0ix;
	}

	ret = avs_ipc_set_system_time(adev);
	if (ret && ret != AVS_IPC_NOT_SUPPORTED) {
		ret = AVS_IPC_RET(ret);
		goto err_ipc;
	}

	ret = avs_dsp_op(adev, enable_logs, AVS_LOG_ENABLE, adev->aging_timer_period,
			 adev->fifo_full_timer_period, resource_mask, priorities);
	if (ret)
		goto err_ipc;

	adev->logged_resources |= resource_mask;
	return 0;

err_ipc:
	if (!adev->logged_resources) {
		avs_dsp_enable_d0ix(adev);
err_d0ix:
		pm_runtime_mark_last_busy(adev->dev);
		pm_runtime_put_autosuspend(adev->dev);
	}

	return ret;
}

static int disable_logs(struct avs_dev *adev, u32 resource_mask)
{
	int ret;

	/* Check if there's anything to do. */
	if (!adev->logged_resources)
		return 0;

	ret = avs_dsp_op(adev, enable_logs, AVS_LOG_DISABLE, DISABLE_TIMERS, DISABLE_TIMERS,
			 resource_mask, NULL);

	/*
	 * If IPC fails causing recovery, logged_resources is already zero
	 * so unsetting bits is still safe.
	 */
	adev->logged_resources &= ~resource_mask;

	/* If that's the last resource, allow for D3. */
	if (!adev->logged_resources) {
		avs_dsp_enable_d0ix(adev);
		pm_runtime_mark_last_busy(adev->dev);
		pm_runtime_put_autosuspend(adev->dev);
	}

	return ret;
}

static ssize_t trace_control_read(struct file *file, char __user *to, size_t count, loff_t *ppos)
{
	struct avs_dev *adev = file->private_data;
	char buf[64];
	int len;

	len = snprintf(buf, sizeof(buf), "0x%08x\n", adev->logged_resources);

	return simple_read_from_buffer(to, count, ppos, buf, len);
}

static ssize_t trace_control_write(struct file *file, const char __user *from, size_t count,
				   loff_t *ppos)
{
	struct avs_dev *adev = file->private_data;
	u32 *array, num_elems;
	u32 resource_mask;
	int ret;

	ret = parse_int_array_user(from, count, (int **)&array);
	if (ret < 0)
		return ret;

	num_elems = *array;
	resource_mask = array[1];

	/*
	 * Disable if just resource mask is provided - no log priority flags.
	 *
	 * Enable input format:   mask, prio1, .., prioN
	 * Where 'N' equals number of bits set in the 'mask'.
	 */
	if (num_elems == 1) {
		ret = disable_logs(adev, resource_mask);
	} else {
		if (num_elems != (hweight_long(resource_mask) + 1)) {
			ret = -EINVAL;
			goto free_array;
		}

		ret = enable_logs(adev, resource_mask, &array[2]);
	}

	if (!ret)
		ret = count;
free_array:
	kfree(array);
	return ret;
}

static const struct file_operations trace_control_fops = {
	.llseek = default_llseek,
	.read = trace_control_read,
	.write = trace_control_write,
	.open = simple_open,
};

void avs_debugfs_init(struct avs_dev *adev)
{
	init_waitqueue_head(&adev->trace_waitq);
	spin_lock_init(&adev->trace_lock);

	adev->debugfs_root = debugfs_create_dir("avs", snd_soc_debugfs_root);

	/* Initialize timer periods with recommended defaults. */
	adev->aging_timer_period = 10;
	adev->fifo_full_timer_period = 10;

	debugfs_create_file("strace", 0444, adev->debugfs_root, adev, &strace_fops);
	debugfs_create_file("trace_control", 0644, adev->debugfs_root, adev, &trace_control_fops);
	debugfs_create_file("fw_regs", 0444, adev->debugfs_root, adev, &fw_regs_fops);

	debugfs_create_u32("trace_aging_period", 0644, adev->debugfs_root,
			   &adev->aging_timer_period);
	debugfs_create_u32("trace_fifo_full_period", 0644, adev->debugfs_root,
			   &adev->fifo_full_timer_period);

	debugfs_create_file("probe_points", 0644, adev->debugfs_root, adev, &probe_points_fops);
	debugfs_create_file("probe_points_disconnect", 0200, adev->debugfs_root, adev,
			    &probe_points_disconnect_fops);
}

void avs_debugfs_exit(struct avs_dev *adev)
{
	debugfs_remove_recursive(adev->debugfs_root);
}