amdgpu_vce.c 25 KB

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