From 04295831e64e24d0f448c349989b4c5619a52942 Mon Sep 17 00:00:00 2001 From: jhicks Date: Sat, 10 Sep 2016 23:17:39 -0500 Subject: [PATCH 1/2] add Kbuild file, adjust Makefile a bit for cross-compiling Note: The install target still doesn't work for cross-compiling. Be careful to not use the install targets unless you are host-compiling. --- driver/linux/Kbuild | 14 ++++++++++++++ driver/linux/Makefile | 29 +++++++++++++---------------- 2 files changed, 27 insertions(+), 16 deletions(-) create mode 100644 driver/linux/Kbuild diff --git a/driver/linux/Kbuild b/driver/linux/Kbuild new file mode 100644 index 0000000..2e50994 --- /dev/null +++ b/driver/linux/Kbuild @@ -0,0 +1,14 @@ +# Kbuild file for riffa driver + +NAME ?= riffa +MAJOR_NUM ?= 100 +VENDOR_ID0 ?= 10EE +VENDOR_ID1 ?= 1172 + +ccflags-y := -DMAJOR_NUM=$(MAJOR_NUM) \ + -DDEVICE_NAME='"$(NAME)"' \ + -DVENDOR_ID0=0x$(VENDOR_ID0) \ + -DVENDOR_ID1=0x$(VENDOR_ID1) + +obj-m := $(NAME).o +$(NAME)-y := riffa_driver.o circ_queue.o diff --git a/driver/linux/Makefile b/driver/linux/Makefile index 97952bd..6055dc3 100644 --- a/driver/linux/Makefile +++ b/driver/linux/Makefile @@ -43,14 +43,14 @@ # like, but make sure they will work in your system. # The VENDOR_ID _must_ match what is configured on your FPGA's PCIe endpoint # header. Xilinx has a VENDOR_ID = 10EE. -NAME := riffa -VENDOR_ID0 := 10EE -VENDOR_ID1 := 1172 -MAJNUM := 100 +NAME ?= riffa +VENDOR_ID0 ?= 10EE +VENDOR_ID1 ?= 1172 +MAJNUM ?= 100 # Build variables -KVER := $(shell uname -r) -KDIR := /lib/modules/`uname -r`/build +KVER ?= $(shell uname -r) +KDIR ?= /lib/modules/`uname -r`/build RHR := /etc/redhat-release LIB_SRCS := riffa.c LIB_OBJS := $(patsubst %.c,%.o,$(LIB_SRCS)) @@ -61,8 +61,7 @@ LIB_VER := $(LIB_VER_MAJ).$(LIB_VER_MIN) DRVR_HDR := riffa_driver.h DBUGVAL := DBUG -obj-m += $(NAME).o -$(NAME)-y := riffa_driver.o circ_queue.o +CC=$(CROSS_COMPILE)cc # Helper functions define assert @@ -88,17 +87,15 @@ all: builddvr debug: CC += -DDEBUG -g debug: DBUGVAL = DEBUG debug: builddvr +$(NAME).so.$(LIB_VER): CC += -DDEVICE_NAME='"$(NAME)"' \ + -DMAJOR_NUM=$(MAJNUM) \ + -DVENDOR_ID0=0x$(VENDOR_ID0) \ + -DVENDOR_ID1=0x$(VENDOR_ID1) builddvr: $(NAME).ko $(NAME).so.$(LIB_VER) $(NAME).ko: *.c *.h $(call assert-variables) - sed -i 's/#define MAJOR_NUM [^\n]*/#define MAJOR_NUM $(MAJNUM)/g' $(DRVR_HDR) - sed -i 's/#define DEVICE_NAME [^\n]*/#define DEVICE_NAME "$(NAME)"/g' $(DRVR_HDR) - sed -i 's/#define VENDOR_ID0 [^\n]*/#define VENDOR_ID0 0x$(VENDOR_ID0)/g' $(DRVR_HDR) - sed -i 's/#define VENDOR_ID1 [^\n]*/#define VENDOR_ID1 0x$(VENDOR_ID1)/g' $(DRVR_HDR) - sed -i 's/#define DEBUG [^\n]*/#define DBUG 1/g' $(DRVR_HDR) - sed -i 's/#define DBUG [^\n]*/#define $(DBUGVAL) 1/g' $(DRVR_HDR) - make -C $(KDIR) SUBDIRS=`pwd` modules + make -C $(KDIR) M=$(PWD) rm -rf $(LIB_OBJS) $(NAME).so.$(LIB_VER): $(LIB_OBJS) @@ -109,7 +106,7 @@ $(LIB_OBJS): $(LIB_SRCS) load: $(NAME).ko insmod $(NAME).ko - + unload: rmmod $(NAME) From b7ce46c7b6b31b6d812dd71433c23ea57bfe3d93 Mon Sep 17 00:00:00 2001 From: jhicks Date: Sat, 10 Sep 2016 23:20:43 -0500 Subject: [PATCH 2/2] Get rid of the compiler emitted libgcc calls --- driver/linux/riffa_driver.c | 27 +++++++++++++++++---------- driver/linux/riffa_driver.h | 11 +++++++++++ 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/driver/linux/riffa_driver.c b/driver/linux/riffa_driver.c index dc4b86b..df5fc83 100644 --- a/driver/linux/riffa_driver.c +++ b/driver/linux/riffa_driver.c @@ -145,18 +145,25 @@ static inline void write_reg(struct fpga_state * sc, int offset, unsigned int va writel(val, sc->bar0 + (offset<<2)); } -#ifdef BUILD_32 /** - * Needed for 32 bit OS because dma_map_sg macro eventually does some 64 bit - * division. + * Converts a 64-bit RIFFA API timeout to 32-bit jiffies */ -unsigned long long __udivdi3(unsigned long long num, unsigned long long den) +static inline long timeout64_to_jiffies(uint64_t timeout) { - do_div(num, den); - return num; -} -#endif + uint32_t denom; + if (timeout == 0) + return MAX_SCHEDULE_TIMEOUT; + else { + denom = 1000; + timeout = timeout * HZ; + do_div(timeout, denom); + if (timeout > LONG_MAX) + return LONG_MAX; + else + return (long) timeout; + } +} // These are not defined in the 2.x.y kernels, so just define them #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,39) @@ -586,7 +593,7 @@ static inline unsigned int chnl_recv(struct fpga_state * sc, int chnl, DEFINE_WAIT(wait); // Convert timeout to jiffies. - tymeout = (timeout == 0 ? MAX_SCHEDULE_TIMEOUT : (timeout * HZ/1000 > LONG_MAX ? LONG_MAX : timeout * HZ/1000)); + tymeout = timeout64_to_jiffies(timeout); tymeouto = tymeout; // Initialize the sg_maps @@ -780,7 +787,7 @@ static inline unsigned int chnl_send(struct fpga_state * sc, int chnl, DEFINE_WAIT(wait); // Convert timeout to jiffies. - tymeout = (timeout == 0 ? MAX_SCHEDULE_TIMEOUT : (timeout * HZ/1000 > LONG_MAX ? LONG_MAX : timeout * HZ/1000)); + tymeout = timeout64_to_jiffies(timeout); tymeouto = tymeout; // Clear the message queue. diff --git a/driver/linux/riffa_driver.h b/driver/linux/riffa_driver.h index 255018c..64d4572 100644 --- a/driver/linux/riffa_driver.h +++ b/driver/linux/riffa_driver.h @@ -58,10 +58,21 @@ // The major device number. We can't rely on dynamic registration because ioctls // need to know it. +#ifndef MAJOR_NUM #define MAJOR_NUM 100 +#endif + +#ifndef DEVICE_NAME #define DEVICE_NAME "riffa" +#endif + +#ifndef VENDOR_ID0 #define VENDOR_ID0 0x10EE +#endif + +#ifndef VENDOR_ID1 #define VENDOR_ID1 0x1172 +#endif // Message events for readmsgs/writemsgs queues. #define EVENT_TXN_LEN 1