amdgpu_uvd.c 26 KB

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