amdgpu_uvd.c 25 KB

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