Skip to contentSkip to conversation

tools/intel_gpu_frequency: Add multi-GT support and Xe driver compatibility

PATCH In review S Sebinraj 2 participants 7 messages Dec 5, 2025

lore
Share & tools
lore

S Sebinraj

author
#0 lore raw

v6 cover tools/intel_gpu_frequency: Add multi-GT support and Xe driver compatibility

This patch enhances intel_gpu_frequency to support Intel GPUs with multiple Graphics Technology (GT) units and adds compatibility with both i915 and Xe drivers. This also includes the changes to use get, set in order and vice-versa which was resulting in assertion errors.

Key improvements:
- Add runtime GT detection using igt_sysfs_get_num_gt() and xe_number_gt()
- Auto-detect driver type (i915 vs Xe) using is_xe_device()
- New --gt/-t option to specify target GT (defaults to GT0)
- New --list/-l option to display available GTs
- Driver-agnostic frequency management with unified interface
- Add support for get, set inorder and vice-versa.

Driver-specific implementations:
- Frequency name mapping for Xe (RP1→rpe, RP0→rp0, RPn→rpn for Xe)

Usage examples:
intel_gpu_frequency --get --gt 1 # Get GT1 frequencies
intel_gpu_frequency --set 400 --gt 0 # Set GT0 frequency
intel_gpu_frequency --list # List available GTs

The tool now auto-detects i915 / Xe drivers and uses appropriate interfaces.
---

Changes in v6:
- Remove the patch added for regular user read as it already updated
- Rebase code with latest changes in intel_gpu_frequency
- Remove bool argument in functions and re-design approach
- Revert the change made in drmtest in v4

Changes in v5:
- Add patch for regular user read

Changes in v4:
- Rebase the patch to fix Ci.build issue in v3

Changes in v3:
- Use --list as arg instead of --list-gts
- Change printing of frequencies to tabular
and with additional informations

Changes in v2:
- Remove output from commit message
- Add support for simultaneous read and write

---
S Sebinraj (3):
tools/intel_gpu_frequency: Add support for get, set in order &
vice-versa
tools/intel_gpu_frequency: Add multi-GT support tools/intel_gpu_frequency: Add Xe driver support

 man/intel_gpu_frequency.rst |  34 +++-
 tools/intel_gpu_frequency.c | 360 +++++++++++++++++++++++++++++-------
 2 files changed, 318 insertions(+), 76 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v6 1/3 tools/intel_gpu_frequency: Add support for get, set in order & vice-versa

Add proper conditions for handling get and set in order (also vice-versa), as the same was failing before with the below error. To note here, In this scenario w.r.t set operation, only writable sysfs path has to be used ie. [min, max].

Error: set_frequency:

        Assertion `fprintf(freq_info->filp, "%d", val) > 0' failed.
---
 tools/intel_gpu_frequency.c | 63 ++++++++++++++++++++++++-------------
 1 file changed, 41 insertions(+), 22 deletions(-)
tools/intel_gpu_frequency.c +41 -22
index 6316c2e58..6b6997ab1 100644
@@ -63,6 +63,11 @@ static struct freq_info info[] = {
 	{ "RPn", "r" }
 };
 
+struct parse_result {
+	bool write;
+	bool read;
+};
+
 static char *
 get_sysfs_path(const char *which)
 {
@@ -319,16 +324,17 @@ parse_version_or_help(int argc, char *argv[])
 }
 
 /* Returns read or write operation */
-static bool
+static struct parse_result
 parse(int argc, char *argv[], bool *act_upon, size_t act_upon_n, int *new_freq)
 {
 	int c, tmp;
-	bool write = false;
+	struct parse_result result = { .write = false, .read = false };
 
 	/* No args means -g" */
 	if (argc == 1) {
 		for (c = 0; c < act_upon_n; c++)
 			act_upon[c] = true;
+		result.read = true;
 		goto done;
 	}
 	while (1) {
@@ -351,24 +357,23 @@ parse(int argc, char *argv[], bool *act_upon, size_t act_upon_n, int *new_freq)
 
 		switch (c) {
 		case 'g':
-			if (write == true)
-				fprintf(stderr, "Read and write operations not support simultaneously.\n");
 			{
 				int i;
 				for (i = 0; i < act_upon_n; i++)
 					act_upon[i] = true;
 			}
+			result.read = true;
 			break;
 		case 's':
 			if (!optarg)
 				usage(argv[0], EXIT_FAILURE);
 
-			if (write == true) {
+			if (result.write == true) {
 				fprintf(stderr, "Only one write may be specified at a time\n");
 				exit(EXIT_FAILURE);
 			}
 
-			write = true;
+			result.write = true;
 			act_upon[MIN] = true;
 			act_upon[MAX] = true;
 			sscanf(optarg, "%d", &new_freq[MAX]);
@@ -378,12 +383,12 @@ parse(int argc, char *argv[], bool *act_upon, size_t act_upon_n, int *new_freq)
 			if (!optarg)
 				usage(argv[0], EXIT_FAILURE);
 
-			if (write == true) {
+			if (result.write == true) {
 				fprintf(stderr, "Only one write may be specified at a time\n");
 				exit(EXIT_FAILURE);
 			}
 
-			write = true;
+			result.write = true;
 
 			if (!strncmp("min=", optarg, 4)) {
 				act_upon[MIN] = true;
@@ -408,7 +413,7 @@ parse(int argc, char *argv[], bool *act_upon, size_t act_upon_n, int *new_freq)
 			new_freq[MAX] = tmp;
 			act_upon[MIN] = true;
 			act_upon[MAX] = true;
-			write = true;
+			result.write = true;
 			break;
 		case 'i': /* mIn */
 			tmp = get_frequency(&info[RPn]);
@@ -416,7 +421,7 @@ parse(int argc, char *argv[], bool *act_upon, size_t act_upon_n, int *new_freq)
 			new_freq[MAX] = tmp;
 			act_upon[MIN] = true;
 			act_upon[MAX] = true;
-			write = true;
+			result.write = true;
 			break;
 		case 'm': /* max */
 			tmp = get_frequency(&info[RP0]);
@@ -424,14 +429,14 @@ parse(int argc, char *argv[], bool *act_upon, size_t act_upon_n, int *new_freq)
 			new_freq[MAX] = tmp;
 			act_upon[MIN] = true;
 			act_upon[MAX] = true;
-			write = true;
+			result.write = true;
 			break;
 		case 'd': /* defaults */
 			new_freq[MIN] = get_frequency(&info[RPn]);
 			new_freq[MAX] = get_frequency(&info[RP0]);
 			act_upon[MIN] = true;
 			act_upon[MAX] = true;
-			write = true;
+			result.write = true;
 			break;
 		default:
 			fprintf(stderr, "Error: unknown option\n");
@@ -440,14 +445,15 @@ parse(int argc, char *argv[], bool *act_upon, size_t act_upon_n, int *new_freq)
 	}
 
 done:
-	return write;
+	return result;
 }
 
 int main(int argc, char *argv[])
 {
 
-	bool write, fail, targets[MAX+1] = {false};
+	bool fail, targets[MAX+1] = {false};
 	int i, fd, try = 1, set_freq[MAX+1] = {0};
+	struct parse_result result;
 
 	parse_version_or_help(argc, argv);
 
@@ -461,8 +467,8 @@ int main(int argc, char *argv[])
 		close(fd);
 	}
 
-	write = parse(argc, argv, targets, ARRAY_SIZE(targets), set_freq);
-	fail = write;
+	result = parse(argc, argv, targets, ARRAY_SIZE(targets), set_freq);
+	fail = result.write;
 
 	/* If we've previously locked the frequency, we need to make sure to set things
 	 * in the correct order, or else the operation will fail (ie. min = max = 200,
@@ -478,18 +484,24 @@ again:
 		fprintf(stderr, "Did not achieve desired freq.\n");
 		exit(EXIT_FAILURE);
 	}
-	for (i = 0; i < ARRAY_SIZE(targets); i++) {
-		if (targets[i] == false)
-			continue;
 
-		if (write) {
+	if (result.write) {
+		/* only reset and set max & min act_upons flags if a read operation is also requested */
+		if (result.read) {
+			for (int j = 0; j < ARRAY_SIZE(targets); j++)
+				targets[j] = false;
+			targets[MIN] = true;
+			targets[MAX] = true;
+		}
+
+		for (i = 0; i < ARRAY_SIZE(targets); i++) {
+			if (targets[i] == false)
+				continue;
 			set_frequency(&info[i], set_freq[i]);
 			if (get_frequency(&info[i]) != set_freq[i])
 				fail = true;
 			else
 				fail = false;
-		} else {
-			printf("%s: %d MHz\n", info[i].name, get_frequency(&info[i]));
 		}
 	}
 
@@ -498,6 +510,13 @@ again:
 		goto again;
 	}
 
+	if (result.read) {
+		for (i = 0; i < ARRAY_SIZE(targets); i++) {
+			/* loop for all frequencies for get operations */
+			printf("%s: %d MHz\n", info[i].name, get_frequency(&info[i]));
+		}
+	}
+
 	for (i = 0; i < ARRAY_SIZE(targets); i++) {
 		if (info[i].filp) {
 			fclose(info[i].filp);
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH i-g-t v6 1/3] tools/intel_gpu_frequency: Add support for get, set in order & vice-versa

Hi S,

Quoted 51 lines · S Sebinraj

Add proper conditions for handling get and set in order (also vice-versa), as the same was failing before with the below error. To note here, In this scenario w.r.t set operation, only writable sysfs path has to be used ie. [min, max].

Error: set_frequency:

        Assertion `fprintf(freq_info->filp, "%d", val) > 0' failed.
