NvMapDeviceFile.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. using Ryujinx.Common;
  2. using Ryujinx.Common.Logging;
  3. using Ryujinx.Graphics.Gpu.Memory;
  4. using Ryujinx.HLE.HOS.Kernel.Process;
  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<KProcess, IdDictionary> _maps = new ConcurrentDictionary<KProcess, IdDictionary>();
  13. public NvMapDeviceFile(ServiceCtx context) : base(context)
  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.PrintWarning(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.PrintInfo(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.PrintWarning(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.PrintWarning(LogClass.ServiceNv, $"Invalid handle 0x{arguments.Handle:x8}!");
  89. return NvInternalResult.InvalidInput;
  90. }
  91. if ((arguments.Align & (arguments.Align - 1)) != 0)
  92. {
  93. Logger.PrintWarning(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. long 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.PrintWarning(LogClass.ServiceNv, $"Invalid handle 0x{arguments.Handle:x8}!");
  129. return NvInternalResult.InvalidInput;
  130. }
  131. if (map.DecrementRefCount() <= 0)
  132. {
  133. DeleteMapWithHandle(arguments.Handle);
  134. Logger.PrintInfo(LogClass.ServiceNv, $"Deleted map {arguments.Handle}!");
  135. arguments.Address = map.Address;
  136. arguments.Flags = 0;
  137. }
  138. else
  139. {
  140. arguments.Address = 0;
  141. arguments.Flags = FlagNotFreedYet;
  142. }
  143. arguments.Size = map.Size;
  144. return NvInternalResult.Success;
  145. }
  146. private NvInternalResult Param(ref NvMapParam arguments)
  147. {
  148. NvMapHandle map = GetMapFromHandle(Owner, arguments.Handle);
  149. if (map == null)
  150. {
  151. Logger.PrintWarning(LogClass.ServiceNv, $"Invalid handle 0x{arguments.Handle:x8}!");
  152. return NvInternalResult.InvalidInput;
  153. }
  154. switch (arguments.Param)
  155. {
  156. case NvMapHandleParam.Size: arguments.Result = map.Size; break;
  157. case NvMapHandleParam.Align: arguments.Result = map.Align; break;
  158. case NvMapHandleParam.Heap: arguments.Result = 0x40000000; break;
  159. case NvMapHandleParam.Kind: arguments.Result = map.Kind; break;
  160. case NvMapHandleParam.Compr: arguments.Result = 0; break;
  161. // Note: Base is not supported and returns an error.
  162. // Any other value also returns an error.
  163. default: return NvInternalResult.InvalidInput;
  164. }
  165. return NvInternalResult.Success;
  166. }
  167. private NvInternalResult GetId(ref NvMapGetId arguments)
  168. {
  169. NvMapHandle map = GetMapFromHandle(Owner, arguments.Handle);
  170. if (map == null)
  171. {
  172. Logger.PrintWarning(LogClass.ServiceNv, $"Invalid handle 0x{arguments.Handle:x8}!");
  173. return NvInternalResult.InvalidInput;
  174. }
  175. arguments.Id = arguments.Handle;
  176. return NvInternalResult.Success;
  177. }
  178. public override void Close()
  179. {
  180. // TODO: refcount NvMapDeviceFile instances and remove when closing
  181. // _maps.TryRemove(GetOwner(), out _);
  182. }
  183. private int CreateHandleFromMap(NvMapHandle map)
  184. {
  185. IdDictionary dict = _maps.GetOrAdd(Owner, (key) =>
  186. {
  187. IdDictionary newDict = new IdDictionary();
  188. newDict.Add(0, new NvMapHandle());
  189. return newDict;
  190. });
  191. return dict.Add(map);
  192. }
  193. private bool DeleteMapWithHandle(int handle)
  194. {
  195. if (_maps.TryGetValue(Owner, out IdDictionary dict))
  196. {
  197. return dict.Delete(handle) != null;
  198. }
  199. return false;
  200. }
  201. public static NvMapHandle GetMapFromHandle(KProcess process, int handle, bool allowHandleZero = false)
  202. {
  203. if ((allowHandleZero || handle != 0) && _maps.TryGetValue(process, out IdDictionary dict))
  204. {
  205. return dict.GetData<NvMapHandle>(handle);
  206. }
  207. return null;
  208. }
  209. }
  210. }