1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ 2 /* 3 * 4 * (C) COPYRIGHT 2015-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_TLSTREAM_H) 23 #define _KBASE_TLSTREAM_H 24 25 #include <linux/spinlock.h> 26 #include <linux/atomic.h> 27 #include <linux/wait.h> 28 29 /* The maximum size of a single packet used by timeline. */ 30 #define PACKET_SIZE 4096 /* bytes */ 31 32 /* The number of packets used by one timeline stream. */ 33 #define PACKET_COUNT 128 34 35 /* The maximum expected length of string in tracepoint descriptor. */ 36 #define STRLEN_MAX 64 /* bytes */ 37 38 /** 39 * struct kbase_tlstream - timeline stream structure 40 * @lock: Message order lock 41 * @buffer: Array of buffers 42 * @buffer.size: Number of bytes in buffer 43 * @buffer.data: Buffer's data 44 * @wbi: Write buffer index 45 * @rbi: Read buffer index 46 * @numbered: If non-zero stream's packets are sequentially numbered 47 * @autoflush_counter: Counter tracking stream's autoflush state 48 * @ready_read: Pointer to a wait queue, which is signaled when 49 * timeline messages are ready for collection. 50 * @bytes_generated: Number of bytes generated by tracepoint messages 51 * 52 * This structure holds information needed to construct proper packets in the 53 * timeline stream. 54 * 55 * Each message in the sequence must bear a timestamp that is 56 * greater than the previous message in the same stream. For this reason 57 * a lock is held throughout the process of message creation. 58 * 59 * Each stream contains a set of buffers. Each buffer will hold one MIPE 60 * packet. In case there is no free space required to store the incoming 61 * message the oldest buffer is discarded. Each packet in timeline body 62 * stream has a sequence number embedded, this value must increment 63 * monotonically and is used by the packets receiver to discover these 64 * buffer overflows. 65 * 66 * The autoflush counter is set to a negative number when there is no data 67 * pending for flush and it is set to zero on every update of the buffer. The 68 * autoflush timer will increment the counter by one on every expiry. If there 69 * is no activity on the buffer for two consecutive timer expiries, the stream 70 * buffer will be flushed. 71 */ 72 struct kbase_tlstream { 73 spinlock_t lock; 74 75 struct { 76 atomic_t size; 77 char data[PACKET_SIZE]; 78 } buffer[PACKET_COUNT]; 79 80 atomic_t wbi; 81 atomic_t rbi; 82 83 int numbered; 84 atomic_t autoflush_counter; 85 wait_queue_head_t *ready_read; 86 #if MALI_UNIT_TEST 87 atomic_t bytes_generated; 88 #endif 89 }; 90 91 /* Types of streams generated by timeline. */ 92 enum tl_stream_type { 93 TL_STREAM_TYPE_FIRST, 94 TL_STREAM_TYPE_OBJ_SUMMARY = TL_STREAM_TYPE_FIRST, 95 TL_STREAM_TYPE_OBJ, 96 TL_STREAM_TYPE_AUX, 97 #if MALI_USE_CSF 98 TL_STREAM_TYPE_CSFFW, 99 #endif 100 TL_STREAM_TYPE_COUNT 101 }; 102 103 /** 104 * kbase_tlstream_init - initialize timeline stream 105 * @stream: Pointer to the stream structure 106 * @stream_type: Stream type 107 * @ready_read: Pointer to a wait queue to signal when 108 * timeline messages are ready for collection. 109 */ 110 void kbase_tlstream_init(struct kbase_tlstream *stream, 111 enum tl_stream_type stream_type, 112 wait_queue_head_t *ready_read); 113 114 /** 115 * kbase_tlstream_term - terminate timeline stream 116 * @stream: Pointer to the stream structure 117 */ 118 void kbase_tlstream_term(struct kbase_tlstream *stream); 119 120 /** 121 * kbase_tlstream_reset - reset stream 122 * @stream: Pointer to the stream structure 123 * 124 * Function discards all pending messages and resets packet counters. 125 */ 126 void kbase_tlstream_reset(struct kbase_tlstream *stream); 127 128 /** 129 * kbase_tlstream_msgbuf_acquire - lock selected stream and reserve a buffer 130 * @stream: Pointer to the stream structure 131 * @msg_size: Message size 132 * @flags: Pointer to store flags passed back on stream release 133 * 134 * Lock the stream and reserve the number of bytes requested 135 * in msg_size for the user. 136 * 137 * Return: pointer to the buffer where a message can be stored 138 * 139 * Warning: The stream must be released with kbase_tlstream_msgbuf_release(). 140 * Only atomic operations are allowed while the stream is locked 141 * (i.e. do not use any operation that may sleep). 142 */ 143 char *kbase_tlstream_msgbuf_acquire(struct kbase_tlstream *stream, 144 size_t msg_size, unsigned long *flags) __acquires(&stream->lock); 145 146 /** 147 * kbase_tlstream_msgbuf_release - unlock selected stream 148 * @stream: Pointer to the stream structure 149 * @flags: Value obtained during stream acquire 150 * 151 * Release the stream that has been previously 152 * locked with a call to kbase_tlstream_msgbuf_acquire(). 153 */ 154 void kbase_tlstream_msgbuf_release(struct kbase_tlstream *stream, 155 unsigned long flags) __releases(&stream->lock); 156 157 /** 158 * kbase_tlstream_flush_stream - flush stream 159 * @stream: Pointer to the stream structure 160 * 161 * Flush pending data in the timeline stream. 162 * 163 * Return: Number of bytes available flushed and available to be read 164 * 165 */ 166 size_t kbase_tlstream_flush_stream(struct kbase_tlstream *stream); 167 168 #endif /* _KBASE_TLSTREAM_H */ 169