ring-buffer.rst 4.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. =============
  2. Ring Buffer
  3. =============
  4. To handle communication between user space and kernel space, AMD GPUs use a
  5. ring buffer design to feed the engines (GFX, Compute, SDMA, UVD, VCE, VCN, VPE,
  6. etc.). See the figure below that illustrates how this communication works:
  7. .. kernel-figure:: ring_buffers.svg
  8. Ring buffers in the amdgpu work as a producer-consumer model, where userspace
  9. acts as the producer, constantly filling the ring buffer with GPU commands to
  10. be executed. Meanwhile, the GPU retrieves the information from the ring, parses
  11. it, and distributes the specific set of instructions between the different
  12. amdgpu blocks.
  13. Notice from the diagram that the ring has a Read Pointer (rptr), which
  14. indicates where the engine is currently reading packets from the ring, and a
  15. Write Pointer (wptr), which indicates how many packets software has added to
  16. the ring. When the rptr and wptr are equal, the ring is idle. When software
  17. adds packets to the ring, it updates the wptr, this causes the engine to start
  18. fetching and processing packets. As the engine processes packets, the rptr gets
  19. updates until the rptr catches up to the wptr and they are equal again.
  20. Usually, ring buffers in the driver have a limited size (search for occurrences
  21. of `amdgpu_ring_init()`). One of the reasons for the small ring buffer size is
  22. that CP (Command Processor) is capable of following addresses inserted into the
  23. ring; this is illustrated in the image by the reference to the IB (Indirect
  24. Buffer). The IB gives userspace the possibility to have an area in memory that
  25. CP can read and feed the hardware with extra instructions.
  26. All ASICs pre-GFX11 use what is called a kernel queue, which means
  27. the ring is allocated in kernel space and has some restrictions, such as not
  28. being able to be :ref:`preempted directly by the scheduler<amdgpu-mes>`. GFX11
  29. and newer support kernel queues, but also provide a new mechanism named
  30. :ref:`user queues<amdgpu-userq>`, where the queue is moved to the user space
  31. and can be mapped and unmapped via the scheduler. In practice, both queues
  32. insert user-space-generated GPU commands from different jobs into the requested
  33. component ring.
  34. Enforce Isolation
  35. =================
  36. .. note:: After reading this section, you might want to check the
  37. :ref:`Process Isolation<amdgpu-process-isolation>` page for more details.
  38. Before examining the Enforce Isolation mechanism in the ring buffer context, it
  39. is helpful to briefly discuss how instructions from the ring buffer are
  40. processed in the graphics pipeline. Let’s expand on this topic by checking the
  41. diagram below that illustrates the graphics pipeline:
  42. .. kernel-figure:: gfx_pipeline_seq.svg
  43. In terms of executing instructions, the GFX pipeline follows the sequence:
  44. Shader Export (SX), Geometry Engine (GE), Shader Process or Input (SPI), Scan
  45. Converter (SC), Primitive Assembler (PA), and cache manipulation (which may
  46. vary across ASICs). Another common way to describe the pipeline is to use Pixel
  47. Shader (PS), raster, and Vertex Shader (VS) to symbolize the two shader stages.
  48. Now, with this pipeline in mind, let's assume that Job B causes a hang issue,
  49. but Job C's instruction might already be executing, leading developers to
  50. incorrectly identify Job C as the problematic one. This problem can be
  51. mitigated on multiple levels; the diagram below illustrates how to minimize
  52. part of this problem:
  53. .. kernel-figure:: no_enforce_isolation.svg
  54. Note from the diagram that there is no guarantee of order or a clear separation
  55. between instructions, which is not a problem most of the time, and is also good
  56. for performance. Furthermore, notice some circles between jobs in the diagram
  57. that represent a **fence wait** used to avoid overlapping work in the ring. At
  58. the end of the fence, a cache flush occurs, ensuring that when the next job
  59. starts, it begins in a clean state and, if issues arise, the developer can
  60. pinpoint the problematic process more precisely.
  61. To increase the level of isolation between jobs, there is the "Enforce
  62. Isolation" method described in the picture below:
  63. .. kernel-figure:: enforce_isolation.svg
  64. As shown in the diagram, enforcing isolation introduces ordering between
  65. submissions, since the access to GFX/Compute is serialized, think about it as
  66. single process at a time mode for gfx/compute. Notice that this approach has a
  67. significant performance impact, as it allows only one job to submit commands at
  68. a time. However, this option can help pinpoint the job that caused the problem.
  69. Although enforcing isolation improves the situation, it does not fully resolve
  70. the issue of precisely pinpointing bad jobs, since isolation might mask the
  71. problem. In summary, identifying which job caused the issue may not be precise,
  72. but enforcing isolation might help with the debugging.
  73. Ring Operations
  74. ===============
  75. .. kernel-doc:: drivers/gpu/drm/amd/amdgpu/amdgpu_ring.c
  76. :internal: