amdgpu_uvd.c 30 KB

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