amdgpu_uvd.c 31 KB

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