amdgpu_vce.c 20 KB

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