Skip to contentSkip to conversation

AMD XDNA driver

PATCH Superseded Lizhi Hou 3 participants 36 messages Sep 11, 2024 – Oct 9, 2024

Subsystems (6)

v3 of 11 Superseded Latest is v11 →

lore
Share & tools
lore

Lizhi Hou

author maintainer
#0 lore raw

v3 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 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 (11):
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
accel/amdxdna: Add firmware debug buffer support

 Documentation/accel/amdxdna/amdnpu.rst        | 283 ++++++
 Documentation/accel/amdxdna/index.rst         |  11 +
 Documentation/accel/index.rst                 |   1 +
 MAINTAINERS                                   |  10 +
 drivers/accel/Kconfig                         |   1 +
 drivers/accel/Makefile                        |   1 +
 drivers/accel/amdxdna/Kconfig                 |  15 +
 drivers/accel/amdxdna/Makefile                |  21 +
 drivers/accel/amdxdna/TODO                    |   5 +
 drivers/accel/amdxdna/aie2_ctx.c              | 960 ++++++++++++++++++
 drivers/accel/amdxdna/aie2_error.c            | 357 +++++++
 drivers/accel/amdxdna/aie2_message.c          | 790 ++++++++++++++
 drivers/accel/amdxdna/aie2_msg_priv.h         | 370 +++++++
 drivers/accel/amdxdna/aie2_pci.c              | 766 ++++++++++++++
 drivers/accel/amdxdna/aie2_pci.h              | 251 +++++
 drivers/accel/amdxdna/aie2_psp.c              | 146 +++
 drivers/accel/amdxdna/aie2_smu.c              | 121 +++
 drivers/accel/amdxdna/aie2_solver.c           | 330 ++++++
 drivers/accel/amdxdna/aie2_solver.h           | 154 +++
 drivers/accel/amdxdna/amdxdna_ctx.c           | 608 +++++++++++
 drivers/accel/amdxdna/amdxdna_ctx.h           | 161 +++
 drivers/accel/amdxdna/amdxdna_gem.c           | 709 +++++++++++++
 drivers/accel/amdxdna/amdxdna_gem.h           |  69 ++
 drivers/accel/amdxdna/amdxdna_mailbox.c       | 575 +++++++++++
 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       | 402 ++++++++
 drivers/accel/amdxdna/amdxdna_pci_drv.h       | 122 +++
 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              | 461 +++++++++
 36 files changed, 8550 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
author maintainer
#1 lore raw

PATCH v3 1/11 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 | 283 +++++++++++++++++++++++++
 Documentation/accel/amdxdna/index.rst  |  11 +
 Documentation/accel/index.rst          |   1 +
 3 files changed, 295 insertions(+)
 create mode 100644 Documentation/accel/amdxdna/amdnpu.rst
 create mode 100644 Documentation/accel/amdxdna/index.rst
Documentation/accel/amdxdna/amdnpu.rst new file +283 -0
new file mode 100644
index 000000000000..2af3bc5b2a9e
@@ -0,0 +1,283 @@
+.. SPDX-License-Identifier: GPL-2.0-only
+
+.. include:: <isonum.txt>
+
+.. SPDX-License-Identifier: GPL-2.0-only
+
+=========
+ 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 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
new file mode 100644
index 000000000000..38c16939f1fc
@@ -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
index e94a0160b6a0..0a94b6766263 100644
@@ -9,6 +9,7 @@ Compute Accelerators
 
    introduction
    qaic/index
+   amdxdna/index
 
 .. only::  subproject and html
 
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH V3 01/11] accel/amdxdna: Add documentation for AMD NPU accelerator driver

Quoted 22 lines · Lizhi Hou

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 | 283 +++++++++++++++++++++++++
  Documentation/accel/amdxdna/index.rst  |  11 +
  Documentation/accel/index.rst          |   1 +
  3 files changed, 295 insertions(+)

create mode 100644 Documentation/accel/amdxdna/amdnpu.rst create mode 100644 Documentation/accel/amdxdna/index.rst

Documentation/accel/amdxdna/amdnpu.rst new file +0 -0
new file mode 100644
index 000000000000..2af3bc5b2a9e

Documentation/accel/amdxdna/amdnpu.rst line 5

Quoted 5 lines
Documentation/accel/amdxdna/amdnpu.rst new file +5 -0
@@ -0,0 +1,283 @@
+.. SPDX-License-Identifier: GPL-2.0-only
+
+.. include:: <isonum.txt>
+
+.. SPDX-License-Identifier: GPL-2.0-only

SPDX twice?

Quoted 31 lines
Documentation/accel/amdxdna/amdnpu.rst new file +31 -0
+
+=========
+ 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.

Documentation/accel/amdxdna/amdnpu.rst line 46

Quoted 10 lines
Documentation/accel/amdxdna/amdnpu.rst new file +10 -0
+
+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
+................

Why a line of "." instead of "-" likse elsewhere?

Quoted 12 lines
Documentation/accel/amdxdna/amdnpu.rst new file +12 -0
+
+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.

Documentation/accel/amdxdna/amdnpu.rst line 68

Quoted 10 lines
Documentation/accel/amdxdna/amdnpu.rst new file +10 -0
+
+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
+.........

Again, odd delimiter

Quoted 5 lines
Documentation/accel/amdxdna/amdnpu.rst new file +5 -0
+
+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.

Documentation/accel/amdxdna/amdnpu.rst line 83

Quoted 10 lines
Documentation/accel/amdxdna/amdnpu.rst new file +10 -0
+
+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 as a PCIe device with multiple BARs and some MSI-X

"to the x86" - feels like something is missing here. Maybe "x86 host CPU"?

Quoted 3 lines
Documentation/accel/amdxdna/amdnpu.rst new file +3 -0
+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.

<snip>

Quoted 22 lines
Documentation/accel/amdxdna/index.rst new file +11 -0
new file mode 100644
index 000000000000..38c16939f1fc
@@ -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 +0 -0
index e94a0160b6a0..0a94b6766263 100644

Documentation/accel/index.rst Compute Accelerators · line 12

Quoted 4 lines
Documentation/accel/index.rst +1 -0
@@ -9,6 +9,7 @@ Compute Accelerators
 
     introduction
     qaic/index
+   amdxdna/index

I think alphabetical order makes sense to me, considering there probably should be more entries added over time. This would suggest that your addition should occur one line up. What do you think?

Quoted 3 lines
Documentation/accel/index.rst +0 -0
  .. only::  subproject and html
 
author maintainer
#3 lore raw

Re: [PATCH V3 01/11] accel/amdxdna: Add documentation for AMD NPU accelerator driver

Quoted 17 lines · Jeffrey Hugo
Quoted 16 lines · Lizhi Hou

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 | 283 +++++++++++++++++++++++++ Documentation/accel/amdxdna/index.rst  |  11 + Documentation/accel/index.rst          |   1 +

  3 files changed, 295 insertions(+)

  create mode 100644 Documentation/accel/amdxdna/amdnpu.rst create mode 100644 Documentation/accel/amdxdna/index.rst

Quoted 13 lines

Documentation/accel/amdxdna/amdnpu.rst line 5

Quoted 11 lines
Documentation/accel/amdxdna/amdnpu.rst new file +5 -0
new file mode 100644
index 000000000000..2af3bc5b2a9e
@@ -0,0 +1,283 @@
+.. SPDX-License-Identifier: GPL-2.0-only
+
+.. include:: <isonum.txt>
+
+.. SPDX-License-Identifier: GPL-2.0-only

SPDX twice?

I will remove one.

Quoted 46 lines
Quoted 46 lines
Documentation/accel/amdxdna/amdnpu.rst new file +12 -0
+
+=========
+ 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

Documentation/accel/amdxdna/amdnpu.rst new file +13 -0
+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

Documentation/accel/amdxdna/amdnpu.rst new file +3 -0
+partition which can be bound to a workload context.
+
+Each column also has dedicated DMA engines to move data between host 

DDR and

Documentation/accel/amdxdna/amdnpu.rst new file +3 -0
+memory tile.
+
+AMD Phoenix and AMD Hawk Point client NPU have a 4x5 topology, i.e., 

4 rows of

Quoted 8 lines

Documentation/accel/amdxdna/amdnpu.rst line 46

Quoted 6 lines

+compute tiles arranged into 5 columns. AMD Strix Point client APU have 4x8

Documentation/accel/amdxdna/amdnpu.rst new file +4 -0
+topology, i.e., 4 rows of compute tiles arranged into 8 columns.
+
+Shared L2 Memory
+................

Why a line of "." instead of "-" likse elsewhere?

I will fix it.

Quoted 48 lines
Quoted 26 lines
Documentation/accel/amdxdna/amdnpu.rst new file +2 -0
+
+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.

Documentation/accel/amdxdna/amdnpu.rst new file +6 -0
+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,

Documentation/accel/amdxdna/amdnpu.rst new file +4 -0
+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

Documentation/accel/amdxdna/amdnpu.rst new file +3 -0
+provided ``ctrlcode`` associated with the workload context.
+
+NPU Firmware uses a single isolated privileged context called MERT 

to service

Documentation/accel/amdxdna/amdnpu.rst line 68

Quoted 4 lines
Documentation/accel/amdxdna/amdnpu.rst new file +4 -0
+management commands from the amdxdna driver.
+
+Mailboxes
+.........

Again, odd delimiter

Quoted 15 lines
Documentation/accel/amdxdna/amdnpu.rst new file +2 -0
+
+The microcontroller and amdxdna driver use a privileged channel for 

management
+tasks like setting up of contexts, telemetry, query, error handling, setting up

Documentation/accel/amdxdna/amdnpu.rst new file +5 -0
+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.

Quoted 8 lines
Quoted 6 lines
Documentation/accel/amdxdna/amdnpu.rst new file +5 -0
+
+PCIe EP
+-------
+
+NPU is visible to the x86 as a PCIe device with multiple BARs and 

some MSI-X

"to the x86" - feels like something is missing here.  Maybe "x86 host CPU"?

Yes. I will change to "to the x86 host CPU".

Quoted 28 lines
Quoted 6 lines

+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.

<snip>

Quoted 19 lines
Documentation/accel/amdxdna/index.rst new file +11 -0
new file mode 100644
index 000000000000..38c16939f1fc
@@ -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 +0 -0
index e94a0160b6a0..0a94b6766263 100644
Quoted 8 lines
Quoted 6 lines
Documentation/accel/index.rst +0 -0
@@ -9,6 +9,7 @@ Compute Accelerators

       introduction
     qaic/index
+   amdxdna/index

I think alphabetical order makes sense to me, considering there probably should be more entries added over time. This would suggest that your addition should occur one line up. What do you think?

I will fix it.

Thanks,

