amdgpu_vce.c 20 KB

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