amdgpu_uvd.c 30 KB

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