amdgpu_vce.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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. static int amdgpu_vce_free_job(
  302. struct amdgpu_job *job)
  303. {
  304. amdgpu_ib_free(job->adev, job->ibs);
  305. kfree(job->ibs);
  306. return 0;
  307. }
  308. /**
  309. * amdgpu_vce_get_create_msg - generate a VCE create msg
  310. *
  311. * @adev: amdgpu_device pointer
  312. * @ring: ring we should submit the msg to
  313. * @handle: VCE session handle to use
  314. * @fence: optional fence to return
  315. *
  316. * Open up a stream for HW test
  317. */
  318. int amdgpu_vce_get_create_msg(struct amdgpu_ring *ring, uint32_t handle,
  319. struct fence **fence)
  320. {
  321. const unsigned ib_size_dw = 1024;
  322. struct amdgpu_ib *ib = NULL;
  323. struct fence *f = NULL;
  324. struct amdgpu_device *adev = ring->adev;
  325. uint64_t dummy;
  326. int i, r;
  327. ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
  328. if (!ib)
  329. return -ENOMEM;
  330. r = amdgpu_ib_get(ring, NULL, ib_size_dw * 4, ib);
  331. if (r) {
  332. DRM_ERROR("amdgpu: failed to get ib (%d).\n", r);
  333. kfree(ib);
  334. return r;
  335. }
  336. dummy = ib->gpu_addr + 1024;
  337. /* stitch together an VCE create msg */
  338. ib->length_dw = 0;
  339. ib->ptr[ib->length_dw++] = 0x0000000c; /* len */
  340. ib->ptr[ib->length_dw++] = 0x00000001; /* session cmd */
  341. ib->ptr[ib->length_dw++] = handle;
  342. if ((ring->adev->vce.fw_version >> 24) >= 52)
  343. ib->ptr[ib->length_dw++] = 0x00000040; /* len */
  344. else
  345. ib->ptr[ib->length_dw++] = 0x00000030; /* len */
  346. ib->ptr[ib->length_dw++] = 0x01000001; /* create cmd */
  347. ib->ptr[ib->length_dw++] = 0x00000000;
  348. ib->ptr[ib->length_dw++] = 0x00000042;
  349. ib->ptr[ib->length_dw++] = 0x0000000a;
  350. ib->ptr[ib->length_dw++] = 0x00000001;
  351. ib->ptr[ib->length_dw++] = 0x00000080;
  352. ib->ptr[ib->length_dw++] = 0x00000060;
  353. ib->ptr[ib->length_dw++] = 0x00000100;
  354. ib->ptr[ib->length_dw++] = 0x00000100;
  355. ib->ptr[ib->length_dw++] = 0x0000000c;
  356. ib->ptr[ib->length_dw++] = 0x00000000;
  357. if ((ring->adev->vce.fw_version >> 24) >= 52) {
  358. ib->ptr[ib->length_dw++] = 0x00000000;
  359. ib->ptr[ib->length_dw++] = 0x00000000;
  360. ib->ptr[ib->length_dw++] = 0x00000000;
  361. ib->ptr[ib->length_dw++] = 0x00000000;
  362. }
  363. ib->ptr[ib->length_dw++] = 0x00000014; /* len */
  364. ib->ptr[ib->length_dw++] = 0x05000005; /* feedback buffer */
  365. ib->ptr[ib->length_dw++] = upper_32_bits(dummy);
  366. ib->ptr[ib->length_dw++] = dummy;
  367. ib->ptr[ib->length_dw++] = 0x00000001;
  368. for (i = ib->length_dw; i < ib_size_dw; ++i)
  369. ib->ptr[i] = 0x0;
  370. r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
  371. &amdgpu_vce_free_job,
  372. AMDGPU_FENCE_OWNER_UNDEFINED,
  373. &f);
  374. if (r)
  375. goto err;
  376. if (fence)
  377. *fence = fence_get(f);
  378. fence_put(f);
  379. return 0;
  380. err:
  381. amdgpu_ib_free(adev, ib);
  382. kfree(ib);
  383. return r;
  384. }
  385. /**
  386. * amdgpu_vce_get_destroy_msg - generate a VCE destroy msg
  387. *
  388. * @adev: amdgpu_device pointer
  389. * @ring: ring we should submit the msg to
  390. * @handle: VCE session handle to use
  391. * @fence: optional fence to return
  392. *
  393. * Close up a stream for HW test or if userspace failed to do so
  394. */
  395. int amdgpu_vce_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
  396. struct fence **fence)
  397. {
  398. const unsigned ib_size_dw = 1024;
  399. struct amdgpu_ib *ib = NULL;
  400. struct fence *f = NULL;
  401. struct amdgpu_device *adev = ring->adev;
  402. uint64_t dummy;
  403. int i, r;
  404. ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
  405. if (!ib)
  406. return -ENOMEM;
  407. r = amdgpu_ib_get(ring, NULL, ib_size_dw * 4, ib);
  408. if (r) {
  409. kfree(ib);
  410. DRM_ERROR("amdgpu: failed to get ib (%d).\n", r);
  411. return r;
  412. }
  413. dummy = ib->gpu_addr + 1024;
  414. /* stitch together an VCE destroy msg */
  415. ib->length_dw = 0;
  416. ib->ptr[ib->length_dw++] = 0x0000000c; /* len */
  417. ib->ptr[ib->length_dw++] = 0x00000001; /* session cmd */
  418. ib->ptr[ib->length_dw++] = handle;
  419. ib->ptr[ib->length_dw++] = 0x00000014; /* len */
  420. ib->ptr[ib->length_dw++] = 0x05000005; /* feedback buffer */
  421. ib->ptr[ib->length_dw++] = upper_32_bits(dummy);
  422. ib->ptr[ib->length_dw++] = dummy;
  423. ib->ptr[ib->length_dw++] = 0x00000001;
  424. ib->ptr[ib->length_dw++] = 0x00000008; /* len */
  425. ib->ptr[ib->length_dw++] = 0x02000001; /* destroy cmd */
  426. for (i = ib->length_dw; i < ib_size_dw; ++i)
  427. ib->ptr[i] = 0x0;
  428. r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
  429. &amdgpu_vce_free_job,
  430. AMDGPU_FENCE_OWNER_UNDEFINED,
  431. &f);
  432. if (r)
  433. goto err;
  434. if (fence)
  435. *fence = fence_get(f);
  436. fence_put(f);
  437. return 0;
  438. err:
  439. amdgpu_ib_free(adev, ib);
  440. kfree(ib);
  441. return r;
  442. }
  443. /**
  444. * amdgpu_vce_cs_reloc - command submission relocation
  445. *
  446. * @p: parser context
  447. * @lo: address of lower dword
  448. * @hi: address of higher dword
  449. * @size: minimum size
  450. *
  451. * Patch relocation inside command stream with real buffer address
  452. */
  453. static int amdgpu_vce_cs_reloc(struct amdgpu_cs_parser *p, uint32_t ib_idx,
  454. int lo, int hi, unsigned size, uint32_t index)
  455. {
  456. struct amdgpu_bo_va_mapping *mapping;
  457. struct amdgpu_ib *ib = &p->ibs[ib_idx];
  458. struct amdgpu_bo *bo;
  459. uint64_t addr;
  460. if (index == 0xffffffff)
  461. index = 0;
  462. addr = ((uint64_t)amdgpu_get_ib_value(p, ib_idx, lo)) |
  463. ((uint64_t)amdgpu_get_ib_value(p, ib_idx, hi)) << 32;
  464. addr += ((uint64_t)size) * ((uint64_t)index);
  465. mapping = amdgpu_cs_find_mapping(p, addr, &bo);
  466. if (mapping == NULL) {
  467. DRM_ERROR("Can't find BO for addr 0x%010Lx %d %d %d %d\n",
  468. addr, lo, hi, size, index);
  469. return -EINVAL;
  470. }
  471. if ((addr + (uint64_t)size) >
  472. ((uint64_t)mapping->it.last + 1) * AMDGPU_GPU_PAGE_SIZE) {
  473. DRM_ERROR("BO to small for addr 0x%010Lx %d %d\n",
  474. addr, lo, hi);
  475. return -EINVAL;
  476. }
  477. addr -= ((uint64_t)mapping->it.start) * AMDGPU_GPU_PAGE_SIZE;
  478. addr += amdgpu_bo_gpu_offset(bo);
  479. addr -= ((uint64_t)size) * ((uint64_t)index);
  480. ib->ptr[lo] = addr & 0xFFFFFFFF;
  481. ib->ptr[hi] = addr >> 32;
  482. return 0;
  483. }
  484. /**
  485. * amdgpu_vce_validate_handle - validate stream handle
  486. *
  487. * @p: parser context
  488. * @handle: handle to validate
  489. * @allocated: allocated a new handle?
  490. *
  491. * Validates the handle and return the found session index or -EINVAL
  492. * we we don't have another free session index.
  493. */
  494. static int amdgpu_vce_validate_handle(struct amdgpu_cs_parser *p,
  495. uint32_t handle, bool *allocated)
  496. {
  497. unsigned i;
  498. *allocated = false;
  499. /* validate the handle */
  500. for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
  501. if (atomic_read(&p->adev->vce.handles[i]) == handle) {
  502. if (p->adev->vce.filp[i] != p->filp) {
  503. DRM_ERROR("VCE handle collision detected!\n");
  504. return -EINVAL;
  505. }
  506. return i;
  507. }
  508. }
  509. /* handle not found try to alloc a new one */
  510. for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i) {
  511. if (!atomic_cmpxchg(&p->adev->vce.handles[i], 0, handle)) {
  512. p->adev->vce.filp[i] = p->filp;
  513. p->adev->vce.img_size[i] = 0;
  514. *allocated = true;
  515. return i;
  516. }
  517. }
  518. DRM_ERROR("No more free VCE handles!\n");
  519. return -EINVAL;
  520. }
  521. /**
  522. * amdgpu_vce_cs_parse - parse and validate the command stream
  523. *
  524. * @p: parser context
  525. *
  526. */
  527. int amdgpu_vce_ring_parse_cs(struct amdgpu_cs_parser *p, uint32_t ib_idx)
  528. {
  529. struct amdgpu_ib *ib = &p->ibs[ib_idx];
  530. unsigned fb_idx = 0, bs_idx = 0;
  531. int session_idx = -1;
  532. bool destroyed = false;
  533. bool created = false;
  534. bool allocated = false;
  535. uint32_t tmp, handle = 0;
  536. uint32_t *size = &tmp;
  537. int i, r = 0, idx = 0;
  538. amdgpu_vce_note_usage(p->adev);
  539. while (idx < ib->length_dw) {
  540. uint32_t len = amdgpu_get_ib_value(p, ib_idx, idx);
  541. uint32_t cmd = amdgpu_get_ib_value(p, ib_idx, idx + 1);
  542. if ((len < 8) || (len & 3)) {
  543. DRM_ERROR("invalid VCE command length (%d)!\n", len);
  544. r = -EINVAL;
  545. goto out;
  546. }
  547. if (destroyed) {
  548. DRM_ERROR("No other command allowed after destroy!\n");
  549. r = -EINVAL;
  550. goto out;
  551. }
  552. switch (cmd) {
  553. case 0x00000001: // session
  554. handle = amdgpu_get_ib_value(p, ib_idx, idx + 2);
  555. session_idx = amdgpu_vce_validate_handle(p, handle,
  556. &allocated);
  557. if (session_idx < 0)
  558. return session_idx;
  559. size = &p->adev->vce.img_size[session_idx];
  560. break;
  561. case 0x00000002: // task info
  562. fb_idx = amdgpu_get_ib_value(p, ib_idx, idx + 6);
  563. bs_idx = amdgpu_get_ib_value(p, ib_idx, idx + 7);
  564. break;
  565. case 0x01000001: // create
  566. created = true;
  567. if (!allocated) {
  568. DRM_ERROR("Handle already in use!\n");
  569. r = -EINVAL;
  570. goto out;
  571. }
  572. *size = amdgpu_get_ib_value(p, ib_idx, idx + 8) *
  573. amdgpu_get_ib_value(p, ib_idx, idx + 10) *
  574. 8 * 3 / 2;
  575. break;
  576. case 0x04000001: // config extension
  577. case 0x04000002: // pic control
  578. case 0x04000005: // rate control
  579. case 0x04000007: // motion estimation
  580. case 0x04000008: // rdo
  581. case 0x04000009: // vui
  582. case 0x05000002: // auxiliary buffer
  583. break;
  584. case 0x03000001: // encode
  585. r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 10, idx + 9,
  586. *size, 0);
  587. if (r)
  588. goto out;
  589. r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 12, idx + 11,
  590. *size / 3, 0);
  591. if (r)
  592. goto out;
  593. break;
  594. case 0x02000001: // destroy
  595. destroyed = true;
  596. break;
  597. case 0x05000001: // context buffer
  598. r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
  599. *size * 2, 0);
  600. if (r)
  601. goto out;
  602. break;
  603. case 0x05000004: // video bitstream buffer
  604. tmp = amdgpu_get_ib_value(p, ib_idx, idx + 4);
  605. r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
  606. tmp, bs_idx);
  607. if (r)
  608. goto out;
  609. break;
  610. case 0x05000005: // feedback buffer
  611. r = amdgpu_vce_cs_reloc(p, ib_idx, idx + 3, idx + 2,
  612. 4096, fb_idx);
  613. if (r)
  614. goto out;
  615. break;
  616. default:
  617. DRM_ERROR("invalid VCE command (0x%x)!\n", cmd);
  618. r = -EINVAL;
  619. goto out;
  620. }
  621. if (session_idx == -1) {
  622. DRM_ERROR("no session command at start of IB\n");
  623. r = -EINVAL;
  624. goto out;
  625. }
  626. idx += len / 4;
  627. }
  628. if (allocated && !created) {
  629. DRM_ERROR("New session without create command!\n");
  630. r = -ENOENT;
  631. }
  632. out:
  633. if ((!r && destroyed) || (r && allocated)) {
  634. /*
  635. * IB contains a destroy msg or we have allocated an
  636. * handle and got an error, anyway free the handle
  637. */
  638. for (i = 0; i < AMDGPU_MAX_VCE_HANDLES; ++i)
  639. atomic_cmpxchg(&p->adev->vce.handles[i], handle, 0);
  640. }
  641. return r;
  642. }
  643. /**
  644. * amdgpu_vce_ring_emit_ib - execute indirect buffer
  645. *
  646. * @ring: engine to use
  647. * @ib: the IB to execute
  648. *
  649. */
  650. void amdgpu_vce_ring_emit_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib)
  651. {
  652. amdgpu_ring_write(ring, VCE_CMD_IB);
  653. amdgpu_ring_write(ring, lower_32_bits(ib->gpu_addr));
  654. amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
  655. amdgpu_ring_write(ring, ib->length_dw);
  656. }
  657. /**
  658. * amdgpu_vce_ring_emit_fence - add a fence command to the ring
  659. *
  660. * @ring: engine to use
  661. * @fence: the fence
  662. *
  663. */
  664. void amdgpu_vce_ring_emit_fence(struct amdgpu_ring *ring, u64 addr, u64 seq,
  665. unsigned flags)
  666. {
  667. WARN_ON(flags & AMDGPU_FENCE_FLAG_64BIT);
  668. amdgpu_ring_write(ring, VCE_CMD_FENCE);
  669. amdgpu_ring_write(ring, addr);
  670. amdgpu_ring_write(ring, upper_32_bits(addr));
  671. amdgpu_ring_write(ring, seq);
  672. amdgpu_ring_write(ring, VCE_CMD_TRAP);
  673. amdgpu_ring_write(ring, VCE_CMD_END);
  674. }
  675. /**
  676. * amdgpu_vce_ring_test_ring - test if VCE ring is working
  677. *
  678. * @ring: the engine to test on
  679. *
  680. */
  681. int amdgpu_vce_ring_test_ring(struct amdgpu_ring *ring)
  682. {
  683. struct amdgpu_device *adev = ring->adev;
  684. uint32_t rptr = amdgpu_ring_get_rptr(ring);
  685. unsigned i;
  686. int r;
  687. r = amdgpu_ring_lock(ring, 16);
  688. if (r) {
  689. DRM_ERROR("amdgpu: vce failed to lock ring %d (%d).\n",
  690. ring->idx, r);
  691. return r;
  692. }
  693. amdgpu_ring_write(ring, VCE_CMD_END);
  694. amdgpu_ring_unlock_commit(ring);
  695. for (i = 0; i < adev->usec_timeout; i++) {
  696. if (amdgpu_ring_get_rptr(ring) != rptr)
  697. break;
  698. DRM_UDELAY(1);
  699. }
  700. if (i < adev->usec_timeout) {
  701. DRM_INFO("ring test on %d succeeded in %d usecs\n",
  702. ring->idx, i);
  703. } else {
  704. DRM_ERROR("amdgpu: ring %d test failed\n",
  705. ring->idx);
  706. r = -ETIMEDOUT;
  707. }
  708. return r;
  709. }
  710. /**
  711. * amdgpu_vce_ring_test_ib - test if VCE IBs are working
  712. *
  713. * @ring: the engine to test on
  714. *
  715. */
  716. int amdgpu_vce_ring_test_ib(struct amdgpu_ring *ring)
  717. {
  718. struct fence *fence = NULL;
  719. int r;
  720. /* skip vce ring1 ib test for now, since it's not reliable */
  721. if (ring == &ring->adev->vce.ring[1])
  722. return 0;
  723. r = amdgpu_vce_get_create_msg(ring, 1, NULL);
  724. if (r) {
  725. DRM_ERROR("amdgpu: failed to get create msg (%d).\n", r);
  726. goto error;
  727. }
  728. r = amdgpu_vce_get_destroy_msg(ring, 1, &fence);
  729. if (r) {
  730. DRM_ERROR("amdgpu: failed to get destroy ib (%d).\n", r);
  731. goto error;
  732. }
  733. r = fence_wait(fence, false);
  734. if (r) {
  735. DRM_ERROR("amdgpu: fence wait failed (%d).\n", r);
  736. } else {
  737. DRM_INFO("ib test on ring %d succeeded\n", ring->idx);
  738. }
  739. error:
  740. fence_put(fence);
  741. return r;
  742. }