---
 tools/intel_gpu_frequency.c | 63 ++++++++++++++++++++++++-------------
 1 file changed, 41 insertions(+), 22 deletions(-)
tools/intel_gpu_frequency.c +8 -2
index 6316c2e58..6b6997ab1 100644
@@ -63,6 +63,11 @@ static struct freq_info info[] = {
 	{ "RPn", "r" }
 };
 
+struct parse_result {
+	bool write;
+	bool read;
+};
+
 static char *
 get_sysfs_path(const char *which)
 {
@@ -319,16 +324,17 @@ parse_version_or_help(int argc, char *argv[])
 }
 
 /* Returns read or write operation */
-static bool
+static struct parse_result
 parse(int argc, char *argv[], bool *act_upon, size_t act_upon_n, int *new_freq)
 {
 	int c, tmp;
-	bool write = false;
+	struct parse_result result = { .write = false, .read = false };
 
 	/* No args means -g" */
 	if (argc == 1) {
 		for (c = 0; c < act_upon_n; c++)
 			act_upon[c] = true;
+		result.read = true;
 		goto done;
 	}
 	while (1) {
@@ -351,24 +357,23 @@ parse(int argc, char *argv[], bool *act_upon, size_t act_upon_n, int *new_freq)
 

tools/intel_gpu_frequency.c parse(int argc, char *argv[], bool *act_upon, size_t act_upon_n, int *new_freq) · line 365

Quoted 10 lines
tools/intel_gpu_frequency.c +1 -2
 		switch (c) {
 		case 'g':
-			if (write == true)
-				fprintf(stderr, "Read and write operations not support simultaneously.\n");
 			{
 				int i;
 				for (i = 0; i < act_upon_n; i++)
 					act_upon[i] = true;
 			}
+			result.read = true;

imho this could lead to a mess, especially when mixing different operations. What you think about renaming this function and just do the desired action in place here and in other parsing places? For both write and read, also later for setting freq for specific GT like GT1?

One more thing to consider would be to allow printing all freq for all Intel GPUs, be it i915 or Xe. New option for it could be named -a|--all

Last thing - please keep old way of display only few freqs for one operation, so it will keep compatibility with old behaviour.

Regards,
Kamil

Quoted 143 lines
tools/intel_gpu_frequency.c +32 -18
 			break;
 		case 's':
 			if (!optarg)
 				usage(argv[0], EXIT_FAILURE);
 
-			if (write == true) {
+			if (result.write == true) {
 				fprintf(stderr, "Only one write may be specified at a time\n");
 				exit(EXIT_FAILURE);
 			}
 
-			write = true;
+			result.write = true;
 			act_upon[MIN] = true;
 			act_upon[MAX] = true;
 			sscanf(optarg, "%d", &new_freq[MAX]);
@@ -378,12 +383,12 @@ parse(int argc, char *argv[], bool *act_upon, size_t act_upon_n, int *new_freq)
 			if (!optarg)
 				usage(argv[0], EXIT_FAILURE);
 
-			if (write == true) {
+			if (result.write == true) {
 				fprintf(stderr, "Only one write may be specified at a time\n");
 				exit(EXIT_FAILURE);
 			}
 
-			write = true;
+			result.write = true;
 
 			if (!strncmp("min=", optarg, 4)) {
 				act_upon[MIN] = true;
@@ -408,7 +413,7 @@ parse(int argc, char *argv[], bool *act_upon, size_t act_upon_n, int *new_freq)
 			new_freq[MAX] = tmp;
 			act_upon[MIN] = true;
 			act_upon[MAX] = true;
-			write = true;
+			result.write = true;
 			break;
 		case 'i': /* mIn */
 			tmp = get_frequency(&info[RPn]);
@@ -416,7 +421,7 @@ parse(int argc, char *argv[], bool *act_upon, size_t act_upon_n, int *new_freq)
 			new_freq[MAX] = tmp;
 			act_upon[MIN] = true;
 			act_upon[MAX] = true;
-			write = true;
+			result.write = true;
 			break;
 		case 'm': /* max */
 			tmp = get_frequency(&info[RP0]);
@@ -424,14 +429,14 @@ parse(int argc, char *argv[], bool *act_upon, size_t act_upon_n, int *new_freq)
 			new_freq[MAX] = tmp;
 			act_upon[MIN] = true;
 			act_upon[MAX] = true;
-			write = true;
+			result.write = true;
 			break;
 		case 'd': /* defaults */
 			new_freq[MIN] = get_frequency(&info[RPn]);
 			new_freq[MAX] = get_frequency(&info[RP0]);
 			act_upon[MIN] = true;
 			act_upon[MAX] = true;
-			write = true;
+			result.write = true;
 			break;
 		default:
 			fprintf(stderr, "Error: unknown option\n");
@@ -440,14 +445,15 @@ parse(int argc, char *argv[], bool *act_upon, size_t act_upon_n, int *new_freq)
 	}
 
 done:
-	return write;
+	return result;
 }
 
 int main(int argc, char *argv[])
 {
 
-	bool write, fail, targets[MAX+1] = {false};
+	bool fail, targets[MAX+1] = {false};
 	int i, fd, try = 1, set_freq[MAX+1] = {0};
+	struct parse_result result;
 
 	parse_version_or_help(argc, argv);
 
@@ -461,8 +467,8 @@ int main(int argc, char *argv[])
 		close(fd);
 	}
 
-	write = parse(argc, argv, targets, ARRAY_SIZE(targets), set_freq);
-	fail = write;
+	result = parse(argc, argv, targets, ARRAY_SIZE(targets), set_freq);
+	fail = result.write;
 
 	/* If we've previously locked the frequency, we need to make sure to set things
 	 * in the correct order, or else the operation will fail (ie. min = max = 200,
@@ -478,18 +484,24 @@ again:
 		fprintf(stderr, "Did not achieve desired freq.\n");
 		exit(EXIT_FAILURE);
 	}
-	for (i = 0; i < ARRAY_SIZE(targets); i++) {
-		if (targets[i] == false)
-			continue;
 
-		if (write) {
+	if (result.write) {
+		/* only reset and set max & min act_upons flags if a read operation is also requested */
+		if (result.read) {
+			for (int j = 0; j < ARRAY_SIZE(targets); j++)
+				targets[j] = false;
+			targets[MIN] = true;
+			targets[MAX] = true;
+		}
+
+		for (i = 0; i < ARRAY_SIZE(targets); i++) {
+			if (targets[i] == false)
+				continue;
 			set_frequency(&info[i], set_freq[i]);
 			if (get_frequency(&info[i]) != set_freq[i])
 				fail = true;
 			else
 				fail = false;
-		} else {
-			printf("%s: %d MHz\n", info[i].name, get_frequency(&info[i]));
 		}
 	}
 
@@ -498,6 +510,13 @@ again:
 		goto again;
 	}
 
+	if (result.read) {
+		for (i = 0; i < ARRAY_SIZE(targets); i++) {
+			/* loop for all frequencies for get operations */
+			printf("%s: %d MHz\n", info[i].name, get_frequency(&info[i]));
+		}
+	}
+
 	for (i = 0; i < ARRAY_SIZE(targets); i++) {
 		if (info[i].filp) {
 			fclose(info[i].filp);
Signature · 1 line
2.43.0
author
#3 lore raw

PATCH v6 2/3 tools/intel_gpu_frequency: Add multi-GT support

In reply to S Sebinraj

This change enhances intel_gpu_frequency to support Intel GPUs with multiple Graphics Technology (GT) units.

Key improvements:
- Add runtime GT detection using igt_sysfs_get_num_gt()
- New --gt/-t option to specify target GT (defaults to GT0)
- New --list/-l option to display available GTs
- Update sysfs path generation to use igt_sysfs_gt_path()
- Update the man page for new options

Usage examples:
intel_gpu_frequency --get --gt 1 # Get GT1 frequencies
intel_gpu_frequency --set 400 --gt 0 # Set GT0 frequency
intel_gpu_frequency --list # List available GTs

The tool now dynamically detects available GTs at runtime and provides error messages when invalid GT numbers are specified. Also by default for set / get operations it acts upon all available GTs unless specified.

---
 man/intel_gpu_frequency.rst |  34 ++++-
 tools/intel_gpu_frequency.c | 270 ++++++++++++++++++++++++++++--------
 2 files changed, 238 insertions(+), 66 deletions(-)
man/intel_gpu_frequency.rst +29 -5
index 92f039645..0daa22ba5 100644
@@ -38,7 +38,8 @@ OPTIONS
     Lock frequency to the most efficient frequency.
 
 -g, --get
-    Get all the current frequency settings.
+    Get all the current frequency settings. Displays results
+    showing all GTs with their corresponding frequencies.
 
 -s FREQUENCY, --set=FREQUENCY
     Lock frequency to an absolute value (MHz).
@@ -55,6 +56,14 @@ OPTIONS
 -d, --defaults
     Return the system to hardware defaults.
 
+-t GT_NUMBER, --gt=GT_NUMBER
+    Specify which GT (Graphics Technology) to operate on. GT numbers start from 0.
+    If not specified, operations apply to all available GTs. Use with --list
+    to see available GTs.
+
+-l, --list
+    List all available GTs in the system with their sysfs paths.
+
 -h, --help
     Show help.
 
@@ -64,14 +73,29 @@ OPTIONS
 EXAMPLES
 ========
 
-intel_gpu_frequency -gmin,cur
-    Get the current and minimum frequency.
+intel_gpu_frequency
+    Display frequency information for all GTs in table format.
+
+intel_gpu_frequency -g
+    Get frequency settings for all GTs (displays in table format).
+
+intel_gpu_frequency --list
+    List all available GTs in the system.
+
+intel_gpu_frequency --get --gt 1
+    Get frequency settings for GT1 only.
 
 intel_gpu_frequency -s 400
-    Lock frequency to 400Mhz.
+    Lock frequency to 400Mhz for all GTs.
+
+intel_gpu_frequency -s 400 --gt 0
+    Lock frequency to 400Mhz for GT0 only.
 
 intel_gpu_frequency -c max=750
-    Set the max frequency to 750MHz
+    Set the max frequency to 750MHz for all GTs.
+
+intel_gpu_frequency -c max=750 --gt 1
+    Set the max frequency to 750MHz for GT1 only.
 
 REPORTING BUGS
 ==============
tools/intel_gpu_frequency.c +209 -61

446 lines not on this page · raw message

Signature · 1 line
2.43.0
#4 lore raw

Re: [PATCH i-g-t v6 2/3] tools/intel_gpu_frequency: Add multi-GT support

Hi,

Quoted 63 lines · S Sebinraj

This change enhances intel_gpu_frequency to support Intel GPUs with multiple Graphics Technology (GT) units.

Key improvements:
- Add runtime GT detection using igt_sysfs_get_num_gt()
- New --gt/-t option to specify target GT (defaults to GT0)
- New --list/-l option to display available GTs
- Update sysfs path generation to use igt_sysfs_gt_path()
- Update the man page for new options

Usage examples:
intel_gpu_frequency --get --gt 1 # Get GT1 frequencies
intel_gpu_frequency --set 400 --gt 0 # Set GT0 frequency
intel_gpu_frequency --list # List available GTs

The tool now dynamically detects available GTs at runtime and provides error messages when invalid GT numbers are specified. Also by default for set / get operations it acts upon all available GTs unless specified.

---
 man/intel_gpu_frequency.rst |  34 ++++-
 tools/intel_gpu_frequency.c | 270 ++++++++++++++++++++++++++++--------
 2 files changed, 238 insertions(+), 66 deletions(-)
man/intel_gpu_frequency.rst +11 -3
index 92f039645..0daa22ba5 100644
@@ -38,7 +38,8 @@ OPTIONS
     Lock frequency to the most efficient frequency.
 
 -g, --get
-    Get all the current frequency settings.
+    Get all the current frequency settings. Displays results
+    showing all GTs with their corresponding frequencies.
 
 -s FREQUENCY, --set=FREQUENCY
     Lock frequency to an absolute value (MHz).
@@ -55,6 +56,14 @@ OPTIONS
 -d, --defaults
     Return the system to hardware defaults.
 
+-t GT_NUMBER, --gt=GT_NUMBER
+    Specify which GT (Graphics Technology) to operate on. GT numbers start from 0.
+    If not specified, operations apply to all available GTs. Use with --list
+    to see available GTs.
+
+-l, --list
+    List all available GTs in the system with their sysfs paths.
+
 -h, --help
     Show help.
 
@@ -64,14 +73,29 @@ OPTIONS
 EXAMPLES
 ========
 
-intel_gpu_frequency -gmin,cur
-    Get the current and minimum frequency.
+intel_gpu_frequency

man/intel_gpu_frequency.rst OPTIONS · line 86

Quoted 10 lines
man/intel_gpu_frequency.rst +10 -0
+    Display frequency information for all GTs in table format.
+
+intel_gpu_frequency -g
+    Get frequency settings for all GTs (displays in table format).
+
+intel_gpu_frequency --list
+    List all available GTs in the system.
+
+intel_gpu_frequency --get --gt 1
+    Get frequency settings for GT1 only.

This will make it hard to apply more commands in same line, imho better way would be to have it like:

intel_gpu_frequency -g
Get frequency settings for GT0.
so this will keep old behaviour.

intel_gpu_frequency -g --gt 1
Print frequencies for GT0 and then GT1.

intel_gpu_frequency --gt 1 -g
Print frequency for GT1.

man/intel_gpu_frequency.rst OPTIONS · line 95

Quoted 11 lines
man/intel_gpu_frequency.rst +5 -2
 intel_gpu_frequency -s 400
-    Lock frequency to 400Mhz.
+    Lock frequency to 400Mhz for all GTs.
+
+intel_gpu_frequency -s 400 --gt 0
+    Lock frequency to 400Mhz for GT0 only.
 
 intel_gpu_frequency -c max=750
-    Set the max frequency to 750MHz
+    Set the max frequency to 750MHz for all GTs.

This should be reserved to only one selected GT.
What about:

intel_gpu_frequency -a -c max
Set max freq for all tiles and GTs.

Quoted 14 lines
man/intel_gpu_frequency.rst +4 -1
-    Set the max frequency to 750MHz
+    Set the max frequency to 750MHz for all GTs.
+
+intel_gpu_frequency -c max=750 --gt 1
+    Set the max frequency to 750MHz for GT1 only.
 
 REPORTING BUGS
 ==============
tools/intel_gpu_frequency.c +0 -0
index 6b6997ab1..695a483fd 100644
@@ -26,14 +26,18 @@
 #include <fcntl.h>

tools/intel_gpu_frequency.c line 36

Quoted 10 lines
tools/intel_gpu_frequency.c +3 -0
 #include <getopt.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 #include <time.h>
 #include <unistd.h>
 
 #include "drmtest.h"
 #include "igt_device.h"
+#include "igt_sysfs.h"

This is not a good choice, also note drmtest.h above should be removed as well.

Quoted 20 lines
tools/intel_gpu_frequency.c +3 -3
 #include "intel_chipset.h"
 
 #define VERSION "1.0"
+#define SYSFS_PATH_MAX 96
 
 static int device, devid;
 
@@ -63,22 +67,31 @@ static struct freq_info info[] = {
 	{ "RPn", "r" }
 };
 
-struct parse_result {
+typedef struct parse_result {
 	bool write;
 	bool read;
-};
+} parse_result_t;
 
 static char *
-get_sysfs_path(const char *which)

tools/intel_gpu_frequency.c static struct freq_info info[] = { · line 82

Quoted 10 lines
tools/intel_gpu_frequency.c +6 -3
+get_sysfs_path(int fd, int gt_id, const char *which)
 {
-	static const char fmt[] = "/sys/class/drm/card%1d/gt_%3s_freq_mhz";
-	char *path;
-	int ret;
+	static char path[SYSFS_PATH_MAX];
+	char *gt_path;
+	int path_len, ret;
+
+	gt_path = igt_sysfs_gt_path(fd, gt_id, path, sizeof(path));

This library function uses fd for opened card/render, imho we should not using it. I will explain reasons in reply to 3rd patch.

Reagrds,
Kamil

Quoted 424 lines
tools/intel_gpu_frequency.c +197 -55
+	if (gt_path == NULL) {
+		fprintf(stderr, "igt_sysfs_gt_path returned NULL for GT%d\n", gt_id);
+		return NULL;
+	}
+	path_len = strlen(gt_path);
+	ret = snprintf(path + path_len, sizeof(path) - path_len,
+		       "/rps_%s_freq_mhz", which);
 
-#define STATIC_STRLEN(string) (sizeof(string) / sizeof(string [0]))
-	ret = asprintf(&path, fmt, device, which);
-	assert(ret == (STATIC_STRLEN(fmt) - 3));
-#undef STATIC_STRLEN
+	if (ret >= sizeof(path) - path_len) {
+		fprintf(stderr, "Path truncated\n");
+		return NULL;
+	}
 
 	return path;
 }
@@ -193,28 +206,24 @@ get_intel_card(void)
 }
 
 static void
-initialize_read_info(struct freq_info *freq_info)
+initialize_read_info(struct freq_info *freq_info, int fd, int gt_id)
 {
 	if (freq_info->filp)
-		return;
+		fclose(freq_info->filp);
 
-	freq_info->path = get_sysfs_path(freq_info->name);
+	freq_info->path = get_sysfs_path(fd, gt_id, freq_info->name);
 	assert(freq_info->path);
 	freq_info->filp = fopen(freq_info->path, "r");
 	assert(freq_info->filp);
 }
 
 static void
-initialize_write_info(struct freq_info *freq_info)
+initialize_write_info(struct freq_info *freq_info, int fd, int gt_id)
 {
-	if (freq_info->write)
-		return;
-
 	if (freq_info->filp)
 		fclose(freq_info->filp);
-	else
-		freq_info->path = get_sysfs_path(freq_info->name);
 
+	freq_info->path = get_sysfs_path(fd, gt_id, freq_info->name);
 	assert(freq_info->path);
 	freq_info->filp = fopen(freq_info->path, freq_info->mode);
 	if (!(freq_info->filp)) {
@@ -222,8 +231,6 @@ initialize_write_info(struct freq_info *freq_info)
 			freq_info->path, freq_info->mode, errno, strerror(errno));
 		exit(EXIT_FAILURE);
 	}
-
-	freq_info->write = true;
 }
 
 static void wait_freq_settle(void)
@@ -236,20 +243,20 @@ static void wait_freq_settle(void)
 	clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL);
 }
 
-static void set_frequency(struct freq_info *freq_info, int val)
+static void set_frequency(struct freq_info *freq_info, int val, int fd, int gt_id)
 {
-	initialize_write_info(freq_info);
+	initialize_write_info(freq_info, fd, gt_id);
 	rewind(freq_info->filp);
 	assert(fprintf(freq_info->filp, "%d", val) > 0);
 
 	wait_freq_settle();
 }
 
-static int get_frequency(struct freq_info *freq_info)
+static int get_frequency(struct freq_info *freq_info, int fd, int gt_id)
 {
 	int val;
 
-	initialize_read_info(freq_info);
+	initialize_read_info(freq_info, fd, gt_id);
 	rewind(freq_info->filp);
 	assert(fscanf(freq_info->filp, "%d", &val)==1);
 
@@ -270,7 +277,7 @@ usage(const char *prog, int err)
 	const char *shortname = get_shortname(prog);
 
 	printf("%s A program to manipulate Intel GPU frequencies.\n\n", shortname);
-	printf("Usage: %s [-e] [--min | --max] [--get] [--set frequency_mhz]\n\n", shortname);
+	printf("Usage: %s [-e] [--min | --max] [--get] [--set frequency_mhz] [--gt GT_NUM] [--list]\n\n", shortname);
 	printf("Options: \n");
 	printf("  -e		Lock frequency to the most efficient frequency\n");
 	printf("  -g, --get     Get all the frequency settings\n");
@@ -279,6 +286,8 @@ usage(const char *prog, int err)
 	printf("  -m  --max     Lock frequency to max frequency\n");
 	printf("  -i  --min     Lock frequency to min (never a good idea, DEBUG ONLY)\n");
 	printf("  -d  --defaults  Return the system to hardware defaults\n");
+	printf("  -t  --gt      Specify GT number to operate on (default: all GTs)\n");
+	printf("  -l  --list    List all available GTs\n");
 	printf("  -h  --help    Returns this\n");
 	printf("  -v  --version Version\n");
 	printf("\n");
@@ -286,6 +295,8 @@ usage(const char *prog, int err)
 	printf("   intel_gpu_frequency --get\t\tGet the current and minimum frequency\n");
 	printf("   intel_gpu_frequency --set 400\tLock frequency to 400Mhz\n");
 	printf("   intel_gpu_frequency --custom max=750\tSet the max frequency to 750MHz\n");
+	printf("   intel_gpu_frequency --get --gt 1\tGet frequencies for GT1\n");
+	printf("   intel_gpu_frequency --list\t\tList all available GTs\n");
 	printf("\n");
 	printf("Report bugs to https://gitlab.freedesktop.org/drm/igt-gpu-tools/-/issues\n");
 	exit(err);
@@ -324,11 +335,13 @@ parse_version_or_help(int argc, char *argv[])
 }
 
 /* Returns read or write operation */
-static struct parse_result
-parse(int argc, char *argv[], bool *act_upon, size_t act_upon_n, int *new_freq)
+static parse_result_t
+parse(int argc, char *argv[], bool *act_upon, size_t act_upon_n, int *new_freq,
+      int *specified_gt, bool *list_gts, int fd)
 {
 	int c, tmp;
-	struct parse_result result = { .write = false, .read = false };
+	parse_result_t result = { .write = false, .read = false };
+	bool gtt_specified = false;
 
 	/* No args means -g" */
 	if (argc == 1) {
@@ -346,12 +359,14 @@ parse(int argc, char *argv[], bool *act_upon, size_t act_upon_n, int *new_freq)
 			{ "min", no_argument, NULL, 'i' },
 			{ "max", no_argument, NULL, 'm' },
 			{ "defaults", no_argument, NULL, 'd' },
+			{ "gt", required_argument, NULL, 't' },
+			{ "list", no_argument, NULL, 'l' },
 			{ "help", no_argument, NULL, 'h' },
 			{ "version", no_argument, NULL, 'v' },
 			{ NULL, 0, NULL, 0}
 		};
 
-		c = getopt_long(argc, argv, "egs:c:midh", long_options, &option_index);
+		c = getopt_long(argc, argv, "egs:c:midt:lhv", long_options, &option_index);
 		if (c == -1)
 			break;
 
@@ -408,7 +423,8 @@ parse(int argc, char *argv[], bool *act_upon, size_t act_upon_n, int *new_freq)
 					"FIXME: Warning efficient frequency information is incorrect.\n");
 				exit(EXIT_FAILURE);
 			}
