Author: hailfinger
Date: 2009-08-19 12:46:23 +0200 (Wed, 19 Aug 2009)
New Revision: 690
Modified:
trunk/flash.h
trunk/physmap.c
Log:
Support rdmsr/wrmsr operations on FreeBSD.
So far, AMD Geode LX is the only user of this infrastructure. It needs
/dev/cpu0 from ports/sysutils on FreeBSD during runtime on Geode LX.
Signed-off-by: Stefan Reinauer <stepan(a)coresystems.de>
Signed-off-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006(a)gmx.net>
Acked-by: Idwer Vollering <vidwer(a)gmail.com>
Acked-by: <putlinuxonit(a)gmail.com>
Modified: trunk/flash.h
===================================================================
--- trunk/flash.h 2009-08-18 23:51:22 UTC (rev 689)
+++ trunk/flash.h 2009-08-19 10:46:23 UTC (rev 690)
@@ -309,11 +309,21 @@
void physunmap(void *virt_addr, size_t len);
int setup_cpu_msr(int cpu);
void cleanup_cpu_msr(void);
-#ifndef __DARWIN__
+#if !defined(__DARWIN__) && !defined(__FreeBSD__) && !defined(__DragonFly__)
typedef struct { uint32_t hi, lo; } msr_t;
msr_t rdmsr(int addr);
int wrmsr(int addr, msr_t msr);
#endif
+#if defined(__FreeBSD__) || defined(__DragonFly__)
+/* FreeBSD already has conflicting definitions for wrmsr/rdmsr. */
+#undef rdmsr
+#undef wrmsr
+#define rdmsr freebsd_rdmsr
+#define wrmsr freebsd_wrmsr
+typedef struct { uint32_t hi, lo; } msr_t;
+msr_t freebsd_rdmsr(int addr);
+int freebsd_wrmsr(int addr, msr_t msr);
+#endif
/* internal.c */
struct pci_dev *pci_dev_find_filter(struct pci_filter filter);
Modified: trunk/physmap.c
===================================================================
--- trunk/physmap.c 2009-08-18 23:51:22 UTC (rev 689)
+++ trunk/physmap.c 2009-08-19 10:46:23 UTC (rev 690)
@@ -215,6 +215,91 @@
fd_msr = -1;
}
#else
+#if defined(__FreeBSD__) || defined(__DragonFly__)
+#include <sys/ioctl.h>
+
+typedef struct {
+ int msr;
+ uint64_t data;
+} cpu_msr_args_t;
+#define CPU_RDMSR _IOWR('c', 1, cpu_msr_args_t)
+#define CPU_WRMSR _IOWR('c', 2, cpu_msr_args_t)
+
+static int fd_msr = -1;
+
+msr_t rdmsr(int addr)
+{
+ cpu_msr_args_t args;
+
+ msr_t msr = { 0xffffffff, 0xffffffff };
+
+ args.msr = addr;
+
+ if (ioctl(fd_msr, CPU_RDMSR, &args) < 0) {
+ perror("CPU_RDMSR");
+ close(fd_msr);
+ exit(1);
+ }
+
+ msr.lo = args.data & 0xffffffff;
+ msr.hi = args.data >> 32;
+
+ return msr;
+}
+
+int wrmsr(int addr, msr_t msr)
+{
+ cpu_msr_args_t args;
+
+ args.msr = addr;
+ args.data = (((uint64_t)msr.hi) << 32) | msr.lo;
+
+ if (ioctl(fd_msr, CPU_WRMSR, &args) < 0) {
+ perror("CPU_WRMSR");
+ close(fd_msr);
+ exit(1);
+ }
+
+ return 0;
+}
+
+int setup_cpu_msr(int cpu)
+{
+ char msrfilename[64];
+ memset(msrfilename, 0, 64);
+ sprintf(msrfilename, "/dev/cpu%d", cpu);
+
+ if (fd_msr != -1) {
+ printf("MSR was already initialized\n");
+ return -1;
+ }
+
+ fd_msr = open(msrfilename, O_RDWR);
+
+ if (fd_msr < 0) {
+ perror("Error while opening /dev/cpu0");
+ printf("Did you install ports/sysutils/devcpu?\n");
+ return -1;
+ }
+
+ return 0;
+}
+
+void cleanup_cpu_msr(void)
+{
+ if (fd_msr == -1) {
+ printf("No MSR initialized.\n");
+ return;
+ }
+
+ close(fd_msr);
+
+ /* Clear MSR file descriptor */
+ fd_msr = -1;
+}
+
+#else
+
#ifdef __DARWIN__
int setup_cpu_msr(int cpu)
{
@@ -251,4 +336,5 @@
}
#endif
#endif
+#endif