-
Notifications
You must be signed in to change notification settings - Fork 0
/
xf86drm.c
5286 lines (4449 loc) · 132 KB
/
xf86drm.c
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
/**
* \file xf86drm.c
* User-level interface to DRM device
*
* \author Rickard E. (Rik) Faith <faith@valinux.com>
* \author Kevin E. Martin <martin@valinux.com>
*/
/*
* Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
* Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
* All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
#include <dirent.h>
#include <stddef.h>
#include <fcntl.h>
#include <errno.h>
#include <limits.h>
#include <signal.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#define stat_t struct stat
#include <sys/ioctl.h>
#include <sys/time.h>
#include <stdarg.h>
#ifdef MAJOR_IN_MKDEV
#include <sys/mkdev.h>
#endif
#ifdef MAJOR_IN_SYSMACROS
#include <sys/sysmacros.h>
#endif
#if HAVE_SYS_SYSCTL_H
#include <sys/sysctl.h>
#endif
#include <inttypes.h>
#if defined(__FreeBSD__)
#include <sys/param.h>
#include <sys/pciio.h>
#endif
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
/* Not all systems have MAP_FAILED defined */
#ifndef MAP_FAILED
#define MAP_FAILED ((void *)-1)
#endif
#include "xf86drm.h"
#include "libdrm_macros.h"
#include "drm_fourcc.h"
#include "util_math.h"
#ifdef __DragonFly__
#define DRM_MAJOR 145
#endif
#ifdef __NetBSD__
#define DRM_MAJOR 34
#endif
#ifdef __OpenBSD__
#ifdef __i386__
#define DRM_MAJOR 88
#else
#define DRM_MAJOR 87
#endif
#endif /* __OpenBSD__ */
#ifndef DRM_MAJOR
#define DRM_MAJOR 226 /* Linux */
#endif
#if defined(__OpenBSD__) || defined(__DragonFly__)
struct drm_pciinfo {
uint16_t domain;
uint8_t bus;
uint8_t dev;
uint8_t func;
uint16_t vendor_id;
uint16_t device_id;
uint16_t subvendor_id;
uint16_t subdevice_id;
uint8_t revision_id;
};
#define DRM_IOCTL_GET_PCIINFO DRM_IOR(0x15, struct drm_pciinfo)
#endif
#define DRM_MSG_VERBOSITY 3
#define memclear(s) memset(&s, 0, sizeof(s))
static drmServerInfoPtr drm_server_info;
static bool drmNodeIsDRM(int maj, int min);
static char *drmGetMinorNameForFD(int fd, int type);
#define DRM_MODIFIER(v, f, f_name) \
.modifier = DRM_FORMAT_MOD_##v ## _ ##f, \
.modifier_name = #f_name
#define DRM_MODIFIER_INVALID(v, f_name) \
.modifier = DRM_FORMAT_MOD_INVALID, .modifier_name = #f_name
#define DRM_MODIFIER_LINEAR(v, f_name) \
.modifier = DRM_FORMAT_MOD_LINEAR, .modifier_name = #f_name
/* Intel is abit special as the format doesn't follow other vendors naming
* scheme */
#define DRM_MODIFIER_INTEL(f, f_name) \
.modifier = I915_FORMAT_MOD_##f, .modifier_name = #f_name
struct drmFormatModifierInfo {
uint64_t modifier;
const char *modifier_name;
};
struct drmFormatModifierVendorInfo {
uint8_t vendor;
const char *vendor_name;
};
#include "generated_static_table_fourcc.h"
struct drmVendorInfo {
uint8_t vendor;
char *(*vendor_cb)(uint64_t modifier);
};
struct drmFormatVendorModifierInfo {
uint64_t modifier;
const char *modifier_name;
};
static char *
drmGetFormatModifierNameFromArm(uint64_t modifier);
static char *
drmGetFormatModifierNameFromNvidia(uint64_t modifier);
static char *
drmGetFormatModifierNameFromAmd(uint64_t modifier);
static char *
drmGetFormatModifierNameFromAmlogic(uint64_t modifier);
static char *
drmGetFormatModifierNameFromVivante(uint64_t modifier);
static const struct drmVendorInfo modifier_format_vendor_table[] = {
{ DRM_FORMAT_MOD_VENDOR_ARM, drmGetFormatModifierNameFromArm },
{ DRM_FORMAT_MOD_VENDOR_NVIDIA, drmGetFormatModifierNameFromNvidia },
{ DRM_FORMAT_MOD_VENDOR_AMD, drmGetFormatModifierNameFromAmd },
{ DRM_FORMAT_MOD_VENDOR_AMLOGIC, drmGetFormatModifierNameFromAmlogic },
{ DRM_FORMAT_MOD_VENDOR_VIVANTE, drmGetFormatModifierNameFromVivante },
};
#ifndef AFBC_FORMAT_MOD_MODE_VALUE_MASK
#define AFBC_FORMAT_MOD_MODE_VALUE_MASK 0x000fffffffffffffULL
#endif
static const struct drmFormatVendorModifierInfo arm_mode_value_table[] = {
{ AFBC_FORMAT_MOD_YTR, "YTR" },
{ AFBC_FORMAT_MOD_SPLIT, "SPLIT" },
{ AFBC_FORMAT_MOD_SPARSE, "SPARSE" },
{ AFBC_FORMAT_MOD_CBR, "CBR" },
{ AFBC_FORMAT_MOD_TILED, "TILED" },
{ AFBC_FORMAT_MOD_SC, "SC" },
{ AFBC_FORMAT_MOD_DB, "DB" },
{ AFBC_FORMAT_MOD_BCH, "BCH" },
{ AFBC_FORMAT_MOD_USM, "USM" },
};
static bool is_x_t_amd_gfx9_tile(uint64_t tile)
{
switch (tile) {
case AMD_FMT_MOD_TILE_GFX9_64K_S_X:
case AMD_FMT_MOD_TILE_GFX9_64K_D_X:
case AMD_FMT_MOD_TILE_GFX9_64K_R_X:
return true;
}
return false;
}
static bool
drmGetAfbcFormatModifierNameFromArm(uint64_t modifier, FILE *fp)
{
uint64_t mode_value = modifier & AFBC_FORMAT_MOD_MODE_VALUE_MASK;
uint64_t block_size = mode_value & AFBC_FORMAT_MOD_BLOCK_SIZE_MASK;
const char *block = NULL;
const char *mode = NULL;
bool did_print_mode = false;
/* add block, can only have a (single) block */
switch (block_size) {
case AFBC_FORMAT_MOD_BLOCK_SIZE_16x16:
block = "16x16";
break;
case AFBC_FORMAT_MOD_BLOCK_SIZE_32x8:
block = "32x8";
break;
case AFBC_FORMAT_MOD_BLOCK_SIZE_64x4:
block = "64x4";
break;
case AFBC_FORMAT_MOD_BLOCK_SIZE_32x8_64x4:
block = "32x8_64x4";
break;
}
if (!block) {
return false;
}
fprintf(fp, "BLOCK_SIZE=%s,", block);
/* add mode */
for (unsigned int i = 0; i < ARRAY_SIZE(arm_mode_value_table); i++) {
if (arm_mode_value_table[i].modifier & mode_value) {
mode = arm_mode_value_table[i].modifier_name;
if (!did_print_mode) {
fprintf(fp, "MODE=%s", mode);
did_print_mode = true;
} else {
fprintf(fp, "|%s", mode);
}
}
}
return true;
}
static bool
drmGetAfrcFormatModifierNameFromArm(uint64_t modifier, FILE *fp)
{
bool scan_layout;
for (unsigned int i = 0; i < 2; ++i) {
uint64_t coding_unit_block =
(modifier >> (i * 4)) & AFRC_FORMAT_MOD_CU_SIZE_MASK;
const char *coding_unit_size = NULL;
switch (coding_unit_block) {
case AFRC_FORMAT_MOD_CU_SIZE_16:
coding_unit_size = "CU_16";
break;
case AFRC_FORMAT_MOD_CU_SIZE_24:
coding_unit_size = "CU_24";
break;
case AFRC_FORMAT_MOD_CU_SIZE_32:
coding_unit_size = "CU_32";
break;
}
if (!coding_unit_size) {
if (i == 0) {
return false;
}
break;
}
if (i == 0) {
fprintf(fp, "P0=%s,", coding_unit_size);
} else {
fprintf(fp, "P12=%s,", coding_unit_size);
}
}
scan_layout =
(modifier & AFRC_FORMAT_MOD_LAYOUT_SCAN) == AFRC_FORMAT_MOD_LAYOUT_SCAN;
if (scan_layout) {
fprintf(fp, "SCAN");
} else {
fprintf(fp, "ROT");
}
return true;
}
static char *
drmGetFormatModifierNameFromArm(uint64_t modifier)
{
uint64_t type = (modifier >> 52) & 0xf;
FILE *fp;
size_t size = 0;
char *modifier_name = NULL;
bool result = false;
fp = open_memstream(&modifier_name, &size);
if (!fp)
return NULL;
switch (type) {
case DRM_FORMAT_MOD_ARM_TYPE_AFBC:
result = drmGetAfbcFormatModifierNameFromArm(modifier, fp);
break;
case DRM_FORMAT_MOD_ARM_TYPE_AFRC:
result = drmGetAfrcFormatModifierNameFromArm(modifier, fp);
break;
/* misc type is already handled by the static table */
case DRM_FORMAT_MOD_ARM_TYPE_MISC:
default:
result = false;
break;
}
fclose(fp);
if (!result) {
free(modifier_name);
return NULL;
}
return modifier_name;
}
static char *
drmGetFormatModifierNameFromNvidia(uint64_t modifier)
{
uint64_t height, kind, gen, sector, compression;
height = modifier & 0xf;
kind = (modifier >> 12) & 0xff;
gen = (modifier >> 20) & 0x3;
sector = (modifier >> 22) & 0x1;
compression = (modifier >> 23) & 0x7;
/* just in case there could other simpler modifiers, not yet added, avoid
* testing against TEGRA_TILE */
if ((modifier & 0x10) == 0x10) {
char *mod_nvidia;
asprintf(&mod_nvidia, "BLOCK_LINEAR_2D,HEIGHT=%"PRIu64",KIND=%"PRIu64","
"GEN=%"PRIu64",SECTOR=%"PRIu64",COMPRESSION=%"PRIu64"", height,
kind, gen, sector, compression);
return mod_nvidia;
}
return NULL;
}
static void
drmGetFormatModifierNameFromAmdDcc(uint64_t modifier, FILE *fp)
{
uint64_t dcc_max_compressed_block =
AMD_FMT_MOD_GET(DCC_MAX_COMPRESSED_BLOCK, modifier);
uint64_t dcc_retile = AMD_FMT_MOD_GET(DCC_RETILE, modifier);
const char *dcc_max_compressed_block_str = NULL;
fprintf(fp, ",DCC");
if (dcc_retile)
fprintf(fp, ",DCC_RETILE");
if (!dcc_retile && AMD_FMT_MOD_GET(DCC_PIPE_ALIGN, modifier))
fprintf(fp, ",DCC_PIPE_ALIGN");
if (AMD_FMT_MOD_GET(DCC_INDEPENDENT_64B, modifier))
fprintf(fp, ",DCC_INDEPENDENT_64B");
if (AMD_FMT_MOD_GET(DCC_INDEPENDENT_128B, modifier))
fprintf(fp, ",DCC_INDEPENDENT_128B");
switch (dcc_max_compressed_block) {
case AMD_FMT_MOD_DCC_BLOCK_64B:
dcc_max_compressed_block_str = "64B";
break;
case AMD_FMT_MOD_DCC_BLOCK_128B:
dcc_max_compressed_block_str = "128B";
break;
case AMD_FMT_MOD_DCC_BLOCK_256B:
dcc_max_compressed_block_str = "256B";
break;
}
if (dcc_max_compressed_block_str)
fprintf(fp, ",DCC_MAX_COMPRESSED_BLOCK=%s",
dcc_max_compressed_block_str);
if (AMD_FMT_MOD_GET(DCC_CONSTANT_ENCODE, modifier))
fprintf(fp, ",DCC_CONSTANT_ENCODE");
}
static void
drmGetFormatModifierNameFromAmdTile(uint64_t modifier, FILE *fp)
{
uint64_t pipe_xor_bits, bank_xor_bits, packers, rb;
uint64_t pipe, pipe_align, dcc, dcc_retile, tile_version;
pipe_align = AMD_FMT_MOD_GET(DCC_PIPE_ALIGN, modifier);
pipe_xor_bits = AMD_FMT_MOD_GET(PIPE_XOR_BITS, modifier);
dcc = AMD_FMT_MOD_GET(DCC, modifier);
dcc_retile = AMD_FMT_MOD_GET(DCC_RETILE, modifier);
tile_version = AMD_FMT_MOD_GET(TILE_VERSION, modifier);
fprintf(fp, ",PIPE_XOR_BITS=%"PRIu64, pipe_xor_bits);
if (tile_version == AMD_FMT_MOD_TILE_VER_GFX9) {
bank_xor_bits = AMD_FMT_MOD_GET(BANK_XOR_BITS, modifier);
fprintf(fp, ",BANK_XOR_BITS=%"PRIu64, bank_xor_bits);
}
if (tile_version == AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS) {
packers = AMD_FMT_MOD_GET(PACKERS, modifier);
fprintf(fp, ",PACKERS=%"PRIu64, packers);
}
if (dcc && tile_version == AMD_FMT_MOD_TILE_VER_GFX9) {
rb = AMD_FMT_MOD_GET(RB, modifier);
fprintf(fp, ",RB=%"PRIu64, rb);
}
if (dcc && tile_version == AMD_FMT_MOD_TILE_VER_GFX9 &&
(dcc_retile || pipe_align)) {
pipe = AMD_FMT_MOD_GET(PIPE, modifier);
fprintf(fp, ",PIPE_%"PRIu64, pipe);
}
}
static char *
drmGetFormatModifierNameFromAmd(uint64_t modifier)
{
uint64_t tile, tile_version, dcc;
FILE *fp;
char *mod_amd = NULL;
size_t size = 0;
const char *str_tile = NULL;
const char *str_tile_version = NULL;
tile = AMD_FMT_MOD_GET(TILE, modifier);
tile_version = AMD_FMT_MOD_GET(TILE_VERSION, modifier);
dcc = AMD_FMT_MOD_GET(DCC, modifier);
fp = open_memstream(&mod_amd, &size);
if (!fp)
return NULL;
/* add tile */
switch (tile_version) {
case AMD_FMT_MOD_TILE_VER_GFX9:
str_tile_version = "GFX9";
break;
case AMD_FMT_MOD_TILE_VER_GFX10:
str_tile_version = "GFX10";
break;
case AMD_FMT_MOD_TILE_VER_GFX10_RBPLUS:
str_tile_version = "GFX10_RBPLUS";
break;
case AMD_FMT_MOD_TILE_VER_GFX11:
str_tile_version = "GFX11";
break;
}
if (str_tile_version) {
fprintf(fp, "%s", str_tile_version);
} else {
fclose(fp);
free(mod_amd);
return NULL;
}
/* add tile str */
switch (tile) {
case AMD_FMT_MOD_TILE_GFX9_64K_S:
str_tile = "GFX9_64K_S";
break;
case AMD_FMT_MOD_TILE_GFX9_64K_D:
str_tile = "GFX9_64K_D";
break;
case AMD_FMT_MOD_TILE_GFX9_64K_S_X:
str_tile = "GFX9_64K_S_X";
break;
case AMD_FMT_MOD_TILE_GFX9_64K_D_X:
str_tile = "GFX9_64K_D_X";
break;
case AMD_FMT_MOD_TILE_GFX9_64K_R_X:
str_tile = "GFX9_64K_R_X";
break;
case AMD_FMT_MOD_TILE_GFX11_256K_R_X:
str_tile = "GFX11_256K_R_X";
break;
}
if (str_tile)
fprintf(fp, ",%s", str_tile);
if (dcc)
drmGetFormatModifierNameFromAmdDcc(modifier, fp);
if (tile_version >= AMD_FMT_MOD_TILE_VER_GFX9 && is_x_t_amd_gfx9_tile(tile))
drmGetFormatModifierNameFromAmdTile(modifier, fp);
fclose(fp);
return mod_amd;
}
static char *
drmGetFormatModifierNameFromAmlogic(uint64_t modifier)
{
uint64_t layout = modifier & 0xff;
uint64_t options = (modifier >> 8) & 0xff;
char *mod_amlogic = NULL;
const char *layout_str;
const char *opts_str;
switch (layout) {
case AMLOGIC_FBC_LAYOUT_BASIC:
layout_str = "BASIC";
break;
case AMLOGIC_FBC_LAYOUT_SCATTER:
layout_str = "SCATTER";
break;
default:
layout_str = "INVALID_LAYOUT";
break;
}
if (options & AMLOGIC_FBC_OPTION_MEM_SAVING)
opts_str = "MEM_SAVING";
else
opts_str = "0";
asprintf(&mod_amlogic, "FBC,LAYOUT=%s,OPTIONS=%s", layout_str, opts_str);
return mod_amlogic;
}
static char *
drmGetFormatModifierNameFromVivante(uint64_t modifier)
{
const char *color_tiling, *tile_status, *compression;
char *mod_vivante = NULL;
switch (modifier & VIVANTE_MOD_TS_MASK) {
case 0:
tile_status = "";
break;
case VIVANTE_MOD_TS_64_4:
tile_status = ",TS=64B_4";
break;
case VIVANTE_MOD_TS_64_2:
tile_status = ",TS=64B_2";
break;
case VIVANTE_MOD_TS_128_4:
tile_status = ",TS=128B_4";
break;
case VIVANTE_MOD_TS_256_4:
tile_status = ",TS=256B_4";
break;
default:
tile_status = ",TS=UNKNOWN";
break;
}
switch (modifier & VIVANTE_MOD_COMP_MASK) {
case 0:
compression = "";
break;
case VIVANTE_MOD_COMP_DEC400:
compression = ",COMP=DEC400";
break;
default:
compression = ",COMP=UNKNOWN";
break;
}
switch (modifier & ~VIVANTE_MOD_EXT_MASK) {
case 0:
color_tiling = "LINEAR";
break;
case DRM_FORMAT_MOD_VIVANTE_TILED:
color_tiling = "TILED";
break;
case DRM_FORMAT_MOD_VIVANTE_SUPER_TILED:
color_tiling = "SUPER_TILED";
break;
case DRM_FORMAT_MOD_VIVANTE_SPLIT_TILED:
color_tiling = "SPLIT_TILED";
break;
case DRM_FORMAT_MOD_VIVANTE_SPLIT_SUPER_TILED:
color_tiling = "SPLIT_SUPER_TILED";
break;
default:
color_tiling = "UNKNOWN";
break;
}
asprintf(&mod_vivante, "%s%s%s", color_tiling, tile_status, compression);
return mod_vivante;
}
static unsigned log2_int(unsigned x)
{
unsigned l;
if (x < 2) {
return 0;
}
for (l = 2; ; l++) {
if ((unsigned)(1 << l) > x) {
return l - 1;
}
}
return 0;
}
drm_public void drmSetServerInfo(drmServerInfoPtr info)
{
drm_server_info = info;
}
/**
* Output a message to stderr.
*
* \param format printf() like format string.
*
* \internal
* This function is a wrapper around vfprintf().
*/
static int DRM_PRINTFLIKE(1, 0)
drmDebugPrint(const char *format, va_list ap)
{
return vfprintf(stderr, format, ap);
}
drm_public void
drmMsg(const char *format, ...)
{
va_list ap;
const char *env;
if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) ||
(drm_server_info && drm_server_info->debug_print))
{
va_start(ap, format);
if (drm_server_info) {
drm_server_info->debug_print(format,ap);
} else {
drmDebugPrint(format, ap);
}
va_end(ap);
}
}
static void *drmHashTable = NULL; /* Context switch callbacks */
drm_public void *drmGetHashTable(void)
{
return drmHashTable;
}
drm_public void *drmMalloc(int size)
{
return calloc(1, size);
}
drm_public void drmFree(void *pt)
{
free(pt);
}
/**
* Call ioctl, restarting if it is interrupted
*/
drm_public int
drmIoctl(int fd, unsigned long request, void *arg)
{
int ret;
do {
ret = ioctl(fd, request, arg);
} while (ret == -1 && (errno == EINTR || errno == EAGAIN));
return ret;
}
static unsigned long drmGetKeyFromFd(int fd)
{
stat_t st;
st.st_rdev = 0;
fstat(fd, &st);
return st.st_rdev;
}
drm_public drmHashEntry *drmGetEntry(int fd)
{
unsigned long key = drmGetKeyFromFd(fd);
void *value;
drmHashEntry *entry;
if (!drmHashTable)
drmHashTable = drmHashCreate();
if (drmHashLookup(drmHashTable, key, &value)) {
entry = drmMalloc(sizeof(*entry));
entry->fd = fd;
entry->f = NULL;
entry->tagTable = drmHashCreate();
drmHashInsert(drmHashTable, key, entry);
} else {
entry = value;
}
return entry;
}
/**
* Compare two busid strings
*
* \param first
* \param second
*
* \return 1 if matched.
*
* \internal
* This function compares two bus ID strings. It understands the older
* PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
* domain, b is bus, d is device, f is function.
*/
static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
{
/* First, check if the IDs are exactly the same */
if (strcasecmp(id1, id2) == 0)
return 1;
/* Try to match old/new-style PCI bus IDs. */
if (strncasecmp(id1, "pci", 3) == 0) {
unsigned int o1, b1, d1, f1;
unsigned int o2, b2, d2, f2;
int ret;
ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
if (ret != 4) {
o1 = 0;
ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
if (ret != 3)
return 0;
}
ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
if (ret != 4) {
o2 = 0;
ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
if (ret != 3)
return 0;
}
/* If domains aren't properly supported by the kernel interface,
* just ignore them, which sucks less than picking a totally random
* card with "open by name"
*/
if (!pci_domain_ok)
o1 = o2 = 0;
if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
return 0;
else
return 1;
}
return 0;
}
/**
* Handles error checking for chown call.
*
* \param path to file.
* \param id of the new owner.
* \param id of the new group.
*
* \return zero if success or -1 if failure.
*
* \internal
* Checks for failure. If failure was caused by signal call chown again.
* If any other failure happened then it will output error message using
* drmMsg() call.
*/
#if !UDEV
static int chown_check_return(const char *path, uid_t owner, gid_t group)
{
int rv;
do {
rv = chown(path, owner, group);
} while (rv != 0 && errno == EINTR);
if (rv == 0)
return 0;
drmMsg("Failed to change owner or group for file %s! %d: %s\n",
path, errno, strerror(errno));
return -1;
}
#endif
static const char *drmGetDeviceName(int type)
{
switch (type) {
case DRM_NODE_PRIMARY:
return DRM_DEV_NAME;
case DRM_NODE_RENDER:
return DRM_RENDER_DEV_NAME;
}
return NULL;
}
/**
* Open the DRM device, creating it if necessary.
*
* \param dev major and minor numbers of the device.
* \param minor minor number of the device.
*
* \return a file descriptor on success, or a negative value on error.
*
* \internal
* Assembles the device name from \p minor and opens it, creating the device
* special file node with the major and minor numbers specified by \p dev and
* parent directory if necessary and was called by root.
*/
static int drmOpenDevice(dev_t dev, int minor, int type)
{
stat_t st;
const char *dev_name = drmGetDeviceName(type);
char buf[DRM_NODE_NAME_MAX];
int fd;
mode_t devmode = DRM_DEV_MODE, serv_mode;
gid_t serv_group;
#if !UDEV
int isroot = !geteuid();
uid_t user = DRM_DEV_UID;
gid_t group = DRM_DEV_GID;
#endif
if (!dev_name)
return -EINVAL;
sprintf(buf, dev_name, DRM_DIR_NAME, minor);
drmMsg("drmOpenDevice: node name is %s\n", buf);
if (drm_server_info && drm_server_info->get_perms) {
drm_server_info->get_perms(&serv_group, &serv_mode);
devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
}
#if !UDEV
if (stat(DRM_DIR_NAME, &st)) {
if (!isroot)
return DRM_ERR_NOT_ROOT;
mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
}
/* Check if the device node exists and create it if necessary. */
if (stat(buf, &st)) {
if (!isroot)
return DRM_ERR_NOT_ROOT;
remove(buf);
mknod(buf, S_IFCHR | devmode, dev);
}
if (drm_server_info && drm_server_info->get_perms) {
group = ((int)serv_group >= 0) ? serv_group : DRM_DEV_GID;
chown_check_return(buf, user, group);
chmod(buf, devmode);
}
#else
/* if we modprobed then wait for udev */
{
int udev_count = 0;
wait_for_udev:
if (stat(DRM_DIR_NAME, &st)) {
usleep(20);
udev_count++;
if (udev_count == 50)
return -1;
goto wait_for_udev;
}
if (stat(buf, &st)) {
usleep(20);
udev_count++;
if (udev_count == 50)
return -1;
goto wait_for_udev;
}
}
#endif
fd = open(buf, O_RDWR | O_CLOEXEC);
drmMsg("drmOpenDevice: open result is %d, (%s)\n",
fd, fd < 0 ? strerror(errno) : "OK");
if (fd >= 0)
return fd;
#if !UDEV
/* Check if the device node is not what we expect it to be, and recreate it
* and try again if so.
*/
if (st.st_rdev != dev) {
if (!isroot)
return DRM_ERR_NOT_ROOT;
remove(buf);
mknod(buf, S_IFCHR | devmode, dev);
if (drm_server_info && drm_server_info->get_perms) {
chown_check_return(buf, user, group);
chmod(buf, devmode);
}
}
fd = open(buf, O_RDWR | O_CLOEXEC);
drmMsg("drmOpenDevice: open result is %d, (%s)\n",
fd, fd < 0 ? strerror(errno) : "OK");
if (fd >= 0)
return fd;
drmMsg("drmOpenDevice: Open failed\n");
remove(buf);
#endif
return -errno;
}
/**
* Open the DRM device
*
* \param minor device minor number.
* \param create allow to create the device if set.
*
* \return a file descriptor on success, or a negative value on error.
*
* \internal
* Calls drmOpenDevice() if \p create is set, otherwise assembles the device
* name from \p minor and opens it.
*/
static int drmOpenMinor(int minor, int create, int type)
{
int fd;
char buf[DRM_NODE_NAME_MAX];
const char *dev_name = drmGetDeviceName(type);
if (create)
return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
if (!dev_name)
return -EINVAL;
sprintf(buf, dev_name, DRM_DIR_NAME, minor);
if ((fd = open(buf, O_RDWR | O_CLOEXEC)) >= 0)
return fd;
return -errno;
}
/**
* Determine whether the DRM kernel driver has been loaded.
*
* \return 1 if the DRM driver is loaded, 0 otherwise.
*
* \internal
* Determine the presence of the kernel driver by attempting to open the 0
* minor and get version information. For backward compatibility with older
* Linux implementations, /proc/dri is also checked.
*/
drm_public int drmAvailable(void)
{
drmVersionPtr version;
int retval = 0;