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