Lizhi

Quoted 3 lines
Quoted 2 lines

    .. only::  subproject and html

author maintainer
#4 lore raw

PATCH v3 2/11 accel/amdxdna: Add a new driver for AMD AI Engine

In reply to Lizhi Hou

AMD 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                             |   9 ++
 drivers/accel/Kconfig                   |   1 +
 drivers/accel/Makefile                  |   1 +
 drivers/accel/amdxdna/Kconfig           |  15 ++
 drivers/accel/amdxdna/Makefile          |  13 ++
 drivers/accel/amdxdna/TODO              |   5 +
 drivers/accel/amdxdna/aie2_pci.c        | 183 ++++++++++++++++++++++++
 drivers/accel/amdxdna/aie2_pci.h        | 130 +++++++++++++++++
 drivers/accel/amdxdna/aie2_psp.c        | 141 ++++++++++++++++++
 drivers/accel/amdxdna/aie2_smu.c        | 117 +++++++++++++++
 drivers/accel/amdxdna/amdxdna_pci_drv.c | 128 +++++++++++++++++
 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, 1341 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 +9 -0
index a70b7c9c3533..82d805bb5632 100644
@@ -1146,6 +1146,15 @@ 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:	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
index 64065fb8922b..5b9490367a39 100644
@@ -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
index ab3df932937f..a301fb6089d4 100644
@@ -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 +15 -0
new file mode 100644
index 000000000000..b6f4c364dd6b
@@ -0,0 +1,15 @@
+# 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 FW_LOADER
+	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
new file mode 100644
index 000000000000..1dee0cba8390
@@ -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
new file mode 100644
index 000000000000..009ee3f5dc5c
@@ -0,0 +1,5 @@
+- Replace idr with xa
+- Add import and export BO support
+- Add debugfs support
+- Improve debug BO support
+- Improve power management
drivers/accel/amdxdna/aie2_pci.c new file +183 -0

183 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 +141 -0

141 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 +128 -0

128 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
new file mode 100644
index 000000000000..6d97e8e90cf6
@@ -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
#5 lore raw

Re: [PATCH V3 02/11] accel/amdxdna: Add a new driver for AMD AI Engine

Quoted 11 lines · Lizhi Hou
drivers/accel/amdxdna/aie2_pci.c new file +5 -0
new file mode 100644
index 000000000000..e21b32557fc2
@@ -0,0 +1,183 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2023-2024, Advanced Micro Devices, Inc.
+ */
+

drivers/accel/amdxdna/aie2_pci.c line 15

Quoted 10 lines
drivers/accel/amdxdna/aie2_pci.c new file +10 -0
+#include <drm/drm_device.h>
+#include <drm/drm_managed.h>
+#include <drm/drm_print.h>
+#include <linux/errno.h>
+#include <linux/firmware.h>
+#include <linux/iommu.h>
+#include <linux/pci.h>
+
+#include "amdxdna_pci_drv.h"
+#include "aie2_pci.h"

Feels like a blank line is missing here.

Quoted 19 lines
drivers/accel/amdxdna/aie2_pci.c new file +9 -0
+static void aie2_hw_stop(struct amdxdna_dev *xdna)
+{
+	struct pci_dev *pdev = to_pci_dev(xdna->ddev.dev);
+	struct amdxdna_dev_hdl *ndev = xdna->dev_handle;
+
+	aie2_psp_stop(ndev->psp_hdl);
+	aie2_smu_fini(ndev);
+	pci_disable_device(pdev);
+}
drivers/accel/amdxdna/aie2_pci.h new file +4 -0
new file mode 100644
index 000000000000..b3c360a10c04
@@ -0,0 +1,130 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2023-2024, Advanced Micro Devices, Inc.
+ */

drivers/accel/amdxdna/aie2_pci.h line 14

Quoted 10 lines
drivers/accel/amdxdna/aie2_pci.h new file +10 -0
+
+#ifndef _AIE2_PCI_H_
+#define _AIE2_PCI_H_
+
+#define AIE2_INTERVAL	20000	/* us */
+#define AIE2_TIMEOUT	1000000	/* us */
+
+/* Firmware determines device memory base address and size */
+#define AIE2_DEVM_BASE	0x4000000
+#define AIE2_DEVM_SIZE	(64 * SZ_1M)

SZ_64M? Then the code, and reader's brains, are not doing multiplication.

Quoted 15 lines
drivers/accel/amdxdna/amdxdna_pci_drv.c new file +9 -0
new file mode 100644
index 000000000000..33133f2503cd
@@ -0,0 +1,128 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2022-2024, Advanced Micro Devices, Inc.
+ */
+
+#include <drm/drm_accel.h>
+#include <drm/drm_drv.h>
+#include <drm/drm_gem.h>
+#include <drm/drm_ioctl.h>

drivers/accel/amdxdna/amdxdna_pci_drv.c line 19

Quoted 10 lines
drivers/accel/amdxdna/amdxdna_pci_drv.c new file +10 -0
+#include <drm/drm_managed.h>
+#include <linux/pci.h>
+
+#include "amdxdna_pci_drv.h"
+
+/*
+ *  Bind the driver base on PCI class and later use the (device_id, rev_id)
+ *  pair as a key to select the devices. The devices with same device_id
+ *  have very similar interface to host driver.
+ */

Looks like this comment didn't get updated when the class matching was removed.

Quoted 7 lines
drivers/accel/amdxdna/amdxdna_pci_drv.c new file +7 -0
+static const struct pci_device_id pci_ids[] = {
+	{ PCI_DEVICE(PCI_VENDOR_ID_AMD, 0x1502) },
+	{ PCI_DEVICE(PCI_VENDOR_ID_AMD, 0x17f0) },
+	{0}
+};
+
+MODULE_DEVICE_TABLE(pci, pci_ids);
author maintainer
#6 lore raw

Re: [PATCH V3 02/11] accel/amdxdna: Add a new driver for AMD AI Engine

Quoted 16 lines · Jeffrey Hugo
Quoted 15 lines · Lizhi Hou
drivers/accel/amdxdna/aie2_pci.c new file +9 -0
new file mode 100644
index 000000000000..e21b32557fc2
@@ -0,0 +1,183 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2023-2024, Advanced Micro Devices, Inc.
+ */
+
+#include <drm/drm_device.h>
+#include <drm/drm_managed.h>
+#include <drm/drm_print.h>
+#include <linux/errno.h>
Quoted 8 lines

drivers/accel/amdxdna/aie2_pci.c line 15

Quoted 6 lines
drivers/accel/amdxdna/aie2_pci.c new file +6 -0
+#include <linux/firmware.h>
+#include <linux/iommu.h>
+#include <linux/pci.h>
+
+#include "amdxdna_pci_drv.h"
+#include "aie2_pci.h"

Feels like a blank line is missing here.

sure. Will add it.

Quoted 24 lines
Quoted 24 lines
drivers/accel/amdxdna/aie2_pci.c new file +9 -0
+static void aie2_hw_stop(struct amdxdna_dev *xdna)
+{
+    struct pci_dev *pdev = to_pci_dev(xdna->ddev.dev);
+    struct amdxdna_dev_hdl *ndev = xdna->dev_handle;
+
+    aie2_psp_stop(ndev->psp_hdl);
+    aie2_smu_fini(ndev);
+    pci_disable_device(pdev);
+}
drivers/accel/amdxdna/aie2_pci.h new file +8 -0
new file mode 100644
index 000000000000..b3c360a10c04
@@ -0,0 +1,130 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (C) 2023-2024, Advanced Micro Devices, Inc.
+ */
+
+#ifndef _AIE2_PCI_H_
+#define _AIE2_PCI_H_
+
Quoted 8 lines

drivers/accel/amdxdna/aie2_pci.h line 14

Quoted 6 lines
drivers/accel/amdxdna/aie2_pci.h new file +6 -0
+#define AIE2_INTERVAL    20000    /* us */
+#define AIE2_TIMEOUT    1000000    /* us */
+
+/* Firmware determines device memory base address and size */
+#define AIE2_DEVM_BASE    0x4000000
+#define AIE2_DEVM_SIZE    (64 * SZ_1M)

SZ_64M? Then the code, and reader's brains, are not doing multiplication.

ok.

Quoted 22 lines
Quoted 22 lines
drivers/accel/amdxdna/amdxdna_pci_drv.c new file +15 -0
new file mode 100644
index 000000000000..33133f2503cd
@@ -0,0 +1,128 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2022-2024, Advanced Micro Devices, Inc.
+ */
+
+#include <drm/drm_accel.h>
+#include <drm/drm_drv.h>
+#include <drm/drm_gem.h>
+#include <drm/drm_ioctl.h>
+#include <drm/drm_managed.h>
+#include <linux/pci.h>
+
+#include "amdxdna_pci_drv.h"
+
+/*
Quoted 8 lines
Quoted 6 lines
drivers/accel/amdxdna/amdxdna_pci_drv.c new file +1 -0
+ *  Bind the driver base on PCI class and later use the (device_id, 

rev_id)
+ *  pair as a key to select the devices. The devices with same device_id
+ *  have very similar interface to host driver.
+ */

Looks like this comment didn't get updated when the class matching was removed.

I will fix the comment.

Thanks,

Lizhi

Quoted 8 lines
Quoted 8 lines
drivers/accel/amdxdna/amdxdna_pci_drv.c new file +7 -0
+static const struct pci_device_id pci_ids[] = {
+    { PCI_DEVICE(PCI_VENDOR_ID_AMD, 0x1502) },
+    { PCI_DEVICE(PCI_VENDOR_ID_AMD, 0x17f0) },
+    {0}
+};
+
+MODULE_DEVICE_TABLE(pci, pci_ids);
author maintainer
#7 lore raw

PATCH v3 3/11 accel/amdxdna: Support hardware mailbox

In reply to Lizhi Hou

The 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              | 257 +++++++-
 drivers/accel/amdxdna/aie2_pci.h              |  58 ++
 drivers/accel/amdxdna/aie2_psp.c              |   2 +
 drivers/accel/amdxdna/aie2_smu.c              |   1 +
 drivers/accel/amdxdna/amdxdna_mailbox.c       | 575 ++++++++++++++++++
 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             |   3 +-
 drivers/accel/amdxdna/npu2_regs.c             |   3 +-
 drivers/accel/amdxdna/npu4_regs.c             |   3 +-
 drivers/accel/amdxdna/npu5_regs.c             |   3 +-
 include/trace/events/amdxdna.h                |  60 ++
 19 files changed, 1769 insertions(+), 5 deletions(-)
 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
index 82d805bb5632..8e0a440a00dc 100644
@@ -1153,6 +1153,7 @@ L:	[email protected]
 S:	Supported
 T:	git https://gitlab.freedesktop.org/drm/misc/kernel.git
 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
