AMD XDNA driver
Subsystems (6)
Lizhi Hou
maintainerv7 cover AMD XDNA driver
This patchset introduces a new Linux Kernel Driver, amdxdna for AMD NPUs. The driver is based on Linux accel subsystem.
NPU (Neural Processing Unit) is an AI inference accelerator integrated into AMD client CPUs. NPU enables efficient execution of Machine Learning applications like CNNs, LLMs, etc. NPU is based on AMD XDNA architecture [1].
AMD NPU consists of the following components:
- Tiled array of AMD AI Engine processors.
- Micro Controller which runs the NPU Firmware responsible for command processing, AIE array configuration, and execution management.
- PCI EP for host control of the NPU device.
- Interconnect for connecting the NPU components together.
- SRAM for use by the NPU Firmware.
- Address translation hardware for protected host memory access by the NPU.
NPU supports multiple concurrent fully isolated contexts. Concurrent contexts may be bound to AI Engine array spatially and or temporarily.
The driver is licensed under GPL-2.0 except for UAPI header which is licensed GPL-2.0 WITH Linux-syscall-note.
User mode driver stack consists of XRT [2] and AMD AIE Plugin for IREE [3].
The firmware for the NPU is distributed as a closed source binary, and has already been pushed to the DRM firmware repository [4].
[1] https://www.amd.com/en/technologies/xdna.html
[2] https://github.com/Xilinx/XRT
[3] https://github.com/nod-ai/iree-amd-aie
[4] https://gitlab.freedesktop.org/drm/firmware/-/tree/amd-ipu-staging/amdnpu
Changes since v6:
- Revise command submission flow
Changes since v5:
- Remove wait_cmd ioctl and use syncobj instead
- Cleanup spelling errors
- Add dependencies in Kconfig
Changes since v4:
- Fix lockdep errors
- Use __u* structure for struct aie_error
Changes since v3:
- Remove debug BO patch
- Changes based on code review comments
Changes since v2:
- Add document amdnpu.rst
- Change AIE2_DEVM_SIZE to 64M due to firmware change
- Changes based on code review comments
Changes since v1:
- Remove some inline defines
- Minor changes based on code review comments
Lizhi Hou (10):
accel/amdxdna: Add documentation for AMD NPU accelerator driver accel/amdxdna: Add a new driver for AMD AI Engine accel/amdxdna: Support hardware mailbox accel/amdxdna: Add hardware resource solver accel/amdxdna: Add hardware context
accel/amdxdna: Add GEM buffer object management accel/amdxdna: Add command execution
accel/amdxdna: Add suspend and resume
accel/amdxdna: Add error handling
accel/amdxdna: Add query functions
Documentation/accel/amdxdna/amdnpu.rst | 281 ++++++ Documentation/accel/amdxdna/index.rst | 11 + Documentation/accel/index.rst | 1 + MAINTAINERS | 11 + drivers/accel/Kconfig | 1 + drivers/accel/Makefile | 1 + drivers/accel/amdxdna/Kconfig | 18 + drivers/accel/amdxdna/Makefile | 21 + drivers/accel/amdxdna/TODO | 5 + drivers/accel/amdxdna/aie2_ctx.c | 927 ++++++++++++++++++ drivers/accel/amdxdna/aie2_error.c | 360 +++++++ drivers/accel/amdxdna/aie2_message.c | 791 +++++++++++++++ drivers/accel/amdxdna/aie2_msg_priv.h | 370 +++++++ drivers/accel/amdxdna/aie2_pci.c | 761 ++++++++++++++ drivers/accel/amdxdna/aie2_pci.h | 260 +++++ drivers/accel/amdxdna/aie2_psp.c | 146 +++ drivers/accel/amdxdna/aie2_smu.c | 119 +++ drivers/accel/amdxdna/aie2_solver.c | 330 +++++++ drivers/accel/amdxdna/aie2_solver.h | 154 +++ drivers/accel/amdxdna/amdxdna_ctx.c | 553 +++++++++++ drivers/accel/amdxdna/amdxdna_ctx.h | 162 +++ drivers/accel/amdxdna/amdxdna_gem.c | 622 ++++++++++++ drivers/accel/amdxdna/amdxdna_gem.h | 65 ++ drivers/accel/amdxdna/amdxdna_mailbox.c | 576 +++++++++++ drivers/accel/amdxdna/amdxdna_mailbox.h | 124 +++ .../accel/amdxdna/amdxdna_mailbox_helper.c | 61 ++ .../accel/amdxdna/amdxdna_mailbox_helper.h | 42 + drivers/accel/amdxdna/amdxdna_pci_drv.c | 409 ++++++++ drivers/accel/amdxdna/amdxdna_pci_drv.h | 123 +++ drivers/accel/amdxdna/amdxdna_sysfs.c | 67 ++ drivers/accel/amdxdna/npu1_regs.c | 101 ++ drivers/accel/amdxdna/npu2_regs.c | 118 +++ drivers/accel/amdxdna/npu4_regs.c | 118 +++ drivers/accel/amdxdna/npu5_regs.c | 118 +++ include/trace/events/amdxdna.h | 101 ++ include/uapi/drm/amdxdna_accel.h | 436 ++++++++ 36 files changed, 8364 insertions(+) create mode 100644 Documentation/accel/amdxdna/amdnpu.rst create mode 100644 Documentation/accel/amdxdna/index.rst create mode 100644 drivers/accel/amdxdna/Kconfig create mode 100644 drivers/accel/amdxdna/Makefile create mode 100644 drivers/accel/amdxdna/TODO create mode 100644 drivers/accel/amdxdna/aie2_ctx.c create mode 100644 drivers/accel/amdxdna/aie2_error.c create mode 100644 drivers/accel/amdxdna/aie2_message.c create mode 100644 drivers/accel/amdxdna/aie2_msg_priv.h create mode 100644 drivers/accel/amdxdna/aie2_pci.c create mode 100644 drivers/accel/amdxdna/aie2_pci.h create mode 100644 drivers/accel/amdxdna/aie2_psp.c create mode 100644 drivers/accel/amdxdna/aie2_smu.c create mode 100644 drivers/accel/amdxdna/aie2_solver.c create mode 100644 drivers/accel/amdxdna/aie2_solver.h create mode 100644 drivers/accel/amdxdna/amdxdna_ctx.c create mode 100644 drivers/accel/amdxdna/amdxdna_ctx.h create mode 100644 drivers/accel/amdxdna/amdxdna_gem.c create mode 100644 drivers/accel/amdxdna/amdxdna_gem.h create mode 100644 drivers/accel/amdxdna/amdxdna_mailbox.c create mode 100644 drivers/accel/amdxdna/amdxdna_mailbox.h create mode 100644 drivers/accel/amdxdna/amdxdna_mailbox_helper.c create mode 100644 drivers/accel/amdxdna/amdxdna_mailbox_helper.h create mode 100644 drivers/accel/amdxdna/amdxdna_pci_drv.c create mode 100644 drivers/accel/amdxdna/amdxdna_pci_drv.h create mode 100644 drivers/accel/amdxdna/amdxdna_sysfs.c create mode 100644 drivers/accel/amdxdna/npu1_regs.c create mode 100644 drivers/accel/amdxdna/npu2_regs.c create mode 100644 drivers/accel/amdxdna/npu4_regs.c create mode 100644 drivers/accel/amdxdna/npu5_regs.c create mode 100644 include/trace/events/amdxdna.h create mode 100644 include/uapi/drm/amdxdna_accel.h
Signature · 1 line
2.34.1
Lizhi Hou
maintainerPATCH v7 1/10 accel/amdxdna: Add documentation for AMD NPU accelerator driver
AMD NPU (Neural Processing Unit) is a multi-user AI inference accelerator integrated into AMD client APU. NPU enables efficient execution of Machine Learning applications like CNN, LLM, etc. NPU is based on AMD XDNA Architecture. NPU is managed by amdxdna driver.
--- Documentation/accel/amdxdna/amdnpu.rst | 281 +++++++++++++++++++++++++ Documentation/accel/amdxdna/index.rst | 11 + Documentation/accel/index.rst | 1 + 3 files changed, 293 insertions(+) create mode 100644 Documentation/accel/amdxdna/amdnpu.rst create mode 100644 Documentation/accel/amdxdna/index.rst
Documentation/accel/amdxdna/amdnpu.rst new file +281 -0
@@ -0,0 +1,281 @@ +.. SPDX-License-Identifier: GPL-2.0-only + +.. include:: <isonum.txt> + +========= + AMD NPU +========= + +:Copyright: |copy| 2024 Advanced Micro Devices, Inc. +:Author: Sonal Santan <[email protected]> + +Overview +======== + +AMD NPU (Neural Processing Unit) is a multi-user AI inference accelerator +integrated into AMD client APU. NPU enables efficient execution of Machine +Learning applications like CNN, LLM, etc. NPU is based on +`AMD XDNA Architecture`_. NPU is managed by **amdxdna** driver. + + +Hardware Description +==================== + +AMD NPU consists of the following hardware components: + +AMD XDNA Array +-------------- + +AMD XDNA Array comprises of 2D array of compute and memory tiles built with +`AMD AI Engine Technology`_. Each column has 4 rows of compute tiles and 1 +row of memory tile. Each compute tile contains a VLIW processor with its own +dedicated program and data memory. The memory tile acts as L2 memory. The 2D +array can be partitioned at a column boundary creating a spatially isolated +partition which can be bound to a workload context. + +Each column also has dedicated DMA engines to move data between host DDR and +memory tile. + +AMD Phoenix and AMD Hawk Point client NPU have a 4x5 topology, i.e., 4 rows of +compute tiles arranged into 5 columns. AMD Strix Point client APU have 4x8 +topology, i.e., 4 rows of compute tiles arranged into 8 columns. + +Shared L2 Memory +---------------- + +The single row of memory tiles create a pool of software managed on chip L2 +memory. DMA engines are used to move data between host DDR and memory tiles. +AMD Phoenix and AMD Hawk Point NPUs have a total of 2560 KB of L2 memory. +AMD Strix Point NPU has a total of 4096 KB of L2 memory. + +Microcontroller +--------------- + +A microcontroller runs NPU Firmware which is responsible for command processing, +XDNA Array partition setup, XDNA Array configuration, workload context +management and workload orchestration. + +NPU Firmware uses a dedicated instance of an isolated non-privileged context +called ERT to service each workload context. ERT is also used to execute user +provided ``ctrlcode`` associated with the workload context. + +NPU Firmware uses a single isolated privileged context called MERT to service +management commands from the amdxdna driver. + +Mailboxes +--------- + +The microcontroller and amdxdna driver use a privileged channel for management +tasks like setting up of contexts, telemetry, query, error handling, setting up +user channel, etc. As mentioned before, privileged channel requests are +serviced by MERT. The privileged channel is bound to a single mailbox. + +The microcontroller and amdxdna driver use a dedicated user channel per +workload context. The user channel is primarily used for submitting work to +the NPU. As mentioned before, a user channel requests are serviced by an +instance of ERT. Each user channel is bound to its own dedicated mailbox. + +PCIe EP +------- + +NPU is visible to the x86 host CPU as a PCIe device with multiple BARs and some +MSI-X interrupt vectors. NPU uses a dedicated high bandwidth SoC level fabric +for reading or writing into host memory. Each instance of ERT gets its own +dedicated MSI-X interrupt. MERT gets a single instance of MSI-X interrupt. + +The number of PCIe BARs varies depending on the specific device. Based on their +functions, PCIe BARs can generally be categorized into the following types. + +* PSP BAR: Expose the AMD PSP (Platform Security Processor) function +* SMU BAR: Expose the AMD SMU (System Management Unit) function +* SRAM BAR: Expose ring buffers for the mailbox +* Mailbox BAR: Expose the mailbox control registers (head, tail and ISR + registers etc.) +* Public Register BAR: Expose public registers + +On specific devices, the above-mentioned BAR type might be combined into a +single physical PCIe BAR. Or a module might require two physical PCIe BARs to +be fully functional. For example, + +* On AMD Phoenix device, PSP, SMU, Public Register BARs are on PCIe BAR index 0. +* On AMD Strix Point device, Mailbox and Public Register BARs are on PCIe BAR + index 0. The PSP has some registers in PCIe BAR index 0 (Public Register BAR) + and PCIe BAR index 4 (PSP BAR). + +Process Isolation Hardware +-------------------------- + +As explained before, XDNA Array can be dynamically divided into isolated +spatial partitions, each of which may have one or more columns. The spatial +partition is setup by programming the column isolation registers by the +microcontroller. Each spatial partition is associated with a PASID which is +also programmed by the microcontroller. Hence multiple spatial partitions in +the NPU can make concurrent host access protected by PASID. + +The NPU FW itself uses microcontroller MMU enforced isolated contexts for +servicing user and privileged channel requests. + + +Mixed Spatial and Temporal Scheduling +===================================== + +AMD XDNA architecture supports mixed spatial and temporal (time sharing) +scheduling of 2D array. This means that spatial partitions may be setup and +torn down dynamically to accommodate various workloads. A *spatial* partition +may be *exclusively* bound to one workload context while another partition may +be *temporarily* bound to more than one workload contexts. The microcontroller +updates the PASID for a temporarily shared partition to match the context that +has been bound to the partition at any moment. + +Resource Solver +--------------- + +The Resource Solver component of the amdxdna driver manages the allocation +of 2D array among various workloads. Every workload describes the number +of columns required to run the NPU binary in its metadata. The Resource Solver +component uses hints passed by the workload and its own heuristics to +decide 2D array (re)partition strategy and mapping of workloads for spatial and +temporal sharing of columns. The FW enforces the context-to-column(s) resource +binding decisions made by the Resource Solver. + +AMD Phoenix and AMD Hawk Point client NPU can support 6 concurrent workload +contexts. AMD Strix Point can support 16 concurrent workload contexts. + + +Application Binaries +==================== + +A NPU application workload is comprised of two separate binaries which are +generated by the NPU compiler. + +1. AMD XDNA Array overlay, which is used to configure a NPU spatial partition. + The overlay contains instructions for setting up the stream switch + configuration and ELF for the compute tiles. The overlay is loaded on the + spatial partition bound to the workload by the associated ERT instance. + Refer to the + `Versal Adaptive SoC AIE-ML Architecture Manual (AM020)`_ for more details. + +2. ``ctrlcode``, used for orchestrating the overlay loaded on the spatial + partition. ``ctrlcode`` is executed by the ERT running in protected mode on + the microcontroller in the context of the workload. ``ctrlcode`` is made up + of a sequence of opcodes named ``XAie_TxnOpcode``. Refer to the + `AI Engine Run Time`_ for more details. + + +Special Host Buffers +==================== + +Per-context Instruction Buffer +------------------------------ + +Every workload context uses a host resident 64 MB buffer which is memory +mapped into the ERT instance created to service the workload. The ``ctrlcode`` +used by the workload is copied into this special memory. This buffer is +protected by PASID like all other input/output buffers used by that workload. +Instruction buffer is also mapped into the user space of the workload. + +Global Privileged Buffer +------------------------ + +In addition, the driver also allocates a single buffer for maintenance tasks +like recording errors from MERT. This global buffer uses the global IOMMU +domain and is only accessible by MERT. + + +High-level Use Flow +=================== + +Here are the steps to run a workload on AMD NPU: + +1. Compile the workload into an overlay and a ``ctrlcode`` binary. +2. Userspace opens a context in the driver and provides the overlay. +3. The driver checks with the Resource Solver for provisioning a set of columns + for the workload. +4. The driver then asks MERT to create a context on the device with the desired + columns. +5. MERT then creates an instance of ERT. MERT also maps the Instruction Buffer + into ERT memory. +6. The userspace then copies the ``ctrlcode`` to the Instruction Buffer. +7. Userspace then creates a command buffer with pointers to input, output, and + instruction buffer; it then submits command buffer with the driver and goes + to sleep waiting for completion. +8. The driver sends the command over the Mailbox to ERT. +9. ERT *executes* the ``ctrlcode`` in the instruction buffer. +10. Execution of the ``ctrlcode`` kicks off DMAs to and from the host DDR while + AMD XDNA Array is running. +11. When ERT reaches end of ``ctrlcode``, it raises an MSI-X to send completion + signal to the driver which then wakes up the waiting workload. + + +Boot Flow +========= + +amdxdna driver uses PSP to securely load signed NPU FW and kick off the boot +of the NPU microcontroller. amdxdna driver then waits for the alive signal in +a special location on BAR 0. The NPU is switched off during SoC suspend and +turned on after resume where the NPU FW is reloaded, and the handshake is +performed again. + + +Userspace components +==================== + +Compiler +-------- + +Peano is an LLVM based open-source compiler for AMD XDNA Array compute tile +available at: +https://github.com/Xilinx/llvm-aie + +The open-source IREE compiler supports graph compilation of ML models for AMD +NPU and uses Peano underneath. It is available at: +https://github.com/nod-ai/iree-amd-aie + +Usermode Driver (UMD) +--------------------- + +The open-source XRT runtime stack interfaces with amdxdna kernel driver. XRT +can be found at: +https://github.com/Xilinx/XRT + +The open-source XRT shim for NPU is can be found at: +https://github.com/amd/xdna-driver + + +DMA Operation +============= + +DMA operation instructions are encoded in the ``ctrlcode`` as +``XAIE_IO_BLOCKWRITE`` opcode. When ERT executes ``XAIE_IO_BLOCKWRITE``, DMA +operations between host DDR and L2 memory are effected. + + +Error Handling +============== + +When MERT detects an error in AMD XDNA Array, it pauses execution for that +workload context and sends an asynchronous message to the driver over the +privileged channel. The driver then sends a buffer pointer to MERT to capture +the register states for the partition bound to faulting workload context. The +driver then decodes the error by reading the contents of the buffer pointer. + + +Telemetry +========= + +MERT can report various kinds of telemetry information like the following: + +* L1 interrupt counter +* DMA counter +* Deep Sleep counter +* etc. + + +References +========== + +- `AMD XDNA Architecture <https://www.amd.com/en/technologies/xdna.html>`_ +- `AMD AI Engine Technology <https://www.xilinx.com/products/technology/ai-engine.html>`_ +- `Peano <https://github.com/Xilinx/llvm-aie>`_ +- `Versal Adaptive SoC AIE-ML Architecture Manual (AM020) <https://docs.amd.com/r/en-US/am020-versal-aie-ml>`_ +- `AI Engine Run Time <https://github.com/Xilinx/aie-rt/tree/release/main_aig>`_
Documentation/accel/amdxdna/index.rst new file +11 -0
@@ -0,0 +1,11 @@ +.. SPDX-License-Identifier: GPL-2.0-only + +===================================== + accel/amdxdna NPU driver +===================================== + +The accel/amdxdna driver supports the AMD NPU (Neural Processing Unit). + +.. toctree:: + + amdnpu
Documentation/accel/index.rst +1 -0
@@ -8,6 +8,7 @@ Compute Accelerators :maxdepth: 1 introduction + amdxdna/index qaic/index .. only:: subproject and html
Signature · 1 line
2.34.1
Lizhi Hou
maintainerPATCH v7 2/10 accel/amdxdna: Add a new driver for AMD AI Engine
In reply to Lizhi HouAMD AI Engine forms the core of AMD NPU and can be used for accelerating machine learning applications.
Add the driver to support AI Engine integrated to AMD CPU. Only very basic functionalities are added.
- module and PCI device initialization
- firmware load
- power up
- low level hardware initialization
--- MAINTAINERS | 10 ++ drivers/accel/Kconfig | 1 + drivers/accel/Makefile | 1 + drivers/accel/amdxdna/Kconfig | 18 +++ drivers/accel/amdxdna/Makefile | 13 ++ drivers/accel/amdxdna/TODO | 5 + drivers/accel/amdxdna/aie2_pci.c | 180 ++++++++++++++++++++++++ drivers/accel/amdxdna/aie2_pci.h | 130 +++++++++++++++++ drivers/accel/amdxdna/aie2_psp.c | 142 +++++++++++++++++++ drivers/accel/amdxdna/aie2_smu.c | 117 +++++++++++++++ drivers/accel/amdxdna/amdxdna_pci_drv.c | 129 +++++++++++++++++ drivers/accel/amdxdna/amdxdna_pci_drv.h | 76 ++++++++++ drivers/accel/amdxdna/amdxdna_sysfs.c | 51 +++++++ drivers/accel/amdxdna/npu1_regs.c | 99 +++++++++++++ drivers/accel/amdxdna/npu2_regs.c | 116 +++++++++++++++ drivers/accel/amdxdna/npu4_regs.c | 116 +++++++++++++++ drivers/accel/amdxdna/npu5_regs.c | 116 +++++++++++++++ include/uapi/drm/amdxdna_accel.h | 24 ++++ 18 files changed, 1344 insertions(+) create mode 100644 drivers/accel/amdxdna/Kconfig create mode 100644 drivers/accel/amdxdna/Makefile create mode 100644 drivers/accel/amdxdna/TODO create mode 100644 drivers/accel/amdxdna/aie2_pci.c create mode 100644 drivers/accel/amdxdna/aie2_pci.h create mode 100644 drivers/accel/amdxdna/aie2_psp.c create mode 100644 drivers/accel/amdxdna/aie2_smu.c create mode 100644 drivers/accel/amdxdna/amdxdna_pci_drv.c create mode 100644 drivers/accel/amdxdna/amdxdna_pci_drv.h create mode 100644 drivers/accel/amdxdna/amdxdna_sysfs.c create mode 100644 drivers/accel/amdxdna/npu1_regs.c create mode 100644 drivers/accel/amdxdna/npu2_regs.c create mode 100644 drivers/accel/amdxdna/npu4_regs.c create mode 100644 drivers/accel/amdxdna/npu5_regs.c create mode 100644 include/uapi/drm/amdxdna_accel.h
MAINTAINERS +10 -0
@@ -1178,6 +1178,16 @@ M: Sanjay R Mehta <[email protected]> S: Maintained F: drivers/spi/spi-amd.c +AMD XDNA DRIVER +M: Min Ma <[email protected]> +M: Lizhi Hou <[email protected]> +L: [email protected] +S: Supported +T: git https://gitlab.freedesktop.org/drm/misc/kernel.git +F: Documentation/accel/amdxdna/ +F: drivers/accel/amdxdna/ +F: include/uapi/drm/amdxdna_accel.h + AMD XGBE DRIVER M: "Shyam Sundar S K" <[email protected]> L: [email protected]
drivers/accel/Kconfig +1 -0
@@ -24,6 +24,7 @@ menuconfig DRM_ACCEL different device files, called accel/accel* (in /dev, sysfs and debugfs). +source "drivers/accel/amdxdna/Kconfig" source "drivers/accel/habanalabs/Kconfig" source "drivers/accel/ivpu/Kconfig" source "drivers/accel/qaic/Kconfig"
drivers/accel/Makefile +1 -0
@@ -1,5 +1,6 @@ # SPDX-License-Identifier: GPL-2.0-only +obj-$(CONFIG_DRM_ACCEL_AMDXDNA) += amdxdna/ obj-$(CONFIG_DRM_ACCEL_HABANALABS) += habanalabs/ obj-$(CONFIG_DRM_ACCEL_IVPU) += ivpu/ obj-$(CONFIG_DRM_ACCEL_QAIC) += qaic/
drivers/accel/amdxdna/Kconfig new file +18 -0
@@ -0,0 +1,18 @@ +# SPDX-License-Identifier: GPL-2.0-only + +config DRM_ACCEL_AMDXDNA + tristate "AMD AI Engine" + depends on AMD_IOMMU + depends on DRM_ACCEL + depends on PCI && HAS_IOMEM + depends on X86_64 + select DRM_SCHED + select DRM_GEM_SHMEM_HELPER + select FW_LOADER + select HMM_MIRROR + help + Choose this option to enable support for NPU integrated into AMD + client CPUs like AMD Ryzen AI 300 Series. AMD NPU can be used to + accelerate machine learning applications. + + If "M" is selected, the driver module will be amdxdna.
drivers/accel/amdxdna/Makefile new file +13 -0
@@ -0,0 +1,13 @@ +# SPDX-License-Identifier: GPL-2.0-only + +amdxdna-y := \ + aie2_pci.o \ + aie2_psp.o \ + aie2_smu.o \ + amdxdna_pci_drv.o \ + amdxdna_sysfs.o \ + npu1_regs.o \ + npu2_regs.o \ + npu4_regs.o \ + npu5_regs.o +obj-$(CONFIG_DRM_ACCEL_AMDXDNA) = amdxdna.o
drivers/accel/amdxdna/TODO new file +5 -0
@@ -0,0 +1,5 @@ +- Replace idr with xa +- Add import and export BO support +- Add debugfs support +- Add debug BO support +- Improve power management
drivers/accel/amdxdna/aie2_pci.c new file +180 -0
180 lines not on this page · raw message
drivers/accel/amdxdna/aie2_pci.h new file +130 -0
130 lines not on this page · raw message
drivers/accel/amdxdna/aie2_psp.c new file +142 -0
142 lines not on this page · raw message
drivers/accel/amdxdna/aie2_smu.c new file +117 -0
117 lines not on this page · raw message
drivers/accel/amdxdna/amdxdna_pci_drv.c new file +129 -0
129 lines not on this page · raw message
drivers/accel/amdxdna/amdxdna_pci_drv.h new file +76 -0
76 lines not on this page · raw message
drivers/accel/amdxdna/amdxdna_sysfs.c new file +51 -0
51 lines not on this page · raw message
drivers/accel/amdxdna/npu1_regs.c new file +99 -0
99 lines not on this page · raw message
drivers/accel/amdxdna/npu2_regs.c new file +116 -0
116 lines not on this page · raw message
drivers/accel/amdxdna/npu4_regs.c new file +116 -0
116 lines not on this page · raw message
drivers/accel/amdxdna/npu5_regs.c new file +116 -0
116 lines not on this page · raw message
include/uapi/drm/amdxdna_accel.h new file +24 -0
@@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +/* +* Copyright (C) 2022-2024, Advanced Micro Devices, Inc. +*/ + +#ifndef _UAPI_AMDXDNA_ACCEL_H_ +#define _UAPI_AMDXDNA_ACCEL_H_ + +#include "drm.h" + +#if defined(__cplusplus) +extern "C" { +#endif + +enum amdxdna_device_type { +AMDXDNA_DEV_TYPE_UNKNOWN =-1 , +AMDXDNA_DEV_TYPE_KMQ , +}; + +#if defined(__cplusplus) +} /* extern c end */ +#endif + +#endif /* _UAPI_AMDXDNA_ACCEL_H_ */
Signature · 1 line
2.34.1
Lizhi Hou
maintainerPATCH v7 3/10 accel/amdxdna: Support hardware mailbox
In reply to Lizhi HouThe hardware mailboxes are used by the driver to submit requests to firmware and receive the completion notices from hardware.
Initially, a management mailbox channel is up and running. The driver may request firmware to create/destroy more channels dynamically through management channel.
Add driver internal mailbox interfaces.
- create/destroy a mailbox channel instance
- send a message to the firmware through a specific channel
- wait for a notification from the specific channel
--- MAINTAINERS | 1 + drivers/accel/amdxdna/Makefile | 3 + drivers/accel/amdxdna/aie2_message.c | 194 ++++++ drivers/accel/amdxdna/aie2_msg_priv.h | 370 +++++++++++ drivers/accel/amdxdna/aie2_pci.c | 256 +++++++- drivers/accel/amdxdna/aie2_pci.h | 62 ++ drivers/accel/amdxdna/aie2_psp.c | 2 + drivers/accel/amdxdna/amdxdna_mailbox.c | 576 ++++++++++++++++++ drivers/accel/amdxdna/amdxdna_mailbox.h | 124 ++++ .../accel/amdxdna/amdxdna_mailbox_helper.c | 56 ++ .../accel/amdxdna/amdxdna_mailbox_helper.h | 42 ++ drivers/accel/amdxdna/amdxdna_pci_drv.h | 8 + drivers/accel/amdxdna/amdxdna_sysfs.c | 11 + drivers/accel/amdxdna/npu1_regs.c | 1 + drivers/accel/amdxdna/npu2_regs.c | 1 + drivers/accel/amdxdna/npu4_regs.c | 1 + drivers/accel/amdxdna/npu5_regs.c | 1 + include/trace/events/amdxdna.h | 60 ++ 18 files changed, 1768 insertions(+), 1 deletion(-) create mode 100644 drivers/accel/amdxdna/aie2_message.c create mode 100644 drivers/accel/amdxdna/aie2_msg_priv.h create mode 100644 drivers/accel/amdxdna/amdxdna_mailbox.c create mode 100644 drivers/accel/amdxdna/amdxdna_mailbox.h create mode 100644 drivers/accel/amdxdna/amdxdna_mailbox_helper.c create mode 100644 drivers/accel/amdxdna/amdxdna_mailbox_helper.h create mode 100644 include/trace/events/amdxdna.h
MAINTAINERS +1 -0
@@ -1186,6 +1186,7 @@ S: Supported T: git https://gitlab.freedesktop.org/drm/misc/kernel.git F: Documentation/accel/amdxdna/ F: drivers/accel/amdxdna/ +F: include/trace/events/amdxdna.h F: include/uapi/drm/amdxdna_accel.h AMD XGBE DRIVER
drivers/accel/amdxdna/Makefile +3 -0
@@ -1,9 +1,12 @@ # SPDX-License-Identifier: GPL-2.0-only amdxdna-y := \ + aie2_message.o \ aie2_pci.o \ aie2_psp.o \ aie2_smu.o \ + amdxdna_mailbox.o \ + amdxdna_mailbox_helper.o \ amdxdna_pci_drv.o \ amdxdna_sysfs.o \ npu1_regs.o \
drivers/accel/amdxdna/aie2_message.c new file +194 -0
194 lines not on this page · raw message
drivers/accel/amdxdna/aie2_msg_priv.h new file +370 -0
370 lines not on this page · raw message
drivers/accel/amdxdna/aie2_pci.c +255 -1
301 lines not on this page · raw message
drivers/accel/amdxdna/aie2_pci.h +62 -0
99 lines not on this page · raw message
drivers/accel/amdxdna/aie2_psp.c +2 -0
@@ -10,6 +10,8 @@#include <linux/iopoll.h> #include "aie2_pci.h" +#include "amdxdna_mailbox.h" +#include "amdxdna_pci_drv.h" #define PSP_STATUS_READY BIT(31)
drivers/accel/amdxdna/amdxdna_mailbox.c new file +576 -0
576 lines not on this page · raw message
drivers/accel/amdxdna/amdxdna_mailbox.h new file +124 -0
124 lines not on this page · raw message
drivers/accel/amdxdna/amdxdna_mailbox_helper.c new file +56 -0
56 lines not on this page · raw message
drivers/accel/amdxdna/amdxdna_mailbox_helper.h new file +42 -0
42 lines not on this page · raw message
drivers/accel/amdxdna/amdxdna_pci_drv.h +8 -0
@@ -47,12 +47,20 @@ struct amdxdna_dev_info {const struct amdxdna_dev_ops *ops; }; +struct amdxdna_fw_ver { +u32 major; +u32 minor; +u32 sub; +u32 build; +}; +struct amdxdna_dev {struct drm_device ddev;struct amdxdna_dev_hdl *dev_handle;const struct amdxdna_dev_info *dev_info;struct mutex dev_lock;/* per device lock */ +struct amdxdna_fw_ver fw_ver; };/*
drivers/accel/amdxdna/amdxdna_sysfs.c +11 -0
@@ -24,9 +24,20 @@ static ssize_t device_type_show(struct device *dev, struct device_attribute *att }static DEVICE_ATTR_RO (device_type); +static ssize_t fw_version_show (struct device *dev,struct device_attribute *attr,char *buf) +{ +struct amdxdna_dev *xdna =dev_get_drvdata (dev); + +return sprintf (buf,"%d.%d.%d.%d\n" , xdna->fw_ver.major, + xdna->fw_ver.minor, xdna->fw_ver.sub, + xdna->fw_ver.build); +} +static DEVICE_ATTR_RO (fw_version); +static struct attribute *amdxdna_attrs[] = { &dev_attr_device_type.attr, &dev_attr_vbnv.attr, + &dev_attr_fw_version.attr,NULL , };
drivers/accel/amdxdna/npu1_regs.c +1 -0
@@ -8,6 +8,7 @@#include <linux/sizes.h> #include "aie2_pci.h" +#include "amdxdna_mailbox.h" #include "amdxdna_pci_drv.h" /* Address definition from NPU1 docs */
drivers/accel/amdxdna/npu2_regs.c +1 -0
@@ -8,6 +8,7 @@#include <linux/sizes.h> #include "aie2_pci.h" +#include "amdxdna_mailbox.h" #include "amdxdna_pci_drv.h" /* NPU Public Registers on MpNPUAxiXbar (refer to Diag npu_registers.h) */
drivers/accel/amdxdna/npu4_regs.c +1 -0
@@ -8,6 +8,7 @@#include <linux/sizes.h> #include "aie2_pci.h" +#include "amdxdna_mailbox.h" #include "amdxdna_pci_drv.h" /* NPU Public Registers on MpNPUAxiXbar (refer to Diag npu_registers.h) */
drivers/accel/amdxdna/npu5_regs.c +1 -0
@@ -8,6 +8,7 @@#include <linux/sizes.h> #include "aie2_pci.h" +#include "amdxdna_mailbox.h" #include "amdxdna_pci_drv.h" /* NPU Public Registers on MpNPUAxiXbar (refer to Diag npu_registers.h) */
include/trace/events/amdxdna.h new file +60 -0
60 lines not on this page · raw message
Signature · 1 line
2.34.1
Lizhi Hou
maintainerPATCH v7 4/10 accel/amdxdna: Add hardware resource solver
In reply to Lizhi HouThe AI Engine consists of 2D array of tiles arranged as columns. Provides the basic column allocation and release functions for the tile columns.
--- drivers/accel/amdxdna/Makefile | 1 + drivers/accel/amdxdna/aie2_pci.c | 23 +- drivers/accel/amdxdna/aie2_solver.c | 330 ++++++++++++++++++++++++ drivers/accel/amdxdna/aie2_solver.h | 154 +++++++++++ drivers/accel/amdxdna/amdxdna_pci_drv.h | 1 + 5 files changed, 508 insertions(+), 1 deletion(-) create mode 100644 drivers/accel/amdxdna/aie2_solver.c create mode 100644 drivers/accel/amdxdna/aie2_solver.h
drivers/accel/amdxdna/Makefile +1 -0
@@ -5,6 +5,7 @@ amdxdna-y := \ aie2_pci.o \ aie2_psp.o \ aie2_smu.o \ + aie2_solver.o \ amdxdna_mailbox.o \ amdxdna_mailbox_helper.o \ amdxdna_pci_drv.o \
drivers/accel/amdxdna/aie2_pci.c +22 -1
@@ -14,9 +14,14 @@#include "aie2_msg_priv.h" #include "aie2_pci.h" +#include "aie2_solver.h" #include "amdxdna_mailbox.h" #include "amdxdna_pci_drv.h" +int aie2_max_col =XRS_MAX_COL ; +module_param (aie2_max_col, uint,0600 ); +MODULE_PARM_DESC (aie2_max_col,"Maximum column could be used" ); +/* * The management mailbox channel is allocated by firmware. * The related register and ring buffer information is on SRAM BAR.
@@ -307,6 +312,7 @@ static int aie2_init(struct amdxdna_dev *xdna) {struct pci_dev *pdev =to_pci_dev (xdna->ddev.dev);void __iomem *tbl[PCI_NUM_RESOURCES ] = {0 }; +struct init_config xrs_cfg = {0 };struct amdxdna_dev_hdl *ndev;struct psp_config psp_conf;const struct firmware *fw;
@@ -402,7 +408,22 @@ static int aie2_init(struct amdxdna_dev *xdna)XDNA_ERR (xdna,"Query firmware failed, ret %d" , ret); goto stop_hw; } - ndev->total_col = ndev->metadata.cols; + ndev->total_col =min (aie2_max_col, ndev->metadata.cols); + + xrs_cfg.clk_list.num_levels =3 ; + xrs_cfg.clk_list.cu_clk_list[0 ] =0 ; + xrs_cfg.clk_list.cu_clk_list[1 ] =800 ; + xrs_cfg.clk_list.cu_clk_list[2 ] =1000 ; + xrs_cfg.sys_eff_factor =1 ; + xrs_cfg.ddev = &xdna->ddev; + xrs_cfg.total_col = ndev->total_col; + + xdna->xrs_hdl =xrsm_init (&xrs_cfg); +if (!xdna->xrs_hdl) { +XDNA_ERR (xdna,"Initialize resolver failed" ); + ret = -EINVAL ; + goto stop_hw; + }release_firmware (fw);return 0 ;
drivers/accel/amdxdna/aie2_solver.c new file +330 -0
330 lines not on this page · raw message
drivers/accel/amdxdna/aie2_solver.h new file +154 -0
154 lines not on this page · raw message
drivers/accel/amdxdna/amdxdna_pci_drv.h +1 -0
@@ -58,6 +58,7 @@ struct amdxdna_dev {struct drm_device ddev;struct amdxdna_dev_hdl *dev_handle;const struct amdxdna_dev_info *dev_info; +void *xrs_hdl;struct mutex dev_lock;/* per device lock */ struct amdxdna_fw_ver fw_ver;
Signature · 1 line
2.34.1
Lizhi Hou
maintainerPATCH v7 5/10 accel/amdxdna: Add hardware context
In reply to Lizhi HouThe hardware can be shared among multiple user applications. The hardware resources are allocated/freed based on the request from user application via driver IOCTLs.
DRM_IOCTL_AMDXDNA_CREATE_HWCTX
Allocate tile columns and create a hardware context structure to track the usage and status of the resources. A hardware context ID is returned for XDNA command execution.
DRM_IOCTL_AMDXDNA_DESTROY_HWCTX
Release hardware context based on its ID. The tile columns belong to this hardware context will be reclaimed.
DRM_IOCTL_AMDXDNA_CONFIG_HWCTX
Config hardware context. Bind the hardware context to the required resources.
--- drivers/accel/amdxdna/Makefile | 2 + drivers/accel/amdxdna/aie2_ctx.c | 186 ++++++++++++++++++++ drivers/accel/amdxdna/aie2_message.c | 90 ++++++++++ drivers/accel/amdxdna/aie2_pci.c | 43 +++++ drivers/accel/amdxdna/aie2_pci.h | 13 ++ drivers/accel/amdxdna/amdxdna_ctx.c | 219 ++++++++++++++++++++++++ drivers/accel/amdxdna/amdxdna_ctx.h | 39 +++++ drivers/accel/amdxdna/amdxdna_pci_drv.c | 125 +++++++++++++- drivers/accel/amdxdna/amdxdna_pci_drv.h | 20 +++ include/uapi/drm/amdxdna_accel.h | 131 ++++++++++++++ 10 files changed, 867 insertions(+), 1 deletion(-) create mode 100644 drivers/accel/amdxdna/aie2_ctx.c create mode 100644 drivers/accel/amdxdna/amdxdna_ctx.c create mode 100644 drivers/accel/amdxdna/amdxdna_ctx.h
drivers/accel/amdxdna/Makefile +2 -0
@@ -1,11 +1,13 @@ # SPDX-License-Identifier: GPL-2.0-only amdxdna-y := \ + aie2_ctx.o \ aie2_message.o \ aie2_pci.o \ aie2_psp.o \ aie2_smu.o \ aie2_solver.o \ + amdxdna_ctx.o \ amdxdna_mailbox.o \ amdxdna_mailbox_helper.o \ amdxdna_pci_drv.o \
drivers/accel/amdxdna/aie2_ctx.c new file +186 -0
186 lines not on this page · raw message
drivers/accel/amdxdna/aie2_message.c +90 -0
106 lines not on this page · raw message
drivers/accel/amdxdna/aie2_pci.c +43 -0
71 lines not on this page · raw message
drivers/accel/amdxdna/aie2_pci.h +13 -0
@@ -77,6 +77,7 @@ enum psp_reg_idx { };struct amdxdna_fw_ver ; +struct amdxdna_hwctx ;struct psp_config {const void *fw_buf;
@@ -117,6 +118,10 @@ struct rt_config {u32 value; }; +struct amdxdna_hwctx_priv { +void *mbox_chann; +}; +struct amdxdna_dev_hdl {struct amdxdna_dev *xdna;const struct amdxdna_dev_priv *priv;
@@ -189,4 +194,12 @@ int aie2_query_aie_version(struct amdxdna_dev_hdl *ndev, struct aie_version *verint aie2_query_aie_metadata (struct amdxdna_dev_hdl *ndev,struct aie_metadata *metadata);int aie2_query_firmware_version (struct amdxdna_dev_hdl *ndev,struct amdxdna_fw_ver *fw_ver); +int aie2_create_context (struct amdxdna_dev_hdl *ndev,struct amdxdna_hwctx *hwctx); +int aie2_destroy_context (struct amdxdna_dev_hdl *ndev,struct amdxdna_hwctx *hwctx); + +/* aie2_hwctx.c */ +int aie2_hwctx_init (struct amdxdna_hwctx *hwctx); +void aie2_hwctx_fini (struct amdxdna_hwctx *hwctx); +int aie2_hwctx_config (struct amdxdna_hwctx *hwctx,u32 type,u64 value,void *buf,u32 size); +#endif /* _AIE2_PCI_H_ */
drivers/accel/amdxdna/amdxdna_ctx.c new file +219 -0
219 lines not on this page · raw message
drivers/accel/amdxdna/amdxdna_ctx.h new file +39 -0
@@ -0,0 +1,39 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2022-2024, Advanced Micro Devices, Inc. +*/ + +#ifndef _AMDXDNA_CTX_H_ +#define _AMDXDNA_CTX_H_ + +struct amdxdna_hwctx { +struct amdxdna_client *client; +struct amdxdna_hwctx_priv *priv; +char *name; + +u32 id; +u32 max_opc; +u32 num_tiles; +u32 mem_size; +u32 fw_ctx_id; +u32 col_list_len; +u32 *col_list; +u32 start_col; +u32 num_col; +#define HWCTX_STAT_INIT 0 +#define HWCTX_STAT_READY 1 +#define HWCTX_STAT_STOP 2 +u32 status; +u32 old_status; + +struct amdxdna_qos_info qos; +struct amdxdna_hwctx_param_config_cu *cus; +u32 syncobj_hdl; +}; + +void amdxdna_hwctx_remove_all (struct amdxdna_client *client); +int amdxdna_drm_create_hwctx_ioctl (struct drm_device *dev,void *data,struct drm_file *filp); +int amdxdna_drm_config_hwctx_ioctl (struct drm_device *dev,void *data,struct drm_file *filp); +int amdxdna_drm_destroy_hwctx_ioctl (struct drm_device *dev,void *data,struct drm_file *filp); + +#endif /* _AMDXDNA_CTX_H_ */
drivers/accel/amdxdna/amdxdna_pci_drv.c +124 -1
167 lines not on this page · raw message
drivers/accel/amdxdna/amdxdna_pci_drv.h +20 -0
@@ -18,6 +18,7 @@extern const struct drm_driver amdxdna_drm_drv;struct amdxdna_dev ; +struct amdxdna_hwctx ;/* * struct amdxdna_dev_ops - Device hardware operation callbacks
@@ -25,6 +26,9 @@ struct amdxdna_dev;struct amdxdna_dev_ops {int (*init)(struct amdxdna_dev *xdna);void (*fini)(struct amdxdna_dev *xdna); +int (*hwctx_init)(struct amdxdna_hwctx *hwctx); +void (*hwctx_fini)(struct amdxdna_hwctx *hwctx); +int (*hwctx_config)(struct amdxdna_hwctx *hwctx,u32 type,u64 value,void *buf,u32 size); };/*
@@ -61,6 +65,7 @@ struct amdxdna_dev {void *xrs_hdl;struct mutex dev_lock;/* per device lock */ +struct list_head client_list;struct amdxdna_fw_ver fw_ver; };
@@ -73,6 +78,21 @@ struct amdxdna_device_id {const struct amdxdna_dev_info *dev_info; }; +/* +* struct amdxdna_client - amdxdna client +* A per fd data structure for managing context and other user process stuffs. +*/ +struct amdxdna_client { +struct list_head node; +pid_t pid; +struct mutex hwctx_lock;/* protect hwctx */ +struct idr hwctx_idr; +struct amdxdna_dev *xdna; +struct drm_file *filp; +struct iommu_sva *sva; +int pasid; +}; +/* Add device info below */ extern const struct amdxdna_dev_info dev_npu1_info;extern const struct amdxdna_dev_info dev_npu2_info;
include/uapi/drm/amdxdna_accel.h +131 -0
148 lines not on this page · raw message
Signature · 1 line
2.34.1
Lizhi Hou
maintainerPATCH v7 6/10 accel/amdxdna: Add GEM buffer object management
In reply to Lizhi HouThere different types of BOs are supported:
- shmem
A user application uses shmem BOs as input/output for its workload running on NPU.
- device memory heap
The fixed size buffer dedicated to the device.
- device buffer
The buffer object allocated from device memory heap.
- command buffer
The buffer object created for delivering commands. The command buffer object is small and pinned on creation.
New IOCTLs are added: CREATE_BO, GET_BO_INFO, SYNC_BO. SYNC_BO is used to explicitly flush CPU cache for BO memory.
--- drivers/accel/amdxdna/Makefile | 1 + drivers/accel/amdxdna/aie2_ctx.c | 85 +++- drivers/accel/amdxdna/aie2_message.c | 80 +++ drivers/accel/amdxdna/aie2_pci.h | 3 + drivers/accel/amdxdna/amdxdna_ctx.h | 10 + drivers/accel/amdxdna/amdxdna_gem.c | 621 ++++++++++++++++++++++++ drivers/accel/amdxdna/amdxdna_gem.h | 65 +++ drivers/accel/amdxdna/amdxdna_pci_drv.c | 12 + drivers/accel/amdxdna/amdxdna_pci_drv.h | 6 + include/uapi/drm/amdxdna_accel.h | 77 +++ 10 files changed, 959 insertions(+), 1 deletion(-) create mode 100644 drivers/accel/amdxdna/amdxdna_gem.c create mode 100644 drivers/accel/amdxdna/amdxdna_gem.h
drivers/accel/amdxdna/Makefile +1 -0
@@ -8,6 +8,7 @@ amdxdna-y := \ aie2_smu.o \ aie2_solver.o \ amdxdna_ctx.o \ + amdxdna_gem.o \ amdxdna_mailbox.o \ amdxdna_mailbox_helper.o \ amdxdna_pci_drv.o \
drivers/accel/amdxdna/aie2_ctx.c +84 -1
143 lines not on this page · raw message
drivers/accel/amdxdna/aie2_message.c +80 -0
96 lines not on this page · raw message
drivers/accel/amdxdna/aie2_pci.h +3 -0
@@ -119,6 +119,7 @@ struct rt_config { };struct amdxdna_hwctx_priv { +struct amdxdna_gem_obj *heap;void *mbox_chann; };
@@ -196,6 +197,8 @@ int aie2_query_firmware_version(struct amdxdna_dev_hdl *ndev, struct amdxdna_fw_ver *fw_ver);int aie2_create_context (struct amdxdna_dev_hdl *ndev,struct amdxdna_hwctx *hwctx);int aie2_destroy_context (struct amdxdna_dev_hdl *ndev,struct amdxdna_hwctx *hwctx); +int aie2_map_host_buf (struct amdxdna_dev_hdl *ndev,u32 context_id,u64 addr,u64 size); +int aie2_config_cu (struct amdxdna_hwctx *hwctx);/* aie2_hwctx.c */ int aie2_hwctx_init (struct amdxdna_hwctx *hwctx);
drivers/accel/amdxdna/amdxdna_ctx.h +10 -0
@@ -6,6 +6,16 @@ #ifndef _AMDXDNA_CTX_H_ #define _AMDXDNA_CTX_H_ +/* Exec buffer command header format */ +#define AMDXDNA_CMD_STATE GENMASK(3, 0) +#define AMDXDNA_CMD_EXTRA_CU_MASK GENMASK(11, 10) +#define AMDXDNA_CMD_COUNT GENMASK(22, 12) +#define AMDXDNA_CMD_OPCODE GENMASK(27, 23) +struct amdxdna_cmd { + u32 header; + u32 data[]; +}; + struct amdxdna_hwctx { struct amdxdna_client *client; struct amdxdna_hwctx_priv *priv;
drivers/accel/amdxdna/amdxdna_gem.c new file +621 -0
621 lines not on this page · raw message
drivers/accel/amdxdna/amdxdna_gem.h new file +65 -0
65 lines not on this page · raw message
drivers/accel/amdxdna/amdxdna_pci_drv.c +12 -0
@@ -7,12 +7,14 @@#include <drm/drm_accel.h> #include <drm/drm_drv.h> #include <drm/drm_gem.h> +#include <drm/drm_gem_shmem_helper.h> #include <drm/drm_ioctl.h> #include <drm/drm_managed.h> #include <linux/iommu.h> #include <linux/pci.h> #include "amdxdna_ctx.h" +#include "amdxdna_gem.h" #include "amdxdna_pci_drv.h" /*
@@ -63,6 +65,7 @@ static int amdxdna_drm_open(struct drm_device *ddev, struct drm_file *filp) }mutex_init (&client->hwctx_lock);idr_init_base (&client->hwctx_idr,AMDXDNA_INVALID_CTX_HANDLE +1 ); +mutex_init (&client->mm_lock);mutex_lock (&xdna->dev_lock);list_add_tail (&client->node, &xdna->client_list);
@@ -91,6 +94,9 @@ static void amdxdna_drm_close(struct drm_device *ddev, struct drm_file *filp)idr_destroy (&client->hwctx_idr);mutex_destroy (&client->hwctx_lock); +mutex_destroy (&client->mm_lock); +if (client->dev_heap) +drm_gem_object_put (to_gobj (client->dev_heap));iommu_sva_unbind_device (client->sva);
@@ -123,6 +129,10 @@ static const struct drm_ioctl_desc amdxdna_drm_ioctls[] = {DRM_IOCTL_DEF_DRV (AMDXDNA_CREATE_HWCTX , amdxdna_drm_create_hwctx_ioctl,0 ),DRM_IOCTL_DEF_DRV (AMDXDNA_DESTROY_HWCTX , amdxdna_drm_destroy_hwctx_ioctl,0 ),DRM_IOCTL_DEF_DRV (AMDXDNA_CONFIG_HWCTX , amdxdna_drm_config_hwctx_ioctl,0 ), +/* BO */ +DRM_IOCTL_DEF_DRV (AMDXDNA_CREATE_BO , amdxdna_drm_create_bo_ioctl,0 ), +DRM_IOCTL_DEF_DRV (AMDXDNA_GET_BO_INFO , amdxdna_drm_get_bo_info_ioctl,0 ), + DRM_IOCTL_DEF_DRV(AMDXDNA_SYNC_BO, amdxdna_drm_sync_bo_ioctl, 0), }; static const struct file_operations amdxdna_fops = {
@@ -149,6 +159,8 @@ const struct drm_driver amdxdna_drm_drv = { .postclose = amdxdna_drm_close, .ioctls = amdxdna_drm_ioctls, .num_ioctls = ARRAY_SIZE(amdxdna_drm_ioctls), + + .gem_create_object = amdxdna_gem_create_object_cb, }; static const struct amdxdna_dev_info *
drivers/accel/amdxdna/amdxdna_pci_drv.h +6 -0
@@ -18,6 +18,7 @@extern const struct drm_driver amdxdna_drm_drv;struct amdxdna_dev ; +struct amdxdna_gem_obj ;struct amdxdna_hwctx ;/*
@@ -29,6 +30,7 @@ struct amdxdna_dev_ops {int (*hwctx_init)(struct amdxdna_hwctx *hwctx);void (*hwctx_fini)(struct amdxdna_hwctx *hwctx);int (*hwctx_config)(struct amdxdna_hwctx *hwctx,u32 type,u64 value,void *buf,u32 size); +void (*hmm_invalidate)(struct amdxdna_gem_obj *abo,unsigned long cur_seq); };/*
@@ -89,6 +91,10 @@ struct amdxdna_client {struct idr hwctx_idr;struct amdxdna_dev *xdna;struct drm_file *filp; + +struct mutex mm_lock;/* protect memory related */ +struct amdxdna_gem_obj *dev_heap; +struct iommu_sva *sva;int pasid; };
include/uapi/drm/amdxdna_accel.h +77 -0
102 lines not on this page · raw message
Signature · 1 line
2.34.1
Lizhi Hou
maintainerPATCH v7 7/10 accel/amdxdna: Add command execution
In reply to Lizhi HouAdd interfaces for user application to submit command and wait for its completion.
--- drivers/accel/amdxdna/aie2_ctx.c | 634 +++++++++++++++++- drivers/accel/amdxdna/aie2_message.c | 343 ++++++++++ drivers/accel/amdxdna/aie2_pci.c | 5 + drivers/accel/amdxdna/aie2_pci.h | 40 ++ drivers/accel/amdxdna/aie2_psp.c | 2 + drivers/accel/amdxdna/aie2_smu.c | 2 + drivers/accel/amdxdna/amdxdna_ctx.c | 320 ++++++++- drivers/accel/amdxdna/amdxdna_ctx.h | 111 +++ drivers/accel/amdxdna/amdxdna_gem.c | 1 + .../accel/amdxdna/amdxdna_mailbox_helper.c | 5 + drivers/accel/amdxdna/amdxdna_pci_drv.c | 12 + drivers/accel/amdxdna/amdxdna_pci_drv.h | 5 + drivers/accel/amdxdna/amdxdna_sysfs.c | 5 + drivers/accel/amdxdna/npu1_regs.c | 1 + drivers/accel/amdxdna/npu2_regs.c | 1 + drivers/accel/amdxdna/npu4_regs.c | 1 + drivers/accel/amdxdna/npu5_regs.c | 1 + include/trace/events/amdxdna.h | 41 ++ include/uapi/drm/amdxdna_accel.h | 38 ++ 19 files changed, 1559 insertions(+), 9 deletions(-)
drivers/accel/amdxdna/aie2_ctx.c +631 -3
@@ -8,8 +8,12 @@#include <drm/drm_gem.h> #include <drm/drm_gem_shmem_helper.h> #include <drm/drm_print.h> +#include <drm/drm_syncobj.h> +#include <linux/hmm.h> #include <linux/types.h> +#include <trace/events/amdxdna.h> +#include "aie2_msg_priv.h" #include "aie2_pci.h" #include "aie2_solver.h" #include "amdxdna_ctx.h"
@@ -17,6 +21,342 @@#include "amdxdna_mailbox.h" #include "amdxdna_pci_drv.h" +bool force_cmdlist; +module_param (force_cmdlist, bool,0600 ); +MODULE_PARM_DESC (force_cmdlist,"Force use command list (Default false)" ); + +#define HWCTX_MAX_TIMEOUT 60000/* milliseconds */ + +static void aie2_job_release (struct kref *ref) +{ +struct amdxdna_sched_job *job; + + job = container_of(ref, struct amdxdna_sched_job, refcnt); +amdxdna_sched_job_cleanup (job); +kfree (job); +} + +static void aie2_job_put (struct amdxdna_sched_job *job) +{ +kref_put (&job->refcnt, aie2_job_release); +} + +/* The bad_job is used in aie2_sched_job_timedout, otherwise, set it to NULL */ +static void aie2_hwctx_stop (struct amdxdna_dev *xdna,struct amdxdna_hwctx *hwctx, +struct drm_sched_job *bad_job) +{ +drm_sched_stop (&hwctx->priv->sched, bad_job); +aie2_destroy_context (xdna->dev_handle, hwctx); +} + +static int aie2_hwctx_restart (struct amdxdna_dev *xdna,struct amdxdna_hwctx *hwctx) +{ +struct amdxdna_gem_obj *heap = hwctx->priv->heap; +int ret; + + ret =aie2_create_context (xdna->dev_handle, hwctx); +if (ret) { +XDNA_ERR (xdna,"Create hwctx failed, ret %d" , ret); + goto out; + } + + ret =aie2_map_host_buf (xdna->dev_handle, hwctx->fw_ctx_id, + heap->mem.userptr, heap->mem.size); +if (ret) { +XDNA_ERR (xdna,"Map host buf failed, ret %d" , ret); + goto out; + } + +if (hwctx->status !=HWCTX_STAT_READY ) { +XDNA_DBG (xdna,"hwctx is not ready, status %d" , hwctx->status); + goto out; + } + + ret =aie2_config_cu (hwctx); +if (ret) { +XDNA_ERR (xdna,"Config cu failed, ret %d" , ret); + goto out; + } + +out: +drm_sched_start (&hwctx->priv->sched); +XDNA_DBG (xdna,"%s restarted, ret %d" , hwctx->name, ret); +return ret; +} + +void aie2_stop_ctx_by_col_map (struct amdxdna_client *client,u32 col_map) +{ +struct amdxdna_dev *xdna = client->xdna; +struct amdxdna_hwctx *hwctx; +int next =0 ; + +drm_WARN_ON (&xdna->ddev, !mutex_is_locked (&xdna->dev_lock)); +mutex_lock (&client->hwctx_lock); +idr_for_each_entry_continue (&client->hwctx_idr, hwctx, next) { +/* check if the HW context uses the error column */ +if (!(col_map &amdxdna_hwctx_col_map (hwctx))) +continue ; + +aie2_hwctx_stop (xdna, hwctx,NULL ); + hwctx->old_status = hwctx->status; + hwctx->status =HWCTX_STAT_STOP ; +XDNA_DBG (xdna,"Stop %s" , hwctx->name); + } +mutex_unlock (&client->hwctx_lock); +} + +void aie2_restart_ctx (struct amdxdna_client *client) +{ +struct amdxdna_dev *xdna = client->xdna; +struct amdxdna_hwctx *hwctx; +int next =0 ; + +drm_WARN_ON (&xdna->ddev, !mutex_is_locked (&xdna->dev_lock)); +mutex_lock (&client->hwctx_lock); +idr_for_each_entry_continue (&client->hwctx_idr, hwctx, next) { +if (hwctx->status !=HWCTX_STAT_STOP ) +continue ; + + hwctx->status = hwctx->old_status; +XDNA_DBG (xdna,"Resetting %s" , hwctx->name); +aie2_hwctx_restart (xdna, hwctx); + } +mutex_unlock (&client->hwctx_lock); +} + +static struct dma_fence *aie2_cmd_get_out_fence (struct amdxdna_hwctx *hwctx,u64 seq) +{ +struct dma_fence *fence, *out_fence =NULL ; +int ret; + + fence =drm_syncobj_fence_get (hwctx->priv->syncobj); +if (!fence) +return NULL ; + + ret =dma_fence_chain_find_seqno (&fence, seq); +if (ret) + goto out; + + out_fence =dma_fence_get (dma_fence_chain_contained (fence)); + +out: +dma_fence_put (fence); +return out_fence; +} + +static void aie2_hwctx_wait_for_idle (struct amdxdna_hwctx *hwctx) +{ +struct dma_fence *fence; + + fence =aie2_cmd_get_out_fence (hwctx, hwctx->priv->seq -1 ); +if (!fence) +return ; + +dma_fence_wait (fence, false); +dma_fence_put (fence); +} + +static void +aie2_sched_notify (struct amdxdna_sched_job *job) +{ +struct dma_fence *fence = job->fence; + +trace_xdna_job (&job->base, job->hwctx->name,"signaled fence" , job->seq); + job->hwctx->priv->completed++; +dma_fence_signal (fence); + +up (&job->hwctx->priv->job_sem); + job->job_done = true; +dma_fence_put (fence); +mmput (job->mm); +aie2_job_put (job); +} + +static int +aie2_sched_resp_handler (void *handle,const u32 *data,size_t size) +{ +struct amdxdna_sched_job *job = handle; +struct amdxdna_gem_obj *cmd_abo; +u32 ret =0 ; +u32 status; + + cmd_abo = job->cmd_bo; + +if (unlikely (!data)) + goto out; + +if (unlikely (size !=sizeof (u32))) { +amdxdna_cmd_set_state (cmd_abo,ERT_CMD_STATE_ABORT ); + ret = -EINVAL ; + goto out; + } + + status = *data; +XDNA_DBG (job->hwctx->client->xdna,"Resp status 0x%x" , status); +if (status ==AIE2_STATUS_SUCCESS ) +amdxdna_cmd_set_state (cmd_abo,ERT_CMD_STATE_COMPLETED ); +else +amdxdna_cmd_set_state (cmd_abo,ERT_CMD_STATE_ERROR ); + +out: +aie2_sched_notify (job); +return ret; +} + +static int +aie2_sched_nocmd_resp_handler (void *handle,const u32 *data,size_t size) +{ +struct amdxdna_sched_job *job = handle; +u32 ret =0 ; +u32 status; + +if (unlikely (!data)) + goto out; + +if (unlikely (size !=sizeof (u32))) { + ret = -EINVAL ; + goto out; + } + + status = *data; +XDNA_DBG (job->hwctx->client->xdna,"Resp status 0x%x" , status); + +out: +aie2_sched_notify (job); +return ret; +} + +static int +aie2_sched_cmdlist_resp_handler (void *handle,const u32 *data,size_t size) +{ +struct amdxdna_sched_job *job = handle; +struct amdxdna_gem_obj *cmd_abo; +struct cmd_chain_resp *resp; +struct amdxdna_dev *xdna; +u32 fail_cmd_status; +u32 fail_cmd_idx; +u32 ret =0 ; + + cmd_abo = job->cmd_bo; +if (unlikely (!data) ||unlikely (size !=sizeof (u32) *3 )) { +amdxdna_cmd_set_state (cmd_abo,ERT_CMD_STATE_ABORT ); + ret = -EINVAL ; + goto out; + } + + resp = (struct cmd_chain_resp *)data; + xdna = job->hwctx->client->xdna; +XDNA_DBG (xdna,"Status 0x%x" , resp->status); +if (resp->status ==AIE2_STATUS_SUCCESS ) { +amdxdna_cmd_set_state (cmd_abo,ERT_CMD_STATE_COMPLETED ); + goto out; + } + +/* Slow path to handle error, read from ringbuf on BAR */ + fail_cmd_idx = resp->fail_cmd_idx; + fail_cmd_status = resp->fail_cmd_status; +XDNA_DBG (xdna,"Failed cmd idx %d, status 0x%x" , + fail_cmd_idx, fail_cmd_status); + +if (fail_cmd_status ==AIE2_STATUS_SUCCESS ) { +amdxdna_cmd_set_state (cmd_abo,ERT_CMD_STATE_ABORT ); + ret = -EINVAL ; + goto out; + } +amdxdna_cmd_set_state (cmd_abo, fail_cmd_status); + +if (amdxdna_cmd_get_op (cmd_abo) ==ERT_CMD_CHAIN ) { +struct amdxdna_cmd_chain *cc =amdxdna_cmd_get_payload (cmd_abo,NULL ); + + cc->error_index = fail_cmd_idx; +if (cc->error_index >= cc->command_count) + cc->error_index =0 ; + } +out: +aie2_sched_notify (job); +return ret; +} + +static struct dma_fence * +aie2_sched_job_run (struct drm_sched_job *sched_job) +{ +struct amdxdna_sched_job *job =drm_job_to_xdna_job (sched_job); +struct amdxdna_gem_obj *cmd_abo = job->cmd_bo; +struct amdxdna_hwctx *hwctx = job->hwctx; +struct dma_fence *fence; +int ret; + +if (!mmget_not_zero (job->mm)) +return ERR_PTR (-ESRCH ); + +kref_get (&job->refcnt); + fence =dma_fence_get (job->fence); + +if (unlikely (!cmd_abo)) { + ret =aie2_sync_bo (hwctx, job, aie2_sched_nocmd_resp_handler); + goto out; + } + +amdxdna_cmd_set_state (cmd_abo,ERT_CMD_STATE_NEW ); + +if (amdxdna_cmd_get_op (cmd_abo) ==ERT_CMD_CHAIN ) + ret =aie2_cmdlist_multi_execbuf (hwctx, job, aie2_sched_cmdlist_resp_handler); +else if (force_cmdlist) + ret =aie2_cmdlist_single_execbuf (hwctx, job, aie2_sched_cmdlist_resp_handler); +else + ret =aie2_execbuf (hwctx, job, aie2_sched_resp_handler); + +out: +if (ret) { +dma_fence_put (job->fence); +aie2_job_put (job); +mmput (job->mm); + fence =ERR_PTR (ret); + } +trace_xdna_job (sched_job, hwctx->name,"sent to device" , job->seq); + +return fence; +} + +static void aie2_sched_job_free (struct drm_sched_job *sched_job) +{ +struct amdxdna_sched_job *job =drm_job_to_xdna_job (sched_job); +struct amdxdna_hwctx *hwctx = job->hwctx; + +trace_xdna_job (sched_job, hwctx->name,"job free" , job->seq); +if (!job->job_done) +up (&hwctx->priv->job_sem); + +if (job->out_fence) +dma_fence_put (job->out_fence); +drm_sched_job_cleanup (sched_job); +aie2_job_put (job); +} + +static enum drm_gpu_sched_stat +aie2_sched_job_timedout (struct drm_sched_job *sched_job) +{ +struct amdxdna_sched_job *job =drm_job_to_xdna_job (sched_job); +struct amdxdna_hwctx *hwctx = job->hwctx; +struct amdxdna_dev *xdna; + + xdna = hwctx->client->xdna; +trace_xdna_job (sched_job, hwctx->name,"job timedout" , job->seq); +mutex_lock (&xdna->dev_lock); +aie2_hwctx_stop (xdna, hwctx, sched_job); + +aie2_hwctx_restart (xdna, hwctx); +mutex_unlock (&xdna->dev_lock); + +return DRM_GPU_SCHED_STAT_NOMINAL ; +} + +const struct drm_sched_backend_ops sched_ops = { + .run_job = aie2_sched_job_run, + .free_job = aie2_sched_job_free, + .timedout_job = aie2_sched_job_timedout, +}; +static int aie2_hwctx_col_list (struct amdxdna_hwctx *hwctx) {struct amdxdna_dev *xdna = hwctx->client->xdna;
@@ -126,13 +466,66 @@ static void aie2_release_resource(struct amdxdna_hwctx *hwctx)XDNA_ERR (xdna,"Release AIE resource failed, ret %d" , ret); } +static int aie2_ctx_syncobj_create (struct amdxdna_hwctx *hwctx) +{ +struct amdxdna_dev *xdna = hwctx->client->xdna; +struct drm_file *filp = hwctx->client->filp; +struct drm_syncobj *syncobj; +u32 hdl; +int ret; + + hwctx->syncobj_hdl =AMDXDNA_INVALID_FENCE_HANDLE ; + + ret =drm_syncobj_create (&syncobj,0 ,NULL ); +if (ret) { +XDNA_ERR (xdna,"Create ctx syncobj failed, ret %d" , ret); +return ret; + } + ret =drm_syncobj_get_handle (filp, syncobj, &hdl); +if (ret) { +drm_syncobj_put (syncobj); +XDNA_ERR (xdna,"Create ctx syncobj handle failed, ret %d" , ret); +return ret; + } + hwctx->priv->syncobj = syncobj; + hwctx->syncobj_hdl = hdl; + +return 0 ; +} + +static void aie2_ctx_syncobj_destroy (struct amdxdna_hwctx *hwctx) +{ +/* +* The syncobj_hdl is owned by user space and will be cleaned up +* separately. +*/ +drm_syncobj_put (hwctx->priv->syncobj); +} + +static void aie2_ctx_syncobj_add_fence (struct amdxdna_hwctx *hwctx, +struct dma_fence *ofence,u64 seq) +{ +struct drm_syncobj *syncobj = hwctx->priv->syncobj; +struct dma_fence_chain *chain; + +if (!syncobj) +return ; + + chain =dma_fence_chain_alloc (); +if (!chain) +return ; + +drm_syncobj_add_point (syncobj, chain, ofence, seq); +} +int aie2_hwctx_init (struct amdxdna_hwctx *hwctx) {struct amdxdna_client *client = hwctx->client;struct amdxdna_dev *xdna = client->xdna; +struct drm_gpu_scheduler *sched;struct amdxdna_hwctx_priv *priv;struct amdxdna_gem_obj *heap; -int ret; +int i, ret; priv =kzalloc (sizeof (*hwctx->priv),GFP_KERNEL );if (!priv)
@@ -150,6 +543,7 @@ int aie2_hwctx_init(struct amdxdna_hwctx *hwctx)drm_gem_object_get (to_gobj (heap));mutex_unlock (&client->mm_lock); priv->heap = heap; +sema_init (&priv->job_sem,HWCTX_MAX_CMDS ); ret =amdxdna_gem_pin (heap);if (ret) {
@@ -157,10 +551,47 @@ int aie2_hwctx_init(struct amdxdna_hwctx *hwctx) goto put_heap; } +for (i =0 ; i <ARRAY_SIZE (priv->cmd_buf); i++) { +struct amdxdna_gem_obj *abo; +struct amdxdna_drm_create_bo args = { + .flags =0 , + .type =AMDXDNA_BO_DEV , + .vaddr =0 , + .size =MAX_CHAIN_CMDBUF_SIZE , + }; + + abo =amdxdna_drm_alloc_dev_bo (&xdna->ddev, &args, client->filp, true); +if (IS_ERR (abo)) { + ret =PTR_ERR (abo); + goto free_cmd_bufs; + } + +XDNA_DBG (xdna,"Command buf %d addr 0x%llx size 0x%lx" , + i, abo->mem.dev_addr, abo->mem.size); + priv->cmd_buf[i] = abo; + } + + sched = &priv->sched; +mutex_init (&priv->io_lock); + ret =drm_sched_init (sched, &sched_ops,NULL ,DRM_SCHED_PRIORITY_COUNT , +HWCTX_MAX_CMDS ,0 ,msecs_to_jiffies (HWCTX_MAX_TIMEOUT ), +NULL ,NULL , hwctx->name, xdna->ddev.dev); +if (ret) { +XDNA_ERR (xdna,"Failed to init DRM scheduler. ret %d" , ret); + goto free_cmd_bufs; + } + + ret =drm_sched_entity_init (&priv->entity,DRM_SCHED_PRIORITY_NORMAL , + &sched,1 ,NULL ); +if (ret) { +XDNA_ERR (xdna,"Failed to initial sched entiry. ret %d" , ret); + goto free_sched; + } + ret =aie2_hwctx_col_list (hwctx);if (ret) {XDNA_ERR (xdna,"Create col list failed, ret %d" , ret); - goto unpin; + goto free_entity; } ret =aie2_alloc_resource (hwctx);
@@ -175,6 +606,13 @@ int aie2_hwctx_init(struct amdxdna_hwctx *hwctx)XDNA_ERR (xdna,"Map host buffer failed, ret %d" , ret); goto release_resource; } + + ret =aie2_ctx_syncobj_create (hwctx); +if (ret) { +XDNA_ERR (xdna,"Create syncobj failed, ret %d" , ret); + goto release_resource; + } + hwctx->status =HWCTX_STAT_INIT ;XDNA_DBG (xdna,"hwctx %s init completed" , hwctx->name);
@@ -185,7 +623,16 @@ int aie2_hwctx_init(struct amdxdna_hwctx *hwctx)aie2_release_resource (hwctx); free_col_list:kfree (hwctx->col_list); -unpin: +free_entity: +drm_sched_entity_destroy (&priv->entity); +free_sched: +drm_sched_fini (&priv->sched); +free_cmd_bufs: +for (i =0 ; i <ARRAY_SIZE (priv->cmd_buf); i++) { +if (!priv->cmd_buf[i]) +continue ; +drm_gem_object_put (to_gobj (priv->cmd_buf[i])); + }amdxdna_gem_unpin (heap); put_heap:drm_gem_object_put (to_gobj (heap));
@@ -196,11 +643,35 @@ int aie2_hwctx_init(struct amdxdna_hwctx *hwctx)void aie2_hwctx_fini (struct amdxdna_hwctx *hwctx) { +struct amdxdna_dev *xdna; +int idx; + + xdna = hwctx->client->xdna; +drm_sched_wqueue_stop (&hwctx->priv->sched); + +/* Now, scheduler will not send command to device. */ aie2_release_resource (hwctx); +/* +* All submitted commands are aborted. +* Restart scheduler queues to cleanup jobs. The amdxdna_sched_job_run() +* will return NODEV if it is called. +*/ +drm_sched_wqueue_start (&hwctx->priv->sched); + +aie2_hwctx_wait_for_idle (hwctx); +drm_sched_entity_destroy (&hwctx->priv->entity); +drm_sched_fini (&hwctx->priv->sched); +aie2_ctx_syncobj_destroy (hwctx); + +XDNA_DBG (xdna,"%s sequence number %lld" , hwctx->name, hwctx->priv->seq); + +for (idx =0 ; idx <ARRAY_SIZE (hwctx->priv->cmd_buf); idx++) +drm_gem_object_put (to_gobj (hwctx->priv->cmd_buf[idx]));amdxdna_gem_unpin (hwctx->priv->heap);drm_gem_object_put (to_gobj (hwctx->priv->heap)); +mutex_destroy (&hwctx->priv->io_lock);kfree (hwctx->col_list);kfree (hwctx->priv);kfree (hwctx->cus);
@@ -267,3 +738,160 @@ int aie2_hwctx_config(struct amdxdna_hwctx *hwctx, u32 type, u64 value, void *bureturn -EOPNOTSUPP ; } } + +static int aie2_populate_range (struct amdxdna_gem_obj *abo) +{ +struct amdxdna_dev *xdna =to_xdna_dev (to_gobj (abo)->dev); +struct mm_struct *mm = abo->mem.notifier.mm; +struct hmm_range range = {0 }; +unsigned long timeout; +int ret; + +XDNA_INFO_ONCE (xdna,"populate memory range %llx size %lx" , + abo->mem.userptr, abo->mem.size); + range.notifier = &abo->mem.notifier; + range.start = abo->mem.userptr; + range.end = abo->mem.userptr + abo->mem.size; + range.hmm_pfns = abo->mem.pfns; + range.default_flags =HMM_PFN_REQ_FAULT ; + +if (!mmget_not_zero (mm)) +return -EFAULT ; + + timeout = jiffies +msecs_to_jiffies (HMM_RANGE_DEFAULT_TIMEOUT ); +again: + range.notifier_seq =mmu_interval_read_begin (&abo->mem.notifier); +mmap_read_lock (mm); + ret =hmm_range_fault (&range); +mmap_read_unlock (mm); +if (ret) { +if (time_after (jiffies, timeout)) { + ret = -ETIME ; + goto put_mm; + } + +if (ret == -EBUSY ) + goto again; + + goto put_mm; + } + +read_lock (&xdna->notifier_lock); +if (mmu_interval_read_retry (&abo->mem.notifier, range.notifier_seq)) { +read_unlock (&xdna->notifier_lock); + goto again; + } + abo->mem.map_invalid = false; +read_unlock (&xdna->notifier_lock); + +put_mm: +mmput (mm); +return ret; +} + +static void aie2_hwctx_push_job (struct amdxdna_sched_job *job,u64 *seq) +{ +struct amdxdna_hwctx *hwctx = job->hwctx; + +mutex_lock (&hwctx->priv->io_lock); +drm_sched_job_arm (&job->base); + job->seq = hwctx->priv->seq++; + *seq = job->seq; + + job->out_fence =dma_fence_get (&job->base.s_fence->finished); +drm_sched_entity_push_job (&job->base); +aie2_ctx_syncobj_add_fence (hwctx, job->out_fence, *seq); +mutex_unlock (&hwctx->priv->io_lock); +} + +int aie2_cmd_submit (struct amdxdna_hwctx *hwctx,struct amdxdna_sched_job *job,u64 *seq) +{ +struct amdxdna_dev *xdna = hwctx->client->xdna; +struct ww_acquire_ctx acquire_ctx; +struct amdxdna_gem_obj *abo; +unsigned long timeout =0 ; +int ret, i; + + ret =down_interruptible (&hwctx->priv->job_sem); +if (ret) { +XDNA_ERR (xdna,"Grab job sem failed, ret %d" , ret); +return ret; + } + + ret =drm_sched_job_init (&job->base, &hwctx->priv->entity,1 , hwctx); +if (ret) { +XDNA_ERR (xdna,"DRM job init failed, ret %d" , ret); + goto up_sem; + } + +retry: + ret =drm_gem_lock_reservations (job->bos, job->bo_cnt, &acquire_ctx); +if (ret) { +XDNA_WARN (xdna,"Failed to lock BOs, ret %d" , ret); + goto cleanup_job; + } + +for (i =0 ; i < job->bo_cnt; i++) { + ret =dma_resv_reserve_fences (job->bos[i]->resv,1 ); +if (ret) { +XDNA_WARN (xdna,"Failed to reserve fences %d" , ret); +drm_gem_unlock_reservations (job->bos, job->bo_cnt, &acquire_ctx); + goto cleanup_job; + } + } + +read_lock (&xdna->notifier_lock); +for (i =0 ; i < job->bo_cnt; i++) { + abo =to_xdna_obj (job->bos[i]); +if (abo->mem.map_invalid) { +read_unlock (&xdna->notifier_lock); +drm_gem_unlock_reservations (job->bos, job->bo_cnt, &acquire_ctx); +if (!timeout) { + timeout = jiffies + +msecs_to_jiffies (HMM_RANGE_DEFAULT_TIMEOUT ); + }else if (time_after (jiffies, timeout)) { + ret = -ETIME ; + goto cleanup_job; + } + + ret =aie2_populate_range (abo); +if (ret) + goto cleanup_job; + goto retry; + } + } + +for (i =0 ; i < job->bo_cnt; i++) +dma_resv_add_fence (job->bos[i]->resv, job->fence,DMA_RESV_USAGE_WRITE ); + +read_unlock (&xdna->notifier_lock); +drm_gem_unlock_reservations (job->bos, job->bo_cnt, &acquire_ctx); + +aie2_hwctx_push_job (job, seq); + +return 0 ; + +cleanup_job: +drm_sched_job_cleanup (&job->base); +up_sem: +up (&hwctx->priv->job_sem); + job->job_done = true; +return ret; +} + +void aie2_hmm_invalidate (struct amdxdna_gem_obj *abo, +unsigned long cur_seq) +{ +struct amdxdna_dev *xdna =to_xdna_dev (to_gobj (abo)->dev); +struct drm_gem_object *gobj =to_gobj (abo); +long ret; + +write_lock (&xdna->notifier_lock); + abo->mem.map_invalid = true; +mmu_interval_set_seq (&abo->mem.notifier, cur_seq); +write_unlock (&xdna->notifier_lock); + ret =dma_resv_wait_timeout (gobj->resv,DMA_RESV_USAGE_BOOKKEEP , + true,MAX_SCHEDULE_TIMEOUT ); +if (!ret || ret == -ERESTARTSYS ) +XDNA_ERR (xdna,"Failed to wait for bo, ret %ld" , ret); +}
drivers/accel/amdxdna/aie2_message.c +343 -0
356 lines not on this page · raw message
drivers/accel/amdxdna/aie2_pci.c +5 -0
@@ -5,8 +5,10 @@#include <drm/amdxdna_accel.h> #include <drm/drm_device.h> +#include <drm/drm_gem_shmem_helper.h> #include <drm/drm_managed.h> #include <drm/drm_print.h> +#include <drm/gpu_scheduler.h> #include <linux/errno.h> #include <linux/firmware.h> #include <linux/iommu.h>
@@ -17,6 +19,7 @@#include "aie2_pci.h" #include "aie2_solver.h" #include "amdxdna_ctx.h" +#include "amdxdna_gem.h" #include "amdxdna_mailbox.h" #include "amdxdna_pci_drv.h"
@@ -495,4 +498,6 @@ const struct amdxdna_dev_ops aie2_ops = { .hwctx_init = aie2_hwctx_init, .hwctx_fini = aie2_hwctx_fini, .hwctx_config = aie2_hwctx_config, + .cmd_submit = aie2_cmd_submit, + .hmm_invalidate = aie2_hmm_invalidate, };
drivers/accel/amdxdna/aie2_pci.h +40 -0
@@ -6,6 +6,8 @@#ifndef _AIE2_PCI_H_#define _AIE2_PCI_H_ +#include <linux/semaphore.h> +#include "amdxdna_mailbox.h" #define AIE2_INTERVAL 20000/* us */
@@ -76,8 +78,10 @@ enum psp_reg_idx {PSP_MAX_REGS /* Keep this at the end */ }; +struct amdxdna_client ;struct amdxdna_fw_ver ;struct amdxdna_hwctx ; +struct amdxdna_sched_job ;struct psp_config {const void *fw_buf;
@@ -118,9 +122,31 @@ struct rt_config {u32 value; }; +/* +* Define the maximum number of pending commands in a hardware context. +* Must be power of 2! +*/ +#define HWCTX_MAX_CMDS 4 +#define get_job_idx (seq) ((seq) & (HWCTX_MAX_CMDS - 1))struct amdxdna_hwctx_priv {struct amdxdna_gem_obj *heap;void *mbox_chann; + +struct drm_gpu_scheduler sched; +struct drm_sched_entity entity; + +struct mutex io_lock;/* protect seq and cmd order */ +struct wait_queue_head job_free_wq; +u32 num_pending; +u64 seq; +struct semaphore job_sem; +bool job_done; + +/* Completed job counter */ +u64 completed; + +struct amdxdna_gem_obj *cmd_buf[HWCTX_MAX_CMDS ]; +struct drm_syncobj *syncobj; };struct amdxdna_dev_hdl {
@@ -199,10 +225,24 @@ int aie2_create_context(struct amdxdna_dev_hdl *ndev, struct amdxdna_hwctx *hwctint aie2_destroy_context (struct amdxdna_dev_hdl *ndev,struct amdxdna_hwctx *hwctx);int aie2_map_host_buf (struct amdxdna_dev_hdl *ndev,u32 context_id,u64 addr,u64 size);int aie2_config_cu (struct amdxdna_hwctx *hwctx); +int aie2_execbuf (struct amdxdna_hwctx *hwctx,struct amdxdna_sched_job *job, +int (*notify_cb)(void *,const u32 *,size_t )); +int aie2_cmdlist_single_execbuf (struct amdxdna_hwctx *hwctx, +struct amdxdna_sched_job *job, +int (*notify_cb)(void *,const u32 *,size_t )); +int aie2_cmdlist_multi_execbuf (struct amdxdna_hwctx *hwctx, +struct amdxdna_sched_job *job, +int (*notify_cb)(void *,const u32 *,size_t )); +int aie2_sync_bo (struct amdxdna_hwctx *hwctx,struct amdxdna_sched_job *job, +int (*notify_cb)(void *,const u32 *,size_t ));/* aie2_hwctx.c */ int aie2_hwctx_init (struct amdxdna_hwctx *hwctx);void aie2_hwctx_fini (struct amdxdna_hwctx *hwctx);int aie2_hwctx_config (struct amdxdna_hwctx *hwctx,u32 type,u64 value,void *buf,u32 size); +int aie2_cmd_submit (struct amdxdna_hwctx *hwctx,struct amdxdna_sched_job *job,u64 *seq); +void aie2_hmm_invalidate (struct amdxdna_gem_obj *abo,unsigned long cur_seq); +void aie2_stop_ctx_by_col_map (struct amdxdna_client *client,u32 col_map); +void aie2_restart_ctx (struct amdxdna_client *client);#endif /* _AIE2_PCI_H_ */
drivers/accel/amdxdna/aie2_psp.c +2 -0
@@ -4,8 +4,10 @@*/ #include <drm/drm_device.h> +#include <drm/drm_gem_shmem_helper.h> #include <drm/drm_managed.h> #include <drm/drm_print.h> +#include <drm/gpu_scheduler.h> #include <linux/bitfield.h> #include <linux/iopoll.h>
drivers/accel/amdxdna/aie2_smu.c +2 -0
@@ -4,7 +4,9 @@*/ #include <drm/drm_device.h> +#include <drm/drm_gem_shmem_helper.h> #include <drm/drm_print.h> +#include <drm/gpu_scheduler.h> #include <linux/iopoll.h> #include "aie2_pci.h"
drivers/accel/amdxdna/amdxdna_ctx.c +314 -6
384 lines not on this page · raw message
drivers/accel/amdxdna/amdxdna_ctx.h +111 -0
126 lines not on this page · raw message
drivers/accel/amdxdna/amdxdna_gem.c +1 -0
@@ -8,6 +8,7 @@#include <drm/drm_device.h> #include <drm/drm_gem.h> #include <drm/drm_gem_shmem_helper.h> +#include <drm/gpu_scheduler.h> #include <linux/iosys-map.h> #include <linux/vmalloc.h>
drivers/accel/amdxdna/amdxdna_mailbox_helper.c +5 -0
@@ -3,10 +3,15 @@* Copyright (C) 2024, Advanced Micro Devices, Inc. */ +#include <drm/amdxdna_accel.h> #include <drm/drm_device.h> #include <drm/drm_print.h> +#include <drm/drm_gem.h> +#include <drm/drm_gem_shmem_helper.h> +#include <drm/gpu_scheduler.h> #include <linux/completion.h> +#include "amdxdna_gem.h" #include "amdxdna_mailbox.h" #include "amdxdna_mailbox_helper.h" #include "amdxdna_pci_drv.h"
drivers/accel/amdxdna/amdxdna_pci_drv.c +12 -0
@@ -10,6 +10,7 @@#include <drm/drm_gem_shmem_helper.h> #include <drm/drm_ioctl.h> #include <drm/drm_managed.h> +#include <drm/gpu_scheduler.h> #include <linux/iommu.h> #include <linux/pci.h>
@@ -64,6 +65,7 @@ static int amdxdna_drm_open(struct drm_device *ddev, struct drm_file *filp) goto unbind_sva; }mutex_init (&client->hwctx_lock); +init_srcu_struct (&client->hwctx_srcu);idr_init_base (&client->hwctx_idr,AMDXDNA_INVALID_CTX_HANDLE +1 );mutex_init (&client->mm_lock);
@@ -93,6 +95,7 @@ static void amdxdna_drm_close(struct drm_device *ddev, struct drm_file *filp)XDNA_DBG (xdna,"closing pid %d" , client->pid);idr_destroy (&client->hwctx_idr); +cleanup_srcu_struct (&client->hwctx_srcu);mutex_destroy (&client->hwctx_lock);mutex_destroy (&client->mm_lock);if (client->dev_heap)
@@ -133,6 +136,8 @@ static const struct drm_ioctl_desc amdxdna_drm_ioctls[] = {DRM_IOCTL_DEF_DRV (AMDXDNA_CREATE_BO , amdxdna_drm_create_bo_ioctl,0 ),DRM_IOCTL_DEF_DRV (AMDXDNA_GET_BO_INFO , amdxdna_drm_get_bo_info_ioctl,0 ),DRM_IOCTL_DEF_DRV (AMDXDNA_SYNC_BO , amdxdna_drm_sync_bo_ioctl,0 ), +/* Execution */ + DRM_IOCTL_DEF_DRV(AMDXDNA_EXEC_CMD, amdxdna_drm_submit_cmd_ioctl, 0), }; static const struct file_operations amdxdna_fops = {
@@ -190,9 +195,16 @@ static int amdxdna_probe(struct pci_dev *pdev, const struct pci_device_id *id)return -ENODEV ;drmm_mutex_init (&xdna->ddev, &xdna->dev_lock); +rwlock_init (&xdna->notifier_lock);INIT_LIST_HEAD (&xdna->client_list);pci_set_drvdata (pdev, xdna); +if (IS_ENABLED (CONFIG_LOCKDEP )) { +fs_reclaim_acquire (GFP_KERNEL ); +might_lock (&xdna->notifier_lock); +fs_reclaim_release (GFP_KERNEL ); + } +mutex_lock (&xdna->dev_lock); ret = xdna->dev_info->ops->init (xdna);mutex_unlock (&xdna->dev_lock);
drivers/accel/amdxdna/amdxdna_pci_drv.h +5 -0
@@ -20,6 +20,7 @@ extern const struct drm_driver amdxdna_drm_drv;struct amdxdna_dev ;struct amdxdna_gem_obj ;struct amdxdna_hwctx ; +struct amdxdna_sched_job ;/* * struct amdxdna_dev_ops - Device hardware operation callbacks
@@ -31,6 +32,7 @@ struct amdxdna_dev_ops {void (*hwctx_fini)(struct amdxdna_hwctx *hwctx);int (*hwctx_config)(struct amdxdna_hwctx *hwctx,u32 type,u64 value,void *buf,u32 size);void (*hmm_invalidate)(struct amdxdna_gem_obj *abo,unsigned long cur_seq); +int (*cmd_submit)(struct amdxdna_hwctx *hwctx,struct amdxdna_sched_job *job,u64 *seq); };/*
@@ -69,6 +71,7 @@ struct amdxdna_dev {struct mutex dev_lock;/* per device lock */ struct list_head client_list;struct amdxdna_fw_ver fw_ver; +rwlock_t notifier_lock;/* for mmu notifier*/ };/*
@@ -88,6 +91,8 @@ struct amdxdna_client {struct list_head node;pid_t pid;struct mutex hwctx_lock;/* protect hwctx */ +/* do NOT wait this srcu when hwctx_lock is hold */ +struct srcu_struct hwctx_srcu;struct idr hwctx_idr;struct amdxdna_dev *xdna;struct drm_file *filp;
drivers/accel/amdxdna/amdxdna_sysfs.c +5 -0
@@ -3,9 +3,14 @@* Copyright (C) 2023-2024, Advanced Micro Devices, Inc. */ +#include <drm/amdxdna_accel.h> #include <drm/drm_device.h> +#include <drm/drm_gem_shmem_helper.h> #include <drm/drm_print.h> +#include <drm/gpu_scheduler.h> +#include <linux/types.h> +#include "amdxdna_gem.h" #include "amdxdna_pci_drv.h" static ssize_t vbnv_show (struct device *dev,struct device_attribute *attr,char *buf)
drivers/accel/amdxdna/npu1_regs.c +1 -0
@@ -5,6 +5,7 @@#include <drm/amdxdna_accel.h> #include <drm/drm_device.h> +#include <drm/gpu_scheduler.h> #include <linux/sizes.h> #include "aie2_pci.h"
drivers/accel/amdxdna/npu2_regs.c +1 -0
@@ -5,6 +5,7 @@#include <drm/amdxdna_accel.h> #include <drm/drm_device.h> +#include <drm/gpu_scheduler.h> #include <linux/sizes.h> #include "aie2_pci.h"
drivers/accel/amdxdna/npu4_regs.c +1 -0
@@ -5,6 +5,7 @@#include <drm/amdxdna_accel.h> #include <drm/drm_device.h> +#include <drm/gpu_scheduler.h> #include <linux/sizes.h> #include "aie2_pci.h"
drivers/accel/amdxdna/npu5_regs.c +1 -0
@@ -5,6 +5,7 @@#include <drm/amdxdna_accel.h> #include <drm/drm_device.h> +#include <drm/gpu_scheduler.h> #include <linux/sizes.h> #include "aie2_pci.h"
include/trace/events/amdxdna.h +41 -0
49 lines not on this page · raw message
include/uapi/drm/amdxdna_accel.h +38 -0
@@ -13,9 +13,11 @@extern "C" {#endif +#define AMDXDNA_INVALID_CMD_HANDLE (~0UL)#define AMDXDNA_INVALID_ADDR (~0UL)#define AMDXDNA_INVALID_CTX_HANDLE 0#define AMDXDNA_INVALID_BO_HANDLE 0 +#define AMDXDNA_INVALID_FENCE_HANDLE 0enum amdxdna_device_type {AMDXDNA_DEV_TYPE_UNKNOWN =-1 ,
@@ -29,6 +31,7 @@ enum amdxdna_drm_ioctl_id {DRM_AMDXDNA_CREATE_BO ,DRM_AMDXDNA_GET_BO_INFO ,DRM_AMDXDNA_SYNC_BO , + DRM_AMDXDNA_EXEC_CMD, };/**
@@ -201,6 +204,37 @@ struct amdxdna_drm_sync_bo {__u64 size; }; +enum amdxdna_cmd_type { +AMDXDNA_CMD_SUBMIT_EXEC_BUF =0 , +AMDXDNA_CMD_SUBMIT_DEPENDENCY , +AMDXDNA_CMD_SUBMIT_SIGNAL , +}; + +/** +* struct amdxdna_drm_exec_cmd - Execute command. +* @ext: MBZ. +* @ext_flags: MBZ. +* @hwctx: Hardware context handle. +* @type: One of command type in enum amdxdna_cmd_type. +* @cmd_handles: Array of command handles or the command handle itself +* in case of just one. +* @args: Array of arguments for all command handles. +* @cmd_count: Number of command handles in the cmd_handles array. +* @arg_count: Number of arguments in the args array. +* @seq: Returned sequence number for this command. +*/ +struct amdxdna_drm_exec_cmd { +__u64 ext; +__u64 ext_flags; +__u32 hwctx; +__u32 type; +__u64 cmd_handles; +__u64 args; +__u32 cmd_count; +__u32 arg_count; +__u64 seq; +}; +#define DRM_IOCTL_AMDXDNA_CREATE_HWCTX \ DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDXDNA_CREATE_HWCTX, \ struct amdxdna_drm_create_hwctx)
@@ -225,6 +259,10 @@ struct amdxdna_drm_sync_bo {DRM_IOWR (DRM_COMMAND_BASE +DRM_AMDXDNA_SYNC_BO , \ struct amdxdna_drm_sync_bo) +#define DRM_IOCTL_AMDXDNA_EXEC_CMD \ + DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDXDNA_EXEC_CMD, \ + struct amdxdna_drm_exec_cmd) +#if defined(__cplusplus) } /* extern c end */#endif
Signature · 1 line
2.34.1
Re: [PATCH V7 07/10] accel/amdxdna: Add command execution
Quoted 430 lines · Lizhi Hou
Add interfaces for user application to submit command and wait for its completion.
Co-developed-by Min Ma <[email protected]>Signed-off-by Min Ma <[email protected]>Signed-off-by Lizhi Hou <[email protected]>--- drivers/accel/amdxdna/aie2_ctx.c | 634 +++++++++++++++++- drivers/accel/amdxdna/aie2_message.c | 343 ++++++++++ drivers/accel/amdxdna/aie2_pci.c | 5 + drivers/accel/amdxdna/aie2_pci.h | 40 ++ drivers/accel/amdxdna/aie2_psp.c | 2 + drivers/accel/amdxdna/aie2_smu.c | 2 + drivers/accel/amdxdna/amdxdna_ctx.c | 320 ++++++++- drivers/accel/amdxdna/amdxdna_ctx.h | 111 +++ drivers/accel/amdxdna/amdxdna_gem.c | 1 + .../accel/amdxdna/amdxdna_mailbox_helper.c | 5 + drivers/accel/amdxdna/amdxdna_pci_drv.c | 12 + drivers/accel/amdxdna/amdxdna_pci_drv.h | 5 + drivers/accel/amdxdna/amdxdna_sysfs.c | 5 + drivers/accel/amdxdna/npu1_regs.c | 1 + drivers/accel/amdxdna/npu2_regs.c | 1 + drivers/accel/amdxdna/npu4_regs.c | 1 + drivers/accel/amdxdna/npu5_regs.c | 1 + include/trace/events/amdxdna.h | 41 ++ include/uapi/drm/amdxdna_accel.h | 38 ++ 19 files changed, 1559 insertions(+), 9 deletions(-)drivers/accel/amdxdna/aie2_ctx.c +378
-0@@ -8,8 +8,12 @@#include <drm/drm_gem.h> #include <drm/drm_gem_shmem_helper.h> #include <drm/drm_print.h> +#include <drm/drm_syncobj.h> +#include <linux/hmm.h> #include <linux/types.h> +#include <trace/events/amdxdna.h> +#include "aie2_msg_priv.h" #include "aie2_pci.h" #include "aie2_solver.h" #include "amdxdna_ctx.h" @@ -17,6 +21,342 @@#include "amdxdna_mailbox.h" #include "amdxdna_pci_drv.h" +bool force_cmdlist; +module_param (force_cmdlist, bool,0600 ); +MODULE_PARM_DESC (force_cmdlist,"Force use command list (Default false)" ); + +#define HWCTX_MAX_TIMEOUT 60000/* milliseconds */ + +static void aie2_job_release (struct kref *ref) +{ +struct amdxdna_sched_job *job; + + job = container_of(ref, struct amdxdna_sched_job, refcnt); +amdxdna_sched_job_cleanup (job); +kfree (job); +} + +static void aie2_job_put (struct amdxdna_sched_job *job) +{ +kref_put (&job->refcnt, aie2_job_release); +} + +/* The bad_job is used in aie2_sched_job_timedout, otherwise, set it to NULL */ +static void aie2_hwctx_stop (struct amdxdna_dev *xdna,struct amdxdna_hwctx *hwctx, +struct drm_sched_job *bad_job) +{ +drm_sched_stop (&hwctx->priv->sched, bad_job); +aie2_destroy_context (xdna->dev_handle, hwctx); +} + +static int aie2_hwctx_restart (struct amdxdna_dev *xdna,struct amdxdna_hwctx *hwctx) +{ +struct amdxdna_gem_obj *heap = hwctx->priv->heap; +int ret; + + ret =aie2_create_context (xdna->dev_handle, hwctx); +if (ret) { +XDNA_ERR (xdna,"Create hwctx failed, ret %d" , ret); + goto out; + } + + ret =aie2_map_host_buf (xdna->dev_handle, hwctx->fw_ctx_id, + heap->mem.userptr, heap->mem.size); +if (ret) { +XDNA_ERR (xdna,"Map host buf failed, ret %d" , ret); + goto out; + } + +if (hwctx->status !=HWCTX_STAT_READY ) { +XDNA_DBG (xdna,"hwctx is not ready, status %d" , hwctx->status); + goto out; + } + + ret =aie2_config_cu (hwctx); +if (ret) { +XDNA_ERR (xdna,"Config cu failed, ret %d" , ret); + goto out; + } + +out: +drm_sched_start (&hwctx->priv->sched); +XDNA_DBG (xdna,"%s restarted, ret %d" , hwctx->name, ret); +return ret; +} + +void aie2_stop_ctx_by_col_map (struct amdxdna_client *client,u32 col_map) +{ +struct amdxdna_dev *xdna = client->xdna; +struct amdxdna_hwctx *hwctx; +int next =0 ; + +drm_WARN_ON (&xdna->ddev, !mutex_is_locked (&xdna->dev_lock)); +mutex_lock (&client->hwctx_lock); +idr_for_each_entry_continue (&client->hwctx_idr, hwctx, next) { +/* check if the HW context uses the error column */ +if (!(col_map &amdxdna_hwctx_col_map (hwctx))) +continue ; + +aie2_hwctx_stop (xdna, hwctx,NULL ); + hwctx->old_status = hwctx->status; + hwctx->status =HWCTX_STAT_STOP ; +XDNA_DBG (xdna,"Stop %s" , hwctx->name); + } +mutex_unlock (&client->hwctx_lock); +} + +void aie2_restart_ctx (struct amdxdna_client *client) +{ +struct amdxdna_dev *xdna = client->xdna; +struct amdxdna_hwctx *hwctx; +int next =0 ; + +drm_WARN_ON (&xdna->ddev, !mutex_is_locked (&xdna->dev_lock)); +mutex_lock (&client->hwctx_lock); +idr_for_each_entry_continue (&client->hwctx_idr, hwctx, next) { +if (hwctx->status !=HWCTX_STAT_STOP ) +continue ; + + hwctx->status = hwctx->old_status; +XDNA_DBG (xdna,"Resetting %s" , hwctx->name); +aie2_hwctx_restart (xdna, hwctx); + } +mutex_unlock (&client->hwctx_lock); +} + +static struct dma_fence *aie2_cmd_get_out_fence (struct amdxdna_hwctx *hwctx,u64 seq) +{ +struct dma_fence *fence, *out_fence =NULL ; +int ret; + + fence =drm_syncobj_fence_get (hwctx->priv->syncobj); +if (!fence) +return NULL ; + + ret =dma_fence_chain_find_seqno (&fence, seq); +if (ret) + goto out; + + out_fence =dma_fence_get (dma_fence_chain_contained (fence)); + +out: +dma_fence_put (fence); +return out_fence; +} + +static void aie2_hwctx_wait_for_idle (struct amdxdna_hwctx *hwctx) +{ +struct dma_fence *fence; + + fence =aie2_cmd_get_out_fence (hwctx, hwctx->priv->seq -1 ); +if (!fence) +return ; + +dma_fence_wait (fence, false); +dma_fence_put (fence); +} + +static void +aie2_sched_notify (struct amdxdna_sched_job *job) +{ +struct dma_fence *fence = job->fence; + +trace_xdna_job (&job->base, job->hwctx->name,"signaled fence" , job->seq); + job->hwctx->priv->completed++; +dma_fence_signal (fence); + +up (&job->hwctx->priv->job_sem); + job->job_done = true; +dma_fence_put (fence); +mmput (job->mm); +aie2_job_put (job); +} + +static int +aie2_sched_resp_handler (void *handle,const u32 *data,size_t size) +{ +struct amdxdna_sched_job *job = handle; +struct amdxdna_gem_obj *cmd_abo; +u32 ret =0 ; +u32 status; + + cmd_abo = job->cmd_bo; + +if (unlikely (!data)) + goto out; + +if (unlikely (size !=sizeof (u32))) { +amdxdna_cmd_set_state (cmd_abo,ERT_CMD_STATE_ABORT ); + ret = -EINVAL ; + goto out; + } + + status = *data; +XDNA_DBG (job->hwctx->client->xdna,"Resp status 0x%x" , status); +if (status ==AIE2_STATUS_SUCCESS ) +amdxdna_cmd_set_state (cmd_abo,ERT_CMD_STATE_COMPLETED ); +else +amdxdna_cmd_set_state (cmd_abo,ERT_CMD_STATE_ERROR ); + +out: +aie2_sched_notify (job); +return ret; +} + +static int +aie2_sched_nocmd_resp_handler (void *handle,const u32 *data,size_t size) +{ +struct amdxdna_sched_job *job = handle; +u32 ret =0 ; +u32 status; + +if (unlikely (!data)) + goto out; + +if (unlikely (size !=sizeof (u32))) { + ret = -EINVAL ; + goto out; + } + + status = *data; +XDNA_DBG (job->hwctx->client->xdna,"Resp status 0x%x" , status); + +out: +aie2_sched_notify (job); +return ret; +} + +static int +aie2_sched_cmdlist_resp_handler (void *handle,const u32 *data,size_t size) +{ +struct amdxdna_sched_job *job = handle; +struct amdxdna_gem_obj *cmd_abo; +struct cmd_chain_resp *resp; +struct amdxdna_dev *xdna; +u32 fail_cmd_status; +u32 fail_cmd_idx; +u32 ret =0 ; + + cmd_abo = job->cmd_bo; +if (unlikely (!data) ||unlikely (size !=sizeof (u32) *3 )) { +amdxdna_cmd_set_state (cmd_abo,ERT_CMD_STATE_ABORT ); + ret = -EINVAL ; + goto out; + } + + resp = (struct cmd_chain_resp *)data; + xdna = job->hwctx->client->xdna; +XDNA_DBG (xdna,"Status 0x%x" , resp->status); +if (resp->status ==AIE2_STATUS_SUCCESS ) { +amdxdna_cmd_set_state (cmd_abo,ERT_CMD_STATE_COMPLETED ); + goto out; + } + +/* Slow path to handle error, read from ringbuf on BAR */ + fail_cmd_idx = resp->fail_cmd_idx; + fail_cmd_status = resp->fail_cmd_status; +XDNA_DBG (xdna,"Failed cmd idx %d, status 0x%x" , + fail_cmd_idx, fail_cmd_status); + +if (fail_cmd_status ==AIE2_STATUS_SUCCESS ) { +amdxdna_cmd_set_state (cmd_abo,ERT_CMD_STATE_ABORT ); + ret = -EINVAL ; + goto out; + } +amdxdna_cmd_set_state (cmd_abo, fail_cmd_status); + +if (amdxdna_cmd_get_op (cmd_abo) ==ERT_CMD_CHAIN ) { +struct amdxdna_cmd_chain *cc =amdxdna_cmd_get_payload (cmd_abo,NULL ); + + cc->error_index = fail_cmd_idx; +if (cc->error_index >= cc->command_count) + cc->error_index =0 ; + } +out: +aie2_sched_notify (job); +return ret; +} + +static struct dma_fence * +aie2_sched_job_run (struct drm_sched_job *sched_job) +{ +struct amdxdna_sched_job *job =drm_job_to_xdna_job (sched_job); +struct amdxdna_gem_obj *cmd_abo = job->cmd_bo; +struct amdxdna_hwctx *hwctx = job->hwctx; +struct dma_fence *fence; +int ret; + +if (!mmget_not_zero (job->mm)) +return ERR_PTR (-ESRCH ); + +kref_get (&job->refcnt); + fence =dma_fence_get (job->fence); + +if (unlikely (!cmd_abo)) { + ret =aie2_sync_bo (hwctx, job, aie2_sched_nocmd_resp_handler); + goto out; + } + +amdxdna_cmd_set_state (cmd_abo,ERT_CMD_STATE_NEW ); + +if (amdxdna_cmd_get_op (cmd_abo) ==ERT_CMD_CHAIN ) + ret =aie2_cmdlist_multi_execbuf (hwctx, job, aie2_sched_cmdlist_resp_handler); +else if (force_cmdlist) + ret =aie2_cmdlist_single_execbuf (hwctx, job, aie2_sched_cmdlist_resp_handler); +else + ret =aie2_execbuf (hwctx, job, aie2_sched_resp_handler); + +out: +if (ret) { +dma_fence_put (job->fence); +aie2_job_put (job); +mmput (job->mm); + fence =ERR_PTR (ret); + } +trace_xdna_job (sched_job, hwctx->name,"sent to device" , job->seq); + +return fence; +} + +static void aie2_sched_job_free (struct drm_sched_job *sched_job) +{ +struct amdxdna_sched_job *job =drm_job_to_xdna_job (sched_job); +struct amdxdna_hwctx *hwctx = job->hwctx; + +trace_xdna_job (sched_job, hwctx->name,"job free" , job->seq); +if (!job->job_done) +up (&hwctx->priv->job_sem); + +if (job->out_fence) +dma_fence_put (job->out_fence); +drm_sched_job_cleanup (sched_job); +aie2_job_put (job); +} + +static enum drm_gpu_sched_stat +aie2_sched_job_timedout (struct drm_sched_job *sched_job) +{ +struct amdxdna_sched_job *job =drm_job_to_xdna_job (sched_job); +struct amdxdna_hwctx *hwctx = job->hwctx; +struct amdxdna_dev *xdna; + + xdna = hwctx->client->xdna; +trace_xdna_job (sched_job, hwctx->name,"job timedout" , job->seq); +mutex_lock (&xdna->dev_lock); +aie2_hwctx_stop (xdna, hwctx, sched_job); + +aie2_hwctx_restart (xdna, hwctx); +mutex_unlock (&xdna->dev_lock); + +return DRM_GPU_SCHED_STAT_NOMINAL ; +} + +const struct drm_sched_backend_ops sched_ops = { + .run_job = aie2_sched_job_run, + .free_job = aie2_sched_job_free, + .timedout_job = aie2_sched_job_timedout, +}; +static int aie2_hwctx_col_list (struct amdxdna_hwctx *hwctx) {struct amdxdna_dev *xdna = hwctx->client->xdna;@@ -126,13 +466,66 @@ static void aie2_release_resource(struct amdxdna_hwctx *hwctx)XDNA_ERR (xdna,"Release AIE resource failed, ret %d" , ret); } +static int aie2_ctx_syncobj_create (struct amdxdna_hwctx *hwctx) +{ +struct amdxdna_dev *xdna = hwctx->client->xdna; +struct drm_file *filp = hwctx->client->filp; +struct drm_syncobj *syncobj; +u32 hdl; +int ret; + + hwctx->syncobj_hdl =AMDXDNA_INVALID_FENCE_HANDLE ; + + ret =drm_syncobj_create (&syncobj,0 ,NULL ); +if (ret) { +XDNA_ERR (xdna,"Create ctx syncobj failed, ret %d" , ret); +return ret; + } + ret =drm_syncobj_get_handle (filp, syncobj, &hdl); +if (ret) { +drm_syncobj_put (syncobj); +XDNA_ERR (xdna,"Create ctx syncobj handle failed, ret %d" , ret); +return ret; + } + hwctx->priv->syncobj = syncobj; + hwctx->syncobj_hdl = hdl; + +return 0 ; +} + +static void aie2_ctx_syncobj_destroy (struct amdxdna_hwctx *hwctx) +{ +/* +* The syncobj_hdl is owned by user space and will be cleaned up +* separately. +*/ +drm_syncobj_put (hwctx->priv->syncobj); +} + +static void aie2_ctx_syncobj_add_fence (struct amdxdna_hwctx *hwctx, +struct dma_fence *ofence,u64 seq)
drivers/accel/amdxdna/aie2_ctx.c static void aie2_release_resource(struct amdxdna_hwctx *hwctx) · line 516
Quoted 10 lines
drivers/accel/amdxdna/aie2_ctx.c +10
-0+{ +struct drm_syncobj *syncobj = hwctx->priv->syncobj; +struct dma_fence_chain *chain; + +if (!syncobj) +return ; + + chain =dma_fence_chain_alloc (); +if (!chain) +return ;
You have very subtlety broken dma-fencing rules. This is going to create a lock chain of:
mutex_lock(&hwctx->priv->io_lock)recliam();mutex_unlock(&hwctx->priv->io_lock)
But you published the dma-fence for the job which is in the path of reclaim. Lockdep should complain if you have all the correct annotations and in theory you can deadlock.
So I think you prealloc the chain() before publishing the dma-fence and then I'd also prime 'hwctx->priv->io_lock' which a reclaim annotation.
e.g.
fs_reclaim_acquire();might_lock(&hwctx->priv->io_lock);fs_reclaim_release();
Quoted 258 lines
drivers/accel/amdxdna/aie2_ctx.c +199
-3+ +drm_syncobj_add_point (syncobj, chain, ofence, seq); +} +int aie2_hwctx_init (struct amdxdna_hwctx *hwctx) {struct amdxdna_client *client = hwctx->client;struct amdxdna_dev *xdna = client->xdna; +struct drm_gpu_scheduler *sched;struct amdxdna_hwctx_priv *priv;struct amdxdna_gem_obj *heap; -int ret; +int i, ret; priv =kzalloc (sizeof (*hwctx->priv),GFP_KERNEL );if (!priv)@@ -150,6 +543,7 @@ int aie2_hwctx_init(struct amdxdna_hwctx *hwctx)drm_gem_object_get (to_gobj (heap));mutex_unlock (&client->mm_lock); priv->heap = heap; +sema_init (&priv->job_sem,HWCTX_MAX_CMDS ); ret =amdxdna_gem_pin (heap);if (ret) {@@ -157,10 +551,47 @@ int aie2_hwctx_init(struct amdxdna_hwctx *hwctx) goto put_heap; } +for (i =0 ; i <ARRAY_SIZE (priv->cmd_buf); i++) { +struct amdxdna_gem_obj *abo; +struct amdxdna_drm_create_bo args = { + .flags =0 , + .type =AMDXDNA_BO_DEV , + .vaddr =0 , + .size =MAX_CHAIN_CMDBUF_SIZE , + }; + + abo =amdxdna_drm_alloc_dev_bo (&xdna->ddev, &args, client->filp, true); +if (IS_ERR (abo)) { + ret =PTR_ERR (abo); + goto free_cmd_bufs; + } + +XDNA_DBG (xdna,"Command buf %d addr 0x%llx size 0x%lx" , + i, abo->mem.dev_addr, abo->mem.size); + priv->cmd_buf[i] = abo; + } + + sched = &priv->sched; +mutex_init (&priv->io_lock); + ret =drm_sched_init (sched, &sched_ops,NULL ,DRM_SCHED_PRIORITY_COUNT , +HWCTX_MAX_CMDS ,0 ,msecs_to_jiffies (HWCTX_MAX_TIMEOUT ), +NULL ,NULL , hwctx->name, xdna->ddev.dev); +if (ret) { +XDNA_ERR (xdna,"Failed to init DRM scheduler. ret %d" , ret); + goto free_cmd_bufs; + } + + ret =drm_sched_entity_init (&priv->entity,DRM_SCHED_PRIORITY_NORMAL , + &sched,1 ,NULL ); +if (ret) { +XDNA_ERR (xdna,"Failed to initial sched entiry. ret %d" , ret); + goto free_sched; + } + ret =aie2_hwctx_col_list (hwctx);if (ret) {XDNA_ERR (xdna,"Create col list failed, ret %d" , ret); - goto unpin; + goto free_entity; } ret =aie2_alloc_resource (hwctx);@@ -175,6 +606,13 @@ int aie2_hwctx_init(struct amdxdna_hwctx *hwctx)XDNA_ERR (xdna,"Map host buffer failed, ret %d" , ret); goto release_resource; } + + ret =aie2_ctx_syncobj_create (hwctx); +if (ret) { +XDNA_ERR (xdna,"Create syncobj failed, ret %d" , ret); + goto release_resource; + } + hwctx->status =HWCTX_STAT_INIT ;XDNA_DBG (xdna,"hwctx %s init completed" , hwctx->name);@@ -185,7 +623,16 @@ int aie2_hwctx_init(struct amdxdna_hwctx *hwctx)aie2_release_resource (hwctx); free_col_list:kfree (hwctx->col_list); -unpin: +free_entity: +drm_sched_entity_destroy (&priv->entity); +free_sched: +drm_sched_fini (&priv->sched); +free_cmd_bufs: +for (i =0 ; i <ARRAY_SIZE (priv->cmd_buf); i++) { +if (!priv->cmd_buf[i]) +continue ; +drm_gem_object_put (to_gobj (priv->cmd_buf[i])); + }amdxdna_gem_unpin (heap); put_heap:drm_gem_object_put (to_gobj (heap));@@ -196,11 +643,35 @@ int aie2_hwctx_init(struct amdxdna_hwctx *hwctx)void aie2_hwctx_fini (struct amdxdna_hwctx *hwctx) { +struct amdxdna_dev *xdna; +int idx; + + xdna = hwctx->client->xdna; +drm_sched_wqueue_stop (&hwctx->priv->sched); + +/* Now, scheduler will not send command to device. */ aie2_release_resource (hwctx); +/* +* All submitted commands are aborted. +* Restart scheduler queues to cleanup jobs. The amdxdna_sched_job_run() +* will return NODEV if it is called. +*/ +drm_sched_wqueue_start (&hwctx->priv->sched); + +aie2_hwctx_wait_for_idle (hwctx); +drm_sched_entity_destroy (&hwctx->priv->entity); +drm_sched_fini (&hwctx->priv->sched); +aie2_ctx_syncobj_destroy (hwctx); + +XDNA_DBG (xdna,"%s sequence number %lld" , hwctx->name, hwctx->priv->seq); + +for (idx =0 ; idx <ARRAY_SIZE (hwctx->priv->cmd_buf); idx++) +drm_gem_object_put (to_gobj (hwctx->priv->cmd_buf[idx]));amdxdna_gem_unpin (hwctx->priv->heap);drm_gem_object_put (to_gobj (hwctx->priv->heap)); +mutex_destroy (&hwctx->priv->io_lock);kfree (hwctx->col_list);kfree (hwctx->priv);kfree (hwctx->cus);@@ -267,3 +738,160 @@ int aie2_hwctx_config(struct amdxdna_hwctx *hwctx, u32 type, u64 value, void *bureturn -EOPNOTSUPP ; } } + +static int aie2_populate_range (struct amdxdna_gem_obj *abo) +{ +struct amdxdna_dev *xdna =to_xdna_dev (to_gobj (abo)->dev); +struct mm_struct *mm = abo->mem.notifier.mm; +struct hmm_range range = {0 }; +unsigned long timeout; +int ret; + +XDNA_INFO_ONCE (xdna,"populate memory range %llx size %lx" , + abo->mem.userptr, abo->mem.size); + range.notifier = &abo->mem.notifier; + range.start = abo->mem.userptr; + range.end = abo->mem.userptr + abo->mem.size; + range.hmm_pfns = abo->mem.pfns; + range.default_flags =HMM_PFN_REQ_FAULT ; + +if (!mmget_not_zero (mm)) +return -EFAULT ; + + timeout = jiffies +msecs_to_jiffies (HMM_RANGE_DEFAULT_TIMEOUT ); +again: + range.notifier_seq =mmu_interval_read_begin (&abo->mem.notifier); +mmap_read_lock (mm); + ret =hmm_range_fault (&range); +mmap_read_unlock (mm); +if (ret) { +if (time_after (jiffies, timeout)) { + ret = -ETIME ; + goto put_mm; + } + +if (ret == -EBUSY ) + goto again; + + goto put_mm; + } + +read_lock (&xdna->notifier_lock); +if (mmu_interval_read_retry (&abo->mem.notifier, range.notifier_seq)) { +read_unlock (&xdna->notifier_lock); + goto again; + } + abo->mem.map_invalid = false; +read_unlock (&xdna->notifier_lock); + +put_mm: +mmput (mm); +return ret; +} + +static void aie2_hwctx_push_job (struct amdxdna_sched_job *job,u64 *seq) +{ +struct amdxdna_hwctx *hwctx = job->hwctx; + +mutex_lock (&hwctx->priv->io_lock); +drm_sched_job_arm (&job->base); + job->seq = hwctx->priv->seq++; + *seq = job->seq; + + job->out_fence =dma_fence_get (&job->base.s_fence->finished); +drm_sched_entity_push_job (&job->base); +aie2_ctx_syncobj_add_fence (hwctx, job->out_fence, *seq); +mutex_unlock (&hwctx->priv->io_lock); +} + +int aie2_cmd_submit (struct amdxdna_hwctx *hwctx,struct amdxdna_sched_job *job,u64 *seq) +{ +struct amdxdna_dev *xdna = hwctx->client->xdna; +struct ww_acquire_ctx acquire_ctx; +struct amdxdna_gem_obj *abo; +unsigned long timeout =0 ; +int ret, i; + + ret =down_interruptible (&hwctx->priv->job_sem); +if (ret) { +XDNA_ERR (xdna,"Grab job sem failed, ret %d" , ret); +return ret; + } + + ret =drm_sched_job_init (&job->base, &hwctx->priv->entity,1 , hwctx); +if (ret) { +XDNA_ERR (xdna,"DRM job init failed, ret %d" , ret); + goto up_sem; + } + +retry: + ret =drm_gem_lock_reservations (job->bos, job->bo_cnt, &acquire_ctx); +if (ret) { +XDNA_WARN (xdna,"Failed to lock BOs, ret %d" , ret); + goto cleanup_job; + } + +for (i =0 ; i < job->bo_cnt; i++) { + ret =dma_resv_reserve_fences (job->bos[i]->resv,1 ); +if (ret) { +XDNA_WARN (xdna,"Failed to reserve fences %d" , ret); +drm_gem_unlock_reservations (job->bos, job->bo_cnt, &acquire_ctx); + goto cleanup_job; + } + } + +read_lock (&xdna->notifier_lock); +for (i =0 ; i < job->bo_cnt; i++) { + abo =to_xdna_obj (job->bos[i]); +if (abo->mem.map_invalid) { +read_unlock (&xdna->notifier_lock); +drm_gem_unlock_reservations (job->bos, job->bo_cnt, &acquire_ctx); +if (!timeout) { + timeout = jiffies + +msecs_to_jiffies (HMM_RANGE_DEFAULT_TIMEOUT ); + }else if (time_after (jiffies, timeout)) { + ret = -ETIME ;
drivers/accel/amdxdna/aie2_ctx.c int aie2_hwctx_config(struct amdxdna_hwctx *hwctx, u32 type, u64 value, void *bu · line 863
Quoted 10 lines
drivers/accel/amdxdna/aie2_ctx.c +10
-0+ goto cleanup_job; + } + + ret =aie2_populate_range (abo); +if (ret) + goto cleanup_job; + goto retry; + } + } +
Any reason you can't arm the job here and install job's finished fence in dma-resv slot? IIRC at one point in time in Xe we used the 'job->fence', this is a hardware fence in Xe, assuming this the same here and it ended up causing some issues - exactly what I cannot recall. The scheduler again is really designed to install the job's finished fence in dma-resv / syncobjs rather than driver internal fences.
That of course would mean taking hwctx->priv->io_lock underneath all other locks in this loop at the final step.
Matt
Quoted 1,383 lines
drivers/accel/amdxdna/aie2_ctx.c +34
-0+for (i =0 ; i < job->bo_cnt; i++) +dma_resv_add_fence (job->bos[i]->resv, job->fence,DMA_RESV_USAGE_WRITE ); + +read_unlock (&xdna->notifier_lock); +drm_gem_unlock_reservations (job->bos, job->bo_cnt, &acquire_ctx); + +aie2_hwctx_push_job (job, seq); + +return 0 ; + +cleanup_job: +drm_sched_job_cleanup (&job->base); +up_sem: +up (&hwctx->priv->job_sem); + job->job_done = true; +return ret; +} + +void aie2_hmm_invalidate (struct amdxdna_gem_obj *abo, +unsigned long cur_seq) +{ +struct amdxdna_dev *xdna =to_xdna_dev (to_gobj (abo)->dev); +struct drm_gem_object *gobj =to_gobj (abo); +long ret; + +write_lock (&xdna->notifier_lock); + abo->mem.map_invalid = true; +mmu_interval_set_seq (&abo->mem.notifier, cur_seq); +write_unlock (&xdna->notifier_lock); + ret =dma_resv_wait_timeout (gobj->resv,DMA_RESV_USAGE_BOOKKEEP , + true,MAX_SCHEDULE_TIMEOUT ); +if (!ret || ret == -ERESTARTSYS ) +XDNA_ERR (xdna,"Failed to wait for bo, ret %ld" , ret); +}drivers/accel/amdxdna/aie2_message.c +343
-0@@ -4,10 +4,12 @@*/ #include <drm/amdxdna_accel.h> +#include <drm/drm_cache.h> #include <drm/drm_device.h> #include <drm/drm_gem.h> #include <drm/drm_gem_shmem_helper.h> #include <drm/drm_print.h> +#include <drm/gpu_scheduler.h> #include <linux/bitfield.h> #include <linux/errno.h> #include <linux/pci.h> @@ -362,3 +364,344 @@ int aie2_config_cu(struct amdxdna_hwctx *hwctx) msg.opcode, resp.status, ret);return ret; } + +int aie2_execbuf (struct amdxdna_hwctx *hwctx,struct amdxdna_sched_job *job, +int (*notify_cb)(void *,const u32 *,size_t )) +{ +struct mailbox_channel *chann = hwctx->priv->mbox_chann; +struct amdxdna_dev *xdna = hwctx->client->xdna; +struct amdxdna_gem_obj *cmd_abo = job->cmd_bo; +union { +struct execute_buffer_req ebuf; +struct exec_dpu_req dpu; + } req; +struct xdna_mailbox_msg msg; +u32 payload_len; +void *payload; +int cu_idx; +int ret; +u32 op; + +if (!chann) +return -ENODEV ; + + payload =amdxdna_cmd_get_payload (cmd_abo, &payload_len); +if (!payload) { +XDNA_ERR (xdna,"Invalid command, cannot get payload" ); +return -EINVAL ; + } + + cu_idx =amdxdna_cmd_get_cu_idx (cmd_abo); +if (cu_idx <0 ) { +XDNA_DBG (xdna,"Invalid cu idx" ); +return -EINVAL ; + } + + op =amdxdna_cmd_get_op (cmd_abo); +switch (op) { +case ERT_START_CU : +if (unlikely (payload_len >sizeof (req.ebuf.payload))) +XDNA_DBG (xdna,"Invalid ebuf payload len: %d" , payload_len); + req.ebuf.cu_idx = cu_idx; +memcpy (req.ebuf.payload, payload,sizeof (req.ebuf.payload)); + msg.send_size =sizeof (req.ebuf); + msg.opcode =MSG_OP_EXECUTE_BUFFER_CF ; +break ; +case ERT_START_NPU : { +struct amdxdna_cmd_start_npu *sn = payload; + +if (unlikely (payload_len -sizeof (*sn) >sizeof (req.dpu.payload))) +XDNA_DBG (xdna,"Invalid dpu payload len: %d" , payload_len); + req.dpu.inst_buf_addr = sn->buffer; + req.dpu.inst_size = sn->buffer_size; + req.dpu.inst_prop_cnt = sn->prop_count; + req.dpu.cu_idx = cu_idx; +memcpy (req.dpu.payload, sn->prop_args,sizeof (req.dpu.payload)); + msg.send_size =sizeof (req.dpu); + msg.opcode =MSG_OP_EXEC_DPU ; +break ; + } +default : +XDNA_DBG (xdna,"Invalid ERT cmd op code: %d" , op); +return -EINVAL ; + } + msg.handle = job; + msg.notify_cb = notify_cb; + msg.send_data = (u8 *)&req; +print_hex_dump_debug ("cmd: " ,DUMP_PREFIX_OFFSET ,16 ,4 , &req, +0x40 , false); + + ret =xdna_mailbox_send_msg (chann, &msg,TX_TIMEOUT ); +if (ret) { +XDNA_ERR (xdna,"Send message failed" ); +return ret; + } + +return 0 ; +} + +static int +aie2_cmdlist_fill_one_slot_cf (void *cmd_buf,u32 offset, +struct amdxdna_gem_obj *abo,u32 *size) +{ +struct cmd_chain_slot_execbuf_cf *buf = cmd_buf + offset; +int cu_idx =amdxdna_cmd_get_cu_idx (abo); +u32 payload_len; +void *payload; + +if (cu_idx <0 ) +return -EINVAL ; + + payload =amdxdna_cmd_get_payload (abo, &payload_len); +if (!payload) +return -EINVAL ; + +if (!slot_cf_has_space (offset, payload_len)) +return -ENOSPC ; + + buf->cu_idx = cu_idx; + buf->arg_cnt = payload_len /sizeof (u32); +memcpy (buf->args, payload, payload_len); +/* Accurate buf size to hint firmware to do necessary copy */ + *size =sizeof (*buf) + payload_len; +return 0 ; +} + +static int +aie2_cmdlist_fill_one_slot_dpu (void *cmd_buf,u32 offset, +struct amdxdna_gem_obj *abo,u32 *size) +{ +struct cmd_chain_slot_dpu *buf = cmd_buf + offset; +int cu_idx =amdxdna_cmd_get_cu_idx (abo); +struct amdxdna_cmd_start_npu *sn; +u32 payload_len; +void *payload; +u32 arg_sz; + +if (cu_idx <0 ) +return -EINVAL ; + + payload =amdxdna_cmd_get_payload (abo, &payload_len); +if (!payload) +return -EINVAL ; + sn = payload; + arg_sz = payload_len -sizeof (*sn); +if (payload_len <sizeof (*sn) || arg_sz >MAX_DPU_ARGS_SIZE ) +return -EINVAL ; + +if (!slot_dpu_has_space (offset, arg_sz)) +return -ENOSPC ; + + buf->inst_buf_addr = sn->buffer; + buf->inst_size = sn->buffer_size; + buf->inst_prop_cnt = sn->prop_count; + buf->cu_idx = cu_idx; + buf->arg_cnt = arg_sz /sizeof (u32); +memcpy (buf->args, sn->prop_args, arg_sz); + +/* Accurate buf size to hint firmware to do necessary copy */ + *size +=sizeof (*buf) + arg_sz; +return 0 ; +} + +static int +aie2_cmdlist_fill_one_slot (u32 op,struct amdxdna_gem_obj *cmdbuf_abo,u32 offset, +struct amdxdna_gem_obj *abo,u32 *size) +{ +u32 this_op =amdxdna_cmd_get_op (abo); +void *cmd_buf = cmdbuf_abo->mem.kva; +int ret; + +if (this_op != op) { + ret = -EINVAL ; + goto done; + } + +switch (op) { +case ERT_START_CU : + ret =aie2_cmdlist_fill_one_slot_cf (cmd_buf, offset, abo, size); +break ; +case ERT_START_NPU : + ret =aie2_cmdlist_fill_one_slot_dpu (cmd_buf, offset, abo, size); +break ; +default : + ret = -EOPNOTSUPP ; + } + +done: +if (ret) { +XDNA_ERR (abo->client->xdna,"Can't fill slot for cmd op %d ret %d" , + op, ret); + } +return ret; +} + +static inline struct amdxdna_gem_obj * +aie2_cmdlist_get_cmd_buf (struct amdxdna_sched_job *job) +{ +int idx =get_job_idx (job->seq); + +return job->hwctx->priv->cmd_buf[idx]; +} + +static void +aie2_cmdlist_prepare_request (struct cmd_chain_req *req, +struct amdxdna_gem_obj *cmdbuf_abo,u32 size,u32 cnt) +{ + req->buf_addr = cmdbuf_abo->mem.dev_addr; + req->buf_size = size; + req->count = cnt; +drm_clflush_virt_range (cmdbuf_abo->mem.kva, size); +XDNA_DBG (cmdbuf_abo->client->xdna,"Command buf addr 0x%llx size 0x%x count %d" , + req->buf_addr, size, cnt); +} + +static inline u32 +aie2_cmd_op_to_msg_op (u32 op) +{ +switch (op) { +case ERT_START_CU : +return MSG_OP_CHAIN_EXEC_BUFFER_CF ; +case ERT_START_NPU : +return MSG_OP_CHAIN_EXEC_DPU ; +default : +return MSG_OP_MAX_OPCODE ; + } +} + +int aie2_cmdlist_multi_execbuf (struct amdxdna_hwctx *hwctx, +struct amdxdna_sched_job *job, +int (*notify_cb)(void *,const u32 *,size_t )) +{ +struct amdxdna_gem_obj *cmdbuf_abo =aie2_cmdlist_get_cmd_buf (job); +struct mailbox_channel *chann = hwctx->priv->mbox_chann; +struct amdxdna_client *client = hwctx->client; +struct amdxdna_gem_obj *cmd_abo = job->cmd_bo; +struct amdxdna_cmd_chain *payload; +struct xdna_mailbox_msg msg; +struct cmd_chain_req req; +u32 payload_len; +u32 offset =0 ; +u32 size; +int ret; +u32 op; +u32 i; + + op =amdxdna_cmd_get_op (cmd_abo); + payload =amdxdna_cmd_get_payload (cmd_abo, &payload_len); +if (op !=ERT_CMD_CHAIN || !payload || + payload_len <struct_size (payload, data, payload->command_count)) +return -EINVAL ; + +for (i =0 ; i < payload->command_count; i++) { +u32 boh = (u32)(payload->data[i]); +struct amdxdna_gem_obj *abo; + + abo =amdxdna_gem_get_obj (client, boh,AMDXDNA_BO_CMD ); +if (!abo) { +XDNA_ERR (client->xdna,"Failed to find cmd BO %d" , boh); +return -ENOENT ; + } + +/* All sub-cmd should have same op, use the first one. */ +if (i ==0 ) + op =amdxdna_cmd_get_op (abo); + + ret =aie2_cmdlist_fill_one_slot (op, cmdbuf_abo, offset, abo, &size); +amdxdna_gem_put_obj (abo); +if (ret) +return -EINVAL ; + + offset += size; + } + +/* The offset is the accumulated total size of the cmd buffer */ +aie2_cmdlist_prepare_request (&req, cmdbuf_abo, offset, payload->command_count); + + msg.opcode =aie2_cmd_op_to_msg_op (op); +if (msg.opcode ==MSG_OP_MAX_OPCODE ) +return -EOPNOTSUPP ; + msg.handle = job; + msg.notify_cb = notify_cb; + msg.send_data = (u8 *)&req; + msg.send_size =sizeof (req); + ret =xdna_mailbox_send_msg (chann, &msg,TX_TIMEOUT ); +if (ret) { +XDNA_ERR (hwctx->client->xdna,"Send message failed" ); +return ret; + } + +return 0 ; +} + +int aie2_cmdlist_single_execbuf (struct amdxdna_hwctx *hwctx, +struct amdxdna_sched_job *job, +int (*notify_cb)(void *,const u32 *,size_t )) +{ +struct amdxdna_gem_obj *cmdbuf_abo =aie2_cmdlist_get_cmd_buf (job); +struct mailbox_channel *chann = hwctx->priv->mbox_chann; +struct amdxdna_gem_obj *cmd_abo = job->cmd_bo; +struct xdna_mailbox_msg msg; +struct cmd_chain_req req; +u32 size; +int ret; +u32 op; + + op =amdxdna_cmd_get_op (cmd_abo); + ret =aie2_cmdlist_fill_one_slot (op, cmdbuf_abo,0 , cmd_abo, &size); +if (ret) +return ret; + +aie2_cmdlist_prepare_request (&req, cmdbuf_abo, size,1 ); + + msg.opcode =aie2_cmd_op_to_msg_op (op); +if (msg.opcode ==MSG_OP_MAX_OPCODE ) +return -EOPNOTSUPP ; + msg.handle = job; + msg.notify_cb = notify_cb; + msg.send_data = (u8 *)&req; + msg.send_size =sizeof (req); + ret =xdna_mailbox_send_msg (chann, &msg,TX_TIMEOUT ); +if (ret) { +XDNA_ERR (hwctx->client->xdna,"Send message failed" ); +return ret; + } + +return 0 ; +} + +int aie2_sync_bo (struct amdxdna_hwctx *hwctx,struct amdxdna_sched_job *job, +int (*notify_cb)(void *,const u32 *,size_t )) +{ +struct mailbox_channel *chann = hwctx->priv->mbox_chann; +struct amdxdna_gem_obj *abo =to_xdna_obj (job->bos[0 ]); +struct amdxdna_dev *xdna = hwctx->client->xdna; +struct xdna_mailbox_msg msg; +struct sync_bo_req req; +int ret =0 ; + + req.src_addr =0 ; + req.dst_addr = abo->mem.dev_addr - hwctx->client->dev_heap->mem.dev_addr; + req.size = abo->mem.size; + +/* Device to Host */ + req.type =FIELD_PREP (AIE2_MSG_SYNC_BO_SRC_TYPE ,SYNC_BO_DEV_MEM ) | +FIELD_PREP (AIE2_MSG_SYNC_BO_DST_TYPE ,SYNC_BO_HOST_MEM ); + +XDNA_DBG (xdna,"sync %d bytes src(0x%llx) to dst(0x%llx) completed" , + req.size, req.src_addr, req.dst_addr); + + msg.handle = job; + msg.notify_cb = notify_cb; + msg.send_data = (u8 *)&req; + msg.send_size =sizeof (req); + msg.opcode =MSG_OP_SYNC_BO ; + + ret =xdna_mailbox_send_msg (chann, &msg,TX_TIMEOUT ); +if (ret) { +XDNA_ERR (xdna,"Send message failed" ); +return ret; + } + +return 0 ; +}drivers/accel/amdxdna/aie2_pci.c +5
-0@@ -5,8 +5,10 @@#include <drm/amdxdna_accel.h> #include <drm/drm_device.h> +#include <drm/drm_gem_shmem_helper.h> #include <drm/drm_managed.h> #include <drm/drm_print.h> +#include <drm/gpu_scheduler.h> #include <linux/errno.h> #include <linux/firmware.h> #include <linux/iommu.h> @@ -17,6 +19,7 @@#include "aie2_pci.h" #include "aie2_solver.h" #include "amdxdna_ctx.h" +#include "amdxdna_gem.h" #include "amdxdna_mailbox.h" #include "amdxdna_pci_drv.h" @@ -495,4 +498,6 @@ const struct amdxdna_dev_ops aie2_ops = { .hwctx_init = aie2_hwctx_init, .hwctx_fini = aie2_hwctx_fini, .hwctx_config = aie2_hwctx_config, + .cmd_submit = aie2_cmd_submit, + .hmm_invalidate = aie2_hmm_invalidate, };drivers/accel/amdxdna/aie2_pci.h +40
-0@@ -6,6 +6,8 @@#ifndef _AIE2_PCI_H_#define _AIE2_PCI_H_ +#include <linux/semaphore.h> +#include "amdxdna_mailbox.h" #define AIE2_INTERVAL 20000/* us */ @@ -76,8 +78,10 @@ enum psp_reg_idx {PSP_MAX_REGS /* Keep this at the end */ }; +struct amdxdna_client ;struct amdxdna_fw_ver ;struct amdxdna_hwctx ; +struct amdxdna_sched_job ;struct psp_config {const void *fw_buf;@@ -118,9 +122,31 @@ struct rt_config {u32 value; }; +/* +* Define the maximum number of pending commands in a hardware context. +* Must be power of 2! +*/ +#define HWCTX_MAX_CMDS 4 +#define get_job_idx (seq) ((seq) & (HWCTX_MAX_CMDS - 1))struct amdxdna_hwctx_priv {struct amdxdna_gem_obj *heap;void *mbox_chann; + +struct drm_gpu_scheduler sched; +struct drm_sched_entity entity; + +struct mutex io_lock;/* protect seq and cmd order */ +struct wait_queue_head job_free_wq; +u32 num_pending; +u64 seq; +struct semaphore job_sem; +bool job_done; + +/* Completed job counter */ +u64 completed; + +struct amdxdna_gem_obj *cmd_buf[HWCTX_MAX_CMDS ]; +struct drm_syncobj *syncobj; };struct amdxdna_dev_hdl {@@ -199,10 +225,24 @@ int aie2_create_context(struct amdxdna_dev_hdl *ndev, struct amdxdna_hwctx *hwctint aie2_destroy_context (struct amdxdna_dev_hdl *ndev,struct amdxdna_hwctx *hwctx);int aie2_map_host_buf (struct amdxdna_dev_hdl *ndev,u32 context_id,u64 addr,u64 size);int aie2_config_cu (struct amdxdna_hwctx *hwctx); +int aie2_execbuf (struct amdxdna_hwctx *hwctx,struct amdxdna_sched_job *job, +int (*notify_cb)(void *,const u32 *,size_t )); +int aie2_cmdlist_single_execbuf (struct amdxdna_hwctx *hwctx, +struct amdxdna_sched_job *job, +int (*notify_cb)(void *,const u32 *,size_t )); +int aie2_cmdlist_multi_execbuf (struct amdxdna_hwctx *hwctx, +struct amdxdna_sched_job *job, +int (*notify_cb)(void *,const u32 *,size_t )); +int aie2_sync_bo (struct amdxdna_hwctx *hwctx,struct amdxdna_sched_job *job, +int (*notify_cb)(void *,const u32 *,size_t ));/* aie2_hwctx.c */ int aie2_hwctx_init (struct amdxdna_hwctx *hwctx);void aie2_hwctx_fini (struct amdxdna_hwctx *hwctx);int aie2_hwctx_config (struct amdxdna_hwctx *hwctx,u32 type,u64 value,void *buf,u32 size); +int aie2_cmd_submit (struct amdxdna_hwctx *hwctx,struct amdxdna_sched_job *job,u64 *seq); +void aie2_hmm_invalidate (struct amdxdna_gem_obj *abo,unsigned long cur_seq); +void aie2_stop_ctx_by_col_map (struct amdxdna_client *client,u32 col_map); +void aie2_restart_ctx (struct amdxdna_client *client);#endif /* _AIE2_PCI_H_ */ drivers/accel/amdxdna/aie2_psp.c +2
-0@@ -4,8 +4,10 @@*/ #include <drm/drm_device.h> +#include <drm/drm_gem_shmem_helper.h> #include <drm/drm_managed.h> #include <drm/drm_print.h> +#include <drm/gpu_scheduler.h> #include <linux/bitfield.h> #include <linux/iopoll.h> drivers/accel/amdxdna/aie2_smu.c +2
-0@@ -4,7 +4,9 @@*/ #include <drm/drm_device.h> +#include <drm/drm_gem_shmem_helper.h> #include <drm/drm_print.h> +#include <drm/gpu_scheduler.h> #include <linux/iopoll.h> #include "aie2_pci.h" drivers/accel/amdxdna/amdxdna_ctx.c +314
-6@@ -7,17 +7,65 @@#include <drm/drm_device.h> #include <drm/drm_drv.h> #include <drm/drm_file.h> +#include <drm/drm_gem.h> +#include <drm/drm_gem_shmem_helper.h> #include <drm/drm_print.h> +#include <drm/gpu_scheduler.h> +#include <trace/events/amdxdna.h> #include "amdxdna_ctx.h" +#include "amdxdna_gem.h" #include "amdxdna_pci_drv.h" #define MAX_HWCTX_ID 255 +#define MAX_ARG_COUNT 4095 -static void amdxdna_hwctx_destroy (struct amdxdna_hwctx *hwctx) +struct amdxdna_fence { +struct dma_fence base; +spinlock_t lock;/* for base */ +struct amdxdna_hwctx *hwctx; +}; + +static const char *amdxdna_fence_get_driver_name (struct dma_fence *fence) +{ +return KBUILD_MODNAME ; +} + +static const char *amdxdna_fence_get_timeline_name (struct dma_fence *fence) +{ +struct amdxdna_fence *xdna_fence; + + xdna_fence = container_of(fence, struct amdxdna_fence, base); + +return xdna_fence->hwctx->name; +} + +static const struct dma_fence_ops fence_ops = { + .get_driver_name = amdxdna_fence_get_driver_name, + .get_timeline_name = amdxdna_fence_get_timeline_name, +}; + +static struct dma_fence *amdxdna_fence_create (struct amdxdna_hwctx *hwctx) +{ +struct amdxdna_fence *fence; + + fence =kzalloc (sizeof (*fence),GFP_KERNEL ); +if (!fence) +return NULL ; + + fence->hwctx = hwctx; +spin_lock_init (&fence->lock); +dma_fence_init (&fence->base, &fence_ops, &fence->lock, hwctx->id,0 ); +return &fence->base; +} + +static void amdxdna_hwctx_destroy_rcu (struct amdxdna_hwctx *hwctx, +struct srcu_struct *ss) {struct amdxdna_dev *xdna = hwctx->client->xdna; +synchronize_srcu (ss); +/* At this point, user is not able to submit new commands */ mutex_lock (&xdna->dev_lock); xdna->dev_info->ops->hwctx_fini (hwctx);@@ -27,6 +75,46 @@ static void amdxdna_hwctx_destroy(struct amdxdna_hwctx *hwctx)kfree (hwctx); } +void *amdxdna_cmd_get_payload (struct amdxdna_gem_obj *abo,u32 *size) +{ +struct amdxdna_cmd *cmd = abo->mem.kva; +u32 num_masks, count; + +if (amdxdna_cmd_get_op (abo) ==ERT_CMD_CHAIN ) + num_masks =0 ; +else + num_masks =1 +FIELD_GET (AMDXDNA_CMD_EXTRA_CU_MASK , cmd->header); + +if (size) { + count =FIELD_GET (AMDXDNA_CMD_COUNT , cmd->header); +if (unlikely (count <= num_masks)) { + *size =0 ; +return NULL ; + } + *size = (count - num_masks) *sizeof (u32); + } +return &cmd->data[num_masks]; +} + +int amdxdna_cmd_get_cu_idx (struct amdxdna_gem_obj *abo) +{ +struct amdxdna_cmd *cmd = abo->mem.kva; +u32 num_masks, i; +u32 *cu_mask; + +if (amdxdna_cmd_get_op (abo) ==ERT_CMD_CHAIN ) +return -1 ; + + num_masks =1 +FIELD_GET (AMDXDNA_CMD_EXTRA_CU_MASK , cmd->header); + cu_mask = cmd->data; +for (i =0 ; i < num_masks; i++) { +if (cu_mask[i]) +return ffs (cu_mask[i]) -1 ; + } + +return -1 ; +} +/* * This should be called in close() and remove(). DO NOT call in other syscalls. * This guarantee that when hwctx and resources will be released, if user @@ -43,7 +131,7 @@ void amdxdna_hwctx_remove_all(struct amdxdna_client *client) client->pid, hwctx->id);idr_remove (&client->hwctx_idr, hwctx->id);mutex_unlock (&client->hwctx_lock); -amdxdna_hwctx_destroy (hwctx); +amdxdna_hwctx_destroy_rcu (hwctx, &client->hwctx_srcu);mutex_lock (&client->hwctx_lock); }mutex_unlock (&client->hwctx_lock);@@ -135,6 +223,12 @@ int amdxdna_drm_destroy_hwctx_ioctl(struct drm_device *dev, void *data, struct dif (!drm_dev_enter (dev, &idx))return -ENODEV ; +/* +* Use hwctx_lock to achieve exclusion with other hwctx writers, +* SRCU to synchronize with exec/wait command ioctls. +* +* The pushed jobs are handled by DRM scheduler during destroy. +*/ mutex_lock (&client->hwctx_lock); hwctx =idr_find (&client->hwctx_idr, args->handle);if (!hwctx) {@@ -147,7 +241,7 @@ int amdxdna_drm_destroy_hwctx_ioctl(struct drm_device *dev, void *data, struct didr_remove (&client->hwctx_idr, hwctx->id);mutex_unlock (&client->hwctx_lock); -amdxdna_hwctx_destroy (hwctx); +amdxdna_hwctx_destroy_rcu (hwctx, &client->hwctx_srcu);XDNA_DBG (xdna,"PID %d destroyed HW context %d" , client->pid, args->handle); out:@@ -161,10 +255,10 @@ int amdxdna_drm_config_hwctx_ioctl(struct drm_device *dev, void *data, struct drstruct amdxdna_drm_config_hwctx *args = data;struct amdxdna_dev *xdna =to_xdna_dev (dev);struct amdxdna_hwctx *hwctx; +int ret, idx;u32 buf_size;void *buf;u64 val; -int ret;if (!xdna->dev_info->ops->hwctx_config)return -EOPNOTSUPP ;@@ -203,17 +297,231 @@ int amdxdna_drm_config_hwctx_ioctl(struct drm_device *dev, void *data, struct dr }mutex_lock (&xdna->dev_lock); + idx =srcu_read_lock (&client->hwctx_srcu); hwctx =idr_find (&client->hwctx_idr, args->handle);if (!hwctx) {XDNA_DBG (xdna,"PID %d failed to get hwctx %d" , client->pid, args->handle); ret = -EINVAL ; - goto unlock; + goto unlock_srcu; } ret = xdna->dev_info->ops->hwctx_config (hwctx, args->param_type, val, buf, buf_size); -unlock: +unlock_srcu: +srcu_read_unlock (&client->hwctx_srcu, idx);mutex_unlock (&xdna->dev_lock);kfree (buf);return ret; } + +static void +amdxdna_arg_bos_put (struct amdxdna_sched_job *job) +{ +int i; + +for (i =0 ; i < job->bo_cnt; i++) { +if (!job->bos[i]) +break ; +drm_gem_object_put (job->bos[i]); + } +} + +static int +amdxdna_arg_bos_lookup (struct amdxdna_client *client, +struct amdxdna_sched_job *job, +u32 *bo_hdls,u32 bo_cnt) +{ +struct drm_gem_object *gobj; +int i, ret; + + job->bo_cnt = bo_cnt; +for (i =0 ; i < job->bo_cnt; i++) { +struct amdxdna_gem_obj *abo; + + gobj =drm_gem_object_lookup (client->filp, bo_hdls[i]); +if (!gobj) { + ret = -ENOENT ; + goto put_shmem_bo; + } + abo =to_xdna_obj (gobj); + +mutex_lock (&abo->lock); +if (abo->pinned) { +mutex_unlock (&abo->lock); + job->bos[i] = gobj; +continue ; + } + + ret =amdxdna_gem_pin_nolock (abo); +if (ret) { +mutex_unlock (&abo->lock); +drm_gem_object_put (gobj); + goto put_shmem_bo; + } + abo->pinned = true; +mutex_unlock (&abo->lock); + + job->bos[i] = gobj; + } + +return 0 ; + +put_shmem_bo: +amdxdna_arg_bos_put (job); +return ret; +} + +void amdxdna_sched_job_cleanup (struct amdxdna_sched_job *job) +{ +trace_amdxdna_debug_point (job->hwctx->name, job->seq,"job release" ); +amdxdna_arg_bos_put (job); +amdxdna_gem_put_obj (job->cmd_bo); +} + +int amdxdna_cmd_submit (struct amdxdna_client *client, +u32 cmd_bo_hdl,u32 *arg_bo_hdls,u32 arg_bo_cnt, +u32 hwctx_hdl,u64 *seq) +{ +struct amdxdna_dev *xdna = client->xdna; +struct amdxdna_sched_job *job; +struct amdxdna_hwctx *hwctx; +int ret, idx; + +XDNA_DBG (xdna,"Command BO hdl %d, Arg BO count %d" , cmd_bo_hdl, arg_bo_cnt); + job =kzalloc (struct_size (job, bos, arg_bo_cnt),GFP_KERNEL ); +if (!job) +return -ENOMEM ; + +if (cmd_bo_hdl !=AMDXDNA_INVALID_BO_HANDLE ) { + job->cmd_bo =amdxdna_gem_get_obj (client, cmd_bo_hdl,AMDXDNA_BO_CMD ); +if (!job->cmd_bo) { +XDNA_ERR (xdna,"Failed to get cmd bo from %d" , cmd_bo_hdl); + ret = -EINVAL ; + goto free_job; + } + }else { + job->cmd_bo =NULL ; + } + + ret =amdxdna_arg_bos_lookup (client, job, arg_bo_hdls, arg_bo_cnt); +if (ret) { +XDNA_ERR (xdna,"Argument BOs lookup failed, ret %d" , ret); + goto cmd_put; + } + + idx =srcu_read_lock (&client->hwctx_srcu); + hwctx =idr_find (&client->hwctx_idr, hwctx_hdl); +if (!hwctx) { +XDNA_DBG (xdna,"PID %d failed to get hwctx %d" , + client->pid, hwctx_hdl); + ret = -EINVAL ; + goto unlock_srcu; + } + +if (hwctx->status !=HWCTX_STAT_READY ) { +XDNA_ERR (xdna,"HW Context is not ready" ); + ret = -EINVAL ; + goto unlock_srcu; + } + + job->hwctx = hwctx; + job->mm = current->mm; + + job->fence =amdxdna_fence_create (hwctx); +if (!job->fence) { +XDNA_ERR (xdna,"Failed to create fence" ); + ret = -ENOMEM ; + goto unlock_srcu; + } +kref_init (&job->refcnt); + + ret = xdna->dev_info->ops->cmd_submit (hwctx, job, seq); +if (ret) + goto put_fence; + +/* +* The amdxdna_hwctx_destroy_rcu() will release hwctx and associated +* resource after synchronize_srcu(). The submitted jobs should be +* handled by the queue, for example DRM scheduler, in device layer. +* For here we can unlock SRCU. +*/ +srcu_read_unlock (&client->hwctx_srcu, idx); +trace_amdxdna_debug_point (hwctx->name, *seq,"job pushed" ); + +return 0 ; + +put_fence: +dma_fence_put (job->fence); +unlock_srcu: +srcu_read_unlock (&client->hwctx_srcu, idx); +amdxdna_arg_bos_put (job); +cmd_put: +amdxdna_gem_put_obj (job->cmd_bo); +free_job: +kfree (job); +return ret; +} + +/* +* The submit command ioctl submits a command to firmware. One firmware command +* may contain multiple command BOs for processing as a whole. +* The command sequence number is returned which can be used for wait command ioctl. +*/ +static int amdxdna_drm_submit_execbuf (struct amdxdna_client *client, +struct amdxdna_drm_exec_cmd *args) +{ +struct amdxdna_dev *xdna = client->xdna; +u32 *arg_bo_hdls; +u32 cmd_bo_hdl; +int ret; + +if (!args->arg_count || args->arg_count >MAX_ARG_COUNT ) { +XDNA_ERR (xdna,"Invalid arg bo count %d" , args->arg_count); +return -EINVAL ; + } + +/* Only support single command for now. */ +if (args->cmd_count !=1 ) { +XDNA_ERR (xdna,"Invalid cmd bo count %d" , args->cmd_count); +return -EINVAL ; + } + + cmd_bo_hdl = (u32 )args->cmd_handles; + arg_bo_hdls =kcalloc (args->arg_count,sizeof (u32),GFP_KERNEL ); +if (!arg_bo_hdls) +return -ENOMEM ; + ret =copy_from_user (arg_bo_hdls,u64_to_user_ptr (args->args), + args->arg_count *sizeof (u32)); +if (ret) { + ret = -EFAULT ; + goto free_cmd_bo_hdls; + } + + ret =amdxdna_cmd_submit (client, cmd_bo_hdl, arg_bo_hdls, + args->arg_count, args->hwctx, &args->seq); +if (ret) +XDNA_DBG (xdna,"Submit cmds failed, ret %d" , ret); + +free_cmd_bo_hdls: +kfree (arg_bo_hdls); +if (!ret) +XDNA_DBG (xdna,"Pushed cmd %lld to scheduler" , args->seq); +return ret; +} + +int amdxdna_drm_submit_cmd_ioctl (struct drm_device *dev,void *data,struct drm_file *filp) +{ +struct amdxdna_client *client = filp->driver_priv; +struct amdxdna_drm_exec_cmd *args = data; + +if (args->ext || args->ext_flags) +return -EINVAL ; + +switch (args->type) { +case AMDXDNA_CMD_SUBMIT_EXEC_BUF : +return amdxdna_drm_submit_execbuf (client, args); + } + +XDNA_ERR (client->xdna,"Invalid command type %d" , args->type); +return -EINVAL ; +}drivers/accel/amdxdna/amdxdna_ctx.h +111
-0@@ -6,6 +6,54 @@#ifndef _AMDXDNA_CTX_H_#define _AMDXDNA_CTX_H_ +#include <linux/bitfield.h> + +#include "amdxdna_gem.h" + +struct amdxdna_hwctx_priv ; + +enum ert_cmd_opcode { +ERT_START_CU =0 , +ERT_CMD_CHAIN =19 , +ERT_START_NPU =20 , +}; + +enum ert_cmd_state { +ERT_CMD_STATE_INVALID , +ERT_CMD_STATE_NEW , +ERT_CMD_STATE_QUEUED , +ERT_CMD_STATE_RUNNING , +ERT_CMD_STATE_COMPLETED , +ERT_CMD_STATE_ERROR , +ERT_CMD_STATE_ABORT , +ERT_CMD_STATE_SUBMITTED , +ERT_CMD_STATE_TIMEOUT , +ERT_CMD_STATE_NORESPONSE , +}; + +/* +* Interpretation of the beginning of data payload for ERT_START_NPU in +* amdxdna_cmd. The rest of the payload in amdxdna_cmd is regular kernel args. +*/ +struct amdxdna_cmd_start_npu { +u64 buffer;/* instruction buffer address */ +u32 buffer_size;/* size of buffer in bytes */ +u32 prop_count;/* properties count */ +u32 prop_args[];/* properties and regular kernel arguments */ +}; + +/* +* Interpretation of the beginning of data payload for ERT_CMD_CHAIN in +* amdxdna_cmd. The rest of the payload in amdxdna_cmd is cmd BO handles. +*/ +struct amdxdna_cmd_chain { +u32 command_count; +u32 submit_index; +u32 error_index; +u32 reserved[3 ]; + u64 data[] __counted_by(command_count); +}; +/* Exec buffer command header format */ #define AMDXDNA_CMD_STATE GENMASK(3, 0)#define AMDXDNA_CMD_EXTRA_CU_MASK GENMASK(11, 10)@@ -41,9 +89,72 @@ struct amdxdna_hwctx {u32 syncobj_hdl; }; +#define drm_job_to_xdna_job (j) \ + container_of(j, struct amdxdna_sched_job, base) + +struct amdxdna_sched_job { +struct drm_sched_job base; +struct kref refcnt; +struct amdxdna_hwctx *hwctx; +struct mm_struct *mm; +/* The fence to notice DRM scheduler that job is done by hardware */ +struct dma_fence *fence; +/* user can wait on this fence */ +struct dma_fence *out_fence; +bool job_done; +u64 seq; +struct amdxdna_gem_obj *cmd_bo; +size_t bo_cnt; + struct drm_gem_object *bos[] __counted_by(bo_cnt); +}; + +static inline u32 +amdxdna_cmd_get_op (struct amdxdna_gem_obj *abo) +{ +struct amdxdna_cmd *cmd = abo->mem.kva; + +return FIELD_GET (AMDXDNA_CMD_OPCODE , cmd->header); +} + +static inline void +amdxdna_cmd_set_state (struct amdxdna_gem_obj *abo,enum ert_cmd_state s) +{ +struct amdxdna_cmd *cmd = abo->mem.kva; + + cmd->header &= ~AMDXDNA_CMD_STATE ; + cmd->header |=FIELD_PREP (AMDXDNA_CMD_STATE , s); +} + +static inline enum ert_cmd_state +amdxdna_cmd_get_state (struct amdxdna_gem_obj *abo) +{ +struct amdxdna_cmd *cmd = abo->mem.kva; + +return FIELD_GET (AMDXDNA_CMD_STATE , cmd->header); +} + +void *amdxdna_cmd_get_payload (struct amdxdna_gem_obj *abo,u32 *size); +int amdxdna_cmd_get_cu_idx (struct amdxdna_gem_obj *abo); + +static inline u32 amdxdna_hwctx_col_map (struct amdxdna_hwctx *hwctx) +{ +return GENMASK (hwctx->start_col + hwctx->num_col -1 , + hwctx->start_col); +} + +void amdxdna_sched_job_cleanup (struct amdxdna_sched_job *job);void amdxdna_hwctx_remove_all (struct amdxdna_client *client); + +int amdxdna_cmd_submit (struct amdxdna_client *client, +u32 cmd_bo_hdls,u32 *arg_bo_hdls,u32 arg_bo_cnt, +u32 hwctx_hdl,u64 *seq); + +int amdxdna_cmd_wait (struct amdxdna_client *client,u32 hwctx_hdl, +u64 seq,u32 timeout); +int amdxdna_drm_create_hwctx_ioctl (struct drm_device *dev,void *data,struct drm_file *filp);int amdxdna_drm_config_hwctx_ioctl (struct drm_device *dev,void *data,struct drm_file *filp);int amdxdna_drm_destroy_hwctx_ioctl (struct drm_device *dev,void *data,struct drm_file *filp); +int amdxdna_drm_submit_cmd_ioctl (struct drm_device *dev,void *data,struct drm_file *filp);#endif /* _AMDXDNA_CTX_H_ */ drivers/accel/amdxdna/amdxdna_gem.c +1
-0@@ -8,6 +8,7 @@#include <drm/drm_device.h> #include <drm/drm_gem.h> #include <drm/drm_gem_shmem_helper.h> +#include <drm/gpu_scheduler.h> #include <linux/iosys-map.h> #include <linux/vmalloc.h> drivers/accel/amdxdna/amdxdna_mailbox_helper.c +5
-0@@ -3,10 +3,15 @@* Copyright (C) 2024, Advanced Micro Devices, Inc. */ +#include <drm/amdxdna_accel.h> #include <drm/drm_device.h> #include <drm/drm_print.h> +#include <drm/drm_gem.h> +#include <drm/drm_gem_shmem_helper.h> +#include <drm/gpu_scheduler.h> #include <linux/completion.h> +#include "amdxdna_gem.h" #include "amdxdna_mailbox.h" #include "amdxdna_mailbox_helper.h" #include "amdxdna_pci_drv.h" drivers/accel/amdxdna/amdxdna_pci_drv.c +12
-0@@ -10,6 +10,7 @@#include <drm/drm_gem_shmem_helper.h> #include <drm/drm_ioctl.h> #include <drm/drm_managed.h> +#include <drm/gpu_scheduler.h> #include <linux/iommu.h> #include <linux/pci.h> @@ -64,6 +65,7 @@ static int amdxdna_drm_open(struct drm_device *ddev, struct drm_file *filp) goto unbind_sva; }mutex_init (&client->hwctx_lock); +init_srcu_struct (&client->hwctx_srcu);idr_init_base (&client->hwctx_idr,AMDXDNA_INVALID_CTX_HANDLE +1 );mutex_init (&client->mm_lock);@@ -93,6 +95,7 @@ static void amdxdna_drm_close(struct drm_device *ddev, struct drm_file *filp)XDNA_DBG (xdna,"closing pid %d" , client->pid);idr_destroy (&client->hwctx_idr); +cleanup_srcu_struct (&client->hwctx_srcu);mutex_destroy (&client->hwctx_lock);mutex_destroy (&client->mm_lock);if (client->dev_heap)@@ -133,6 +136,8 @@ static const struct drm_ioctl_desc amdxdna_drm_ioctls[] = {DRM_IOCTL_DEF_DRV (AMDXDNA_CREATE_BO , amdxdna_drm_create_bo_ioctl,0 ),DRM_IOCTL_DEF_DRV (AMDXDNA_GET_BO_INFO , amdxdna_drm_get_bo_info_ioctl,0 ),DRM_IOCTL_DEF_DRV (AMDXDNA_SYNC_BO , amdxdna_drm_sync_bo_ioctl,0 ), +/* Execution */ + DRM_IOCTL_DEF_DRV(AMDXDNA_EXEC_CMD, amdxdna_drm_submit_cmd_ioctl, 0), }; static const struct file_operations amdxdna_fops = {@@ -190,9 +195,16 @@ static int amdxdna_probe(struct pci_dev *pdev, const struct pci_device_id *id)return -ENODEV ;drmm_mutex_init (&xdna->ddev, &xdna->dev_lock); +rwlock_init (&xdna->notifier_lock);INIT_LIST_HEAD (&xdna->client_list);pci_set_drvdata (pdev, xdna); +if (IS_ENABLED (CONFIG_LOCKDEP )) { +fs_reclaim_acquire (GFP_KERNEL ); +might_lock (&xdna->notifier_lock); +fs_reclaim_release (GFP_KERNEL ); + } +mutex_lock (&xdna->dev_lock); ret = xdna->dev_info->ops->init (xdna);mutex_unlock (&xdna->dev_lock);drivers/accel/amdxdna/amdxdna_pci_drv.h +5
-0@@ -20,6 +20,7 @@ extern const struct drm_driver amdxdna_drm_drv;struct amdxdna_dev ;struct amdxdna_gem_obj ;struct amdxdna_hwctx ; +struct amdxdna_sched_job ;/* * struct amdxdna_dev_ops - Device hardware operation callbacks @@ -31,6 +32,7 @@ struct amdxdna_dev_ops {void (*hwctx_fini)(struct amdxdna_hwctx *hwctx);int (*hwctx_config)(struct amdxdna_hwctx *hwctx,u32 type,u64 value,void *buf,u32 size);void (*hmm_invalidate)(struct amdxdna_gem_obj *abo,unsigned long cur_seq); +int (*cmd_submit)(struct amdxdna_hwctx *hwctx,struct amdxdna_sched_job *job,u64 *seq); };/* @@ -69,6 +71,7 @@ struct amdxdna_dev {struct mutex dev_lock;/* per device lock */ struct list_head client_list;struct amdxdna_fw_ver fw_ver; +rwlock_t notifier_lock;/* for mmu notifier*/ };/* @@ -88,6 +91,8 @@ struct amdxdna_client {struct list_head node;pid_t pid;struct mutex hwctx_lock;/* protect hwctx */ +/* do NOT wait this srcu when hwctx_lock is hold */ +struct srcu_struct hwctx_srcu;struct idr hwctx_idr;struct amdxdna_dev *xdna;struct drm_file *filp;drivers/accel/amdxdna/amdxdna_sysfs.c +5
-0@@ -3,9 +3,14 @@* Copyright (C) 2023-2024, Advanced Micro Devices, Inc. */ +#include <drm/amdxdna_accel.h> #include <drm/drm_device.h> +#include <drm/drm_gem_shmem_helper.h> #include <drm/drm_print.h> +#include <drm/gpu_scheduler.h> +#include <linux/types.h> +#include "amdxdna_gem.h" #include "amdxdna_pci_drv.h" static ssize_t vbnv_show (struct device *dev,struct device_attribute *attr,char *buf)drivers/accel/amdxdna/npu1_regs.c +1
-0@@ -5,6 +5,7 @@#include <drm/amdxdna_accel.h> #include <drm/drm_device.h> +#include <drm/gpu_scheduler.h> #include <linux/sizes.h> #include "aie2_pci.h" drivers/accel/amdxdna/npu2_regs.c +1
-0@@ -5,6 +5,7 @@#include <drm/amdxdna_accel.h> #include <drm/drm_device.h> +#include <drm/gpu_scheduler.h> #include <linux/sizes.h> #include "aie2_pci.h" drivers/accel/amdxdna/npu4_regs.c +1
-0@@ -5,6 +5,7 @@#include <drm/amdxdna_accel.h> #include <drm/drm_device.h> +#include <drm/gpu_scheduler.h> #include <linux/sizes.h> #include "aie2_pci.h" drivers/accel/amdxdna/npu5_regs.c +1
-0@@ -5,6 +5,7 @@#include <drm/amdxdna_accel.h> #include <drm/drm_device.h> +#include <drm/gpu_scheduler.h> #include <linux/sizes.h> #include "aie2_pci.h" include/trace/events/amdxdna.h +41
-0@@ -9,8 +9,49 @@#if !defined(_TRACE_AMDXDNA_H) || defined(TRACE_HEADER_MULTI_READ )#define _TRACE_AMDXDNA_H +#include <drm/gpu_scheduler.h> #include <linux/tracepoint.h> +TRACE_EVENT (amdxdna_debug_point, + TP_PROTO(const char *name, u64 number, const char *str), + +TP_ARGS (name, number, str), + + TP_STRUCT__entry(__string(name, name) + __field(u64, number) +__string (str, str)), + + TP_fast_assign(__assign_str(name); + __entry->number = number; + __assign_str(str);), + +TP_printk ("%s:%llu %s" ,__get_str (name), __entry->number, +__get_str (str)) +); + +TRACE_EVENT (xdna_job, + TP_PROTO(struct drm_sched_job *sched_job, const char *name, const char *str, u64 seq), + +TP_ARGS (sched_job, name, str, seq), + + TP_STRUCT__entry(__string(name, name) + __string(str, str) + __field(u64, fence_context) + __field(u64, fence_seqno) +__field (u64, seq)), + + TP_fast_assign(__assign_str(name); + __assign_str(str); + __entry->fence_context = sched_job->s_fence->finished.context; + __entry->fence_seqno = sched_job->s_fence->finished.seqno; + __entry->seq = seq;), + +TP_printk ("fence=(context:%llu, seqno:%lld), %s seq#:%lld %s" , + __entry->fence_context, __entry->fence_seqno, +__get_str (name), __entry->seq, +__get_str (str)) +); + DECLARE_EVENT_CLASS(xdna_mbox_msg, TP_PROTO(char *name, u8 chann_id, u32 opcode, u32 msg_id),include/uapi/drm/amdxdna_accel.h +38
-0@@ -13,9 +13,11 @@extern "C" {#endif +#define AMDXDNA_INVALID_CMD_HANDLE (~0UL)#define AMDXDNA_INVALID_ADDR (~0UL)#define AMDXDNA_INVALID_CTX_HANDLE 0#define AMDXDNA_INVALID_BO_HANDLE 0 +#define AMDXDNA_INVALID_FENCE_HANDLE 0enum amdxdna_device_type {AMDXDNA_DEV_TYPE_UNKNOWN =-1 ,@@ -29,6 +31,7 @@ enum amdxdna_drm_ioctl_id {DRM_AMDXDNA_CREATE_BO ,DRM_AMDXDNA_GET_BO_INFO ,DRM_AMDXDNA_SYNC_BO , + DRM_AMDXDNA_EXEC_CMD, };/** @@ -201,6 +204,37 @@ struct amdxdna_drm_sync_bo {__u64 size; }; +enum amdxdna_cmd_type { +AMDXDNA_CMD_SUBMIT_EXEC_BUF =0 , +AMDXDNA_CMD_SUBMIT_DEPENDENCY , +AMDXDNA_CMD_SUBMIT_SIGNAL , +}; + +/** +* struct amdxdna_drm_exec_cmd - Execute command. +* @ext: MBZ. +* @ext_flags: MBZ. +* @hwctx: Hardware context handle. +* @type: One of command type in enum amdxdna_cmd_type. +* @cmd_handles: Array of command handles or the command handle itself +* in case of just one. +* @args: Array of arguments for all command handles. +* @cmd_count: Number of command handles in the cmd_handles array. +* @arg_count: Number of arguments in the args array. +* @seq: Returned sequence number for this command. +*/ +struct amdxdna_drm_exec_cmd { +__u64 ext; +__u64 ext_flags; +__u32 hwctx; +__u32 type; +__u64 cmd_handles; +__u64 args; +__u32 cmd_count; +__u32 arg_count; +__u64 seq; +}; +#define DRM_IOCTL_AMDXDNA_CREATE_HWCTX \ DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDXDNA_CREATE_HWCTX, \ struct amdxdna_drm_create_hwctx)@@ -225,6 +259,10 @@ struct amdxdna_drm_sync_bo {DRM_IOWR (DRM_COMMAND_BASE +DRM_AMDXDNA_SYNC_BO , \ struct amdxdna_drm_sync_bo) +#define DRM_IOCTL_AMDXDNA_EXEC_CMD \ + DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDXDNA_EXEC_CMD, \ + struct amdxdna_drm_exec_cmd) +#if defined(__cplusplus) } /* extern c end */#endif Signature · 1 line
2.34.1
Lizhi Hou
maintainerRe: [PATCH V7 07/10] accel/amdxdna: Add command execution
Quoted 452 lines · Matthew Brost
Quoted 430 lines · Lizhi Hou
Add interfaces for user application to submit command and wait for its completion.
Co-developed-by Min Ma <[email protected]>Signed-off-by Min Ma <[email protected]>Signed-off-by Lizhi Hou <[email protected]>--- drivers/accel/amdxdna/aie2_ctx.c | 634 +++++++++++++++++- drivers/accel/amdxdna/aie2_message.c | 343 ++++++++++ drivers/accel/amdxdna/aie2_pci.c | 5 + drivers/accel/amdxdna/aie2_pci.h | 40 ++ drivers/accel/amdxdna/aie2_psp.c | 2 + drivers/accel/amdxdna/aie2_smu.c | 2 + drivers/accel/amdxdna/amdxdna_ctx.c | 320 ++++++++- drivers/accel/amdxdna/amdxdna_ctx.h | 111 +++ drivers/accel/amdxdna/amdxdna_gem.c | 1 + .../accel/amdxdna/amdxdna_mailbox_helper.c | 5 + drivers/accel/amdxdna/amdxdna_pci_drv.c | 12 + drivers/accel/amdxdna/amdxdna_pci_drv.h | 5 + drivers/accel/amdxdna/amdxdna_sysfs.c | 5 + drivers/accel/amdxdna/npu1_regs.c | 1 + drivers/accel/amdxdna/npu2_regs.c | 1 + drivers/accel/amdxdna/npu4_regs.c | 1 + drivers/accel/amdxdna/npu5_regs.c | 1 + include/trace/events/amdxdna.h | 41 ++ include/uapi/drm/amdxdna_accel.h | 38 ++ 19 files changed, 1559 insertions(+), 9 deletions(-)drivers/accel/amdxdna/aie2_ctx.c +378
-0@@ -8,8 +8,12 @@#include <drm/drm_gem.h> #include <drm/drm_gem_shmem_helper.h> #include <drm/drm_print.h> +#include <drm/drm_syncobj.h> +#include <linux/hmm.h> #include <linux/types.h> +#include <trace/events/amdxdna.h> +#include "aie2_msg_priv.h" #include "aie2_pci.h" #include "aie2_solver.h" #include "amdxdna_ctx.h" @@ -17,6 +21,342 @@#include "amdxdna_mailbox.h" #include "amdxdna_pci_drv.h" +bool force_cmdlist; +module_param (force_cmdlist, bool,0600 ); +MODULE_PARM_DESC (force_cmdlist,"Force use command list (Default false)" ); + +#define HWCTX_MAX_TIMEOUT 60000/* milliseconds */ + +static void aie2_job_release (struct kref *ref) +{ +struct amdxdna_sched_job *job; + + job = container_of(ref, struct amdxdna_sched_job, refcnt); +amdxdna_sched_job_cleanup (job); +kfree (job); +} + +static void aie2_job_put (struct amdxdna_sched_job *job) +{ +kref_put (&job->refcnt, aie2_job_release); +} + +/* The bad_job is used in aie2_sched_job_timedout, otherwise, set it to NULL */ +static void aie2_hwctx_stop (struct amdxdna_dev *xdna,struct amdxdna_hwctx *hwctx, +struct drm_sched_job *bad_job) +{ +drm_sched_stop (&hwctx->priv->sched, bad_job); +aie2_destroy_context (xdna->dev_handle, hwctx); +} + +static int aie2_hwctx_restart (struct amdxdna_dev *xdna,struct amdxdna_hwctx *hwctx) +{ +struct amdxdna_gem_obj *heap = hwctx->priv->heap; +int ret; + + ret =aie2_create_context (xdna->dev_handle, hwctx); +if (ret) { +XDNA_ERR (xdna,"Create hwctx failed, ret %d" , ret); + goto out; + } + + ret =aie2_map_host_buf (xdna->dev_handle, hwctx->fw_ctx_id, + heap->mem.userptr, heap->mem.size); +if (ret) { +XDNA_ERR (xdna,"Map host buf failed, ret %d" , ret); + goto out; + } + +if (hwctx->status !=HWCTX_STAT_READY ) { +XDNA_DBG (xdna,"hwctx is not ready, status %d" , hwctx->status); + goto out; + } + + ret =aie2_config_cu (hwctx); +if (ret) { +XDNA_ERR (xdna,"Config cu failed, ret %d" , ret); + goto out; + } + +out: +drm_sched_start (&hwctx->priv->sched); +XDNA_DBG (xdna,"%s restarted, ret %d" , hwctx->name, ret); +return ret; +} + +void aie2_stop_ctx_by_col_map (struct amdxdna_client *client,u32 col_map) +{ +struct amdxdna_dev *xdna = client->xdna; +struct amdxdna_hwctx *hwctx; +int next =0 ; + +drm_WARN_ON (&xdna->ddev, !mutex_is_locked (&xdna->dev_lock)); +mutex_lock (&client->hwctx_lock); +idr_for_each_entry_continue (&client->hwctx_idr, hwctx, next) { +/* check if the HW context uses the error column */ +if (!(col_map &amdxdna_hwctx_col_map (hwctx))) +continue ; + +aie2_hwctx_stop (xdna, hwctx,NULL ); + hwctx->old_status = hwctx->status; + hwctx->status =HWCTX_STAT_STOP ; +XDNA_DBG (xdna,"Stop %s" , hwctx->name); + } +mutex_unlock (&client->hwctx_lock); +} + +void aie2_restart_ctx (struct amdxdna_client *client) +{ +struct amdxdna_dev *xdna = client->xdna; +struct amdxdna_hwctx *hwctx; +int next =0 ; + +drm_WARN_ON (&xdna->ddev, !mutex_is_locked (&xdna->dev_lock)); +mutex_lock (&client->hwctx_lock); +idr_for_each_entry_continue (&client->hwctx_idr, hwctx, next) { +if (hwctx->status !=HWCTX_STAT_STOP ) +continue ; + + hwctx->status = hwctx->old_status; +XDNA_DBG (xdna,"Resetting %s" , hwctx->name); +aie2_hwctx_restart (xdna, hwctx); + } +mutex_unlock (&client->hwctx_lock); +} + +static struct dma_fence *aie2_cmd_get_out_fence (struct amdxdna_hwctx *hwctx,u64 seq) +{ +struct dma_fence *fence, *out_fence =NULL ; +int ret; + + fence =drm_syncobj_fence_get (hwctx->priv->syncobj); +if (!fence) +return NULL ; + + ret =dma_fence_chain_find_seqno (&fence, seq); +if (ret) + goto out; + + out_fence =dma_fence_get (dma_fence_chain_contained (fence)); + +out: +dma_fence_put (fence); +return out_fence; +} + +static void aie2_hwctx_wait_for_idle (struct amdxdna_hwctx *hwctx) +{ +struct dma_fence *fence; + + fence =aie2_cmd_get_out_fence (hwctx, hwctx->priv->seq -1 ); +if (!fence) +return ; + +dma_fence_wait (fence, false); +dma_fence_put (fence); +} + +static void +aie2_sched_notify (struct amdxdna_sched_job *job) +{ +struct dma_fence *fence = job->fence; + +trace_xdna_job (&job->base, job->hwctx->name,"signaled fence" , job->seq); + job->hwctx->priv->completed++; +dma_fence_signal (fence); + +up (&job->hwctx->priv->job_sem); + job->job_done = true; +dma_fence_put (fence); +mmput (job->mm); +aie2_job_put (job); +} + +static int +aie2_sched_resp_handler (void *handle,const u32 *data,size_t size) +{ +struct amdxdna_sched_job *job = handle; +struct amdxdna_gem_obj *cmd_abo; +u32 ret =0 ; +u32 status; + + cmd_abo = job->cmd_bo; + +if (unlikely (!data)) + goto out; + +if (unlikely (size !=sizeof (u32))) { +amdxdna_cmd_set_state (cmd_abo,ERT_CMD_STATE_ABORT ); + ret = -EINVAL ; + goto out; + } + + status = *data; +XDNA_DBG (job->hwctx->client->xdna,"Resp status 0x%x" , status); +if (status ==AIE2_STATUS_SUCCESS ) +amdxdna_cmd_set_state (cmd_abo,ERT_CMD_STATE_COMPLETED ); +else +amdxdna_cmd_set_state (cmd_abo,ERT_CMD_STATE_ERROR ); + +out: +aie2_sched_notify (job); +return ret; +} + +static int +aie2_sched_nocmd_resp_handler (void *handle,const u32 *data,size_t size) +{ +struct amdxdna_sched_job *job = handle; +u32 ret =0 ; +u32 status; + +if (unlikely (!data)) + goto out; + +if (unlikely (size !=sizeof (u32))) { + ret = -EINVAL ; + goto out; + } + + status = *data; +XDNA_DBG (job->hwctx->client->xdna,"Resp status 0x%x" , status); + +out: +aie2_sched_notify (job); +return ret; +} + +static int +aie2_sched_cmdlist_resp_handler (void *handle,const u32 *data,size_t size) +{ +struct amdxdna_sched_job *job = handle; +struct amdxdna_gem_obj *cmd_abo; +struct cmd_chain_resp *resp; +struct amdxdna_dev *xdna; +u32 fail_cmd_status; +u32 fail_cmd_idx; +u32 ret =0 ; + + cmd_abo = job->cmd_bo; +if (unlikely (!data) ||unlikely (size !=sizeof (u32) *3 )) { +amdxdna_cmd_set_state (cmd_abo,ERT_CMD_STATE_ABORT ); + ret = -EINVAL ; + goto out; + } + + resp = (struct cmd_chain_resp *)data; + xdna = job->hwctx->client->xdna; +XDNA_DBG (xdna,"Status 0x%x" , resp->status); +if (resp->status ==AIE2_STATUS_SUCCESS ) { +amdxdna_cmd_set_state (cmd_abo,ERT_CMD_STATE_COMPLETED ); + goto out; + } + +/* Slow path to handle error, read from ringbuf on BAR */ + fail_cmd_idx = resp->fail_cmd_idx; + fail_cmd_status = resp->fail_cmd_status; +XDNA_DBG (xdna,"Failed cmd idx %d, status 0x%x" , + fail_cmd_idx, fail_cmd_status); + +if (fail_cmd_status ==AIE2_STATUS_SUCCESS ) { +amdxdna_cmd_set_state (cmd_abo,ERT_CMD_STATE_ABORT ); + ret = -EINVAL ; + goto out; + } +amdxdna_cmd_set_state (cmd_abo, fail_cmd_status); + +if (amdxdna_cmd_get_op (cmd_abo) ==ERT_CMD_CHAIN ) { +struct amdxdna_cmd_chain *cc =amdxdna_cmd_get_payload (cmd_abo,NULL ); + + cc->error_index = fail_cmd_idx; +if (cc->error_index >= cc->command_count) + cc->error_index =0 ; + } +out: +aie2_sched_notify (job); +return ret; +} + +static struct dma_fence * +aie2_sched_job_run (struct drm_sched_job *sched_job) +{ +struct amdxdna_sched_job *job =drm_job_to_xdna_job (sched_job); +struct amdxdna_gem_obj *cmd_abo = job->cmd_bo; +struct amdxdna_hwctx *hwctx = job->hwctx; +struct dma_fence *fence; +int ret; + +if (!mmget_not_zero (job->mm)) +return ERR_PTR (-ESRCH ); + +kref_get (&job->refcnt); + fence =dma_fence_get (job->fence); + +if (unlikely (!cmd_abo)) { + ret =aie2_sync_bo (hwctx, job, aie2_sched_nocmd_resp_handler); + goto out; + } + +amdxdna_cmd_set_state (cmd_abo,ERT_CMD_STATE_NEW ); + +if (amdxdna_cmd_get_op (cmd_abo) ==ERT_CMD_CHAIN ) + ret =aie2_cmdlist_multi_execbuf (hwctx, job, aie2_sched_cmdlist_resp_handler); +else if (force_cmdlist) + ret =aie2_cmdlist_single_execbuf (hwctx, job, aie2_sched_cmdlist_resp_handler); +else + ret =aie2_execbuf (hwctx, job, aie2_sched_resp_handler); + +out: +if (ret) { +dma_fence_put (job->fence); +aie2_job_put (job); +mmput (job->mm); + fence =ERR_PTR (ret); + } +trace_xdna_job (sched_job, hwctx->name,"sent to device" , job->seq); + +return fence; +} + +static void aie2_sched_job_free (struct drm_sched_job *sched_job) +{ +struct amdxdna_sched_job *job =drm_job_to_xdna_job (sched_job); +struct amdxdna_hwctx *hwctx = job->hwctx; + +trace_xdna_job (sched_job, hwctx->name,"job free" , job->seq); +if (!job->job_done) +up (&hwctx->priv->job_sem); + +if (job->out_fence) +dma_fence_put (job->out_fence); +drm_sched_job_cleanup (sched_job); +aie2_job_put (job); +} + +static enum drm_gpu_sched_stat +aie2_sched_job_timedout (struct drm_sched_job *sched_job) +{ +struct amdxdna_sched_job *job =drm_job_to_xdna_job (sched_job); +struct amdxdna_hwctx *hwctx = job->hwctx; +struct amdxdna_dev *xdna; + + xdna = hwctx->client->xdna; +trace_xdna_job (sched_job, hwctx->name,"job timedout" , job->seq); +mutex_lock (&xdna->dev_lock); +aie2_hwctx_stop (xdna, hwctx, sched_job); + +aie2_hwctx_restart (xdna, hwctx); +mutex_unlock (&xdna->dev_lock); + +return DRM_GPU_SCHED_STAT_NOMINAL ; +} + +const struct drm_sched_backend_ops sched_ops = { + .run_job = aie2_sched_job_run, + .free_job = aie2_sched_job_free, + .timedout_job = aie2_sched_job_timedout, +}; +static int aie2_hwctx_col_list (struct amdxdna_hwctx *hwctx) {struct amdxdna_dev *xdna = hwctx->client->xdna;@@ -126,13 +466,66 @@ static void aie2_release_resource(struct amdxdna_hwctx *hwctx)XDNA_ERR (xdna,"Release AIE resource failed, ret %d" , ret); } +static int aie2_ctx_syncobj_create (struct amdxdna_hwctx *hwctx) +{ +struct amdxdna_dev *xdna = hwctx->client->xdna; +struct drm_file *filp = hwctx->client->filp; +struct drm_syncobj *syncobj; +u32 hdl; +int ret; + + hwctx->syncobj_hdl =AMDXDNA_INVALID_FENCE_HANDLE ; + + ret =drm_syncobj_create (&syncobj,0 ,NULL ); +if (ret) { +XDNA_ERR (xdna,"Create ctx syncobj failed, ret %d" , ret); +return ret; + } + ret =drm_syncobj_get_handle (filp, syncobj, &hdl); +if (ret) { +drm_syncobj_put (syncobj); +XDNA_ERR (xdna,"Create ctx syncobj handle failed, ret %d" , ret); +return ret; + } + hwctx->priv->syncobj = syncobj; + hwctx->syncobj_hdl = hdl; + +return 0 ; +} + +static void aie2_ctx_syncobj_destroy (struct amdxdna_hwctx *hwctx) +{ +/* +* The syncobj_hdl is owned by user space and will be cleaned up +* separately. +*/ +drm_syncobj_put (hwctx->priv->syncobj); +} + +static void aie2_ctx_syncobj_add_fence (struct amdxdna_hwctx *hwctx, +struct dma_fence *ofence,u64 seq)drivers/accel/amdxdna/aie2_ctx.c static void aie2_release_resource(struct amdxdna_hwctx *hwctx) · line 516
Quoted 10 lines
drivers/accel/amdxdna/aie2_ctx.c +10
-0+{ +struct drm_syncobj *syncobj = hwctx->priv->syncobj; +struct dma_fence_chain *chain; + +if (!syncobj) +return ; + + chain =dma_fence_chain_alloc (); +if (!chain) +return ;You have very subtlety broken dma-fencing rules. This is going to create a lock chain of:
mutex_lock(&hwctx->priv->io_lock)recliam();mutex_unlock(&hwctx->priv->io_lock)But you published the dma-fence for the job which is in the path of reclaim. Lockdep should complain if you have all the correct annotations and in theory you can deadlock.
Quoted 8 lines
So I think you prealloc the
chain()before publishing the dma-fence and then I'd also prime 'hwctx->priv->io_lock' which a reclaim annotation.e.g.
fs_reclaim_acquire();might_lock(&hwctx->priv->io_lock);fs_reclaim_release();
Ok, it makes sense to prealloc the chain.
Quoted 276 lines
Quoted 259 lines
drivers/accel/amdxdna/aie2_ctx.c +199
-3+ +drm_syncobj_add_point (syncobj, chain, ofence, seq); +} +int aie2_hwctx_init (struct amdxdna_hwctx *hwctx) {struct amdxdna_client *client = hwctx->client;struct amdxdna_dev *xdna = client->xdna; +struct drm_gpu_scheduler *sched;struct amdxdna_hwctx_priv *priv;struct amdxdna_gem_obj *heap; -int ret; +int i, ret; priv =kzalloc (sizeof (*hwctx->priv),GFP_KERNEL );if (!priv)@@ -150,6 +543,7 @@ int aie2_hwctx_init(struct amdxdna_hwctx *hwctx)drm_gem_object_get (to_gobj (heap));mutex_unlock (&client->mm_lock); priv->heap = heap; +sema_init (&priv->job_sem,HWCTX_MAX_CMDS ); ret =amdxdna_gem_pin (heap);if (ret) {@@ -157,10 +551,47 @@ int aie2_hwctx_init(struct amdxdna_hwctx *hwctx) goto put_heap; } +for (i =0 ; i <ARRAY_SIZE (priv->cmd_buf); i++) { +struct amdxdna_gem_obj *abo; +struct amdxdna_drm_create_bo args = { + .flags =0 , + .type =AMDXDNA_BO_DEV , + .vaddr =0 , + .size =MAX_CHAIN_CMDBUF_SIZE , + }; + + abo =amdxdna_drm_alloc_dev_bo (&xdna->ddev, &args, client->filp, true); +if (IS_ERR (abo)) { + ret =PTR_ERR (abo); + goto free_cmd_bufs; + } + +XDNA_DBG (xdna,"Command buf %d addr 0x%llx size 0x%lx" , + i, abo->mem.dev_addr, abo->mem.size); + priv->cmd_buf[i] = abo; + } + + sched = &priv->sched; +mutex_init (&priv->io_lock); + ret =drm_sched_init (sched, &sched_ops,NULL ,DRM_SCHED_PRIORITY_COUNT , +HWCTX_MAX_CMDS ,0 ,msecs_to_jiffies (HWCTX_MAX_TIMEOUT ), +NULL ,NULL , hwctx->name, xdna->ddev.dev); +if (ret) { +XDNA_ERR (xdna,"Failed to init DRM scheduler. ret %d" , ret); + goto free_cmd_bufs; + } + + ret =drm_sched_entity_init (&priv->entity,DRM_SCHED_PRIORITY_NORMAL , + &sched,1 ,NULL ); +if (ret) { +XDNA_ERR (xdna,"Failed to initial sched entiry. ret %d" , ret); + goto free_sched; + } + ret =aie2_hwctx_col_list (hwctx);if (ret) {XDNA_ERR (xdna,"Create col list failed, ret %d" , ret); - goto unpin; + goto free_entity; } ret =aie2_alloc_resource (hwctx);@@ -175,6 +606,13 @@ int aie2_hwctx_init(struct amdxdna_hwctx *hwctx)XDNA_ERR (xdna,"Map host buffer failed, ret %d" , ret); goto release_resource; } + + ret =aie2_ctx_syncobj_create (hwctx); +if (ret) { +XDNA_ERR (xdna,"Create syncobj failed, ret %d" , ret); + goto release_resource; + } + hwctx->status =HWCTX_STAT_INIT ;XDNA_DBG (xdna,"hwctx %s init completed" , hwctx->name);@@ -185,7 +623,16 @@ int aie2_hwctx_init(struct amdxdna_hwctx *hwctx)aie2_release_resource (hwctx); free_col_list:kfree (hwctx->col_list); -unpin: +free_entity: +drm_sched_entity_destroy (&priv->entity); +free_sched: +drm_sched_fini (&priv->sched); +free_cmd_bufs: +for (i =0 ; i <ARRAY_SIZE (priv->cmd_buf); i++) { +if (!priv->cmd_buf[i]) +continue ; +drm_gem_object_put (to_gobj (priv->cmd_buf[i])); + }amdxdna_gem_unpin (heap); put_heap:drm_gem_object_put (to_gobj (heap));@@ -196,11 +643,35 @@ int aie2_hwctx_init(struct amdxdna_hwctx *hwctx)void aie2_hwctx_fini (struct amdxdna_hwctx *hwctx) { +struct amdxdna_dev *xdna; +int idx; + + xdna = hwctx->client->xdna; +drm_sched_wqueue_stop (&hwctx->priv->sched); + +/* Now, scheduler will not send command to device. */ aie2_release_resource (hwctx); +/* +* All submitted commands are aborted. +* Restart scheduler queues to cleanup jobs. The amdxdna_sched_job_run() +* will return NODEV if it is called. +*/ +drm_sched_wqueue_start (&hwctx->priv->sched); + +aie2_hwctx_wait_for_idle (hwctx); +drm_sched_entity_destroy (&hwctx->priv->entity); +drm_sched_fini (&hwctx->priv->sched); +aie2_ctx_syncobj_destroy (hwctx); + +XDNA_DBG (xdna,"%s sequence number %lld" , hwctx->name, hwctx->priv->seq); + +for (idx =0 ; idx <ARRAY_SIZE (hwctx->priv->cmd_buf); idx++) +drm_gem_object_put (to_gobj (hwctx->priv->cmd_buf[idx]));amdxdna_gem_unpin (hwctx->priv->heap);drm_gem_object_put (to_gobj (hwctx->priv->heap)); +mutex_destroy (&hwctx->priv->io_lock);kfree (hwctx->col_list);kfree (hwctx->priv);kfree (hwctx->cus);@@ -267,3 +738,160 @@ int aie2_hwctx_config(struct amdxdna_hwctx *hwctx, u32 type, u64 value, void *bureturn -EOPNOTSUPP ; } } + +static int aie2_populate_range (struct amdxdna_gem_obj *abo) +{ +struct amdxdna_dev *xdna =to_xdna_dev (to_gobj (abo)->dev); +struct mm_struct *mm = abo->mem.notifier.mm; +struct hmm_range range = {0 }; +unsigned long timeout; +int ret; + +XDNA_INFO_ONCE (xdna,"populate memory range %llx size %lx" , + abo->mem.userptr, abo->mem.size); + range.notifier = &abo->mem.notifier; + range.start = abo->mem.userptr; + range.end = abo->mem.userptr + abo->mem.size; + range.hmm_pfns = abo->mem.pfns; + range.default_flags =HMM_PFN_REQ_FAULT ; + +if (!mmget_not_zero (mm)) +return -EFAULT ; + + timeout = jiffies +msecs_to_jiffies (HMM_RANGE_DEFAULT_TIMEOUT ); +again: + range.notifier_seq =mmu_interval_read_begin (&abo->mem.notifier); +mmap_read_lock (mm); + ret =hmm_range_fault (&range); +mmap_read_unlock (mm); +if (ret) { +if (time_after (jiffies, timeout)) { + ret = -ETIME ; + goto put_mm; + } + +if (ret == -EBUSY ) + goto again; + + goto put_mm; + } + +read_lock (&xdna->notifier_lock); +if (mmu_interval_read_retry (&abo->mem.notifier, range.notifier_seq)) { +read_unlock (&xdna->notifier_lock); + goto again; + } + abo->mem.map_invalid = false; +read_unlock (&xdna->notifier_lock); + +put_mm: +mmput (mm); +return ret; +} + +static void aie2_hwctx_push_job (struct amdxdna_sched_job *job,u64 *seq) +{ +struct amdxdna_hwctx *hwctx = job->hwctx; + +mutex_lock (&hwctx->priv->io_lock); +drm_sched_job_arm (&job->base); + job->seq = hwctx->priv->seq++; + *seq = job->seq; + + job->out_fence =dma_fence_get (&job->base.s_fence->finished); +drm_sched_entity_push_job (&job->base); +aie2_ctx_syncobj_add_fence (hwctx, job->out_fence, *seq); +mutex_unlock (&hwctx->priv->io_lock); +} + +int aie2_cmd_submit (struct amdxdna_hwctx *hwctx,struct amdxdna_sched_job *job,u64 *seq) +{ +struct amdxdna_dev *xdna = hwctx->client->xdna; +struct ww_acquire_ctx acquire_ctx; +struct amdxdna_gem_obj *abo; +unsigned long timeout =0 ; +int ret, i; + + ret =down_interruptible (&hwctx->priv->job_sem); +if (ret) { +XDNA_ERR (xdna,"Grab job sem failed, ret %d" , ret); +return ret; + } + + ret =drm_sched_job_init (&job->base, &hwctx->priv->entity,1 , hwctx); +if (ret) { +XDNA_ERR (xdna,"DRM job init failed, ret %d" , ret); + goto up_sem; + } + +retry: + ret =drm_gem_lock_reservations (job->bos, job->bo_cnt, &acquire_ctx); +if (ret) { +XDNA_WARN (xdna,"Failed to lock BOs, ret %d" , ret); + goto cleanup_job; + } + +for (i =0 ; i < job->bo_cnt; i++) { + ret =dma_resv_reserve_fences (job->bos[i]->resv,1 ); +if (ret) { +XDNA_WARN (xdna,"Failed to reserve fences %d" , ret); +drm_gem_unlock_reservations (job->bos, job->bo_cnt, &acquire_ctx); + goto cleanup_job; + } + } + +read_lock (&xdna->notifier_lock); +for (i =0 ; i < job->bo_cnt; i++) { + abo =to_xdna_obj (job->bos[i]); +if (abo->mem.map_invalid) { +read_unlock (&xdna->notifier_lock); +drm_gem_unlock_reservations (job->bos, job->bo_cnt, &acquire_ctx); +if (!timeout) { + timeout = jiffies + +msecs_to_jiffies (HMM_RANGE_DEFAULT_TIMEOUT ); + }else if (time_after (jiffies, timeout)) { + ret = -ETIME ;drivers/accel/amdxdna/aie2_ctx.c int aie2_hwctx_config(struct amdxdna_hwctx *hwctx, u32 type, u64 value, void *bu · line 863
Quoted 10 lines
drivers/accel/amdxdna/aie2_ctx.c +10
-0+ goto cleanup_job; + } + + ret =aie2_populate_range (abo); +if (ret) + goto cleanup_job; + goto retry; + } + } +Any reason you can't arm the job here and install job's finished fence in dma-resv slot? IIRC at one point in time in Xe we used the '
job->fence', this is a hardware fence in Xe, assuming this the same here and it ended up causing some issues - exactly what I cannot recall. The scheduler again is really designed to install the job's finished fence in dma-resv / syncobjs rather than driver internal fences.
Quoted 2 lines
That of course would mean taking
hwctx->priv->io_lockunderneath all other locks in this loop at the final step.
Ok, I will change this.
Thanks,
Lizhi
Quoted 1,386 lines
Matt
Quoted 1,383 lines
drivers/accel/amdxdna/aie2_ctx.c +34
-0+for (i =0 ; i < job->bo_cnt; i++) +dma_resv_add_fence (job->bos[i]->resv, job->fence,DMA_RESV_USAGE_WRITE ); + +read_unlock (&xdna->notifier_lock); +drm_gem_unlock_reservations (job->bos, job->bo_cnt, &acquire_ctx); + +aie2_hwctx_push_job (job, seq); + +return 0 ; + +cleanup_job: +drm_sched_job_cleanup (&job->base); +up_sem: +up (&hwctx->priv->job_sem); + job->job_done = true; +return ret; +} + +void aie2_hmm_invalidate (struct amdxdna_gem_obj *abo, +unsigned long cur_seq) +{ +struct amdxdna_dev *xdna =to_xdna_dev (to_gobj (abo)->dev); +struct drm_gem_object *gobj =to_gobj (abo); +long ret; + +write_lock (&xdna->notifier_lock); + abo->mem.map_invalid = true; +mmu_interval_set_seq (&abo->mem.notifier, cur_seq); +write_unlock (&xdna->notifier_lock); + ret =dma_resv_wait_timeout (gobj->resv,DMA_RESV_USAGE_BOOKKEEP , + true,MAX_SCHEDULE_TIMEOUT ); +if (!ret || ret == -ERESTARTSYS ) +XDNA_ERR (xdna,"Failed to wait for bo, ret %ld" , ret); +}drivers/accel/amdxdna/aie2_message.c +343
-0@@ -4,10 +4,12 @@*/ #include <drm/amdxdna_accel.h> +#include <drm/drm_cache.h> #include <drm/drm_device.h> #include <drm/drm_gem.h> #include <drm/drm_gem_shmem_helper.h> #include <drm/drm_print.h> +#include <drm/gpu_scheduler.h> #include <linux/bitfield.h> #include <linux/errno.h> #include <linux/pci.h> @@ -362,3 +364,344 @@ int aie2_config_cu(struct amdxdna_hwctx *hwctx) msg.opcode, resp.status, ret);return ret; } + +int aie2_execbuf (struct amdxdna_hwctx *hwctx,struct amdxdna_sched_job *job, +int (*notify_cb)(void *,const u32 *,size_t )) +{ +struct mailbox_channel *chann = hwctx->priv->mbox_chann; +struct amdxdna_dev *xdna = hwctx->client->xdna; +struct amdxdna_gem_obj *cmd_abo = job->cmd_bo; +union { +struct execute_buffer_req ebuf; +struct exec_dpu_req dpu; + } req; +struct xdna_mailbox_msg msg; +u32 payload_len; +void *payload; +int cu_idx; +int ret; +u32 op; + +if (!chann) +return -ENODEV ; + + payload =amdxdna_cmd_get_payload (cmd_abo, &payload_len); +if (!payload) { +XDNA_ERR (xdna,"Invalid command, cannot get payload" ); +return -EINVAL ; + } + + cu_idx =amdxdna_cmd_get_cu_idx (cmd_abo); +if (cu_idx <0 ) { +XDNA_DBG (xdna,"Invalid cu idx" ); +return -EINVAL ; + } + + op =amdxdna_cmd_get_op (cmd_abo); +switch (op) { +case ERT_START_CU : +if (unlikely (payload_len >sizeof (req.ebuf.payload))) +XDNA_DBG (xdna,"Invalid ebuf payload len: %d" , payload_len); + req.ebuf.cu_idx = cu_idx; +memcpy (req.ebuf.payload, payload,sizeof (req.ebuf.payload)); + msg.send_size =sizeof (req.ebuf); + msg.opcode =MSG_OP_EXECUTE_BUFFER_CF ; +break ; +case ERT_START_NPU : { +struct amdxdna_cmd_start_npu *sn = payload; + +if (unlikely (payload_len -sizeof (*sn) >sizeof (req.dpu.payload))) +XDNA_DBG (xdna,"Invalid dpu payload len: %d" , payload_len); + req.dpu.inst_buf_addr = sn->buffer; + req.dpu.inst_size = sn->buffer_size; + req.dpu.inst_prop_cnt = sn->prop_count; + req.dpu.cu_idx = cu_idx; +memcpy (req.dpu.payload, sn->prop_args,sizeof (req.dpu.payload)); + msg.send_size =sizeof (req.dpu); + msg.opcode =MSG_OP_EXEC_DPU ; +break ; + } +default : +XDNA_DBG (xdna,"Invalid ERT cmd op code: %d" , op); +return -EINVAL ; + } + msg.handle = job; + msg.notify_cb = notify_cb; + msg.send_data = (u8 *)&req; +print_hex_dump_debug ("cmd: " ,DUMP_PREFIX_OFFSET ,16 ,4 , &req, +0x40 , false); + + ret =xdna_mailbox_send_msg (chann, &msg,TX_TIMEOUT ); +if (ret) { +XDNA_ERR (xdna,"Send message failed" ); +return ret; + } + +return 0 ; +} + +static int +aie2_cmdlist_fill_one_slot_cf (void *cmd_buf,u32 offset, +struct amdxdna_gem_obj *abo,u32 *size) +{ +struct cmd_chain_slot_execbuf_cf *buf = cmd_buf + offset; +int cu_idx =amdxdna_cmd_get_cu_idx (abo); +u32 payload_len; +void *payload; + +if (cu_idx <0 ) +return -EINVAL ; + + payload =amdxdna_cmd_get_payload (abo, &payload_len); +if (!payload) +return -EINVAL ; + +if (!slot_cf_has_space (offset, payload_len)) +return -ENOSPC ; + + buf->cu_idx = cu_idx; + buf->arg_cnt = payload_len /sizeof (u32); +memcpy (buf->args, payload, payload_len); +/* Accurate buf size to hint firmware to do necessary copy */ + *size =sizeof (*buf) + payload_len; +return 0 ; +} + +static int +aie2_cmdlist_fill_one_slot_dpu (void *cmd_buf,u32 offset, +struct amdxdna_gem_obj *abo,u32 *size) +{ +struct cmd_chain_slot_dpu *buf = cmd_buf + offset; +int cu_idx =amdxdna_cmd_get_cu_idx (abo); +struct amdxdna_cmd_start_npu *sn; +u32 payload_len; +void *payload; +u32 arg_sz; + +if (cu_idx <0 ) +return -EINVAL ; + + payload =amdxdna_cmd_get_payload (abo, &payload_len); +if (!payload) +return -EINVAL ; + sn = payload; + arg_sz = payload_len -sizeof (*sn); +if (payload_len <sizeof (*sn) || arg_sz >MAX_DPU_ARGS_SIZE ) +return -EINVAL ; + +if (!slot_dpu_has_space (offset, arg_sz)) +return -ENOSPC ; + + buf->inst_buf_addr = sn->buffer; + buf->inst_size = sn->buffer_size; + buf->inst_prop_cnt = sn->prop_count; + buf->cu_idx = cu_idx; + buf->arg_cnt = arg_sz /sizeof (u32); +memcpy (buf->args, sn->prop_args, arg_sz); + +/* Accurate buf size to hint firmware to do necessary copy */ + *size +=sizeof (*buf) + arg_sz; +return 0 ; +} + +static int +aie2_cmdlist_fill_one_slot (u32 op,struct amdxdna_gem_obj *cmdbuf_abo,u32 offset, +struct amdxdna_gem_obj *abo,u32 *size) +{ +u32 this_op =amdxdna_cmd_get_op (abo); +void *cmd_buf = cmdbuf_abo->mem.kva; +int ret; + +if (this_op != op) { + ret = -EINVAL ; + goto done; + } + +switch (op) { +case ERT_START_CU : + ret =aie2_cmdlist_fill_one_slot_cf (cmd_buf, offset, abo, size); +break ; +case ERT_START_NPU : + ret =aie2_cmdlist_fill_one_slot_dpu (cmd_buf, offset, abo, size); +break ; +default : + ret = -EOPNOTSUPP ; + } + +done: +if (ret) { +XDNA_ERR (abo->client->xdna,"Can't fill slot for cmd op %d ret %d" , + op, ret); + } +return ret; +} + +static inline struct amdxdna_gem_obj * +aie2_cmdlist_get_cmd_buf (struct amdxdna_sched_job *job) +{ +int idx =get_job_idx (job->seq); + +return job->hwctx->priv->cmd_buf[idx]; +} + +static void +aie2_cmdlist_prepare_request (struct cmd_chain_req *req, +struct amdxdna_gem_obj *cmdbuf_abo,u32 size,u32 cnt) +{ + req->buf_addr = cmdbuf_abo->mem.dev_addr; + req->buf_size = size; + req->count = cnt; +drm_clflush_virt_range (cmdbuf_abo->mem.kva, size); +XDNA_DBG (cmdbuf_abo->client->xdna,"Command buf addr 0x%llx size 0x%x count %d" , + req->buf_addr, size, cnt); +} + +static inline u32 +aie2_cmd_op_to_msg_op (u32 op) +{ +switch (op) { +case ERT_START_CU : +return MSG_OP_CHAIN_EXEC_BUFFER_CF ; +case ERT_START_NPU : +return MSG_OP_CHAIN_EXEC_DPU ; +default : +return MSG_OP_MAX_OPCODE ; + } +} + +int aie2_cmdlist_multi_execbuf (struct amdxdna_hwctx *hwctx, +struct amdxdna_sched_job *job, +int (*notify_cb)(void *,const u32 *,size_t )) +{ +struct amdxdna_gem_obj *cmdbuf_abo =aie2_cmdlist_get_cmd_buf (job); +struct mailbox_channel *chann = hwctx->priv->mbox_chann; +struct amdxdna_client *client = hwctx->client; +struct amdxdna_gem_obj *cmd_abo = job->cmd_bo; +struct amdxdna_cmd_chain *payload; +struct xdna_mailbox_msg msg; +struct cmd_chain_req req; +u32 payload_len; +u32 offset =0 ; +u32 size; +int ret; +u32 op; +u32 i; + + op =amdxdna_cmd_get_op (cmd_abo); + payload =amdxdna_cmd_get_payload (cmd_abo, &payload_len); +if (op !=ERT_CMD_CHAIN || !payload || + payload_len <struct_size (payload, data, payload->command_count)) +return -EINVAL ; + +for (i =0 ; i < payload->command_count; i++) { +u32 boh = (u32)(payload->data[i]); +struct amdxdna_gem_obj *abo; + + abo =amdxdna_gem_get_obj (client, boh,AMDXDNA_BO_CMD ); +if (!abo) { +XDNA_ERR (client->xdna,"Failed to find cmd BO %d" , boh); +return -ENOENT ; + } + +/* All sub-cmd should have same op, use the first one. */ +if (i ==0 ) + op =amdxdna_cmd_get_op (abo); + + ret =aie2_cmdlist_fill_one_slot (op, cmdbuf_abo, offset, abo, &size); +amdxdna_gem_put_obj (abo); +if (ret) +return -EINVAL ; + + offset += size; + } + +/* The offset is the accumulated total size of the cmd buffer */ +aie2_cmdlist_prepare_request (&req, cmdbuf_abo, offset, payload->command_count); + + msg.opcode =aie2_cmd_op_to_msg_op (op); +if (msg.opcode ==MSG_OP_MAX_OPCODE ) +return -EOPNOTSUPP ; + msg.handle = job; + msg.notify_cb = notify_cb; + msg.send_data = (u8 *)&req; + msg.send_size =sizeof (req); + ret =xdna_mailbox_send_msg (chann, &msg,TX_TIMEOUT ); +if (ret) { +XDNA_ERR (hwctx->client->xdna,"Send message failed" ); +return ret; + } + +return 0 ; +} + +int aie2_cmdlist_single_execbuf (struct amdxdna_hwctx *hwctx, +struct amdxdna_sched_job *job, +int (*notify_cb)(void *,const u32 *,size_t )) +{ +struct amdxdna_gem_obj *cmdbuf_abo =aie2_cmdlist_get_cmd_buf (job); +struct mailbox_channel *chann = hwctx->priv->mbox_chann; +struct amdxdna_gem_obj *cmd_abo = job->cmd_bo; +struct xdna_mailbox_msg msg; +struct cmd_chain_req req; +u32 size; +int ret; +u32 op; + + op =amdxdna_cmd_get_op (cmd_abo); + ret =aie2_cmdlist_fill_one_slot (op, cmdbuf_abo,0 , cmd_abo, &size); +if (ret) +return ret; + +aie2_cmdlist_prepare_request (&req, cmdbuf_abo, size,1 ); + + msg.opcode =aie2_cmd_op_to_msg_op (op); +if (msg.opcode ==MSG_OP_MAX_OPCODE ) +return -EOPNOTSUPP ; + msg.handle = job; + msg.notify_cb = notify_cb; + msg.send_data = (u8 *)&req; + msg.send_size =sizeof (req); + ret =xdna_mailbox_send_msg (chann, &msg,TX_TIMEOUT ); +if (ret) { +XDNA_ERR (hwctx->client->xdna,"Send message failed" ); +return ret; + } + +return 0 ; +} + +int aie2_sync_bo (struct amdxdna_hwctx *hwctx,struct amdxdna_sched_job *job, +int (*notify_cb)(void *,const u32 *,size_t )) +{ +struct mailbox_channel *chann = hwctx->priv->mbox_chann; +struct amdxdna_gem_obj *abo =to_xdna_obj (job->bos[0 ]); +struct amdxdna_dev *xdna = hwctx->client->xdna; +struct xdna_mailbox_msg msg; +struct sync_bo_req req; +int ret =0 ; + + req.src_addr =0 ; + req.dst_addr = abo->mem.dev_addr - hwctx->client->dev_heap->mem.dev_addr; + req.size = abo->mem.size; + +/* Device to Host */ + req.type =FIELD_PREP (AIE2_MSG_SYNC_BO_SRC_TYPE ,SYNC_BO_DEV_MEM ) | +FIELD_PREP (AIE2_MSG_SYNC_BO_DST_TYPE ,SYNC_BO_HOST_MEM ); + +XDNA_DBG (xdna,"sync %d bytes src(0x%llx) to dst(0x%llx) completed" , + req.size, req.src_addr, req.dst_addr); + + msg.handle = job; + msg.notify_cb = notify_cb; + msg.send_data = (u8 *)&req; + msg.send_size =sizeof (req); + msg.opcode =MSG_OP_SYNC_BO ; + + ret =xdna_mailbox_send_msg (chann, &msg,TX_TIMEOUT ); +if (ret) { +XDNA_ERR (xdna,"Send message failed" ); +return ret; + } + +return 0 ; +}drivers/accel/amdxdna/aie2_pci.c +5
-0@@ -5,8 +5,10 @@#include <drm/amdxdna_accel.h> #include <drm/drm_device.h> +#include <drm/drm_gem_shmem_helper.h> #include <drm/drm_managed.h> #include <drm/drm_print.h> +#include <drm/gpu_scheduler.h> #include <linux/errno.h> #include <linux/firmware.h> #include <linux/iommu.h> @@ -17,6 +19,7 @@#include "aie2_pci.h" #include "aie2_solver.h" #include "amdxdna_ctx.h" +#include "amdxdna_gem.h" #include "amdxdna_mailbox.h" #include "amdxdna_pci_drv.h" @@ -495,4 +498,6 @@ const struct amdxdna_dev_ops aie2_ops = { .hwctx_init = aie2_hwctx_init, .hwctx_fini = aie2_hwctx_fini, .hwctx_config = aie2_hwctx_config, + .cmd_submit = aie2_cmd_submit, + .hmm_invalidate = aie2_hmm_invalidate, };drivers/accel/amdxdna/aie2_pci.h +40
-0@@ -6,6 +6,8 @@#ifndef _AIE2_PCI_H_#define _AIE2_PCI_H_ +#include <linux/semaphore.h> +#include "amdxdna_mailbox.h" #define AIE2_INTERVAL 20000/* us */ @@ -76,8 +78,10 @@ enum psp_reg_idx {PSP_MAX_REGS /* Keep this at the end */ }; +struct amdxdna_client ;struct amdxdna_fw_ver ;struct amdxdna_hwctx ; +struct amdxdna_sched_job ;struct psp_config {const void *fw_buf;@@ -118,9 +122,31 @@ struct rt_config {u32 value; }; +/* +* Define the maximum number of pending commands in a hardware context. +* Must be power of 2! +*/ +#define HWCTX_MAX_CMDS 4 +#define get_job_idx (seq) ((seq) & (HWCTX_MAX_CMDS - 1))struct amdxdna_hwctx_priv {struct amdxdna_gem_obj *heap;void *mbox_chann; + +struct drm_gpu_scheduler sched; +struct drm_sched_entity entity; + +struct mutex io_lock;/* protect seq and cmd order */ +struct wait_queue_head job_free_wq; +u32 num_pending; +u64 seq; +struct semaphore job_sem; +bool job_done; + +/* Completed job counter */ +u64 completed; + +struct amdxdna_gem_obj *cmd_buf[HWCTX_MAX_CMDS ]; +struct drm_syncobj *syncobj; };struct amdxdna_dev_hdl {@@ -199,10 +225,24 @@ int aie2_create_context(struct amdxdna_dev_hdl *ndev, struct amdxdna_hwctx *hwctint aie2_destroy_context (struct amdxdna_dev_hdl *ndev,struct amdxdna_hwctx *hwctx);int aie2_map_host_buf (struct amdxdna_dev_hdl *ndev,u32 context_id,u64 addr,u64 size);int aie2_config_cu (struct amdxdna_hwctx *hwctx); +int aie2_execbuf (struct amdxdna_hwctx *hwctx,struct amdxdna_sched_job *job, +int (*notify_cb)(void *,const u32 *,size_t )); +int aie2_cmdlist_single_execbuf (struct amdxdna_hwctx *hwctx, +struct amdxdna_sched_job *job, +int (*notify_cb)(void *,const u32 *,size_t )); +int aie2_cmdlist_multi_execbuf (struct amdxdna_hwctx *hwctx, +struct amdxdna_sched_job *job, +int (*notify_cb)(void *,const u32 *,size_t )); +int aie2_sync_bo (struct amdxdna_hwctx *hwctx,struct amdxdna_sched_job *job, +int (*notify_cb)(void *,const u32 *,size_t ));/* aie2_hwctx.c */ int aie2_hwctx_init (struct amdxdna_hwctx *hwctx);void aie2_hwctx_fini (struct amdxdna_hwctx *hwctx);int aie2_hwctx_config (struct amdxdna_hwctx *hwctx,u32 type,u64 value,void *buf,u32 size); +int aie2_cmd_submit (struct amdxdna_hwctx *hwctx,struct amdxdna_sched_job *job,u64 *seq); +void aie2_hmm_invalidate (struct amdxdna_gem_obj *abo,unsigned long cur_seq); +void aie2_stop_ctx_by_col_map (struct amdxdna_client *client,u32 col_map); +void aie2_restart_ctx (struct amdxdna_client *client);#endif /* _AIE2_PCI_H_ */ drivers/accel/amdxdna/aie2_psp.c +2
-0@@ -4,8 +4,10 @@*/ #include <drm/drm_device.h> +#include <drm/drm_gem_shmem_helper.h> #include <drm/drm_managed.h> #include <drm/drm_print.h> +#include <drm/gpu_scheduler.h> #include <linux/bitfield.h> #include <linux/iopoll.h> drivers/accel/amdxdna/aie2_smu.c +2
-0@@ -4,7 +4,9 @@*/ #include <drm/drm_device.h> +#include <drm/drm_gem_shmem_helper.h> #include <drm/drm_print.h> +#include <drm/gpu_scheduler.h> #include <linux/iopoll.h> #include "aie2_pci.h" drivers/accel/amdxdna/amdxdna_ctx.c +314
-6@@ -7,17 +7,65 @@#include <drm/drm_device.h> #include <drm/drm_drv.h> #include <drm/drm_file.h> +#include <drm/drm_gem.h> +#include <drm/drm_gem_shmem_helper.h> #include <drm/drm_print.h> +#include <drm/gpu_scheduler.h> +#include <trace/events/amdxdna.h> #include "amdxdna_ctx.h" +#include "amdxdna_gem.h" #include "amdxdna_pci_drv.h" #define MAX_HWCTX_ID 255 +#define MAX_ARG_COUNT 4095 -static void amdxdna_hwctx_destroy (struct amdxdna_hwctx *hwctx) +struct amdxdna_fence { +struct dma_fence base; +spinlock_t lock;/* for base */ +struct amdxdna_hwctx *hwctx; +}; + +static const char *amdxdna_fence_get_driver_name (struct dma_fence *fence) +{ +return KBUILD_MODNAME ; +} + +static const char *amdxdna_fence_get_timeline_name (struct dma_fence *fence) +{ +struct amdxdna_fence *xdna_fence; + + xdna_fence = container_of(fence, struct amdxdna_fence, base); + +return xdna_fence->hwctx->name; +} + +static const struct dma_fence_ops fence_ops = { + .get_driver_name = amdxdna_fence_get_driver_name, + .get_timeline_name = amdxdna_fence_get_timeline_name, +}; + +static struct dma_fence *amdxdna_fence_create (struct amdxdna_hwctx *hwctx) +{ +struct amdxdna_fence *fence; + + fence =kzalloc (sizeof (*fence),GFP_KERNEL ); +if (!fence) +return NULL ; + + fence->hwctx = hwctx; +spin_lock_init (&fence->lock); +dma_fence_init (&fence->base, &fence_ops, &fence->lock, hwctx->id,0 ); +return &fence->base; +} + +static void amdxdna_hwctx_destroy_rcu (struct amdxdna_hwctx *hwctx, +struct srcu_struct *ss) {struct amdxdna_dev *xdna = hwctx->client->xdna; +synchronize_srcu (ss); +/* At this point, user is not able to submit new commands */ mutex_lock (&xdna->dev_lock); xdna->dev_info->ops->hwctx_fini (hwctx);@@ -27,6 +75,46 @@ static void amdxdna_hwctx_destroy(struct amdxdna_hwctx *hwctx)kfree (hwctx); } +void *amdxdna_cmd_get_payload (struct amdxdna_gem_obj *abo,u32 *size) +{ +struct amdxdna_cmd *cmd = abo->mem.kva; +u32 num_masks, count; + +if (amdxdna_cmd_get_op (abo) ==ERT_CMD_CHAIN ) + num_masks =0 ; +else + num_masks =1 +FIELD_GET (AMDXDNA_CMD_EXTRA_CU_MASK , cmd->header); + +if (size) { + count =FIELD_GET (AMDXDNA_CMD_COUNT , cmd->header); +if (unlikely (count <= num_masks)) { + *size =0 ; +return NULL ; + } + *size = (count - num_masks) *sizeof (u32); + } +return &cmd->data[num_masks]; +} + +int amdxdna_cmd_get_cu_idx (struct amdxdna_gem_obj *abo) +{ +struct amdxdna_cmd *cmd = abo->mem.kva; +u32 num_masks, i; +u32 *cu_mask; + +if (amdxdna_cmd_get_op (abo) ==ERT_CMD_CHAIN ) +return -1 ; + + num_masks =1 +FIELD_GET (AMDXDNA_CMD_EXTRA_CU_MASK , cmd->header); + cu_mask = cmd->data; +for (i =0 ; i < num_masks; i++) { +if (cu_mask[i]) +return ffs (cu_mask[i]) -1 ; + } + +return -1 ; +} +/* * This should be called in close() and remove(). DO NOT call in other syscalls. * This guarantee that when hwctx and resources will be released, if user @@ -43,7 +131,7 @@ void amdxdna_hwctx_remove_all(struct amdxdna_client *client) client->pid, hwctx->id);idr_remove (&client->hwctx_idr, hwctx->id);mutex_unlock (&client->hwctx_lock); -amdxdna_hwctx_destroy (hwctx); +amdxdna_hwctx_destroy_rcu (hwctx, &client->hwctx_srcu);mutex_lock (&client->hwctx_lock); }mutex_unlock (&client->hwctx_lock);@@ -135,6 +223,12 @@ int amdxdna_drm_destroy_hwctx_ioctl(struct drm_device *dev, void *data, struct dif (!drm_dev_enter (dev, &idx))return -ENODEV ; +/* +* Use hwctx_lock to achieve exclusion with other hwctx writers, +* SRCU to synchronize with exec/wait command ioctls. +* +* The pushed jobs are handled by DRM scheduler during destroy. +*/ mutex_lock (&client->hwctx_lock); hwctx =idr_find (&client->hwctx_idr, args->handle);if (!hwctx) {@@ -147,7 +241,7 @@ int amdxdna_drm_destroy_hwctx_ioctl(struct drm_device *dev, void *data, struct didr_remove (&client->hwctx_idr, hwctx->id);mutex_unlock (&client->hwctx_lock); -amdxdna_hwctx_destroy (hwctx); +amdxdna_hwctx_destroy_rcu (hwctx, &client->hwctx_srcu);XDNA_DBG (xdna,"PID %d destroyed HW context %d" , client->pid, args->handle); out:@@ -161,10 +255,10 @@ int amdxdna_drm_config_hwctx_ioctl(struct drm_device *dev, void *data, struct drstruct amdxdna_drm_config_hwctx *args = data;struct amdxdna_dev *xdna =to_xdna_dev (dev);struct amdxdna_hwctx *hwctx; +int ret, idx;u32 buf_size;void *buf;u64 val; -int ret;if (!xdna->dev_info->ops->hwctx_config)return -EOPNOTSUPP ;@@ -203,17 +297,231 @@ int amdxdna_drm_config_hwctx_ioctl(struct drm_device *dev, void *data, struct dr }mutex_lock (&xdna->dev_lock); + idx =srcu_read_lock (&client->hwctx_srcu); hwctx =idr_find (&client->hwctx_idr, args->handle);if (!hwctx) {XDNA_DBG (xdna,"PID %d failed to get hwctx %d" , client->pid, args->handle); ret = -EINVAL ; - goto unlock; + goto unlock_srcu; } ret = xdna->dev_info->ops->hwctx_config (hwctx, args->param_type, val, buf, buf_size); -unlock: +unlock_srcu: +srcu_read_unlock (&client->hwctx_srcu, idx);mutex_unlock (&xdna->dev_lock);kfree (buf);return ret; } + +static void +amdxdna_arg_bos_put (struct amdxdna_sched_job *job) +{ +int i; + +for (i =0 ; i < job->bo_cnt; i++) { +if (!job->bos[i]) +break ; +drm_gem_object_put (job->bos[i]); + } +} + +static int +amdxdna_arg_bos_lookup (struct amdxdna_client *client, +struct amdxdna_sched_job *job, +u32 *bo_hdls,u32 bo_cnt) +{ +struct drm_gem_object *gobj; +int i, ret; + + job->bo_cnt = bo_cnt; +for (i =0 ; i < job->bo_cnt; i++) { +struct amdxdna_gem_obj *abo; + + gobj =drm_gem_object_lookup (client->filp, bo_hdls[i]); +if (!gobj) { + ret = -ENOENT ; + goto put_shmem_bo; + } + abo =to_xdna_obj (gobj); + +mutex_lock (&abo->lock); +if (abo->pinned) { +mutex_unlock (&abo->lock); + job->bos[i] = gobj; +continue ; + } + + ret =amdxdna_gem_pin_nolock (abo); +if (ret) { +mutex_unlock (&abo->lock); +drm_gem_object_put (gobj); + goto put_shmem_bo; + } + abo->pinned = true; +mutex_unlock (&abo->lock); + + job->bos[i] = gobj; + } + +return 0 ; + +put_shmem_bo: +amdxdna_arg_bos_put (job); +return ret; +} + +void amdxdna_sched_job_cleanup (struct amdxdna_sched_job *job) +{ +trace_amdxdna_debug_point (job->hwctx->name, job->seq,"job release" ); +amdxdna_arg_bos_put (job); +amdxdna_gem_put_obj (job->cmd_bo); +} + +int amdxdna_cmd_submit (struct amdxdna_client *client, +u32 cmd_bo_hdl,u32 *arg_bo_hdls,u32 arg_bo_cnt, +u32 hwctx_hdl,u64 *seq) +{ +struct amdxdna_dev *xdna = client->xdna; +struct amdxdna_sched_job *job; +struct amdxdna_hwctx *hwctx; +int ret, idx; + +XDNA_DBG (xdna,"Command BO hdl %d, Arg BO count %d" , cmd_bo_hdl, arg_bo_cnt); + job =kzalloc (struct_size (job, bos, arg_bo_cnt),GFP_KERNEL ); +if (!job) +return -ENOMEM ; + +if (cmd_bo_hdl !=AMDXDNA_INVALID_BO_HANDLE ) { + job->cmd_bo =amdxdna_gem_get_obj (client, cmd_bo_hdl,AMDXDNA_BO_CMD ); +if (!job->cmd_bo) { +XDNA_ERR (xdna,"Failed to get cmd bo from %d" , cmd_bo_hdl); + ret = -EINVAL ; + goto free_job; + } + }else { + job->cmd_bo =NULL ; + } + + ret =amdxdna_arg_bos_lookup (client, job, arg_bo_hdls, arg_bo_cnt); +if (ret) { +XDNA_ERR (xdna,"Argument BOs lookup failed, ret %d" , ret); + goto cmd_put; + } + + idx =srcu_read_lock (&client->hwctx_srcu); + hwctx =idr_find (&client->hwctx_idr, hwctx_hdl); +if (!hwctx) { +XDNA_DBG (xdna,"PID %d failed to get hwctx %d" , + client->pid, hwctx_hdl); + ret = -EINVAL ; + goto unlock_srcu; + } + +if (hwctx->status !=HWCTX_STAT_READY ) { +XDNA_ERR (xdna,"HW Context is not ready" ); + ret = -EINVAL ; + goto unlock_srcu; + } + + job->hwctx = hwctx; + job->mm = current->mm; + + job->fence =amdxdna_fence_create (hwctx); +if (!job->fence) { +XDNA_ERR (xdna,"Failed to create fence" ); + ret = -ENOMEM ; + goto unlock_srcu; + } +kref_init (&job->refcnt); + + ret = xdna->dev_info->ops->cmd_submit (hwctx, job, seq); +if (ret) + goto put_fence; + +/* +* The amdxdna_hwctx_destroy_rcu() will release hwctx and associated +* resource after synchronize_srcu(). The submitted jobs should be +* handled by the queue, for example DRM scheduler, in device layer. +* For here we can unlock SRCU. +*/ +srcu_read_unlock (&client->hwctx_srcu, idx); +trace_amdxdna_debug_point (hwctx->name, *seq,"job pushed" ); + +return 0 ; + +put_fence: +dma_fence_put (job->fence); +unlock_srcu: +srcu_read_unlock (&client->hwctx_srcu, idx); +amdxdna_arg_bos_put (job); +cmd_put: +amdxdna_gem_put_obj (job->cmd_bo); +free_job: +kfree (job); +return ret; +} + +/* +* The submit command ioctl submits a command to firmware. One firmware command +* may contain multiple command BOs for processing as a whole. +* The command sequence number is returned which can be used for wait command ioctl. +*/ +static int amdxdna_drm_submit_execbuf (struct amdxdna_client *client, +struct amdxdna_drm_exec_cmd *args) +{ +struct amdxdna_dev *xdna = client->xdna; +u32 *arg_bo_hdls; +u32 cmd_bo_hdl; +int ret; + +if (!args->arg_count || args->arg_count >MAX_ARG_COUNT ) { +XDNA_ERR (xdna,"Invalid arg bo count %d" , args->arg_count); +return -EINVAL ; + } + +/* Only support single command for now. */ +if (args->cmd_count !=1 ) { +XDNA_ERR (xdna,"Invalid cmd bo count %d" , args->cmd_count); +return -EINVAL ; + } + + cmd_bo_hdl = (u32 )args->cmd_handles; + arg_bo_hdls =kcalloc (args->arg_count,sizeof (u32),GFP_KERNEL ); +if (!arg_bo_hdls) +return -ENOMEM ; + ret =copy_from_user (arg_bo_hdls,u64_to_user_ptr (args->args), + args->arg_count *sizeof (u32)); +if (ret) { + ret = -EFAULT ; + goto free_cmd_bo_hdls; + } + + ret =amdxdna_cmd_submit (client, cmd_bo_hdl, arg_bo_hdls, + args->arg_count, args->hwctx, &args->seq); +if (ret) +XDNA_DBG (xdna,"Submit cmds failed, ret %d" , ret); + +free_cmd_bo_hdls: +kfree (arg_bo_hdls); +if (!ret) +XDNA_DBG (xdna,"Pushed cmd %lld to scheduler" , args->seq); +return ret; +} + +int amdxdna_drm_submit_cmd_ioctl (struct drm_device *dev,void *data,struct drm_file *filp) +{ +struct amdxdna_client *client = filp->driver_priv; +struct amdxdna_drm_exec_cmd *args = data; + +if (args->ext || args->ext_flags) +return -EINVAL ; + +switch (args->type) { +case AMDXDNA_CMD_SUBMIT_EXEC_BUF : +return amdxdna_drm_submit_execbuf (client, args); + } + +XDNA_ERR (client->xdna,"Invalid command type %d" , args->type); +return -EINVAL ; +}drivers/accel/amdxdna/amdxdna_ctx.h +111
-0@@ -6,6 +6,54 @@#ifndef _AMDXDNA_CTX_H_#define _AMDXDNA_CTX_H_ +#include <linux/bitfield.h> + +#include "amdxdna_gem.h" + +struct amdxdna_hwctx_priv ; + +enum ert_cmd_opcode { +ERT_START_CU =0 , +ERT_CMD_CHAIN =19 , +ERT_START_NPU =20 , +}; + +enum ert_cmd_state { +ERT_CMD_STATE_INVALID , +ERT_CMD_STATE_NEW , +ERT_CMD_STATE_QUEUED , +ERT_CMD_STATE_RUNNING , +ERT_CMD_STATE_COMPLETED , +ERT_CMD_STATE_ERROR , +ERT_CMD_STATE_ABORT , +ERT_CMD_STATE_SUBMITTED , +ERT_CMD_STATE_TIMEOUT , +ERT_CMD_STATE_NORESPONSE , +}; + +/* +* Interpretation of the beginning of data payload for ERT_START_NPU in +* amdxdna_cmd. The rest of the payload in amdxdna_cmd is regular kernel args. +*/ +struct amdxdna_cmd_start_npu { +u64 buffer;/* instruction buffer address */ +u32 buffer_size;/* size of buffer in bytes */ +u32 prop_count;/* properties count */ +u32 prop_args[];/* properties and regular kernel arguments */ +}; + +/* +* Interpretation of the beginning of data payload for ERT_CMD_CHAIN in +* amdxdna_cmd. The rest of the payload in amdxdna_cmd is cmd BO handles. +*/ +struct amdxdna_cmd_chain { +u32 command_count; +u32 submit_index; +u32 error_index; +u32 reserved[3 ]; + u64 data[] __counted_by(command_count); +}; +/* Exec buffer command header format */ #define AMDXDNA_CMD_STATE GENMASK(3, 0)#define AMDXDNA_CMD_EXTRA_CU_MASK GENMASK(11, 10)@@ -41,9 +89,72 @@ struct amdxdna_hwctx {u32 syncobj_hdl; }; +#define drm_job_to_xdna_job (j) \ + container_of(j, struct amdxdna_sched_job, base) + +struct amdxdna_sched_job { +struct drm_sched_job base; +struct kref refcnt; +struct amdxdna_hwctx *hwctx; +struct mm_struct *mm; +/* The fence to notice DRM scheduler that job is done by hardware */ +struct dma_fence *fence; +/* user can wait on this fence */ +struct dma_fence *out_fence; +bool job_done; +u64 seq; +struct amdxdna_gem_obj *cmd_bo; +size_t bo_cnt; + struct drm_gem_object *bos[] __counted_by(bo_cnt); +}; + +static inline u32 +amdxdna_cmd_get_op (struct amdxdna_gem_obj *abo) +{ +struct amdxdna_cmd *cmd = abo->mem.kva; + +return FIELD_GET (AMDXDNA_CMD_OPCODE , cmd->header); +} + +static inline void +amdxdna_cmd_set_state (struct amdxdna_gem_obj *abo,enum ert_cmd_state s) +{ +struct amdxdna_cmd *cmd = abo->mem.kva; + + cmd->header &= ~AMDXDNA_CMD_STATE ; + cmd->header |=FIELD_PREP (AMDXDNA_CMD_STATE , s); +} + +static inline enum ert_cmd_state +amdxdna_cmd_get_state (struct amdxdna_gem_obj *abo) +{ +struct amdxdna_cmd *cmd = abo->mem.kva; + +return FIELD_GET (AMDXDNA_CMD_STATE , cmd->header); +} + +void *amdxdna_cmd_get_payload (struct amdxdna_gem_obj *abo,u32 *size); +int amdxdna_cmd_get_cu_idx (struct amdxdna_gem_obj *abo); + +static inline u32 amdxdna_hwctx_col_map (struct amdxdna_hwctx *hwctx) +{ +return GENMASK (hwctx->start_col + hwctx->num_col -1 , + hwctx->start_col); +} + +void amdxdna_sched_job_cleanup (struct amdxdna_sched_job *job);void amdxdna_hwctx_remove_all (struct amdxdna_client *client); + +int amdxdna_cmd_submit (struct amdxdna_client *client, +u32 cmd_bo_hdls,u32 *arg_bo_hdls,u32 arg_bo_cnt, +u32 hwctx_hdl,u64 *seq); + +int amdxdna_cmd_wait (struct amdxdna_client *client,u32 hwctx_hdl, +u64 seq,u32 timeout); +int amdxdna_drm_create_hwctx_ioctl (struct drm_device *dev,void *data,struct drm_file *filp);int amdxdna_drm_config_hwctx_ioctl (struct drm_device *dev,void *data,struct drm_file *filp);int amdxdna_drm_destroy_hwctx_ioctl (struct drm_device *dev,void *data,struct drm_file *filp); +int amdxdna_drm_submit_cmd_ioctl (struct drm_device *dev,void *data,struct drm_file *filp);#endif /* _AMDXDNA_CTX_H_ */ drivers/accel/amdxdna/amdxdna_gem.c +1
-0@@ -8,6 +8,7 @@#include <drm/drm_device.h> #include <drm/drm_gem.h> #include <drm/drm_gem_shmem_helper.h> +#include <drm/gpu_scheduler.h> #include <linux/iosys-map.h> #include <linux/vmalloc.h> drivers/accel/amdxdna/amdxdna_mailbox_helper.c +5
-0@@ -3,10 +3,15 @@* Copyright (C) 2024, Advanced Micro Devices, Inc. */ +#include <drm/amdxdna_accel.h> #include <drm/drm_device.h> #include <drm/drm_print.h> +#include <drm/drm_gem.h> +#include <drm/drm_gem_shmem_helper.h> +#include <drm/gpu_scheduler.h> #include <linux/completion.h> +#include "amdxdna_gem.h" #include "amdxdna_mailbox.h" #include "amdxdna_mailbox_helper.h" #include "amdxdna_pci_drv.h" drivers/accel/amdxdna/amdxdna_pci_drv.c +12
-0@@ -10,6 +10,7 @@#include <drm/drm_gem_shmem_helper.h> #include <drm/drm_ioctl.h> #include <drm/drm_managed.h> +#include <drm/gpu_scheduler.h> #include <linux/iommu.h> #include <linux/pci.h> @@ -64,6 +65,7 @@ static int amdxdna_drm_open(struct drm_device *ddev, struct drm_file *filp) goto unbind_sva; }mutex_init (&client->hwctx_lock); +init_srcu_struct (&client->hwctx_srcu);idr_init_base (&client->hwctx_idr,AMDXDNA_INVALID_CTX_HANDLE +1 );mutex_init (&client->mm_lock);@@ -93,6 +95,7 @@ static void amdxdna_drm_close(struct drm_device *ddev, struct drm_file *filp)XDNA_DBG (xdna,"closing pid %d" , client->pid);idr_destroy (&client->hwctx_idr); +cleanup_srcu_struct (&client->hwctx_srcu);mutex_destroy (&client->hwctx_lock);mutex_destroy (&client->mm_lock);if (client->dev_heap)@@ -133,6 +136,8 @@ static const struct drm_ioctl_desc amdxdna_drm_ioctls[] = {DRM_IOCTL_DEF_DRV (AMDXDNA_CREATE_BO , amdxdna_drm_create_bo_ioctl,0 ),DRM_IOCTL_DEF_DRV (AMDXDNA_GET_BO_INFO , amdxdna_drm_get_bo_info_ioctl,0 ),DRM_IOCTL_DEF_DRV (AMDXDNA_SYNC_BO , amdxdna_drm_sync_bo_ioctl,0 ), +/* Execution */ + DRM_IOCTL_DEF_DRV(AMDXDNA_EXEC_CMD, amdxdna_drm_submit_cmd_ioctl, 0), }; static const struct file_operations amdxdna_fops = {@@ -190,9 +195,16 @@ static int amdxdna_probe(struct pci_dev *pdev, const struct pci_device_id *id)return -ENODEV ;drmm_mutex_init (&xdna->ddev, &xdna->dev_lock); +rwlock_init (&xdna->notifier_lock);INIT_LIST_HEAD (&xdna->client_list);pci_set_drvdata (pdev, xdna); +if (IS_ENABLED (CONFIG_LOCKDEP )) { +fs_reclaim_acquire (GFP_KERNEL ); +might_lock (&xdna->notifier_lock); +fs_reclaim_release (GFP_KERNEL ); + } +mutex_lock (&xdna->dev_lock); ret = xdna->dev_info->ops->init (xdna);mutex_unlock (&xdna->dev_lock);drivers/accel/amdxdna/amdxdna_pci_drv.h +5
-0@@ -20,6 +20,7 @@ extern const struct drm_driver amdxdna_drm_drv;struct amdxdna_dev ;struct amdxdna_gem_obj ;struct amdxdna_hwctx ; +struct amdxdna_sched_job ;/* * struct amdxdna_dev_ops - Device hardware operation callbacks @@ -31,6 +32,7 @@ struct amdxdna_dev_ops {void (*hwctx_fini)(struct amdxdna_hwctx *hwctx);int (*hwctx_config)(struct amdxdna_hwctx *hwctx,u32 type,u64 value,void *buf,u32 size);void (*hmm_invalidate)(struct amdxdna_gem_obj *abo,unsigned long cur_seq); +int (*cmd_submit)(struct amdxdna_hwctx *hwctx,struct amdxdna_sched_job *job,u64 *seq); };/* @@ -69,6 +71,7 @@ struct amdxdna_dev {struct mutex dev_lock;/* per device lock */ struct list_head client_list;struct amdxdna_fw_ver fw_ver; +rwlock_t notifier_lock;/* for mmu notifier*/ };/* @@ -88,6 +91,8 @@ struct amdxdna_client {struct list_head node;pid_t pid;struct mutex hwctx_lock;/* protect hwctx */ +/* do NOT wait this srcu when hwctx_lock is hold */ +struct srcu_struct hwctx_srcu;struct idr hwctx_idr;struct amdxdna_dev *xdna;struct drm_file *filp;drivers/accel/amdxdna/amdxdna_sysfs.c +5
-0@@ -3,9 +3,14 @@* Copyright (C) 2023-2024, Advanced Micro Devices, Inc. */ +#include <drm/amdxdna_accel.h> #include <drm/drm_device.h> +#include <drm/drm_gem_shmem_helper.h> #include <drm/drm_print.h> +#include <drm/gpu_scheduler.h> +#include <linux/types.h> +#include "amdxdna_gem.h" #include "amdxdna_pci_drv.h" static ssize_t vbnv_show (struct device *dev,struct device_attribute *attr,char *buf)drivers/accel/amdxdna/npu1_regs.c +1
-0@@ -5,6 +5,7 @@#include <drm/amdxdna_accel.h> #include <drm/drm_device.h> +#include <drm/gpu_scheduler.h> #include <linux/sizes.h> #include "aie2_pci.h" drivers/accel/amdxdna/npu2_regs.c +1
-0@@ -5,6 +5,7 @@#include <drm/amdxdna_accel.h> #include <drm/drm_device.h> +#include <drm/gpu_scheduler.h> #include <linux/sizes.h> #include "aie2_pci.h" drivers/accel/amdxdna/npu4_regs.c +1
-0@@ -5,6 +5,7 @@#include <drm/amdxdna_accel.h> #include <drm/drm_device.h> +#include <drm/gpu_scheduler.h> #include <linux/sizes.h> #include "aie2_pci.h" drivers/accel/amdxdna/npu5_regs.c +1
-0@@ -5,6 +5,7 @@#include <drm/amdxdna_accel.h> #include <drm/drm_device.h> +#include <drm/gpu_scheduler.h> #include <linux/sizes.h> #include "aie2_pci.h" include/trace/events/amdxdna.h +41
-0@@ -9,8 +9,49 @@#if !defined(_TRACE_AMDXDNA_H) || defined(TRACE_HEADER_MULTI_READ )#define _TRACE_AMDXDNA_H +#include <drm/gpu_scheduler.h> #include <linux/tracepoint.h> +TRACE_EVENT (amdxdna_debug_point, + TP_PROTO(const char *name, u64 number, const char *str), + +TP_ARGS (name, number, str), + + TP_STRUCT__entry(__string(name, name) + __field(u64, number) +__string (str, str)), + + TP_fast_assign(__assign_str(name); + __entry->number = number; + __assign_str(str);), + +TP_printk ("%s:%llu %s" ,__get_str (name), __entry->number, +__get_str (str)) +); + +TRACE_EVENT (xdna_job, + TP_PROTO(struct drm_sched_job *sched_job, const char *name, const char *str, u64 seq), + +TP_ARGS (sched_job, name, str, seq), + + TP_STRUCT__entry(__string(name, name) + __string(str, str) + __field(u64, fence_context) + __field(u64, fence_seqno) +__field (u64, seq)), + + TP_fast_assign(__assign_str(name); + __assign_str(str); + __entry->fence_context = sched_job->s_fence->finished.context; + __entry->fence_seqno = sched_job->s_fence->finished.seqno; + __entry->seq = seq;), + +TP_printk ("fence=(context:%llu, seqno:%lld), %s seq#:%lld %s" , + __entry->fence_context, __entry->fence_seqno, +__get_str (name), __entry->seq, +__get_str (str)) +); + DECLARE_EVENT_CLASS(xdna_mbox_msg, TP_PROTO(char *name, u8 chann_id, u32 opcode, u32 msg_id),include/uapi/drm/amdxdna_accel.h +38
-0@@ -13,9 +13,11 @@extern "C" {#endif +#define AMDXDNA_INVALID_CMD_HANDLE (~0UL)#define AMDXDNA_INVALID_ADDR (~0UL)#define AMDXDNA_INVALID_CTX_HANDLE 0#define AMDXDNA_INVALID_BO_HANDLE 0 +#define AMDXDNA_INVALID_FENCE_HANDLE 0enum amdxdna_device_type {AMDXDNA_DEV_TYPE_UNKNOWN =-1 ,@@ -29,6 +31,7 @@ enum amdxdna_drm_ioctl_id {DRM_AMDXDNA_CREATE_BO ,DRM_AMDXDNA_GET_BO_INFO ,DRM_AMDXDNA_SYNC_BO , + DRM_AMDXDNA_EXEC_CMD, };/** @@ -201,6 +204,37 @@ struct amdxdna_drm_sync_bo {__u64 size; }; +enum amdxdna_cmd_type { +AMDXDNA_CMD_SUBMIT_EXEC_BUF =0 , +AMDXDNA_CMD_SUBMIT_DEPENDENCY , +AMDXDNA_CMD_SUBMIT_SIGNAL , +}; + +/** +* struct amdxdna_drm_exec_cmd - Execute command. +* @ext: MBZ. +* @ext_flags: MBZ. +* @hwctx: Hardware context handle. +* @type: One of command type in enum amdxdna_cmd_type. +* @cmd_handles: Array of command handles or the command handle itself +* in case of just one. +* @args: Array of arguments for all command handles. +* @cmd_count: Number of command handles in the cmd_handles array. +* @arg_count: Number of arguments in the args array. +* @seq: Returned sequence number for this command. +*/ +struct amdxdna_drm_exec_cmd { +__u64 ext; +__u64 ext_flags; +__u32 hwctx; +__u32 type; +__u64 cmd_handles; +__u64 args; +__u32 cmd_count; +__u32 arg_count; +__u64 seq; +}; +#define DRM_IOCTL_AMDXDNA_CREATE_HWCTX \ DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDXDNA_CREATE_HWCTX, \ struct amdxdna_drm_create_hwctx)@@ -225,6 +259,10 @@ struct amdxdna_drm_sync_bo {DRM_IOWR (DRM_COMMAND_BASE +DRM_AMDXDNA_SYNC_BO , \ struct amdxdna_drm_sync_bo) +#define DRM_IOCTL_AMDXDNA_EXEC_CMD \ + DRM_IOWR(DRM_COMMAND_BASE + DRM_AMDXDNA_EXEC_CMD, \ + struct amdxdna_drm_exec_cmd) +#if defined(__cplusplus) } /* extern c end */#endif Signature · 1 line
2.34.1
Lizhi Hou
maintainerPATCH v7 8/10 accel/amdxdna: Add suspend and resume
In reply to Lizhi HouImplement PCI power management suspend and resume callbacks.
--- drivers/accel/amdxdna/aie2_ctx.c | 30 ++++++ drivers/accel/amdxdna/aie2_pci.c | 4 + drivers/accel/amdxdna/aie2_pci.h | 2 + drivers/accel/amdxdna/amdxdna_ctx.c | 26 +++++ drivers/accel/amdxdna/amdxdna_ctx.h | 2 + drivers/accel/amdxdna/amdxdna_pci_drv.c | 120 +++++++++++++++++++++++- drivers/accel/amdxdna/amdxdna_pci_drv.h | 4 + 7 files changed, 185 insertions(+), 3 deletions(-)
drivers/accel/amdxdna/aie2_ctx.c +30 -0
@@ -156,6 +156,36 @@ static void aie2_hwctx_wait_for_idle(struct amdxdna_hwctx *hwctx)dma_fence_put (fence); } +void aie2_hwctx_suspend (struct amdxdna_hwctx *hwctx) +{ +struct amdxdna_dev *xdna = hwctx->client->xdna; + +/* +* Command timeout is unlikely. But if it happens, it doesn't +* break the system. aie2_hwctx_stop() will destroy mailbox +* and abort all commands. +*/ +drm_WARN_ON (&xdna->ddev, !mutex_is_locked (&xdna->dev_lock)); +aie2_hwctx_wait_for_idle (hwctx); +aie2_hwctx_stop (xdna, hwctx,NULL ); + hwctx->old_status = hwctx->status; + hwctx->status =HWCTX_STAT_STOP ; +} + +void aie2_hwctx_resume (struct amdxdna_hwctx *hwctx) +{ +struct amdxdna_dev *xdna = hwctx->client->xdna; + +/* +* The resume path cannot guarantee that mailbox channel can be +* regenerated. If this happen, when submit message to this +* mailbox channel, error will return. +*/ +drm_WARN_ON (&xdna->ddev, !mutex_is_locked (&xdna->dev_lock)); + hwctx->status = hwctx->old_status; +aie2_hwctx_restart (xdna, hwctx); +} +static void aie2_sched_notify (struct amdxdna_sched_job *job) {
drivers/accel/amdxdna/aie2_pci.c +4 -0
@@ -495,9 +495,13 @@ static void aie2_fini(struct amdxdna_dev *xdna)const struct amdxdna_dev_ops aie2_ops = { .init = aie2_init, .fini = aie2_fini, + .resume = aie2_hw_start, + .suspend = aie2_hw_stop, .hwctx_init = aie2_hwctx_init, .hwctx_fini = aie2_hwctx_fini, .hwctx_config = aie2_hwctx_config, .cmd_submit = aie2_cmd_submit, .hmm_invalidate = aie2_hmm_invalidate, + .hwctx_suspend = aie2_hwctx_suspend, + .hwctx_resume = aie2_hwctx_resume, };
drivers/accel/amdxdna/aie2_pci.h +2 -0
@@ -240,6 +240,8 @@ int aie2_sync_bo(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job,int aie2_hwctx_init (struct amdxdna_hwctx *hwctx);void aie2_hwctx_fini (struct amdxdna_hwctx *hwctx);int aie2_hwctx_config (struct amdxdna_hwctx *hwctx,u32 type,u64 value,void *buf,u32 size); +void aie2_hwctx_suspend (struct amdxdna_hwctx *hwctx); +void aie2_hwctx_resume (struct amdxdna_hwctx *hwctx);int aie2_cmd_submit (struct amdxdna_hwctx *hwctx,struct amdxdna_sched_job *job,u64 *seq);void aie2_hmm_invalidate (struct amdxdna_gem_obj *abo,unsigned long cur_seq);void aie2_stop_ctx_by_col_map (struct amdxdna_client *client,u32 col_map);
drivers/accel/amdxdna/amdxdna_ctx.c +26 -0
@@ -59,6 +59,32 @@ static struct dma_fence *amdxdna_fence_create(struct amdxdna_hwctx *hwctx)return &fence->base; } +void amdxdna_hwctx_suspend (struct amdxdna_client *client) +{ +struct amdxdna_dev *xdna = client->xdna; +struct amdxdna_hwctx *hwctx; +int next =0 ; + +drm_WARN_ON (&xdna->ddev, !mutex_is_locked (&xdna->dev_lock)); +mutex_lock (&client->hwctx_lock); +idr_for_each_entry_continue (&client->hwctx_idr, hwctx, next) + xdna->dev_info->ops->hwctx_suspend (hwctx); +mutex_unlock (&client->hwctx_lock); +} + +void amdxdna_hwctx_resume (struct amdxdna_client *client) +{ +struct amdxdna_dev *xdna = client->xdna; +struct amdxdna_hwctx *hwctx; +int next =0 ; + +drm_WARN_ON (&xdna->ddev, !mutex_is_locked (&xdna->dev_lock)); +mutex_lock (&client->hwctx_lock); +idr_for_each_entry_continue (&client->hwctx_idr, hwctx, next) + xdna->dev_info->ops->hwctx_resume (hwctx); +mutex_unlock (&client->hwctx_lock); +} +static void amdxdna_hwctx_destroy_rcu (struct amdxdna_hwctx *hwctx,struct srcu_struct *ss) {
drivers/accel/amdxdna/amdxdna_ctx.h +2 -0
@@ -144,6 +144,8 @@ static inline u32 amdxdna_hwctx_col_map(struct amdxdna_hwctx *hwctx)void amdxdna_sched_job_cleanup (struct amdxdna_sched_job *job);void amdxdna_hwctx_remove_all (struct amdxdna_client *client); +void amdxdna_hwctx_suspend (struct amdxdna_client *client); +void amdxdna_hwctx_resume (struct amdxdna_client *client); int amdxdna_cmd_submit(struct amdxdna_client *client, u32 cmd_bo_hdls, u32 *arg_bo_hdls, u32 arg_bo_cnt,
drivers/accel/amdxdna/amdxdna_pci_drv.c +117 -3
190 lines not on this page · raw message
drivers/accel/amdxdna/amdxdna_pci_drv.h +4 -0
@@ -28,10 +28,14 @@ struct amdxdna_sched_job;struct amdxdna_dev_ops {int (*init)(struct amdxdna_dev *xdna);void (*fini)(struct amdxdna_dev *xdna); +int (*resume)(struct amdxdna_dev *xdna); +void (*suspend)(struct amdxdna_dev *xdna);int (*hwctx_init)(struct amdxdna_hwctx *hwctx);void (*hwctx_fini)(struct amdxdna_hwctx *hwctx);int (*hwctx_config)(struct amdxdna_hwctx *hwctx,u32 type,u64 value,void *buf,u32 size);void (*hmm_invalidate)(struct amdxdna_gem_obj *abo,unsigned long cur_seq); +void (*hwctx_suspend)(struct amdxdna_hwctx *hwctx); +void (*hwctx_resume)(struct amdxdna_hwctx *hwctx);int (*cmd_submit)(struct amdxdna_hwctx *hwctx,struct amdxdna_sched_job *job,u64 *seq); };
Signature · 1 line
2.34.1
Lizhi Hou
maintainerPATCH v7 9/10 accel/amdxdna: Add error handling
In reply to Lizhi HouWhen there is a hardware error, the NPU firmware notifies the host through a mailbox message. The message includes details of the error, such as the tile and column indexes where the error occurred.
The driver starts a thread to handle the NPU error message. The thread stops the clients which are using the column where error occurred. Then the driver resets that column.
--- drivers/accel/amdxdna/Makefile | 1 + drivers/accel/amdxdna/aie2_error.c | 360 +++++++++++++++++++++++++++ drivers/accel/amdxdna/aie2_message.c | 19 ++ drivers/accel/amdxdna/aie2_pci.c | 32 +++ drivers/accel/amdxdna/aie2_pci.h | 9 + 5 files changed, 421 insertions(+) create mode 100644 drivers/accel/amdxdna/aie2_error.c
drivers/accel/amdxdna/Makefile +1 -0
@@ -2,6 +2,7 @@ amdxdna-y := \ aie2_ctx.o \ + aie2_error.o \ aie2_message.o \ aie2_pci.o \ aie2_psp.o \
drivers/accel/amdxdna/aie2_error.c new file +360 -0
360 lines not on this page · raw message
drivers/accel/amdxdna/aie2_message.c +19 -0
@@ -308,6 +308,25 @@ int aie2_map_host_buf(struct amdxdna_dev_hdl *ndev, u32 context_id, u64 addr, u6return 0 ; } +int aie2_register_asyn_event_msg (struct amdxdna_dev_hdl *ndev,dma_addr_t addr,u32 size, +void *handle,int (*cb)(void *,const u32 *,size_t )) +{ +struct async_event_msg_req req = {0 }; +struct xdna_mailbox_msg msg = { + .send_data = (u8 *)&req, + .send_size =sizeof (req), + .handle = handle, + .opcode =MSG_OP_REGISTER_ASYNC_EVENT_MSG , + .notify_cb = cb, + }; + + req.buf_addr = addr; + req.buf_size = size; + +XDNA_DBG (ndev->xdna,"Register addr 0x%llx size 0x%x" , addr, size); +return xdna_mailbox_send_msg (ndev->mgmt_chann, &msg,TX_TIMEOUT ); +} +int aie2_config_cu (struct amdxdna_hwctx *hwctx) {struct mailbox_channel *chann = hwctx->priv->mbox_chann;
drivers/accel/amdxdna/aie2_pci.c +32 -0
@@ -180,6 +180,15 @@ static int aie2_mgmt_fw_init(struct amdxdna_dev_hdl *ndev)return ret; } +if (!ndev->async_events) +return 0 ; + + ret =aie2_error_async_events_send (ndev); +if (ret) { +XDNA_ERR (ndev->xdna,"Send async events failed" ); +return ret; + } +return 0 ; }
@@ -468,9 +477,30 @@ static int aie2_init(struct amdxdna_dev *xdna) goto stop_hw; } + ret =aie2_error_async_events_alloc (ndev); +if (ret) { +XDNA_ERR (xdna,"Allocate async events failed, ret %d" , ret); + goto stop_hw; + } + + ret =aie2_error_async_events_send (ndev); +if (ret) { +XDNA_ERR (xdna,"Send async events failed, ret %d" , ret); + goto async_event_free; + } + +/* Issue a command to make sure firmware handled async events */ + ret =aie2_query_firmware_version (ndev, &ndev->xdna->fw_ver); +if (ret) { +XDNA_ERR (xdna,"Re-query firmware version failed" ); + goto async_event_free; + } +release_firmware (fw);return 0 ; +async_event_free: +aie2_error_async_events_free (ndev); stop_hw:aie2_hw_stop (xdna); disable_sva:
@@ -486,8 +516,10 @@ static int aie2_init(struct amdxdna_dev *xdna)static void aie2_fini (struct amdxdna_dev *xdna) {struct pci_dev *pdev =to_pci_dev (xdna->ddev.dev); +struct amdxdna_dev_hdl *ndev = xdna->dev_handle;aie2_hw_stop (xdna); +aie2_error_async_events_free (ndev);iommu_dev_disable_feature (&pdev->dev,IOMMU_DEV_FEAT_SVA );pci_free_irq_vectors (pdev); }
drivers/accel/amdxdna/aie2_pci.h +9 -0
@@ -170,6 +170,7 @@ struct amdxdna_dev_hdl {/* Mailbox and the management channel */ struct mailbox *mbox;struct mailbox_channel *mgmt_chann; +struct async_events *async_events; };#define DEFINE_BAR_OFFSET (reg_name, bar, reg_addr) \
@@ -210,6 +211,12 @@ struct psp_device *aie2m_psp_create(struct drm_device *ddev, struct psp_config *int aie2_psp_start (struct psp_device *psp);void aie2_psp_stop (struct psp_device *psp); +/* aie2_error.c */ +int aie2_error_async_events_alloc (struct amdxdna_dev_hdl *ndev); +void aie2_error_async_events_free (struct amdxdna_dev_hdl *ndev); +int aie2_error_async_events_send (struct amdxdna_dev_hdl *ndev); +int aie2_error_async_msg_thread (void *data); +/* aie2_message.c */ int aie2_suspend_fw (struct amdxdna_dev_hdl *ndev);int aie2_resume_fw (struct amdxdna_dev_hdl *ndev);
@@ -224,6 +231,8 @@ int aie2_query_firmware_version(struct amdxdna_dev_hdl *ndev,int aie2_create_context (struct amdxdna_dev_hdl *ndev,struct amdxdna_hwctx *hwctx);int aie2_destroy_context (struct amdxdna_dev_hdl *ndev,struct amdxdna_hwctx *hwctx);int aie2_map_host_buf (struct amdxdna_dev_hdl *ndev,u32 context_id,u64 addr,u64 size); +int aie2_register_asyn_event_msg (struct amdxdna_dev_hdl *ndev,dma_addr_t addr,u32 size, +void *handle,int (*cb)(void *,const u32 *,size_t ));int aie2_config_cu (struct amdxdna_hwctx *hwctx);int aie2_execbuf (struct amdxdna_hwctx *hwctx,struct amdxdna_sched_job *job,int (*notify_cb)(void *,const u32 *,size_t ));
Signature · 1 line
2.34.1
Lizhi Hou
maintainerPATCH v7 10/10 accel/amdxdna: Add query functions
In reply to Lizhi HouAdd GET_INFO ioctl to retrieve hardware information, including AIE, clock, hardware context etc.
--- drivers/accel/amdxdna/aie2_message.c | 65 +++++++ drivers/accel/amdxdna/aie2_pci.c | 222 ++++++++++++++++++++++++ drivers/accel/amdxdna/aie2_pci.h | 1 + drivers/accel/amdxdna/amdxdna_pci_drv.c | 19 ++ drivers/accel/amdxdna/amdxdna_pci_drv.h | 3 + include/uapi/drm/amdxdna_accel.h | 166 ++++++++++++++++++ 6 files changed, 476 insertions(+)
drivers/accel/amdxdna/aie2_message.c +65 -0
@@ -308,6 +308,71 @@ int aie2_map_host_buf(struct amdxdna_dev_hdl *ndev, u32 context_id, u64 addr, u6return 0 ; } +int aie2_query_status (struct amdxdna_dev_hdl *ndev,char __user *buf, +u32 size,u32 *cols_filled) +{ +DECLARE_AIE2_MSG (aie_column_info,MSG_OP_QUERY_COL_STATUS ); +struct amdxdna_dev *xdna = ndev->xdna; +struct amdxdna_client *client; +struct amdxdna_hwctx *hwctx; +dma_addr_t dma_addr; +u32 aie_bitmap =0 ; +u8 *buff_addr; +int next =0 ; +int ret, idx; + + buff_addr =dma_alloc_noncoherent (xdna->ddev.dev, size, &dma_addr, +DMA_FROM_DEVICE ,GFP_KERNEL ); +if (!buff_addr) +return -ENOMEM ; + +/* Go through each hardware context and mark the AIE columns that are active */ +list_for_each_entry (client, &xdna->client_list, node) { + idx =srcu_read_lock (&client->hwctx_srcu); +idr_for_each_entry_continue (&client->hwctx_idr, hwctx, next) + aie_bitmap |=amdxdna_hwctx_col_map (hwctx); +srcu_read_unlock (&client->hwctx_srcu, idx); + } + + *cols_filled =0 ; + req.dump_buff_addr = dma_addr; + req.dump_buff_size = size; + req.num_cols =hweight32 (aie_bitmap); + req.aie_bitmap = aie_bitmap; + +drm_clflush_virt_range (buff_addr, size);/* device can access */ + ret =aie2_send_mgmt_msg_wait (ndev, &msg); +if (ret) { +XDNA_ERR (xdna,"Error during NPU query, status %d" , ret); + goto fail; + } + +if (resp.status !=AIE2_STATUS_SUCCESS ) { +XDNA_ERR (xdna,"Query NPU status failed, status 0x%x" , resp.status); + ret = -EINVAL ; + goto fail; + } +XDNA_DBG (xdna,"Query NPU status completed" ); + +if (size < resp.size) { + ret = -EINVAL ; +XDNA_ERR (xdna,"Bad buffer size. Available: %u. Needs: %u" , size, resp.size); + goto fail; + } + +if (copy_to_user (buf, buff_addr, resp.size)) { + ret = -EFAULT ; +XDNA_ERR (xdna,"Failed to copy NPU status to user space" ); + goto fail; + } + + *cols_filled = aie_bitmap; + +fail: +dma_free_noncoherent (xdna->ddev.dev, size, buff_addr, dma_addr,DMA_FROM_DEVICE ); +return ret; +} +int aie2_register_asyn_event_msg (struct amdxdna_dev_hdl *ndev,dma_addr_t addr,u32 size,void *handle,int (*cb)(void *,const u32 *,size_t )) {
drivers/accel/amdxdna/aie2_pci.c +222 -0
239 lines not on this page · raw message
drivers/accel/amdxdna/aie2_pci.h +1 -0
@@ -231,6 +231,7 @@ int aie2_query_firmware_version(struct amdxdna_dev_hdl *ndev,int aie2_create_context (struct amdxdna_dev_hdl *ndev,struct amdxdna_hwctx *hwctx);int aie2_destroy_context (struct amdxdna_dev_hdl *ndev,struct amdxdna_hwctx *hwctx);int aie2_map_host_buf (struct amdxdna_dev_hdl *ndev,u32 context_id,u64 addr,u64 size); +int aie2_query_status (struct amdxdna_dev_hdl *ndev,char *buf,u32 size,u32 *cols_filled);int aie2_register_asyn_event_msg (struct amdxdna_dev_hdl *ndev,dma_addr_t addr,u32 size,void *handle,int (*cb)(void *,const u32 *,size_t ));int aie2_config_cu (struct amdxdna_hwctx *hwctx);
drivers/accel/amdxdna/amdxdna_pci_drv.c +19 -0
@@ -143,6 +143,23 @@ static int amdxdna_flush(struct file *f, fl_owner_t id)return 0 ; } +static int amdxdna_drm_get_info_ioctl (struct drm_device *dev,void *data,struct drm_file *filp) +{ +struct amdxdna_client *client = filp->driver_priv; +struct amdxdna_dev *xdna =to_xdna_dev (dev); +struct amdxdna_drm_get_info *args = data; +int ret; + +if (!xdna->dev_info->ops->get_aie_info) +return -EOPNOTSUPP ; + +XDNA_DBG (xdna,"Request parameter %u" , args->param); +mutex_lock (&xdna->dev_lock); + ret = xdna->dev_info->ops->get_aie_info (client, args); +mutex_unlock (&xdna->dev_lock); +return ret; +} +static const struct drm_ioctl_desc amdxdna_drm_ioctls[] = {/* Context */ DRM_IOCTL_DEF_DRV (AMDXDNA_CREATE_HWCTX , amdxdna_drm_create_hwctx_ioctl,0 ),
@@ -154,6 +171,8 @@ static const struct drm_ioctl_desc amdxdna_drm_ioctls[] = {DRM_IOCTL_DEF_DRV (AMDXDNA_SYNC_BO , amdxdna_drm_sync_bo_ioctl,0 ),/* Execution */ DRM_IOCTL_DEF_DRV (AMDXDNA_EXEC_CMD , amdxdna_drm_submit_cmd_ioctl,0 ), +/* AIE hardware */ + DRM_IOCTL_DEF_DRV(AMDXDNA_GET_INFO, amdxdna_drm_get_info_ioctl, 0), }; static const struct file_operations amdxdna_fops = {
drivers/accel/amdxdna/amdxdna_pci_drv.h +3 -0
@@ -17,7 +17,9 @@extern const struct drm_driver amdxdna_drm_drv; +struct amdxdna_client ;struct amdxdna_dev ; +struct amdxdna_drm_get_info ;struct amdxdna_gem_obj ;struct amdxdna_hwctx ;struct amdxdna_sched_job ;
@@ -37,6 +39,7 @@ struct amdxdna_dev_ops {void (*hwctx_suspend)(struct amdxdna_hwctx *hwctx);void (*hwctx_resume)(struct amdxdna_hwctx *hwctx);int (*cmd_submit)(struct amdxdna_hwctx *hwctx,struct amdxdna_sched_job *job,u64 *seq); +int (*get_aie_info)(struct amdxdna_client *client,struct amdxdna_drm_get_info *args); };/*
include/uapi/drm/amdxdna_accel.h +166 -0
184 lines not on this page · raw message
Signature · 1 line
2.34.1