MALI: rockchip: upgrade midgard DDK to r14p0-01rel0
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / arm / midgard / mali_kbase_tlstream.c
1 /*
2  *
3  * (C) COPYRIGHT 2015-2016 ARM Limited. All rights reserved.
4  *
5  * This program is free software and is provided to you under the terms of the
6  * GNU General Public License version 2 as published by the Free Software
7  * Foundation, and any use by you of this program is subject to the terms
8  * of such GNU licence.
9  *
10  * A copy of the licence is included with the program, and can also be obtained
11  * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
12  * Boston, MA  02110-1301, USA.
13  *
14  */
15
16
17
18 #include <linux/anon_inodes.h>
19 #include <linux/atomic.h>
20 #include <linux/file.h>
21 #include <linux/mutex.h>
22 #include <linux/poll.h>
23 #include <linux/spinlock.h>
24 #include <linux/string.h>
25 #include <linux/stringify.h>
26 #include <linux/timer.h>
27 #include <linux/wait.h>
28
29 #include <mali_kbase.h>
30 #include <mali_kbase_jm.h>
31 #include <mali_kbase_tlstream.h>
32
33 /*****************************************************************************/
34
35 /* The version of swtrace protocol used in timeline stream. */
36 #define SWTRACE_VERSION    3
37
38 /* The maximum expected length of string in tracepoint descriptor. */
39 #define STRLEN_MAX         64 /* bytes */
40
41 /* The number of nanoseconds in a second. */
42 #define NSECS_IN_SEC       1000000000ull /* ns */
43
44 /* The period of autoflush checker execution in milliseconds. */
45 #define AUTOFLUSH_INTERVAL 1000 /* ms */
46
47 /* The maximum size of a single packet used by timeline. */
48 #define PACKET_SIZE        4096 /* bytes */
49
50 /* The number of packets used by one timeline stream. */
51 #define PACKET_COUNT       16
52
53 /* The number of bytes reserved for packet header.
54  * These value must be defined according to MIPE documentation. */
55 #define PACKET_HEADER_SIZE 8 /* bytes */
56
57 /* The number of bytes reserved for packet sequence number.
58  * These value must be defined according to MIPE documentation. */
59 #define PACKET_NUMBER_SIZE 4 /* bytes */
60
61 /* Packet header - first word.
62  * These values must be defined according to MIPE documentation. */
63 #define PACKET_STREAMID_POS  0
64 #define PACKET_STREAMID_LEN  8
65 #define PACKET_RSVD1_POS     (PACKET_STREAMID_POS + PACKET_STREAMID_LEN)
66 #define PACKET_RSVD1_LEN     8
67 #define PACKET_TYPE_POS      (PACKET_RSVD1_POS + PACKET_RSVD1_LEN)
68 #define PACKET_TYPE_LEN      3
69 #define PACKET_CLASS_POS     (PACKET_TYPE_POS + PACKET_TYPE_LEN)
70 #define PACKET_CLASS_LEN     7
71 #define PACKET_FAMILY_POS    (PACKET_CLASS_POS + PACKET_CLASS_LEN)
72 #define PACKET_FAMILY_LEN    6
73
74 /* Packet header - second word
75  * These values must be defined according to MIPE documentation. */
76 #define PACKET_LENGTH_POS    0
77 #define PACKET_LENGTH_LEN    24
78 #define PACKET_SEQBIT_POS    (PACKET_LENGTH_POS + PACKET_LENGTH_LEN)
79 #define PACKET_SEQBIT_LEN    1
80 #define PACKET_RSVD2_POS     (PACKET_SEQBIT_POS + PACKET_SEQBIT_LEN)
81 #define PACKET_RSVD2_LEN     7
82
83 /* Types of streams generated by timeline.
84  * Order is significant! Header streams must precede respective body streams. */
85 enum tl_stream_type {
86         TL_STREAM_TYPE_OBJ_HEADER,
87         TL_STREAM_TYPE_OBJ_SUMMARY,
88         TL_STREAM_TYPE_OBJ,
89         TL_STREAM_TYPE_AUX_HEADER,
90         TL_STREAM_TYPE_AUX,
91
92         TL_STREAM_TYPE_COUNT
93 };
94
95 /* Timeline packet family ids.
96  * Values are significant! Check MIPE documentation. */
97 enum tl_packet_family {
98         TL_PACKET_FAMILY_CTRL = 0, /* control packets */
99         TL_PACKET_FAMILY_TL   = 1, /* timeline packets */
100
101         TL_PACKET_FAMILY_COUNT
102 };
103
104 /* Packet classes used in timeline streams.
105  * Values are significant! Check MIPE documentation. */
106 enum tl_packet_class {
107         TL_PACKET_CLASS_OBJ = 0, /* timeline objects packet */
108         TL_PACKET_CLASS_AUX = 1, /* auxiliary events packet */
109 };
110
111 /* Packet types used in timeline streams.
112  * Values are significant! Check MIPE documentation. */
113 enum tl_packet_type {
114         TL_PACKET_TYPE_HEADER  = 0, /* stream's header/directory */
115         TL_PACKET_TYPE_BODY    = 1, /* stream's body */
116         TL_PACKET_TYPE_SUMMARY = 2, /* stream's summary */
117 };
118
119 /* Message ids of trace events that are recorded in the timeline stream. */
120 enum tl_msg_id_obj {
121         /* Timeline object events. */
122         KBASE_TL_NEW_CTX,
123         KBASE_TL_NEW_GPU,
124         KBASE_TL_NEW_LPU,
125         KBASE_TL_NEW_ATOM,
126         KBASE_TL_NEW_AS,
127         KBASE_TL_DEL_CTX,
128         KBASE_TL_DEL_ATOM,
129         KBASE_TL_LIFELINK_LPU_GPU,
130         KBASE_TL_LIFELINK_AS_GPU,
131         KBASE_TL_RET_CTX_LPU,
132         KBASE_TL_RET_ATOM_CTX,
133         KBASE_TL_RET_ATOM_LPU,
134         KBASE_TL_NRET_CTX_LPU,
135         KBASE_TL_NRET_ATOM_CTX,
136         KBASE_TL_NRET_ATOM_LPU,
137         KBASE_TL_RET_AS_CTX,
138         KBASE_TL_NRET_AS_CTX,
139         KBASE_TL_RET_ATOM_AS,
140         KBASE_TL_NRET_ATOM_AS,
141         KBASE_TL_DEP_ATOM_ATOM,
142         KBASE_TL_NDEP_ATOM_ATOM,
143         KBASE_TL_RDEP_ATOM_ATOM,
144         KBASE_TL_ATTRIB_ATOM_CONFIG,
145         KBASE_TL_ATTRIB_ATOM_PRIORITY,
146         KBASE_TL_ATTRIB_ATOM_STATE,
147         KBASE_TL_ATTRIB_ATOM_PRIORITY_CHANGE,
148         KBASE_TL_ATTRIB_AS_CONFIG,
149         KBASE_TL_EVENT_LPU_SOFTSTOP,
150         KBASE_TL_EVENT_ATOM_SOFTSTOP_EX,
151         KBASE_TL_EVENT_ATOM_SOFTSTOP_ISSUE,
152
153         /* Job dump specific events. */
154         KBASE_JD_GPU_SOFT_RESET
155 };
156
157 /* Message ids of trace events that are recorded in the auxiliary stream. */
158 enum tl_msg_id_aux {
159         KBASE_AUX_PM_STATE,
160         KBASE_AUX_PAGEFAULT,
161         KBASE_AUX_PAGESALLOC,
162         KBASE_AUX_DEVFREQ_TARGET
163 };
164
165 /*****************************************************************************/
166
167 /**
168  * struct tl_stream - timeline stream structure
169  * @lock: message order lock
170  * @buffer: array of buffers
171  * @wbi: write buffer index
172  * @rbi: read buffer index
173  * @numbered: if non-zero stream's packets are sequentially numbered
174  * @autoflush_counter: counter tracking stream's autoflush state
175  *
176  * This structure holds information needed to construct proper packets in the
177  * timeline stream. Each message in sequence must bear timestamp that is greater
178  * to one in previous message in the same stream. For this reason lock is held
179  * throughout the process of message creation. Each stream contains set of
180  * buffers. Each buffer will hold one MIPE packet. In case there is no free
181  * space required to store incoming message the oldest buffer is discarded.
182  * Each packet in timeline body stream has sequence number embedded (this value
183  * must increment monotonically and is used by packets receiver to discover
184  * buffer overflows.
185  * Autoflush counter is set to negative number when there is no data pending
186  * for flush and it is set to zero on every update of the buffer. Autoflush
187  * timer will increment the counter by one on every expiry. In case there will
188  * be no activity on the buffer during two consecutive timer expiries, stream
189  * buffer will be flushed.
190  */
191 struct tl_stream {
192         spinlock_t lock;
193
194         struct {
195                 atomic_t size;              /* number of bytes in buffer */
196                 char     data[PACKET_SIZE]; /* buffer's data */
197         } buffer[PACKET_COUNT];
198
199         atomic_t wbi;
200         atomic_t rbi;
201
202         int      numbered;
203         atomic_t autoflush_counter;
204 };
205
206 /**
207  * struct tp_desc - tracepoint message descriptor structure
208  * @id:        tracepoint ID identifying message in stream
209  * @id_str:    human readable version of tracepoint ID
210  * @name:      tracepoint description
211  * @arg_types: tracepoint's arguments types declaration
212  * @arg_names: comma separated list of tracepoint's arguments names
213  */
214 struct tp_desc {
215         u32        id;
216         const char *id_str;
217         const char *name;
218         const char *arg_types;
219         const char *arg_names;
220 };
221
222 /*****************************************************************************/
223
224 /* Configuration of timeline streams generated by kernel.
225  * Kernel emit only streams containing either timeline object events or
226  * auxiliary events. All streams have stream id value of 1 (as opposed to user
227  * space streams that have value of 0). */
228 static const struct {
229         enum tl_packet_family pkt_family;
230         enum tl_packet_class  pkt_class;
231         enum tl_packet_type   pkt_type;
232         unsigned int          stream_id;
233 } tl_stream_cfg[TL_STREAM_TYPE_COUNT] = {
234         {TL_PACKET_FAMILY_TL, TL_PACKET_CLASS_OBJ, TL_PACKET_TYPE_HEADER,  1},
235         {TL_PACKET_FAMILY_TL, TL_PACKET_CLASS_OBJ, TL_PACKET_TYPE_SUMMARY, 1},
236         {TL_PACKET_FAMILY_TL, TL_PACKET_CLASS_OBJ, TL_PACKET_TYPE_BODY,    1},
237         {TL_PACKET_FAMILY_TL, TL_PACKET_CLASS_AUX, TL_PACKET_TYPE_HEADER,  1},
238         {TL_PACKET_FAMILY_TL, TL_PACKET_CLASS_AUX, TL_PACKET_TYPE_BODY,    1}
239 };
240
241 /* The timeline streams generated by kernel. */
242 static struct tl_stream *tl_stream[TL_STREAM_TYPE_COUNT];
243
244 /* Autoflush timer. */
245 static struct timer_list autoflush_timer;
246
247 /* If non-zero autoflush timer is active. */
248 static atomic_t autoflush_timer_active;
249
250 /* Reader lock. Only one reader is allowed to have access to the timeline
251  * streams at any given time. */
252 static DEFINE_MUTEX(tl_reader_lock);
253
254 /* Timeline stream event queue. */
255 static DECLARE_WAIT_QUEUE_HEAD(tl_event_queue);
256
257 /* The timeline stream file operations functions. */
258 static ssize_t kbasep_tlstream_read(
259                 struct file *filp,
260                 char __user *buffer,
261                 size_t      size,
262                 loff_t      *f_pos);
263 static unsigned int kbasep_tlstream_poll(struct file *filp, poll_table *wait);
264 static int kbasep_tlstream_release(struct inode *inode, struct file *filp);
265
266 /* The timeline stream file operations structure. */
267 static const struct file_operations kbasep_tlstream_fops = {
268         .release = kbasep_tlstream_release,
269         .read    = kbasep_tlstream_read,
270         .poll    = kbasep_tlstream_poll,
271 };
272
273 /* Descriptors of timeline messages transmitted in object events stream. */
274 static const struct tp_desc tp_desc_obj[] = {
275         {
276                 KBASE_TL_NEW_CTX,
277                 __stringify(KBASE_TL_NEW_CTX),
278                 "object ctx is created",
279                 "@pII",
280                 "ctx,ctx_nr,tgid"
281         },
282         {
283                 KBASE_TL_NEW_GPU,
284                 __stringify(KBASE_TL_NEW_GPU),
285                 "object gpu is created",
286                 "@pII",
287                 "gpu,gpu_id,core_count"
288         },
289         {
290                 KBASE_TL_NEW_LPU,
291                 __stringify(KBASE_TL_NEW_LPU),
292                 "object lpu is created",
293                 "@pII",
294                 "lpu,lpu_nr,lpu_fn"
295         },
296         {
297                 KBASE_TL_NEW_ATOM,
298                 __stringify(KBASE_TL_NEW_ATOM),
299                 "object atom is created",
300                 "@pI",
301                 "atom,atom_nr"
302         },
303         {
304                 KBASE_TL_NEW_AS,
305                 __stringify(KBASE_TL_NEW_AS),
306                 "address space object is created",
307                 "@pI",
308                 "address_space,as_nr"
309         },
310         {
311                 KBASE_TL_DEL_CTX,
312                 __stringify(KBASE_TL_DEL_CTX),
313                 "context is destroyed",
314                 "@p",
315                 "ctx"
316         },
317         {
318                 KBASE_TL_DEL_ATOM,
319                 __stringify(KBASE_TL_DEL_ATOM),
320                 "atom is destroyed",
321                 "@p",
322                 "atom"
323         },
324         {
325                 KBASE_TL_LIFELINK_LPU_GPU,
326                 __stringify(KBASE_TL_LIFELINK_LPU_GPU),
327                 "lpu is deleted with gpu",
328                 "@pp",
329                 "lpu,gpu"
330         },
331         {
332                 KBASE_TL_LIFELINK_AS_GPU,
333                 __stringify(KBASE_TL_LIFELINK_AS_GPU),
334                 "address space is deleted with gpu",
335                 "@pp",
336                 "address_space,gpu"
337         },
338         {
339                 KBASE_TL_RET_CTX_LPU,
340                 __stringify(KBASE_TL_RET_CTX_LPU),
341                 "context is retained by lpu",
342                 "@pp",
343                 "ctx,lpu"
344         },
345         {
346                 KBASE_TL_RET_ATOM_CTX,
347                 __stringify(KBASE_TL_RET_ATOM_CTX),
348                 "atom is retained by context",
349                 "@pp",
350                 "atom,ctx"
351         },
352         {
353                 KBASE_TL_RET_ATOM_LPU,
354                 __stringify(KBASE_TL_RET_ATOM_LPU),
355                 "atom is retained by lpu",
356                 "@pps",
357                 "atom,lpu,attrib_match_list"
358         },
359         {
360                 KBASE_TL_NRET_CTX_LPU,
361                 __stringify(KBASE_TL_NRET_CTX_LPU),
362                 "context is released by lpu",
363                 "@pp",
364                 "ctx,lpu"
365         },
366         {
367                 KBASE_TL_NRET_ATOM_CTX,
368                 __stringify(KBASE_TL_NRET_ATOM_CTX),
369                 "atom is released by context",
370                 "@pp",
371                 "atom,ctx"
372         },
373         {
374                 KBASE_TL_NRET_ATOM_LPU,
375                 __stringify(KBASE_TL_NRET_ATOM_LPU),
376                 "atom is released by lpu",
377                 "@pp",
378                 "atom,lpu"
379         },
380         {
381                 KBASE_TL_RET_AS_CTX,
382                 __stringify(KBASE_TL_RET_AS_CTX),
383                 "address space is retained by context",
384                 "@pp",
385                 "address_space,ctx"
386         },
387         {
388                 KBASE_TL_NRET_AS_CTX,
389                 __stringify(KBASE_TL_NRET_AS_CTX),
390                 "address space is released by context",
391                 "@pp",
392                 "address_space,ctx"
393         },
394         {
395                 KBASE_TL_RET_ATOM_AS,
396                 __stringify(KBASE_TL_RET_ATOM_AS),
397                 "atom is retained by address space",
398                 "@pp",
399                 "atom,address_space"
400         },
401         {
402                 KBASE_TL_NRET_ATOM_AS,
403                 __stringify(KBASE_TL_NRET_ATOM_AS),
404                 "atom is released by address space",
405                 "@pp",
406                 "atom,address_space"
407         },
408         {
409                 KBASE_TL_DEP_ATOM_ATOM,
410                 __stringify(KBASE_TL_DEP_ATOM_ATOM),
411                 "atom2 depends on atom1",
412                 "@pp",
413                 "atom1,atom2"
414         },
415         {
416                 KBASE_TL_NDEP_ATOM_ATOM,
417                 __stringify(KBASE_TL_NDEP_ATOM_ATOM),
418                 "atom2 no longer depends on atom1",
419                 "@pp",
420                 "atom1,atom2"
421         },
422         {
423                 KBASE_TL_RDEP_ATOM_ATOM,
424                 __stringify(KBASE_TL_RDEP_ATOM_ATOM),
425                 "resolved dependecy of atom2 depending on atom1",
426                 "@pp",
427                 "atom1,atom2"
428         },
429         {
430                 KBASE_TL_ATTRIB_ATOM_CONFIG,
431                 __stringify(KBASE_TL_ATTRIB_ATOM_CONFIG),
432                 "atom job slot attributes",
433                 "@pLLI",
434                 "atom,descriptor,affinity,config"
435         },
436         {
437                 KBASE_TL_ATTRIB_ATOM_PRIORITY,
438                 __stringify(KBASE_TL_ATTRIB_ATOM_PRIORITY),
439                 "atom priority",
440                 "@pI",
441                 "atom,prio"
442         },
443         {
444                 KBASE_TL_ATTRIB_ATOM_STATE,
445                 __stringify(KBASE_TL_ATTRIB_ATOM_STATE),
446                 "atom state",
447                 "@pI",
448                 "atom,state"
449         },
450         {
451                 KBASE_TL_ATTRIB_ATOM_PRIORITY_CHANGE,
452                 __stringify(KBASE_TL_ATTRIB_ATOM_PRIORITY_CHANGE),
453                 "atom caused priority change",
454                 "@p",
455                 "atom"
456         },
457         {
458                 KBASE_TL_ATTRIB_AS_CONFIG,
459                 __stringify(KBASE_TL_ATTRIB_AS_CONFIG),
460                 "address space attributes",
461                 "@pLLL",
462                 "address_space,transtab,memattr,transcfg"
463         },
464         {
465                 KBASE_TL_EVENT_LPU_SOFTSTOP,
466                 __stringify(KBASE_TL_EVENT_LPU_SOFTSTOP),
467                 "softstop event on given lpu",
468                 "@p",
469                 "lpu"
470         },
471         {
472                 KBASE_TL_EVENT_ATOM_SOFTSTOP_EX,
473                 __stringify(KBASE_TL_EVENT_ATOM_SOFTSTOP_EX),
474                 "atom softstopped",
475                 "@p",
476                 "atom"
477         },
478         {
479                 KBASE_TL_EVENT_ATOM_SOFTSTOP_ISSUE,
480                 __stringify(KBASE_TL_EVENT_SOFTSTOP_ISSUE),
481                 "atom softstop issued",
482                 "@p",
483                 "atom"
484         },
485         {
486                 KBASE_JD_GPU_SOFT_RESET,
487                 __stringify(KBASE_JD_GPU_SOFT_RESET),
488                 "gpu soft reset",
489                 "@p",
490                 "gpu"
491         },
492 };
493
494 /* Descriptors of timeline messages transmitted in auxiliary events stream. */
495 static const struct tp_desc tp_desc_aux[] = {
496         {
497                 KBASE_AUX_PM_STATE,
498                 __stringify(KBASE_AUX_PM_STATE),
499                 "PM state",
500                 "@IL",
501                 "core_type,core_state_bitset"
502         },
503         {
504                 KBASE_AUX_PAGEFAULT,
505                 __stringify(KBASE_AUX_PAGEFAULT),
506                 "Page fault",
507                 "@IL",
508                 "ctx_nr,page_cnt_change"
509         },
510         {
511                 KBASE_AUX_PAGESALLOC,
512                 __stringify(KBASE_AUX_PAGESALLOC),
513                 "Total alloc pages change",
514                 "@IL",
515                 "ctx_nr,page_cnt"
516         },
517         {
518                 KBASE_AUX_DEVFREQ_TARGET,
519                 __stringify(KBASE_AUX_DEVFREQ_TARGET),
520                 "New device frequency target",
521                 "@L",
522                 "target_freq"
523         }
524 };
525
526 #if MALI_UNIT_TEST
527 /* Number of bytes read by user. */
528 static atomic_t tlstream_bytes_collected = {0};
529
530 /* Number of bytes generated by tracepoint messages. */
531 static atomic_t tlstream_bytes_generated = {0};
532 #endif /* MALI_UNIT_TEST */
533
534 /*****************************************************************************/
535
536 /* Indicator of whether the timeline stream file descriptor is used. */
537 atomic_t kbase_tlstream_enabled = {0};
538
539 /*****************************************************************************/
540
541 /**
542  * kbasep_tlstream_get_timestamp - return timestamp
543  *
544  * Function returns timestamp value based on raw monotonic timer. Value will
545  * wrap around zero in case of overflow.
546  * Return: timestamp value
547  */
548 static u64 kbasep_tlstream_get_timestamp(void)
549 {
550         struct timespec ts;
551         u64             timestamp;
552
553         getrawmonotonic(&ts);
554         timestamp = (u64)ts.tv_sec * NSECS_IN_SEC + ts.tv_nsec;
555         return timestamp;
556 }
557
558 /**
559  * kbasep_tlstream_write_bytes - write data to message buffer
560  * @buffer: buffer where data will be written
561  * @pos:    position in the buffer where to place data
562  * @bytes:  pointer to buffer holding data
563  * @len:    length of data to be written
564  *
565  * Return: updated position in the buffer
566  */
567 static size_t kbasep_tlstream_write_bytes(
568                 char       *buffer,
569                 size_t     pos,
570                 const void *bytes,
571                 size_t     len)
572 {
573         KBASE_DEBUG_ASSERT(buffer);
574         KBASE_DEBUG_ASSERT(bytes);
575
576         memcpy(&buffer[pos], bytes, len);
577
578         return pos + len;
579 }
580
581 /**
582  * kbasep_tlstream_write_string - write string to message buffer
583  * @buffer:         buffer where data will be written
584  * @pos:            position in the buffer where to place data
585  * @string:         pointer to buffer holding the source string
586  * @max_write_size: number of bytes that can be stored in buffer
587  *
588  * Return: updated position in the buffer
589  */
590 static size_t kbasep_tlstream_write_string(
591                 char       *buffer,
592                 size_t     pos,
593                 const char *string,
594                 size_t     max_write_size)
595 {
596         u32 string_len;
597
598         KBASE_DEBUG_ASSERT(buffer);
599         KBASE_DEBUG_ASSERT(string);
600         /* Timeline string consists of at least string length and nul
601          * terminator. */
602         KBASE_DEBUG_ASSERT(max_write_size >= sizeof(string_len) + sizeof(char));
603         max_write_size -= sizeof(string_len);
604
605         string_len = strlcpy(
606                         &buffer[pos + sizeof(string_len)],
607                         string,
608                         max_write_size);
609         string_len += sizeof(char);
610
611         /* Make sure that the source string fit into the buffer. */
612         KBASE_DEBUG_ASSERT(string_len <= max_write_size);
613
614         /* Update string length. */
615         memcpy(&buffer[pos], &string_len, sizeof(string_len));
616
617         return pos + sizeof(string_len) + string_len;
618 }
619
620 /**
621  * kbasep_tlstream_write_timestamp - write timestamp to message buffer
622  * @buffer: buffer where data will be written
623  * @pos:    position in the buffer where to place data
624  *
625  * Return: updated position in the buffer
626  */
627 static size_t kbasep_tlstream_write_timestamp(void *buffer, size_t pos)
628 {
629         u64 timestamp = kbasep_tlstream_get_timestamp();
630
631         return kbasep_tlstream_write_bytes(
632                         buffer, pos,
633                         &timestamp, sizeof(timestamp));
634 }
635
636 /**
637  * kbasep_tlstream_put_bits - put bits in a word
638  * @word:   pointer to the words being modified
639  * @value:  value that shall be written to given position
640  * @bitpos: position where value shall be written (in bits)
641  * @bitlen: length of value (in bits)
642  */
643 static void kbasep_tlstream_put_bits(
644                 u32          *word,
645                 u32          value,
646                 unsigned int bitpos,
647                 unsigned int bitlen)
648 {
649         const u32 mask = ((1 << bitlen) - 1) << bitpos;
650
651         KBASE_DEBUG_ASSERT(word);
652         KBASE_DEBUG_ASSERT((0 != bitlen) && (32 >= bitlen));
653         KBASE_DEBUG_ASSERT((bitpos + bitlen) <= 32);
654
655         *word &= ~mask;
656         *word |= ((value << bitpos) & mask);
657 }
658
659 /**
660  * kbasep_tlstream_packet_header_setup - setup the packet header
661  * @buffer:     pointer to the buffer
662  * @pkt_family: packet's family
663  * @pkt_type:   packet's type
664  * @pkt_class:  packet's class
665  * @stream_id:  stream id
666  * @numbered:   non-zero if this stream is numbered
667  *
668  * Function sets up immutable part of packet header in the given buffer.
669  */
670 static void kbasep_tlstream_packet_header_setup(
671                 char                  *buffer,
672                 enum tl_packet_family pkt_family,
673                 enum tl_packet_class  pkt_class,
674                 enum tl_packet_type   pkt_type,
675                 unsigned int          stream_id,
676                 int                   numbered)
677 {
678         u32 word0 = 0;
679         u32 word1 = 0;
680
681         KBASE_DEBUG_ASSERT(buffer);
682         KBASE_DEBUG_ASSERT(pkt_family == TL_PACKET_FAMILY_TL);
683         KBASE_DEBUG_ASSERT(
684                         (pkt_type == TL_PACKET_TYPE_HEADER)  ||
685                         (pkt_type == TL_PACKET_TYPE_SUMMARY) ||
686                         (pkt_type == TL_PACKET_TYPE_BODY));
687         KBASE_DEBUG_ASSERT(
688                         (pkt_class == TL_PACKET_CLASS_OBJ) ||
689                         (pkt_class == TL_PACKET_CLASS_AUX));
690
691         kbasep_tlstream_put_bits(
692                         &word0, pkt_family,
693                         PACKET_FAMILY_POS, PACKET_FAMILY_LEN);
694         kbasep_tlstream_put_bits(
695                         &word0, pkt_class,
696                         PACKET_CLASS_POS, PACKET_CLASS_LEN);
697         kbasep_tlstream_put_bits(
698                         &word0, pkt_type,
699                         PACKET_TYPE_POS, PACKET_TYPE_LEN);
700         kbasep_tlstream_put_bits(
701                         &word0, stream_id,
702                         PACKET_STREAMID_POS, PACKET_STREAMID_LEN);
703
704         if (numbered)
705                 kbasep_tlstream_put_bits(
706                                 &word1, 1,
707                                 PACKET_SEQBIT_POS, PACKET_SEQBIT_LEN);
708
709         memcpy(&buffer[0],             &word0, sizeof(word0));
710         memcpy(&buffer[sizeof(word0)], &word1, sizeof(word1));
711 }
712
713 /**
714  * kbasep_tlstream_packet_header_update - update the packet header
715  * @buffer:    pointer to the buffer
716  * @data_size: amount of data carried in this packet
717  *
718  * Function updates mutable part of packet header in the given buffer.
719  * Note that value of data_size must not including size of the header.
720  */
721 static void kbasep_tlstream_packet_header_update(
722                 char   *buffer,
723                 size_t data_size)
724 {
725         u32 word0;
726         u32 word1;
727
728         KBASE_DEBUG_ASSERT(buffer);
729         CSTD_UNUSED(word0);
730
731         memcpy(&word1, &buffer[sizeof(word0)], sizeof(word1));
732
733         kbasep_tlstream_put_bits(
734                         &word1, data_size,
735                         PACKET_LENGTH_POS, PACKET_LENGTH_LEN);
736
737         memcpy(&buffer[sizeof(word0)], &word1, sizeof(word1));
738 }
739
740 /**
741  * kbasep_tlstream_packet_number_update - update the packet number
742  * @buffer:  pointer to the buffer
743  * @counter: value of packet counter for this packet's stream
744  *
745  * Function updates packet number embedded within the packet placed in the
746  * given buffer.
747  */
748 static void kbasep_tlstream_packet_number_update(char *buffer, u32 counter)
749 {
750         KBASE_DEBUG_ASSERT(buffer);
751
752         memcpy(&buffer[PACKET_HEADER_SIZE], &counter, sizeof(counter));
753 }
754
755 /**
756  * kbasep_timeline_stream_reset - reset stream
757  * @stream:  pointer to the stream structure
758  *
759  * Function discards all pending messages and resets packet counters.
760  */
761 static void kbasep_timeline_stream_reset(struct tl_stream *stream)
762 {
763         unsigned int i;
764
765         for (i = 0; i < PACKET_COUNT; i++) {
766                 if (stream->numbered)
767                         atomic_set(
768                                         &stream->buffer[i].size,
769                                         PACKET_HEADER_SIZE +
770                                         PACKET_NUMBER_SIZE);
771                 else
772                         atomic_set(&stream->buffer[i].size, PACKET_HEADER_SIZE);
773         }
774
775         atomic_set(&stream->wbi, 0);
776         atomic_set(&stream->rbi, 0);
777 }
778
779 /**
780  * kbasep_timeline_stream_init - initialize timeline stream
781  * @stream:      pointer to the stream structure
782  * @stream_type: stream type
783  */
784 static void kbasep_timeline_stream_init(
785                 struct tl_stream    *stream,
786                 enum tl_stream_type stream_type)
787 {
788         unsigned int i;
789
790         KBASE_DEBUG_ASSERT(stream);
791         KBASE_DEBUG_ASSERT(TL_STREAM_TYPE_COUNT > stream_type);
792
793         spin_lock_init(&stream->lock);
794
795         /* All packets carrying tracepoints shall be numbered. */
796         if (TL_PACKET_TYPE_BODY == tl_stream_cfg[stream_type].pkt_type)
797                 stream->numbered = 1;
798         else
799                 stream->numbered = 0;
800
801         for (i = 0; i < PACKET_COUNT; i++)
802                 kbasep_tlstream_packet_header_setup(
803                                 stream->buffer[i].data,
804                                 tl_stream_cfg[stream_type].pkt_family,
805                                 tl_stream_cfg[stream_type].pkt_class,
806                                 tl_stream_cfg[stream_type].pkt_type,
807                                 tl_stream_cfg[stream_type].stream_id,
808                                 stream->numbered);
809
810         kbasep_timeline_stream_reset(tl_stream[stream_type]);
811 }
812
813 /**
814  * kbasep_timeline_stream_term - terminate timeline stream
815  * @stream: pointer to the stream structure
816  */
817 static void kbasep_timeline_stream_term(struct tl_stream *stream)
818 {
819         KBASE_DEBUG_ASSERT(stream);
820 }
821
822 /**
823  * kbasep_tlstream_msgbuf_submit - submit packet to the user space
824  * @stream:     pointer to the stream structure
825  * @wb_idx_raw: write buffer index
826  * @wb_size:    length of data stored in current buffer
827  *
828  * Function updates currently written buffer with packet header. Then write
829  * index is incremented and buffer is handled to user space. Parameters
830  * of new buffer are returned using provided arguments.
831  *
832  * Return: length of data in new buffer
833  *
834  * Warning:  User must update the stream structure with returned value.
835  */
836 static size_t kbasep_tlstream_msgbuf_submit(
837                 struct tl_stream *stream,
838                 unsigned int      wb_idx_raw,
839                 unsigned int      wb_size)
840 {
841         unsigned int rb_idx_raw = atomic_read(&stream->rbi);
842         unsigned int wb_idx = wb_idx_raw % PACKET_COUNT;
843
844         /* Set stream as flushed. */
845         atomic_set(&stream->autoflush_counter, -1);
846
847         kbasep_tlstream_packet_header_update(
848                         stream->buffer[wb_idx].data,
849                         wb_size - PACKET_HEADER_SIZE);
850
851         if (stream->numbered)
852                 kbasep_tlstream_packet_number_update(
853                                 stream->buffer[wb_idx].data,
854                                 wb_idx_raw);
855
856         /* Increasing write buffer index will expose this packet to the reader.
857          * As stream->lock is not taken on reader side we must make sure memory
858          * is updated correctly before this will happen. */
859         smp_wmb();
860         wb_idx_raw++;
861         atomic_set(&stream->wbi, wb_idx_raw);
862
863         /* Inform user that packets are ready for reading. */
864         wake_up_interruptible(&tl_event_queue);
865
866         /* Detect and mark overflow in this stream. */
867         if (PACKET_COUNT == wb_idx_raw - rb_idx_raw) {
868                 /* Reader side depends on this increment to correctly handle
869                  * overflows. The value shall be updated only if it was not
870                  * modified by the reader. The data holding buffer will not be
871                  * updated before stream->lock is released, however size of the
872                  * buffer will. Make sure this increment is globally visible
873                  * before information about selected write buffer size. */
874                 atomic_cmpxchg(&stream->rbi, rb_idx_raw, rb_idx_raw + 1);
875         }
876
877         wb_size = PACKET_HEADER_SIZE;
878         if (stream->numbered)
879                 wb_size += PACKET_NUMBER_SIZE;
880
881         return wb_size;
882 }
883
884 /**
885  * kbasep_tlstream_msgbuf_acquire - lock selected stream and reserves buffer
886  * @stream_type: type of the stream that shall be locked
887  * @msg_size:    message size
888  * @flags:       pointer to store flags passed back on stream release
889  *
890  * Function will lock the stream and reserve the number of bytes requested
891  * in msg_size for the user.
892  *
893  * Return: pointer to the buffer where message can be stored
894  *
895  * Warning: Stream must be released with kbasep_tlstream_msgbuf_release().
896  *          Only atomic operations are allowed while stream is locked
897  *          (i.e. do not use any operation that may sleep).
898  */
899 static char *kbasep_tlstream_msgbuf_acquire(
900                 enum tl_stream_type stream_type,
901                 size_t              msg_size,
902                 unsigned long       *flags) __acquires(&stream->lock)
903 {
904         struct tl_stream *stream;
905         unsigned int     wb_idx_raw;
906         unsigned int     wb_idx;
907         size_t           wb_size;
908
909         KBASE_DEBUG_ASSERT(TL_STREAM_TYPE_COUNT > stream_type);
910         KBASE_DEBUG_ASSERT(
911                         PACKET_SIZE - PACKET_HEADER_SIZE - PACKET_NUMBER_SIZE >=
912                         msg_size);
913
914         stream = tl_stream[stream_type];
915
916         spin_lock_irqsave(&stream->lock, *flags);
917
918         wb_idx_raw = atomic_read(&stream->wbi);
919         wb_idx     = wb_idx_raw % PACKET_COUNT;
920         wb_size    = atomic_read(&stream->buffer[wb_idx].size);
921
922         /* Select next buffer if data will not fit into current one. */
923         if (PACKET_SIZE < wb_size + msg_size) {
924                 wb_size = kbasep_tlstream_msgbuf_submit(
925                                 stream, wb_idx_raw, wb_size);
926                 wb_idx  = (wb_idx_raw + 1) % PACKET_COUNT;
927         }
928
929         /* Reserve space in selected buffer. */
930         atomic_set(&stream->buffer[wb_idx].size, wb_size + msg_size);
931
932 #if MALI_UNIT_TEST
933         atomic_add(msg_size, &tlstream_bytes_generated);
934 #endif /* MALI_UNIT_TEST */
935
936         return &stream->buffer[wb_idx].data[wb_size];
937 }
938
939 /**
940  * kbasep_tlstream_msgbuf_release - unlock selected stream
941  * @stream_type:  type of the stream that shall be locked
942  * @flags:        value obtained during stream acquire
943  *
944  * Function releases stream that has been previously locked with a call to
945  * kbasep_tlstream_msgbuf_acquire().
946  */
947 static void kbasep_tlstream_msgbuf_release(
948                 enum tl_stream_type stream_type,
949                 unsigned long       flags) __releases(&stream->lock)
950 {
951         struct tl_stream *stream;
952
953         KBASE_DEBUG_ASSERT(TL_STREAM_TYPE_COUNT > stream_type);
954
955         stream = tl_stream[stream_type];
956
957         /* Mark stream as containing unflushed data. */
958         atomic_set(&stream->autoflush_counter, 0);
959
960         spin_unlock_irqrestore(&stream->lock, flags);
961 }
962
963 /*****************************************************************************/
964
965 /**
966  * kbasep_tlstream_flush_stream - flush stream
967  * @stype:  type of stream to be flushed
968  *
969  * Flush pending data in timeline stream.
970  */
971 static void kbasep_tlstream_flush_stream(enum tl_stream_type stype)
972 {
973         struct tl_stream *stream = tl_stream[stype];
974         unsigned long    flags;
975         unsigned int     wb_idx_raw;
976         unsigned int     wb_idx;
977         size_t           wb_size;
978         size_t           min_size = PACKET_HEADER_SIZE;
979
980         if (stream->numbered)
981                 min_size += PACKET_NUMBER_SIZE;
982
983         spin_lock_irqsave(&stream->lock, flags);
984
985         wb_idx_raw = atomic_read(&stream->wbi);
986         wb_idx     = wb_idx_raw % PACKET_COUNT;
987         wb_size    = atomic_read(&stream->buffer[wb_idx].size);
988
989         if (wb_size > min_size) {
990                 wb_size = kbasep_tlstream_msgbuf_submit(
991                                 stream, wb_idx_raw, wb_size);
992                 wb_idx = (wb_idx_raw + 1) % PACKET_COUNT;
993                 atomic_set(&stream->buffer[wb_idx].size, wb_size);
994         }
995         spin_unlock_irqrestore(&stream->lock, flags);
996 }
997
998 /**
999  * kbasep_tlstream_autoflush_timer_callback - autoflush timer callback
1000  * @data:  unused
1001  *
1002  * Timer is executed periodically to check if any of the stream contains
1003  * buffer ready to be submitted to user space.
1004  */
1005 static void kbasep_tlstream_autoflush_timer_callback(unsigned long data)
1006 {
1007         enum tl_stream_type stype;
1008         int                 rcode;
1009
1010         CSTD_UNUSED(data);
1011
1012         for (stype = 0; stype < TL_STREAM_TYPE_COUNT; stype++) {
1013                 struct tl_stream *stream = tl_stream[stype];
1014                 unsigned long    flags;
1015                 unsigned int     wb_idx_raw;
1016                 unsigned int     wb_idx;
1017                 size_t           wb_size;
1018                 size_t           min_size = PACKET_HEADER_SIZE;
1019
1020                 int af_cnt = atomic_read(&stream->autoflush_counter);
1021
1022                 /* Check if stream contain unflushed data. */
1023                 if (0 > af_cnt)
1024                         continue;
1025
1026                 /* Check if stream should be flushed now. */
1027                 if (af_cnt != atomic_cmpxchg(
1028                                         &stream->autoflush_counter,
1029                                         af_cnt,
1030                                         af_cnt + 1))
1031                         continue;
1032                 if (!af_cnt)
1033                         continue;
1034
1035                 /* Autoflush this stream. */
1036                 if (stream->numbered)
1037                         min_size += PACKET_NUMBER_SIZE;
1038
1039                 spin_lock_irqsave(&stream->lock, flags);
1040
1041                 wb_idx_raw = atomic_read(&stream->wbi);
1042                 wb_idx     = wb_idx_raw % PACKET_COUNT;
1043                 wb_size    = atomic_read(&stream->buffer[wb_idx].size);
1044
1045                 if (wb_size > min_size) {
1046                         wb_size = kbasep_tlstream_msgbuf_submit(
1047                                         stream, wb_idx_raw, wb_size);
1048                         wb_idx = (wb_idx_raw + 1) % PACKET_COUNT;
1049                         atomic_set(&stream->buffer[wb_idx].size,
1050                                         wb_size);
1051                 }
1052                 spin_unlock_irqrestore(&stream->lock, flags);
1053         }
1054
1055         if (atomic_read(&autoflush_timer_active))
1056                 rcode = mod_timer(
1057                                 &autoflush_timer,
1058                                 jiffies + msecs_to_jiffies(AUTOFLUSH_INTERVAL));
1059         CSTD_UNUSED(rcode);
1060 }
1061
1062 /**
1063  * kbasep_tlstream_packet_pending - check timeline streams for pending packets
1064  * @stype:      pointer to variable where stream type will be placed
1065  * @rb_idx_raw: pointer to variable where read buffer index will be placed
1066  *
1067  * Function checks all streams for pending packets. It will stop as soon as
1068  * packet ready to be submitted to user space is detected. Variables under
1069  * pointers, passed as the parameters to this function will be updated with
1070  * values pointing to right stream and buffer.
1071  *
1072  * Return: non-zero if any of timeline streams has at last one packet ready
1073  */
1074 static int kbasep_tlstream_packet_pending(
1075                 enum tl_stream_type *stype,
1076                 unsigned int        *rb_idx_raw)
1077 {
1078         int pending = 0;
1079
1080         KBASE_DEBUG_ASSERT(stype);
1081         KBASE_DEBUG_ASSERT(rb_idx_raw);
1082
1083         for (
1084                         *stype = 0;
1085                         (*stype < TL_STREAM_TYPE_COUNT) && !pending;
1086                         (*stype)++) {
1087                 if (NULL != tl_stream[*stype]) {
1088                         *rb_idx_raw = atomic_read(&tl_stream[*stype]->rbi);
1089                         /* Read buffer index may be updated by writer in case of
1090                          * overflow. Read and write buffer indexes must be
1091                          * loaded in correct order. */
1092                         smp_rmb();
1093                         if (atomic_read(&tl_stream[*stype]->wbi) != *rb_idx_raw)
1094                                 pending = 1;
1095                 }
1096         }
1097         (*stype)--;
1098
1099         return pending;
1100 }
1101
1102 /**
1103  * kbasep_tlstream_read - copy data from streams to buffer provided by user
1104  * @filp:   pointer to file structure (unused)
1105  * @buffer: pointer to the buffer provided by user
1106  * @size:   maximum amount of data that can be stored in the buffer
1107  * @f_pos:  pointer to file offset (unused)
1108  *
1109  * Return: number of bytes stored in the buffer
1110  */
1111 static ssize_t kbasep_tlstream_read(
1112                 struct file *filp,
1113                 char __user *buffer,
1114                 size_t      size,
1115                 loff_t      *f_pos)
1116 {
1117         ssize_t copy_len = 0;
1118
1119         KBASE_DEBUG_ASSERT(filp);
1120         KBASE_DEBUG_ASSERT(f_pos);
1121
1122         if (!buffer)
1123                 return -EINVAL;
1124
1125         if ((0 > *f_pos) || (PACKET_SIZE > size))
1126                 return -EINVAL;
1127
1128         mutex_lock(&tl_reader_lock);
1129
1130         while (copy_len < size) {
1131                 enum tl_stream_type stype;
1132                 unsigned int        rb_idx_raw = 0;
1133                 unsigned int        rb_idx;
1134                 size_t              rb_size;
1135
1136                 /* If we don't have any data yet, wait for packet to be
1137                  * submitted. If we already read some packets and there is no
1138                  * packet pending return back to user. */
1139                 if (0 < copy_len) {
1140                         if (!kbasep_tlstream_packet_pending(
1141                                                 &stype,
1142                                                 &rb_idx_raw))
1143                                 break;
1144                 } else {
1145                         if (wait_event_interruptible(
1146                                                 tl_event_queue,
1147                                                 kbasep_tlstream_packet_pending(
1148                                                         &stype,
1149                                                         &rb_idx_raw))) {
1150                                 copy_len = -ERESTARTSYS;
1151                                 break;
1152                         }
1153                 }
1154
1155                 /* Check if this packet fits into the user buffer.
1156                  * If so copy its content. */
1157                 rb_idx = rb_idx_raw % PACKET_COUNT;
1158                 rb_size = atomic_read(&tl_stream[stype]->buffer[rb_idx].size);
1159                 if (rb_size > size - copy_len)
1160                         break;
1161                 if (copy_to_user(
1162                                         &buffer[copy_len],
1163                                         tl_stream[stype]->buffer[rb_idx].data,
1164                                         rb_size)) {
1165                         copy_len = -EFAULT;
1166                         break;
1167                 }
1168
1169                 /* If the rbi still points to the packet we just processed
1170                  * then there was no overflow so we add the copied size to
1171                  * copy_len and move rbi on to the next packet
1172                  */
1173                 smp_rmb();
1174                 if (atomic_read(&tl_stream[stype]->rbi) == rb_idx_raw) {
1175                         copy_len += rb_size;
1176                         atomic_inc(&tl_stream[stype]->rbi);
1177
1178 #if MALI_UNIT_TEST
1179                         atomic_add(rb_size, &tlstream_bytes_collected);
1180 #endif /* MALI_UNIT_TEST */
1181                 }
1182         }
1183
1184         mutex_unlock(&tl_reader_lock);
1185
1186         return copy_len;
1187 }
1188
1189 /**
1190  * kbasep_tlstream_poll - poll timeline stream for packets
1191  * @filp: pointer to file structure
1192  * @wait: pointer to poll table
1193  * Return: POLLIN if data can be read without blocking, otherwise zero
1194  */
1195 static unsigned int kbasep_tlstream_poll(struct file *filp, poll_table *wait)
1196 {
1197         enum tl_stream_type stream_type;
1198         unsigned int        rb_idx;
1199
1200         KBASE_DEBUG_ASSERT(filp);
1201         KBASE_DEBUG_ASSERT(wait);
1202
1203         poll_wait(filp, &tl_event_queue, wait);
1204         if (kbasep_tlstream_packet_pending(&stream_type, &rb_idx))
1205                 return POLLIN;
1206         return 0;
1207 }
1208
1209 /**
1210  * kbasep_tlstream_release - release timeline stream descriptor
1211  * @inode: pointer to inode structure
1212  * @filp:  pointer to file structure
1213  *
1214  * Return always return zero
1215  */
1216 static int kbasep_tlstream_release(struct inode *inode, struct file *filp)
1217 {
1218         KBASE_DEBUG_ASSERT(inode);
1219         KBASE_DEBUG_ASSERT(filp);
1220         CSTD_UNUSED(inode);
1221         CSTD_UNUSED(filp);
1222
1223         /* Stop autoflush timer before releasing access to streams. */
1224         atomic_set(&autoflush_timer_active, 0);
1225         del_timer_sync(&autoflush_timer);
1226
1227         atomic_set(&kbase_tlstream_enabled, 0);
1228         return 0;
1229 }
1230
1231 /**
1232  * kbasep_tlstream_timeline_header - prepare timeline header stream packet
1233  * @stream_type: type of the stream that will carry header data
1234  * @tp_desc:     pointer to array with tracepoint descriptors
1235  * @tp_count:    number of descriptors in the given array
1236  *
1237  * Functions fills in information about tracepoints stored in body stream
1238  * associated with this header stream.
1239  */
1240 static void kbasep_tlstream_timeline_header(
1241                 enum tl_stream_type  stream_type,
1242                 const struct tp_desc *tp_desc,
1243                 u32                  tp_count)
1244 {
1245         const u8      tv = SWTRACE_VERSION; /* protocol version */
1246         const u8      ps = sizeof(void *); /* pointer size */
1247         size_t        msg_size = sizeof(tv) + sizeof(ps) + sizeof(tp_count);
1248         char          *buffer;
1249         size_t        pos = 0;
1250         unsigned long flags;
1251         unsigned int  i;
1252
1253         KBASE_DEBUG_ASSERT(TL_STREAM_TYPE_COUNT > stream_type);
1254         KBASE_DEBUG_ASSERT(tp_desc);
1255
1256         /* Calculate the size of the timeline message. */
1257         for (i = 0; i < tp_count; i++) {
1258                 msg_size += sizeof(tp_desc[i].id);
1259                 msg_size +=
1260                         strnlen(tp_desc[i].id_str,    STRLEN_MAX) +
1261                         sizeof(char) + sizeof(u32);
1262                 msg_size +=
1263                         strnlen(tp_desc[i].name,      STRLEN_MAX) +
1264                         sizeof(char) + sizeof(u32);
1265                 msg_size +=
1266                         strnlen(tp_desc[i].arg_types, STRLEN_MAX) +
1267                         sizeof(char) + sizeof(u32);
1268                 msg_size +=
1269                         strnlen(tp_desc[i].arg_names, STRLEN_MAX) +
1270                         sizeof(char) + sizeof(u32);
1271         }
1272
1273         KBASE_DEBUG_ASSERT(PACKET_SIZE - PACKET_HEADER_SIZE >= msg_size);
1274
1275         buffer = kbasep_tlstream_msgbuf_acquire(stream_type, msg_size, &flags);
1276         KBASE_DEBUG_ASSERT(buffer);
1277
1278         pos = kbasep_tlstream_write_bytes(buffer, pos, &tv, sizeof(tv));
1279         pos = kbasep_tlstream_write_bytes(buffer, pos, &ps, sizeof(ps));
1280         pos = kbasep_tlstream_write_bytes(
1281                         buffer, pos, &tp_count, sizeof(tp_count));
1282
1283         for (i = 0; i < tp_count; i++) {
1284                 pos = kbasep_tlstream_write_bytes(
1285                                 buffer, pos,
1286                                 &tp_desc[i].id, sizeof(tp_desc[i].id));
1287                 pos = kbasep_tlstream_write_string(
1288                                 buffer, pos,
1289                                 tp_desc[i].id_str, msg_size - pos);
1290                 pos = kbasep_tlstream_write_string(
1291                                 buffer, pos,
1292                                 tp_desc[i].name, msg_size - pos);
1293                 pos = kbasep_tlstream_write_string(
1294                                 buffer, pos,
1295                                 tp_desc[i].arg_types, msg_size - pos);
1296                 pos = kbasep_tlstream_write_string(
1297                                 buffer, pos,
1298                                 tp_desc[i].arg_names, msg_size - pos);
1299         }
1300
1301         KBASE_DEBUG_ASSERT(msg_size == pos);
1302
1303         kbasep_tlstream_msgbuf_release(stream_type, flags);
1304
1305         /* We don't expect any more data to be read in this stream.
1306          * As header stream must be read before its associated body stream,
1307          * make this packet visible to the user straightaway. */
1308         kbasep_tlstream_flush_stream(stream_type);
1309 }
1310
1311 /*****************************************************************************/
1312
1313 int kbase_tlstream_init(void)
1314 {
1315         enum tl_stream_type i;
1316
1317         /* Prepare stream structures. */
1318         for (i = 0; i < TL_STREAM_TYPE_COUNT; i++) {
1319                 tl_stream[i] = kmalloc(sizeof(**tl_stream), GFP_KERNEL);
1320                 if (!tl_stream[i])
1321                         break;
1322                 kbasep_timeline_stream_init(tl_stream[i], i);
1323         }
1324         if (TL_STREAM_TYPE_COUNT > i) {
1325                 for (; i > 0; i--) {
1326                         kbasep_timeline_stream_term(tl_stream[i - 1]);
1327                         kfree(tl_stream[i - 1]);
1328                 }
1329                 return -ENOMEM;
1330         }
1331
1332         /* Initialize autoflush timer. */
1333         atomic_set(&autoflush_timer_active, 0);
1334         setup_timer(&autoflush_timer,
1335                         kbasep_tlstream_autoflush_timer_callback,
1336                         0);
1337
1338         return 0;
1339 }
1340
1341 void kbase_tlstream_term(void)
1342 {
1343         enum tl_stream_type i;
1344
1345         for (i = 0; i < TL_STREAM_TYPE_COUNT; i++) {
1346                 kbasep_timeline_stream_term(tl_stream[i]);
1347                 kfree(tl_stream[i]);
1348         }
1349 }
1350
1351 int kbase_tlstream_acquire(struct kbase_context *kctx, int *fd, u32 flags)
1352 {
1353         u32 tlstream_enabled = TLSTREAM_ENABLED | flags;
1354
1355         if (0 == atomic_cmpxchg(&kbase_tlstream_enabled, 0, tlstream_enabled)) {
1356                 int rcode;
1357
1358                 *fd = anon_inode_getfd(
1359                                 "[mali_tlstream]",
1360                                 &kbasep_tlstream_fops,
1361                                 kctx,
1362                                 O_RDONLY | O_CLOEXEC);
1363                 if (0 > *fd) {
1364                         atomic_set(&kbase_tlstream_enabled, 0);
1365                         return *fd;
1366                 }
1367
1368                 /* Reset and initialize header streams. */
1369                 kbasep_timeline_stream_reset(
1370                                 tl_stream[TL_STREAM_TYPE_OBJ_HEADER]);
1371                 kbasep_timeline_stream_reset(
1372                                 tl_stream[TL_STREAM_TYPE_OBJ_SUMMARY]);
1373                 kbasep_timeline_stream_reset(
1374                                 tl_stream[TL_STREAM_TYPE_AUX_HEADER]);
1375                 kbasep_tlstream_timeline_header(
1376                                 TL_STREAM_TYPE_OBJ_HEADER,
1377                                 tp_desc_obj,
1378                                 ARRAY_SIZE(tp_desc_obj));
1379                 kbasep_tlstream_timeline_header(
1380                                 TL_STREAM_TYPE_AUX_HEADER,
1381                                 tp_desc_aux,
1382                                 ARRAY_SIZE(tp_desc_aux));
1383
1384                 /* Start autoflush timer. */
1385                 atomic_set(&autoflush_timer_active, 1);
1386                 rcode = mod_timer(
1387                                 &autoflush_timer,
1388                                 jiffies + msecs_to_jiffies(AUTOFLUSH_INTERVAL));
1389                 CSTD_UNUSED(rcode);
1390
1391         } else {
1392                 *fd = -EBUSY;
1393         }
1394
1395         return 0;
1396 }
1397
1398 void kbase_tlstream_flush_streams(void)
1399 {
1400         enum tl_stream_type stype;
1401
1402         for (stype = 0; stype < TL_STREAM_TYPE_COUNT; stype++)
1403                 kbasep_tlstream_flush_stream(stype);
1404 }
1405
1406 void kbase_tlstream_reset_body_streams(void)
1407 {
1408         kbasep_timeline_stream_reset(
1409                         tl_stream[TL_STREAM_TYPE_OBJ]);
1410         kbasep_timeline_stream_reset(
1411                         tl_stream[TL_STREAM_TYPE_AUX]);
1412 }
1413
1414 #if MALI_UNIT_TEST
1415 void kbase_tlstream_stats(u32 *bytes_collected, u32 *bytes_generated)
1416 {
1417         KBASE_DEBUG_ASSERT(bytes_collected);
1418         KBASE_DEBUG_ASSERT(bytes_generated);
1419         *bytes_collected = atomic_read(&tlstream_bytes_collected);
1420         *bytes_generated = atomic_read(&tlstream_bytes_generated);
1421 }
1422 #endif /* MALI_UNIT_TEST */
1423
1424 /*****************************************************************************/
1425
1426 void __kbase_tlstream_tl_summary_new_ctx(void *context, u32 nr, u32 tgid)
1427 {
1428         const u32     msg_id = KBASE_TL_NEW_CTX;
1429         const size_t  msg_size =
1430                 sizeof(msg_id) + sizeof(u64) + sizeof(context) + sizeof(nr) +
1431                 sizeof(tgid);
1432         unsigned long flags;
1433         char          *buffer;
1434         size_t        pos = 0;
1435
1436         buffer = kbasep_tlstream_msgbuf_acquire(
1437                         TL_STREAM_TYPE_OBJ_SUMMARY,
1438                         msg_size, &flags);
1439         KBASE_DEBUG_ASSERT(buffer);
1440
1441         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
1442         pos = kbasep_tlstream_write_timestamp(buffer, pos);
1443         pos = kbasep_tlstream_write_bytes(
1444                         buffer, pos, &context, sizeof(context));
1445         pos = kbasep_tlstream_write_bytes(
1446                         buffer, pos, &nr, sizeof(nr));
1447         pos = kbasep_tlstream_write_bytes(
1448                         buffer, pos, &tgid, sizeof(tgid));
1449
1450         KBASE_DEBUG_ASSERT(msg_size == pos);
1451
1452         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ_SUMMARY, flags);
1453 }
1454
1455 void __kbase_tlstream_tl_summary_new_gpu(void *gpu, u32 id, u32 core_count)
1456 {
1457         const u32     msg_id = KBASE_TL_NEW_GPU;
1458         const size_t  msg_size =
1459                 sizeof(msg_id) + sizeof(u64) + sizeof(gpu) + sizeof(id) +
1460                 sizeof(core_count);
1461         unsigned long flags;
1462         char          *buffer;
1463         size_t        pos = 0;
1464
1465         buffer = kbasep_tlstream_msgbuf_acquire(
1466                         TL_STREAM_TYPE_OBJ_SUMMARY,
1467                         msg_size, &flags);
1468         KBASE_DEBUG_ASSERT(buffer);
1469
1470         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
1471         pos = kbasep_tlstream_write_timestamp(buffer, pos);
1472         pos = kbasep_tlstream_write_bytes(
1473                         buffer, pos, &gpu, sizeof(gpu));
1474         pos = kbasep_tlstream_write_bytes(
1475                         buffer, pos, &id, sizeof(id));
1476         pos = kbasep_tlstream_write_bytes(
1477                         buffer, pos, &core_count, sizeof(core_count));
1478         KBASE_DEBUG_ASSERT(msg_size == pos);
1479
1480         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ_SUMMARY, flags);
1481 }
1482
1483 void __kbase_tlstream_tl_summary_new_lpu(void *lpu, u32 nr, u32 fn)
1484 {
1485         const u32     msg_id = KBASE_TL_NEW_LPU;
1486         const size_t  msg_size =
1487                 sizeof(msg_id) + sizeof(u64) + sizeof(lpu) + sizeof(nr) +
1488                 sizeof(fn);
1489         unsigned long flags;
1490         char          *buffer;
1491         size_t        pos = 0;
1492
1493         buffer = kbasep_tlstream_msgbuf_acquire(
1494                         TL_STREAM_TYPE_OBJ_SUMMARY,
1495                         msg_size, &flags);
1496         KBASE_DEBUG_ASSERT(buffer);
1497
1498         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
1499         pos = kbasep_tlstream_write_timestamp(buffer, pos);
1500         pos = kbasep_tlstream_write_bytes(
1501                         buffer, pos, &lpu, sizeof(lpu));
1502         pos = kbasep_tlstream_write_bytes(
1503                         buffer, pos, &nr, sizeof(nr));
1504         pos = kbasep_tlstream_write_bytes(
1505                         buffer, pos, &fn, sizeof(fn));
1506         KBASE_DEBUG_ASSERT(msg_size == pos);
1507
1508         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ_SUMMARY, flags);
1509 }
1510
1511 void __kbase_tlstream_tl_summary_lifelink_lpu_gpu(void *lpu, void *gpu)
1512 {
1513         const u32     msg_id = KBASE_TL_LIFELINK_LPU_GPU;
1514         const size_t  msg_size =
1515                 sizeof(msg_id) + sizeof(u64) + sizeof(lpu) + sizeof(gpu);
1516         unsigned long flags;
1517         char          *buffer;
1518         size_t        pos = 0;
1519
1520         buffer = kbasep_tlstream_msgbuf_acquire(
1521                         TL_STREAM_TYPE_OBJ_SUMMARY,
1522                         msg_size, &flags);
1523         KBASE_DEBUG_ASSERT(buffer);
1524
1525         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
1526         pos = kbasep_tlstream_write_timestamp(buffer, pos);
1527         pos = kbasep_tlstream_write_bytes(
1528                         buffer, pos, &lpu, sizeof(lpu));
1529         pos = kbasep_tlstream_write_bytes(
1530                         buffer, pos, &gpu, sizeof(gpu));
1531         KBASE_DEBUG_ASSERT(msg_size == pos);
1532
1533         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ_SUMMARY, flags);
1534 }
1535
1536 void __kbase_tlstream_tl_summary_new_as(void *as, u32 nr)
1537 {
1538         const u32     msg_id = KBASE_TL_NEW_AS;
1539         const size_t  msg_size =
1540                 sizeof(msg_id) + sizeof(u64) + sizeof(as) + sizeof(nr);
1541         unsigned long flags;
1542         char          *buffer;
1543         size_t        pos = 0;
1544
1545         buffer = kbasep_tlstream_msgbuf_acquire(
1546                         TL_STREAM_TYPE_OBJ_SUMMARY,
1547                         msg_size, &flags);
1548         KBASE_DEBUG_ASSERT(buffer);
1549
1550         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
1551         pos = kbasep_tlstream_write_timestamp(buffer, pos);
1552         pos = kbasep_tlstream_write_bytes(
1553                         buffer, pos, &as, sizeof(as));
1554         pos = kbasep_tlstream_write_bytes(
1555                         buffer, pos, &nr, sizeof(nr));
1556         KBASE_DEBUG_ASSERT(msg_size == pos);
1557
1558         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ_SUMMARY, flags);
1559 }
1560
1561 void __kbase_tlstream_tl_summary_lifelink_as_gpu(void *as, void *gpu)
1562 {
1563         const u32     msg_id = KBASE_TL_LIFELINK_AS_GPU;
1564         const size_t  msg_size =
1565                 sizeof(msg_id) + sizeof(u64) + sizeof(as) + sizeof(gpu);
1566         unsigned long flags;
1567         char          *buffer;
1568         size_t        pos = 0;
1569
1570         buffer = kbasep_tlstream_msgbuf_acquire(
1571                         TL_STREAM_TYPE_OBJ_SUMMARY,
1572                         msg_size, &flags);
1573         KBASE_DEBUG_ASSERT(buffer);
1574
1575         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
1576         pos = kbasep_tlstream_write_timestamp(buffer, pos);
1577         pos = kbasep_tlstream_write_bytes(
1578                         buffer, pos, &as, sizeof(as));
1579         pos = kbasep_tlstream_write_bytes(
1580                         buffer, pos, &gpu, sizeof(gpu));
1581         KBASE_DEBUG_ASSERT(msg_size == pos);
1582
1583         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ_SUMMARY, flags);
1584 }
1585
1586 /*****************************************************************************/
1587
1588 void __kbase_tlstream_tl_new_ctx(void *context, u32 nr, u32 tgid)
1589 {
1590         const u32     msg_id = KBASE_TL_NEW_CTX;
1591         const size_t  msg_size =
1592                 sizeof(msg_id) + sizeof(u64) + sizeof(context) + sizeof(nr) +
1593                 sizeof(tgid);
1594         unsigned long flags;
1595         char          *buffer;
1596         size_t        pos = 0;
1597
1598         buffer = kbasep_tlstream_msgbuf_acquire(
1599                         TL_STREAM_TYPE_OBJ,
1600                         msg_size, &flags);
1601         KBASE_DEBUG_ASSERT(buffer);
1602
1603         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
1604         pos = kbasep_tlstream_write_timestamp(buffer, pos);
1605         pos = kbasep_tlstream_write_bytes(
1606                         buffer, pos, &context, sizeof(context));
1607         pos = kbasep_tlstream_write_bytes(
1608                         buffer, pos, &nr, sizeof(nr));
1609         pos = kbasep_tlstream_write_bytes(
1610                         buffer, pos, &tgid, sizeof(tgid));
1611         KBASE_DEBUG_ASSERT(msg_size == pos);
1612
1613         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
1614 }
1615
1616 void __kbase_tlstream_tl_new_atom(void *atom, u32 nr)
1617 {
1618         const u32     msg_id = KBASE_TL_NEW_ATOM;
1619         const size_t  msg_size = sizeof(msg_id) + sizeof(u64) + sizeof(atom) +
1620                         sizeof(nr);
1621         unsigned long flags;
1622         char          *buffer;
1623         size_t        pos = 0;
1624
1625         buffer = kbasep_tlstream_msgbuf_acquire(
1626                         TL_STREAM_TYPE_OBJ,
1627                         msg_size, &flags);
1628         KBASE_DEBUG_ASSERT(buffer);
1629
1630         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
1631         pos = kbasep_tlstream_write_timestamp(buffer, pos);
1632         pos = kbasep_tlstream_write_bytes(
1633                         buffer, pos, &atom, sizeof(atom));
1634         pos = kbasep_tlstream_write_bytes(
1635                         buffer, pos, &nr, sizeof(nr));
1636         KBASE_DEBUG_ASSERT(msg_size == pos);
1637
1638         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
1639 }
1640
1641 void __kbase_tlstream_tl_del_ctx(void *context)
1642 {
1643         const u32     msg_id = KBASE_TL_DEL_CTX;
1644         const size_t  msg_size =
1645                 sizeof(msg_id) + sizeof(u64) + sizeof(context);
1646         unsigned long flags;
1647         char          *buffer;
1648         size_t        pos = 0;
1649
1650         buffer = kbasep_tlstream_msgbuf_acquire(
1651                         TL_STREAM_TYPE_OBJ,
1652                         msg_size, &flags);
1653         KBASE_DEBUG_ASSERT(buffer);
1654
1655         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
1656         pos = kbasep_tlstream_write_timestamp(buffer, pos);
1657         pos = kbasep_tlstream_write_bytes(
1658                         buffer, pos, &context, sizeof(context));
1659         KBASE_DEBUG_ASSERT(msg_size == pos);
1660
1661         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
1662 }
1663
1664 void __kbase_tlstream_tl_del_atom(void *atom)
1665 {
1666         const u32     msg_id = KBASE_TL_DEL_ATOM;
1667         const size_t  msg_size =
1668                 sizeof(msg_id) + sizeof(u64) + sizeof(atom);
1669         unsigned long flags;
1670         char          *buffer;
1671         size_t        pos = 0;
1672
1673         buffer = kbasep_tlstream_msgbuf_acquire(
1674                         TL_STREAM_TYPE_OBJ,
1675                         msg_size, &flags);
1676         KBASE_DEBUG_ASSERT(buffer);
1677
1678         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
1679         pos = kbasep_tlstream_write_timestamp(buffer, pos);
1680         pos = kbasep_tlstream_write_bytes(
1681                         buffer, pos, &atom, sizeof(atom));
1682         KBASE_DEBUG_ASSERT(msg_size == pos);
1683
1684         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
1685 }
1686
1687 void __kbase_tlstream_tl_ret_ctx_lpu(void *context, void *lpu)
1688 {
1689         const u32     msg_id = KBASE_TL_RET_CTX_LPU;
1690         const size_t  msg_size =
1691                 sizeof(msg_id) + sizeof(u64) + sizeof(context) + sizeof(lpu);
1692         unsigned long flags;
1693         char          *buffer;
1694         size_t        pos = 0;
1695
1696         buffer = kbasep_tlstream_msgbuf_acquire(
1697                         TL_STREAM_TYPE_OBJ,
1698                         msg_size, &flags);
1699         KBASE_DEBUG_ASSERT(buffer);
1700
1701         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
1702         pos = kbasep_tlstream_write_timestamp(buffer, pos);
1703         pos = kbasep_tlstream_write_bytes(
1704                         buffer, pos, &context, sizeof(context));
1705         pos = kbasep_tlstream_write_bytes(
1706                         buffer, pos, &lpu, sizeof(lpu));
1707         KBASE_DEBUG_ASSERT(msg_size == pos);
1708
1709         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
1710 }
1711
1712 void __kbase_tlstream_tl_ret_atom_ctx(void *atom, void *context)
1713 {
1714         const u32     msg_id = KBASE_TL_RET_ATOM_CTX;
1715         const size_t  msg_size =
1716                 sizeof(msg_id) + sizeof(u64) + sizeof(atom) + sizeof(context);
1717         unsigned long flags;
1718         char          *buffer;
1719         size_t        pos = 0;
1720
1721         buffer = kbasep_tlstream_msgbuf_acquire(
1722                         TL_STREAM_TYPE_OBJ,
1723                         msg_size, &flags);
1724         KBASE_DEBUG_ASSERT(buffer);
1725
1726         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
1727         pos = kbasep_tlstream_write_timestamp(buffer, pos);
1728         pos = kbasep_tlstream_write_bytes(
1729                         buffer, pos, &atom, sizeof(atom));
1730         pos = kbasep_tlstream_write_bytes(
1731                         buffer, pos, &context, sizeof(context));
1732         KBASE_DEBUG_ASSERT(msg_size == pos);
1733
1734         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
1735 }
1736
1737 void __kbase_tlstream_tl_ret_atom_lpu(
1738                 void *atom, void *lpu, const char *attrib_match_list)
1739 {
1740         const u32     msg_id = KBASE_TL_RET_ATOM_LPU;
1741         const size_t  msg_s0 = sizeof(u32) + sizeof(char) +
1742                         strnlen(attrib_match_list, STRLEN_MAX);
1743         const size_t  msg_size =
1744                         sizeof(msg_id) + sizeof(u64) +
1745                         sizeof(atom) + sizeof(lpu) + msg_s0;
1746         unsigned long flags;
1747         char          *buffer;
1748         size_t        pos = 0;
1749
1750         buffer = kbasep_tlstream_msgbuf_acquire(
1751                         TL_STREAM_TYPE_OBJ,
1752                         msg_size, &flags);
1753         KBASE_DEBUG_ASSERT(buffer);
1754
1755         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
1756         pos = kbasep_tlstream_write_timestamp(buffer, pos);
1757         pos = kbasep_tlstream_write_bytes(
1758                         buffer, pos, &atom, sizeof(atom));
1759         pos = kbasep_tlstream_write_bytes(
1760                         buffer, pos, &lpu, sizeof(lpu));
1761         pos = kbasep_tlstream_write_string(
1762                         buffer, pos, attrib_match_list, msg_s0);
1763         KBASE_DEBUG_ASSERT(msg_size == pos);
1764
1765         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
1766 }
1767
1768 void __kbase_tlstream_tl_nret_ctx_lpu(void *context, void *lpu)
1769 {
1770         const u32     msg_id = KBASE_TL_NRET_CTX_LPU;
1771         const size_t  msg_size =
1772                 sizeof(msg_id) + sizeof(u64) + sizeof(context) + sizeof(lpu);
1773         unsigned long flags;
1774         char          *buffer;
1775         size_t        pos = 0;
1776
1777         buffer = kbasep_tlstream_msgbuf_acquire(
1778                         TL_STREAM_TYPE_OBJ,
1779                         msg_size, &flags);
1780         KBASE_DEBUG_ASSERT(buffer);
1781
1782         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
1783         pos = kbasep_tlstream_write_timestamp(buffer, pos);
1784         pos = kbasep_tlstream_write_bytes(
1785                         buffer, pos, &context, sizeof(context));
1786         pos = kbasep_tlstream_write_bytes(
1787                         buffer, pos, &lpu, sizeof(lpu));
1788         KBASE_DEBUG_ASSERT(msg_size == pos);
1789
1790         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
1791 }
1792
1793 void __kbase_tlstream_tl_nret_atom_ctx(void *atom, void *context)
1794 {
1795         const u32     msg_id = KBASE_TL_NRET_ATOM_CTX;
1796         const size_t  msg_size =
1797                 sizeof(msg_id) + sizeof(u64) + sizeof(atom) + sizeof(context);
1798         unsigned long flags;
1799         char          *buffer;
1800         size_t        pos = 0;
1801
1802         buffer = kbasep_tlstream_msgbuf_acquire(
1803                         TL_STREAM_TYPE_OBJ,
1804                         msg_size, &flags);
1805         KBASE_DEBUG_ASSERT(buffer);
1806
1807         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
1808         pos = kbasep_tlstream_write_timestamp(buffer, pos);
1809         pos = kbasep_tlstream_write_bytes(
1810                         buffer, pos, &atom, sizeof(atom));
1811         pos = kbasep_tlstream_write_bytes(
1812                         buffer, pos, &context, sizeof(context));
1813         KBASE_DEBUG_ASSERT(msg_size == pos);
1814
1815         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
1816 }
1817
1818 void __kbase_tlstream_tl_dep_atom_atom(void *atom1, void *atom2)
1819 {
1820         const u32     msg_id = KBASE_TL_DEP_ATOM_ATOM;
1821         const size_t  msg_size =
1822                 sizeof(msg_id) + sizeof(u64) + sizeof(atom1) + sizeof(atom2);
1823         unsigned long flags;
1824         char          *buffer;
1825         size_t        pos = 0;
1826
1827         buffer = kbasep_tlstream_msgbuf_acquire(
1828                         TL_STREAM_TYPE_OBJ,
1829                         msg_size, &flags);
1830         KBASE_DEBUG_ASSERT(buffer);
1831
1832         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
1833         pos = kbasep_tlstream_write_timestamp(buffer, pos);
1834         pos = kbasep_tlstream_write_bytes(
1835                         buffer, pos, &atom1, sizeof(atom1));
1836         pos = kbasep_tlstream_write_bytes(
1837                         buffer, pos, &atom2, sizeof(atom2));
1838         KBASE_DEBUG_ASSERT(msg_size == pos);
1839
1840         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
1841 }
1842
1843 void __kbase_tlstream_tl_ndep_atom_atom(void *atom1, void *atom2)
1844 {
1845         const u32     msg_id = KBASE_TL_NDEP_ATOM_ATOM;
1846         const size_t  msg_size =
1847                 sizeof(msg_id) + sizeof(u64) + sizeof(atom1) + sizeof(atom2);
1848         unsigned long flags;
1849         char          *buffer;
1850         size_t        pos = 0;
1851
1852         buffer = kbasep_tlstream_msgbuf_acquire(
1853                         TL_STREAM_TYPE_OBJ,
1854                         msg_size, &flags);
1855         KBASE_DEBUG_ASSERT(buffer);
1856
1857         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
1858         pos = kbasep_tlstream_write_timestamp(buffer, pos);
1859         pos = kbasep_tlstream_write_bytes(
1860                         buffer, pos, &atom1, sizeof(atom1));
1861         pos = kbasep_tlstream_write_bytes(
1862                         buffer, pos, &atom2, sizeof(atom2));
1863         KBASE_DEBUG_ASSERT(msg_size == pos);
1864
1865         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
1866 }
1867
1868 void __kbase_tlstream_tl_rdep_atom_atom(void *atom1, void *atom2)
1869 {
1870         const u32     msg_id = KBASE_TL_RDEP_ATOM_ATOM;
1871         const size_t  msg_size =
1872                 sizeof(msg_id) + sizeof(u64) + sizeof(atom1) + sizeof(atom2);
1873         unsigned long flags;
1874         char          *buffer;
1875         size_t        pos = 0;
1876
1877         buffer = kbasep_tlstream_msgbuf_acquire(
1878                         TL_STREAM_TYPE_OBJ,
1879                         msg_size, &flags);
1880         KBASE_DEBUG_ASSERT(buffer);
1881
1882         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
1883         pos = kbasep_tlstream_write_timestamp(buffer, pos);
1884         pos = kbasep_tlstream_write_bytes(
1885                         buffer, pos, &atom1, sizeof(atom1));
1886         pos = kbasep_tlstream_write_bytes(
1887                         buffer, pos, &atom2, sizeof(atom2));
1888         KBASE_DEBUG_ASSERT(msg_size == pos);
1889
1890         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
1891 }
1892
1893 void __kbase_tlstream_tl_nret_atom_lpu(void *atom, void *lpu)
1894 {
1895         const u32     msg_id = KBASE_TL_NRET_ATOM_LPU;
1896         const size_t  msg_size =
1897                 sizeof(msg_id) + sizeof(u64) + sizeof(atom) + sizeof(lpu);
1898         unsigned long flags;
1899         char          *buffer;
1900         size_t        pos = 0;
1901
1902         buffer = kbasep_tlstream_msgbuf_acquire(
1903                         TL_STREAM_TYPE_OBJ,
1904                         msg_size, &flags);
1905         KBASE_DEBUG_ASSERT(buffer);
1906
1907         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
1908         pos = kbasep_tlstream_write_timestamp(buffer, pos);
1909         pos = kbasep_tlstream_write_bytes(
1910                         buffer, pos, &atom, sizeof(atom));
1911         pos = kbasep_tlstream_write_bytes(
1912                         buffer, pos, &lpu, sizeof(lpu));
1913         KBASE_DEBUG_ASSERT(msg_size == pos);
1914
1915         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
1916 }
1917
1918 void __kbase_tlstream_tl_ret_as_ctx(void *as, void *ctx)
1919 {
1920         const u32     msg_id = KBASE_TL_RET_AS_CTX;
1921         const size_t  msg_size =
1922                 sizeof(msg_id) + sizeof(u64) + sizeof(as) + sizeof(ctx);
1923         unsigned long flags;
1924         char          *buffer;
1925         size_t        pos = 0;
1926
1927         buffer = kbasep_tlstream_msgbuf_acquire(
1928                         TL_STREAM_TYPE_OBJ,
1929                         msg_size, &flags);
1930         KBASE_DEBUG_ASSERT(buffer);
1931
1932         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
1933         pos = kbasep_tlstream_write_timestamp(buffer, pos);
1934         pos = kbasep_tlstream_write_bytes(
1935                         buffer, pos, &as, sizeof(as));
1936         pos = kbasep_tlstream_write_bytes(
1937                         buffer, pos, &ctx, sizeof(ctx));
1938         KBASE_DEBUG_ASSERT(msg_size == pos);
1939
1940         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
1941 }
1942
1943 void __kbase_tlstream_tl_nret_as_ctx(void *as, void *ctx)
1944 {
1945         const u32     msg_id = KBASE_TL_NRET_AS_CTX;
1946         const size_t  msg_size =
1947                 sizeof(msg_id) + sizeof(u64) + sizeof(as) + sizeof(ctx);
1948         unsigned long flags;
1949         char          *buffer;
1950         size_t        pos = 0;
1951
1952         buffer = kbasep_tlstream_msgbuf_acquire(
1953                         TL_STREAM_TYPE_OBJ,
1954                         msg_size, &flags);
1955         KBASE_DEBUG_ASSERT(buffer);
1956
1957         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
1958         pos = kbasep_tlstream_write_timestamp(buffer, pos);
1959         pos = kbasep_tlstream_write_bytes(
1960                         buffer, pos, &as, sizeof(as));
1961         pos = kbasep_tlstream_write_bytes(
1962                         buffer, pos, &ctx, sizeof(ctx));
1963         KBASE_DEBUG_ASSERT(msg_size == pos);
1964
1965         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
1966 }
1967
1968 void __kbase_tlstream_tl_ret_atom_as(void *atom, void *as)
1969 {
1970         const u32     msg_id = KBASE_TL_RET_ATOM_AS;
1971         const size_t  msg_size =
1972                 sizeof(msg_id) + sizeof(u64) + sizeof(atom) + sizeof(as);
1973         unsigned long flags;
1974         char          *buffer;
1975         size_t        pos = 0;
1976
1977         buffer = kbasep_tlstream_msgbuf_acquire(
1978                         TL_STREAM_TYPE_OBJ,
1979                         msg_size, &flags);
1980         KBASE_DEBUG_ASSERT(buffer);
1981
1982         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
1983         pos = kbasep_tlstream_write_timestamp(buffer, pos);
1984         pos = kbasep_tlstream_write_bytes(
1985                         buffer, pos, &atom, sizeof(atom));
1986         pos = kbasep_tlstream_write_bytes(
1987                         buffer, pos, &as, sizeof(as));
1988         KBASE_DEBUG_ASSERT(msg_size == pos);
1989
1990         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
1991 }
1992
1993 void __kbase_tlstream_tl_nret_atom_as(void *atom, void *as)
1994 {
1995         const u32     msg_id = KBASE_TL_NRET_ATOM_AS;
1996         const size_t  msg_size =
1997                 sizeof(msg_id) + sizeof(u64) + sizeof(atom) + sizeof(as);
1998         unsigned long flags;
1999         char          *buffer;
2000         size_t        pos = 0;
2001
2002         buffer = kbasep_tlstream_msgbuf_acquire(
2003                         TL_STREAM_TYPE_OBJ,
2004                         msg_size, &flags);
2005         KBASE_DEBUG_ASSERT(buffer);
2006
2007         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
2008         pos = kbasep_tlstream_write_timestamp(buffer, pos);
2009         pos = kbasep_tlstream_write_bytes(
2010                         buffer, pos, &atom, sizeof(atom));
2011         pos = kbasep_tlstream_write_bytes(
2012                         buffer, pos, &as, sizeof(as));
2013         KBASE_DEBUG_ASSERT(msg_size == pos);
2014
2015         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
2016 }
2017
2018 void __kbase_tlstream_tl_attrib_atom_config(
2019                 void *atom, u64 jd, u64 affinity, u32 config)
2020 {
2021         const u32     msg_id = KBASE_TL_ATTRIB_ATOM_CONFIG;
2022         const size_t  msg_size =
2023                 sizeof(msg_id) + sizeof(u64) + sizeof(atom) +
2024                 sizeof(jd) + sizeof(affinity) + sizeof(config);
2025         unsigned long flags;
2026         char          *buffer;
2027         size_t        pos = 0;
2028
2029         buffer = kbasep_tlstream_msgbuf_acquire(
2030                         TL_STREAM_TYPE_OBJ,
2031                         msg_size, &flags);
2032         KBASE_DEBUG_ASSERT(buffer);
2033
2034         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
2035         pos = kbasep_tlstream_write_timestamp(buffer, pos);
2036         pos = kbasep_tlstream_write_bytes(
2037                         buffer, pos, &atom, sizeof(atom));
2038         pos = kbasep_tlstream_write_bytes(
2039                         buffer, pos, &jd, sizeof(jd));
2040         pos = kbasep_tlstream_write_bytes(
2041                         buffer, pos, &affinity, sizeof(affinity));
2042         pos = kbasep_tlstream_write_bytes(
2043                         buffer, pos, &config, sizeof(config));
2044         KBASE_DEBUG_ASSERT(msg_size == pos);
2045
2046         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
2047 }
2048
2049 void __kbase_tlstream_tl_attrib_atom_priority(void *atom, u32 prio)
2050 {
2051         const u32     msg_id = KBASE_TL_ATTRIB_ATOM_PRIORITY;
2052         const size_t  msg_size =
2053                 sizeof(msg_id) + sizeof(u64) + sizeof(atom) + sizeof(prio);
2054         unsigned long flags;
2055         char          *buffer;
2056         size_t        pos = 0;
2057
2058         buffer = kbasep_tlstream_msgbuf_acquire(
2059                         TL_STREAM_TYPE_OBJ,
2060                         msg_size, &flags);
2061         KBASE_DEBUG_ASSERT(buffer);
2062
2063         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
2064         pos = kbasep_tlstream_write_timestamp(buffer, pos);
2065         pos = kbasep_tlstream_write_bytes(
2066                         buffer, pos, &atom, sizeof(atom));
2067         pos = kbasep_tlstream_write_bytes(
2068                         buffer, pos, &prio, sizeof(prio));
2069         KBASE_DEBUG_ASSERT(msg_size == pos);
2070
2071         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
2072 }
2073
2074 void __kbase_tlstream_tl_attrib_atom_state(void *atom, u32 state)
2075 {
2076         const u32     msg_id = KBASE_TL_ATTRIB_ATOM_STATE;
2077         const size_t  msg_size =
2078                 sizeof(msg_id) + sizeof(u64) + sizeof(atom) + sizeof(state);
2079         unsigned long flags;
2080         char          *buffer;
2081         size_t        pos = 0;
2082
2083         buffer = kbasep_tlstream_msgbuf_acquire(
2084                         TL_STREAM_TYPE_OBJ,
2085                         msg_size, &flags);
2086         KBASE_DEBUG_ASSERT(buffer);
2087
2088         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
2089         pos = kbasep_tlstream_write_timestamp(buffer, pos);
2090         pos = kbasep_tlstream_write_bytes(
2091                         buffer, pos, &atom, sizeof(atom));
2092         pos = kbasep_tlstream_write_bytes(
2093                         buffer, pos, &state, sizeof(state));
2094         KBASE_DEBUG_ASSERT(msg_size == pos);
2095
2096         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
2097 }
2098
2099 void __kbase_tlstream_tl_attrib_atom_priority_change(void *atom)
2100 {
2101         const u32     msg_id = KBASE_TL_ATTRIB_ATOM_PRIORITY_CHANGE;
2102         const size_t  msg_size =
2103                 sizeof(msg_id) + sizeof(u64) + sizeof(atom);
2104         unsigned long flags;
2105         char          *buffer;
2106         size_t        pos = 0;
2107
2108         buffer = kbasep_tlstream_msgbuf_acquire(
2109                         TL_STREAM_TYPE_OBJ,
2110                         msg_size, &flags);
2111         KBASE_DEBUG_ASSERT(buffer);
2112
2113         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
2114         pos = kbasep_tlstream_write_timestamp(buffer, pos);
2115         pos = kbasep_tlstream_write_bytes(
2116                         buffer, pos, &atom, sizeof(atom));
2117         KBASE_DEBUG_ASSERT(msg_size == pos);
2118
2119         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
2120 }
2121
2122 void __kbase_tlstream_tl_attrib_as_config(
2123                 void *as, u64 transtab, u64 memattr, u64 transcfg)
2124 {
2125         const u32     msg_id = KBASE_TL_ATTRIB_AS_CONFIG;
2126         const size_t  msg_size =
2127                 sizeof(msg_id) + sizeof(u64) + sizeof(as) +
2128                 sizeof(transtab) + sizeof(memattr) + sizeof(transcfg);
2129         unsigned long flags;
2130         char          *buffer;
2131         size_t        pos = 0;
2132
2133         buffer = kbasep_tlstream_msgbuf_acquire(
2134                         TL_STREAM_TYPE_OBJ,
2135                         msg_size, &flags);
2136         KBASE_DEBUG_ASSERT(buffer);
2137
2138         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
2139         pos = kbasep_tlstream_write_timestamp(buffer, pos);
2140         pos = kbasep_tlstream_write_bytes(
2141                         buffer, pos, &as, sizeof(as));
2142         pos = kbasep_tlstream_write_bytes(
2143                         buffer, pos, &transtab, sizeof(transtab));
2144         pos = kbasep_tlstream_write_bytes(
2145                         buffer, pos, &memattr, sizeof(memattr));
2146         pos = kbasep_tlstream_write_bytes(
2147                         buffer, pos, &transcfg, sizeof(transcfg));
2148         KBASE_DEBUG_ASSERT(msg_size == pos);
2149
2150         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
2151 }
2152
2153 void __kbase_tlstream_tl_event_lpu_softstop(void *lpu)
2154 {
2155         const u32     msg_id = KBASE_TL_EVENT_LPU_SOFTSTOP;
2156         const size_t  msg_size =
2157                 sizeof(msg_id) + sizeof(u64) + sizeof(lpu);
2158         unsigned long flags;
2159         char          *buffer;
2160         size_t        pos = 0;
2161
2162         buffer = kbasep_tlstream_msgbuf_acquire(
2163                         TL_STREAM_TYPE_OBJ,
2164                         msg_size, &flags);
2165         KBASE_DEBUG_ASSERT(buffer);
2166
2167         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
2168         pos = kbasep_tlstream_write_timestamp(buffer, pos);
2169         pos = kbasep_tlstream_write_bytes(
2170                         buffer, pos, &lpu, sizeof(lpu));
2171         KBASE_DEBUG_ASSERT(msg_size == pos);
2172
2173         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
2174 }
2175
2176 void __kbase_tlstream_tl_event_atom_softstop_ex(void *atom)
2177 {
2178         const u32     msg_id = KBASE_TL_EVENT_ATOM_SOFTSTOP_EX;
2179         const size_t  msg_size =
2180                 sizeof(msg_id) + sizeof(u64) + sizeof(atom);
2181         unsigned long flags;
2182         char          *buffer;
2183         size_t        pos = 0;
2184
2185         buffer = kbasep_tlstream_msgbuf_acquire(
2186                         TL_STREAM_TYPE_OBJ,
2187                         msg_size, &flags);
2188         KBASE_DEBUG_ASSERT(buffer);
2189
2190         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
2191         pos = kbasep_tlstream_write_timestamp(buffer, pos);
2192         pos = kbasep_tlstream_write_bytes(
2193                         buffer, pos, &atom, sizeof(atom));
2194         KBASE_DEBUG_ASSERT(msg_size == pos);
2195
2196         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
2197 }
2198
2199 void __kbase_tlstream_tl_event_atom_softstop_issue(void *atom)
2200 {
2201         const u32     msg_id = KBASE_TL_EVENT_ATOM_SOFTSTOP_ISSUE;
2202         const size_t  msg_size =
2203                 sizeof(msg_id) + sizeof(u64) + sizeof(atom);
2204         unsigned long flags;
2205         char          *buffer;
2206         size_t        pos = 0;
2207
2208         buffer = kbasep_tlstream_msgbuf_acquire(
2209                         TL_STREAM_TYPE_OBJ,
2210                         msg_size, &flags);
2211         KBASE_DEBUG_ASSERT(buffer);
2212
2213         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
2214         pos = kbasep_tlstream_write_timestamp(buffer, pos);
2215         pos = kbasep_tlstream_write_bytes(
2216                         buffer, pos, &atom, sizeof(atom));
2217         KBASE_DEBUG_ASSERT(msg_size == pos);
2218
2219         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
2220 }
2221
2222 void __kbase_tlstream_jd_gpu_soft_reset(void *gpu)
2223 {
2224         const u32     msg_id = KBASE_JD_GPU_SOFT_RESET;
2225         const size_t  msg_size =
2226                 sizeof(msg_id) + sizeof(u64) + sizeof(gpu);
2227         unsigned long flags;
2228         char          *buffer;
2229         size_t        pos = 0;
2230
2231         buffer = kbasep_tlstream_msgbuf_acquire(
2232                         TL_STREAM_TYPE_OBJ,
2233                         msg_size, &flags);
2234         KBASE_DEBUG_ASSERT(buffer);
2235
2236         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
2237         pos = kbasep_tlstream_write_timestamp(buffer, pos);
2238         pos = kbasep_tlstream_write_bytes(
2239                         buffer, pos, &gpu, sizeof(gpu));
2240         KBASE_DEBUG_ASSERT(msg_size == pos);
2241
2242         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
2243 }
2244
2245 /*****************************************************************************/
2246
2247 void __kbase_tlstream_aux_pm_state(u32 core_type, u64 state)
2248 {
2249         const u32     msg_id = KBASE_AUX_PM_STATE;
2250         const size_t  msg_size =
2251                 sizeof(msg_id) + sizeof(u64) + sizeof(core_type) +
2252                 sizeof(state);
2253         unsigned long flags;
2254         char          *buffer;
2255         size_t        pos = 0;
2256
2257         buffer = kbasep_tlstream_msgbuf_acquire(
2258                         TL_STREAM_TYPE_AUX,
2259                         msg_size, &flags);
2260         KBASE_DEBUG_ASSERT(buffer);
2261
2262         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
2263         pos = kbasep_tlstream_write_timestamp(buffer, pos);
2264         pos = kbasep_tlstream_write_bytes(
2265                         buffer, pos, &core_type, sizeof(core_type));
2266         pos = kbasep_tlstream_write_bytes(buffer, pos, &state, sizeof(state));
2267         KBASE_DEBUG_ASSERT(msg_size == pos);
2268
2269         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_AUX, flags);
2270 }
2271
2272 void __kbase_tlstream_aux_pagefault(u32 ctx_nr, u64 page_count_change)
2273 {
2274         const u32     msg_id = KBASE_AUX_PAGEFAULT;
2275         const size_t  msg_size =
2276                 sizeof(msg_id) + sizeof(u64) + sizeof(ctx_nr) +
2277                 sizeof(page_count_change);
2278         unsigned long flags;
2279         char          *buffer;
2280         size_t        pos = 0;
2281
2282         buffer = kbasep_tlstream_msgbuf_acquire(
2283                         TL_STREAM_TYPE_AUX, msg_size, &flags);
2284         KBASE_DEBUG_ASSERT(buffer);
2285
2286         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
2287         pos = kbasep_tlstream_write_timestamp(buffer, pos);
2288         pos = kbasep_tlstream_write_bytes(buffer, pos, &ctx_nr, sizeof(ctx_nr));
2289         pos = kbasep_tlstream_write_bytes(
2290                         buffer, pos,
2291                         &page_count_change, sizeof(page_count_change));
2292         KBASE_DEBUG_ASSERT(msg_size == pos);
2293
2294         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_AUX, flags);
2295 }
2296
2297 void __kbase_tlstream_aux_pagesalloc(u32 ctx_nr, u64 page_count)
2298 {
2299         const u32     msg_id = KBASE_AUX_PAGESALLOC;
2300         const size_t  msg_size =
2301                 sizeof(msg_id) + sizeof(u64) + sizeof(ctx_nr) +
2302                 sizeof(page_count);
2303         unsigned long flags;
2304         char          *buffer;
2305         size_t        pos = 0;
2306
2307         buffer = kbasep_tlstream_msgbuf_acquire(
2308                         TL_STREAM_TYPE_AUX, msg_size, &flags);
2309         KBASE_DEBUG_ASSERT(buffer);
2310
2311         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
2312         pos = kbasep_tlstream_write_timestamp(buffer, pos);
2313         pos = kbasep_tlstream_write_bytes(buffer, pos, &ctx_nr, sizeof(ctx_nr));
2314         pos = kbasep_tlstream_write_bytes(
2315                         buffer, pos, &page_count, sizeof(page_count));
2316         KBASE_DEBUG_ASSERT(msg_size == pos);
2317
2318         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_AUX, flags);
2319 }
2320
2321 void __kbase_tlstream_aux_devfreq_target(u64 target_freq)
2322 {
2323         const u32       msg_id = KBASE_AUX_DEVFREQ_TARGET;
2324         const size_t    msg_size =
2325                 sizeof(msg_id) + sizeof(u64) + sizeof(target_freq);
2326         unsigned long   flags;
2327         char            *buffer;
2328         size_t          pos = 0;
2329
2330         buffer = kbasep_tlstream_msgbuf_acquire(
2331                         TL_STREAM_TYPE_AUX, msg_size, &flags);
2332         KBASE_DEBUG_ASSERT(buffer);
2333
2334         pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
2335         pos = kbasep_tlstream_write_timestamp(buffer, pos);
2336         pos = kbasep_tlstream_write_bytes(
2337                         buffer, pos, &target_freq, sizeof(target_freq));
2338         KBASE_DEBUG_ASSERT(msg_size == pos);
2339
2340         kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_AUX, flags);
2341 }
2342