summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc Balmer <mbalmer@netbsd.org>2014-09-25 16:11:56 +0300
committerMartin-Éric Racine <martin-eric.racine@iki.fi>2014-09-25 16:11:56 +0300
commitefb42fc933a4198645691a94fe350f361cbf55a8 (patch)
treef5c199db5cd2989b7bcadb83ad6b4eb39b590099
parent6ec1e2df7aeb78332fee0dd90900906e58aa790b (diff)
Geode MSR support for OpenBSD
This patch adds conditional support for the Geode MSR device on OpenBSD. Signed-off-by: Marc Balmer <mbalmer@netbsd.org> Signed-off-by: Matthieu Herrb <matthieu@openbsd.org>
-rw-r--r--src/geode_msr.c50
1 files changed, 46 insertions, 4 deletions
diff --git a/src/geode_msr.c b/src/geode_msr.c
index b7c56d9..8f57939 100644
--- a/src/geode_msr.c
+++ b/src/geode_msr.c
@@ -33,18 +33,36 @@
#include <fcntl.h>
#include <sys/types.h>
#include <errno.h>
+
+#ifdef __OpenBSD__
+#include <sys/ioctl.h>
+#include <machine/amdmsr.h>
+#endif
+
#include "os.h"
#include "geode.h"
+#ifdef __OpenBSD__
+#define _PATH_MSRDEV "/dev/amdmsr"
+#define X_PRIVSEP
+#else
+#define _PATH_MSRDEV "/dev/cpu/0/msr"
+#endif
+
static int
_msr_open(void)
{
static int msrfd = 0;
if (msrfd == 0) {
- msrfd = open("/dev/cpu/0/msr", O_RDWR);
+#ifdef X_PRIVSEP
+ msrfd = priv_open_device(_PATH_MSRDEV);
+#else
+ msrfd = open(_PATH_MSRDEV, O_RDWR);
+#endif
if (msrfd == -1)
- ErrorF("Unable to open /dev/cpu/0/msr: %d\n", errno);
+ FatalError("Unable to open %s: %s\n", _PATH_MSRDEV,
+ strerror(errno));
}
return msrfd;
@@ -53,6 +71,19 @@ _msr_open(void)
int
GeodeReadMSR(unsigned long addr, unsigned long *lo, unsigned long *hi)
{
+#ifdef __OpenBSD__
+ struct amdmsr_req req;
+ int fd = _msr_open();
+
+ req.addr = addr;
+
+ if (ioctl(fd, RDMSR, &req) == -1)
+ FatalError("Unable to read MSR at address %0x06x: %s\n", addr,
+ strerror(errno));
+
+ *hi = req.val >> 32;
+ *lo = req.val & 0xffffffff;
+#else
unsigned int data[2];
int fd = _msr_open();
int ret;
@@ -72,13 +103,24 @@ GeodeReadMSR(unsigned long addr, unsigned long *lo, unsigned long *hi)
*hi = data[1];
*lo = data[0];
-
+#endif
return 0;
}
int
GeodeWriteMSR(unsigned long addr, unsigned long lo, unsigned long hi)
{
+#ifdef __OpenBSD__
+ struct amdmsr_req req;
+ int fd = _msr_open();
+
+ req.addr = addr;
+ req.val = (u_int64_t) hi << 32 | (u_int64_t)lo;
+
+ if (ioctl(fd, WRMSR, &req) == -1)
+ FatalError("Unable to write MSR at address 0x%06x: %s\n", addr,
+ strerror(errno));
+#else
unsigned int data[2];
int fd = _msr_open();
@@ -93,6 +135,6 @@ GeodeWriteMSR(unsigned long addr, unsigned long lo, unsigned long hi)
if (write(fd, (void *) data, 8) != 8)
return -1;
-
+#endif
return 0;
}