amdgpu_vce.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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, uint32_t index)
  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. if (index == 0xffffffff)
  409. index = 0;
  410. addr = ((uint64_t)amdgpu_get_ib_value(p, ib_idx, lo)) |
  411. ((uint64_t)amdgpu_get_ib_value(p, ib_idx, hi)) << 32;
  412. addr += ((uint64_t)size) * ((uint64_t)index);
  413. mapping = amdgpu_cs_find_mapping(p, addr, &bo);
  414. if (mapping == NULL) {
  415. DRM_ERROR("Can't find BO for addr 0x%010Lx %d %d %d %d\n",
  416. addr, lo, hi, size, index);
  417. return -EINVAL;
  418. }
  419. if ((addr + (uint64_t)size) >
  420. ((uint64_t)mapping->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
  421. DRM_ERROR("BO to small for addr 0x%010Lx %d %d\n",
  422. addr, lo, hi);
  423. return -EINVAL;
  424. }
  425. addr -= ((uint64_t)mapping->it.start) * AMDGPU_GPU_PAGE_SIZE;
  426. addr += amdgpu_bo_gpu_offset(bo);
  427. addr -= ((uint64_t)size) * ((uint64_t)index);
  428. ib->ptr[lo] = addr & 0xFFFFFFFF;
  429. ib->ptr[hi] = addr >> 32;
  430. return 0;
  431. }
  432. /**
  433. * amdgpu_vce_validate_handle - validate stream handle
  434. *
  435. * @p: parser context
  436. * @handle: handle to validate
  437. * @allocated: allocated a new handle?
  438. *
  439. * Validates the handle and return the found session index or -EINVAL
  440. * we we don't have another free session index.
  441. */
  442. static int amdgpu_vce_validate_handle(struct amdgpu_cs_parser *p,
  443. uint32_t handle, bool *allocated)
  444. {
  445. unsigned i;
  446. *allocated = false;
  447. /* validate the handle */
  448. for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
  449. if (atomic_read(&p->adev->vce.handles[i]) == handle) {
  450. if (p->adev->vce.filp[i] != p->filp) {
  451. DRM_ERROR("VCE handle collision detected!\n");
  452. return -EINVAL;
  453. }
  454. return i;
  455. }
  456. }
  457. /* handle not found try to alloc a new one */
  458. for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
  459. if (!atomic_cmpxchg(&p->adev->vce.handles[i], 0, handle)) {
  460. p->adev->vce.filp[i] = p->filp;
  461. p->adev->vce.img_size[i] = 0;
  462. *allocated = true;
  463. return i;
  464. }
  465. }
  466. DRM_ERROR("No more free VCE handles!\n");
  467. return -EINVAL;
  468. }
  469. /**
  470. * amdgpu_vce_cs_parse - parse and validate the command stream
  471. *
  472. * @p: parser context
  473. *
  474. */
  475. int amdgpu_vce_ring_parse_cs(struct amdgpu_cs_parser *p, uint32_t ib_idx)
  476. {
  477. struct amdgpu_ib *ib = &p->ibs[ib_idx];
  478. unsigned fb_idx = 0, bs_idx = 0;
  479. int session_idx = -1;
  480. bool destroyed = false;
  481. bool created = false;
  482. bool allocated = false;
  483. uint32_t tmp, handle = 0;
  484. uint32_t *size = &tmp;
  485. int i, r = 0, idx = 0;
  486. amdgpu_vce_note_usage(p->adev);
  487. while (idx < ib->length_dw) {
  488. uint32_t len = amdgpu_get_ib_value(p, ib_idx, idx);
  489. uint32_t cmd = amdgpu_get_ib_value(p, ib_idx, idx + 1);
  490. if ((len < 8) || (len & 3)) {
  491. DRM_ERROR("invalid VCE command length (%d)!\n", len);
  492. r = -EINVAL;
  493. goto out;
  494. }
  495. if (destroyed) {
  496. DRM_ERROR("No other command allowed after destroy!\n");
  497. r = -EINVAL;
  498. goto out;
  499. }
  500. switch (cmd) {
  501. case 0x00000001: // session
  502. handle = amdgpu_get_ib_value(p, ib_idx, idx + 2);
  503. session_idx = amdgpu_vce_validate_handle(p, handle,
  504. &allocated);
  505. if (session_idx < 0)
  506. return session_idx;
  507. size = &p->adev->vce.img_size[session_idx];
  508. break;
  509. case 0x00000002: // task info
  510. fb_idx = amdgpu_get_ib_value(p, ib_idx, idx + 6);
  511. bs_idx = amdgpu_get_ib_value(p, ib_idx, idx + 7);
  512. break;
  513. case 0x01000001: // create
  514. created = true;
  515. if (!allocated) {
  516. DRM_ERROR("Handle already in use!\n");
  517. r = -EINVAL;
  518. goto out;
  519. }
  520. *size = amdgpu_get_ib_value(p, ib_idx, idx + 8) *
  521. amdgpu_get_ib_value(p, ib_idx, idx + 10) *
  522. 8 * 3 / 2;
  523. break;
  524. case 0x04000001: // config extension
  525. case 0x04000002: // pic control
  526. case 0x04000005: // rate control
  527. case 0x04000007: // motion estimation
  528. case 0x04000008: // rdo
  529. case 0x04000009: // vui
  530. case 0x05000002: // auxiliary buffer
  531. break;
  532. case 0x03000001: // encode
  533. r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 10, idx + 9,
  534. *size, 0);
  535. if (r)
  536. goto out;
  537. r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 12, idx + 11,
  538. *size / 3, 0);
  539. if (r)
  540. goto out;
  541. break;
  542. case 0x02000001: // destroy
  543. destroyed = true;
  544. break;
  545. case 0x05000001: // context buffer
  546. r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
  547. *size * 2, 0);
  548. if (r)
  549. goto out;
  550. break;
  551. case 0x05000004: // video bitstream buffer
  552. tmp = amdgpu_get_ib_value(p, ib_idx, idx + 4);
  553. r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
  554. tmp, bs_idx);
  555. if (r)
  556. goto out;
  557. break;
  558. case 0x05000005: // feedback buffer
  559. r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
  560. 4096, fb_idx);
  561. if (r)
  562. goto out;
  563. break;
  564. default:
  565. DRM_ERROR("invalid VCE command (0x%x)!\n", cmd);
  566. r = -EINVAL;
  567. goto out;
  568. }
  569. if (session_idx == -1) {
  570. DRM_ERROR("no session command at start of IB\n");
  571. r = -EINVAL;
  572. goto out;
  573. }
  574. idx += len / 4;
  575. }
  576. if (allocated && !created) {
  577. DRM_ERROR("New session without create command!\n");
  578. r = -ENOENT;
  579. }
  580. out:
  581. if ((!r && destroyed) || (r && allocated)) {
  582. /*
  583. * IB contains a destroy msg or we have allocated an
  584. * handle and got an error, anyway free the handle
  585. */
  586. for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
  587. atomic_cmpxchg(&p->adev->vce.handles[i], handle, 0);
  588. }
  589. return r;
  590. }
  591. /**
  592. * amdgpu_vce_ring_emit_semaphore - emit a semaphore command
  593. *
  594. * @ring: engine to use
  595. * @semaphore: address of semaphore
  596. * @emit_wait: true=emit wait, false=emit signal
  597. *
  598. */
  599. bool amdgpu_vce_ring_emit_semaphore(struct amdgpu_ring *ring,
  600. struct amdgpu_semaphore *semaphore,
  601. bool emit_wait)
  602. {
  603. uint64_t addr = semaphore->gpu_addr;
  604. amdgpu_ring_write(ring, VCE_CMD_SEMAPHORE);
  605. amdgpu_ring_write(ring, (addr >> 3) & 0x000FFFFF);
  606. amdgpu_ring_write(ring, (addr >> 23) & 0x000FFFFF);
  607. amdgpu_ring_write(ring, 0x01003000 | (emit_wait ? 1 : 0));
  608. if (!emit_wait)
  609. amdgpu_ring_write(ring, VCE_CMD_END);
  610. return true;
  611. }
  612. /**
  613. * amdgpu_vce_ring_emit_ib - execute indirect buffer
  614. *
  615. * @ring: engine to use
  616. * @ib: the IB to execute
  617. *
  618. */
  619. void amdgpu_vce_ring_emit_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib)
  620. {
  621. amdgpu_ring_write(ring, VCE_CMD_IB);
  622. amdgpu_ring_write(ring, lower_32_bits(ib->gpu_addr));
  623. amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
  624. amdgpu_ring_write(ring, ib->length_dw);
  625. }
  626. /**
  627. * amdgpu_vce_ring_emit_fence - add a fence command to the ring
  628. *
  629. * @ring: engine to use
  630. * @fence: the fence
  631. *
  632. */
  633. void amdgpu_vce_ring_emit_fence(struct amdgpu_ring *ring, u64 addr, u64 seq,
  634. unsigned flags)
  635. {
  636. WARN_ON(flags & AMDGPU_FENCE_FLAG_64BIT);
  637. amdgpu_ring_write(ring, VCE_CMD_FENCE);
  638. amdgpu_ring_write(ring, addr);
  639. amdgpu_ring_write(ring, upper_32_bits(addr));
  640. amdgpu_ring_write(ring, seq);
  641. amdgpu_ring_write(ring, VCE_CMD_TRAP);
  642. amdgpu_ring_write(ring, VCE_CMD_END);
  643. }
  644. /**
  645. * amdgpu_vce_ring_test_ring - test if VCE ring is working
  646. *
  647. * @ring: the engine to test on
  648. *
  649. */
  650. int amdgpu_vce_ring_test_ring(struct amdgpu_ring *ring)
  651. {
  652. struct amdgpu_device *adev = ring->adev;
  653. uint32_t rptr = amdgpu_ring_get_rptr(ring);
  654. unsigned i;
  655. int r;
  656. r = amdgpu_ring_lock(ring, 16);
  657. if (r) {
  658. DRM_ERROR("amdgpu: vce failed to lock ring %d (%d).\n",
  659. ring->idx, r);
  660. return r;
  661. }
  662. amdgpu_ring_write(ring, VCE_CMD_END);
  663. amdgpu_ring_unlock_commit(ring);
  664. for (i = 0; i < adev->usec_timeout; i++) {
  665. if (amdgpu_ring_get_rptr(ring) != rptr)
  666. break;
  667. DRM_UDELAY(1);
  668. }
  669. if (i < adev->usec_timeout) {
  670. DRM_INFO("ring test on %d succeeded in %d usecs\n",
  671. ring->idx, i);
  672. } else {
  673. DRM_ERROR("amdgpu: ring %d test failed\n",
  674. ring->idx);
  675. r = -ETIMEDOUT;
  676. }
  677. return r;
  678. }
  679. /**
  680. * amdgpu_vce_ring_test_ib - test if VCE IBs are working
  681. *
  682. * @ring: the engine to test on
  683. *
  684. */
  685. int amdgpu_vce_ring_test_ib(struct amdgpu_ring *ring)
  686. {
  687. struct amdgpu_fence *fence = NULL;
  688. int r;
  689. r = amdgpu_vce_get_create_msg(ring, 1, NULL);
  690. if (r) {
  691. DRM_ERROR("amdgpu: failed to get create msg (%d).\n", r);
  692. goto error;
  693. }
  694. r = amdgpu_vce_get_destroy_msg(ring, 1, &fence);
  695. if (r) {
  696. DRM_ERROR("amdgpu: failed to get destroy ib (%d).\n", r);
  697. goto error;
  698. }
  699. r = amdgpu_fence_wait(fence, false);
  700. if (r) {
  701. DRM_ERROR("amdgpu: fence wait failed (%d).\n", r);
  702. } else {
  703. DRM_INFO("ib test on ring %d succeeded\n", ring->idx);
  704. }
  705. error:
  706. amdgpu_fence_unref(&fence);
  707. return r;
  708. }