amdgpu_uvd.c 25 KB

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