aboutsummaryrefslogtreecommitdiff
path: root/drivers/gpu/arm/midgard/mali_kbase_tlstream.c
blob: 871dc316c99cafb5907526f770e1494944517985 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
/*
 *
 * (C) COPYRIGHT 2015 ARM Limited. All rights reserved.
 *
 * This program is free software and is provided to you under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation, and any use by you of this program is subject to the terms
 * of such GNU licence.
 *
 * A copy of the licence is included with the program, and can also be obtained
 * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 *
 */



#include <linux/anon_inodes.h>
#include <linux/atomic.h>
#include <linux/file.h>
#include <linux/mutex.h>
#include <linux/poll.h>
#include <linux/spinlock.h>
#include <linux/string.h>
#include <linux/stringify.h>
#include <linux/timer.h>
#include <linux/wait.h>

#include <mali_kbase.h>
#include <mali_kbase_jm.h>
#include <mali_kbase_tlstream.h>

/*****************************************************************************/

/* The version of timeline stream. */
#define KBASEP_TLSTREAM_VERSION 1

/* The maximum expected length of string in tracepoint descriptor. */
#define STRLEN_MAX         64 /* bytes */

/* The number of nanoseconds in a second. */
#define NSECS_IN_SEC       1000000000ull /* ns */

/* The number of nanoseconds to wait before autoflushing the stream. */
#define AUTOFLUSH_TIMEOUT  (2ull * NSECS_IN_SEC) /* ns */

/* The period of autoflush checker execution in milliseconds. */
#define AUTOFLUSH_INTERVAL 1000 /* ms */

/* The maximum size of a single packet used by timeline. */
#define PACKET_SIZE        2048 /* bytes */

/* The number of packets used by one timeline stream. */
#define PACKET_COUNT       16

/* The number of bytes reserved for packet header.
 * These value must be defined according to MIPE documentation. */
#define PACKET_HEADER_SIZE 8 /* bytes */

/* The number of bytes reserved for packet sequence number.
 * These value must be defined according to MIPE documentation. */
#define PACKET_NUMBER_SIZE 4 /* bytes */

/* Packet header - first word.
 * These values must be defined according to MIPE documentation. */
#define PACKET_STREAMID_POS  0
#define PACKET_STREAMID_LEN  8
#define PACKET_RSVD1_POS     (PACKET_STREAMID_POS + PACKET_STREAMID_LEN)
#define PACKET_RSVD1_LEN     8
#define PACKET_TYPE_POS      (PACKET_RSVD1_POS + PACKET_RSVD1_LEN)
#define PACKET_TYPE_LEN      3
#define PACKET_CLASS_POS     (PACKET_TYPE_POS + PACKET_TYPE_LEN)
#define PACKET_CLASS_LEN     7
#define PACKET_FAMILY_POS    (PACKET_CLASS_POS + PACKET_CLASS_LEN)
#define PACKET_FAMILY_LEN    6

/* Packet header - second word
 * These values must be defined according to MIPE documentation. */
#define PACKET_LENGTH_POS    0
#define PACKET_LENGTH_LEN    24
#define PACKET_SEQBIT_POS    (PACKET_LENGTH_POS + PACKET_LENGTH_LEN)
#define PACKET_SEQBIT_LEN    1
#define PACKET_RSVD2_POS     (PACKET_SEQBIT_POS + PACKET_SEQBIT_LEN)
#define PACKET_RSVD2_LEN     7

/* Types of streams generated by timeline.
 * Order is significant! Header streams must precede respective body streams. */
enum tl_stream_type {
	TL_STREAM_TYPE_OBJ_HEADER,
	TL_STREAM_TYPE_OBJ_SUMMARY,
	TL_STREAM_TYPE_OBJ,
	TL_STREAM_TYPE_AUX_HEADER,
	TL_STREAM_TYPE_AUX,

	TL_STREAM_TYPE_COUNT
};

/* Timeline packet family ids.
 * Values are significant! Check MIPE documentation. */
enum tl_packet_family {
	TL_PACKET_FAMILY_CTRL = 0, /* control packets */
	TL_PACKET_FAMILY_TL   = 1, /* timeline packets */

	TL_PACKET_FAMILY_COUNT
};

/* Packet classes used in timeline streams.
 * Values are significant! Check MIPE documentation. */
enum tl_packet_class {
	TL_PACKET_CLASS_OBJ = 0, /* timeline objects packet */
	TL_PACKET_CLASS_AUX = 1, /* auxiliary events packet */
};

/* Packet types used in timeline streams.
 * Values are significant! Check MIPE documentation. */
enum tl_packet_type {
	TL_PACKET_TYPE_HEADER  = 0, /* stream's header/directory */
	TL_PACKET_TYPE_BODY    = 1, /* stream's body */
	TL_PACKET_TYPE_SUMMARY = 2, /* stream's summary */
};

/* Message ids of trace events that are recorded in the timeline stream. */
enum tl_msg_id {
	/* Timeline object events. */
	KBASE_TL_NEW_CTX,
	KBASE_TL_NEW_GPU,
	KBASE_TL_NEW_LPU,
	KBASE_TL_NEW_ATOM,
	KBASE_TL_DEL_CTX,
	KBASE_TL_DEL_ATOM,
	KBASE_TL_LIFELINK_LPU_GPU,
	KBASE_TL_RET_GPU_CTX,
	KBASE_TL_RET_ATOM_CTX,
	KBASE_TL_RET_ATOM_LPU,
	KBASE_TL_NRET_GPU_CTX,
	KBASE_TL_NRET_ATOM_CTX,
	KBASE_TL_NRET_ATOM_LPU,

	/* Timeline non-object events. */
	KBASE_AUX_PM_STATE,
	KBASE_AUX_JOB_SOFTSTOP,
	KBASE_AUX_PAGEFAULT,
	KBASE_AUX_PAGESALLOC
};

/*****************************************************************************/

/**
 * struct tl_stream - timeline stream structure
 * @lock: message order lock
 * @buffer: array of buffers
 * @wbi: write buffer index
 * @rbi: read buffer index
 * @numbered: if non-zero stream's packets are sequentially numbered
 * @last_write_time: timestamp indicating last write
 *
 * This structure holds information needed to construct proper packets in the
 * timeline stream. Each message in sequence must bear timestamp that is greater
 * to one in previous message in the same stream. For this reason lock is held
 * throughout the process of message creation. Each stream contains set of
 * buffers. Each buffer will hold one MIPE packet. In case there is no free
 * space required to store incoming message the oldest buffer is discarded.
 * Each packet in timeline body stream has sequence number embedded (this value
 * must increment monotonically and is used by packets receiver to discover
 * buffer overflows.
 */
struct tl_stream {
	spinlock_t lock;

	struct {
		atomic_t size;              /* number of bytes in buffer */
		char     data[PACKET_SIZE]; /* buffer's data */
	} buffer[PACKET_COUNT];

	atomic_t wbi;
	atomic_t rbi;

	int      numbered;
	u64      last_write_time;
};

/**
 * struct tp_desc - tracepoint message descriptor structure
 * @id:        tracepoint ID identifying message in stream
 * @id_str:    human readable version of tracepoint ID
 * @name:      tracepoint description
 * @arg_types: tracepoint's arguments types declaration
 * @arg_names: comma separated list of tracepoint's arguments names
 */
struct tp_desc {
	u32        id;
	const char *id_str;
	const char *name;
	const char *arg_types;
	const char *arg_names;
};

/*****************************************************************************/

/* Configuration of timeline streams generated by kernel.
 * Kernel emit only streams containing either timeline object events or
 * auxiliary events. All streams have stream id value of 1 (as opposed to user
 * space streams that have value of 0). */
