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)  * Copyright 2013-2015, Michael Ellerman, IBM Corp.
^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) #define _GNU_SOURCE	/* For CPU_ZERO etc. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <elf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <link.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <sys/ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <sys/sysinfo.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <sys/utsname.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <asm/unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/limits.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include "utils.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) static char auxv[4096];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) int read_auxv(char *buf, ssize_t buf_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	ssize_t num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	int rc, fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	fd = open("/proc/self/auxv", O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	if (fd == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		perror("open");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 		return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	num = read(fd, buf, buf_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	if (num < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		perror("read");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		rc = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	if (num > buf_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		printf("overflowed auxv buffer\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		rc = -EOVERFLOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) void *find_auxv_entry(int type, char *auxv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	ElfW(auxv_t) *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	p = (ElfW(auxv_t) *)auxv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	while (p->a_type != AT_NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		if (p->a_type == type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 			return p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		p++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) void *get_auxv_entry(int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	ElfW(auxv_t) *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	if (read_auxv(auxv, sizeof(auxv)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	p = find_auxv_entry(type, auxv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	if (p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		return (void *)p->a_un.a_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) int pick_online_cpu(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	int ncpus, cpu = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	cpu_set_t *mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	ncpus = get_nprocs_conf();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	size = CPU_ALLOC_SIZE(ncpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	mask = CPU_ALLOC(ncpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	if (!mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		perror("malloc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	CPU_ZERO_S(size, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (sched_getaffinity(0, size, mask)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		perror("sched_getaffinity");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	/* We prefer a primary thread, but skip 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	for (cpu = 8; cpu < ncpus; cpu += 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		if (CPU_ISSET_S(cpu, size, mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	/* Search for anything, but in reverse */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	for (cpu = ncpus - 1; cpu >= 0; cpu--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		if (CPU_ISSET_S(cpu, size, mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	printf("No cpus in affinity mask?!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	CPU_FREE(mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	return cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) bool is_ppc64le(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	struct utsname uts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	errno = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	rc = uname(&uts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		perror("uname");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	return strcmp(uts.machine, "ppc64le") == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) int read_sysfs_file(char *fpath, char *result, size_t result_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	char path[PATH_MAX] = "/sys/";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	int rc = -1, fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	strncat(path, fpath, PATH_MAX - strlen(path) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	if ((fd = open(path, O_RDONLY)) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	rc = read(fd, result, result_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) int read_debugfs_file(char *debugfs_file, int *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	int rc = -1, fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	char path[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	char value[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	strcpy(path, "/sys/kernel/debug/");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	strncat(path, debugfs_file, PATH_MAX - strlen(path) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	if ((fd = open(path, O_RDONLY)) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	if ((rc = read(fd, value, sizeof(value))) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	value[15] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	*result = atoi(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) int write_debugfs_file(char *debugfs_file, int result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	int rc = -1, fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	char path[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	char value[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	strcpy(path, "/sys/kernel/debug/");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	strncat(path, debugfs_file, PATH_MAX - strlen(path) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	if ((fd = open(path, O_WRONLY)) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	snprintf(value, 16, "%d", result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	if ((rc = write(fd, value, strlen(value))) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		int cpu, int group_fd, unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	return syscall(__NR_perf_event_open, hw_event, pid, cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		      group_fd, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) static void perf_event_attr_init(struct perf_event_attr *event_attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 					unsigned int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 					unsigned long config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	memset(event_attr, 0, sizeof(*event_attr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	event_attr->type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	event_attr->size = sizeof(struct perf_event_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	event_attr->config = config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	event_attr->read_format = PERF_FORMAT_GROUP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	event_attr->disabled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	event_attr->exclude_kernel = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	event_attr->exclude_hv = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	event_attr->exclude_guest = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) int perf_event_open_counter(unsigned int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 			    unsigned long config, int group_fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	struct perf_event_attr event_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	perf_event_attr_init(&event_attr, type, config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	fd = perf_event_open(&event_attr, 0, -1, group_fd, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 		perror("perf_event_open() failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	return fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) int perf_event_enable(int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	if (ioctl(fd, PERF_EVENT_IOC_ENABLE, PERF_IOC_FLAG_GROUP) == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		perror("error while enabling perf events");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) int perf_event_disable(int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	if (ioctl(fd, PERF_EVENT_IOC_DISABLE, PERF_IOC_FLAG_GROUP) == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		perror("error disabling perf events");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) int perf_event_reset(int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	if (ioctl(fd, PERF_EVENT_IOC_RESET, PERF_IOC_FLAG_GROUP) == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		perror("error resetting perf events");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) int using_hash_mmu(bool *using_hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	char line[128];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	FILE *f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	f = fopen("/proc/cpuinfo", "r");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	FAIL_IF(!f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	while (fgets(line, sizeof(line), f) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		if (!strcmp(line, "MMU		: Hash\n") ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		    !strcmp(line, "platform	: Cell\n") ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		    !strcmp(line, "platform	: PowerMac\n")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 			*using_hash = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		if (strcmp(line, "MMU		: Radix\n") == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 			*using_hash = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	rc = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	fclose(f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }