amdgpu_uvd.c 24 KB

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