amdgpu_uvd.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  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 dma_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. dma_fence_wait(fence, false);
  294. dma_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. static u64 amdgpu_uvd_get_addr_from_ctx(struct amdgpu_uvd_cs_ctx *ctx)
  309. {
  310. uint32_t lo, hi;
  311. uint64_t addr;
  312. lo = amdgpu_get_ib_value(ctx->parser, ctx->ib_idx, ctx->data0);
  313. hi = amdgpu_get_ib_value(ctx->parser, ctx->ib_idx, ctx->data1);
  314. addr = ((uint64_t)lo) | (((uint64_t)hi) << 32);
  315. return addr;
  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;
  330. uint64_t addr = amdgpu_uvd_get_addr_from_ctx(ctx);
  331. int r = 0;
  332. mapping = amdgpu_cs_find_mapping(ctx->parser, addr, &bo);
  333. if (mapping == NULL) {
  334. DRM_ERROR("Can't find BO for addr 0x%08Lx\n", addr);
  335. return -EINVAL;
  336. }
  337. if (!ctx->parser->adev->uvd.address_64_bit) {
  338. /* check if it's a message or feedback command */
  339. cmd = amdgpu_get_ib_value(ctx->parser, ctx->ib_idx, ctx->idx) >> 1;
  340. if (cmd == 0x0 || cmd == 0x3) {
  341. /* yes, force it into VRAM */
  342. uint32_t domain = AMDGPU_GEM_DOMAIN_VRAM;
  343. amdgpu_ttm_placement_from_domain(bo, domain);
  344. }
  345. amdgpu_uvd_force_into_uvd_segment(bo);
  346. r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
  347. }
  348. return r;
  349. }
  350. /**
  351. * amdgpu_uvd_cs_msg_decode - handle UVD decode message
  352. *
  353. * @msg: pointer to message structure
  354. * @buf_sizes: returned buffer sizes
  355. *
  356. * Peek into the decode message and calculate the necessary buffer sizes.
  357. */
  358. static int amdgpu_uvd_cs_msg_decode(struct amdgpu_device *adev, uint32_t *msg,
  359. unsigned buf_sizes[])
  360. {
  361. unsigned stream_type = msg[4];
  362. unsigned width = msg[6];
  363. unsigned height = msg[7];
  364. unsigned dpb_size = msg[9];
  365. unsigned pitch = msg[28];
  366. unsigned level = msg[57];
  367. unsigned width_in_mb = width / 16;
  368. unsigned height_in_mb = ALIGN(height / 16, 2);
  369. unsigned fs_in_mb = width_in_mb * height_in_mb;
  370. unsigned image_size, tmp, min_dpb_size, num_dpb_buffer;
  371. unsigned min_ctx_size = ~0;
  372. image_size = width * height;
  373. image_size += image_size / 2;
  374. image_size = ALIGN(image_size, 1024);
  375. switch (stream_type) {
  376. case 0: /* H264 */
  377. switch(level) {
  378. case 30:
  379. num_dpb_buffer = 8100 / fs_in_mb;
  380. break;
  381. case 31:
  382. num_dpb_buffer = 18000 / fs_in_mb;
  383. break;
  384. case 32:
  385. num_dpb_buffer = 20480 / fs_in_mb;
  386. break;
  387. case 41:
  388. num_dpb_buffer = 32768 / fs_in_mb;
  389. break;
  390. case 42:
  391. num_dpb_buffer = 34816 / fs_in_mb;
  392. break;
  393. case 50:
  394. num_dpb_buffer = 110400 / fs_in_mb;
  395. break;
  396. case 51:
  397. num_dpb_buffer = 184320 / fs_in_mb;
  398. break;
  399. default:
  400. num_dpb_buffer = 184320 / fs_in_mb;
  401. break;
  402. }
  403. num_dpb_buffer++;
  404. if (num_dpb_buffer > 17)
  405. num_dpb_buffer = 17;
  406. /* reference picture buffer */
  407. min_dpb_size = image_size * num_dpb_buffer;
  408. /* macroblock context buffer */
  409. min_dpb_size += width_in_mb * height_in_mb * num_dpb_buffer * 192;
  410. /* IT surface buffer */
  411. min_dpb_size += width_in_mb * height_in_mb * 32;
  412. break;
  413. case 1: /* VC1 */
  414. /* reference picture buffer */
  415. min_dpb_size = image_size * 3;
  416. /* CONTEXT_BUFFER */
  417. min_dpb_size += width_in_mb * height_in_mb * 128;
  418. /* IT surface buffer */
  419. min_dpb_size += width_in_mb * 64;
  420. /* DB surface buffer */
  421. min_dpb_size += width_in_mb * 128;
  422. /* BP */
  423. tmp = max(width_in_mb, height_in_mb);
  424. min_dpb_size += ALIGN(tmp * 7 * 16, 64);
  425. break;
  426. case 3: /* MPEG2 */
  427. /* reference picture buffer */
  428. min_dpb_size = image_size * 3;
  429. break;
  430. case 4: /* MPEG4 */
  431. /* reference picture buffer */
  432. min_dpb_size = image_size * 3;
  433. /* CM */
  434. min_dpb_size += width_in_mb * height_in_mb * 64;
  435. /* IT surface buffer */
  436. min_dpb_size += ALIGN(width_in_mb * height_in_mb * 32, 64);
  437. break;
  438. case 7: /* H264 Perf */
  439. switch(level) {
  440. case 30:
  441. num_dpb_buffer = 8100 / fs_in_mb;
  442. break;
  443. case 31:
  444. num_dpb_buffer = 18000 / fs_in_mb;
  445. break;
  446. case 32:
  447. num_dpb_buffer = 20480 / fs_in_mb;
  448. break;
  449. case 41:
  450. num_dpb_buffer = 32768 / fs_in_mb;
  451. break;
  452. case 42:
  453. num_dpb_buffer = 34816 / fs_in_mb;
  454. break;
  455. case 50:
  456. num_dpb_buffer = 110400 / fs_in_mb;
  457. break;
  458. case 51:
  459. num_dpb_buffer = 184320 / fs_in_mb;
  460. break;
  461. default:
  462. num_dpb_buffer = 184320 / fs_in_mb;
  463. break;
  464. }
  465. num_dpb_buffer++;
  466. if (num_dpb_buffer > 17)
  467. num_dpb_buffer = 17;
  468. /* reference picture buffer */
  469. min_dpb_size = image_size * num_dpb_buffer;
  470. if (!adev->uvd.use_ctx_buf){
  471. /* macroblock context buffer */
  472. min_dpb_size +=
  473. width_in_mb * height_in_mb * num_dpb_buffer * 192;
  474. /* IT surface buffer */
  475. min_dpb_size += width_in_mb * height_in_mb * 32;
  476. } else {
  477. /* macroblock context buffer */
  478. min_ctx_size =
  479. width_in_mb * height_in_mb * num_dpb_buffer * 192;
  480. }
  481. break;
  482. case 16: /* H265 */
  483. image_size = (ALIGN(width, 16) * ALIGN(height, 16) * 3) / 2;
  484. image_size = ALIGN(image_size, 256);
  485. num_dpb_buffer = (le32_to_cpu(msg[59]) & 0xff) + 2;
  486. min_dpb_size = image_size * num_dpb_buffer;
  487. min_ctx_size = ((width + 255) / 16) * ((height + 255) / 16)
  488. * 16 * num_dpb_buffer + 52 * 1024;
  489. break;
  490. default:
  491. DRM_ERROR("UVD codec not handled %d!\n", stream_type);
  492. return -EINVAL;
  493. }
  494. if (width > pitch) {
  495. DRM_ERROR("Invalid UVD decoding target pitch!\n");
  496. return -EINVAL;
  497. }
  498. if (dpb_size < min_dpb_size) {
  499. DRM_ERROR("Invalid dpb_size in UVD message (%d / %d)!\n",
  500. dpb_size, min_dpb_size);
  501. return -EINVAL;
  502. }
  503. buf_sizes[0x1] = dpb_size;
  504. buf_sizes[0x2] = image_size;
  505. buf_sizes[0x4] = min_ctx_size;
  506. return 0;
  507. }
  508. /**
  509. * amdgpu_uvd_cs_msg - handle UVD message
  510. *
  511. * @ctx: UVD parser context
  512. * @bo: buffer object containing the message
  513. * @offset: offset into the buffer object
  514. *
  515. * Peek into the UVD message and extract the session id.
  516. * Make sure that we don't open up to many sessions.
  517. */
  518. static int amdgpu_uvd_cs_msg(struct amdgpu_uvd_cs_ctx *ctx,
  519. struct amdgpu_bo *bo, unsigned offset)
  520. {
  521. struct amdgpu_device *adev = ctx->parser->adev;
  522. int32_t *msg, msg_type, handle;
  523. void *ptr;
  524. long r;
  525. int i;
  526. if (offset & 0x3F) {
  527. DRM_ERROR("UVD messages must be 64 byte aligned!\n");
  528. return -EINVAL;
  529. }
  530. r = amdgpu_bo_kmap(bo, &ptr);
  531. if (r) {
  532. DRM_ERROR("Failed mapping the UVD message (%ld)!\n", r);
  533. return r;
  534. }
  535. msg = ptr + offset;
  536. msg_type = msg[1];
  537. handle = msg[2];
  538. if (handle == 0) {
  539. DRM_ERROR("Invalid UVD handle!\n");
  540. return -EINVAL;
  541. }
  542. switch (msg_type) {
  543. case 0:
  544. /* it's a create msg, calc image size (width * height) */
  545. amdgpu_bo_kunmap(bo);
  546. /* try to alloc a new handle */
  547. for (i = 0; i < adev->uvd.max_handles; ++i) {
  548. if (atomic_read(&adev->uvd.handles[i]) == handle) {
  549. DRM_ERROR("Handle 0x%x already in use!\n", handle);
  550. return -EINVAL;
  551. }
  552. if (!atomic_cmpxchg(&adev->uvd.handles[i], 0, handle)) {
  553. adev->uvd.filp[i] = ctx->parser->filp;
  554. return 0;
  555. }
  556. }
  557. DRM_ERROR("No more free UVD handles!\n");
  558. return -ENOSPC;
  559. case 1:
  560. /* it's a decode msg, calc buffer sizes */
  561. r = amdgpu_uvd_cs_msg_decode(adev, msg, ctx->buf_sizes);
  562. amdgpu_bo_kunmap(bo);
  563. if (r)
  564. return r;
  565. /* validate the handle */
  566. for (i = 0; i < adev->uvd.max_handles; ++i) {
  567. if (atomic_read(&adev->uvd.handles[i]) == handle) {
  568. if (adev->uvd.filp[i] != ctx->parser->filp) {
  569. DRM_ERROR("UVD handle collision detected!\n");
  570. return -EINVAL;
  571. }
  572. return 0;
  573. }
  574. }
  575. DRM_ERROR("Invalid UVD handle 0x%x!\n", handle);
  576. return -ENOENT;
  577. case 2:
  578. /* it's a destroy msg, free the handle */
  579. for (i = 0; i < adev->uvd.max_handles; ++i)
  580. atomic_cmpxchg(&adev->uvd.handles[i], handle, 0);
  581. amdgpu_bo_kunmap(bo);
  582. return 0;
  583. default:
  584. DRM_ERROR("Illegal UVD message type (%d)!\n", msg_type);
  585. return -EINVAL;
  586. }
  587. BUG();
  588. return -EINVAL;
  589. }
  590. /**
  591. * amdgpu_uvd_cs_pass2 - second parsing round
  592. *
  593. * @ctx: UVD parser context
  594. *
  595. * Patch buffer addresses, make sure buffer sizes are correct.
  596. */
  597. static int amdgpu_uvd_cs_pass2(struct amdgpu_uvd_cs_ctx *ctx)
  598. {
  599. struct amdgpu_bo_va_mapping *mapping;
  600. struct amdgpu_bo *bo;
  601. uint32_t cmd;
  602. uint64_t start, end;
  603. uint64_t addr = amdgpu_uvd_get_addr_from_ctx(ctx);
  604. int r;
  605. mapping = amdgpu_cs_find_mapping(ctx->parser, addr, &bo);
  606. if (mapping == NULL) {
  607. DRM_ERROR("Can't find BO for addr 0x%08Lx\n", addr);
  608. return -EINVAL;
  609. }
  610. start = amdgpu_bo_gpu_offset(bo);
  611. end = (mapping->it.last + 1 - mapping->it.start);
  612. end = end * AMDGPU_GPU_PAGE_SIZE + start;
  613. addr -= ((uint64_t)mapping->it.start) * AMDGPU_GPU_PAGE_SIZE;
  614. start += addr;
  615. amdgpu_set_ib_value(ctx->parser, ctx->ib_idx, ctx->data0,
  616. lower_32_bits(start));
  617. amdgpu_set_ib_value(ctx->parser, ctx->ib_idx, ctx->data1,
  618. upper_32_bits(start));
  619. cmd = amdgpu_get_ib_value(ctx->parser, ctx->ib_idx, ctx->idx) >> 1;
  620. if (cmd < 0x4) {
  621. if ((end - start) < ctx->buf_sizes[cmd]) {
  622. DRM_ERROR("buffer (%d) to small (%d / %d)!\n", cmd,
  623. (unsigned)(end - start),
  624. ctx->buf_sizes[cmd]);
  625. return -EINVAL;
  626. }
  627. } else if (cmd == 0x206) {
  628. if ((end - start) < ctx->buf_sizes[4]) {
  629. DRM_ERROR("buffer (%d) to small (%d / %d)!\n", cmd,
  630. (unsigned)(end - start),
  631. ctx->buf_sizes[4]);
  632. return -EINVAL;
  633. }
  634. } else if ((cmd != 0x100) && (cmd != 0x204)) {
  635. DRM_ERROR("invalid UVD command %X!\n", cmd);
  636. return -EINVAL;
  637. }
  638. if (!ctx->parser->adev->uvd.address_64_bit) {
  639. if ((start >> 28) != ((end - 1) >> 28)) {
  640. DRM_ERROR("reloc %LX-%LX crossing 256MB boundary!\n",
  641. start, end);
  642. return -EINVAL;
  643. }
  644. if ((cmd == 0 || cmd == 0x3) &&
  645. (start >> 28) != (ctx->parser->adev->uvd.gpu_addr >> 28)) {
  646. DRM_ERROR("msg/fb buffer %LX-%LX out of 256MB segment!\n",
  647. start, end);
  648. return -EINVAL;
  649. }
  650. }
  651. if (cmd == 0) {
  652. ctx->has_msg_cmd = true;
  653. r = amdgpu_uvd_cs_msg(ctx, bo, addr);
  654. if (r)
  655. return r;
  656. } else if (!ctx->has_msg_cmd) {
  657. DRM_ERROR("Message needed before other commands are send!\n");
  658. return -EINVAL;
  659. }
  660. return 0;
  661. }
  662. /**
  663. * amdgpu_uvd_cs_reg - parse register writes
  664. *
  665. * @ctx: UVD parser context
  666. * @cb: callback function
  667. *
  668. * Parse the register writes, call cb on each complete command.
  669. */
  670. static int amdgpu_uvd_cs_reg(struct amdgpu_uvd_cs_ctx *ctx,
  671. int (*cb)(struct amdgpu_uvd_cs_ctx *ctx))
  672. {
  673. struct amdgpu_ib *ib = &ctx->parser->job->ibs[ctx->ib_idx];
  674. int i, r;
  675. ctx->idx++;
  676. for (i = 0; i <= ctx->count; ++i) {
  677. unsigned reg = ctx->reg + i;
  678. if (ctx->idx >= ib->length_dw) {
  679. DRM_ERROR("Register command after end of CS!\n");
  680. return -EINVAL;
  681. }
  682. switch (reg) {
  683. case mmUVD_GPCOM_VCPU_DATA0:
  684. ctx->data0 = ctx->idx;
  685. break;
  686. case mmUVD_GPCOM_VCPU_DATA1:
  687. ctx->data1 = ctx->idx;
  688. break;
  689. case mmUVD_GPCOM_VCPU_CMD:
  690. r = cb(ctx);
  691. if (r)
  692. return r;
  693. break;
  694. case mmUVD_ENGINE_CNTL:
  695. case mmUVD_NO_OP:
  696. break;
  697. default:
  698. DRM_ERROR("Invalid reg 0x%X!\n", reg);
  699. return -EINVAL;
  700. }
  701. ctx->idx++;
  702. }
  703. return 0;
  704. }
  705. /**
  706. * amdgpu_uvd_cs_packets - parse UVD packets
  707. *
  708. * @ctx: UVD parser context
  709. * @cb: callback function
  710. *
  711. * Parse the command stream packets.
  712. */
  713. static int amdgpu_uvd_cs_packets(struct amdgpu_uvd_cs_ctx *ctx,
  714. int (*cb)(struct amdgpu_uvd_cs_ctx *ctx))
  715. {
  716. struct amdgpu_ib *ib = &ctx->parser->job->ibs[ctx->ib_idx];
  717. int r;
  718. for (ctx->idx = 0 ; ctx->idx < ib->length_dw; ) {
  719. uint32_t cmd = amdgpu_get_ib_value(ctx->parser, ctx->ib_idx, ctx->idx);
  720. unsigned type = CP_PACKET_GET_TYPE(cmd);
  721. switch (type) {
  722. case PACKET_TYPE0:
  723. ctx->reg = CP_PACKET0_GET_REG(cmd);
  724. ctx->count = CP_PACKET_GET_COUNT(cmd);
  725. r = amdgpu_uvd_cs_reg(ctx, cb);
  726. if (r)
  727. return r;
  728. break;
  729. case PACKET_TYPE2:
  730. ++ctx->idx;
  731. break;
  732. default:
  733. DRM_ERROR("Unknown packet type %d !\n", type);
  734. return -EINVAL;
  735. }
  736. }
  737. return 0;
  738. }
  739. /**
  740. * amdgpu_uvd_ring_parse_cs - UVD command submission parser
  741. *
  742. * @parser: Command submission parser context
  743. *
  744. * Parse the command stream, patch in addresses as necessary.
  745. */
  746. int amdgpu_uvd_ring_parse_cs(struct amdgpu_cs_parser *parser, uint32_t ib_idx)
  747. {
  748. struct amdgpu_uvd_cs_ctx ctx = {};
  749. unsigned buf_sizes[] = {
  750. [0x00000000] = 2048,
  751. [0x00000001] = 0xFFFFFFFF,
  752. [0x00000002] = 0xFFFFFFFF,
  753. [0x00000003] = 2048,
  754. [0x00000004] = 0xFFFFFFFF,
  755. };
  756. struct amdgpu_ib *ib = &parser->job->ibs[ib_idx];
  757. int r;
  758. parser->job->vm = NULL;
  759. ib->gpu_addr = amdgpu_sa_bo_gpu_addr(ib->sa_bo);
  760. if (ib->length_dw % 16) {
  761. DRM_ERROR("UVD IB length (%d) not 16 dwords aligned!\n",
  762. ib->length_dw);
  763. return -EINVAL;
  764. }
  765. r = amdgpu_cs_sysvm_access_required(parser);
  766. if (r)
  767. return r;
  768. ctx.parser = parser;
  769. ctx.buf_sizes = buf_sizes;
  770. ctx.ib_idx = ib_idx;
  771. /* first round only required on chips without UVD 64 bit address support */
  772. if (!parser->adev->uvd.address_64_bit) {
  773. /* first round, make sure the buffers are actually in the UVD segment */
  774. r = amdgpu_uvd_cs_packets(&ctx, amdgpu_uvd_cs_pass1);
  775. if (r)
  776. return r;
  777. }
  778. /* second round, patch buffer addresses into the command stream */
  779. r = amdgpu_uvd_cs_packets(&ctx, amdgpu_uvd_cs_pass2);
  780. if (r)
  781. return r;
  782. if (!ctx.has_msg_cmd) {
  783. DRM_ERROR("UVD-IBs need a msg command!\n");
  784. return -EINVAL;
  785. }
  786. return 0;
  787. }
  788. static int amdgpu_uvd_send_msg(struct amdgpu_ring *ring, struct amdgpu_bo *bo,
  789. bool direct, struct dma_fence **fence)
  790. {
  791. struct ttm_validate_buffer tv;
  792. struct ww_acquire_ctx ticket;
  793. struct list_head head;
  794. struct amdgpu_job *job;
  795. struct amdgpu_ib *ib;
  796. struct dma_fence *f = NULL;
  797. struct amdgpu_device *adev = ring->adev;
  798. uint64_t addr;
  799. int i, r;
  800. memset(&tv, 0, sizeof(tv));
  801. tv.bo = &bo->tbo;
  802. INIT_LIST_HEAD(&head);
  803. list_add(&tv.head, &head);
  804. r = ttm_eu_reserve_buffers(&ticket, &head, true, NULL);
  805. if (r)
  806. return r;
  807. if (!ring->adev->uvd.address_64_bit) {
  808. amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_VRAM);
  809. amdgpu_uvd_force_into_uvd_segment(bo);
  810. }
  811. r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
  812. if (r)
  813. goto err;
  814. r = amdgpu_job_alloc_with_ib(adev, 64, &job);
  815. if (r)
  816. goto err;
  817. ib = &job->ibs[0];
  818. addr = amdgpu_bo_gpu_offset(bo);
  819. ib->ptr[0] = PACKET0(mmUVD_GPCOM_VCPU_DATA0, 0);
  820. ib->ptr[1] = addr;
  821. ib->ptr[2] = PACKET0(mmUVD_GPCOM_VCPU_DATA1, 0);
  822. ib->ptr[3] = addr >> 32;
  823. ib->ptr[4] = PACKET0(mmUVD_GPCOM_VCPU_CMD, 0);
  824. ib->ptr[5] = 0;
  825. for (i = 6; i < 16; i += 2) {
  826. ib->ptr[i] = PACKET0(mmUVD_NO_OP, 0);
  827. ib->ptr[i+1] = 0;
  828. }
  829. ib->length_dw = 16;
  830. if (direct) {
  831. r = amdgpu_ib_schedule(ring, 1, ib, NULL, NULL, &f);
  832. job->fence = dma_fence_get(f);
  833. if (r)
  834. goto err_free;
  835. amdgpu_job_free(job);
  836. } else {
  837. r = amdgpu_job_submit(job, ring, &adev->uvd.entity,
  838. AMDGPU_FENCE_OWNER_UNDEFINED, &f);
  839. if (r)
  840. goto err_free;
  841. }
  842. ttm_eu_fence_buffer_objects(&ticket, &head, f);
  843. if (fence)
  844. *fence = dma_fence_get(f);
  845. amdgpu_bo_unref(&bo);
  846. dma_fence_put(f);
  847. return 0;
  848. err_free:
  849. amdgpu_job_free(job);
  850. err:
  851. ttm_eu_backoff_reservation(&ticket, &head);
  852. return r;
  853. }
  854. /* multiple fence commands without any stream commands in between can
  855. crash the vcpu so just try to emmit a dummy create/destroy msg to
  856. avoid this */
  857. int amdgpu_uvd_get_create_msg(struct amdgpu_ring *ring, uint32_t handle,
  858. struct dma_fence **fence)
  859. {
  860. struct amdgpu_device *adev = ring->adev;
  861. struct amdgpu_bo *bo;
  862. uint32_t *msg;
  863. int r, i;
  864. r = amdgpu_bo_create(adev, 1024, PAGE_SIZE, true,
  865. AMDGPU_GEM_DOMAIN_VRAM,
  866. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
  867. AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
  868. NULL, NULL, &bo);
  869. if (r)
  870. return r;
  871. r = amdgpu_bo_reserve(bo, false);
  872. if (r) {
  873. amdgpu_bo_unref(&bo);
  874. return r;
  875. }
  876. r = amdgpu_bo_kmap(bo, (void **)&msg);
  877. if (r) {
  878. amdgpu_bo_unreserve(bo);
  879. amdgpu_bo_unref(&bo);
  880. return r;
  881. }
  882. /* stitch together an UVD create msg */
  883. msg[0] = cpu_to_le32(0x00000de4);
  884. msg[1] = cpu_to_le32(0x00000000);
  885. msg[2] = cpu_to_le32(handle);
  886. msg[3] = cpu_to_le32(0x00000000);
  887. msg[4] = cpu_to_le32(0x00000000);
  888. msg[5] = cpu_to_le32(0x00000000);
  889. msg[6] = cpu_to_le32(0x00000000);
  890. msg[7] = cpu_to_le32(0x00000780);
  891. msg[8] = cpu_to_le32(0x00000440);
  892. msg[9] = cpu_to_le32(0x00000000);
  893. msg[10] = cpu_to_le32(0x01b37000);
  894. for (i = 11; i < 1024; ++i)
  895. msg[i] = cpu_to_le32(0x0);
  896. amdgpu_bo_kunmap(bo);
  897. amdgpu_bo_unreserve(bo);
  898. return amdgpu_uvd_send_msg(ring, bo, true, fence);
  899. }
  900. int amdgpu_uvd_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
  901. bool direct, struct dma_fence **fence)
  902. {
  903. struct amdgpu_device *adev = ring->adev;
  904. struct amdgpu_bo *bo;
  905. uint32_t *msg;
  906. int r, i;
  907. r = amdgpu_bo_create(adev, 1024, PAGE_SIZE, true,
  908. AMDGPU_GEM_DOMAIN_VRAM,
  909. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
  910. AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
  911. NULL, NULL, &bo);
  912. if (r)
  913. return r;
  914. r = amdgpu_bo_reserve(bo, false);
  915. if (r) {
  916. amdgpu_bo_unref(&bo);
  917. return r;
  918. }
  919. r = amdgpu_bo_kmap(bo, (void **)&msg);
  920. if (r) {
  921. amdgpu_bo_unreserve(bo);
  922. amdgpu_bo_unref(&bo);
  923. return r;
  924. }
  925. /* stitch together an UVD destroy msg */
  926. msg[0] = cpu_to_le32(0x00000de4);
  927. msg[1] = cpu_to_le32(0x00000002);
  928. msg[2] = cpu_to_le32(handle);
  929. msg[3] = cpu_to_le32(0x00000000);
  930. for (i = 4; i < 1024; ++i)
  931. msg[i] = cpu_to_le32(0x0);
  932. amdgpu_bo_kunmap(bo);
  933. amdgpu_bo_unreserve(bo);
  934. return amdgpu_uvd_send_msg(ring, bo, direct, fence);
  935. }
  936. static void amdgpu_uvd_idle_work_handler(struct work_struct *work)
  937. {
  938. struct amdgpu_device *adev =
  939. container_of(work, struct amdgpu_device, uvd.idle_work.work);
  940. unsigned fences = amdgpu_fence_count_emitted(&adev->uvd.ring);
  941. if (fences == 0) {
  942. if (adev->pm.dpm_enabled) {
  943. amdgpu_dpm_enable_uvd(adev, false);
  944. } else {
  945. amdgpu_asic_set_uvd_clocks(adev, 0, 0);
  946. }
  947. } else {
  948. schedule_delayed_work(&adev->uvd.idle_work, UVD_IDLE_TIMEOUT);
  949. }
  950. }
  951. void amdgpu_uvd_ring_begin_use(struct amdgpu_ring *ring)
  952. {
  953. struct amdgpu_device *adev = ring->adev;
  954. bool set_clocks = !cancel_delayed_work_sync(&adev->uvd.idle_work);
  955. if (set_clocks) {
  956. if (adev->pm.dpm_enabled) {
  957. amdgpu_dpm_enable_uvd(adev, true);
  958. } else {
  959. amdgpu_asic_set_uvd_clocks(adev, 53300, 40000);
  960. }
  961. }
  962. }
  963. void amdgpu_uvd_ring_end_use(struct amdgpu_ring *ring)
  964. {
  965. schedule_delayed_work(&ring->adev->uvd.idle_work, UVD_IDLE_TIMEOUT);
  966. }
  967. /**
  968. * amdgpu_uvd_ring_test_ib - test ib execution
  969. *
  970. * @ring: amdgpu_ring pointer
  971. *
  972. * Test if we can successfully execute an IB
  973. */
  974. int amdgpu_uvd_ring_test_ib(struct amdgpu_ring *ring, long timeout)
  975. {
  976. struct dma_fence *fence;
  977. long r;
  978. r = amdgpu_uvd_get_create_msg(ring, 1, NULL);
  979. if (r) {
  980. DRM_ERROR("amdgpu: failed to get create msg (%ld).\n", r);
  981. goto error;
  982. }
  983. r = amdgpu_uvd_get_destroy_msg(ring, 1, true, &fence);
  984. if (r) {
  985. DRM_ERROR("amdgpu: failed to get destroy ib (%ld).\n", r);
  986. goto error;
  987. }
  988. r = dma_fence_wait_timeout(fence, false, timeout);
  989. if (r == 0) {
  990. DRM_ERROR("amdgpu: IB test timed out.\n");
  991. r = -ETIMEDOUT;
  992. } else if (r < 0) {
  993. DRM_ERROR("amdgpu: fence wait failed (%ld).\n", r);
  994. } else {
  995. DRM_INFO("ib test on ring %d succeeded\n", ring->idx);
  996. r = 0;
  997. }
  998. dma_fence_put(fence);
  999. error:
  1000. return r;
  1001. }