amdgpu_uvd.c 32 KB

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