static const struct {
	enum tl_packet_family pkt_family;
	enum tl_packet_class  pkt_class;
	enum tl_packet_type   pkt_type;
	unsigned int          stream_id;
} tl_stream_cfg[TL_STREAM_TYPE_COUNT] = {
	{TL_PACKET_FAMILY_TL, TL_PACKET_CLASS_OBJ, TL_PACKET_TYPE_HEADER,  1},
	{TL_PACKET_FAMILY_TL, TL_PACKET_CLASS_OBJ, TL_PACKET_TYPE_SUMMARY, 1},
	{TL_PACKET_FAMILY_TL, TL_PACKET_CLASS_OBJ, TL_PACKET_TYPE_BODY,    1},
	{TL_PACKET_FAMILY_TL, TL_PACKET_CLASS_AUX, TL_PACKET_TYPE_HEADER,  1},
	{TL_PACKET_FAMILY_TL, TL_PACKET_CLASS_AUX, TL_PACKET_TYPE_BODY,    1}
};

/* The timeline streams generated by kernel. */
static struct tl_stream *tl_stream[TL_STREAM_TYPE_COUNT];

/* Autoflush timer. */
static struct timer_list autoflush_timer;

/* If non-zero autoflush timer is active. */
static atomic_t autoflush_timer_active;

/* Reader lock. Only one reader is allowed to have access to the timeline
 * streams at any given time. */
static DEFINE_MUTEX(tl_reader_lock);

/* Indicator of whether the timeline stream file descriptor is already used. */
static atomic_t tlstream_busy = {0};

/* Timeline stream event queue. */
static DECLARE_WAIT_QUEUE_HEAD(tl_event_queue);

/* The timeline stream file operations functions. */
static ssize_t kbasep_tlstream_read(
		struct file *filp,
		char __user *buffer,
		size_t      size,
		loff_t      *f_pos);
static unsigned int kbasep_tlstream_poll(struct file *filp, poll_table *wait);
static int kbasep_tlstream_release(struct inode *inode, struct file *filp);

/* The timeline stream file operations structure. */
static const struct file_operations kbasep_tlstream_fops = {
	.release = kbasep_tlstream_release,
	.read    = kbasep_tlstream_read,
	.poll    = kbasep_tlstream_poll,
};

/* Descriptors of timeline messages transmitted in object events stream. */
static const struct tp_desc tp_desc_obj[] = {
	{
		KBASE_TL_NEW_CTX,
		__stringify(KBASE_TL_NEW_CTX),
		"object ctx is created",
		"@pI",
		"ctx,ctx_nr"
	},
	{
		KBASE_TL_NEW_GPU,
		__stringify(KBASE_TL_NEW_GPU),
		"object gpu is created",
		"@pII",
		"gpu,gpu_id,core_count"
	},
	{
		KBASE_TL_NEW_LPU,
		__stringify(KBASE_TL_NEW_LPU),
		"object lpu is created",
		"@pII",
		"lpu,lpu_nr,lpu_fn"
	},
	{
		KBASE_TL_NEW_ATOM,
		__stringify(KBASE_TL_NEW_ATOM),
		"object atom is created",
		"@pI",
		"atom,atom_nr"
	},
	{
		KBASE_TL_DEL_CTX,
		__stringify(KBASE_TL_DEL_CTX),
		"context is destroyed",
		"@p",
		"context"
	},
	{
		KBASE_TL_DEL_ATOM,
		__stringify(KBASE_TL_DEL_ATOM),
		"atom is destroyed",
		"@p",
		"atom"
	},
	{
		KBASE_TL_LIFELINK_LPU_GPU,
		__stringify(KBASE_TL_LIFELINK_LPU_GPU),
		"lpu is deleted with gpu",
		"@pp",
		"lpu,gpu"
	},
	{
		KBASE_TL_RET_GPU_CTX,
		__stringify(KBASE_TL_RET_GPU_CTX),
		"gpu is retained by context",
		"@pp",
		"gpu,ctx"
	},
	{
		KBASE_TL_RET_ATOM_CTX,
		__stringify(KBASE_TL_RET_ATOM_CTX),
		"atom is retained by context",
		"@pp",
		"atom,ctx"
	},
	{
		KBASE_TL_RET_ATOM_LPU,
		__stringify(KBASE_TL_RET_ATOM_LPU),
		"atom is retained by lpu",
		"@pp",
		"atom,lpu"
	},
	{
		KBASE_TL_NRET_GPU_CTX,
		__stringify(KBASE_TL_NRET_GPU_CTX),
		"gpu is released by context",
		"@pp",
		"gpu,ctx"
	},
	{
		KBASE_TL_NRET_ATOM_CTX,
		__stringify(KBASE_TL_NRET_ATOM_CTX),
		"atom is released by context",
		"@pp",
		"atom,context"
	},
	{
		KBASE_TL_NRET_ATOM_LPU,
		__stringify(KBASE_TL_NRET_ATOM_LPU),
		"atom is released by lpu",
		"@pp",
		"atom,lpu"
	},
};

/* Descriptors of timeline messages transmitted in auxiliary events stream. */
static const struct tp_desc tp_desc_aux[] = {
	{
		KBASE_AUX_PM_STATE,
		__stringify(KBASE_AUX_PM_STATE),
		"PM state",
		"@IL",
		"core_type,core_state_bitset"
	},
	{
		KBASE_AUX_JOB_SOFTSTOP,
		__stringify(KBASE_AUX_JOB_SOFTSTOP),
		"Job soft stop",
		"@I",
		"tag_id"
	},
	{
		KBASE_AUX_PAGEFAULT,
		__stringify(KBASE_AUX_PAGEFAULT),
		"Page fault",
		"@II",
		"as_id,page_cnt"
	},
	{
		KBASE_AUX_PAGESALLOC,
		__stringify(KBASE_AUX_PAGESALLOC),
		"Total alloc pages change",
		"@l",
		"page_cnt_change"
	}
};

#if MALI_UNIT_TEST
/* Number of bytes read by user. */
static atomic_t tlstream_bytes_collected = {0};

/* Number of bytes generated by tracepoint messages. */
static atomic_t tlstream_bytes_generated = {0};
#endif /* MALI_UNIT_TEST */

/*****************************************************************************/

/**
 * kbasep_tlstream_get_timestamp - return timestamp
 *
 * Function returns timestamp value based on raw monotonic timer. Value will
 * wrap around zero in case of overflow.
 * Return: timestamp value
 */
static u64 kbasep_tlstream_get_timestamp(void)
{
	struct timespec ts;
	u64             timestamp;

	getrawmonotonic(&ts);
	timestamp = (u64)ts.tv_sec * NSECS_IN_SEC + ts.tv_nsec;
	return timestamp;
}

/**
 * kbasep_tlstream_write_bytes - write data to message buffer
 * @buffer: buffer where data will be written
 * @pos:    position in the buffer where to place data
 * @bytes:  pointer to buffer holding data
 * @len:    length of data to be written
 *
 * Return: updated position in the buffer
 */
static size_t kbasep_tlstream_write_bytes(
		char       *buffer,
		size_t     pos,
		const void *bytes,
		size_t     len)
{
	KBASE_DEBUG_ASSERT(buffer);
	KBASE_DEBUG_ASSERT(bytes);

	memcpy(&buffer[pos], bytes, len);

	return pos + len;
}

/**
 * kbasep_tlstream_write_string - write string to message buffer
 * @buffer:         buffer where data will be written
 * @pos:            position in the buffer where to place data
 * @string:         pointer to buffer holding the source string
 * @max_write_size: number of bytes that can be stored in buffer
 *
 * Return: updated position in the buffer
 */