index 1dee0cba8390..1b4e78b43b44 100644
@@ -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 +256 -1

301 lines not on this page · raw message

drivers/accel/amdxdna/aie2_pci.h +58 -0

83 lines not on this page · raw message

drivers/accel/amdxdna/aie2_psp.c +2 -0
index c87ca322e206..ac5296f4f2ae 100644
@@ -8,6 +8,8 @@
 #include <drm/drm_print.h>
 #include <linux/iopoll.h>
 
+#include "amdxdna_mailbox.h"
+#include "amdxdna_pci_drv.h"
 #include "aie2_pci.h"
 
 #define PSP_STATUS_READY	BIT(31)
drivers/accel/amdxdna/aie2_smu.c +1 -0
index e0ac695b002e..df973a167c17 100644
@@ -7,6 +7,7 @@
 #include <drm/drm_print.h>
 #include <linux/iopoll.h>
 
+#include "amdxdna_mailbox.h"
 #include "amdxdna_pci_drv.h"
 #include "aie2_pci.h"
 
drivers/accel/amdxdna/amdxdna_mailbox.c new file +575 -0

575 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
index 2f1a1c2441f9..64bce970514b 100644
@@ -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
index 5dd652fcf9d4..668b94b92714 100644
@@ -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 +2 -1
index 858b31a82888..1878f23b2831 100644
@@ -7,8 +7,9 @@
 #include <drm/drm_device.h>
 #include <linux/sizes.h>
 
-#include "aie2_pci.h"
+#include "amdxdna_mailbox.h"
 #include "amdxdna_pci_drv.h"
+#include "aie2_pci.h"
 
 /* Address definition from NPU1 docs */
 #define MPNPU_PUB_SEC_INTR		0x3010090
drivers/accel/amdxdna/npu2_regs.c +2 -1
index 02b0f22c9f14..eae5f3994a4c 100644
@@ -7,8 +7,9 @@
 #include <drm/drm_device.h>
 #include <linux/sizes.h>
 
-#include "aie2_pci.h"
+#include "amdxdna_mailbox.h"
 #include "amdxdna_pci_drv.h"
+#include "aie2_pci.h"
 
 /* NPU Public Registers on MpNPUAxiXbar (refer to Diag npu_registers.h) */
 #define MPNPU_PUB_SEC_INTR             0x3010060
drivers/accel/amdxdna/npu4_regs.c +2 -1
index ca5ca5a6c751..90790ebe41bd 100644
@@ -7,8 +7,9 @@
 #include <drm/drm_device.h>
 #include <linux/sizes.h>
 
-#include "aie2_pci.h"
+#include "amdxdna_mailbox.h"
 #include "amdxdna_pci_drv.h"
+#include "aie2_pci.h"
 
 /* NPU Public Registers on MpNPUAxiXbar (refer to Diag npu_registers.h) */
 #define MPNPU_PUB_SEC_INTR             0x3010060
drivers/accel/amdxdna/npu5_regs.c +2 -1
index 07fddcbb86ec..95c856d03468 100644
@@ -7,8 +7,9 @@
 #include <drm/drm_device.h>
 #include <linux/sizes.h>
 
-#include "aie2_pci.h"
+#include "amdxdna_mailbox.h"
 #include "amdxdna_pci_drv.h"
+#include "aie2_pci.h"
 
 /* NPU Public Registers on MpNPUAxiXbar (refer to Diag npu_registers.h) */
 #define MPNPU_PUB_SEC_INTR             0x3010060
include/trace/events/amdxdna.h new file +60 -0

60 lines not on this page · raw message

Signature · 1 line
2.34.1
#8 lore raw

Re: [PATCH V3 03/11] accel/amdxdna: Support hardware mailbox

drivers/accel/amdxdna/aie2_msg_priv.h line 118

Quoted 11 lines · Lizhi Hou
drivers/accel/amdxdna/aie2_msg_priv.h new file +11 -0
+struct create_ctx_req {
+	__u32	aie_type;
+	__u8	start_col;
+	__u8	num_col;
+	__u16	reserved;
+	__u8	num_cq_pairs_requested;
+	__u8	reserved1;
+	__u16	pasid;
+	__u32	pad[2];
+	__u32	sec_comm_target_type;
+	__u32     context_priority;

Alignment

Quoted 19 lines
drivers/accel/amdxdna/aie2_msg_priv.h new file +19 -0
+} __packed;
+
+struct create_ctx_resp {
+	enum aie2_msg_status	status;
+	__u32			context_id;
+	__u16			msix_id;
+	__u8			num_cq_pairs_allocated;
+	__u8			reserved;
+	struct cq_pair		cq_pair[MAX_CQ_PAIRS];
+} __packed;
+
+struct destroy_ctx_req {
+	__u32	context_id;
+} __packed;
+
+struct destroy_ctx_resp {
+	enum aie2_msg_status	status;
+} __packed;
+

drivers/accel/amdxdna/aie2_msg_priv.h line 147

Quoted 10 lines
drivers/accel/amdxdna/aie2_msg_priv.h new file +10 -0
+struct execute_buffer_req {
+	__u32	cu_idx;
+	__u32	payload[19];
+} __packed;
+
+struct exec_dpu_req {
+	__u64	inst_buf_addr;
+	__u32     inst_size;
+	__u32     inst_prop_cnt;
+	__u32     cu_idx;

Alignment

Quoted 8 lines
drivers/accel/amdxdna/aie2_msg_priv.h new file +3 -0
+	__u32	payload[35];
+} __packed;
+
drivers/accel/amdxdna/aie2_psp.c +0 -0
index c87ca322e206..ac5296f4f2ae 100644

drivers/accel/amdxdna/aie2_psp.c line 13

Quoted 6 lines
drivers/accel/amdxdna/aie2_psp.c +2 -0
@@ -8,6 +8,8 @@
  #include <drm/drm_print.h>
  #include <linux/iopoll.h>
 
+#include "amdxdna_mailbox.h"
+#include "amdxdna_pci_drv.h"
  #include "aie2_pci.h"

Doesn't look like alphabetical order to me. Also similar instances in the *_regs.c files below

Quoted 2 lines
drivers/accel/amdxdna/aie2_psp.c +0 -0
  #define PSP_STATUS_READY	BIT(31)
author maintainer
#9 lore raw

Re: [PATCH V3 03/11] accel/amdxdna: Support hardware mailbox

Quoted 13 lines · Jeffrey Hugo
Quoted 11 lines · Lizhi Hou

+struct create_ctx_req {
+    __u32    aie_type;
+    __u8    start_col;
+    __u8    num_col;
+    __u16    reserved;
+    __u8    num_cq_pairs_requested;
+    __u8    reserved1;
+    __u16    pasid;
+    __u32    pad[2];
+    __u32    sec_comm_target_type;
+    __u32     context_priority;

Quoted 1 line

Alignment

Will fix it.

Quoted 24 lines
Quoted 24 lines
drivers/accel/amdxdna/aie2_msg_priv.h new file +23 -0
+} __packed;
+
+struct create_ctx_resp {
+    enum aie2_msg_status    status;
+    __u32            context_id;
+    __u16            msix_id;
+    __u8            num_cq_pairs_allocated;
+    __u8            reserved;
+    struct cq_pair        cq_pair[MAX_CQ_PAIRS];
+} __packed;
+
+struct destroy_ctx_req {
+    __u32    context_id;
+} __packed;
+
+struct destroy_ctx_resp {
+    enum aie2_msg_status    status;
+} __packed;
+
+struct execute_buffer_req {
+    __u32    cu_idx;
+    __u32    payload[19];
+} __packed;
Quoted 8 lines

drivers/accel/amdxdna/aie2_msg_priv.h line 147

Quoted 6 lines
drivers/accel/amdxdna/aie2_msg_priv.h new file +6 -0
+
+struct exec_dpu_req {
+    __u64    inst_buf_addr;
+    __u32     inst_size;
+    __u32     inst_prop_cnt;
+    __u32     cu_idx;

Alignment

will fix it.

Quoted 16 lines
Quoted 11 lines

+    __u32    payload[35];

(quoted diff) +2 -0
+} __packed;
+
drivers/accel/amdxdna/aie2_psp.c +0 -0
index c87ca322e206..ac5296f4f2ae 100644
@@ -8,6 +8,8 @@
   #include <drm/drm_print.h>
   #include <linux/iopoll.h>
Quoted 3 lines

  +#include "amdxdna_mailbox.h"
+#include "amdxdna_pci_drv.h"
  #include "aie2_pci.h"

Doesn't look like alphabetical order to me.  Also similar instances in the *_regs.c files below

aie2 is one type of amdxdna hw platform. I would put amdxdna_*.h ahead of aie_*.h. Otherwise I probably need to add include in aie2_*.h.  Is it acceptable reason? Or I must use alphabetical for #include?

Thanks,

Lizhi

Quoted 4 lines
Quoted 2 lines

    #define PSP_STATUS_READY    BIT(31)

#10 lore raw

Re: [PATCH V3 03/11] accel/amdxdna: Support hardware mailbox

Quoted 67 lines · Lizhi Hou
Quoted 13 lines · Jeffrey Hugo
Quoted 11 lines · Lizhi Hou

+struct create_ctx_req {
+    __u32    aie_type;
+    __u8    start_col;
+    __u8    num_col;
+    __u16    reserved;
+    __u8    num_cq_pairs_requested;
+    __u8    reserved1;
+    __u16    pasid;
+    __u32    pad[2];
+    __u32    sec_comm_target_type;
+    __u32     context_priority;

Quoted 1 line

Alignment

Will fix it.

Quoted 24 lines
Quoted 24 lines
drivers/accel/amdxdna/aie2_msg_priv.h new file +23 -0
+} __packed;
+
+struct create_ctx_resp {
+    enum aie2_msg_status    status;
+    __u32            context_id;
+    __u16            msix_id;
+    __u8            num_cq_pairs_allocated;
+    __u8            reserved;
+    struct cq_pair        cq_pair[MAX_CQ_PAIRS];
+} __packed;
+
+struct destroy_ctx_req {
+    __u32    context_id;
+} __packed;
+
+struct destroy_ctx_resp {
+    enum aie2_msg_status    status;
+} __packed;
+
+struct execute_buffer_req {
+    __u32    cu_idx;
+    __u32    payload[19];
+} __packed;
Quoted 8 lines

drivers/accel/amdxdna/aie2_msg_priv.h line 147

