amdgpu_uvd.c 25 KB

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