amdgpu_uvd.c 32 KB

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