amdgpu_vce.c 21 KB

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