amdgpu_uvd.c 27 KB

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