Last active
February 27, 2019 04:58
-
-
Save buttercutter/65e9331d55a43a2815239430a28e29c6 to your computer and use it in GitHub Desktop.
Modified https://github.com/KastnerRG/riffa/blob/master/driver/linux/circ_queue.c using kernel built-in ptr_ring struct
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Filename: circ_ring.c | |
* Version: 1.0 | |
* Description: A circular buffer using API from | |
* https://github.com/torvalds/linux/blob/master/include/linux/ptr_ring.h | |
*/ | |
#include <linux/slab.h> | |
#include <linux/mm.h> | |
#include <linux/ptr_ring.h> | |
#include "circ_ring.h" | |
//#include <assert.h> | |
#define DEBUG 1 | |
#ifdef DEBUG | |
#define DEBUG_MSG(...) printk(__VA_ARGS__) | |
#else | |
#define DEBUG_MSG(...) | |
#endif | |
struct mutex lock_send; | |
struct mutex lock_recv; | |
struct mutex lock_cleanup; | |
struct ptr_ring * init_circ_queue(int len) | |
{ | |
struct ptr_ring * q; | |
q = kzalloc(sizeof(struct ptr_ring), GFP_KERNEL); | |
if (q == NULL) { | |
DEBUG_MSG(KERN_ERR "Not enough memory to allocate ptr_ring"); | |
return NULL; | |
} | |
// creates an array of length 'len' where each array location can store a struct * item | |
if(ptr_ring_init(q, len, GFP_KERNEL) != 0) { | |
DEBUG_MSG(KERN_ERR "Not enough memory to allocate ptr_ring array"); | |
return NULL; | |
} | |
return q; | |
} | |
/*inline int push_circ_queue(struct ptr_ring * buffer, struct item * item_push) | |
{ | |
mutex_lock(&lock_push); | |
// insert one item into the buffer | |
if(ptr_ring_produce_any(buffer, item_push) == 0) // operation is successful | |
{ | |
DEBUG_MSG(KERN_INFO "Successfully pushed val1 = %u and val2 = %u\n", item_push->val1, item_push->val2); | |
mutex_unlock(&lock_push); | |
return 0; | |
} | |
else { | |
DEBUG_MSG(KERN_INFO "full, not enough buffer space\n"); | |
mutex_unlock(&lock_push); | |
return 1; | |
} | |
} | |
inline int pop_circ_queue(struct ptr_ring * buffer, struct item * item_pop) | |
{ | |
mutex_lock(&lock_pop); | |
struct item * item_temp; | |
// extract one item struct containing two unsigned integers from the buffer | |
item_temp = (struct item *)ptr_ring_consume_any(buffer); | |
if(item_temp) // (!= NULL) | |
{ | |
item_pop->val1 = item_temp->val1; | |
item_pop->val2 = item_temp->val2; | |
// val1 will never be zero since the event number starts from 1 (so, val1 in push_circ_queue() will not be zero, | |
// same case after pop_circ_queue()), and 0 is only possible during initialization, not during pop_circ_queue() | |
//assert(item_pop->val1 != 0); | |
DEBUG_MSG(KERN_INFO "Before pop, head = %u , tail = %u\n", buffer->consumer_head, buffer->consumer_tail); | |
DEBUG_MSG(KERN_INFO "val1 = %u , val2 = %u\n", item_pop->val1, item_pop->val2); | |
DEBUG_MSG(KERN_INFO "After pop, head = %u , tail = %u\n", buffer->consumer_head, buffer->consumer_tail); | |
mutex_unlock(&lock_pop); | |
return 0; | |
} | |
else { | |
//DEBUG_MSG(KERN_INFO "empty, nothing to pop from the ring\n"); | |
mutex_unlock(&lock_pop); | |
return 1; | |
} | |
}*/ | |
void free_circ_queue(struct ptr_ring * q) | |
{ | |
mutex_lock(&lock_cleanup); | |
ptr_ring_cleanup(q, NULL); | |
mutex_unlock(&lock_cleanup); | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ---------------------------------------------------------------------- | |
# Copyright (c) 2016, The Regents of the University of California All | |
# rights reserved. | |
# | |
# Redistribution and use in source and binary forms, with or without | |
# modification, are permitted provided that the following conditions are | |
# met: | |
# | |
# * Redistributions of source code must retain the above copyright | |
# notice, this list of conditions and the following disclaimer. | |
# | |
# * Redistributions in binary form must reproduce the above | |
# copyright notice, this list of conditions and the following | |
# disclaimer in the documentation and/or other materials provided | |
# with the distribution. | |
# | |
# * Neither the name of The Regents of the University of California | |
# nor the names of its contributors may be used to endorse or | |
# promote products derived from this software without specific | |
# prior written permission. | |
# | |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL REGENTS OF THE | |
# UNIVERSITY OF CALIFORNIA BE LIABLE FOR ANY DIRECT, INDIRECT, | |
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | |
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR | |
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE | |
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH | |
# DAMAGE. | |
# ---------------------------------------------------------------------- | |
# Filename: Makefile | |
# Version: 2.0 | |
# Description: Makefile for Linux PCIe device driver for RIFFA. | |
# Author: Matthew Jacobsen | |
# History: @mattj: Initial release. Version 2.0. | |
# You must specify the following variables. You can leave the defaults if you | |
# 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 | |
# Build variables | |
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)) | |
LIB_HDR := riffa.h | |
LIB_VER_MAJ := 1 | |
LIB_VER_MIN := 0 | |
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_ring.o | |
# Helper functions | |
define assert | |
$(if $1,,$(error Assertion failed: $2)) | |
endef | |
define assert-not-null | |
$(call assert,$($1),The variable "$1" is null, please specify it.) | |
endef | |
define assert-variables | |
$(call assert-not-null,NAME) | |
$(call assert-not-null,MAJNUM) | |
$(call assert-not-null,VENDOR_ID0) | |
$(call assert-not-null,VENDOR_ID1) | |
@printf "Compiling driver for kernel: %s with the following values\n" $(KVER) | |
@printf " NAME: '%s'\n" $(NAME) | |
@printf " MAJNUM: '%s'\n" $(MAJNUM) | |
@printf "VENDOR_ID0: '%s'\n" $(VENDOR_ID0) | |
@printf "VENDOR_ID1: '%s'\n" $(VENDOR_ID1) | |
@printf "\n" | |
endef | |
all: CC += -O3 -pthread -pedantic -Wall -Werror -Wextra -fsanitize=thread -fno-omit-frame-pointer | |
all: builddvr | |
debug: CC += -DDEBUG -g -O3 -pthread -pedantic -Wall -Werror -Wextra -Warray-bounds=2 -Wstringop-overflow=4 -Wnull-dereference -Wchkp -fdelete-null-pointer-checks -fsanitize=thread -fno-omit-frame-pointer -mmpx | |
debug: DBUGVAL = DEBUG | |
debug: builddvr | |
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) V=1 CONFIG_DEBUG_INFO=Y CONFIG_SCHED_DEBUG=Y CONFIG_PM_DEBUG=Y SUBDIRS=`pwd` modules | |
rm -rf $(LIB_OBJS) | |
$(NAME).so.$(LIB_VER): $(LIB_OBJS) | |
$(CC) -shared -Wl,-soname,lib$(NAME).so.$(LIB_VER_MAJ) -o lib$@ $^ | |
$(LIB_OBJS): $(LIB_SRCS) | |
$(CC) -g -Wall -fPIC -c $^ | |
load: $(NAME).ko | |
insmod $(NAME).ko | |
unload: | |
rmmod $(NAME) | |
clean: | |
rm -Rf *.ko *.cmd *.o *.so *.so.* .*.cmd Module.symvers Module.markers modules.order *.mod.c .tmp_versions | |
setup: | |
if [ -f "$(RHR)" ]; then yum install kernel-devel-`uname -r`;\ | |
else apt-get install linux-headers-`uname -r`; fi | |
install: $(NAME).so.$(LIB_VER) $(NAME).ko | |
mkdir -p /lib/modules/$(KVER)/kernel/drivers/$(NAME) | |
cp $(NAME).ko /lib/modules/$(KVER)/kernel/drivers/$(NAME)/ | |
if [ -f "$(RHR)" ]; then\ | |
printf "%b\n" "#!/bin/sh\nexec /sbin/modprobe $(NAME) >/dev/null 2>&1" > /etc/sysconfig/modules/$(NAME).modules;\ | |
chmod 755 /etc/sysconfig/modules/$(NAME).modules;\ | |
else\ | |
if ! grep -Fxq "$(NAME)" /etc/modules; then echo "$(NAME)" >> /etc/modules; fi;\ | |
fi | |
printf "%b\n" "KERNEL==\"$(NAME)\", MODE=\"777\", GROUP=\"root\"" > /etc/udev/rules.d/99-$(NAME).rules | |
printf "/usr/local/lib\n" > $(NAME).conf | |
mv $(NAME).conf /etc/ld.so.conf.d/ | |
cp $(DRVR_HDR) /usr/local/include/ | |
cp $(LIB_HDR) /usr/local/include/ | |
mv lib$(NAME).so.1.0 /usr/local/lib | |
ln -sf /usr/local/lib/lib$(NAME).so.$(LIB_VER) /usr/local/lib/lib$(NAME).so.$(LIB_VER_MAJ) | |
ln -sf /usr/local/lib/lib$(NAME).so.$(LIB_VER) /usr/local/lib/lib$(NAME).so | |
ldconfig | |
depmod | |
make unload | |
make load | |
uninstall: | |
rm -f /usr/local/lib/lib$(NAME).so* | |
rm -f /usr/local/include/$(LIB_HDR) | |
rm -f /usr/local/include/$(DRVR_HDR) | |
rm -f /etc/ld.so.conf.d/$(NAME).conf | |
rm -rf /lib/modules/$(KVER)/kernel/drivers/$(NAME) | |
rm -f /etc/udev/rules.d/99-$(NAME).rules | |
if [ -f "$(RHR)" ]; then rm -f /etc/sysconfig/modules/$(NAME).modules;\ | |
else cp /etc/modules ./etc.modules.bak; sed -i '/$(NAME)/d' /etc/modules; fi | |
ldconfig | |
depmod | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment