xref: /OK3568_Linux_fs/kernel/drivers/gpu/arm/bifrost/tl/mali_kbase_tl_serialize.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 /*
3  *
4  * (C) COPYRIGHT 2019-2022 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 #if !defined(_KBASE_TL_SERIALIZE_H)
23 #define _KBASE_TL_SERIALIZE_H
24 
25 #include <mali_kbase.h>
26 
27 #include <linux/timer.h>
28 
29 /* The number of nanoseconds in a second. */
30 #define NSECS_IN_SEC       1000000000ull /* ns */
31 
32 /**
33  * kbasep_serialize_bytes - serialize bytes to the message buffer
34  *
35  * @buffer:    Message buffer
36  * @pos:       Message buffer offset
37  * @bytes:     Bytes to serialize
38  * @len:       Length of bytes array
39  *
40  * Serialize bytes as if using memcpy().
41  *
42  * Return: updated position in the buffer
43  */
kbasep_serialize_bytes(char * buffer,size_t pos,const void * bytes,size_t len)44 static inline size_t kbasep_serialize_bytes(
45 		char       *buffer,
46 		size_t     pos,
47 		const void *bytes,
48 		size_t     len)
49 {
50 	KBASE_DEBUG_ASSERT(buffer);
51 	KBASE_DEBUG_ASSERT(bytes);
52 
53 	memcpy(&buffer[pos], bytes, len);
54 
55 	return pos + len;
56 }
57 
58 /**
59  * kbasep_serialize_string - serialize string to the message buffer
60  *
61  * @buffer:         Message buffer
62  * @pos:            Message buffer offset
63  * @string:         String to serialize
64  * @max_write_size: Number of bytes that can be stored in buffer
65  *
66  * String is serialized as 4 bytes for string size,
67  * then string content and then null terminator.
68  *
69  * Return: updated position in the buffer
70  */
kbasep_serialize_string(char * buffer,size_t pos,const char * string,size_t max_write_size)71 static inline size_t kbasep_serialize_string(
72 		char       *buffer,
73 		size_t     pos,
74 		const char *string,
75 		size_t     max_write_size)
76 {
77 	u32 string_len;
78 
79 	KBASE_DEBUG_ASSERT(buffer);
80 	KBASE_DEBUG_ASSERT(string);
81 	/* Timeline string consists of at least string length and nul
82 	 * terminator.
83 	 */
84 	KBASE_DEBUG_ASSERT(max_write_size >= sizeof(string_len) + sizeof(char));
85 	max_write_size -= sizeof(string_len);
86 
87 	string_len = strscpy(
88 			&buffer[pos + sizeof(string_len)],
89 			string,
90 			max_write_size);
91 	string_len += sizeof(char);
92 
93 	/* Make sure that the source string fit into the buffer. */
94 	KBASE_DEBUG_ASSERT(string_len <= max_write_size);
95 
96 	/* Update string length. */
97 	memcpy(&buffer[pos], &string_len, sizeof(string_len));
98 
99 	return pos + sizeof(string_len) + string_len;
100 }
101 
102 /**
103  * kbasep_serialize_timestamp - serialize timestamp to the message buffer
104  *
105  * @buffer: Message buffer
106  * @pos:    Message buffer offset
107  *
108  * Get current timestamp using kbasep_get_timestamp()
109  * and serialize it as 64 bit unsigned integer.
110  *
111  * Return: updated position in the buffer
112  */
kbasep_serialize_timestamp(void * buffer,size_t pos)113 static inline size_t kbasep_serialize_timestamp(void *buffer, size_t pos)
114 {
115 	u64             timestamp;
116 
117 	timestamp = ktime_get_raw_ns();
118 
119 	return kbasep_serialize_bytes(
120 			buffer, pos,
121 			&timestamp, sizeof(timestamp));
122 }
123 #endif /* _KBASE_TL_SERIALIZE_H */
124