msm-preemption.rst 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. .. SPDX-License-Identifier: GPL-2.0
  2. :orphan:
  3. ==============
  4. MSM Preemption
  5. ==============
  6. Preemption allows Adreno GPUs to switch to a higher priority ring when work is
  7. pushed to it, reducing latency for high priority submissions.
  8. When preemption is enabled 4 rings are initialized, corresponding to different
  9. priority levels. Having multiple rings is purely a software concept as the GPU
  10. only has registers to keep track of one graphics ring.
  11. The kernel is able to switch which ring is currently being processed by
  12. requesting preemption. When certain conditions are met, depending on the
  13. priority level, the GPU will save its current state in a series of buffers,
  14. then restores state from a similar set of buffers specified by the kernel. It
  15. then resumes execution and fires an IRQ to let the kernel know the context
  16. switch has completed.
  17. This mechanism can be used by the kernel to switch between rings. Whenever a
  18. submission occurs the kernel finds the highest priority ring which isn't empty
  19. and preempts to it if said ring is not the one being currently executed. This is
  20. also done whenever a submission completes to make sure execution resumes on a
  21. lower priority ring when a higher priority ring is done.
  22. Preemption levels
  23. -----------------
  24. Preemption can only occur at certain boundaries. The exact conditions can be
  25. configured by changing the preemption level, this allows to compromise between
  26. latency (ie. the time that passes between when the kernel requests preemption
  27. and when the SQE begins saving state) and overhead (the amount of state that
  28. needs to be saved).
  29. The GPU offers 3 levels:
  30. Level 0
  31. Preemption only occurs at the submission level. This requires the least amount
  32. of state to be saved as the execution of userspace submitted IBs is never
  33. interrupted, however it offers very little benefit compared to not enabling
  34. preemption of any kind.
  35. Level 1
  36. Preemption occurs at either bin level, if using GMEM rendering, or draw level
  37. in the sysmem rendering case.
  38. Level 2
  39. Preemption occurs at draw level.
  40. Level 1 is the mode that is used by the msm driver.
  41. Additionally the GPU allows to specify a `skip_save_restore` option. This
  42. disables the saving and restoring of all registers except those relating to the
  43. operation of the SQE itself, reducing overhead. Saving and restoring is only
  44. skipped when using GMEM with Level 1 preemption. When enabling this userspace is
  45. expected to set the state that isn't preserved whenever preemption occurs which
  46. is done by specifying preamble and postambles. Those are IBs that are executed
  47. before and after preemption.
  48. Preemption buffers
  49. ------------------
  50. A series of buffers are necessary to store the state of rings while they are not
  51. being executed. There are different kinds of preemption records and most of
  52. those require one buffer per ring. This is because preemption never occurs
  53. between submissions on the same ring, which always run in sequence when the ring
  54. is active. This means that only one context per ring is effectively active.
  55. SMMU_INFO
  56. This buffer contains info about the current SMMU configuration such as the
  57. ttbr0 register. The SQE firmware isn't actually able to save this record.
  58. As a result SMMU info must be saved manually from the CP to a buffer and the
  59. SMMU record updated with info from said buffer before triggering
  60. preemption.
  61. NON_SECURE
  62. This is the main preemption record where most state is saved. It is mostly
  63. opaque to the kernel except for the first few words that must be initialized
  64. by the kernel.
  65. SECURE
  66. This saves state related to the GPU's secure mode.
  67. NON_PRIV
  68. The intended purpose of this record is unknown. The SQE firmware actually
  69. ignores it and therefore msm doesn't handle it.
  70. COUNTER
  71. This record is used to save and restore performance counters.
  72. Handling the permissions of those buffers is critical for security. All but the
  73. NON_PRIV records need to be inaccessible from userspace, so they must be mapped
  74. in the kernel address space with the MSM_BO_MAP_PRIV flag.
  75. For example, making the NON_SECURE record accessible from userspace would allow
  76. any process to manipulate a saved ring's RPTR which can be used to skip the
  77. execution of some packets in a ring and execute user commands with higher
  78. privileges.