amdgpu_uvd.c 32 KB

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