static size_t kbasep_tlstream_write_string(
		char       *buffer,
		size_t     pos,
		const char *string,
		size_t     max_write_size)
{
	u32 string_len;

	KBASE_DEBUG_ASSERT(buffer);
	KBASE_DEBUG_ASSERT(string);
	/* Timeline string consists of at least string length and nul
	 * terminator. */
	KBASE_DEBUG_ASSERT(max_write_size >= sizeof(string_len) + sizeof(char));
	max_write_size -= sizeof(string_len);

	string_len = strlcpy(
			&buffer[pos + sizeof(string_len)],
			string,
			max_write_size);
	string_len += sizeof(char);

	/* Make sure that the source string fit into the buffer. */
	KBASE_DEBUG_ASSERT(string_len <= max_write_size);

	/* Update string length. */
	memcpy(&buffer[pos], &string_len, sizeof(string_len));

	return pos + sizeof(string_len) + string_len;
}

/**
 * kbasep_tlstream_write_timestamp - write timestamp to message buffer
 * @buffer: buffer where data will be written
 * @pos:    position in the buffer where to place data
 *
 * Return: updated position in the buffer
 */
static size_t kbasep_tlstream_write_timestamp(void *buffer, size_t pos)
{
	u64 timestamp = kbasep_tlstream_get_timestamp();

	return kbasep_tlstream_write_bytes(
			buffer, pos,
			&timestamp, sizeof(timestamp));
}

/**
 * kbasep_tlstream_put_bits - put bits in a word
 * @word:   pointer to the words being modified
 * @value:  value that shall be written to given position
 * @bitpos: position where value shall be written (in bits)
 * @bitlen: length of value (in bits)
 */
static void kbasep_tlstream_put_bits(
		u32          *word,
		u32          value,
		unsigned int bitpos,
		unsigned int bitlen)
{
	const u32 mask = ((1 << bitlen) - 1) << bitpos;

	KBASE_DEBUG_ASSERT(word);
	KBASE_DEBUG_ASSERT((0 != bitlen) && (32 >= bitlen));
	KBASE_DEBUG_ASSERT((bitpos + bitlen) <= 32);

	*word &= ~mask;
	*word |= ((value << bitpos) & mask);
}

/**
 * kbasep_tlstream_packet_header_setup - setup the packet header
 * @buffer:     pointer to the buffer
 * @pkt_family: packet's family
 * @pkt_type:   packet's type
 * @pkt_class:  packet's class
 * @stream_id:  stream id
 * @numbered:   non-zero if this stream is numbered
 *
 * Function sets up immutable part of packet header in the given buffer.
 */
static void kbasep_tlstream_packet_header_setup(
		char                  *buffer,
		enum tl_packet_family pkt_family,
		enum tl_packet_class  pkt_class,
		enum tl_packet_type   pkt_type,
		unsigned int          stream_id,
		int                   numbered)
{
	u32 word0 = 0;
	u32 word1 = 0;

	KBASE_DEBUG_ASSERT(buffer);
	KBASE_DEBUG_ASSERT(pkt_family == TL_PACKET_FAMILY_TL);
	KBASE_DEBUG_ASSERT(
			(pkt_type == TL_PACKET_TYPE_HEADER)  ||
			(pkt_type == TL_PACKET_TYPE_SUMMARY) ||
			(pkt_type == TL_PACKET_TYPE_BODY));
	KBASE_DEBUG_ASSERT(
			(pkt_class == TL_PACKET_CLASS_OBJ) ||
			(pkt_class == TL_PACKET_CLASS_AUX));

	kbasep_tlstream_put_bits(
			&word0, pkt_family,
			PACKET_FAMILY_POS, PACKET_FAMILY_LEN);
	kbasep_tlstream_put_bits(
			&word0, pkt_class,
			PACKET_CLASS_POS, PACKET_CLASS_LEN);
	kbasep_tlstream_put_bits(
			&word0, pkt_type,
			PACKET_TYPE_POS, PACKET_TYPE_LEN);
	kbasep_tlstream_put_bits(
			&word0, stream_id,
			PACKET_STREAMID_POS, PACKET_STREAMID_LEN);

	if (numbered)
		kbasep_tlstream_put_bits(
				&word1, 1,
				PACKET_SEQBIT_POS, PACKET_SEQBIT_LEN);

	memcpy(&buffer[0],             &word0, sizeof(word0));
	memcpy(&buffer[sizeof(word0)], &word1, sizeof(word1));
}

/**
 * kbasep_tlstream_packet_header_update - update the packet header
 * @buffer:    pointer to the buffer
 * @data_size: amount of data carried in this packet
 *
 * Function updates mutable part of packet header in the given buffer.
 * Note that value of data_size must not including size of the header.
 */
static void kbasep_tlstream_packet_header_update(
		char   *buffer,
		size_t data_size)
{
	u32 word0;
	u32 word1;

	KBASE_DEBUG_ASSERT(buffer);
	CSTD_UNUSED(word0);

	memcpy(&word1, &buffer[sizeof(word0)], sizeof(word1));

	kbasep_tlstream_put_bits(
			&word1, data_size,
			PACKET_LENGTH_POS, PACKET_LENGTH_LEN);

	memcpy(&buffer[sizeof(word0)], &word1, sizeof(word1));
}

/**
 * kbasep_tlstream_packet_number_update - update the packet number
 * @buffer:  pointer to the buffer
 * @counter: value of packet counter for this packet's stream
 *
 * Function updates packet number embedded within the packet placed in the
 * given buffer.
 */
static void kbasep_tlstream_packet_number_update(char *buffer, u32 counter)
{
	KBASE_DEBUG_ASSERT(buffer);

	memcpy(&buffer[PACKET_HEADER_SIZE], &counter, sizeof(counter));
}

/**
 * kbasep_timeline_stream_reset - reset stream
 * @stream:  pointer to the stream structure
 *
 * Function discards all pending messages and resets packet counters.
 */
static void kbasep_timeline_stream_reset(struct tl_stream *stream)
{
	unsigned int i;

	for (i = 0; i < PACKET_COUNT; i++) {
		if (stream->numbered)
			atomic_set(
					&stream->buffer[i].size,
					PACKET_HEADER_SIZE +
					PACKET_NUMBER_SIZE);
		else
			atomic_set(&stream->buffer[i].size, PACKET_HEADER_SIZE);
	}

	atomic_set(&stream->wbi, 0);
	atomic_set(&stream->rbi, 0);
}

/**
 * kbasep_timeline_stream_init - initialize timeline stream
 * @stream:      pointer to the stream structure
 * @stream_type: stream type
 */
static void kbasep_timeline_stream_init(
		struct tl_stream    *stream,
		enum tl_stream_type stream_type)
{
	unsigned int i;

	KBASE_DEBUG_ASSERT(stream);
	KBASE_DEBUG_ASSERT(TL_STREAM_TYPE_COUNT > stream_type);

	spin_lock_init(&stream->lock);

	/* All packets carrying tracepoints shall be numbered. */
	if (TL_PACKET_TYPE_BODY == tl_stream_cfg[stream_type].pkt_type)
		stream->numbered = 1;
	else
		stream->numbered = 0;

	for (i = 0; i < PACKET_COUNT; i++)
		kbasep_tlstream_packet_header_setup(
				stream->buffer[i].data,
				tl_stream_cfg[stream_type].pkt_family,
				tl_stream_cfg[stream_type].pkt_class,
				tl_stream_cfg[stream_type].pkt_type,
				tl_stream_cfg[stream_type].stream_id,
				stream->numbered);

