misc: nnpi: New PCIe driver for Intel's NNP-I pcie device
Subsystems (2)
v2 cover misc: nnpi: New PCIe driver for Intel's NNP-I pcie device
Hi,
The following series is a driver for a new PCIe device from Intel named NNP-I (Nirvana Neural Processor for Inference). NNP-I is a PCIe connected compute device used for acceleration of AI deep learning inference applications in the data-center.
The reason that this driver should be in the kernel is that it aims to serve multiple users and user-space applications which might share the same NNP-I card. Workloads from multiple applications can be processed simultanously by the NNP-I card if enough compute resources exist.
Overview of the NNP-I device, driver structure and ABIs used in the driver is in patch#1, which adds the info as a document as it might be a useful info for anyone trying to understand the driver even past review.
In order to ease the review process, there will be multiple series for the entire driver code. This is the first series, and it implements everything necessary to initialize the NNP-I device and allow a user-space inference application to use it. Other features, which are mostly related to maintenance, device status visibility and error-handling, will be submitted on the next stage.
A basic user-space library and test application which illustrates the flow of an NNP-I inference application can be found here: https://github.com/IntelAI/nnpi-host
(This series is enough for the test application to run)
This patchset has gone through internal review inside Intel, the summary of the change log from the internal review follows.
I would appreciate any feedback, questions or comments to this series.
Changes in v2:
- Removed email disclaimer added to the end of each patch.
- Small fix to Kconfig requested by Randy
- Removed from this cover letter the long Intel internal pre-review change log of this patchset.
Link to v1 cover letter: https://lwn.net/Articles/856037/
Guy Zadicario (15):
misc: nnpi: Document NNP-I's driver overview
misc: nnpi: Initialize NNP-I framework and PCIe modules
misc: nnpi: Manage and schedule messages to device
misc: nnpi: Define host/card ipc protocol
misc: nnpi: Manage host memory resources
misc: nnpi: Allow usermode to manage host resources
misc: nnpi: Disallow host memory resource access if no NNP-I devices
exist
misc: nnpi: Boot NNP-I device
misc: nnpi: Process device response messages
misc: nnpi: Query and verify device protocol
misc: nnpi: Create comm channel from app to device
misc: nnpi: Route device response messages
misc: nnpi: Expose command channel file interface
misc: nnpi: Create command channel from userspace
misc: nnpi: Map host resources to device channel
Documentation/ABI/testing/sysfs-driver-intel_nnpi | 5 + Documentation/misc-devices/index.rst | 1 + Documentation/misc-devices/intel-nnpi.rst | 237 +++++ MAINTAINERS | 6 + drivers/misc/Kconfig | 1 + drivers/misc/Makefile | 1 + drivers/misc/intel-nnpi/Kconfig | 18 + drivers/misc/intel-nnpi/Makefile | 13 + drivers/misc/intel-nnpi/bootimage.c | 246 +++++ drivers/misc/intel-nnpi/bootimage.h | 43 + drivers/misc/intel-nnpi/cmd_chan.c | 790 ++++++++++++++ drivers/misc/intel-nnpi/cmd_chan.h | 134 +++ drivers/misc/intel-nnpi/device.c | 1081 ++++++++++++++++++++ drivers/misc/intel-nnpi/device.h | 182 ++++ drivers/misc/intel-nnpi/device_chardev.c | 789 ++++++++++++++ drivers/misc/intel-nnpi/device_chardev.h | 14 + drivers/misc/intel-nnpi/host_chardev.c | 353 +++++++ drivers/misc/intel-nnpi/host_chardev.h | 12 + drivers/misc/intel-nnpi/hostres.c | 627 ++++++++++++ drivers/misc/intel-nnpi/hostres.h | 167 +++ .../misc/intel-nnpi/ipc_include/ipc_c2h_events.h | 198 ++++ drivers/misc/intel-nnpi/ipc_include/ipc_protocol.h | 340 ++++++ .../misc/intel-nnpi/ipc_include/nnp_boot_defs.h | 71 ++ drivers/misc/intel-nnpi/ipc_include/nnp_elbi.h | 91 ++ drivers/misc/intel-nnpi/msg_scheduler.c | 319 ++++++ drivers/misc/intel-nnpi/msg_scheduler.h | 153 +++ drivers/misc/intel-nnpi/nnp_pcie.c | 530 ++++++++++ drivers/misc/intel-nnpi/nnp_user.c | 131 +++ drivers/misc/intel-nnpi/nnp_user.h | 79 ++ include/uapi/misc/intel_nnpi.h | 304 ++++++ 30 files changed, 6936 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-driver-intel_nnpi create mode 100644 Documentation/misc-devices/intel-nnpi.rst create mode 100644 drivers/misc/intel-nnpi/Kconfig create mode 100644 drivers/misc/intel-nnpi/Makefile create mode 100644 drivers/misc/intel-nnpi/bootimage.c create mode 100644 drivers/misc/intel-nnpi/bootimage.h create mode 100644 drivers/misc/intel-nnpi/cmd_chan.c create mode 100644 drivers/misc/intel-nnpi/cmd_chan.h create mode 100644 drivers/misc/intel-nnpi/device.c create mode 100644 drivers/misc/intel-nnpi/device.h create mode 100644 drivers/misc/intel-nnpi/device_chardev.c create mode 100644 drivers/misc/intel-nnpi/device_chardev.h create mode 100644 drivers/misc/intel-nnpi/host_chardev.c create mode 100644 drivers/misc/intel-nnpi/host_chardev.h create mode 100644 drivers/misc/intel-nnpi/hostres.c create mode 100644 drivers/misc/intel-nnpi/hostres.h create mode 100644 drivers/misc/intel-nnpi/ipc_include/ipc_c2h_events.h create mode 100644 drivers/misc/intel-nnpi/ipc_include/ipc_protocol.h create mode 100644 drivers/misc/intel-nnpi/ipc_include/nnp_boot_defs.h create mode 100644 drivers/misc/intel-nnpi/ipc_include/nnp_elbi.h create mode 100644 drivers/misc/intel-nnpi/msg_scheduler.c create mode 100644 drivers/misc/intel-nnpi/msg_scheduler.h create mode 100644 drivers/misc/intel-nnpi/nnp_pcie.c create mode 100644 drivers/misc/intel-nnpi/nnp_user.c create mode 100644 drivers/misc/intel-nnpi/nnp_user.h create mode 100644 include/uapi/misc/intel_nnpi.h
Signature · 1 line
1.8.3.1
PATCH v2 1/15 misc: nnpi: Document NNP-I's driver overview
Introduce overview documentation for NNP-I card and driver to let new readers of the driver understand better the driver and the NNP-I device.
--- Documentation/misc-devices/index.rst | 1 + Documentation/misc-devices/intel-nnpi.rst | 237 ++++++++++++++++++++++++++++++ 2 files changed, 238 insertions(+) create mode 100644 Documentation/misc-devices/intel-nnpi.rst
Documentation/misc-devices/index.rst +1 -0
@@ -22,6 +22,7 @@ fit into other categories. dw-xdata-pcie ibmvmc ics932s401 + intel-nnpi isl29003 lis3lv02d max6875
Documentation/misc-devices/intel-nnpi.rst new file +237 -0
237 lines not on this page · raw message
Signature · 1 line
1.8.3.1
PATCH v2 2/15 misc: nnpi: Initialize NNP-I framework and PCIe modules
In reply to Guy ZadicarioInitialize the NNP-I framework module and expose PCIe device driver.
The NNP-I driver is divided into two modules, the framework module and the PCIe driver module.
The PCIe driver probes the device and provides, to the framework module, functions for sending and receiving raw commands and responses to/from the device. It does not have semantic understanding of these commands and responses, and transfers them as abstract binary data.
The framework module unpacks command and response messages, and uses the PCIe driver module to send and receive them. It uses the PCIe driver module as an abstract communication layer, and is unaware of the PCIe HW used to communicate with the NNP-I card.
Using TCP/IP jargon, the PCIe driver module is the "transport" layer and the main framework module is the "application" layer. This logical seperation was done in order to ease maintenance and validation, and make adding support for other "transport" layers easier.
The header files under the ipc_include directory provide HW register definitions and protocol structures used to communicate with the NNP-I device. These header files are used also by the NNP-I card's SW stack.
--- MAINTAINERS | 5 + drivers/misc/Kconfig | 1 + drivers/misc/Makefile | 1 + drivers/misc/intel-nnpi/Kconfig | 18 ++ drivers/misc/intel-nnpi/Makefile | 12 + drivers/misc/intel-nnpi/device.c | 84 +++++ drivers/misc/intel-nnpi/device.h | 39 +++ .../misc/intel-nnpi/ipc_include/nnp_boot_defs.h | 71 ++++ drivers/misc/intel-nnpi/ipc_include/nnp_elbi.h | 91 ++++++ drivers/misc/intel-nnpi/nnp_pcie.c | 358 +++++++++++++++++++++ 10 files changed, 680 insertions(+) create mode 100644 drivers/misc/intel-nnpi/Kconfig create mode 100644 drivers/misc/intel-nnpi/Makefile create mode 100644 drivers/misc/intel-nnpi/device.c create mode 100644 drivers/misc/intel-nnpi/device.h create mode 100644 drivers/misc/intel-nnpi/ipc_include/nnp_boot_defs.h create mode 100644 drivers/misc/intel-nnpi/ipc_include/nnp_elbi.h create mode 100644 drivers/misc/intel-nnpi/nnp_pcie.c
MAINTAINERS +5 -0
@@ -9316,6 +9316,11 @@ S: Supported W: https://01.org/linux-acpi F: drivers/platform/x86/intel_menlow.c +INTEL NNP-I PCI DRIVER +M: Guy Zadicario <[email protected]> +S: Supported +F: drivers/misc/intel-nnpi/ + INTEL P-Unit IPC DRIVER M: Zha Qipeng <[email protected]> L: [email protected]
drivers/misc/Kconfig +1 -0
@@ -462,6 +462,7 @@ source "drivers/misc/ocxl/Kconfig" source "drivers/misc/bcm-vk/Kconfig" source "drivers/misc/cardreader/Kconfig" source "drivers/misc/habanalabs/Kconfig" +source "drivers/misc/intel-nnpi/Kconfig" source "drivers/misc/uacce/Kconfig" source "drivers/misc/pvpanic/Kconfig" endmenu
drivers/misc/Makefile +1 -0
@@ -54,6 +54,7 @@ obj-$(CONFIG_BCM_VK) += bcm-vk/ obj-y += cardreader/ obj-$(CONFIG_PVPANIC) += pvpanic/ obj-$(CONFIG_HABANA_AI) += habanalabs/ +obj-$(CONFIG_INTEL_NNPI) += intel-nnpi/ obj-$(CONFIG_UACCE) += uacce/ obj-$(CONFIG_XILINX_SDFEC) += xilinx_sdfec.o obj-$(CONFIG_HISI_HIKEY_USB) += hisi_hikey_usb.o
drivers/misc/intel-nnpi/Kconfig new file +18 -0
@@ -0,0 +1,18 @@ +# SPDX-License-Identifier: GPL-2.0-only + +# +# Copyright (C) 2019-2021 Intel Corporation +# +# + +config INTEL_NNPI + tristate "Intel(R) PCIe NNP-I (AI accelerator for inference) device driver" + depends on PCI + select DMA_SHARED_BUFFER + help + Device driver for Intel NNP-I PCIe accelerator card for AI inference. + + If unsure, say N. + + To compile this driver as a module, choose M here. Two modules will + be generated: intel_nnpi and intel_nnpi_pcie.
drivers/misc/intel-nnpi/Makefile new file +12 -0
@@ -0,0 +1,12 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# Copyright (C) 2019-2021 Intel Corporation +# + +obj-$(CONFIG_INTEL_NNPI) := intel_nnpi.o intel_nnpi_pcie.o + +intel_nnpi-y := device.o + +intel_nnpi_pcie-y := nnp_pcie.o + +ccflags-y += -I$(srctree)/$(src)/ipc_include
drivers/misc/intel-nnpi/device.c new file +84 -0
84 lines not on this page · raw message
drivers/misc/intel-nnpi/device.h new file +39 -0
@@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* Copyright (C) 2019-2021 Intel Corporation */ + +#ifndef _NNPDRV_DEVICE_H +#define _NNPDRV_DEVICE_H + +#define NNP_MAX_DEVS 256 + +/** +* struct nnp_device - structure for NNP-I device info +* @ops: device operations implemented by the underlying device driver +* @dev: pointer to struct device representing the NNP-I card. +* @id: NNP-I device number +*/ +struct nnp_device { +const struct nnp_device_ops *ops; +struct device *dev; +int id; +}; + +/** +* struct nnp_device_ops - operations implemented by underlying device driver +* @cmdq_flush: empties the device command queue, discarding all queued +* commands. +*/ +struct nnp_device_ops { +int (*cmdq_flush)(struct nnp_device *hw_dev); +}; + +/* +* Functions exported by the device framework module which are +* called by the lower layer NNP-I device driver module +*/ +int nnpdev_init (struct nnp_device *nnpdev,struct device *dev, +const struct nnp_device_ops *ops); +void nnpdev_destroy (struct nnp_device *nnpdev); +void nnpdev_card_doorbell_value_changed (struct nnp_device *nnpdev, +u32 doorbell_val); +#endif
drivers/misc/intel-nnpi/ipc_include/nnp_boot_defs.h new file +71 -0
71 lines not on this page · raw message
drivers/misc/intel-nnpi/ipc_include/nnp_elbi.h new file +91 -0
91 lines not on this page · raw message
drivers/misc/intel-nnpi/nnp_pcie.c new file +358 -0
358 lines not on this page · raw message
Signature · 1 line
1.8.3.1
PATCH v2 3/15 misc: nnpi: Manage and schedule messages to device
In reply to Guy ZadicarioAllocate a msg_scheduler object for each NNP-I device. Each object manages multiple command queues - one for driver initiated commands (called "cmdq"), and the rest for commands coming from user-space. A kernel thread in the msg_scheduler schedules sending the commands from these queues to the NNP-I device by using the cmdq_write_mesg function of the NNP-I device driver ops.
The msg_scheduler object allows multiple user-mode applications to put messages into the queue without blocking or waiting on a lock. It's created on device creation and destroyed on device removal.
--- drivers/misc/intel-nnpi/Makefile | 2 +- drivers/misc/intel-nnpi/device.c | 20 ++ drivers/misc/intel-nnpi/device.h | 9 + drivers/misc/intel-nnpi/msg_scheduler.c | 319 ++++++++++++++++++++++++++++++++ drivers/misc/intel-nnpi/msg_scheduler.h | 153 +++++++++++++++ drivers/misc/intel-nnpi/nnp_pcie.c | 153 +++++++++++++++ 6 files changed, 655 insertions(+), 1 deletion(-) create mode 100644 drivers/misc/intel-nnpi/msg_scheduler.c create mode 100644 drivers/misc/intel-nnpi/msg_scheduler.h
drivers/misc/intel-nnpi/Makefile +1 -1
@@ -5,7 +5,7 @@ obj-$(CONFIG_INTEL_NNPI) := intel_nnpi.o intel_nnpi_pcie.o -intel_nnpi-y := device.o +intel_nnpi-y := device.o msg_scheduler.o intel_nnpi_pcie-y := nnp_pcie.o
drivers/misc/intel-nnpi/device.c +20 -0
@@ -8,6 +8,7 @@#include <linux/module.h> #include "device.h" +#include "msg_scheduler.h" static DEFINE_IDA (dev_ida);
@@ -45,7 +46,25 @@ int nnpdev_init(struct nnp_device *nnpdev, struct device *dev, nnpdev->dev = dev; nnpdev->ops = ops; + nnpdev->cmdq_sched =nnp_msched_create (nnpdev); +if (!nnpdev->cmdq_sched) { + ret = -ENOMEM ; + goto err_ida; + } + + nnpdev->cmdq =nnp_msched_queue_create (nnpdev->cmdq_sched); +if (!nnpdev->cmdq) { + ret = -ENOMEM ; + goto err_msg_sched; + } +return 0 ; + +err_msg_sched: +nnp_msched_destroy (nnpdev->cmdq_sched); +err_ida: +ida_simple_remove (&dev_ida, nnpdev->id); +return ret; }EXPORT_SYMBOL (nnpdev_init);
@@ -75,6 +94,7 @@ void nnpdev_destroy(struct nnp_device *nnpdev) {dev_dbg (nnpdev->dev,"Destroying NNP-I device\n" ); +nnp_msched_destroy (nnpdev->cmdq_sched);ida_simple_remove (&dev_ida, nnpdev->id); }EXPORT_SYMBOL (nnpdev_destroy);
drivers/misc/intel-nnpi/device.h +9 -0
@@ -11,20 +11,29 @@* @ops: device operations implemented by the underlying device driver * @dev: pointer to struct device representing the NNP-I card. * @id: NNP-I device number +* @cmdq_sched: message scheduler thread which schedules and serializes command +* submissions to the device's command queue. +* @cmdq: input queue to @cmdq_sched used to schedule driver internal commands +* to be sent to the device. */ struct nnp_device {const struct nnp_device_ops *ops;struct device *dev;int id; + +struct nnp_msched *cmdq_sched; +struct nnp_msched_queue *cmdq; };/** * struct nnp_device_ops - operations implemented by underlying device driver * @cmdq_flush: empties the device command queue, discarding all queued * commands. +* @cmdq_write_mesg: inserts a command message to the card's command queue. */ struct nnp_device_ops {int (*cmdq_flush)(struct nnp_device *hw_dev); +int (*cmdq_write_mesg)(struct nnp_device *nnpdev,u64 *msg,u32 size); };/*
drivers/misc/intel-nnpi/msg_scheduler.c new file +319 -0
319 lines not on this page · raw message
drivers/misc/intel-nnpi/msg_scheduler.h new file +153 -0
153 lines not on this page · raw message
drivers/misc/intel-nnpi/nnp_pcie.c +153 -0
195 lines not on this page · raw message
Signature · 1 line
1.8.3.1
PATCH v2 4/15 misc: nnpi: Define host/card ipc protocol
In reply to Guy ZadicarioDefine the commands and memory block structures which can be sent to the NNP-I device from the "device" layer.
--- drivers/misc/intel-nnpi/ipc_include/ipc_protocol.h | 337 +++++++++++++++++++++ 1 file changed, 337 insertions(+) create mode 100644 drivers/misc/intel-nnpi/ipc_include/ipc_protocol.h
drivers/misc/intel-nnpi/ipc_include/ipc_protocol.h new file +337 -0
@@ -0,0 +1,337 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* Copyright (C) 2019-2021 Intel Corporation */ + +#ifndef _IPC_PROTOCOL_H +#define _IPC_PROTOCOL_H + +#include <linux/bits.h> +#include <linux/bitfield.h> +#include <linux/dma-mapping.h> +#include <linux/types.h> + +#define IPC_OP_MAX BIT(6) +#define NNP_IPC_OPCODE_MASK GENMASK(5, 0) + +#define NNP_MSG_SIZE (msg) (sizeof(msg) / sizeof(__le64)) + +/* +* NNP_PAGE_SIZE is a size of a page in the protocol. +* We do not use just PAGE_SIZE since it may differ between +* card and host. +*/ +#define NNP_PAGE_SHIFT 12 +#define NNP_PAGE_SIZE BIT(12) + +#define NNP_VERSION_DOT_MASK GENMASK(4, 0) +#define NNP_VERSION_MINOR_MASK GENMASK(9, 5) +#define NNP_VERSION_MAJOR_MASK GENMASK(14, 10) +#define NNP_VERSION_MAJOR (ver) FIELD_GET(NNP_VERSION_MAJOR_MASK, (ver)) +#define NNP_VERSION_MINOR (ver) FIELD_GET(NNP_VERSION_MINOR_MASK, (ver)) +#define NNP_VERSION_DOT (ver) FIELD_GET(NNP_VERSION_DOT_MASK, (ver)) +#define NNP_MAKE_VERSION (major, minor, dot) \ + (FIELD_PREP(NNP_VERSION_MAJOR_MASK, (major)) | \ + FIELD_PREP(NNP_VERSION_MINOR_MASK, (minor)) | \ + FIELD_PREP(NNP_VERSION_DOT_MASK, (dot))) + +#define NNP_IPC_PROTOCOL_VERSION NNP_MAKE_VERSION(4, 1, 0) + +#define NNP_IPC_DMA_PFN_BITS 45/* size of physical address in protocol */ +#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT +#define NNP_IPC_DMA_PFN_MASK GENMASK_ULL(56, 12) +#define NNP_IPC_DMA_MAX_ADDR GENMASK_ULL(56, 0) +#else +#define NNP_IPC_DMA_PFN_MASK GENMASK_ULL(31, 12) +#define NNP_IPC_DMA_MAX_ADDR GENMASK_ULL(31, 0) +#endif +#define NNP_IPC_DMA_ADDR_TO_PFN (dma_addr) \ + FIELD_GET(NNP_IPC_DMA_PFN_MASK, (dma_addr)) +#define NNP_IPC_DMA_PFN_TO_ADDR (dma_pfn) \ + FIELD_PREP(NNP_IPC_DMA_PFN_MASK, (dma_pfn)) + +#define NNP_IPC_INF_CHANNEL_BITS 8 +#define NNP_IPC_CHANNEL_BITS 10 +#define NNP_IPC_MAX_CHANNEL_RB 2 + +/* +* Structures used inside data packets transferred in the protocol +*/ +struct nnp_dma_chain_header { +__le64 dma_next; +__le32 total_nents; +__le32 start_offset; +__le64 size; +} __packed; + +#define DMA_CHAIN_ENTRY_SIZE sizeof(__le64) +#define DMA_CHAIN_HEADER_SIZE sizeof(struct nnp_dma_chain_header) +#define DMA_CHAIN_ENTRY_NPAGES_BITS \ + (sizeof(__le64) * __CHAR_BIT__ - NNP_IPC_DMA_PFN_BITS) +#define NNP_MAX_CHUNK_SIZE \ + (((1lu << DMA_CHAIN_ENTRY_NPAGES_BITS) - 1) << NNP_PAGE_SHIFT) + +struct nnp_dma_chain_entry { +u64 dma_chunk_pfn :NNP_IPC_DMA_PFN_BITS ; +u64 n_pages :DMA_CHAIN_ENTRY_NPAGES_BITS ; +} __packed; + +#define DMA_CHAIN_ENTRY_PFN_MASK GENMASK_ULL(NNP_IPC_DMA_PFN_BITS - 1, 0) +#define DMA_CHAIN_ENTRY_NPAGES_MASK GENMASK_ULL(63, NNP_IPC_DMA_PFN_BITS) + +#define NENTS_PER_PAGE \ + ((NNP_PAGE_SIZE - DMA_CHAIN_HEADER_SIZE) / DMA_CHAIN_ENTRY_SIZE) + +/* +* IPC messages layout definition +*/ + +#define NNP_C2H_OP_MASK GENMASK_ULL(5, 0) + +/* NNP_IPC_C2H_OP_QUERY_VERSION_REPLY3 - 3 qwords */ +/* qword 0: */ +#define NNP_C2H_VERSION_REPLY_QW0_OP_MASK NNP_C2H_OP_MASK +#define NNP_C2H_VERSION_REPLY_QW0_PROT_VER_MASK GENMASK_ULL(21, 6) +#define NNP_C2H_VERSION_REPLY_QW0_FW_VER_MASK GENMASK_ULL(37, 22) +#define NNP_C2H_VERSION_REPLY_QW0_CHAN_VER_MASK GENMASK_ULL(53, 38) +/* qword 1: two bits for each possible response opcode specifying its size */ +/* qword 2: two bits for each possible command opcode specifying its size */ + +/* NNP_IPC_C2H_OP_EVENT_REPORT - 1 qword */ +#define NNP_C2H_EVENT_REPORT_OP_MASK NNP_C2H_OP_MASK +#define NNP_C2H_EVENT_REPORT_CODE_MASK GENMASK_ULL(12, 6) +#define NNP_C2H_EVENT_REPORT_CHAN_ID_MASK GENMASK_ULL(20, 13) +#define NNP_C2H_EVENT_REPORT_OBJ_ID_MASK GENMASK_ULL(36, 21) +#define NNP_C2H_EVENT_REPORT_OBJ_ID2_MASK GENMASK_ULL(52, 37) +#define NNP_C2H_EVENT_REPORT_VAL_MASK GENMASK_ULL(60, 53) +#define NNP_C2H_EVENT_REPORT_CHAN_VALID_MASK BIT_ULL(61) +#define NNP_C2H_EVENT_REPORT_OBJ_VALID_MASK BIT_ULL(62) +#define NNP_C2H_EVENT_REPORT_OBJ2_VALID_MASK BIT_ULL(63) + +/* NNP_IPC_C2H_OP_SYS_INFO - 1 qword */ +#define NNP_C2H_SYS_INFO_OP_MASK NNP_C2H_OP_MASK + +#define NNP_H2C_OP_MASK GENMASK_ULL(5, 0) + +/* NNP_IPC_H2C_OP_QUERY_VERSION - 1 qword */ +#define NNP_H2C_QUERY_VERSION_OP_MASK NNP_H2C_OP_MASK + +/* NNP_IPC_H2C_OP_SETUP_CRASH_DUMP - 2 qwords */ +/* qword 0: */ +#define NNP_H2C_SETUP_CRASH_DUMP_QW0_OP_MASK NNP_H2C_OP_MASK +#define NNP_H2C_SETUP_CRASH_DUMP_QW0_DMA_ADDR_MASK GENMASK_ULL(63, 19) +/* qword 1: physical address of BAR2 */ + +/* NNP_IPC_H2C_OP_SETUP_SYS_INFO_PAGE - 1 qword */ +#define NNP_H2C_SETUP_SYS_INFO_OP_MASK NNP_H2C_OP_MASK +#define NNP_H2C_SETUP_SYS_INFO_NPAGES GENMASK_ULL(15, 6) +#define NNP_H2C_SETUP_SYS_INFO_DMA_ADDR_MASK GENMASK_ULL(63, 19) + +/* NNP_IPC_H2C_OP_CHANNEL_OP - 1 qword */ +#define NNP_H2C_CHANNEL_OP_OP_MASK NNP_H2C_OP_MASK +#define NNP_H2C_CHANNEL_OP_CHAN_ID_MASK GENMASK_ULL(15, 6) +#define NNP_H2C_CHANNEL_OP_DESTROY_MASK BIT_ULL(16) +#define NNP_H2C_CHANNEL_OP_PRIV_MASK BIT_ULL(31) +#define NNP_H2C_CHANNEL_OP_UID_MASK GENMASK_ULL(63, 32) + +/* NNP_IPC_H2C_OP_CHANNEL_RB_OP - 1 qword */ +#define NNP_H2C_CHANNEL_RB_OP_OP_MASK NNP_H2C_OP_MASK +#define NNP_H2C_CHANNEL_RB_OP_CHAN_ID_MASK GENMASK_ULL(15, 6) +#define NNP_H2C_CHANNEL_RB_OP_H2C_MASK BIT_ULL(16) +#define NNP_H2C_CHANNEL_RB_OP_ID_MASK BIT_ULL(17) +#define NNP_H2C_CHANNEL_RB_OP_DESTROY_MASK BIT_ULL(18) +#define NNP_H2C_CHANNEL_RB_OP_HOST_PFN_MASK GENMASK_ULL(63, 19) + +/* NNP_IPC_H2C_OP_CHANNEL_HOSTRES_OP - 2 qwords */ +/* qword 0: */ +#define NNP_H2C_CHANNEL_HOSTRES_QW0_OP_MASK NNP_H2C_OP_MASK +#define NNP_H2C_CHANNEL_HOSTRES_QW0_CHAN_ID_MASK GENMASK_ULL(15, 6) +#define NNP_H2C_CHANNEL_HOSTRES_QW0_ID_MASK GENMASK_ULL(31, 16) +#define NNP_H2C_CHANNEL_HOSTRES_QW0_UNMAP_MASK BIT_ULL(32) +/* qword 1: */ +#define NNP_H2C_CHANNEL_HOSTRES_QW1_HOST_PFN_MASK GENMASK_ULL(44, 0) + +/* NNP_IPC_H2C_OP_CLOCK_STAMP - 2 qwords */ +/* qword 0: */ +#define NNP_H2C_CLOCK_STAMP_QW0_OP_MASK NNP_H2C_OP_MASK +#define NNP_H2C_CLOCK_STAMP_QW0_TYPE_MASK GENMASK_ULL(63, 8) +/* qword 1: clock stamp value */ + +/* +* IPC messages opcodes and related utility macros +*/ +#define H2C_OPCODE_NAME (name) NNP_IPC_H2C_OP_ ## name +#define H2C_OPCODE_NAME_STR (name) #name +#define C2H_OPCODE_NAME (name) NNP_IPC_C2H_OP_ ## name +#define C2H_OPCODE_NAME_STR (name) #name + +/* +* Define Host-to-card opcodes (valid range is 0 - 31) +*/ +enum nnp_h2c_opcodes { + H2C_OPCODE_NAME(QUERY_VERSION) = 0, + H2C_OPCODE_NAME(CLOCK_STAMP) = 2, + H2C_OPCODE_NAME(SETUP_CRASH_DUMP) = 6, + H2C_OPCODE_NAME(SETUP_SYS_INFO_PAGE) = 7, + H2C_OPCODE_NAME(CHANNEL_OP) = 22, + H2C_OPCODE_NAME(CHANNEL_RB_OP) = 23, + H2C_OPCODE_NAME(CHANNEL_HOSTRES_OP) = 24, + + H2C_OPCODE_NAME(BIOS_PROTOCOL) = 31, + H2C_OPCODE_NAME(LAST) = NNP_IPC_H2C_OP_BIOS_PROTOCOL, +}; + +/* +* Define Card-to-host opcodes +*/ +enum nnp_c2h_opcodes { +NNP_IPC_C2H_OP_QUERY_VERSION_REPLY =0 , +NNP_IPC_C2H_OP_QUERY_VERSION_REPLY2 =1 , +NNP_IPC_C2H_OP_QUERY_VERSION_REPLY3 =2 , +NNP_IPC_C2H_OP_EVENT_REPORT =4 , +NNP_IPC_C2H_OP_SYS_INFO =11 , + +NNP_IPC_C2H_OP_BIOS_PROTOCOL =31 , +NNP_IPC_C2H_OPCODE_LAST =NNP_IPC_C2H_OP_BIOS_PROTOCOL , +}; + +/* +* IPC messages protocol between the host driver and card BIOS +*/ + +enum nnp_bios_c2h_msg_types { +NNP_IPC_C2H_TYPE_BIOS_VERSION =0x1 , +}; + +enum nnp_bios_h2c_msg_types { +NNP_IPC_H2C_TYPE_BOOT_IMAGE_READY =0x10 , +NNP_IPC_H2C_TYPE_SYSTEM_INFO_REQ =0x11 , +}; + +/* NNP_IPC_C2H_OP_BIOS_PROTOCOL - 1 qword */ +#define NNP_C2H_BIOS_PROTOCOL_OP_MASK NNP_C2H_OP_MASK +/* bios message type */ +#define NNP_C2H_BIOS_PROTOCOL_TYPE_MASK GENMASK_ULL(15, 8) +/* message size in bytes */ +#define NNP_C2H_BIOS_PROTOCOL_SIZE_MASK GENMASK_ULL(31, 16) + +/* BIOS Revision Identification Specification, Rev. 2.0, 01/30/2015 */ +struct nnp_c2h_bios_version { +__le16 board_id[7 ]; +__le16 board_rev; +__le16 dot1; +__le16 board_ext[3 ]; +__le16 dot2; +__le16 version_major[4 ]; +__le16 dot3; +__le16 build_type; +__le16 version_minor[2 ]; +__le16 dot4; +__le16 time_stamp[10 ]; +__le16 null_terminator; +} __packed; + +struct nnp_c2h_bios_fw_ver_ack_data { +__le16 code_minor; +__le16 code_major; +__le16 code_build_no; +__le16 code_hot_fix; +__le16 rcvyminor; +__le16 rcvymajor; +__le16 rcvybuildno; +__le16 rcvy_hot_fix; +__le16 fitc_minor; +__le16 fitc_major; +__le16 fitcbuildno; +__le16 fitc_hot_fix; +} __packed; + +struct nnp_c2h_fw_version { +__le16 major; +__le16 minor; +__le16 hotfix; +__le16 build; +} __packed; + +struct nnp_c2h_cpu_info { +__le32 cpu_family;/* CPU Family: 0x000906D0 */ +__u8 cpu_stepping;/* CPU Stepping */ +__u8 cpu_sku;/* CPU SKU */ +__le16 cpu_did;/* range 0x4580-0x45FF */ +__le16 cpu_core_count;/* Number of enabled cores */ +__le16 cpu_thread_count;/* Number of threads */ +} __packed; + +struct nnp_c2h_ice_info { +__le16 ice_count; +__le32 ice_available_mask; +} __packed; + +struct nnp_c2h_system_info { +__u8 version;/* NNP_SYSTEM_INFO structure version */ +__le16 board_id;/* Board identification- for RVP = 0x25 */ +__u8 fab_id;/* Board Revision identification */ +__u8 bom_id;/* Board Bill Of Material identification */ +__u8 platform_type;/* For RVP= 0x2, M.2 = 0x3 */ +__u8 platform_flavor;/* For NNP = 0x5- Embedded */ +struct nnp_c2h_cpu_info cpu_info;/* CPU Information */ +struct nnp_c2h_ice_info ice_info;/* ICE Information */ +struct nnp_c2h_bios_version bios_ver;/* BIOS version string */ +struct nnp_c2h_bios_fw_ver_ack_data csme_version; +struct nnp_c2h_fw_version pmc_version; +} __packed; + +/* NNP_H2C_BOOT_IMAGE_READY command - 3 qwords */ +/* qword 0: */ +/* op should be set to NNP_IPC_H2C_OP_BIOS_PROTOCOL */ +#define NNP_H2C_BOOT_IMAGE_READY_QW0_OP_MASK NNP_H2C_OP_MASK +/* bios message type = NNP_IPC_H2C_TYPE_BOOT_IMAGE_READY */ +#define NNP_H2C_BOOT_IMAGE_READY_QW0_TYPE_MASK GENMASK_ULL(15, 8) +/* message size in bytes = 2*sizeof(u64) */ +#define NNP_H2C_BOOT_IMAGE_READY_QW0_SIZE_MASK GENMASK_ULL(31, 16) +/* qword 1: page table describtor address */ +/* qword 2: */ +#define NNP_H2C_BOOT_IMAGE_READY_QW2_DESC_SIZE_MASK GENMASK_ULL(31, 0) +#define NNP_H2C_BOOT_IMAGE_READY_QW2_IMAGE_SIZE_MASK GENMASK_ULL(63, 32) + +/* NNP_H2C_BIOS_SYS_INFO_REQ command - 3 qwords */ +/* qword 0: */ +/* op should be set to NNP_IPC_H2C_OP_BIOS_PROTOCOL */ +#define NNP_H2C_BIOS_SYS_INFO_REQ_QW0_OP_MASK NNP_H2C_OP_MASK +/* bios message type = NNP_IPC_H2C_TYPE_SYSTEM_INFO_REQ */ +#define NNP_H2C_BIOS_SYS_INFO_REQ_QW0_TYPE_MASK GENMASK_ULL(15, 8) +/* message size in bytes = 2*sizeof(u64) */ +#define NNP_H2C_BIOS_SYS_INFO_REQ_QW0_SIZE_MASK GENMASK_ULL(31, 16) +/* qword 1: allocate sys info page address */ +/* qword 2: */ +#define NNP_H2C_BIOS_SYS_INFO_REQ_QW2_SIZE_MASK GENMASK_ULL(31, 0) + +#define NNP_BIOS_VERSION_LEN \ + (sizeof(struct nnp_c2h_bios_version) / sizeof(__le16)) +#define NNP_BOARD_NAME_LEN 72 +#define NNP_IMAGE_VERSION_LEN 128 +#define NNP_PRD_SERIAL_LEN 16 +#define NNP_PART_NUM_LEN 12 + +struct nnp_sys_info { +__le32 ice_mask; +char bios_version[NNP_BIOS_VERSION_LEN ]; +char board_name[NNP_BOARD_NAME_LEN ]; +char image_version[NNP_IMAGE_VERSION_LEN ]; +char prd_serial[NNP_PRD_SERIAL_LEN ]; +char brd_part_no[NNP_PART_NUM_LEN ]; +__le16 fpga_rev; +__le64 total_unprotected_memory; +__le64 total_ecc_memory; +__u8 stepping; +} __packed; + +/* +* Define header structure for all "channel" message protocols. +* This protocol defines communication between host UMD and card. +*/ +#define NNP_H2C_CHAN_MSG_OP_MASK NNP_H2C_OP_MASK +#define NNP_H2C_CHAN_MSG_CHAN_ID_MASK GENMASK_ULL(15, 6) + +#define NNP_C2H_CHAN_MSG_OP_MASK NNP_C2H_OP_MASK +#define NNP_C2H_CHAN_MSG_CHAN_ID_MASK GENMASK_ULL(15, 6) + +#endif /* of _IPC_PROTOCOL_H */
Signature · 1 line
1.8.3.1
PATCH v2 5/15 misc: nnpi: Manage host memory resources
In reply to Guy ZadicarioProvide interface for creating a "host resource" - a memory object which can be mapped to a dma address space of one or more NNP-I devices. These host resource objects manage memory blocks from which big chunks of data, such as inference tensors, the device's embedded OS and telemtry data are DMA'ed to and from the NNP-I device.
--- Documentation/ABI/testing/sysfs-driver-intel_nnpi | 5 + drivers/misc/intel-nnpi/Makefile | 2 +- drivers/misc/intel-nnpi/hostres.c | 627 ++++++++++++++++++++++ drivers/misc/intel-nnpi/hostres.h | 167 ++++++ 4 files changed, 800 insertions(+), 1 deletion(-) create mode 100644 Documentation/ABI/testing/sysfs-driver-intel_nnpi create mode 100644 drivers/misc/intel-nnpi/hostres.c create mode 100644 drivers/misc/intel-nnpi/hostres.h
Documentation/ABI/testing/sysfs-driver-intel_nnpi new file +5 -0
@@ -0,0 +1,5 @@ +What: /sys/class/nnpi_host/nnpi_host/total_hostres_size +Date: Mar 2021 +Kernelversion: 5.13 +Contact: [email protected] +Description: Total size in bytes of all allocated NNP-I host resources.
drivers/misc/intel-nnpi/Makefile +1 -1
@@ -5,7 +5,7 @@ obj-$(CONFIG_INTEL_NNPI) := intel_nnpi.o intel_nnpi_pcie.o -intel_nnpi-y := device.o msg_scheduler.o +intel_nnpi-y := device.o msg_scheduler.o hostres.o intel_nnpi_pcie-y := nnp_pcie.o
drivers/misc/intel-nnpi/hostres.c new file +627 -0
627 lines not on this page · raw message
drivers/misc/intel-nnpi/hostres.h new file +167 -0
167 lines not on this page · raw message
Signature · 1 line
1.8.3.1
PATCH v2 6/15 misc: nnpi: Allow usermode to manage host resources
In reply to Guy ZadicarioProvide an IOCTL interface for creating and destroying host memory resources through a character device (/dev/nnpi_host).
There is a single instance of this character device in the system regardless of the number of NNP-I devices attached because it controls host resources which may be shared between different devices.
A nnp_user object, created when an application opens this character device, identifies the user (client) of the driver and holds a list of all host resources allocated by that user through the opened file descriptor.
Host memory resources created through this character device can be mapped to device access through IOCTLs made to a different, per-device, chardev (will be introduced on next commits).
All resources will be destroyed when the application closes the connection or exits.
The IOCTL interface is defined in: include/uapi/misc/intel_nnpi.h
--- MAINTAINERS | 1 + drivers/misc/intel-nnpi/Makefile | 2 +- drivers/misc/intel-nnpi/device.c | 14 ++ drivers/misc/intel-nnpi/host_chardev.c | 346 +++++++++++++++++++++++++++++++++ drivers/misc/intel-nnpi/host_chardev.h | 12 ++ drivers/misc/intel-nnpi/nnp_user.c | 131 +++++++++++++ drivers/misc/intel-nnpi/nnp_user.h | 79 ++++++++ include/uapi/misc/intel_nnpi.h | 150 ++++++++++++++ 8 files changed, 734 insertions(+), 1 deletion(-) create mode 100644 drivers/misc/intel-nnpi/host_chardev.c create mode 100644 drivers/misc/intel-nnpi/host_chardev.h create mode 100644 drivers/misc/intel-nnpi/nnp_user.c create mode 100644 drivers/misc/intel-nnpi/nnp_user.h create mode 100644 include/uapi/misc/intel_nnpi.h
MAINTAINERS +1 -0
@@ -9320,6 +9320,7 @@ INTEL NNP-I PCI DRIVER M: Guy Zadicario <[email protected]> S: Supported F: drivers/misc/intel-nnpi/ +F: include/uapi/misc/intel_nnpi.h INTEL P-Unit IPC DRIVER M: Zha Qipeng <[email protected]>
drivers/misc/intel-nnpi/Makefile +1 -1
@@ -5,7 +5,7 @@ obj-$(CONFIG_INTEL_NNPI) := intel_nnpi.o intel_nnpi_pcie.o -intel_nnpi-y := device.o msg_scheduler.o hostres.o +intel_nnpi-y := device.o msg_scheduler.o hostres.o host_chardev.o nnp_user.o intel_nnpi_pcie-y := nnp_pcie.o
drivers/misc/intel-nnpi/device.c +14 -0
@@ -8,6 +8,7 @@#include <linux/module.h> #include "device.h" +#include "host_chardev.h" #include "msg_scheduler.h" static DEFINE_IDA (dev_ida);
@@ -99,6 +100,19 @@ void nnpdev_destroy(struct nnp_device *nnpdev) }EXPORT_SYMBOL (nnpdev_destroy); +static int __init nnp_init (void ) +{ +return nnp_init_host_interface (); +} +subsys_initcall (nnp_init); + +static void __exit nnp_cleanup(void) +{ +nnp_release_host_interface (); +/* dev_ida is already empty here - no point calling ida_destroy */ +} +module_exit (nnp_cleanup); +MODULE_LICENSE ("GPL" );MODULE_DESCRIPTION ("Intel(R) NNPI Framework" );MODULE_AUTHOR ("Intel Corporation" );
drivers/misc/intel-nnpi/host_chardev.c new file +346 -0
346 lines not on this page · raw message
drivers/misc/intel-nnpi/host_chardev.h new file +12 -0
@@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* Copyright (C) 2019-2021 Intel Corporation */ + +#ifndef _NNPDRV_INFERENCE_H +#define _NNPDRV_INFERENCE_H + +int nnp_init_host_interface (void ); +void nnp_release_host_interface (void ); + +struct file *nnp_host_file_get (int host_fd); + +#endif
drivers/misc/intel-nnpi/nnp_user.c new file +131 -0
131 lines not on this page · raw message
drivers/misc/intel-nnpi/nnp_user.h new file +79 -0
79 lines not on this page · raw message
include/uapi/misc/intel_nnpi.h new file +150 -0
150 lines not on this page · raw message
Signature · 1 line
1.8.3.1
Re: [PATCH v2 06/15] misc: nnpi: Allow usermode to manage host resources
Quoted 707 lines · Guy Zadicario
Provide an IOCTL interface for creating and destroying host memory resources through a character device (
/dev/nnpi_host).There is a single instance of this character device in the system regardless of the number of NNP-I devices attached because it controls host resources which may be shared between different devices.
A
nnp_userobject, created when an application opens this character device, identifies the user (client) of the driver and holds a list of all host resources allocated by that user through the opened file descriptor.Host memory resources created through this character device can be mapped to device access through IOCTLs made to a different, per-device, chardev (will be introduced on next commits).
All resources will be destroyed when the application closes the connection or exits.
The IOCTL interface is defined in:
include/uapi/misc/intel_nnpi.hSigned-off-by Guy Zadicario <[email protected]>Reviewed-by Alexander Shishkin <[email protected]>--- MAINTAINERS | 1 + drivers/misc/intel-nnpi/Makefile | 2 +- drivers/misc/intel-nnpi/device.c | 14 ++ drivers/misc/intel-nnpi/host_chardev.c | 346 +++++++++++++++++++++++++++++++++ drivers/misc/intel-nnpi/host_chardev.h | 12 ++ drivers/misc/intel-nnpi/nnp_user.c | 131 +++++++++++++ drivers/misc/intel-nnpi/nnp_user.h | 79 ++++++++ include/uapi/misc/intel_nnpi.h | 150 ++++++++++++++ 8 files changed, 734 insertions(+), 1 deletion(-) create mode 100644 drivers/misc/intel-nnpi/host_chardev.c create mode 100644 drivers/misc/intel-nnpi/host_chardev.h create mode 100644 drivers/misc/intel-nnpi/nnp_user.c create mode 100644 drivers/misc/intel-nnpi/nnp_user.h create mode 100644 include/uapi/misc/intel_nnpi.hMAINTAINERS +1
-0@@ -9320,6 +9320,7 @@ INTEL NNP-I PCI DRIVER M: Guy Zadicario <[email protected]> S: Supported F: drivers/misc/intel-nnpi/ +F: include/uapi/misc/intel_nnpi.h INTEL P-Unit IPC DRIVER M: Zha Qipeng <[email protected]>drivers/misc/intel-nnpi/Makefile +1
-1@@ -5,7 +5,7 @@ obj-$(CONFIG_INTEL_NNPI) := intel_nnpi.o intel_nnpi_pcie.o -intel_nnpi-y := device.o msg_scheduler.o hostres.o +intel_nnpi-y := device.o msg_scheduler.o hostres.o host_chardev.o nnp_user.o intel_nnpi_pcie-y := nnp_pcie.odrivers/misc/intel-nnpi/device.c +14
-0@@ -8,6 +8,7 @@#include <linux/module.h> #include "device.h" +#include "host_chardev.h" #include "msg_scheduler.h" static DEFINE_IDA (dev_ida);@@ -99,6 +100,19 @@ void nnpdev_destroy(struct nnp_device *nnpdev) }EXPORT_SYMBOL (nnpdev_destroy); +static int __init nnp_init (void ) +{ +return nnp_init_host_interface (); +} +subsys_initcall (nnp_init); + +static void __exit nnp_cleanup(void) +{ +nnp_release_host_interface (); +/* dev_ida is already empty here - no point calling ida_destroy */ +} +module_exit (nnp_cleanup); +MODULE_LICENSE ("GPL" );MODULE_DESCRIPTION ("Intel(R) NNPI Framework" );MODULE_AUTHOR ("Intel Corporation" );drivers/misc/intel-nnpi/host_chardev.c new file +346
-0@@ -0,0 +1,346 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (C) 2019-2021 Intel Corporation */ + +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt + +#include <linux/cdev.h> +#include <linux/device.h> +#include <linux/file.h> +#include <linux/fs.h> +#include <linux/hashtable.h> +#include <linux/idr.h> +#include <linux/printk.h> +#include <linux/slab.h> +#include <uapi/misc/intel_nnpi.h> + +#include "device.h" +#include "host_chardev.h" +#include "ipc_protocol.h" +#include "nnp_user.h" + +static struct cdev cdev; +static dev_t devnum; +static struct class *class; +static struct device *dev; + +static inline int is_host_file (struct file *f); + +static enum dma_data_direction to_dma_dir (unsigned int nnp_dir) +{ +/* Ignore IOCTL_INF_RES_NETWORK */ +switch (nnp_dir & (IOCTL_INF_RES_INPUT |IOCTL_INF_RES_OUTPUT )) { +case (IOCTL_INF_RES_INPUT |IOCTL_INF_RES_OUTPUT ): +return DMA_BIDIRECTIONAL ; +case IOCTL_INF_RES_INPUT : +return DMA_TO_DEVICE ; +case IOCTL_INF_RES_OUTPUT : +return DMA_FROM_DEVICE ; +default : +break ; + } + +return DMA_NONE ; +} + +static long create_hostres (struct nnp_user_info *user_info,void __user *arg, +unsigned int size) +{ +int ret; +struct nnpdrv_ioctl_create_hostres req; +struct host_resource *hostres; +struct user_hostres *user_hostres_entry; +void __user *uptr; +unsigned int io_size =sizeof (req); + +if (size != io_size) +return -EINVAL ; + +if (copy_from_user (&req, arg, io_size)) +return -EFAULT ; + +if (req.usage_flags & ~IOCTL_RES_USAGE_VALID_MASK ) +return -EINVAL ; + + uptr =u64_to_user_ptr (req.user_ptr); + hostres =nnp_hostres_from_usermem (uptr, req.size, +to_dma_dir (req.usage_flags)); + +if (IS_ERR (hostres)) +return PTR_ERR (hostres); + + ret =nnp_user_add_hostres (user_info, hostres, &user_hostres_entry); +if (ret <0 ) { +nnp_hostres_put (hostres); +return ret; + } + + req.size =nnp_hostres_size (hostres); + +/* +* The created user_hostres_entry holds refcount to the resource, +* no need to keep another one here. +*/ +nnp_hostres_put (hostres); + + req.user_handle = user_hostres_entry->user_handle; +if (copy_to_user (arg, &req, io_size)) { + ret = -EFAULT ; + goto destroy_hostres_entry; + } + +return 0 ; + +destroy_hostres_entry: +nnp_user_remove_hostres (user_hostres_entry); + +return ret; +} + +static long destroy_hostres (struct nnp_user_info *user_info,void __user *arg, +unsigned int size) +{ +struct nnpdrv_ioctl_destroy_hostres destroy_args; +struct user_hostres *user_hostres_entry; +unsigned int io_size =sizeof (destroy_args); +int ret =0 ; + +if (size != io_size) +return -EINVAL ; + +if (copy_from_user (&destroy_args, arg, io_size)) +return -EFAULT ; + +/* errno must be cleared on entry */ +if (destroy_args.o_errno) +return -EINVAL ; + +mutex_lock (&user_info->mutex); + user_hostres_entry =idr_find (&user_info->idr, destroy_args.user_handle); +if (user_hostres_entry) { +nnp_user_remove_hostres_locked (user_hostres_entry); + }else { + destroy_args.o_errno =NNPER_NO_SUCH_RESOURCE ; +if (copy_to_user (arg, &destroy_args, io_size)) + ret = -EFAULT ; + } + +mutex_unlock (&user_info->mutex); +return ret; +} + +static long lock_hostres (struct nnp_user_info *user_info,void __user *arg, +unsigned int size) +{ +int ret =0 ; +struct nnpdrv_ioctl_lock_hostres lock_args; +struct user_hostres *user_hostres_entry; +unsigned int io_size =sizeof (lock_args); + +if (size != io_size) +return -EINVAL ; + +if (copy_from_user (&lock_args, arg, io_size)) +return -EFAULT ; + +/* errno must be cleared on entry */ +if (lock_args.o_errno) +return -EINVAL ; + +mutex_lock (&user_info->mutex); + user_hostres_entry =idr_find (&user_info->idr, lock_args.user_handle); +if (user_hostres_entry) { + ret =nnp_hostres_user_lock (user_hostres_entry->hostres); + }else { + lock_args.o_errno =NNPER_NO_SUCH_RESOURCE ; +if (copy_to_user (arg, &lock_args, io_size)) + ret = -EFAULT ; + } + +mutex_unlock (&user_info->mutex); +return ret; +} + +static long unlock_hostres (struct nnp_user_info *user_info,void __user *arg, +unsigned int size) +{ +int ret =0 ; +struct user_hostres *user_hostres_entry; +struct nnpdrv_ioctl_lock_hostres lock_args; +unsigned int io_size =sizeof (lock_args); + +if (size != io_size) +return -EINVAL ; + +if (copy_from_user (&lock_args, arg, io_size)) +return -EFAULT ; + +/* errno must be cleared on entry */ +if (lock_args.o_errno) +return -EINVAL ; + +mutex_lock (&user_info->mutex); + user_hostres_entry =idr_find (&user_info->idr, lock_args.user_handle); +if (user_hostres_entry) { + ret =nnp_hostres_user_unlock (user_hostres_entry->hostres); + }else { + lock_args.o_errno =NNPER_NO_SUCH_RESOURCE ; +if (copy_to_user (arg, &lock_args,sizeof (lock_args))) + ret = -EFAULT ; + } + +mutex_unlock (&user_info->mutex); +return ret; +} + +struct file *nnp_host_file_get (int host_fd) +{ +struct file *host_file; + + host_file =fget (host_fd); +if (is_host_file (host_file)) +return host_file; + +if (host_file) +fput (host_file); + +return NULL ; +} + +/* +* Inference host cdev (/dev/nnpi_host) file operation functions +*/ + +static int host_open (struct inode *inode,struct file *f) +{ +struct nnp_user_info *user_info; + +if (!is_host_file (f)) +return -EINVAL ; + + user_info =kzalloc (sizeof (*user_info),GFP_KERNEL ); +if (!user_info) +return -ENOMEM ; + +nnp_user_init (user_info); + + f->private_data = user_info; + +return 0 ; +} + +static int host_release (struct inode *inode,struct file *f) +{ +struct nnp_user_info *user_info; + +if (!is_host_file (f)) +return -EINVAL ; + + user_info = f->private_data; + +nnp_user_destroy_all (user_info); + f->private_data =NULL ; + +return 0 ; +} + +static long host_ioctl (struct file *f,unsigned int cmd,unsigned long arg) +{ +long ret =0 ; +struct nnp_user_info *user_info = f->private_data; +unsigned int ioc_nr, size; + +if (!is_host_file (f)) +return -ENOTTY ; + +if (_IOC_TYPE (cmd) !='h' ) +return -EINVAL ; + + ioc_nr =_IOC_NR (cmd); + size =_IOC_SIZE (cmd); + +switch (ioc_nr) { +case _IOC_NR (IOCTL_INF_CREATE_HOST_RESOURCE ): + ret =create_hostres (user_info, (void __user *)arg, size); +break ; +case _IOC_NR (IOCTL_INF_DESTROY_HOST_RESOURCE ): + ret =destroy_hostres (user_info, (void __user *)arg, size); +break ; +case _IOC_NR (IOCTL_INF_UNLOCK_HOST_RESOURCE ): + ret =unlock_hostres (user_info, (void __user *)arg, size); +break ; +case _IOC_NR (IOCTL_INF_LOCK_HOST_RESOURCE ): + ret =lock_hostres (user_info, (void __user *)arg, size); +break ; +default : + ret = -EINVAL ; + } + +return ret; +} + +static const struct file_operations host_fops = { + .owner =THIS_MODULE , + .open = host_open, + .release = host_release, + .unlocked_ioctl = host_ioctl, + .compat_ioctl = host_ioctl, +}; + +static inline int is_host_file (struct file *f) +{ +return f && f->f_op == &host_fops; +} + +int nnp_init_host_interface (void ) +{ +int ret; + + ret =alloc_chrdev_region (&devnum,0 ,1 ,NNPDRV_INF_HOST_DEV_NAME ); +if (ret <0 ) +return ret; + +cdev_init (&cdev, &host_fops); + cdev.owner =THIS_MODULE ; + + ret =cdev_add (&cdev, devnum,1 ); +if (ret <0 ) + goto err_region; + + class =class_create (THIS_MODULE ,NNPDRV_INF_HOST_DEV_NAME ); +if (IS_ERR (class)) { + ret =PTR_ERR (class); + goto err_cdev; + } + + dev =device_create (class,NULL , devnum,NULL ,NNPDRV_INF_HOST_DEV_NAME ); +if (IS_ERR (dev)) { + ret =PTR_ERR (dev); + goto err_class; + } + + ret =nnp_hostres_init_sysfs (dev); +if (ret <0 ) + goto err_device; + +return 0 ; + +err_device: +device_destroy (class, devnum); +err_class: +class_destroy (class); +err_cdev: +cdev_del (&cdev); +err_region: +unregister_chrdev_region (devnum,1 ); + +return ret; +} + +void nnp_release_host_interface (void ) +{ +nnp_hostres_fini_sysfs (dev); +device_destroy (class, devnum); +class_destroy (class); +cdev_del (&cdev); +unregister_chrdev_region (devnum,1 ); +}drivers/misc/intel-nnpi/host_chardev.h new file +12
-0@@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* Copyright (C) 2019-2021 Intel Corporation */ + +#ifndef _NNPDRV_INFERENCE_H +#define _NNPDRV_INFERENCE_H + +int nnp_init_host_interface (void ); +void nnp_release_host_interface (void ); + +struct file *nnp_host_file_get (int host_fd); + +#endif drivers/misc/intel-nnpi/nnp_user.c new file +131
-0@@ -0,0 +1,131 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (C) 2019-2021 Intel Corporation */ + +#include <linux/slab.h> + +#include "nnp_user.h" + +void nnp_user_init (struct nnp_user_info *user_info) +{ +INIT_LIST_HEAD (&user_info->hostres_list); +mutex_init (&user_info->mutex); +kref_init (&user_info->ref); +idr_init (&user_info->idr); +} + +void nnp_user_get (struct nnp_user_info *user_info) +{ +kref_get (&user_info->ref); +} + +static void nnp_user_release (struct kref *kref) +{ +struct nnp_user_info *user_info = + container_of(kref, struct nnp_user_info, ref); +struct completion *completion = user_info->close_completion; + +idr_destroy (&user_info->idr); +kfree (user_info); +complete (completion); +} + +void nnp_user_put (struct nnp_user_info *user_info) +{ +kref_put (&user_info->ref, nnp_user_release); +} + +int nnp_user_add_hostres (struct nnp_user_info *user_info, +struct host_resource *hostres, +struct user_hostres **user_hostres_entry) +{ +struct user_hostres *hr_entry; +int id; + + hr_entry =kmalloc (sizeof (*hr_entry),GFP_KERNEL ); +if (!hr_entry) +return -ENOMEM ; + +/* +* Increment refcount to hostres for the entry reference. +* (caller holds reference to it, so we know it exist). +*/ +nnp_hostres_get (hostres); + hr_entry->hostres = hostres; + +/* +* We are called from ioctl of file that own this user_info, +* So it safe to assume it exist. +*/ +nnp_user_get (user_info); + hr_entry->user_info = user_info; + +mutex_lock (&user_info->mutex); +/* +* We allocate handle starting from 1 and not 0 to allow +* user-space treat zero as invalid handle +*/ + id =idr_alloc (&user_info->idr, hr_entry,1 ,-1 ,GFP_KERNEL ); +if (id <0 ) { +nnp_user_put (user_info); +nnp_hostres_put (hostres); +kfree (hr_entry); +mutex_unlock (&user_info->mutex); +return -ENOSPC ; + } + hr_entry->user_handle = id; +list_add (&hr_entry->node, &user_info->hostres_list); +mutex_unlock (&user_info->mutex); + + *user_hostres_entry = hr_entry; + +return 0 ; +} + +void nnp_user_remove_hostres_locked (struct user_hostres *hr_entry) +{ +struct nnp_user_info *user_info = hr_entry->user_info; + +idr_remove (&user_info->idr, hr_entry->user_handle); +list_del (&hr_entry->node); + +nnp_hostres_put (hr_entry->hostres); + +kfree (hr_entry); +nnp_user_put (user_info); +} + +void nnp_user_remove_hostres (struct user_hostres *hr_entry) +{ +struct nnp_user_info *user_info = hr_entry->user_info; + +mutex_lock (&user_info->mutex); +nnp_user_remove_hostres_locked (hr_entry); +mutex_unlock (&user_info->mutex); +} + +void nnp_user_destroy_all (struct nnp_user_info *user_info) +{ +struct user_hostres *user_hostres_entry; +DECLARE_COMPLETION_ONSTACK (completion); + +mutex_lock (&user_info->mutex); + +/* destroy all hostreses owned by the "user" */ +while (!list_empty (&user_info->hostres_list)) { + user_hostres_entry =list_first_entry (&user_info->hostres_list, + struct user_hostres, node); +/* +* We can safely destroy this object without checking +* its refcount since we get here only after the host char-dev +* as well as all cmd_chan char-devs that may hold temporary +* reference to this object are already released. +*/ +nnp_user_remove_hostres_locked (user_hostres_entry); + } +mutex_unlock (&user_info->mutex); + +/* wait for all channels and hostreses to be destroyed */ + user_info->close_completion = &completion; +nnp_user_put (user_info); +wait_for_completion (&completion); +}drivers/misc/intel-nnpi/nnp_user.h new file +79
-0@@ -0,0 +1,79 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* Copyright (C) 2019-2021 Intel Corporation */ + +#ifndef _NNPDRV_INF_PROC_H +#define _NNPDRV_INF_PROC_H + +#include <linux/kref.h> +#include <linux/types.h> + +#include "hostres.h" + +/** +* struct nnp_user_info - structure for per-user info +* @ref: refcount to this "user" object +* @hostres_list: list of host resources +* @close_completion: used to wait for all channels of this user to be +* destroyed before closing the user. +* @mutex: protects hostres_list and idr modifications +* @idr: used to generate user handles to created host resources +* @user_list_node: list node to attach this struct in "list of users". +* +* structure to hold per-user info, +* a "user" is created for each open made to the host char dev (/dev/nnpi_host). +* It holds a list of all host resources created through requests from +* the same client ("user"). +* device communication "channels", created by device char dev (/dev/nnpi%d) +* must be correlated with a "user" object which is supplied from user-space +* by the opened file descriptor to /dev/nnpi_host. Such "channel" may access +* only host resources created by the same "user". +* The lifetime of this object last at least for the duration of the host char +* device file struct but can last longer if some channel objects still hold +* a reference to it (this is why @ref is needed). +*/ +struct nnp_user_info { +struct kref ref; +struct list_head hostres_list; +struct completion *close_completion; +struct mutex mutex; +struct idr idr; +struct list_head user_list_node; +}; + +/** +* struct user_hostres - structure for host resource created by user +* @node: list node to attach this struct to nnp_user_info::hostres_list +* @hostres: the actual host resource object +* @user_handle: handle allocated from idr object, used as handle to this +* object in ioctl ABI. +* @user_info: pointer to "user" which created this resource. +* it is used only during destruction of the object. +* +* structure for a host resource object which created through host char dev +* request. The lifetime of this structure ends when the user request to +* destroy it through ioctl call. The underlying @hostres may still continue +* to exist if command channel (cmd_chan) objects has mapped the resource to +* device access. +*/ +struct user_hostres { +struct list_head node; +struct host_resource *hostres; +int user_handle; +struct nnp_user_info *user_info; +}; + +void nnp_user_init (struct nnp_user_info *user_info); + +void nnp_user_get (struct nnp_user_info *user_info); +void nnp_user_put (struct nnp_user_info *user_info); + +int nnp_user_add_hostres (struct nnp_user_info *user_info, +struct host_resource *hostres, +struct user_hostres **user_hostres_entry); + +void nnp_user_remove_hostres (struct user_hostres *hr_entry); +void nnp_user_remove_hostres_locked (struct user_hostres *hr_entry); + +void nnp_user_destroy_all (struct nnp_user_info *user_info); + +#endif include/uapi/misc/intel_nnpi.h new file +12
-0@@ -0,0 +1,150 @@ +/* SPDX-License-Identifier: GPL-2.0-only WITH Linux-syscall-note */ +/* Copyright (C) 2019-2021 Intel Corporation */ + +#ifndef _NNP_UAPI_H +#define _NNP_UAPI_H + +#include <linux/types.h> +#include <linux/ioctl.h> +#include <stdbool.h> + +#define NNPDRV_INF_HOST_DEV_NAME "nnpi_host" +
include/uapi/misc/intel_nnpi.h line 22
Quoted 10 lines
include/uapi/misc/intel_nnpi.h new file +10
-0+/* +* ioctls for /dev/nnpi_host device +*/ + +/* +* IOCTL_INF_CREATE_HOST_RESOURCE: +* +* A request to create a host memory resource object that can then be mapped +* and accessed by the NNP-I device's DMA engine. +* The created host resource is pinned in memory for its entire lifecycle.
Drive-by comments, but who has access to this interface? non-root?
How much memory can they pin with it?
Pinning is not a nice way to do things, you likely need some sort of memory manager and fencing system.
Dave.
PATCH v2 7/15 misc: nnpi: Disallow host memory resource access if no NNP-I devices exist
In reply to Guy ZadicarioThere is no point allowing a user application to create host resources on a system that does not equipped with any NNP-I devices. Fail openning the nnpi_host character device when no NNP-I devices are attached.
It is OK to do that check without any synchronization as a race would not be an issue.
--- drivers/misc/intel-nnpi/device.c | 5 +++++ drivers/misc/intel-nnpi/device.h | 2 ++ drivers/misc/intel-nnpi/host_chardev.c | 7 +++++++ 3 files changed, 14 insertions(+)
drivers/misc/intel-nnpi/device.c +5 -0
@@ -13,6 +13,11 @@static DEFINE_IDA (dev_ida); +bool nnpdev_no_devices (void ) +{ +return ida_is_empty (&dev_ida); +} +/** * nnpdev_init() - initialize NNP-I device structure. * @nnpdev: device to be initialized
drivers/misc/intel-nnpi/device.h +2 -0
@@ -36,6 +36,8 @@ struct nnp_device_ops {int (*cmdq_write_mesg)(struct nnp_device *nnpdev,u64 *msg,u32 size); }; +bool nnpdev_no_devices (void ); +/* * Functions exported by the device framework module which are * called by the lower layer NNP-I device driver module
drivers/misc/intel-nnpi/host_chardev.c +7 -0
@@ -217,6 +217,13 @@ static int host_open(struct inode *inode, struct file *f)if (!is_host_file (f))return -EINVAL ; +/* +* No point to serve host resource creation while no +* NNP-I devices exist in the system. +*/ +if (nnpdev_no_devices ()) +return -ENODEV ; + user_info =kzalloc (sizeof (*user_info),GFP_KERNEL );if (!user_info)return -ENOMEM ;
Signature · 1 line
1.8.3.1
PATCH v2 8/15 misc: nnpi: Boot NNP-I device
In reply to Guy ZadicarioBoot the NNP-I device after the card is powered-on or reset. When the NNP-I card comes up, it's flashed BIOS starts running.
Since the persistent storage on the device is rather small, it needs to get its OS boot image from the host driver. So once the card's BIOS is running, the host driver loads and provides a boot image to the card BIOS, which uses it to run the Embedded Linux image and SW stack on the card.
--- drivers/misc/intel-nnpi/Makefile | 3 +- drivers/misc/intel-nnpi/bootimage.c | 246 ++++++++++++++++++++++++++++++ drivers/misc/intel-nnpi/bootimage.h | 43 ++++++ drivers/misc/intel-nnpi/device.c | 291 ++++++++++++++++++++++++++++++++++++ drivers/misc/intel-nnpi/device.h | 77 ++++++++++ drivers/misc/intel-nnpi/nnp_pcie.c | 16 ++ 6 files changed, 675 insertions(+), 1 deletion(-) create mode 100644 drivers/misc/intel-nnpi/bootimage.c create mode 100644 drivers/misc/intel-nnpi/bootimage.h
drivers/misc/intel-nnpi/Makefile +2 -1
@@ -5,7 +5,8 @@ obj-$(CONFIG_INTEL_NNPI) := intel_nnpi.o intel_nnpi_pcie.o -intel_nnpi-y := device.o msg_scheduler.o hostres.o host_chardev.o nnp_user.o +intel_nnpi-y := device.o msg_scheduler.o hostres.o host_chardev.o nnp_user.o \ + bootimage.o intel_nnpi_pcie-y := nnp_pcie.o
drivers/misc/intel-nnpi/bootimage.c new file +246 -0
246 lines not on this page · raw message
drivers/misc/intel-nnpi/bootimage.h new file +43 -0
43 lines not on this page · raw message
drivers/misc/intel-nnpi/device.c +291 -0
340 lines not on this page · raw message
drivers/misc/intel-nnpi/device.h +77 -0
111 lines not on this page · raw message
drivers/misc/intel-nnpi/nnp_pcie.c +16 -0
@@ -375,9 +375,25 @@ static int nnp_cmdq_flush(struct nnp_device *nnpdev)return 0 ; } +static int nnp_set_host_doorbell_value (struct nnp_device *nnpdev,u32 value) +{ + struct nnp_pci *nnp_pci = container_of(nnpdev, struct nnp_pci, nnpdev); + +/* +* The SELF_RESET bit is set only by the h/w layer, +* do not allow higher layer to set it +*/ + value &= ~NNP_HOST_DRV_REQUEST_SELF_RESET_MASK ; + +nnp_mmio_write (nnp_pci,ELBI_PCI_HOST_DOORBELL_VALUE , value); + +return 0 ; +} +static struct nnp_device_ops nnp_device_ops = { .cmdq_flush = nnp_cmdq_flush, .cmdq_write_mesg = nnp_cmdq_write_mesg, + .set_host_doorbell_value = nnp_set_host_doorbell_value, };static void set_host_boot_state (struct nnp_pci *nnp_pci,int boot_state)
Signature · 1 line
1.8.3.1
PATCH v2 9/15 misc: nnpi: Process device response messages
In reply to Guy ZadicarioThe nnpdev_process_messages function handles all messages coming from a NNP-I device. Based on an opcode field attached to each message, it calls the correct response processing handler. The function is called from the NNP-I device driver, from a threaded interrupt handler, when responses arrive in the HW response queue.
--- drivers/misc/intel-nnpi/device.c | 136 +++++++++++++++++++++++++++++++++++++ drivers/misc/intel-nnpi/device.h | 10 +++ drivers/misc/intel-nnpi/nnp_pcie.c | 3 + 3 files changed, 149 insertions(+)
drivers/misc/intel-nnpi/device.c +136 -0
@@ -23,6 +23,142 @@ bool nnpdev_no_devices(void)return ida_is_empty (&dev_ida); } +/** +* handle_bios_protocol() - process response coming from card's BIOS. +* @nnpdev: The nnp device +* @msgbuf: pointer to response message content +* @avail_qwords: number of 64-bit units available in @msgbuf +* +* IPC protocol with card's BIOS may have different response sizes. +* @avail_qwords is the number of 64-bit units available in @msgbuf buffer. +* If the actual response size is larger then available data in the buffer, +* the function returns 0 to indicate that this is a partial response. Otherwise +* the actual response size is returned (in units of qwords). +* +* Return: 0 if @msgbuf contains a partial response otherwise the number of +* qwords of the response in @msgbuf. +*/ +static int handle_bios_protocol (struct nnp_device *nnpdev,const u64 *msgbuf, +int avail_qwords) +{ +int msg_size, msg_qwords; + + msg_size =FIELD_GET (NNP_C2H_BIOS_PROTOCOL_TYPE_MASK , msgbuf[0 ]); + +/* The +1 is because size field does not include header */ + msg_qwords =DIV_ROUND_UP (msg_size,8 ) +1 ; + +if (msg_qwords > avail_qwords) +return 0 ; + +return msg_qwords; +} + +typedef int (*response_handler )(struct nnp_device *nnpdev,const u64 *msgbuf, +int avail_qwords); + +static response_handler resp_handlers[NNP_IPC_C2H_OPCODE_LAST +1 ] = { + [NNP_IPC_C2H_OP_BIOS_PROTOCOL ] = handle_bios_protocol +}; + +/** +* nnpdev_process_messages() - process response messages from nnpi device +* @nnpdev: The nnp device +* @hw_msg: pointer to response message content +* @hw_nof_msg: number of 64-bit units available in hw_msg buffer. +* +* This function is called from the PCIe device driver when response messages +* are arrived in the HWQ. It is called in sequence, should not be re-entrant. +* The function may not block ! +*/ +void nnpdev_process_messages (struct nnp_device *nnpdev,u64 *hw_msg, +unsigned int hw_nof_msg) +{ +int j =0 ; +int msg_size; +u64 *msg; +unsigned int nof_msg; +bool fatal_protocol_error = false; + +/* ignore any response if protocol error detected */ +if ((nnpdev->state &NNP_DEVICE_PROTOCOL_ERROR ) !=0 ) +return ; + +/* +* If we have pending messages from previous round +* copy the new messages to the pending list and process +* the pending list. +* otherwise process the messages received from HW directly +*/ + msg = hw_msg; + nof_msg = hw_nof_msg; +if (nnpdev->response_num_msgs >0 ) { +/* +* Check to prevent response buffer overrun. +* This should never happen since the buffer is twice +* the size of the HW response queue. This check is +* for safety and debug purposes. +*/ +if (hw_nof_msg + nnpdev->response_num_msgs >= +NNP_DEVICE_RESPONSE_BUFFER_LEN ) { +dev_dbg (nnpdev->dev, +"device response buffer would overrun: %d + %d !!\n" , + nnpdev->response_num_msgs, hw_nof_msg); +return ; + } + +memcpy (&nnpdev->response_buf[nnpdev->response_num_msgs], hw_msg, + hw_nof_msg *sizeof (u64)); + msg = nnpdev->response_buf; + nof_msg = nnpdev->response_num_msgs + hw_nof_msg; + } + +/* +* loop for each message +*/ +do { +int op_code =FIELD_GET (NNP_C2H_OP_MASK , msg[j]); +response_handler handler; + +/* opcodes above OP_BIOS_PROTOCOL are not yet supported */ +if (op_code >NNP_IPC_C2H_OP_BIOS_PROTOCOL ) { + fatal_protocol_error = true; +break ; + } + +/* dispatch the message request */ + handler = resp_handlers[op_code]; +if (!handler) { +/* Should not happen! */ +dev_dbg (nnpdev->dev, +"Unknown response opcode received %d (0x%llx)\n" , + op_code, msg[j]); + fatal_protocol_error = true; +break ; + } + + msg_size = (*handler)(nnpdev, &msg[j], (nof_msg - j)); + + j += msg_size; + }while (j < nof_msg || !msg_size); + +if (fatal_protocol_error) + nnpdev->state |=NNP_DEVICE_PROTOCOL_ERROR ; + +/* +* If unprocessed messages left, copy it to the pending messages buffer +* for the next time this function will be called. +*/ +if (j < nof_msg) { +memcpy (&nnpdev->response_buf[0 ], &msg[j], + (nof_msg - j) *sizeof (u64)); + nnpdev->response_num_msgs = nof_msg - j; + }else { + nnpdev->response_num_msgs =0 ; + } +} +EXPORT_SYMBOL (nnpdev_process_messages); +static void send_sysinfo_request_to_bios (struct nnp_device *nnpdev) {u64 cmd[3 ];
drivers/misc/intel-nnpi/device.h +10 -0
@@ -48,6 +48,9 @@#define NNP_DEVICE_CORRUPTED_BOOT_IMAGE BIT(30)#define NNP_DEVICE_ERROR_MASK GENMASK(31, 16) +#define NNP_DEVICE_RESPONSE_FIFO_LEN 16 +#define NNP_DEVICE_RESPONSE_BUFFER_LEN (NNP_DEVICE_RESPONSE_FIFO_LEN * 2) +/** * struct nnp_device - structure for NNP-I device info * @ops: device operations implemented by the underlying device driver
@@ -61,6 +64,8 @@* @lock: protects accesses to @state * @is_recovery_bios: true if device has booted from the recovery bios flash * @boot_image_loaded: true if boot image load has started +* @response_buf: buffer of device response messages arrived from "pci" layer. +* @response_num_msgs: number of qwords available in @response_buf * @bios_system_info_dma_addr: dma page allocated for bios system info. * @bios_system_info: virtual pointer to bios system info page * @bios_version_str: the device's started bios version string
@@ -83,6 +88,9 @@ struct nnp_device {bool is_recovery_bios;bool boot_image_loaded; +u64 response_buf[NNP_DEVICE_RESPONSE_BUFFER_LEN ]; +unsigned int response_num_msgs; +dma_addr_t bios_system_info_dma_addr;struct nnp_c2h_system_info *bios_system_info;char bios_version_str[NNP_BIOS_VERSION_LEN ];
@@ -118,6 +126,8 @@ int nnpdev_init(struct nnp_device *nnpdev, struct device *dev,void nnpdev_destroy (struct nnp_device *nnpdev);void nnpdev_card_doorbell_value_changed (struct nnp_device *nnpdev,u32 doorbell_val); +void nnpdev_process_messages (struct nnp_device *nnpdev,u64 *hw_msg, +unsigned int hw_nof_msg);/* * Framework internal functions (not exported)
drivers/misc/intel-nnpi/nnp_pcie.c +3 -0
@@ -150,6 +150,9 @@ static void nnp_process_commands(struct nnp_pci *nnp_pci) response_pci_control |=FIELD_PREP (RESPQ_READ_PTR_MASK , read_pointer);nnp_mmio_write (nnp_pci,ELBI_RESPONSE_PCI_CONTROL , response_pci_control); + +nnpdev_process_messages (&nnp_pci->nnpdev, nnp_pci->response_buf, + avail_slots); }static void mask_all_interrupts (struct nnp_pci *nnp_pci)
Signature · 1 line
1.8.3.1
PATCH v2 10/15 misc: nnpi: Query and verify device protocol
In reply to Guy ZadicarioCheck that the card booted SW stack is compatible with the driver running on the host. The IPC protocol between the driver and cards's SW stack may change from time to time when releasing new versions of the card boot image.
When the card boots and signals to the host that it is booted and ready (through doorbell change interrupt), the driver sends it a "query version" command. In response, the device sends back a "query version reply" response with information of the IPC protocol version which it supports. Only when the version check passes, the device is considered booted and ready for operation.
If the version check fails, the device is put in error state. In order to recover from this error condition, a device reset is required (probably preceded by installation of a new, compatile boot image).
--- drivers/misc/intel-nnpi/device.c | 145 +++++++++++++++++++++++++++++++++++++++ drivers/misc/intel-nnpi/device.h | 17 +++++ 2 files changed, 162 insertions(+)
drivers/misc/intel-nnpi/device.c +145 -0
@@ -24,6 +24,119 @@ bool nnpdev_no_devices(void) }/** +* process_query_version_reply() - process a "query_version_reply" response +* @work: work struct of the calling work +* +* This function processes a "query_version_reply" response message from +* the card which is sent as reply to query_version command submitted +* earlier. +* The function checks that the IPC protocol version that is supported by the +* device matches the one supported by the driver. If there is no match the +* device state is put in error. +* There are two IPC protocol versions which are checked: +* 'protocol_version': is IPC protocol version of command and response messages +* That are built (for commands) and processed by this kernel mode +* driver. The protocol is defined in ipc_include/ipc_protocol.h +* A mismatch is possible in cases that the device has booted with +* a wrong/older version of the card boot image. +* 'chan_protocol_version': is IPC protocol of command and responses which are +* supported by the device but are built and processed in user-space. +* The structure of the commands and responses are mostly opaque to +* the kernel mode driver. This separation allows to update the +* device boot image and user-space library to support new sets +* of commands without changing the kernel driver. +* The restriction for such commands and responses is that the lowest +* 16-bits of the command/response are defined to include the +* command/response opcode and the channel id. +* The kernel driver should also know for each possible command and +* response opcode the size of the message. This info is received +* from the device within this "query_version_reply" response +* encoded in the chan_resp_op_size and chan_cmd_op_size fields +* of the response. +*/ +static void process_query_version_reply (struct work_struct *work) +{ +struct query_version_work *query_version_work; +struct nnp_device *nnpdev; +u32 protocol_version; +u32 card_boot_state; +u32 val; + + query_version_work = + container_of(work, struct query_version_work, work); + nnpdev = container_of(query_version_work, struct nnp_device, + query_version_work); + protocol_version =NNP_IPC_PROTOCOL_VERSION ; + card_boot_state =FIELD_GET (NNP_CARD_BOOT_STATE_MASK , + nnpdev->card_doorbell_val); + + nnpdev->protocol_version = + query_version_work->protocol_version; + nnpdev->chan_protocol_version = + query_version_work->chan_protocol_version; + +/* +* NOTE: The card firmware and host driver protocol version must +* exactly match in the major and minor version components. +* There is no backwards compatibility on the protocol! +* When a device is put in a protocol version error state, the +* user must install a matching device firmware and reset the device +* in order to allow the device to function. +*/ +if (NNP_VERSION_MAJOR (query_version_work->protocol_version) != +NNP_VERSION_MAJOR (protocol_version) || +NNP_VERSION_MINOR (query_version_work->protocol_version) != +NNP_VERSION_MINOR (protocol_version) || + query_version_work->chan_resp_op_size ==0 ) { +nnpdev_set_boot_state (nnpdev,NNP_DEVICE_FAILED_VERSION ); +/* set host driver state in doorbell register */ + val =FIELD_PREP (NNP_HOST_DRV_STATE_MASK , +NNP_HOST_DRV_STATE_VERSION_ERROR ); + nnpdev->ops->set_host_doorbell_value (nnpdev, val); + }else if (card_boot_state ==NNP_CARD_BOOT_STATE_DRV_READY ) { +nnpdev_set_boot_state (nnpdev,NNP_DEVICE_CARD_DRIVER_READY ); + }else if (card_boot_state ==NNP_CARD_BOOT_STATE_CARD_READY ) { +/* Card driver finished initialization */ +nnpdev_set_boot_state (nnpdev, +NNP_DEVICE_CARD_DRIVER_READY | +NNP_DEVICE_CARD_READY | +NNP_DEVICE_CARD_ENABLED ); + } + + query_version_work->running = false; +} + +static int handle_query_version_reply3 (struct nnp_device *nnpdev, +const u64 *msgbuf,int avail_qwords) +{ +int msg_qwords =3 ;/* QUERY_VERSION_REPLY3 response len is 3 qwords */ + +if (avail_qwords < msg_qwords) +return 0 ; + +/* +* This should not happen, but if it does, just ignore the message +* There is no fear in race condition on "running" flag as only +* single version reply message should be processed after each +* device reset. +*/ +if (nnpdev->query_version_work.running) +return msg_qwords; + + nnpdev->query_version_work.running = true; + nnpdev->query_version_work.protocol_version = +FIELD_GET (NNP_C2H_VERSION_REPLY_QW0_PROT_VER_MASK , msgbuf[0 ]); + nnpdev->query_version_work.chan_protocol_version = +FIELD_GET (NNP_C2H_VERSION_REPLY_QW0_CHAN_VER_MASK , msgbuf[0 ]); + nnpdev->query_version_work.chan_resp_op_size = msgbuf[1 ]; + nnpdev->query_version_work.chan_cmd_op_size = msgbuf[2 ]; + +queue_work (nnpdev->wq, &nnpdev->query_version_work.work); + +return msg_qwords; +} + +/** * handle_bios_protocol() - process response coming from card's BIOS. * @nnpdev: The nnp device * @msgbuf: pointer to response message content
@@ -58,6 +171,7 @@ typedef int (*response_handler)(struct nnp_device *nnpdev, const u64 *msgbuf, int avail_qwords);static response_handler resp_handlers[NNP_IPC_C2H_OPCODE_LAST +1 ] = { + [NNP_IPC_C2H_OP_QUERY_VERSION_REPLY3 ] = handle_query_version_reply3, [NNP_IPC_C2H_OP_BIOS_PROTOCOL ] = handle_bios_protocol };
@@ -329,6 +443,18 @@ void nnpdev_set_boot_state(struct nnp_device *nnpdev, u32 mask)"Unexpected error while loading boot image. rc=%d\n" , ret); } + +/* Handle transition to active state */ +if (((state &NNP_DEVICE_CARD_DRIVER_READY ) || + (state &NNP_DEVICE_CARD_READY )) && + !(prev_state &NNP_DEVICE_CARD_DRIVER_READY ) && + !(prev_state &NNP_DEVICE_CARD_READY )) { +u32 val; + +/* set host driver state to "Driver ready" */ + val =FIELD_PREP (NNP_HOST_DRV_STATE_MASK ,NNP_HOST_DRV_STATE_READY ); + nnpdev->ops->set_host_doorbell_value (nnpdev, val); + } }/**
@@ -364,6 +490,7 @@ int nnpdev_init(struct nnp_device *nnpdev, struct device *dev,*/ nnpdev->dev = dev; nnpdev->ops = ops; + nnpdev->protocol_version =0 ; nnpdev->cmdq_sched =nnp_msched_create (nnpdev);if (!nnpdev->cmdq_sched) {
@@ -397,6 +524,7 @@ int nnpdev_init(struct nnp_device *nnpdev, struct device *dev,spin_lock_init (&nnpdev->lock);nnpdev_boot_image_init (&nnpdev->boot_image); +INIT_WORK (&nnpdev->query_version_work.work, process_query_version_reply);return 0 ;
@@ -426,6 +554,7 @@ static void doorbell_changed_handler(struct work_struct *work)u32 error_state;u32 doorbell_val = req->val;struct nnp_device *nnpdev = req->nnpdev; +u64 query_cmd; nnpdev->card_doorbell_val = doorbell_val;
@@ -466,6 +595,22 @@ static void doorbell_changed_handler(struct work_struct *work)case NNP_CARD_BOOT_STATE_BIOS_FLASH_STARTED : state =NNP_DEVICE_BIOS_UPDATE_STARTED ;break ; +case NNP_CARD_BOOT_STATE_DRV_READY : +case NNP_CARD_BOOT_STATE_CARD_READY : +/* card is up - send "query_version" command */ + query_cmd =FIELD_PREP (NNP_H2C_OP_MASK , +NNP_IPC_H2C_OP_QUERY_VERSION ); +if (nnp_msched_queue_msg (nnpdev->cmdq, query_cmd) || +nnp_msched_queue_sync (nnpdev->cmdq)) +dev_err (nnpdev->dev,"Query version msg error\n" ); +break ; + +case NNP_CARD_BOOT_STATE_NOT_READY : +/* card is down reset the device boot and error state */ +spin_lock (&nnpdev->lock); + nnpdev->state =0 ; +spin_unlock (&nnpdev->lock); +break ;default :break ; }
drivers/misc/intel-nnpi/device.h +17 -0
@@ -51,6 +51,15 @@#define NNP_DEVICE_RESPONSE_FIFO_LEN 16#define NNP_DEVICE_RESPONSE_BUFFER_LEN (NNP_DEVICE_RESPONSE_FIFO_LEN * 2) +struct query_version_work { +struct work_struct work; +u64 chan_resp_op_size; +u64 chan_cmd_op_size; +u16 protocol_version; +u16 chan_protocol_version; +bool running; +}; +/** * struct nnp_device - structure for NNP-I device info * @ops: device operations implemented by the underlying device driver
@@ -71,9 +80,13 @@* @bios_version_str: the device's started bios version string * @bios_system_info_valid: true if @bios_system_info has been filled and valid * @state: current device boot state mask (see device state bits above) +* @protocol_version: version of host->card IPC protocol +* @chan_protocol_version: version of user-space->card IPC protocol * @curr_boot_state: last boot state field received from device doorbell reg * @card_doorbell_val: last received device doorbell register value. * @boot_image: boot image object used to boot the card +* @query_version_work: work struct used to schedule processing of version +* reply response message arrived from card. */ struct nnp_device {const struct nnp_device_ops *ops;
@@ -98,8 +111,12 @@ struct nnp_device {u32 state;u32 curr_boot_state; +unsigned short protocol_version; +unsigned short chan_protocol_version;u32 card_doorbell_val;struct image_info boot_image; + +struct query_version_work query_version_work; };/**
Signature · 1 line
1.8.3.1
PATCH v2 11/15 misc: nnpi: Create comm channel from app to device
In reply to Guy ZadicarioEstablish bi-directional communication channels between user-mode processes and NNP-I devices. Each command channel object ("channel") holds a queue of messages from a single user-mode connection to a single NNP-I device, as well as a ring-buffer to hold response messages from this NNP-I device back to the user-mode process.
Messages to the NNP-I device are put by the channel into a command queue. Response messages coming back from the device are being routed to the intended channel's ring-buffer, where they are consumed by this channel's user-mode connection. Routing of messages to and from the device is done based on a channel's 10-bit unique id, which is included in the messages.
The interface for consuming responses from the ring-buffer and writing command messages into the msg_scheduler's command queue will be added in a future patch. This patch only adds the channel creation code and response message routing to the targeted channel.
When creating a "command channel", the user should give an open file descriptor to the /dev/nnpi_host device. This file descriptor associates the channel with a particular "nnp_user" object. The channel can only reference host resources created by that "user".
--- drivers/misc/intel-nnpi/Makefile | 2 +- drivers/misc/intel-nnpi/cmd_chan.c | 314 +++++++++++++++++++++ drivers/misc/intel-nnpi/cmd_chan.h | 72 +++++ drivers/misc/intel-nnpi/device.c | 113 +++++++- drivers/misc/intel-nnpi/device.h | 17 +- drivers/misc/intel-nnpi/ipc_include/ipc_protocol.h | 3 + 6 files changed, 516 insertions(+), 5 deletions(-) create mode 100644 drivers/misc/intel-nnpi/cmd_chan.c create mode 100644 drivers/misc/intel-nnpi/cmd_chan.h
drivers/misc/intel-nnpi/Makefile +1 -1
@@ -6,7 +6,7 @@ obj-$(CONFIG_INTEL_NNPI) := intel_nnpi.o intel_nnpi_pcie.o intel_nnpi-y := device.o msg_scheduler.o hostres.o host_chardev.o nnp_user.o \ - bootimage.o + bootimage.o cmd_chan.o intel_nnpi_pcie-y := nnp_pcie.o
drivers/misc/intel-nnpi/cmd_chan.c new file +314 -0
314 lines not on this page · raw message
drivers/misc/intel-nnpi/cmd_chan.h new file +72 -0
72 lines not on this page · raw message
drivers/misc/intel-nnpi/device.c +110 -3
166 lines not on this page · raw message
drivers/misc/intel-nnpi/device.h +16 -1
@@ -4,6 +4,8 @@#ifndef _NNPDRV_DEVICE_H#define _NNPDRV_DEVICE_H +#include <linux/hashtable.h> +#include <linux/idr.h> #include <linux/spinlock.h> #include <linux/workqueue.h>
@@ -70,11 +72,13 @@ struct query_version_work {* @cmdq: input queue to @cmdq_sched used to schedule driver internal commands * to be sent to the device. * @wq: singlethread workqueue for processing device's response messages. -* @lock: protects accesses to @state +* @lock: protects accesses to @state and @cmd_chan_hash * @is_recovery_bios: true if device has booted from the recovery bios flash * @boot_image_loaded: true if boot image load has started * @response_buf: buffer of device response messages arrived from "pci" layer. * @response_num_msgs: number of qwords available in @response_buf +* @cmd_chan_ida: allocate channel ids to be used in ipc protocol. +* @cmd_chan_hash: maps command channel id to its struct pointer. * @bios_system_info_dma_addr: dma page allocated for bios system info. * @bios_system_info: virtual pointer to bios system info page * @bios_version_str: the device's started bios version string
@@ -87,6 +91,9 @@ struct query_version_work {* @boot_image: boot image object used to boot the card * @query_version_work: work struct used to schedule processing of version * reply response message arrived from card. +* @ipc_chan_resp_op_size: holds response size for each possible channel +* response. +* @ipc_chan_cmd_op_size: holds command size for each possible channel command. */ struct nnp_device {const struct nnp_device_ops *ops;
@@ -104,6 +111,9 @@ struct nnp_device {u64 response_buf[NNP_DEVICE_RESPONSE_BUFFER_LEN ];unsigned int response_num_msgs; +struct ida cmd_chan_ida; +DECLARE_HASHTABLE (cmd_chan_hash,6 ); +dma_addr_t bios_system_info_dma_addr;struct nnp_c2h_system_info *bios_system_info;char bios_version_str[NNP_BIOS_VERSION_LEN ];
@@ -117,6 +127,9 @@ struct nnp_device {struct image_info boot_image;struct query_version_work query_version_work; + +u8 ipc_chan_resp_op_size[NNP_IPC_NUM_USER_OPS ]; +u8 ipc_chan_cmd_op_size[NNP_IPC_NUM_USER_OPS ]; };/**
@@ -151,4 +164,6 @@ void nnpdev_process_messages(struct nnp_device *nnpdev, u64 *hw_msg,*/ void nnpdev_set_boot_state (struct nnp_device *nnpdev,u32 mask); +struct nnp_chan *nnpdev_find_channel (struct nnp_device *nnpdev,u16 chan_id); +#endif
drivers/misc/intel-nnpi/ipc_include/ipc_protocol.h +3 -0
@@ -11,6 +11,9 @@#define IPC_OP_MAX BIT(6)#define NNP_IPC_OPCODE_MASK GENMASK(5, 0) +#define NNP_IPC_MIN_USER_OP 32 +#define NNP_IPC_MAX_USER_OP 63 +#define NNP_IPC_NUM_USER_OPS (NNP_IPC_MAX_USER_OP - NNP_IPC_MIN_USER_OP + 1)#define NNP_MSG_SIZE (msg) (sizeof(msg) / sizeof(__le64))
Signature · 1 line
1.8.3.1
PATCH v2 12/15 misc: nnpi: Route device response messages
In reply to Guy ZadicarioRoute both types of messages coming from the NNP-I card - event report messages, which are handled by the driver, and command-response messages, which should be routed to a specific command channel object ("channel").
Event report messages are device-level messages which are not associated with a specific channel. They are typically initiated by the NNP-I card, to indicate an error, status change, or any event which is not a response to a message sent from a spcific channel. These event report messages are handled by the driver.
In contrast, command-response messages are associated with a specific channel, and are typically sent from the NNP-I card in response to a message sent from a channel to the card. These command-response messages are added to the intended channel ring-buffer for consumption by this channel.
The list of messages, of both types, coming from the card, is defined in ipc_include/ipc_c2h_events.h included in this patch.
--- drivers/misc/intel-nnpi/cmd_chan.h | 6 + drivers/misc/intel-nnpi/device.c | 104 +++++++++++ drivers/misc/intel-nnpi/device.h | 2 + .../misc/intel-nnpi/ipc_include/ipc_c2h_events.h | 198 +++++++++++++++++++++ 4 files changed, 310 insertions(+) create mode 100644 drivers/misc/intel-nnpi/ipc_include/ipc_c2h_events.h
drivers/misc/intel-nnpi/cmd_chan.h +6 -0
@@ -4,6 +4,7 @@#ifndef NNPDRV_CMD_CHAN_H #define NNPDRV_CMD_CHAN_H +#include <linux/bitfield.h> #include <linux/circ_buf.h> #include <linux/hashtable.h> #include <linux/kref.h>
@@ -12,6 +13,7 @@#include <linux/spinlock.h> #include "device.h" +#include "ipc_c2h_events.h" /** * struct nnp_chan - structure object for user<->device communication
@@ -21,6 +23,7 @@* @chan_id: the ipc channel id for this channel * @hash_node: node to include this object in list of channels * hash is in (cmd_chan_hash in nnp_device). +* @card_critical_error_msg: last critical event report received from device * @get_device_events: true if device-level events received from card should * be sent over this channel to user. * @cmdq: message queue added to msg_scheduler, for user commands to be sent
@@ -43,6 +46,7 @@ struct nnp_chan {struct nnp_device *nnpdev;u16 chan_id;struct hlist_node hash_node; +u64 card_critical_error_msg;bool get_device_events;struct nnp_msched_queue *cmdq;
@@ -59,6 +63,8 @@ struct nnp_chan {unsigned int resp_lost; }; +#define chan_broken (chan) FIELD_GET(NNP_C2H_EVENT_REPORT_CODE_MASK, (chan)->card_critical_error_msg) +struct nnp_chan *nnpdev_chan_create (struct nnp_device *nnpdev,int host_fd,unsigned int min_id,unsigned int max_id,bool get_device_events);
drivers/misc/intel-nnpi/device.c +104 -0
127 lines not on this page · raw message
drivers/misc/intel-nnpi/device.h +2 -0
@@ -79,6 +79,7 @@ struct query_version_work {* @response_num_msgs: number of qwords available in @response_buf * @cmd_chan_ida: allocate channel ids to be used in ipc protocol. * @cmd_chan_hash: maps command channel id to its struct pointer. +* @waitq: used to wait for device response messages * @bios_system_info_dma_addr: dma page allocated for bios system info. * @bios_system_info: virtual pointer to bios system info page * @bios_version_str: the device's started bios version string
@@ -113,6 +114,7 @@ struct nnp_device {struct ida cmd_chan_ida;DECLARE_HASHTABLE (cmd_chan_hash,6 ); +wait_queue_head_t waitq;dma_addr_t bios_system_info_dma_addr;struct nnp_c2h_system_info *bios_system_info;
drivers/misc/intel-nnpi/ipc_include/ipc_c2h_events.h new file +198 -0
198 lines not on this page · raw message
Signature · 1 line
1.8.3.1
PATCH v2 13/15 misc: nnpi: Expose command channel file interface
In reply to Guy ZadicarioExpose an anon file descriptor interface to a command channel object which allows user-space to send commands to the device by writing to that file as well as consume device response messages by reading the file.
When the file is released (closed), a channel shut-down sequence starts. First, a message is sent to the device notifying it that the channel is closing. Once the response to this message is received from the device, the command channel object is destroyed.
--- drivers/misc/intel-nnpi/cmd_chan.c | 362 +++++++++++++++++++++++++++++++++++++ drivers/misc/intel-nnpi/cmd_chan.h | 22 ++- drivers/misc/intel-nnpi/device.c | 71 ++++++++ 3 files changed, 454 insertions(+), 1 deletion(-)
drivers/misc/intel-nnpi/cmd_chan.c +362 -0
@@ -4,13 +4,16 @@#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt#include <linux/anon_inodes.h> +#include <linux/bitfield.h> #include <linux/dev_printk.h> #include <linux/file.h> #include <linux/minmax.h> +#include <linux/poll.h> #include <linux/slab.h> #include "cmd_chan.h" #include "host_chardev.h" +#include "ipc_c2h_events.h" #include "ipc_protocol.h" #include "nnp_user.h"
@@ -52,6 +55,258 @@ static inline void respq_pop(struct nnp_chan *chan, void *buf, int count) chan->respq.tail = (chan->respq.tail + count) & (chan->respq_size -1 ); } +static inline void respq_unpop (struct nnp_chan *chan,int count) +{ + chan->respq.tail = (chan->respq.tail - count) & (chan->respq_size -1 ); +} + +enum respq_state { +RESPQ_EMPTY =0 , +RESPQ_MSG_AVAIL , +RESPQ_DISCONNECTED +}; + +/** +* respq_state() - check if a response message is available to be popped +* @chan: the cmd_chan object +* +* Checks if new response message is available or channel has been destroyed. +* +* Return: +* * RESPQ_EMPTY - response queue is empty +* * RESPQ_MSG_AVAIL - a response message is available in the queue +* * RESPQ_DISCONNECTED - the channel in a destroyed state +*/ +static enum respq_state respq_state (struct nnp_chan *chan) +{ +bool ret; + +mutex_lock (&chan->dev_mutex); +if (chan->state ==NNP_CHAN_DESTROYED ) { +mutex_unlock (&chan->dev_mutex); +return RESPQ_DISCONNECTED ; + } + +spin_lock (&chan->respq_lock); +/* +* response messages are pushed into the respq ring-buffer by pushing +* the size of the message (as u32) followed by message content. +* So an entire message is available only if more than sizeof(u32) +* bytes are available (there is no message with zero size). +*/ +if (CIRC_CNT (chan->respq.head, chan->respq.tail, chan->respq_size) > +sizeof (u32)) + ret =RESPQ_MSG_AVAIL ; +else + ret =RESPQ_EMPTY ; +spin_unlock (&chan->respq_lock); + +mutex_unlock (&chan->dev_mutex); + +return ret; +} + +static inline int is_cmd_chan_file (struct file *f); + +static int cmd_chan_file_release (struct inode *inode,struct file *f) +{ +struct nnp_chan *chan = f->private_data; +struct file *host_file; + +if (!is_cmd_chan_file (f)) +return -EINVAL ; + +nnp_chan_send_destroy (chan); + + host_file = chan->host_file; +nnp_chan_put (chan); +fput (host_file); + +return 0 ; +} + +/** +* cmd_chan_file_read() - reads a single response message arrived from device +* @f: cmd_chan file descriptor +* @buf: buffer to receive the message +* @size: size of buf, must be at least 16 qwords (16 * sizeof(u64)) +* @off: ignored. +* +* This function will block and wait until interrupted or a response +* message from device is available. +* When message(s) are available, it reads a single message, copy it to +* @buf and returns the message size. +* The given @buf and @size must be large enough to receive the largest +* possible response which is 16 qwords, otherwise -EINVAL is returned. +* The function returns the size of the received message, a return value +* of zero means that corrupted message has been detected and no more reads +* can be made from this channel. +* +* Return: if positive, the size in bytes of the read message. +* zero, if a corrupted message has been detected. +* error code otherwise +*/ +static ssize_t cmd_chan_file_read (struct file *f,char __user *buf,size_t size, +loff_t *off) +{ +struct nnp_chan *chan = f->private_data; +u64 msg[NNP_DEVICE_RESPONSE_FIFO_LEN ]; +enum respq_state state; +u32 msg_size; +int ret; + +if (!is_cmd_chan_file (f)) +return -EINVAL ; + +if (size <sizeof (msg)) +return -EINVAL ; + +/* +* wait for response message to be available, interrupted or channel +* has been destroyed on us. +*/ + ret =wait_event_interruptible (chan->resp_waitq, + (state =respq_state (chan)) !=RESPQ_EMPTY ); +if (ret <0 ) +return ret; + +if (state ==RESPQ_DISCONNECTED ) +return -EPIPE ; + +spin_lock (&chan->respq_lock); +respq_pop (chan, &msg_size,sizeof (msg_size)); +/* +* Check msg_size does not overrun msg size. +* This will never happen unless the response ring buffer got +* corrupted in some way. +* We detect it here for safety and return zero +*/ +if (msg_size >sizeof (msg)) { +/* +* unpop the bad size to let subsequent read attempts +* to fail as well. +*/ +respq_unpop (chan,sizeof (msg_size)); +spin_unlock (&chan->respq_lock); +return 0 ; + } +respq_pop (chan, msg, msg_size); +spin_unlock (&chan->respq_lock); + +if (copy_to_user (buf, msg, msg_size)) +return -EFAULT ; + +return (ssize_t )msg_size; +} + +/** +* cmd_chan_file_write() - schedule a command message to be sent to the device. +* @f: a cmd_chan file descriptor +* @buf: the command message content +* @size: size in bytes of the message, must be multiple of 8 and not larger +* than 3 qwords. +* @off: ignored +* +* This function reads a command message from buffer and puts it in the +* channel's message queue to schedule it to be delivered to the device. +* The function returns when the message is copied to the message scheduler +* queue without waiting for it to be sent out. +* A valid command message size must be qword aligned and not larger than +* the maximum size the message scheduler support, which is 3 qwords. +* +* The function also validate the command content and fail if the chan_id +* field of the command header does not belong to the same channel of this +* file descriptor, or the command opcode is out of range, or the command +* size does not fit the size of this opcode. +* +* Return: the size of the message written or error code. +*/ +static ssize_t cmd_chan_file_write (struct file *f,const char __user *buf, +size_t size,loff_t *off) +{ +struct nnp_chan *chan = f->private_data; +u64 msg[MSG_SCHED_MAX_MSG_SIZE ]; +unsigned int chan_id, opcode; +unsigned int op; +int rc =0 ; + +if (!is_cmd_chan_file (f)) +return -EINVAL ; + +/* +* size must be positive, multiple of 8 bytes and +* cannot exceed maximum message size +*/ +if (!size || size >sizeof (msg) || (size &0x7 ) !=0 ) +return -EINVAL ; + +if (copy_from_user (msg, buf, size)) +return -EFAULT ; + +/* +* Check chan_id, opcode and message size are valid +*/ + opcode =FIELD_GET (NNP_H2C_CHAN_MSG_OP_MASK , msg[0 ]); + chan_id =FIELD_GET (NNP_H2C_CHAN_MSG_CHAN_ID_MASK , msg[0 ]); +if (chan_id != chan->chan_id) +return -EINVAL ; +if (opcode <NNP_IPC_MIN_USER_OP ) +return -EINVAL ; + op = opcode -NNP_IPC_MIN_USER_OP ; + +mutex_lock (&chan->dev_mutex); +if (!chan->nnpdev) { +/* The device was removed */ +mutex_unlock (&chan->dev_mutex); +return -EPIPE ; + } +if (size != chan->nnpdev->ipc_chan_cmd_op_size[op] *8 ) { +mutex_unlock (&chan->dev_mutex); +return -EINVAL ; + } + +if (!is_card_fatal_drv_event (chan_broken (chan))) + rc =nnp_msched_queue_add_msg (chan->cmdq, msg, size /8 ); +mutex_unlock (&chan->dev_mutex); + +if (rc <0 ) +return rc; + +return size; +} + +static unsigned int cmd_chan_file_poll (struct file *f,struct poll_table_struct *pt) +{ +struct nnp_chan *chan = f->private_data; +unsigned int mask =POLLOUT |POLLWRNORM ; +enum respq_state state; + +if (!is_cmd_chan_file (f)) +return 0 ; + +poll_wait (f, &chan->resp_waitq, pt); + state =respq_state (chan); +if (state !=RESPQ_EMPTY ) + mask |=POLLIN |POLLRDNORM ; +if (state ==RESPQ_DISCONNECTED ) + mask |=POLLHUP ; + +return mask; +} + +static const struct file_operations nnp_chan_fops = { + .owner =THIS_MODULE , + .release = cmd_chan_file_release, + .read = cmd_chan_file_read, + .write = cmd_chan_file_write, + .poll = cmd_chan_file_poll, +}; + +static inline int is_cmd_chan_file (struct file *f) +{ +return f->f_op == &nnp_chan_fops; +} +/** * nnpdev_chan_create() - creates a command channel object * @nnpdev: the device
@@ -119,6 +374,7 @@ struct nnp_chan *nnpdev_chan_create(struct nnp_device *nnpdev, int host_fd,kref_init (&cmd_chan->ref); cmd_chan->chan_id = chan_id; cmd_chan->nnpdev = nnpdev; + cmd_chan->fd =-1 ; cmd_chan->get_device_events = get_device_events; cmd_chan->nnp_user = cmd_chan->host_file->private_data;
@@ -154,6 +410,17 @@ static void nnp_chan_release(struct kref *kref)nnp_chan_disconnect (cmd_chan); +/* +* If a chan file was created (through nnp_chan_create_file), +* the host_file was already put when the file has released, otherwise +* we put it here. +* This is because we want to release host_file once the channel file +* has been closed even though the channel object may continue to exist +* until the card will send a respond that it was destroyed. +*/ +if (cmd_chan->fd <0 ) +fput (cmd_chan->host_file); +nnp_user_put (cmd_chan->nnp_user);kfree (cmd_chan->respq_buf);
@@ -170,6 +437,99 @@ void nnp_chan_put(struct nnp_chan *cmd_chan)kref_put (&cmd_chan->ref, nnp_chan_release); } +int nnp_chan_create_file (struct nnp_chan *cmd_chan) +{ +/* +* get refcount to the channel that will drop when +* the file is released. +*/ +nnp_chan_get (cmd_chan); + + cmd_chan->fd =anon_inode_getfd ("nnpi_chan" , &nnp_chan_fops, cmd_chan, +O_RDWR |O_CLOEXEC ); +if (cmd_chan->fd <0 ) +nnp_chan_put (cmd_chan); + +return cmd_chan->fd; +} + +/** +* nnp_chan_set_destroyed() - atomically mark the channel "destroyed" +* @chan: the cmd_chan +* +* This function sets the command channel state to "destroyed" and returns +* the previous destroyed state. +* This function should be called once the channel has been destructed on the +* device and a "channel destroyed" response message arrived. +* +* Return: true if the channel was already marked destroyed. +*/ +bool nnp_chan_set_destroyed (struct nnp_chan *chan) +{ +bool ret; + +mutex_lock (&chan->dev_mutex); + ret = (chan->state ==NNP_CHAN_DESTROYED ); + chan->state =NNP_CHAN_DESTROYED ; +mutex_unlock (&chan->dev_mutex); + +wake_up_all (&chan->resp_waitq); + +return ret; +} + +/** +* nnp_chan_send_destroy() - sends a "destroy channel" command to device +* @chan: the cmd_chan to destroy. +* +* This function sends a command to the device to destroy a command channel, +* The channel object remains to exist, it will be dropped only when the device +* send back a "channel destroyed" response message. +* In case the device is in critical error state, we treat it as not +* functional, and the function will immediately drop the channel object without +* sending any command and will return with success. +* +* Return: 0 on success, error value otherwise. +*/ +int nnp_chan_send_destroy (struct nnp_chan *chan) +{ +u64 cmd; +int ret =0 ; +bool do_put = false; + +mutex_lock (&chan->dev_mutex); +if (chan->state ==NNP_CHAN_DESTROYED || !chan->nnpdev) + goto done; + + cmd =FIELD_PREP (NNP_H2C_OP_MASK ,NNP_IPC_H2C_OP_CHANNEL_OP ); + cmd |=FIELD_PREP (NNP_H2C_CHANNEL_OP_CHAN_ID_MASK , chan->chan_id); + cmd |=FIELD_PREP (NNP_H2C_CHANNEL_OP_DESTROY_MASK ,1 ); + + chan->event_msg =0 ; + +/* +* If card is in critical state (or was during the channel lifetime) +* we destroy the channel. +* otherwise, we send a destroy command to card and will destroy when +* the destroy reply arrives. +*/ +if (is_card_fatal_drv_event (chan_broken (chan))) { + chan->state =NNP_CHAN_DESTROYED ; + do_put = true; + goto done; + } + + ret =nnp_msched_queue_msg (chan->cmdq, cmd); + +done: +mutex_unlock (&chan->dev_mutex); +if (do_put) { +wake_up_all (&chan->resp_waitq); +nnp_chan_put (chan); + } +return ret; +} +/** * nnp_chan_disconnect() - disconnect the channel from the NNP-I device object * @cmd_chan: the command channel object
@@ -194,8 +554,10 @@ void nnp_chan_disconnect(struct nnp_chan *cmd_chan)spin_lock (&nnpdev->lock);hash_del (&cmd_chan->hash_node);spin_unlock (&nnpdev->lock); + cmd_chan->state =NNP_CHAN_DESTROYED ;mutex_unlock (&cmd_chan->dev_mutex); +wake_up_all (&cmd_chan->resp_waitq);nnp_msched_queue_sync (cmd_chan->cmdq);nnp_msched_queue_destroy (cmd_chan->cmdq);
drivers/misc/intel-nnpi/cmd_chan.h +21 -1
@@ -16,6 +16,16 @@#include "ipc_c2h_events.h" /** +* enum nnp_chan_state - indicate special state of a command channel +* @NNP_CHAN_NORMAL: channel is in normal state. +* @NNP_CHAN_DESTROYED: channel should be treated as no-longer-exist on card. +*/ +enum nnp_chan_state { +NNP_CHAN_NORMAL =0 , +NNP_CHAN_DESTROYED , +}; + +/** * struct nnp_chan - structure object for user<->device communication * @ref: refcount for this object * @nnpdev: the device this channel is connected to. May be NULL after device
@@ -23,9 +33,11 @@* @chan_id: the ipc channel id for this channel * @hash_node: node to include this object in list of channels * hash is in (cmd_chan_hash in nnp_device). +* @event_msg: ipc event response received from device during create channel * @card_critical_error_msg: last critical event report received from device * @get_device_events: true if device-level events received from card should * be sent over this channel to user. +* @fd: file descriptor created for the channel (implements read/write) * @cmdq: message queue added to msg_scheduler, for user commands to be sent * to the device. * @host_file: reference to opened "/dev/nnpi_host" object which defines the
@@ -33,7 +45,8 @@* @nnp_user: the nnp_user this channel belongs to. * the channel can reference host resources created by this * nnp_user object. -* @dev_mutex: protects @nnpdev +* @dev_mutex: protects @nnpdev and @state +* @state: the current state of this channel. * @resp_waitq: waitqueue used for waiting for response messages be available. * @respq: circular buffer object that receive response messages from device. * @respq_lock: protects @respq
@@ -46,15 +59,18 @@ struct nnp_chan {struct nnp_device *nnpdev;u16 chan_id;struct hlist_node hash_node; +u64 event_msg;u64 card_critical_error_msg;bool get_device_events; +int fd;struct nnp_msched_queue *cmdq;struct file *host_file;struct nnp_user_info *nnp_user;struct mutex dev_mutex;wait_queue_head_t resp_waitq; +enum nnp_chan_state state;struct circ_buf respq;spinlock_t respq_lock;
@@ -73,6 +89,10 @@ struct nnp_chan *nnpdev_chan_create(struct nnp_device *nnpdev, int host_fd,void nnp_chan_put (struct nnp_chan *cmd_chan);void nnp_chan_disconnect (struct nnp_chan *cmd_chan); +int nnp_chan_create_file (struct nnp_chan *cmd_chan); +int nnp_chan_send_destroy (struct nnp_chan *chan); +bool nnp_chan_set_destroyed (struct nnp_chan *chan); +int nnp_chan_add_response (struct nnp_chan *cmd_chan,u64 *hw_msg,u32 size);#endif
drivers/misc/intel-nnpi/device.c +71 -0
83 lines not on this page · raw message
Signature · 1 line
1.8.3.1
PATCH v2 14/15 misc: nnpi: Create command channel from userspace
In reply to Guy ZadicarioExpose a character device for each NNP-I device (/dev/nnpi%d) with IOCTL interface. Using this character device, user-space can create a command channel object, through which it can send and receive messages to and from the device.
--- drivers/misc/intel-nnpi/Makefile | 2 +- drivers/misc/intel-nnpi/cmd_chan.c | 11 + drivers/misc/intel-nnpi/cmd_chan.h | 1 + drivers/misc/intel-nnpi/device.c | 50 ++++- drivers/misc/intel-nnpi/device.h | 11 + drivers/misc/intel-nnpi/device_chardev.c | 348 +++++++++++++++++++++++++++++++ drivers/misc/intel-nnpi/device_chardev.h | 14 ++ include/uapi/misc/intel_nnpi.h | 43 ++++ 8 files changed, 478 insertions(+), 2 deletions(-) create mode 100644 drivers/misc/intel-nnpi/device_chardev.c create mode 100644 drivers/misc/intel-nnpi/device_chardev.h
drivers/misc/intel-nnpi/Makefile +1 -1
@@ -6,7 +6,7 @@ obj-$(CONFIG_INTEL_NNPI) := intel_nnpi.o intel_nnpi_pcie.o intel_nnpi-y := device.o msg_scheduler.o hostres.o host_chardev.o nnp_user.o \ - bootimage.o cmd_chan.o + bootimage.o cmd_chan.o device_chardev.o intel_nnpi_pcie-y := nnp_pcie.o
drivers/misc/intel-nnpi/cmd_chan.c +11 -0
@@ -557,6 +557,17 @@ void nnp_chan_disconnect(struct nnp_chan *cmd_chan) cmd_chan->state =NNP_CHAN_DESTROYED ;mutex_unlock (&cmd_chan->dev_mutex); +/* +* If the channel is not in critical state, +* put it in critical state and wake any user +* which might wait for the device. +*/ +if (!chan_drv_fatal (cmd_chan)) { + cmd_chan->card_critical_error_msg =FIELD_PREP (NNP_C2H_EVENT_REPORT_CODE_MASK , +NNP_IPC_ERROR_CHANNEL_KILLED ); +wake_up_all (&nnpdev->waitq); + } +wake_up_all (&cmd_chan->resp_waitq);nnp_msched_queue_sync (cmd_chan->cmdq);nnp_msched_queue_destroy (cmd_chan->cmdq);
drivers/misc/intel-nnpi/cmd_chan.h +1 -0
@@ -80,6 +80,7 @@ struct nnp_chan { };#define chan_broken (chan) FIELD_GET(NNP_C2H_EVENT_REPORT_CODE_MASK, (chan)->card_critical_error_msg) +#define chan_drv_fatal (chan) (is_card_fatal_drv_event(chan_broken(chan))) struct nnp_chan *nnpdev_chan_create(struct nnp_device *nnpdev, int host_fd, unsigned int min_id, unsigned int max_id,
drivers/misc/intel-nnpi/device.c +49 -1
98 lines not on this page · raw message
drivers/misc/intel-nnpi/device.h +11 -0
@@ -4,8 +4,10 @@#ifndef _NNPDRV_DEVICE_H#define _NNPDRV_DEVICE_H +#include <linux/cdev.h> #include <linux/hashtable.h> #include <linux/idr.h> +#include <linux/list.h> #include <linux/spinlock.h> #include <linux/workqueue.h>
@@ -53,6 +55,9 @@#define NNP_DEVICE_RESPONSE_FIFO_LEN 16#define NNP_DEVICE_RESPONSE_BUFFER_LEN (NNP_DEVICE_RESPONSE_FIFO_LEN * 2) +#define NNP_MAX_CHANNEL_ID 1023/* has 10 bits in ipc protocol */ +#define NNP_MAX_INF_CONTEXT_CHANNEL_ID 255/* [0, 255] are reserved for inference contexts */ +struct query_version_work {struct work_struct work;u64 chan_resp_op_size;
@@ -92,6 +97,9 @@ struct query_version_work {* @boot_image: boot image object used to boot the card * @query_version_work: work struct used to schedule processing of version * reply response message arrived from card. +* @cdev: cdev object of NNP-I device char dev. +* @chardev: character device for this device +* @cdev_clients: list of opened struct file to the chardev of this device. * @ipc_chan_resp_op_size: holds response size for each possible channel * response. * @ipc_chan_cmd_op_size: holds command size for each possible channel command.
@@ -130,6 +138,9 @@ struct nnp_device {struct query_version_work query_version_work; +struct cdev cdev; +struct device *chardev; +struct list_head cdev_clients;u8 ipc_chan_resp_op_size[NNP_IPC_NUM_USER_OPS ];u8 ipc_chan_cmd_op_size[NNP_IPC_NUM_USER_OPS ]; };
drivers/misc/intel-nnpi/device_chardev.c new file +348 -0
348 lines not on this page · raw message
drivers/misc/intel-nnpi/device_chardev.h new file +14 -0
@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* Copyright (C) 2019-2021 Intel Corporation */ + +#ifndef _NNPDRV_DEVICE_CHARDEV_H +#define _NNPDRV_DEVICE_CHARDEV_H + +#include "device.h" + +int nnpdev_cdev_create (struct nnp_device *nnpdev); +void nnpdev_cdev_destroy (struct nnp_device *nnpdev); +int nnpdev_cdev_class_init (void ); +void nnpdev_cdev_class_cleanup (void ); + +#endif
include/uapi/misc/intel_nnpi.h +43 -0
49 lines not on this page · raw message
Signature · 1 line
1.8.3.1
PATCH v2 15/15 misc: nnpi: Map host resources to device channel
In reply to Guy ZadicarioProvide an IOCTL interface for mapping and unmapping host resources to a channel, through the device's /dev/nnpi%d char device. The mapping gets a uniqueue ID and the page list of the host resource is transferred to the device. Later, commands to the device can reference the resource by the channel ID and map ID.
There is a special interface to map host resources which serve as host-to-card and card-to-host ring buffers. These ring buffers can be referenced later by the ring-buffer direction and index, rather than by a map ID.
--- drivers/misc/intel-nnpi/cmd_chan.c | 103 ++++++++ drivers/misc/intel-nnpi/cmd_chan.h | 37 ++- drivers/misc/intel-nnpi/device.c | 58 +++- drivers/misc/intel-nnpi/device_chardev.c | 441 +++++++++++++++++++++++++++++++ include/uapi/misc/intel_nnpi.h | 111 ++++++++ 5 files changed, 748 insertions(+), 2 deletions(-)
drivers/misc/intel-nnpi/cmd_chan.c +103 -0
@@ -383,6 +383,9 @@ struct nnp_chan *nnpdev_chan_create(struct nnp_device *nnpdev, int host_fd,init_waitqueue_head (&cmd_chan->resp_waitq);mutex_init (&cmd_chan->dev_mutex); +ida_init (&cmd_chan->hostres_map_ida); +hash_init (cmd_chan->hostres_hash); +/* * Add channel to the channel hash */
@@ -411,6 +414,11 @@ static void nnp_chan_release(struct kref *kref)nnp_chan_disconnect (cmd_chan);/* +* cmd_chan->hostres_map_ida is empty at this point, +* calling ida_destroy is not needed +*/ + +/* * If a chan file was created (through nnp_chan_create_file), * the host_file was already put when the file has released, otherwise * we put it here.
@@ -542,6 +550,9 @@ int nnp_chan_send_destroy(struct nnp_chan *chan)void nnp_chan_disconnect (struct nnp_chan *cmd_chan) {struct nnp_device *nnpdev; +struct chan_hostres_map *hostres_map; +struct hlist_node *tmp; +int i;mutex_lock (&cmd_chan->dev_mutex);if (!cmd_chan->nnpdev) {
@@ -573,6 +584,32 @@ void nnp_chan_disconnect(struct nnp_chan *cmd_chan)nnp_msched_queue_destroy (cmd_chan->cmdq);ida_simple_remove (&nnpdev->cmd_chan_ida, cmd_chan->chan_id); + +/* +* Unmap ring buffers +*/ +for (i =0 ; i <NNP_IPC_MAX_CHANNEL_RB ; i++) { +if (cmd_chan->h2c_rb_hostres_map[i]) { +nnp_hostres_unmap_device (cmd_chan->h2c_rb_hostres_map[i]); + cmd_chan->h2c_rb_hostres_map[i] =NULL ; + } +if (cmd_chan->c2h_rb_hostres_map[i]) { +nnp_hostres_unmap_device (cmd_chan->c2h_rb_hostres_map[i]); + cmd_chan->c2h_rb_hostres_map[i] =NULL ; + } + } + +/* +* Destroy all host resource maps +*/ +mutex_lock (&cmd_chan->dev_mutex); +hash_for_each_safe (cmd_chan->hostres_hash, i, tmp, hostres_map, hash_node) { +hash_del (&hostres_map->hash_node); +ida_simple_remove (&cmd_chan->hostres_map_ida, hostres_map->id); +nnp_hostres_unmap_device (hostres_map->hostres_map); +kfree (hostres_map); + } +mutex_unlock (&cmd_chan->dev_mutex); }static int resize_respq (struct nnp_chan *cmd_chan)
@@ -685,3 +722,69 @@ int nnp_chan_add_response(struct nnp_chan *cmd_chan, u64 *hw_msg, u32 size)return ret; } + +int nnp_chan_set_ringbuf (struct nnp_chan *chan,bool h2c,unsigned int id, +struct nnpdev_mapping *hostres_map) +{ +if (id >=NNP_IPC_MAX_CHANNEL_RB ) +return -EINVAL ; + +mutex_lock (&chan->dev_mutex); +if (h2c) { +if (chan->h2c_rb_hostres_map[id]) +nnp_hostres_unmap_device (chan->h2c_rb_hostres_map[id]); + chan->h2c_rb_hostres_map[id] = hostres_map; + }else { +if (chan->c2h_rb_hostres_map[id]) +nnp_hostres_unmap_device (chan->c2h_rb_hostres_map[id]); + chan->c2h_rb_hostres_map[id] = hostres_map; + } +mutex_unlock (&chan->dev_mutex); + +return 0 ; +} + +static struct chan_hostres_map *find_map (struct nnp_chan *chan,unsigned int map_id) +{ +struct chan_hostres_map *hostres_map; + +lockdep_assert_held (&chan->dev_mutex); + +hash_for_each_possible (chan->hostres_hash, hostres_map, hash_node, + map_id) +if (hostres_map->id == map_id) +return hostres_map; + +return NULL ; +} + +struct chan_hostres_map *nnp_chan_find_map (struct nnp_chan *chan,unsigned int map_id) +{ +struct chan_hostres_map *map; + +mutex_lock (&chan->dev_mutex); + map =find_map (chan, map_id); +mutex_unlock (&chan->dev_mutex); + +return map; +} + +int nnp_chan_unmap_hostres (struct nnp_chan *chan,unsigned int map_id) +{ +struct chan_hostres_map *hostres_map; + +mutex_lock (&chan->dev_mutex); + hostres_map =find_map (chan, map_id); +if (!hostres_map) { +mutex_unlock (&chan->dev_mutex); +return -ENXIO ; + } +hash_del (&hostres_map->hash_node); +ida_simple_remove (&chan->hostres_map_ida, hostres_map->id); +nnp_hostres_unmap_device (hostres_map->hostres_map); +mutex_unlock (&chan->dev_mutex); + +kfree (hostres_map); + +return 0 ; +}
drivers/misc/intel-nnpi/cmd_chan.h +36 -1
@@ -19,10 +19,13 @@* enum nnp_chan_state - indicate special state of a command channel * @NNP_CHAN_NORMAL: channel is in normal state. * @NNP_CHAN_DESTROYED: channel should be treated as no-longer-exist on card. +* @NNP_CHAN_RB_OP_IN_FLIGHT: a ring-buffer create or destroy command has been +* sent to device and response did not yet arrived. */ enum nnp_chan_state {NNP_CHAN_NORMAL =0 ,NNP_CHAN_DESTROYED , +NNP_CHAN_RB_OP_IN_FLIGHT , };/**
@@ -45,14 +48,20 @@ enum nnp_chan_state {* @nnp_user: the nnp_user this channel belongs to. * the channel can reference host resources created by this * nnp_user object. -* @dev_mutex: protects @nnpdev and @state +* @dev_mutex: protects @nnpdev, @state, @hostres_hash and @hostres_map_ida * @state: the current state of this channel. +* @hostres_map_ida: generate ipc ids for hostres mapping +* @hostres_hash: hash table to store all host resource mapping, key is ipc id * @resp_waitq: waitqueue used for waiting for response messages be available. * @respq: circular buffer object that receive response messages from device. * @respq_lock: protects @respq * @respq_buf: buffer space allocated for circular response buffer. * @respq_size: current allocated size of circular response buffer. * @resp_lost: number of response messages lost due to response buffer full. +* @h2c_rb_hostres_map: host resource mapping used for each host-to-card ring buffer +* There may be up to 2 such ring buffers, both can be NULL. +* @c2h_rb_hostres_map: host resource mapping used for each card-to-host ring buffer +* There may be up to 2 such ring buffers, both can be NULL. */ struct nnp_chan {struct kref ref;
@@ -72,11 +81,32 @@ struct nnp_chan {wait_queue_head_t resp_waitq;enum nnp_chan_state state; +struct ida hostres_map_ida; +DECLARE_HASHTABLE (hostres_hash,6 ); +struct circ_buf respq;spinlock_t respq_lock;char *respq_buf;unsigned int respq_size;unsigned int resp_lost; + +struct nnpdev_mapping *h2c_rb_hostres_map[NNP_IPC_MAX_CHANNEL_RB ]; +struct nnpdev_mapping *c2h_rb_hostres_map[NNP_IPC_MAX_CHANNEL_RB ]; +}; + +/** +* struct chan_hostres_map - holds host resource mapping to channel +* +* @id: ipc map id of the mapping +* @hash_node: node to include this mapping in @hostres_hash of nnpdrv_cmd_chan +* @hostres_map: the host resource mapping object +* @event_msg: device response to the map create request +*/ +struct chan_hostres_map { +unsigned int id; +struct hlist_node hash_node; +struct nnpdev_mapping *hostres_map; +u64 event_msg; };#define chan_broken (chan) FIELD_GET(NNP_C2H_EVENT_REPORT_CODE_MASK, (chan)->card_critical_error_msg)
@@ -96,4 +126,9 @@ struct nnp_chan *nnpdev_chan_create(struct nnp_device *nnpdev, int host_fd,int nnp_chan_add_response (struct nnp_chan *cmd_chan,u64 *hw_msg,u32 size); +int nnp_chan_set_ringbuf (struct nnp_chan *chan,bool h2c,unsigned int id, +struct nnpdev_mapping *hostres_map); + +struct chan_hostres_map *nnp_chan_find_map (struct nnp_chan *chan,unsigned int map_id); +int nnp_chan_unmap_hostres (struct nnp_chan *chan,unsigned int map_id);#endif
drivers/misc/intel-nnpi/device.c +57 -1
83 lines not on this page · raw message
drivers/misc/intel-nnpi/device_chardev.c +441 -0
465 lines not on this page · raw message
include/uapi/misc/intel_nnpi.h +111 -0
123 lines not on this page · raw message
Signature · 1 line
1.8.3.1
Greg KH
maintainerQuoted 6 lines · Guy Zadicario
Hi,
The following series is a driver for a new PCIe device from Intel named NNP-I (Nirvana Neural Processor for Inference). NNP-I is a PCIe connected compute device used for acceleration of AI deep learning inference applications in the data-center.
How does this differ from the "intel_gna" driver being proposed here:
https://lore.kernel.org/linux-doc/[email protected]/
Please work together to come up with a unified userspace api for these devices, and hopefully, shared kernel code as well.
thanks,
greg k-h
Quoted 8 lines · Greg KH
Quoted 6 lines · Guy Zadicario
Hi,
The following series is a driver for a new PCIe device from Intel named NNP-I (Nirvana Neural Processor for Inference). NNP-I is a PCIe connected compute device used for acceleration of AI deep learning inference applications in the data-center.
Quoted 6 lines
How does this differ from the "
intel_gna" driver being proposed here:https://lore.kernel.org/linux-doc/[email protected]/Please work together to come up with a unified userspace api for these devices, and hopefully, shared kernel code as well.
Ok, after consulting, we will try to share api and some kernel code between the two drivers.
The plan is to suggest a common framework for both devices and let each device driver register with that framework, some functionality will be common and some will be device driver specific (same model as drm for gpus). At first I think the common part will be small and will deal only with host memory management, later, it might grow, if/when some other drivers will adopt it as well.
We will first change the NNP-I driver to use this framework. The GNA driver will wait for this API to be accepted and only then will adopt it and request to upstream.
Thanks,
Guy.
Greg KH
maintainerQuoted 25 lines · Guy Zadicario
Quoted 8 lines · Greg KH
Quoted 6 lines · Guy Zadicario
Hi,
The following series is a driver for a new PCIe device from Intel named NNP-I (Nirvana Neural Processor for Inference). NNP-I is a PCIe connected compute device used for acceleration of AI deep learning inference applications in the data-center.
Quoted 6 lines
How does this differ from the "
intel_gna" driver being proposed here:https://lore.kernel.org/linux-doc/[email protected]/Please work together to come up with a unified userspace api for these devices, and hopefully, shared kernel code as well.
Ok, after consulting, we will try to share api and some kernel code between the two drivers.
The plan is to suggest a common framework for both devices and let each device driver register with that framework, some functionality will be common and some will be device driver specific (same model as drm for gpus). At first I think the common part will be small and will deal only with host memory management, later, it might grow, if/when some other drivers will adopt it as well.
Quoted 3 lines
We will first change the NNP-I driver to use this framework. The GNA driver will wait for this API to be accepted and only then will adopt it and request to upstream.
Great, can you also look into what it would take to move the habanna code to this new api as well? A new api doesn't mean much if there is only one user :)
thanks,
greg k-h