amdgpu_vce.c 21 KB

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