amdgpu_uvd.c 28 KB

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