aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Niewöhner <foss@mniewoehner.de>2021-04-20 18:51:42 +0200
committerPatrick Georgi <pgeorgi@google.com>2021-04-30 06:41:08 +0000
commit1d9f8b0c428885cfe807752716716f656eb018e5 (patch)
tree987dfc6323ad0b1bdc27121b5dc73e3513054157 /src
parent3f36a8c7e15233a1b516f06bfa1b068f7bb34147 (diff)
cpu/x86/msr: introduce helpers msr_read, msr_write
The existing helpers for reading/writing MSRs (rdmsr, wrmsr) require use of the struct `msr_t`, which splits the MSR value into two 32 bit parts. In many cases, where simple 32 bit or 64 bit values are written, this bloats the code by unnecessarly having to use that struct. Thus, introduce the helpers `msr_read` and `msr_write`, which take or return `uint64_t` values, so the code condenses to a single line or two, without having to deal with `msr_t`. Example 1: ~~~ msr_t msr = { .lo = read32((void *)(uintptr_t)0xfed30880), .hi = 0, }; msr.lo |= 1; wrmsr(0x123, msr); ~~~ becomes ~~~ uint32_t foo = read32((void *)(uintptr_t)0xfed30880); msr_write(0x123, foo | 1) ~~~ Example 2: ~~~ msr_t msr = rdmsr(0xff); uint64_t msr_val = (msr.hi << 32) | msr.lo; ~~~ becomes ~~~ uint64_t msr_val = msr_read(0xff); ~~~ Change-Id: I27333a4bdfe3c8cebfe49a16a4f1a066f558c4ce Signed-off-by: Michael Niewöhner <foss@mniewoehner.de> Reviewed-on: https://review.coreboot.org/c/coreboot/+/52548 Reviewed-by: Nico Huber <nico.h@gmx.de> Reviewed-by: Felix Held <felix-coreboot@felixheld.de> Reviewed-by: Tim Wawrzynczak <twawrzynczak@chromium.org> Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Diffstat (limited to 'src')
-rw-r--r--src/include/cpu/x86/msr.h26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/include/cpu/x86/msr.h b/src/include/cpu/x86/msr.h
index 5ae3ddf93a..bc367d72ec 100644
--- a/src/include/cpu/x86/msr.h
+++ b/src/include/cpu/x86/msr.h
@@ -301,6 +301,32 @@ static inline enum mca_err_code_types mca_err_type(msr_t reg)
}
/**
+ * Helper for reading a MSR
+ *
+ * @param[in] reg The MSR.
+ */
+static inline uint64_t msr_read(unsigned int reg)
+{
+ msr_t msr = rdmsr(reg);
+ return (((uint64_t)msr.hi << 32) | msr.lo);
+}
+
+/**
+ * Helper for writing a MSR
+ *
+ * @param[in] reg The MSR.
+ * @param[in] value The value to be written to the MSR.
+ */
+static inline void msr_write(unsigned int reg, uint64_t value)
+{
+ msr_t msr = {
+ .lo = (unsigned int)value,
+ .hi = (unsigned int)(value >> 32)
+ };
+ wrmsr(reg, msr);
+}
+
+/**
* Helper for (un)setting MSR bitmasks
*
* @param[in] reg The MSR.