-			tmp = get_frequency(&info[EFF]);
+			tmp = get_frequency(&info[EFF], fd,
+					    *specified_gt >= 0 ? *specified_gt : 0);
 			new_freq[MIN] = tmp;
 			new_freq[MAX] = tmp;
 			act_upon[MIN] = true;
@@ -416,7 +432,8 @@ parse(int argc, char *argv[], bool *act_upon, size_t act_upon_n, int *new_freq)
 			result.write = true;
 			break;
 		case 'i': /* mIn */
-			tmp = get_frequency(&info[RPn]);
+			tmp = get_frequency(&info[RPn], fd,
+					    *specified_gt >= 0 ? *specified_gt : 0);
 			new_freq[MIN] = tmp;
 			new_freq[MAX] = tmp;
 			act_upon[MIN] = true;
@@ -424,7 +441,8 @@ parse(int argc, char *argv[], bool *act_upon, size_t act_upon_n, int *new_freq)
 			result.write = true;
 			break;
 		case 'm': /* max */
-			tmp = get_frequency(&info[RP0]);
+			tmp = get_frequency(&info[RP0], fd,
+					    *specified_gt >= 0 ? *specified_gt : 0);
 			new_freq[MIN] = tmp;
 			new_freq[MAX] = tmp;
 			act_upon[MIN] = true;