Quoted 6 lines
drivers/accel/amdxdna/aie2_msg_priv.h new file +6 -0
+
+struct exec_dpu_req {
+    __u64    inst_buf_addr;
+    __u32     inst_size;
+    __u32     inst_prop_cnt;
+    __u32     cu_idx;

Alignment

will fix it.

Quoted 16 lines
Quoted 11 lines

+    __u32    payload[35];

(quoted diff) +2 -0
+} __packed;
+
drivers/accel/amdxdna/aie2_psp.c +0 -0
index c87ca322e206..ac5296f4f2ae 100644
@@ -8,6 +8,8 @@
   #include <drm/drm_print.h>
   #include <linux/iopoll.h>
Quoted 3 lines

  +#include "amdxdna_mailbox.h"
+#include "amdxdna_pci_drv.h"
  #include "aie2_pci.h"

Doesn't look like alphabetical order to me.  Also similar instances in the *_regs.c files below

Quoted 1 line

aie2 is one type of amdxdna hw platform. I would put amdxdna_*.h ahead of aie_*.h. Otherwise I probably need to add include in aie2_*.h.  Is it acceptable reason? Or I must use alphabetical for #include?

Sorting the includes is part of the Linux coding style, and the current style document mentions using clang-format for ordering them (in alphabetical order). Requesting ordered includes is common feedback per Lore.

Relying on include ordering seems very fragile.

-Jeff

author maintainer
#11 lore raw

PATCH v3 4/11 accel/amdxdna: Add hardware resource solver

In reply to Lizhi Hou

The 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
index 1b4e78b43b44..39d3404fbc8f 100644
@@ -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
index 490c36d94f50..fc1e5b2107d8 100644
@@ -16,6 +16,11 @@
 #include "amdxdna_pci_drv.h"
 #include "aie2_msg_priv.h"
 #include "aie2_pci.h"
