amdgpu_device.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * Copyright 2014 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. */
  23. /**
  24. * \file amdgpu_device.c
  25. *
  26. * Implementation of functions for AMD GPU device
  27. *
  28. */
  29. #include <sys/stat.h>
  30. #include <errno.h>
  31. #include <string.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <unistd.h>
  35. #include <fcntl.h>
  36. #include "xf86drm.h"
  37. #include "amdgpu_drm.h"
  38. #include "amdgpu_internal.h"
  39. #include "util_math.h"
  40. #define PTR_TO_UINT(x) ((unsigned)((intptr_t)(x)))
  41. static pthread_mutex_t dev_mutex = PTHREAD_MUTEX_INITIALIZER;
  42. static amdgpu_device_handle dev_list;
  43. static int fd_compare(int fd1, int fd2)
  44. {
  45. char *name1 = drmGetPrimaryDeviceNameFromFd(fd1);
  46. char *name2 = drmGetPrimaryDeviceNameFromFd(fd2);
  47. int result;
  48. if (name1 == NULL || name2 == NULL) {
  49. free(name1);
  50. free(name2);
  51. return 0;
  52. }
  53. result = strcmp(name1, name2);
  54. free(name1);
  55. free(name2);
  56. return result;
  57. }
  58. /**
  59. * Get the authenticated form fd,
  60. *
  61. * \param fd - \c [in] File descriptor for AMD GPU device
  62. * \param auth - \c [out] Pointer to output the fd is authenticated or not
  63. * A render node fd, output auth = 0
  64. * A legacy fd, get the authenticated for compatibility root
  65. *
  66. * \return 0 on success\n
  67. * >0 - AMD specific error code\n
  68. * <0 - Negative POSIX Error code
  69. */
  70. static int amdgpu_get_auth(int fd, int *auth)
  71. {
  72. int r = 0;
  73. drm_client_t client = {};
  74. if (drmGetNodeTypeFromFd(fd) == DRM_NODE_RENDER)
  75. *auth = 0;
  76. else {
  77. client.idx = 0;
  78. r = drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client);
  79. if (!r)
  80. *auth = client.auth;
  81. }
  82. return r;
  83. }
  84. static void amdgpu_device_free_internal(amdgpu_device_handle dev)
  85. {
  86. /* Remove dev from dev_list, if it was added there. */
  87. if (dev == dev_list) {
  88. dev_list = dev->next;
  89. } else {
  90. for (amdgpu_device_handle node = dev_list; node; node = node->next) {
  91. if (node->next == dev) {
  92. node->next = dev->next;
  93. break;
  94. }
  95. }
  96. }
  97. close(dev->fd);
  98. if ((dev->flink_fd >= 0) && (dev->fd != dev->flink_fd))
  99. close(dev->flink_fd);
  100. amdgpu_vamgr_deinit(&dev->va_mgr.vamgr_32);
  101. amdgpu_vamgr_deinit(&dev->va_mgr.vamgr_low);
  102. amdgpu_vamgr_deinit(&dev->va_mgr.vamgr_high_32);
  103. amdgpu_vamgr_deinit(&dev->va_mgr.vamgr_high);
  104. handle_table_fini(&dev->bo_handles);
  105. handle_table_fini(&dev->bo_flink_names);
  106. pthread_mutex_destroy(&dev->bo_table_mutex);
  107. free(dev->marketing_name);
  108. free(dev);
  109. }
  110. /**
  111. * Assignment between two amdgpu_device pointers with reference counting.
  112. *
  113. * Usage:
  114. * struct amdgpu_device *dst = ... , *src = ...;
  115. *
  116. * dst = src;
  117. * // No reference counting. Only use this when you need to move
  118. * // a reference from one pointer to another.
  119. *
  120. * amdgpu_device_reference(&dst, src);
  121. * // Reference counters are updated. dst is decremented and src is
  122. * // incremented. dst is freed if its reference counter is 0.
  123. */
  124. static void amdgpu_device_reference(struct amdgpu_device **dst,
  125. struct amdgpu_device *src)
  126. {
  127. if (update_references(&(*dst)->refcount, &src->refcount))
  128. amdgpu_device_free_internal(*dst);
  129. *dst = src;
  130. }
  131. #define IP_VERSION_MAJ(ver) ((ver >> 16) & 0xFF)
  132. #define IP_VERSION_MIN(ver) ((ver >> 8) & 0xFF)
  133. #define IP_VERSION_REV(ver) (ver & 0xFF)
  134. static int amdgpu_query_gfx_version(amdgpu_device_handle dev, uint32_t *gfx_version)
  135. {
  136. struct drm_amdgpu_info_hw_ip ip_info;
  137. uint32_t gfx_ip_count = 0;
  138. int r;
  139. *gfx_version = 0;
  140. r = amdgpu_query_hw_ip_count(dev, AMDGPU_HW_IP_GFX, &gfx_ip_count);
  141. if (r)
  142. return r;
  143. /* No graphics support. */
  144. if (gfx_ip_count == 0)
  145. return 0;
  146. memset(&ip_info, 0, sizeof(ip_info));
  147. r = amdgpu_query_hw_ip_info(dev, AMDGPU_HW_IP_GFX, 0, &ip_info);
  148. if (r)
  149. return r;
  150. /* GFX6-8 don't set ip_discovery_version. */
  151. if (dev->minor_version >= 48 && ip_info.ip_discovery_version) {
  152. *gfx_version = ip_info.ip_discovery_version;
  153. } else {
  154. *gfx_version = ip_info.hw_ip_version_major << 16 |
  155. ip_info.hw_ip_version_minor << 8;
  156. }
  157. return r;
  158. }
  159. static bool amdgpu_needs_smem_prt_wa(amdgpu_device_handle dev)
  160. {
  161. uint32_t gfx_version;
  162. int r;
  163. r = amdgpu_query_gfx_version(dev, &gfx_version);
  164. if (r) {
  165. /* Ignore if it's not possible to determine the GFX version. */
  166. return false;
  167. }
  168. return IP_VERSION_MAJ(gfx_version) >= 6 &&
  169. IP_VERSION_MAJ(gfx_version) <= 12 &&
  170. IP_VERSION_MAJ(gfx_version) != 9 &&
  171. !(IP_VERSION_MAJ(gfx_version) == 11 &&
  172. IP_VERSION_MIN(gfx_version) == 5 &&
  173. IP_VERSION_REV(gfx_version) == 6);
  174. }
  175. static int _amdgpu_device_initialize(int fd,
  176. uint32_t *major_version,
  177. uint32_t *minor_version,
  178. amdgpu_device_handle *device_handle,
  179. bool deduplicate_device)
  180. {
  181. struct amdgpu_device *dev = NULL;
  182. drmVersionPtr version;
  183. int r;
  184. int flag_auth = 0;
  185. int flag_authexist=0;
  186. uint32_t accel_working = 0;
  187. uint32_t va_mgr_flags = 0;
  188. *device_handle = NULL;
  189. pthread_mutex_lock(&dev_mutex);
  190. r = amdgpu_get_auth(fd, &flag_auth);
  191. if (r) {
  192. fprintf(stderr, "%s: amdgpu_get_auth (1) failed (%i)\n",
  193. __func__, r);
  194. pthread_mutex_unlock(&dev_mutex);
  195. return r;
  196. }
  197. if (deduplicate_device)
  198. for (dev = dev_list; dev; dev = dev->next)
  199. if (fd_compare(dev->fd, fd) == 0)
  200. break;
  201. if (dev) {
  202. r = amdgpu_get_auth(dev->fd, &flag_authexist);
  203. if (r) {
  204. fprintf(stderr, "%s: amdgpu_get_auth (2) failed (%i)\n",
  205. __func__, r);
  206. pthread_mutex_unlock(&dev_mutex);
  207. return r;
  208. }
  209. if ((flag_auth) && (!flag_authexist)) {
  210. dev->flink_fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
  211. }
  212. *major_version = dev->major_version;
  213. *minor_version = dev->minor_version;
  214. amdgpu_device_reference(device_handle, dev);
  215. pthread_mutex_unlock(&dev_mutex);
  216. return 0;
  217. }
  218. dev = calloc(1, sizeof(struct amdgpu_device));
  219. if (!dev) {
  220. fprintf(stderr, "%s: calloc failed\n", __func__);
  221. pthread_mutex_unlock(&dev_mutex);
  222. return -ENOMEM;
  223. }
  224. dev->fd = -1;
  225. dev->flink_fd = -1;
  226. atomic_set(&dev->refcount, 1);
  227. version = drmGetVersion(fd);
  228. if (!version) {
  229. fprintf(stderr, "%s: drmGetVersion failed\n", __func__);
  230. r = -EBADF;
  231. goto cleanup;
  232. }
  233. if (version->version_major != 3) {
  234. fprintf(stderr, "%s: DRM version is %d.%d.%d but this driver is "
  235. "only compatible with 3.x.x.\n",
  236. __func__,
  237. version->version_major,
  238. version->version_minor,
  239. version->version_patchlevel);
  240. drmFreeVersion(version);
  241. r = -EBADF;
  242. goto cleanup;
  243. }
  244. dev->fd = fcntl(fd, F_DUPFD_CLOEXEC, 0);
  245. dev->flink_fd = dev->fd;
  246. dev->major_version = version->version_major;
  247. dev->minor_version = version->version_minor;
  248. drmFreeVersion(version);
  249. pthread_mutex_init(&dev->bo_table_mutex, NULL);
  250. /* Check if acceleration is working. */
  251. r = amdgpu_query_info(dev, AMDGPU_INFO_ACCEL_WORKING, 4, &accel_working);
  252. if (r) {
  253. fprintf(stderr, "%s: amdgpu_query_info(ACCEL_WORKING) failed (%i)\n",
  254. __func__, r);
  255. goto cleanup;
  256. }
  257. if (!accel_working) {
  258. fprintf(stderr, "%s: AMDGPU_INFO_ACCEL_WORKING = 0\n", __func__);
  259. r = -EBADF;
  260. goto cleanup;
  261. }
  262. r = amdgpu_query_gpu_info_init(dev);
  263. if (r) {
  264. fprintf(stderr, "%s: amdgpu_query_gpu_info_init failed\n", __func__);
  265. goto cleanup;
  266. }
  267. if (amdgpu_needs_smem_prt_wa(dev)) {
  268. /* Reserve half of the addr space to implement a workaround for SMEM loads
  269. * with NULL PRT pages.
  270. */
  271. va_mgr_flags |= AMDGPU_VA_MGR_RESERVE_HALF_VA_FOR_PRT;
  272. }
  273. amdgpu_va_manager_init2(&dev->va_mgr,
  274. dev->dev_info.virtual_address_offset,
  275. dev->dev_info.virtual_address_max,
  276. dev->dev_info.high_va_offset,
  277. dev->dev_info.high_va_max,
  278. dev->dev_info.virtual_address_alignment,
  279. va_mgr_flags);
  280. amdgpu_parse_asic_ids(dev);
  281. *major_version = dev->major_version;
  282. *minor_version = dev->minor_version;
  283. *device_handle = dev;
  284. if (deduplicate_device) {
  285. dev->next = dev_list;
  286. dev_list = dev;
  287. }
  288. pthread_mutex_unlock(&dev_mutex);
  289. return 0;
  290. cleanup:
  291. if (dev->fd >= 0)
  292. close(dev->fd);
  293. free(dev);
  294. pthread_mutex_unlock(&dev_mutex);
  295. return r;
  296. }
  297. drm_public int amdgpu_device_initialize(int fd,
  298. uint32_t *major_version,
  299. uint32_t *minor_version,
  300. amdgpu_device_handle *device_handle)
  301. {
  302. return _amdgpu_device_initialize(fd, major_version, minor_version, device_handle, true);
  303. }
  304. drm_public int amdgpu_device_initialize2(int fd, bool deduplicate_device,
  305. uint32_t *major_version,
  306. uint32_t *minor_version,
  307. amdgpu_device_handle *device_handle)
  308. {
  309. return _amdgpu_device_initialize(fd, major_version, minor_version, device_handle, deduplicate_device);
  310. }
  311. drm_public int amdgpu_device_deinitialize(amdgpu_device_handle dev)
  312. {
  313. pthread_mutex_lock(&dev_mutex);
  314. amdgpu_device_reference(&dev, NULL);
  315. pthread_mutex_unlock(&dev_mutex);
  316. return 0;
  317. }
  318. drm_public int amdgpu_device_get_fd(amdgpu_device_handle device_handle)
  319. {
  320. return device_handle->fd;
  321. }
  322. drm_public const char *amdgpu_get_marketing_name(amdgpu_device_handle dev)
  323. {
  324. if (dev->marketing_name)
  325. return dev->marketing_name;
  326. else
  327. return "AMD Radeon Graphics";
  328. }
  329. drm_public int amdgpu_query_sw_info(amdgpu_device_handle dev,
  330. enum amdgpu_sw_info info,
  331. void *value)
  332. {
  333. uint32_t *val32 = (uint32_t*)value;
  334. switch (info) {
  335. case amdgpu_sw_info_address32_hi:
  336. if (dev->va_mgr.vamgr_high_32.va_max)
  337. *val32 = (dev->va_mgr.vamgr_high_32.va_max - 1) >> 32;
  338. else
  339. *val32 = (dev->va_mgr.vamgr_32.va_max - 1) >> 32;
  340. return 0;
  341. case amdgpu_sw_info_address_prt_wa_control_bit:
  342. *val32 = dev->va_mgr.address_prt_wa_control_bit;
  343. return 0;
  344. }
  345. return -EINVAL;
  346. }