Patchwork inteltool support for FreeBSD

login
register
about
Submitter Idwer Vollering
Date 2010-10-17 16:37:31
Message ID <AANLkTikJfQgxV9HRkaCno5mNTJn=u3YZH63PpYbjLeAU@mail.gmail.com>
Download mbox | patch
Permalink /patch/2129/
State Superseded
Delegated to: Warren Turkal
Headers show

Comments

Idwer Vollering - 2010-10-17 16:37:31
Add support for FreeBSD.

Signed-off-by: Idwer Vollering <vidwer@gmail.com>

---

I took the liberty to copy MSR code from flashrom's hwaccess.h.
Patch was written using FreeBSD 8.1, earlier versions might or might not
work.
Warren Turkal - 2010-10-18 05:07:41
On Sunday, October 17, 2010 09:37:31 am Idwer Vollering wrote:
> Add support for FreeBSD.
> 
> Signed-off-by: Idwer Vollering <vidwer@gmail.com>

In inteltool.h:
* Can you please briefly explain the need for the macros for {IN,OUT}{B,W,L} 
when I don't seen them called from anywhere in the code?
* If you mean to use them, why are they implemented as macros instead of 
functions. I think it'd be easier to read if they were function, and gcc could 
possibly even inline such a simple function.

In inteltool.c:
* Why not just include unistd.h on all platforms?
* I think the #ifdef __FREEBSD__ just makes the code difficult to read. I think 
the platform specific code need to be factored out somehow.
* The io_fd variable doesn't appear to be used anywhere after opening the 
/dev/io file. Doesn't it need to be closed somewhere? If not, why even bother 
creating a variable to hold the value of the open instead of just testing it 
directly?

The Makefile change looks ok.

Thanks,
wt

Patch

Index: inteltool.h
===================================================================
--- inteltool.h	(revision 5954)
+++ inteltool.h	(working copy)
@@ -29,6 +29,23 @@ 
 #endif
 #include <pci/pci.h>
 
+#if defined(__FreeBSD__)
+	#include <machine/cpufunc.h>
+	#define OUTB(x, y) do { u_int tmp = (y); outb(tmp, (x)); } while (0)
+	#define OUTW(x, y) do { u_int tmp = (y); outw(tmp, (x)); } while (0)
+	#define OUTL(x, y) do { u_int tmp = (y); outl(tmp, (x)); } while (0)
+	#define INB(x) __extension__ ({ u_int inb_tmp = (x); inb(inb_tmp); })
+	#define INW(x) __extension__ ({ u_int inw_tmp = (x); inw(inw_tmp); })
+	#define INL(x) __extension__ ({ u_int inl_tmp = (x); inl(inl_tmp); })
+#else
+	#define OUTB outb
+	#define OUTW outw
+	#define OUTL outl
+	#define INB inb
+	#define INW inw
+	#define INL inl
+#endif
+
 #define INTELTOOL_VERSION "1.0"
 
 /* Tested chipsets: */
@@ -88,9 +105,19 @@ 
 
 #define ARRAY_SIZE(a) ((int)(sizeof(a) / sizeof((a)[0])))
 
-#ifndef __DARWIN__
+#if !defined(__DARWIN__) && !defined(__FreeBSD__)
 typedef struct { uint32_t hi, lo; } msr_t;
 #endif
+#if defined (__FreeBSD__)
+/* 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
 typedef struct { uint16_t addr; int size; char *name; } io_register_t;
 
 void *map_physical(unsigned long phys_addr, size_t len);
Index: inteltool.c
===================================================================
--- inteltool.c	(revision 5954)
+++ inteltool.c	(working copy)
@@ -24,6 +24,9 @@ 
 #include <fcntl.h>
 #include <sys/mman.h>
 #include "inteltool.h"
+#if defined(__FreeBSD__)
+#include <unistd.h>
+#endif
 
 static const struct {
 	uint16_t vendor_id, device_id;
@@ -213,7 +216,17 @@ 
 		}
 	}
 
+#if defined(__FreeBSD__)
+	int io_fd;
+#endif
+
+#if defined(__FreeBSD__)
+	if ((io_fd = open("/dev/io", O_RDWR)) < 0) {
+		perror("/dev/io");
+#else
 	if (iopl(3)) {
+		perror("iopl");
+#endif
 		printf("You need to be root.\n");
 		exit(1);
 	}
Index: Makefile
===================================================================
--- Makefile	(revision 5954)
+++ Makefile	(working copy)
@@ -33,6 +33,11 @@ 
 ifeq ($(OS_ARCH), Darwin)
 LDFLAGS = -framework DirectIO -lpci -lz
 endif
+ifeq ($(OS_ARCH), FreeBSD)
+CFLAGS += -I/usr/local/include
+LDFLAGS += -L/usr/local/lib
+LIBS = -lz
+endif
 
 all: pciutils dep $(PROGRAM)