amdgpu_vce.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. /*
  2. * Copyright 2013 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
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sub license, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  16. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  17. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  18. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  19. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. *
  21. * The above copyright notice and this permission notice (including the
  22. * next paragraph) shall be included in all copies or substantial portions
  23. * of the Software.
  24. *
  25. * Authors: Christian König <christian.koenig@amd.com>
  26. */
  27. #include <linux/firmware.h>
  28. #include <linux/module.h>
  29. #include <drm/drmP.h>
  30. #include <drm/drm.h>
  31. #include "amdgpu.h"
  32. #include "amdgpu_pm.h"
  33. #include "amdgpu_vce.h"
  34. #include "cikd.h"
  35. /* 1 second timeout */
  36. #define VCE_IDLE_TIMEOUT_MS 1000
  37. /* Firmware Names */
  38. #ifdef CONFIG_DRM_AMDGPU_CIK
  39. #define FIRMWARE_BONAIRE "radeon/bonaire_vce.bin"
  40. #define FIRMWARE_KABINI "radeon/kabini_vce.bin"
  41. #define FIRMWARE_KAVERI "radeon/kaveri_vce.bin"
  42. #define FIRMWARE_HAWAII "radeon/hawaii_vce.bin"
  43. #define FIRMWARE_MULLINS "radeon/mullins_vce.bin"
  44. #endif
  45. #define FIRMWARE_TONGA "amdgpu/tonga_vce.bin"
  46. #define FIRMWARE_CARRIZO "amdgpu/carrizo_vce.bin"
  47. #ifdef CONFIG_DRM_AMDGPU_CIK
  48. MODULE_FIRMWARE(FIRMWARE_BONAIRE);
  49. MODULE_FIRMWARE(FIRMWARE_KABINI);
  50. MODULE_FIRMWARE(FIRMWARE_KAVERI);
  51. MODULE_FIRMWARE(FIRMWARE_HAWAII);
  52. MODULE_FIRMWARE(FIRMWARE_MULLINS);
  53. #endif
  54. MODULE_FIRMWARE(FIRMWARE_TONGA);
  55. MODULE_FIRMWARE(FIRMWARE_CARRIZO);
  56. static void amdgpu_vce_idle_work_handler(struct work_struct *work);
  57. /**
  58. * amdgpu_vce_init - allocate memory, load vce firmware
  59. *
  60. * @adev: amdgpu_device pointer
  61. *
  62. * First step to get VCE online, allocate memory and load the firmware
  63. */
  64. int amdgpu_vce_sw_init(struct amdgpu_device *adev)
  65. {
  66. unsigned long size;
  67. const char *fw_name;
  68. const struct common_firmware_header *hdr;
  69. unsigned ucode_version, version_major, version_minor, binary_id;
  70. int i, r;
  71. INIT_DELAYED_WORK(&adev->vce.idle_work, amdgpu_vce_idle_work_handler);
  72. switch (adev->asic_type) {
  73. #ifdef CONFIG_DRM_AMDGPU_CIK
  74. case CHIP_BONAIRE:
  75. fw_name = FIRMWARE_BONAIRE;
  76. break;
  77. case CHIP_KAVERI:
  78. fw_name = FIRMWARE_KAVERI;
  79. break;
  80. case CHIP_KABINI:
  81. fw_name = FIRMWARE_KABINI;
  82. break;
  83. case CHIP_HAWAII:
  84. fw_name = FIRMWARE_HAWAII;
  85. break;
  86. case CHIP_MULLINS:
  87. fw_name = FIRMWARE_MULLINS;
  88. break;
  89. #endif
  90. case CHIP_TONGA:
  91. fw_name = FIRMWARE_TONGA;
  92. break;
  93. case CHIP_CARRIZO:
  94. fw_name = FIRMWARE_CARRIZO;
  95. break;
  96. default:
  97. return -EINVAL;
  98. }
  99. r = request_firmware(&adev->vce.fw, fw_name, adev->dev);
  100. if (r) {
  101. dev_err(adev->dev, "amdgpu_vce: Can't load firmware \"%s\"\n",
  102. fw_name);
  103. return r;
  104. }
  105. r = amdgpu_ucode_validate(adev->vce.fw);
  106. if (r) {
  107. dev_err(adev->dev, "amdgpu_vce: Can't validate firmware \"%s\"\n",
  108. fw_name);
  109. release_firmware(adev->vce.fw);
  110. adev->vce.fw = NULL;
  111. return r;
  112. }
  113. hdr = (const struct common_firmware_header *)adev->vce.fw->data;
  114. ucode_version = le32_to_cpu(hdr->ucode_version);
  115. version_major = (ucode_version >> 20) & 0xfff;
  116. version_minor = (ucode_version >> 8) & 0xfff;
  117. binary_id = ucode_version & 0xff;
  118. DRM_INFO("Found VCE firmware Version: %hhd.%hhd Binary ID: %hhd\n",
  119. version_major, version_minor, binary_id);
  120. adev->vce.fw_version = ((version_major << 24) | (version_minor << 16) |
  121. (binary_id << 8));
  122. /* allocate firmware, stack and heap BO */
  123. size = AMDGPU_GPU_PAGE_ALIGN(le32_to_cpu(hdr->ucode_size_bytes)) +
  124. AMDGPU_VCE_STACK_SIZE + AMDGPU_VCE_HEAP_SIZE;
  125. r = amdgpu_bo_create(adev, size, PAGE_SIZE, true,
  126. AMDGPU_GEM_DOMAIN_VRAM, 0, NULL, &adev->vce.vcpu_bo);
  127. if (r) {
  128. dev_err(adev->dev, "(%d) failed to allocate VCE bo\n", r);
  129. return r;
  130. }
  131. r = amdgpu_bo_reserve(adev->vce.vcpu_bo, false);
  132. if (r) {
  133. amdgpu_bo_unref(&adev->vce.vcpu_bo);
  134. dev_err(adev->dev, "(%d) failed to reserve VCE bo\n", r);
  135. return r;
  136. }
  137. r = amdgpu_bo_pin(adev->vce.vcpu_bo, AMDGPU_GEM_DOMAIN_VRAM,
  138. &adev->vce.gpu_addr);
  139. amdgpu_bo_unreserve(adev->vce.vcpu_bo);
  140. if (r) {
  141. amdgpu_bo_unref(&adev->vce.vcpu_bo);
  142. dev_err(adev->dev, "(%d) VCE bo pin failed\n", r);
  143. return r;
  144. }
  145. for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
  146. atomic_set(&adev->vce.handles[i], 0);
  147. adev->vce.filp[i] = NULL;
  148. }
  149. return 0;
  150. }
  151. /**
  152. * amdgpu_vce_fini - free memory
  153. *
  154. * @adev: amdgpu_device pointer
  155. *
  156. * Last step on VCE teardown, free firmware memory
  157. */
  158. int amdgpu_vce_sw_fini(struct amdgpu_device *adev)
  159. {
  160. if (adev->vce.vcpu_bo == NULL)
  161. return 0;
  162. amdgpu_bo_unref(&adev->vce.vcpu_bo);
  163. amdgpu_ring_fini(&adev->vce.ring[0]);
  164. amdgpu_ring_fini(&adev->vce.ring[1]);
  165. release_firmware(adev->vce.fw);
  166. return 0;
  167. }
  168. /**
  169. * amdgpu_vce_suspend - unpin VCE fw memory
  170. *
  171. * @adev: amdgpu_device pointer
  172. *
  173. */
  174. int amdgpu_vce_suspend(struct amdgpu_device *adev)
  175. {
  176. int i;
  177. if (adev->vce.vcpu_bo == NULL)
  178. return 0;
  179. for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
  180. if (atomic_read(&adev->vce.handles[i]))
  181. break;
  182. if (i == AMDGPU_MAX_VCE_HANDLES)
  183. return 0;
  184. /* TODO: suspending running encoding sessions isn't supported */
  185. return -EINVAL;
  186. }
  187. /**
  188. * amdgpu_vce_resume - pin VCE fw memory
  189. *
  190. * @adev: amdgpu_device pointer
  191. *
  192. */
  193. int amdgpu_vce_resume(struct amdgpu_device *adev)
  194. {
  195. void *cpu_addr;
  196. const struct common_firmware_header *hdr;
  197. unsigned offset;
  198. int r;
  199. if (adev->vce.vcpu_bo == NULL)
  200. return -EINVAL;
  201. r = amdgpu_bo_reserve(adev->vce.vcpu_bo, false);
  202. if (r) {
  203. dev_err(adev->dev, "(%d) failed to reserve VCE bo\n", r);
  204. return r;
  205. }
  206. r = amdgpu_bo_kmap(adev->vce.vcpu_bo, &cpu_addr);
  207. if (r) {
  208. amdgpu_bo_unreserve(adev->vce.vcpu_bo);
  209. dev_err(adev->dev, "(%d) VCE map failed\n", r);
  210. return r;
  211. }
  212. hdr = (const struct common_firmware_header *)adev->vce.fw->data;
  213. offset = le32_to_cpu(hdr->ucode_array_offset_bytes);
  214. memcpy(cpu_addr, (adev->vce.fw->data) + offset,
  215. (adev->vce.fw->size) - offset);
  216. amdgpu_bo_kunmap(adev->vce.vcpu_bo);
  217. amdgpu_bo_unreserve(adev->vce.vcpu_bo);
  218. return 0;
  219. }
  220. /**
  221. * amdgpu_vce_idle_work_handler - power off VCE
  222. *
  223. * @work: pointer to work structure
  224. *
  225. * power of VCE when it's not used any more
  226. */
  227. static void amdgpu_vce_idle_work_handler(struct work_struct *work)
  228. {
  229. struct amdgpu_device *adev =
  230. container_of(work, struct amdgpu_device, vce.idle_work.work);
  231. if ((amdgpu_fence_count_emitted(&adev->vce.ring[0]) == 0) &&
  232. (amdgpu_fence_count_emitted(&adev->vce.ring[1]) == 0)) {
  233. if (adev->pm.dpm_enabled) {
  234. amdgpu_dpm_enable_vce(adev, false);
  235. } else {
  236. amdgpu_asic_set_vce_clocks(adev, 0, 0);
  237. }
  238. } else {
  239. schedule_delayed_work(&adev->vce.idle_work,
  240. msecs_to_jiffies(VCE_IDLE_TIMEOUT_MS));
  241. }
  242. }
  243. /**
  244. * amdgpu_vce_note_usage - power up VCE
  245. *
  246. * @adev: amdgpu_device pointer
  247. *
  248. * Make sure VCE is powerd up when we want to use it
  249. */
  250. static void amdgpu_vce_note_usage(struct amdgpu_device *adev)
  251. {
  252. bool streams_changed = false;
  253. bool set_clocks = !cancel_delayed_work_sync(&adev->vce.idle_work);
  254. set_clocks &= schedule_delayed_work(&adev->vce.idle_work,
  255. msecs_to_jiffies(VCE_IDLE_TIMEOUT_MS));
  256. if (adev->pm.dpm_enabled) {
  257. /* XXX figure out if the streams changed */
  258. streams_changed = false;
  259. }
  260. if (set_clocks || streams_changed) {
  261. if (adev->pm.dpm_enabled) {
  262. amdgpu_dpm_enable_vce(adev, true);
  263. } else {
  264. amdgpu_asic_set_vce_clocks(adev, 53300, 40000);
  265. }
  266. }
  267. }
  268. /**
  269. * amdgpu_vce_free_handles - free still open VCE handles
  270. *
  271. * @adev: amdgpu_device pointer
  272. * @filp: drm file pointer
  273. *
  274. * Close all VCE handles still open by this file pointer
  275. */
  276. void amdgpu_vce_free_handles(struct amdgpu_device *adev, struct drm_file *filp)
  277. {
  278. struct amdgpu_ring *ring = &adev->vce.ring[0];
  279. int i, r;
  280. for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
  281. uint32_t handle = atomic_read(&adev->vce.handles[i]);
  282. if (!handle || adev->vce.filp[i] != filp)
  283. continue;
  284. amdgpu_vce_note_usage(adev);
  285. r = amdgpu_vce_get_destroy_msg(ring, handle, NULL);
  286. if (r)
  287. DRM_ERROR("Error destroying VCE handle (%d)!\n", r);
  288. adev->vce.filp[i] = NULL;
  289. atomic_set(&adev->vce.handles[i], 0);
  290. }
  291. }
  292. /**
  293. * amdgpu_vce_get_create_msg - generate a VCE create msg
  294. *
  295. * @adev: amdgpu_device pointer
  296. * @ring: ring we should submit the msg to
  297. * @handle: VCE session handle to use
  298. * @fence: optional fence to return
  299. *
  300. * Open up a stream for HW test
  301. */
  302. int amdgpu_vce_get_create_msg(struct amdgpu_ring *ring, uint32_t handle,
  303. struct amdgpu_fence **fence)
  304. {
  305. const unsigned ib_size_dw = 1024;
  306. struct amdgpu_ib ib;
  307. uint64_t dummy;
  308. int i, r;
  309. r = amdgpu_ib_get(ring, NULL, ib_size_dw * 4, &ib);
  310. if (r) {
  311. DRM_ERROR("amdgpu: failed to get ib (%d).\n", r);
  312. return r;
  313. }
  314. dummy = ib.gpu_addr + 1024;
  315. /* stitch together an VCE create msg */
  316. ib.length_dw = 0;
  317. ib.ptr[ib.length_dw++] = 0x0000000c; /* len */
  318. ib.ptr[ib.length_dw++] = 0x00000001; /* session cmd */
  319. ib.ptr[ib.length_dw++] = handle;
  320. ib.ptr[ib.length_dw++] = 0x00000030; /* len */
  321. ib.ptr[ib.length_dw++] = 0x01000001; /* create cmd */
  322. ib.ptr[ib.length_dw++] = 0x00000000;
  323. ib.ptr[ib.length_dw++] = 0x00000042;
  324. ib.ptr[ib.length_dw++] = 0x0000000a;
  325. ib.ptr[ib.length_dw++] = 0x00000001;
  326. ib.ptr[ib.length_dw++] = 0x00000080;
  327. ib.ptr[ib.length_dw++] = 0x00000060;
  328. ib.ptr[ib.length_dw++] = 0x00000100;
  329. ib.ptr[ib.length_dw++] = 0x00000100;
  330. ib.ptr[ib.length_dw++] = 0x0000000c;
  331. ib.ptr[ib.length_dw++] = 0x00000000;
  332. ib.ptr[ib.length_dw++] = 0x00000014; /* len */
  333. ib.ptr[ib.length_dw++] = 0x05000005; /* feedback buffer */
  334. ib.ptr[ib.length_dw++] = upper_32_bits(dummy);
  335. ib.ptr[ib.length_dw++] = dummy;
  336. ib.ptr[ib.length_dw++] = 0x00000001;
  337. for (i = ib.length_dw; i < ib_size_dw; ++i)
  338. ib.ptr[i] = 0x0;
  339. r = amdgpu_ib_schedule(ring->adev, 1, &ib, AMDGPU_FENCE_OWNER_UNDEFINED);
  340. if (r) {
  341. DRM_ERROR("amdgpu: failed to schedule ib (%d).\n", r);
  342. }
  343. if (fence)
  344. *fence = amdgpu_fence_ref(ib.fence);
  345. amdgpu_ib_free(ring->adev, &ib);
  346. return r;
  347. }
  348. /**
  349. * amdgpu_vce_get_destroy_msg - generate a VCE destroy msg
  350. *
  351. * @adev: amdgpu_device pointer
  352. * @ring: ring we should submit the msg to
  353. * @handle: VCE session handle to use
  354. * @fence: optional fence to return
  355. *
  356. * Close up a stream for HW test or if userspace failed to do so
  357. */
  358. int amdgpu_vce_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
  359. struct amdgpu_fence **fence)
  360. {
  361. const unsigned ib_size_dw = 1024;
  362. struct amdgpu_ib ib;
  363. uint64_t dummy;
  364. int i, r;
  365. r = amdgpu_ib_get(ring, NULL, ib_size_dw * 4, &ib);
  366. if (r) {
  367. DRM_ERROR("amdgpu: failed to get ib (%d).\n", r);
  368. return r;
  369. }
  370. dummy = ib.gpu_addr + 1024;
  371. /* stitch together an VCE destroy msg */
  372. ib.length_dw = 0;
  373. ib.ptr[ib.length_dw++] = 0x0000000c; /* len */
  374. ib.ptr[ib.length_dw++] = 0x00000001; /* session cmd */
  375. ib.ptr[ib.length_dw++] = handle;
  376. ib.ptr[ib.length_dw++] = 0x00000014; /* len */
  377. ib.ptr[ib.length_dw++] = 0x05000005; /* feedback buffer */
  378. ib.ptr[ib.length_dw++] = upper_32_bits(dummy);
  379. ib.ptr[ib.length_dw++] = dummy;
  380. ib.ptr[ib.length_dw++] = 0x00000001;
  381. ib.ptr[ib.length_dw++] = 0x00000008; /* len */
  382. ib.ptr[ib.length_dw++] = 0x02000001; /* destroy cmd */
  383. for (i = ib.length_dw; i < ib_size_dw; ++i)
  384. ib.ptr[i] = 0x0;
  385. r = amdgpu_ib_schedule(ring->adev, 1, &ib, AMDGPU_FENCE_OWNER_UNDEFINED);
  386. if (r) {
  387. DRM_ERROR("amdgpu: failed to schedule ib (%d).\n", r);
  388. }
  389. if (fence)
  390. *fence = amdgpu_fence_ref(ib.fence);
  391. amdgpu_ib_free(ring->adev, &ib);
  392. return r;
  393. }
  394. /**
  395. * amdgpu_vce_cs_reloc - command submission relocation
  396. *
  397. * @p: parser context
  398. * @lo: address of lower dword
  399. * @hi: address of higher dword
  400. *
  401. * Patch relocation inside command stream with real buffer address
  402. */
  403. int amdgpu_vce_cs_reloc(struct amdgpu_cs_parser *p, uint32_t ib_idx, int lo, int hi)
  404. {
  405. struct amdgpu_bo_va_mapping *mapping;
  406. struct amdgpu_ib *ib = &p->ibs[ib_idx];
  407. struct amdgpu_bo *bo;
  408. uint64_t addr;
  409. addr = ((uint64_t)amdgpu_get_ib_value(p, ib_idx, lo)) |
  410. ((uint64_t)amdgpu_get_ib_value(p, ib_idx, hi)) << 32;
  411. mapping = amdgpu_cs_find_mapping(p, addr, &bo);
  412. if (mapping == NULL) {
  413. DRM_ERROR("Can't find BO for addr 0x%010Lx %d %d\n",
  414. addr, lo, hi);
  415. return -EINVAL;
  416. }
  417. addr -= ((uint64_t)mapping->it.start) * AMDGPU_GPU_PAGE_SIZE;
  418. addr += amdgpu_bo_gpu_offset(bo);
  419. ib->ptr[lo] = addr & 0xFFFFFFFF;
  420. ib->ptr[hi] = addr >> 32;
  421. return 0;
  422. }
  423. /**
  424. * amdgpu_vce_cs_parse - parse and validate the command stream
  425. *
  426. * @p: parser context
  427. *
  428. */
  429. int amdgpu_vce_ring_parse_cs(struct amdgpu_cs_parser *p, uint32_t ib_idx)
  430. {
  431. uint32_t handle = 0;
  432. bool destroy = false;
  433. int i, r, idx = 0;
  434. struct amdgpu_ib *ib = &p->ibs[ib_idx];
  435. amdgpu_vce_note_usage(p->adev);
  436. while (idx < ib->length_dw) {
  437. uint32_t len = amdgpu_get_ib_value(p, ib_idx, idx);
  438. uint32_t cmd = amdgpu_get_ib_value(p, ib_idx, idx + 1);
  439. if ((len < 8) || (len & 3)) {
  440. DRM_ERROR("invalid VCE command length (%d)!\n", len);
  441. return -EINVAL;
  442. }
  443. switch (cmd) {
  444. case 0x00000001: // session
  445. handle = amdgpu_get_ib_value(p, ib_idx, idx + 2);
  446. break;
  447. case 0x00000002: // task info
  448. case 0x01000001: // create
  449. case 0x04000001: // config extension
  450. case 0x04000002: // pic control
  451. case 0x04000005: // rate control
  452. case 0x04000007: // motion estimation
  453. case 0x04000008: // rdo
  454. case 0x04000009: // vui
  455. case 0x05000002: // auxiliary buffer
  456. break;
  457. case 0x03000001: // encode
  458. r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 10, idx + 9);
  459. if (r)
  460. return r;
  461. r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 12, idx + 11);
  462. if (r)
  463. return r;
  464. break;
  465. case 0x02000001: // destroy
  466. destroy = true;
  467. break;
  468. case 0x05000001: // context buffer
  469. case 0x05000004: // video bitstream buffer
  470. case 0x05000005: // feedback buffer
  471. r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2);
  472. if (r)
  473. return r;
  474. break;
  475. default:
  476. DRM_ERROR("invalid VCE command (0x%x)!\n", cmd);
  477. return -EINVAL;
  478. }
  479. idx += len / 4;
  480. }
  481. if (destroy) {
  482. /* IB contains a destroy msg, free the handle */
  483. for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
  484. atomic_cmpxchg(&p->adev->vce.handles[i], handle, 0);
  485. return 0;
  486. }
  487. /* create or encode, validate the handle */
  488. for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
  489. if (atomic_read(&p->adev->vce.handles[i]) == handle)
  490. return 0;
  491. }
  492. /* handle not found try to alloc a new one */
  493. for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
  494. if (!atomic_cmpxchg(&p->adev->vce.handles[i], 0, handle)) {
  495. p->adev->vce.filp[i] = p->filp;
  496. return 0;
  497. }
  498. }
  499. DRM_ERROR("No more free VCE handles!\n");
  500. return -EINVAL;
  501. }
  502. /**
  503. * amdgpu_vce_ring_emit_semaphore - emit a semaphore command
  504. *
  505. * @ring: engine to use
  506. * @semaphore: address of semaphore
  507. * @emit_wait: true=emit wait, false=emit signal
  508. *
  509. */
  510. bool amdgpu_vce_ring_emit_semaphore(struct amdgpu_ring *ring,
  511. struct amdgpu_semaphore *semaphore,
  512. bool emit_wait)
  513. {
  514. uint64_t addr = semaphore->gpu_addr;
  515. amdgpu_ring_write(ring, VCE_CMD_SEMAPHORE);
  516. amdgpu_ring_write(ring, (addr >> 3) & 0x000FFFFF);
  517. amdgpu_ring_write(ring, (addr >> 23) & 0x000FFFFF);
  518. amdgpu_ring_write(ring, 0x01003000 | (emit_wait ? 1 : 0));
  519. if (!emit_wait)
  520. amdgpu_ring_write(ring, VCE_CMD_END);
  521. return true;
  522. }
  523. /**
  524. * amdgpu_vce_ring_emit_ib - execute indirect buffer
  525. *
  526. * @ring: engine to use
  527. * @ib: the IB to execute
  528. *
  529. */
  530. void amdgpu_vce_ring_emit_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib)
  531. {
  532. amdgpu_ring_write(ring, VCE_CMD_IB);
  533. amdgpu_ring_write(ring, lower_32_bits(ib->gpu_addr));
  534. amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
  535. amdgpu_ring_write(ring, ib->length_dw);
  536. }
  537. /**
  538. * amdgpu_vce_ring_emit_fence - add a fence command to the ring
  539. *
  540. * @ring: engine to use
  541. * @fence: the fence
  542. *
  543. */
  544. void amdgpu_vce_ring_emit_fence(struct amdgpu_ring *ring, u64 addr, u64 seq,
  545. bool write64bits)
  546. {
  547. WARN_ON(write64bits);
  548. amdgpu_ring_write(ring, VCE_CMD_FENCE);
  549. amdgpu_ring_write(ring, addr);
  550. amdgpu_ring_write(ring, upper_32_bits(addr));
  551. amdgpu_ring_write(ring, seq);
  552. amdgpu_ring_write(ring, VCE_CMD_TRAP);
  553. amdgpu_ring_write(ring, VCE_CMD_END);
  554. }
  555. /**
  556. * amdgpu_vce_ring_test_ring - test if VCE ring is working
  557. *
  558. * @ring: the engine to test on
  559. *
  560. */
  561. int amdgpu_vce_ring_test_ring(struct amdgpu_ring *ring)
  562. {
  563. struct amdgpu_device *adev = ring->adev;
  564. uint32_t rptr = amdgpu_ring_get_rptr(ring);
  565. unsigned i;
  566. int r;
  567. r = amdgpu_ring_lock(ring, 16);
  568. if (r) {
  569. DRM_ERROR("amdgpu: vce failed to lock ring %d (%d).\n",
  570. ring->idx, r);
  571. return r;
  572. }
  573. amdgpu_ring_write(ring, VCE_CMD_END);
  574. amdgpu_ring_unlock_commit(ring);
  575. for (i = 0; i < adev->usec_timeout; i++) {
  576. if (amdgpu_ring_get_rptr(ring) != rptr)
  577. break;
  578. DRM_UDELAY(1);
  579. }
  580. if (i < adev->usec_timeout) {
  581. DRM_INFO("ring test on %d succeeded in %d usecs\n",
  582. ring->idx, i);
  583. } else {
  584. DRM_ERROR("amdgpu: ring %d test failed\n",
  585. ring->idx);
  586. r = -ETIMEDOUT;
  587. }
  588. return r;
  589. }
  590. /**
  591. * amdgpu_vce_ring_test_ib - test if VCE IBs are working
  592. *
  593. * @ring: the engine to test on
  594. *
  595. */
  596. int amdgpu_vce_ring_test_ib(struct amdgpu_ring *ring)
  597. {
  598. struct amdgpu_fence *fence = NULL;
  599. int r;
  600. r = amdgpu_vce_get_create_msg(ring, 1, NULL);
  601. if (r) {
  602. DRM_ERROR("amdgpu: failed to get create msg (%d).\n", r);
  603. goto error;
  604. }
  605. r = amdgpu_vce_get_destroy_msg(ring, 1, &fence);
  606. if (r) {
  607. DRM_ERROR("amdgpu: failed to get destroy ib (%d).\n", r);
  608. goto error;
  609. }
  610. r = amdgpu_fence_wait(fence, false);
  611. if (r) {
  612. DRM_ERROR("amdgpu: fence wait failed (%d).\n", r);
  613. } else {
  614. DRM_INFO("ib test on ring %d succeeded\n", ring->idx);
  615. }
  616. error:
  617. amdgpu_fence_unref(&fence);
  618. return r;
  619. }