1 /*
2  * Copyright (c) 2024 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <ctype.h>
8 #include <zephyr/kernel.h>
9 #include <string.h>
10 #include <tracing_core.h>
11 #include <tracing_buffer.h>
12 #include <tracing_backend.h>
13 
14 #include <adsp_memory.h>
15 #include <adsp_debug_window.h>
16 
17 /* structure of memory window */
18 struct tracing_backend_adsp_memory_window {
19 	uint32_t head_offset;	/* offset of the first not used byte in data[] */
20 	uint8_t data[];		/* tracing data */
21 } __packed __aligned(8);
22 
23 #define ADSP_TRACING_WINDOW_DATA_SIZE		\
24 	(ADSP_DW_SLOT_SIZE - offsetof(struct tracing_backend_adsp_memory_window, data))
25 
26 static volatile struct tracing_backend_adsp_memory_window *mem_window;
27 
tracing_backend_adsp_memory_window_output(const struct tracing_backend * backend,uint8_t * data,uint32_t length)28 static void tracing_backend_adsp_memory_window_output(
29 		const struct tracing_backend *backend,
30 		uint8_t *data, uint32_t length)
31 {
32 	/* copy data to ring buffer,
33 	 * to make FW part fast, there's no sync with the data reader
34 	 * the reader MUST read data before they got overwritten
35 	 */
36 	size_t to_copy = MIN(length, ADSP_TRACING_WINDOW_DATA_SIZE - mem_window->head_offset);
37 
38 	memcpy((void *)(mem_window->data + mem_window->head_offset), data, to_copy);
39 
40 	length -= to_copy;
41 	if (length) {
42 		memcpy((void *)mem_window->data, data + to_copy, length);
43 		mem_window->head_offset = length;
44 	} else {
45 		mem_window->head_offset += to_copy;
46 	}
47 }
48 
tracing_backend_adsp_memory_window_init(void)49 static void tracing_backend_adsp_memory_window_init(void)
50 {
51 	volatile struct adsp_debug_window *window = ADSP_DW;
52 
53 	window->descs[ADSP_DW_SLOT_NUM_TRACE].type = ADSP_DW_SLOT_TRACE;
54 	window->descs[ADSP_DW_SLOT_NUM_TRACE].resource_id = 0;
55 	mem_window = (struct tracing_backend_adsp_memory_window *)
56 			ADSP_DW->slots[ADSP_DW_SLOT_NUM_TRACE];
57 
58 	mem_window->head_offset = 0;
59 }
60 
61 const struct tracing_backend_api tracing_backend_adsp_memory_window_api = {
62 	.init = tracing_backend_adsp_memory_window_init,
63 	.output  = tracing_backend_adsp_memory_window_output
64 };
65 
66 TRACING_BACKEND_DEFINE(tracing_backend_adsp_memory_window, tracing_backend_adsp_memory_window_api);
67