amdgpu_uvd.c 28 KB

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