amdgpu_uvd.c 30 KB

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