blob: bfb3c274d7f06f699930f43c726f894406843ab9 [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
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
31#include <linux/firmware.h>
32#include <linux/module.h>
33#include <drm/drmP.h>
34#include <drm/drm.h>
35
36#include "amdgpu.h"
37#include "amdgpu_pm.h"
38#include "amdgpu_uvd.h"
39#include "cikd.h"
40#include "uvd/uvd_4_2_d.h"
41
42/* 1 second timeout */
Christian König08086632016-07-01 17:45:49 +020043#define UVD_IDLE_TIMEOUT msecs_to_jiffies(1000)
Sonny Jiang8e008dd2016-05-11 13:29:48 -040044/* Polaris10/11 firmware version */
45#define FW_1_66_16 ((1 << 24) | (66 << 16) | (16 << 8))
Alex Deucherd38ceaf2015-04-20 16:55:21 -040046
47/* Firmware Names */
48#ifdef CONFIG_DRM_AMDGPU_CIK
49#define FIRMWARE_BONAIRE "radeon/bonaire_uvd.bin"
Christian Königedf600d2016-05-03 15:54:54 +020050#define FIRMWARE_KABINI "radeon/kabini_uvd.bin"
51#define FIRMWARE_KAVERI "radeon/kaveri_uvd.bin"
52#define FIRMWARE_HAWAII "radeon/hawaii_uvd.bin"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040053#define FIRMWARE_MULLINS "radeon/mullins_uvd.bin"
54#endif
Jammy Zhouc65444f2015-05-13 22:49:04 +080055#define FIRMWARE_TONGA "amdgpu/tonga_uvd.bin"
56#define FIRMWARE_CARRIZO "amdgpu/carrizo_uvd.bin"
David Zhang974ee3d2015-07-08 17:32:15 +080057#define FIRMWARE_FIJI "amdgpu/fiji_uvd.bin"
Samuel Lia39c8ce2015-10-08 16:27:21 -040058#define FIRMWARE_STONEY "amdgpu/stoney_uvd.bin"
Flora Cui2cc0c0b2016-03-14 18:33:29 -040059#define FIRMWARE_POLARIS10 "amdgpu/polaris10_uvd.bin"
Rex Zhu925a51c2016-03-23 14:48:03 +080060#define FIRMWARE_POLARIS11 "amdgpu/polaris11_uvd.bin"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040061
62/**
63 * amdgpu_uvd_cs_ctx - Command submission parser context
64 *
65 * Used for emulating virtual memory support on UVD 4.2.
66 */
67struct amdgpu_uvd_cs_ctx {
68 struct amdgpu_cs_parser *parser;
69 unsigned reg, count;
70 unsigned data0, data1;
71 unsigned idx;
72 unsigned ib_idx;
73
74 /* does the IB has a msg command */
75 bool has_msg_cmd;
76
77 /* minimum buffer sizes */
78 unsigned *buf_sizes;
79};
80
81#ifdef CONFIG_DRM_AMDGPU_CIK
82MODULE_FIRMWARE(FIRMWARE_BONAIRE);
83MODULE_FIRMWARE(FIRMWARE_KABINI);
84MODULE_FIRMWARE(FIRMWARE_KAVERI);
85MODULE_FIRMWARE(FIRMWARE_HAWAII);
86MODULE_FIRMWARE(FIRMWARE_MULLINS);
87#endif
88MODULE_FIRMWARE(FIRMWARE_TONGA);
89MODULE_FIRMWARE(FIRMWARE_CARRIZO);
David Zhang974ee3d2015-07-08 17:32:15 +080090MODULE_FIRMWARE(FIRMWARE_FIJI);
Samuel Lia39c8ce2015-10-08 16:27:21 -040091MODULE_FIRMWARE(FIRMWARE_STONEY);
Flora Cui2cc0c0b2016-03-14 18:33:29 -040092MODULE_FIRMWARE(FIRMWARE_POLARIS10);
93MODULE_FIRMWARE(FIRMWARE_POLARIS11);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040094
Alex Deucherd38ceaf2015-04-20 16:55:21 -040095static void amdgpu_uvd_idle_work_handler(struct work_struct *work);
96
97int amdgpu_uvd_sw_init(struct amdgpu_device *adev)
98{
Christian Königead833e2016-02-10 14:35:19 +010099 struct amdgpu_ring *ring;
100 struct amd_sched_rq *rq;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400101 unsigned long bo_size;
102 const char *fw_name;
103 const struct common_firmware_header *hdr;
104 unsigned version_major, version_minor, family_id;
105 int i, r;
106
107 INIT_DELAYED_WORK(&adev->uvd.idle_work, amdgpu_uvd_idle_work_handler);
108
109 switch (adev->asic_type) {
110#ifdef CONFIG_DRM_AMDGPU_CIK
111 case CHIP_BONAIRE:
112 fw_name = FIRMWARE_BONAIRE;
113 break;
114 case CHIP_KABINI:
115 fw_name = FIRMWARE_KABINI;
116 break;
117 case CHIP_KAVERI:
118 fw_name = FIRMWARE_KAVERI;
119 break;
120 case CHIP_HAWAII:
121 fw_name = FIRMWARE_HAWAII;
122 break;
123 case CHIP_MULLINS:
124 fw_name = FIRMWARE_MULLINS;
125 break;
126#endif
127 case CHIP_TONGA:
128 fw_name = FIRMWARE_TONGA;
129 break;
David Zhang974ee3d2015-07-08 17:32:15 +0800130 case CHIP_FIJI:
131 fw_name = FIRMWARE_FIJI;
132 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400133 case CHIP_CARRIZO:
134 fw_name = FIRMWARE_CARRIZO;
135 break;
Samuel Lia39c8ce2015-10-08 16:27:21 -0400136 case CHIP_STONEY:
137 fw_name = FIRMWARE_STONEY;
138 break;
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400139 case CHIP_POLARIS10:
140 fw_name = FIRMWARE_POLARIS10;
Sonny Jiang38d75812015-11-05 15:17:18 -0500141 break;
Flora Cui2cc0c0b2016-03-14 18:33:29 -0400142 case CHIP_POLARIS11:
143 fw_name = FIRMWARE_POLARIS11;
Sonny Jiang38d75812015-11-05 15:17:18 -0500144 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400145 default:
146 return -EINVAL;
147 }
148
149 r = request_firmware(&adev->uvd.fw, fw_name, adev->dev);
150 if (r) {
151 dev_err(adev->dev, "amdgpu_uvd: Can't load firmware \"%s\"\n",
152 fw_name);
153 return r;
154 }
155
156 r = amdgpu_ucode_validate(adev->uvd.fw);
157 if (r) {
158 dev_err(adev->dev, "amdgpu_uvd: Can't validate firmware \"%s\"\n",
159 fw_name);
160 release_firmware(adev->uvd.fw);
161 adev->uvd.fw = NULL;
162 return r;
163 }
164
Arindam Nathc0365542016-04-12 13:46:15 +0200165 /* Set the default UVD handles that the firmware can handle */
166 adev->uvd.max_handles = AMDGPU_DEFAULT_UVD_HANDLES;
167
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400168 hdr = (const struct common_firmware_header *)adev->uvd.fw->data;
169 family_id = le32_to_cpu(hdr->ucode_version) & 0xff;
170 version_major = (le32_to_cpu(hdr->ucode_version) >> 24) & 0xff;
171 version_minor = (le32_to_cpu(hdr->ucode_version) >> 8) & 0xff;
172 DRM_INFO("Found UVD firmware Version: %hu.%hu Family ID: %hu\n",
173 version_major, version_minor, family_id);
174
Arindam Nathc0365542016-04-12 13:46:15 +0200175 /*
176 * Limit the number of UVD handles depending on microcode major
177 * and minor versions. The firmware version which has 40 UVD
178 * instances support is 1.80. So all subsequent versions should
179 * also have the same support.
180 */
181 if ((version_major > 0x01) ||
182 ((version_major == 0x01) && (version_minor >= 0x50)))
183 adev->uvd.max_handles = AMDGPU_MAX_UVD_HANDLES;
184
Sonny Jiang562e2682016-04-18 16:05:04 -0400185 adev->uvd.fw_version = ((version_major << 24) | (version_minor << 16) |
186 (family_id << 8));
187
Sonny Jiang8e008dd2016-05-11 13:29:48 -0400188 if ((adev->asic_type == CHIP_POLARIS10 ||
189 adev->asic_type == CHIP_POLARIS11) &&
190 (adev->uvd.fw_version < FW_1_66_16))
191 DRM_ERROR("POLARIS10/11 UVD firmware version %hu.%hu is too old.\n",
192 version_major, version_minor);
193
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400194 bo_size = AMDGPU_GPU_PAGE_ALIGN(le32_to_cpu(hdr->ucode_size_bytes) + 8)
Arindam Nathc0365542016-04-12 13:46:15 +0200195 + AMDGPU_UVD_STACK_SIZE + AMDGPU_UVD_HEAP_SIZE
196 + AMDGPU_UVD_SESSION_SIZE * adev->uvd.max_handles;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400197 r = amdgpu_bo_create(adev, bo_size, PAGE_SIZE, true,
Alex Deucher857d9132015-08-27 00:14:16 -0400198 AMDGPU_GEM_DOMAIN_VRAM,
199 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
Christian König72d76682015-09-03 17:34:59 +0200200 NULL, NULL, &adev->uvd.vcpu_bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400201 if (r) {
202 dev_err(adev->dev, "(%d) failed to allocate UVD bo\n", r);
203 return r;
204 }
205
206 r = amdgpu_bo_reserve(adev->uvd.vcpu_bo, false);
207 if (r) {
208 amdgpu_bo_unref(&adev->uvd.vcpu_bo);
209 dev_err(adev->dev, "(%d) failed to reserve UVD bo\n", r);
210 return r;
211 }
212
213 r = amdgpu_bo_pin(adev->uvd.vcpu_bo, AMDGPU_GEM_DOMAIN_VRAM,
214 &adev->uvd.gpu_addr);
215 if (r) {
216 amdgpu_bo_unreserve(adev->uvd.vcpu_bo);
217 amdgpu_bo_unref(&adev->uvd.vcpu_bo);
218 dev_err(adev->dev, "(%d) UVD bo pin failed\n", r);
219 return r;
220 }
221
222 r = amdgpu_bo_kmap(adev->uvd.vcpu_bo, &adev->uvd.cpu_addr);
223 if (r) {
224 dev_err(adev->dev, "(%d) UVD map failed\n", r);
225 return r;
226 }
227
228 amdgpu_bo_unreserve(adev->uvd.vcpu_bo);
229
Christian Königead833e2016-02-10 14:35:19 +0100230 ring = &adev->uvd.ring;
231 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_NORMAL];
232 r = amd_sched_entity_init(&ring->sched, &adev->uvd.entity,
233 rq, amdgpu_sched_jobs);
234 if (r != 0) {
235 DRM_ERROR("Failed setting up UVD run queue.\n");
236 return r;
237 }
238
Arindam Nathc0365542016-04-12 13:46:15 +0200239 for (i = 0; i < adev->uvd.max_handles; ++i) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400240 atomic_set(&adev->uvd.handles[i], 0);
241 adev->uvd.filp[i] = NULL;
242 }
243
244 /* from uvd v5.0 HW addressing capacity increased to 64 bits */
yanyang15fc3aee2015-05-22 14:39:35 -0400245 if (!amdgpu_ip_block_version_cmp(adev, AMD_IP_BLOCK_TYPE_UVD, 5, 0))
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400246 adev->uvd.address_64_bit = true;
247
248 return 0;
249}
250
251int amdgpu_uvd_sw_fini(struct amdgpu_device *adev)
252{
253 int r;
254
Monk Liu05f19eb2016-05-30 15:13:59 +0800255 kfree(adev->uvd.saved_bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400256
Christian Königead833e2016-02-10 14:35:19 +0100257 amd_sched_entity_fini(&adev->uvd.ring.sched, &adev->uvd.entity);
258
Monk Liu05f19eb2016-05-30 15:13:59 +0800259 if (adev->uvd.vcpu_bo) {
260 r = amdgpu_bo_reserve(adev->uvd.vcpu_bo, false);
261 if (!r) {
262 amdgpu_bo_kunmap(adev->uvd.vcpu_bo);
263 amdgpu_bo_unpin(adev->uvd.vcpu_bo);
264 amdgpu_bo_unreserve(adev->uvd.vcpu_bo);
265 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400266
Monk Liu05f19eb2016-05-30 15:13:59 +0800267 amdgpu_bo_unref(&adev->uvd.vcpu_bo);
268 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400269
270 amdgpu_ring_fini(&adev->uvd.ring);
271
272 release_firmware(adev->uvd.fw);
273
274 return 0;
275}
276
277int amdgpu_uvd_suspend(struct amdgpu_device *adev)
278{
Leo Liu3f99dd82016-04-01 10:36:06 -0400279 unsigned size;
280 void *ptr;
Leo Liu3f99dd82016-04-01 10:36:06 -0400281 int i;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400282
283 if (adev->uvd.vcpu_bo == NULL)
284 return 0;
285
Arindam Nathc0365542016-04-12 13:46:15 +0200286 for (i = 0; i < adev->uvd.max_handles; ++i)
Leo Liu3f99dd82016-04-01 10:36:06 -0400287 if (atomic_read(&adev->uvd.handles[i]))
288 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400289
Leo Liu3f99dd82016-04-01 10:36:06 -0400290 if (i == AMDGPU_MAX_UVD_HANDLES)
291 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400292
Rex Zhu85cc88f2016-04-12 19:25:52 +0800293 cancel_delayed_work_sync(&adev->uvd.idle_work);
294
Leo Liu3f99dd82016-04-01 10:36:06 -0400295 size = amdgpu_bo_size(adev->uvd.vcpu_bo);
Leo Liu3f99dd82016-04-01 10:36:06 -0400296 ptr = adev->uvd.cpu_addr;
Leo Liu3f99dd82016-04-01 10:36:06 -0400297
298 adev->uvd.saved_bo = kmalloc(size, GFP_KERNEL);
299 if (!adev->uvd.saved_bo)
300 return -ENOMEM;
301
302 memcpy(adev->uvd.saved_bo, ptr, size);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400303
304 return 0;
305}
306
307int amdgpu_uvd_resume(struct amdgpu_device *adev)
308{
309 unsigned size;
310 void *ptr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400311
312 if (adev->uvd.vcpu_bo == NULL)
313 return -EINVAL;
314
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400315 size = amdgpu_bo_size(adev->uvd.vcpu_bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400316 ptr = adev->uvd.cpu_addr;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400317
Leo Liu3f99dd82016-04-01 10:36:06 -0400318 if (adev->uvd.saved_bo != NULL) {
319 memcpy(ptr, adev->uvd.saved_bo, size);
320 kfree(adev->uvd.saved_bo);
321 adev->uvd.saved_bo = NULL;
Leo Liud23be4e2016-04-04 10:55:43 -0400322 } else {
323 const struct common_firmware_header *hdr;
324 unsigned offset;
325
326 hdr = (const struct common_firmware_header *)adev->uvd.fw->data;
327 offset = le32_to_cpu(hdr->ucode_array_offset_bytes);
328 memcpy(adev->uvd.cpu_addr, (adev->uvd.fw->data) + offset,
329 (adev->uvd.fw->size) - offset);
330 size -= le32_to_cpu(hdr->ucode_size_bytes);
331 ptr += le32_to_cpu(hdr->ucode_size_bytes);
Leo Liu3f99dd82016-04-01 10:36:06 -0400332 memset(ptr, 0, size);
Leo Liud23be4e2016-04-04 10:55:43 -0400333 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400334
335 return 0;
336}
337
338void amdgpu_uvd_free_handles(struct amdgpu_device *adev, struct drm_file *filp)
339{
340 struct amdgpu_ring *ring = &adev->uvd.ring;
341 int i, r;
342
Arindam Nathc0365542016-04-12 13:46:15 +0200343 for (i = 0; i < adev->uvd.max_handles; ++i) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400344 uint32_t handle = atomic_read(&adev->uvd.handles[i]);
345 if (handle != 0 && adev->uvd.filp[i] == filp) {
Chunming Zhou0e3f1542015-08-03 13:11:04 +0800346 struct fence *fence;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400347
Christian Königd7af97d2016-02-03 16:01:06 +0100348 r = amdgpu_uvd_get_destroy_msg(ring, handle,
349 false, &fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400350 if (r) {
351 DRM_ERROR("Error destroying UVD (%d)!\n", r);
352 continue;
353 }
354
Chunming Zhou0e3f1542015-08-03 13:11:04 +0800355 fence_wait(fence, false);
356 fence_put(fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400357
358 adev->uvd.filp[i] = NULL;
359 atomic_set(&adev->uvd.handles[i], 0);
360 }
361 }
362}
363
364static void amdgpu_uvd_force_into_uvd_segment(struct amdgpu_bo *rbo)
365{
366 int i;
367 for (i = 0; i < rbo->placement.num_placement; ++i) {
368 rbo->placements[i].fpfn = 0 >> PAGE_SHIFT;
369 rbo->placements[i].lpfn = (256 * 1024 * 1024) >> PAGE_SHIFT;
370 }
371}
372
373/**
374 * amdgpu_uvd_cs_pass1 - first parsing round
375 *
376 * @ctx: UVD parser context
377 *
378 * Make sure UVD message and feedback buffers are in VRAM and
379 * nobody is violating an 256MB boundary.
380 */
381static int amdgpu_uvd_cs_pass1(struct amdgpu_uvd_cs_ctx *ctx)
382{
383 struct amdgpu_bo_va_mapping *mapping;
384 struct amdgpu_bo *bo;
385 uint32_t cmd, lo, hi;
386 uint64_t addr;
387 int r = 0;
388
389 lo = amdgpu_get_ib_value(ctx->parser, ctx->ib_idx, ctx->data0);
390 hi = amdgpu_get_ib_value(ctx->parser, ctx->ib_idx, ctx->data1);
391 addr = ((uint64_t)lo) | (((uint64_t)hi) << 32);
392
393 mapping = amdgpu_cs_find_mapping(ctx->parser, addr, &bo);
394 if (mapping == NULL) {
395 DRM_ERROR("Can't find BO for addr 0x%08Lx\n", addr);
396 return -EINVAL;
397 }
398
399 if (!ctx->parser->adev->uvd.address_64_bit) {
400 /* check if it's a message or feedback command */
401 cmd = amdgpu_get_ib_value(ctx->parser, ctx->ib_idx, ctx->idx) >> 1;
402 if (cmd == 0x0 || cmd == 0x3) {
403 /* yes, force it into VRAM */
404 uint32_t domain = AMDGPU_GEM_DOMAIN_VRAM;
405 amdgpu_ttm_placement_from_domain(bo, domain);
406 }
407 amdgpu_uvd_force_into_uvd_segment(bo);
408
409 r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false);
410 }
411
412 return r;
413}
414
415/**
416 * amdgpu_uvd_cs_msg_decode - handle UVD decode message
417 *
418 * @msg: pointer to message structure
419 * @buf_sizes: returned buffer sizes
420 *
421 * Peek into the decode message and calculate the necessary buffer sizes.
422 */
Sonny Jiang8e008dd2016-05-11 13:29:48 -0400423static int amdgpu_uvd_cs_msg_decode(struct amdgpu_device *adev, uint32_t *msg,
424 unsigned buf_sizes[])
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400425{
426 unsigned stream_type = msg[4];
427 unsigned width = msg[6];
428 unsigned height = msg[7];
429 unsigned dpb_size = msg[9];
430 unsigned pitch = msg[28];
431 unsigned level = msg[57];
432
433 unsigned width_in_mb = width / 16;
434 unsigned height_in_mb = ALIGN(height / 16, 2);
435 unsigned fs_in_mb = width_in_mb * height_in_mb;
436
Jammy Zhou21df89a2015-08-07 15:30:44 +0800437 unsigned image_size, tmp, min_dpb_size, num_dpb_buffer;
438 unsigned min_ctx_size = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400439
440 image_size = width * height;
441 image_size += image_size / 2;
442 image_size = ALIGN(image_size, 1024);
443
444 switch (stream_type) {
445 case 0: /* H264 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400446 switch(level) {
447 case 30:
448 num_dpb_buffer = 8100 / fs_in_mb;
449 break;
450 case 31:
451 num_dpb_buffer = 18000 / fs_in_mb;
452 break;
453 case 32:
454 num_dpb_buffer = 20480 / fs_in_mb;
455 break;
456 case 41:
457 num_dpb_buffer = 32768 / fs_in_mb;
458 break;
459 case 42:
460 num_dpb_buffer = 34816 / fs_in_mb;
461 break;
462 case 50:
463 num_dpb_buffer = 110400 / fs_in_mb;
464 break;
465 case 51:
466 num_dpb_buffer = 184320 / fs_in_mb;
467 break;
468 default:
469 num_dpb_buffer = 184320 / fs_in_mb;
470 break;
471 }
472 num_dpb_buffer++;
473 if (num_dpb_buffer > 17)
474 num_dpb_buffer = 17;
475
476 /* reference picture buffer */
477 min_dpb_size = image_size * num_dpb_buffer;
478
479 /* macroblock context buffer */
480 min_dpb_size += width_in_mb * height_in_mb * num_dpb_buffer * 192;
481
482 /* IT surface buffer */
483 min_dpb_size += width_in_mb * height_in_mb * 32;
484 break;
485
486 case 1: /* VC1 */
487
488 /* reference picture buffer */
489 min_dpb_size = image_size * 3;
490
491 /* CONTEXT_BUFFER */
492 min_dpb_size += width_in_mb * height_in_mb * 128;
493
494 /* IT surface buffer */
495 min_dpb_size += width_in_mb * 64;
496
497 /* DB surface buffer */
498 min_dpb_size += width_in_mb * 128;
499
500 /* BP */
501 tmp = max(width_in_mb, height_in_mb);
502 min_dpb_size += ALIGN(tmp * 7 * 16, 64);
503 break;
504
505 case 3: /* MPEG2 */
506
507 /* reference picture buffer */
508 min_dpb_size = image_size * 3;
509 break;
510
511 case 4: /* MPEG4 */
512
513 /* reference picture buffer */
514 min_dpb_size = image_size * 3;
515
516 /* CM */
517 min_dpb_size += width_in_mb * height_in_mb * 64;
518
519 /* IT surface buffer */
520 min_dpb_size += ALIGN(width_in_mb * height_in_mb * 32, 64);
521 break;
522
Sonny Jiang8e008dd2016-05-11 13:29:48 -0400523 case 7: /* H264 Perf */
524 switch(level) {
525 case 30:
526 num_dpb_buffer = 8100 / fs_in_mb;
527 break;
528 case 31:
529 num_dpb_buffer = 18000 / fs_in_mb;
530 break;
531 case 32:
532 num_dpb_buffer = 20480 / fs_in_mb;
533 break;
534 case 41:
535 num_dpb_buffer = 32768 / fs_in_mb;
536 break;
537 case 42:
538 num_dpb_buffer = 34816 / fs_in_mb;
539 break;
540 case 50:
541 num_dpb_buffer = 110400 / fs_in_mb;
542 break;
543 case 51:
544 num_dpb_buffer = 184320 / fs_in_mb;
545 break;
546 default:
547 num_dpb_buffer = 184320 / fs_in_mb;
548 break;
549 }
550 num_dpb_buffer++;
551 if (num_dpb_buffer > 17)
552 num_dpb_buffer = 17;
553
554 /* reference picture buffer */
555 min_dpb_size = image_size * num_dpb_buffer;
556
557 if (adev->asic_type < CHIP_POLARIS10){
558 /* macroblock context buffer */
559 min_dpb_size +=
560 width_in_mb * height_in_mb * num_dpb_buffer * 192;
561
562 /* IT surface buffer */
563 min_dpb_size += width_in_mb * height_in_mb * 32;
564 } else {
565 /* macroblock context buffer */
566 min_ctx_size =
567 width_in_mb * height_in_mb * num_dpb_buffer * 192;
568 }
569 break;
570
Christian König86fa0bd2015-05-05 16:36:01 +0200571 case 16: /* H265 */
572 image_size = (ALIGN(width, 16) * ALIGN(height, 16) * 3) / 2;
573 image_size = ALIGN(image_size, 256);
574
575 num_dpb_buffer = (le32_to_cpu(msg[59]) & 0xff) + 2;
576 min_dpb_size = image_size * num_dpb_buffer;
Boyuan Zhang8c8bac52015-08-05 14:03:48 -0400577 min_ctx_size = ((width + 255) / 16) * ((height + 255) / 16)
578 * 16 * num_dpb_buffer + 52 * 1024;
Christian König86fa0bd2015-05-05 16:36:01 +0200579 break;
580
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400581 default:
582 DRM_ERROR("UVD codec not handled %d!\n", stream_type);
583 return -EINVAL;
584 }
585
586 if (width > pitch) {
587 DRM_ERROR("Invalid UVD decoding target pitch!\n");
588 return -EINVAL;
589 }
590
591 if (dpb_size < min_dpb_size) {
592 DRM_ERROR("Invalid dpb_size in UVD message (%d / %d)!\n",
593 dpb_size, min_dpb_size);
594 return -EINVAL;
595 }
596
597 buf_sizes[0x1] = dpb_size;
598 buf_sizes[0x2] = image_size;
Boyuan Zhang8c8bac52015-08-05 14:03:48 -0400599 buf_sizes[0x4] = min_ctx_size;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400600 return 0;
601}
602
603/**
604 * amdgpu_uvd_cs_msg - handle UVD message
605 *
606 * @ctx: UVD parser context
607 * @bo: buffer object containing the message
608 * @offset: offset into the buffer object
609 *
610 * Peek into the UVD message and extract the session id.
611 * Make sure that we don't open up to many sessions.
612 */
613static int amdgpu_uvd_cs_msg(struct amdgpu_uvd_cs_ctx *ctx,
614 struct amdgpu_bo *bo, unsigned offset)
615{
616 struct amdgpu_device *adev = ctx->parser->adev;
617 int32_t *msg, msg_type, handle;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400618 void *ptr;
Christian König4127a592015-08-11 16:35:54 +0200619 long r;
620 int i;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400621
622 if (offset & 0x3F) {
623 DRM_ERROR("UVD messages must be 64 byte aligned!\n");
624 return -EINVAL;
625 }
626
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400627 r = amdgpu_bo_kmap(bo, &ptr);
628 if (r) {
Christian König4127a592015-08-11 16:35:54 +0200629 DRM_ERROR("Failed mapping the UVD message (%ld)!\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400630 return r;
631 }
632
633 msg = ptr + offset;
634
635 msg_type = msg[1];
636 handle = msg[2];
637
638 if (handle == 0) {
639 DRM_ERROR("Invalid UVD handle!\n");
640 return -EINVAL;
641 }
642
Leo Liu51464192015-09-15 10:38:38 -0400643 switch (msg_type) {
644 case 0:
645 /* it's a create msg, calc image size (width * height) */
646 amdgpu_bo_kunmap(bo);
647
648 /* try to alloc a new handle */
Arindam Nathc0365542016-04-12 13:46:15 +0200649 for (i = 0; i < adev->uvd.max_handles; ++i) {
Leo Liu51464192015-09-15 10:38:38 -0400650 if (atomic_read(&adev->uvd.handles[i]) == handle) {
651 DRM_ERROR("Handle 0x%x already in use!\n", handle);
652 return -EINVAL;
653 }
654
655 if (!atomic_cmpxchg(&adev->uvd.handles[i], 0, handle)) {
656 adev->uvd.filp[i] = ctx->parser->filp;
657 return 0;
658 }
659 }
660
661 DRM_ERROR("No more free UVD handles!\n");
Christian König7129d3a2016-07-13 21:24:59 +0200662 return -ENOSPC;
Leo Liu51464192015-09-15 10:38:38 -0400663
664 case 1:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400665 /* it's a decode msg, calc buffer sizes */
Sonny Jiang8e008dd2016-05-11 13:29:48 -0400666 r = amdgpu_uvd_cs_msg_decode(adev, msg, ctx->buf_sizes);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400667 amdgpu_bo_kunmap(bo);
668 if (r)
669 return r;
670
Leo Liu51464192015-09-15 10:38:38 -0400671 /* validate the handle */
Arindam Nathc0365542016-04-12 13:46:15 +0200672 for (i = 0; i < adev->uvd.max_handles; ++i) {
Leo Liu51464192015-09-15 10:38:38 -0400673 if (atomic_read(&adev->uvd.handles[i]) == handle) {
674 if (adev->uvd.filp[i] != ctx->parser->filp) {
675 DRM_ERROR("UVD handle collision detected!\n");
676 return -EINVAL;
677 }
678 return 0;
679 }
680 }
681
682 DRM_ERROR("Invalid UVD handle 0x%x!\n", handle);
683 return -ENOENT;
684
685 case 2:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400686 /* it's a destroy msg, free the handle */
Arindam Nathc0365542016-04-12 13:46:15 +0200687 for (i = 0; i < adev->uvd.max_handles; ++i)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400688 atomic_cmpxchg(&adev->uvd.handles[i], handle, 0);
689 amdgpu_bo_kunmap(bo);
690 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400691
Leo Liu51464192015-09-15 10:38:38 -0400692 default:
693 DRM_ERROR("Illegal UVD message type (%d)!\n", msg_type);
694 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400695 }
Leo Liu51464192015-09-15 10:38:38 -0400696 BUG();
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400697 return -EINVAL;
698}
699
700/**
701 * amdgpu_uvd_cs_pass2 - second parsing round
702 *
703 * @ctx: UVD parser context
704 *
705 * Patch buffer addresses, make sure buffer sizes are correct.
706 */
707static int amdgpu_uvd_cs_pass2(struct amdgpu_uvd_cs_ctx *ctx)
708{
709 struct amdgpu_bo_va_mapping *mapping;
710 struct amdgpu_bo *bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400711 uint32_t cmd, lo, hi;
712 uint64_t start, end;
713 uint64_t addr;
714 int r;
715
716 lo = amdgpu_get_ib_value(ctx->parser, ctx->ib_idx, ctx->data0);
717 hi = amdgpu_get_ib_value(ctx->parser, ctx->ib_idx, ctx->data1);
718 addr = ((uint64_t)lo) | (((uint64_t)hi) << 32);
719
720 mapping = amdgpu_cs_find_mapping(ctx->parser, addr, &bo);
721 if (mapping == NULL)
722 return -EINVAL;
723
724 start = amdgpu_bo_gpu_offset(bo);
725
726 end = (mapping->it.last + 1 - mapping->it.start);
727 end = end * AMDGPU_GPU_PAGE_SIZE + start;
728
729 addr -= ((uint64_t)mapping->it.start) * AMDGPU_GPU_PAGE_SIZE;
730 start += addr;
731
Christian König7270f832016-01-31 11:00:41 +0100732 amdgpu_set_ib_value(ctx->parser, ctx->ib_idx, ctx->data0,
733 lower_32_bits(start));
734 amdgpu_set_ib_value(ctx->parser, ctx->ib_idx, ctx->data1,
735 upper_32_bits(start));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400736
737 cmd = amdgpu_get_ib_value(ctx->parser, ctx->ib_idx, ctx->idx) >> 1;
738 if (cmd < 0x4) {
739 if ((end - start) < ctx->buf_sizes[cmd]) {
740 DRM_ERROR("buffer (%d) to small (%d / %d)!\n", cmd,
741 (unsigned)(end - start),
742 ctx->buf_sizes[cmd]);
743 return -EINVAL;
744 }
745
Boyuan Zhang8c8bac52015-08-05 14:03:48 -0400746 } else if (cmd == 0x206) {
747 if ((end - start) < ctx->buf_sizes[4]) {
748 DRM_ERROR("buffer (%d) to small (%d / %d)!\n", cmd,
749 (unsigned)(end - start),
750 ctx->buf_sizes[4]);
751 return -EINVAL;
752 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400753 } else if ((cmd != 0x100) && (cmd != 0x204)) {
754 DRM_ERROR("invalid UVD command %X!\n", cmd);
755 return -EINVAL;
756 }
757
758 if (!ctx->parser->adev->uvd.address_64_bit) {
759 if ((start >> 28) != ((end - 1) >> 28)) {
760 DRM_ERROR("reloc %LX-%LX crossing 256MB boundary!\n",
761 start, end);
762 return -EINVAL;
763 }
764
765 if ((cmd == 0 || cmd == 0x3) &&
766 (start >> 28) != (ctx->parser->adev->uvd.gpu_addr >> 28)) {
767 DRM_ERROR("msg/fb buffer %LX-%LX out of 256MB segment!\n",
768 start, end);
769 return -EINVAL;
770 }
771 }
772
773 if (cmd == 0) {
774 ctx->has_msg_cmd = true;
775 r = amdgpu_uvd_cs_msg(ctx, bo, addr);
776 if (r)
777 return r;
778 } else if (!ctx->has_msg_cmd) {
779 DRM_ERROR("Message needed before other commands are send!\n");
780 return -EINVAL;
781 }
782
783 return 0;
784}
785
786/**
787 * amdgpu_uvd_cs_reg - parse register writes
788 *
789 * @ctx: UVD parser context
790 * @cb: callback function
791 *
792 * Parse the register writes, call cb on each complete command.
793 */
794static int amdgpu_uvd_cs_reg(struct amdgpu_uvd_cs_ctx *ctx,
795 int (*cb)(struct amdgpu_uvd_cs_ctx *ctx))
796{
Christian König50838c82016-02-03 13:44:52 +0100797 struct amdgpu_ib *ib = &ctx->parser->job->ibs[ctx->ib_idx];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400798 int i, r;
799
800 ctx->idx++;
801 for (i = 0; i <= ctx->count; ++i) {
802 unsigned reg = ctx->reg + i;
803
804 if (ctx->idx >= ib->length_dw) {
805 DRM_ERROR("Register command after end of CS!\n");
806 return -EINVAL;
807 }
808
809 switch (reg) {
810 case mmUVD_GPCOM_VCPU_DATA0:
811 ctx->data0 = ctx->idx;
812 break;
813 case mmUVD_GPCOM_VCPU_DATA1:
814 ctx->data1 = ctx->idx;
815 break;
816 case mmUVD_GPCOM_VCPU_CMD:
817 r = cb(ctx);
818 if (r)
819 return r;
820 break;
821 case mmUVD_ENGINE_CNTL:
822 break;
823 default:
824 DRM_ERROR("Invalid reg 0x%X!\n", reg);
825 return -EINVAL;
826 }
827 ctx->idx++;
828 }
829 return 0;
830}
831
832/**
833 * amdgpu_uvd_cs_packets - parse UVD packets
834 *
835 * @ctx: UVD parser context
836 * @cb: callback function
837 *
838 * Parse the command stream packets.
839 */
840static int amdgpu_uvd_cs_packets(struct amdgpu_uvd_cs_ctx *ctx,
841 int (*cb)(struct amdgpu_uvd_cs_ctx *ctx))
842{
Christian König50838c82016-02-03 13:44:52 +0100843 struct amdgpu_ib *ib = &ctx->parser->job->ibs[ctx->ib_idx];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400844 int r;
845
846 for (ctx->idx = 0 ; ctx->idx < ib->length_dw; ) {
847 uint32_t cmd = amdgpu_get_ib_value(ctx->parser, ctx->ib_idx, ctx->idx);
848 unsigned type = CP_PACKET_GET_TYPE(cmd);
849 switch (type) {
850 case PACKET_TYPE0:
851 ctx->reg = CP_PACKET0_GET_REG(cmd);
852 ctx->count = CP_PACKET_GET_COUNT(cmd);
853 r = amdgpu_uvd_cs_reg(ctx, cb);
854 if (r)
855 return r;
856 break;
857 case PACKET_TYPE2:
858 ++ctx->idx;
859 break;
860 default:
861 DRM_ERROR("Unknown packet type %d !\n", type);
862 return -EINVAL;
863 }
864 }
865 return 0;
866}
867
868/**
869 * amdgpu_uvd_ring_parse_cs - UVD command submission parser
870 *
871 * @parser: Command submission parser context
872 *
873 * Parse the command stream, patch in addresses as necessary.
874 */
875int amdgpu_uvd_ring_parse_cs(struct amdgpu_cs_parser *parser, uint32_t ib_idx)
876{
877 struct amdgpu_uvd_cs_ctx ctx = {};
878 unsigned buf_sizes[] = {
879 [0x00000000] = 2048,
Boyuan Zhang8c8bac52015-08-05 14:03:48 -0400880 [0x00000001] = 0xFFFFFFFF,
881 [0x00000002] = 0xFFFFFFFF,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400882 [0x00000003] = 2048,
Boyuan Zhang8c8bac52015-08-05 14:03:48 -0400883 [0x00000004] = 0xFFFFFFFF,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400884 };
Christian König50838c82016-02-03 13:44:52 +0100885 struct amdgpu_ib *ib = &parser->job->ibs[ib_idx];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400886 int r;
887
888 if (ib->length_dw % 16) {
889 DRM_ERROR("UVD IB length (%d) not 16 dwords aligned!\n",
890 ib->length_dw);
891 return -EINVAL;
892 }
893
894 ctx.parser = parser;
895 ctx.buf_sizes = buf_sizes;
896 ctx.ib_idx = ib_idx;
897
898 /* first round, make sure the buffers are actually in the UVD segment */
899 r = amdgpu_uvd_cs_packets(&ctx, amdgpu_uvd_cs_pass1);
900 if (r)
901 return r;
902
903 /* second round, patch buffer addresses into the command stream */
904 r = amdgpu_uvd_cs_packets(&ctx, amdgpu_uvd_cs_pass2);
905 if (r)
906 return r;
907
908 if (!ctx.has_msg_cmd) {
909 DRM_ERROR("UVD-IBs need a msg command!\n");
910 return -EINVAL;
911 }
912
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400913 return 0;
914}
915
Christian Königd7af97d2016-02-03 16:01:06 +0100916static int amdgpu_uvd_send_msg(struct amdgpu_ring *ring, struct amdgpu_bo *bo,
917 bool direct, struct fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400918{
919 struct ttm_validate_buffer tv;
920 struct ww_acquire_ctx ticket;
921 struct list_head head;
Christian Königd71518b2016-02-01 12:20:25 +0100922 struct amdgpu_job *job;
923 struct amdgpu_ib *ib;
Chunming Zhou17635522015-08-03 11:43:19 +0800924 struct fence *f = NULL;
Chunming Zhou7b5ec432015-07-03 14:08:18 +0800925 struct amdgpu_device *adev = ring->adev;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400926 uint64_t addr;
927 int i, r;
928
929 memset(&tv, 0, sizeof(tv));
930 tv.bo = &bo->tbo;
931
932 INIT_LIST_HEAD(&head);
933 list_add(&tv.head, &head);
934
935 r = ttm_eu_reserve_buffers(&ticket, &head, true, NULL);
936 if (r)
937 return r;
938
939 if (!bo->adev->uvd.address_64_bit) {
940 amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_VRAM);
941 amdgpu_uvd_force_into_uvd_segment(bo);
942 }
943
944 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
945 if (r)
946 goto err;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400947
Christian Königd71518b2016-02-01 12:20:25 +0100948 r = amdgpu_job_alloc_with_ib(adev, 64, &job);
949 if (r)
950 goto err;
951
952 ib = &job->ibs[0];
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400953 addr = amdgpu_bo_gpu_offset(bo);
Chunming Zhou7b5ec432015-07-03 14:08:18 +0800954 ib->ptr[0] = PACKET0(mmUVD_GPCOM_VCPU_DATA0, 0);
955 ib->ptr[1] = addr;
956 ib->ptr[2] = PACKET0(mmUVD_GPCOM_VCPU_DATA1, 0);
957 ib->ptr[3] = addr >> 32;
958 ib->ptr[4] = PACKET0(mmUVD_GPCOM_VCPU_CMD, 0);
959 ib->ptr[5] = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400960 for (i = 6; i < 16; ++i)
Chunming Zhou7b5ec432015-07-03 14:08:18 +0800961 ib->ptr[i] = PACKET2(0);
962 ib->length_dw = 16;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400963
Christian Königd7af97d2016-02-03 16:01:06 +0100964 if (direct) {
Monk Liuc5637832016-04-19 20:11:32 +0800965 r = amdgpu_ib_schedule(ring, 1, ib, NULL, NULL, &f);
Christian König22a77cf2016-07-05 14:48:17 +0200966 job->fence = fence_get(f);
Christian Königd7af97d2016-02-03 16:01:06 +0100967 if (r)
968 goto err_free;
969
970 amdgpu_job_free(job);
971 } else {
Christian Königead833e2016-02-10 14:35:19 +0100972 r = amdgpu_job_submit(job, ring, &adev->uvd.entity,
Christian Königd7af97d2016-02-03 16:01:06 +0100973 AMDGPU_FENCE_OWNER_UNDEFINED, &f);
974 if (r)
975 goto err_free;
976 }
Chunming Zhou7b5ec432015-07-03 14:08:18 +0800977
Chunming Zhou17635522015-08-03 11:43:19 +0800978 ttm_eu_fence_buffer_objects(&ticket, &head, f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400979
980 if (fence)
Chunming Zhou17635522015-08-03 11:43:19 +0800981 *fence = fence_get(f);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400982 amdgpu_bo_unref(&bo);
Chunming Zhou281b4222015-08-12 12:58:31 +0800983 fence_put(f);
Chunming Zhou7b5ec432015-07-03 14:08:18 +0800984
Chunming Zhou7b5ec432015-07-03 14:08:18 +0800985 return 0;
Christian Königd71518b2016-02-01 12:20:25 +0100986
987err_free:
988 amdgpu_job_free(job);
989
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400990err:
991 ttm_eu_backoff_reservation(&ticket, &head);
992 return r;
993}
994
995/* multiple fence commands without any stream commands in between can
996 crash the vcpu so just try to emmit a dummy create/destroy msg to
997 avoid this */
998int amdgpu_uvd_get_create_msg(struct amdgpu_ring *ring, uint32_t handle,
Chunming Zhou0e3f1542015-08-03 13:11:04 +0800999 struct fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001000{
1001 struct amdgpu_device *adev = ring->adev;
1002 struct amdgpu_bo *bo;
1003 uint32_t *msg;
1004 int r, i;
1005
1006 r = amdgpu_bo_create(adev, 1024, PAGE_SIZE, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001007 AMDGPU_GEM_DOMAIN_VRAM,
1008 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
Christian König72d76682015-09-03 17:34:59 +02001009 NULL, NULL, &bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001010 if (r)
1011 return r;
1012
1013 r = amdgpu_bo_reserve(bo, false);
1014 if (r) {
1015 amdgpu_bo_unref(&bo);
1016 return r;
1017 }
1018
1019 r = amdgpu_bo_kmap(bo, (void **)&msg);
1020 if (r) {
1021 amdgpu_bo_unreserve(bo);
1022 amdgpu_bo_unref(&bo);
1023 return r;
1024 }
1025
1026 /* stitch together an UVD create msg */
1027 msg[0] = cpu_to_le32(0x00000de4);
1028 msg[1] = cpu_to_le32(0x00000000);
1029 msg[2] = cpu_to_le32(handle);
1030 msg[3] = cpu_to_le32(0x00000000);
1031 msg[4] = cpu_to_le32(0x00000000);
1032 msg[5] = cpu_to_le32(0x00000000);
1033 msg[6] = cpu_to_le32(0x00000000);
1034 msg[7] = cpu_to_le32(0x00000780);
1035 msg[8] = cpu_to_le32(0x00000440);
1036 msg[9] = cpu_to_le32(0x00000000);
1037 msg[10] = cpu_to_le32(0x01b37000);
1038 for (i = 11; i < 1024; ++i)
1039 msg[i] = cpu_to_le32(0x0);
1040
1041 amdgpu_bo_kunmap(bo);
1042 amdgpu_bo_unreserve(bo);
1043
Christian Königd7af97d2016-02-03 16:01:06 +01001044 return amdgpu_uvd_send_msg(ring, bo, true, fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001045}
1046
1047int amdgpu_uvd_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
Christian Königd7af97d2016-02-03 16:01:06 +01001048 bool direct, struct fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001049{
1050 struct amdgpu_device *adev = ring->adev;
1051 struct amdgpu_bo *bo;
1052 uint32_t *msg;
1053 int r, i;
1054
1055 r = amdgpu_bo_create(adev, 1024, PAGE_SIZE, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001056 AMDGPU_GEM_DOMAIN_VRAM,
1057 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
Christian König72d76682015-09-03 17:34:59 +02001058 NULL, NULL, &bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001059 if (r)
1060 return r;
1061
1062 r = amdgpu_bo_reserve(bo, false);
1063 if (r) {
1064 amdgpu_bo_unref(&bo);
1065 return r;
1066 }
1067
1068 r = amdgpu_bo_kmap(bo, (void **)&msg);
1069 if (r) {
1070 amdgpu_bo_unreserve(bo);
1071 amdgpu_bo_unref(&bo);
1072 return r;
1073 }
1074
1075 /* stitch together an UVD destroy msg */
1076 msg[0] = cpu_to_le32(0x00000de4);
1077 msg[1] = cpu_to_le32(0x00000002);
1078 msg[2] = cpu_to_le32(handle);
1079 msg[3] = cpu_to_le32(0x00000000);
1080 for (i = 4; i < 1024; ++i)
1081 msg[i] = cpu_to_le32(0x0);
1082
1083 amdgpu_bo_kunmap(bo);
1084 amdgpu_bo_unreserve(bo);
1085
Christian Königd7af97d2016-02-03 16:01:06 +01001086 return amdgpu_uvd_send_msg(ring, bo, direct, fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001087}
1088
1089static void amdgpu_uvd_idle_work_handler(struct work_struct *work)
1090{
1091 struct amdgpu_device *adev =
1092 container_of(work, struct amdgpu_device, uvd.idle_work.work);
1093 unsigned i, fences, handles = 0;
1094
1095 fences = amdgpu_fence_count_emitted(&adev->uvd.ring);
1096
Arindam Nathc0365542016-04-12 13:46:15 +02001097 for (i = 0; i < adev->uvd.max_handles; ++i)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001098 if (atomic_read(&adev->uvd.handles[i]))
1099 ++handles;
1100
1101 if (fences == 0 && handles == 0) {
1102 if (adev->pm.dpm_enabled) {
1103 amdgpu_dpm_enable_uvd(adev, false);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001104 } else {
1105 amdgpu_asic_set_uvd_clocks(adev, 0, 0);
1106 }
1107 } else {
Christian König08086632016-07-01 17:45:49 +02001108 schedule_delayed_work(&adev->uvd.idle_work, UVD_IDLE_TIMEOUT);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001109 }
1110}
1111
Christian Königc4120d52016-07-20 14:11:26 +02001112void amdgpu_uvd_ring_begin_use(struct amdgpu_ring *ring)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001113{
Christian Königc4120d52016-07-20 14:11:26 +02001114 struct amdgpu_device *adev = ring->adev;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001115 bool set_clocks = !cancel_delayed_work_sync(&adev->uvd.idle_work);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001116
1117 if (set_clocks) {
1118 if (adev->pm.dpm_enabled) {
1119 amdgpu_dpm_enable_uvd(adev, true);
1120 } else {
1121 amdgpu_asic_set_uvd_clocks(adev, 53300, 40000);
1122 }
1123 }
1124}
Christian Königc4120d52016-07-20 14:11:26 +02001125
1126void amdgpu_uvd_ring_end_use(struct amdgpu_ring *ring)
1127{
1128 schedule_delayed_work(&ring->adev->uvd.idle_work, UVD_IDLE_TIMEOUT);
1129}