@@ -432,12 +450,36 @@ parse(int argc, char *argv[], bool *act_upon, size_t act_upon_n, int *new_freq)
 			result.write = true;
 			break;
 		case 'd': /* defaults */
-			new_freq[MIN] = get_frequency(&info[RPn]);
-			new_freq[MAX] = get_frequency(&info[RP0]);
+			new_freq[MIN] = get_frequency(&info[RPn], fd,
+						      *specified_gt >= 0 ? *specified_gt : 0);
+			new_freq[MAX] = get_frequency(&info[RP0], fd,
+						      *specified_gt >= 0 ? *specified_gt : 0);
 			act_upon[MIN] = true;
 			act_upon[MAX] = true;
 			result.write = true;
 			break;
+		case 't': /* gt */
+			if (!optarg)
+				usage(argv[0], EXIT_FAILURE);
+
+			if (gtt_specified) {
+				fprintf(stderr, "Only one GT may be specified at a time\n");
+				exit(EXIT_FAILURE);
+			}
+			*specified_gt = atoi(optarg);
+			if (*specified_gt < 0) {
+				fprintf(stderr, "Invalid GT number: %d\n", *specified_gt);
+				exit(EXIT_FAILURE);
+			}
+			gtt_specified = true;
+			break;
+		case 'l': /* list */
+			*list_gts = true;
+			break;
+		case 'v':
+			version(argv[0]);
+			exit(0);
+		case 'h':
 		default:
 			fprintf(stderr, "Error: unknown option\n");
 			usage(argv[0], EXIT_FAILURE);
