From 7b2bf826bbe003fa65703cc91c44243c80e9744a Mon Sep 17 00:00:00 2001 From: Michael Margolin Date: Thu, 9 Nov 2023 14:36:27 +0000 Subject: [PATCH] linux/efa_nv_peermem: Add efa_nv_peermem kernel module Add a new kernel module that links to Nvidia P2P API functions and supplies EFA NV peermem API through EXPORT_SYMBOL_GPL. This will intentionally work only with Nvidia opensource driver that is licensed as GPL but at this point in time doesn't export p2p symbols using EXPORT_SYMBOL_GPL. Reviewed-by: Daniel Kranzdorf Reviewed-by: Yehuda Yitschak Signed-off-by: Michael Margolin --- kernel/linux/efa_nv_peermem/CMakeLists.txt | 36 ++ kernel/linux/efa_nv_peermem/COPYING | 121 +++++ kernel/linux/efa_nv_peermem/README | 52 ++ kernel/linux/efa_nv_peermem/RELEASENOTES.md | 8 + kernel/linux/efa_nv_peermem/build.sh | 18 + .../efa_nv_peermem/conf/configure-dkms.sh | 27 + kernel/linux/efa_nv_peermem/conf/dkms.conf | 13 + .../efa_nv_peermem/conf/efa_nv_peermem.conf | 2 + kernel/linux/efa_nv_peermem/debian/changelog | 4 + kernel/linux/efa_nv_peermem/debian/compat | 1 + kernel/linux/efa_nv_peermem/debian/control | 12 + kernel/linux/efa_nv_peermem/debian/copyright | 10 + .../debian/efa-nv-peermem.postinst | 18 + .../debian/efa-nv-peermem.prerm | 10 + kernel/linux/efa_nv_peermem/debian/rules | 42 ++ .../linux/efa_nv_peermem/debian/source/format | 1 + kernel/linux/efa_nv_peermem/rpm/Makefile | 40 ++ .../efa_nv_peermem/rpm/efa-nv-peermem.spec | 74 +++ .../linux/efa_nv_peermem/src/CMakeLists.txt | 30 ++ kernel/linux/efa_nv_peermem/src/Kbuild.in | 2 + .../efa_nv_peermem/src/efa_nv_peermem_main.c | 87 ++++ kernel/linux/efa_nv_peermem/src/nv-p2p.h | 478 ++++++++++++++++++ 22 files changed, 1086 insertions(+) create mode 100644 kernel/linux/efa_nv_peermem/CMakeLists.txt create mode 100644 kernel/linux/efa_nv_peermem/COPYING create mode 100644 kernel/linux/efa_nv_peermem/README create mode 100644 kernel/linux/efa_nv_peermem/RELEASENOTES.md create mode 100755 kernel/linux/efa_nv_peermem/build.sh create mode 100644 kernel/linux/efa_nv_peermem/conf/configure-dkms.sh create mode 100644 kernel/linux/efa_nv_peermem/conf/dkms.conf create mode 100644 kernel/linux/efa_nv_peermem/conf/efa_nv_peermem.conf create mode 100644 kernel/linux/efa_nv_peermem/debian/changelog create mode 100644 kernel/linux/efa_nv_peermem/debian/compat create mode 100644 kernel/linux/efa_nv_peermem/debian/control create mode 100644 kernel/linux/efa_nv_peermem/debian/copyright create mode 100644 kernel/linux/efa_nv_peermem/debian/efa-nv-peermem.postinst create mode 100644 kernel/linux/efa_nv_peermem/debian/efa-nv-peermem.prerm create mode 100755 kernel/linux/efa_nv_peermem/debian/rules create mode 100644 kernel/linux/efa_nv_peermem/debian/source/format create mode 100644 kernel/linux/efa_nv_peermem/rpm/Makefile create mode 100644 kernel/linux/efa_nv_peermem/rpm/efa-nv-peermem.spec create mode 100644 kernel/linux/efa_nv_peermem/src/CMakeLists.txt create mode 100644 kernel/linux/efa_nv_peermem/src/Kbuild.in create mode 100644 kernel/linux/efa_nv_peermem/src/efa_nv_peermem_main.c create mode 100644 kernel/linux/efa_nv_peermem/src/nv-p2p.h diff --git a/kernel/linux/efa_nv_peermem/CMakeLists.txt b/kernel/linux/efa_nv_peermem/CMakeLists.txt new file mode 100644 index 00000000..e5892d42 --- /dev/null +++ b/kernel/linux/efa_nv_peermem/CMakeLists.txt @@ -0,0 +1,36 @@ +# SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause +# Copyright 2023 Amazon.com, Inc. or its affiliates. All rights reserved. + +cmake_minimum_required(VERSION 2.8.11) +project(efa_nv_peermem C) + +set(KERNEL_VER "" CACHE STRING "Kernel version to build for") +if(NOT KERNEL_VER) + execute_process(COMMAND uname -r OUTPUT_VARIABLE uname_r + OUTPUT_STRIP_TRAILING_WHITESPACE) + set(KERNEL_DIR "/lib/modules/${uname_r}/build") +else() + set(KERNEL_DIR "/lib/modules/${KERNEL_VER}/build") +endif() + +unset(KERNEL_MAKEFILE CACHE) +find_file(KERNEL_MAKEFILE Makefile PATHS ${KERNEL_DIR} NO_DEFAULT_PATH) +if(NOT KERNEL_MAKEFILE) + message(FATAL_ERROR "No kernel Makefile") +endif() +message("-- Kernel directory - ${KERNEL_DIR}") + +set(NVIDIA_DIR "" CACHE PATH "Path to NVIDIA directory") +if(NVIDIA_DIR) + unset(NVIDIA_MODULE_SYMVERS CACHE) + find_file(NVIDIA_MODULE_SYMVERS Module.symvers PATHS ${NVIDIA_DIR} REQUIRED NO_DEFAULT_PATH) + if(NOT NVIDIA_MODULE_SYMVERS) + message(FATAL_ERROR "No NVIDIA Module.symvers") + else() + message("-- NVIDIA directory - ${NVIDIA_DIR}") + endif() +endif() + +set(GCOV_PROFILE OFF CACHE BOOL "Enable GCOV profiling") + +add_subdirectory(src) diff --git a/kernel/linux/efa_nv_peermem/COPYING b/kernel/linux/efa_nv_peermem/COPYING new file mode 100644 index 00000000..2b1bbf47 --- /dev/null +++ b/kernel/linux/efa_nv_peermem/COPYING @@ -0,0 +1,121 @@ +GPL-2.0 +------- +GNU GENERAL PUBLIC LICENSE +Version 2, June 1991 + +Copyright (C) 1989, 1991 Free Software Foundation, Inc. +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + +Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. + +Preamble + +The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Lesser General Public License instead.) You can apply it to your programs, too. + +When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. + +To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. + +For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. + +We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. + +Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. + +Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. + +The precise terms and conditions for copying, distribution and modification follow. + +TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + +0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". +Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. + +1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. +You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. + +2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: +a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. +b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. +c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) +These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. + +3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: +a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, +b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, +c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) +The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. + +If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. + +4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. +5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. +6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. +7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. +If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. + +This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. + +8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. +9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. +Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. + +10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. +NO WARRANTY + +11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. +12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. +END OF TERMS AND CONDITIONS + +How to Apply These Terms to Your New Programs + +If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. + +To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. + + +Copyright (C) < yyyy> + +This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this when it starts in an interactive mode: + +Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: + +Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. + +, 1 April 1989 Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. + +BSD-2-Clause +------------ +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. +2. 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. +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 THE COPYRIGHT HOLDER OR CONTRIBUTORS 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. + +Linux-OpenIB +------------ +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. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/kernel/linux/efa_nv_peermem/README b/kernel/linux/efa_nv_peermem/README new file mode 100644 index 00000000..26415d66 --- /dev/null +++ b/kernel/linux/efa_nv_peermem/README @@ -0,0 +1,52 @@ +Linux kernel module for EFA support of Nvidia peermem +===================================================== + +Overview +======== +EFA NV Peermem kernel module is an extension module for the Elastic Fabric +Adapter (EFA) that is serving as a link between Nvidia opensource drivers +and the EFA driver for the needs of Cuda memory GPU direct support. + +For more information regarding GPUDirect RDMA, visit: +https://docs.nvidia.com/cuda/gpudirect-rdma/index.html + +EFA NV Peermem supports Nvidia opensource drivers only and it is unable to +load when such drivers are not avilable in the system. + +Driver compilation +================== +For list of supported kernels and distributions, please refer to the release +notes documentation in the same directory. + +sudo yum update +sudo yum install gcc +sudo yum install kernel-devel-$(uname -r) + +Compilation: +Run: +mkdir build +cd build +cmake .. +make + +efa_nv_peermem.ko is created inside the src/ folder. + +To build EFA NV Peermem RPMs run `make` in the rpm/ folder. Your environment +will need to be setup to build RPMs. The EFA NV Peermem RPM will install the +efa_nv_peermem kernel driver source, and setup DKMS in order to build the driver +when the kernel is updated. + +Driver installation +=================== +Loading driver +-------------- +insmod efa_nv_peermem.ko + +EFA NV Peermem doesn't need to be automatically started upon the OS boot, +and it will be requested by EFA driver whenever it is needed for P2P +memory registration operations to work. + +EFA NV Peermem Source Code Directory Structure (under src/) +=========================================================== +efa_nv_peermem_main.c - Main Linux kernel driver. +nv-p2p.h - NV P2P API diff --git a/kernel/linux/efa_nv_peermem/RELEASENOTES.md b/kernel/linux/efa_nv_peermem/RELEASENOTES.md new file mode 100644 index 00000000..9d24dc7e --- /dev/null +++ b/kernel/linux/efa_nv_peermem/RELEASENOTES.md @@ -0,0 +1,8 @@ +# EFA NV Peermem Linux Kernel Driver Release Notes + +## Supported Kernel Versions and Distributions +https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/efa.html#efa-amis + +## r1.1.0 release notes + +Initial commit diff --git a/kernel/linux/efa_nv_peermem/build.sh b/kernel/linux/efa_nv_peermem/build.sh new file mode 100755 index 00000000..0eb15e5b --- /dev/null +++ b/kernel/linux/efa_nv_peermem/build.sh @@ -0,0 +1,18 @@ +#!/bin/bash -e + +SRCDIR=`dirname $0` +BUILDDIR="$SRCDIR/build" + +mkdir -p "$BUILDDIR" + +if hash cmake3 2>/dev/null; then + # CentOS users are encouraged to install cmake3 from EPEL + CMAKE=cmake3 +else + CMAKE=cmake +fi + +cd "$BUILDDIR" + +$CMAKE ${EXTRA_CMAKE_FLAGS:-} .. +make diff --git a/kernel/linux/efa_nv_peermem/conf/configure-dkms.sh b/kernel/linux/efa_nv_peermem/conf/configure-dkms.sh new file mode 100644 index 00000000..59293c57 --- /dev/null +++ b/kernel/linux/efa_nv_peermem/conf/configure-dkms.sh @@ -0,0 +1,27 @@ +#!/bin/bash +# Auxiliary script for driver packaging + +kernelver=$1 +dkms_source_tree=$2 +extra_flags= + +nvidia_path=$(find $dkms_source_tree -maxdepth 1 -type d -name '*nvidia-*' | sort | tail -n 1) +if [ $nvidia_path ]; then + echo "== Building NVIDIA Module.symvers" + pushd $nvidia_path + make KERNEL_UNAME="$kernelver" clean || failed_nvidia=true + make KERNEL_UNAME="$kernelver" -j$(nproc) || failed_nvidia=true + popd + + if [ $failed_nvidia ]; then + echo "== Failed building NVIDIA Module.symvers, proceeding without" + else + echo "== Using NVIDIA driver path $nvidia_path" + extra_flags="-DNVIDIA_DIR=$nvidia_path" + fi +fi + +mkdir -p build +pushd build +cmake -DKERNEL_VER=$kernelver $extra_flags .. +popd diff --git a/kernel/linux/efa_nv_peermem/conf/dkms.conf b/kernel/linux/efa_nv_peermem/conf/dkms.conf new file mode 100644 index 00000000..71b9b8f3 --- /dev/null +++ b/kernel/linux/efa_nv_peermem/conf/dkms.conf @@ -0,0 +1,13 @@ +PACKAGE_NAME="efa-nv-peermem" +PACKAGE_VERSION="1.1.0" +CLEAN="cd build; make modules_clean; make clean" +PRE_BUILD="./configure-dkms.sh $kernelver $source_tree" +# Quoted 'make' to suppress DKMS append of KERNELRELEASE +MAKE="cd build; 'make'" +BUILT_MODULE_NAME[0]="efa_nv_peermem" +BUILT_MODULE_LOCATION="build/src/" +DEST_MODULE_LOCATION="/extra" +DEST_MODULE_NAME[0]="efa_nv_peermem" +AUTOINSTALL="yes" +NO_WEAK_MODULES="yes" +BUILD_DEPENDS="nvidia" diff --git a/kernel/linux/efa_nv_peermem/conf/efa_nv_peermem.conf b/kernel/linux/efa_nv_peermem/conf/efa_nv_peermem.conf new file mode 100644 index 00000000..b00fa5e3 --- /dev/null +++ b/kernel/linux/efa_nv_peermem/conf/efa_nv_peermem.conf @@ -0,0 +1,2 @@ +# /etc/modules-load.d/efa_nv_peermem.conf +nvidia diff --git a/kernel/linux/efa_nv_peermem/debian/changelog b/kernel/linux/efa_nv_peermem/debian/changelog new file mode 100644 index 00000000..d47dbe16 --- /dev/null +++ b/kernel/linux/efa_nv_peermem/debian/changelog @@ -0,0 +1,4 @@ +efa-nv-peermem (1.1.0-1.amzn1) unstable; urgency=medium + * Initial release of efa_nv_peermem kernel driver Debian package + + -- Michael Margolin Wed, 25 Oct 2023 16:42:57 +0000 diff --git a/kernel/linux/efa_nv_peermem/debian/compat b/kernel/linux/efa_nv_peermem/debian/compat new file mode 100644 index 00000000..f599e28b --- /dev/null +++ b/kernel/linux/efa_nv_peermem/debian/compat @@ -0,0 +1 @@ +10 diff --git a/kernel/linux/efa_nv_peermem/debian/control b/kernel/linux/efa_nv_peermem/debian/control new file mode 100644 index 00000000..bacf2985 --- /dev/null +++ b/kernel/linux/efa_nv_peermem/debian/control @@ -0,0 +1,12 @@ +Source: efa-nv-peermem +Maintainer: ec2-efa-maintainers +Build-Depends: debhelper +Standards-Version: 4.3.0 +Homepage: https://github.com/amzn/amzn-drivers + +Package: efa-nv-peermem +Architecture: any +Depends: ${shlibs:Depends}, ${misc:Depends}, dkms, cmake +Description: Linux kernel driver for EFA Nvidia peermem + Elastic Fabric Adapter (EFA) peermem provides an interface for nv-p2p + capabilities for the EFA driver use. diff --git a/kernel/linux/efa_nv_peermem/debian/copyright b/kernel/linux/efa_nv_peermem/debian/copyright new file mode 100644 index 00000000..145800f2 --- /dev/null +++ b/kernel/linux/efa_nv_peermem/debian/copyright @@ -0,0 +1,10 @@ +Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ +Upstream-Name: efa-nv-peermem +Upstream-Contact: ec2-efa-maintainers +Source: https://github.com/amzn/amzn-drivers + +Files: * +Copyright: + Copyright 2023 Amazon.com, Inc. or its affiliates. All rights reserved. +License: + SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) diff --git a/kernel/linux/efa_nv_peermem/debian/efa-nv-peermem.postinst b/kernel/linux/efa_nv_peermem/debian/efa-nv-peermem.postinst new file mode 100644 index 00000000..770ab6fc --- /dev/null +++ b/kernel/linux/efa_nv_peermem/debian/efa-nv-peermem.postinst @@ -0,0 +1,18 @@ +#!/bin/sh + +set -e + +NAME=efa-nv-peermem +DRIVER_VERSION=1.1.0 +INSTALL_PATH=/usr/src/${NAME}-${DRIVER_VERSION} + +cd $INSTALL_PATH +dkms add -m ${NAME} -v ${DRIVER_VERSION} +for kernel in $(/bin/ls /lib/modules); do + if [ -e /lib/modules/$kernel/build/include ]; then + dkms build -m ${NAME} -v ${DRIVER_VERSION} -k $kernel + dkms install --force -m ${NAME} -v ${DRIVER_VERSION} -k $kernel + fi +done + +#DEBHELPER# diff --git a/kernel/linux/efa_nv_peermem/debian/efa-nv-peermem.prerm b/kernel/linux/efa_nv_peermem/debian/efa-nv-peermem.prerm new file mode 100644 index 00000000..9d08c7ef --- /dev/null +++ b/kernel/linux/efa_nv_peermem/debian/efa-nv-peermem.prerm @@ -0,0 +1,10 @@ +#!/bin/sh + +set -e + +NAME=efa-nv-peermem +DRIVER_VERSION=1.1.0 + +dkms remove -m ${NAME} -v ${DRIVER_VERSION} --all + +#DEBHELPER# diff --git a/kernel/linux/efa_nv_peermem/debian/rules b/kernel/linux/efa_nv_peermem/debian/rules new file mode 100755 index 00000000..dbaf18d0 --- /dev/null +++ b/kernel/linux/efa_nv_peermem/debian/rules @@ -0,0 +1,42 @@ +#!/usr/bin/make -f + +include /usr/share/dpkg/architecture.mk +include /usr/share/dpkg/buildflags.mk +include /usr/share/dpkg/pkg-info.mk + +# See debhelper(7) (uncomment to enable) +# output every command that modifies files on the build system. +export DH_VERBOSE = 1 + +NAME = efa-nv-peermem +VERSION = 1.1.0 +DESTDIR:=`pwd`/debian/${NAME} +INSTALL_PATH = /usr/src/${NAME}-${VERSION} + + +%: + dh $@ + +override_dh_auto_clean: + dh_clean + +override_dh_auto_build: + echo "Skip build" + +override_dh_auto_configure: + echo "Skip configure" + +override_dh_auto_install: + mkdir -p ${DESTDIR}/${INSTALL_PATH} + mkdir -p ${DESTDIR}/${INSTALL_PATH}/config + mkdir -p ${DESTDIR}/${INSTALL_PATH}/src + install -D -m 644 conf/efa_nv_peermem.conf ${DESTDIR}/etc/modules-load.d/efa_nv_peermem.conf + install -m 644 conf/dkms.conf ${DESTDIR}/${INSTALL_PATH} + install -m 744 conf/configure-dkms.sh ${DESTDIR}/${INSTALL_PATH} + install -m 644 src/efa_nv_peermem_main.c ${DESTDIR}/${INSTALL_PATH}/src + install -m 644 src/nv-p2p.h ${DESTDIR}/${INSTALL_PATH}/src + install -m 644 src/CMakeLists.txt ${DESTDIR}/${INSTALL_PATH}/src + install -m 644 src/Kbuild.in ${DESTDIR}/${INSTALL_PATH}/src + install -m 644 README ${DESTDIR}/${INSTALL_PATH} + install -m 644 RELEASENOTES.md ${DESTDIR}/${INSTALL_PATH} + install -m 644 CMakeLists.txt ${DESTDIR}/${INSTALL_PATH} diff --git a/kernel/linux/efa_nv_peermem/debian/source/format b/kernel/linux/efa_nv_peermem/debian/source/format new file mode 100644 index 00000000..163aaf8d --- /dev/null +++ b/kernel/linux/efa_nv_peermem/debian/source/format @@ -0,0 +1 @@ +3.0 (quilt) diff --git a/kernel/linux/efa_nv_peermem/rpm/Makefile b/kernel/linux/efa_nv_peermem/rpm/Makefile new file mode 100644 index 00000000..2495643e --- /dev/null +++ b/kernel/linux/efa_nv_peermem/rpm/Makefile @@ -0,0 +1,40 @@ +# Makefile for creating rpm of the Amazon EFA NV Peermem driver + +NAME = efa-nv-peermem +SPEC = $(NAME).spec +VERSION = 1.1.0 + +TOPDIR := $(shell git rev-parse --show-toplevel) +TAG ?= HEAD +TARBALL = $(NAME)-$(VERSION).tar + +RPMDEFS = --define '_topdir %(pwd)' \ + --define '_ntopdir %(pwd)' \ + --define '_builddir %{_ntopdir}/build' \ + --define '_buildrootdir %{_builddir}' \ + --define '_sourcedir %{_ntopdir}' \ + --define '_specdir %{_ntopdir}' \ + --define '_rpmdir %{_ntopdir}' \ + --define '_srcrpmdir %{_ntopdir}' \ + --define "driver_version $(VERSION)" + +all : rpm + +tarball : $(TARBALL) +$(TARBALL) : always + (cd $(TOPDIR) && \ + git archive --format=tar --prefix=$(NAME)-$(VERSION)/ $(TAG) \ + kernel/linux/efa_nv_peermem/) > $@ + +srpm : $(TARBALL) Makefile + rpmbuild -bs $(RPMDEFS) $(SPEC) + +rpm : $(TARBALL) Makefile + rpmbuild -ba $(RPMDEFS) $(SPEC) + +clean : + rm -f $(TARBALL) *.src.rpm + +always: + +.PHONY : srpm clean always diff --git a/kernel/linux/efa_nv_peermem/rpm/efa-nv-peermem.spec b/kernel/linux/efa_nv_peermem/rpm/efa-nv-peermem.spec new file mode 100644 index 00000000..4c1cb11b --- /dev/null +++ b/kernel/linux/efa_nv_peermem/rpm/efa-nv-peermem.spec @@ -0,0 +1,74 @@ +# Copyright 2023 Amazon.com, Inc. or its affiliates. All rights reserved + +%define name efa-nv-peermem +%define driver_name efa_nv_peermem +%define debug_package %{nil} + +Name: %{name} +Version: %{driver_version} +Release: 1%{?dist} +Summary: %{driver_name} kernel module + +Group: System/Kernel +License: Dual BSD/GPL +URL: https://github.com/amzn/amzn-drivers/ +Source0: %{name}-%{version}.tar + +Requires: dkms %kernel_module_package_buildreqs cmake +%if %{defined kernel_module_package_buildreqs} +Requires: %kernel_module_package_buildreqs +%endif +# RHEL 8.4 has a broken dependency between cmake and libarchive which +# causes libarchive to not be updated properly in the update case. Express the +# dependency so that our install does not break. +%if 0%{?rhel} >= 8 +Requires: libarchive >= 3.3.3 +%endif + +%define install_path /usr/src/%{name}-%{version} + +%description +%{driver_name} kernel module source and DKMS scripts to build the kernel module. + +%prep +%setup -n %{name}-%{version} -q + +%post +cd %{install_path} +dkms add -m %{name} -v %{driver_version} +for kernel in $(/bin/ls /lib/modules); do + if [ -e /lib/modules/$kernel/build/include ]; then + dkms build -m %{name} -v %{driver_version} -k $kernel + dkms install --force -m %{name} -v %{driver_version} -k $kernel + fi +done + +%preun +dkms remove -m %{name} -v %{driver_version} --all + +%build + +%install +cd kernel/linux/efa_nv_peermem +mkdir -p %{buildroot}%{install_path} +mkdir -p %{buildroot}%{install_path}/config +mkdir -p %{buildroot}%{install_path}/src +install -D -m 644 conf/efa_nv_peermem.conf %{buildroot}/etc/modules-load.d/efa_nv_peermem.conf +install -m 644 conf/dkms.conf %{buildroot}%{install_path} +install -m 744 conf/configure-dkms.sh %{buildroot}%{install_path} +install -m 644 CMakeLists.txt %{buildroot}%{install_path} +install -m 644 README %{buildroot}%{install_path} +install -m 644 RELEASENOTES.md %{buildroot}%{install_path} +cd src +install -m 644 efa_nv_peermem_main.c %{buildroot}%{install_path}/src +install -m 644 nv-p2p.h %{buildroot}%{install_path}/src +install -m 644 CMakeLists.txt %{buildroot}%{install_path}/src +install -m 644 Kbuild.in %{buildroot}%{install_path}/src + +%files +%{install_path} +/etc/modules-load.d/efa_nv_peermem.conf + +%changelog +* Wed Oct 25 2023 Michael Margolin - 1.1.0 +- initial build for RHEL diff --git a/kernel/linux/efa_nv_peermem/src/CMakeLists.txt b/kernel/linux/efa_nv_peermem/src/CMakeLists.txt new file mode 100644 index 00000000..049e9cfd --- /dev/null +++ b/kernel/linux/efa_nv_peermem/src/CMakeLists.txt @@ -0,0 +1,30 @@ +# SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause +# Copyright 2023 Amazon.com, Inc. or its affiliates. All rights reserved. + +list(APPEND efa_nv_peermem_sources + nv-p2p.h efa_nv_peermem_main.c) + +string(REPLACE ";" " " efa_nv_peermem_sources_string "${efa_nv_peermem_sources}") +configure_file(Kbuild.in Kbuild @ONLY) + +if(NVIDIA_DIR) + file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/Kbuild +"KBUILD_EXTRA_SYMBOLS = ${NVIDIA_MODULE_SYMVERS}") +endif() + +foreach(src ${efa_nv_peermem_sources}) + configure_file(${src} ${src} COPYONLY) +endforeach() + +set(module_cmd make -C ${KERNEL_DIR} M=${CMAKE_CURRENT_BINARY_DIR}) +if(GCOV_PROFILE) + set(module_cmd ${module_cmd} GCOV_PROFILE=y) +endif() +add_custom_command(OUTPUT efa_nv_peermem.ko + COMMAND ${module_cmd} modules + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + DEPENDS ${efa_nv_peermem_sources} ${CMAKE_CURRENT_BINARY_DIR}/Kbuild + VERBATIM) + +add_custom_target(modules ALL DEPENDS efa_nv_peermem.ko) +add_custom_target(modules_clean COMMAND ${module_cmd} clean) diff --git a/kernel/linux/efa_nv_peermem/src/Kbuild.in b/kernel/linux/efa_nv_peermem/src/Kbuild.in new file mode 100644 index 00000000..5e77a439 --- /dev/null +++ b/kernel/linux/efa_nv_peermem/src/Kbuild.in @@ -0,0 +1,2 @@ +obj-m := efa_nv_peermem.o +efa_nv_peermem-y := $(patsubst %.c,%.o, $(filter %.c, @efa_nv_peermem_sources_string@)) diff --git a/kernel/linux/efa_nv_peermem/src/efa_nv_peermem_main.c b/kernel/linux/efa_nv_peermem/src/efa_nv_peermem_main.c new file mode 100644 index 00000000..9cdf9ef8 --- /dev/null +++ b/kernel/linux/efa_nv_peermem/src/efa_nv_peermem_main.c @@ -0,0 +1,87 @@ +// SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause +/* + * Copyright 2023 Amazon.com, Inc. or its affiliates. All rights reserved. + */ + +#include +#include +#include +#include "nv-p2p.h" + +#define DRV_MODULE_NAME "EFA NV Peermem" +#define DRV_MODULE_VER_MAJOR 1 +#define DRV_MODULE_VER_MINOR 1 +#define DRV_MODULE_VER_SUBMINOR 0 + +#ifndef DRV_MODULE_VERSION +#define DRV_MODULE_VERSION \ + __stringify(DRV_MODULE_VER_MAJOR) "." \ + __stringify(DRV_MODULE_VER_MINOR) "." \ + __stringify(DRV_MODULE_VER_SUBMINOR) +#endif + +MODULE_AUTHOR("Amazon.com, Inc. or its affiliates"); +MODULE_LICENSE("Dual BSD/GPL"); +MODULE_DESCRIPTION(DRV_MODULE_NAME); +MODULE_VERSION(DRV_MODULE_VERSION); +MODULE_SOFTDEP("pre: nvidia"); + +static char module_string[] = DRV_MODULE_NAME " v" DRV_MODULE_VERSION; + +int efa_nv_peermem_p2p_get_pages(uint64_t p2p_token, uint32_t va_space, + uint64_t virtual_address, uint64_t length, + struct nvidia_p2p_page_table **page_table, + void (*free_callback)(void *data), void *data) +{ + return nvidia_p2p_get_pages(p2p_token, va_space, virtual_address, length, + page_table, free_callback, data); +} +EXPORT_SYMBOL_GPL(efa_nv_peermem_p2p_get_pages); + +int efa_nv_peermem_p2p_dma_map_pages(struct pci_dev *peer, + struct nvidia_p2p_page_table *page_table, + struct nvidia_p2p_dma_mapping **dma_mapping) +{ + return nvidia_p2p_dma_map_pages(peer, page_table, dma_mapping); +} +EXPORT_SYMBOL_GPL(efa_nv_peermem_p2p_dma_map_pages); + +int efa_nv_peermem_p2p_dma_unmap_pages(struct pci_dev *peer, + struct nvidia_p2p_page_table *page_table, + struct nvidia_p2p_dma_mapping *dma_mapping) +{ + return nvidia_p2p_dma_unmap_pages(peer, page_table, dma_mapping); +} +EXPORT_SYMBOL_GPL(efa_nv_peermem_p2p_dma_unmap_pages); + +int efa_nv_peermem_p2p_put_pages(uint64_t p2p_token, + uint32_t va_space, uint64_t virtual_address, + struct nvidia_p2p_page_table *page_table) +{ + return nvidia_p2p_put_pages(p2p_token, va_space, virtual_address, page_table); +} +EXPORT_SYMBOL_GPL(efa_nv_peermem_p2p_put_pages); + +int init_module(void) +{ + /* Make sure that we didn't inherit taint from Nvidia proprietary driver*/ + if (test_bit(TAINT_PROPRIETARY_MODULE, &THIS_MODULE->taints)) { + pr_info("efa_nv_peermem: Nvidia proprietary is unsupported\n"); + return -EINVAL; + } + + if (!nvidia_p2p_get_pages || + !nvidia_p2p_dma_map_pages || + !nvidia_p2p_dma_unmap_pages || + !nvidia_p2p_put_pages) { + pr_info("efa_nv_peermem: Nvidia P2P symbols are unavailable\n"); + return -ENOSYS; + } + + pr_info("%s\n", module_string); + return 0; +} + +void cleanup_module(void) +{ +} diff --git a/kernel/linux/efa_nv_peermem/src/nv-p2p.h b/kernel/linux/efa_nv_peermem/src/nv-p2p.h new file mode 100644 index 00000000..20d252e9 --- /dev/null +++ b/kernel/linux/efa_nv_peermem/src/nv-p2p.h @@ -0,0 +1,478 @@ +/* + * SPDX-FileCopyrightText: Copyright (c) 2011-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved. + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#ifndef _NV_P2P_H_ +#define _NV_P2P_H_ + +/* + * NVIDIA P2P Structure Versioning + * + * For the nvidia_p2p_*_t structures allocated by the NVIDIA driver, it will + * set the version field of the structure according to the definition used by + * the NVIDIA driver. The "major" field of the version is defined as the upper + * 16 bits, and the "minor" field of the version is defined as the lower 16 + * bits. The version field will always be the first 4 bytes of the structure, + * and third-party drivers should check the value of this field in structures + * allocated by the NVIDIA driver to ensure runtime compatibility. + * + * In general, version numbers will be incremented as follows: + * - When a backwards-compatible change is made to the structure layout, the + * minor version for that structure will be incremented. Third-party drivers + * built against an older minor version will continue to work with the newer + * minor version used by the NVIDIA driver, without recompilation. + * - When a breaking change is made to the structure layout, the major version + * will be incremented. Third-party drivers built against an older major + * version require at least recompilation and potentially additional updates + * to use the new API. + */ +#define NVIDIA_P2P_MAJOR_VERSION_MASK 0xffff0000 +#define NVIDIA_P2P_MINOR_VERSION_MASK 0x0000ffff + +#define NVIDIA_P2P_MAJOR_VERSION(v) \ + (((v) & NVIDIA_P2P_MAJOR_VERSION_MASK) >> 16) + +#define NVIDIA_P2P_MINOR_VERSION(v) \ + (((v) & NVIDIA_P2P_MINOR_VERSION_MASK)) + +#define NVIDIA_P2P_MAJOR_VERSION_MATCHES(p, v) \ + (NVIDIA_P2P_MAJOR_VERSION((p)->version) == NVIDIA_P2P_MAJOR_VERSION(v)) + +#define NVIDIA_P2P_VERSION_COMPATIBLE(p, v) \ + (NVIDIA_P2P_MAJOR_VERSION_MATCHES(p, v) && \ + (NVIDIA_P2P_MINOR_VERSION((p)->version) >= (NVIDIA_P2P_MINOR_VERSION(v)))) + +enum { + NVIDIA_P2P_ARCHITECTURE_TESLA = 0, + NVIDIA_P2P_ARCHITECTURE_FERMI, + NVIDIA_P2P_ARCHITECTURE_CURRENT = NVIDIA_P2P_ARCHITECTURE_FERMI +}; + +#define NVIDIA_P2P_PARAMS_VERSION 0x00010001 + +enum { + NVIDIA_P2P_PARAMS_ADDRESS_INDEX_GPU = 0, + NVIDIA_P2P_PARAMS_ADDRESS_INDEX_THIRD_PARTY_DEVICE, + NVIDIA_P2P_PARAMS_ADDRESS_INDEX_MAX = \ + NVIDIA_P2P_PARAMS_ADDRESS_INDEX_THIRD_PARTY_DEVICE +}; + +#define NVIDIA_P2P_GPU_UUID_LEN 16 + +typedef +struct nvidia_p2p_params { + uint32_t version; + uint32_t architecture; + union nvidia_p2p_mailbox_addresses { + struct { + uint64_t wmb_addr; + uint64_t wmb_data; + uint64_t rreq_addr; + uint64_t rcomp_addr; + uint64_t reserved[2]; + } fermi; + } addresses[NVIDIA_P2P_PARAMS_ADDRESS_INDEX_MAX+1]; +} nvidia_p2p_params_t; + +/* + * Macro for users to detect + * driver support for persistent pages. + */ +#define NVIDIA_P2P_CAP_GET_PAGES_PERSISTENT_API + +/* + * This API is not supported. + */ +int nvidia_p2p_init_mapping(uint64_t p2p_token, + struct nvidia_p2p_params *params, + void (*destroy_callback)(void *data), + void *data); + +/* + * This API is not supported. + */ +int nvidia_p2p_destroy_mapping(uint64_t p2p_token); + +enum nvidia_p2p_page_size_type { + NVIDIA_P2P_PAGE_SIZE_4KB = 0, + NVIDIA_P2P_PAGE_SIZE_64KB, + NVIDIA_P2P_PAGE_SIZE_128KB, + NVIDIA_P2P_PAGE_SIZE_COUNT +}; + +typedef +struct nvidia_p2p_page { + uint64_t physical_address; + union nvidia_p2p_request_registers { + struct { + uint32_t wreqmb_h; + uint32_t rreqmb_h; + uint32_t rreqmb_0; + uint32_t reserved[3]; + } fermi; + } registers; +} nvidia_p2p_page_t; + +#define NVIDIA_P2P_PAGE_TABLE_VERSION 0x00010002 + +#define NVIDIA_P2P_PAGE_TABLE_VERSION_COMPATIBLE(p) \ + NVIDIA_P2P_VERSION_COMPATIBLE(p, NVIDIA_P2P_PAGE_TABLE_VERSION) + +typedef +struct nvidia_p2p_page_table { + uint32_t version; + uint32_t page_size; /* enum nvidia_p2p_page_size_type */ + struct nvidia_p2p_page **pages; + uint32_t entries; + uint8_t *gpu_uuid; +} nvidia_p2p_page_table_t; + +/* + * @brief + * Make the pages underlying a range of GPU virtual memory + * accessible to a third-party device. + * + * This API only supports pinned, GPU-resident memory, such as that provided + * by cudaMalloc(). + * + * This API may sleep. + * + * @param[in] p2p_token + * A token that uniquely identifies the P2P mapping. + * @param[in] va_space + * A GPU virtual address space qualifier. + * @param[in] virtual_address + * The start address in the specified virtual address space. + * Address must be aligned to the 64KB boundary. + * @param[in] length + * The length of the requested P2P mapping. + * Length must be a multiple of 64KB. + * @param[out] page_table + * A pointer to an array of structures with P2P PTEs. + * @param[in] free_callback + * A pointer to the function to be invoked when the pages + * underlying the virtual address range are freed + * implicitly. + * @param[in] data + * A non-NULL opaque pointer to private data to be passed to the + * callback function. + * + * @return + * 0 upon successful completion. + * -EINVAL if an invalid argument was supplied. + * -ENOTSUPP if the requested operation is not supported. + * -ENOMEM if the driver failed to allocate memory or if + * insufficient resources were available to complete the operation. + * -EIO if an unknown error occurred. + */ +int __weak nvidia_p2p_get_pages( uint64_t p2p_token, uint32_t va_space, + uint64_t virtual_address, uint64_t length, + struct nvidia_p2p_page_table **page_table, + void (*free_callback)(void *data), void *data); + +/* + * @brief + * Pin and make the pages underlying a range of GPU virtual memory + * accessible to a third-party device. The pages will persist until + * explicitly freed by nvidia_p2p_put_pages_persistent(). + * + * Persistent GPU memory mappings are not supported on PowerPC, + * MIG-enabled devices and vGPU. + * + * This API only supports pinned, GPU-resident memory, such as that provided + * by cudaMalloc(). + * + * This API may sleep. + * + * @param[in] virtual_address + * The start address in the specified virtual address space. + * Address must be aligned to the 64KB boundary. + * @param[in] length + * The length of the requested P2P mapping. + * Length must be a multiple of 64KB. + * @param[out] page_table + * A pointer to an array of structures with P2P PTEs. + * @param[in] flags + * Must be set to zero for now. + * + * @return + * 0 upon successful completion. + * -EINVAL if an invalid argument was supplied. + * -ENOTSUPP if the requested operation is not supported. + * -ENOMEM if the driver failed to allocate memory or if + * insufficient resources were available to complete the operation. + * -EIO if an unknown error occurred. + */ +int __weak nvidia_p2p_get_pages_persistent(uint64_t virtual_address, + uint64_t length, + struct nvidia_p2p_page_table **page_table, + uint32_t flags); + +#define NVIDIA_P2P_DMA_MAPPING_VERSION 0x00020003 + +#define NVIDIA_P2P_DMA_MAPPING_VERSION_COMPATIBLE(p) \ + NVIDIA_P2P_VERSION_COMPATIBLE(p, NVIDIA_P2P_DMA_MAPPING_VERSION) + +struct pci_dev; + +typedef +struct nvidia_p2p_dma_mapping { + uint32_t version; + enum nvidia_p2p_page_size_type page_size_type; + uint32_t entries; + uint64_t *dma_addresses; + void *private; + struct pci_dev *pci_dev; +} nvidia_p2p_dma_mapping_t; + +/* + * @brief + * Make the physical pages retrieved using nvidia_p2p_get_pages accessible to + * a third-party device. + * + * @param[in] peer + * The struct pci_dev * of the peer device that needs to DMA to/from the + * mapping. + * @param[in] page_table + * The page table outlining the physical pages underlying the mapping, as + * retrieved with nvidia_p2p_get_pages(). + * @param[out] dma_mapping + * The DMA mapping containing the DMA addresses to use on the third-party + * device. + * + * @return + * 0 upon successful completion. + * -EINVAL if an invalid argument was supplied. + * -ENOTSUPP if the requested operation is not supported. + * -EIO if an unknown error occurred. + */ +int __weak nvidia_p2p_dma_map_pages(struct pci_dev *peer, + struct nvidia_p2p_page_table *page_table, + struct nvidia_p2p_dma_mapping **dma_mapping); + +/* + * @brief + * Unmap the physical pages previously mapped to the third-party device by + * nvidia_p2p_dma_map_pages(). + * + * @param[in] peer + * The struct pci_dev * of the peer device that the DMA mapping belongs to. + * @param[in] page_table + * The page table backing the DMA mapping to be unmapped. + * @param[in] dma_mapping + * The DMA mapping containing the DMA addresses used by the third-party + * device, as retrieved with nvidia_p2p_dma_map_pages(). After this call + * returns, neither this struct nor the addresses contained within will be + * valid for use by the third-party device. + * + * @return + * 0 upon successful completion. + * -EINVAL if an invalid argument was supplied. + * -EIO if an unknown error occurred. + */ +int __weak nvidia_p2p_dma_unmap_pages(struct pci_dev *peer, + struct nvidia_p2p_page_table *page_table, + struct nvidia_p2p_dma_mapping *dma_mapping); + +/* + * @brief + * Release a set of pages previously made accessible to + * a third-party device. + * + * This API may sleep. + * + * @param[in] p2p_token + * A token that uniquely identifies the P2P mapping. + * @param[in] va_space + * A GPU virtual address space qualifier. + * @param[in] virtual_address + * The start address in the specified virtual address space. + * @param[in] page_table + * A pointer to the array of structures with P2P PTEs. + * + * @return + * 0 upon successful completion. + * -EINVAL if an invalid argument was supplied. + * -EIO if an unknown error occurred. + */ +int __weak nvidia_p2p_put_pages(uint64_t p2p_token, + uint32_t va_space, uint64_t virtual_address, + struct nvidia_p2p_page_table *page_table); + +/* + * @brief + * Release a set of persistent pages previously made accessible to + * a third-party device. + * + * This API may sleep. + * + * @param[in] virtual_address + * The start address in the specified virtual address space. + * @param[in] page_table + * A pointer to the array of structures with P2P PTEs. + * @param[in] flags + * Must be set to zero for now. + * + * @return + * 0 upon successful completion. + * -EINVAL if an invalid argument was supplied. + * -EIO if an unknown error occurred. + */ +int __weak nvidia_p2p_put_pages_persistent(uint64_t virtual_address, + struct nvidia_p2p_page_table *page_table, + uint32_t flags); + +/* + * @brief + * Free a third-party P2P page table. (This function is a no-op.) + * + * @param[in] page_table + * A pointer to the array of structures with P2P PTEs. + * + * @return + * 0 upon successful completion. + * -EINVAL if an invalid argument was supplied. + */ +int nvidia_p2p_free_page_table(struct nvidia_p2p_page_table *page_table); + +/* + * @brief + * Free a third-party P2P DMA mapping. (This function is a no-op.) + * + * @param[in] dma_mapping + * A pointer to the DMA mapping structure. + * + * @return + * 0 upon successful completion. + * -EINVAL if an invalid argument was supplied. + */ +int nvidia_p2p_free_dma_mapping(struct nvidia_p2p_dma_mapping *dma_mapping); + +#define NVIDIA_P2P_RSYNC_DRIVER_VERSION 0x00010001 + +#define NVIDIA_P2P_RSYNC_DRIVER_VERSION_COMPATIBLE(p) \ + NVIDIA_P2P_VERSION_COMPATIBLE(p, NVIDIA_P2P_RSYNC_DRIVER_VERSION) + +typedef +struct nvidia_p2p_rsync_driver { + uint32_t version; + int (*get_relaxed_ordering_mode)(int *mode, void *data); + void (*put_relaxed_ordering_mode)(int mode, void *data); + void (*wait_for_rsync)(struct pci_dev *gpu, void *data); +} nvidia_p2p_rsync_driver_t; + +/* + * @brief + * Registers the rsync driver. + * + * @param[in] driver + * A pointer to the rsync driver structure. The NVIDIA driver would use, + * + * get_relaxed_ordering_mode to obtain a reference to the current relaxed + * ordering mode (treated as a boolean) from the rsync driver. + * + * put_relaxed_ordering_mode to release a reference to the current relaxed + * ordering mode back to the rsync driver. The NVIDIA driver will call this + * function once for each successful call to get_relaxed_ordering_mode, and + * the relaxed ordering mode must not change until the last reference is + * released. + * + * wait_for_rsync to call into the rsync module to issue RSYNC. This callback + * can't sleep or re-schedule as it may arrive under spinlocks. + * @param[in] data + * A pointer to the rsync driver's private data. + * + * @Returns + * 0 upon successful completion. + * -EINVAL parameters are incorrect. + * -EBUSY if a module is already registered or GPU devices are in use. + */ +int nvidia_p2p_register_rsync_driver(nvidia_p2p_rsync_driver_t *driver, + void *data); + +/* + * @brief + * Unregisters the rsync driver. + * + * @param[in] driver + * A pointer to the rsync driver structure. + * @param[in] data + * A pointer to the rsync driver's private data. + */ +void nvidia_p2p_unregister_rsync_driver(nvidia_p2p_rsync_driver_t *driver, + void *data); + +#define NVIDIA_P2P_RSYNC_REG_INFO_VERSION 0x00020001 + +#define NVIDIA_P2P_RSYNC_REG_INFO_VERSION_COMPATIBLE(p) \ + NVIDIA_P2P_VERSION_COMPATIBLE(p, NVIDIA_P2P_RSYNC_REG_INFO_VERSION) + +typedef struct nvidia_p2p_rsync_reg { + void *ptr; + size_t size; + struct pci_dev *ibmnpu; + struct pci_dev *gpu; + uint32_t cluster_id; + uint32_t socket_id; +} nvidia_p2p_rsync_reg_t; + +typedef struct nvidia_p2p_rsync_reg_info { + uint32_t version; + nvidia_p2p_rsync_reg_t *regs; + size_t entries; +} nvidia_p2p_rsync_reg_info_t; + +/* + * @brief + * Gets rsync (GEN-ID) register information associated with the supported + * NPUs. + * + * The caller would use the returned information {GPU device, NPU device, + * socket-id, cluster-id} to pick the optimal generation registers to issue + * RSYNC (NVLink HW flush). + * + * The interface allocates structures to return the information, hence + * nvidia_p2p_put_rsync_registers() must be called to free the structures. + * + * Note, cluster-id is hardcoded to zero as early system configurations would + * only support cluster mode i.e. all devices would share the same cluster-id + * (0). In the future, appropriate kernel support would be needed to query + * cluster-ids. + * + * @param[out] reg_info + * A pointer to the rsync reg info structure. + * + * @Returns + * 0 Upon successful completion. Otherwise, returns negative value. + */ +int nvidia_p2p_get_rsync_registers(nvidia_p2p_rsync_reg_info_t **reg_info); + +/* + * @brief + * Frees the structures allocated by nvidia_p2p_get_rsync_registers(). + * + * @param[in] reg_info + * A pointer to the rsync reg info structure. + */ +void nvidia_p2p_put_rsync_registers(nvidia_p2p_rsync_reg_info_t *reg_info); + +#endif /* _NV_P2P_H_ */