	kbasep_timeline_stream_reset(tl_stream[stream_type]);
}

/**
 * kbasep_timeline_stream_term - terminate timeline stream
 * @stream: pointer to the stream structure
 */
static void kbasep_timeline_stream_term(struct tl_stream *stream)
{
	KBASE_DEBUG_ASSERT(stream);
}

/**
 * kbasep_tlstream_msgbuf_submit - submit packet to the user space
 * @stream:     pointer to the stream structure
 * @wb_idx_raw: write buffer index
 * @wb_size:    length of data stored in current buffer
 *
 * Function updates currently written buffer with packet header. Then write
 * index is incremented and buffer is handled to user space. Parameters
 * of new buffer are returned using provided arguments.
 *
 * Return: length of data in new buffer
 *
 * Warning:  User must update the stream structure with returned value.
 */
static size_t kbasep_tlstream_msgbuf_submit(
		struct tl_stream *stream,
		unsigned int      wb_idx_raw,
		unsigned int      wb_size)
{
	unsigned int rb_idx_raw = atomic_read(&stream->rbi);
	unsigned int wb_idx = wb_idx_raw % PACKET_COUNT;

	kbasep_tlstream_packet_header_update(
			stream->buffer[wb_idx].data,
			wb_size - PACKET_HEADER_SIZE);

	if (stream->numbered)
		kbasep_tlstream_packet_number_update(
				stream->buffer[wb_idx].data,
				wb_idx_raw);

	/* Increasing write buffer index will expose this packet to the reader.
	 * As stream->lock is not taken on reader side we must make sure memory
	 * is updated correctly before this will happen. */
	smp_wmb();
	wb_idx_raw++;
	atomic_set(&stream->wbi, wb_idx_raw);

	/* Inform user that packets are ready for reading. */
	wake_up_interruptible(&tl_event_queue);

	/* Detect and mark overflow in this stream. */
	if (PACKET_COUNT == wb_idx_raw - rb_idx_raw) {
		/* Reader side depends on this increment to correctly handle
		 * overflows. The value shall be updated only if it was not
		 * modified by the reader. The data holding buffer will not be
		 * updated before stream->lock is released, however size of the
		 * buffer will. Make sure this increment is globally visible
		 * before information about selected write buffer size. */
		atomic_cmpxchg(&stream->rbi, rb_idx_raw, rb_idx_raw + 1);
	}

	wb_size = PACKET_HEADER_SIZE;
	if (stream->numbered)
		wb_size += PACKET_NUMBER_SIZE;

	return wb_size;
}

/**
 * kbasep_tlstream_msgbuf_acquire - lock selected stream and reserves buffer
 * @stream_type: type of the stream that shall be locked
 * @msg_size:    message size
 * @flags:       pointer to store flags passed back on stream release
 *
 * Function will lock the stream and reserve the number of bytes requested
 * in msg_size for the user.
 *
 * Return: pointer to the buffer where message can be stored
 *
 * Warning: Stream must be relased with kbasep_tlstream_msgbuf_release().
 *          Only atomic operations are allowed while stream is locked
 *          (i.e. do not use any operation that may sleep).
 */
static char *kbasep_tlstream_msgbuf_acquire(
		enum tl_stream_type stream_type,
		size_t              msg_size,
		unsigned long       *flags)
{
	struct tl_stream *stream;
	unsigned int     wb_idx_raw;
	unsigned int     wb_idx;
	size_t           wb_size;

	KBASE_DEBUG_ASSERT(TL_STREAM_TYPE_COUNT > stream_type);
	KBASE_DEBUG_ASSERT(
			PACKET_SIZE - PACKET_HEADER_SIZE - PACKET_NUMBER_SIZE >=
			msg_size);

	stream = tl_stream[stream_type];

	spin_lock_irqsave(&stream->lock, *flags);

	wb_idx_raw = atomic_read(&stream->wbi);
	wb_idx     = wb_idx_raw % PACKET_COUNT;
	wb_size    = atomic_read(&stream->buffer[wb_idx].size);

	/* Select next buffer if data will not fit into current one. */
	if (PACKET_SIZE < wb_size + msg_size) {
		wb_size = kbasep_tlstream_msgbuf_submit(
				stream, wb_idx_raw, wb_size);
		wb_idx  = (wb_idx_raw + 1) % PACKET_COUNT;
	}

	/* Reserve space in selected buffer. */
	atomic_set(&stream->buffer[wb_idx].size, wb_size + msg_size);

#if MALI_UNIT_TEST
	atomic_add(msg_size, &tlstream_bytes_generated);
#endif /* MALI_UNIT_TEST */

	return &stream->buffer[wb_idx].data[wb_size];
}

/**
 * kbasep_tlstream_msgbuf_release - unlock selected stream
 * @stream_type:  type of the stream that shall be locked
 * @flags:        value obtained during stream acquire
 *
 * Function releases stream that has been previously locked with a call to
 * kbasep_tlstream_msgbuf_acquire().
 */
static void kbasep_tlstream_msgbuf_release(
		enum tl_stream_type stream_type,
		unsigned long       flags)
{
	struct tl_stream *stream;

	KBASE_DEBUG_ASSERT(TL_STREAM_TYPE_COUNT > stream_type);

	stream = tl_stream[stream_type];
	stream->last_write_time = kbasep_tlstream_get_timestamp();

	spin_unlock_irqrestore(&stream->lock, flags);
}

/*****************************************************************************/

/**
 * kbasep_tlstream_flush_stream - flush stream
 * @stype:  type of stream to be flushed
 *
 * Flush pending data in timeline stream.
 */
static void kbasep_tlstream_flush_stream(enum tl_stream_type stype)
{
	struct tl_stream *stream = tl_stream[stype];
	unsigned long    flags;
	unsigned int     wb_idx_raw;
	unsigned int     wb_idx;
	size_t           wb_size;
	size_t           min_size = PACKET_HEADER_SIZE;

	if (stream->numbered)
		min_size += PACKET_NUMBER_SIZE;

	spin_lock_irqsave(&stream->lock, flags);

	wb_idx_raw = atomic_read(&stream->wbi);
	wb_idx     = wb_idx_raw % PACKET_COUNT;
	wb_size    = atomic_read(&stream->buffer[wb_idx].size);

	if (wb_size > min_size) {
		wb_size = kbasep_tlstream_msgbuf_submit(
				stream, wb_idx_raw, wb_size);
		wb_idx = (wb_idx_raw + 1) % PACKET_COUNT;
		atomic_set(&stream->buffer[wb_idx].size, wb_size);
	}
	spin_unlock_irqrestore(&stream->lock, flags);
}

/**
 * kbasep_tlstream_autoflush_timer_callback - autoflush timer callback
 * @data:  unused
 *
 * Timer is executed periodically to check if any of the stream contains
 * buffer ready to be submitted to user space.
 */