@@ -448,27 +490,92 @@ done:
 	return result;
 }
 
-int main(int argc, char *argv[])
+static void print_freq_table_header(void)
 {
+	printf("%-15s %-15s %6s %6s %6s %6s %6s %6s %3s\n",
+	       "card/tile/GT", "BDF", "min", "curr", "RP1", "max", "RP0", "RPn", "(MHz)");
+	printf("%-15s %-15s %6s %6s %6s %6s %6s %6s\n",
+	       "------------", "------------", "----", "----", "----", "----", "----", "----");
+}
 
-	bool fail, targets[MAX+1] = {false};
-	int i, fd, try = 1, set_freq[MAX+1] = {0};
-	struct parse_result result;
+static void print_freq_table_row(int fd, int card, int gt_id)
+{
+	char bdf[16] = "N/A";
+	char gt_label[16];
+	int freqs[ARRAY_SIZE(info)];
+	int i;
+
+	/* Get BDF (Bus:Device.Function) for this device */
+	igt_device_get_pci_slot_name(fd, bdf);
+
+	/* Format GT label as card/tile/GT */
+	snprintf(gt_label, sizeof(gt_label), "%d/t0/GT%d", card, gt_id);
+
+	/* Read all frequencies */
+	for (i = 0; i < ARRAY_SIZE(info); i++)
+		freqs[i] = get_frequency(&info[i], fd, gt_id);
+
+	/* Print the row: GT label, BDF, and all frequencies */
+	printf("%-15s %-15s %6d %6d %6d %6d %6d %6d\n",
+	       gt_label, bdf,
+	       freqs[MIN], freqs[CUR], freqs[EFF],
+	       freqs[MAX], freqs[RP0], freqs[RPn]);
+}
 
