amdgpu_vce.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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, unsigned long size)
  65. {
  66. const char *fw_name;
  67. const struct common_firmware_header *hdr;
  68. unsigned ucode_version, version_major, version_minor, binary_id;
  69. int i, r;
  70. INIT_DELAYED_WORK(&adev->vce.idle_work, amdgpu_vce_idle_work_handler);
  71. switch (adev->asic_type) {
  72. #ifdef CONFIG_DRM_AMDGPU_CIK
  73. case CHIP_BONAIRE:
  74. fw_name = FIRMWARE_BONAIRE;
  75. break;
  76. case CHIP_KAVERI:
  77. fw_name = FIRMWARE_KAVERI;
  78. break;
  79. case CHIP_KABINI:
  80. fw_name = FIRMWARE_KABINI;
  81. break;
  82. case CHIP_HAWAII:
  83. fw_name = FIRMWARE_HAWAII;
  84. break;
  85. case CHIP_MULLINS:
  86. fw_name = FIRMWARE_MULLINS;
  87. break;
  88. #endif
  89. case CHIP_TONGA:
  90. fw_name = FIRMWARE_TONGA;
  91. break;
  92. case CHIP_CARRIZO:
  93. fw_name = FIRMWARE_CARRIZO;
  94. break;
  95. default:
  96. return -EINVAL;
  97. }
  98. r = request_firmware(&adev->vce.fw, fw_name, adev->dev);
  99. if (r) {
  100. dev_err(adev->dev, "amdgpu_vce: Can't load firmware \"%s\"\n",
  101. fw_name);
  102. return r;
  103. }
  104. r = amdgpu_ucode_validate(adev->vce.fw);
  105. if (r) {
  106. dev_err(adev->dev, "amdgpu_vce: Can't validate firmware \"%s\"\n",
  107. fw_name);
  108. release_firmware(adev->vce.fw);
  109. adev->vce.fw = NULL;
  110. return r;
  111. }
  112. hdr = (const struct common_firmware_header *)adev->vce.fw->data;
  113. ucode_version = le32_to_cpu(hdr->ucode_version);
  114. version_major = (ucode_version >> 20) & 0xfff;
  115. version_minor = (ucode_version >> 8) & 0xfff;
  116. binary_id = ucode_version & 0xff;
  117. DRM_INFO("Found VCE firmware Version: %hhd.%hhd Binary ID: %hhd\n",
  118. version_major, version_minor, binary_id);
  119. adev->vce.fw_version = ((version_major << 24) | (version_minor << 16) |
  120. (binary_id << 8));
  121. /* allocate firmware, stack and heap BO */
  122. r = amdgpu_bo_create(adev, size, PAGE_SIZE, true,
  123. AMDGPU_GEM_DOMAIN_VRAM, 0, NULL, &adev->vce.vcpu_bo);
  124. if (r) {
  125. dev_err(adev->dev, "(%d) failed to allocate VCE bo\n", r);
  126. return r;
  127. }
  128. r = amdgpu_bo_reserve(adev->vce.vcpu_bo, false);
  129. if (r) {
  130. amdgpu_bo_unref(&adev->vce.vcpu_bo);
  131. dev_err(adev->dev, "(%d) failed to reserve VCE bo\n", r);
  132. return r;
  133. }
  134. r = amdgpu_bo_pin(adev->vce.vcpu_bo, AMDGPU_GEM_DOMAIN_VRAM,
  135. &adev->vce.gpu_addr);
  136. amdgpu_bo_unreserve(adev->vce.vcpu_bo);
  137. if (r) {
  138. amdgpu_bo_unref(&adev->vce.vcpu_bo);
  139. dev_err(adev->dev, "(%d) VCE bo pin failed\n", r);
  140. return r;
  141. }
  142. for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
  143. atomic_set(&adev->vce.handles[i], 0);
  144. adev->vce.filp[i] = NULL;
  145. }
  146. return 0;
  147. }
  148. /**
  149. * amdgpu_vce_fini - free memory
  150. *
  151. * @adev: amdgpu_device pointer
  152. *
  153. * Last step on VCE teardown, free firmware memory
  154. */
  155. int amdgpu_vce_sw_fini(struct amdgpu_device *adev)
  156. {
  157. if (adev->vce.vcpu_bo == NULL)
  158. return 0;
  159. amdgpu_bo_unref(&adev->vce.vcpu_bo);
  160. amdgpu_ring_fini(&adev->vce.ring[0]);
  161. amdgpu_ring_fini(&adev->vce.ring[1]);
  162. release_firmware(adev->vce.fw);
  163. return 0;
  164. }
  165. /**
  166. * amdgpu_vce_suspend - unpin VCE fw memory
  167. *
  168. * @adev: amdgpu_device pointer
  169. *
  170. */
  171. int amdgpu_vce_suspend(struct amdgpu_device *adev)
  172. {
  173. int i;
  174. if (adev->vce.vcpu_bo == NULL)
  175. return 0;
  176. for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
  177. if (atomic_read(&adev->vce.handles[i]))
  178. break;
  179. if (i == AMDGPU_MAX_VCE_HANDLES)
  180. return 0;
  181. /* TODO: suspending running encoding sessions isn't supported */
  182. return -EINVAL;
  183. }
  184. /**
  185. * amdgpu_vce_resume - pin VCE fw memory
  186. *
  187. * @adev: amdgpu_device pointer
  188. *
  189. */
  190. int amdgpu_vce_resume(struct amdgpu_device *adev)
  191. {
  192. void *cpu_addr;
  193. const struct common_firmware_header *hdr;
  194. unsigned offset;
  195. int r;
  196. if (adev->vce.vcpu_bo == NULL)
  197. return -EINVAL;
  198. r = amdgpu_bo_reserve(adev->vce.vcpu_bo, false);
  199. if (r) {
  200. dev_err(adev->dev, "(%d) failed to reserve VCE bo\n", r);
  201. return r;
  202. }
  203. r = amdgpu_bo_kmap(adev->vce.vcpu_bo, &cpu_addr);
  204. if (r) {
  205. amdgpu_bo_unreserve(adev->vce.vcpu_bo);
  206. dev_err(adev->dev, "(%d) VCE map failed\n", r);
  207. return r;
  208. }
  209. hdr = (const struct common_firmware_header *)adev->vce.fw->data;
  210. offset = le32_to_cpu(hdr->ucode_array_offset_bytes);
  211. memcpy(cpu_addr, (adev->vce.fw->data) + offset,
  212. (adev->vce.fw->size) - offset);
  213. amdgpu_bo_kunmap(adev->vce.vcpu_bo);
  214. amdgpu_bo_unreserve(adev->vce.vcpu_bo);
  215. return 0;
  216. }
  217. /**
  218. * amdgpu_vce_idle_work_handler - power off VCE
  219. *
  220. * @work: pointer to work structure
  221. *
  222. * power of VCE when it's not used any more
  223. */
  224. static void amdgpu_vce_idle_work_handler(struct work_struct *work)
  225. {
  226. struct amdgpu_device *adev =
  227. container_of(work, struct amdgpu_device, vce.idle_work.work);
  228. if ((amdgpu_fence_count_emitted(&adev->vce.ring[0]) == 0) &&
  229. (amdgpu_fence_count_emitted(&adev->vce.ring[1]) == 0)) {
  230. if (adev->pm.dpm_enabled) {
  231. amdgpu_dpm_enable_vce(adev, false);
  232. } else {
  233. amdgpu_asic_set_vce_clocks(adev, 0, 0);
  234. }
  235. } else {
  236. schedule_delayed_work(&adev->vce.idle_work,
  237. msecs_to_jiffies(VCE_IDLE_TIMEOUT_MS));
  238. }
  239. }
  240. /**
  241. * amdgpu_vce_note_usage - power up VCE
  242. *
  243. * @adev: amdgpu_device pointer
  244. *
  245. * Make sure VCE is powerd up when we want to use it
  246. */
  247. static void amdgpu_vce_note_usage(struct amdgpu_device *adev)
  248. {
  249. bool streams_changed = false;
  250. bool set_clocks = !cancel_delayed_work_sync(&adev->vce.idle_work);
  251. set_clocks &= schedule_delayed_work(&adev->vce.idle_work,
  252. msecs_to_jiffies(VCE_IDLE_TIMEOUT_MS));
  253. if (adev->pm.dpm_enabled) {
  254. /* XXX figure out if the streams changed */
  255. streams_changed = false;
  256. }
  257. if (set_clocks || streams_changed) {
  258. if (adev->pm.dpm_enabled) {
  259. amdgpu_dpm_enable_vce(adev, true);
  260. } else {
  261. amdgpu_asic_set_vce_clocks(adev, 53300, 40000);
  262. }
  263. }
  264. }
  265. /**
  266. * amdgpu_vce_free_handles - free still open VCE handles
  267. *
  268. * @adev: amdgpu_device pointer
  269. * @filp: drm file pointer
  270. *
  271. * Close all VCE handles still open by this file pointer
  272. */
  273. void amdgpu_vce_free_handles(struct amdgpu_device *adev, struct drm_file *filp)
  274. {
  275. struct amdgpu_ring *ring = &adev->vce.ring[0];
  276. int i, r;
  277. for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
  278. uint32_t handle = atomic_read(&adev->vce.handles[i]);
  279. if (!handle || adev->vce.filp[i] != filp)
  280. continue;
  281. amdgpu_vce_note_usage(adev);
  282. r = amdgpu_vce_get_destroy_msg(ring, handle, NULL);
  283. if (r)
  284. DRM_ERROR("Error destroying VCE handle (%d)!\n", r);
  285. adev->vce.filp[i] = NULL;
  286. atomic_set(&adev->vce.handles[i], 0);
  287. }
  288. }
  289. /**
  290. * amdgpu_vce_get_create_msg - generate a VCE create msg
  291. *
  292. * @adev: amdgpu_device pointer
  293. * @ring: ring we should submit the msg to
  294. * @handle: VCE session handle to use
  295. * @fence: optional fence to return
  296. *
  297. * Open up a stream for HW test
  298. */
  299. int amdgpu_vce_get_create_msg(struct amdgpu_ring *ring, uint32_t handle,
  300. struct amdgpu_fence **fence)
  301. {
  302. const unsigned ib_size_dw = 1024;
  303. struct amdgpu_ib ib;
  304. uint64_t dummy;
  305. int i, r;
  306. r = amdgpu_ib_get(ring, NULL, ib_size_dw * 4, &ib);
  307. if (r) {
  308. DRM_ERROR("amdgpu: failed to get ib (%d).\n", r);
  309. return r;
  310. }
  311. dummy = ib.gpu_addr + 1024;
  312. /* stitch together an VCE create msg */
  313. ib.length_dw = 0;
  314. ib.ptr[ib.length_dw++] = 0x0000000c; /* len */
  315. ib.ptr[ib.length_dw++] = 0x00000001; /* session cmd */
  316. ib.ptr[ib.length_dw++] = handle;
  317. ib.ptr[ib.length_dw++] = 0x00000030; /* len */
  318. ib.ptr[ib.length_dw++] = 0x01000001; /* create cmd */
  319. ib.ptr[ib.length_dw++] = 0x00000000;
  320. ib.ptr[ib.length_dw++] = 0x00000042;
  321. ib.ptr[ib.length_dw++] = 0x0000000a;
  322. ib.ptr[ib.length_dw++] = 0x00000001;
  323. ib.ptr[ib.length_dw++] = 0x00000080;
  324. ib.ptr[ib.length_dw++] = 0x00000060;
  325. ib.ptr[ib.length_dw++] = 0x00000100;
  326. ib.ptr[ib.length_dw++] = 0x00000100;
  327. ib.ptr[ib.length_dw++] = 0x0000000c;
  328. ib.ptr[ib.length_dw++] = 0x00000000;
  329. ib.ptr[ib.length_dw++] = 0x00000014; /* len */
  330. ib.ptr[ib.length_dw++] = 0x05000005; /* feedback buffer */
  331. ib.ptr[ib.length_dw++] = upper_32_bits(dummy);
  332. ib.ptr[ib.length_dw++] = dummy;
  333. ib.ptr[ib.length_dw++] = 0x00000001;
  334. for (i = ib.length_dw; i < ib_size_dw; ++i)
  335. ib.ptr[i] = 0x0;
  336. r = amdgpu_ib_schedule(ring->adev, 1, &ib, AMDGPU_FENCE_OWNER_UNDEFINED);
  337. if (r) {
  338. DRM_ERROR("amdgpu: failed to schedule ib (%d).\n", r);
  339. }
  340. if (fence)
  341. *fence = amdgpu_fence_ref(ib.fence);
  342. amdgpu_ib_free(ring->adev, &ib);
  343. return r;
  344. }
  345. /**
  346. * amdgpu_vce_get_destroy_msg - generate a VCE destroy msg
  347. *
  348. * @adev: amdgpu_device pointer
  349. * @ring: ring we should submit the msg to
  350. * @handle: VCE session handle to use
  351. * @fence: optional fence to return
  352. *
  353. * Close up a stream for HW test or if userspace failed to do so
  354. */
  355. int amdgpu_vce_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
  356. struct amdgpu_fence **fence)
  357. {
  358. const unsigned ib_size_dw = 1024;
  359. struct amdgpu_ib ib;
  360. uint64_t dummy;
  361. int i, r;
  362. r = amdgpu_ib_get(ring, NULL, ib_size_dw * 4, &ib);
  363. if (r) {
  364. DRM_ERROR("amdgpu: failed to get ib (%d).\n", r);
  365. return r;
  366. }
  367. dummy = ib.gpu_addr + 1024;
  368. /* stitch together an VCE destroy msg */
  369. ib.length_dw = 0;
  370. ib.ptr[ib.length_dw++] = 0x0000000c; /* len */
  371. ib.ptr[ib.length_dw++] = 0x00000001; /* session cmd */
  372. ib.ptr[ib.length_dw++] = handle;
  373. ib.ptr[ib.length_dw++] = 0x00000014; /* len */
  374. ib.ptr[ib.length_dw++] = 0x05000005; /* feedback buffer */
  375. ib.ptr[ib.length_dw++] = upper_32_bits(dummy);
  376. ib.ptr[ib.length_dw++] = dummy;
  377. ib.ptr[ib.length_dw++] = 0x00000001;
  378. ib.ptr[ib.length_dw++] = 0x00000008; /* len */
  379. ib.ptr[ib.length_dw++] = 0x02000001; /* destroy cmd */
  380. for (i = ib.length_dw; i < ib_size_dw; ++i)
  381. ib.ptr[i] = 0x0;
  382. r = amdgpu_ib_schedule(ring->adev, 1, &ib, AMDGPU_FENCE_OWNER_UNDEFINED);
  383. if (r) {
  384. DRM_ERROR("amdgpu: failed to schedule ib (%d).\n", r);
  385. }
  386. if (fence)
  387. *fence = amdgpu_fence_ref(ib.fence);
  388. amdgpu_ib_free(ring->adev, &ib);
  389. return r;
  390. }
  391. /**
  392. * amdgpu_vce_cs_reloc - command submission relocation
  393. *
  394. * @p: parser context
  395. * @lo: address of lower dword
  396. * @hi: address of higher dword
  397. * @size: minimum size
  398. *
  399. * Patch relocation inside command stream with real buffer address
  400. */
  401. static int amdgpu_vce_cs_reloc(struct amdgpu_cs_parser *p, uint32_t ib_idx,
  402. int lo, int hi, unsigned size)
  403. {
  404. struct amdgpu_bo_va_mapping *mapping;
  405. struct amdgpu_ib *ib = &p->ibs[ib_idx];
  406. struct amdgpu_bo *bo;
  407. uint64_t addr;
  408. addr = ((uint64_t)amdgpu_get_ib_value(p, ib_idx, lo)) |
  409. ((uint64_t)amdgpu_get_ib_value(p, ib_idx, hi)) << 32;
  410. mapping = amdgpu_cs_find_mapping(p, addr, &bo);
  411. if (mapping == NULL) {
  412. DRM_ERROR("Can't find BO for addr 0x%010Lx %d %d\n",
  413. addr, lo, hi);
  414. return -EINVAL;
  415. }
  416. if ((addr + (uint64_t)size) >
  417. ((uint64_t)mapping->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
  418. DRM_ERROR("BO to small for addr 0x%010Lx %d %d\n",
  419. addr, lo, hi);
  420. return -EINVAL;
  421. }
  422. addr -= ((uint64_t)mapping->it.start) * AMDGPU_GPU_PAGE_SIZE;
  423. addr += amdgpu_bo_gpu_offset(bo);
  424. ib->ptr[lo] = addr & 0xFFFFFFFF;
  425. ib->ptr[hi] = addr >> 32;
  426. return 0;
  427. }
  428. /**
  429. * amdgpu_vce_validate_handle - validate stream handle
  430. *
  431. * @p: parser context
  432. * @handle: handle to validate
  433. *
  434. * Validates the handle and return the found session index or -EINVAL
  435. * we we don't have another free session index.
  436. */
  437. static int amdgpu_vce_validate_handle(struct amdgpu_cs_parser *p,
  438. uint32_t handle)
  439. {
  440. unsigned i;
  441. /* validate the handle */
  442. for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
  443. if (atomic_read(&p->adev->vce.handles[i]) == handle)
  444. return i;
  445. }
  446. /* handle not found try to alloc a new one */
  447. for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
  448. if (!atomic_cmpxchg(&p->adev->vce.handles[i], 0, handle)) {
  449. p->adev->vce.filp[i] = p->filp;
  450. p->adev->vce.img_size[i] = 0;
  451. return i;
  452. }
  453. }
  454. DRM_ERROR("No more free VCE handles!\n");
  455. return -EINVAL;
  456. }
  457. /**
  458. * amdgpu_vce_cs_parse - parse and validate the command stream
  459. *
  460. * @p: parser context
  461. *
  462. */
  463. int amdgpu_vce_ring_parse_cs(struct amdgpu_cs_parser *p, uint32_t ib_idx)
  464. {
  465. struct amdgpu_ib *ib = &p->ibs[ib_idx];
  466. int session_idx = -1;
  467. bool destroyed = false;
  468. uint32_t tmp, handle = 0;
  469. uint32_t *size = &tmp;
  470. int i, r, idx = 0;
  471. amdgpu_vce_note_usage(p->adev);
  472. while (idx < ib->length_dw) {
  473. uint32_t len = amdgpu_get_ib_value(p, ib_idx, idx);
  474. uint32_t cmd = amdgpu_get_ib_value(p, ib_idx, idx + 1);
  475. if ((len < 8) || (len & 3)) {
  476. DRM_ERROR("invalid VCE command length (%d)!\n", len);
  477. return -EINVAL;
  478. }
  479. if (destroyed) {
  480. DRM_ERROR("No other command allowed after destroy!\n");
  481. return -EINVAL;
  482. }
  483. switch (cmd) {
  484. case 0x00000001: // session
  485. handle = amdgpu_get_ib_value(p, ib_idx, idx + 2);
  486. session_idx = amdgpu_vce_validate_handle(p, handle);
  487. if (session_idx < 0)
  488. return session_idx;
  489. size = &p->adev->vce.img_size[session_idx];
  490. break;
  491. case 0x00000002: // task info
  492. break;
  493. case 0x01000001: // create
  494. *size = amdgpu_get_ib_value(p, ib_idx, idx + 8) *
  495. amdgpu_get_ib_value(p, ib_idx, idx + 10) *
  496. 8 * 3 / 2;
  497. break;
  498. case 0x04000001: // config extension
  499. case 0x04000002: // pic control
  500. case 0x04000005: // rate control
  501. case 0x04000007: // motion estimation
  502. case 0x04000008: // rdo
  503. case 0x04000009: // vui
  504. case 0x05000002: // auxiliary buffer
  505. break;
  506. case 0x03000001: // encode
  507. r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 10, idx + 9,
  508. *size);
  509. if (r)
  510. return r;
  511. r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 12, idx + 11,
  512. *size / 3);
  513. if (r)
  514. return r;
  515. break;
  516. case 0x02000001: // destroy
  517. destroyed = true;
  518. break;
  519. case 0x05000001: // context buffer
  520. r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
  521. *size * 2);
  522. if (r)
  523. return r;
  524. break;
  525. case 0x05000004: // video bitstream buffer
  526. tmp = amdgpu_get_ib_value(p, ib_idx, idx + 4);
  527. r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
  528. tmp);
  529. if (r)
  530. return r;
  531. break;
  532. case 0x05000005: // feedback buffer
  533. r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
  534. 4096);
  535. if (r)
  536. return r;
  537. break;
  538. default:
  539. DRM_ERROR("invalid VCE command (0x%x)!\n", cmd);
  540. return -EINVAL;
  541. }
  542. if (session_idx == -1) {
  543. DRM_ERROR("no session command at start of IB\n");
  544. return -EINVAL;
  545. }
  546. idx += len / 4;
  547. }
  548. if (destroyed) {
  549. /* IB contains a destroy msg, free the handle */
  550. for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
  551. atomic_cmpxchg(&p->adev->vce.handles[i], handle, 0);
  552. }
  553. return 0;
  554. }
  555. /**
  556. * amdgpu_vce_ring_emit_semaphore - emit a semaphore command
  557. *
  558. * @ring: engine to use
  559. * @semaphore: address of semaphore
  560. * @emit_wait: true=emit wait, false=emit signal
  561. *
  562. */
  563. bool amdgpu_vce_ring_emit_semaphore(struct amdgpu_ring *ring,
  564. struct amdgpu_semaphore *semaphore,
  565. bool emit_wait)
  566. {
  567. uint64_t addr = semaphore->gpu_addr;
  568. amdgpu_ring_write(ring, VCE_CMD_SEMAPHORE);
  569. amdgpu_ring_write(ring, (addr >> 3) & 0x000FFFFF);
  570. amdgpu_ring_write(ring, (addr >> 23) & 0x000FFFFF);
  571. amdgpu_ring_write(ring, 0x01003000 | (emit_wait ? 1 : 0));
  572. if (!emit_wait)
  573. amdgpu_ring_write(ring, VCE_CMD_END);
  574. return true;
  575. }
  576. /**
  577. * amdgpu_vce_ring_emit_ib - execute indirect buffer
  578. *
  579. * @ring: engine to use
  580. * @ib: the IB to execute
  581. *
  582. */
  583. void amdgpu_vce_ring_emit_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib)
  584. {
  585. amdgpu_ring_write(ring, VCE_CMD_IB);
  586. amdgpu_ring_write(ring, lower_32_bits(ib->gpu_addr));
  587. amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
  588. amdgpu_ring_write(ring, ib->length_dw);
  589. }
  590. /**
  591. * amdgpu_vce_ring_emit_fence - add a fence command to the ring
  592. *
  593. * @ring: engine to use
  594. * @fence: the fence
  595. *
  596. */
  597. void amdgpu_vce_ring_emit_fence(struct amdgpu_ring *ring, u64 addr, u64 seq,
  598. unsigned flags)
  599. {
  600. WARN_ON(flags & AMDGPU_FENCE_FLAG_64BIT);
  601. amdgpu_ring_write(ring, VCE_CMD_FENCE);
  602. amdgpu_ring_write(ring, addr);
  603. amdgpu_ring_write(ring, upper_32_bits(addr));
  604. amdgpu_ring_write(ring, seq);
  605. amdgpu_ring_write(ring, VCE_CMD_TRAP);
  606. amdgpu_ring_write(ring, VCE_CMD_END);
  607. }
  608. /**
  609. * amdgpu_vce_ring_test_ring - test if VCE ring is working
  610. *
  611. * @ring: the engine to test on
  612. *
  613. */
  614. int amdgpu_vce_ring_test_ring(struct amdgpu_ring *ring)
  615. {
  616. struct amdgpu_device *adev = ring->adev;
  617. uint32_t rptr = amdgpu_ring_get_rptr(ring);
  618. unsigned i;
  619. int r;
  620. r = amdgpu_ring_lock(ring, 16);
  621. if (r) {
  622. DRM_ERROR("amdgpu: vce failed to lock ring %d (%d).\n",
  623. ring->idx, r);
  624. return r;
  625. }
  626. amdgpu_ring_write(ring, VCE_CMD_END);
  627. amdgpu_ring_unlock_commit(ring);
  628. for (i = 0; i < adev->usec_timeout; i++) {
  629. if (amdgpu_ring_get_rptr(ring) != rptr)
  630. break;
  631. DRM_UDELAY(1);
  632. }
  633. if (i < adev->usec_timeout) {
  634. DRM_INFO("ring test on %d succeeded in %d usecs\n",
  635. ring->idx, i);
  636. } else {
  637. DRM_ERROR("amdgpu: ring %d test failed\n",
  638. ring->idx);
  639. r = -ETIMEDOUT;
  640. }
  641. return r;
  642. }
  643. /**
  644. * amdgpu_vce_ring_test_ib - test if VCE IBs are working
  645. *
  646. * @ring: the engine to test on
  647. *
  648. */
  649. int amdgpu_vce_ring_test_ib(struct amdgpu_ring *ring)
  650. {
  651. struct amdgpu_fence *fence = NULL;
  652. int r;
  653. r = amdgpu_vce_get_create_msg(ring, 1, NULL);
  654. if (r) {
  655. DRM_ERROR("amdgpu: failed to get create msg (%d).\n", r);
  656. goto error;
  657. }
  658. r = amdgpu_vce_get_destroy_msg(ring, 1, &fence);
  659. if (r) {
  660. DRM_ERROR("amdgpu: failed to get destroy ib (%d).\n", r);
  661. goto error;
  662. }
  663. r = amdgpu_fence_wait(fence, false);
  664. if (r) {
  665. DRM_ERROR("amdgpu: fence wait failed (%d).\n", r);
  666. } else {
  667. DRM_INFO("ib test on ring %d succeeded\n", ring->idx);
  668. }
  669. error:
  670. amdgpu_fence_unref(&fence);
  671. return r;
  672. }