amdgpu_uvd.c 24 KB

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