NvMapDeviceFile.cs 8.4 KB

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