+#include "aie2_solver.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.
@@ -306,6 +311,7 @@ static int aie2_hw_start(struct amdxdna_dev *xdna)
 static int aie2_init(struct amdxdna_dev *xdna)
 {
 	struct pci_dev *pdev = to_pci_dev(xdna->ddev.dev);
+	struct init_config xrs_cfg = { 0 };
 	struct amdxdna_dev_hdl *ndev;
 	struct psp_config psp_conf;
 	const struct firmware *fw;
@@ -406,7 +412,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
index 64bce970514b..c0710d3130fd 100644
@@ -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
author maintainer
#13 lore raw

PATCH v3 5/11 accel/amdxdna: Add hardware context

In reply to Lizhi Hou

The 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        |  14 ++
 drivers/accel/amdxdna/amdxdna_ctx.c     | 218 ++++++++++++++++++++++++
 drivers/accel/amdxdna/amdxdna_ctx.h     |  38 +++++
 drivers/accel/amdxdna/amdxdna_pci_drv.c | 124 +++++++++++++-
 drivers/accel/amdxdna/amdxdna_pci_drv.h |  20 +++
 include/uapi/drm/amdxdna_accel.h        | 131 ++++++++++++++
 10 files changed, 865 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
index 39d3404fbc8f..c86c90dfd303 100644
@@ -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

104 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 +14 -0
index b89636c2fe1b..a5354e84d533 100644
@@ -74,6 +74,8 @@ enum psp_reg_idx {
 	PSP_MAX_REGS /* Keep this at the end */
 };
 
+struct amdxdna_hwctx;
+
 struct psp_config {
 	const void	*fw_buf;
 	u32		fw_size;
@@ -113,6 +115,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;
@@ -185,4 +191,12 @@ int aie2_query_aie_version(struct amdxdna_dev_hdl *ndev, struct aie_version *ver
 int 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 +218 -0

218 lines not on this page · raw message

drivers/accel/amdxdna/amdxdna_ctx.h new file +38 -0
new file mode 100644
index 000000000000..3627770e5a98
@@ -0,0 +1,38 @@
+/* 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;
+};
+
+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 +123 -1

166 lines not on this page · raw message

drivers/accel/amdxdna/amdxdna_pci_drv.h +20 -0
index c0710d3130fd..5ec7fe168406 100644
@@ -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
#14 lore raw

Re: [PATCH V3 05/11] accel/amdxdna: Add hardware context

Quoted 11 lines · Lizhi Hou
drivers/accel/amdxdna/aie2_ctx.c new file +5 -0
new file mode 100644
index 000000000000..52a71661f887
@@ -0,0 +1,186 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2024, Advanced Micro Devices, Inc.
+ */
+

drivers/accel/amdxdna/aie2_ctx.c line 15

Quoted 10 lines
drivers/accel/amdxdna/aie2_ctx.c new file +10 -0
+#include <drm/amdxdna_accel.h>
+#include <drm/drm_device.h>
+#include <drm/drm_print.h>
+#include <linux/types.h>
+
+#include "amdxdna_ctx.h"
+#include "amdxdna_mailbox.h"
+#include "amdxdna_pci_drv.h"
+#include "aie2_pci.h"
+#include "aie2_solver.h"

Alphabetical order?

<snip>

drivers/accel/amdxdna/aie2_ctx.c line 28

Quoted 13 lines
drivers/accel/amdxdna/aie2_ctx.c new file +13 -0
+/**
+ * struct amdxdna_drm_create_hwctx - Create hardware context.
+ * @ext: MBZ.
+ * @ext_flags: MBZ.
+ * @qos_p: Address of QoS info.
+ * @umq_bo: BO handle for user mode queue(UMQ).
+ * @log_buf_bo: BO handle for log buffer.
+ * @max_opc: Maximum operations per cycle.
+ * @num_tiles: Number of AIE tiles.
+ * @mem_size: Size of AIE tile memory.
+ * @umq_doorbell: Returned offset of doorbell associated with UMQ.
+ * @handle: Returned hardware context handle.
+ * @pad: MBZ.

You specify must be zero, but I don't see that checked anywhere.

Quoted 14 lines
drivers/accel/amdxdna/aie2_ctx.c new file +14 -0
+ */
+struct amdxdna_drm_create_hwctx {
+	__u64 ext;
+	__u64 ext_flags;
+	__u64 qos_p;
+	__u32 umq_bo;
+	__u32 log_buf_bo;
+	__u32 max_opc;
+	__u32 num_tiles;
+	__u32 mem_size;
+	__u32 umq_doorbell;
+	__u32 handle;
+	__u32 pad;
+};
author maintainer
#15 lore raw

Re: [PATCH V3 05/11] accel/amdxdna: Add hardware context

Quoted 40 lines · Jeffrey Hugo
Quoted 11 lines · Lizhi Hou
drivers/accel/amdxdna/aie2_ctx.c new file +5 -0
new file mode 100644
index 000000000000..52a71661f887
@@ -0,0 +1,186 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2024, Advanced Micro Devices, Inc.
+ */
+

drivers/accel/amdxdna/aie2_ctx.c line 15

Quoted 10 lines
drivers/accel/amdxdna/aie2_ctx.c new file +10 -0
+#include <drm/amdxdna_accel.h>
+#include <drm/drm_device.h>
+#include <drm/drm_print.h>
+#include <linux/types.h>
+
+#include "amdxdna_ctx.h"
+#include "amdxdna_mailbox.h"
+#include "amdxdna_pci_drv.h"
+#include "aie2_pci.h"
+#include "aie2_solver.h"

Alphabetical order?

<snip>

drivers/accel/amdxdna/aie2_ctx.c line 28

Quoted 13 lines
drivers/accel/amdxdna/aie2_ctx.c new file +13 -0
+/**
+ * struct amdxdna_drm_create_hwctx - Create hardware context.
+ * @ext: MBZ.
+ * @ext_flags: MBZ.
+ * @qos_p: Address of QoS info.
+ * @umq_bo: BO handle for user mode queue(UMQ).
+ * @log_buf_bo: BO handle for log buffer.
+ * @max_opc: Maximum operations per cycle.
+ * @num_tiles: Number of AIE tiles.
+ * @mem_size: Size of AIE tile memory.
+ * @umq_doorbell: Returned offset of doorbell associated with UMQ.
+ * @handle: Returned hardware context handle.
+ * @pad: MBZ.
Quoted 1 line

You specify must be zero, but I don't see that checked anywhere.

Ok. I did not see any driver checks structure pad. And pad should not be used. I will remove MBZ for pad.

Thanks,

Lizhi

Quoted 15 lines
Quoted 15 lines
drivers/accel/amdxdna/aie2_ctx.c new file +14 -0
+ */
+struct amdxdna_drm_create_hwctx {
+    __u64 ext;
+    __u64 ext_flags;
+    __u64 qos_p;
+    __u32 umq_bo;
+    __u32 log_buf_bo;
+    __u32 max_opc;
+    __u32 num_tiles;
+    __u32 mem_size;
+    __u32 umq_doorbell;
+    __u32 handle;
+    __u32 pad;
+};
#16 lore raw

Re: [PATCH V3 05/11] accel/amdxdna: Add hardware context

Quoted 42 lines · Lizhi Hou
Quoted 40 lines · Jeffrey Hugo
Quoted 11 lines · Lizhi Hou
drivers/accel/amdxdna/aie2_ctx.c new file +5 -0
new file mode 100644
index 000000000000..52a71661f887
@@ -0,0 +1,186 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2024, Advanced Micro Devices, Inc.
+ */
+

drivers/accel/amdxdna/aie2_ctx.c line 15

Quoted 10 lines
drivers/accel/amdxdna/aie2_ctx.c new file +10 -0
+#include <drm/amdxdna_accel.h>
+#include <drm/drm_device.h>
+#include <drm/drm_print.h>
+#include <linux/types.h>
+
+#include "amdxdna_ctx.h"
+#include "amdxdna_mailbox.h"
+#include "amdxdna_pci_drv.h"
+#include "aie2_pci.h"
+#include "aie2_solver.h"

Alphabetical order?

<snip>

Quoted 13 lines
drivers/accel/amdxdna/aie2_ctx.c new file +13 -0
+/**
+ * struct amdxdna_drm_create_hwctx - Create hardware context.
+ * @ext: MBZ.
+ * @ext_flags: MBZ.
+ * @qos_p: Address of QoS info.
+ * @umq_bo: BO handle for user mode queue(UMQ).
+ * @log_buf_bo: BO handle for log buffer.
+ * @max_opc: Maximum operations per cycle.
+ * @num_tiles: Number of AIE tiles.
+ * @mem_size: Size of AIE tile memory.
+ * @umq_doorbell: Returned offset of doorbell associated with UMQ.
+ * @handle: Returned hardware context handle.
+ * @pad: MBZ.
Quoted 3 lines
Quoted 1 line

You specify must be zero, but I don't see that checked anywhere.

Ok. I did not see any driver checks structure pad. And pad should not be used. I will remove MBZ for pad.

Be aware, if you don't check for 0, you can never reuse that portion of the structure for future use as there may be existing userspace that happens to be putting random garbage in that area at that future point.

-Jeff

author maintainer
#17 lore raw

PATCH v3 6/11 accel/amdxdna: Add GEM buffer object management

In reply to Lizhi Hou

There 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    |  79 +++
 drivers/accel/amdxdna/aie2_pci.h        |   3 +
 drivers/accel/amdxdna/amdxdna_ctx.h     |  10 +
 drivers/accel/amdxdna/amdxdna_gem.c     | 627 ++++++++++++++++++++++++
 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        |  81 +++
 10 files changed, 968 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
index c86c90dfd303..a688c378761f 100644
@@ -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 +79 -0

94 lines not on this page · raw message

drivers/accel/amdxdna/aie2_pci.h +3 -0
index a5354e84d533..aa0a1940a545 100644
@@ -116,6 +116,7 @@ struct rt_config {
 };
 
 struct amdxdna_hwctx_priv {
+	struct amdxdna_gem_obj		*heap;
 	void				*mbox_chann;
 };
 
@@ -193,6 +194,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
index 3627770e5a98..ee647b98218e 100644
@@ -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[] __counted_by(count);
+};
+
 struct amdxdna_hwctx {
 	struct amdxdna_client		*client;
 	struct amdxdna_hwctx_priv	*priv;
drivers/accel/amdxdna/amdxdna_gem.c new file +627 -0

627 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
index 9ca62901fcf1..4fc6677a7973 100644
@@ -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 = {
@@ -147,6 +157,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
index 5ec7fe168406..3dddde4ac12a 100644
@@ -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 +81 -0

106 lines not on this page · raw message

Signature · 1 line
2.34.1
#18 lore raw

Re: [PATCH V3 06/11] accel/amdxdna: Add GEM buffer object management

kernel test robot noticed the following build errors:

Hi Lizhi,

kernel test robot noticed the following build errors:

[auto build test ERROR on drm-misc/drm-misc-next]
[also build test ERROR on drm-tip/drm-tip linus/master v6.11-rc7 next-20240913] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Lizhi-Hou/accel-amdxdna-Add-documentation-for-AMD-NPU-accelerator-driver/20240912-020807
base:   git://anongit.freedesktop.org/drm/drm-misc drm-misc-next
patch link:    https://lore.kernel.org/r/20240911180604.1834434-7-lizhi.hou%40amd.com
patch subject: [PATCH V3 06/11] accel/amdxdna: Add GEM buffer object management
config: x86_64-allyesconfig (https://download.01.org/0day-ci/archive/20240914/[email protected]/config)
compiler: clang version 18.1.8 (https://github.com/llvm/llvm-project 3b5b5c1ec4a3095ab096dd780e84d7ab81f3d7ff)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240914/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

In file included from drivers/accel/amdxdna/aie2_ctx.c:13:

>> drivers/accel/amdxdna/amdxdna_ctx.h:16:26: error: use of undeclared identifier 'count'
      16 |         u32 data[] __counted_by(count);
         |                                 ^
   1 error generated.
--
   In file included from drivers/accel/amdxdna/aie2_pci.c:16:
>> drivers/accel/amdxdna/amdxdna_ctx.h:16:26: error: use of undeclared identifier 'count'
      16 |         u32 data[] __counted_by(count);
         |                                 ^
   drivers/accel/amdxdna/aie2_pci.c:406:46: warning: shift count >= width of type [-Wshift-count-overflow]
     406 |         ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
         |                                                     ^~~~~~~~~~~~~~~~

include/linux/dma-mapping.h:77:54: note: expanded from macro 'DMA_BIT_MASK'
77 | #define DMA_BIT_MASK(n) (((n) == 64) ? ~0ULL : ((1ULL<<(n))-1))
| ^ ~~~
1 warning and 1 error generated.

vim +/count +16 drivers/accel/amdxdna/amdxdna_ctx.h

     8	
     9	/* Exec buffer command header format */
    10	#define AMDXDNA_CMD_STATE		GENMASK(3, 0)
    11	#define AMDXDNA_CMD_EXTRA_CU_MASK	GENMASK(11, 10)
    12	#define AMDXDNA_CMD_COUNT		GENMASK(22, 12)
    13	#define AMDXDNA_CMD_OPCODE		GENMASK(27, 23)
    14	struct amdxdna_cmd {
    15		u32 header;
  > 16		u32 data[] __counted_by(count);
    17	};
    18	
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#19 lore raw

Re: [PATCH V3 06/11] accel/amdxdna: Add GEM buffer object management

In reply to Lizhi Hou
Quoted 9 lines · Lizhi Hou
include/uapi/drm/amdxdna_accel.h +9 -0
+/**
+ * struct amdxdna_drm_create_bo - Create a buffer object.
+ * @flags: Buffer flags. MBZ.
+ * @type: Buffer type.
+ * @pad1: MBZ.
+ * @vaddr: User VA of buffer if applied. MBZ.
+ * @size: Size in bytes.
+ * @handle: Returned DRM buffer object handle.
+ * @pad2: MBZ.

include/uapi/drm/amdxdna_accel.h struct amdxdna_drm_config_hwctx { · line 170

Quoted 10 lines
include/uapi/drm/amdxdna_accel.h +10 -0
+ */
+struct amdxdna_drm_create_bo {
+	__u64	flags;
+	__u32	type;
+	__u32	pad1;
+	__u64	vaddr;
+	__u64	size;
+	__u32	handle;
+	__u32	pad2;
+};

Why not eliminate both padding fields by either moving "handle" up, or "type" down?

author maintainer
#20 lore raw

Re: [PATCH V3 06/11] accel/amdxdna: Add GEM buffer object management

Quoted 14 lines · Jeffrey Hugo
Quoted 13 lines · Lizhi Hou
include/uapi/drm/amdxdna_accel.h +13 -0
+/**
+ * struct amdxdna_drm_create_bo - Create a buffer object.
+ * @flags: Buffer flags. MBZ.
+ * @type: Buffer type.
+ * @pad1: MBZ.
+ * @vaddr: User VA of buffer if applied. MBZ.
+ * @size: Size in bytes.
+ * @handle: Returned DRM buffer object handle.
+ * @pad2: MBZ.
+ */
+struct amdxdna_drm_create_bo {
+    __u64    flags;
+    __u32    type;
Quoted 8 lines
Quoted 6 lines

+    __u32    pad1;
+    __u64    vaddr;
+    __u64    size;
+    __u32    handle;
+    __u32    pad2;
+};

Why not eliminate both padding fields by either moving "handle" up, or "type" down?

Ok. I will change this.

Thanks,

Lizhi

author maintainer
#21 lore raw

PATCH v3 7/11 accel/amdxdna: Add command execution

In reply to Lizhi Hou

Add interfaces for user application to submit command and wait for its completion.

---
 drivers/accel/amdxdna/aie2_ctx.c              | 626 +++++++++++++++++-
 drivers/accel/amdxdna/aie2_message.c          | 343 ++++++++++
 drivers/accel/amdxdna/aie2_pci.c              |   6 +
 drivers/accel/amdxdna/aie2_pci.h              |  34 +
 drivers/accel/amdxdna/aie2_psp.c              |   2 +
 drivers/accel/amdxdna/aie2_smu.c              |   2 +
 drivers/accel/amdxdna/amdxdna_ctx.c           | 375 ++++++++++-
 drivers/accel/amdxdna/amdxdna_ctx.h           | 110 +++
 drivers/accel/amdxdna/amdxdna_gem.c           |   1 +
 .../accel/amdxdna/amdxdna_mailbox_helper.c    |   5 +
 drivers/accel/amdxdna/amdxdna_pci_drv.c       |   6 +
 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              |  59 ++
 19 files changed, 1614 insertions(+), 10 deletions(-)
drivers/accel/amdxdna/aie2_ctx.c +622 -4
index 7904ccd36518..7395ec1367ea 100644
@@ -8,14 +8,372 @@
 #include <drm/drm_gem.h>
 #include <drm/drm_gem_shmem_helper.h>
 #include <drm/drm_print.h>
+#include <linux/hmm.h>
 #include <linux/types.h>
+#include <trace/events/amdxdna.h>
 
 #include "amdxdna_ctx.h"
+#include "amdxdna_gem.h"
 #include "amdxdna_mailbox.h"
 #include "amdxdna_pci_drv.h"
+#include "aie2_msg_priv.h"
 #include "aie2_pci.h"
 #include "aie2_solver.h"
-#include "amdxdna_gem.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 /* miliseconds */
+
+static int
+aie2_hwctx_add_job(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job)
+{
+	struct amdxdna_sched_job *other;
+	int idx;
+
+	idx = get_job_idx(hwctx->priv->seq);
+	/* When pending list full, hwctx->seq points to oldest fence */
+	other = hwctx->priv->pending[idx];
+	if (other && other->fence)
+		return -EAGAIN;
+
+	if (other) {
+		dma_fence_put(other->out_fence);
+		amdxdna_job_put(other);
+	}
+
+	hwctx->priv->pending[idx] = job;
+	job->seq = hwctx->priv->seq++;
+	kref_get(&job->refcnt);
+
+	return 0;
+}
+
+static struct amdxdna_sched_job *
+aie2_hwctx_get_job(struct amdxdna_hwctx *hwctx, u64 seq)
+{
+	int idx;
+
+	/* Special sequence number for oldest fence if exist */
+	if (seq == AMDXDNA_INVALID_CMD_HANDLE) {
+		idx = get_job_idx(hwctx->priv->seq);
+		goto out;
+	}
+
+	if (seq >= hwctx->priv->seq)
+		return ERR_PTR(-EINVAL);
+
+	if (seq + HWCTX_MAX_CMDS < hwctx->priv->seq)
+		return NULL;
+
+	idx = get_job_idx(seq);
+
+out:
+	return hwctx->priv->pending[idx];
+}
+
+/* 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, true);
+	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 int aie2_hwctx_wait_for_idle(struct amdxdna_hwctx *hwctx)
+{
+	struct amdxdna_sched_job *job;
+
+	mutex_lock(&hwctx->priv->io_lock);
+	if (!hwctx->priv->seq) {
+		mutex_unlock(&hwctx->priv->io_lock);
+		return 0;
+	}
+
+	job = aie2_hwctx_get_job(hwctx, hwctx->priv->seq - 1);
+	if (IS_ERR_OR_NULL(job)) {
+		mutex_unlock(&hwctx->priv->io_lock);
+		XDNA_WARN(hwctx->client->xdna, "Corrupted pending list");
+		return 0;
+	}
+	mutex_unlock(&hwctx->priv->io_lock);
+
+	wait_event(hwctx->priv->job_free_wq, !job->fence);
+
+	return 0;
+}
+
+static void
+aie2_sched_notify(struct amdxdna_sched_job *job)
+{
+	struct dma_fence *fence = job->fence;
+
+	job->hwctx->priv->completed++;
+	dma_fence_signal(fence);
+	trace_xdna_job(&job->base, job->hwctx->name, "signaled fence", job->seq);
+	dma_fence_put(fence);
+	mmput(job->mm);
+	amdxdna_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);
+		amdxdna_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);
+	drm_sched_job_cleanup(sched_job);
+	job->fence = NULL;
+	amdxdna_job_put(job);
+
+	wake_up(&hwctx->priv->job_free_wq);
+}
+
+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)
 {
@@ -130,9 +488,10 @@ 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)
@@ -157,10 +516,48 @@ 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;
+	}
+	init_waitqueue_head(&priv->job_free_wq);
+
 	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);
@@ -185,7 +582,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 +602,43 @@ int aie2_hwctx_init(struct amdxdna_hwctx *hwctx)
 
 void aie2_hwctx_fini(struct amdxdna_hwctx *hwctx)
 {
+	struct amdxdna_sched_job *job;
+	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);
+
+	for (idx = 0; idx < HWCTX_MAX_CMDS; idx++) {
+		job = hwctx->priv->pending[idx];
+		if (!job)
+			continue;
+
+		dma_fence_put(job->out_fence);
+		amdxdna_job_put(job);
+	}
+	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 +705,183 @@ int aie2_hwctx_config(struct amdxdna_hwctx *hwctx, u32 type, u64 value, void *bu
 		return -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;
+	}
+
+	dma_resv_lock(to_gobj(abo)->resv, NULL);
+	if (mmu_interval_read_retry(&abo->mem.notifier, range.notifier_seq)) {
+		dma_resv_unlock(to_gobj(abo)->resv);
+		goto again;
+	}
+	abo->mem.map_invalid = false;
+	dma_resv_unlock(to_gobj(abo)->resv);
+
+put_mm:
+	mmput(mm);
+	return ret;
+}
+
+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 = drm_sched_job_init(&job->base, &hwctx->priv->entity, 1, hwctx);
+	if (ret) {
+		XDNA_ERR(xdna, "DRM job init failed, ret %d", ret);
+		return ret;
+	}
+
+	drm_sched_job_arm(&job->base);
+	job->out_fence = dma_fence_get(&job->base.s_fence->finished);
+
+retry:
+	ret = drm_gem_lock_reservations(job->bos, job->bo_cnt, &acquire_ctx);
+	if (ret) {
+		XDNA_WARN(xdna, "Failed to reverve fence, ret %d", ret);
+		goto put_fence;
+	}
+
+	for (i = 0; i < job->bo_cnt; i++) {
+		abo = to_xdna_obj(job->bos[i]);
+		if (abo->mem.map_invalid) {
+			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 put_fence;
+			}
+
+			ret = aie2_populate_range(abo);
+			if (ret)
+				goto put_fence;
+			goto retry;
+		}
+
+		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 put_fence;
+		}
+	}
+
+	for (i = 0; i < job->bo_cnt; i++)
+		dma_resv_add_fence(job->bos[i]->resv, job->out_fence, DMA_RESV_USAGE_WRITE);
+	drm_gem_unlock_reservations(job->bos, job->bo_cnt, &acquire_ctx);
+
+	mutex_lock(&hwctx->priv->io_lock);
+	ret = aie2_hwctx_add_job(hwctx, job);
+	if (ret) {
+		mutex_unlock(&hwctx->priv->io_lock);
+		goto signal_fence;
+	}
+
+	*seq = job->seq;
+	drm_sched_entity_push_job(&job->base);
+	mutex_unlock(&hwctx->priv->io_lock);
+
+	return 0;
+
+signal_fence:
+	dma_fence_signal(job->out_fence);
+put_fence:
+	dma_fence_put(job->out_fence);
+	drm_sched_job_cleanup(&job->base);
+	return ret;
+}
+
+int aie2_cmd_wait(struct amdxdna_hwctx *hwctx, u64 seq, u32 timeout)
+{
+	signed long remaining = MAX_SCHEDULE_TIMEOUT;
+	struct amdxdna_sched_job *job;
+	struct dma_fence *out_fence;
+	long ret;
+
+	mutex_lock(&hwctx->priv->io_lock);
+	job = aie2_hwctx_get_job(hwctx, seq);
+	if (IS_ERR(job)) {
+		mutex_unlock(&hwctx->priv->io_lock);
+		ret = PTR_ERR(job);
+		goto out;
+	}
+
+	if (unlikely(!job)) {
+		mutex_unlock(&hwctx->priv->io_lock);
+		ret = 0;
+		goto out;
+	}
+	out_fence = dma_fence_get(job->out_fence);
+	mutex_unlock(&hwctx->priv->io_lock);
+
+	if (timeout)
+		remaining = msecs_to_jiffies(timeout);
+
+	ret = dma_fence_wait_timeout(out_fence, true, remaining);
+	if (!ret)
+		ret = -ETIME;
+	else if (ret > 0)
+		ret = 0;
+
+	dma_fence_put(out_fence);
+out:
+	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;
+
+	dma_resv_lock(gobj->resv, NULL);
+	abo->mem.map_invalid = true;
+	mmu_interval_set_seq(&abo->mem.notifier, cur_seq);
+	ret = dma_resv_wait_timeout(gobj->resv, DMA_RESV_USAGE_BOOKKEEP,
+				    true, MAX_SCHEDULE_TIMEOUT);
+	dma_resv_unlock(gobj->resv);
+
+	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 +6 -0
index bbcafed9ff2f..d5afca68414e 100644
@@ -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>
@@ -14,6 +16,7 @@
 #include <linux/pci.h>
 
 #include "amdxdna_ctx.h"
+#include "amdxdna_gem.h"
 #include "amdxdna_mailbox.h"
 #include "amdxdna_pci_drv.h"
 #include "aie2_msg_priv.h"
@@ -499,4 +502,7 @@ 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,
+	.cmd_wait       = aie2_cmd_wait,
+	.hmm_invalidate = aie2_hmm_invalidate,
 };
drivers/accel/amdxdna/aie2_pci.h +34 -0
index aa0a1940a545..5bfcfdfe7d3a 100644
@@ -115,9 +115,28 @@ 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;
+	struct amdxdna_sched_job	*pending[HWCTX_MAX_CMDS];
+	u32				num_pending;
+	u64				seq;
+	/* Completed job counter */
+	u64				completed;
+
+	struct amdxdna_gem_obj		*cmd_buf[HWCTX_MAX_CMDS];
 };
 
 struct amdxdna_dev_hdl {
@@ -196,10 +215,25 @@ int aie2_create_context(struct amdxdna_dev_hdl *ndev, struct amdxdna_hwctx *hwct
 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);
+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);
+int aie2_cmd_wait(struct amdxdna_hwctx *hwctx, u64 seq, u32 timeout);
+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
index ac5296f4f2ae..d6e71d4df0e5 100644
@@ -4,6 +4,8 @@
  */
 
 #include <drm/drm_device.h>
+#include <drm/drm_gem_shmem_helper.h>
+#include <drm/gpu_scheduler.h>
 #include <drm/drm_managed.h>
 #include <drm/drm_print.h>
 #include <linux/iopoll.h>
drivers/accel/amdxdna/aie2_smu.c +2 -0
index df973a167c17..f9197b8715c8 100644
@@ -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 "amdxdna_mailbox.h"
drivers/accel/amdxdna/amdxdna_ctx.c +369 -6

439 lines not on this page · raw message

drivers/accel/amdxdna/amdxdna_ctx.h +110 -0

125 lines not on this page · raw message

drivers/accel/amdxdna/amdxdna_gem.c +1 -0
index 9526ff390a44..8d813edf371e 100644
@@ -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
index 42b615394605..5139a9c96a91 100644
@@ -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 +6 -0
index 4fc6677a7973..ddb74ce630b9 100644
@@ -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,9 @@ 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),
+	/* Exectuion */
+	DRM_IOCTL_DEF_DRV(AMDXDNA_EXEC_CMD, amdxdna_drm_submit_cmd_ioctl, 0),
+	DRM_IOCTL_DEF_DRV(AMDXDNA_WAIT_CMD, amdxdna_drm_wait_cmd_ioctl, 0),
 };
 
 static const struct file_operations amdxdna_fops = {
drivers/accel/amdxdna/amdxdna_pci_drv.h +5 -0
index 3dddde4ac12a..0324e73094b2 100644
@@ -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,8 @@ 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);
+	int (*cmd_wait)(struct amdxdna_hwctx *hwctx, u64 seq, u32 timeout);
 };
 
 /*
@@ -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
index 668b94b92714..f27e4ee960a0 100644
@@ -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
index 1878f23b2831..1fc07203320b 100644
@@ -5,6 +5,7 @@
 
 #include <drm/amdxdna_accel.h>
 #include <drm/drm_device.h>
+#include <drm/gpu_scheduler.h>
 #include <linux/sizes.h>
 
 #include "amdxdna_mailbox.h"
drivers/accel/amdxdna/npu2_regs.c +1 -0
index eae5f3994a4c..9c3ec52a6197 100644
@@ -5,6 +5,7 @@
 
 #include <drm/amdxdna_accel.h>
 #include <drm/drm_device.h>
+#include <drm/gpu_scheduler.h>
 #include <linux/sizes.h>
 
 #include "amdxdna_mailbox.h"
drivers/accel/amdxdna/npu4_regs.c +1 -0
index 90790ebe41bd..5391af55544d 100644
@@ -5,6 +5,7 @@
 
 #include <drm/amdxdna_accel.h>
 #include <drm/drm_device.h>
+#include <drm/gpu_scheduler.h>
 #include <linux/sizes.h>
 
 #include "amdxdna_mailbox.h"
drivers/accel/amdxdna/npu5_regs.c +1 -0
index 95c856d03468..432a9a18ed79 100644
@@ -5,6 +5,7 @@
 
 #include <drm/amdxdna_accel.h>
 #include <drm/drm_device.h>
+#include <drm/gpu_scheduler.h>
 #include <linux/sizes.h>
 
 #include "amdxdna_mailbox.h"
include/trace/events/amdxdna.h +41 -0

49 lines not on this page · raw message

include/uapi/drm/amdxdna_accel.h +59 -0

83 lines not on this page · raw message

Signature · 1 line
2.34.1
#22 lore raw

Re: [PATCH V3 07/11] accel/amdxdna: Add command execution

include/uapi/drm/amdxdna_accel.h struct amdxdna_drm_sync_bo { · line 220

Quoted 10 lines · Lizhi Hou
include/uapi/drm/amdxdna_accel.h +10 -0
+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.

I see a check for ext_flags, but not ext

Quoted 20 lines

+ * @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.
+ */

