tools/power/cpupower/utils/helpers/msr.c
Source file repositories/reference/linux-study-clean/tools/power/cpupower/utils/helpers/msr.c
File Facts
- System
- Linux kernel
- Corpus path
tools/power/cpupower/utils/helpers/msr.c- Extension
.c- Size
- 1852 bytes
- Lines
- 89
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
Dependency Surface
fcntl.hstdio.hunistd.hstdint.hhelpers/helpers.h
Detected Declarations
function read_msrfunction write_msrfunction msr_intel_get_turbo_ratio
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
#if defined(__i386__) || defined(__x86_64__)
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include "helpers/helpers.h"
/* Intel specific MSRs */
#define MSR_IA32_PERF_STATUS 0x198
#define MSR_IA32_MISC_ENABLES 0x1a0
#define MSR_NEHALEM_TURBO_RATIO_LIMIT 0x1ad
/*
* read_msr
*
* Will return 0 on success and -1 on failure.
* Possible errno values could be:
* EFAULT -If the read/write did not fully complete
* EIO -If the CPU does not support MSRs
* ENXIO -If the CPU does not exist
*/
int read_msr(int cpu, unsigned int idx, unsigned long long *val)
{
int fd;
char msr_file_name[64];
sprintf(msr_file_name, "/dev/cpu/%d/msr", cpu);
fd = open(msr_file_name, O_RDONLY);
if (fd < 0)
return -1;
if (lseek(fd, idx, SEEK_CUR) == -1)
goto err;
if (read(fd, val, sizeof *val) != sizeof *val)
goto err;
close(fd);
return 0;
err:
close(fd);
return -1;
}
/*
* write_msr
*
* Will return 0 on success and -1 on failure.
* Possible errno values could be:
* EFAULT -If the read/write did not fully complete
* EIO -If the CPU does not support MSRs
* ENXIO -If the CPU does not exist
*/
int write_msr(int cpu, unsigned int idx, unsigned long long val)
{
int fd;
char msr_file_name[64];
sprintf(msr_file_name, "/dev/cpu/%d/msr", cpu);
fd = open(msr_file_name, O_WRONLY);
if (fd < 0)
return -1;
if (lseek(fd, idx, SEEK_CUR) == -1)
goto err;
if (write(fd, &val, sizeof val) != sizeof val)
goto err;
close(fd);
return 0;
err:
close(fd);
return -1;
}
unsigned long long msr_intel_get_turbo_ratio(unsigned int cpu)
{
unsigned long long val;
int ret;
if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_HAS_TURBO_RATIO))
return -1;
ret = read_msr(cpu, MSR_NEHALEM_TURBO_RATIO_LIMIT, &val);
if (ret)
return ret;
return val;
}
#endif
Annotation
- Immediate include surface: `fcntl.h`, `stdio.h`, `unistd.h`, `stdint.h`, `helpers/helpers.h`.
- Detected declarations: `function read_msr`, `function write_msr`, `function msr_intel_get_turbo_ratio`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.