Skip to content

Commit

Permalink
Add Linux litepcie kernel module source
Browse files Browse the repository at this point in the history
  • Loading branch information
rjonaitis committed Mar 25, 2024
1 parent be69ae1 commit 3c3e277
Show file tree
Hide file tree
Showing 23 changed files with 3,101 additions and 7 deletions.
13 changes: 12 additions & 1 deletion src/comms/PCIe/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,15 @@ endif()
## Add to library
########################################################################
target_sources(${MAIN_LIBRARY_NAME} PRIVATE ${COMMS_LITE_PCIE_SOURCES})
target_include_directories(${MAIN_LIBRARY_NAME} PUBLIC ${THIS_SOURCE_DIR} ${THIS_SOURCE_DIR}/software/user/liblitepcie ${THIS_SOURCE_DIR}/software/kernel)
target_include_directories(${MAIN_LIBRARY_NAME} PUBLIC ${THIS_SOURCE_DIR})

# build kernel module only on Linux
if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
target_include_directories(${MAIN_LIBRARY_NAME} PRIVATE ${THIS_SOURCE_DIR}/linux-kernel-module)

cmake_dependent_option(LITEPCIE_KERNEL_MODULE "Build LitePCIe Linux kernel module" ON "UNIX; NOT APPLE" OFF)
add_feature_info(LITEPCIE_KERNEL_MODULE LITEPCIE_KERNEL_MODULE "Build linux litepcie kernel module")
if (LITEPCIE_KERNEL_MODULE)
add_subdirectory(${THIS_SOURCE_DIR}/linux-kernel-module)
endif()
endif()
2 changes: 1 addition & 1 deletion src/comms/PCIe/LitePCIe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <poll.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include "software/kernel/litepcie.h"
#include "linux-kernel-module/litepcie.h"
#endif

using namespace std;
Expand Down
4 changes: 0 additions & 4 deletions src/comms/PCIe/LitePCIe.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@
#include "limesuite/config.h"
#include "limesuite/OpStatus.h"

#ifdef __unix__
#include "software/kernel/litepcie.h"
#endif // __unix__