static void kbasep_tlstream_autoflush_timer_callback(unsigned long data)
{
	u64                 timestamp = kbasep_tlstream_get_timestamp();
	enum tl_stream_type stype;
	int                 rcode;

	CSTD_UNUSED(data);

	for (stype = 0; stype < TL_STREAM_TYPE_COUNT; stype++) {
		struct tl_stream *stream = tl_stream[stype];
		unsigned long    flags;
		unsigned int     wb_idx_raw;
		unsigned int     wb_idx;
		size_t           wb_size;
		size_t           min_size = PACKET_HEADER_SIZE;

		if (stream->numbered)
			min_size += PACKET_NUMBER_SIZE;

		spin_lock_irqsave(&stream->lock, flags);

		wb_idx_raw = atomic_read(&stream->wbi);
		wb_idx     = wb_idx_raw % PACKET_COUNT;
		wb_size    = atomic_read(&stream->buffer[wb_idx].size);

		if (
				(wb_size > min_size) &&
				(
				 timestamp - stream->last_write_time >
				 AUTOFLUSH_TIMEOUT)) {

			wb_size = kbasep_tlstream_msgbuf_submit(
					stream, wb_idx_raw, wb_size);
			wb_idx = (wb_idx_raw + 1) % PACKET_COUNT;
			atomic_set(&stream->buffer[wb_idx].size, wb_size);
		}
		spin_unlock_irqrestore(&stream->lock, flags);
	}

	if (atomic_read(&autoflush_timer_active))
		rcode = mod_timer(
				&autoflush_timer,
				jiffies + msecs_to_jiffies(AUTOFLUSH_INTERVAL));
	CSTD_UNUSED(rcode);
}

/**
 * kbasep_tlstream_packet_pending - check timeline streams for pending packets
 * @stype:      pointer to variable where stream type will be placed
 * @rb_idx_raw: pointer to variable where read buffer index will be placed
 *
 * Function checks all streams for pending packets. It will stop as soon as
 * packet ready to be submitted to user space is detected. Variables under
 * pointers, passed as the parameters to this function will be updated with
 * values pointing to right stream and buffer.
 *
 * Return: non-zero if any of timeline streams has at last one packet ready
 */
static int kbasep_tlstream_packet_pending(
		enum tl_stream_type *stype,
		unsigned int        *rb_idx_raw)
{
	int pending = 0;

	KBASE_DEBUG_ASSERT(stype);
	KBASE_DEBUG_ASSERT(rb_idx_raw);

	for (
			*stype = 0;
			(*stype < TL_STREAM_TYPE_COUNT) && !pending;
			(*stype)++) {
		if (NULL != tl_stream[*stype]) {
			*rb_idx_raw = atomic_read(&tl_stream[*stype]->rbi);
			/* Read buffer index may be updated by writer in case of
			 * overflow. Read and write buffer indexes must be
			 * loaded in correct order. */
			smp_rmb();
			if (atomic_read(&tl_stream[*stype]->wbi) != *rb_idx_raw)
				pending = 1;
		}
	}
	(*stype)--;

	return pending;
}

/**
 * kbasep_tlstream_read - copy data from streams to buffer provided by user
 * @filp:   pointer to file structure (unused)
 * @buffer: pointer to the buffer provided by user
 * @size:   maximum amount of data that can be stored in the buffer
 * @f_pos:  pointer to file offset (unused)
 *
 * Return: number of bytes stored in the buffer
 */
static ssize_t kbasep_tlstream_read(
		struct file *filp,
		char __user *buffer,
		size_t      size,
		loff_t      *f_pos)
{
	ssize_t copy_len = 0;

	KBASE_DEBUG_ASSERT(filp);
	KBASE_DEBUG_ASSERT(buffer);
	KBASE_DEBUG_ASSERT(f_pos);
	CSTD_UNUSED(filp);

	if ((0 > *f_pos) || (PACKET_SIZE > size))
		return -EINVAL;

	mutex_lock(&tl_reader_lock);

	while (copy_len < size) {
		enum tl_stream_type stype;
		unsigned int        rb_idx_raw;
		unsigned int        rb_idx;
		size_t              rb_size;

		/* If we don't have any data yet, wait for packet to be
		 * submitted. If we already read some packets and there is no
		 * packet pending return back to user. */
		if (0 < copy_len) {
			if (!kbasep_tlstream_packet_pending(
						&stype,
						&rb_idx_raw))
				break;
		} else {
			if (wait_event_interruptible(
						tl_event_queue,
						kbasep_tlstream_packet_pending(
							&stype,
							&rb_idx_raw))) {
				copy_len = -ERESTARTSYS;
				break;
			}
		}

		/* Check if this packet fits into the user buffer.
		 * If so copy its content. */
		rb_idx = rb_idx_raw % PACKET_COUNT;
		rb_size = atomic_read(&tl_stream[stype]->buffer[rb_idx].size);
		if (rb_size > size - copy_len)
			break;
		if (copy_to_user(
					&buffer[copy_len],
					tl_stream[stype]->buffer[rb_idx].data,
					rb_size)) {
			copy_len = -EFAULT;
			break;
		}

		/* Verify if there was no overflow in selected stream. Make sure
		 * that if incorrect size was used we will know about it. */
		smp_rmb();
		if (atomic_read(&tl_stream[stype]->rbi) == rb_idx_raw) {
			copy_len += rb_size;
			atomic_inc(&tl_stream[stype]->rbi);

#if MALI_UNIT_TEST
			atomic_add(rb_size, &tlstream_bytes_collected);
#endif /* MALI_UNIT_TEST */
		}
	}

	mutex_unlock(&tl_reader_lock);

	return copy_len;
}

/**
 * kbasep_tlstream_poll - poll timeline stream for packets
 * @filp: pointer to file structure
 * @wait: pointer to poll table
 * Return: POLLIN if data can be read without blocking, otherwise zero
 */
static unsigned int kbasep_tlstream_poll(struct file *filp, poll_table *wait)
{
	enum tl_stream_type stream_type;
	unsigned int        rb_idx;

	KBASE_DEBUG_ASSERT(filp);
	KBASE_DEBUG_ASSERT(wait);

	poll_wait(filp, &tl_event_queue, wait);
	if (kbasep_tlstream_packet_pending(&stream_type, &rb_idx))
		return POLLIN;
	return 0;
}

/**
 * kbasep_tlstream_release - release timeline stream descriptor
 * @inode: pointer to inode structure
 * @filp:  pointer to file structure
 *
 * Return always return zero
 */
static int kbasep_tlstream_release(struct inode *inode, struct file *filp)
{
	KBASE_DEBUG_ASSERT(inode);
	KBASE_DEBUG_ASSERT(filp);
	CSTD_UNUSED(inode);
	CSTD_UNUSED(filp);
	atomic_set(&tlstream_busy, 0);
	return 0;
}

/**
 * kbasep_tlstream_timeline_header - prepare timeline header stream packet
 * @stream_type: type of the stream that will carry header data
 * @tp_desc:     pointer to array with tracepoint descriptors
 * @tp_count:    number of descriptors in the given array
 *
 * Functions fills in information about tracepoints stored in body stream
 * associated with this header stream.
 */
