amdgpu_bo.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. /*
  2. * Copyright © 2014 Advanced Micro Devices, Inc.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. */
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <stdint.h>
  27. #include <string.h>
  28. #include <errno.h>
  29. #include <fcntl.h>
  30. #include <unistd.h>
  31. #include <sys/ioctl.h>
  32. #include <sys/mman.h>
  33. #include <sys/time.h>
  34. #include "libdrm_macros.h"
  35. #include "xf86drm.h"
  36. #include "amdgpu_drm.h"
  37. #include "amdgpu_internal.h"
  38. #include "util_math.h"
  39. static int amdgpu_bo_create(amdgpu_device_handle dev,
  40. uint64_t size,
  41. uint32_t handle,
  42. amdgpu_bo_handle *buf_handle)
  43. {
  44. struct amdgpu_bo *bo;
  45. int r;
  46. bo = calloc(1, sizeof(struct amdgpu_bo));
  47. if (!bo)
  48. return -ENOMEM;
  49. r = handle_table_insert(&dev->bo_handles, handle, bo);
  50. if (r) {
  51. free(bo);
  52. return r;
  53. }
  54. atomic_set(&bo->refcount, 1);
  55. bo->dev = dev;
  56. bo->alloc_size = size;
  57. bo->handle = handle;
  58. pthread_mutex_init(&bo->cpu_access_mutex, NULL);
  59. *buf_handle = bo;
  60. return 0;
  61. }
  62. drm_public int amdgpu_bo_alloc(amdgpu_device_handle dev,
  63. struct amdgpu_bo_alloc_request *alloc_buffer,
  64. amdgpu_bo_handle *buf_handle)
  65. {
  66. union drm_amdgpu_gem_create args;
  67. int r;
  68. if (!alloc_buffer || !buf_handle)
  69. return -EINVAL;
  70. memset(&args, 0, sizeof(args));
  71. args.in.bo_size = alloc_buffer->alloc_size;
  72. args.in.alignment = alloc_buffer->phys_alignment;
  73. /* Set the placement. */
  74. args.in.domains = alloc_buffer->preferred_heap;
  75. args.in.domain_flags = alloc_buffer->flags;
  76. /* Allocate the buffer with the preferred heap. */
  77. r = drmCommandWriteRead(dev->fd, DRM_AMDGPU_GEM_CREATE,
  78. &args, sizeof(args));
  79. if (r)
  80. goto out;
  81. pthread_mutex_lock(&dev->bo_table_mutex);
  82. r = amdgpu_bo_create(dev, alloc_buffer->alloc_size, args.out.handle,
  83. buf_handle);
  84. pthread_mutex_unlock(&dev->bo_table_mutex);
  85. if (r) {
  86. drmCloseBufferHandle(dev->fd, args.out.handle);
  87. }
  88. out:
  89. return r;
  90. }
  91. drm_public int amdgpu_bo_set_metadata(amdgpu_bo_handle bo,
  92. struct amdgpu_bo_metadata *info)
  93. {
  94. struct drm_amdgpu_gem_metadata args = {};
  95. if (!info)
  96. return -EINVAL;
  97. args.handle = bo->handle;
  98. args.op = AMDGPU_GEM_METADATA_OP_SET_METADATA;
  99. args.data.flags = info->flags;
  100. args.data.tiling_info = info->tiling_info;
  101. if (info->size_metadata > sizeof(args.data.data))
  102. return -EINVAL;
  103. if (info->size_metadata) {
  104. args.data.data_size_bytes = info->size_metadata;
  105. memcpy(args.data.data, info->umd_metadata, info->size_metadata);
  106. }
  107. return drmCommandWriteRead(bo->dev->fd,
  108. DRM_AMDGPU_GEM_METADATA,
  109. &args, sizeof(args));
  110. }
  111. drm_public int amdgpu_bo_query_info(amdgpu_bo_handle bo,
  112. struct amdgpu_bo_info *info)
  113. {
  114. struct drm_amdgpu_gem_metadata metadata = {};
  115. struct drm_amdgpu_gem_create_in bo_info = {};
  116. struct drm_amdgpu_gem_op gem_op = {};
  117. int r;
  118. /* Validate the BO passed in */
  119. if (!bo->handle || !info)
  120. return -EINVAL;
  121. /* Query metadata. */
  122. metadata.handle = bo->handle;
  123. metadata.op = AMDGPU_GEM_METADATA_OP_GET_METADATA;
  124. r = drmCommandWriteRead(bo->dev->fd, DRM_AMDGPU_GEM_METADATA,
  125. &metadata, sizeof(metadata));
  126. if (r)
  127. return r;
  128. if (metadata.data.data_size_bytes >
  129. sizeof(info->metadata.umd_metadata))
  130. return -EINVAL;
  131. /* Query buffer info. */
  132. gem_op.handle = bo->handle;
  133. gem_op.op = AMDGPU_GEM_OP_GET_GEM_CREATE_INFO;
  134. gem_op.value = (uintptr_t)&bo_info;
  135. r = drmCommandWriteRead(bo->dev->fd, DRM_AMDGPU_GEM_OP,
  136. &gem_op, sizeof(gem_op));
  137. if (r)
  138. return r;
  139. memset(info, 0, sizeof(*info));
  140. info->alloc_size = bo_info.bo_size;
  141. info->phys_alignment = bo_info.alignment;
  142. info->preferred_heap = bo_info.domains;
  143. info->alloc_flags = bo_info.domain_flags;
  144. info->metadata.flags = metadata.data.flags;
  145. info->metadata.tiling_info = metadata.data.tiling_info;
  146. info->metadata.size_metadata = metadata.data.data_size_bytes;
  147. if (metadata.data.data_size_bytes > 0)
  148. memcpy(info->metadata.umd_metadata, metadata.data.data,
  149. metadata.data.data_size_bytes);
  150. return 0;
  151. }
  152. static int amdgpu_bo_export_flink(amdgpu_bo_handle bo)
  153. {
  154. struct drm_gem_flink flink;
  155. int fd, dma_fd;
  156. uint32_t handle;
  157. int r;
  158. fd = bo->dev->fd;
  159. handle = bo->handle;
  160. if (bo->flink_name)
  161. return 0;
  162. if (bo->dev->flink_fd != bo->dev->fd) {
  163. r = drmPrimeHandleToFD(bo->dev->fd, bo->handle, DRM_CLOEXEC,
  164. &dma_fd);
  165. if (!r) {
  166. r = drmPrimeFDToHandle(bo->dev->flink_fd, dma_fd, &handle);
  167. close(dma_fd);
  168. }
  169. if (r)
  170. return r;
  171. fd = bo->dev->flink_fd;
  172. }
  173. memset(&flink, 0, sizeof(flink));
  174. flink.handle = handle;
  175. r = drmIoctl(fd, DRM_IOCTL_GEM_FLINK, &flink);
  176. if (r)
  177. return r;
  178. bo->flink_name = flink.name;
  179. if (bo->dev->flink_fd != bo->dev->fd)
  180. drmCloseBufferHandle(bo->dev->flink_fd, handle);
  181. pthread_mutex_lock(&bo->dev->bo_table_mutex);
  182. r = handle_table_insert(&bo->dev->bo_flink_names, bo->flink_name, bo);
  183. pthread_mutex_unlock(&bo->dev->bo_table_mutex);
  184. return r;
  185. }
  186. drm_public int amdgpu_bo_export(amdgpu_bo_handle bo,
  187. enum amdgpu_bo_handle_type type,
  188. uint32_t *shared_handle)
  189. {
  190. int r;
  191. switch (type) {
  192. case amdgpu_bo_handle_type_gem_flink_name:
  193. r = amdgpu_bo_export_flink(bo);
  194. if (r)
  195. return r;
  196. *shared_handle = bo->flink_name;
  197. return 0;
  198. case amdgpu_bo_handle_type_kms:
  199. case amdgpu_bo_handle_type_kms_noimport:
  200. *shared_handle = bo->handle;
  201. return 0;
  202. case amdgpu_bo_handle_type_dma_buf_fd:
  203. return drmPrimeHandleToFD(bo->dev->fd, bo->handle,
  204. DRM_CLOEXEC | DRM_RDWR,
  205. (int*)shared_handle);
  206. }
  207. return -EINVAL;
  208. }
  209. drm_public int amdgpu_bo_import(amdgpu_device_handle dev,
  210. enum amdgpu_bo_handle_type type,
  211. uint32_t shared_handle,
  212. struct amdgpu_bo_import_result *output)
  213. {
  214. struct drm_gem_open open_arg = {};
  215. struct amdgpu_bo *bo = NULL;
  216. uint32_t handle = 0, flink_name = 0;
  217. uint64_t alloc_size = 0;
  218. int r = 0;
  219. int dma_fd;
  220. uint64_t dma_buf_size = 0;
  221. /* We must maintain a list of pairs <handle, bo>, so that we always
  222. * return the same amdgpu_bo instance for the same handle. */
  223. pthread_mutex_lock(&dev->bo_table_mutex);
  224. /* Convert a DMA buf handle to a KMS handle now. */
  225. if (type == amdgpu_bo_handle_type_dma_buf_fd) {
  226. off_t size;
  227. /* Get a KMS handle. */
  228. r = drmPrimeFDToHandle(dev->fd, shared_handle, &handle);
  229. if (r)
  230. goto unlock;
  231. /* Query the buffer size. */
  232. size = lseek(shared_handle, 0, SEEK_END);
  233. if (size == (off_t)-1) {
  234. r = -errno;
  235. goto free_bo_handle;
  236. }
  237. lseek(shared_handle, 0, SEEK_SET);
  238. dma_buf_size = size;
  239. shared_handle = handle;
  240. }
  241. /* If we have already created a buffer with this handle, find it. */
  242. switch (type) {
  243. case amdgpu_bo_handle_type_gem_flink_name:
  244. bo = handle_table_lookup(&dev->bo_flink_names, shared_handle);
  245. break;
  246. case amdgpu_bo_handle_type_dma_buf_fd:
  247. bo = handle_table_lookup(&dev->bo_handles, shared_handle);
  248. break;
  249. case amdgpu_bo_handle_type_kms:
  250. case amdgpu_bo_handle_type_kms_noimport:
  251. /* Importing a KMS handle in not allowed. */
  252. r = -EPERM;
  253. goto unlock;
  254. default:
  255. r = -EINVAL;
  256. goto unlock;
  257. }
  258. if (bo) {
  259. /* The buffer already exists, just bump the refcount. */
  260. atomic_inc(&bo->refcount);
  261. pthread_mutex_unlock(&dev->bo_table_mutex);
  262. output->buf_handle = bo;
  263. output->alloc_size = bo->alloc_size;
  264. return 0;
  265. }
  266. /* Open the handle. */
  267. switch (type) {
  268. case amdgpu_bo_handle_type_gem_flink_name:
  269. open_arg.name = shared_handle;
  270. r = drmIoctl(dev->flink_fd, DRM_IOCTL_GEM_OPEN, &open_arg);
  271. if (r)
  272. goto unlock;
  273. flink_name = shared_handle;
  274. handle = open_arg.handle;
  275. alloc_size = open_arg.size;
  276. if (dev->flink_fd != dev->fd) {
  277. r = drmPrimeHandleToFD(dev->flink_fd, handle,
  278. DRM_CLOEXEC, &dma_fd);
  279. if (r)
  280. goto free_bo_handle;
  281. r = drmPrimeFDToHandle(dev->fd, dma_fd, &handle);
  282. close(dma_fd);
  283. if (r)
  284. goto free_bo_handle;
  285. r = drmCloseBufferHandle(dev->flink_fd,
  286. open_arg.handle);
  287. if (r)
  288. goto free_bo_handle;
  289. }
  290. open_arg.handle = 0;
  291. break;
  292. case amdgpu_bo_handle_type_dma_buf_fd:
  293. handle = shared_handle;
  294. alloc_size = dma_buf_size;
  295. break;
  296. case amdgpu_bo_handle_type_kms:
  297. case amdgpu_bo_handle_type_kms_noimport:
  298. assert(0); /* unreachable */
  299. }
  300. /* Initialize it. */
  301. r = amdgpu_bo_create(dev, alloc_size, handle, &bo);
  302. if (r)
  303. goto free_bo_handle;
  304. if (flink_name) {
  305. bo->flink_name = flink_name;
  306. r = handle_table_insert(&dev->bo_flink_names, flink_name,
  307. bo);
  308. if (r)
  309. goto free_bo_handle;
  310. }
  311. output->buf_handle = bo;
  312. output->alloc_size = bo->alloc_size;
  313. pthread_mutex_unlock(&dev->bo_table_mutex);
  314. return 0;
  315. free_bo_handle:
  316. if (flink_name && open_arg.handle)
  317. drmCloseBufferHandle(dev->flink_fd, open_arg.handle);
  318. /* Drop the lock before amdgpu_bo_free(), which re-acquires the same
  319. * non-recursive mutex and would otherwise self-deadlock. */
  320. pthread_mutex_unlock(&dev->bo_table_mutex);
  321. if (bo)
  322. amdgpu_bo_free(bo);
  323. else
  324. drmCloseBufferHandle(dev->fd, handle);
  325. return r;
  326. unlock:
  327. pthread_mutex_unlock(&dev->bo_table_mutex);
  328. return r;
  329. }
  330. drm_public int amdgpu_bo_free(amdgpu_bo_handle buf_handle)
  331. {
  332. struct amdgpu_device *dev;
  333. struct amdgpu_bo *bo = buf_handle;
  334. assert(bo != NULL);
  335. dev = bo->dev;
  336. pthread_mutex_lock(&dev->bo_table_mutex);
  337. if (update_references(&bo->refcount, NULL)) {
  338. /* Remove the buffer from the hash tables. */
  339. handle_table_remove(&dev->bo_handles, bo->handle);
  340. if (bo->flink_name)
  341. handle_table_remove(&dev->bo_flink_names,
  342. bo->flink_name);
  343. /* Release CPU access. */
  344. if (bo->cpu_map_count > 0) {
  345. bo->cpu_map_count = 1;
  346. amdgpu_bo_cpu_unmap(bo);
  347. }
  348. drmCloseBufferHandle(dev->fd, bo->handle);
  349. pthread_mutex_destroy(&bo->cpu_access_mutex);
  350. free(bo);
  351. }
  352. pthread_mutex_unlock(&dev->bo_table_mutex);
  353. return 0;
  354. }
  355. drm_public void amdgpu_bo_inc_ref(amdgpu_bo_handle bo)
  356. {
  357. atomic_inc(&bo->refcount);
  358. }
  359. drm_public int amdgpu_bo_cpu_map(amdgpu_bo_handle bo, void **cpu)
  360. {
  361. union drm_amdgpu_gem_mmap args;
  362. void *ptr;
  363. int r;
  364. pthread_mutex_lock(&bo->cpu_access_mutex);
  365. if (bo->cpu_ptr) {
  366. /* already mapped */
  367. assert(bo->cpu_map_count > 0);
  368. bo->cpu_map_count++;
  369. *cpu = bo->cpu_ptr;
  370. pthread_mutex_unlock(&bo->cpu_access_mutex);
  371. return 0;
  372. }
  373. assert(bo->cpu_map_count == 0);
  374. memset(&args, 0, sizeof(args));
  375. /* Query the buffer address (args.addr_ptr).
  376. * The kernel driver ignores the offset and size parameters. */
  377. args.in.handle = bo->handle;
  378. r = drmCommandWriteRead(bo->dev->fd, DRM_AMDGPU_GEM_MMAP, &args,
  379. sizeof(args));
  380. if (r) {
  381. pthread_mutex_unlock(&bo->cpu_access_mutex);
  382. return r;
  383. }
  384. /* Map the buffer. */
  385. ptr = drm_mmap(NULL, bo->alloc_size, PROT_READ | PROT_WRITE, MAP_SHARED,
  386. bo->dev->fd, args.out.addr_ptr);
  387. if (ptr == MAP_FAILED) {
  388. pthread_mutex_unlock(&bo->cpu_access_mutex);
  389. return -errno;
  390. }
  391. bo->cpu_ptr = ptr;
  392. bo->cpu_map_count = 1;
  393. pthread_mutex_unlock(&bo->cpu_access_mutex);
  394. *cpu = ptr;
  395. return 0;
  396. }
  397. drm_public int amdgpu_bo_cpu_unmap(amdgpu_bo_handle bo)
  398. {
  399. int r;
  400. pthread_mutex_lock(&bo->cpu_access_mutex);
  401. assert(bo->cpu_map_count >= 0);
  402. if (bo->cpu_map_count == 0) {
  403. /* not mapped */
  404. pthread_mutex_unlock(&bo->cpu_access_mutex);
  405. return -EINVAL;
  406. }
  407. bo->cpu_map_count--;
  408. if (bo->cpu_map_count > 0) {
  409. /* mapped multiple times */
  410. pthread_mutex_unlock(&bo->cpu_access_mutex);
  411. return 0;
  412. }
  413. r = drm_munmap(bo->cpu_ptr, bo->alloc_size) == 0 ? 0 : -errno;
  414. bo->cpu_ptr = NULL;
  415. pthread_mutex_unlock(&bo->cpu_access_mutex);
  416. return r;
  417. }
  418. drm_public int amdgpu_query_buffer_size_alignment(amdgpu_device_handle dev,
  419. struct amdgpu_buffer_size_alignments *info)
  420. {
  421. info->size_local = dev->dev_info.pte_fragment_size;
  422. info->size_remote = dev->dev_info.gart_page_size;
  423. return 0;
  424. }
  425. drm_public int amdgpu_bo_wait_for_idle(amdgpu_bo_handle bo,
  426. uint64_t timeout_ns,
  427. bool *busy)
  428. {
  429. union drm_amdgpu_gem_wait_idle args;
  430. int r;
  431. memset(&args, 0, sizeof(args));
  432. args.in.handle = bo->handle;
  433. args.in.timeout = amdgpu_cs_calculate_timeout(timeout_ns);
  434. r = drmCommandWriteRead(bo->dev->fd, DRM_AMDGPU_GEM_WAIT_IDLE,
  435. &args, sizeof(args));
  436. if (r == 0) {
  437. *busy = args.out.status;
  438. return 0;
  439. } else {
  440. fprintf(stderr, "amdgpu: GEM_WAIT_IDLE failed with %i\n", r);
  441. return r;
  442. }
  443. }
  444. drm_public int amdgpu_find_bo_by_cpu_mapping(amdgpu_device_handle dev,
  445. void *cpu,
  446. uint64_t size,
  447. amdgpu_bo_handle *buf_handle,
  448. uint64_t *offset_in_bo)
  449. {
  450. struct amdgpu_bo *bo = NULL;
  451. uint32_t i;
  452. int r = 0;
  453. if (cpu == NULL || size == 0)
  454. return -EINVAL;
  455. /*
  456. * Workaround for a buggy application which tries to import previously
  457. * exposed CPU pointers. If we find a real world use case we should
  458. * improve that by asking the kernel for the right handle.
  459. */
  460. pthread_mutex_lock(&dev->bo_table_mutex);
  461. for (i = 0; i < dev->bo_handles.max_key; i++) {
  462. bo = handle_table_lookup(&dev->bo_handles, i);
  463. if (!bo || !bo->cpu_ptr || size > bo->alloc_size)
  464. continue;
  465. if (cpu >= bo->cpu_ptr &&
  466. cpu < (void*)((uintptr_t)bo->cpu_ptr + (size_t)bo->alloc_size))
  467. break;
  468. }
  469. if (i < dev->bo_handles.max_key) {
  470. atomic_inc(&bo->refcount);
  471. *buf_handle = bo;
  472. *offset_in_bo = (uintptr_t)cpu - (uintptr_t)bo->cpu_ptr;
  473. } else {
  474. *buf_handle = NULL;
  475. *offset_in_bo = 0;
  476. r = -ENXIO;
  477. }
  478. pthread_mutex_unlock(&dev->bo_table_mutex);
  479. return r;
  480. }
  481. drm_public int amdgpu_create_bo_from_user_mem(amdgpu_device_handle dev,
  482. void *cpu,
  483. uint64_t size,
  484. amdgpu_bo_handle *buf_handle)
  485. {
  486. int r;
  487. struct drm_amdgpu_gem_userptr args;
  488. args.addr = (uintptr_t)cpu;
  489. args.flags = AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_REGISTER |
  490. AMDGPU_GEM_USERPTR_VALIDATE;
  491. args.size = size;
  492. r = drmCommandWriteRead(dev->fd, DRM_AMDGPU_GEM_USERPTR,
  493. &args, sizeof(args));
  494. if (r)
  495. goto out;
  496. pthread_mutex_lock(&dev->bo_table_mutex);
  497. r = amdgpu_bo_create(dev, size, args.handle, buf_handle);
  498. pthread_mutex_unlock(&dev->bo_table_mutex);
  499. if (r) {
  500. drmCloseBufferHandle(dev->fd, args.handle);
  501. }
  502. out:
  503. return r;
  504. }
  505. drm_public int amdgpu_bo_list_create_raw(amdgpu_device_handle dev,
  506. uint32_t number_of_buffers,
  507. struct drm_amdgpu_bo_list_entry *buffers,
  508. uint32_t *result)
  509. {
  510. union drm_amdgpu_bo_list args;
  511. int r;
  512. memset(&args, 0, sizeof(args));
  513. args.in.operation = AMDGPU_BO_LIST_OP_CREATE;
  514. args.in.bo_number = number_of_buffers;
  515. args.in.bo_info_size = sizeof(struct drm_amdgpu_bo_list_entry);
  516. args.in.bo_info_ptr = (uint64_t)(uintptr_t)buffers;
  517. r = drmCommandWriteRead(dev->fd, DRM_AMDGPU_BO_LIST,
  518. &args, sizeof(args));
  519. if (!r)
  520. *result = args.out.list_handle;
  521. return r;
  522. }
  523. drm_public int amdgpu_bo_list_destroy_raw(amdgpu_device_handle dev,
  524. uint32_t bo_list)
  525. {
  526. union drm_amdgpu_bo_list args;
  527. memset(&args, 0, sizeof(args));
  528. args.in.operation = AMDGPU_BO_LIST_OP_DESTROY;
  529. args.in.list_handle = bo_list;
  530. return drmCommandWriteRead(dev->fd, DRM_AMDGPU_BO_LIST,
  531. &args, sizeof(args));
  532. }
  533. drm_public int amdgpu_bo_list_create(amdgpu_device_handle dev,
  534. uint32_t number_of_resources,
  535. amdgpu_bo_handle *resources,
  536. uint8_t *resource_prios,
  537. amdgpu_bo_list_handle *result)
  538. {
  539. struct drm_amdgpu_bo_list_entry *list;
  540. union drm_amdgpu_bo_list args;
  541. unsigned i;
  542. int r;
  543. if (!number_of_resources || !resources)
  544. return -EINVAL;
  545. /* overflow check for multiplication */
  546. if (number_of_resources > UINT32_MAX / sizeof(struct drm_amdgpu_bo_list_entry))
  547. return -EINVAL;
  548. list = malloc(number_of_resources * sizeof(struct drm_amdgpu_bo_list_entry));
  549. if (!list)
  550. return -ENOMEM;
  551. *result = malloc(sizeof(struct amdgpu_bo_list));
  552. if (!*result) {
  553. free(list);
  554. return -ENOMEM;
  555. }
  556. memset(&args, 0, sizeof(args));
  557. args.in.operation = AMDGPU_BO_LIST_OP_CREATE;
  558. args.in.bo_number = number_of_resources;
  559. args.in.bo_info_size = sizeof(struct drm_amdgpu_bo_list_entry);
  560. args.in.bo_info_ptr = (uint64_t)(uintptr_t)list;
  561. for (i = 0; i < number_of_resources; i++) {
  562. list[i].bo_handle = resources[i]->handle;
  563. if (resource_prios)
  564. list[i].bo_priority = resource_prios[i];
  565. else
  566. list[i].bo_priority = 0;
  567. }
  568. r = drmCommandWriteRead(dev->fd, DRM_AMDGPU_BO_LIST,
  569. &args, sizeof(args));
  570. free(list);
  571. if (r) {
  572. free(*result);
  573. return r;
  574. }
  575. (*result)->dev = dev;
  576. (*result)->handle = args.out.list_handle;
  577. return 0;
  578. }
  579. drm_public int amdgpu_bo_list_destroy(amdgpu_bo_list_handle list)
  580. {
  581. union drm_amdgpu_bo_list args;
  582. int r;
  583. memset(&args, 0, sizeof(args));
  584. args.in.operation = AMDGPU_BO_LIST_OP_DESTROY;
  585. args.in.list_handle = list->handle;
  586. r = drmCommandWriteRead(list->dev->fd, DRM_AMDGPU_BO_LIST,
  587. &args, sizeof(args));
  588. if (!r)
  589. free(list);
  590. return r;
  591. }
  592. drm_public int amdgpu_bo_list_update(amdgpu_bo_list_handle handle,
  593. uint32_t number_of_resources,
  594. amdgpu_bo_handle *resources,
  595. uint8_t *resource_prios)
  596. {
  597. struct drm_amdgpu_bo_list_entry *list;
  598. union drm_amdgpu_bo_list args;
  599. unsigned i;
  600. int r;
  601. if (!number_of_resources)
  602. return -EINVAL;
  603. /* overflow check for multiplication */
  604. if (number_of_resources > UINT32_MAX / sizeof(struct drm_amdgpu_bo_list_entry))
  605. return -EINVAL;
  606. list = malloc(number_of_resources * sizeof(struct drm_amdgpu_bo_list_entry));
  607. if (!list)
  608. return -ENOMEM;
  609. args.in.operation = AMDGPU_BO_LIST_OP_UPDATE;
  610. args.in.list_handle = handle->handle;
  611. args.in.bo_number = number_of_resources;
  612. args.in.bo_info_size = sizeof(struct drm_amdgpu_bo_list_entry);
  613. args.in.bo_info_ptr = (uintptr_t)list;
  614. for (i = 0; i < number_of_resources; i++) {
  615. list[i].bo_handle = resources[i]->handle;
  616. if (resource_prios)
  617. list[i].bo_priority = resource_prios[i];
  618. else
  619. list[i].bo_priority = 0;
  620. }
  621. r = drmCommandWriteRead(handle->dev->fd, DRM_AMDGPU_BO_LIST,
  622. &args, sizeof(args));
  623. free(list);
  624. return r;
  625. }
  626. drm_public int amdgpu_bo_va_op(amdgpu_bo_handle bo,
  627. uint64_t offset,
  628. uint64_t size,
  629. uint64_t addr,
  630. uint64_t flags,
  631. uint32_t ops)
  632. {
  633. amdgpu_device_handle dev = bo->dev;
  634. size = ALIGN(size, getpagesize());
  635. return amdgpu_bo_va_op_raw(dev, bo, offset, size, addr,
  636. AMDGPU_VM_PAGE_READABLE |
  637. AMDGPU_VM_PAGE_WRITEABLE |
  638. AMDGPU_VM_PAGE_EXECUTABLE, ops);
  639. }
  640. drm_public int amdgpu_bo_va_op_raw(amdgpu_device_handle dev,
  641. amdgpu_bo_handle bo,
  642. uint64_t offset,
  643. uint64_t size,
  644. uint64_t addr,
  645. uint64_t flags,
  646. uint32_t ops)
  647. {
  648. struct drm_amdgpu_gem_va va;
  649. int r;
  650. if (ops != AMDGPU_VA_OP_MAP && ops != AMDGPU_VA_OP_UNMAP &&
  651. ops != AMDGPU_VA_OP_REPLACE && ops != AMDGPU_VA_OP_CLEAR)
  652. return -EINVAL;
  653. memset(&va, 0, sizeof(va));
  654. va.handle = bo ? bo->handle : 0;
  655. va.operation = ops;
  656. va.flags = flags;
  657. va.va_address = addr;
  658. va.offset_in_bo = offset;
  659. va.map_size = size;
  660. r = drmCommandWriteRead(dev->fd, DRM_AMDGPU_GEM_VA, &va, sizeof(va));
  661. return r;
  662. }
  663. drm_public int amdgpu_bo_va_op_raw2(amdgpu_device_handle dev,
  664. amdgpu_bo_handle bo,
  665. uint64_t offset,
  666. uint64_t size,
  667. uint64_t addr,
  668. uint64_t flags,
  669. uint32_t ops,
  670. uint32_t vm_timeline_syncobj_out,
  671. uint64_t vm_timeline_point,
  672. uint64_t input_fence_syncobj_handles,
  673. uint32_t num_syncobj_handles)
  674. {
  675. struct drm_amdgpu_gem_va va;
  676. int r;
  677. if (ops != AMDGPU_VA_OP_MAP && ops != AMDGPU_VA_OP_UNMAP &&
  678. ops != AMDGPU_VA_OP_REPLACE && ops != AMDGPU_VA_OP_CLEAR)
  679. return -EINVAL;
  680. memset(&va, 0, sizeof(va));
  681. va.handle = bo ? bo->handle : 0;
  682. va.operation = ops;
  683. va.flags = flags;
  684. va.va_address = addr;
  685. va.offset_in_bo = offset;
  686. va.map_size = size;
  687. va.vm_timeline_syncobj_out = vm_timeline_syncobj_out;
  688. va.vm_timeline_point = vm_timeline_point;
  689. va.input_fence_syncobj_handles = input_fence_syncobj_handles;
  690. va.num_syncobj_handles = num_syncobj_handles;
  691. r = drmCommandWriteRead(dev->fd, DRM_AMDGPU_GEM_VA, &va, sizeof(va));
  692. return r;
  693. }