1 /***********************************************************************************
2
3 Copyright (C) 2018 Intel Corporation. All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 - Redistributions of source code must retain the above copyright notice,
8 this list of conditions and the following disclaimer.
9 - Redistributions in binary form must reproduce the above copyright notice,
10 this list of conditions and the following disclaimer in the documentation
11 and/or other materials provided with the distribution.
12 - Neither the name of Intel Corporation nor the names of its contributors
13 may be used to endorse or promote products derived from this software
14 without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION "AS IS" AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL INTEL CORPORATION BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 ***********************************************************************************/
28
29 #include "cttmetrics_utils.h"
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <unistd.h>
36 #include <string.h>
37
38 char CARD_N[16] = {0};
39 char CARD[32] = {0};
40
path_gen(char * pdst,size_t sz,const char delim,const char * a,const char * b,const char * c)41 int path_gen(char* pdst, size_t sz, const char delim, const char* a, const char* b, const char* c)
42 {
43 size_t total_len = 2 + strlen(a) + strlen(b) + strlen(c);
44
45 if(total_len >= sz)
46 return -1;
47
48 while(*a) *pdst++ = *a++;
49 *pdst++ = delim;
50 while(*b) *pdst++ = *b++;
51 *pdst++ = delim;
52 while(*c) *pdst++ = *c++;
53 *pdst++ = 0;
54
55 return 0;
56 }
57
58 // get the string value of property and converts it to integer
get_id_from_file(const char * d_name,const char * property)59 static long int get_id_from_file(const char* d_name, const char* property)
60 {
61 long int id = 0;
62 char str[16] = {0};
63 char fname[300] = {0};
64
65 if ((NULL == d_name) || (NULL == property))
66 return -1;
67
68 if(path_gen(fname, sizeof(fname)/sizeof(fname[0]), '/', DRM_DIR, d_name, property))
69 return -1;
70
71 FILE* file = fopen(fname, "r");
72 if (file)
73 {
74 if (fgets(str, sizeof(str), file))
75 {
76 id = strtol(str, NULL, 16);
77 }
78 fclose(file);
79 }
80
81 return id;
82 }
83
84 /*
85 check that system has at least one Intel grraphics adapter
86 support only first adapter
87 */
discover_path_to_gpu()88 cttStatus discover_path_to_gpu()
89 {
90 cttStatus status = CTT_ERR_DRIVER_NOT_FOUND;
91 long int class_id = 0, vendor_id = 0;
92 struct stat buffer;
93 char DRM_CARD[30] = {0};
94 int i;
95 for (i=0; i<100 ; i++)
96 {
97 snprintf(CARD_N, sizeof(CARD_N), "%d", i);
98 snprintf(CARD, sizeof(CARD), "card%s", CARD_N);
99
100 path_gen(DRM_CARD, sizeof(DRM_CARD)/sizeof(DRM_CARD[0]), '/', DRM_DIR, CARD, "");
101 if(lstat(DRM_CARD, &buffer))
102 break;
103
104 // device class id
105 class_id = get_id_from_file(CARD, "device/class");
106
107 if (PCI_DISPLAY_CONTROLLER_CLASS == (class_id >> 16))
108 {
109 // device vendor id
110 vendor_id = get_id_from_file(CARD, "device/vendor");
111 if (vendor_id == INTEL_VENDOR_ID)
112 {
113 status = CTT_ERR_NONE;
114 break;
115 }
116 }
117 }
118
119 return status;
120 }
121
read_freq(int fd)122 int read_freq(int fd)
123 {
124 size_t nread;
125 char freq[16]; /* Intel i915 frequencies are in MHZ, so we expect to read 4 digits */
126
127 if (-1 == fd) goto error;
128
129 /* jump to the very beginning of the file */
130 if (-1 == lseek(fd, 0, SEEK_SET)) goto error;
131
132 /* read the new frequency value */
133 nread = read(fd, freq, sizeof(freq));
134 if (nread >= sizeof(freq)) goto error;
135
136 return atoi(freq);
137
138 error:
139 return 0;
140 }
141