amdnpu.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. .. SPDX-License-Identifier: GPL-2.0-only
  2. .. include:: <isonum.txt>
  3. =========
  4. AMD NPU
  5. =========
  6. :Copyright: |copy| 2024 Advanced Micro Devices, Inc.
  7. :Author: Sonal Santan <sonal.santan@amd.com>
  8. Overview
  9. ========
  10. AMD NPU (Neural Processing Unit) is a multi-user AI inference accelerator
  11. integrated into AMD client APU. NPU enables efficient execution of Machine
  12. Learning applications like CNN, LLM, etc. NPU is based on
  13. `AMD XDNA Architecture`_. NPU is managed by **amdxdna** driver.
  14. Hardware Description
  15. ====================
  16. AMD NPU consists of the following hardware components:
  17. AMD XDNA Array
  18. --------------
  19. AMD XDNA Array comprises of 2D array of compute and memory tiles built with
  20. `AMD AI Engine Technology`_. Each column has 4 rows of compute tiles and 1
  21. row of memory tile. Each compute tile contains a VLIW processor with its own
  22. dedicated program and data memory. The memory tile acts as L2 memory. The 2D
  23. array can be partitioned at a column boundary creating a spatially isolated
  24. partition which can be bound to a workload context.
  25. Each column also has dedicated DMA engines to move data between host DDR and
  26. memory tile.
  27. AMD Phoenix and AMD Hawk Point client NPU have a 4x5 topology, i.e., 4 rows of
  28. compute tiles arranged into 5 columns. AMD Strix Point client APU have 4x8
  29. topology, i.e., 4 rows of compute tiles arranged into 8 columns.
  30. Shared L2 Memory
  31. ----------------
  32. The single row of memory tiles create a pool of software managed on chip L2
  33. memory. DMA engines are used to move data between host DDR and memory tiles.
  34. AMD Phoenix and AMD Hawk Point NPUs have a total of 2560 KB of L2 memory.
  35. AMD Strix Point NPU has a total of 4096 KB of L2 memory.
  36. Microcontroller
  37. ---------------
  38. A microcontroller runs NPU Firmware which is responsible for command processing,
  39. XDNA Array partition setup, XDNA Array configuration, workload context
  40. management and workload orchestration.
  41. NPU Firmware uses a dedicated instance of an isolated non-privileged context
  42. called ERT to service each workload context. ERT is also used to execute user
  43. provided ``ctrlcode`` associated with the workload context.
  44. NPU Firmware uses a single isolated privileged context called MERT to service
  45. management commands from the amdxdna driver.
  46. Mailboxes
  47. ---------
  48. The microcontroller and amdxdna driver use a privileged channel for management
  49. tasks like setting up of contexts, telemetry, query, error handling, setting up
  50. user channel, etc. As mentioned before, privileged channel requests are
  51. serviced by MERT. The privileged channel is bound to a single mailbox.
  52. The microcontroller and amdxdna driver use a dedicated user channel per
  53. workload context. The user channel is primarily used for submitting work to
  54. the NPU. As mentioned before, a user channel requests are serviced by an
  55. instance of ERT. Each user channel is bound to its own dedicated mailbox.
  56. PCIe EP
  57. -------
  58. NPU is visible to the x86 host CPU as a PCIe device with multiple BARs and some
  59. MSI-X interrupt vectors. NPU uses a dedicated high bandwidth SoC level fabric
  60. for reading or writing into host memory. Each instance of ERT gets its own
  61. dedicated MSI-X interrupt. MERT gets a single instance of MSI-X interrupt.
  62. The number of PCIe BARs varies depending on the specific device. Based on their
  63. functions, PCIe BARs can generally be categorized into the following types.
  64. * PSP BAR: Expose the AMD PSP (Platform Security Processor) function
  65. * SMU BAR: Expose the AMD SMU (System Management Unit) function
  66. * SRAM BAR: Expose ring buffers for the mailbox
  67. * Mailbox BAR: Expose the mailbox control registers (head, tail and ISR
  68. registers etc.)
  69. * Public Register BAR: Expose public registers
  70. On specific devices, the above-mentioned BAR type might be combined into a
  71. single physical PCIe BAR. Or a module might require two physical PCIe BARs to
  72. be fully functional. For example,
  73. * On AMD Phoenix device, PSP, SMU, Public Register BARs are on PCIe BAR index 0.
  74. * On AMD Strix Point device, Mailbox and Public Register BARs are on PCIe BAR
  75. index 0. The PSP has some registers in PCIe BAR index 0 (Public Register BAR)
  76. and PCIe BAR index 4 (PSP BAR).
  77. Process Isolation Hardware
  78. --------------------------
  79. As explained before, XDNA Array can be dynamically divided into isolated
  80. spatial partitions, each of which may have one or more columns. The spatial
  81. partition is setup by programming the column isolation registers by the
  82. microcontroller. Each spatial partition is associated with a PASID which is
  83. also programmed by the microcontroller. Hence multiple spatial partitions in
  84. the NPU can make concurrent host access protected by PASID.
  85. The NPU FW itself uses microcontroller MMU enforced isolated contexts for
  86. servicing user and privileged channel requests.
  87. Mixed Spatial and Temporal Scheduling
  88. =====================================
  89. AMD XDNA architecture supports mixed spatial and temporal (time sharing)
  90. scheduling of 2D array. This means that spatial partitions may be setup and
  91. torn down dynamically to accommodate various workloads. A *spatial* partition
  92. may be *exclusively* bound to one workload context while another partition may
  93. be *temporarily* bound to more than one workload contexts. The microcontroller
  94. updates the PASID for a temporarily shared partition to match the context that
  95. has been bound to the partition at any moment.
  96. Resource Solver
  97. ---------------
  98. The Resource Solver component of the amdxdna driver manages the allocation
  99. of 2D array among various workloads. Every workload describes the number
  100. of columns required to run the NPU binary in its metadata. The Resource Solver
  101. component uses hints passed by the workload and its own heuristics to
  102. decide 2D array (re)partition strategy and mapping of workloads for spatial and
  103. temporal sharing of columns. The FW enforces the context-to-column(s) resource
  104. binding decisions made by the Resource Solver.
  105. AMD Phoenix and AMD Hawk Point client NPU can support 6 concurrent workload
  106. contexts. AMD Strix Point can support 16 concurrent workload contexts.
  107. Application Binaries
  108. ====================
  109. A NPU application workload is comprised of two separate binaries which are
  110. generated by the NPU compiler.
  111. 1. AMD XDNA Array overlay, which is used to configure a NPU spatial partition.
  112. The overlay contains instructions for setting up the stream switch
  113. configuration and ELF for the compute tiles. The overlay is loaded on the
  114. spatial partition bound to the workload by the associated ERT instance.
  115. Refer to the
  116. `Versal Adaptive SoC AIE-ML Architecture Manual (AM020)`_ for more details.
  117. 2. ``ctrlcode``, used for orchestrating the overlay loaded on the spatial
  118. partition. ``ctrlcode`` is executed by the ERT running in protected mode on
  119. the microcontroller in the context of the workload. ``ctrlcode`` is made up
  120. of a sequence of opcodes named ``XAie_TxnOpcode``. Refer to the
  121. `AI Engine Run Time`_ for more details.
  122. Special Host Buffers
  123. ====================
  124. Per-context Instruction Buffer
  125. ------------------------------
  126. Every workload context uses a host resident 64 MB buffer which is memory
  127. mapped into the ERT instance created to service the workload. The ``ctrlcode``
  128. used by the workload is copied into this special memory. This buffer is
  129. protected by PASID like all other input/output buffers used by that workload.
  130. Instruction buffer is also mapped into the user space of the workload.
  131. Global Privileged Buffer
  132. ------------------------
  133. In addition, the driver also allocates a single buffer for maintenance tasks
  134. like recording errors from MERT. This global buffer uses the global IOMMU
  135. domain and is only accessible by MERT.
  136. High-level Use Flow
  137. ===================
  138. Here are the steps to run a workload on AMD NPU:
  139. 1. Compile the workload into an overlay and a ``ctrlcode`` binary.
  140. 2. Userspace opens a context in the driver and provides the overlay.
  141. 3. The driver checks with the Resource Solver for provisioning a set of columns
  142. for the workload.
  143. 4. The driver then asks MERT to create a context on the device with the desired
  144. columns.
  145. 5. MERT then creates an instance of ERT. MERT also maps the Instruction Buffer
  146. into ERT memory.
  147. 6. The userspace then copies the ``ctrlcode`` to the Instruction Buffer.
  148. 7. Userspace then creates a command buffer with pointers to input, output, and
  149. instruction buffer; it then submits command buffer with the driver and goes
  150. to sleep waiting for completion.
  151. 8. The driver sends the command over the Mailbox to ERT.
  152. 9. ERT *executes* the ``ctrlcode`` in the instruction buffer.
  153. 10. Execution of the ``ctrlcode`` kicks off DMAs to and from the host DDR while
  154. AMD XDNA Array is running.
  155. 11. When ERT reaches end of ``ctrlcode``, it raises an MSI-X to send completion
  156. signal to the driver which then wakes up the waiting workload.
  157. Boot Flow
  158. =========
  159. amdxdna driver uses PSP to securely load signed NPU FW and kick off the boot
  160. of the NPU microcontroller. amdxdna driver then waits for the alive signal in
  161. a special location on BAR 0. The NPU is switched off during SoC suspend and
  162. turned on after resume where the NPU FW is reloaded, and the handshake is
  163. performed again.
  164. Userspace components
  165. ====================
  166. Compiler
  167. --------
  168. Peano is an LLVM based open-source single core compiler for AMD XDNA Array
  169. compute tile. Peano is available at:
  170. https://github.com/Xilinx/llvm-aie
  171. IRON is an open-source array compiler for AMD XDNA Array based NPU which uses
  172. Peano underneath. IRON is available at:
  173. https://github.com/Xilinx/mlir-aie
  174. Usermode Driver (UMD)
  175. ---------------------
  176. The open-source XRT runtime stack interfaces with amdxdna kernel driver. XRT
  177. can be found at:
  178. https://github.com/Xilinx/XRT
  179. The open-source XRT shim for NPU is can be found at:
  180. https://github.com/amd/xdna-driver
  181. DMA Operation
  182. =============
  183. DMA operation instructions are encoded in the ``ctrlcode`` as
  184. ``XAIE_IO_BLOCKWRITE`` opcode. When ERT executes ``XAIE_IO_BLOCKWRITE``, DMA
  185. operations between host DDR and L2 memory are effected.
  186. Error Handling
  187. ==============
  188. When MERT detects an error in AMD XDNA Array, it pauses execution for that
  189. workload context and sends an asynchronous message to the driver over the
  190. privileged channel. The driver then sends a buffer pointer to MERT to capture
  191. the register states for the partition bound to faulting workload context. The
  192. driver then decodes the error by reading the contents of the buffer pointer.
  193. Telemetry
  194. =========
  195. MERT can report various kinds of telemetry information like the following:
  196. * L1 interrupt counter
  197. * DMA counter
  198. * Deep Sleep counter
  199. * etc.
  200. References
  201. ==========
  202. - `AMD XDNA Architecture <https://www.amd.com/en/technologies/xdna.html>`_
  203. - `AMD AI Engine Technology <https://www.xilinx.com/products/technology/ai-engine.html>`_
  204. - `Peano <https://github.com/Xilinx/llvm-aie>`_
  205. - `Versal Adaptive SoC AIE-ML Architecture Manual (AM020) <https://docs.amd.com/r/en-US/am020-versal-aie-ml>`_
  206. - `AI Engine Run Time <https://github.com/Xilinx/aie-rt/tree/release/main_aig>`_