include/uapi/drm/amdxdna_accel.h +11 -0
+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;
+};
author maintainer
#23 lore raw

Re: [PATCH V3 07/11] accel/amdxdna: Add command execution

Quoted 13 lines · Jeffrey Hugo

include/uapi/drm/amdxdna_accel.h struct amdxdna_drm_sync_bo { · line 220

Quoted 10 lines · Lizhi Hou
include/uapi/drm/amdxdna_accel.h +10 -0
+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.

I see a check for ext_flags, but not ext

I will add the check.

Thanks,

Lizhi

Quoted 21 lines
Quoted 21 lines

+ * @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.
+ */

include/uapi/drm/amdxdna_accel.h +11 -0
+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;
+};
author maintainer
#24 lore raw

PATCH v3 8/11 accel/amdxdna: Add suspend and resume

In reply to Lizhi Hou

Implement 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 | 121 +++++++++++++++++++++++-
 drivers/accel/amdxdna/amdxdna_pci_drv.h |   4 +
 7 files changed, 186 insertions(+), 3 deletions(-)
drivers/accel/amdxdna/aie2_ctx.c +30 -0
index 7395ec1367ea..eba37d2dc933 100644
@@ -179,6 +179,36 @@ static int aie2_hwctx_wait_for_idle(struct amdxdna_hwctx *hwctx)
 	return 0;
 }
 