static void kbasep_tlstream_timeline_header(
		enum tl_stream_type  stream_type,
		const struct tp_desc *tp_desc,
		u32                  tp_count)
{
	const u8      tv = KBASEP_TLSTREAM_VERSION; /* tlstream version */
	const u8      ps = sizeof(void *); /* pointer size */
	size_t        msg_size = sizeof(tv) + sizeof(ps) + sizeof(tp_count);
	char          *buffer;
	size_t        pos = 0;
	unsigned long flags;
	unsigned int  i;

	KBASE_DEBUG_ASSERT(TL_STREAM_TYPE_COUNT > stream_type);
	KBASE_DEBUG_ASSERT(tp_desc);

	/* Calculate the size of the timeline message. */
	for (i = 0; i < tp_count; i++) {
		msg_size += sizeof(tp_desc[i].id);
		msg_size +=
			strnlen(tp_desc[i].id_str,    STRLEN_MAX) +
			sizeof(char) + sizeof(u32);
		msg_size +=
			strnlen(tp_desc[i].name,      STRLEN_MAX) +
			sizeof(char) + sizeof(u32);
		msg_size +=
			strnlen(tp_desc[i].arg_types, STRLEN_MAX) +
			sizeof(char) + sizeof(u32);
		msg_size +=
			strnlen(tp_desc[i].arg_names, STRLEN_MAX) +
			sizeof(char) + sizeof(u32);
	}

	KBASE_DEBUG_ASSERT(PACKET_SIZE - PACKET_HEADER_SIZE >= msg_size);

	buffer = kbasep_tlstream_msgbuf_acquire(stream_type, msg_size, &flags);
	KBASE_DEBUG_ASSERT(buffer);

	pos = kbasep_tlstream_write_bytes(buffer, pos, &tv, sizeof(tv));
	pos = kbasep_tlstream_write_bytes(buffer, pos, &ps, sizeof(ps));
	pos = kbasep_tlstream_write_bytes(
			buffer, pos, &tp_count, sizeof(tp_count));

	for (i = 0; i < tp_count; i++) {
		pos = kbasep_tlstream_write_bytes(
				buffer, pos,
				&tp_desc[i].id, sizeof(tp_desc[i].id));
		pos = kbasep_tlstream_write_string(
				buffer, pos,
				tp_desc[i].id_str, msg_size - pos);
		pos = kbasep_tlstream_write_string(
				buffer, pos,
				tp_desc[i].name, msg_size - pos);
		pos = kbasep_tlstream_write_string(
				buffer, pos,
				tp_desc[i].arg_types, msg_size - pos);
		pos = kbasep_tlstream_write_string(
				buffer, pos,
				tp_desc[i].arg_names, msg_size - pos);
	}

	KBASE_DEBUG_ASSERT(msg_size == pos);

	kbasep_tlstream_msgbuf_release(stream_type, flags);

	/* We don't expect any more data to be read in this stream.
	 * As header stream must be read before its associated body stream,
	 * make this packet visible to the user straightaway. */
	kbasep_tlstream_flush_stream(stream_type);
}

/*****************************************************************************/

int kbase_tlstream_init(void)
{
	enum tl_stream_type i;
	int                 rcode;

	/* Prepare stream structures. */
	for (i = 0; i < TL_STREAM_TYPE_COUNT; i++) {
		tl_stream[i] = kmalloc(sizeof(**tl_stream), GFP_KERNEL);
		if (!tl_stream[i])
			break;
		kbasep_timeline_stream_init(tl_stream[i], i);
	}
	if (TL_STREAM_TYPE_COUNT > i) {
		for (; i > 0; i--) {
			kbasep_timeline_stream_term(tl_stream[i - 1]);
			kfree(tl_stream[i - 1]);
		}
		return -ENOMEM;
	}

	/* Initialize autoflush timer. */
	atomic_set(&autoflush_timer_active, 1);
	setup_timer(&autoflush_timer,
			kbasep_tlstream_autoflush_timer_callback,
			0);
	rcode = mod_timer(
			&autoflush_timer,
			jiffies + msecs_to_jiffies(AUTOFLUSH_INTERVAL));
	CSTD_UNUSED(rcode);

	return 0;
}

void kbase_tlstream_term(void)
{
	enum tl_stream_type i;

	atomic_set(&autoflush_timer_active, 0);
	del_timer_sync(&autoflush_timer);

	for (i = 0; i < TL_STREAM_TYPE_COUNT; i++) {
		kbasep_timeline_stream_term(tl_stream[i]);
		kfree(tl_stream[i]);
	}
}

int kbase_tlstream_acquire(struct kbase_context *kctx, int *fd)
{
	if (0 == atomic_cmpxchg(&tlstream_busy, 0, 1)) {
		*fd = anon_inode_getfd(
				"[mali_tlstream]",
				&kbasep_tlstream_fops,
				kctx,
				O_RDONLY | O_CLOEXEC);
		if (0 > *fd) {
			atomic_set(&tlstream_busy, 0);
			return *fd;
		}

		/* Reset and initialize header streams. */
		kbasep_timeline_stream_reset(
				tl_stream[TL_STREAM_TYPE_OBJ_HEADER]);
		kbasep_timeline_stream_reset(
				tl_stream[TL_STREAM_TYPE_OBJ_SUMMARY]);
		kbasep_timeline_stream_reset(
				tl_stream[TL_STREAM_TYPE_AUX_HEADER]);
		kbasep_tlstream_timeline_header(
				TL_STREAM_TYPE_OBJ_HEADER,
				tp_desc_obj,
				ARRAY_SIZE(tp_desc_obj));
		kbasep_tlstream_timeline_header(
				TL_STREAM_TYPE_AUX_HEADER,
				tp_desc_aux,
				ARRAY_SIZE(tp_desc_aux));
	} else {
		*fd = -EBUSY;
	}

	return 0;
}

void kbase_tlstream_flush_streams(void)
{
	enum tl_stream_type stype;

	for (stype = 0; stype < TL_STREAM_TYPE_COUNT; stype++)
		kbasep_tlstream_flush_stream(stype);
}

void kbase_tlstream_reset_body_streams(void)
{
	kbasep_timeline_stream_reset(
			tl_stream[TL_STREAM_TYPE_OBJ]);
	kbasep_timeline_stream_reset(
			tl_stream[TL_STREAM_TYPE_AUX]);
}

#if MALI_UNIT_TEST
void kbase_tlstream_stats(u32 *bytes_collected, u32 *bytes_generated)
{
	KBASE_DEBUG_ASSERT(bytes_collected);
	KBASE_DEBUG_ASSERT(bytes_generated);
	*bytes_collected = atomic_read(&tlstream_bytes_collected);
	*bytes_generated = atomic_read(&tlstream_bytes_generated);
}
#endif /* MALI_UNIT_TEST */

/*****************************************************************************/

void kbase_tlstream_tl_summary_new_ctx(void *context, u32 nr)
{
	const u32     msg_id = KBASE_TL_NEW_CTX;
	const size_t  msg_size =
		sizeof(msg_id) + sizeof(u64) + sizeof(context) + sizeof(nr);
	unsigned long flags;
	char          *buffer;
	size_t        pos = 0;

	buffer = kbasep_tlstream_msgbuf_acquire(
			TL_STREAM_TYPE_OBJ_SUMMARY,
			msg_size, &flags);
	KBASE_DEBUG_ASSERT(buffer);

	pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
	pos = kbasep_tlstream_write_timestamp(buffer, pos);
	pos = kbasep_tlstream_write_bytes(
			buffer, pos, &context, sizeof(context));
	pos = kbasep_tlstream_write_bytes(
			buffer, pos, &nr, sizeof(nr));
	KBASE_DEBUG_ASSERT(msg_size == pos);

	kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ_SUMMARY, flags);
}

void kbase_tlstream_tl_summary_new_gpu(void *gpu, u32 id, u32 core_count)
{
	const u32     msg_id = KBASE_TL_NEW_GPU;
	const size_t  msg_size =
		sizeof(msg_id) + sizeof(u64) + sizeof(gpu) + sizeof(id) +
		sizeof(core_count);
	unsigned long flags;
	char          *buffer;
	size_t        pos = 0;

	buffer = kbasep_tlstream_msgbuf_acquire(
			TL_STREAM_TYPE_OBJ_SUMMARY,
			msg_size, &flags);
	KBASE_DEBUG_ASSERT(buffer);

	pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
	pos = kbasep_tlstream_write_timestamp(buffer, pos);
	pos = kbasep_tlstream_write_bytes(
			buffer, pos, &gpu, sizeof(gpu));
	pos = kbasep_tlstream_write_bytes(
			buffer, pos, &id, sizeof(id));
	pos = kbasep_tlstream_write_bytes(
			buffer, pos, &core_count, sizeof(core_count));
	KBASE_DEBUG_ASSERT(msg_size == pos);

	kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ_SUMMARY, flags);
}

