xref: /openbmc/linux/sound/soc/sof/ipc3-dtrace.c (revision 404e077a)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2022 Intel Corporation. All rights reserved.
4 //
5 // Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
6 
7 #include <linux/debugfs.h>
8 #include <linux/sched/signal.h>
9 #include "sof-priv.h"
10 #include "sof-audio.h"
11 #include "ops.h"
12 #include "sof-utils.h"
13 #include "ipc3-priv.h"
14 
15 #define TRACE_FILTER_ELEMENTS_PER_ENTRY 4
16 #define TRACE_FILTER_MAX_CONFIG_STRING_LENGTH 1024
17 
18 enum sof_dtrace_state {
19 	SOF_DTRACE_DISABLED,
20 	SOF_DTRACE_STOPPED,
21 	SOF_DTRACE_INITIALIZING,
22 	SOF_DTRACE_ENABLED,
23 };
24 
25 struct sof_dtrace_priv {
26 	struct snd_dma_buffer dmatb;
27 	struct snd_dma_buffer dmatp;
28 	int dma_trace_pages;
29 	wait_queue_head_t trace_sleep;
30 	u32 host_offset;
31 	bool dtrace_error;
32 	bool dtrace_draining;
33 	enum sof_dtrace_state dtrace_state;
34 };
35 
36 static bool trace_pos_update_expected(struct sof_dtrace_priv *priv)
37 {
38 	if (priv->dtrace_state == SOF_DTRACE_ENABLED ||
39 	    priv->dtrace_state == SOF_DTRACE_INITIALIZING)
40 		return true;
41 
42 	return false;
43 }
44 
45 static int trace_filter_append_elem(struct snd_sof_dev *sdev, u32 key, u32 value,
46 				    struct sof_ipc_trace_filter_elem *elem_list,
47 				    int capacity, int *counter)
48 {
49 	if (*counter >= capacity)
50 		return -ENOMEM;
51 
52 	elem_list[*counter].key = key;
53 	elem_list[*counter].value = value;
54 	++*counter;
55 
56 	return 0;
57 }
58 
59 static int trace_filter_parse_entry(struct snd_sof_dev *sdev, const char *line,
60 				    struct sof_ipc_trace_filter_elem *elem,
61 				    int capacity, int *counter)
62 {
63 	int log_level, pipe_id, comp_id, read, ret;
64 	int len = strlen(line);
65 	int cnt = *counter;
66 	u32 uuid_id;
67 
68 	/* ignore empty content */
69 	ret = sscanf(line, " %n", &read);
70 	if (!ret && read == len)
71 		return len;
72 
73 	ret = sscanf(line, " %d %x %d %d %n", &log_level, &uuid_id, &pipe_id, &comp_id, &read);
74 	if (ret != TRACE_FILTER_ELEMENTS_PER_ENTRY || read != len) {
75 		dev_err(sdev->dev, "Invalid trace filter entry '%s'\n", line);
76 		return -EINVAL;
77 	}
78 
79 	if (uuid_id > 0) {
80 		ret = trace_filter_append_elem(sdev, SOF_IPC_TRACE_FILTER_ELEM_BY_UUID,
81 					       uuid_id, elem, capacity, &cnt);
82 		if (ret)
83 			return ret;
84 	}
85 	if (pipe_id >= 0) {
86 		ret = trace_filter_append_elem(sdev, SOF_IPC_TRACE_FILTER_ELEM_BY_PIPE,
87 					       pipe_id, elem, capacity, &cnt);
88 		if (ret)
89 			return ret;
90 	}
91 	if (comp_id >= 0) {
92 		ret = trace_filter_append_elem(sdev, SOF_IPC_TRACE_FILTER_ELEM_BY_COMP,
93 					       comp_id, elem, capacity, &cnt);
94 		if (ret)
95 			return ret;
96 	}
97 
98 	ret = trace_filter_append_elem(sdev, SOF_IPC_TRACE_FILTER_ELEM_SET_LEVEL |
99 				       SOF_IPC_TRACE_FILTER_ELEM_FIN,
100 				       log_level, elem, capacity, &cnt);
101 	if (ret)
102 		return ret;
103 
104 	/* update counter only when parsing whole entry passed */
105 	*counter = cnt;
106 
107 	return len;
108 }
109 
110 static int trace_filter_parse(struct snd_sof_dev *sdev, char *string,
111 			      int *out_elem_cnt,
112 			      struct sof_ipc_trace_filter_elem **out)
113 {
114 	static const char entry_delimiter[] = ";";
115 	char *entry = string;
116 	int capacity = 0;
117 	int entry_len;
118 	int cnt = 0;
119 
120 	/*
121 	 * Each entry contains at least 1, up to TRACE_FILTER_ELEMENTS_PER_ENTRY
122 	 * IPC elements, depending on content. Calculate IPC elements capacity
123 	 * for the input string where each element is set.
124 	 */
125 	while (entry) {
126 		capacity += TRACE_FILTER_ELEMENTS_PER_ENTRY;
127 		entry = strchr(entry + 1, entry_delimiter[0]);
128 	}
129 	*out = kmalloc(capacity * sizeof(**out), GFP_KERNEL);
130 	if (!*out)
131 		return -ENOMEM;
132 
133 	/* split input string by ';', and parse each entry separately in trace_filter_parse_entry */
134 	while ((entry = strsep(&string, entry_delimiter))) {
135 		entry_len = trace_filter_parse_entry(sdev, entry, *out, capacity, &cnt);
136 		if (entry_len < 0) {
137 			dev_err(sdev->dev,
138 				"Parsing filter entry '%s' failed with %d\n",
139 				entry, entry_len);
140 			return -EINVAL;
141 		}
142 	}
143 
144 	*out_elem_cnt = cnt;
145 
146 	return 0;
147 }
148 
149 static int ipc3_trace_update_filter(struct snd_sof_dev *sdev, int num_elems,
150 				    struct sof_ipc_trace_filter_elem *elems)
151 {
152 	struct sof_ipc_trace_filter *msg;
153 	size_t size;
154 	int ret;
155 
156 	size = struct_size(msg, elems, num_elems);
157 	if (size > SOF_IPC_MSG_MAX_SIZE)
158 		return -EINVAL;
159 
160 	msg = kmalloc(size, GFP_KERNEL);
161 	if (!msg)
162 		return -ENOMEM;
163 
164 	msg->hdr.size = size;
165 	msg->hdr.cmd = SOF_IPC_GLB_TRACE_MSG | SOF_IPC_TRACE_FILTER_UPDATE;
166 	msg->elem_cnt = num_elems;
167 	memcpy(&msg->elems[0], elems, num_elems * sizeof(*elems));
168 
169 	ret = pm_runtime_resume_and_get(sdev->dev);
170 	if (ret < 0 && ret != -EACCES) {
171 		dev_err(sdev->dev, "enabling device failed: %d\n", ret);
172 		goto error;
173 	}
174 	ret = sof_ipc_tx_message_no_reply(sdev->ipc, msg, msg->hdr.size);
175 	pm_runtime_mark_last_busy(sdev->dev);
176 	pm_runtime_put_autosuspend(sdev->dev);
177 
178 error:
179 	kfree(msg);
180 	return ret;
181 }
182 
183 static ssize_t dfsentry_trace_filter_write(struct file *file, const char __user *from,
184 					   size_t count, loff_t *ppos)
185 {
186 	struct snd_sof_dfsentry *dfse = file->private_data;
187 	struct sof_ipc_trace_filter_elem *elems = NULL;
188 	struct snd_sof_dev *sdev = dfse->sdev;
189 	loff_t pos = 0;
190 	int num_elems;
191 	char *string;
192 	int ret;
193 
194 	if (count > TRACE_FILTER_MAX_CONFIG_STRING_LENGTH) {
195 		dev_err(sdev->dev, "%s too long input, %zu > %d\n", __func__, count,
196 			TRACE_FILTER_MAX_CONFIG_STRING_LENGTH);
197 		return -EINVAL;
198 	}
199 
200 	string = kmalloc(count + 1, GFP_KERNEL);
201 	if (!string)
202 		return -ENOMEM;
203 
204 	/* assert null termination */
205 	string[count] = 0;
206 	ret = simple_write_to_buffer(string, count, &pos, from, count);
207 	if (ret < 0)
208 		goto error;
209 
210 	ret = trace_filter_parse(sdev, string, &num_elems, &elems);
211 	if (ret < 0)
212 		goto error;
213 
214 	if (num_elems) {
215 		ret = ipc3_trace_update_filter(sdev, num_elems, elems);
216 		if (ret < 0) {
217 			dev_err(sdev->dev, "Filter update failed: %d\n", ret);
218 			goto error;
219 		}
220 	}
221 	ret = count;
222 error:
223 	kfree(string);
224 	kfree(elems);
225 	return ret;
226 }
227 
228 static const struct file_operations sof_dfs_trace_filter_fops = {
229 	.open = simple_open,
230 	.write = dfsentry_trace_filter_write,
231 	.llseek = default_llseek,
232 };
233 
234 static int debugfs_create_trace_filter(struct snd_sof_dev *sdev)
235 {
236 	struct snd_sof_dfsentry *dfse;
237 
238 	dfse = devm_kzalloc(sdev->dev, sizeof(*dfse), GFP_KERNEL);
239 	if (!dfse)
240 		return -ENOMEM;
241 
242 	dfse->sdev = sdev;
243 	dfse->type = SOF_DFSENTRY_TYPE_BUF;
244 
245 	debugfs_create_file("filter", 0200, sdev->debugfs_root, dfse,
246 			    &sof_dfs_trace_filter_fops);
247 	/* add to dfsentry list */
248 	list_add(&dfse->list, &sdev->dfsentry_list);
249 
250 	return 0;
251 }
252 
253 static bool sof_dtrace_set_host_offset(struct sof_dtrace_priv *priv, u32 new_offset)
254 {
255 	u32 host_offset = READ_ONCE(priv->host_offset);
256 
257 	if (host_offset != new_offset) {
258 		/* This is a bit paranoid and unlikely that it is needed */
259 		u32 ret = cmpxchg(&priv->host_offset, host_offset, new_offset);
260 
261 		if (ret == host_offset)
262 			return true;
263 	}
264 
265 	return false;
266 }
267 
268 static size_t sof_dtrace_avail(struct snd_sof_dev *sdev,
269 			       loff_t pos, size_t buffer_size)
270 {
271 	struct sof_dtrace_priv *priv = sdev->fw_trace_data;
272 	loff_t host_offset = READ_ONCE(priv->host_offset);
273 
274 	/*
275 	 * If host offset is less than local pos, it means write pointer of
276 	 * host DMA buffer has been wrapped. We should output the trace data
277 	 * at the end of host DMA buffer at first.
278 	 */
279 	if (host_offset < pos)
280 		return buffer_size - pos;
281 
282 	/* If there is available trace data now, it is unnecessary to wait. */
283 	if (host_offset > pos)
284 		return host_offset - pos;
285 
286 	return 0;
287 }
288 
289 static size_t sof_wait_dtrace_avail(struct snd_sof_dev *sdev, loff_t pos,
290 				    size_t buffer_size)
291 {
292 	size_t ret = sof_dtrace_avail(sdev, pos, buffer_size);
293 	struct sof_dtrace_priv *priv = sdev->fw_trace_data;
294 	wait_queue_entry_t wait;
295 
296 	/* data immediately available */
297 	if (ret)
298 		return ret;
299 
300 	if (priv->dtrace_draining && !trace_pos_update_expected(priv)) {
301 		/*
302 		 * tracing has ended and all traces have been
303 		 * read by client, return EOF
304 		 */
305 		priv->dtrace_draining = false;
306 		return 0;
307 	}
308 
309 	/* wait for available trace data from FW */
310 	init_waitqueue_entry(&wait, current);
311 	set_current_state(TASK_INTERRUPTIBLE);
312 	add_wait_queue(&priv->trace_sleep, &wait);
313 
314 	if (!signal_pending(current)) {
315 		/* set timeout to max value, no error code */
316 		schedule_timeout(MAX_SCHEDULE_TIMEOUT);
317 	}
318 	remove_wait_queue(&priv->trace_sleep, &wait);
319 
320 	return sof_dtrace_avail(sdev, pos, buffer_size);
321 }
322 
323 static ssize_t dfsentry_dtrace_read(struct file *file, char __user *buffer,
324 				    size_t count, loff_t *ppos)
325 {
326 	struct snd_sof_dfsentry *dfse = file->private_data;
327 	struct snd_sof_dev *sdev = dfse->sdev;
328 	struct sof_dtrace_priv *priv = sdev->fw_trace_data;
329 	unsigned long rem;
330 	loff_t lpos = *ppos;
331 	size_t avail, buffer_size = dfse->size;
332 	u64 lpos_64;
333 
334 	/* make sure we know about any failures on the DSP side */
335 	priv->dtrace_error = false;
336 
337 	/* check pos and count */
338 	if (lpos < 0)
339 		return -EINVAL;
340 	if (!count)
341 		return 0;
342 
343 	/* check for buffer wrap and count overflow */
344 	lpos_64 = lpos;
345 	lpos = do_div(lpos_64, buffer_size);
346 
347 	/* get available count based on current host offset */
348 	avail = sof_wait_dtrace_avail(sdev, lpos, buffer_size);
349 	if (priv->dtrace_error) {
350 		dev_err(sdev->dev, "trace IO error\n");
351 		return -EIO;
352 	}
353 
354 	/* no new trace data */
355 	if (!avail)
356 		return 0;
357 
358 	/* make sure count is <= avail */
359 	if (count > avail)
360 		count = avail;
361 
362 	/*
363 	 * make sure that all trace data is available for the CPU as the trace
364 	 * data buffer might be allocated from non consistent memory.
365 	 * Note: snd_dma_buffer_sync() is called for normal audio playback and
366 	 *	 capture streams also.
367 	 */
368 	snd_dma_buffer_sync(&priv->dmatb, SNDRV_DMA_SYNC_CPU);
369 	/* copy available trace data to debugfs */
370 	rem = copy_to_user(buffer, ((u8 *)(dfse->buf) + lpos), count);
371 	if (rem)
372 		return -EFAULT;
373 
374 	*ppos += count;
375 
376 	/* move debugfs reading position */
377 	return count;
378 }
379 
380 static int dfsentry_dtrace_release(struct inode *inode, struct file *file)
381 {
382 	struct snd_sof_dfsentry *dfse = inode->i_private;
383 	struct snd_sof_dev *sdev = dfse->sdev;
384 	struct sof_dtrace_priv *priv = sdev->fw_trace_data;
385 
386 	/* avoid duplicate traces at next open */
387 	if (priv->dtrace_state != SOF_DTRACE_ENABLED)
388 		sof_dtrace_set_host_offset(priv, 0);
389 
390 	return 0;
391 }
392 
393 static const struct file_operations sof_dfs_dtrace_fops = {
394 	.open = simple_open,
395 	.read = dfsentry_dtrace_read,
396 	.llseek = default_llseek,
397 	.release = dfsentry_dtrace_release,
398 };
399 
400 static int debugfs_create_dtrace(struct snd_sof_dev *sdev)
401 {
402 	struct sof_dtrace_priv *priv;
403 	struct snd_sof_dfsentry *dfse;
404 	int ret;
405 
406 	if (!sdev)
407 		return -EINVAL;
408 
409 	priv = sdev->fw_trace_data;
410 
411 	ret = debugfs_create_trace_filter(sdev);
412 	if (ret < 0)
413 		dev_warn(sdev->dev, "failed to create filter debugfs file: %d", ret);
414 
415 	dfse = devm_kzalloc(sdev->dev, sizeof(*dfse), GFP_KERNEL);
416 	if (!dfse)
417 		return -ENOMEM;
418 
419 	dfse->type = SOF_DFSENTRY_TYPE_BUF;
420 	dfse->buf = priv->dmatb.area;
421 	dfse->size = priv->dmatb.bytes;
422 	dfse->sdev = sdev;
423 
424 	debugfs_create_file("trace", 0444, sdev->debugfs_root, dfse,
425 			    &sof_dfs_dtrace_fops);
426 
427 	return 0;
428 }
429 
430 static int ipc3_dtrace_enable(struct snd_sof_dev *sdev)
431 {
432 	struct sof_dtrace_priv *priv = sdev->fw_trace_data;
433 	struct sof_ipc_fw_ready *ready = &sdev->fw_ready;
434 	struct sof_ipc_fw_version *v = &ready->version;
435 	struct sof_ipc_dma_trace_params_ext params;
436 	int ret;
437 
438 	if (!sdev->fw_trace_is_supported)
439 		return 0;
440 
441 	if (priv->dtrace_state == SOF_DTRACE_ENABLED || !priv->dma_trace_pages)
442 		return -EINVAL;
443 
444 	if (priv->dtrace_state == SOF_DTRACE_STOPPED)
445 		goto start;
446 
447 	/* set IPC parameters */
448 	params.hdr.cmd = SOF_IPC_GLB_TRACE_MSG;
449 	/* PARAMS_EXT is only supported from ABI 3.7.0 onwards */
450 	if (v->abi_version >= SOF_ABI_VER(3, 7, 0)) {
451 		params.hdr.size = sizeof(struct sof_ipc_dma_trace_params_ext);
452 		params.hdr.cmd |= SOF_IPC_TRACE_DMA_PARAMS_EXT;
453 		params.timestamp_ns = ktime_get(); /* in nanosecond */
454 	} else {
455 		params.hdr.size = sizeof(struct sof_ipc_dma_trace_params);
456 		params.hdr.cmd |= SOF_IPC_TRACE_DMA_PARAMS;
457 	}
458 	params.buffer.phy_addr = priv->dmatp.addr;
459 	params.buffer.size = priv->dmatb.bytes;
460 	params.buffer.pages = priv->dma_trace_pages;
461 	params.stream_tag = 0;
462 
463 	sof_dtrace_set_host_offset(priv, 0);
464 	priv->dtrace_draining = false;
465 
466 	ret = sof_dtrace_host_init(sdev, &priv->dmatb, &params);
467 	if (ret < 0) {
468 		dev_err(sdev->dev, "Host dtrace init failed: %d\n", ret);
469 		return ret;
470 	}
471 	dev_dbg(sdev->dev, "stream_tag: %d\n", params.stream_tag);
472 
473 	/* send IPC to the DSP */
474 	priv->dtrace_state = SOF_DTRACE_INITIALIZING;
475 	ret = sof_ipc_tx_message_no_reply(sdev->ipc, &params, sizeof(params));
476 	if (ret < 0) {
477 		dev_err(sdev->dev, "can't set params for DMA for trace %d\n", ret);
478 		goto trace_release;
479 	}
480 
481 start:
482 	priv->dtrace_state = SOF_DTRACE_ENABLED;
483 
484 	ret = sof_dtrace_host_trigger(sdev, SNDRV_PCM_TRIGGER_START);
485 	if (ret < 0) {
486 		dev_err(sdev->dev, "Host dtrace trigger start failed: %d\n", ret);
487 		goto trace_release;
488 	}
489 
490 	return 0;
491 
492 trace_release:
493 	priv->dtrace_state = SOF_DTRACE_DISABLED;
494 	sof_dtrace_host_release(sdev);
495 	return ret;
496 }
497 
498 static int ipc3_dtrace_init(struct snd_sof_dev *sdev)
499 {
500 	struct sof_dtrace_priv *priv;
501 	int ret;
502 
503 	/* dtrace is only supported with SOF_IPC */
504 	if (sdev->pdata->ipc_type != SOF_IPC)
505 		return -EOPNOTSUPP;
506 
507 	if (sdev->fw_trace_data) {
508 		dev_err(sdev->dev, "fw_trace_data has been already allocated\n");
509 		return -EBUSY;
510 	}
511 
512 	priv = devm_kzalloc(sdev->dev, sizeof(*priv), GFP_KERNEL);
513 	if (!priv)
514 		return -ENOMEM;
515 
516 	sdev->fw_trace_data = priv;
517 
518 	/* set false before start initialization */
519 	priv->dtrace_state = SOF_DTRACE_DISABLED;
520 
521 	/* allocate trace page table buffer */
522 	ret = snd_dma_alloc_pages(SNDRV_DMA_TYPE_DEV, sdev->dev,
523 				  PAGE_SIZE, &priv->dmatp);
524 	if (ret < 0) {
525 		dev_err(sdev->dev, "can't alloc page table for trace %d\n", ret);
526 		return ret;
527 	}
528 
529 	/* allocate trace data buffer */
530 	ret = snd_dma_alloc_dir_pages(SNDRV_DMA_TYPE_DEV_SG, sdev->dev,
531 				      DMA_FROM_DEVICE, DMA_BUF_SIZE_FOR_TRACE,
532 				      &priv->dmatb);
533 	if (ret < 0) {
534 		dev_err(sdev->dev, "can't alloc buffer for trace %d\n", ret);
535 		goto page_err;
536 	}
537 
538 	/* create compressed page table for audio firmware */
539 	ret = snd_sof_create_page_table(sdev->dev, &priv->dmatb,
540 					priv->dmatp.area, priv->dmatb.bytes);
541 	if (ret < 0)
542 		goto table_err;
543 
544 	priv->dma_trace_pages = ret;
545 	dev_dbg(sdev->dev, "dma_trace_pages: %d\n", priv->dma_trace_pages);
546 
547 	if (sdev->first_boot) {
548 		ret = debugfs_create_dtrace(sdev);
549 		if (ret < 0)
550 			goto table_err;
551 	}
552 
553 	init_waitqueue_head(&priv->trace_sleep);
554 
555 	ret = ipc3_dtrace_enable(sdev);
556 	if (ret < 0)
557 		goto table_err;
558 
559 	return 0;
560 table_err:
561 	priv->dma_trace_pages = 0;
562 	snd_dma_free_pages(&priv->dmatb);
563 page_err:
564 	snd_dma_free_pages(&priv->dmatp);
565 	return ret;
566 }
567 
568 int ipc3_dtrace_posn_update(struct snd_sof_dev *sdev,
569 			    struct sof_ipc_dma_trace_posn *posn)
570 {
571 	struct sof_dtrace_priv *priv = sdev->fw_trace_data;
572 
573 	if (!sdev->fw_trace_is_supported)
574 		return 0;
575 
576 	if (trace_pos_update_expected(priv) &&
577 	    sof_dtrace_set_host_offset(priv, posn->host_offset))
578 		wake_up(&priv->trace_sleep);
579 
580 	if (posn->overflow != 0)
581 		dev_err(sdev->dev,
582 			"DSP trace buffer overflow %u bytes. Total messages %d\n",
583 			posn->overflow, posn->messages);
584 
585 	return 0;
586 }
587 
588 /* an error has occurred within the DSP that prevents further trace */
589 static void ipc3_dtrace_fw_crashed(struct snd_sof_dev *sdev)
590 {
591 	struct sof_dtrace_priv *priv = sdev->fw_trace_data;
592 
593 	if (priv->dtrace_state == SOF_DTRACE_ENABLED) {
594 		priv->dtrace_error = true;
595 		wake_up(&priv->trace_sleep);
596 	}
597 }
598 
599 static void ipc3_dtrace_release(struct snd_sof_dev *sdev, bool only_stop)
600 {
601 	struct sof_dtrace_priv *priv = sdev->fw_trace_data;
602 	struct sof_ipc_fw_ready *ready = &sdev->fw_ready;
603 	struct sof_ipc_fw_version *v = &ready->version;
604 	struct sof_ipc_cmd_hdr hdr;
605 	int ret;
606 
607 	if (!sdev->fw_trace_is_supported || priv->dtrace_state == SOF_DTRACE_DISABLED)
608 		return;
609 
610 	ret = sof_dtrace_host_trigger(sdev, SNDRV_PCM_TRIGGER_STOP);
611 	if (ret < 0)
612 		dev_err(sdev->dev, "Host dtrace trigger stop failed: %d\n", ret);
613 	priv->dtrace_state = SOF_DTRACE_STOPPED;
614 
615 	/*
616 	 * stop and free trace DMA in the DSP. TRACE_DMA_FREE is only supported from
617 	 * ABI 3.20.0 onwards
618 	 */
619 	if (v->abi_version >= SOF_ABI_VER(3, 20, 0)) {
620 		hdr.size = sizeof(hdr);
621 		hdr.cmd = SOF_IPC_GLB_TRACE_MSG | SOF_IPC_TRACE_DMA_FREE;
622 
623 		ret = sof_ipc_tx_message_no_reply(sdev->ipc, &hdr, hdr.size);
624 		if (ret < 0)
625 			dev_err(sdev->dev, "DMA_TRACE_FREE failed with error: %d\n", ret);
626 	}
627 
628 	if (only_stop)
629 		goto out;
630 
631 	ret = sof_dtrace_host_release(sdev);
632 	if (ret < 0)
633 		dev_err(sdev->dev, "Host dtrace release failed %d\n", ret);
634 
635 	priv->dtrace_state = SOF_DTRACE_DISABLED;
636 
637 out:
638 	priv->dtrace_draining = true;
639 	wake_up(&priv->trace_sleep);
640 }
641 
642 static void ipc3_dtrace_suspend(struct snd_sof_dev *sdev, pm_message_t pm_state)
643 {
644 	ipc3_dtrace_release(sdev, pm_state.event == SOF_DSP_PM_D0);
645 }
646 
647 static int ipc3_dtrace_resume(struct snd_sof_dev *sdev)
648 {
649 	return ipc3_dtrace_enable(sdev);
650 }
651 
652 static void ipc3_dtrace_free(struct snd_sof_dev *sdev)
653 {
654 	struct sof_dtrace_priv *priv = sdev->fw_trace_data;
655 
656 	/* release trace */
657 	ipc3_dtrace_release(sdev, false);
658 
659 	if (priv->dma_trace_pages) {
660 		snd_dma_free_pages(&priv->dmatb);
661 		snd_dma_free_pages(&priv->dmatp);
662 		priv->dma_trace_pages = 0;
663 	}
664 }
665 
666 const struct sof_ipc_fw_tracing_ops ipc3_dtrace_ops = {
667 	.init = ipc3_dtrace_init,
668 	.free = ipc3_dtrace_free,
669 	.fw_crashed = ipc3_dtrace_fw_crashed,
670 	.suspend = ipc3_dtrace_suspend,
671 	.resume = ipc3_dtrace_resume,
672 };
673