amdgpu_vce.c 21 KB

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