void kbase_tlstream_tl_summary_new_lpu(void *lpu, u32 nr, u32 fn)
{
	const u32     msg_id = KBASE_TL_NEW_LPU;
	const size_t  msg_size =
		sizeof(msg_id) + sizeof(u64) + sizeof(lpu) + sizeof(nr) +
		sizeof(fn);
	unsigned long flags;
	char          *buffer;
	size_t        pos = 0;

	buffer = kbasep_tlstream_msgbuf_acquire(
			TL_STREAM_TYPE_OBJ_SUMMARY,
			msg_size, &flags);
	KBASE_DEBUG_ASSERT(buffer);

	pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
	pos = kbasep_tlstream_write_timestamp(buffer, pos);
	pos = kbasep_tlstream_write_bytes(
			buffer, pos, &lpu, sizeof(lpu));
	pos = kbasep_tlstream_write_bytes(
			buffer, pos, &nr, sizeof(nr));
	pos = kbasep_tlstream_write_bytes(
			buffer, pos, &fn, sizeof(fn));
	KBASE_DEBUG_ASSERT(msg_size == pos);

	kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ_SUMMARY, flags);
}

void kbase_tlstream_tl_summary_lifelink_lpu_gpu(void *lpu, void *gpu)
{
	const u32     msg_id = KBASE_TL_LIFELINK_LPU_GPU;
	const size_t  msg_size =
		sizeof(msg_id) + sizeof(u64) + sizeof(lpu) + sizeof(gpu);
	unsigned long flags;
	char          *buffer;
	size_t        pos = 0;

	buffer = kbasep_tlstream_msgbuf_acquire(
			TL_STREAM_TYPE_OBJ_SUMMARY,
			msg_size, &flags);
	KBASE_DEBUG_ASSERT(buffer);

	pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
	pos = kbasep_tlstream_write_timestamp(buffer, pos);
	pos = kbasep_tlstream_write_bytes(
			buffer, pos, &lpu, sizeof(lpu));
	pos = kbasep_tlstream_write_bytes(
			buffer, pos, &gpu, sizeof(gpu));
	KBASE_DEBUG_ASSERT(msg_size == pos);

	kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ_SUMMARY, flags);
}

/*****************************************************************************/

void kbase_tlstream_tl_new_ctx(void *context, u32 nr)
{
	const u32     msg_id = KBASE_TL_NEW_CTX;
	const size_t  msg_size =
		sizeof(msg_id) + sizeof(u64) + sizeof(context) + sizeof(nr);
	unsigned long flags;
	char          *buffer;
	size_t        pos = 0;

	buffer = kbasep_tlstream_msgbuf_acquire(
			TL_STREAM_TYPE_OBJ,
			msg_size, &flags);
	KBASE_DEBUG_ASSERT(buffer);

	pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
	pos = kbasep_tlstream_write_timestamp(buffer, pos);
	pos = kbasep_tlstream_write_bytes(
			buffer, pos, &context, sizeof(context));
	pos = kbasep_tlstream_write_bytes(
			buffer, pos, &nr, sizeof(nr));
	KBASE_DEBUG_ASSERT(msg_size == pos);

	kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
}

void kbase_tlstream_tl_new_atom(void *atom, u32 nr)
{
	const u32     msg_id = KBASE_TL_NEW_ATOM;
	const size_t  msg_size =
		sizeof(msg_id) + sizeof(u64) + sizeof(atom) + sizeof(nr);
	unsigned long flags;
	char          *buffer;
	size_t        pos = 0;

	buffer = kbasep_tlstream_msgbuf_acquire(
			TL_STREAM_TYPE_OBJ,
			msg_size, &flags);
	KBASE_DEBUG_ASSERT(buffer);

	pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
	pos = kbasep_tlstream_write_timestamp(buffer, pos);
	pos = kbasep_tlstream_write_bytes(
			buffer, pos, &atom, sizeof(atom));
	pos = kbasep_tlstream_write_bytes(
			buffer, pos, &nr, sizeof(nr));
	KBASE_DEBUG_ASSERT(msg_size == pos);

	kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
}

void kbase_tlstream_tl_del_ctx(void *context)
{
	const u32     msg_id = KBASE_TL_DEL_CTX;
	const size_t  msg_size =
		sizeof(msg_id) + sizeof(u64) + sizeof(context);
	unsigned long flags;
	char          *buffer;
	size_t        pos = 0;

	buffer = kbasep_tlstream_msgbuf_acquire(
			TL_STREAM_TYPE_OBJ,
			msg_size, &flags);
	KBASE_DEBUG_ASSERT(buffer);

	pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
	pos = kbasep_tlstream_write_timestamp(buffer, pos);
	pos = kbasep_tlstream_write_bytes(
			buffer, pos, &context, sizeof(context));
	KBASE_DEBUG_ASSERT(msg_size == pos);

	kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
}

void kbase_tlstream_tl_del_atom(void *atom)
{
	const u32     msg_id = KBASE_TL_DEL_ATOM;
	const size_t  msg_size =
		sizeof(msg_id) + sizeof(u64) + sizeof(atom);
	unsigned long flags;
	char          *buffer;
	size_t        pos = 0;

	buffer = kbasep_tlstream_msgbuf_acquire(
			TL_STREAM_TYPE_OBJ,
			msg_size, &flags);
	KBASE_DEBUG_ASSERT(buffer);

	pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
	pos = kbasep_tlstream_write_timestamp(buffer, pos);
	pos = kbasep_tlstream_write_bytes(
			buffer, pos, &atom, sizeof(atom));
	KBASE_DEBUG_ASSERT(msg_size == pos);

	kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
}

void kbase_tlstream_tl_ret_gpu_ctx(void *gpu, void *context)
{
	const u32     msg_id = KBASE_TL_RET_GPU_CTX;
	const size_t  msg_size =
		sizeof(msg_id) + sizeof(u64) + sizeof(gpu) + sizeof(context);
	unsigned long flags;
	char          *buffer;
	size_t        pos = 0;

	buffer = kbasep_tlstream_msgbuf_acquire(
			TL_STREAM_TYPE_OBJ,
			msg_size, &flags);
	KBASE_DEBUG_ASSERT(buffer);

	pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
	pos = kbasep_tlstream_write_timestamp(buffer, pos);
	pos = kbasep_tlstream_write_bytes(
			buffer, pos, &gpu, sizeof(gpu));
	pos = kbasep_tlstream_write_bytes(
			buffer, pos, &context, sizeof(context));
	KBASE_DEBUG_ASSERT(msg_size == pos);

	kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
}

void kbase_tlstream_tl_ret_atom_ctx(void *atom, void *context)
{
	const u32     msg_id = KBASE_TL_RET_ATOM_CTX;
	const size_t  msg_size =
		sizeof(msg_id) + sizeof(u64) + sizeof(atom) + sizeof(context);
	unsigned long flags;
	char          *buffer;
	size_t        pos = 0;

	buffer = kbasep_tlstream_msgbuf_acquire(
			TL_STREAM_TYPE_OBJ,
			msg_size, &flags);
	KBASE_DEBUG_ASSERT(buffer);

	pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
	pos = kbasep_tlstream_write_timestamp(buffer, pos);
	pos = kbasep_tlstream_write_bytes(
			buffer, pos, &atom, sizeof(atom));
	pos = kbasep_tlstream_write_bytes(
			buffer, pos, &context, sizeof(context));
	KBASE_DEBUG_ASSERT(msg_size == pos);

	kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
}

