amdgpu_uvd.c 32 KB

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