+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
index d5afca68414e..94cc6c07931b 100644
@@ -499,10 +499,14 @@ 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,
 	.cmd_wait       = aie2_cmd_wait,
 	.hmm_invalidate = aie2_hmm_invalidate,
+	.hwctx_suspend  = aie2_hwctx_suspend,
+	.hwctx_resume   = aie2_hwctx_resume,
 };
drivers/accel/amdxdna/aie2_pci.h +2 -0
index 5bfcfdfe7d3a..5eb14ac8c091 100644
@@ -230,6 +230,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);
 int aie2_cmd_wait(struct amdxdna_hwctx *hwctx, u64 seq, u32 timeout);
 void aie2_hmm_invalidate(struct amdxdna_gem_obj *abo, unsigned long cur_seq);
drivers/accel/amdxdna/amdxdna_ctx.c +26 -0
index 0ac51377095f..f242a92cb9aa 100644
@@ -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
index 66f4fbd063fa..e035ebd54045 100644
@@ -141,6 +141,8 @@ static inline u32 amdxdna_hwctx_col_map(struct amdxdna_hwctx *hwctx)
 void amdxdna_job_put(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 +118 -3

191 lines not on this page · raw message

drivers/accel/amdxdna/amdxdna_pci_drv.h +4 -0
index 0324e73094b2..01b516743a00 100644
@@ -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);
 	int (*cmd_wait)(struct amdxdna_hwctx *hwctx, u64 seq, u32 timeout);
 };
Signature · 1 line
2.34.1
#25 lore raw

Re: [PATCH V3 08/11] accel/amdxdna: Add suspend and resume

drivers/accel/amdxdna/amdxdna_pci_drv.c static void amdxdna_remove(struct pci_dev *pdev) · line 345

Quoted 7 lines · Lizhi Hou
drivers/accel/amdxdna/amdxdna_pci_drv.c +7 -0
+static int amdxdna_rpmops_suspend(struct device *dev)
+{
+	struct amdxdna_dev *xdna = pci_get_drvdata(to_pci_dev(dev));
+	int ret;
+
+	mutex_lock(&xdna->dev_lock);
+	WARN_ON(!list_empty(&xdna->client_list));

This feels weird. Can you explain?

Quoted 7 lines
drivers/accel/amdxdna/amdxdna_pci_drv.c +7 -0
+	ret = amdxdna_dev_suspend_nolock(xdna);
+	mutex_unlock(&xdna->dev_lock);
+
+	XDNA_DBG(xdna, "Runtime suspend done ret: %d", ret);
+	return ret;
+}
+
author maintainer
#26 lore raw

Re: [PATCH V3 08/11] accel/amdxdna: Add suspend and resume

Quoted 10 lines · Jeffrey Hugo

drivers/accel/amdxdna/amdxdna_pci_drv.c static void amdxdna_remove(struct pci_dev *pdev) · line 345

Quoted 7 lines · Lizhi Hou
drivers/accel/amdxdna/amdxdna_pci_drv.c +7 -0
+static int amdxdna_rpmops_suspend(struct device *dev)
+{
+    struct amdxdna_dev *xdna = pci_get_drvdata(to_pci_dev(dev));
+    int ret;
+
+    mutex_lock(&xdna->dev_lock);
+    WARN_ON(!list_empty(&xdna->client_list));

This feels weird. Can you explain?

The driver uses auto suspend. When there is opened client, auto suspend is held. So the client list should not be empty here.  The WARN_ON is kind of assert and  can be removed.

Thanks,

Lizhi

Quoted 8 lines
Quoted 8 lines

+    ret = amdxdna_dev_suspend_nolock(xdna);
+    mutex_unlock(&xdna->dev_lock);

drivers/accel/amdxdna/amdxdna_pci_drv.c +5 -0
+
+    XDNA_DBG(xdna, "Runtime suspend done ret: %d", ret);
+    return ret;
+}
+
author maintainer
#27 lore raw

PATCH v3 9/11 accel/amdxdna: Add error handling

In reply to Lizhi Hou

When 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   | 356 +++++++++++++++++++++++++++
 drivers/accel/amdxdna/aie2_message.c |  19 ++
 drivers/accel/amdxdna/aie2_pci.c     |  32 +++
 drivers/accel/amdxdna/aie2_pci.h     |   9 +
 5 files changed, 417 insertions(+)
 create mode 100644 drivers/accel/amdxdna/aie2_error.c
drivers/accel/amdxdna/Makefile +1 -0
index a688c378761f..ed6f87910880 100644
@@ -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 +356 -0

356 lines not on this page · raw message

drivers/accel/amdxdna/aie2_message.c +19 -0
index e192c971f24a..0eaa233b72b2 100644
@@ -307,6 +307,25 @@ int aie2_map_host_buf(struct amdxdna_dev_hdl *ndev, u32 context_id, u64 addr, u6
 	return 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
index 94cc6c07931b..0ec042aaa39f 100644
@@ -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;
 }
 
@@ -472,9 +481,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:
@@ -490,8 +520,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
index 5eb14ac8c091..5c09f2693787 100644
@@ -160,6 +160,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) \
@@ -200,6 +201,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);
@@ -214,6 +221,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
#28 lore raw

Re: [PATCH V3 09/11] accel/amdxdna: Add error handling

Quoted 23 lines · Lizhi Hou
drivers/accel/amdxdna/aie2_error.c new file +23 -0
+/*
+ * Below enum, struct and lookup tables are porting from XAIE util header file.
+ *
+ * Below data is defined by AIE device and it is used for decode error message
+ * from the device.
+ */
+
+enum aie_module_type {
+	AIE_MEM_MOD = 0,
+	AIE_CORE_MOD,
+	AIE_PL_MOD,
+};
+
+enum aie_error_category {
+	AIE_ERROR_SATURATION = 0,
+	AIE_ERROR_FP,
+	AIE_ERROR_STREAM,
+	AIE_ERROR_ACCESS,
+	AIE_ERROR_BUS,
+	AIE_ERROR_INSTRUCTION,
+	AIE_ERROR_ECC,
+	AIE_ERROR_LOCK,
+	AIE_ERROR_DMA,

drivers/accel/amdxdna/aie2_error.c line 70

Quoted 10 lines
drivers/accel/amdxdna/aie2_error.c new file +10 -0
+	AIE_ERROR_MEM_PARITY,
+	/* Unknown is not from XAIE, added for better category */
+	AIE_ERROR_UNKNOWN,
+};
+
+/* Don't pack, unless XAIE side changed */
+struct aie_error {
+	u8			row;
+	u8			col;
+	enum aie_module_type	mod_type;

As far as I am aware, the compiler will use multiple variable sizes to define an enum. This feels very fragile.

Quoted 2 lines
(quoted diff) +2 -0
+	u8			event_id;
+};
author maintainer
#29 lore raw

PATCH v3 10/11 accel/amdxdna: Add query functions

In reply to Lizhi Hou

Add GET_INFO ioctl to retrieve hardware information, including AIE, clock, hardware context etc.

---
 drivers/accel/amdxdna/aie2_error.c      |   1 +
 drivers/accel/amdxdna/aie2_message.c    |  65 +++++++
 drivers/accel/amdxdna/aie2_pci.c        | 222 ++++++++++++++++++++++++
 drivers/accel/amdxdna/aie2_pci.h        |   1 +
 drivers/accel/amdxdna/aie2_psp.c        |   1 +
 drivers/accel/amdxdna/aie2_smu.c        |   1 +
 drivers/accel/amdxdna/amdxdna_pci_drv.c |  19 ++
 drivers/accel/amdxdna/amdxdna_pci_drv.h |   2 +
 include/uapi/drm/amdxdna_accel.h        | 166 ++++++++++++++++++
 9 files changed, 478 insertions(+)
drivers/accel/amdxdna/aie2_error.c +1 -0
index 3b8223420f5b..9dc44ecf2adb 100644
@@ -3,6 +3,7 @@
  * Copyright (C) 2023-2024, Advanced Micro Devices, Inc.
  */
 
+#include <drm/amdxdna_accel.h>
 #include <drm/drm_cache.h>
 #include <drm/drm_device.h>
 #include <drm/drm_print.h>
drivers/accel/amdxdna/aie2_message.c +65 -0

71 lines not on this page · raw message

drivers/accel/amdxdna/aie2_pci.c +222 -0

239 lines not on this page · raw message

drivers/accel/amdxdna/aie2_pci.h +1 -0
index 5c09f2693787..0deb849edddf 100644
@@ -221,6 +221,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/aie2_psp.c +1 -0
index d6e71d4df0e5..facb52740ddc 100644
@@ -3,6 +3,7 @@
  * Copyright (C) 2022-2024, Advanced Micro Devices, Inc.
  */
 
+#include <drm/amdxdna_accel.h>
 #include <drm/drm_device.h>
 #include <drm/drm_gem_shmem_helper.h>
 #include <drm/gpu_scheduler.h>
drivers/accel/amdxdna/aie2_smu.c +1 -0
index f9197b8715c8..956c488c4650 100644
@@ -3,6 +3,7 @@
  * Copyright (C) 2022-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>
