amdgpu_uvd.c 24 KB

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