-	parse_version_or_help(argc, argv);
+static void process_gt_frequencies(int argc, int fd, int gt_id, parse_result_t result,
+						bool *targets, int *set_freq);
 
-	device = get_intel_card();
-	if (device < 0)
-		exit_error("No Intel card found in /dev/dri\n", 0);
+static void list_available_gts(int fd, int num_gts)
+{
+	int i;
 
-	fd = __drm_open_driver(DRIVER_INTEL);
-	if (fd >= 0) {
-		devid = intel_get_drm_devid(fd);
-		close(fd);
+	printf("Available GTs: %d\n", num_gts);
+	for (i = 0; i < num_gts; i++) {
+		char path[SYSFS_PATH_MAX];
+
+		if (igt_sysfs_gt_path(fd, i, path, sizeof(path)))
+			printf("  GT%d: %s\n", i, path);
+	}
+}
+
+static void validate_specified_gt(int fd, int specified_gt, int num_gts)
+{
+	if (specified_gt >= 0) {
+		if (specified_gt >= num_gts) {
+			fprintf(stderr, "Error: GT%d does not exist. Available GTs: 0-%d\n",
+				specified_gt, num_gts - 1);
+			close(fd);
+			exit(EXIT_FAILURE);
+		}
+		igt_debug("Operating on GT%d (Total GTs: %d)\n", specified_gt, num_gts);
+	} else {
+		igt_debug("Operating on all GTs (Total GTs: %d)\n", num_gts);
+	}
+}
+
+static void execute_frequency_operations(int argc, int fd, int specified_gt, parse_result_t result,
+								bool *targets, int *set_freq)
+{
+	int target_gt;
+
+	if (specified_gt >= 0) {
+		/* Process single specified GT */
+		process_gt_frequencies(argc, fd, specified_gt, result, targets, set_freq);
+	} else {
+		/* Process all GTs */
+		int num_gts = igt_sysfs_get_num_gt(fd);
+
+		for (target_gt = 0; target_gt < num_gts; target_gt++)
+			process_gt_frequencies(argc, fd, target_gt, result, targets, set_freq);
 	}
+}
 
-	result = parse(argc, argv, targets, ARRAY_SIZE(targets), set_freq);
-	fail = result.write;
+static void process_gt_frequencies(int argc, int fd, int gt_id, parse_result_t result,
+						bool *targets, int *set_freq)
+{
+	int i, try = 1;
+	bool fail = result.write;
+	static bool table_header_printed = false;
 
 	/* If we've previously locked the frequency, we need to make sure to set things
 	 * in the correct order, or else the operation will fail (ie. min = max = 200,
@@ -481,24 +588,27 @@ int main(int argc, char *argv[])
 	 */
 again:
 	if (try > 2) {
-		fprintf(stderr, "Did not achieve desired freq.\n");
+		fprintf(stderr, "Did not achieve desired freq on GT%d.\n", gt_id);
 		exit(EXIT_FAILURE);
 	}
 
 	if (result.write) {
-		/* only reset and set max & min act_upons flags if a read operation is also requested */
-		if (result.read) {
-			for (int j = 0; j < ARRAY_SIZE(targets); j++)
+		/* only reset and set max & min act_upons flags
+		 * if a read operation is also requested
+		 */
+		if (result.read != false) {
+			for (int j = 0; j < ARRAY_SIZE(info); j++)
 				targets[j] = false;
 			targets[MIN] = true;
 			targets[MAX] = true;
 		}
 
-		for (i = 0; i < ARRAY_SIZE(targets); i++) {
+		for (i = 0; i < ARRAY_SIZE(info); i++) {
 			if (targets[i] == false)
 				continue;
-			set_frequency(&info[i], set_freq[i]);
-			if (get_frequency(&info[i]) != set_freq[i])
+
+			set_frequency(&info[i], set_freq[i], fd, gt_id);
+			if (get_frequency(&info[i], fd, gt_id) != set_freq[i])
 				fail = true;
 			else
 				fail = false;
@@ -510,19 +620,57 @@ again:
 		goto again;
 	}
 
-	if (result.read) {
-		for (i = 0; i < ARRAY_SIZE(targets); i++) {
-			/* loop for all frequencies for get operations */
-			printf("%s: %d MHz\n", info[i].name, get_frequency(&info[i]));
+	if (result.read || argc == 1) {
+		/* Print table header only once for all GTs */
+		if (!table_header_printed) {
+			print_freq_table_header();
+			table_header_printed = true;
 		}
+
+		/* Print frequency row for this GT */
+		print_freq_table_row(fd, device, gt_id);
+	}
+}
+
+int main(int argc, char *argv[])
+{
+
+	bool list_gts = false, targets[MAX+1] = {false};
+	int i, fd, set_freq[MAX+1] = {0};
+	int specified_gt = -1;
+	int num_gts;
+	parse_result_t result;
+
+	parse_version_or_help(argc, argv);
+
+	device = get_intel_card();
+	if (device < 0)
+		exit_error("No Intel card found in /dev/dri\n", 0);
+
+	fd = __drm_open_driver(DRIVER_INTEL);
+	devid = intel_get_drm_devid(fd);
+	device = igt_device_get_card_index(fd);
+	num_gts = igt_sysfs_get_num_gt(fd);
+
+	result = parse(argc, argv, targets, ARRAY_SIZE(targets), set_freq,
+		      &specified_gt, &list_gts, fd);
+
+	if (list_gts) {
+		list_available_gts(fd, num_gts);
+		close(fd);
+		return EXIT_SUCCESS;
 	}
 
+	validate_specified_gt(fd, specified_gt, num_gts);
+
+	execute_frequency_operations(argc, fd, specified_gt, result, targets, set_freq);
+
 	for (i = 0; i < ARRAY_SIZE(targets); i++) {
 		if (info[i].filp) {
 			fclose(info[i].filp);
-			free(info[i].path);
 		}
 	}
 
+	close(fd);
 	return EXIT_SUCCESS;
 }
Signature · 1 line
2.43.0
author
#5 lore raw

PATCH v6 3/3 tools/intel_gpu_frequency: Add Xe driver support

In reply to S Sebinraj

This change enhances intel_gpu_frequency to add compatibility with Xe drivers.

Key improvements:
- Add runtime GT detection using xe_number_gt()
- Auto-detect driver type using is_xe_device()
- Driver-agnostic frequency management with unified interface

Driver-specific implementations:
- Automatic frequency name mapping (RP1→rpe, RP0→rp0, RPn→rpn for Xe)

The tool now auto-detects Xe drivers and uses appropriate interfaces.

---
 tools/intel_gpu_frequency.c | 81 ++++++++++++++++++++++++++++++-------
 1 file changed, 66 insertions(+), 15 deletions(-)
tools/intel_gpu_frequency.c +66 -15
index 695a483fd..9eeaa1223 100644
@@ -35,11 +35,13 @@
 #include "igt_device.h"
 #include "igt_sysfs.h"
 #include "intel_chipset.h"
+#include "xe/xe_query.h"
 
 #define VERSION "1.0"
 #define SYSFS_PATH_MAX 96
 
 static int device, devid;
+static bool is_xe;
 
 enum {
 	CUR=0,
@@ -61,10 +63,10 @@ struct freq_info {
 static struct freq_info info[] = {
 	{ "cur", "r"  },
 	{ "min", "rb+" },
-	{ "RP1", "r" },
+	{ "RP1", "r" },    /* i915: RP1, xe: rpe */
 	{ "max", "rb+" },
-	{ "RP0", "r" },
-	{ "RPn", "r" }
+	{ "RP0", "r" },    /* i915: RP0, xe: rp0 */
+	{ "RPn", "r" }     /* i915: RPn, xe: rpn */
 };
 
 typedef struct parse_result {
@@ -72,21 +74,49 @@ typedef struct parse_result {
 	bool read;
 } parse_result_t;
 
+static int driver_get_num_gts(int fd)
+{
+	if (is_xe) {
+		return xe_number_gt(fd);
+	} else {
+		return igt_sysfs_get_num_gt(fd);
+	}
+}
+
 static char *
 get_sysfs_path(int fd, int gt_id, const char *which)
 {
 	static char path[SYSFS_PATH_MAX];
 	char *gt_path;
 	int path_len, ret;
+	const char *freq_name;
+
+	/* Get driver-specific GT path */
+	if (is_xe)
+		gt_path = xe_sysfs_gt_path(fd, gt_id, path, sizeof(path));
+	else
+		gt_path = igt_sysfs_gt_path(fd, gt_id, path, sizeof(path));
 
-	gt_path = igt_sysfs_gt_path(fd, gt_id, path, sizeof(path));
 	if (gt_path == NULL) {
-		fprintf(stderr, "igt_sysfs_gt_path returned NULL for GT%d\n", gt_id);
+		fprintf(stderr, "sysfs_gt_path returned NULL for GT%d\n", gt_id);
 		return NULL;
 	}
+
 	path_len = strlen(gt_path);
-	ret = snprintf(path + path_len, sizeof(path) - path_len,
-		       "/rps_%s_freq_mhz", which);
+
+	if (is_xe) {
+		/* Xe uses /freq0/ directory with different file names */
+		freq_name = !strcmp(which, "RP1") ? "rpe" :
+			    !strcmp(which, "RP0") ? "rp0" :
+			    !strcmp(which, "RPn") ? "rpn" : which;
+
+		ret = snprintf(path + path_len, sizeof(path) - path_len,
+			       "/freq0/%s_freq", freq_name);
+	} else {
+		/* i915 uses /rps_*_freq_mhz files */
+		ret = snprintf(path + path_len, sizeof(path) - path_len,
+			       "/rps_%s_freq_mhz", which);
+	}
 
 	if (ret >= sizeof(path) - path_len) {
 		fprintf(stderr, "Path truncated\n");
@@ -140,6 +170,11 @@ is_intel_card(int card)
 			found = 1;
 			break;
 		}
+
+		if (!strcmp(entry->d_name, "pci:xe")) {
+			found = 1;
+			break;
+		}
 	}
 
 	closedir(dirp);
@@ -298,6 +333,8 @@ usage(const char *prog, int err)
 	printf("   intel_gpu_frequency --get --gt 1\tGet frequencies for GT1\n");
 	printf("   intel_gpu_frequency --list\t\tList all available GTs\n");
 	printf("\n");
+	printf("Auto-detects i915 vs Xe driver and uses appropriate interfaces.\n");
+	printf("\n");
 	printf("Report bugs to https://gitlab.freedesktop.org/drm/igt-gpu-tools/-/issues\n");
 	exit(err);
 }
@@ -417,7 +454,7 @@ parse(int argc, char *argv[], bool *act_upon, size_t act_upon_n, int *new_freq,
 			}
 			break;
 		case 'e': /* efficient */
-			if (IS_VALLEYVIEW(devid) || IS_CHERRYVIEW(devid)) {
+			if (!is_xe && (IS_VALLEYVIEW(devid) || IS_CHERRYVIEW(devid))) {
 				/* the LP parts have special efficient frequencies */
 				fprintf(stderr,
 					"FIXME: Warning efficient frequency information is incorrect.\n");
@@ -533,8 +570,13 @@ static void list_available_gts(int fd, int num_gts)
 	for (i = 0; i < num_gts; i++) {
 		char path[SYSFS_PATH_MAX];
 
-		if (igt_sysfs_gt_path(fd, i, path, sizeof(path)))
-			printf("  GT%d: %s\n", i, path);
+		if (is_xe) {
+			if (xe_sysfs_gt_path(fd, i, path, sizeof(path)))
+				printf("  GT%d: %s\n", i, path);
+		} else {
+			if (igt_sysfs_gt_path(fd, i, path, sizeof(path)))
+				printf("  GT%d: %s\n", i, path);
+		}
 	}
 }
 
@@ -563,7 +605,7 @@ static void execute_frequency_operations(int argc, int fd, int specified_gt, par
 		process_gt_frequencies(argc, fd, specified_gt, result, targets, set_freq);
 	} else {
 		/* Process all GTs */
-		int num_gts = igt_sysfs_get_num_gt(fd);
+		int num_gts = driver_get_num_gts(fd);
 
 		for (target_gt = 0; target_gt < num_gts; target_gt++)
 			process_gt_frequencies(argc, fd, target_gt, result, targets, set_freq);
@@ -647,10 +689,19 @@ int main(int argc, char *argv[])
 	if (device < 0)
 		exit_error("No Intel card found in /dev/dri\n", 0);
 
-	fd = __drm_open_driver(DRIVER_INTEL);
-	devid = intel_get_drm_devid(fd);
-	device = igt_device_get_card_index(fd);
-	num_gts = igt_sysfs_get_num_gt(fd);
+	fd = __drm_open_driver(DRIVER_INTEL | DRIVER_XE);
+	is_xe = is_xe_device(fd);
+
+	if (is_xe) {
+		igt_debug("Using Xe driver\n");
+		device = igt_device_get_card_index(fd);
+	} else {
+		devid = intel_get_drm_devid(fd);
+		device = igt_device_get_card_index(fd);
+		igt_debug("Using i915 driver\n");
+	}
+
+	num_gts = driver_get_num_gts(fd);
 
 	result = parse(argc, argv, targets, ARRAY_SIZE(targets), set_freq,
 		      &specified_gt, &list_gts, fd);
Signature · 1 line
2.43.0
#6 lore raw

Re: [PATCH i-g-t v6 3/3] tools/intel_gpu_frequency: Add Xe driver support

Hi,

Quoted 24 lines · S Sebinraj

This change enhances intel_gpu_frequency to add compatibility with Xe drivers.

Key improvements:
- Add runtime GT detection using xe_number_gt()
- Auto-detect driver type using is_xe_device()
- Driver-agnostic frequency management with unified interface

Driver-specific implementations:
- Automatic frequency name mapping (RP1→rpe, RP0→rp0, RPn→rpn for Xe)

The tool now auto-detects Xe drivers and uses appropriate interfaces.

---
 tools/intel_gpu_frequency.c | 81 ++++++++++++++++++++++++++++++-------
 1 file changed, 66 insertions(+), 15 deletions(-)
tools/intel_gpu_frequency.c +0 -0
index 695a483fd..9eeaa1223 100644

tools/intel_gpu_frequency.c line 38

Quoted 4 lines
tools/intel_gpu_frequency.c +1 -0
@@ -35,11 +35,13 @@
 #include "igt_device.h"
 #include "igt_sysfs.h"
 #include "intel_chipset.h"
+#include "xe/xe_query.h"

Please do not use it, see below.

tools/intel_gpu_frequency.c line 44

Quoted 6 lines
tools/intel_gpu_frequency.c +1 -0
 #define VERSION "1.0"
 #define SYSFS_PATH_MAX 96
 
 static int device, devid;
+static bool is_xe;

This is a global, imho this should be a part of structure, same for devid above.

tools/intel_gpu_frequency.c struct freq_info { · line 66

Quoted 9 lines
tools/intel_gpu_frequency.c +1 -1
 enum {
 	CUR=0,
@@ -61,10 +63,10 @@ struct freq_info {
 static struct freq_info info[] = {
 	{ "cur", "r"  },
 	{ "min", "rb+" },
-	{ "RP1", "r" },
+	{ "RP1", "r" },    /* i915: RP1, xe: rpe */

imho better extend it into two variants, one for i915 second for Xe.

Quoted 9 lines
tools/intel_gpu_frequency.c +2 -2
 	{ "max", "rb+" },
-	{ "RP0", "r" },
-	{ "RPn", "r" }
+	{ "RP0", "r" },    /* i915: RP0, xe: rp0 */
+	{ "RPn", "r" }     /* i915: RPn, xe: rpn */
 };
 
 typedef struct parse_result {

tools/intel_gpu_frequency.c typedef struct parse_result { · line 82

Quoted 9 lines
tools/intel_gpu_frequency.c +6 -0
@@ -72,21 +74,49 @@ typedef struct parse_result {
 	bool read;
 } parse_result_t;
 
+static int driver_get_num_gts(int fd)
+{
+	if (is_xe) {
+		return xe_number_gt(fd);
+	} else {
+		return igt_sysfs_get_num_gt(fd);

Do not use any of this just read sysfs yourself.

Regards,
Kamil

Quoted 126 lines
tools/intel_gpu_frequency.c +55 -12
+	}
+}
+
 static char *
 get_sysfs_path(int fd, int gt_id, const char *which)
 {
 	static char path[SYSFS_PATH_MAX];
 	char *gt_path;
 	int path_len, ret;
+	const char *freq_name;
+
+	/* Get driver-specific GT path */
+	if (is_xe)
+		gt_path = xe_sysfs_gt_path(fd, gt_id, path, sizeof(path));
+	else
+		gt_path = igt_sysfs_gt_path(fd, gt_id, path, sizeof(path));
 
-	gt_path = igt_sysfs_gt_path(fd, gt_id, path, sizeof(path));
 	if (gt_path == NULL) {
-		fprintf(stderr, "igt_sysfs_gt_path returned NULL for GT%d\n", gt_id);
+		fprintf(stderr, "sysfs_gt_path returned NULL for GT%d\n", gt_id);
 		return NULL;
 	}
+
 	path_len = strlen(gt_path);
-	ret = snprintf(path + path_len, sizeof(path) - path_len,
-		       "/rps_%s_freq_mhz", which);
+
+	if (is_xe) {
+		/* Xe uses /freq0/ directory with different file names */
+		freq_name = !strcmp(which, "RP1") ? "rpe" :
+			    !strcmp(which, "RP0") ? "rp0" :
+			    !strcmp(which, "RPn") ? "rpn" : which;
+
+		ret = snprintf(path + path_len, sizeof(path) - path_len,
+			       "/freq0/%s_freq", freq_name);
+	} else {
+		/* i915 uses /rps_*_freq_mhz files */
+		ret = snprintf(path + path_len, sizeof(path) - path_len,
+			       "/rps_%s_freq_mhz", which);
+	}
 
 	if (ret >= sizeof(path) - path_len) {
 		fprintf(stderr, "Path truncated\n");
@@ -140,6 +170,11 @@ is_intel_card(int card)
 			found = 1;
 			break;
 		}
+
+		if (!strcmp(entry->d_name, "pci:xe")) {
+			found = 1;
+			break;
+		}
 	}
 
 	closedir(dirp);
@@ -298,6 +333,8 @@ usage(const char *prog, int err)
 	printf("   intel_gpu_frequency --get --gt 1\tGet frequencies for GT1\n");
 	printf("   intel_gpu_frequency --list\t\tList all available GTs\n");
 	printf("\n");
+	printf("Auto-detects i915 vs Xe driver and uses appropriate interfaces.\n");
+	printf("\n");
 	printf("Report bugs to https://gitlab.freedesktop.org/drm/igt-gpu-tools/-/issues\n");
 	exit(err);
 }
@@ -417,7 +454,7 @@ parse(int argc, char *argv[], bool *act_upon, size_t act_upon_n, int *new_freq,
 			}
 			break;
 		case 'e': /* efficient */
-			if (IS_VALLEYVIEW(devid) || IS_CHERRYVIEW(devid)) {
+			if (!is_xe && (IS_VALLEYVIEW(devid) || IS_CHERRYVIEW(devid))) {
 				/* the LP parts have special efficient frequencies */
 				fprintf(stderr,
 					"FIXME: Warning efficient frequency information is incorrect.\n");
@@ -533,8 +570,13 @@ static void list_available_gts(int fd, int num_gts)
 	for (i = 0; i < num_gts; i++) {
 		char path[SYSFS_PATH_MAX];
 
-		if (igt_sysfs_gt_path(fd, i, path, sizeof(path)))
-			printf("  GT%d: %s\n", i, path);
+		if (is_xe) {
+			if (xe_sysfs_gt_path(fd, i, path, sizeof(path)))
+				printf("  GT%d: %s\n", i, path);
+		} else {
+			if (igt_sysfs_gt_path(fd, i, path, sizeof(path)))
+				printf("  GT%d: %s\n", i, path);
+		}
 	}
 }
 
@@ -563,7 +605,7 @@ static void execute_frequency_operations(int argc, int fd, int specified_gt, par
 		process_gt_frequencies(argc, fd, specified_gt, result, targets, set_freq);
 	} else {
 		/* Process all GTs */
-		int num_gts = igt_sysfs_get_num_gt(fd);
+		int num_gts = driver_get_num_gts(fd);
 
 		for (target_gt = 0; target_gt < num_gts; target_gt++)
 			process_gt_frequencies(argc, fd, target_gt, result, targets, set_freq);
@@ -647,10 +689,19 @@ int main(int argc, char *argv[])
 	if (device < 0)
 		exit_error("No Intel card found in /dev/dri\n", 0);
 
-	fd = __drm_open_driver(DRIVER_INTEL);
-	devid = intel_get_drm_devid(fd);
-	device = igt_device_get_card_index(fd);
-	num_gts = igt_sysfs_get_num_gt(fd);
+	fd = __drm_open_driver(DRIVER_INTEL | DRIVER_XE);
+	is_xe = is_xe_device(fd);
+
+	if (is_xe) {
+		igt_debug("Using Xe driver\n");
+		device = igt_device_get_card_index(fd);
+	} else {
+		devid = intel_get_drm_devid(fd);
+		device = igt_device_get_card_index(fd);
+		igt_debug("Using i915 driver\n");
+	}
+
+	num_gts = driver_get_num_gts(fd);
 
 	result = parse(argc, argv, targets, ARRAY_SIZE(targets), set_freq,
 		      &specified_gt, &list_gts, fd);
Signature · 1 line
2.43.0