PoolMapper.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. using Ryujinx.Audio.Renderer.Common;
  2. using Ryujinx.Audio.Renderer.Parameter;
  3. using Ryujinx.Audio.Renderer.Utils;
  4. using Ryujinx.Common.Logging;
  5. using System;
  6. using static Ryujinx.Audio.Renderer.Common.BehaviourParameter;
  7. using CpuAddress = System.UInt64;
  8. using DspAddress = System.UInt64;
  9. namespace Ryujinx.Audio.Renderer.Server.MemoryPool
  10. {
  11. /// <summary>
  12. /// Memory pool mapping helper.
  13. /// </summary>
  14. public class PoolMapper
  15. {
  16. const uint CurrentProcessPseudoHandle = 0xFFFF8001;
  17. /// <summary>
  18. /// The result of <see cref="Update(ref MemoryPoolState, ref MemoryPoolInParameter, ref MemoryPoolOutStatus)"/>.
  19. /// </summary>
  20. public enum UpdateResult : uint
  21. {
  22. /// <summary>
  23. /// No error reported.
  24. /// </summary>
  25. Success = 0,
  26. /// <summary>
  27. /// The user parameters were invalid.
  28. /// </summary>
  29. InvalidParameter = 1,
  30. /// <summary>
  31. /// <see cref="Dsp.AudioProcessor"/> mapping failed.
  32. /// </summary>
  33. MapError = 2,
  34. /// <summary>
  35. /// <see cref="Dsp.AudioProcessor"/> unmapping failed.
  36. /// </summary>
  37. UnmapError = 3
  38. }
  39. /// <summary>
  40. /// The handle of the process owning the CPU memory manipulated.
  41. /// </summary>
  42. private uint _processHandle;
  43. /// <summary>
  44. /// The <see cref="Memory{MemoryPoolState}"/> that will be manipulated.
  45. /// </summary>
  46. private Memory<MemoryPoolState> _memoryPools;
  47. /// <summary>
  48. /// If set to true, this will try to force map memory pool even if their state are considered invalid.
  49. /// </summary>
  50. private bool _isForceMapEnabled;
  51. /// <summary>
  52. /// Create a new <see cref="PoolMapper"/> used for system mapping.
  53. /// </summary>
  54. /// <param name="processHandle">The handle of the process owning the CPU memory manipulated.</param>
  55. /// <param name="isForceMapEnabled">If set to true, this will try to force map memory pool even if their state are considered invalid.</param>
  56. public PoolMapper(uint processHandle, bool isForceMapEnabled)
  57. {
  58. _processHandle = processHandle;
  59. _isForceMapEnabled = isForceMapEnabled;
  60. _memoryPools = Memory<MemoryPoolState>.Empty;
  61. }
  62. /// <summary>
  63. /// Create a new <see cref="PoolMapper"/> used for user mapping.
  64. /// </summary>
  65. /// <param name="processHandle">The handle of the process owning the CPU memory manipulated.</param>
  66. /// <param name="memoryPool">The user memory pools.</param>
  67. /// <param name="isForceMapEnabled">If set to true, this will try to force map memory pool even if their state are considered invalid.</param>
  68. public PoolMapper(uint processHandle, Memory<MemoryPoolState> memoryPool, bool isForceMapEnabled)
  69. {
  70. _processHandle = processHandle;
  71. _memoryPools = memoryPool;
  72. _isForceMapEnabled = isForceMapEnabled;
  73. }
  74. /// <summary>
  75. /// Initialize the <see cref="MemoryPoolState"/> for system use.
  76. /// </summary>
  77. /// <param name="memoryPool">The <see cref="MemoryPoolState"/> for system use.</param>
  78. /// <param name="cpuAddress">The <see cref="CpuAddress"/> to assign.</param>
  79. /// <param name="size">The size to assign.</param>
  80. /// <returns>Returns true if mapping on the <see cref="Dsp.AudioProcessor"/> succeeded.</returns>
  81. public bool InitializeSystemPool(ref MemoryPoolState memoryPool, CpuAddress cpuAddress, ulong size)
  82. {
  83. if (memoryPool.Location != MemoryPoolState.LocationType.Dsp)
  84. {
  85. return false;
  86. }
  87. return InitializePool(ref memoryPool, cpuAddress, size);
  88. }
  89. /// <summary>
  90. /// Initialize the <see cref="MemoryPoolState"/>.
  91. /// </summary>
  92. /// <param name="memoryPool">The <see cref="MemoryPoolState"/>.</param>
  93. /// <param name="cpuAddress">The <see cref="CpuAddress"/> to assign.</param>
  94. /// <param name="size">The size to assign.</param>
  95. /// <returns>Returns true if mapping on the <see cref="Dsp.AudioProcessor"/> succeeded.</returns>
  96. public bool InitializePool(ref MemoryPoolState memoryPool, CpuAddress cpuAddress, ulong size)
  97. {
  98. memoryPool.SetCpuAddress(cpuAddress, size);
  99. return Map(ref memoryPool) != 0;
  100. }
  101. /// <summary>
  102. /// Get the process handle associated to the <see cref="MemoryPoolState"/>.
  103. /// </summary>
  104. /// <param name="memoryPool">The <see cref="MemoryPoolState"/>.</param>
  105. /// <returns>Returns the process handle associated to the <see cref="MemoryPoolState"/>.</returns>
  106. public uint GetProcessHandle(ref MemoryPoolState memoryPool)
  107. {
  108. if (memoryPool.Location == MemoryPoolState.LocationType.Cpu)
  109. {
  110. return CurrentProcessPseudoHandle;
  111. }
  112. else if (memoryPool.Location == MemoryPoolState.LocationType.Dsp)
  113. {
  114. return _processHandle;
  115. }
  116. return 0;
  117. }
  118. /// <summary>
  119. /// Map the <see cref="MemoryPoolState"/> on the <see cref="Dsp.AudioProcessor"/>.
  120. /// </summary>
  121. /// <param name="memoryPool">The <see cref="MemoryPoolState"/> to map.</param>
  122. /// <returns>Returns the DSP address mapped.</returns>
  123. public DspAddress Map(ref MemoryPoolState memoryPool)
  124. {
  125. DspAddress result = AudioProcessorMemoryManager.Map(GetProcessHandle(ref memoryPool), memoryPool.CpuAddress, memoryPool.Size);
  126. if (result != 0)
  127. {
  128. memoryPool.DspAddress = result;
  129. }
  130. return result;
  131. }
  132. /// <summary>
  133. /// Unmap the <see cref="MemoryPoolState"/> from the <see cref="Dsp.AudioProcessor"/>.
  134. /// </summary>
  135. /// <param name="memoryPool">The <see cref="MemoryPoolState"/> to unmap.</param>
  136. /// <returns>Returns true if unmapped.</returns>
  137. public bool Unmap(ref MemoryPoolState memoryPool)
  138. {
  139. if (memoryPool.IsUsed)
  140. {
  141. return false;
  142. }
  143. AudioProcessorMemoryManager.Unmap(GetProcessHandle(ref memoryPool), memoryPool.CpuAddress, memoryPool.Size);
  144. memoryPool.SetCpuAddress(0, 0);
  145. memoryPool.DspAddress = 0;
  146. return true;
  147. }
  148. /// <summary>
  149. /// Find a <see cref="MemoryPoolState"/> associated to the region given.
  150. /// </summary>
  151. /// <param name="cpuAddress">The region <see cref="CpuAddress"/>.</param>
  152. /// <param name="size">The region size.</param>
  153. /// <returns>Returns the <see cref="MemoryPoolState"/> found or <see cref="Memory{MemoryPoolState}.Empty"/> if not found.</returns>
  154. private Span<MemoryPoolState> FindMemoryPool(CpuAddress cpuAddress, ulong size)
  155. {
  156. if (!_memoryPools.IsEmpty && _memoryPools.Length > 0)
  157. {
  158. for (int i = 0; i < _memoryPools.Length; i++)
  159. {
  160. if (_memoryPools.Span[i].Contains(cpuAddress, size))
  161. {
  162. return _memoryPools.Span.Slice(i, 1);
  163. }
  164. }
  165. }
  166. return Span<MemoryPoolState>.Empty;
  167. }
  168. /// <summary>
  169. /// Force unmap the given <see cref="AddressInfo"/>.
  170. /// </summary>
  171. /// <param name="addressInfo">The <see cref="AddressInfo"/> to force unmap</param>
  172. public void ForceUnmap(ref AddressInfo addressInfo)
  173. {
  174. if (_isForceMapEnabled)
  175. {
  176. Span<MemoryPoolState> memoryPool = FindMemoryPool(addressInfo.CpuAddress, addressInfo.Size);
  177. if (!memoryPool.IsEmpty)
  178. {
  179. AudioProcessorMemoryManager.Unmap(_processHandle, memoryPool[0].CpuAddress, memoryPool[0].Size);
  180. return;
  181. }
  182. AudioProcessorMemoryManager.Unmap(_processHandle, addressInfo.CpuAddress, 0);
  183. }
  184. }
  185. /// <summary>
  186. /// Try to attach the given region to the <see cref="AddressInfo"/>.
  187. /// </summary>
  188. /// <param name="errorInfo">The error information if an error was generated.</param>
  189. /// <param name="addressInfo">The <see cref="AddressInfo"/> to attach the region to.</param>
  190. /// <param name="cpuAddress">The region <see cref="CpuAddress"/>.</param>
  191. /// <param name="size">The region size.</param>
  192. /// <returns>Returns true if mapping was performed.</returns>
  193. public bool TryAttachBuffer(out ErrorInfo errorInfo, ref AddressInfo addressInfo, CpuAddress cpuAddress, ulong size)
  194. {
  195. errorInfo = new ErrorInfo();
  196. addressInfo.Setup(cpuAddress, size);
  197. if (AssignDspAddress(ref addressInfo))
  198. {
  199. errorInfo.ErrorCode = 0x0;
  200. errorInfo.ExtraErrorInfo = 0x0;
  201. return true;
  202. }
  203. else
  204. {
  205. errorInfo.ErrorCode = ResultCode.InvalidAddressInfo;
  206. errorInfo.ExtraErrorInfo = addressInfo.CpuAddress;
  207. return _isForceMapEnabled;
  208. }
  209. }
  210. /// <summary>
  211. /// Update a <see cref="MemoryPoolState"/> using user parameters.
  212. /// </summary>
  213. /// <param name="memoryPool">The <see cref="MemoryPoolState"/> to update.</param>
  214. /// <param name="inParameter">Input user parameter.</param>
  215. /// <param name="outStatus">Output user parameter.</param>
  216. /// <returns>Returns the <see cref="UpdateResult"/> of the operations performed.</returns>
  217. public UpdateResult Update(ref MemoryPoolState memoryPool, ref MemoryPoolInParameter inParameter, ref MemoryPoolOutStatus outStatus)
  218. {
  219. MemoryPoolUserState inputState = inParameter.State;
  220. MemoryPoolUserState outputState;
  221. const uint pageSize = 0x1000;
  222. if (inputState != MemoryPoolUserState.RequestAttach && inputState != MemoryPoolUserState.RequestDetach)
  223. {
  224. return UpdateResult.Success;
  225. }
  226. if (inParameter.CpuAddress == 0 || (inParameter.CpuAddress % pageSize) != 0)
  227. {
  228. return UpdateResult.InvalidParameter;
  229. }
  230. if (inParameter.Size == 0 || (inParameter.Size % pageSize) != 0)
  231. {
  232. return UpdateResult.InvalidParameter;
  233. }
  234. if (inputState == MemoryPoolUserState.RequestAttach)
  235. {
  236. bool initializeSuccess = InitializePool(ref memoryPool, inParameter.CpuAddress, inParameter.Size);
  237. if (!initializeSuccess)
  238. {
  239. memoryPool.SetCpuAddress(0, 0);
  240. Logger.Error?.Print(LogClass.AudioRenderer, $"Map of memory pool (address: 0x{inParameter.CpuAddress:x}, size 0x{inParameter.Size:x}) failed!");
  241. return UpdateResult.MapError;
  242. }
  243. outputState = MemoryPoolUserState.Attached;
  244. }
  245. else
  246. {
  247. if (memoryPool.CpuAddress != inParameter.CpuAddress || memoryPool.Size != inParameter.Size)
  248. {
  249. return UpdateResult.InvalidParameter;
  250. }
  251. if (!Unmap(ref memoryPool))
  252. {
  253. Logger.Error?.Print(LogClass.AudioRenderer, $"Unmap of memory pool (address: 0x{memoryPool.CpuAddress:x}, size 0x{memoryPool.Size:x}) failed!");
  254. return UpdateResult.UnmapError;
  255. }
  256. outputState = MemoryPoolUserState.Detached;
  257. }
  258. outStatus.State = outputState;
  259. return UpdateResult.Success;
  260. }
  261. /// <summary>
  262. /// Map the <see cref="AddressInfo"/> to the <see cref="Dsp.AudioProcessor"/>.
  263. /// </summary>
  264. /// <param name="addressInfo">The <see cref="AddressInfo"/> to map.</param>
  265. /// <returns>Returns true if mapping was performed.</returns>
  266. private bool AssignDspAddress(ref AddressInfo addressInfo)
  267. {
  268. if (addressInfo.CpuAddress == 0)
  269. {
  270. return false;
  271. }
  272. if (_memoryPools.Length > 0)
  273. {
  274. Span<MemoryPoolState> memoryPool = FindMemoryPool(addressInfo.CpuAddress, addressInfo.Size);
  275. if (!memoryPool.IsEmpty)
  276. {
  277. addressInfo.SetupMemoryPool(memoryPool);
  278. return true;
  279. }
  280. }
  281. if (_isForceMapEnabled)
  282. {
  283. DspAddress dspAddress = AudioProcessorMemoryManager.Map(_processHandle, addressInfo.CpuAddress, addressInfo.Size);
  284. addressInfo.ForceMappedDspAddress = dspAddress;
  285. AudioProcessorMemoryManager.Map(_processHandle, addressInfo.CpuAddress, addressInfo.Size);
  286. }
  287. else
  288. {
  289. unsafe
  290. {
  291. addressInfo.SetupMemoryPool(MemoryPoolState.Null);
  292. }
  293. }
  294. return false;
  295. }
  296. /// <summary>
  297. /// Remove the usage flag from all the <see cref="MemoryPoolState"/>.
  298. /// </summary>
  299. /// <param name="memoryPool">The <see cref="Memory{MemoryPoolState}"/> to reset.</param>
  300. public static void ClearUsageState(Memory<MemoryPoolState> memoryPool)
  301. {
  302. foreach (ref MemoryPoolState info in memoryPool.Span)
  303. {
  304. info.IsUsed = false;
  305. }
  306. }
  307. }
  308. }