amdgpu_uvd.c 32 KB

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