Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *  (C) 2004-2009  Dominik Brodowski <linux@dominikbrodowski.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include "cpupower.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include "cpupower_intern.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) unsigned int cpupower_read_sysfs(const char *path, char *buf, size_t buflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	ssize_t numread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	fd = open(path, O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	if (fd == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	numread = read(fd, buf, buflen - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	if (numread < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 		close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	buf[numread] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	return (unsigned int) numread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  * Detect whether a CPU is online
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  *     1 -> if CPU is online
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  *     0 -> if CPU is offline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  *     negative errno values in error case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) int cpupower_is_cpu_online(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	char path[SYSFS_PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	ssize_t numread;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	unsigned long long value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	char linebuf[MAX_LINE_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	char *endp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	struct stat statbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u", cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	if (stat(path, &statbuf) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	 * kernel without CONFIG_HOTPLUG_CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	 * -> cpuX directory exists, but not cpuX/online file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/online", cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	if (stat(path, &statbuf) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	fd = open(path, O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	if (fd == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	numread = read(fd, linebuf, MAX_LINE_LEN - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	if (numread < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	linebuf[numread] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	value = strtoull(linebuf, &endp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	if (value > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) /* returns -1 on failure, 0 on success */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) static int sysfs_topology_read_file(unsigned int cpu, const char *fname, int *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	char linebuf[MAX_LINE_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	char *endp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	char path[SYSFS_PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/topology/%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 			 cpu, fname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	if (cpupower_read_sysfs(path, linebuf, MAX_LINE_LEN) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	*result = strtol(linebuf, &endp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	if (endp == linebuf || errno == ERANGE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static int __compare(const void *t1, const void *t2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	struct cpuid_core_info *top1 = (struct cpuid_core_info *)t1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	struct cpuid_core_info *top2 = (struct cpuid_core_info *)t2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	if (top1->pkg < top2->pkg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	else if (top1->pkg > top2->pkg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	else if (top1->core < top2->core)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	else if (top1->core > top2->core)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	else if (top1->cpu < top2->cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	else if (top1->cpu > top2->cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)  * Returns amount of cpus, negative on error, cpu_top must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)  * passed to cpu_topology_release to free resources
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)  * Array is sorted after ->pkg, ->core, then ->cpu
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) int get_cpu_topology(struct cpupower_topology *cpu_top)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	int cpu, last_pkg, cpus = sysconf(_SC_NPROCESSORS_CONF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	cpu_top->core_info = malloc(sizeof(struct cpuid_core_info) * cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	if (cpu_top->core_info == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	cpu_top->pkgs = cpu_top->cores = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	for (cpu = 0; cpu < cpus; cpu++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		cpu_top->core_info[cpu].cpu = cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		cpu_top->core_info[cpu].is_online = cpupower_is_cpu_online(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		if(sysfs_topology_read_file(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 			cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			"physical_package_id",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 			&(cpu_top->core_info[cpu].pkg)) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			cpu_top->core_info[cpu].pkg = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			cpu_top->core_info[cpu].core = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		if(sysfs_topology_read_file(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 			cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 			"core_id",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			&(cpu_top->core_info[cpu].core)) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 			cpu_top->core_info[cpu].pkg = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 			cpu_top->core_info[cpu].core = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	qsort(cpu_top->core_info, cpus, sizeof(struct cpuid_core_info),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	      __compare);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	/* Count the number of distinct pkgs values. This works
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	   because the primary sort of the core_info struct was just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	   done by pkg value. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	last_pkg = cpu_top->core_info[0].pkg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	for(cpu = 1; cpu < cpus; cpu++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		if (cpu_top->core_info[cpu].pkg != last_pkg &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 				cpu_top->core_info[cpu].pkg != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 			last_pkg = cpu_top->core_info[cpu].pkg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 			cpu_top->pkgs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	if (!(cpu_top->core_info[0].pkg == -1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		cpu_top->pkgs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	/* Intel's cores count is not consecutively numbered, there may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	 * be a core_id of 3, but none of 2. Assume there always is 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	 * Get amount of cores by counting duplicates in a package
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	for (cpu = 0; cpu_top->core_info[cpu].pkg = 0 && cpu < cpus; cpu++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		if (cpu_top->core_info[cpu].core == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	cpu_top->cores++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	return cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) void cpu_topology_release(struct cpupower_topology cpu_top)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	free(cpu_top.core_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }