amdgpu_uvd.c 27 KB

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