nvmap.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <algorithm>
  4. #include <cstring>
  5. #include "common/assert.h"
  6. #include "common/logging/log.h"
  7. #include "core/hle/service/nvdrv/devices/nvmap.h"
  8. namespace Service::Nvidia::Devices {
  9. nvmap::nvmap(Core::System& system_) : nvdevice{system_} {
  10. // Handle 0 appears to be used when remapping, so we create a placeholder empty nvmap object to
  11. // represent this.
  12. CreateObject(0);
  13. }
  14. nvmap::~nvmap() = default;
  15. NvResult nvmap::Ioctl1(DeviceFD fd, Ioctl command, const std::vector<u8>& input,
  16. std::vector<u8>& output) {
  17. switch (command.group) {
  18. case 0x1:
  19. switch (command.cmd) {
  20. case 0x1:
  21. return IocCreate(input, output);
  22. case 0x3:
  23. return IocFromId(input, output);
  24. case 0x4:
  25. return IocAlloc(input, output);
  26. case 0x5:
  27. return IocFree(input, output);
  28. case 0x9:
  29. return IocParam(input, output);
  30. case 0xe:
  31. return IocGetId(input, output);
  32. default:
  33. break;
  34. }
  35. break;
  36. default:
  37. break;
  38. }
  39. UNIMPLEMENTED_MSG("Unimplemented ioctl={:08X}", command.raw);
  40. return NvResult::NotImplemented;
  41. }
  42. NvResult nvmap::Ioctl2(DeviceFD fd, Ioctl command, const std::vector<u8>& input,
  43. const std::vector<u8>& inline_input, std::vector<u8>& output) {
  44. UNIMPLEMENTED_MSG("Unimplemented ioctl={:08X}", command.raw);
  45. return NvResult::NotImplemented;
  46. }
  47. NvResult nvmap::Ioctl3(DeviceFD fd, Ioctl command, const std::vector<u8>& input,
  48. std::vector<u8>& output, std::vector<u8>& inline_output) {
  49. UNIMPLEMENTED_MSG("Unimplemented ioctl={:08X}", command.raw);
  50. return NvResult::NotImplemented;
  51. }
  52. void nvmap::OnOpen(DeviceFD fd) {}
  53. void nvmap::OnClose(DeviceFD fd) {}
  54. VAddr nvmap::GetObjectAddress(u32 handle) const {
  55. auto object = GetObject(handle);
  56. ASSERT(object);
  57. ASSERT(object->status == Object::Status::Allocated);
  58. return object->addr;
  59. }
  60. u32 nvmap::CreateObject(u32 size) {
  61. // Create a new nvmap object and obtain a handle to it.
  62. auto object = std::make_shared<Object>();
  63. object->id = next_id++;
  64. object->size = size;
  65. object->status = Object::Status::Created;
  66. object->refcount = 1;
  67. const u32 handle = next_handle++;
  68. handles.insert_or_assign(handle, std::move(object));
  69. return handle;
  70. }
  71. NvResult nvmap::IocCreate(const std::vector<u8>& input, std::vector<u8>& output) {
  72. IocCreateParams params;
  73. std::memcpy(&params, input.data(), sizeof(params));
  74. LOG_DEBUG(Service_NVDRV, "size=0x{:08X}", params.size);
  75. if (!params.size) {
  76. LOG_ERROR(Service_NVDRV, "Size is 0");
  77. return NvResult::BadValue;
  78. }
  79. params.handle = CreateObject(params.size);
  80. std::memcpy(output.data(), &params, sizeof(params));
  81. return NvResult::Success;
  82. }
  83. NvResult nvmap::IocAlloc(const std::vector<u8>& input, std::vector<u8>& output) {
  84. IocAllocParams params;
  85. std::memcpy(&params, input.data(), sizeof(params));
  86. LOG_DEBUG(Service_NVDRV, "called, addr={:X}", params.addr);
  87. if (!params.handle) {
  88. LOG_ERROR(Service_NVDRV, "Handle is 0");
  89. return NvResult::BadValue;
  90. }
  91. if ((params.align - 1) & params.align) {
  92. LOG_ERROR(Service_NVDRV, "Incorrect alignment used, alignment={:08X}", params.align);
  93. return NvResult::BadValue;
  94. }
  95. const u32 min_alignment = 0x1000;
  96. if (params.align < min_alignment) {
  97. params.align = min_alignment;
  98. }
  99. auto object = GetObject(params.handle);
  100. if (!object) {
  101. LOG_ERROR(Service_NVDRV, "Object does not exist, handle={:08X}", params.handle);
  102. return NvResult::BadValue;
  103. }
  104. if (object->status == Object::Status::Allocated) {
  105. LOG_ERROR(Service_NVDRV, "Object is already allocated, handle={:08X}", params.handle);
  106. return NvResult::InsufficientMemory;
  107. }
  108. object->flags = params.flags;
  109. object->align = params.align;
  110. object->kind = params.kind;
  111. object->addr = params.addr;
  112. object->status = Object::Status::Allocated;
  113. std::memcpy(output.data(), &params, sizeof(params));
  114. return NvResult::Success;
  115. }
  116. NvResult nvmap::IocGetId(const std::vector<u8>& input, std::vector<u8>& output) {
  117. IocGetIdParams params;
  118. std::memcpy(&params, input.data(), sizeof(params));
  119. LOG_WARNING(Service_NVDRV, "called");
  120. if (!params.handle) {
  121. LOG_ERROR(Service_NVDRV, "Handle is zero");
  122. return NvResult::BadValue;
  123. }
  124. auto object = GetObject(params.handle);
  125. if (!object) {
  126. LOG_ERROR(Service_NVDRV, "Object does not exist, handle={:08X}", params.handle);
  127. return NvResult::BadValue;
  128. }
  129. params.id = object->id;
  130. std::memcpy(output.data(), &params, sizeof(params));
  131. return NvResult::Success;
  132. }
  133. NvResult nvmap::IocFromId(const std::vector<u8>& input, std::vector<u8>& output) {
  134. IocFromIdParams params;
  135. std::memcpy(&params, input.data(), sizeof(params));
  136. LOG_WARNING(Service_NVDRV, "(STUBBED) called");
  137. auto itr = std::find_if(handles.begin(), handles.end(),
  138. [&](const auto& entry) { return entry.second->id == params.id; });
  139. if (itr == handles.end()) {
  140. LOG_ERROR(Service_NVDRV, "Object does not exist, handle={:08X}", params.handle);
  141. return NvResult::BadValue;
  142. }
  143. auto& object = itr->second;
  144. if (object->status != Object::Status::Allocated) {
  145. LOG_ERROR(Service_NVDRV, "Object is not allocated, handle={:08X}", params.handle);
  146. return NvResult::BadValue;
  147. }
  148. itr->second->refcount++;
  149. // Return the existing handle instead of creating a new one.
  150. params.handle = itr->first;
  151. std::memcpy(output.data(), &params, sizeof(params));
  152. return NvResult::Success;
  153. }
  154. NvResult nvmap::IocParam(const std::vector<u8>& input, std::vector<u8>& output) {
  155. enum class ParamTypes { Size = 1, Alignment = 2, Base = 3, Heap = 4, Kind = 5, Compr = 6 };
  156. IocParamParams params;
  157. std::memcpy(&params, input.data(), sizeof(params));
  158. LOG_DEBUG(Service_NVDRV, "(STUBBED) called type={}", params.param);
  159. auto object = GetObject(params.handle);
  160. if (!object) {
  161. LOG_ERROR(Service_NVDRV, "Object does not exist, handle={:08X}", params.handle);
  162. return NvResult::BadValue;
  163. }
  164. if (object->status != Object::Status::Allocated) {
  165. LOG_ERROR(Service_NVDRV, "Object is not allocated, handle={:08X}", params.handle);
  166. return NvResult::BadValue;
  167. }
  168. switch (static_cast<ParamTypes>(params.param)) {
  169. case ParamTypes::Size:
  170. params.result = object->size;
  171. break;
  172. case ParamTypes::Alignment:
  173. params.result = object->align;
  174. break;
  175. case ParamTypes::Heap:
  176. // TODO(Subv): Seems to be a hardcoded value?
  177. params.result = 0x40000000;
  178. break;
  179. case ParamTypes::Kind:
  180. params.result = object->kind;
  181. break;
  182. default:
  183. UNIMPLEMENTED();
  184. }
  185. std::memcpy(output.data(), &params, sizeof(params));
  186. return NvResult::Success;
  187. }
  188. NvResult nvmap::IocFree(const std::vector<u8>& input, std::vector<u8>& output) {
  189. // TODO(Subv): These flags are unconfirmed.
  190. enum FreeFlags {
  191. Freed = 0,
  192. NotFreedYet = 1,
  193. };
  194. IocFreeParams params;
  195. std::memcpy(&params, input.data(), sizeof(params));
  196. LOG_DEBUG(Service_NVDRV, "(STUBBED) called");
  197. auto itr = handles.find(params.handle);
  198. if (itr == handles.end()) {
  199. LOG_ERROR(Service_NVDRV, "Object does not exist, handle={:08X}", params.handle);
  200. return NvResult::BadValue;
  201. }
  202. if (!itr->second->refcount) {
  203. LOG_ERROR(
  204. Service_NVDRV,
  205. "There is no references to this object. The object is already freed. handle={:08X}",
  206. params.handle);
  207. return NvResult::BadValue;
  208. }
  209. itr->second->refcount--;
  210. params.size = itr->second->size;
  211. if (itr->second->refcount == 0) {
  212. params.flags = Freed;
  213. // The address of the nvmap is written to the output if we're finally freeing it, otherwise
  214. // 0 is written.
  215. params.address = itr->second->addr;
  216. } else {
  217. params.flags = NotFreedYet;
  218. params.address = 0;
  219. }
  220. handles.erase(params.handle);
  221. std::memcpy(output.data(), &params, sizeof(params));
  222. return NvResult::Success;
  223. }
  224. } // namespace Service::Nvidia::Devices