amdgpu_uvd.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  1. /*
  2. * Copyright 2011 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. */
  26. /*
  27. * Authors:
  28. * Christian König <deathsimple@vodafone.de>
  29. */
  30. #include <linux/firmware.h>
  31. #include <linux/module.h>
  32. #include <drm/drmP.h>
  33. #include <drm/drm.h>
  34. #include "amdgpu.h"
  35. #include "amdgpu_pm.h"
  36. #include "amdgpu_uvd.h"
  37. #include "cikd.h"
  38. #include "uvd/uvd_4_2_d.h"
  39. /* 1 second timeout */
  40. #define UVD_IDLE_TIMEOUT msecs_to_jiffies(1000)
  41. /* Firmware versions for VI */
  42. #define FW_1_65_10 ((1 << 24) | (65 << 16) | (10 << 8))
  43. #define FW_1_87_11 ((1 << 24) | (87 << 16) | (11 << 8))
  44. #define FW_1_87_12 ((1 << 24) | (87 << 16) | (12 << 8))
  45. #define FW_1_37_15 ((1 << 24) | (37 << 16) | (15 << 8))
  46. /* Polaris10/11 firmware version */
  47. #define FW_1_66_16 ((1 << 24) | (66 << 16) | (16 << 8))
  48. /* Firmware Names */
  49. #ifdef CONFIG_DRM_AMDGPU_CIK
  50. #define FIRMWARE_BONAIRE "radeon/bonaire_uvd.bin"
  51. #define FIRMWARE_KABINI "radeon/kabini_uvd.bin"
  52. #define FIRMWARE_KAVERI "radeon/kaveri_uvd.bin"
  53. #define FIRMWARE_HAWAII "radeon/hawaii_uvd.bin"
  54. #define FIRMWARE_MULLINS "radeon/mullins_uvd.bin"
  55. #endif
  56. #define FIRMWARE_TONGA "amdgpu/tonga_uvd.bin"
  57. #define FIRMWARE_CARRIZO "amdgpu/carrizo_uvd.bin"
  58. #define FIRMWARE_FIJI "amdgpu/fiji_uvd.bin"
  59. #define FIRMWARE_STONEY "amdgpu/stoney_uvd.bin"
  60. #define FIRMWARE_POLARIS10 "amdgpu/polaris10_uvd.bin"
  61. #define FIRMWARE_POLARIS11 "amdgpu/polaris11_uvd.bin"
  62. #define FIRMWARE_POLARIS12 "amdgpu/polaris12_uvd.bin"
  63. #define FIRMWARE_VEGAM "amdgpu/vegam_uvd.bin"
  64. #define FIRMWARE_VEGA10 "amdgpu/vega10_uvd.bin"
  65. #define FIRMWARE_VEGA12 "amdgpu/vega12_uvd.bin"
  66. #define mmUVD_GPCOM_VCPU_DATA0_VEGA10 (0x03c4 + 0x7e00)
  67. #define mmUVD_GPCOM_VCPU_DATA1_VEGA10 (0x03c5 + 0x7e00)
  68. #define mmUVD_GPCOM_VCPU_CMD_VEGA10 (0x03c3 + 0x7e00)
  69. #define mmUVD_NO_OP_VEGA10 (0x03ff + 0x7e00)
  70. #define mmUVD_ENGINE_CNTL_VEGA10 (0x03c6 + 0x7e00)
  71. /**
  72. * amdgpu_uvd_cs_ctx - Command submission parser context
  73. *
  74. * Used for emulating virtual memory support on UVD 4.2.
  75. */
  76. struct amdgpu_uvd_cs_ctx {
  77. struct amdgpu_cs_parser *parser;
  78. unsigned reg, count;
  79. unsigned data0, data1;
  80. unsigned idx;
  81. unsigned ib_idx;
  82. /* does the IB has a msg command */
  83. bool has_msg_cmd;
  84. /* minimum buffer sizes */
  85. unsigned *buf_sizes;
  86. };
  87. #ifdef CONFIG_DRM_AMDGPU_CIK
  88. MODULE_FIRMWARE(FIRMWARE_BONAIRE);
  89. MODULE_FIRMWARE(FIRMWARE_KABINI);
  90. MODULE_FIRMWARE(FIRMWARE_KAVERI);
  91. MODULE_FIRMWARE(FIRMWARE_HAWAII);
  92. MODULE_FIRMWARE(FIRMWARE_MULLINS);
  93. #endif
  94. MODULE_FIRMWARE(FIRMWARE_TONGA);
  95. MODULE_FIRMWARE(FIRMWARE_CARRIZO);
  96. MODULE_FIRMWARE(FIRMWARE_FIJI);
  97. MODULE_FIRMWARE(FIRMWARE_STONEY);
  98. MODULE_FIRMWARE(FIRMWARE_POLARIS10);
  99. MODULE_FIRMWARE(FIRMWARE_POLARIS11);
  100. MODULE_FIRMWARE(FIRMWARE_POLARIS12);
  101. MODULE_FIRMWARE(FIRMWARE_VEGAM);
  102. MODULE_FIRMWARE(FIRMWARE_VEGA10);
  103. MODULE_FIRMWARE(FIRMWARE_VEGA12);
  104. static void amdgpu_uvd_idle_work_handler(struct work_struct *work);
  105. int amdgpu_uvd_sw_init(struct amdgpu_device *adev)
  106. {
  107. struct amdgpu_ring *ring;
  108. struct drm_sched_rq *rq;
  109. unsigned long bo_size;
  110. const char *fw_name;
  111. const struct common_firmware_header *hdr;
  112. unsigned version_major, version_minor, family_id;
  113. int i, r;
  114. INIT_DELAYED_WORK(&adev->uvd.idle_work, amdgpu_uvd_idle_work_handler);
  115. switch (adev->asic_type) {
  116. #ifdef CONFIG_DRM_AMDGPU_CIK
  117. case CHIP_BONAIRE:
  118. fw_name = FIRMWARE_BONAIRE;
  119. break;
  120. case CHIP_KABINI:
  121. fw_name = FIRMWARE_KABINI;
  122. break;
  123. case CHIP_KAVERI:
  124. fw_name = FIRMWARE_KAVERI;
  125. break;
  126. case CHIP_HAWAII:
  127. fw_name = FIRMWARE_HAWAII;
  128. break;
  129. case CHIP_MULLINS:
  130. fw_name = FIRMWARE_MULLINS;
  131. break;
  132. #endif
  133. case CHIP_TONGA:
  134. fw_name = FIRMWARE_TONGA;
  135. break;
  136. case CHIP_FIJI:
  137. fw_name = FIRMWARE_FIJI;
  138. break;
  139. case CHIP_CARRIZO:
  140. fw_name = FIRMWARE_CARRIZO;
  141. break;
  142. case CHIP_STONEY:
  143. fw_name = FIRMWARE_STONEY;
  144. break;
  145. case CHIP_POLARIS10:
  146. fw_name = FIRMWARE_POLARIS10;
  147. break;
  148. case CHIP_POLARIS11:
  149. fw_name = FIRMWARE_POLARIS11;
  150. break;
  151. case CHIP_POLARIS12:
  152. fw_name = FIRMWARE_POLARIS12;
  153. break;
  154. case CHIP_VEGA10:
  155. fw_name = FIRMWARE_VEGA10;
  156. break;
  157. case CHIP_VEGA12:
  158. fw_name = FIRMWARE_VEGA12;
  159. break;
  160. case CHIP_VEGAM:
  161. fw_name = FIRMWARE_VEGAM;
  162. break;
  163. default:
  164. return -EINVAL;
  165. }
  166. r = request_firmware(&adev->uvd.fw, fw_name, adev->dev);
  167. if (r) {
  168. dev_err(adev->dev, "amdgpu_uvd: Can't load firmware \"%s\"\n",
  169. fw_name);
  170. return r;
  171. }
  172. r = amdgpu_ucode_validate(adev->uvd.fw);
  173. if (r) {
  174. dev_err(adev->dev, "amdgpu_uvd: Can't validate firmware \"%s\"\n",
  175. fw_name);
  176. release_firmware(adev->uvd.fw);
  177. adev->uvd.fw = NULL;
  178. return r;
  179. }
  180. /* Set the default UVD handles that the firmware can handle */
  181. adev->uvd.max_handles = AMDGPU_DEFAULT_UVD_HANDLES;
  182. hdr = (const struct common_firmware_header *)adev->uvd.fw->data;
  183. family_id = le32_to_cpu(hdr->ucode_version) & 0xff;
  184. version_major = (le32_to_cpu(hdr->ucode_version) >> 24) & 0xff;
  185. version_minor = (le32_to_cpu(hdr->ucode_version) >> 8) & 0xff;
  186. DRM_INFO("Found UVD firmware Version: %hu.%hu Family ID: %hu\n",
  187. version_major, version_minor, family_id);
  188. /*
  189. * Limit the number of UVD handles depending on microcode major
  190. * and minor versions. The firmware version which has 40 UVD
  191. * instances support is 1.80. So all subsequent versions should
  192. * also have the same support.
  193. */
  194. if ((version_major > 0x01) ||
  195. ((version_major == 0x01) && (version_minor >= 0x50)))
  196. adev->uvd.max_handles = AMDGPU_MAX_UVD_HANDLES;
  197. adev->uvd.fw_version = ((version_major << 24) | (version_minor << 16) |
  198. (family_id << 8));
  199. if ((adev->asic_type == CHIP_POLARIS10 ||
  200. adev->asic_type == CHIP_POLARIS11) &&
  201. (adev->uvd.fw_version < FW_1_66_16))
  202. DRM_ERROR("POLARIS10/11 UVD firmware version %hu.%hu is too old.\n",
  203. version_major, version_minor);
  204. bo_size = AMDGPU_UVD_STACK_SIZE + AMDGPU_UVD_HEAP_SIZE
  205. + AMDGPU_UVD_SESSION_SIZE * adev->uvd.max_handles;
  206. if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
  207. bo_size += AMDGPU_GPU_PAGE_ALIGN(le32_to_cpu(hdr->ucode_size_bytes) + 8);
  208. r = amdgpu_bo_create_kernel(adev, bo_size, PAGE_SIZE,
  209. AMDGPU_GEM_DOMAIN_VRAM, &adev->uvd.vcpu_bo,
  210. &adev->uvd.gpu_addr, &adev->uvd.cpu_addr);
  211. if (r) {
  212. dev_err(adev->dev, "(%d) failed to allocate UVD bo\n", r);
  213. return r;
  214. }
  215. ring = &adev->uvd.ring;
  216. rq = &ring->sched.sched_rq[DRM_SCHED_PRIORITY_NORMAL];
  217. r = drm_sched_entity_init(&ring->sched, &adev->uvd.entity,
  218. rq, NULL);
  219. if (r != 0) {
  220. DRM_ERROR("Failed setting up UVD run queue.\n");
  221. return r;
  222. }
  223. for (i = 0; i < adev->uvd.max_handles; ++i) {
  224. atomic_set(&adev->uvd.handles[i], 0);
  225. adev->uvd.filp[i] = NULL;
  226. }
  227. /* from uvd v5.0 HW addressing capacity increased to 64 bits */
  228. if (!amdgpu_device_ip_block_version_cmp(adev, AMD_IP_BLOCK_TYPE_UVD, 5, 0))
  229. adev->uvd.address_64_bit = true;
  230. switch (adev->asic_type) {
  231. case CHIP_TONGA:
  232. adev->uvd.use_ctx_buf = adev->uvd.fw_version >= FW_1_65_10;
  233. break;
  234. case CHIP_CARRIZO:
  235. adev->uvd.use_ctx_buf = adev->uvd.fw_version >= FW_1_87_11;
  236. break;
  237. case CHIP_FIJI:
  238. adev->uvd.use_ctx_buf = adev->uvd.fw_version >= FW_1_87_12;
  239. break;
  240. case CHIP_STONEY:
  241. adev->uvd.use_ctx_buf = adev->uvd.fw_version >= FW_1_37_15;
  242. break;
  243. default:
  244. adev->uvd.use_ctx_buf = adev->asic_type >= CHIP_POLARIS10;
  245. }
  246. return 0;
  247. }
  248. int amdgpu_uvd_sw_fini(struct amdgpu_device *adev)
  249. {
  250. int i;
  251. kfree(adev->uvd.saved_bo);
  252. drm_sched_entity_fini(&adev->uvd.ring.sched, &adev->uvd.entity);
  253. amdgpu_bo_free_kernel(&adev->uvd.vcpu_bo,
  254. &adev->uvd.gpu_addr,
  255. (void **)&adev->uvd.cpu_addr);
  256. amdgpu_ring_fini(&adev->uvd.ring);
  257. for (i = 0; i < AMDGPU_MAX_UVD_ENC_RINGS; ++i)
  258. amdgpu_ring_fini(&adev->uvd.ring_enc[i]);
  259. release_firmware(adev->uvd.fw);
  260. return 0;
  261. }
  262. int amdgpu_uvd_suspend(struct amdgpu_device *adev)
  263. {
  264. unsigned size;
  265. void *ptr;
  266. int i;
  267. if (adev->uvd.vcpu_bo == NULL)
  268. return 0;
  269. cancel_delayed_work_sync(&adev->uvd.idle_work);
  270. /* only valid for physical mode */
  271. if (adev->asic_type < CHIP_POLARIS10) {
  272. for (i = 0; i < adev->uvd.max_handles; ++i)
  273. if (atomic_read(&adev->uvd.handles[i]))
  274. break;
  275. if (i == adev->uvd.max_handles)
  276. return 0;
  277. }
  278. size = amdgpu_bo_size(adev->uvd.vcpu_bo);
  279. ptr = adev->uvd.cpu_addr;
  280. adev->uvd.saved_bo = kmalloc(size, GFP_KERNEL);
  281. if (!adev->uvd.saved_bo)
  282. return -ENOMEM;
  283. memcpy_fromio(adev->uvd.saved_bo, ptr, size);
  284. return 0;
  285. }
  286. int amdgpu_uvd_resume(struct amdgpu_device *adev)
  287. {
  288. unsigned size;
  289. void *ptr;
  290. if (adev->uvd.vcpu_bo == NULL)
  291. return -EINVAL;
  292. size = amdgpu_bo_size(adev->uvd.vcpu_bo);
  293. ptr = adev->uvd.cpu_addr;
  294. if (adev->uvd.saved_bo != NULL) {
  295. memcpy_toio(ptr, adev->uvd.saved_bo, size);
  296. kfree(adev->uvd.saved_bo);
  297. adev->uvd.saved_bo = NULL;
  298. } else {
  299. const struct common_firmware_header *hdr;
  300. unsigned offset;
  301. hdr = (const struct common_firmware_header *)adev->uvd.fw->data;
  302. if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
  303. offset = le32_to_cpu(hdr->ucode_array_offset_bytes);
  304. memcpy_toio(adev->uvd.cpu_addr, adev->uvd.fw->data + offset,
  305. le32_to_cpu(hdr->ucode_size_bytes));
  306. size -= le32_to_cpu(hdr->ucode_size_bytes);
  307. ptr += le32_to_cpu(hdr->ucode_size_bytes);
  308. }
  309. memset_io(ptr, 0, size);
  310. /* to restore uvd fence seq */
  311. amdgpu_fence_driver_force_completion(&adev->uvd.ring);
  312. }
  313. return 0;
  314. }
  315. void amdgpu_uvd_free_handles(struct amdgpu_device *adev, struct drm_file *filp)
  316. {
  317. struct amdgpu_ring *ring = &adev->uvd.ring;
  318. int i, r;
  319. for (i = 0; i < adev->uvd.max_handles; ++i) {
  320. uint32_t handle = atomic_read(&adev->uvd.handles[i]);
  321. if (handle != 0 && adev->uvd.filp[i] == filp) {
  322. struct dma_fence *fence;
  323. r = amdgpu_uvd_get_destroy_msg(ring, handle,
  324. false, &fence);
  325. if (r) {
  326. DRM_ERROR("Error destroying UVD (%d)!\n", r);
  327. continue;
  328. }
  329. dma_fence_wait(fence, false);
  330. dma_fence_put(fence);
  331. adev->uvd.filp[i] = NULL;
  332. atomic_set(&adev->uvd.handles[i], 0);
  333. }
  334. }
  335. }
  336. static void amdgpu_uvd_force_into_uvd_segment(struct amdgpu_bo *abo)
  337. {
  338. int i;
  339. for (i = 0; i < abo->placement.num_placement; ++i) {
  340. abo->placements[i].fpfn = 0 >> PAGE_SHIFT;
  341. abo->placements[i].lpfn = (256 * 1024 * 1024) >> PAGE_SHIFT;
  342. }
  343. }
  344. static u64 amdgpu_uvd_get_addr_from_ctx(struct amdgpu_uvd_cs_ctx *ctx)
  345. {
  346. uint32_t lo, hi;
  347. uint64_t addr;
  348. lo = amdgpu_get_ib_value(ctx->parser, ctx->ib_idx, ctx->data0);
  349. hi = amdgpu_get_ib_value(ctx->parser, ctx->ib_idx, ctx->data1);
  350. addr = ((uint64_t)lo) | (((uint64_t)hi) << 32);
  351. return addr;
  352. }
  353. /**
  354. * amdgpu_uvd_cs_pass1 - first parsing round
  355. *
  356. * @ctx: UVD parser context
  357. *
  358. * Make sure UVD message and feedback buffers are in VRAM and
  359. * nobody is violating an 256MB boundary.
  360. */
  361. static int amdgpu_uvd_cs_pass1(struct amdgpu_uvd_cs_ctx *ctx)
  362. {
  363. struct ttm_operation_ctx tctx = { false, false };
  364. struct amdgpu_bo_va_mapping *mapping;
  365. struct amdgpu_bo *bo;
  366. uint32_t cmd;
  367. uint64_t addr = amdgpu_uvd_get_addr_from_ctx(ctx);
  368. int r = 0;
  369. r = amdgpu_cs_find_mapping(ctx->parser, addr, &bo, &mapping);
  370. if (r) {
  371. DRM_ERROR("Can't find BO for addr 0x%08Lx\n", addr);
  372. return r;
  373. }
  374. if (!ctx->parser->adev->uvd.address_64_bit) {
  375. /* check if it's a message or feedback command */
  376. cmd = amdgpu_get_ib_value(ctx->parser, ctx->ib_idx, ctx->idx) >> 1;
  377. if (cmd == 0x0 || cmd == 0x3) {
  378. /* yes, force it into VRAM */
  379. uint32_t domain = AMDGPU_GEM_DOMAIN_VRAM;
  380. amdgpu_ttm_placement_from_domain(bo, domain);
  381. }
  382. amdgpu_uvd_force_into_uvd_segment(bo);
  383. r = ttm_bo_validate(&bo->tbo, &bo->placement, &tctx);
  384. }
  385. return r;
  386. }
  387. /**
  388. * amdgpu_uvd_cs_msg_decode - handle UVD decode message
  389. *
  390. * @msg: pointer to message structure
  391. * @buf_sizes: returned buffer sizes
  392. *
  393. * Peek into the decode message and calculate the necessary buffer sizes.
  394. */
  395. static int amdgpu_uvd_cs_msg_decode(struct amdgpu_device *adev, uint32_t *msg,
  396. unsigned buf_sizes[])
  397. {
  398. unsigned stream_type = msg[4];
  399. unsigned width = msg[6];
  400. unsigned height = msg[7];
  401. unsigned dpb_size = msg[9];
  402. unsigned pitch = msg[28];
  403. unsigned level = msg[57];
  404. unsigned width_in_mb = width / 16;
  405. unsigned height_in_mb = ALIGN(height / 16, 2);
  406. unsigned fs_in_mb = width_in_mb * height_in_mb;
  407. unsigned image_size, tmp, min_dpb_size, num_dpb_buffer;
  408. unsigned min_ctx_size = ~0;
  409. image_size = width * height;
  410. image_size += image_size / 2;
  411. image_size = ALIGN(image_size, 1024);
  412. switch (stream_type) {
  413. case 0: /* H264 */
  414. switch(level) {
  415. case 30:
  416. num_dpb_buffer = 8100 / fs_in_mb;
  417. break;
  418. case 31:
  419. num_dpb_buffer = 18000 / fs_in_mb;
  420. break;
  421. case 32:
  422. num_dpb_buffer = 20480 / fs_in_mb;
  423. break;
  424. case 41:
  425. num_dpb_buffer = 32768 / fs_in_mb;
  426. break;
  427. case 42:
  428. num_dpb_buffer = 34816 / fs_in_mb;
  429. break;
  430. case 50:
  431. num_dpb_buffer = 110400 / fs_in_mb;
  432. break;
  433. case 51:
  434. num_dpb_buffer = 184320 / fs_in_mb;
  435. break;
  436. default:
  437. num_dpb_buffer = 184320 / fs_in_mb;
  438. break;
  439. }
  440. num_dpb_buffer++;
  441. if (num_dpb_buffer > 17)
  442. num_dpb_buffer = 17;
  443. /* reference picture buffer */
  444. min_dpb_size = image_size * num_dpb_buffer;
  445. /* macroblock context buffer */
  446. min_dpb_size += width_in_mb * height_in_mb * num_dpb_buffer * 192;
  447. /* IT surface buffer */
  448. min_dpb_size += width_in_mb * height_in_mb * 32;
  449. break;
  450. case 1: /* VC1 */
  451. /* reference picture buffer */
  452. min_dpb_size = image_size * 3;
  453. /* CONTEXT_BUFFER */
  454. min_dpb_size += width_in_mb * height_in_mb * 128;
  455. /* IT surface buffer */
  456. min_dpb_size += width_in_mb * 64;
  457. /* DB surface buffer */
  458. min_dpb_size += width_in_mb * 128;
  459. /* BP */
  460. tmp = max(width_in_mb, height_in_mb);
  461. min_dpb_size += ALIGN(tmp * 7 * 16, 64);
  462. break;
  463. case 3: /* MPEG2 */
  464. /* reference picture buffer */
  465. min_dpb_size = image_size * 3;
  466. break;
  467. case 4: /* MPEG4 */
  468. /* reference picture buffer */
  469. min_dpb_size = image_size * 3;
  470. /* CM */
  471. min_dpb_size += width_in_mb * height_in_mb * 64;
  472. /* IT surface buffer */
  473. min_dpb_size += ALIGN(width_in_mb * height_in_mb * 32, 64);
  474. break;
  475. case 7: /* H264 Perf */
  476. switch(level) {
  477. case 30:
  478. num_dpb_buffer = 8100 / fs_in_mb;
  479. break;
  480. case 31:
  481. num_dpb_buffer = 18000 / fs_in_mb;
  482. break;
  483. case 32:
  484. num_dpb_buffer = 20480 / fs_in_mb;
  485. break;
  486. case 41:
  487. num_dpb_buffer = 32768 / fs_in_mb;
  488. break;
  489. case 42:
  490. num_dpb_buffer = 34816 / fs_in_mb;
  491. break;
  492. case 50:
  493. num_dpb_buffer = 110400 / fs_in_mb;
  494. break;
  495. case 51:
  496. num_dpb_buffer = 184320 / fs_in_mb;
  497. break;
  498. default:
  499. num_dpb_buffer = 184320 / fs_in_mb;
  500. break;
  501. }
  502. num_dpb_buffer++;
  503. if (num_dpb_buffer > 17)
  504. num_dpb_buffer = 17;
  505. /* reference picture buffer */
  506. min_dpb_size = image_size * num_dpb_buffer;
  507. if (!adev->uvd.use_ctx_buf){
  508. /* macroblock context buffer */
  509. min_dpb_size +=
  510. width_in_mb * height_in_mb * num_dpb_buffer * 192;
  511. /* IT surface buffer */
  512. min_dpb_size += width_in_mb * height_in_mb * 32;
  513. } else {
  514. /* macroblock context buffer */
  515. min_ctx_size =
  516. width_in_mb * height_in_mb * num_dpb_buffer * 192;
  517. }
  518. break;
  519. case 8: /* MJPEG */
  520. min_dpb_size = 0;
  521. break;
  522. case 16: /* H265 */
  523. image_size = (ALIGN(width, 16) * ALIGN(height, 16) * 3) / 2;
  524. image_size = ALIGN(image_size, 256);
  525. num_dpb_buffer = (le32_to_cpu(msg[59]) & 0xff) + 2;
  526. min_dpb_size = image_size * num_dpb_buffer;
  527. min_ctx_size = ((width + 255) / 16) * ((height + 255) / 16)
  528. * 16 * num_dpb_buffer + 52 * 1024;
  529. break;
  530. default:
  531. DRM_ERROR("UVD codec not handled %d!\n", stream_type);
  532. return -EINVAL;
  533. }
  534. if (width > pitch) {
  535. DRM_ERROR("Invalid UVD decoding target pitch!\n");
  536. return -EINVAL;
  537. }
  538. if (dpb_size < min_dpb_size) {
  539. DRM_ERROR("Invalid dpb_size in UVD message (%d / %d)!\n",
  540. dpb_size, min_dpb_size);
  541. return -EINVAL;
  542. }
  543. buf_sizes[0x1] = dpb_size;
  544. buf_sizes[0x2] = image_size;
  545. buf_sizes[0x4] = min_ctx_size;
  546. return 0;
  547. }
  548. /**
  549. * amdgpu_uvd_cs_msg - handle UVD message
  550. *
  551. * @ctx: UVD parser context
  552. * @bo: buffer object containing the message
  553. * @offset: offset into the buffer object
  554. *
  555. * Peek into the UVD message and extract the session id.
  556. * Make sure that we don't open up to many sessions.
  557. */
  558. static int amdgpu_uvd_cs_msg(struct amdgpu_uvd_cs_ctx *ctx,
  559. struct amdgpu_bo *bo, unsigned offset)
  560. {
  561. struct amdgpu_device *adev = ctx->parser->adev;
  562. int32_t *msg, msg_type, handle;
  563. void *ptr;
  564. long r;
  565. int i;
  566. if (offset & 0x3F) {
  567. DRM_ERROR("UVD messages must be 64 byte aligned!\n");
  568. return -EINVAL;
  569. }
  570. r = amdgpu_bo_kmap(bo, &ptr);
  571. if (r) {
  572. DRM_ERROR("Failed mapping the UVD message (%ld)!\n", r);
  573. return r;
  574. }
  575. msg = ptr + offset;
  576. msg_type = msg[1];
  577. handle = msg[2];
  578. if (handle == 0) {
  579. DRM_ERROR("Invalid UVD handle!\n");
  580. return -EINVAL;
  581. }
  582. switch (msg_type) {
  583. case 0:
  584. /* it's a create msg, calc image size (width * height) */
  585. amdgpu_bo_kunmap(bo);
  586. /* try to alloc a new handle */
  587. for (i = 0; i < adev->uvd.max_handles; ++i) {
  588. if (atomic_read(&adev->uvd.handles[i]) == handle) {
  589. DRM_ERROR("Handle 0x%x already in use!\n", handle);
  590. return -EINVAL;
  591. }
  592. if (!atomic_cmpxchg(&adev->uvd.handles[i], 0, handle)) {
  593. adev->uvd.filp[i] = ctx->parser->filp;
  594. return 0;
  595. }
  596. }
  597. DRM_ERROR("No more free UVD handles!\n");
  598. return -ENOSPC;
  599. case 1:
  600. /* it's a decode msg, calc buffer sizes */
  601. r = amdgpu_uvd_cs_msg_decode(adev, msg, ctx->buf_sizes);
  602. amdgpu_bo_kunmap(bo);
  603. if (r)
  604. return r;
  605. /* validate the handle */
  606. for (i = 0; i < adev->uvd.max_handles; ++i) {
  607. if (atomic_read(&adev->uvd.handles[i]) == handle) {
  608. if (adev->uvd.filp[i] != ctx->parser->filp) {
  609. DRM_ERROR("UVD handle collision detected!\n");
  610. return -EINVAL;
  611. }
  612. return 0;
  613. }
  614. }
  615. DRM_ERROR("Invalid UVD handle 0x%x!\n", handle);
  616. return -ENOENT;
  617. case 2:
  618. /* it's a destroy msg, free the handle */
  619. for (i = 0; i < adev->uvd.max_handles; ++i)
  620. atomic_cmpxchg(&adev->uvd.handles[i], handle, 0);
  621. amdgpu_bo_kunmap(bo);
  622. return 0;
  623. default:
  624. DRM_ERROR("Illegal UVD message type (%d)!\n", msg_type);
  625. return -EINVAL;
  626. }
  627. BUG();
  628. return -EINVAL;
  629. }
  630. /**
  631. * amdgpu_uvd_cs_pass2 - second parsing round
  632. *
  633. * @ctx: UVD parser context
  634. *
  635. * Patch buffer addresses, make sure buffer sizes are correct.
  636. */
  637. static int amdgpu_uvd_cs_pass2(struct amdgpu_uvd_cs_ctx *ctx)
  638. {
  639. struct amdgpu_bo_va_mapping *mapping;
  640. struct amdgpu_bo *bo;
  641. uint32_t cmd;
  642. uint64_t start, end;
  643. uint64_t addr = amdgpu_uvd_get_addr_from_ctx(ctx);
  644. int r;
  645. r = amdgpu_cs_find_mapping(ctx->parser, addr, &bo, &mapping);
  646. if (r) {
  647. DRM_ERROR("Can't find BO for addr 0x%08Lx\n", addr);
  648. return r;
  649. }
  650. start = amdgpu_bo_gpu_offset(bo);
  651. end = (mapping->last + 1 - mapping->start);
  652. end = end * AMDGPU_GPU_PAGE_SIZE + start;
  653. addr -= mapping->start * AMDGPU_GPU_PAGE_SIZE;
  654. start += addr;
  655. amdgpu_set_ib_value(ctx->parser, ctx->ib_idx, ctx->data0,
  656. lower_32_bits(start));
  657. amdgpu_set_ib_value(ctx->parser, ctx->ib_idx, ctx->data1,
  658. upper_32_bits(start));
  659. cmd = amdgpu_get_ib_value(ctx->parser, ctx->ib_idx, ctx->idx) >> 1;
  660. if (cmd < 0x4) {
  661. if ((end - start) < ctx->buf_sizes[cmd]) {
  662. DRM_ERROR("buffer (%d) to small (%d / %d)!\n", cmd,
  663. (unsigned)(end - start),
  664. ctx->buf_sizes[cmd]);
  665. return -EINVAL;
  666. }
  667. } else if (cmd == 0x206) {
  668. if ((end - start) < ctx->buf_sizes[4]) {
  669. DRM_ERROR("buffer (%d) to small (%d / %d)!\n", cmd,
  670. (unsigned)(end - start),
  671. ctx->buf_sizes[4]);
  672. return -EINVAL;
  673. }
  674. } else if ((cmd != 0x100) && (cmd != 0x204)) {
  675. DRM_ERROR("invalid UVD command %X!\n", cmd);
  676. return -EINVAL;
  677. }
  678. if (!ctx->parser->adev->uvd.address_64_bit) {
  679. if ((start >> 28) != ((end - 1) >> 28)) {
  680. DRM_ERROR("reloc %LX-%LX crossing 256MB boundary!\n",
  681. start, end);
  682. return -EINVAL;
  683. }
  684. if ((cmd == 0 || cmd == 0x3) &&
  685. (start >> 28) != (ctx->parser->adev->uvd.gpu_addr >> 28)) {
  686. DRM_ERROR("msg/fb buffer %LX-%LX out of 256MB segment!\n",
  687. start, end);
  688. return -EINVAL;
  689. }
  690. }
  691. if (cmd == 0) {
  692. ctx->has_msg_cmd = true;
  693. r = amdgpu_uvd_cs_msg(ctx, bo, addr);
  694. if (r)
  695. return r;
  696. } else if (!ctx->has_msg_cmd) {
  697. DRM_ERROR("Message needed before other commands are send!\n");
  698. return -EINVAL;
  699. }
  700. return 0;
  701. }
  702. /**
  703. * amdgpu_uvd_cs_reg - parse register writes
  704. *
  705. * @ctx: UVD parser context
  706. * @cb: callback function
  707. *
  708. * Parse the register writes, call cb on each complete command.
  709. */
  710. static int amdgpu_uvd_cs_reg(struct amdgpu_uvd_cs_ctx *ctx,
  711. int (*cb)(struct amdgpu_uvd_cs_ctx *ctx))
  712. {
  713. struct amdgpu_ib *ib = &ctx->parser->job->ibs[ctx->ib_idx];
  714. int i, r;
  715. ctx->idx++;
  716. for (i = 0; i <= ctx->count; ++i) {
  717. unsigned reg = ctx->reg + i;
  718. if (ctx->idx >= ib->length_dw) {
  719. DRM_ERROR("Register command after end of CS!\n");
  720. return -EINVAL;
  721. }
  722. switch (reg) {
  723. case mmUVD_GPCOM_VCPU_DATA0:
  724. ctx->data0 = ctx->idx;
  725. break;
  726. case mmUVD_GPCOM_VCPU_DATA1:
  727. ctx->data1 = ctx->idx;
  728. break;
  729. case mmUVD_GPCOM_VCPU_CMD:
  730. r = cb(ctx);
  731. if (r)
  732. return r;
  733. break;
  734. case mmUVD_ENGINE_CNTL:
  735. case mmUVD_NO_OP:
  736. break;
  737. default:
  738. DRM_ERROR("Invalid reg 0x%X!\n", reg);
  739. return -EINVAL;
  740. }
  741. ctx->idx++;
  742. }
  743. return 0;
  744. }
  745. /**
  746. * amdgpu_uvd_cs_packets - parse UVD packets
  747. *
  748. * @ctx: UVD parser context
  749. * @cb: callback function
  750. *
  751. * Parse the command stream packets.
  752. */
  753. static int amdgpu_uvd_cs_packets(struct amdgpu_uvd_cs_ctx *ctx,
  754. int (*cb)(struct amdgpu_uvd_cs_ctx *ctx))
  755. {
  756. struct amdgpu_ib *ib = &ctx->parser->job->ibs[ctx->ib_idx];
  757. int r;
  758. for (ctx->idx = 0 ; ctx->idx < ib->length_dw; ) {
  759. uint32_t cmd = amdgpu_get_ib_value(ctx->parser, ctx->ib_idx, ctx->idx);
  760. unsigned type = CP_PACKET_GET_TYPE(cmd);
  761. switch (type) {
  762. case PACKET_TYPE0:
  763. ctx->reg = CP_PACKET0_GET_REG(cmd);
  764. ctx->count = CP_PACKET_GET_COUNT(cmd);
  765. r = amdgpu_uvd_cs_reg(ctx, cb);
  766. if (r)
  767. return r;
  768. break;
  769. case PACKET_TYPE2:
  770. ++ctx->idx;
  771. break;
  772. default:
  773. DRM_ERROR("Unknown packet type %d !\n", type);
  774. return -EINVAL;
  775. }
  776. }
  777. return 0;
  778. }
  779. /**
  780. * amdgpu_uvd_ring_parse_cs - UVD command submission parser
  781. *
  782. * @parser: Command submission parser context
  783. *
  784. * Parse the command stream, patch in addresses as necessary.
  785. */
  786. int amdgpu_uvd_ring_parse_cs(struct amdgpu_cs_parser *parser, uint32_t ib_idx)
  787. {
  788. struct amdgpu_uvd_cs_ctx ctx = {};
  789. unsigned buf_sizes[] = {
  790. [0x00000000] = 2048,
  791. [0x00000001] = 0xFFFFFFFF,
  792. [0x00000002] = 0xFFFFFFFF,
  793. [0x00000003] = 2048,
  794. [0x00000004] = 0xFFFFFFFF,
  795. };
  796. struct amdgpu_ib *ib = &parser->job->ibs[ib_idx];
  797. int r;
  798. parser->job->vm = NULL;
  799. ib->gpu_addr = amdgpu_sa_bo_gpu_addr(ib->sa_bo);
  800. if (ib->length_dw % 16) {
  801. DRM_ERROR("UVD IB length (%d) not 16 dwords aligned!\n",
  802. ib->length_dw);
  803. return -EINVAL;
  804. }
  805. ctx.parser = parser;
  806. ctx.buf_sizes = buf_sizes;
  807. ctx.ib_idx = ib_idx;
  808. /* first round only required on chips without UVD 64 bit address support */
  809. if (!parser->adev->uvd.address_64_bit) {
  810. /* first round, make sure the buffers are actually in the UVD segment */
  811. r = amdgpu_uvd_cs_packets(&ctx, amdgpu_uvd_cs_pass1);
  812. if (r)
  813. return r;
  814. }
  815. /* second round, patch buffer addresses into the command stream */
  816. r = amdgpu_uvd_cs_packets(&ctx, amdgpu_uvd_cs_pass2);
  817. if (r)
  818. return r;
  819. if (!ctx.has_msg_cmd) {
  820. DRM_ERROR("UVD-IBs need a msg command!\n");
  821. return -EINVAL;
  822. }
  823. return 0;
  824. }
  825. static int amdgpu_uvd_send_msg(struct amdgpu_ring *ring, struct amdgpu_bo *bo,
  826. bool direct, struct dma_fence **fence)
  827. {
  828. struct amdgpu_device *adev = ring->adev;
  829. struct dma_fence *f = NULL;
  830. struct amdgpu_job *job;
  831. struct amdgpu_ib *ib;
  832. uint32_t data[4];
  833. uint64_t addr;
  834. long r;
  835. int i;
  836. amdgpu_bo_kunmap(bo);
  837. amdgpu_bo_unpin(bo);
  838. if (!ring->adev->uvd.address_64_bit) {
  839. struct ttm_operation_ctx ctx = { true, false };
  840. amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_VRAM);
  841. amdgpu_uvd_force_into_uvd_segment(bo);
  842. r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
  843. if (r)
  844. goto err;
  845. }
  846. r = amdgpu_job_alloc_with_ib(adev, 64, &job);
  847. if (r)
  848. goto err;
  849. if (adev->asic_type >= CHIP_VEGA10) {
  850. data[0] = PACKET0(mmUVD_GPCOM_VCPU_DATA0_VEGA10, 0);
  851. data[1] = PACKET0(mmUVD_GPCOM_VCPU_DATA1_VEGA10, 0);
  852. data[2] = PACKET0(mmUVD_GPCOM_VCPU_CMD_VEGA10, 0);
  853. data[3] = PACKET0(mmUVD_NO_OP_VEGA10, 0);
  854. } else {
  855. data[0] = PACKET0(mmUVD_GPCOM_VCPU_DATA0, 0);
  856. data[1] = PACKET0(mmUVD_GPCOM_VCPU_DATA1, 0);
  857. data[2] = PACKET0(mmUVD_GPCOM_VCPU_CMD, 0);
  858. data[3] = PACKET0(mmUVD_NO_OP, 0);
  859. }
  860. ib = &job->ibs[0];
  861. addr = amdgpu_bo_gpu_offset(bo);
  862. ib->ptr[0] = data[0];
  863. ib->ptr[1] = addr;
  864. ib->ptr[2] = data[1];
  865. ib->ptr[3] = addr >> 32;
  866. ib->ptr[4] = data[2];
  867. ib->ptr[5] = 0;
  868. for (i = 6; i < 16; i += 2) {
  869. ib->ptr[i] = data[3];
  870. ib->ptr[i+1] = 0;
  871. }
  872. ib->length_dw = 16;
  873. if (direct) {
  874. r = reservation_object_wait_timeout_rcu(bo->tbo.resv,
  875. true, false,
  876. msecs_to_jiffies(10));
  877. if (r == 0)
  878. r = -ETIMEDOUT;
  879. if (r < 0)
  880. goto err_free;
  881. r = amdgpu_ib_schedule(ring, 1, ib, NULL, &f);
  882. job->fence = dma_fence_get(f);
  883. if (r)
  884. goto err_free;
  885. amdgpu_job_free(job);
  886. } else {
  887. r = amdgpu_sync_resv(adev, &job->sync, bo->tbo.resv,
  888. AMDGPU_FENCE_OWNER_UNDEFINED, false);
  889. if (r)
  890. goto err_free;
  891. r = amdgpu_job_submit(job, ring, &adev->uvd.entity,
  892. AMDGPU_FENCE_OWNER_UNDEFINED, &f);
  893. if (r)
  894. goto err_free;
  895. }
  896. amdgpu_bo_fence(bo, f, false);
  897. amdgpu_bo_unreserve(bo);
  898. amdgpu_bo_unref(&bo);
  899. if (fence)
  900. *fence = dma_fence_get(f);
  901. dma_fence_put(f);
  902. return 0;
  903. err_free:
  904. amdgpu_job_free(job);
  905. err:
  906. amdgpu_bo_unreserve(bo);
  907. amdgpu_bo_unref(&bo);
  908. return r;
  909. }
  910. /* multiple fence commands without any stream commands in between can
  911. crash the vcpu so just try to emmit a dummy create/destroy msg to
  912. avoid this */
  913. int amdgpu_uvd_get_create_msg(struct amdgpu_ring *ring, uint32_t handle,
  914. struct dma_fence **fence)
  915. {
  916. struct amdgpu_device *adev = ring->adev;
  917. struct amdgpu_bo *bo = NULL;
  918. uint32_t *msg;
  919. int r, i;
  920. r = amdgpu_bo_create_reserved(adev, 1024, PAGE_SIZE,
  921. AMDGPU_GEM_DOMAIN_VRAM,
  922. &bo, NULL, (void **)&msg);
  923. if (r)
  924. return r;
  925. /* stitch together an UVD create msg */
  926. msg[0] = cpu_to_le32(0x00000de4);
  927. msg[1] = cpu_to_le32(0x00000000);
  928. msg[2] = cpu_to_le32(handle);
  929. msg[3] = cpu_to_le32(0x00000000);
  930. msg[4] = cpu_to_le32(0x00000000);
  931. msg[5] = cpu_to_le32(0x00000000);
  932. msg[6] = cpu_to_le32(0x00000000);
  933. msg[7] = cpu_to_le32(0x00000780);
  934. msg[8] = cpu_to_le32(0x00000440);
  935. msg[9] = cpu_to_le32(0x00000000);
  936. msg[10] = cpu_to_le32(0x01b37000);
  937. for (i = 11; i < 1024; ++i)
  938. msg[i] = cpu_to_le32(0x0);
  939. return amdgpu_uvd_send_msg(ring, bo, true, fence);
  940. }
  941. int amdgpu_uvd_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
  942. bool direct, struct dma_fence **fence)
  943. {
  944. struct amdgpu_device *adev = ring->adev;
  945. struct amdgpu_bo *bo = NULL;
  946. uint32_t *msg;
  947. int r, i;
  948. r = amdgpu_bo_create_reserved(adev, 1024, PAGE_SIZE,
  949. AMDGPU_GEM_DOMAIN_VRAM,
  950. &bo, NULL, (void **)&msg);
  951. if (r)
  952. return r;
  953. /* stitch together an UVD destroy msg */
  954. msg[0] = cpu_to_le32(0x00000de4);
  955. msg[1] = cpu_to_le32(0x00000002);
  956. msg[2] = cpu_to_le32(handle);
  957. msg[3] = cpu_to_le32(0x00000000);
  958. for (i = 4; i < 1024; ++i)
  959. msg[i] = cpu_to_le32(0x0);
  960. return amdgpu_uvd_send_msg(ring, bo, direct, fence);
  961. }
  962. static void amdgpu_uvd_idle_work_handler(struct work_struct *work)
  963. {
  964. struct amdgpu_device *adev =
  965. container_of(work, struct amdgpu_device, uvd.idle_work.work);
  966. unsigned fences = amdgpu_fence_count_emitted(&adev->uvd.ring);
  967. if (fences == 0) {
  968. if (adev->pm.dpm_enabled) {
  969. amdgpu_dpm_enable_uvd(adev, false);
  970. } else {
  971. amdgpu_asic_set_uvd_clocks(adev, 0, 0);
  972. /* shutdown the UVD block */
  973. amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_UVD,
  974. AMD_PG_STATE_GATE);
  975. amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_UVD,
  976. AMD_CG_STATE_GATE);
  977. }
  978. } else {
  979. schedule_delayed_work(&adev->uvd.idle_work, UVD_IDLE_TIMEOUT);
  980. }
  981. }
  982. void amdgpu_uvd_ring_begin_use(struct amdgpu_ring *ring)
  983. {
  984. struct amdgpu_device *adev = ring->adev;
  985. bool set_clocks;
  986. if (amdgpu_sriov_vf(adev))
  987. return;
  988. set_clocks = !cancel_delayed_work_sync(&adev->uvd.idle_work);
  989. if (set_clocks) {
  990. if (adev->pm.dpm_enabled) {
  991. amdgpu_dpm_enable_uvd(adev, true);
  992. } else {
  993. amdgpu_asic_set_uvd_clocks(adev, 53300, 40000);
  994. amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_UVD,
  995. AMD_CG_STATE_UNGATE);
  996. amdgpu_device_ip_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_UVD,
  997. AMD_PG_STATE_UNGATE);
  998. }
  999. }
  1000. }
  1001. void amdgpu_uvd_ring_end_use(struct amdgpu_ring *ring)
  1002. {
  1003. if (!amdgpu_sriov_vf(ring->adev))
  1004. schedule_delayed_work(&ring->adev->uvd.idle_work, UVD_IDLE_TIMEOUT);
  1005. }
  1006. /**
  1007. * amdgpu_uvd_ring_test_ib - test ib execution
  1008. *
  1009. * @ring: amdgpu_ring pointer
  1010. *
  1011. * Test if we can successfully execute an IB
  1012. */
  1013. int amdgpu_uvd_ring_test_ib(struct amdgpu_ring *ring, long timeout)
  1014. {
  1015. struct dma_fence *fence;
  1016. long r;
  1017. r = amdgpu_uvd_get_create_msg(ring, 1, NULL);
  1018. if (r) {
  1019. DRM_ERROR("amdgpu: failed to get create msg (%ld).\n", r);
  1020. goto error;
  1021. }
  1022. r = amdgpu_uvd_get_destroy_msg(ring, 1, true, &fence);
  1023. if (r) {
  1024. DRM_ERROR("amdgpu: failed to get destroy ib (%ld).\n", r);
  1025. goto error;
  1026. }
  1027. r = dma_fence_wait_timeout(fence, false, timeout);
  1028. if (r == 0) {
  1029. DRM_ERROR("amdgpu: IB test timed out.\n");
  1030. r = -ETIMEDOUT;
  1031. } else if (r < 0) {
  1032. DRM_ERROR("amdgpu: fence wait failed (%ld).\n", r);
  1033. } else {
  1034. DRM_DEBUG("ib test on ring %d succeeded\n", ring->idx);
  1035. r = 0;
  1036. }
  1037. dma_fence_put(fence);
  1038. error:
  1039. return r;
  1040. }
  1041. /**
  1042. * amdgpu_uvd_used_handles - returns used UVD handles
  1043. *
  1044. * @adev: amdgpu_device pointer
  1045. *
  1046. * Returns the number of UVD handles in use
  1047. */
  1048. uint32_t amdgpu_uvd_used_handles(struct amdgpu_device *adev)
  1049. {
  1050. unsigned i;
  1051. uint32_t used_handles = 0;
  1052. for (i = 0; i < adev->uvd.max_handles; ++i) {
  1053. /*
  1054. * Handles can be freed in any order, and not
  1055. * necessarily linear. So we need to count
  1056. * all non-zero handles.
  1057. */
  1058. if (atomic_read(&adev->uvd.handles[i]))
  1059. used_handles++;
  1060. }
  1061. return used_handles;
  1062. }