diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 10:05:51 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 10:05:51 +0000 |
commit | 5d1646d90e1f2cceb9f0828f4b28318cd0ec7744 (patch) | |
tree | a94efe259b9009378be6d90eb30d2b019d95c194 /tools/power/cpupower/debug | |
parent | Initial commit. (diff) | |
download | linux-upstream.tar.xz linux-upstream.zip |
Adding upstream version 5.10.209.upstream/5.10.209upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | tools/power/cpupower/debug/i386/Makefile | 42 | ||||
-rw-r--r-- | tools/power/cpupower/debug/i386/centrino-decode.c | 112 | ||||
-rw-r--r-- | tools/power/cpupower/debug/i386/dump_psb.c | 196 | ||||
-rw-r--r-- | tools/power/cpupower/debug/i386/intel_gsic.c | 77 | ||||
-rw-r--r-- | tools/power/cpupower/debug/i386/powernow-k8-decode.c | 95 | ||||
-rw-r--r-- | tools/power/cpupower/debug/kernel/Makefile | 23 | ||||
-rw-r--r-- | tools/power/cpupower/debug/kernel/cpufreq-test_tsc.c | 111 | ||||
-rw-r--r-- | tools/power/cpupower/debug/x86_64/Makefile | 31 |
8 files changed, 687 insertions, 0 deletions
diff --git a/tools/power/cpupower/debug/i386/Makefile b/tools/power/cpupower/debug/i386/Makefile new file mode 100644 index 000000000..b3f771039 --- /dev/null +++ b/tools/power/cpupower/debug/i386/Makefile @@ -0,0 +1,42 @@ +# SPDX-License-Identifier: GPL-2.0 +OUTPUT=./ +ifeq ("$(origin O)", "command line") + OUTPUT := $(O)/ +endif + +DESTDIR = +bindir = /usr/bin + +INSTALL = /usr/bin/install + + +default: all + +$(OUTPUT)centrino-decode: centrino-decode.c + $(CC) $(CFLAGS) -o $@ centrino-decode.c + +$(OUTPUT)dump_psb: dump_psb.c + $(CC) $(CFLAGS) -o $@ dump_psb.c + +$(OUTPUT)intel_gsic: intel_gsic.c + $(CC) $(CFLAGS) -o $@ -llrmi intel_gsic.c + +$(OUTPUT)powernow-k8-decode: powernow-k8-decode.c + $(CC) $(CFLAGS) -o $@ powernow-k8-decode.c + +all: $(OUTPUT)centrino-decode $(OUTPUT)dump_psb $(OUTPUT)intel_gsic $(OUTPUT)powernow-k8-decode + +clean: + rm -rf $(OUTPUT)centrino-decode + rm -rf $(OUTPUT)dump_psb + rm -rf $(OUTPUT)intel_gsic + rm -rf $(OUTPUT)powernow-k8-decode + +install: + $(INSTALL) -d $(DESTDIR)${bindir} + $(INSTALL) $(OUTPUT)centrino-decode $(DESTDIR)${bindir} + $(INSTALL) $(OUTPUT)powernow-k8-decode $(DESTDIR)${bindir} + $(INSTALL) $(OUTPUT)dump_psb $(DESTDIR)${bindir} + $(INSTALL) $(OUTPUT)intel_gsic $(DESTDIR)${bindir} + +.PHONY: all default clean install diff --git a/tools/power/cpupower/debug/i386/centrino-decode.c b/tools/power/cpupower/debug/i386/centrino-decode.c new file mode 100644 index 000000000..700cd31a7 --- /dev/null +++ b/tools/power/cpupower/debug/i386/centrino-decode.c @@ -0,0 +1,112 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * (C) 2003 - 2004 Dominik Brodowski <linux@dominikbrodowski.de> + * + * Based on code found in + * linux/arch/i386/kernel/cpu/cpufreq/speedstep-centrino.c + * and originally developed by Jeremy Fitzhardinge. + * + * USAGE: simply run it to decode the current settings on CPU 0, + * or pass the CPU number as argument, or pass the MSR content + * as argument. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> +#include <unistd.h> +#include <errno.h> +#include <fcntl.h> + +#include <sys/types.h> +#include <sys/stat.h> + +#define MCPU 32 + +#define MSR_IA32_PERF_STATUS 0x198 + +static int rdmsr(unsigned int cpu, unsigned int msr, + unsigned int *lo, unsigned int *hi) +{ + int fd; + char file[20]; + unsigned long long val; + int retval = -1; + + *lo = *hi = 0; + + if (cpu > MCPU) + goto err1; + + sprintf(file, "/dev/cpu/%d/msr", cpu); + fd = open(file, O_RDONLY); + + if (fd < 0) + goto err1; + + if (lseek(fd, msr, SEEK_CUR) == -1) + goto err2; + + if (read(fd, &val, 8) != 8) + goto err2; + + *lo = (uint32_t )(val & 0xffffffffull); + *hi = (uint32_t )(val>>32 & 0xffffffffull); + + retval = 0; +err2: + close(fd); +err1: + return retval; +} + +static void decode (unsigned int msr) +{ + unsigned int multiplier; + unsigned int mv; + + multiplier = ((msr >> 8) & 0xFF); + + mv = (((msr & 0xFF) * 16) + 700); + + printf("0x%x means multiplier %d @ %d mV\n", msr, multiplier, mv); +} + +static int decode_live(unsigned int cpu) +{ + unsigned int lo, hi; + int err; + + err = rdmsr(cpu, MSR_IA32_PERF_STATUS, &lo, &hi); + + if (err) { + printf("can't get MSR_IA32_PERF_STATUS for cpu %d\n", cpu); + printf("Possible trouble: you don't run an Enhanced SpeedStep capable cpu\n"); + printf("or you are not root, or the msr driver is not present\n"); + return 1; + } + + decode(lo); + + return 0; +} + +int main (int argc, char **argv) +{ + unsigned int cpu, mode = 0; + + if (argc < 2) + cpu = 0; + else { + cpu = strtoul(argv[1], NULL, 0); + if (cpu >= MCPU) + mode = 1; + } + + if (mode) + decode(cpu); + else + decode_live(cpu); + + return 0; +} diff --git a/tools/power/cpupower/debug/i386/dump_psb.c b/tools/power/cpupower/debug/i386/dump_psb.c new file mode 100644 index 000000000..2c768cf70 --- /dev/null +++ b/tools/power/cpupower/debug/i386/dump_psb.c @@ -0,0 +1,196 @@ +/* + * dump_psb. (c) 2004, Dave Jones, Red Hat Inc. + * Licensed under the GPL v2. + */ + +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#define _GNU_SOURCE +#include <getopt.h> + +#include <sys/mman.h> + +#define LEN (0x100000 - 0xc0000) +#define OFFSET (0xc0000) + +#ifndef __packed +#define __packed __attribute((packed)) +#endif + +static long relevant; + +static const int fid_to_mult[32] = { + 110, 115, 120, 125, 50, 55, 60, 65, + 70, 75, 80, 85, 90, 95, 100, 105, + 30, 190, 40, 200, 130, 135, 140, 210, + 150, 225, 160, 165, 170, 180, -1, -1, +}; + +static const int vid_to_voltage[32] = { + 2000, 1950, 1900, 1850, 1800, 1750, 1700, 1650, + 1600, 1550, 1500, 1450, 1400, 1350, 1300, 0, + 1275, 1250, 1225, 1200, 1175, 1150, 1125, 1100, + 1075, 1050, 1024, 1000, 975, 950, 925, 0, +}; + +struct psb_header { + char signature[10]; + u_char version; + u_char flags; + u_short settlingtime; + u_char res1; + u_char numpst; +} __packed; + +struct pst_header { + u_int32_t cpuid; + u_char fsb; + u_char maxfid; + u_char startvid; + u_char numpstates; +} __packed; + +static u_int fsb; +static u_int sgtc; + +static int +decode_pst(char *p, int npstates) +{ + int i; + int freq, fid, vid; + + for (i = 0; i < npstates; ++i) { + fid = *p++; + vid = *p++; + freq = 100 * fid_to_mult[fid] * fsb; + + printf(" %2d %8dkHz FID %02x (%2d.%01d) VID %02x (%4dmV)\n", + i, + freq, + fid, fid_to_mult[fid]/10, fid_to_mult[fid]%10, + vid, vid_to_voltage[vid]); + } + + return 0; +} + +static +void decode_psb(char *p, int numpst) +{ + int i; + struct psb_header *psb; + struct pst_header *pst; + + psb = (struct psb_header*) p; + + if (psb->version != 0x12) + return; + + printf("PSB version: %hhx flags: %hhx settling time %hhuus res1 %hhx num pst %hhu\n", + psb->version, + psb->flags, + psb->settlingtime, + psb->res1, + psb->numpst); + sgtc = psb->settlingtime * 100; + + if (sgtc < 10000) + sgtc = 10000; + + p = ((char *) psb) + sizeof(struct psb_header); + + if (numpst < 0) + numpst = psb->numpst; + else + printf("Overriding number of pst :%d\n", numpst); + + for (i = 0; i < numpst; i++) { + pst = (struct pst_header*) p; + + if (relevant != 0) { + if (relevant!= pst->cpuid) + goto next_one; + } + + printf(" PST %d cpuid %.3x fsb %hhu mfid %hhx svid %hhx numberstates %hhu\n", + i+1, + pst->cpuid, + pst->fsb, + pst->maxfid, + pst->startvid, + pst->numpstates); + + fsb = pst->fsb; + decode_pst(p + sizeof(struct pst_header), pst->numpstates); + +next_one: + p += sizeof(struct pst_header) + 2*pst->numpstates; + } + +} + +static struct option info_opts[] = { + {"numpst", no_argument, NULL, 'n'}, +}; + +void print_help(void) +{ + printf ("Usage: dump_psb [options]\n"); + printf ("Options:\n"); + printf (" -n, --numpst Set number of PST tables to scan\n"); + printf (" -r, --relevant Only display PSTs relevant to cpuid N\n"); +} + +int +main(int argc, char *argv[]) +{ + int fd; + int numpst=-1; + int ret=0, cont=1; + char *mem = NULL; + char *p; + + do { + ret = getopt_long(argc, argv, "hr:n:", info_opts, NULL); + switch (ret){ + case '?': + case 'h': + print_help(); + cont = 0; + break; + case 'r': + relevant = strtol(optarg, NULL, 16); + break; + case 'n': + numpst = strtol(optarg, NULL, 10); + break; + case -1: + cont = 0; + break; + } + + } while(cont); + + fd = open("/dev/mem", O_RDONLY); + if (fd < 0) { + printf ("Couldn't open /dev/mem. Are you root?\n"); + exit(1); + } + + mem = mmap(mem, 0x100000 - 0xc0000, PROT_READ, MAP_SHARED, fd, 0xc0000); + close(fd); + + for (p = mem; p - mem < LEN; p+=16) { + if (memcmp(p, "AMDK7PNOW!", 10) == 0) { + decode_psb(p, numpst); + break; + } + } + + munmap(mem, LEN); + return 0; +} diff --git a/tools/power/cpupower/debug/i386/intel_gsic.c b/tools/power/cpupower/debug/i386/intel_gsic.c new file mode 100644 index 000000000..befd837f0 --- /dev/null +++ b/tools/power/cpupower/debug/i386/intel_gsic.c @@ -0,0 +1,77 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * (C) 2003 Bruno Ducrot + * (C) 2004 Dominik Brodowski <linux@dominikbrodowski.de> + * + * Based on code found in + * linux/include/asm-i386/ist.h and linux/arch/i386/kernel/setup.c + * and originally developed by Andy Grover <andrew.grover@intel.com> + */ + +#include <stdio.h> +#include <string.h> +#include <lrmi.h> + +int main (void) +{ + struct LRMI_regs r; + int retval; + + if (!LRMI_init()) + return 0; + + memset(&r, 0, sizeof(r)); + + r.eax = 0x0000E980; + r.edx = 0x47534943; + + retval = LRMI_int(0x15, &r); + + if (!retval) { + printf("Failed!\n"); + return 0; + } + if (r.eax == 0x47534943) { + printf("BIOS supports GSIC call:\n"); + printf("\tsignature: %c%c%c%c\n", + (r.eax >> 24) & 0xff, + (r.eax >> 16) & 0xff, + (r.eax >> 8) & 0xff, + (r.eax) & 0xff); + printf("\tcommand port = 0x%.4x\n", + r.ebx & 0xffff); + printf("\tcommand = 0x%.4x\n", + (r.ebx >> 16) & 0xffff); + printf("\tevent port = 0x%.8x\n", r.ecx); + printf("\tflags = 0x%.8x\n", r.edx); + if (((r.ebx >> 16) & 0xffff) != 0x82) { + printf("non-default command value. If speedstep-smi " + "doesn't work out of the box,\nyou may want to " + "try out the default value by passing " + "smi_cmd=0x82 to the module\n ON YOUR OWN " + "RISK.\n"); + } + if ((r.ebx & 0xffff) != 0xb2) { + printf("non-default command port. If speedstep-smi " + "doesn't work out of the box,\nyou may want to " + "try out the default value by passing " + "smi_port=0x82 to the module\n ON YOUR OWN " + "RISK.\n"); + } + } else { + printf("BIOS DOES NOT support GSIC call. Dumping registers anyway:\n"); + printf("eax = 0x%.8x\n", r.eax); + printf("ebx = 0x%.8x\n", r.ebx); + printf("ecx = 0x%.8x\n", r.ecx); + printf("edx = 0x%.8x\n", r.edx); + printf("Note also that some BIOS do not support the initial " + "GSIC call, but the newer\nspeedstep-smi driver may " + "work.\nFor this, you need to pass some arguments to " + "the speedstep-smi driver:\n"); + printf("\tsmi_cmd=0x?? smi_port=0x?? smi_sig=1\n"); + printf("\nUnfortunately, you have to know what exactly are " + "smi_cmd and smi_port, and this\nis system " + "dependent.\n"); + } + return 1; +} diff --git a/tools/power/cpupower/debug/i386/powernow-k8-decode.c b/tools/power/cpupower/debug/i386/powernow-k8-decode.c new file mode 100644 index 000000000..735dca1e2 --- /dev/null +++ b/tools/power/cpupower/debug/i386/powernow-k8-decode.c @@ -0,0 +1,95 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * (C) 2004 Bruno Ducrot <ducrot@poupinou.org> + * + * Based on code found in + * linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c + * and originally developed by Paul Devriendt + */ + +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> +#include <unistd.h> +#include <errno.h> +#include <fcntl.h> + +#include <sys/types.h> +#include <sys/stat.h> + +#define MCPU 32 + +#define MSR_FIDVID_STATUS 0xc0010042 + +#define MSR_S_HI_CURRENT_VID 0x0000001f +#define MSR_S_LO_CURRENT_FID 0x0000003f + +static int get_fidvid(uint32_t cpu, uint32_t *fid, uint32_t *vid) +{ + int err = 1; + uint64_t msr = 0; + int fd; + char file[20]; + + if (cpu > MCPU) + goto out; + + sprintf(file, "/dev/cpu/%d/msr", cpu); + + fd = open(file, O_RDONLY); + if (fd < 0) + goto out; + lseek(fd, MSR_FIDVID_STATUS, SEEK_CUR); + if (read(fd, &msr, 8) != 8) + goto err1; + + *fid = ((uint32_t )(msr & 0xffffffffull)) & MSR_S_LO_CURRENT_FID; + *vid = ((uint32_t )(msr>>32 & 0xffffffffull)) & MSR_S_HI_CURRENT_VID; + err = 0; +err1: + close(fd); +out: + return err; +} + + +/* Return a frequency in MHz, given an input fid */ +static uint32_t find_freq_from_fid(uint32_t fid) +{ + return 800 + (fid * 100); +} + +/* Return a voltage in miliVolts, given an input vid */ +static uint32_t find_millivolts_from_vid(uint32_t vid) +{ + return 1550-vid*25; +} + +int main (int argc, char *argv[]) +{ + int err; + int cpu; + uint32_t fid, vid; + + if (argc < 2) + cpu = 0; + else + cpu = strtoul(argv[1], NULL, 0); + + err = get_fidvid(cpu, &fid, &vid); + + if (err) { + printf("can't get fid, vid from MSR\n"); + printf("Possible trouble: you don't run a powernow-k8 capable cpu\n"); + printf("or you are not root, or the msr driver is not present\n"); + exit(1); + } + + + printf("cpu %d currently at %d MHz and %d mV\n", + cpu, + find_freq_from_fid(fid), + find_millivolts_from_vid(vid)); + + return 0; +} diff --git a/tools/power/cpupower/debug/kernel/Makefile b/tools/power/cpupower/debug/kernel/Makefile new file mode 100644 index 000000000..7b5c43684 --- /dev/null +++ b/tools/power/cpupower/debug/kernel/Makefile @@ -0,0 +1,23 @@ +# SPDX-License-Identifier: GPL-2.0 +obj-m := + +KDIR := /lib/modules/$(shell uname -r)/build +KMISC := /lib/modules/$(shell uname -r)/cpufrequtils/ + +ifeq ("$(CONFIG_X86_TSC)", "y") + obj-m += cpufreq-test_tsc.o +endif + +default: + $(MAKE) -C $(KDIR) M=$(CURDIR) + +clean: + - rm -rf *.o *.ko .*.cmd .*.mod.* *.mod.c + - rm -rf Module.symvers modules.order + +install: default + install -d $(KMISC) + install -m 644 -c *.ko $(KMISC) + /sbin/depmod -a + +all: default diff --git a/tools/power/cpupower/debug/kernel/cpufreq-test_tsc.c b/tools/power/cpupower/debug/kernel/cpufreq-test_tsc.c new file mode 100644 index 000000000..e364b170b --- /dev/null +++ b/tools/power/cpupower/debug/kernel/cpufreq-test_tsc.c @@ -0,0 +1,111 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * test module to check whether the TSC-based delay routine continues + * to work properly after cpufreq transitions. Needs ACPI to work + * properly. + * + * Based partly on the Power Management Timer (PMTMR) code to be found + * in arch/i386/kernel/timers/timer_pm.c on recent 2.6. kernels, especially + * code written by John Stultz. The read_pmtmr function was copied verbatim + * from that file. + * + * (C) 2004 Dominik Brodowski + * + * To use: + * 1.) pass clock=tsc to the kernel on your bootloader + * 2.) modprobe this module (it'll fail) + * 3.) change CPU frequency + * 4.) modprobe this module again + * 5.) if the third value, "diff_pmtmr", changes between 2. and 4., the + * TSC-based delay routine on the Linux kernel does not correctly + * handle the cpufreq transition. Please report this to + * linux-pm@vger.kernel.org + */ + +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/init.h> +#include <linux/delay.h> +#include <linux/acpi.h> +#include <asm/io.h> + +static int pm_tmr_ioport = 0; + +/*helper function to safely read acpi pm timesource*/ +static u32 read_pmtmr(void) +{ + u32 v1=0,v2=0,v3=0; + /* It has been reported that because of various broken + * chipsets (ICH4, PIIX4 and PIIX4E) where the ACPI PM time + * source is not latched, so you must read it multiple + * times to insure a safe value is read. + */ + do { + v1 = inl(pm_tmr_ioport); + v2 = inl(pm_tmr_ioport); + v3 = inl(pm_tmr_ioport); + } while ((v1 > v2 && v1 < v3) || (v2 > v3 && v2 < v1) + || (v3 > v1 && v3 < v2)); + + /* mask the output to 24 bits */ + return (v2 & 0xFFFFFF); +} + +static int __init cpufreq_test_tsc(void) +{ + u32 now, then, diff; + u64 now_tsc, then_tsc, diff_tsc; + int i; + + /* the following code snipped is copied from arch/x86/kernel/acpi/boot.c + of Linux v2.6.25. */ + + /* detect the location of the ACPI PM Timer */ + if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID) { + /* FADT rev. 2 */ + if (acpi_gbl_FADT.xpm_timer_block.space_id != + ACPI_ADR_SPACE_SYSTEM_IO) + return 0; + + pm_tmr_ioport = acpi_gbl_FADT.xpm_timer_block.address; + /* + * "X" fields are optional extensions to the original V1.0 + * fields, so we must selectively expand V1.0 fields if the + * corresponding X field is zero. + */ + if (!pm_tmr_ioport) + pm_tmr_ioport = acpi_gbl_FADT.pm_timer_block; + } else { + /* FADT rev. 1 */ + pm_tmr_ioport = acpi_gbl_FADT.pm_timer_block; + } + + printk(KERN_DEBUG "start--> \n"); + then = read_pmtmr(); + then_tsc = rdtsc(); + for (i=0;i<20;i++) { + mdelay(100); + now = read_pmtmr(); + now_tsc = rdtsc(); + diff = (now - then) & 0xFFFFFF; + diff_tsc = now_tsc - then_tsc; + printk(KERN_DEBUG "t1: %08u t2: %08u diff_pmtmr: %08u diff_tsc: %016llu\n", then, now, diff, diff_tsc); + then = now; + then_tsc = now_tsc; + } + printk(KERN_DEBUG "<-- end \n"); + return -ENODEV; +} + +static void __exit cpufreq_none(void) +{ + return; +} + +module_init(cpufreq_test_tsc) +module_exit(cpufreq_none) + + +MODULE_AUTHOR("Dominik Brodowski"); +MODULE_DESCRIPTION("Verify the TSC cpufreq notifier working correctly -- needs ACPI-enabled system"); +MODULE_LICENSE ("GPL"); diff --git a/tools/power/cpupower/debug/x86_64/Makefile b/tools/power/cpupower/debug/x86_64/Makefile new file mode 100644 index 000000000..b1b6c4364 --- /dev/null +++ b/tools/power/cpupower/debug/x86_64/Makefile @@ -0,0 +1,31 @@ +# SPDX-License-Identifier: GPL-2.0 +OUTPUT=./ +ifeq ("$(origin O)", "command line") + OUTPUT := $(O)/ +endif + +DESTDIR = +bindir = /usr/bin + +INSTALL = /usr/bin/install + + +default: all + +$(OUTPUT)centrino-decode: ../i386/centrino-decode.c + $(CC) $(CFLAGS) -o $@ $(LDFLAGS) $< + +$(OUTPUT)powernow-k8-decode: ../i386/powernow-k8-decode.c + $(CC) $(CFLAGS) -o $@ $(LDFLAGS) $< + +all: $(OUTPUT)centrino-decode $(OUTPUT)powernow-k8-decode + +clean: + rm -rf $(OUTPUT)centrino-decode $(OUTPUT)powernow-k8-decode + +install: + $(INSTALL) -d $(DESTDIR)${bindir} + $(INSTALL) $(OUTPUT)centrino-decode $(DESTDIR)${bindir} + $(INSTALL) $(OUTPUT)powernow-k8-decode $(DESTDIR)${bindir} + +.PHONY: all default clean install |