amdgpu_uvd.c 25 KB

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