void kbase_tlstream_tl_ret_atom_lpu(void *atom, void *lpu)
{
	const u32     msg_id = KBASE_TL_RET_ATOM_LPU;
	const size_t  msg_size =
		sizeof(msg_id) + sizeof(u64) + sizeof(atom) + sizeof(lpu);
	unsigned long flags;
	char          *buffer;
	size_t        pos = 0;

	buffer = kbasep_tlstream_msgbuf_acquire(
			TL_STREAM_TYPE_OBJ,
			msg_size, &flags);
	KBASE_DEBUG_ASSERT(buffer);

	pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
	pos = kbasep_tlstream_write_timestamp(buffer, pos);
	pos = kbasep_tlstream_write_bytes(
			buffer, pos, &atom, sizeof(atom));
	pos = kbasep_tlstream_write_bytes(
			buffer, pos, &lpu, sizeof(lpu));
	KBASE_DEBUG_ASSERT(msg_size == pos);

	kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
}

void kbase_tlstream_tl_nret_gpu_ctx(void *gpu, void *context)
{
	const u32     msg_id = KBASE_TL_NRET_GPU_CTX;
	const size_t  msg_size =
		sizeof(msg_id) + sizeof(u64) + sizeof(gpu) + sizeof(context);
	unsigned long flags;
	char          *buffer;
	size_t        pos = 0;

	buffer = kbasep_tlstream_msgbuf_acquire(
			TL_STREAM_TYPE_OBJ,
			msg_size, &flags);
	KBASE_DEBUG_ASSERT(buffer);

	pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
	pos = kbasep_tlstream_write_timestamp(buffer, pos);
	pos = kbasep_tlstream_write_bytes(
			buffer, pos, &gpu, sizeof(gpu));
	pos = kbasep_tlstream_write_bytes(
			buffer, pos, &context, sizeof(context));
	KBASE_DEBUG_ASSERT(msg_size == pos);

	kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
}

void kbase_tlstream_tl_nret_atom_ctx(void *atom, void *context)
{
	const u32     msg_id = KBASE_TL_NRET_ATOM_CTX;
	const size_t  msg_size =
		sizeof(msg_id) + sizeof(u64) + sizeof(atom) + sizeof(context);
	unsigned long flags;
	char          *buffer;
	size_t        pos = 0;

	buffer = kbasep_tlstream_msgbuf_acquire(
			TL_STREAM_TYPE_OBJ,
			msg_size, &flags);
	KBASE_DEBUG_ASSERT(buffer);

	pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
	pos = kbasep_tlstream_write_timestamp(buffer, pos);
	pos = kbasep_tlstream_write_bytes(
			buffer, pos, &atom, sizeof(atom));
	pos = kbasep_tlstream_write_bytes(
			buffer, pos, &context, sizeof(context));
	KBASE_DEBUG_ASSERT(msg_size == pos);

	kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
}

void kbase_tlstream_tl_nret_atom_lpu(void *atom, void *lpu)
{
	const u32     msg_id = KBASE_TL_NRET_ATOM_LPU;
	const size_t  msg_size =
		sizeof(msg_id) + sizeof(u64) + sizeof(atom) + sizeof(lpu);
	unsigned long flags;
	char          *buffer;
	size_t        pos = 0;

	buffer = kbasep_tlstream_msgbuf_acquire(
			TL_STREAM_TYPE_OBJ,
			msg_size, &flags);
	KBASE_DEBUG_ASSERT(buffer);

	pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
	pos = kbasep_tlstream_write_timestamp(buffer, pos);
	pos = kbasep_tlstream_write_bytes(
			buffer, pos, &atom, sizeof(atom));
	pos = kbasep_tlstream_write_bytes(
			buffer, pos, &lpu, sizeof(lpu));
	KBASE_DEBUG_ASSERT(msg_size == pos);

	kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_OBJ, flags);
}

/*****************************************************************************/

void kbase_tlstream_aux_pm_state(u32 core_type, u64 state)
{
	const u32     msg_id = KBASE_AUX_PM_STATE;
	const size_t  msg_size =
		sizeof(msg_id) + sizeof(u64) + sizeof(core_type) +
		sizeof(state);
	unsigned long flags;
	char          *buffer;
	size_t        pos = 0;

	buffer = kbasep_tlstream_msgbuf_acquire(
			TL_STREAM_TYPE_AUX,
			msg_size, &flags);
	KBASE_DEBUG_ASSERT(buffer);

	pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
	pos = kbasep_tlstream_write_timestamp(buffer, pos);
	pos = kbasep_tlstream_write_bytes(
			buffer, pos, &core_type, sizeof(core_type));
	pos = kbasep_tlstream_write_bytes(buffer, pos, &state, sizeof(state));
	KBASE_DEBUG_ASSERT(msg_size == pos);

	kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_AUX, flags);
}

void kbase_tlstream_aux_job_softstop(u32 js_id)
{
	const u32     msg_id = KBASE_AUX_JOB_SOFTSTOP;
	const size_t  msg_size =
		sizeof(msg_id) + sizeof(u64) + sizeof(js_id);
	unsigned long flags;
	char          *buffer;
	size_t        pos = 0;

	buffer = kbasep_tlstream_msgbuf_acquire(
			TL_STREAM_TYPE_AUX,
			msg_size, &flags);
	KBASE_DEBUG_ASSERT(buffer);

	pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
	pos = kbasep_tlstream_write_timestamp(buffer, pos);
	pos = kbasep_tlstream_write_bytes(buffer, pos, &js_id, sizeof(js_id));
	KBASE_DEBUG_ASSERT(msg_size == pos);

	kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_AUX, flags);
}

void kbase_tlstream_aux_pagefault(u32 mmu_as, u32 page_count)
{
	const u32     msg_id = KBASE_AUX_PAGEFAULT;
	const size_t  msg_size =
		sizeof(msg_id) + sizeof(u64) + sizeof(mmu_as) +
		sizeof(page_count);
	unsigned long flags;
	char          *buffer;
	size_t        pos = 0;

	buffer = kbasep_tlstream_msgbuf_acquire(
			TL_STREAM_TYPE_AUX, msg_size, &flags);
	KBASE_DEBUG_ASSERT(buffer);

	pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
	pos = kbasep_tlstream_write_timestamp(buffer, pos);
	pos = kbasep_tlstream_write_bytes(buffer, pos, &mmu_as, sizeof(mmu_as));
	pos = kbasep_tlstream_write_bytes(
			buffer, pos, &page_count, sizeof(page_count));
	KBASE_DEBUG_ASSERT(msg_size == pos);

	kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_AUX, flags);
}

void kbase_tlstream_aux_pagesalloc(s64 page_count_change)
{
	const u32     msg_id = KBASE_AUX_PAGESALLOC;
	const size_t  msg_size =
		sizeof(msg_id) + sizeof(u64) + sizeof(page_count_change);
	unsigned long flags;
	char          *buffer;
	size_t        pos = 0;

	buffer = kbasep_tlstream_msgbuf_acquire(
			TL_STREAM_TYPE_AUX, msg_size, &flags);
	KBASE_DEBUG_ASSERT(buffer);

	pos = kbasep_tlstream_write_bytes(buffer, pos, &msg_id, sizeof(msg_id));
	pos = kbasep_tlstream_write_timestamp(buffer, pos);
	pos = kbasep_tlstream_write_bytes(
			buffer, pos,
			&page_count_change, sizeof(page_count_change));
	KBASE_DEBUG_ASSERT(msg_size == pos);

	kbasep_tlstream_msgbuf_release(TL_STREAM_TYPE_AUX, flags);
}