amdgpu_uvd.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  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 8: /* MJPEG */
  501. min_dpb_size = 0;
  502. break;
  503. case 16: /* H265 */
  504. image_size = (ALIGN(width, 16) * ALIGN(height, 16) * 3) / 2;
  505. image_size = ALIGN(image_size, 256);
  506. num_dpb_buffer = (le32_to_cpu(msg[59]) & 0xff) + 2;
  507. min_dpb_size = image_size * num_dpb_buffer;
  508. min_ctx_size = ((width + 255) / 16) * ((height + 255) / 16)
  509. * 16 * num_dpb_buffer + 52 * 1024;
  510. break;
  511. default:
  512. DRM_ERROR("UVD codec not handled %d!\n", stream_type);
  513. return -EINVAL;
  514. }
  515. if (width > pitch) {
  516. DRM_ERROR("Invalid UVD decoding target pitch!\n");
  517. return -EINVAL;
  518. }
  519. if (dpb_size < min_dpb_size) {
  520. DRM_ERROR("Invalid dpb_size in UVD message (%d / %d)!\n",
  521. dpb_size, min_dpb_size);
  522. return -EINVAL;
  523. }
  524. buf_sizes[0x1] = dpb_size;
  525. buf_sizes[0x2] = image_size;
  526. buf_sizes[0x4] = min_ctx_size;
  527. return 0;
  528. }
  529. /**
  530. * amdgpu_uvd_cs_msg - handle UVD message
  531. *
  532. * @ctx: UVD parser context
  533. * @bo: buffer object containing the message
  534. * @offset: offset into the buffer object
  535. *
  536. * Peek into the UVD message and extract the session id.
  537. * Make sure that we don't open up to many sessions.
  538. */
  539. static int amdgpu_uvd_cs_msg(struct amdgpu_uvd_cs_ctx *ctx,
  540. struct amdgpu_bo *bo, unsigned offset)
  541. {
  542. struct amdgpu_device *adev = ctx->parser->adev;
  543. int32_t *msg, msg_type, handle;
  544. void *ptr;
  545. long r;
  546. int i;
  547. if (offset & 0x3F) {
  548. DRM_ERROR("UVD messages must be 64 byte aligned!\n");
  549. return -EINVAL;
  550. }
  551. r = amdgpu_bo_kmap(bo, &ptr);
  552. if (r) {
  553. DRM_ERROR("Failed mapping the UVD message (%ld)!\n", r);
  554. return r;
  555. }
  556. msg = ptr + offset;
  557. msg_type = msg[1];
  558. handle = msg[2];
  559. if (handle == 0) {
  560. DRM_ERROR("Invalid UVD handle!\n");
  561. return -EINVAL;
  562. }
  563. switch (msg_type) {
  564. case 0:
  565. /* it's a create msg, calc image size (width * height) */
  566. amdgpu_bo_kunmap(bo);
  567. /* try to alloc a new handle */
  568. for (i = 0; i < adev->uvd.max_handles; ++i) {
  569. if (atomic_read(&adev->uvd.handles[i]) == handle) {
  570. DRM_ERROR("Handle 0x%x already in use!\n", handle);
  571. return -EINVAL;
  572. }
  573. if (!atomic_cmpxchg(&adev->uvd.handles[i], 0, handle)) {
  574. adev->uvd.filp[i] = ctx->parser->filp;
  575. return 0;
  576. }
  577. }
  578. DRM_ERROR("No more free UVD handles!\n");
  579. return -ENOSPC;
  580. case 1:
  581. /* it's a decode msg, calc buffer sizes */
  582. r = amdgpu_uvd_cs_msg_decode(adev, msg, ctx->buf_sizes);
  583. amdgpu_bo_kunmap(bo);
  584. if (r)
  585. return r;
  586. /* validate the handle */
  587. for (i = 0; i < adev->uvd.max_handles; ++i) {
  588. if (atomic_read(&adev->uvd.handles[i]) == handle) {
  589. if (adev->uvd.filp[i] != ctx->parser->filp) {
  590. DRM_ERROR("UVD handle collision detected!\n");
  591. return -EINVAL;
  592. }
  593. return 0;
  594. }
  595. }
  596. DRM_ERROR("Invalid UVD handle 0x%x!\n", handle);
  597. return -ENOENT;
  598. case 2:
  599. /* it's a destroy msg, free the handle */
  600. for (i = 0; i < adev->uvd.max_handles; ++i)
  601. atomic_cmpxchg(&adev->uvd.handles[i], handle, 0);
  602. amdgpu_bo_kunmap(bo);
  603. return 0;
  604. default:
  605. DRM_ERROR("Illegal UVD message type (%d)!\n", msg_type);
  606. return -EINVAL;
  607. }
  608. BUG();
  609. return -EINVAL;
  610. }
  611. /**
  612. * amdgpu_uvd_cs_pass2 - second parsing round
  613. *
  614. * @ctx: UVD parser context
  615. *
  616. * Patch buffer addresses, make sure buffer sizes are correct.
  617. */
  618. static int amdgpu_uvd_cs_pass2(struct amdgpu_uvd_cs_ctx *ctx)
  619. {
  620. struct amdgpu_bo_va_mapping *mapping;
  621. struct amdgpu_bo *bo;
  622. uint32_t cmd;
  623. uint64_t start, end;
  624. uint64_t addr = amdgpu_uvd_get_addr_from_ctx(ctx);
  625. int r;
  626. mapping = amdgpu_cs_find_mapping(ctx->parser, addr, &bo);
  627. if (mapping == NULL) {
  628. DRM_ERROR("Can't find BO for addr 0x%08Lx\n", addr);
  629. return -EINVAL;
  630. }
  631. start = amdgpu_bo_gpu_offset(bo);
  632. end = (mapping->last + 1 - mapping->start);
  633. end = end * AMDGPU_GPU_PAGE_SIZE + start;
  634. addr -= mapping->start * AMDGPU_GPU_PAGE_SIZE;
  635. start += addr;
  636. amdgpu_set_ib_value(ctx->parser, ctx->ib_idx, ctx->data0,
  637. lower_32_bits(start));
  638. amdgpu_set_ib_value(ctx->parser, ctx->ib_idx, ctx->data1,
  639. upper_32_bits(start));
  640. cmd = amdgpu_get_ib_value(ctx->parser, ctx->ib_idx, ctx->idx) >> 1;
  641. if (cmd < 0x4) {
  642. if ((end - start) < ctx->buf_sizes[cmd]) {
  643. DRM_ERROR("buffer (%d) to small (%d / %d)!\n", cmd,
  644. (unsigned)(end - start),
  645. ctx->buf_sizes[cmd]);
  646. return -EINVAL;
  647. }
  648. } else if (cmd == 0x206) {
  649. if ((end - start) < ctx->buf_sizes[4]) {
  650. DRM_ERROR("buffer (%d) to small (%d / %d)!\n", cmd,
  651. (unsigned)(end - start),
  652. ctx->buf_sizes[4]);
  653. return -EINVAL;
  654. }
  655. } else if ((cmd != 0x100) && (cmd != 0x204)) {
  656. DRM_ERROR("invalid UVD command %X!\n", cmd);
  657. return -EINVAL;
  658. }
  659. if (!ctx->parser->adev->uvd.address_64_bit) {
  660. if ((start >> 28) != ((end - 1) >> 28)) {
  661. DRM_ERROR("reloc %LX-%LX crossing 256MB boundary!\n",
  662. start, end);
  663. return -EINVAL;
  664. }
  665. if ((cmd == 0 || cmd == 0x3) &&
  666. (start >> 28) != (ctx->parser->adev->uvd.gpu_addr >> 28)) {
  667. DRM_ERROR("msg/fb buffer %LX-%LX out of 256MB segment!\n",
  668. start, end);
  669. return -EINVAL;
  670. }
  671. }
  672. if (cmd == 0) {
  673. ctx->has_msg_cmd = true;
  674. r = amdgpu_uvd_cs_msg(ctx, bo, addr);
  675. if (r)
  676. return r;
  677. } else if (!ctx->has_msg_cmd) {
  678. DRM_ERROR("Message needed before other commands are send!\n");
  679. return -EINVAL;
  680. }
  681. return 0;
  682. }
  683. /**
  684. * amdgpu_uvd_cs_reg - parse register writes
  685. *
  686. * @ctx: UVD parser context
  687. * @cb: callback function
  688. *
  689. * Parse the register writes, call cb on each complete command.
  690. */
  691. static int amdgpu_uvd_cs_reg(struct amdgpu_uvd_cs_ctx *ctx,
  692. int (*cb)(struct amdgpu_uvd_cs_ctx *ctx))
  693. {
  694. struct amdgpu_ib *ib = &ctx->parser->job->ibs[ctx->ib_idx];
  695. int i, r;
  696. ctx->idx++;
  697. for (i = 0; i <= ctx->count; ++i) {
  698. unsigned reg = ctx->reg + i;
  699. if (ctx->idx >= ib->length_dw) {
  700. DRM_ERROR("Register command after end of CS!\n");
  701. return -EINVAL;
  702. }
  703. switch (reg) {
  704. case mmUVD_GPCOM_VCPU_DATA0:
  705. ctx->data0 = ctx->idx;
  706. break;
  707. case mmUVD_GPCOM_VCPU_DATA1:
  708. ctx->data1 = ctx->idx;
  709. break;
  710. case mmUVD_GPCOM_VCPU_CMD:
  711. r = cb(ctx);
  712. if (r)
  713. return r;
  714. break;
  715. case mmUVD_ENGINE_CNTL:
  716. case mmUVD_NO_OP:
  717. break;
  718. default:
  719. DRM_ERROR("Invalid reg 0x%X!\n", reg);
  720. return -EINVAL;
  721. }
  722. ctx->idx++;
  723. }
  724. return 0;
  725. }
  726. /**
  727. * amdgpu_uvd_cs_packets - parse UVD packets
  728. *
  729. * @ctx: UVD parser context
  730. * @cb: callback function
  731. *
  732. * Parse the command stream packets.
  733. */
  734. static int amdgpu_uvd_cs_packets(struct amdgpu_uvd_cs_ctx *ctx,
  735. int (*cb)(struct amdgpu_uvd_cs_ctx *ctx))
  736. {
  737. struct amdgpu_ib *ib = &ctx->parser->job->ibs[ctx->ib_idx];
  738. int r;
  739. for (ctx->idx = 0 ; ctx->idx < ib->length_dw; ) {
  740. uint32_t cmd = amdgpu_get_ib_value(ctx->parser, ctx->ib_idx, ctx->idx);
  741. unsigned type = CP_PACKET_GET_TYPE(cmd);
  742. switch (type) {
  743. case PACKET_TYPE0:
  744. ctx->reg = CP_PACKET0_GET_REG(cmd);
  745. ctx->count = CP_PACKET_GET_COUNT(cmd);
  746. r = amdgpu_uvd_cs_reg(ctx, cb);
  747. if (r)
  748. return r;
  749. break;
  750. case PACKET_TYPE2:
  751. ++ctx->idx;
  752. break;
  753. default:
  754. DRM_ERROR("Unknown packet type %d !\n", type);
  755. return -EINVAL;
  756. }
  757. }
  758. return 0;
  759. }
  760. /**
  761. * amdgpu_uvd_ring_parse_cs - UVD command submission parser
  762. *
  763. * @parser: Command submission parser context
  764. *
  765. * Parse the command stream, patch in addresses as necessary.
  766. */
  767. int amdgpu_uvd_ring_parse_cs(struct amdgpu_cs_parser *parser, uint32_t ib_idx)
  768. {
  769. struct amdgpu_uvd_cs_ctx ctx = {};
  770. unsigned buf_sizes[] = {
  771. [0x00000000] = 2048,
  772. [0x00000001] = 0xFFFFFFFF,
  773. [0x00000002] = 0xFFFFFFFF,
  774. [0x00000003] = 2048,
  775. [0x00000004] = 0xFFFFFFFF,
  776. };
  777. struct amdgpu_ib *ib = &parser->job->ibs[ib_idx];
  778. int r;
  779. parser->job->vm = NULL;
  780. ib->gpu_addr = amdgpu_sa_bo_gpu_addr(ib->sa_bo);
  781. if (ib->length_dw % 16) {
  782. DRM_ERROR("UVD IB length (%d) not 16 dwords aligned!\n",
  783. ib->length_dw);
  784. return -EINVAL;
  785. }
  786. r = amdgpu_cs_sysvm_access_required(parser);
  787. if (r)
  788. return r;
  789. ctx.parser = parser;
  790. ctx.buf_sizes = buf_sizes;
  791. ctx.ib_idx = ib_idx;
  792. /* first round only required on chips without UVD 64 bit address support */
  793. if (!parser->adev->uvd.address_64_bit) {
  794. /* first round, make sure the buffers are actually in the UVD segment */
  795. r = amdgpu_uvd_cs_packets(&ctx, amdgpu_uvd_cs_pass1);
  796. if (r)
  797. return r;
  798. }
  799. /* second round, patch buffer addresses into the command stream */
  800. r = amdgpu_uvd_cs_packets(&ctx, amdgpu_uvd_cs_pass2);
  801. if (r)
  802. return r;
  803. if (!ctx.has_msg_cmd) {
  804. DRM_ERROR("UVD-IBs need a msg command!\n");
  805. return -EINVAL;
  806. }
  807. return 0;
  808. }
  809. static int amdgpu_uvd_send_msg(struct amdgpu_ring *ring, struct amdgpu_bo *bo,
  810. bool direct, struct dma_fence **fence)
  811. {
  812. struct ttm_validate_buffer tv;
  813. struct ww_acquire_ctx ticket;
  814. struct list_head head;
  815. struct amdgpu_job *job;
  816. struct amdgpu_ib *ib;
  817. struct dma_fence *f = NULL;
  818. struct amdgpu_device *adev = ring->adev;
  819. uint64_t addr;
  820. uint32_t data[4];
  821. int i, r;
  822. memset(&tv, 0, sizeof(tv));
  823. tv.bo = &bo->tbo;
  824. INIT_LIST_HEAD(&head);
  825. list_add(&tv.head, &head);
  826. r = ttm_eu_reserve_buffers(&ticket, &head, true, NULL);
  827. if (r)
  828. return r;
  829. if (!ring->adev->uvd.address_64_bit) {
  830. amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_VRAM);
  831. amdgpu_uvd_force_into_uvd_segment(bo);
  832. }
  833. r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
  834. if (r)
  835. goto err;
  836. r = amdgpu_job_alloc_with_ib(adev, 64, &job);
  837. if (r)
  838. goto err;
  839. if (adev->asic_type >= CHIP_VEGA10) {
  840. data[0] = PACKET0(mmUVD_GPCOM_VCPU_DATA0_VEGA10, 0);
  841. data[1] = PACKET0(mmUVD_GPCOM_VCPU_DATA1_VEGA10, 0);
  842. data[2] = PACKET0(mmUVD_GPCOM_VCPU_CMD_VEGA10, 0);
  843. data[3] = PACKET0(mmUVD_NO_OP_VEGA10, 0);
  844. } else {
  845. data[0] = PACKET0(mmUVD_GPCOM_VCPU_DATA0, 0);
  846. data[1] = PACKET0(mmUVD_GPCOM_VCPU_DATA1, 0);
  847. data[2] = PACKET0(mmUVD_GPCOM_VCPU_CMD, 0);
  848. data[3] = PACKET0(mmUVD_NO_OP, 0);
  849. }
  850. ib = &job->ibs[0];
  851. addr = amdgpu_bo_gpu_offset(bo);
  852. ib->ptr[0] = data[0];
  853. ib->ptr[1] = addr;
  854. ib->ptr[2] = data[1];
  855. ib->ptr[3] = addr >> 32;
  856. ib->ptr[4] = data[2];
  857. ib->ptr[5] = 0;
  858. for (i = 6; i < 16; i += 2) {
  859. ib->ptr[i] = data[3];
  860. ib->ptr[i+1] = 0;
  861. }
  862. ib->length_dw = 16;
  863. if (direct) {
  864. r = amdgpu_ib_schedule(ring, 1, ib, NULL, &f);
  865. job->fence = dma_fence_get(f);
  866. if (r)
  867. goto err_free;
  868. amdgpu_job_free(job);
  869. } else {
  870. r = amdgpu_job_submit(job, ring, &adev->uvd.entity,
  871. AMDGPU_FENCE_OWNER_UNDEFINED, &f);
  872. if (r)
  873. goto err_free;
  874. }
  875. ttm_eu_fence_buffer_objects(&ticket, &head, f);
  876. if (fence)
  877. *fence = dma_fence_get(f);
  878. amdgpu_bo_unref(&bo);
  879. dma_fence_put(f);
  880. return 0;
  881. err_free:
  882. amdgpu_job_free(job);
  883. err:
  884. ttm_eu_backoff_reservation(&ticket, &head);
  885. return r;
  886. }
  887. /* multiple fence commands without any stream commands in between can
  888. crash the vcpu so just try to emmit a dummy create/destroy msg to
  889. avoid this */
  890. int amdgpu_uvd_get_create_msg(struct amdgpu_ring *ring, uint32_t handle,
  891. struct dma_fence **fence)
  892. {
  893. struct amdgpu_device *adev = ring->adev;
  894. struct amdgpu_bo *bo;
  895. uint32_t *msg;
  896. int r, i;
  897. r = amdgpu_bo_create(adev, 1024, PAGE_SIZE, true,
  898. AMDGPU_GEM_DOMAIN_VRAM,
  899. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
  900. AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
  901. NULL, NULL, 0, &bo);
  902. if (r)
  903. return r;
  904. r = amdgpu_bo_reserve(bo, false);
  905. if (r) {
  906. amdgpu_bo_unref(&bo);
  907. return r;
  908. }
  909. r = amdgpu_bo_kmap(bo, (void **)&msg);
  910. if (r) {
  911. amdgpu_bo_unreserve(bo);
  912. amdgpu_bo_unref(&bo);
  913. return r;
  914. }
  915. /* stitch together an UVD create msg */
  916. msg[0] = cpu_to_le32(0x00000de4);
  917. msg[1] = cpu_to_le32(0x00000000);
  918. msg[2] = cpu_to_le32(handle);
  919. msg[3] = cpu_to_le32(0x00000000);
  920. msg[4] = cpu_to_le32(0x00000000);
  921. msg[5] = cpu_to_le32(0x00000000);
  922. msg[6] = cpu_to_le32(0x00000000);
  923. msg[7] = cpu_to_le32(0x00000780);
  924. msg[8] = cpu_to_le32(0x00000440);
  925. msg[9] = cpu_to_le32(0x00000000);
  926. msg[10] = cpu_to_le32(0x01b37000);
  927. for (i = 11; i < 1024; ++i)
  928. msg[i] = cpu_to_le32(0x0);
  929. amdgpu_bo_kunmap(bo);
  930. amdgpu_bo_unreserve(bo);
  931. return amdgpu_uvd_send_msg(ring, bo, true, fence);
  932. }
  933. int amdgpu_uvd_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
  934. bool direct, struct dma_fence **fence)
  935. {
  936. struct amdgpu_device *adev = ring->adev;
  937. struct amdgpu_bo *bo;
  938. uint32_t *msg;
  939. int r, i;
  940. r = amdgpu_bo_create(adev, 1024, PAGE_SIZE, true,
  941. AMDGPU_GEM_DOMAIN_VRAM,
  942. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
  943. AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
  944. NULL, NULL, 0, &bo);
  945. if (r)
  946. return r;
  947. r = amdgpu_bo_reserve(bo, false);
  948. if (r) {
  949. amdgpu_bo_unref(&bo);
  950. return r;
  951. }
  952. r = amdgpu_bo_kmap(bo, (void **)&msg);
  953. if (r) {
  954. amdgpu_bo_unreserve(bo);
  955. amdgpu_bo_unref(&bo);
  956. return r;
  957. }
  958. /* stitch together an UVD destroy msg */
  959. msg[0] = cpu_to_le32(0x00000de4);
  960. msg[1] = cpu_to_le32(0x00000002);
  961. msg[2] = cpu_to_le32(handle);
  962. msg[3] = cpu_to_le32(0x00000000);
  963. for (i = 4; i < 1024; ++i)
  964. msg[i] = cpu_to_le32(0x0);
  965. amdgpu_bo_kunmap(bo);
  966. amdgpu_bo_unreserve(bo);
  967. return amdgpu_uvd_send_msg(ring, bo, direct, fence);
  968. }
  969. static void amdgpu_uvd_idle_work_handler(struct work_struct *work)
  970. {
  971. struct amdgpu_device *adev =
  972. container_of(work, struct amdgpu_device, uvd.idle_work.work);
  973. unsigned fences = amdgpu_fence_count_emitted(&adev->uvd.ring);
  974. if (amdgpu_sriov_vf(adev))
  975. return;
  976. if (fences == 0) {
  977. if (adev->pm.dpm_enabled) {
  978. amdgpu_dpm_enable_uvd(adev, false);
  979. } else {
  980. amdgpu_asic_set_uvd_clocks(adev, 0, 0);
  981. /* shutdown the UVD block */
  982. amdgpu_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_UVD,
  983. AMD_PG_STATE_GATE);
  984. amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_UVD,
  985. AMD_CG_STATE_GATE);
  986. }
  987. } else {
  988. schedule_delayed_work(&adev->uvd.idle_work, UVD_IDLE_TIMEOUT);
  989. }
  990. }
  991. void amdgpu_uvd_ring_begin_use(struct amdgpu_ring *ring)
  992. {
  993. struct amdgpu_device *adev = ring->adev;
  994. bool set_clocks = !cancel_delayed_work_sync(&adev->uvd.idle_work);
  995. if (amdgpu_sriov_vf(adev))
  996. return;
  997. if (set_clocks) {
  998. if (adev->pm.dpm_enabled) {
  999. amdgpu_dpm_enable_uvd(adev, true);
  1000. } else {
  1001. amdgpu_asic_set_uvd_clocks(adev, 53300, 40000);
  1002. amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_UVD,
  1003. AMD_CG_STATE_UNGATE);
  1004. amdgpu_set_powergating_state(adev, AMD_IP_BLOCK_TYPE_UVD,
  1005. AMD_PG_STATE_UNGATE);
  1006. }
  1007. }
  1008. }
  1009. void amdgpu_uvd_ring_end_use(struct amdgpu_ring *ring)
  1010. {
  1011. schedule_delayed_work(&ring->adev->uvd.idle_work, UVD_IDLE_TIMEOUT);
  1012. }
  1013. /**
  1014. * amdgpu_uvd_ring_test_ib - test ib execution
  1015. *
  1016. * @ring: amdgpu_ring pointer
  1017. *
  1018. * Test if we can successfully execute an IB
  1019. */
  1020. int amdgpu_uvd_ring_test_ib(struct amdgpu_ring *ring, long timeout)
  1021. {
  1022. struct dma_fence *fence;
  1023. long r;
  1024. r = amdgpu_uvd_get_create_msg(ring, 1, NULL);
  1025. if (r) {
  1026. DRM_ERROR("amdgpu: failed to get create msg (%ld).\n", r);
  1027. goto error;
  1028. }
  1029. r = amdgpu_uvd_get_destroy_msg(ring, 1, true, &fence);
  1030. if (r) {
  1031. DRM_ERROR("amdgpu: failed to get destroy ib (%ld).\n", r);
  1032. goto error;
  1033. }
  1034. r = dma_fence_wait_timeout(fence, false, timeout);
  1035. if (r == 0) {
  1036. DRM_ERROR("amdgpu: IB test timed out.\n");
  1037. r = -ETIMEDOUT;
  1038. } else if (r < 0) {
  1039. DRM_ERROR("amdgpu: fence wait failed (%ld).\n", r);
  1040. } else {
  1041. DRM_INFO("ib test on ring %d succeeded\n", ring->idx);
  1042. r = 0;
  1043. }
  1044. dma_fence_put(fence);
  1045. error:
  1046. return r;
  1047. }
  1048. /**
  1049. * amdgpu_uvd_used_handles - returns used UVD handles
  1050. *
  1051. * @adev: amdgpu_device pointer
  1052. *
  1053. * Returns the number of UVD handles in use
  1054. */
  1055. uint32_t amdgpu_uvd_used_handles(struct amdgpu_device *adev)
  1056. {
  1057. unsigned i;
  1058. uint32_t used_handles = 0;
  1059. for (i = 0; i < adev->uvd.max_handles; ++i) {
  1060. /*
  1061. * Handles can be freed in any order, and not
  1062. * necessarily linear. So we need to count
  1063. * all non-zero handles.
  1064. */
  1065. if (atomic_read(&adev->uvd.handles[i]))
  1066. used_handles++;
  1067. }
  1068. return used_handles;
  1069. }