NvMapDeviceFile.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using Ryujinx.Common;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.Graphics.Gpu.Memory;
  4. using Ryujinx.Memory;
  5. using System;
  6. using System.Collections.Concurrent;
  7. namespace Ryujinx.HLE.HOS.Services.Nv.NvDrvServices.NvMap
  8. {
  9. internal class NvMapDeviceFile : NvDeviceFile
  10. {
  11. private const int FlagNotFreedYet = 1;
  12. private static ConcurrentDictionary<long, IdDictionary> _maps = new ConcurrentDictionary<long, IdDictionary>();
  13. public NvMapDeviceFile(ServiceCtx context, IVirtualMemoryManager memory, long owner) : base(context, owner)
  14. {
  15. IdDictionary dict = _maps.GetOrAdd(Owner, (key) => new IdDictionary());
  16. dict.Add(0, new NvMapHandle());
  17. }
  18. public override NvInternalResult Ioctl(NvIoctl command, Span<byte> arguments)
  19. {
  20. NvInternalResult result = NvInternalResult.NotImplemented;
  21. if (command.Type == NvIoctl.NvMapCustomMagic)
  22. {
  23. switch (command.Number)
  24. {
  25. case 0x01:
  26. result = CallIoctlMethod<NvMapCreate>(Create, arguments);
  27. break;
  28. case 0x03:
  29. result = CallIoctlMethod<NvMapFromId>(FromId, arguments);
  30. break;
  31. case 0x04:
  32. result = CallIoctlMethod<NvMapAlloc>(Alloc, arguments);
  33. break;
  34. case 0x05:
  35. result = CallIoctlMethod<NvMapFree>(Free, arguments);
  36. break;
  37. case 0x09:
  38. result = CallIoctlMethod<NvMapParam>(Param, arguments);
  39. break;
  40. case 0x0e:
  41. result = CallIoctlMethod<NvMapGetId>(GetId, arguments);
  42. break;
  43. case 0x02:
  44. case 0x06:
  45. case 0x07:
  46. case 0x08:
  47. case 0x0a:
  48. case 0x0c:
  49. case 0x0d:
  50. case 0x0f:
  51. case 0x10:
  52. case 0x11:
  53. result = NvInternalResult.NotSupported;
  54. break;
  55. }
  56. }
  57. return result;
  58. }
  59. private NvInternalResult Create(ref NvMapCreate arguments)
  60. {
  61. if (arguments.Size == 0)
  62. {
  63. Logger.Warning?.Print(LogClass.ServiceNv, $"Invalid size 0x{arguments.Size:x8}!");
  64. return NvInternalResult.InvalidInput;
  65. }
  66. int size = BitUtils.AlignUp(arguments.Size, (int)MemoryManager.PageSize);
  67. arguments.Handle = CreateHandleFromMap(new NvMapHandle(size));
  68. Logger.Info?.Print(LogClass.ServiceNv, $"Created map {arguments.Handle} with size 0x{size:x8}!");
  69. return NvInternalResult.Success;
  70. }
  71. private NvInternalResult FromId(ref NvMapFromId arguments)
  72. {
  73. NvMapHandle map = GetMapFromHandle(Owner, arguments.Id);
  74. if (map == null)
  75. {
  76. Logger.Warning?.Print(LogClass.ServiceNv, $"Invalid handle 0x{arguments.Handle:x8}!");
  77. return NvInternalResult.InvalidInput;
  78. }
  79. map.IncrementRefCount();
  80. arguments.Handle = arguments.Id;
  81. return NvInternalResult.Success;
  82. }
  83. private NvInternalResult Alloc(ref NvMapAlloc arguments)
  84. {
  85. NvMapHandle map = GetMapFromHandle(Owner, arguments.Handle);
  86. if (map == null)
  87. {
  88. Logger.Warning?.Print(LogClass.ServiceNv, $"Invalid handle 0x{arguments.Handle:x8}!");
  89. return NvInternalResult.InvalidInput;
  90. }
  91. if ((arguments.Align & (arguments.Align - 1)) != 0)
  92. {
  93. Logger.Warning?.Print(LogClass.ServiceNv, $"Invalid alignment 0x{arguments.Align:x8}!");
  94. return NvInternalResult.InvalidInput;
  95. }
  96. if ((uint)arguments.Align < MemoryManager.PageSize)
  97. {
  98. arguments.Align = (int)MemoryManager.PageSize;
  99. }
  100. NvInternalResult result = NvInternalResult.Success;
  101. if (!map.Allocated)
  102. {
  103. map.Allocated = true;
  104. map.Align = arguments.Align;
  105. map.Kind = (byte)arguments.Kind;
  106. int size = BitUtils.AlignUp(map.Size, (int)MemoryManager.PageSize);
  107. ulong address = arguments.Address;
  108. if (address == 0)
  109. {
  110. // When the address is zero, we need to allocate
  111. // our own backing memory for the NvMap.
  112. // TODO: Is this allocation inside the transfer memory?
  113. result = NvInternalResult.OutOfMemory;
  114. }
  115. if (result == NvInternalResult.Success)
  116. {
  117. map.Size = size;
  118. map.Address = address;
  119. }
  120. }
  121. return result;
  122. }
  123. private NvInternalResult Free(ref NvMapFree arguments)
  124. {
  125. NvMapHandle map = GetMapFromHandle(Owner, arguments.Handle);
  126. if (map == null)
  127. {
  128. Logger.Warning?.Print(LogClass.ServiceNv, $"Invalid handle 0x{arguments.Handle:x8}!");
  129. return NvInternalResult.InvalidInput;
  130. }
  131. if (DecrementMapRefCount(Owner, arguments.Handle))
  132. {
  133. arguments.Address = map.Address;
  134. arguments.Flags = 0;
  135. }
  136. else
  137. {
  138. arguments.Address = 0;
  139. arguments.Flags = FlagNotFreedYet;
  140. }
  141. arguments.Size = map.Size;
  142. return NvInternalResult.Success;
  143. }
  144. private NvInternalResult Param(ref NvMapParam arguments)
  145. {
  146. NvMapHandle map = GetMapFromHandle(Owner, arguments.Handle);
  147. if (map == null)
  148. {
  149. Logger.Warning?.Print(LogClass.ServiceNv, $"Invalid handle 0x{arguments.Handle:x8}!");
  150. return NvInternalResult.InvalidInput;
  151. }
  152. switch (arguments.Param)
  153. {
  154. case NvMapHandleParam.Size: arguments.Result = map.Size; break;
  155. case NvMapHandleParam.Align: arguments.Result = map.Align; break;
  156. case NvMapHandleParam.Heap: arguments.Result = 0x40000000; break;
  157. case NvMapHandleParam.Kind: arguments.Result = map.Kind; break;
  158. case NvMapHandleParam.Compr: arguments.Result = 0; break;
  159. // Note: Base is not supported and returns an error.
  160. // Any other value also returns an error.
  161. default: return NvInternalResult.InvalidInput;
  162. }
  163. return NvInternalResult.Success;
  164. }
  165. private NvInternalResult GetId(ref NvMapGetId arguments)
  166. {
  167. NvMapHandle map = GetMapFromHandle(Owner, arguments.Handle);
  168. if (map == null)
  169. {
  170. Logger.Warning?.Print(LogClass.ServiceNv, $"Invalid handle 0x{arguments.Handle:x8}!");
  171. return NvInternalResult.InvalidInput;
  172. }
  173. arguments.Id = arguments.Handle;
  174. return NvInternalResult.Success;
  175. }
  176. public override void Close()
  177. {
  178. // TODO: refcount NvMapDeviceFile instances and remove when closing
  179. // _maps.TryRemove(GetOwner(), out _);
  180. }
  181. private int CreateHandleFromMap(NvMapHandle map)
  182. {
  183. IdDictionary dict = _maps.GetOrAdd(Owner, (key) => new IdDictionary());
  184. return dict.Add(map);
  185. }
  186. private static bool DeleteMapWithHandle(long pid, int handle)
  187. {
  188. if (_maps.TryGetValue(pid, out IdDictionary dict))
  189. {
  190. return dict.Delete(handle) != null;
  191. }
  192. return false;
  193. }
  194. public static void IncrementMapRefCount(long pid, int handle)
  195. {
  196. GetMapFromHandle(pid, handle)?.IncrementRefCount();
  197. }
  198. public static bool DecrementMapRefCount(long pid, int handle)
  199. {
  200. NvMapHandle map = GetMapFromHandle(pid, handle);
  201. if (map == null)
  202. {
  203. return false;
  204. }
  205. if (map.DecrementRefCount() <= 0)
  206. {
  207. DeleteMapWithHandle(pid, handle);
  208. Logger.Info?.Print(LogClass.ServiceNv, $"Deleted map {handle}!");
  209. return true;
  210. }
  211. else
  212. {
  213. return false;
  214. }
  215. }
  216. public static NvMapHandle GetMapFromHandle(long pid, int handle)
  217. {
  218. if (_maps.TryGetValue(pid, out IdDictionary dict))
  219. {
  220. return dict.GetData<NvMapHandle>(handle);
  221. }
  222. return null;
  223. }
  224. }
  225. }