amdgpu_uvd.c 30 KB

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