amdgpu_uvd.c 29 KB

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