amdgpu_vce.c 24 KB

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