11
11
#include <bus/pci/pcireg.h> // DragonFly
12
12
#endif
13
13
14
- #include "common/library.h"
15
- #include "util/stringUtils.h"
16
- #include <xf86drm.h>
17
- #include <i915_drm.h>
18
-
19
- #ifdef FF_HAVE_DRM_AMDGPU
20
- #include <amdgpu.h>
21
- #include <amdgpu_drm.h>
22
- #include <fcntl.h>
23
- #endif
24
-
25
14
static void fillGPUTypeGeneric (FFGPUResult * gpu )
26
15
{
27
16
if (gpu -> type == FF_GPU_TYPE_UNKNOWN )
@@ -47,137 +36,10 @@ static void fillGPUTypeGeneric(FFGPUResult* gpu)
47
36
}
48
37
49
38
#if FF_HAVE_DRM
50
- static const char * drmDetectAmdSpecific (const FFGPUOptions * options , FFGPUResult * gpu , const char * renderPath )
51
- {
52
- #if FF_HAVE_DRM_AMDGPU
53
- FF_LIBRARY_LOAD (libdrm , "dlopen libdrm_amdgpu" FF_LIBRARY_EXTENSION " failed" , "libdrm_amdgpu" FF_LIBRARY_EXTENSION , 1 )
54
- FF_LIBRARY_LOAD_SYMBOL_MESSAGE (libdrm , amdgpu_device_initialize )
55
- FF_LIBRARY_LOAD_SYMBOL_MESSAGE (libdrm , amdgpu_get_marketing_name )
56
- FF_LIBRARY_LOAD_SYMBOL_MESSAGE (libdrm , amdgpu_query_gpu_info )
57
- FF_LIBRARY_LOAD_SYMBOL_MESSAGE (libdrm , amdgpu_query_sensor_info )
58
- FF_LIBRARY_LOAD_SYMBOL_MESSAGE (libdrm , amdgpu_query_heap_info )
59
- FF_LIBRARY_LOAD_SYMBOL_MESSAGE (libdrm , amdgpu_device_deinitialize )
60
-
61
- FF_AUTO_CLOSE_FD int fd = open (renderPath , O_RDONLY );
62
- if (fd < 0 ) return "Failed to open DRM device" ;
63
-
64
- amdgpu_device_handle handle ;
65
- uint32_t majorVersion , minorVersion ;
66
- if (ffamdgpu_device_initialize (fd , & majorVersion , & minorVersion , & handle ) < 0 )
67
- return "Failed to initialize AMDGPU device" ;
68
-
69
- ffStrbufAppendF (& gpu -> driver , " %u.%u" , (unsigned ) majorVersion , (unsigned ) minorVersion );
70
-
71
- uint32_t value ;
72
-
73
- if (options -> temp )
74
- {
75
- if (ffamdgpu_query_sensor_info (handle , AMDGPU_INFO_SENSOR_GPU_TEMP , sizeof (value ), & value ) >= 0 )
76
- gpu -> temperature = value / 1000. ;
77
- }
78
-
79
- ffStrbufSetS (& gpu -> name , ffamdgpu_get_marketing_name (handle ));
80
-
81
- struct amdgpu_gpu_info gpuInfo ;
82
- if (ffamdgpu_query_gpu_info (handle , & gpuInfo ) >= 0 )
83
- {
84
- gpu -> coreCount = (int32_t ) gpuInfo .cu_active_number ;
85
- gpu -> frequency = (uint32_t ) (gpuInfo .max_engine_clk / 1000u );
86
- gpu -> index = FF_GPU_INDEX_UNSET ;
87
- gpu -> type = gpuInfo .ids_flags & AMDGPU_IDS_FLAGS_FUSION ? FF_GPU_TYPE_INTEGRATED : FF_GPU_TYPE_DISCRETE ;
88
- #define FF_VRAM_CASE (name , value ) case value /* AMDGPU_VRAM_TYPE_ ## name */ : ffStrbufSetStatic (& gpu ->memoryType, #name); break
89
- switch (gpuInfo .vram_type )
90
- {
91
- FF_VRAM_CASE (UNKNOWN , 0 );
92
- FF_VRAM_CASE (GDDR1 , 1 );
93
- FF_VRAM_CASE (DDR2 , 2 );
94
- FF_VRAM_CASE (GDDR3 , 3 );
95
- FF_VRAM_CASE (GDDR4 , 4 );
96
- FF_VRAM_CASE (GDDR5 , 5 );
97
- FF_VRAM_CASE (HBM , 6 );
98
- FF_VRAM_CASE (DDR3 , 7 );
99
- FF_VRAM_CASE (DDR4 , 8 );
100
- FF_VRAM_CASE (GDDR6 , 9 );
101
- FF_VRAM_CASE (DDR5 , 10 );
102
- FF_VRAM_CASE (LPDDR4 , 11 );
103
- FF_VRAM_CASE (LPDDR5 , 12 );
104
- default :
105
- ffStrbufAppendF (& gpu -> memoryType , "Unknown (%u)" , gpuInfo .vram_type );
106
- break ;
107
- }
108
-
109
- struct amdgpu_heap_info heapInfo ;
110
- if (ffamdgpu_query_heap_info (handle , AMDGPU_GEM_DOMAIN_VRAM , 0 , & heapInfo ) >= 0 )
111
- {
112
- if (gpu -> type == FF_GPU_TYPE_DISCRETE )
113
- {
114
- gpu -> dedicated .total = heapInfo .heap_size ;
115
- gpu -> dedicated .used = heapInfo .heap_usage ;
116
- }
117
- else
118
- {
119
- gpu -> shared .total = heapInfo .heap_size ;
120
- gpu -> shared .used = heapInfo .heap_usage ;
121
- }
122
- }
123
- }
124
-
125
- if (ffamdgpu_query_sensor_info (handle , AMDGPU_INFO_SENSOR_GPU_LOAD , sizeof (value ), & value ) >= 0 )
126
- gpu -> coreUsage = value ;
127
-
128
- ffamdgpu_device_deinitialize (handle );
129
-
130
- return NULL ;
131
- #else
132
- FF_UNUSED (options , gpu , drmKey , buffer );
133
- return "Fastfetch is compiled without libdrm support" ;
134
- #endif
135
- }
39
+ #include "common/library.h"
40
+ #include "util/stringUtils.h"
136
41
137
- static const char * drmDetectIntelSpecific (FFGPUResult * gpu , int fd )
138
- {
139
- {
140
- int value ;
141
- drm_i915_getparam_t getparam = { .param = I915_PARAM_EU_TOTAL , .value = & value };
142
- if (ioctl (fd , DRM_IOCTL_I915_GETPARAM , & getparam ) >= 0 )
143
- gpu -> coreCount = value ;
144
- }
145
- {
146
- struct drm_i915_query_item queryItem = {
147
- .query_id = DRM_I915_QUERY_MEMORY_REGIONS ,
148
- };
149
- struct drm_i915_query query = {
150
- .items_ptr = (uintptr_t ) & queryItem ,
151
- .num_items = 1 ,
152
- };
153
- if (ioctl (fd , DRM_IOCTL_I915_QUERY , & query ) >= 0 )
154
- {
155
- FF_AUTO_FREE uint8_t * buffer = calloc (1 , (size_t ) queryItem .length );
156
- queryItem .data_ptr = (uintptr_t ) buffer ;
157
- if (ioctl (fd , DRM_IOCTL_I915_QUERY , & query ) >= 0 )
158
- {
159
- gpu -> dedicated .total = gpu -> shared .total = gpu -> dedicated .used = gpu -> shared .used = 0 ;
160
- struct drm_i915_query_memory_regions * regionInfo = (void * ) buffer ;
161
- for (uint32_t i = 0 ; i < regionInfo -> num_regions ; i ++ )
162
- {
163
- struct drm_i915_memory_region_info * region = regionInfo -> regions + i ;
164
- switch (region -> region .memory_class )
165
- {
166
- case I915_MEMORY_CLASS_SYSTEM :
167
- gpu -> shared .total += region -> probed_size ;
168
- gpu -> shared .used += region -> probed_size - region -> unallocated_size ;
169
- break ;
170
- case I915_MEMORY_CLASS_DEVICE :
171
- gpu -> dedicated .total += region -> probed_size ;
172
- gpu -> dedicated .used += region -> probed_size - region -> unallocated_size ;
173
- break ;
174
- }
175
- }
176
- }
177
- }
178
- }
179
- return NULL ;
180
- }
42
+ #include <xf86drm.h>
181
43
182
44
static const char * detectByDrm (const FFGPUOptions * options , FFlist * gpus )
183
45
{
@@ -235,43 +97,30 @@ static const char* detectByDrm(const FFGPUOptions* options, FFlist* gpus)
235
97
if (fd < 0 ) continue ;
236
98
237
99
char driverName [64 ];
238
- char driverDesc [ 64 ] ;
100
+ driverName [ 0 ] = '\0' ;
239
101
struct drm_version ver = {
240
102
.name = driverName ,
241
103
.name_len = ARRAY_SIZE (driverName ),
242
- .desc = driverDesc ,
243
- .desc_len = ARRAY_SIZE (driverDesc ),
244
104
};
245
105
if (ioctl (fd , DRM_IOCTL_VERSION , & ver ) == 0 )
246
106
ffStrbufSetF (& gpu -> driver , "%*s %d.%d.%d" , (int ) ver .name_len , ver .name , ver .version_major , ver .version_minor , ver .version_patchlevel );
247
107
248
- if (dev -> bustype != DRM_BUS_PCI )
249
- continue ;
250
-
251
- if (ffStrEquals (ver .name , "i915" ))
252
- drmDetectIntelSpecific (gpu , fd );
253
- else if (ffStrEquals (ver .name , "amdgpu" ))
254
- drmDetectAmdSpecific (options , gpu , dev -> nodes [DRM_NODE_RENDER ]);
255
- else if (ffStrEquals (ver .name , "nvidia-drm" ) && (options -> temp || options -> driverSpecific ))
108
+ if (ffStrStartsWith (driverName , "i915" ))
109
+ ffDrmDetectI915 (gpu , fd );
110
+ else if (ffStrStartsWith (driverName , "amdgpu" ))
111
+ ffDrmDetectAmdgpu (options , gpu , dev -> nodes [DRM_NODE_RENDER ]);
112
+ else if (ffStrStartsWith (driverName , "xe" ))
113
+ ffDrmDetectXe (gpu , fd );
114
+ else if (ffStrStartsWith (driverName , "asahi" ))
115
+ ffDrmDetectAsahi (gpu , fd );
116
+ else if (dev -> bustype == DRM_BUS_PCI )
256
117
{
257
- ffDetectNvidiaGpuInfo (& (FFGpuDriverCondition ) {
258
- .type = FF_GPU_DRIVER_CONDITION_TYPE_BUS_ID ,
259
- .pciBusId = {
260
- .domain = (uint32_t ) dev -> businfo .pci -> domain ,
261
- .bus = dev -> businfo .pci -> bus ,
262
- .device = dev -> businfo .pci -> dev ,
263
- .func = dev -> businfo .pci -> func ,
264
- },
265
- }, (FFGpuDriverResult ) {
266
- .index = & gpu -> index ,
267
- .temp = options -> temp ? & gpu -> temperature : NULL ,
268
- .memory = options -> driverSpecific ? & gpu -> dedicated : NULL ,
269
- .coreCount = options -> driverSpecific ? (uint32_t * ) & gpu -> coreCount : NULL ,
270
- .type = & gpu -> type ,
271
- .frequency = & gpu -> frequency ,
272
- .coreUsage = & gpu -> coreUsage ,
273
- .name = & gpu -> name ,
274
- }, "libnvidia-ml.so" );
118
+ ffGPUDetectDriverSpecific (options , gpu , (FFGpuDriverPciBusId ) {
119
+ .domain = (uint32_t ) dev -> businfo .pci -> domain ,
120
+ .bus = dev -> businfo .pci -> bus ,
121
+ .device = dev -> businfo .pci -> dev ,
122
+ .func = dev -> businfo .pci -> func ,
123
+ });
275
124
}
276
125
277
126
if (gpu -> name .length == 0 )
@@ -333,27 +182,12 @@ static const char* detectByPci(const FFGPUOptions* options, FFlist* gpus)
333
182
gpu -> deviceId = (pc -> pc_sel .pc_domain * 100000ull ) + (pc -> pc_sel .pc_bus * 1000ull ) + (pc -> pc_sel .pc_dev * 10ull ) + pc -> pc_sel .pc_func ;
334
183
gpu -> frequency = FF_GPU_FREQUENCY_UNSET ;
335
184
336
- if (gpu -> vendor .chars == FF_GPU_VENDOR_NAME_NVIDIA && (options -> temp || options -> driverSpecific ))
337
- {
338
- ffDetectNvidiaGpuInfo (& (FFGpuDriverCondition ) {
339
- .type = FF_GPU_DRIVER_CONDITION_TYPE_BUS_ID ,
340
- .pciBusId = {
341
- .domain = (uint32_t ) pc -> pc_sel .pc_domain ,
342
- .bus = pc -> pc_sel .pc_bus ,
343
- .device = pc -> pc_sel .pc_dev ,
344
- .func = pc -> pc_sel .pc_func ,
345
- },
346
- }, (FFGpuDriverResult ) {
347
- .index = & gpu -> index ,
348
- .temp = options -> temp ? & gpu -> temperature : NULL ,
349
- .memory = options -> driverSpecific ? & gpu -> dedicated : NULL ,
350
- .coreCount = options -> driverSpecific ? (uint32_t * ) & gpu -> coreCount : NULL ,
351
- .type = & gpu -> type ,
352
- .frequency = & gpu -> frequency ,
353
- .coreUsage = & gpu -> coreUsage ,
354
- .name = & gpu -> name ,
355
- }, "libnvidia-ml.so" );
356
- }
185
+ ffGPUDetectDriverSpecific (options , gpu , (FFGpuDriverPciBusId ) {
186
+ .domain = (uint32_t ) pc -> pc_sel .pc_domain ,
187
+ .bus = pc -> pc_sel .pc_bus ,
188
+ .device = pc -> pc_sel .pc_dev ,
189
+ .func = pc -> pc_sel .pc_func ,
190
+ });
357
191
358
192
if (gpu -> name .length == 0 )
359
193
{
0 commit comments