namespace lime {

/** @brief Class for communicating with a PCIe device. */
Expand Down
84 changes: 84 additions & 0 deletions src/comms/PCIe/linux-kernel-module/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
---
Language: Cpp
AlignAfterOpenBracket: DontAlign
AlignConsecutiveMacros: false
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: DontAlign
AlignOperands: true
AlignTrailingComments: false
AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: InlineOnly
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
BinPackArguments: false
BinPackParameters: false
BreakBeforeBraces: Custom
BraceWrapping:
AfterCaseLabel: false
AfterControlStatement: Always
AfterEnum: false
AfterFunction: true
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeElse: true
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakInheritanceList: BeforeComma
BreakBeforeTernaryOperators: true
ColumnLimit: 132
ContinuationIndentWidth: 4
DeriveLineEnding: false
DerivePointerAlignment: false
IncludeBlocks: Preserve

IndentCaseLabels: false
IndentGotoLabels: true
IndentPPDirectives: BeforeHash
IndentWidth: 4
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
ReflowComments: false
SortIncludes: false
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceBeforeSquareBrackets: false
TabWidth: 4
UseCRLF: false
UseTab: Never
---
1 change: 1 addition & 0 deletions src/comms/PCIe/linux-kernel-module/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/**
95 changes: 95 additions & 0 deletions src/comms/PCIe/linux-kernel-module/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
cmake_minimum_required(VERSION 3.2.0 FATAL_ERROR)

project(litepcie-kernel VERSION 0.1.0 LANGUAGES C)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_LIST_DIR}/cmake")
set(MODULE_NAME litepcie)

find_package(LinuxKernelHeaders REQUIRED)

# Get kernel version
execute_process(COMMAND uname -r
OUTPUT_VARIABLE KERNEL_RELEASE
OUTPUT_STRIP_TRAILING_WHITESPACE
)
set(KERNEL_SOURCE_DIR /lib/modules/${KERNEL_RELEASE}/build)

# Get architecture
execute_process(COMMAND uname -m
OUTPUT_VARIABLE ARCH
OUTPUT_STRIP_TRAILING_WHITESPACE
)
# ARCH might be 'aarch64', but the linux lib directories might be named 'arm64'
if(${ARCH} STREQUAL "aarch64" AND NOT EXISTS ${KERNEL_SOURCE_DIR}/arch/${ARCH})
set(ARCH "arm64")
endif()

# where Kbuild file will be placed
set(KBUILD_FILE_DIR ${CMAKE_CURRENT_BINARY_DIR})

# Generate the Kbuild file through cmake.
FILE(WRITE ${KBUILD_FILE_DIR}/Kbuild
"ccflags-y = -Wno-declaration-after-statement
ccflags-y = -I${KBUILD_FILE_DIR}/bsp
obj-m = litepcie.o"
)

set(MODULE_OBJECT ${KBUILD_FILE_DIR}/${MODULE_NAME}.ko)
set(KBUILD_CMD
$(MAKE)
-C ${KERNEL_SOURCE_DIR}
ARCH=${ARCH}
# Informs kbuild that an external module is being built.
# The value given to "M" is the absolute path of the directory where the external module (kbuild file) is located.
M=${KBUILD_FILE_DIR}
modules
)

set(KBUILD_CLEAN_CMD
$(MAKE)
-C ${KERNEL_SOURCE_DIR}
ARCH=${ARCH}
M=${KBUILD_FILE_DIR}
clean
)

set(KERNEL_SOURCE_RELATIVE_PATHS
litepcie.c
litepcie.h
boards.h
bsp/config.h
bsp/csr.h
bsp/flags.h
bsp/mem.h
bsp/soc.h
)

# Copy all source files into build directory and compile there, as the Kbuild produces artifacts in tree
foreach(SRC_FILENAME ${KERNEL_SOURCE_RELATIVE_PATHS})
configure_file(${CMAKE_CURRENT_LIST_DIR}/${SRC_FILENAME} ${KBUILD_FILE_DIR}/${SRC_FILENAME} COPYONLY)
endforeach()

add_custom_command(OUTPUT ${MODULE_OBJECT}
COMMAND ${KBUILD_CLEAN_CMD}
COMMAND ${KBUILD_CMD}
WORKING_DIRECTORY ${KBUILD_FILE_DIR}
DEPENDS ${CMAKE_CURRENT_LIST_DIR} # rebuild if anything changes in the source dir
VERBATIM
COMMENT "Building Linux kernel module in dir: ${KBUILD_FILE_DIR}"
)

add_custom_target(litepcie-kernel ALL DEPENDS ${MODULE_OBJECT})

# If module is already loaded, unload it
install(CODE "exec_program(${CMAKE_CURRENT_LIST_DIR}/unload.sh ARGS ${MODULE_NAME})")
set(KERNEL_MODULE_DESTINATION /lib/modules/${KERNELRELEASE}/extra)
install(
FILES ${MODULE_OBJECT}
DESTINATION ${KERNEL_MODULE_DESTINATION}
)
# load installed module
install(CODE "exec_program(${CMAKE_CURRENT_LIST_DIR}/install.sh ARGS ${MODULE_NAME})")

add_custom_target(uninstall-kernel-module
COMMAND ${CMAKE_CURRENT_LIST_DIR}/uninstall.sh ${MODULE_NAME}
COMMENT "Uninstalling Linux kernel module ${MODULE_NAME}"
)
22 changes: 22 additions & 0 deletions src/comms/PCIe/linux-kernel-module/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Makefile for kernel module
KERNEL_VERSION:=$(shell uname -r)
KERNEL_PATH?=/lib/modules/$(KERNEL_VERSION)/build
# ARCH?=$(shell uname -m)
# ARCH might be 'aarch64', but the linux lib directories might be named 'arm64'
ARCH?=$(shell if [ "$(shell uname -m)" = "aarch64" ] && [ ! -d $(KERNEL_PATH)/arch/$(shell uname -m) ]; then echo "arm64"; else echo $(shell uname -m); fi)

obj-m = litepcie.o
litepcie-objs = main.o

all: litepcie.ko

export EXTRA_CFLAGS := -std=gnu99 -Wno-declaration-after-statement

litepcie.ko: main.c
make -C $(KERNEL_PATH) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) M=$(shell pwd) modules

litepcie.ko: litepcie.h config.h flags.h csr.h soc.h

clean:
make -C $(KERNEL_PATH) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) M=$(shell pwd) clean
rm -f *~
11 changes: 11 additions & 0 deletions src/comms/PCIe/linux-kernel-module/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Linux litepcie driver

Linux Litepcie kernel module for communicating with LimeSDR devices using PCIe interface.

## Installation
<pre>
mkdir build && cd build
cmake ../ && cmake --build .
sudo make install
</pre>

78 changes: 78 additions & 0 deletions src/comms/PCIe/linux-kernel-module/boards.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#ifndef LMS_BOARDS_H
#define LMS_BOARDS_H

enum eLMS_DEV {
LMS_DEV_UNKNOWN = 0,
LMS_DEV_EVB6 = 1,
LMS_DEV_DIGIGREEN = 2,
LMS_DEV_DIGIRED = 3, //2x USB3, LMS6002,
LMS_DEV_EVB7 = 4,
LMS_DEV_ZIPPER = 5, //MyRiad bridge to FMC, HSMC bridge
LMS_DEV_SOCKETBOARD = 6,
LMS_DEV_EVB7V2 = 7,
LMS_DEV_STREAM = 8, //Altera Cyclone IV, USB3, 2x 128 MB RAM, RFDIO, FMC
LMS_DEV_NOVENA = 9, //Freescale iMX6 CPU
LMS_DEV_DATASPARK = 10, //Altera Cyclone V, 2x 256 MB RAM, 2x FMC (HPC, LPC), USB3
LMS_DEV_RFSPARK = 11, //LMS7002 EVB
LMS_DEV_LMS6002USB = 12, //LM6002-USB (USB stick: FX3, FPGA, LMS6002, RaspberryPi con)
LMS_DEV_RFESPARK = 13, //LMS7002 EVB
LMS_DEV_LIMESDR = 14, //LimeSDR-USB, 32bit FX3, 2xRAM, LMS7
LMS_DEV_LIMESDR_PCIE = 15,
LMS_DEV_LIMESDR_QPCIE = 16, //2x LMS, 14 bit ADC and DAC
LMS_DEV_LIMESDRMINI = 17, //FTDI + MAX10 + LMS
LMS_DEV_USTREAM = 18, //with expansion booards (uMyriad)
LMS_DEV_LIMESDR_SONY_PA = 19, //stand alone board with Sony PAs, tuners
LMS_DEV_LIMESDR_USB_SP = 20,
LMS_DEV_LMS7002M_ULTIMATE_EVB = 21,
LMS_DEV_LIMENET_MICRO = 22, //Raspberry Pi CM3(L), Ethernet, MAX10, LMS7002,
LMS_DEV_LIMESDR_CORE_SDR = 23, //LMS7002, Intel Cyclone 4, RAM, GNSS
LMS_DEV_LIMESDR_CORE_HE = 24, //PA board
LMS_DEV_LIMESDRMINI_V2 = 25, //FTDI + ECP5 + LMS
LMS_DEV_LIMESDR_X3 = 26, // 3xLMS
LMS_DEV_LIMESDR_XTRX = 27, // XTRX
LMS_DEV_LIME_MM_X8 = 28, // X8

LMS_DEV_COUNT
};

const char LMS_DEV_NAMES[][80] = {
"UNKNOWN",
"EVB6",
"DigiGreen",
"DigiRed",
"EVB7",
"ZIPPER",
"Socket Board",
"EVB7_v2",
"Stream",
"Novena",
"DataSpark",
"RF-Spark",
"LMS6002-USB Stick",
"RF-ESpark",
"LimeSDR-USB",
"LimeSDR-PCIe",
"QPCIe",
"LimeSDR-Mini",
"uStream",
"LimeSDR SONY PA",
"LimeSDR-USB SP",
"LMS7002M Ultimate EVB",
"LimeNET-Micro",
"LimeSDR-Core",
"LimeSDR-Core-HE",
"LimeSDR-Mini_v2",
"LimeX3",
"LimeXTRX",
"LimeMM-X8",
};

static inline const char *GetDeviceName(enum eLMS_DEV device)
{
if (LMS_DEV_UNKNOWN < device && device < LMS_DEV_COUNT)
return LMS_DEV_NAMES[device];
else
return LMS_DEV_NAMES[LMS_DEV_UNKNOWN];
}

#endif
1 change: 1 addition & 0 deletions src/comms/PCIe/linux-kernel-module/bsp/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DisableFormat: true
Loading

0 comments on commit 3c3e277

Please sign in to comment.