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. 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 amdgpu_bo_va_mapping *mapping;
  349. struct amdgpu_bo *bo;
  350. uint32_t cmd;
  351. uint64_t addr = amdgpu_uvd_get_addr_from_ctx(ctx);
  352. int r = 0;
  353. r = amdgpu_cs_find_mapping(ctx->parser, addr, &bo, &mapping);
  354. if (r) {
  355. DRM_ERROR("Can't find BO for addr 0x%08Lx\n", addr);
  356. return r;
  357. }
  358. if (!ctx->parser->adev->uvd.address_64_bit) {
  359. /* check if it's a message or feedback command */
  360. cmd = amdgpu_get_ib_value(ctx->parser, ctx->ib_idx, ctx->idx) >> 1;
  361. if (cmd == 0x0 || cmd == 0x3) {
  362. /* yes, force it into VRAM */
  363. uint32_t domain = AMDGPU_GEM_DOMAIN_VRAM;
  364. amdgpu_ttm_placement_from_domain(bo, domain);
  365. }
  366. amdgpu_uvd_force_into_uvd_segment(bo);
  367. r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
  368. }
  369. return r;
  370. }
  371. /**
  372. * amdgpu_uvd_cs_msg_decode - handle UVD decode message
  373. *
  374. * @msg: pointer to message structure
  375. * @buf_sizes: returned buffer sizes
  376. *
  377. * Peek into the decode message and calculate the necessary buffer sizes.
  378. */
  379. static int amdgpu_uvd_cs_msg_decode(struct amdgpu_device *adev, uint32_t *msg,
  380. unsigned buf_sizes[])
  381. {
  382. unsigned stream_type = msg[4];
  383. unsigned width = msg[6];
  384. unsigned height = msg[7];
  385. unsigned dpb_size = msg[9];
  386. unsigned pitch = msg[28];
  387. unsigned level = msg[57];
  388. unsigned width_in_mb = width / 16;
  389. unsigned height_in_mb = ALIGN(height / 16, 2);
  390. unsigned fs_in_mb = width_in_mb * height_in_mb;
  391. unsigned image_size, tmp, min_dpb_size, num_dpb_buffer;
  392. unsigned min_ctx_size = ~0;
  393. image_size = width * height;
  394. image_size += image_size / 2;
  395. image_size = ALIGN(image_size, 1024);
  396. switch (stream_type) {
  397. case 0: /* H264 */
  398. switch(level) {
  399. case 30:
  400. num_dpb_buffer = 8100 / fs_in_mb;
  401. break;
  402. case 31:
  403. num_dpb_buffer = 18000 / fs_in_mb;
  404. break;
  405. case 32:
  406. num_dpb_buffer = 20480 / fs_in_mb;
  407. break;
  408. case 41:
  409. num_dpb_buffer = 32768 / fs_in_mb;
  410. break;
  411. case 42:
  412. num_dpb_buffer = 34816 / fs_in_mb;
  413. break;
  414. case 50:
  415. num_dpb_buffer = 110400 / fs_in_mb;
  416. break;
  417. case 51:
  418. num_dpb_buffer = 184320 / fs_in_mb;
  419. break;
  420. default:
  421. num_dpb_buffer = 184320 / fs_in_mb;
  422. break;
  423. }
  424. num_dpb_buffer++;
  425. if (num_dpb_buffer > 17)
  426. num_dpb_buffer = 17;
  427. /* reference picture buffer */
  428. min_dpb_size = image_size * num_dpb_buffer;
  429. /* macroblock context buffer */
  430. min_dpb_size += width_in_mb * height_in_mb * num_dpb_buffer * 192;
  431. /* IT surface buffer */
  432. min_dpb_size += width_in_mb * height_in_mb * 32;
  433. break;
  434. case 1: /* VC1 */
  435. /* reference picture buffer */
  436. min_dpb_size = image_size * 3;
  437. /* CONTEXT_BUFFER */
  438. min_dpb_size += width_in_mb * height_in_mb * 128;
  439. /* IT surface buffer */
  440. min_dpb_size += width_in_mb * 64;
  441. /* DB surface buffer */
  442. min_dpb_size += width_in_mb * 128;
  443. /* BP */
  444. tmp = max(width_in_mb, height_in_mb);
  445. min_dpb_size += ALIGN(tmp * 7 * 16, 64);
  446. break;
  447. case 3: /* MPEG2 */
  448. /* reference picture buffer */
  449. min_dpb_size = image_size * 3;
  450. break;
  451. case 4: /* MPEG4 */
  452. /* reference picture buffer */
  453. min_dpb_size = image_size * 3;
  454. /* CM */
  455. min_dpb_size += width_in_mb * height_in_mb * 64;
  456. /* IT surface buffer */
  457. min_dpb_size += ALIGN(width_in_mb * height_in_mb * 32, 64);
  458. break;
  459. case 7: /* H264 Perf */
  460. switch(level) {
  461. case 30:
  462. num_dpb_buffer = 8100 / fs_in_mb;
  463. break;
  464. case 31:
  465. num_dpb_buffer = 18000 / fs_in_mb;
  466. break;
  467. case 32:
  468. num_dpb_buffer = 20480 / fs_in_mb;
  469. break;
  470. case 41:
  471. num_dpb_buffer = 32768 / fs_in_mb;
  472. break;
  473. case 42:
  474. num_dpb_buffer = 34816 / fs_in_mb;
  475. break;
  476. case 50:
  477. num_dpb_buffer = 110400 / fs_in_mb;
  478. break;
  479. case 51:
  480. num_dpb_buffer = 184320 / fs_in_mb;
  481. break;
  482. default:
  483. num_dpb_buffer = 184320 / fs_in_mb;
  484. break;
  485. }
  486. num_dpb_buffer++;
  487. if (num_dpb_buffer > 17)
  488. num_dpb_buffer = 17;
  489. /* reference picture buffer */
  490. min_dpb_size = image_size * num_dpb_buffer;
  491. if (!adev->uvd.use_ctx_buf){
  492. /* macroblock context buffer */
  493. min_dpb_size +=
  494. width_in_mb * height_in_mb * num_dpb_buffer * 192;
  495. /* IT surface buffer */
  496. min_dpb_size += width_in_mb * height_in_mb * 32;
  497. } else {
  498. /* macroblock context buffer */
  499. min_ctx_size =
  500. width_in_mb * height_in_mb * num_dpb_buffer * 192;
  501. }
  502. break;
  503. case 8: /* MJPEG */
  504. min_dpb_size = 0;
  505. break;
  506. case 16: /* H265 */
  507. image_size = (ALIGN(width, 16) * ALIGN(height, 16) * 3) / 2;
  508. image_size = ALIGN(image_size, 256);
  509. num_dpb_buffer = (le32_to_cpu(msg[59]) & 0xff) + 2;
  510. min_dpb_size = image_size * num_dpb_buffer;
  511. min_ctx_size = ((width + 255) / 16) * ((height + 255) / 16)
  512. * 16 * num_dpb_buffer + 52 * 1024;
  513. break;
  514. default:
  515. DRM_ERROR("UVD codec not handled %d!\n", stream_type);
  516. return -EINVAL;
  517. }
  518. if (width > pitch) {
  519. DRM_ERROR("Invalid UVD decoding target pitch!\n");
  520. return -EINVAL;
  521. }
  522. if (dpb_size < min_dpb_size) {
  523. DRM_ERROR("Invalid dpb_size in UVD message (%d / %d)!\n",
  524. dpb_size, min_dpb_size);
  525. return -EINVAL;
  526. }
  527. buf_sizes[0x1] = dpb_size;
  528. buf_sizes[0x2] = image_size;
  529. buf_sizes[0x4] = min_ctx_size;
  530. return 0;
  531. }
  532. /**
  533. * amdgpu_uvd_cs_msg - handle UVD message
  534. *
  535. * @ctx: UVD parser context
  536. * @bo: buffer object containing the message
  537. * @offset: offset into the buffer object
  538. *
  539. * Peek into the UVD message and extract the session id.
  540. * Make sure that we don't open up to many sessions.
  541. */
  542. static int amdgpu_uvd_cs_msg(struct amdgpu_uvd_cs_ctx *ctx,
  543. struct amdgpu_bo *bo, unsigned offset)
  544. {
  545. struct amdgpu_device *adev = ctx->parser->adev;
  546. int32_t *msg, msg_type, handle;
  547. void *ptr;
  548. long r;
  549. int i;
  550. if (offset & 0x3F) {
  551. DRM_ERROR("UVD messages must be 64 byte aligned!\n");
  552. return -EINVAL;
  553. }
  554. r = amdgpu_bo_kmap(bo, &ptr);
  555. if (r) {
  556. DRM_ERROR("Failed mapping the UVD message (%ld)!\n", r);
  557. return r;
  558. }
  559. msg = ptr + offset;
  560. msg_type = msg[1];
  561. handle = msg[2];
  562. if (handle == 0) {
  563. DRM_ERROR("Invalid UVD handle!\n");
  564. return -EINVAL;
  565. }
  566. switch (msg_type) {
  567. case 0:
  568. /* it's a create msg, calc image size (width * height) */
  569. amdgpu_bo_kunmap(bo);
  570. /* try to alloc a new handle */
  571. for (i = 0; i < adev->uvd.max_handles; ++i) {
  572. if (atomic_read(&adev->uvd.handles[i]) == handle) {
  573. DRM_ERROR("Handle 0x%x already in use!\n", handle);
  574. return -EINVAL;
  575. }
  576. if (!atomic_cmpxchg(&adev->uvd.handles[i], 0, handle)) {
  577. adev->uvd.filp[i] = ctx->parser->filp;
  578. return 0;
  579. }
  580. }
  581. DRM_ERROR("No more free UVD handles!\n");
  582. return -ENOSPC;
  583. case 1:
  584. /* it's a decode msg, calc buffer sizes */
  585. r = amdgpu_uvd_cs_msg_decode(adev, msg, ctx->buf_sizes);
  586. amdgpu_bo_kunmap(bo);
  587. if (r)
  588. return r;
  589. /* validate the handle */
  590. for (i = 0; i < adev->uvd.max_handles; ++i) {
  591. if (atomic_read(&adev->uvd.handles[i]) == handle) {
  592. if (adev->uvd.filp[i] != ctx->parser->filp) {
  593. DRM_ERROR("UVD handle collision detected!\n");
  594. return -EINVAL;
  595. }
  596. return 0;
  597. }
  598. }
  599. DRM_ERROR("Invalid UVD handle 0x%x!\n", handle);
  600. return -ENOENT;
  601. case 2:
  602. /* it's a destroy msg, free the handle */
  603. for (i = 0; i < adev->uvd.max_handles; ++i)
  604. atomic_cmpxchg(&adev->uvd.handles[i], handle, 0);
  605. amdgpu_bo_kunmap(bo);
  606. return 0;
  607. default:
  608. DRM_ERROR("Illegal UVD message type (%d)!\n", msg_type);
  609. return -EINVAL;
  610. }
  611. BUG();
  612. return -EINVAL;
  613. }
  614. /**
  615. * amdgpu_uvd_cs_pass2 - second parsing round
  616. *
  617. * @ctx: UVD parser context
  618. *
  619. * Patch buffer addresses, make sure buffer sizes are correct.
  620. */
  621. static int amdgpu_uvd_cs_pass2(struct amdgpu_uvd_cs_ctx *ctx)
  622. {
  623. struct amdgpu_bo_va_mapping *mapping;
  624. struct amdgpu_bo *bo;
  625. uint32_t cmd;
  626. uint64_t start, end;
  627. uint64_t addr = amdgpu_uvd_get_addr_from_ctx(ctx);
  628. int r;
  629. r = amdgpu_cs_find_mapping(ctx->parser, addr, &bo, &mapping);
  630. if (r) {
  631. DRM_ERROR("Can't find BO for addr 0x%08Lx\n", addr);
  632. return r;
  633. }
  634. start = amdgpu_bo_gpu_offset(bo);
  635. end = (mapping->last + 1 - mapping->start);
  636. end = end * AMDGPU_GPU_PAGE_SIZE + start;
  637. addr -= mapping->start * AMDGPU_GPU_PAGE_SIZE;
  638. start += addr;
  639. amdgpu_set_ib_value(ctx->parser, ctx->ib_idx, ctx->data0,
  640. lower_32_bits(start));
  641. amdgpu_set_ib_value(ctx->parser, ctx->ib_idx, ctx->data1,
  642. upper_32_bits(start));
  643. cmd = amdgpu_get_ib_value(ctx->parser, ctx->ib_idx, ctx->idx) >> 1;
  644. if (cmd < 0x4) {
  645. if ((end - start) < ctx->buf_sizes[cmd]) {
  646. DRM_ERROR("buffer (%d) to small (%d / %d)!\n", cmd,
  647. (unsigned)(end - start),
  648. ctx->buf_sizes[cmd]);
  649. return -EINVAL;
  650. }
  651. } else if (cmd == 0x206) {
  652. if ((end - start) < ctx->buf_sizes[4]) {
  653. DRM_ERROR("buffer (%d) to small (%d / %d)!\n", cmd,
  654. (unsigned)(end - start),
  655. ctx->buf_sizes[4]);
  656. return -EINVAL;
  657. }
  658. } else if ((cmd != 0x100) && (cmd != 0x204)) {
  659. DRM_ERROR("invalid UVD command %X!\n", cmd);
  660. return -EINVAL;
  661. }
  662. if (!ctx->parser->adev->uvd.address_64_bit) {
  663. if ((start >> 28) != ((end - 1) >> 28)) {
  664. DRM_ERROR("reloc %LX-%LX crossing 256MB boundary!\n",
  665. start, end);
  666. return -EINVAL;
  667. }
  668. if ((cmd == 0 || cmd == 0x3) &&
  669. (start >> 28) != (ctx->parser->adev->uvd.gpu_addr >> 28)) {
  670. DRM_ERROR("msg/fb buffer %LX-%LX out of 256MB segment!\n",
  671. start, end);
  672. return -EINVAL;
  673. }
  674. }
  675. if (cmd == 0) {
  676. ctx->has_msg_cmd = true;
  677. r = amdgpu_uvd_cs_msg(ctx, bo, addr);
  678. if (r)
  679. return r;
  680. } else if (!ctx->has_msg_cmd) {
  681. DRM_ERROR("Message needed before other commands are send!\n");
  682. return -EINVAL;
  683. }
  684. return 0;
  685. }
  686. /**
  687. * amdgpu_uvd_cs_reg - parse register writes
  688. *
  689. * @ctx: UVD parser context
  690. * @cb: callback function
  691. *
  692. * Parse the register writes, call cb on each complete command.
  693. */
  694. static int amdgpu_uvd_cs_reg(struct amdgpu_uvd_cs_ctx *ctx,
  695. int (*cb)(struct amdgpu_uvd_cs_ctx *ctx))
  696. {
  697. struct amdgpu_ib *ib = &ctx->parser->job->ibs[ctx->ib_idx];
  698. int i, r;
  699. ctx->idx++;
  700. for (i = 0; i <= ctx->count; ++i) {
  701. unsigned reg = ctx->reg + i;
  702. if (ctx->idx >= ib->length_dw) {
  703. DRM_ERROR("Register command after end of CS!\n");
  704. return -EINVAL;
  705. }
  706. switch (reg) {
  707. case mmUVD_GPCOM_VCPU_DATA0:
  708. ctx->data0 = ctx->idx;
  709. break;
  710. case mmUVD_GPCOM_VCPU_DATA1:
  711. ctx->data1 = ctx->idx;
  712. break;
  713. case mmUVD_GPCOM_VCPU_CMD:
  714. r = cb(ctx);
  715. if (r)
  716. return r;
  717. break;
  718. case mmUVD_ENGINE_CNTL:
  719. case mmUVD_NO_OP:
  720. break;
  721. default:
  722. DRM_ERROR("Invalid reg 0x%X!\n", reg);
  723. return -EINVAL;
  724. }
  725. ctx->idx++;
  726. }
  727. return 0;
  728. }
  729. /**
  730. * amdgpu_uvd_cs_packets - parse UVD packets
  731. *
  732. * @ctx: UVD parser context
  733. * @cb: callback function
  734. *
  735. * Parse the command stream packets.
  736. */
  737. static int amdgpu_uvd_cs_packets(struct amdgpu_uvd_cs_ctx *ctx,
  738. int (*cb)(struct amdgpu_uvd_cs_ctx *ctx))
  739. {
  740. struct amdgpu_ib *ib = &ctx->parser->job->ibs[ctx->ib_idx];
  741. int r;
  742. for (ctx->idx = 0 ; ctx->idx < ib->length_dw; ) {
  743. uint32_t cmd = amdgpu_get_ib_value(ctx->parser, ctx->ib_idx, ctx->idx);
  744. unsigned type = CP_PACKET_GET_TYPE(cmd);
  745. switch (type) {
  746. case PACKET_TYPE0:
  747. ctx->reg = CP_PACKET0_GET_REG(cmd);
  748. ctx->count = CP_PACKET_GET_COUNT(cmd);
  749. r = amdgpu_uvd_cs_reg(ctx, cb);
  750. if (r)
  751. return r;
  752. break;
  753. case PACKET_TYPE2:
  754. ++ctx->idx;
  755. break;
  756. default:
  757. DRM_ERROR("Unknown packet type %d !\n", type);
  758. return -EINVAL;
  759. }
  760. }
  761. return 0;
  762. }
  763. /**
  764. * amdgpu_uvd_ring_parse_cs - UVD command submission parser
  765. *
  766. * @parser: Command submission parser context
  767. *
  768. * Parse the command stream, patch in addresses as necessary.
  769. */
  770. int amdgpu_uvd_ring_parse_cs(struct amdgpu_cs_parser *parser, uint32_t ib_idx)
  771. {
  772. struct amdgpu_uvd_cs_ctx ctx = {};
  773. unsigned buf_sizes[] = {
  774. [0x00000000] = 2048,
  775. [0x00000001] = 0xFFFFFFFF,
  776. [0x00000002] = 0xFFFFFFFF,
  777. [0x00000003] = 2048,
  778. [0x00000004] = 0xFFFFFFFF,
  779. };
  780. struct amdgpu_ib *ib = &parser->job->ibs[ib_idx];
  781. int r;
  782. parser->job->vm = NULL;
  783. ib->gpu_addr = amdgpu_sa_bo_gpu_addr(ib->sa_bo);
  784. if (ib->length_dw % 16) {
  785. DRM_ERROR("UVD IB length (%d) not 16 dwords aligned!\n",
  786. ib->length_dw);
  787. return -EINVAL;
  788. }
  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. }