amdgpu_uvd.c 26 KB

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