xref: /linux/tools/testing/selftests/kvm/lib/proc_util.c (revision 67f8bc848ee31831336bd478e57d2f993551902e)
1 // SPDX-License-Identifier: GPL-2.0
2 #include "kvm_util.h"
3 #include "test_util.h"
4 #include "proc_util.h"
5 
6 static FILE *open_proc_interrupts(void)
7 {
8 	FILE *fp;
9 
10 	fp = fopen("/proc/interrupts", "r");
11 	TEST_ASSERT(fp, "fopen(/proc/interrupts) failed");
12 
13 	return fp;
14 }
15 
16 unsigned int vfio_msix_to_host_irq(const char *device_bdf, int msix)
17 {
18 	char search_string[64];
19 	char line[4096];
20 	int irq = -1;
21 	FILE *fp;
22 
23 	fp = open_proc_interrupts();
24 
25 	snprintf(search_string, sizeof(search_string), "vfio-msix[%d]", msix);
26 
27 	while (fgets(line, sizeof(line), fp)) {
28 		if (strstr(line, device_bdf) && strstr(line, search_string)) {
29 			TEST_ASSERT_EQ(1, sscanf(line, "%d:", &irq));
30 			break;
31 		}
32 	}
33 
34 	fclose(fp);
35 
36 	TEST_ASSERT(irq != -1, "Failed to locate IRQ for %s %s", device_bdf,
37 		    search_string);
38 	return (unsigned int)irq;
39 }
40 
41 void proc_irq_set_smp_affinity(unsigned int irq, int cpu)
42 {
43 	char path[PATH_MAX];
44 	int r, fd;
45 
46 	snprintf(path, sizeof(path), "/proc/irq/%u/smp_affinity_list", irq);
47 	fd = open(path, O_RDWR);
48 	TEST_ASSERT(fd >= 0, "Failed to open %s", path);
49 
50 	r = dprintf(fd, "%d\n", cpu);
51 	TEST_ASSERT(r > 0, "Failed to affinitize IRQ-%u to CPU %d", irq, cpu);
52 
53 	kvm_close(fd);
54 }
55