amdgpu_uvd.c 32 KB

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