drivers/accel/amdxdna/amdxdna_pci_drv.c +19 -0
index 193c9babdb9f..2c0d6e9a2c86 100644
@@ -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),
@@ -155,6 +172,8 @@ static const struct drm_ioctl_desc amdxdna_drm_ioctls[] = {
 	/* Exectuion */
 	DRM_IOCTL_DEF_DRV(AMDXDNA_EXEC_CMD, amdxdna_drm_submit_cmd_ioctl, 0),
 	DRM_IOCTL_DEF_DRV(AMDXDNA_WAIT_CMD, amdxdna_drm_wait_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 +2 -0
index 01b516743a00..a3e7e525c54d 100644
@@ -17,6 +17,7 @@
 
 extern const struct drm_driver amdxdna_drm_drv;
 
+struct amdxdna_client;
 struct amdxdna_dev;
 struct amdxdna_gem_obj;
 struct amdxdna_hwctx;
@@ -38,6 +39,7 @@ struct amdxdna_dev_ops {
 	void (*hwctx_resume)(struct amdxdna_hwctx *hwctx);
 	int (*cmd_submit)(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job, u64 *seq);
 	int (*cmd_wait)(struct amdxdna_hwctx *hwctx, u64 seq, u32 timeout);
+	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
#30 lore raw

Re: [PATCH V3 10/11] accel/amdxdna: Add query functions

drivers/accel/amdxdna/aie2_error.c line 9

Quoted 12 lines · Lizhi Hou
drivers/accel/amdxdna/aie2_error.c +1 -0
index 3b8223420f5b..9dc44ecf2adb 100644
@@ -3,6 +3,7 @@
   * Copyright (C) 2023-2024, Advanced Micro Devices, Inc.
   */
 
+#include <drm/amdxdna_accel.h>
  #include <drm/drm_cache.h>
  #include <drm/drm_device.h>
  #include <drm/drm_print.h>

Feels like this should be in the previous patch.

author maintainer
#31 lore raw

Re: [PATCH V3 10/11] accel/amdxdna: Add query functions

Quoted 14 lines · Jeffrey Hugo
Quoted 11 lines · Lizhi Hou
drivers/accel/amdxdna/aie2_error.c +0 -0
index 3b8223420f5b..9dc44ecf2adb 100644
@@ -3,6 +3,7 @@
    * Copyright (C) 2023-2024, Advanced Micro Devices, Inc.
    */

  +#include <drm/amdxdna_accel.h>
  #include <drm/drm_cache.h>
  #include <drm/drm_device.h>
  #include <drm/drm_print.h>

Feels like this should be in the previous patch.

I will fix this.

Thanks,

Lizhi

author maintainer
#32 lore raw

PATCH v3 11/11 accel/amdxdna: Add firmware debug buffer support

In reply to Lizhi Hou

User application may allocate a debug buffer and attach it to an NPU context through the driver. Then the NPU firmware prints its debug information to this buffer for debugging.

---
 drivers/accel/amdxdna/aie2_ctx.c    | 45 +++++++++++++++-
 drivers/accel/amdxdna/amdxdna_ctx.c |  1 +
 drivers/accel/amdxdna/amdxdna_ctx.h |  1 +
 drivers/accel/amdxdna/amdxdna_gem.c | 81 +++++++++++++++++++++++++++++
 drivers/accel/amdxdna/amdxdna_gem.h |  4 ++
 5 files changed, 131 insertions(+), 1 deletion(-)
drivers/accel/amdxdna/aie2_ctx.c +44 -1
index eba37d2dc933..6d35c67b9e47 100644
@@ -719,6 +719,48 @@ static int aie2_hwctx_cu_config(struct amdxdna_hwctx *hwctx, void *buf, u32 size
 	return ret;
 }
 
+static int aie2_hwctx_attach_debug_bo(struct amdxdna_hwctx *hwctx, u32 bo_hdl)
+{
+	struct amdxdna_client *client = hwctx->client;
+	struct amdxdna_dev *xdna = client->xdna;
+	struct amdxdna_gem_obj *abo;
+	int ret;
+
+	abo = amdxdna_gem_get_obj(client, bo_hdl, AMDXDNA_BO_DEV);
+	if (!abo) {
+		XDNA_ERR(xdna, "Get bo %d failed", bo_hdl);
+		ret = -EINVAL;
+		goto err_out;
+	}
+
+	ret = amdxdna_gem_set_assigned_hwctx(client, bo_hdl, hwctx->id);
+	if (ret) {
+		XDNA_ERR(xdna, "Failed to attach debug BO %d to %s: %d", bo_hdl, hwctx->name, ret);
+		goto put_obj;
+	}
+	XDNA_DBG(xdna, "Attached debug BO %d to %s", bo_hdl, hwctx->name);
+
+put_obj:
+	amdxdna_gem_put_obj(abo);
+err_out:
+	return ret;
+}
+
+static int aie2_hwctx_detach_debug_bo(struct amdxdna_hwctx *hwctx, u32 bo_hdl)
+{
+	struct amdxdna_client *client = hwctx->client;
+	struct amdxdna_dev *xdna = client->xdna;
+
+	if (amdxdna_gem_get_assigned_hwctx(client, bo_hdl) != hwctx->id) {
+		XDNA_ERR(xdna, "Debug BO %d isn't attached to %s", bo_hdl, hwctx->name);
+		return -EINVAL;
+	}
+
+	amdxdna_gem_clear_assigned_hwctx(client, bo_hdl);
+	XDNA_DBG(xdna, "Detached debug BO %d from %s", bo_hdl, hwctx->name);
+	return 0;
+}
+
 int aie2_hwctx_config(struct amdxdna_hwctx *hwctx, u32 type, u64 value, void *buf, u32 size)
 {
 	struct amdxdna_dev *xdna = hwctx->client->xdna;
@@ -728,8 +770,9 @@ int aie2_hwctx_config(struct amdxdna_hwctx *hwctx, u32 type, u64 value, void *bu
 	case DRM_AMDXDNA_HWCTX_CONFIG_CU:
 		return aie2_hwctx_cu_config(hwctx, buf, size);
 	case DRM_AMDXDNA_HWCTX_ASSIGN_DBG_BUF:
+		return aie2_hwctx_attach_debug_bo(hwctx, (u32)value);
 	case DRM_AMDXDNA_HWCTX_REMOVE_DBG_BUF:
-		return -EOPNOTSUPP;
+		return aie2_hwctx_detach_debug_bo(hwctx, (u32)value);
 	default:
 		XDNA_DBG(xdna, "Not supported type %d", type);
 		return -EOPNOTSUPP;
drivers/accel/amdxdna/amdxdna_ctx.c +1 -0
index f242a92cb9aa..58b8dbbec7ce 100644
@@ -194,6 +194,7 @@ int amdxdna_drm_create_hwctx_ioctl(struct drm_device *dev, void *data, struct dr
 	hwctx->num_tiles = args->num_tiles;
 	hwctx->mem_size = args->mem_size;
 	hwctx->max_opc = args->max_opc;
+	hwctx->log_buf_bo = args->log_buf_bo;
 	mutex_lock(&client->hwctx_lock);
 	ret = idr_alloc_cyclic(&client->hwctx_idr, hwctx, 0, MAX_HWCTX_ID, GFP_KERNEL);
 	if (ret < 0) {
drivers/accel/amdxdna/amdxdna_ctx.h +1 -0
index e035ebd54045..6349819cc959 100644
@@ -76,6 +76,7 @@ struct amdxdna_hwctx {
 	u32				*col_list;
 	u32				start_col;
 	u32				num_col;
+	u32				log_buf_bo;
 #define HWCTX_STAT_INIT  0
 #define HWCTX_STAT_READY 1
 #define HWCTX_STAT_STOP  2
drivers/accel/amdxdna/amdxdna_gem.c +81 -0

100 lines not on this page · raw message

drivers/accel/amdxdna/amdxdna_gem.h +4 -0
index 8ccc0375dd9d..d7337191d5ea 100644
@@ -58,6 +58,10 @@ int amdxdna_gem_pin_nolock(struct amdxdna_gem_obj *abo);
 int amdxdna_gem_pin(struct amdxdna_gem_obj *abo);
 void amdxdna_gem_unpin(struct amdxdna_gem_obj *abo);
 
+u32 amdxdna_gem_get_assigned_hwctx(struct amdxdna_client *client, u32 bo_hdl);
+int amdxdna_gem_set_assigned_hwctx(struct amdxdna_client *client, u32 bo_hdl, u32 ctx_hdl);
+void amdxdna_gem_clear_assigned_hwctx(struct amdxdna_client *client, u32 bo_hdl);
+
 int amdxdna_drm_create_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
 int amdxdna_drm_get_bo_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
 int amdxdna_drm_sync_bo_ioctl(struct drm_device *dev, void *data, struct drm_file *filp);
Signature · 1 line
2.34.1
#33 lore raw

Re: [PATCH V3 11/11] accel/amdxdna: Add firmware debug buffer support

Quoted 3 lines · Lizhi Hou

User application may allocate a debug buffer and attach it to an NPU context through the driver. Then the NPU firmware prints its debug information to this buffer for debugging.

I feel like I must be missing something. It looks like this patch accepts a buffer from the user, and stores it. However I don't see how the NPU firmware ever learns that this special buffer exists to then use it.

author maintainer
#34 lore raw

Re: [PATCH V3 11/11] accel/amdxdna: Add firmware debug buffer support

Quoted 6 lines · Jeffrey Hugo
Quoted 3 lines · Lizhi Hou

User application may allocate a debug buffer and attach it to an NPU context through the driver. Then the NPU firmware prints its debug information to this buffer for debugging.

I feel like I must be missing something. It looks like this patch accepts a buffer from the user, and stores it. However I don't see how the NPU firmware ever learns that this special buffer exists to then use it.

The debug function is incomplete while I was creating the patch. Thus, I put a line in TODO "Improve debug bo support".

We made progress on this feature. Should I add the code to V4 patch? Or I can add it within future patches.

Thanks,

Lizhi

author maintainer
#35 lore raw

Re: [PATCH V3 11/11] accel/amdxdna: Add firmware debug buffer support

Quoted 7 lines · Lizhi Hou
Quoted 5 lines · Jeffrey Hugo
Quoted 3 lines · Lizhi Hou

User application may allocate a debug buffer and attach it to an NPU context through the driver. Then the NPU firmware prints its debug information to this buffer for debugging.

Quoted 5 lines
Quoted 1 line

I feel like I must be missing something. It looks like this patch accepts a buffer from the user, and stores it. However I don't see how the NPU firmware ever learns that this special buffer exists to then use it.

The debug function is incomplete while I was creating the patch. Thus, I put a line in TODO "Improve debug bo support".

We made progress on this feature. Should I add the code to V4 patch? Or I can add it within future patches.

Discussed this more internally. And we would postpone debug_bo feature. Thus, I will  drop this patch for now.

Thanks,

Lizhi

Quoted 6 lines

Thanks,

Lizhi